00001
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #include "config.h"
00048 #include "conf_usb.h"
00049 #include "lib_mcu/usb/usb_drv.h"
00050 #include "usb_descriptors.h"
00051 #include "modules/usb/device_chap9/usb_standard_request.h"
00052 #include "usb_specific_request.h"
00053 #include "lib_mcu/uart/uart_lib.h"
00054 #if ((USB_DEVICE_SN_USE==ENABLE) && (USE_DEVICE_SN_UNIQUE==ENABLE))
00055 #include "lib_mcu/flash/flash_drv.h"
00056 #endif
00057
00058
00059
00060 #ifdef __GNUC__
00061 extern PGM_VOID_P pbuffer;
00062 #else
00063 extern U8 code *pbuffer;
00064 #endif
00065 extern U8 data_to_transfer;
00066 extern S_line_coding line_coding;
00067 extern S_line_status line_status;
00068
00069
00070
00071
00072 extern S_serial_state serial_state;
00073 static S_serial_state serial_state_saved;
00074 volatile U8 usb_request_break_generation=FALSE;
00075
00076
00077
00078
00087 Bool usb_user_read_request(U8 type, U8 request)
00088 {
00089 U16 wValue;
00090
00091 LSB(wValue) = Usb_read_byte();
00092 MSB(wValue) = Usb_read_byte();
00093
00094
00095 if( USB_SETUP_SET_CLASS_INTER == type )
00096 {
00097 switch( request )
00098 {
00099 case SETUP_CDC_SET_LINE_CODING:
00100 cdc_set_line_coding();
00101 return TRUE;
00102 break;
00103
00104 case SETUP_CDC_SET_CONTROL_LINE_STATE:
00105 cdc_set_control_line_state(wValue);
00106 return TRUE;
00107 break;
00108
00109 case SETUP_CDC_SEND_BREAK:
00110 cdc_send_break(wValue);
00111 return TRUE;
00112 break;
00113 }
00114 }
00115 if( USB_SETUP_GET_CLASS_INTER == type )
00116 {
00117 switch( request )
00118 {
00119 case SETUP_CDC_GET_LINE_CODING:
00120 cdc_get_line_coding();
00121 return TRUE;
00122 break;
00123 }
00124 }
00125 return FALSE;
00126 }
00127
00128
00133 void usb_user_endpoint_init(U8 conf_nb)
00134 {
00135 usb_configure_endpoint(INT_EP, \
00136 TYPE_INTERRUPT,\
00137 DIRECTION_IN, \
00138 SIZE_32, \
00139 ONE_BANK, \
00140 NYET_ENABLED);
00141
00142 usb_configure_endpoint(TX_EP, \
00143 TYPE_BULK, \
00144 DIRECTION_IN, \
00145 SIZE_64, \
00146 TWO_BANKS, \
00147 NYET_ENABLED);
00148
00149 usb_configure_endpoint(RX_EP, \
00150 TYPE_BULK, \
00151 DIRECTION_OUT, \
00152 SIZE_64, \
00153 TWO_BANKS, \
00154 NYET_ENABLED);
00155
00156 Usb_reset_endpoint(INT_EP);
00157 Usb_reset_endpoint(TX_EP);
00158 Usb_reset_endpoint(RX_EP);
00159 }
00160
00161
00168 U8 usb_user_interface_get( U16 wInterface )
00169 {
00170 return 0;
00171 }
00172
00173
00179 void usb_user_interface_reset(U16 wInterface, U8 alternate_setting)
00180 {
00181
00182 if( INTERFACE0_NB == wInterface )
00183 {
00184
00185 Usb_select_endpoint(INT_EP);
00186 Usb_disable_stall_handshake();
00187 Usb_reset_endpoint(INT_EP);
00188 Usb_reset_data_toggle();
00189 }
00190 if( INTERFACE1_NB == wInterface )
00191 {
00192
00193 Usb_select_endpoint(TX_EP);
00194 Usb_disable_stall_handshake();
00195 Usb_reset_endpoint(TX_EP);
00196 Usb_reset_data_toggle();
00197 Usb_select_endpoint(RX_EP);
00198 Usb_disable_stall_handshake();
00199 Usb_reset_endpoint(RX_EP);
00200 Usb_reset_data_toggle();
00201 }
00202 }
00203
00204
00212 Bool usb_user_get_descriptor(U8 type, U8 string)
00213 {
00214 return FALSE;
00215 }
00216
00225 void cdc_get_line_coding(void)
00226 {
00227 Usb_ack_receive_setup();
00228 Usb_write_byte(LSB0(line_coding.dwDTERate));
00229 Usb_write_byte(LSB1(line_coding.dwDTERate));
00230 Usb_write_byte(LSB2(line_coding.dwDTERate));
00231 Usb_write_byte(LSB3(line_coding.dwDTERate));
00232 Usb_write_byte(line_coding.bCharFormat);
00233 Usb_write_byte(line_coding.bParityType);
00234 Usb_write_byte(line_coding.bDataBits);
00235
00236 Usb_send_control_in();
00237 while(!(Is_usb_read_control_enabled()));
00238
00239
00240 while(!Is_usb_receive_out());
00241 Usb_ack_receive_out();
00242 }
00243
00244
00253 void cdc_set_line_coding (void)
00254 {
00255 Usb_ack_receive_setup();
00256 while (!(Is_usb_receive_out()));
00257 LSB0(line_coding.dwDTERate) = Usb_read_byte();
00258 LSB1(line_coding.dwDTERate) = Usb_read_byte();
00259 LSB2(line_coding.dwDTERate) = Usb_read_byte();
00260 LSB3(line_coding.dwDTERate) = Usb_read_byte();
00261 line_coding.bCharFormat = Usb_read_byte();
00262 line_coding.bParityType = Usb_read_byte();
00263 line_coding.bDataBits = Usb_read_byte();
00264 Usb_ack_receive_out();
00265
00266 Usb_send_control_in();
00267 while(!(Is_usb_read_control_enabled()));
00268 #ifdef UART_U2
00269 Uart_set_baudrate((line_coding.dwDTERate)/2);
00270 #else
00271 Uart_set_baudrate(line_coding.dwDTERate);
00272 #endif
00273 }
00274
00275
00286 void cdc_set_control_line_state (U16 state)
00287 {
00288 Usb_ack_receive_setup();
00289 Usb_send_control_in();
00290 line_status.all = state;
00291
00292 while(!(Is_usb_read_control_enabled()));
00293
00294 }
00295
00308 Bool cdc_update_serial_state()
00309 {
00310 if( serial_state_saved.all != serial_state.all)
00311 {
00312 serial_state_saved.all = serial_state.all;
00313
00314 Usb_select_endpoint(INT_EP);
00315 if (Is_usb_write_enabled())
00316 {
00317 Usb_write_byte(USB_SETUP_GET_CLASS_INTER);
00318 Usb_write_byte(SETUP_CDC_BN_SERIAL_STATE);
00319
00320 Usb_write_byte(0x00);
00321 Usb_write_byte(0x00);
00322
00323 Usb_write_byte(0x00);
00324 Usb_write_byte(0x00);
00325
00326 Usb_write_byte(0x02);
00327 Usb_write_byte(0x00);
00328
00329 Usb_write_byte(LSB(serial_state.all));
00330 Usb_write_byte(MSB(serial_state.all));
00331 Usb_ack_in_ready();
00332 }
00333 return TRUE;
00334 }
00335 return FALSE;
00336 }
00337
00344 void cdc_send_break(U16 break_duration)
00345 {
00346 Usb_ack_receive_setup();
00347 Usb_send_control_in();
00348 usb_request_break_generation=TRUE;
00349 while(!(Is_usb_read_control_enabled()));
00350 }
00351
00352