Skip to content

Commit 362fec9

Browse files
committed
refactor: move cherryrb and cherrymp into common dir, rename with usb_ringbuffer and usb_mempool
Signed-off-by: sakumisu <1203593632@qq.com>
1 parent 081c872 commit 362fec9

30 files changed

Lines changed: 327 additions & 1515 deletions

SConscript

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,6 @@ if GetDepend(['PKG_CHERRYUSB_DEVICE']):
140140
src += Glob('class/dfu/usbd_dfu.c')
141141
if GetDepend(['PKG_CHERRYUSB_DEVICE_DISPLAY']):
142142
src += Glob('class/vendor/display/usbd_display.c')
143-
src += Glob('third_party/cherrymp/chry_mempool.c')
144-
src += Glob('third_party/cherrymp/chry_mempool_osal_rtthread.c')
145-
path += [cwd + '/third_party/cherrymp']
146143
if GetDepend(['PKG_CHERRYUSB_DEVICE_ADB']):
147144
src += Glob('class/adb/usbd_adb.c')
148145
src += Glob('platform/rtthread/usbd_adb_shell.c')

cherryusb.cmake

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -380,18 +380,3 @@ if(DEFINED CONFIG_CHERRYUSB_OSAL)
380380
list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/osal/usb_osal_zephyr.c)
381381
endif()
382382
endif()
383-
384-
if(CONFIG_CHERRYRB)
385-
list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/third_party/cherryrb/chry_ringbuffer.c)
386-
list(APPEND cherryusb_incs ${CMAKE_CURRENT_LIST_DIR}/third_party/cherryrb)
387-
endif()
388-
389-
if(CONFIG_CHERRYMP)
390-
list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/third_party/cherrymp/chry_mempool.c)
391-
list(APPEND cherryusb_incs ${CMAKE_CURRENT_LIST_DIR}/third_party/cherrymp)
392-
if("${CONFIG_CHERRYUSB_OSAL}" STREQUAL "freertos")
393-
list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/third_party/cherrymp/chry_mempool_osal_freertos.c)
394-
elseif("${CONFIG_CHERRYUSB_OSAL}" STREQUAL "rtthread")
395-
list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/third_party/cherrymp/chry_mempool_osal_rtthread.c)
396-
endif()
397-
endif()

class/serial/usbh_serial.c

Lines changed: 8 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -23,94 +23,6 @@ static uint32_t g_cdcacm_devinuse = 0;
2323

2424
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_serial_iobuffer[CONFIG_USBHOST_MAX_SERIAL_CLASS][USB_ALIGN_UP((USBH_SERIAL_RX2_NOCACHE_OFFSET + USBH_SERIAL_RX2_NOCACHE_SIZE), CONFIG_USB_ALIGN_SIZE)];
2525

26-
/* refer to cherryrb */
27-
static int usbh_serial_ringbuffer_init(usbh_serial_ringbuf_t *rb, void *pool, uint32_t size)
28-
{
29-
if (NULL == rb) {
30-
return -1;
31-
}
32-
33-
if (NULL == pool) {
34-
return -1;
35-
}
36-
37-
if ((size < 2) || (size & (size - 1))) {
38-
return -1;
39-
}
40-
41-
rb->in = 0;
42-
rb->out = 0;
43-
rb->mask = size - 1;
44-
rb->pool = pool;
45-
46-
return 0;
47-
}
48-
49-
static void usbh_serial_ringbuffer_reset(usbh_serial_ringbuf_t *rb)
50-
{
51-
rb->in = 0;
52-
rb->out = 0;
53-
}
54-
55-
static uint32_t usbh_serial_ringbuffer_get_used(usbh_serial_ringbuf_t *rb)
56-
{
57-
return rb->in - rb->out;
58-
}
59-
60-
static uint32_t usbh_serial_ringbuffer_write(usbh_serial_ringbuf_t *rb, void *data, uint32_t size)
61-
{
62-
uint32_t unused;
63-
uint32_t offset;
64-
uint32_t remain;
65-
66-
unused = (rb->mask + 1) - (rb->in - rb->out);
67-
68-
if (size > unused) {
69-
size = unused;
70-
}
71-
72-
offset = rb->in & rb->mask;
73-
74-
remain = rb->mask + 1 - offset;
75-
remain = remain > size ? size : remain;
76-
77-
memcpy(((uint8_t *)(rb->pool)) + offset, data, remain);
78-
memcpy(rb->pool, (uint8_t *)data + remain, size - remain);
79-
80-
rb->in += size;
81-
82-
return size;
83-
}
84-
85-
static uint32_t usbh_serial_ringbuffer_peek(usbh_serial_ringbuf_t *rb, void *data, uint32_t size)
86-
{
87-
uint32_t used;
88-
uint32_t offset;
89-
uint32_t remain;
90-
91-
used = rb->in - rb->out;
92-
if (size > used) {
93-
size = used;
94-
}
95-
96-
offset = rb->out & rb->mask;
97-
98-
remain = rb->mask + 1 - offset;
99-
remain = remain > size ? size : remain;
100-
101-
memcpy(data, ((uint8_t *)(rb->pool)) + offset, remain);
102-
memcpy((uint8_t *)data + remain, rb->pool, size - remain);
103-
104-
return size;
105-
}
106-
107-
static uint32_t usbh_serial_ringbuffer_read(usbh_serial_ringbuf_t *rb, void *data, uint32_t size)
108-
{
109-
size = usbh_serial_ringbuffer_peek(rb, data, size);
110-
rb->out += size;
111-
return size;
112-
}
113-
11426
static struct usbh_serial *usbh_serial_alloc(bool is_cdcacm)
11527
{
11628
uint8_t devno;
@@ -193,9 +105,9 @@ static void usbh_serial_callback(void *arg, int nbytes)
193105
return;
194106
}
195107

