From 81780008e977fc2093155bfb7be63e00447ad3be Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 11 May 2013 00:40:21 +0700 Subject: [PATCH] add cardinality_of function to return number of set bits refractor tests in ehci --- tests/readme.md | 7 ++++++ tests/test/host/ehci/test_ehci_init.c | 9 ++++---- tests/test/host/ehci/test_ehci_isr.c | 13 ++++++----- tests/test/host/ehci/test_ehci_structure.c | 8 ++++--- .../ehci/test_ehci_usbh_hcd_integration.c | 18 +++++++-------- tests/test/host/ehci/test_pipe_bulk_open.c | 10 ++++----- tests/test/host/ehci/test_pipe_bulk_xfer.c | 18 +++++++-------- tests/test/host/ehci/test_pipe_control_open.c | 14 ++++++------ tests/test/host/ehci/test_pipe_control_xfer.c | 22 +++++++++---------- .../test/host/ehci/test_pipe_interrupt_open.c | 14 ++++++------ .../test/host/ehci/test_pipe_interrupt_xfer.c | 22 +++++++++---------- .../host/ehci/test_pipe_isochronous_open.c | 10 ++++----- tinyusb/common/common.h | 20 ++++++++++++++++- 13 files changed, 107 insertions(+), 78 deletions(-) diff --git a/tests/readme.md b/tests/readme.md index 0ed3b1e6..0f6e370b 100644 --- a/tests/readme.md +++ b/tests/readme.md @@ -2,7 +2,14 @@ TDD indeed works with C & embedded with the help of Ceedling, Unity & CMock as a testing framework. +comming soon + More detail on TDD can be found at - [James W. Grenning's book "Test Driven Development for Embedded C"](http://www.amazon.com/Driven-Development-Embedded-Pragmatic-Programmers/dp/193435662X) - [throwtheswitch's Ceedling, CMock & Unity](http://throwtheswitch.org/) + +## Continuous Integration ## + +comming soon + diff --git a/tests/test/host/ehci/test_ehci_init.c b/tests/test/host/ehci/test_ehci_init.c index b75ebd84..e311c7c7 100644 --- a/tests/test/host/ehci/test_ehci_init.c +++ b/tests/test/host/ehci/test_ehci_init.c @@ -44,16 +44,17 @@ #include "binary.h" #include "hal.h" -#include "mock_osal.h" #include "hcd.h" -#include "mock_usbh_hcd.h" #include "ehci.h" + #include "ehci_controller_fake.h" +#include "mock_osal.h" +#include "mock_usbh_hcd.h" usbh_device_info_t usbh_devices[TUSB_CFG_HOST_DEVICE_MAX+1]; -uint8_t dev_addr; -uint8_t hostid; +static uint8_t dev_addr; +static uint8_t hostid; //--------------------------------------------------------------------+ // Setup/Teardown + helper declare diff --git a/tests/test/host/ehci/test_ehci_isr.c b/tests/test/host/ehci/test_ehci_isr.c index 0d781dbe..51346568 100644 --- a/tests/test/host/ehci/test_ehci_isr.c +++ b/tests/test/host/ehci/test_ehci_isr.c @@ -36,31 +36,32 @@ */ /**************************************************************************/ +#include #include "unity.h" #include "tusb_option.h" #include "errors.h" #include "binary.h" #include "hal.h" -#include "mock_osal.h" #include "hcd.h" -#include "mock_usbh_hcd.h" #include "ehci.h" + #include "ehci_controller_fake.h" +#include "mock_osal.h" +#include "mock_usbh_hcd.h" usbh_device_info_t usbh_devices[TUSB_CFG_HOST_DEVICE_MAX+1]; -uint8_t hostid; -ehci_registers_t * regs; +static uint8_t hostid; +static ehci_registers_t * regs; void setUp(void) { ehci_controller_init(); + TEST_ASSERT_EQUAL( TUSB_ERROR_NONE, hcd_init()); hostid = RANDOM(CONTROLLER_HOST_NUMBER) + TEST_CONTROLLER_HOST_START_INDEX; regs = get_operational_register(hostid); - - hcd_init(); } void tearDown(void) diff --git a/tests/test/host/ehci/test_ehci_structure.c b/tests/test/host/ehci/test_ehci_structure.c index deb3d380..467bb92a 100644 --- a/tests/test/host/ehci/test_ehci_structure.c +++ b/tests/test/host/ehci/test_ehci_structure.c @@ -36,17 +36,19 @@ */ /**************************************************************************/ +#include #include "unity.h" #include "tusb_option.h" #include "errors.h" #include "binary.h" #include "hal.h" -#include "mock_osal.h" #include "hcd.h" -#include "mock_usbh_hcd.h" #include "ehci.h" + #include "ehci_controller_fake.h" +#include "mock_osal.h" +#include "mock_usbh_hcd.h" usbh_device_info_t usbh_devices[TUSB_CFG_HOST_DEVICE_MAX+1]; @@ -318,7 +320,7 @@ void test_ehci_data(void) TEST_ASSERT_BITS_LOW(4096-1, (uint32_t)get_period_frame_list(hostid) ); } - TEST_IGNORE(); + // TODO more tests on ehci_data } //--------------------------------------------------------------------+ diff --git a/tests/test/host/ehci/test_ehci_usbh_hcd_integration.c b/tests/test/host/ehci/test_ehci_usbh_hcd_integration.c index 75c3ca5f..520de73d 100644 --- a/tests/test/host/ehci/test_ehci_usbh_hcd_integration.c +++ b/tests/test/host/ehci/test_ehci_usbh_hcd_integration.c @@ -36,6 +36,7 @@ */ /**************************************************************************/ +#include #include "unity.h" #include "tusb_option.h" #include "errors.h" @@ -51,16 +52,15 @@ #include "ehci.h" #include "ehci_controller_fake.h" -#define _TINY_USB_SOURCE_FILE_ usbh_device_info_t usbh_devices[TUSB_CFG_HOST_DEVICE_MAX+1]; -uint8_t const control_max_packet_size = 64; -uint8_t hub_addr; -uint8_t hub_port; -uint8_t dev_addr; -uint8_t hostid; -ehci_registers_t * regs; -ehci_qhd_t *async_head; -ehci_qhd_t *period_head_arr; +static uint8_t const control_max_packet_size = 64; +static uint8_t hub_addr; +static uint8_t hub_port; +static uint8_t dev_addr; +static uint8_t hostid; +static ehci_registers_t * regs; +static ehci_qhd_t *async_head; +static ehci_qhd_t *period_head_arr; void setUp(void) { diff --git a/tests/test/host/ehci/test_pipe_bulk_open.c b/tests/test/host/ehci/test_pipe_bulk_open.c index 70d8d74a..7669f18f 100644 --- a/tests/test/host/ehci/test_pipe_bulk_open.c +++ b/tests/test/host/ehci/test_pipe_bulk_open.c @@ -50,12 +50,12 @@ usbh_device_info_t usbh_devices[TUSB_CFG_HOST_DEVICE_MAX+1]; -uint8_t const hub_addr = 2; -uint8_t const hub_port = 2; -uint8_t dev_addr; -uint8_t hostid; +static uint8_t const hub_addr = 2; +static uint8_t const hub_port = 2; +static uint8_t dev_addr; +static uint8_t hostid; -ehci_qhd_t *async_head; +static ehci_qhd_t *async_head; //--------------------------------------------------------------------+ // Setup/Teardown + helper declare diff --git a/tests/test/host/ehci/test_pipe_bulk_xfer.c b/tests/test/host/ehci/test_pipe_bulk_xfer.c index e1476549..2a39cd62 100644 --- a/tests/test/host/ehci/test_pipe_bulk_xfer.c +++ b/tests/test/host/ehci/test_pipe_bulk_xfer.c @@ -50,16 +50,16 @@ usbh_device_info_t usbh_devices[TUSB_CFG_HOST_DEVICE_MAX+1]; -uint8_t const hub_addr = 2; -uint8_t const hub_port = 2; -uint8_t dev_addr; -uint8_t hostid; -uint8_t xfer_data [18000]; // 18K to test buffer pointer list -uint8_t data2[100]; +static uint8_t const hub_addr = 2; +static uint8_t const hub_port = 2; +static uint8_t dev_addr; +static uint8_t hostid; +static uint8_t xfer_data [18000]; // 18K to test buffer pointer list +static uint8_t data2[100]; -ehci_qhd_t *async_head; -ehci_qhd_t *p_qhd_bulk; -pipe_handle_t pipe_hdl_bulk; +static ehci_qhd_t *async_head; +static ehci_qhd_t *p_qhd_bulk; +static pipe_handle_t pipe_hdl_bulk; tusb_descriptor_endpoint_t const desc_ept_bulk_in = { diff --git a/tests/test/host/ehci/test_pipe_control_open.c b/tests/test/host/ehci/test_pipe_control_open.c index 28f2c991..2e758b2e 100644 --- a/tests/test/host/ehci/test_pipe_control_open.c +++ b/tests/test/host/ehci/test_pipe_control_open.c @@ -50,14 +50,14 @@ usbh_device_info_t usbh_devices[TUSB_CFG_HOST_DEVICE_MAX+1]; -uint8_t const control_max_packet_size = 64; -uint8_t const hub_addr = 2; -uint8_t const hub_port = 2; -uint8_t dev_addr; -uint8_t hostid; +static uint8_t const control_max_packet_size = 64; +static uint8_t const hub_addr = 2; +static uint8_t const hub_port = 2; +static uint8_t dev_addr; +static uint8_t hostid; -ehci_qhd_t *async_head; -ehci_qhd_t *p_control_qhd; +static ehci_qhd_t *async_head; +static ehci_qhd_t *p_control_qhd; //--------------------------------------------------------------------+ // Setup/Teardown + helper declare diff --git a/tests/test/host/ehci/test_pipe_control_xfer.c b/tests/test/host/ehci/test_pipe_control_xfer.c index 3052b0cd..f62b72ba 100644 --- a/tests/test/host/ehci/test_pipe_control_xfer.c +++ b/tests/test/host/ehci/test_pipe_control_xfer.c @@ -50,19 +50,19 @@ usbh_device_info_t usbh_devices[TUSB_CFG_HOST_DEVICE_MAX+1]; -uint8_t const control_max_packet_size = 64; -uint8_t const hub_addr = 2; -uint8_t const hub_port = 2; -uint8_t dev_addr; -uint8_t hostid; -uint8_t xfer_data [100]; +static uint8_t const control_max_packet_size = 64; +static uint8_t const hub_addr = 2; +static uint8_t const hub_port = 2; +static uint8_t dev_addr; +static uint8_t hostid; +static uint8_t xfer_data [100]; -ehci_qhd_t *async_head; -ehci_qhd_t *p_control_qhd; +static ehci_qhd_t *async_head; +static ehci_qhd_t *p_control_qhd; -ehci_qtd_t *p_setup; -ehci_qtd_t *p_data; -ehci_qtd_t *p_status; +static ehci_qtd_t *p_setup; +static ehci_qtd_t *p_data; +static ehci_qtd_t *p_status; //--------------------------------------------------------------------+ // Setup/Teardown + helper declare diff --git a/tests/test/host/ehci/test_pipe_interrupt_open.c b/tests/test/host/ehci/test_pipe_interrupt_open.c index af499f73..987267aa 100644 --- a/tests/test/host/ehci/test_pipe_interrupt_open.c +++ b/tests/test/host/ehci/test_pipe_interrupt_open.c @@ -50,15 +50,15 @@ usbh_device_info_t usbh_devices[TUSB_CFG_HOST_DEVICE_MAX+1]; -uint8_t const hub_addr = 2; -uint8_t const hub_port = 2; -uint8_t dev_addr; -uint8_t hostid; +static uint8_t const hub_addr = 2; +static uint8_t const hub_port = 2; +static uint8_t dev_addr; +static uint8_t hostid; -ehci_qhd_t *period_head_arr; +static ehci_qhd_t *period_head_arr; -ehci_qhd_t *p_int_qhd; -pipe_handle_t pipe_hdl; +static ehci_qhd_t *p_int_qhd; +static pipe_handle_t pipe_hdl; uint8_t count_set_bits(uint8_t x); diff --git a/tests/test/host/ehci/test_pipe_interrupt_xfer.c b/tests/test/host/ehci/test_pipe_interrupt_xfer.c index 108378e9..c3747672 100644 --- a/tests/test/host/ehci/test_pipe_interrupt_xfer.c +++ b/tests/test/host/ehci/test_pipe_interrupt_xfer.c @@ -50,18 +50,18 @@ usbh_device_info_t usbh_devices[TUSB_CFG_HOST_DEVICE_MAX+1]; -uint8_t const hub_addr = 2; -uint8_t const hub_port = 2; -uint8_t dev_addr; -uint8_t hostid; -uint8_t xfer_data [100]; -uint8_t data2[100]; +static uint8_t const hub_addr = 2; +static uint8_t const hub_port = 2; +static uint8_t dev_addr; +static uint8_t hostid; +static uint8_t xfer_data [100]; +static uint8_t data2[100]; -ehci_qhd_t *period_head_arr; -ehci_qhd_t *p_qhd_interrupt; -pipe_handle_t pipe_hdl_interrupt; +static ehci_qhd_t *period_head_arr; +static ehci_qhd_t *p_qhd_interrupt; +static pipe_handle_t pipe_hdl_interrupt; -tusb_descriptor_endpoint_t const desc_ept_interrupt_in = +static tusb_descriptor_endpoint_t const desc_ept_interrupt_in = { .bLength = sizeof(tusb_descriptor_endpoint_t), .bDescriptorType = TUSB_DESC_ENDPOINT, @@ -71,7 +71,7 @@ tusb_descriptor_endpoint_t const desc_ept_interrupt_in = .bInterval = 2 }; -tusb_descriptor_endpoint_t const desc_ept_interupt_out = +static tusb_descriptor_endpoint_t const desc_ept_interupt_out = { .bLength = sizeof(tusb_descriptor_endpoint_t), .bDescriptorType = TUSB_DESC_ENDPOINT, diff --git a/tests/test/host/ehci/test_pipe_isochronous_open.c b/tests/test/host/ehci/test_pipe_isochronous_open.c index 9b45fbe4..560ad06c 100644 --- a/tests/test/host/ehci/test_pipe_isochronous_open.c +++ b/tests/test/host/ehci/test_pipe_isochronous_open.c @@ -50,12 +50,12 @@ usbh_device_info_t usbh_devices[TUSB_CFG_HOST_DEVICE_MAX+1]; -uint8_t const hub_addr = 2; -uint8_t const hub_port = 2; -uint8_t dev_addr; -uint8_t hostid; +static uint8_t const hub_addr = 2; +static uint8_t const hub_port = 2; +static uint8_t dev_addr; +static uint8_t hostid; -ehci_qhd_t *period_head_arr; +static ehci_qhd_t *period_head_arr; //--------------------------------------------------------------------+ // Setup/Teardown + helper declare //--------------------------------------------------------------------+ diff --git a/tinyusb/common/common.h b/tinyusb/common/common.h index 1e78539b..fd1d2b29 100644 --- a/tinyusb/common/common.h +++ b/tinyusb/common/common.h @@ -170,7 +170,7 @@ static inline uint32_t offset4k(uint32_t value) static inline uint8_t log2_of(uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST; static inline uint8_t log2_of(uint32_t value) { - uint8_t result = 0; // log2 of value is its MSB's position + uint8_t result = 0; // log2 of a value is its MSB's position while (value >>= 1) { @@ -179,6 +179,24 @@ static inline uint8_t log2_of(uint32_t value) return result; } +// return the number of set bits in value +static inline uint8_t cardinality_of(uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST; +static inline uint8_t cardinality_of(uint32_t value) +{ + // Brian Kernighan's method goes through as many iterations as there are set bits. So if we have a 32-bit word with only + // the high bit set, then it will only go once through the loop + // Published in 1988, the C Programming Language 2nd Ed. (by Brian W. Kernighan and Dennis M. Ritchie) + // mentions this in exercise 2-9. On April 19, 2006 Don Knuth pointed out to me that this method + // "was first published by Peter Wegner in CACM 3 (1960), 322. (Also discovered independently by Derrick Lehmer and + // published in 1964 in a book edited by Beckenbach.)" + uint8_t count; + for (count = 0; value; count++) + { + value &= value - 1; // clear the least significant bit set + } + + return count; +} #ifdef __cplusplus }