Skip to content

Commit 9a41831

Browse files
authored
Merge pull request #2694 from feaser/keil_weak_fix
Additional fix related to issue #1018. Corrects the usage of TU_ATTR_WEAK for the Keil compiler
2 parents dcf133b + 8183433 commit 9a41831

File tree

2 files changed

+63
-47
lines changed

2 files changed

+63
-47
lines changed

src/device/usbd.c

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,46 @@
4646
// Weak stubs: invoked if no strong implementation is available
4747
//--------------------------------------------------------------------+
4848
TU_ATTR_WEAK void tud_event_hook_cb(uint8_t rhport, uint32_t eventid, bool in_isr) {
49-
(void)rhport;
50-
(void)eventid;
51-
(void)in_isr;
49+
(void) rhport;
50+
(void) eventid;
51+
(void) in_isr;
5252
}
5353

5454
TU_ATTR_WEAK void tud_sof_cb(uint32_t frame_count) {
55-
(void)frame_count;
55+
(void) frame_count;
56+
}
57+
58+
TU_ATTR_WEAK uint8_t const* tud_descriptor_bos_cb(void) {
59+
return NULL;
60+
}
61+
62+
TU_ATTR_WEAK uint8_t const* tud_descriptor_device_qualifier_cb(void) {
63+
return NULL;
64+
}
65+
66+
TU_ATTR_WEAK uint8_t const* tud_descriptor_other_speed_configuration_cb(uint8_t index) {
67+
(void) index;
68+
return NULL;
69+
}
70+
71+
TU_ATTR_WEAK void tud_mount_cb(void) {
72+
}
73+
74+
TU_ATTR_WEAK void tud_umount_cb(void) {
75+
}
76+
77+
TU_ATTR_WEAK void tud_suspend_cb(bool remote_wakeup_en) {
78+
(void) remote_wakeup_en;
79+
}
80+
81+
TU_ATTR_WEAK void tud_resume_cb(void) {
82+
}
83+
84+
TU_ATTR_WEAK bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const* request) {
85+
(void) rhport;
86+
(void) stage;
87+
(void) request;
88+
return false;
5689
}
5790