196-
usbh_serial_ringbuffer_write(&serial->rx_rb,
197-
&serial->iobuffer[(serial->rx_buf_index ? USBH_SERIAL_RX2_NOCACHE_OFFSET : USBH_SERIAL_RX_NOCACHE_OFFSET) + serial->driver->ignore_rx_header],
198-
(nbytes - serial->driver->ignore_rx_header));
108+
usb_ringbuffer_write(&serial->rx_rb,
109+
&serial->iobuffer[(serial->rx_buf_index ? USBH_SERIAL_RX2_NOCACHE_OFFSET : USBH_SERIAL_RX_NOCACHE_OFFSET) + serial->driver->ignore_rx_header],
110+
(nbytes - serial->driver->ignore_rx_header));
199111

200112
if (serial->rx_complete_callback) {
201113
serial->rx_complete_callback(serial, nbytes - serial->driver->ignore_rx_header);
@@ -319,7 +231,7 @@ struct usbh_serial *usbh_serial_open(const char *devname, uint32_t open_flags)
319231
}
320232
}
321233

322-
usbh_serial_ringbuffer_init(&serial->rx_rb, serial->rx_rb_pool, CONFIG_USBHOST_SERIAL_RX_SIZE);
234+
usb_ringbuffer_init(&serial->rx_rb, serial->rx_rb_pool, CONFIG_USBHOST_SERIAL_RX_SIZE);
323235

324236
serial->ref_count++;
325237
serial->open_flags = open_flags;
@@ -454,7 +366,7 @@ int usbh_serial_control(struct usbh_serial *serial, int cmd, void *arg)
454366
return ret;
455367
}
456368

457-
usbh_serial_ringbuffer_reset(&serial->rx_rb);
369+
usb_ringbuffer_reset(&serial->rx_rb);
458370
usb_osal_sem_reset(serial->rx_complete_sem);
459371
serial->rx_buf_index = 0;
460372
usbh_bulk_urb_fill(&serial->bulkin_urb, serial->hport, serial->bulkin, &serial->iobuffer[serial->rx_buf_index ? USBH_SERIAL_RX2_NOCACHE_OFFSET : USBH_SERIAL_RX_NOCACHE_OFFSET], serial->bulkin->wMaxPacketSize,
@@ -561,9 +473,9 @@ int usbh_serial_read(struct usbh_serial *serial, void *buffer, uint32_t buflen)
561473
}
562474

563475
if (serial->open_flags & USBH_SERIAL_O_NONBLOCK) {
564-
return usbh_serial_ringbuffer_read(&serial->rx_rb, buffer, buflen);
476+
return usb_ringbuffer_read(&serial->rx_rb, buffer, buflen);
565477
} else {
566-
if (usbh_serial_ringbuffer_get_used(&serial->rx_rb) == 0) {
478+
if (usb_ringbuffer_get_used(&serial->rx_rb) == 0) {
567479
ret = usb_osal_sem_take(serial->rx_complete_sem, serial->rx_timeout_ms == 0 ? USB_OSAL_WAITING_FOREVER : serial->rx_timeout_ms);
568480
if (ret < 0) {
569481
return ret;
@@ -572,7 +484,7 @@ int usbh_serial_read(struct usbh_serial *serial, void *buffer, uint32_t buflen)
572484
return serial->rx_errorcode;
573485
}
574486
}
575-
return usbh_serial_ringbuffer_read(&serial->rx_rb, buffer, buflen);
487+
return usb_ringbuffer_read(&serial->rx_rb, buffer, buflen);
576488
}
577489
}
578490

