Skip to content

Commit dc87bab

Browse files
committed
ch58x: code cleanup and minor fixes
1 parent 56f94bb commit dc87bab

7 files changed

Lines changed: 147 additions & 121 deletions

File tree

hw/bsp/ch58x/ch58x_it.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
* This file is part of the TinyUSB stack.
2525
*/
2626

27-
#ifndef __CH58X_IT_H
28-
#define __CH58X_IT_H
27+
#ifndef CH58X_IT_H_
28+
#define CH58X_IT_H_
2929

3030
#ifdef __cplusplus
3131
extern "C" {
@@ -43,4 +43,4 @@ void SysTick_Handler(void);
4343
}
4444
#endif
4545

46-
#endif /* __CH58X_IT_H */
46+
#endif /* CH58X_IT_H_ */

hw/bsp/ch58x/debug_uart.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@
3535
#define UART_RINGBUFFER_MASK_TX (UART_RINGBUFFER_SIZE_TX - 1)
3636

3737
static char tx_buf[UART_RINGBUFFER_SIZE_TX];
38-
static unsigned int tx_produce;
39-
static volatile unsigned int tx_consume;
38+
static uint32_t tx_produce;
39+
static volatile uint32_t tx_consume;
4040

4141
void uart_write(char c) {
42-
unsigned int tx_produce_next = (tx_produce + 1) & UART_RINGBUFFER_MASK_TX;
42+
uint32_t tx_produce_next = (tx_produce + 1) & UART_RINGBUFFER_MASK_TX;
4343

4444
// If ring buffer is full, wait
4545
while (tx_produce_next == tx_consume) {}

hw/bsp/ch58x/debug_uart.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,16 @@
2929

3030
#include <stdint.h>
3131

32+
#ifdef __cplusplus
33+
extern "C" {
34+
#endif
35+
3236
void uart_write(char c);
3337
void uart_sync(void);
3438
void usart_printf_init(uint32_t baudrate);
3539

40+
#ifdef __cplusplus
41+
}
42+
#endif
43+
3644
#endif /* DEBUG_UART_H_ */

hw/bsp/ch58x/system_ch58x.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
* This file is part of the TinyUSB stack.
2525
*/
2626

27-
#ifndef __SYSTEM_CH58X_H
28-
#define __SYSTEM_CH58X_H
27+
#ifndef SYSTEM_CH58X_H_
28+
#define SYSTEM_CH58X_H_
2929

3030
#ifdef __cplusplus
3131
extern "C" {
@@ -40,4 +40,4 @@ extern void SystemCoreClockUpdate(void);
4040
}
4141
#endif
4242

43-
#endif /* __SYSTEM_CH58X_H */
43+
#endif /* SYSTEM_CH58X_H_ */

src/portable/wch/ch58x_usbfs_reg.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,13 @@
279279
#define CH58X_SLP_CLK_OFF1 (*(volatile uint8_t *)0x4000100D)
280280
#define CH58X_SLP_CLK_USB 0x10
281281

282+
//--------------------------------------------------------------------+
283+
// PFIC (Platform-level Fast Interrupt Controller)
284+
//--------------------------------------------------------------------+
285+
#define CH58X_PFIC_IENR ((volatile uint32_t *)0xE000E100) // interrupt enable
286+
#define CH58X_PFIC_IRER ((volatile uint32_t *)0xE000E180) // interrupt reset (disable)
287+
288+
#define CH58X_USB_IRQn 22
289+
#define CH58X_USB2_IRQn 23
290+
282291
#endif /* CH58X_USBFS_REG_H */

src/portable/wch/dcd_ch58x_usbfs.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ static inline void ep_set_both_response(uint32_t base, uint8_t ep, uint8_t tx_re
8484
//--------------------------------------------------------------------+
8585
// Private data structures
8686
//--------------------------------------------------------------------+
87-
struct usb_xfer {
87+
typedef struct {
8888
bool valid;
8989
uint8_t* buffer;
9090
size_t len;
9191
size_t processed_len;
9292
size_t max_size;
93-
};
93+
} xfer_ctl_t;
9494

9595
typedef struct {
9696
uint32_t usb_base;
@@ -99,7 +99,7 @@ typedef struct {
9999
volatile bool ep0_completion_pending; // EP0 XFER_COMPLETE in event queue, not yet processed
100100
uint8_t pending_addr; // Address to set in ISR after Set Address status ZLP
101101
bool isochronous[EP_MAX][2]; // [ep][dir]
102-
struct usb_xfer xfer[EP_MAX][2]; // [ep][dir]
102+
xfer_ctl_t xfer[EP_MAX][2]; // [ep][dir]
103103

104104
// EP0 + EP4 shared buffer: EP0 OUT(64) + EP4 OUT(64) + EP4 IN(64)
105105
TU_ATTR_ALIGNED(4) uint8_t ep0_buffer[EP_BUF_SIZE + EP_BUF_SIZE + EP_BUF_SIZE];
@@ -173,7 +173,7 @@ static uint8_t* ep_dma_buffer(dcd_data_t* d, uint8_t ep) {
173173
static void update_in(uint8_t rhport, uint8_t ep, bool force, bool in_isr) {
174174
dcd_data_t* d = &_dcd_data[rhport];
175175
uint32_t base = d->usb_base;
176-
struct usb_xfer* xfer = &d->xfer[ep][TUSB_DIR_IN];
176+
xfer_ctl_t* xfer = &d->xfer[ep][TUSB_DIR_IN];
177177

178178
if (xfer->valid) {
179179
if (force || xfer->len) {
@@ -218,7 +218,7 @@ static void update_in(uint8_t rhport, uint8_t ep, bool force, bool in_isr) {
218218
static void update_out(uint8_t rhport, uint8_t ep, size_t rx_len, bool in_isr) {
219219
dcd_data_t* d = &_dcd_data[rhport];
220220
uint32_t base = d->usb_base;
221-
struct usb_xfer* xfer = &d->xfer[ep][TUSB_DIR_OUT];
221+
xfer_ctl_t* xfer = &d->xfer[ep][TUSB_DIR_OUT];
222222

223223
if (xfer->valid) {
224224
size_t len = TU_MIN(xfer->max_size, TU_MIN(xfer->len, rx_len));
@@ -426,16 +426,13 @@ void dcd_int_handler(uint8_t rhport) {
426426
}
427427

428428
void dcd_int_enable(uint8_t rhport) {
429-
// PFIC enable: USB_IRQn=22, USB2_IRQn=23
430-
volatile uint32_t* pfic_ienr = (volatile uint32_t*) 0xE000E100;
431-
uint8_t irqn = (rhport == 0) ? 22 : 23;
432-
pfic_ienr[irqn / 32] = (1u << (irqn % 32));
429+
uint8_t irqn = (rhport == 0) ? CH58X_USB_IRQn : CH58X_USB2_IRQn;
430+
CH58X_PFIC_IENR[irqn / 32] = (1u << (irqn % 32));
433431
}
434432

435433
void dcd_int_disable(uint8_t rhport) {
436-
volatile uint32_t* pfic_irer = (volatile uint32_t*) 0xE000E180;
437-
uint8_t irqn = (rhport == 0) ? 22 : 23;
438-
pfic_irer[irqn / 32] = (1u << (irqn % 32));
434+
uint8_t irqn = (rhport == 0) ? CH58X_USB_IRQn : CH58X_USB2_IRQn;
435+
CH58X_PFIC_IRER[irqn / 32] = (1u << (irqn % 32));
439436
__asm volatile ("fence.i");
440437
}
441438

@@ -496,7 +493,11 @@ bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const* desc_ep) {
496493
TU_ASSERT(ep < EP_MAX);
497494

498495
d->isochronous[ep][dir] = (desc_ep->bmAttributes.xfer == TUSB_XFER_ISOCHRONOUS);
499-
d->xfer[ep][dir].max_size = tu_edpt_packet_size(desc_ep);
496+
uint16_t max_size = tu_edpt_packet_size(desc_ep);
497+
if (max_size > EP_BUF_SIZE) {
498+
max_size = EP_BUF_SIZE;
499+
}
500+
d->xfer[ep][dir].max_size = max_size;
500501

501502
if (ep != 0) {
502503
dcd_int_disable(rhport);
@@ -554,7 +555,7 @@ bool dcd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t* buffer,
554555
uint8_t dir = tu_edpt_dir(ep_addr);
555556

556557
dcd_data_t* d = &_dcd_data[rhport];
557-
struct usb_xfer* xfer = &d->xfer[ep][dir];
558+
xfer_ctl_t* xfer = &d->xfer[ep][dir];
558559

559560
dcd_int_disable(rhport);
560561

@@ -576,7 +577,6 @@ bool dcd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t* buffer,
576577
if (dir == TUSB_DIR_IN) {
577578
update_in(rhport, ep, true, is_isr);
578579
} else {
579-
580580
if (d->isochronous[ep][TUSB_DIR_OUT]) {
581581
ep_set_rx_response(d->usb_base, ep, CH58X_EP_R_RES_TOUT);
582582
} else {

0 commit comments

Comments
 (0)