forked from rene-dev/stmbl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usb_cdc.c
175 lines (137 loc) · 3.84 KB
/
usb_cdc.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "usbd_cdc_if.h"
#include "usbd_cdc_core.h"
#include "usbd_usr.h"
#include "usbd_desc.h"
#include "usbd_conf.h"
#include "usb_dcd_int.h"
#include "stm32f4xx.h"
#include "stm32f4xx_conf.h"
#define RX_QUEUE_SIZE 512
struct ringbuf usb_rx_buf = {.buf = (char[RX_QUEUE_SIZE]){0}, .bufsize = RX_QUEUE_SIZE};
static USB_OTG_CORE_HANDLE USB_OTG_dev;
static uint16_t VCP_Init(void) {
return USBD_OK;
}
static uint16_t VCP_DeInit(void) {
return USBD_OK;
}
static uint16_t VCP_Ctrl(uint32_t Cmd, uint8_t *Buf, uint32_t Len) {
return USBD_OK;
}
// this function is not called
static uint16_t VCP_DataTx(void) {
return USBD_OK;
}
static uint16_t VCP_DataRx(uint8_t *buf, uint32_t len) {
rb_write(&usb_rx_buf, buf, len);
return USBD_OK;
}
const CDC_IF_Prop_TypeDef VCP_fops = {
.pIf_Init = VCP_Init,
.pIf_DeInit = VCP_DeInit,
.pIf_Ctrl = VCP_Ctrl,
.pIf_DataTx = VCP_DataTx,
.pIf_DataRx = VCP_DataRx};
void usb_init(void) {
USBD_Init(&USB_OTG_dev, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_CDC_cb, &USR_cb);
}
void OTG_FS_IRQHandler(void) {
USBD_OTG_ISR_Handler(&USB_OTG_dev);
}
void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev) {
// Enable peripheral clocks
//
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
RCC->AHB2ENR |= RCC_APB2ENR_SYSCFGEN;
RCC->AHB2ENR |= RCC_AHB2ENR_OTGFSEN;
// enable I/O compensation cell to reduce the I/O noise on power supply
SYSCFG->CMPCR = SYSCFG_CMPCR_CMP_PD;
// Configure DM and DP Pins
//
GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_OTG1_FS);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_OTG1_FS);
GPIO_Init(GPIOA, &(GPIO_InitTypeDef){.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12, .GPIO_Speed = GPIO_Speed_50MHz, .GPIO_Mode = GPIO_Mode_AF, .GPIO_OType = GPIO_OType_PP, .GPIO_PuPd = GPIO_PuPd_NOPULL});
}
void USB_OTG_BSP_EnableInterrupt(USB_OTG_CORE_HANDLE *pdev) {
NVIC_Init(&(NVIC_InitTypeDef){
.NVIC_IRQChannel = OTG_FS_IRQn,
.NVIC_IRQChannelPreemptionPriority = 15,
.NVIC_IRQChannelSubPriority = 0,
.NVIC_IRQChannelCmd = ENABLE});
}
void USB_OTG_BSP_uDelay(const uint32_t usec) {
uint32_t count = 0;
const uint32_t utime = (120 * usec / 7);
do {
if(++count > utime) {
return;
}
} while(1);
}
void USB_OTG_BSP_mDelay(const uint32_t msec) {
USB_OTG_BSP_uDelay(msec * 1000);
}
// USB_Usr
enum {
USB_CDC_DETACHED,
USB_CDC_CONNECTED
} E_USB_STAT;
uint8_t usb_cdc_status = USB_CDC_DETACHED;
void USBD_USR_Init(void) {
usb_cdc_status = USB_CDC_DETACHED;
}
void USBD_USR_DeviceReset(uint8_t speed) {
usb_cdc_status = USB_CDC_DETACHED;
}
void USBD_USR_DeviceConfigured(void) {
usb_cdc_status = USB_CDC_CONNECTED;
}
void USBD_USR_DeviceConnected(void) {
usb_cdc_status = USB_CDC_DETACHED;
}
void USBD_USR_DeviceDisconnected(void) {
usb_cdc_status = USB_CDC_DETACHED;
}
void USBD_USR_DeviceSuspended(void) {
usb_cdc_status = USB_CDC_DETACHED;
}
void USBD_USR_DeviceResumed(void) {
usb_cdc_status = USB_CDC_DETACHED;
}
USBD_Usr_cb_TypeDef USR_cb =
{
USBD_USR_Init,
USBD_USR_DeviceReset,
USBD_USR_DeviceConfigured,
USBD_USR_DeviceSuspended,
USBD_USR_DeviceResumed,
USBD_USR_DeviceConnected,
USBD_USR_DeviceDisconnected,
};
uint8_t USB_CDC_is_connected(void) {
return usb_cdc_status;
}
//TODO: implement new term API
void cdc_init(void) {}
int cdc_tx(void *data, uint32_t len) {
if(!cdc_is_connected()){
return 0;
}
while(len--) {
// send a queued byte - copy to usb stack buffer
APP_Rx_Buffer[APP_Rx_ptr_in++] = *(uint8_t *)data;
data++;
// To avoid buffer overflow
if(APP_Rx_ptr_in >= APP_RX_DATA_SIZE) {
APP_Rx_ptr_in = 0;
}
}
return len;
}
int cdc_getline(char *ptr, int len) {
return rb_getline(&usb_rx_buf, ptr, len);
}
int cdc_is_connected() {
return USB_CDC_is_connected();
}
void cdc_poll() {}