-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUSBD_USER_CORE.c
169 lines (144 loc) · 5.21 KB
/
USBD_USER_CORE.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
/*------------------------------------------------------------------------------
* MDK Middleware - Component ::USB:Device
* Copyright (c) 2004-2016 ARM Germany GmbH. All rights reserved.
*------------------------------------------------------------------------------
* Name: USBD_User_CustomClass_0.c
* Purpose: USB Device Custom Class User module
* Rev.: V6.7.3
*----------------------------------------------------------------------------*/
/*
* USBD_User_CustomClass_0.c is a code template for the Custom Class 0
* class request handling. It allows user to handle all Custom Class class
* requests.
*
* Uncomment "Example code" lines to see example that receives data on
* Endpoint 1 OUT and echoes it back on Endpoint 1 IN.
* To try the example you also have to enable Bulk Endpoint 1 IN/OUT in Custom
* Class configuration in USBD_Config_CustomClass_0.h file.
*/
/**
* \addtogroup usbd_custom_classFunctions
*
*/
//! [code_USBD_User_CustomClass]
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "rl_usb.h"
#include "Driver_USBD.h"
#include "stdlib.h"
#include "Driver_CAN.h"
#include "main.h"
extern queue_t *q_from_host;
// Variable declaration
static bool data_received, send_active;
static uint32_t data_len_received;
static uint16_t bulk_max_packet_size;
static uint8_t bulk_out_buf[512];
static uint8_t bulk_in_buf [512];
// \brief Callback function called during USBD_Initialize to initialize the USB Custom class instance
void USBD_CustomClass0_Initialize (void) {
// Clear buffers
memset((void *)bulk_out_buf, 0, 512U);
memset((void *)bulk_in_buf, 0, 512U);
data_received = false;
data_len_received = 0U;
send_active = false;
}
// \brief Callback function called during USBD_Uninitialize to de-initialize the USB Custom class instance
void USBD_CustomClass0_Uninitialize (void) {
data_received = false;
data_len_received = 0U;
send_active = false;
}
// \brief Callback function called upon USB Bus Reset signaling
void USBD_CustomClass0_Reset (void) {
data_received = false;
data_len_received = 0U;
send_active = false;
}
// \brief Callback function called when Endpoint Start was requested (by activating interface or configuration)
// \param[in] ep_addr endpoint address.
void USBD_CustomClass0_EndpointStart (uint8_t ep_addr) {
if (USBD_GetState(0U).speed == USB_SPEED_HIGH) {
bulk_max_packet_size = 512U;
} else {
bulk_max_packet_size = 64U;
}
// Start reception on Endpoint 1 OUT
if (ep_addr == USB_ENDPOINT_OUT(1)) {
USBD_EndpointRead(0U, USB_ENDPOINT_OUT(1), bulk_out_buf, bulk_max_packet_size);
}
}
// \brief Callback function called when DATA was sent or received on Endpoint 1
// \param[in] event event on Endpoint:
// - ARM_USBD_EVENT_OUT = data OUT received
// - ARM_USBD_EVENT_IN = data IN sent
void USBD_CustomClass0_Endpoint1_Event (uint32_t event) {
if (event & ARM_USBD_EVENT_IN) { // IN event
send_active = false;
}
if (event & ARM_USBD_EVENT_OUT) { // OUT event
data_len_received = USBD_EndpointReadGetResult(0U, USB_ENDPOINT_OUT(1));
data_received = true;
}
if ((data_received) && (!send_active)) {
// Copy received data bytes to transmit buffer
memcpy((void *)bulk_in_buf, (void *)bulk_out_buf, data_len_received);
// Transmit back data bytes
send_active = true;
USBD_EndpointWrite(0U, USB_ENDPOINT_IN(1) , bulk_in_buf, data_len_received);
struct gs_host_frame *frame;
frame = calloc(1, sizeof(struct gs_host_frame));
memcpy(frame->data, bulk_out_buf, 8);
frame->can_id = ARM_CAN_EXTENDED_ID(0x00000033);
frame->can_dlc = 0x00;
queue_push_back(q_from_host, frame);
// Restart reception on Endpoint 1 OUT
data_received = false;
USBD_EndpointRead (0U, USB_ENDPOINT_OUT(1), bulk_out_buf, bulk_max_packet_size);
}
}
void USBD_CustomClass0_Endpoint2_Event (uint32_t event) {
// Handle Endpoint 2 events
}
void USBD_CustomClass0_Endpoint3_Event (uint32_t event) {
// Handle Endpoint 3 events
}
void USBD_CustomClass0_Endpoint4_Event (uint32_t event) {
// Handle Endpoint 4 events
}
void USBD_CustomClass0_Endpoint5_Event (uint32_t event) {
// Handle Endpoint 5 events
}
void USBD_CustomClass0_Endpoint6_Event (uint32_t event) {
// Handle Endpoint 6 events
}
void USBD_CustomClass0_Endpoint7_Event (uint32_t event) {
// Handle Endpoint 7 events
}
void USBD_CustomClass0_Endpoint8_Event (uint32_t event) {
// Handle Endpoint 8 events
}
void USBD_CustomClass0_Endpoint9_Event (uint32_t event) {
// Handle Endpoint 9 events
}
void USBD_CustomClass0_Endpoint10_Event (uint32_t event) {
// Handle Endpoint 10 events
}
void USBD_CustomClass0_Endpoint11_Event (uint32_t event) {
// Handle Endpoint 11 events
}
void USBD_CustomClass0_Endpoint12_Event (uint32_t event) {
// Handle Endpoint 12 events
}
void USBD_CustomClass0_Endpoint13_Event (uint32_t event) {
// Handle Endpoint 13 events
}
void USBD_CustomClass0_Endpoint14_Event (uint32_t event) {
// Handle Endpoint 14 events
}
void USBD_CustomClass0_Endpoint15_Event (uint32_t event) {
// Handle Endpoint 15 events
}
//! [code_USBD_User_CustomClass]