-
Notifications
You must be signed in to change notification settings - Fork 518
/
Copy pathusb_hal.cpp
183 lines (152 loc) · 5.43 KB
/
usb_hal.cpp
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
176
177
178
179
180
181
182
183
/*
* Copyright (c) 2018 Particle Industries, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "usb_hal.h"
#include "usb_hal_cdc.h"
#include "usb_settings.h"
#include <mutex>
#include <nrf52840.h>
#include <nrf_nvic.h>
#ifdef USB_CDC_ENABLE
void HAL_USB_Init(void) {
usb_hal_init();
}
void HAL_USB_Attach() {
usb_hal_attach();
}
void HAL_USB_Detach() {
usb_hal_detach();
}
void HAL_USB_USART_Init(HAL_USB_USART_Serial serial, const HAL_USB_USART_Config* config) {
/*
* Note: This function will call into SD API, which requires the SVC interrupt to be enabled,
* otherwise, device runs into hardfault. Without the workaround, When LOG_FROM_ISR is defined,
* device will run into hardfault due to the following code in spark_wiring_logging.cpp:
*
* if (stream_ == &Serial && Network.listening()) {
* return; // Do not mix logging and serial console output
* }
*/
int st = __get_BASEPRI();
__set_BASEPRI(5 << (8 - __NVIC_PRIO_BITS)); // The SVC interrupt priority is 4
if ((config == NULL) ||
(config && (config->rx_buffer == NULL ||
config->rx_buffer_size == 0 ||
config->tx_buffer == NULL ||
config->tx_buffer_size == 0)))
{
static uint8_t *p_tx_buffer = NULL;
static uint8_t *p_rx_buffer = NULL;
if (p_tx_buffer == NULL) {
p_tx_buffer = (uint8_t *)malloc(USB_TX_BUFFER_SIZE);
}
if (p_rx_buffer == NULL) {
p_rx_buffer = (uint8_t *)malloc(USB_RX_BUFFER_SIZE);
}
usb_uart_init(p_rx_buffer, USB_RX_BUFFER_SIZE, p_tx_buffer, USB_TX_BUFFER_SIZE);
} else {
usb_uart_init(config->rx_buffer, config->rx_buffer_size, config->tx_buffer, config->tx_buffer_size);
}
__set_BASEPRI(st);
}
void HAL_USB_USART_Begin(HAL_USB_USART_Serial serial, uint32_t baud, void *reserved) {
HAL_USB_Attach();
usb_uart_set_baudrate(baud);
}
void HAL_USB_USART_End(HAL_USB_USART_Serial serial) {
HAL_USB_Detach();
}
unsigned int HAL_USB_USART_Baud_Rate(HAL_USB_USART_Serial serial) {
return usb_uart_get_baudrate();
}
int32_t HAL_USB_USART_Available_Data(HAL_USB_USART_Serial serial) {
return usb_uart_available_rx_data();
}
int32_t HAL_USB_USART_Available_Data_protected(HAL_USB_USART_Serial serial) {
CHECK_SECURITY_MODE_PROTECTED();
return HAL_USB_USART_Available_Data(serial);
}
int32_t HAL_USB_USART_Available_Data_For_Write(HAL_USB_USART_Serial serial) {
return usb_uart_available_tx_data();
}
int32_t HAL_USB_USART_Available_Data_For_Write_protected(HAL_USB_USART_Serial serial) {
CHECK_SECURITY_MODE_PROTECTED();
return HAL_USB_USART_Available_Data_For_Write(serial);
}
int32_t HAL_USB_USART_Receive_Data(HAL_USB_USART_Serial serial, uint8_t peek) {
if (usb_uart_available_rx_data() == 0) {
return -1;
}
if (peek) {
return usb_uart_peek_rx_data(0);
} else {
return usb_uart_get_rx_data();
}
}
int32_t HAL_USB_USART_Receive_Data_protected(HAL_USB_USART_Serial serial, uint8_t peek) {
CHECK_SECURITY_MODE_PROTECTED();
return HAL_USB_USART_Receive_Data(serial, peek);
}
int32_t HAL_USB_USART_Send_Data(HAL_USB_USART_Serial serial, uint8_t data) {
return usb_uart_send(&data, 1);
}
int32_t HAL_USB_USART_Send_Data_protected(HAL_USB_USART_Serial serial, uint8_t data) {
CHECK_SECURITY_MODE_PROTECTED();
return HAL_USB_USART_Send_Data(serial, data);
}
void HAL_USB_USART_Flush_Data(HAL_USB_USART_Serial serial) {
usb_uart_flush_tx_data();
}
void HAL_USB_USART_Flush_Data_protected(HAL_USB_USART_Serial serial) {
if (security_mode_get(NULL) == MODULE_INFO_SECURITY_MODE_PROTECTED) {
return;
}
HAL_USB_USART_Flush_Data(serial);
}
bool HAL_USB_USART_Is_Enabled(HAL_USB_USART_Serial serial) {
return usb_hal_is_enabled();
}
bool HAL_USB_USART_Is_Connected(HAL_USB_USART_Serial serial) {
return usb_hal_is_connected();
}
#endif
void USB_USART_LineCoding_BitRate_Handler(void (*handler)(uint32_t bitRate))
{
// Old USB API, just for compatibility in main.cpp
// Enable Serial by default
HAL_USB_USART_LineCoding_BitRate_Handler(handler, NULL);
}
int32_t HAL_USB_USART_LineCoding_BitRate_Handler(void (*handler)(uint32_t bitRate), void* reserved) {
// Enable Serial by default
HAL_USB_USART_Init(HAL_USB_USART_SERIAL, nullptr);
HAL_USB_USART_Begin(HAL_USB_USART_SERIAL, 9600, NULL);
usb_hal_set_bit_rate_changed_handler(handler);
return 0;
}
int32_t USB_USART_Flush_Output(unsigned timeout, void* reserved)
{
return 0;
}
HAL_USB_State HAL_USB_Get_State(void* reserved) {
return usb_hal_get_state();
}
int HAL_USB_Set_State_Change_Callback(HAL_USB_State_Callback cb, void* context, void* reserved) {
usb_hal_set_state_change_callback(cb, context, reserved);
return 0;
}