add cardinality_of function to return number of set bits

refractor tests in ehci
This commit is contained in:
hathach 2013-05-11 00:40:21 +07:00
parent e50010c36c
commit 81780008e9
13 changed files with 107 additions and 78 deletions

View File

@ -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
<!-- https://travis-ci.org/hathach/tinyusb -->

View File

@ -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

View File

@ -36,31 +36,32 @@
*/
/**************************************************************************/
#include <stdlib.h>
#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)

View File

@ -36,17 +36,19 @@
*/
/**************************************************************************/
#include <stdlib.h>
#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
}
//--------------------------------------------------------------------+

View File

@ -36,6 +36,7 @@
*/
/**************************************************************************/
#include <stdlib.h>
#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)
{

View File

@ -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

View File

@ -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 =
{

View File

@ -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

View File

@ -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

View File

@ -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);

View File

@ -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,

View File

@ -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
//--------------------------------------------------------------------+

View File

@ -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
}