Skip to content

Commit 6f40b52

Browse files
committed
Major refactoring
- Configuration is now implementation dependant instead of global - Abstraction of common platform operations (time & memcpy) - Added some Bugfixes to usb implementation - Added timeout for TX buffer to avoid deadlocks / endless waiting
1 parent 593e2b2 commit 6f40b52

21 files changed

+869
-334
lines changed

.vscode/settings.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
"usb.h": "c",
77
"stdint.h": "c",
88
"hid_config.h": "c",
9-
"platform.h": "c"
9+
"platform.h": "c",
10+
"ncm_netif.h": "c",
11+
"usb_config.h": "c",
12+
"ncm_config.h": "c"
1013
}
1114
}

Inc/cdc/cdc_config.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef __CDC_CONFIG_H__
2+
#define __CDC_CONFIG_H__
3+
4+
#include "usb.h"
5+
#include "platform.h"
6+
7+
USB_Implementation CDC_GetImplementation();
8+
9+
#endif

Inc/hid/hid_config.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1-
#include "usb_config.h"
1+
#ifndef __HID_CONFIG_H__
2+
#define __HID_CONFIG_H__
23

3-
USB_Implementation HID_GetImplementation();
4+
#include "usb.h"
5+
6+
USB_Implementation HID_GetImplementation();
7+
8+
#endif

Inc/ncm/ncm_config.h

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef __NCM_CONFIG_H__
2+
#define __NCM_CONFIG_H__
3+
4+
#include "usb.h"
5+
#include "platform.h"
6+
7+
void NCM_Init();
8+
void NCM_Loop();
9+
USB_Implementation NCM_GetImplementation();
10+
11+
#endif
File renamed without changes.
File renamed without changes.

Inc/platform.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,12 @@
33

44
#define STM32G441xx
55
#include "stm32g4xx.h"
6+
#include <stddef.h>
67

7-
#endif
8+
unsigned int sys_jiffies();
9+
unsigned int sys_now();
10+
void delay_ms(unsigned int ms);
11+
12+
void Systick_Init();
13+
14+
#endif

Inc/usb.h

+42-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef __USB_H
22
#define __USB_H
33

