96 #define CDC_BULK_EP_SIZE (USB_FS_BULK_EP_MAXSIZE)
97 #define CDC_USB_RX_BUF_SIZ CDC_BULK_EP_SIZE
98 #define CDC_USB_TX_BUF_SIZ 127
102 #define CDC_RX_TIMEOUT SL_MAX(10U, 50000 / (cdcLineCoding.dwDTERate))
120 static
int UsbDataReceived(USB_Status_TypeDef status, uint32_t xferred,
122 static
void DmaSetup(
void);
123 static
int LineCodingReceived(USB_Status_TypeDef status,
126 static
void SerialPortInit(
void);
127 static
void UartRxTimeout(
void);
143 STATIC_UBUF(usbRxBuffer0, CDC_USB_RX_BUF_SIZ);
144 STATIC_UBUF(usbRxBuffer1, CDC_USB_RX_BUF_SIZ);
145 STATIC_UBUF(uartRxBuffer0, CDC_USB_TX_BUF_SIZ);
146 STATIC_UBUF(uartRxBuffer1, CDC_USB_TX_BUF_SIZ);
148 static const uint8_t *usbRxBuffer[ 2 ] = { usbRxBuffer0, usbRxBuffer1 };
149 static const uint8_t *uartRxBuffer[ 2 ] = { uartRxBuffer0, uartRxBuffer1 };
151 static int usbRxIndex, usbBytesReceived;
152 static int uartRxIndex, uartRxCount;
153 static int LastUsbTxCnt;
155 static bool dmaRxCompleted;
156 static bool usbRxActive, dmaTxActive;
157 static bool usbTxActive, dmaRxActive;
185 int retVal = USB_STATUS_REQ_UNHANDLED;
187 if ( ( setup->Type == USB_SETUP_TYPE_CLASS ) &&
188 ( setup->Recipient == USB_SETUP_RECIPIENT_INTERFACE ) )
190 switch (setup->bRequest)
192 case USB_CDC_GETLINECODING:
194 if ( ( setup->wValue == 0 ) &&
195 ( setup->wIndex == CDC_CTRL_INTERFACE_NO ) &&
196 ( setup->wLength == 7 ) &&
197 ( setup->Direction == USB_SETUP_DIR_IN ) )
200 USBD_Write(0, (
void*) &cdcLineCoding, 7, NULL);
201 retVal = USB_STATUS_OK;
205 case USB_CDC_SETLINECODING:
207 if ( ( setup->wValue == 0 ) &&
208 ( setup->wIndex == CDC_CTRL_INTERFACE_NO ) &&
209 ( setup->wLength == 7 ) &&
210 ( setup->Direction != USB_SETUP_DIR_IN ) )
213 USBD_Read(0, (
void*) &cdcLineCoding, 7, LineCodingReceived);
214 retVal = USB_STATUS_OK;
218 case USB_CDC_SETCTRLLINESTATE:
220 if ( ( setup->wIndex == CDC_CTRL_INTERFACE_NO ) &&
221 ( setup->wLength == 0 ) )
224 retVal = USB_STATUS_OK;
242 USBD_State_TypeDef newState)
244 if (newState == USBD_STATE_CONFIGURED)
248 if (oldState == USBD_STATE_SUSPENDED)
256 USBD_Read(CDC_EP_DATA_OUT, (
void*) usbRxBuffer[ usbRxIndex ],
257 CDC_USB_RX_BUF_SIZ, UsbDataReceived);
265 dmaRxCompleted =
true;
267 (
void *) uartRxBuffer[ uartRxIndex ],
268 (
void *) &(CDC_UART->RXDATA),
269 CDC_USB_TX_BUF_SIZ - 1);
270 USBTIMER_Start(CDC_TIMER_ID, CDC_RX_TIMEOUT, UartRxTimeout);
273 else if ((oldState == USBD_STATE_CONFIGURED) &&
274 (newState != USBD_STATE_SUSPENDED))
277 USBTIMER_Stop(CDC_TIMER_ID);
279 DMA->CHENC = ( 1 << CDC_UART_TX_DMA_CHANNEL ) |
280 ( 1 << CDC_UART_RX_DMA_CHANNEL );
283 else if (newState == USBD_STATE_SUSPENDED)
287 USBTIMER_Stop(CDC_TIMER_ID);
289 DMA->CHENC = ( 1 << CDC_UART_TX_DMA_CHANNEL ) |
290 ( 1 << CDC_UART_RX_DMA_CHANNEL );
306 static int UsbDataReceived(USB_Status_TypeDef status,
312 if ((status == USB_STATUS_OK) && (xferred > 0))
321 (
void *) &(CDC_UART->TXDATA),
322 (
void *) usbRxBuffer[ usbRxIndex ^ 1 ],
326 USBD_Read(CDC_EP_DATA_OUT, (
void*) usbRxBuffer[ usbRxIndex ],
327 CDC_USB_RX_BUF_SIZ, UsbDataReceived);
333 usbBytesReceived = xferred;
336 return USB_STATUS_OK;
346 static void DmaTxComplete(
unsigned int channel,
bool primary,
void *user)
363 (
void *) &(CDC_UART->TXDATA),
364 (
void *) usbRxBuffer[ usbRxIndex ^ 1 ],
365 usbBytesReceived - 1);
369 USBD_Read(CDC_EP_DATA_OUT, (
void*) usbRxBuffer[ usbRxIndex ],
370 CDC_USB_RX_BUF_SIZ, UsbDataReceived);
391 static int UsbDataTransmitted(USB_Status_TypeDef status,
398 if (status == USB_STATUS_OK)
404 USBD_Write(CDC_EP_DATA_IN, (
void*) uartRxBuffer[ uartRxIndex ^ 1],
405 uartRxCount, UsbDataTransmitted);
406 LastUsbTxCnt = uartRxCount;
409 dmaRxCompleted =
true;
411 (
void *) uartRxBuffer[ uartRxIndex ],
412 (
void *) &(CDC_UART->RXDATA),
413 CDC_USB_TX_BUF_SIZ - 1);
415 USBTIMER_Start(CDC_TIMER_ID, CDC_RX_TIMEOUT, UartRxTimeout);
423 return USB_STATUS_OK;
433 static void DmaRxComplete(
unsigned int channel,
bool primary,
void *user)
450 uartRxCount = CDC_USB_TX_BUF_SIZ;
454 uartRxCount = CDC_USB_TX_BUF_SIZ - 1 -
463 USBD_Write(CDC_EP_DATA_IN, (
void*) uartRxBuffer[ uartRxIndex ^ 1],
464 uartRxCount, UsbDataTransmitted);
465 LastUsbTxCnt = uartRxCount;
468 dmaRxCompleted =
true;
470 (
void *) uartRxBuffer[ uartRxIndex ],
471 (
void *) &(CDC_UART->RXDATA),
472 CDC_USB_TX_BUF_SIZ - 1);
474 USBTIMER_Start(CDC_TIMER_ID, CDC_RX_TIMEOUT, UartRxTimeout);
480 USBTIMER_Stop(CDC_TIMER_ID);
493 static void UartRxTimeout(
void)
498 dmaCtrl = dmaControlBlock[ 1 ].
CTRL;
506 cnt = CDC_USB_TX_BUF_SIZ - 1 -
509 if ((cnt == 0) && (LastUsbTxCnt == CDC_BULK_EP_SIZE))
516 DMA->CHENC = 1 << CDC_UART_RX_DMA_CHANNEL;
517 dmaRxCompleted =
false;
519 DmaRxComplete(CDC_UART_RX_DMA_CHANNEL,
true, NULL);
523 if ((cnt > 0) && (cnt == uartRxCount))
530 DMA->CHENC = 1 << CDC_UART_RX_DMA_CHANNEL;
531 dmaRxCompleted =
false;
533 DmaRxComplete(CDC_UART_RX_DMA_CHANNEL,
true, NULL);
539 USBTIMER_Start(CDC_TIMER_ID, CDC_RX_TIMEOUT, UartRxTimeout);
554 static int LineCodingReceived(USB_Status_TypeDef status,
562 if ((status == USB_STATUS_OK) && (xferred == 7))
565 if (cdcLineCoding.bDataBits == 5)
568 else if (cdcLineCoding.bDataBits == 6)
571 else if (cdcLineCoding.bDataBits == 7)
574 else if (cdcLineCoding.bDataBits == 8)
577 else if (cdcLineCoding.bDataBits == 16)
581 return USB_STATUS_REQ_ERR;
584 if (cdcLineCoding.bParityType == 0)
587 else if (cdcLineCoding.bParityType == 1)
590 else if (cdcLineCoding.bParityType == 2)
593 else if (cdcLineCoding.bParityType == 3)
594 return USB_STATUS_REQ_ERR;
596 else if (cdcLineCoding.bParityType == 4)
597 return USB_STATUS_REQ_ERR;
600 return USB_STATUS_REQ_ERR;
603 if (cdcLineCoding.bCharFormat == 0)
606 else if (cdcLineCoding.bCharFormat == 1)
609 else if (cdcLineCoding.bCharFormat == 2)
613 return USB_STATUS_REQ_ERR;
616 CDC_UART->FRAME = frame;
619 return USB_STATUS_OK;
621 return USB_STATUS_REQ_ERR;
627 static void DmaSetup(
void)
642 DmaTxCallBack.
cbFunc = DmaTxComplete;
648 chnlCfgTx.
select = CDC_TX_DMA_SIGNAL;
649 chnlCfgTx.
cb = &DmaTxCallBack;
660 descrCfgTx.
hprot = 0;
663 DMA_CfgDescr(CDC_UART_TX_DMA_CHANNEL,
true, &descrCfgTx);
668 DmaRxCallBack.
cbFunc = DmaRxComplete;
674 chnlCfgRx.
select = CDC_RX_DMA_SIGNAL;
675 chnlCfgRx.
cb = &DmaRxCallBack;
686 descrCfgRx.
hprot = 0;
689 DMA_CfgDescr(CDC_UART_RX_DMA_CHANNEL,
true, &descrCfgRx);
695 static void SerialPortInit(
void)
706 CDC_ENABLE_DK_UART_SWITCH();
717 CDC_UART->ROUTE = CDC_UART_ROUTE;
Clock management unit (CMU) API.
Board support package API definitions.
#define CORE_DECLARE_IRQ_STATE
DMA_ArbiterConfig_TypeDef arbRate
#define USART_FRAME_DATABITS_EIGHT
#define USART_FRAME_STOPBITS_ONEANDAHALF
#define SL_PACK_END()
Macro for handling packed structs. Use this macro after the struct definition. With GCC...
DMA_DataInc_TypeDef dstInc
Callback structure that can be used to define DMA complete actions.
void CDC_Init(void)
CDC device initialization.
DMA_CB_TypeDef * cb
User definable callback handling configuration.
CMSIS Cortex-M Peripheral Access Layer for Silicon Laboratories microcontroller devices.
#define SL_ALIGN(X)
Macro for aligning a variable. Use this macro before the variable definition. X denotes the stora...
#define USART_FRAME_DATABITS_SIXTEEN
Universal synchronous/asynchronous receiver/transmitter (USART/UART) peripheral API.
USART_Enable_TypeDef enable
General purpose utilities.
#define SL_PACK_START(x)
Macro for handling packed structs. Use this macro before the struct definition. X denotes the max...
void CDC_StateChangeEvent(USBD_State_TypeDef oldState, USBD_State_TypeDef newState)
Callback function called each time the USB device state is changed. Starts CDC operation when device ...
#define USART_FRAME_DATABITS_SEVEN
void DMA_ActivateBasic(unsigned int channel, bool primary, bool useBurst, void *dst, const void *src, unsigned int nMinus1)
Activate DMA basic cycle (used for memory-peripheral transfers).
#define USART_FRAME_STOPBITS_TWO
#define CORE_ENTER_ATOMIC()
int CDC_SetupCmd(const USB_Setup_TypeDef *setup)
Handle USB setup commands. Implements CDC class specific commands.
void GPIO_PinModeSet(GPIO_Port_TypeDef port, unsigned int pin, GPIO_Mode_TypeDef mode, unsigned int out)
Set the mode for a GPIO pin.
General Purpose IO (GPIO) peripheral API.
void DMA_Init(DMA_Init_TypeDef *init)
Initializes DMA controller.
void DMA_CfgChannel(unsigned int channel, DMA_CfgChannel_TypeDef *cfg)
Configure a DMA channel.
#define DMA_CTRL_CYCLE_CTRL_INVALID
#define SL_ATTRIBUTE_ALIGN(X)
GCC style macro for aligning a variable.
DMA_DataSize_TypeDef size
Core interrupt handling API.
void CMU_ClockEnable(CMU_Clock_TypeDef clock, bool enable)
Enable/disable a clock.
#define USART_FRAME_PARITY_ODD
DMA_DataInc_TypeDef srcInc
#define USART_FRAME_STOPBITS_ONE
#define CORE_EXIT_ATOMIC()
#define _DMA_CTRL_N_MINUS_1_MASK
DMA_DESCRIPTOR_TypeDef * controlBlock
#define USART_INITASYNC_DEFAULT
#define SL_ATTRIBUTE_PACKED
GCC style macro for handling packed structs.
DMA_FuncPtr_TypeDef cbFunc
USB Communication Device Class (CDC) driver.
void USART_Enable(USART_TypeDef *usart, USART_Enable_TypeDef enable)
Enable/disable USART/UART receiver and/or transmitter.
#define USART_FRAME_PARITY_NONE
void USART_InitAsync(USART_TypeDef *usart, const USART_InitAsync_TypeDef *init)
Init USART/UART for normal asynchronous mode.
#define USART_FRAME_PARITY_EVEN
#define USART_FRAME_DATABITS_SIX
Direct memory access (DMA) API.
#define USART_FRAME_DATABITS_FIVE
#define _DMA_CTRL_N_MINUS_1_SHIFT
#define _DMA_CTRL_CYCLE_CTRL_MASK
void DMA_CfgDescr(unsigned int channel, bool primary, DMA_CfgDescr_TypeDef *cfg)
Configure DMA descriptor for auto-request, basic or ping-pong DMA cycles.
void USART_BaudrateAsyncSet(USART_TypeDef *usart, uint32_t refFreq, uint32_t baudrate, USART_OVS_TypeDef ovs)
Configure USART/UART operating in asynchronous mode to use a given baudrate (or as close as possible ...