-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcdc_trans.c
147 lines (123 loc) · 3.3 KB
/
cdc_trans.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "cdc_trans.h"
#include "usbd_core.h"
#include "winusb_cdc.h"
#include "ring_buffer.h"
#include "FreeRTOS.h"
#include "event_groups.h"
#include <stdio.h>
#include <stdarg.h>
static ring_buf_t cdc_rb;
static uint8_t cdc_rb_buf[512];
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t cdc_rx_buf[512];
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t cdc_tx_buf[512];
static EventGroupHandle_t cdc_evt_handle;
static StaticEventGroup_t cdc_evt_group;
#define CDC_EVT_TX_DONE (0x01 << 0)
#define CDC_EVT_RX_DONE (0x01 << 1)
static void cdc_rx_cb(uint32_t len)
{
ring_buf_put(&cdc_rb, cdc_rx_buf, len);
BaseType_t xHigherPriorityTaskWoken, xResult;
xHigherPriorityTaskWoken = pdFALSE;
xResult = xEventGroupSetBitsFromISR(
cdc_evt_handle,
CDC_EVT_RX_DONE,
&xHigherPriorityTaskWoken );
if( xResult != pdFAIL )
{
portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
}
cdc_recv(cdc_rx_buf, sizeof(cdc_rx_buf));
}
static void tx_done_cb(void)
{
BaseType_t xHigherPriorityTaskWoken, xResult;
xHigherPriorityTaskWoken = pdFALSE;
xResult = xEventGroupSetBitsFromISR(
cdc_evt_handle,
CDC_EVT_TX_DONE,
&xHigherPriorityTaskWoken );
if( xResult != pdFAIL )
{
portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
}
}
static void cfg_done(void)
{
cdc_recv(cdc_rx_buf, sizeof(cdc_rx_buf));
}
void cdc_trans_init(void)
{
ring_buf_init(&cdc_rb, cdc_rb_buf, sizeof(cdc_rb_buf));
cdc_evt_handle = xEventGroupCreateStatic(&cdc_evt_group);
cdc_register_cb(cdc_rx_cb, tx_done_cb, cfg_done);
}
int cdc_trans_send(const uint8_t *buf, uint16_t len)
{
if(0 == len || NULL == buf)
{
return 0;
}
memcpy(cdc_tx_buf, buf, len);
cdc_send(cdc_tx_buf, len);
EventBits_t uxBits = xEventGroupWaitBits(
cdc_evt_handle,
CDC_EVT_TX_DONE,
pdTRUE,
pdFALSE,
pdMS_TO_TICKS(1000));
if(0 == uxBits)
{
return 0;
}
return len;
}
int cdc_trans_wait_readable(void)
{
if(0 != ring_buf_len(&cdc_rb))
{
return ring_buf_len(&cdc_rb);
}
EventBits_t uxBits = xEventGroupWaitBits(
cdc_evt_handle,
CDC_EVT_RX_DONE,
pdTRUE,
pdFALSE,
portMAX_DELAY);
if(0 == uxBits)
{
return 0;
}
return ring_buf_len(&cdc_rb);
}
int cdc_trans_read_nonblock(uint8_t *buf, uint16_t len)
{
if(0 == len || NULL == buf)
{
return 0;
}
if(0 == ring_buf_len(&cdc_rb))
{
return 0;
}
return ring_buf_get(&cdc_rb, (uint8_t *)buf, len);
}
int cdc_trans_printf(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
int len = vsnprintf((char *)cdc_tx_buf, sizeof(cdc_tx_buf), fmt, args);
va_end(args);
cdc_send(cdc_tx_buf, len);
EventBits_t uxBits = xEventGroupWaitBits(
cdc_evt_handle,
CDC_EVT_TX_DONE,
pdTRUE,
pdFALSE,
pdMS_TO_TICKS(1000));
if(0 == uxBits)
{
return 0;
}
return len;
}