5891
TU_ATTR_WEAK bool dcd_deinit(uint8_t rhport) {
@@ -557,7 +590,7 @@ void tud_task_ext(uint32_t timeout_ms, bool in_isr) {
557590
case DCD_EVENT_UNPLUGGED:
558591
TU_LOG_USBD("\r\n");
559592
usbd_reset(event.rhport);
560-
if (tud_umount_cb) tud_umount_cb();
593+
tud_umount_cb();
561594
break;
562595

563596
case DCD_EVENT_SETUP_RECEIVED:
@@ -617,7 +650,7 @@ void tud_task_ext(uint32_t timeout_ms, bool in_isr) {
617650
// e.g suspend -> resume -> unplug/plug. Skip suspend/resume if not connected
618651
if (_usbd_dev.connected) {
619652
TU_LOG_USBD(": Remote Wakeup = %u\r\n", _usbd_dev.remote_wakeup_en);
620-
if (tud_suspend_cb) tud_suspend_cb(_usbd_dev.remote_wakeup_en);
653+
tud_suspend_cb(_usbd_dev.remote_wakeup_en);
621654
} else {
622655
TU_LOG_USBD(" Skipped\r\n");
623656
}
@@ -626,7 +659,7 @@ void tud_task_ext(uint32_t timeout_ms, bool in_isr) {
626659
case DCD_EVENT_RESUME:
627660
if (_usbd_dev.connected) {
628661
TU_LOG_USBD("\r\n");
629-
if (tud_resume_cb) tud_resume_cb();
662+
tud_resume_cb();
630663
} else {
631664
TU_LOG_USBD(" Skipped\r\n");
632665
}
@@ -675,8 +708,6 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const
675708

676709
// Vendor request
677710
if ( p_request->bmRequestType_bit.type == TUSB_REQ_TYPE_VENDOR ) {
678-
TU_VERIFY(tud_vendor_control_xfer_cb);
679-
680711
usbd_control_set_complete_callback(tud_vendor_control_xfer_cb);
681712
return tud_vendor_control_xfer_cb(rhport, CONTROL_STAGE_SETUP, p_request);
682713
}
@@ -758,9 +789,9 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const
758789
_usbd_dev.cfg_num = 0;
759790
return false;
760791
}
761-
if ( tud_mount_cb ) tud_mount_cb();
792+
tud_mount_cb();
762793
} else {
763-
if ( tud_umount_cb ) tud_umount_cb();
794+
tud_umount_cb();
764795
}
765796
}
766797

@@ -1027,39 +1058,34 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const
10271058

10281059
switch(desc_type)
10291060
{
1030-
case TUSB_DESC_DEVICE:
1031-
{
1061+
case TUSB_DESC_DEVICE: {
10321062
TU_LOG_USBD(" Device\r\n");
10331063

10341064
void* desc_device = (void*) (uintptr_t) tud_descriptor_device_cb();
1065+
TU_ASSERT(desc_device);
10351066

10361067
// Only response with exactly 1 Packet if: not addressed and host requested more data than device descriptor has.
10371068
// This only happens with the very first get device descriptor and EP0 size = 8 or 16.
10381069
if ((CFG_TUD_ENDPOINT0_SIZE < sizeof(tusb_desc_device_t)) && !_usbd_dev.addressed &&
1039-
((tusb_control_request_t const*) p_request)->wLength > sizeof(tusb_desc_device_t))
1040-
{
1070+
((tusb_control_request_t const*) p_request)->wLength > sizeof(tusb_desc_device_t)) {
10411071
// Hack here: we modify the request length to prevent usbd_control response with zlp
10421072
// since we are responding with 1 packet & less data than wLength.
10431073
tusb_control_request_t mod_request = *p_request;
10441074
mod_request.wLength = CFG_TUD_ENDPOINT0_SIZE;
10451075

10461076
return tud_control_xfer(rhport, &mod_request, desc_device, CFG_TUD_ENDPOINT0_SIZE);
1047-
}else
1048-
{
1077+
}else {
10491078
return tud_control_xfer(rhport, p_request, desc_device, sizeof(tusb_desc_device_t));
10501079
}
10511080
}
10521081
// break; // unreachable
10531082

1054-
case TUSB_DESC_BOS:
1055-
{
1083+
case TUSB_DESC_BOS: {
10561084
TU_LOG_USBD(" BOS\r\n");
10571085

10581086
// requested by host if USB > 2.0 ( i.e 2.1 or 3.x )
1059-
if (!tud_descriptor_bos_cb) return false;
1060-
10611087
uintptr_t desc_bos = (uintptr_t) tud_descriptor_bos_cb();
1062-
TU_ASSERT(desc_bos);
1088+
TU_VERIFY(desc_bos);
10631089

10641090
// Use offsetof to avoid pointer to the odd/misaligned address
10651091
uint16_t const total_len = tu_le16toh( tu_unaligned_read16((const void*) (desc_bos + offsetof(tusb_desc_bos_t, wTotalLength))) );
@@ -1069,24 +1095,20 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const
10691095
// break; // unreachable
10701096

10711097
case TUSB_DESC_CONFIGURATION:
1072-
case TUSB_DESC_OTHER_SPEED_CONFIG:
1073-
{
1098+
case TUSB_DESC_OTHER_SPEED_CONFIG: {
10741099
uintptr_t desc_config;
10751100

1076-
if ( desc_type == TUSB_DESC_CONFIGURATION )
1077-
{
1101+
if ( desc_type == TUSB_DESC_CONFIGURATION ) {
10781102
TU_LOG_USBD(" Configuration[%u]\r\n", desc_index);
10791103
desc_config = (uintptr_t) tud_descriptor_configuration_cb(desc_index);
1080-
}else
1081-
{
1104+
TU_ASSERT(desc_config);
1105+
}else {
10821106
// Host only request this after getting Device Qualifier descriptor
10831107
TU_LOG_USBD(" Other Speed Configuration\r\n");
1084-
TU_VERIFY( tud_descriptor_other_speed_configuration_cb );
10851108
desc_config = (uintptr_t) tud_descriptor_other_speed_configuration_cb(desc_index);
1109+
TU_VERIFY(desc_config);
10861110
}
10871111

1088-
TU_ASSERT(desc_config);
1089-
10901112
// Use offsetof to avoid pointer to the odd/misaligned address
10911113
uint16_t const total_len = tu_le16toh( tu_unaligned_read16((const void*) (desc_config + offsetof(tusb_desc_configuration_t, wTotalLength))) );
10921114

@@ -1107,16 +1129,10 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const
11071129
}
11081130
// break; // unreachable
11091131

1110-
case TUSB_DESC_DEVICE_QUALIFIER:
1111-
{
1132+
case TUSB_DESC_DEVICE_QUALIFIER: {
11121133
TU_LOG_USBD(" Device Qualifier\r\n");
1113-
1114-
TU_VERIFY( tud_descriptor_device_qualifier_cb );
1115-
11161134
uint8_t const* desc_qualifier = tud_descriptor_device_qualifier_cb();
11171135
TU_VERIFY(desc_qualifier);
1118-
1119-
// first byte of descriptor is its size
11201136
return tud_control_xfer(rhport, p_request, (void*) (uintptr_t) desc_qualifier, tu_desc_len(desc_qualifier));
11211137
}
11221138
// break; // unreachable

src/device/usbd.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ bool tud_control_xfer(uint8_t rhport, tusb_control_request_t const * request, vo
109109
bool tud_control_status(uint8_t rhport, tusb_control_request_t const * request);
110110

111111
//--------------------------------------------------------------------+
112-
// Application Callbacks (WEAK is optional)
112+
// Application Callbacks
113113
//--------------------------------------------------------------------+
114114

115115
// Invoked when received GET DEVICE DESCRIPTOR request
@@ -126,31 +126,31 @@ uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid);
126126

127127
// Invoked when received GET BOS DESCRIPTOR request
128128
// Application return pointer to descriptor
129-
TU_ATTR_WEAK uint8_t const * tud_descriptor_bos_cb(void);
129+
uint8_t const * tud_descriptor_bos_cb(void);
130130

131131
// Invoked when received GET DEVICE QUALIFIER DESCRIPTOR request
132132
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete.
133133
// device_qualifier descriptor describes information about a high-speed capable device that would
134134
// change if the device were operating at the other speed. If not highspeed capable stall this request.
135-
TU_ATTR_WEAK uint8_t const* tud_descriptor_device_qualifier_cb(void);
135+
uint8_t const* tud_descriptor_device_qualifier_cb(void);
136136

137137
// Invoked when received GET OTHER SEED CONFIGURATION DESCRIPTOR request
138138
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
139139
// Configuration descriptor in the other speed e.g if high speed then this is for full speed and vice versa
140-
TU_ATTR_WEAK uint8_t const* tud_descriptor_other_speed_configuration_cb(uint8_t index);
140+
uint8_t const* tud_descriptor_other_speed_configuration_cb(uint8_t index);
141141

142142
// Invoked when device is mounted (configured)
143-
TU_ATTR_WEAK void tud_mount_cb(void);
143+
void tud_mount_cb(void);
144144

145145
// Invoked when device is unmounted
146-
TU_ATTR_WEAK void tud_umount_cb(void);
146+
void tud_umount_cb(void);
147147

148148
// Invoked when usb bus is suspended
149149
// Within 7ms, device must draw an average of current less than 2.5 mA from bus
150-
TU_ATTR_WEAK void tud_suspend_cb(bool remote_wakeup_en);
150+
void tud_suspend_cb(bool remote_wakeup_en);
151151

152152
// Invoked when usb bus is resumed
153-
TU_ATTR_WEAK void tud_resume_cb(void);
153+
void tud_resume_cb(void);
154154

155155
// Invoked when there is a new usb event, which need to be processed by tud_task()/tud_task_ext()
156156
void tud_event_hook_cb(uint8_t rhport, uint32_t eventid, bool in_isr);
@@ -159,7 +159,7 @@ void tud_event_hook_cb(uint8_t rhport, uint32_t eventid, bool in_isr);
159159
void tud_sof_cb(uint32_t frame_count);
160160

161161
// Invoked when received control request with VENDOR TYPE
162-
TU_ATTR_WEAK bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
162+
bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
163163

164164
//--------------------------------------------------------------------+
165165
// Binary Device Object Store (BOS) Descriptor Templates

0 commit comments

Comments
 (0)