class/serial/usbh_serial.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,6 @@
6767
extern "C" {
6868
#endif
6969

70-
typedef struct {
71-
uint32_t in; /*!< Define the write pointer. */
72-
uint32_t out; /*!< Define the read pointer. */
73-
uint32_t mask; /*!< Define the write and read pointer mask. */
74-
void *pool; /*!< Define the memory pointer. */
75-
} usbh_serial_ringbuf_t;
76-
7770
/*
7871
* Counters of the input lines (CTS, DSR, RI, CD) interrupts
7972
*/
@@ -143,7 +136,7 @@ struct usbh_serial {
143136

144137
const struct usbh_serial_driver *driver;
145138

146-
usbh_serial_ringbuf_t rx_rb;
139+
usb_ringbuffer_t rx_rb;
147140
uint8_t rx_rb_pool[CONFIG_USBHOST_SERIAL_RX_SIZE];
148141
usb_osal_sem_t rx_complete_sem;
149142
uint8_t rx_buf_index;

class/vendor/display/usbd_display.c

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,37 @@
66

77
#include "usbd_core.h"
88
#include "usbd_display.h"
9-
#include "chry_mempool.h"
10-
11-
struct usbd_disp_frame_header {
12-
uint16_t crc16; //payload crc16
13-
uint8_t type; //raw rgb,yuv,jpg,other
14-
uint8_t cmd;
15-
uint16_t x; //32bit
16-
uint16_t y;
17-
uint16_t width; //32bit
18-
uint16_t height;
19-
uint32_t frame_id : 10;
20-
uint32_t payload_total : 22; //payload max 4MB
21-
} __PACKED;
229

2310
struct usbd_display_priv {
24-
struct chry_mempool pool;
11+
struct usb_mempool pool;
2512
struct usbd_endpoint out_ep;
2613
struct usbd_endpoint in_ep;
2714
struct usbd_display_frame *current_frame;
2815
} g_usbd_display;
2916

3017
int usbd_display_frame_create(struct usbd_display_frame *frame, uint32_t count)
3118
{
32-
return chry_mempool_create(&g_usbd_display.pool, frame, sizeof(struct usbd_display_frame), count);
19+
return usb_mempool_create(&g_usbd_display.pool, frame, sizeof(struct usbd_display_frame), count);
3320
}
3421

3522
struct usbd_display_frame *usbd_display_frame_alloc(void)
3623
{
37-
return (struct usbd_display_frame *)chry_mempool_alloc(&g_usbd_display.pool);
24+
return (struct usbd_display_frame *)usb_mempool_alloc(&g_usbd_display.pool);
3825
}
3926

4027
int usbd_display_frame_free(struct usbd_display_frame *frame)
4128
{
42-
return chry_mempool_free(&g_usbd_display.pool, (uintptr_t *)frame);
29+
return usb_mempool_free(&g_usbd_display.pool, (uintptr_t *)frame);
4330
}
4431

4532
int usbd_display_frame_send(struct usbd_display_frame *frame)
4633
{
47-
return chry_mempool_send(&g_usbd_display.pool, (uintptr_t *)frame);
34+
return usb_mempool_send(&g_usbd_display.pool, (uintptr_t *)frame);
4835
}
4936

5037
int usbd_display_frame_recv(struct usbd_display_frame **frame, uint32_t timeout)
5138
{
52-
return chry_mempool_recv(&g_usbd_display.pool, (uintptr_t **)frame, timeout);
39+
return usb_mempool_recv(&g_usbd_display.pool, (uintptr_t **)frame, timeout);
5340
}
5441

5542
uint8_t usb_dispay_dummy[512];
@@ -65,6 +52,7 @@ static void display_notify_handler(uint8_t busid, uint8_t event, void *arg)
6552
usb_display_buf_offset = 0;
6653
usb_display_ignore_frame = true;
6754
g_usbd_display.current_frame = NULL;
55+
usb_mempool_reset(&g_usbd_display.pool);
6856
usbd_ep_start_read(busid, g_usbd_display.out_ep.ep_addr, usb_dispay_dummy, usbd_get_ep_mps(0, g_usbd_display.out_ep.ep_addr));
6957
break;
7058
default:

class/vendor/display/usbd_display.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111
#define USBD_DISPLAY_TYPE_YUV420 2
1212
#define USBD_DISPLAY_TYPE_JPG 3
1313

14+
struct usbd_disp_frame_header {
15+
uint16_t crc16; //payload crc16
16+
uint8_t type; //raw rgb,yuv,jpg,other
17+
uint8_t cmd;
18+
uint16_t x; //32bit
19+
uint16_t y;
20+
uint16_t width; //32bit
21+
uint16_t height;
22+
uint32_t frame_id : 10;
23+
uint32_t payload_total : 22; //payload max 4MB
24+
} __PACKED;
25+
1426
struct usbd_display_frame {
1527
uint8_t *frame_buf;
1628
uint32_t frame_bufsize;

0 commit comments

Comments
 (0)