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
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 #include "board.h"
00088
00089 #include "USBD.h"
00090 #include "CDCDSerialDriver.h"
00091
00092 #include <stdbool.h>
00093 #include <stdint.h>
00094
00095
00096
00097
00098
00099
00100 #define DATAPACKETSIZE (128)
00101
00102
00103 #define DATABUFFERSIZE (DATAPACKETSIZE+2)
00104
00105
00106 #define PINS_USART PIN_USART2_TXD, PIN_USART2_RXD
00107
00108 #define BASE_USART USART2
00109
00110 #define ID_USART ID_USART2
00111
00112 #define USART_TIMEOUT 115200
00113
00114 #define TEST_BUFFER_SIZE (2*1024)
00115
00116 #define TEST_COUNT (30)
00117
00118
00119
00120
00121
00122 extern const USBDDriverDescriptors cdcdSerialDriverDescriptors;
00123
00124
00125
00126
00127
00128
00129 static sXdmad dmad;
00130
00131
00132 static uint32_t usartDmaRxChannel;
00133
00134 static uint32_t usartDmaTxChannel;
00135
00136
00137 static LinkedListDescriporView1 dmaRxLinkList;
00138
00139
00140 static const Pin pins[] = {PINS_USART};
00141
00142
00143 COMPILER_ALIGNED(32) static uint8_t usartBuffers[2][DATABUFFERSIZE];
00144
00145
00146 static uint8_t usartCurrentBuffer = 0;
00147
00148
00149 COMPILER_ALIGNED(32) static uint8_t usbBuffer[DATABUFFERSIZE];
00150
00151
00152 static uint8_t isCdcSerialON = 0;
00153
00154
00155 static uint8_t isCdcEchoON = 0;
00156
00157
00158 static volatile uint8_t txDoneFlag = 0;
00159
00160 COMPILER_ALIGNED(32) static uint8_t testBuffer[TEST_BUFFER_SIZE];
00161 static void _UsartDmaRx(void);
00162 void *memset(void *pBuffer, int value, size_t num);
00163
00164
00165
00166
00167
00168
00169
00170 void XDMAC_Handler(void)
00171 {
00172 XDMAD_Handler(&dmad);
00173 }
00174
00175
00176
00177
00178 void USART2_Handler(void)
00179 {
00180 Usart *pUs = BASE_USART;
00181 uint32_t status;
00182 uint16_t serialState;
00183 uint32_t count;
00184
00185 status = USART_GetStatus(pUs);
00186 status &= USART_GetItMask(pUs);
00187
00188
00189 if (!isCdcSerialON) {
00190 USART_DisableIt(pUs, 0xFFFFFFFF);
00191 return;
00192 }
00193
00194 if (status & US_CSR_TIMEOUT) {
00195
00196 USART_AcknowledgeRxTimeOut(BASE_USART, 0);
00197
00198 XDMAC_SoftwareFlushReq(dmad.pXdmacs, usartDmaRxChannel);
00199
00200 count = dmad.pXdmacs->XDMAC_CHID[usartDmaRxChannel].XDMAC_CUBC;
00201 SCB_InvalidateDCache_by_Addr((uint32_t *)usartBuffers, DATAPACKETSIZE - count);
00202
00203 while (CDCDSerialDriver_Write(usartBuffers, DATAPACKETSIZE - count, 0, 0)
00204 != USBD_STATUS_SUCCESS);
00205
00206
00207 XDMAD_StopTransfer(&dmad, usartDmaRxChannel);
00208 _UsartDmaRx();
00209 } else {
00210
00211 serialState = CDCDSerialDriver_GetSerialState();
00212
00213
00214 if ((status & US_CSR_OVRE) != 0) {
00215 TRACE_WARNING("USART_IrqHandler: Overrun\n\r");
00216 serialState |= CDCSerialState_OVERRUN;
00217 }
00218
00219
00220 if ((status & US_CSR_FRAME) != 0) {
00221 TRACE_WARNING("USART_IrqHandler: Framing error\n\r");
00222 serialState |= CDCSerialState_FRAMING;
00223 }
00224
00225 CDCDSerialDriver_SetSerialState(serialState);
00226 }
00227 }
00228
00229
00230
00231
00232
00233
00234
00235
00236 void USBDDriverCallbacks_ConfigurationChanged(unsigned char cfgnum)
00237 {
00238 CDCDSerialDriver_ConfigurationChangedHandler(cfgnum);
00239 }
00240
00241
00242
00243
00244
00245
00246 void USBDCallbacks_RequestReceived(const USBGenericRequest *request)
00247 {
00248 CDCDSerialDriver_RequestHandler(request);
00249 }
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259 static void _UsartDmaTx(uint32_t dwDestAddr,
00260 void *pBuffer, uint16_t wSize)
00261 {
00262 sXdmad *pDmad = &dmad;
00263
00264 sXdmadCfg xdmadCfg;
00265 xdmadCfg.mbr_ubc = wSize;
00266 xdmadCfg.mbr_sa = (uint32_t) pBuffer;
00267 xdmadCfg.mbr_da = (uint32_t) dwDestAddr;
00268 xdmadCfg.mbr_cfg = XDMAC_CC_TYPE_PER_TRAN
00269 | XDMAC_CC_MEMSET_NORMAL_MODE
00270 | XDMAC_CC_DSYNC_MEM2PER
00271 | XDMAC_CC_CSIZE_CHK_1
00272 | XDMAC_CC_DWIDTH_BYTE
00273 | XDMAC_CC_SIF_AHB_IF1
00274 | XDMAC_CC_DIF_AHB_IF1
00275 | XDMAC_CC_SAM_INCREMENTED_AM
00276 | XDMAC_CC_DAM_FIXED_AM
00277 | XDMAC_CC_PERID(XDMAIF_Get_ChannelNumber(ID_USART, XDMAD_TRANSFER_TX));
00278 xdmadCfg.mbr_bc = 0;
00279 XDMAD_ConfigureTransfer(pDmad, usartDmaTxChannel, &xdmadCfg, 0, 0, (
00280 XDMAC_CIE_BIE |
00281 XDMAC_CIE_DIE |
00282 XDMAC_CIE_FIE |
00283 XDMAC_CIE_RBIE |
00284 XDMAC_CIE_WBIE |
00285 XDMAC_CIE_ROIE));
00286
00287 SCB_CleanDCache_by_Addr((uint32_t *)pBuffer, wSize);
00288
00289 XDMAD_StartTransfer(pDmad, usartDmaTxChannel);
00290 }
00291
00292
00293
00294
00295
00296 static void _UsartDmaRxSetup(void)
00297 {
00298 Usart *pUs = BASE_USART;
00299 dmaRxLinkList.mbr_ubc = XDMA_UBC_NVIEW_NDV1
00300 | XDMA_UBC_NDE_FETCH_EN
00301 | XDMA_UBC_NSEN_UPDATED
00302 | XDMAC_CUBC_UBLEN(DATAPACKETSIZE);
00303 dmaRxLinkList.mbr_sa = (uint32_t)&pUs->US_RHR;
00304 dmaRxLinkList.mbr_da = (uint32_t)usartBuffers[0];
00305
00306 }
00307
00308
00309
00310
00311
00312 static void _UsartDmaRx()
00313 {
00314 sXdmad *pDmad = &dmad;
00315 sXdmadCfg xdmadUsartRxCfg;
00316 uint32_t xdmaUsartRxCndc, xdmaInt;
00317 xdmadUsartRxCfg.mbr_cfg = XDMAC_CC_TYPE_PER_TRAN
00318 | XDMAC_CC_MBSIZE_SINGLE
00319 | XDMAC_CC_DSYNC_PER2MEM
00320 | XDMAC_CC_CSIZE_CHK_1
00321 | XDMAC_CC_DWIDTH_BYTE
00322 | XDMAC_CC_SIF_AHB_IF1
00323 | XDMAC_CC_DIF_AHB_IF1
00324 | XDMAC_CC_SAM_FIXED_AM
00325 | XDMAC_CC_DAM_INCREMENTED_AM
00326 | XDMAC_CC_PERID(XDMAIF_Get_ChannelNumber(ID_USART, XDMAD_TRANSFER_RX));
00327 xdmadUsartRxCfg.mbr_sa = dmaRxLinkList.mbr_sa;
00328 xdmadUsartRxCfg.mbr_da = dmaRxLinkList.mbr_da;
00329 xdmadUsartRxCfg.mbr_ubc = DATAPACKETSIZE;
00330 xdmadUsartRxCfg.mbr_bc = 0;
00331 xdmaUsartRxCndc = 0;
00332
00333
00334 xdmaInt = XDMAC_CIE_BIE;
00335
00336 XDMAD_ConfigureTransfer(pDmad, usartDmaRxChannel, &xdmadUsartRxCfg,
00337 xdmaUsartRxCndc, (uint32_t)&dmaRxLinkList, xdmaInt);
00338 XDMAD_StartTransfer(pDmad, usartDmaRxChannel);
00339 }
00340
00341
00342
00343
00344 static void _DebugHelp(void)
00345 {
00346 printf("\n\r==========================================================\n\r");
00347 printf("-- ESC to Enable/Disable ECHO on cdc serial --\n\r");
00348 printf("-- Press 't' to test transfer --\n\r");
00349 printf("\n\r==========================================================\n\r");
00350 }
00351
00352
00353
00354
00355 static void _UsbDataSent(void)
00356 {
00357 txDoneFlag = 1;
00358 }
00359
00360
00361
00362 static void _UsbDataReceived(uint32_t unused,
00363 uint8_t status,
00364 uint32_t received,
00365 uint32_t remaining)
00366 {
00367 unused = unused;
00368 Usart *pUs = BASE_USART;
00369
00370
00371 if (status == USBD_STATUS_SUCCESS) {
00372
00373 SCB_InvalidateDCache_by_Addr((uint32_t *)usbBuffer, received);
00374
00375
00376 if (isCdcEchoON) {
00377 while (CDCDSerialDriver_Write(usbBuffer, received, 0, 0)
00378 != USBD_STATUS_SUCCESS);
00379 }
00380
00381
00382 if (isCdcSerialON)
00383 _UsartDmaTx((uint32_t)&pUs->US_THR, usbBuffer, received);
00384
00385
00386 if ((received == DATAPACKETSIZE) && (remaining > 0)) {
00387 TRACE_WARNING(
00388 "_UsbDataReceived: %u bytes discarded\n\r",
00389 (unsigned int)remaining);
00390 }
00391 } else {
00392 TRACE_WARNING("_UsbDataReceived: Transfer error\n\r");
00393 }
00394 }
00395
00396
00397
00398
00399
00400 static void _UsDmaRxCallback(uint32_t channel, void *pArg)
00401 {
00402 pArg = pArg;
00403
00404 if (channel != usartDmaRxChannel)
00405 return;
00406
00407
00408 USART_AcknowledgeRxTimeOut(BASE_USART, 0);
00409
00410 SCB_InvalidateDCache_by_Addr((uint32_t *)(usartBuffers[usartCurrentBuffer]),
00411 DATABUFFERSIZE);
00412
00413
00414 while (CDCDSerialDriver_Write(usartBuffers[usartCurrentBuffer],
00415 DATAPACKETSIZE, 0, 0) != USBD_STATUS_SUCCESS);
00416
00417
00418 _UsartDmaRx();
00419
00420 }
00421
00422
00423
00424
00425 static void _UsDmaTxCallback(uint32_t channel, void *pArg)
00426 {
00427 pArg = pArg;
00428
00429 if (channel != usartDmaTxChannel)
00430 return;
00431
00432
00433 CDCDSerialDriver_Read(usbBuffer,
00434 DATAPACKETSIZE,
00435 (TransferCallback) _UsbDataReceived,
00436 0);
00437 }
00438
00439
00440
00441
00442 static void _ConfigureDma(void)
00443 {
00444 sXdmad *pDmad = &dmad;
00445
00446 XDMAD_Initialize(&dmad, 0);
00447
00448 NVIC_EnableIRQ(XDMAC_IRQn);
00449
00450
00451 usartDmaTxChannel = XDMAD_AllocateChannel(pDmad, XDMAD_TRANSFER_MEMORY,
00452 ID_USART);
00453 usartDmaRxChannel = XDMAD_AllocateChannel(pDmad, ID_USART,
00454 XDMAD_TRANSFER_MEMORY);
00455
00456
00457 XDMAD_SetCallback(pDmad, usartDmaRxChannel,
00458 (XdmadTransferCallback)_UsDmaRxCallback,
00459 0);
00460
00461 XDMAD_SetCallback(pDmad, usartDmaTxChannel,
00462 (XdmadTransferCallback)_UsDmaTxCallback,
00463 0);
00464 XDMAD_PrepareChannel(pDmad, usartDmaRxChannel);
00465 XDMAD_PrepareChannel(pDmad, usartDmaTxChannel);
00466 }
00467
00468
00469
00470
00471 static void _ConfigureUsart(void)
00472 {
00473 PIO_Configure(pins, PIO_LISTSIZE(pins));
00474 PMC_EnablePeripheral(ID_USART);
00475 USART_DisableIt(BASE_USART, 0xFFFFFFFF);
00476 USART_Configure(BASE_USART,
00477 USART_MODE_ASYNCHRONOUS,
00478 115200,
00479 BOARD_MCK);
00480
00481 USART_SetTransmitterEnabled(BASE_USART, 1);
00482 USART_SetReceiverEnabled(BASE_USART, 1);
00483 NVIC_EnableIRQ(USART2_IRQn);
00484 }
00485
00486
00487
00488
00489 static void _SendText(void)
00490 {
00491 uint32_t i, testCnt;
00492
00493 if (!isCdcSerialON) {
00494 printf("\n\r!! Host serial program not ready!\n\r");
00495 return;
00496 }
00497
00498 printf("\n\r- USB CDC Serial writing ...\n\r");
00499
00500
00501 for (i = 0; i < TEST_BUFFER_SIZE; i ++) testBuffer[i] = (i % 10) + '0';
00502
00503 printf("- Send 0,1,2 ... to host:\n\r");
00504
00505 for (testCnt = 0; testCnt < TEST_COUNT; testCnt ++) {
00506 txDoneFlag = 0;
00507 CDCDSerialDriver_Write(testBuffer,
00508 TEST_BUFFER_SIZE,
00509 (TransferCallback) _UsbDataSent, 0);
00510
00511 while (!txDoneFlag);
00512 }
00513 }
00514
00515
00516
00517
00518 static void _ConfigureUotghs(void)
00519 {
00520
00521
00522 PMC->PMC_SCDR = PMC_SCDR_USBCLK;
00523
00524 PMC->PMC_USB = PMC_USB_USBS;
00525
00526 PMC_EnablePeripheral(ID_USBHS);
00527 USBHS->USBHS_CTRL = USBHS_CTRL_UIMOD_DEVICE;
00528
00529 PMC->CKGR_UCKR = CKGR_UCKR_UPLLEN | CKGR_UCKR_UPLLCOUNT(0xF);
00530
00531
00532 while (!(PMC->PMC_SR & PMC_SR_LOCKU));
00533
00534
00535 NVIC_EnableIRQ(USBHS_IRQn);
00536 }
00537
00538
00539
00540
00541 int main(void)
00542 {
00543 uint8_t isUsbConnected = 0;
00544
00545 WDT_Disable(WDT);
00546
00547
00548 SCB_EnableICache();
00549 SCB_EnableDCache();
00550
00551
00552 printf("-- USB Device CDC Serial Project %s --\n\r", SOFTPACK_VERSION);
00553 printf("-- %s\n\r", BOARD_NAME);
00554 printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ ,
00555 COMPILER_NAME);
00556
00557
00558 PIO_InitializeInterrupts(0);
00559
00560
00561 NVIC_SetPriority(USBHS_IRQn, 2);
00562
00563
00564 _ConfigureDma();
00565
00566
00567 _ConfigureUsart();
00568 _UsartDmaRxSetup();
00569
00570
00571 _ConfigureUotghs();
00572
00573
00574 CDCDSerialDriver_Initialize(&cdcdSerialDriverDescriptors);
00575
00576
00577 _DebugHelp();
00578
00579
00580 USBD_Connect();
00581
00582
00583 while (1) {
00584
00585 if (USBD_GetState() < USBD_STATE_CONFIGURED) {
00586 if (isUsbConnected) {
00587 isUsbConnected = 0;
00588 isCdcSerialON = 0;
00589 }
00590 } else if (isUsbConnected == 0)
00591 isUsbConnected = 1;
00592
00593
00594 if (CDCDSerialDriver_GetControlLineState() & CDCControlLineState_DTR) {
00595 if (!isCdcSerialON) {
00596 isCdcSerialON = 1;
00597
00598
00599 _UsartDmaRx();
00600 USART_EnableIt(BASE_USART, US_CSR_FRAME | US_CSR_OVRE | US_IER_TIMEOUT);
00601 USART_EnableRecvTimeOut(BASE_USART, USART_TIMEOUT);
00602
00603 CDCDSerialDriver_Read(usbBuffer,
00604 DATAPACKETSIZE,
00605 (TransferCallback) _UsbDataReceived,
00606 0);
00607 }
00608 } else if (isCdcSerialON)
00609 isCdcSerialON = 0;
00610
00611 if (DBG_IsRxReady()) {
00612 uint8_t key = DBG_GetChar();
00613
00614
00615 if (key == 27) {
00616 printf("** CDC Echo %s\n\r",
00617 isCdcEchoON ? "OFF" : "ON");
00618 isCdcEchoON = !isCdcEchoON;
00619 }
00620
00621 else if (key == 't')
00622 _SendText();
00623 else {
00624 printf("Alive\n\r");
00625
00626 while (CDCDSerialDriver_Write((char *)"Alive\n\r", 8, 0, 0)
00627 != USBD_STATUS_SUCCESS);
00628
00629 _DebugHelp();
00630 }
00631 }
00632 }
00633 }