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.
    ddb3fbe826 (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.
pull/67/head
Adam Green 2013-09-01 12:19:59 -07:00
parent c0d7c3fb39
commit 2056ad025e
2 changed files with 7 additions and 9 deletions

View File

@ -1153,14 +1153,12 @@ USB_TYPE USBHost::controlTransfer(USBDeviceConnected * dev, uint8_t requestType,
void USBHost::fillControlBuf(uint8_t requestType, uint8_t request, uint16_t value, uint16_t index, int len)
{
#ifdef __BIG_ENDIAN
#error "Must implement BE to LE conv here"
#endif
setupPacket[0] = requestType;
setupPacket[1] = request;
//We are in LE so it's fine
uint16_t* setupPacketHalfWords = (uint16_t*)setupPacket;
setupPacketHalfWords[1] = value;
setupPacketHalfWords[2] = index;
setupPacketHalfWords[3] = (uint32_t) len;
setupPacket[2] = (uint8_t) value;
setupPacket[3] = (uint8_t) (value >> 8);
setupPacket[4] = (uint8_t) index;
setupPacket[5] = (uint8_t) (index >> 8);
setupPacket[6] = (uint8_t) len;
setupPacket[7] = (uint8_t) (len >> 8);
}

View File

@ -252,7 +252,7 @@ private:
#endif
// to store a setup packet
__attribute__((aligned(2))) uint8_t setupPacket[8];
uint8_t setupPacket[8];
typedef struct {
uint8_t event_id;