95 #define CDC_BULK_EP_SIZE (USB_FS_BULK_EP_MAXSIZE)
96 #define CDC_USB_RX_BUF_SIZ CDC_BULK_EP_SIZE
97 #define CDC_USB_TX_BUF_SIZ 127
101 #define CDC_RX_TIMEOUT EFM32_MAX(10U, 50000 / (cdcLineCoding.dwDTERate))
113 } __attribute__ ((packed)) cdcLineCoding_TypeDef;
121 static
void DmaSetup(
void);
125 static
void SerialPortInit(
void);
126 static
void UartRxTimeout(
void);
136 static cdcLineCoding_TypeDef __attribute__ ((aligned(4))) cdcLineCoding =
147 static const uint8_t *usbRxBuffer[ 2 ] = { usbRxBuffer0, usbRxBuffer1 };
148 static const uint8_t *uartRxBuffer[ 2 ] = { uartRxBuffer0, uartRxBuffer1 };
150 static int usbRxIndex, usbBytesReceived;
151 static int uartRxIndex, uartRxCount;
152 static int LastUsbTxCnt;
154 static bool dmaRxCompleted;
155 static bool usbRxActive, dmaTxActive;
156 static bool usbTxActive, dmaRxActive;
193 if ( ( setup->
wValue == 0 ) &&
194 ( setup->
wIndex == CDC_CTRL_INTERFACE_NO ) &&
199 USBD_Write(0, (
void*) &cdcLineCoding, 7, NULL);
206 if ( ( setup->
wValue == 0 ) &&
207 ( setup->
wIndex == CDC_CTRL_INTERFACE_NO ) &&
212 USBD_Read(0, (
void*) &cdcLineCoding, 7, LineCodingReceived);
219 if ( ( setup->
wIndex == CDC_CTRL_INTERFACE_NO ) &&
255 USBD_Read(CDC_EP_DATA_OUT, (
void*) usbRxBuffer[ usbRxIndex ],
256 CDC_USB_RX_BUF_SIZ, UsbDataReceived);
264 dmaRxCompleted =
true;
266 (
void *) uartRxBuffer[ uartRxIndex ],
267 (
void *) &(CDC_UART->RXDATA),
268 CDC_USB_TX_BUF_SIZ - 1);
278 DMA->CHENC = ( 1 << CDC_UART_TX_DMA_CHANNEL ) |
279 ( 1 << CDC_UART_RX_DMA_CHANNEL );
288 DMA->CHENC = ( 1 << CDC_UART_TX_DMA_CHANNEL ) |
289 ( 1 << CDC_UART_RX_DMA_CHANNEL );
320 (
void *) &(CDC_UART->TXDATA),
321 (
void *) usbRxBuffer[ usbRxIndex ^ 1 ],
325 USBD_Read(CDC_EP_DATA_OUT, (
void*) usbRxBuffer[ usbRxIndex ],
326 CDC_USB_RX_BUF_SIZ, UsbDataReceived);
332 usbBytesReceived = xferred;
345 static void DmaTxComplete(
unsigned int channel,
bool primary,
void *user)
361 (
void *) &(CDC_UART->TXDATA),
362 (
void *) usbRxBuffer[ usbRxIndex ^ 1 ],
363 usbBytesReceived - 1);
367 USBD_Read(CDC_EP_DATA_OUT, (
void*) usbRxBuffer[ usbRxIndex ],
368 CDC_USB_RX_BUF_SIZ, UsbDataReceived);
402 USBD_Write(CDC_EP_DATA_IN, (
void*) uartRxBuffer[ uartRxIndex ^ 1],
403 uartRxCount, UsbDataTransmitted);
404 LastUsbTxCnt = uartRxCount;
407 dmaRxCompleted =
true;
409 (
void *) uartRxBuffer[ uartRxIndex ],
410 (
void *) &(CDC_UART->RXDATA),
411 CDC_USB_TX_BUF_SIZ - 1);
431 static void DmaRxComplete(
unsigned int channel,
bool primary,
void *user)
447 uartRxCount = CDC_USB_TX_BUF_SIZ;
451 uartRxCount = CDC_USB_TX_BUF_SIZ - 1 -
460 USBD_Write(CDC_EP_DATA_IN, (
void*) uartRxBuffer[ uartRxIndex ^ 1],
461 uartRxCount, UsbDataTransmitted);
462 LastUsbTxCnt = uartRxCount;
465 dmaRxCompleted =
true;
467 (
void *) uartRxBuffer[ uartRxIndex ],
468 (
void *) &(CDC_UART->RXDATA),
469 CDC_USB_TX_BUF_SIZ - 1);
490 static void UartRxTimeout(
void)
503 cnt = CDC_USB_TX_BUF_SIZ - 1 -
506 if ((cnt == 0) && (LastUsbTxCnt == CDC_BULK_EP_SIZE))
513 DMA->CHENC = 1 << CDC_UART_RX_DMA_CHANNEL;
514 dmaRxCompleted =
false;
516 DmaRxComplete(CDC_UART_RX_DMA_CHANNEL,
true, NULL);
520 if ((cnt > 0) && (cnt == uartRxCount))
527 DMA->CHENC = 1 << CDC_UART_RX_DMA_CHANNEL;
528 dmaRxCompleted =
false;
530 DmaRxComplete(CDC_UART_RX_DMA_CHANNEL,
true, NULL);
562 if (cdcLineCoding.bDataBits == 5)
565 else if (cdcLineCoding.bDataBits == 6)
568 else if (cdcLineCoding.bDataBits == 7)
571 else if (cdcLineCoding.bDataBits == 8)
574 else if (cdcLineCoding.bDataBits == 16)
581 if (cdcLineCoding.bParityType == 0)
584 else if (cdcLineCoding.bParityType == 1)
587 else if (cdcLineCoding.bParityType == 2)
590 else if (cdcLineCoding.bParityType == 3)
593 else if (cdcLineCoding.bParityType == 4)
600 if (cdcLineCoding.bCharFormat == 0)
603 else if (cdcLineCoding.bCharFormat == 1)
606 else if (cdcLineCoding.bCharFormat == 2)
613 CDC_UART->FRAME = frame;
624 static void DmaSetup(
void)
639 DmaTxCallBack.
cbFunc = DmaTxComplete;
645 chnlCfgTx.
select = CDC_TX_DMA_SIGNAL;
646 chnlCfgTx.
cb = &DmaTxCallBack;
657 descrCfgTx.
hprot = 0;
660 DMA_CfgDescr(CDC_UART_TX_DMA_CHANNEL,
true, &descrCfgTx);
665 DmaRxCallBack.
cbFunc = DmaRxComplete;
671 chnlCfgRx.
select = CDC_RX_DMA_SIGNAL;
672 chnlCfgRx.
cb = &DmaRxCallBack;
683 descrCfgRx.
hprot = 0;
686 DMA_CfgDescr(CDC_UART_RX_DMA_CHANNEL,
true, &descrCfgRx);
692 static void SerialPortInit(
void)
703 CDC_ENABLE_DK_UART_SWITCH();
714 CDC_UART->ROUTE = CDC_UART_ROUTE;
Clock management unit (CMU) API.
#define USB_CDC_SETCTRLLINESTATE
#define _DMA_CTRL_N_MINUS_1_MASK
Board support package API definitions.
#define USART_FRAME_DATABITS_SEVEN
#define _DMA_CTRL_N_MINUS_1_SHIFT
#define USB_CDC_SETLINECODING
DMA_ArbiterConfig_TypeDef arbRate
void USBTIMER_Stop(uint32_t id)
Stop a timer.
DMA_DataInc_TypeDef dstInc
Callback structure that can be used to define DMA complete actions.
void CDC_Init(void)
CDC device initialization.
#define USART_FRAME_STOPBITS_TWO
__STATIC_INLINE uint32_t INT_Enable(void)
Enable interrupts.
#define EFM32_PACK_START(x)
DMA_CB_TypeDef * cb
User definable callback handling configuration.
#define DMA_CTRL_CYCLE_CTRL_INVALID
#define USB_CDC_GETLINECODING
CMSIS Cortex-M Peripheral Access Layer for Silicon Laboratories microcontroller devices.
#define USART_FRAME_PARITY_EVEN
Universal synchronous/asynchronous receiver/transmitter (USART/UART) peripheral API.
USART_Enable_TypeDef enable
Emlib general purpose utilities.
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 ...
USB Setup request package.
USBD_State_TypeDef
USB device state enumerator.
USB_Status_TypeDef
USB transfer status enumerator.
#define _DMA_CTRL_CYCLE_CTRL_MASK
int CDC_SetupCmd(const USB_Setup_TypeDef *setup)
Handle USB setup commands. Implements CDC class specific commands.
void DMA_ActivateBasic(unsigned int channel, bool primary, bool useBurst, void *dst, void *src, unsigned int nMinus1)
Activate DMA basic cycle (used for memory-peripheral transfers).
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.
DMA_DESCRIPTOR_TypeDef dmaControlBlock[DMACTRL_CH_CNT *2]
#define USART_FRAME_STOPBITS_ONE
USB protocol stack library API for EFM32/EZR32.
DMA_DataSize_TypeDef size
#define USART_FRAME_STOPBITS_ONEANDAHALF
#define USART_FRAME_PARITY_NONE
void CMU_ClockEnable(CMU_Clock_TypeDef clock, bool enable)
Enable/disable a clock.
#define USB_SETUP_RECIPIENT_INTERFACE
#define USART_FRAME_DATABITS_SIX
DMA_DataInc_TypeDef srcInc
DMA_DESCRIPTOR_TypeDef * controlBlock
int USBD_Write(int epAddr, void *data, int byteCount, USB_XferCompleteCb_TypeDef callback)
Start a write (IN) transfer on an endpoint.
#define USART_FRAME_DATABITS_SIXTEEN
#define USART_INITASYNC_DEFAULT
DMA_FuncPtr_TypeDef cbFunc
USB Communication Device Class (CDC) driver.
#define USART_FRAME_DATABITS_FIVE
__STATIC_INLINE uint32_t INT_Disable(void)
Disable interrupts.
void USART_Enable(USART_TypeDef *usart, USART_Enable_TypeDef enable)
Enable/disable USART/UART receiver and/or transmitter.
#define USART_FRAME_DATABITS_EIGHT
void USART_InitAsync(USART_TypeDef *usart, const USART_InitAsync_TypeDef *init)
Init USART/UART for normal asynchronous mode.
#define USB_SETUP_TYPE_CLASS
#define USART_FRAME_PARITY_ODD
void USBTIMER_Start(uint32_t id, uint32_t timeout, USBTIMER_Callback_TypeDef callback)
Start a timer.
Direct memory access (DMA) API.
int USBD_Read(int epAddr, void *data, int byteCount, USB_XferCompleteCb_TypeDef callback)
Start a read (OUT) transfer on an endpoint.
#define STATIC_UBUF(x, y)
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 ...