From f3a757550ae5b4f5da067eb36d9f59f3c5f21a55 Mon Sep 17 00:00:00 2001 From: arighi Date: Thu, 23 Sep 2010 17:30:11 +0000 Subject: [PATCH] USB: make use of standard byte order functions git-svn-id: https://src.develer.com/svnoss/bertos/trunk@4279 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/cpu/cortex-m3/drv/usb_stm32.c | 3 ++- bertos/drv/usb.h | 25 ++++--------------------- bertos/drv/usb_keyboard.c | 7 ++++--- bertos/drv/usb_mouse.c | 7 ++++--- bertos/drv/usb_serial.c | 6 +++--- 5 files changed, 17 insertions(+), 31 deletions(-) diff --git a/bertos/cpu/cortex-m3/drv/usb_stm32.c b/bertos/cpu/cortex-m3/drv/usb_stm32.c index a47237e9..a7f9d8c8 100644 --- a/bertos/cpu/cortex-m3/drv/usb_stm32.c +++ b/bertos/cpu/cortex-m3/drv/usb_stm32.c @@ -1308,6 +1308,7 @@ static int usb_get_device_descriptor(int id) #define USB_BUFSIZE (128) static uint8_t usb_cfg_buffer[USB_BUFSIZE]; +STATIC_ASSERT(USB_BUFSIZE < (1 << (sizeof(uint16_t) * 8))); static int usb_get_configuration_descriptor(int id) { @@ -1332,7 +1333,7 @@ static int usb_get_configuration_descriptor(int id) } } ((UsbConfigDesc *)usb_cfg_buffer)->wTotalLength = - usb_cpu_to_le16(p - usb_cfg_buffer); + usb_cpu_to_le16((uint16_t)(p - usb_cfg_buffer)); __usb_ep_write(CTRL_ENP_IN, usb_cfg_buffer, usb_size(p - usb_cfg_buffer, diff --git a/bertos/drv/usb.h b/bertos/drv/usb.h index 3804fdce..02448f2c 100644 --- a/bertos/drv/usb.h +++ b/bertos/drv/usb.h @@ -47,27 +47,10 @@ #include -/* - * Handle CPU endianess - * - * TODO: consider to move this stuff in compiler.h - */ -#define usb_bswap16(x) (((x & 0xff) << 8) | (x >> 8)) -#define usb_bswap32(x) ((usb_bswap16(x & 0xffff) << 16) | usb_bswap16(x >> 16)) - -#if CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN -#define usb_cpu_to_le16(x) (x) -#define usb_le16_to_cpu(x) (x) -#define usb_cpu_to_le32(x) (x) -#define usb_le32_to_cpu(x) (x) -#elif CPU_BYTE_ORDER == CPU_BIG_ENDIAN -#define usb_cpu_to_le16(x) usb_bswap16(x) -#define usb_le16_to_cpu(x) usb_bswap16(x) -#define usb_cpu_to_le32(x) usb_bswap32(x) -#define usb_le32_to_cpu(x) usb_bswap32(x) -#else -#error "unrecognized CPU endianness" -#endif +#define usb_cpu_to_le16(x) cpu_to_le16(x) +#define usb_le16_to_cpu(x) le16_to_cpu(x) +#define usb_cpu_to_le32(x) cpu_to_le32(x) +#define usb_le32_to_cpu(x) le32_to_cpu(x) /* State of a USB device */ enum usb_device_state { diff --git a/bertos/drv/usb_keyboard.c b/bertos/drv/usb_keyboard.c index 89264c8e..69ad6417 100644 --- a/bertos/drv/usb_keyboard.c +++ b/bertos/drv/usb_keyboard.c @@ -155,11 +155,12 @@ static const usb_hid_descriptor_t usb_hid_descriptor = { .bLength = sizeof(usb_hid_descriptor), .bDescriptorType = HID_DT_HID, - .bcdHID = usb_cpu_to_le16(0x0110), + .bcdHID = usb_cpu_to_le16((uint16_t)0x0110), .bCountryCode = 0, .bNumDescriptors = 1, .bDescriptorHidType = HID_DT_REPORT, - .wDescriptorLength = usb_cpu_to_le16(sizeof(hid_report_descriptor)), + .wDescriptorLength = + usb_cpu_to_le16((uint16_t)sizeof(hid_report_descriptor)), }; static const UsbEndpointDesc usb_hid_ep_descriptor = @@ -168,7 +169,7 @@ static const UsbEndpointDesc usb_hid_ep_descriptor = .bDescriptorType = USB_DT_ENDPOINT, .bEndpointAddress = USB_HID_REPORT_EP, .bmAttributes = USB_ENDPOINT_XFER_INT, - .wMaxPacketSize = usb_cpu_to_le16(4), + .wMaxPacketSize = usb_cpu_to_le16((uint16_t)4), .bInterval = 10, /* resolution in ms */ }; diff --git a/bertos/drv/usb_mouse.c b/bertos/drv/usb_mouse.c index 7ab6c7cd..748ffc3b 100644 --- a/bertos/drv/usb_mouse.c +++ b/bertos/drv/usb_mouse.c @@ -144,11 +144,12 @@ static const usb_hid_descriptor_t usb_hid_descriptor = { .bLength = sizeof(usb_hid_descriptor), .bDescriptorType = HID_DT_HID, - .bcdHID = usb_cpu_to_le16(0x0110), + .bcdHID = usb_cpu_to_le16((uint16_t)0x0110), .bCountryCode = 0, .bNumDescriptors = 1, .bDescriptorHidType = HID_DT_REPORT, - .wDescriptorLength = usb_cpu_to_le16(sizeof(hid_report_descriptor)), + .wDescriptorLength = + usb_cpu_to_le16((uint16_t)sizeof(hid_report_descriptor)), }; static const UsbEndpointDesc usb_hid_ep_descriptor = @@ -157,7 +158,7 @@ static const UsbEndpointDesc usb_hid_ep_descriptor = .bDescriptorType = USB_DT_ENDPOINT, .bEndpointAddress = USB_HID_REPORT_EP, .bmAttributes = USB_ENDPOINT_XFER_INT, - .wMaxPacketSize = usb_cpu_to_le16(4), + .wMaxPacketSize = usb_cpu_to_le16((uint16_t)4), .bInterval = 10, /* resolution in ms */ }; diff --git a/bertos/drv/usb_serial.c b/bertos/drv/usb_serial.c index 0a57a7b6..be39fe69 100644 --- a/bertos/drv/usb_serial.c +++ b/bertos/drv/usb_serial.c @@ -114,7 +114,7 @@ static const UsbEndpointDesc usb_serial_ep_report_descriptor = .bDescriptorType = USB_DT_ENDPOINT, .bEndpointAddress = USB_DIR_IN | 1, .bmAttributes = USB_ENDPOINT_XFER_INT, - .wMaxPacketSize = usb_cpu_to_le16(8), + .wMaxPacketSize = usb_cpu_to_le16((uint16_t)8), .bInterval = 1, }; @@ -124,7 +124,7 @@ static const UsbEndpointDesc usb_serial_ep_in_descriptor = .bDescriptorType = USB_DT_ENDPOINT, .bEndpointAddress = USB_DIR_IN | 3, .bmAttributes = USB_ENDPOINT_XFER_BULK, - .wMaxPacketSize = usb_cpu_to_le16(64), + .wMaxPacketSize = usb_cpu_to_le16((uint16_t)64), .bInterval = 0, }; @@ -134,7 +134,7 @@ static const UsbEndpointDesc usb_serial_ep_out_descriptor = .bDescriptorType = USB_DT_ENDPOINT, .bEndpointAddress = USB_DIR_OUT | 2, .bmAttributes = USB_ENDPOINT_XFER_BULK, - .wMaxPacketSize = usb_cpu_to_le16(64), + .wMaxPacketSize = usb_cpu_to_le16((uint16_t)64), .bInterval = 0, }; -- 2.25.1