1 #include <twr_usb_cdc.h>
2 #include <twr_scheduler.h>
4 #include <twr_system.h>
8 #include <usbd_cdc_if.h>
11 #include <stm32l0xx.h>
16 uint8_t receive_buffer[1024];
17 uint8_t transmit_buffer[512];
18 size_t transmit_length;
23 USBD_HandleTypeDef hUsbDeviceFS;
25 static void _twr_usb_cdc_task_start(
void *param);
26 static void _twr_usb_cdc_task(
void *param);
27 static void _twr_usb_cdc_init_hsi48();
31 memset(&_twr_usb_cdc, 0,
sizeof(_twr_usb_cdc));
33 _twr_usb_cdc_init_hsi48();
35 twr_fifo_init(&_twr_usb_cdc.receive_fifo, _twr_usb_cdc.receive_buffer,
sizeof(_twr_usb_cdc.receive_buffer));
37 __HAL_RCC_GPIOA_CLK_ENABLE();
39 USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS);
40 USBD_RegisterClass(&hUsbDeviceFS, &USBD_CDC);
41 USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS);
48 if (length > (
sizeof(_twr_usb_cdc.transmit_buffer) - _twr_usb_cdc.transmit_length))
53 memcpy(&_twr_usb_cdc.transmit_buffer[_twr_usb_cdc.transmit_length], buffer, length);
55 _twr_usb_cdc.transmit_length += length;
64 size_t bytes_read = 0;
70 if (
twr_fifo_read(&_twr_usb_cdc.receive_fifo, &value, 1) == 1)
72 *(uint8_t *) buffer = value;
74 buffer = (uint8_t *) buffer + 1;
89 void twr_usb_cdc_received_data(
const void *buffer,
size_t length)
94 static void _twr_usb_cdc_task_start(
void *param)
102 USBD_Start(&hUsbDeviceFS);
105 static void _twr_usb_cdc_task(
void *param)
109 if (_twr_usb_cdc.transmit_length == 0)
114 HAL_NVIC_DisableIRQ(USB_IRQn);
116 if (CDC_Transmit_FS(_twr_usb_cdc.transmit_buffer, _twr_usb_cdc.transmit_length) == USBD_OK)
118 _twr_usb_cdc.transmit_length = 0;
121 HAL_NVIC_EnableIRQ(USB_IRQn);
126 static void _twr_usb_cdc_init_hsi48()
128 twr_system_pll_enable();
130 RCC->CRRCR |= RCC_CRRCR_HSI48ON;
131 RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
132 SYSCFG->CFGR3 |= SYSCFG_CFGR3_ENREF_HSI48;
134 while((RCC->CRRCR & RCC_CRRCR_HSI48ON) == 0)
139 RCC->CCIPR |= RCC_USBCLKSOURCE_HSI48;
140 RCC->CFGR &= ~RCC_CFGR_STOPWUCK_Msk;
size_t twr_fifo_irq_write(twr_fifo_t *fifo, const void *buffer, size_t length)
Write data to FIFO from interrupt.
size_t twr_fifo_read(twr_fifo_t *fifo, void *buffer, size_t length)
Read data from FIFO.
void twr_fifo_init(twr_fifo_t *fifo, void *buffer, size_t size)
Initialize FIFO buffer.
void twr_scheduler_unregister(twr_scheduler_task_id_t task_id)
Unregister specified task.
size_t twr_scheduler_task_id_t
Task ID assigned by scheduler.
void twr_scheduler_plan_now(twr_scheduler_task_id_t task_id)
Schedule specified task for immediate execution.
void twr_scheduler_plan_current_now(void)
Schedule current task for immediate execution.
twr_scheduler_task_id_t twr_scheduler_register(void(*task)(void *), void *param, twr_tick_t tick)
Register task in scheduler.
size_t twr_usb_cdc_read(void *buffer, size_t length)
Read buffer from USB CDC (non-blocking call)
void twr_usb_cdc_init(void)
Initialize USB CDC library.
bool twr_usb_cdc_write(const void *buffer, size_t length)
Write buffer to USB CDC (non-blocking call)
Structure of FIFO instance.