From 3e6d911ce9c01729af3b5d3093ccba250dfdfe2e Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 14 May 2019 12:54:29 +0700 Subject: [PATCH] more clean up use inline bit funciton instead of macros --- hw/bsp/ea4088qs/board_ea4088qs.c | 2 +- hw/bsp/ea4357/board_ea4357.c | 2 +- hw/bsp/lpcxpresso1769/board_lpcxpresso1769.c | 2 +- hw/bsp/mcb1800/board_mcb1800.c | 2 +- src/class/cdc/cdc_device.c | 4 ++-- src/class/msc/msc_device.c | 4 ++-- src/common/tusb_common.h | 20 +++------------- src/common/tusb_compiler.h | 24 ++++++++++++------- src/device/usbd.c | 4 ++-- src/device/usbd.h | 2 +- src/host/hub.c | 4 ++-- src/portable/nordic/nrf5x/dcd_nrf5x.c | 8 +++---- .../nxp/lpc11_13_15/dcd_lpc11_13_15.c | 6 ++--- src/portable/nxp/lpc17_40/dcd_lpc17_40.c | 4 ++-- src/portable/nxp/lpc18_43/dcd_lpc18_43.c | 2 +- 15 files changed, 41 insertions(+), 49 deletions(-) diff --git a/hw/bsp/ea4088qs/board_ea4088qs.c b/hw/bsp/ea4088qs/board_ea4088qs.c index 5c89937ec..35baa1a03 100644 --- a/hw/bsp/ea4088qs/board_ea4088qs.c +++ b/hw/bsp/ea4088qs/board_ea4088qs.c @@ -123,7 +123,7 @@ void board_led_write(bool state) #if 0 static bool button_read(uint8_t id) { -// return !TU_BIT_TEST( GPIO_ReadValue(buttons[id].gpio_port), buttons[id].gpio_pin ); // button is active low +// return !tu_bit_test( GPIO_ReadValue(buttons[id].gpio_port), buttons[id].gpio_pin ); // button is active low } #endif diff --git a/hw/bsp/ea4357/board_ea4357.c b/hw/bsp/ea4357/board_ea4357.c index f9ff4d1ca..d02a58b47 100644 --- a/hw/bsp/ea4357/board_ea4357.c +++ b/hw/bsp/ea4357/board_ea4357.c @@ -265,7 +265,7 @@ void board_led_write(bool state) #if 0 static bool button_read(uint8_t id) { -// return !TU_BIT_TEST( GPIO_ReadValue(buttons[id].gpio_port), buttons[id].gpio_pin ); // button is active low +// return !tu_bit_test( GPIO_ReadValue(buttons[id].gpio_port), buttons[id].gpio_pin ); // button is active low } #endif diff --git a/hw/bsp/lpcxpresso1769/board_lpcxpresso1769.c b/hw/bsp/lpcxpresso1769/board_lpcxpresso1769.c index c91461e80..d0c59e089 100644 --- a/hw/bsp/lpcxpresso1769/board_lpcxpresso1769.c +++ b/hw/bsp/lpcxpresso1769/board_lpcxpresso1769.c @@ -175,7 +175,7 @@ void board_led_write(bool state) #if 0 static bool button_read(uint8_t id) { -// return !TU_BIT_TEST( GPIO_ReadValue(buttons[id].port), buttons[id].pin ); // button is active low +// return !tu_bit_test( GPIO_ReadValue(buttons[id].port), buttons[id].pin ); // button is active low return false; } #endif diff --git a/hw/bsp/mcb1800/board_mcb1800.c b/hw/bsp/mcb1800/board_mcb1800.c index 6b7655cbc..85fcc2bfb 100644 --- a/hw/bsp/mcb1800/board_mcb1800.c +++ b/hw/bsp/mcb1800/board_mcb1800.c @@ -188,7 +188,7 @@ void board_led_write(bool state) static bool button_read(uint8_t id) { (void) id; -// return !TU_BIT_TEST( GPIO_ReadValue(buttons[id].gpio_port), buttons[id].gpio_pin ); // button is active low +// return !tu_bit_test( GPIO_ReadValue(buttons[id].gpio_port), buttons[id].gpio_pin ); // button is active low return false; } */ diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index ceaac72c1..81ce2e432 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -99,7 +99,7 @@ static void _prep_out_transaction (uint8_t itf) bool tud_cdc_n_connected(uint8_t itf) { // DTR (bit 0) active is considered as connected - return tud_ready() && TU_BIT_TEST(_cdcd_itf[itf].line_state, 0); + return tud_ready() && tu_bit_test(_cdcd_itf[itf].line_state, 0); } uint8_t tud_cdc_n_get_line_state (uint8_t itf) @@ -358,7 +358,7 @@ bool cdcd_control_request(uint8_t rhport, tusb_control_request_t const * request usbd_control_status(rhport, request); // Invoke callback - if ( tud_cdc_line_state_cb) tud_cdc_line_state_cb(itf, TU_BIT_TEST(request->wValue, 0), TU_BIT_TEST(request->wValue, 1)); + if ( tud_cdc_line_state_cb) tud_cdc_line_state_cb(itf, tu_bit_test(request->wValue, 0), tu_bit_test(request->wValue, 1)); break; default: return false; // stall unsupported request diff --git a/src/class/msc/msc_device.c b/src/class/msc/msc_device.c index 0b7a703d6..2fd3d44a1 100644 --- a/src/class/msc/msc_device.c +++ b/src/class/msc/msc_device.c @@ -391,7 +391,7 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t // For other SCSI commands // 1. OUT : queue transfer (invoke app callback after done) // 2. IN & Zero: Process if is built-in, else Invoke app callback. Skip DATA if zero length - if ( (p_cbw->total_bytes > 0 ) && !TU_BIT_TEST(p_cbw->dir, 7) ) + if ( (p_cbw->total_bytes > 0 ) && !tu_bit_test(p_cbw->dir, 7) ) { // queue transfer TU_ASSERT( dcd_edpt_xfer(rhport, p_msc->ep_out, _mscd_buf, p_msc->total_len) ); @@ -440,7 +440,7 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t case MSC_STAGE_DATA: // OUT transfer, invoke callback if needed - if ( !TU_BIT_TEST(p_cbw->dir, 7) ) + if ( !tu_bit_test(p_cbw->dir, 7) ) { if ( SCSI_CMD_WRITE_10 != p_cbw->command[0] ) { diff --git a/src/common/tusb_common.h b/src/common/tusb_common.h index 7c5f11dc2..82793d042 100644 --- a/src/common/tusb_common.h +++ b/src/common/tusb_common.h @@ -55,11 +55,7 @@ #define U32_TO_U8S_BE(u32) U32_B1_U8(u32), U32_B2_U8(u32), U32_B3_U8(u32), U32_B4_U8(u32) #define U32_TO_U8S_LE(u32) U32_B4_U8(u32), U32_B3_U8(u32), U32_B2_U8(u32), U32_B1_U8(u32) -//------------- Bit -------------// -#define TU_BIT(n) (1U << (n)) ///< n-th Bit -#define TU_BIT_SET(x, n) ( (x) | TU_BIT(n) ) ///< set n-th bit of x to 1 -#define TU_BIT_CLEAR(x, n) ( (x) & (~TU_BIT(n)) ) ///< clear n-th bit of x -#define TU_BIT_TEST(x, n) ( ((x) & TU_BIT(n)) ? true : false ) ///< check if n-th bit of x is 1 +#define TU_BIT(n) (1U << (n)) // for declaration of reserved field, make use of _TU_COUNTER_ #define TU_RESERVED XSTRING_CONCAT_(reserved, _TU_COUNTER_) @@ -89,14 +85,6 @@ #define tu_memclr(buffer, size) memset((buffer), 0, (size)) #define tu_varclr(_var) tu_memclr(_var, sizeof(*(_var))) -static inline bool tu_mem_test_zero (void const* buffer, uint32_t size) -{ - uint8_t const* p_mem = (uint8_t const*) buffer; - for(uint32_t i=0; i y) ? x : // Align static inline uint32_t tu_align32 (uint32_t value) { return (value & 0xFFFFFFE0UL); } static inline uint32_t tu_align16 (uint32_t value) { return (value & 0xFFFFFFF0UL); } -static inline uint32_t tu_align_n (uint32_t alignment, uint32_t value) { return value & ((uint32_t) ~(alignment-1)); } static inline uint32_t tu_align4k (uint32_t value) { return (value & 0xFFFFF000UL); } - static inline uint32_t tu_offset4k(uint32_t value) { return (value & 0xFFFUL); } //------------- Mathematics -------------// @@ -164,9 +150,9 @@ static inline uint8_t tu_log2(uint32_t value) } // Bit -static inline uint32_t tu_bit_set(uint32_t value, uint8_t n) { return value | TU_BIT(n); } +static inline uint32_t tu_bit_set (uint32_t value, uint8_t n) { return value | TU_BIT(n); } static inline uint32_t tu_bit_clear(uint32_t value, uint8_t n) { return value & (~TU_BIT(n)); } -static inline bool tu_bit_test(uint32_t value, uint8_t n) { return (value & TU_BIT(n)) ? true : false; } +static inline bool tu_bit_test (uint32_t value, uint8_t n) { return (value & TU_BIT(n)) ? true : false; } /*------------------------------------------------------------------*/ /* Count number of arguments of __VA_ARGS__ diff --git a/src/common/tusb_compiler.h b/src/common/tusb_compiler.h index 9536873cf..0978d1c0f 100644 --- a/src/common/tusb_compiler.h +++ b/src/common/tusb_compiler.h @@ -43,15 +43,16 @@ #define _TU_COUNTER_ __LINE__ #endif -//--------------------------------------------------------------------+ -// Compile-time Assert (use TU_VERIFY_STATIC to avoid name conflict) -//--------------------------------------------------------------------+ +// Compile-time Assert #if __STDC_VERSION__ >= 201112L #define TU_VERIFY_STATIC _Static_assert #else #define TU_VERIFY_STATIC(const_expr, _mess) enum { XSTRING_CONCAT_(_verify_static_, _TU_COUNTER_) = 1/(!!(const_expr)) } #endif +//--------------------------------------------------------------------+ +// Compiler porting with Attribute and Endian +//--------------------------------------------------------------------+ #if defined(__GNUC__) #define ATTR_ALIGNED(Bytes) __attribute__ ((aligned(Bytes))) #define ATTR_SECTION(sec_name) __attribute__ ((section(#sec_name))) @@ -61,15 +62,20 @@ #define ATTR_DEPRECATED(mess) __attribute__ ((deprecated(mess))) // warn if function with this attribute is used #define ATTR_UNUSED __attribute__ ((unused)) // Function/Variable is meant to be possibly unused - #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ // Endian conversion use well-known host to network (big endian) naming - #define tu_htonl(x) __builtin_bswap32(x) - #define tu_ntohl(x) tu_htonl(x) + #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + #define tu_htonl(u32) __builtin_bswap32(u32) + #define tu_ntohl(u32) __builtin_bswap32(u32) - #define tu_htons(u16) __builtin_bswap16(u16) - #define tu_ntohs(u16) tu_htons(u16) + #define tu_htons(u16) __builtin_bswap16(u16) + #define tu_ntohs(u16) __builtin_bswap16(u16) + #else + #define tu_htonl(u32) (u32) + #define tu_ntohl(u32) (u32) + + #define tu_htons(u16) (u16) + #define tu_ntohs(u16) (u16) #endif - #else #error "Compiler attribute porting are required" #endif diff --git a/src/device/usbd.c b/src/device/usbd.c index 833a142f1..95b3f37d4 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -739,7 +739,7 @@ void usbd_edpt_stall(uint8_t rhport, uint8_t ep_addr) uint8_t const dir = tu_edpt_dir(ep_addr); dcd_edpt_stall(rhport, ep_addr); - _usbd_dev.ep_stall_mask[dir] = tu_bit_set(_usbd_dev.ep_stall_mask[dir], epnum); + _usbd_dev.ep_stall_mask[dir] = (uint8_t) tu_bit_set(_usbd_dev.ep_stall_mask[dir], epnum); } void usbd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr) @@ -748,7 +748,7 @@ void usbd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr) uint8_t const dir = tu_edpt_dir(ep_addr); dcd_edpt_clear_stall(rhport, ep_addr); - _usbd_dev.ep_stall_mask[dir] = tu_bit_clear(_usbd_dev.ep_stall_mask[dir], epnum); + _usbd_dev.ep_stall_mask[dir] = (uint8_t) tu_bit_clear(_usbd_dev.ep_stall_mask[dir], epnum); } bool usbd_edpt_stalled(uint8_t rhport, uint8_t ep_addr) diff --git a/src/device/usbd.h b/src/device/usbd.h index 60610c311..46143a215 100644 --- a/src/device/usbd.h +++ b/src/device/usbd.h @@ -94,7 +94,7 @@ ATTR_WEAK void tud_resume_cb(void); #define TUD_CONFIG_DESC_LEN (9) -// Inteface count, string index, total length, attribute, power in mA +// Interface count, string index, total length, attribute, power in mA #define TUD_CONFIG_DESCRIPTOR(_itfcount, _stridx, _total_len, _attribute, _power_ma) \ 9, TUSB_DESC_CONFIGURATION, U16_TO_U8S_LE(_total_len), _itfcount, 1, _stridx, TU_BIT(7) | _attribute, (_power_ma)/2 diff --git a/src/host/hub.c b/src/host/hub.c index 0c4f8c4a2..cc440086f 100644 --- a/src/host/hub.c +++ b/src/host/hub.c @@ -83,7 +83,7 @@ bool hub_port_clear_feature_subtask(uint8_t hub_addr, uint8_t hub_port, uint8_t hub_port_status_response_t * p_port_status; p_port_status = (hub_port_status_response_t *) hub_enum_buffer; - TU_ASSERT( !TU_BIT_TEST(p_port_status->status_change.value, feature-16) ); + TU_ASSERT( !tu_bit_test(p_port_status->status_change.value, feature-16) ); return true; } @@ -211,7 +211,7 @@ void hub_isr(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xf for (uint8_t port=1; port <= p_hub->port_number; port++) { // TODO HUB ignore bit0 hub_status_change - if ( TU_BIT_TEST(p_hub->status_change, port) ) + if ( tu_bit_test(p_hub->status_change, port) ) { hcd_event_t event = { diff --git a/src/portable/nordic/nrf5x/dcd_nrf5x.c b/src/portable/nordic/nrf5x/dcd_nrf5x.c index b834de919..37b5459f6 100644 --- a/src/portable/nordic/nrf5x/dcd_nrf5x.c +++ b/src/portable/nordic/nrf5x/dcd_nrf5x.c @@ -375,7 +375,7 @@ void USBD_IRQHandler(void) for(uint8_t i=0; iEPOUT[epnum].AMOUNT; @@ -517,7 +517,7 @@ void USBD_IRQHandler(void) // CBI In: Endpoint -> Host (transaction complete) for(uint8_t epnum=0; epnum<8; epnum++) { - if ( TU_BIT_TEST(data_status, epnum ) || ( epnum == 0 && is_control_in) ) + if ( tu_bit_test(data_status, epnum ) || ( epnum == 0 && is_control_in) ) { xfer_td_t* xfer = get_td(epnum, TUSB_DIR_IN); @@ -538,7 +538,7 @@ void USBD_IRQHandler(void) // CBI OUT: Host -> Endpoint for(uint8_t epnum=0; epnum<8; epnum++) { - if ( TU_BIT_TEST(data_status, 16+epnum ) || ( epnum == 0 && is_control_out) ) + if ( tu_bit_test(data_status, 16+epnum ) || ( epnum == 0 && is_control_out) ) { xfer_td_t* xfer = get_td(epnum, TUSB_DIR_OUT); diff --git a/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c b/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c index fe7156e7d..b63bf1feb 100644 --- a/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c +++ b/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c @@ -279,7 +279,7 @@ static void process_xfer_isr(uint32_t int_status) { for(uint8_t ep_id = 0; ep_id < EP_COUNT; ep_id++ ) { - if ( TU_BIT_TEST(int_status, ep_id) ) + if ( tu_bit_test(int_status, ep_id) ) { ep_cmd_sts_t * ep_cs = &_dcd.ep[ep_id][0]; xfer_dma_t* xfer_dma = &_dcd.dma[ep_id]; @@ -354,7 +354,7 @@ void USB_IRQHandler(void) } // Setup Receive - if ( TU_BIT_TEST(int_status, 0) && (dev_cmd_stat & CMDSTAT_SETUP_RECEIVED_MASK) ) + if ( tu_bit_test(int_status, 0) && (dev_cmd_stat & CMDSTAT_SETUP_RECEIVED_MASK) ) { // Follow UM flowchart to clear Active & Stall on both Control IN/OUT endpoints _dcd.ep[0][0].active = _dcd.ep[1][0].active = 0; @@ -368,7 +368,7 @@ void USB_IRQHandler(void) _dcd.ep[0][1].buffer_offset = get_buf_offset(_dcd.setup_packet); // clear bit0 - int_status = TU_BIT_CLEAR(int_status, 0); + int_status = tu_bit_clear(int_status, 0); } // Endpoint transfer complete interrupt diff --git a/src/portable/nxp/lpc17_40/dcd_lpc17_40.c b/src/portable/nxp/lpc17_40/dcd_lpc17_40.c index e969e6673..a14d20e30 100644 --- a/src/portable/nxp/lpc17_40/dcd_lpc17_40.c +++ b/src/portable/nxp/lpc17_40/dcd_lpc17_40.c @@ -530,7 +530,7 @@ void hal_dcd_isr(uint8_t rhport) { for ( uint8_t ep_id = 3; ep_id < DCD_ENDPOINT_MAX; ep_id += 2 ) { - if ( TU_BIT_TEST(ep_int_status, ep_id) ) + if ( tu_bit_test(ep_int_status, ep_id) ) { LPC_USB->EpIntClr = TU_BIT(ep_id); @@ -553,7 +553,7 @@ void hal_dcd_isr(uint8_t rhport) for ( uint8_t ep_id = 2; ep_id < DCD_ENDPOINT_MAX; ep_id++ ) { - if ( TU_BIT_TEST(eot, ep_id) ) + if ( tu_bit_test(eot, ep_id) ) { if ( ep_id & 0x01 ) { diff --git a/src/portable/nxp/lpc18_43/dcd_lpc18_43.c b/src/portable/nxp/lpc18_43/dcd_lpc18_43.c index 38ea22380..89dc98ce4 100644 --- a/src/portable/nxp/lpc18_43/dcd_lpc18_43.c +++ b/src/portable/nxp/lpc18_43/dcd_lpc18_43.c @@ -341,7 +341,7 @@ void hal_dcd_isr(uint8_t rhport) { for(uint8_t ep_idx = 0; ep_idx < QHD_MAX; ep_idx++) { - if ( TU_BIT_TEST(edpt_complete, ep_idx2bit(ep_idx)) ) + if ( tu_bit_test(edpt_complete, ep_idx2bit(ep_idx)) ) { // 23.10.12.3 Failed QTD also get ENDPTCOMPLETE set dcd_qtd_t * p_qtd = &dcd_data_ptr[rhport]->qtd[ep_idx];