Compare commits

..

No commits in common. "master" and "ch32_drv" have entirely different histories.

8 changed files with 45 additions and 140 deletions

View File

@ -9,15 +9,12 @@
#include "acm.h"
#include <memory.h>
#include <stdint.h>
#include <string.h>
#include "../usb.h"
#include "../usb_device_types.h"
#include "cmsis_os2.h"
#include <blocking_io/blocking_fifo.h>
#include <strings.h>
// -------------------------
@ -25,45 +22,6 @@ static Usb_AcmState acms = {0}; ///< ACM module state
static uint8_t tx_buffer[USB_ACM_PCKT_BUFSIZE]; ///< Transmit buffer
static uint8_t fifo_mem[USB_ACM_FIFO_MEM_SIZE]; ///< Memory assigned to the TX BFifo
static BFifo fifo; ///< TX Blocking FIFO
static osThreadId_t th; ///< ACM thread
static osEventFlagsId_t flags; ///< Event flags
#define USB_ACM_DATA_IN_DONE (0x01) ///< IN transfer done flag
#define USB_ACM_COMM_INIT (0x02) ///< Communication has been initialized
#define USB_ACM_HOSTBOUND_DATA_AVAIL (0x04) ///< Hostbound data is available
#define USB_ACM_LOOP_TIMEOUT_TICKS (100) ///< Main loop timeout for interrupt status transmission
#define USB_ACM_INITIAL_DELAY_TICKS (100) ///< Delay before sending data
// -------------------------
static void thread_usb_acm(void *arg) {
osEventFlagsWait(flags, USB_ACM_COMM_INIT, 0, osWaitForever); // wait for communication to become initialized
osEventFlagsSet(flags, USB_ACM_DATA_IN_DONE); // assume we can write to the data endpoint
osDelay(USB_ACM_INITIAL_DELAY_TICKS); // inject some initial delay
while (true) {
uint32_t signals = osEventFlagsWait(flags, USB_ACM_HOSTBOUND_DATA_AVAIL, osFlagsWaitAny, USB_ACM_LOOP_TIMEOUT_TICKS);
if (signals != osErrorTimeout) { // check timeout
if (signals & USB_ACM_HOSTBOUND_DATA_AVAIL) { // data hostbound available
do {
osEventFlagsWait(flags, USB_ACM_DATA_IN_DONE, osFlagsWaitAny, 1); // wait for the IN DONE flag
uint32_t readSize = bfifo_read(&fifo, tx_buffer, USB_ACM_PCKT_BUFSIZE); // read from the fifo
if (readSize > 0) {
uint32_t writeSize = usbcore_schedule_transmission(acms.ep_assignments.data_ep, tx_buffer, readSize); // write data acquired from the buffer
bfifo_pop(&fifo, writeSize, 0); // pop with no blocking
}
} while (bfifo_get_used(&fifo) > 0);
}
} else { // timeout
// send an all-zero interrupt
usbcore_schedule_transmission(acms.ep_assignments.control_ep, (const uint8_t *)&(acms.interrupt_data), sizeof(uint16_t));
}
}
}
// -------------------------
@ -84,22 +42,13 @@ void usb_acm_init(const Usb_Acm_EpAssignments *as) {
// initialize an all-0 interrupt
acms.interrupt_data = 0;
acms.interrupt_pending = true;
// communication parameters have not been set
acms.commInit = false;
// from now on CDC module is considered initialized
acms.moduleInit = true;
// create flags
flags = osEventFlagsNew(NULL);
// create thread
osThreadAttr_t attr;
memset(&attr, 0, sizeof(osThreadAttr_t));
attr.stack_size = 512;
attr.name = "acm";
th = osThreadNew(thread_usb_acm, NULL, &attr);
}
static void usb_cdc_review_comm_init() {
@ -112,9 +61,6 @@ static void usb_cdc_review_comm_init() {
// combine the above criteria
acms.commInit = lcOk && clsOk;
// signal the processing thread
osEventFlagsSet(flags, USB_ACM_COMM_INIT);
}
int usb_acm_process_and_return(Usb_CallbackEvent *cbevt) {
@ -162,11 +108,24 @@ int usb_acm_process_and_return(Usb_CallbackEvent *cbevt) {
}
case USB_CBEVT_IN: {
if (cbevt->ep == acms.ep_assignments.data_ep) {
osEventFlagsSet(flags, USB_ACM_DATA_IN_DONE);
if (cbevt->ep == acms.ep_assignments.control_ep) { // if notification feeding is requested
if (acms.interrupt_pending) {
usbcore_schedule_transmission(acms.ep_assignments.control_ep, (const uint8_t *)&(acms.interrupt_data), sizeof(uint16_t)); // send ZLP
acms.interrupt_pending = false;
}
ret = 0;
break;
} else if (cbevt->ep == acms.ep_assignments.data_ep) { // if data are requested
ret = 0;
// read from the fifo
if (acms.commInit) {
uint32_t readSize = bfifo_read(&fifo, tx_buffer, USB_ACM_PCKT_BUFSIZE);
if (readSize > 0) {
uint32_t writeSize = usbcore_schedule_transmission(acms.ep_assignments.data_ep, tx_buffer, readSize); // write data acquired from the buffer
bfifo_pop(&fifo, writeSize, 0); // pop with no blocking
}
}
}
}
default:
@ -179,10 +138,11 @@ int usb_acm_process_and_return(Usb_CallbackEvent *cbevt) {
void usb_acm_write(const uint8_t *data, uint32_t size) {
if (acms.moduleInit) {
bfifo_push_all(&fifo, data, size);
osEventFlagsSet(flags, USB_ACM_HOSTBOUND_DATA_AVAIL);
usbcore_wake_up_endpoint(acms.ep_assignments.data_ep, USB_IN);
}
}
/**
* @fn void usb_acm_read_callback(const uint8_t *data, uint32_t size)
* Callback function prototype for data reception. This function is

View File

@ -82,6 +82,7 @@ typedef struct {
Usb_Acm_LineCodingStruct line_coding; ///< Line Coding
Usb_Acm_ControlLineStateStruct control_line_state; ///< Control Line State
uint16_t interrupt_data; ///< Data sent though the next transfer on the notification element
bool interrupt_pending; ///< Interrupt data is valid and should be send in the next cycle
bool moduleInit; ///< CDC module is initialized
bool commInit; ///< Communication protocol is initialized
} Usb_AcmState;

View File

@ -34,8 +34,8 @@
#define USB_EP_TX_BUF_SIZE (64) ///< Transmit buffer size
#define USB_EP_RX_BUF_SIZE (64) ///< Receive buffer size
#define USB_EP_COMBINED_BUF_SIZE ((USB_EP_TX_BUF_SIZE + USB_EP_RX_BUF_SIZE)) ///< Combined buffer size in a single direction
#define USB_EP_SUMMED_BUF_SIZE (USB_EP_COMBINED_BUF_SIZE * USB_NUM_OF_ENDPOINTS) ///< Summed size for each endpoint in each direction
#define USB_EP_COMBINED_BUF_SIZE ((USB_EP_TX_BUF_SIZE + USB_EP_RX_BUF_SIZE) * 2) ///< Combined buffer size in a single direction
#define USB_EP_SUMMED_BUF_SIZE (USB_EP_COMBINED_BUF_SIZE * USB_NUM_OF_ENDPOINTS * 2) ///< Summed size for each endpoint in each direction
static USBDRV_GlobalState gs; ///< Global USB state
static uint8_t buf[USB_EP_SUMMED_BUF_SIZE] DWORD_ALIGN; ///< Transmit/Receive buffer
@ -43,8 +43,8 @@ static UsbDrv_IN_cb cbs[USB_NUM_OF_ENDPOINTS]; ///< Callbacks for IN co
// FIXME: ez lehet, hogy pont fordĂ­tva van...
#define USB_EP_GET_EP0_BUFFER() (gs.buf)
#define USB_EP_GET_TX_BUFFER(ep) (gs.buf + ((ep) * USB_EP_COMBINED_BUF_SIZE) + USB_EP_RX_BUF_SIZE)
#define USB_EP_GET_RX_BUFFER(ep) (gs.buf + ((ep) * USB_EP_COMBINED_BUF_SIZE))
#define USB_EP_GET_TX_BUFFER(ep) (gs.buf + ((ep) * USB_EP_COMBINED_BUF_SIZE))
#define USB_EP_GET_RX_BUFFER(ep) (gs.buf + ((ep) * USB_EP_COMBINED_BUF_SIZE) + USB_EP_TX_BUF_SIZE)
/** \cond false */
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
@ -141,14 +141,6 @@ void usbdrv_init_global_state() {
gs.buf = buf;
}
/**
* Hook for initializing modules after the low-level driver has been initialized
* but has not been connected to the bus yet.
*/
__weak void usbdrv_init_hook() {
return;
}
// initialize USB subsystem
void usbdrv_init() {
USB_IRQ_DISABLE(USB_IRQ_N); // disable USB interrupts
@ -158,7 +150,6 @@ void usbdrv_init() {
usbdrv_gpio_init();
usbdrv_periph_init(false);
usbdrv_initial_ep0_setup();
usbdrv_init_hook(); // <---
usbdrv_power_and_connect(true);
USB_IRQ_SET_PRIORITY(USB_IRQ_N, USB_IRQ_PRIORITY);
@ -214,7 +205,7 @@ typedef struct {
} USB_EP_Ctrl;
// clang-format off
static const USB_EP_Ctrl USB_EPCtrl[USB_MAX_NUM_OF_ENDPOINTS] = {
static const USB_EP_Ctrl USB_EPCtrl[USB_NUM_OF_ENDPOINTS] = {
{NULL, 0, 0, 0, &(USBG->UEP0_DMA), &(USBG->UEP0_TX_LEN), &(USBG->UEP0_TX_CTRL), &(USBG->UEP0_RX_CTRL)}, // EP0
{&(USBG->UEP4_1_MOD), USBFS_UEP1_RX_EN, USBFS_UEP1_TX_EN, USBFS_UEP1_BUF_MOD, &(USBG->UEP1_DMA), &(USBG->UEP1_TX_LEN), &(USBG->UEP1_TX_CTRL), &(USBG->UEP1_RX_CTRL) }, // EP1
{&(USBG->UEP2_3_MOD), USBFS_UEP2_RX_EN, USBFS_UEP2_TX_EN, USBFS_UEP2_BUF_MOD, &(USBG->UEP2_DMA), &(USBG->UEP2_TX_LEN), &(USBG->UEP2_TX_CTRL), &(USBG->UEP2_RX_CTRL) }, // EP2
@ -320,7 +311,7 @@ uint32_t usbdrv_arm_IN_endpoint(uint8_t ep, const uint8_t *data, uint16_t len) {
// copy data to the output buffer
if (txLen > 0) {
uint8_t *txBuf = (ep == 0) ? USB_EP_GET_EP0_BUFFER() : USB_EP_GET_TX_BUFFER(ep);
uint8_t *txBuf = USB_EP_GET_EP0_BUFFER();
memcpy(txBuf, data, txLen);
}
@ -330,9 +321,9 @@ uint32_t usbdrv_arm_IN_endpoint(uint8_t ep, const uint8_t *data, uint16_t len) {
// append ZLP only, if packet size is MAX_PCKT_SIZE (this way to ZLP is injected in a longer packet whose first part is limited to 64 bytes)
gs.ep_IN[ep].zlp_next = (len == USB_MAX_FS_PCKT_SIZE_NON_ISOCHRONOUS);
// non-EP0 must begin responding with DATA0
// if (ep != 0) {
// CLEAR_BIT(*epc->TX_CTRL, USBFS_UEP_T_TOG);
// EP0 must begin responding with DATA1
// if (ep == 0) {
// SET_BIT(*epc->TX_CTRL, USBFS_UEP_T_TOG);
// }
// signal that transmission is in progress
@ -454,7 +445,7 @@ void usbdrv_fetch_endpoint_configuration(uint8_t config_index) {
// build buffer structure, allocate buffers (compute addresses)
void usbdrv_allocate_buffers() {
for (uint8_t i = 0; i < USB_NUM_OF_ENDPOINTS; i++) {
*USB_EPCtrl[i].BUF_START_ADDR = (uint32_t)USB_EP_GET_RX_BUFFER(i);
*USB_EPCtrl[i].BUF_START_ADDR = (uint32_t)USB_EP_GET_TX_BUFFER(i);
USBDRV_EpConfig *cfg = &gs.ep_IN[i];
if (cfg->is_configured) {

View File

@ -5,18 +5,8 @@
#include "../../usb_driver_common.h"
// maximum number of endpoints that can be supported
#define USB_MAX_NUM_OF_ENDPOINTS (8)
// number of supported endpoints
#ifndef USB_NUM_OF_ENDPOINTS // number of endpoints can be overridden to conserve memory
#define USB_NUM_OF_ENDPOINTS (USB_MAUSB_MAX_NUM_OF_ENDPOINTS) // set it to the maximum that this type of module can support
#else
#if USB_MAX_NUM_OF_ENDPOINTS > USB_MAX_NUM_OF_ENDPOINTS // do not allow greater number of endpoints than what the device supports
#undef USB_NUM_OF_ENDPOINTS
#define USB_NUM_OF_ENDPOINTS (USB_MUSB_MAX_NUM_OF_ENDPOINTS)
#endif
#endif
#define USB_NUM_OF_ENDPOINTS (8) // set it to the maximum that this type of module can support
// non isochronous transfers
#define USB_MAX_FS_PCKT_SIZE_NON_ISOCHRONOUS (64)

View File

@ -192,14 +192,6 @@ void usbdrv_init_global_state() {
gs.rx_buf_level = 0;
}
/**
* Hook for initializing modules after the low-level driver has been initialized
* but has not been connected to the bus yet.
*/
__weak void usbdrv_init_hook() {
return;
}
// initialize USB subsystem
void usbdrv_init() {
USB_IRQ_DISABLE(USB_IRQ_N); // disable USB interrupts
@ -209,7 +201,6 @@ void usbdrv_init() {
usbdrv_gpio_init();
usbdrv_periph_init(false);
usbdrv_initial_ep0_setup();
usbdrv_init_hook(); // <---
usbdrv_power_and_connect(true);
USB_IRQ_SET_PRIORITY(USB_IRQ_N, USB_IRQ_PRIORITY);

View File

@ -1,38 +0,0 @@
#ifndef CONFIGS_FLATUSB_CONFIG
#define CONFIGS_FLATUSB_CONFIG
#ifndef SRC_FLATUSB_CONFIG
#define SRC_FLATUSB_CONFIG
#include <ch32f20x_usb.h>
#include <ch32f20x_rcc.h>
#include <ch32f20x.h>
static inline void usbdrv_gpio_init(void) {
// turn ON GPIOA clocks
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
}
#define USB_IRQ_N OTG_FS_IRQn
#define USB_IRQ_HANDLER OTG_FS_IRQHandler
#define USB_IRQ_PRIORITY (8)
#define USB_IRQ_SET_PRIORITY(irq, priority) NVIC_SetPriority((irq),(priority))
#define USB_IRQ_ENABLE(irq) NVIC_EnableIRQ((irq))
#define USB_IRQ_DISABLE(irq) NVIC_DisableIRQ((irq))
// define USBG
#define USBG (USBOTG_FS)
#define USB_CLOCK_ENABLE() RCC_AHBPeriphClockCmd(RCC_AHBPeriph_OTG_FS, ENABLE);\
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE)
#include "embfmt/embformat.h"
#define SNPRINTF(str, n, fmt, ...) embfmt(str, n, fmt, __VA_ARGS__)
#ifdef USBDBGMSG
#define USBMSG(...) MSG(__VA_ARGS__)
#endif
#endif /* SRC_FLATUSB_CONFIG */
#endif /* CONFIGS_FLATUSB_CONFIG */

6
usb.c
View File

@ -271,9 +271,11 @@ void usbcore_process_nonsetup_event(UsbDrv_CallbackCompound *cbcpd) {
}
}
void usbcore_wake_up_endpoint(uint8_t ep, uint8_t dir) {
drv->en_ep_irq(ep, dir, true);
}
void usbcore_register_IN_callback(uint8_t ep, UsbDrv_IN_cb cb) {
bool en = cb != NULL;
drv->en_ep_irq(ep, USB_IN, en);
drv->reg_IN_cb(ep, cb);
}

8
usb.h
View File

@ -60,6 +60,14 @@ uint32_t usbcore_schedule_transmission(uint8_t ep, const uint8_t *data, uint16_t
*/
uint32_t usbcore_schedule_reception(uint8_t ep, uint16_t size);
/**
* Wake up an Endpoint.
*
* @param ep index of the Endpoint
* @param dir direction of the Endpoint
*/
void usbcore_wake_up_endpoint(uint8_t ep, uint8_t dir);
/**
* Register a callback for IN transmission completion.
*