4+
#include "usb_config.h"
5+
46
typedef struct {
57
unsigned char RequestType;
68
unsigned char Request;
@@ -20,16 +22,38 @@ typedef struct {
2022
unsigned char RxBufferSize;
2123
unsigned char TxBufferSize;
2224
unsigned short Type;
23-
void (*RxCallback)(char ep, short length);
24-
void (*TxCallback)(char ep, short length);
25+
void (*RxCallback)(unsigned char ep, short length);
26+
void (*TxCallback)(unsigned char ep, short length);
2527
} USB_CONFIG_EP;
2628

29+
typedef struct {
30+
const USB_DESCRIPTOR_DEVICE *DeviceDescriptor;
31+
const unsigned char *ConfigDescriptor;
32+
unsigned short ConfigDescriptorLength;
33+
34+
const USB_CONFIG_EP* Endpoints;
35+
unsigned short NumEndpoints;
36+
unsigned short NumInterfaces;
37+
38+
char *(*GetString)(char, short, short *);
39+
char *(*GetOSDescriptor)(short *);
40+
41+
char (*SetupPacket_Handler)(USB_SETUP_PACKET *, const unsigned char *, short);
42+
void (*ResetInterface_Handler)(char, char);
43+
44+
void (*Suspend_Handler)();
45+
void (*Wakeup_Handler)();
46+
} USB_Implementation;
47+
2748
#define USB_OK 0
2849
#define USB_BUSY 1
2950
#define USB_ERR 2
3051

52+
// Disable this define to disable the timeout feature
53+
#define USB_TXTIMEOUT 50
54+
3155
/// @brief Initialize all USB related stuff
32-
void USB_Init();
56+
void USB_Init(USB_Implementation impl);
3357
/// @brief Low Priority handler for most interrupts
3458
void USB_LP_IRQHandler();
3559
/// @brief High Priority handler for Burst and Isochronous transfers
@@ -40,17 +64,29 @@ void USB_HP_IRQHandler();
4064
/// @param buffer A pointer to the buffer containing the data
4165
/// @param length The number of bytes to sent
4266
/// @remark Will automatically split the transmission into multiple chunks if necessary
43-
void USB_Transmit(char ep, char* buffer, short length);
67+
void USB_Transmit(unsigned char ep, const unsigned char* buffer, short length);
4468
/// @brief Whether there is currently any unfinished transfer running
4569
/// @param ep The endpoint to check
4670
/// @remark Do not busy-wait on this during reception. It will stall the USB-ISR
47-
char USB_IsTransmitPending(char ep);
71+
char USB_IsTransmitPending(unsigned char ep);
4872
/// @brief Get data out of the reception buffers
4973
/// @param ep The endpoint id to fetch data from
5074
/// @param buffer The target buffer to write to
5175
/// @param length The length of the buffer. Will contain the number of bytes read
52-
void USB_Fetch(char ep, char* buffer, short *length);
76+
void USB_Fetch(unsigned char ep, unsigned char* buffer, short *length);
5377
/// @brief Configure an endpoint
5478
void USB_SetEPConfig(USB_CONFIG_EP config);
5579

80+
/// @brief Set USB implementation details
81+
/// @param impl The function calls & settings to use
82+
void USB_SetImplementation(USB_Implementation impl);
83+
84+
/// @brief Compose the complete USB descriptor
85+
/// @param buffer the target to build the descriptor into
86+
/// @param size the size of the target buffer for sanity checks
87+
/// @param num The number of parts
88+
/// @param parts A list of parts
89+
/// @returns Length of the final descriptor in the buffer
90+
unsigned short USB_BuildDescriptor(unsigned char *buffer, unsigned short size, unsigned char num, const void **parts);
91+
5692
#endif

Inc/usb_config.h

+24-44
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef __USB_CONFIG_H
22
#define __USB_CONFIG_H
33

4-
#include "usb.h"
54
#include "platform.h"
65

76
#ifndef __weak
@@ -101,6 +100,27 @@ typedef struct {
101100
unsigned char SubInterface0;
102101
} USB_DESC_FUNC_UNION1;
103102

103+
// ECM120 Table 3
104+
typedef struct {
105+
unsigned char Length;
106+
unsigned char Type;
107+
unsigned char SubType;
108+
unsigned char strMacAddress;
109+
unsigned int EthernetStatistics;
110+
unsigned short MaxSegmentSize;
111+
unsigned short NumberMcFilters;
112+
unsigned char NumberPowerFilters;
113+
} USB_DESC_FUNC_ECM;
114+
115+
// NCM10 Table 5-2
116+
typedef struct {
117+
unsigned char Length;
118+
unsigned char Type;
119+
unsigned char SubType;
120+
unsigned short NcmVersion;
121+
unsigned char NetworkCapabilities;
122+
} USB_DESC_FUNC_NCM;
123+
104124
// PSTN120 Table 3
105125
typedef struct {
106126
unsigned char Length;
@@ -131,48 +151,8 @@ typedef struct {
131151

132152
#pragma pack()
133153

134-
typedef struct {
135-
USB_DESCRIPTOR_DEVICE *(*GetDeviceDescriptor)();
136-
char *(*GetConfigDescriptor)(short *);
137-
char *(*GetString)(char, short, short *);
138-
char *(*GetOSDescriptor)(short *);
139-
void (*ConfigureEndpoints)();
140-
} USB_Implementation;
141-
142-
/// @brief Set config implementation for example flexibility
143-
/// @param impl The function calls to use
144-
void USB_SetImplementation(USB_Implementation impl);
145-
146-
/// @brief Get the device descriptor
147-
USB_DESCRIPTOR_DEVICE *USB_GetDeviceDescriptor();
148-
/// @brief Get the complete descriptor
149-
/// @param length A pointer which will contain the length of the descriptor
150-
char *USB_GetConfigDescriptor(short *length);
151-
/// @brief Get a string from the string table (str... entries)
152-
/// @param index The index of the string
153-
/// @param lcid The language code of the string
154-
/// @param length Will contain the length of the string
155-
char *USB_GetString(char index, short lcid, short *length);
156-
/// @brief Get the OS Descriptor (String descriptor at 0xEE)
157-
/// @param length Will contain the length of the descriptor
158-
char *USB_GetOSDescriptor(short *length);
159-
/// @brief Configure all endpoints used by the configuration
160-
void USB_ConfigureEndpoints();
161-
162-
/// @brief Handle a setup packet that is targeted at an interface or class
163-
/// @param setup The setup packet
164-
/// @param data A pointer to data that was sent with the setup
165-
/// @param length The length of the data
166-
char USB_HandleClassSetup(USB_SETUP_PACKET *setup, char *data, short length);
167-
168-
/// @brief Called when the host triggers a SetInterface to choose an alternate id
169-
/// @param interface The interface id that was triggered
170-
/// @param alternateId The new alternate id that was activated
171-
void USB_ResetClass(char interface, char alternateId);
172-
173-
/// @brief Suspend the device and go into low power mode
174-
void USB_SuspendDevice();
175-
/// @brief Reactivate the device
176-
void USB_WakeupDevice();
154+
#define USB_SelfPowered 0
155+
#define USB_NumEndpoints 8
156+
#define USB_MaxControlData 64
177157

178158
#endif

0 commit comments

Comments
 (0)