uart_usb_lib.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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 #include "config.h"
00046 #include "usb_drv.h"
00047 #include "usb_descriptors.h"
00048 #include "serial/uart_usb_lib.h"
00049 #include "cdc_task.h"
00050 #include <stdio.h>
00051
00052
00053
00054
00055
00056
00057
00058
00059 #ifndef USB_CDC_ACM_HOOK_TX_START
00060 #define USB_CDC_ACM_HOOK_TX_START(char)
00061 #endif
00062
00063 #ifndef USB_CDC_ACM_HOOK_TX_END
00064 #define USB_CDC_ACM_HOOK_TX_END(char)
00065 #endif
00066
00067 #ifndef USB_CDC_ACM_HOOK_CLS_CHANGED
00068 #define USB_CDC_ACM_HOOK_CLS_CHANGED(state)
00069 #endif
00070
00071 #ifndef USB_CDC_ACM_HOOK_CONFIGURED
00072 #define USB_CDC_ACM_HOOK_CONFIGURED()
00073 #endif
00074
00075 #ifndef USB_CDC_ACM_CONF_LOCAL_ECHO
00076 #define USB_CDC_ACM_CONF_LOCAL_ECHO 0
00077 #endif
00078
00079
00080
00081 Uchar tx_counter;
00082 Uchar rx_counter;
00083 S_line_coding line_coding;
00084
00085
00086
00087 void uart_usb_configure_endpoints() {
00088 usb_configure_endpoint(
00089 VCP_INT_EP,
00090 TYPE_INTERRUPT,
00091 DIRECTION_IN,
00092 SIZE_32,
00093 ONE_BANK,
00094 NYET_ENABLED
00095 );
00096
00097 usb_configure_endpoint(
00098 VCP_TX_EP,
00099 TYPE_BULK,
00100 DIRECTION_IN,
00101 SIZE_32,
00102 TWO_BANKS,
00103 NYET_ENABLED
00104 );
00105
00106 usb_configure_endpoint(
00107 VCP_RX_EP,
00108 TYPE_BULK,
00109 DIRECTION_OUT,
00110 SIZE_32,
00111 TWO_BANKS,
00112 NYET_ENABLED
00113 );
00114
00115 Usb_reset_endpoint(VCP_INT_EP);
00116 Usb_reset_endpoint(VCP_TX_EP);
00117 Usb_reset_endpoint(VCP_RX_EP);
00118
00119 USB_CDC_ACM_HOOK_CONFIGURED();
00120 }
00121
00122
00123 int usb_stdout_putchar(char c, FILE *stream)
00124 {
00125
00126 uint8_t uenum = UENUM;
00127
00128
00129
00130 Usb_select_endpoint(VCP_TX_EP);
00131
00132 if(usb_endpoint_wait_for_write_enabled()!=0)
00133 return 0;
00134
00135 if(c=='\n')
00136 uart_usb_putchar('\r');
00137
00138 if(c!='\r')
00139 uart_usb_putchar(c);
00140
00141
00142 UENUM = uenum;
00143
00144 return 0;
00145 }
00146
00147 static FILE usb_stdout = FDEV_SETUP_STREAM(usb_stdout_putchar,
00148 NULL,
00149 _FDEV_SETUP_WRITE);
00150
00151
00152
00153
00154 void uart_usb_init(void)
00155 {
00156 tx_counter = 0;
00157 rx_counter = 0;
00158 }
00159
00160 void uart_usb_set_stdout(void)
00161 {
00162 stdout = &usb_stdout;
00163 }
00164
00165
00166 static uint8_t uart_usb_control_line_state = 0;
00167
00168 uint8_t uart_usb_get_control_line_state(void) {
00169 return uart_usb_control_line_state;
00170 }
00171
00172
00173 void uart_usb_set_control_line_state(uint8_t control_line_state)
00174 {
00175 uart_usb_control_line_state = control_line_state;
00176 USB_CDC_ACM_HOOK_CLS_CHANGED(control_line_state);
00177 }
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 bit uart_usb_tx_ready(void)
00188 {
00189 Usb_select_endpoint(VCP_TX_EP);
00190 if (!Is_usb_write_enabled())
00191 {
00192 return FALSE;
00193 }
00194 return TRUE;
00195 }
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 int uart_usb_putchar(int data_to_send)
00207 {
00208
00209 uint8_t uenum = UENUM;
00210
00211 USB_CDC_ACM_HOOK_TX_START(data_to_send);
00212
00213 Usb_select_endpoint(VCP_TX_EP);
00214
00215 if(!uart_usb_tx_ready()) {
00216 data_to_send=-1;
00217 goto bail;
00218 }
00219
00220 Usb_write_byte(data_to_send);
00221 tx_counter++;
00222
00223
00224 if(!Is_usb_write_enabled())
00225 uart_usb_flush();
00226
00227 USB_CDC_ACM_HOOK_TX_END(data_to_send);
00228
00229 bail:
00230
00231 UENUM = uenum;
00232
00233 return data_to_send;
00234 }
00235
00236
00237
00238
00239
00240
00241 bit uart_usb_test_hit(void)
00242 {
00243 if (!rx_counter)
00244 {
00245
00246 uint8_t uenum = UENUM;
00247 Usb_select_endpoint(VCP_RX_EP);
00248 if (Is_usb_receive_out())
00249 {
00250 rx_counter = Usb_byte_counter();
00251 if (!rx_counter)
00252 {
00253 Usb_ack_receive_out();
00254 }
00255 }
00256
00257 UENUM = uenum;
00258 }
00259 return (rx_counter!=0);
00260 }
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270 char uart_usb_getchar(void)
00271 {
00272 register Uchar data_rx;
00273
00274
00275 uint8_t uenum = UENUM;
00276
00277 Usb_select_endpoint(VCP_RX_EP);
00278 if (!rx_counter) while (!uart_usb_test_hit());
00279 data_rx=Usb_read_byte();
00280 rx_counter--;
00281 if (!rx_counter) Usb_ack_receive_out();
00282
00283 #if USB_CDC_ACM_CONF_LOCAL_ECHO
00284
00285 uart_usb_putchar(data_rx);
00286 #endif
00287
00288
00289 UENUM = uenum;
00290
00291 return data_rx;
00292 }
00293
00294
00295
00296
00297
00298
00299 void uart_usb_flush (void)
00300 {
00301
00302 uint8_t uenum = UENUM;
00303
00304 Usb_select_endpoint(VCP_TX_EP);
00305 Usb_send_in();
00306 tx_counter = 0;
00307 usb_endpoint_wait_for_write_enabled();
00308
00309
00310 UENUM = uenum;
00311 }
00312
00313