Skip to content

Commit 2056ad0

Browse files
committed
usbhost: USBHost::fileControlBuf() handle alignment and endianness
Based on great feedback from Martin Kojtal on my previous commit, I have modified my USBHost::fileControlBuf() change to be more portable. https://github.com/adamgreen/mbed/commit/ddb3fbe826f800a62c68ae57b7deb7c4777527d8#commitcomment-3988044 The code now fills in the setupPacket byte array a byte at a time, using bit shifts to extract lower and upper bytes from the 16-bit values so that the code should work on big endian or little endian machines. I also removed the 2-byte alignment attribute from the setupPacket array as it is no longer required.
1 parent c0d7c3f commit 2056ad0

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

libraries/USBHost/USBHost/USBHost.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,14 +1153,12 @@ USB_TYPE USBHost::controlTransfer(USBDeviceConnected * dev, uint8_t requestType,
11531153

11541154
void USBHost::fillControlBuf(uint8_t requestType, uint8_t request, uint16_t value, uint16_t index, int len)
11551155
{
1156-
#ifdef __BIG_ENDIAN
1157-
#error "Must implement BE to LE conv here"
1158-
#endif
11591156
setupPacket[0] = requestType;
11601157
setupPacket[1] = request;
1161-
//We are in LE so it's fine
1162-
uint16_t* setupPacketHalfWords = (uint16_t*)setupPacket;
1163-
setupPacketHalfWords[1] = value;
1164-
setupPacketHalfWords[2] = index;
1165-
setupPacketHalfWords[3] = (uint32_t) len;
1158+
setupPacket[2] = (uint8_t) value;
1159+
setupPacket[3] = (uint8_t) (value >> 8);
1160+
setupPacket[4] = (uint8_t) index;
1161+
setupPacket[5] = (uint8_t) (index >> 8);
1162+
setupPacket[6] = (uint8_t) len;
1163+
setupPacket[7] = (uint8_t) (len >> 8);
11661164
}

libraries/USBHost/USBHost/USBHost.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ class USBHost : public USBHALHost {
252252
#endif
253253

254254
// to store a setup packet
255-
__attribute__((aligned(2))) uint8_t setupPacket[8];
255+
uint8_t setupPacket[8];
256256

257257
typedef struct {
258258
uint8_t event_id;

0 commit comments

Comments
 (0)