add mutex support for osal
add test for mutex in test_osal_none.c implement usbh_control_xfer using mutex to get access to queue xfer on control pipe (while semaphore is used to sync with hcd DMA) failed to issue control xfer: set idle & get report descriptor in hidh_open_subtask (more to work on)
This commit is contained in:
		@@ -58,6 +58,7 @@ static inline void helper_usbh_init_expect(void)
 | 
			
		||||
  osal_semaphore_create_IgnoreAndReturn( (osal_semaphore_handle_t) 0x1234);
 | 
			
		||||
  osal_task_create_IgnoreAndReturn(TUSB_ERROR_NONE);
 | 
			
		||||
  osal_queue_create_IgnoreAndReturn( (osal_queue_handle_t) 0x4566 );
 | 
			
		||||
  osal_mutex_create_IgnoreAndReturn((osal_mutex_handle_t) 0x789a);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static inline void helper_usbh_device_emulate(uint8_t dev_addr, uint8_t hub_addr, uint8_t hub_port, uint8_t hostid, tusb_speed_t speed)
 | 
			
		||||
 
 | 
			
		||||
@@ -50,7 +50,6 @@
 | 
			
		||||
 | 
			
		||||
extern usbh_device_info_t usbh_devices[TUSB_CFG_HOST_DEVICE_MAX+1];
 | 
			
		||||
extern uint8_t enum_data_buffer[TUSB_CFG_HOST_ENUM_BUFFER_SIZE];
 | 
			
		||||
uint8_t dev_addr;
 | 
			
		||||
 | 
			
		||||
usbh_enumerate_t const enum_connect = {
 | 
			
		||||
    .core_id  = 0,
 | 
			
		||||
@@ -70,12 +69,15 @@ void setUp(void)
 | 
			
		||||
 | 
			
		||||
  osal_queue_receive_StubWithCallback(queue_recv_stub);
 | 
			
		||||
  osal_semaphore_wait_StubWithCallback(semaphore_wait_success_stub);
 | 
			
		||||
  osal_mutex_wait_StubWithCallback(semaphore_wait_success_stub);
 | 
			
		||||
  osal_mutex_release_IgnoreAndReturn(TUSB_ERROR_NONE);
 | 
			
		||||
  hcd_pipe_control_xfer_StubWithCallback(control_xfer_stub);
 | 
			
		||||
 | 
			
		||||
  hcd_port_connect_status_ExpectAndReturn(enum_connect.core_id, true);
 | 
			
		||||
  hcd_port_reset_Expect(enum_connect.core_id);
 | 
			
		||||
  hcd_port_speed_get_ExpectAndReturn(enum_connect.core_id, device_speed);
 | 
			
		||||
  osal_semaphore_reset_Expect( usbh_devices[0].control.sem_hdl );
 | 
			
		||||
  osal_mutex_reset_Expect( usbh_devices[0].control.mutex_hdl );
 | 
			
		||||
  hcd_pipe_control_open_ExpectAndReturn(0, 8, TUSB_ERROR_NONE);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -172,6 +174,7 @@ tusb_error_t hidh_install_stub(uint8_t dev_addr, tusb_descriptor_interface_t con
 | 
			
		||||
void test_addr0_failed_dev_desc(void)
 | 
			
		||||
{
 | 
			
		||||
  osal_semaphore_wait_StubWithCallback(semaphore_wait_timeout_stub(0));
 | 
			
		||||
 | 
			
		||||
  tusbh_device_mount_failed_cb_Expect(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL);
 | 
			
		||||
 | 
			
		||||
  usbh_enumeration_task(NULL);
 | 
			
		||||
@@ -199,6 +202,7 @@ void test_enum_failed_get_full_dev_desc(void)
 | 
			
		||||
  hcd_pipe_control_close_ExpectAndReturn(0, TUSB_ERROR_NONE);
 | 
			
		||||
 | 
			
		||||
  osal_semaphore_reset_Expect( usbh_devices[0].control.sem_hdl );
 | 
			
		||||
  osal_mutex_reset_Expect( usbh_devices[0].control.mutex_hdl );
 | 
			
		||||
  hcd_pipe_control_open_ExpectAndReturn(1, desc_device.bMaxPacketSize0, TUSB_ERROR_NONE);
 | 
			
		||||
  tusbh_device_mount_failed_cb_Expect(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL);
 | 
			
		||||
 | 
			
		||||
@@ -218,6 +222,8 @@ void test_enum_failed_get_9byte_config_desc(void)
 | 
			
		||||
  hcd_port_reset_Expect( usbh_devices[0].core_id );
 | 
			
		||||
  hcd_pipe_control_close_ExpectAndReturn(0, TUSB_ERROR_NONE);
 | 
			
		||||
  osal_semaphore_reset_Expect( usbh_devices[0].control.sem_hdl );
 | 
			
		||||
  osal_mutex_reset_Expect( usbh_devices[0].control.mutex_hdl );
 | 
			
		||||
 | 
			
		||||
  hcd_pipe_control_open_ExpectAndReturn(1, desc_device.bMaxPacketSize0, TUSB_ERROR_NONE);
 | 
			
		||||
  tusbh_device_attached_cb_ExpectAndReturn((tusb_descriptor_device_t*) enum_data_buffer, 1);
 | 
			
		||||
  tusbh_device_mount_failed_cb_Expect(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL);
 | 
			
		||||
@@ -235,6 +241,7 @@ void test_enum_failed_get_full_config_desc(void)
 | 
			
		||||
  hcd_port_reset_Expect( usbh_devices[0].core_id );
 | 
			
		||||
  hcd_pipe_control_close_ExpectAndReturn(0, TUSB_ERROR_NONE);
 | 
			
		||||
  osal_semaphore_reset_Expect( usbh_devices[0].control.sem_hdl );
 | 
			
		||||
  osal_mutex_reset_Expect( usbh_devices[0].control.mutex_hdl );
 | 
			
		||||
  hcd_pipe_control_open_ExpectAndReturn(1, desc_device.bMaxPacketSize0, TUSB_ERROR_NONE);
 | 
			
		||||
  tusbh_device_attached_cb_ExpectAndReturn((tusb_descriptor_device_t*) enum_data_buffer, 1);
 | 
			
		||||
  tusbh_device_mount_failed_cb_Expect(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL);
 | 
			
		||||
@@ -253,6 +260,7 @@ void test_enum_parse_config_desc(void)
 | 
			
		||||
  hcd_port_reset_Expect( usbh_devices[0].core_id );
 | 
			
		||||
  hcd_pipe_control_close_ExpectAndReturn(0, TUSB_ERROR_NONE);
 | 
			
		||||
  osal_semaphore_reset_Expect( usbh_devices[0].control.sem_hdl );
 | 
			
		||||
  osal_mutex_reset_Expect( usbh_devices[0].control.mutex_hdl );
 | 
			
		||||
  hcd_pipe_control_open_ExpectAndReturn(1, desc_device.bMaxPacketSize0, TUSB_ERROR_NONE);
 | 
			
		||||
  tusbh_device_attached_cb_ExpectAndReturn((tusb_descriptor_device_t*) enum_data_buffer, 1);
 | 
			
		||||
 | 
			
		||||
@@ -269,6 +277,7 @@ void test_enum_set_configure(void)
 | 
			
		||||
  hcd_port_reset_Expect( usbh_devices[0].core_id );
 | 
			
		||||
  hcd_pipe_control_close_ExpectAndReturn(0, TUSB_ERROR_NONE);
 | 
			
		||||
  osal_semaphore_reset_Expect( usbh_devices[0].control.sem_hdl );
 | 
			
		||||
  osal_mutex_reset_Expect( usbh_devices[0].control.mutex_hdl );
 | 
			
		||||
  hcd_pipe_control_open_ExpectAndReturn(1, desc_device.bMaxPacketSize0, TUSB_ERROR_NONE);
 | 
			
		||||
  tusbh_device_attached_cb_ExpectAndReturn((tusb_descriptor_device_t*) enum_data_buffer, 1);
 | 
			
		||||
  class_install_expect();
 | 
			
		||||
 
 | 
			
		||||
@@ -90,7 +90,6 @@ void test_usbh_init_hcd_failed(void)
 | 
			
		||||
void test_usbh_init_enum_task_create_failed(void)
 | 
			
		||||
{
 | 
			
		||||
  hcd_init_ExpectAndReturn(TUSB_ERROR_NONE);
 | 
			
		||||
  osal_semaphore_create_IgnoreAndReturn( (osal_semaphore_handle_t) 0x1234);
 | 
			
		||||
  osal_task_create_IgnoreAndReturn(TUSB_ERROR_OSAL_TASK_FAILED);
 | 
			
		||||
  TEST_ASSERT_EQUAL(TUSB_ERROR_OSAL_TASK_FAILED, usbh_init());
 | 
			
		||||
}
 | 
			
		||||
@@ -98,12 +97,30 @@ void test_usbh_init_enum_task_create_failed(void)
 | 
			
		||||
void test_usbh_init_enum_queue_create_failed(void)
 | 
			
		||||
{
 | 
			
		||||
  hcd_init_ExpectAndReturn(TUSB_ERROR_NONE);
 | 
			
		||||
  osal_semaphore_create_IgnoreAndReturn( (osal_semaphore_handle_t) 0x1234);
 | 
			
		||||
  osal_task_create_IgnoreAndReturn(TUSB_ERROR_NONE);
 | 
			
		||||
  osal_queue_create_IgnoreAndReturn(NULL);
 | 
			
		||||
  TEST_ASSERT_EQUAL(TUSB_ERROR_OSAL_QUEUE_FAILED, usbh_init());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void test_usbh_init_semaphore_create_failed(void)
 | 
			
		||||
{
 | 
			
		||||
  hcd_init_ExpectAndReturn(TUSB_ERROR_NONE);
 | 
			
		||||
  osal_task_create_IgnoreAndReturn(TUSB_ERROR_NONE);
 | 
			
		||||
  osal_queue_create_IgnoreAndReturn((osal_queue_handle_t) 0x1234);
 | 
			
		||||
  osal_semaphore_create_IgnoreAndReturn(NULL);
 | 
			
		||||
  TEST_ASSERT_EQUAL(TUSB_ERROR_OSAL_SEMAPHORE_FAILED, usbh_init());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void test_usbh_init_mutex_create_failed(void)
 | 
			
		||||
{
 | 
			
		||||
  hcd_init_ExpectAndReturn(TUSB_ERROR_NONE);
 | 
			
		||||
  osal_task_create_IgnoreAndReturn(TUSB_ERROR_NONE);
 | 
			
		||||
  osal_queue_create_IgnoreAndReturn((osal_queue_handle_t) 0x1234);
 | 
			
		||||
  osal_semaphore_create_IgnoreAndReturn((osal_semaphore_handle_t) 0x1234);
 | 
			
		||||
  osal_mutex_create_IgnoreAndReturn(NULL);
 | 
			
		||||
  TEST_ASSERT_EQUAL(TUSB_ERROR_OSAL_MUTEX_FAILED, usbh_init());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void test_usbh_init_ok(void)
 | 
			
		||||
{
 | 
			
		||||
  hcd_init_ExpectAndReturn(TUSB_ERROR_NONE);
 | 
			
		||||
@@ -151,3 +168,39 @@ void test_usbh_device_unplugged_isr(void)
 | 
			
		||||
 | 
			
		||||
  TEST_ASSERT_EQUAL(TUSB_DEVICE_STATE_REMOVING, usbh_devices[dev_addr].state);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void semaphore_wait_success_stub(osal_mutex_handle_t const sem_hdl, uint32_t msec, tusb_error_t *p_error, int num_call)
 | 
			
		||||
{
 | 
			
		||||
  (*p_error) = TUSB_ERROR_NONE;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void mutex_wait_failed_stub(osal_mutex_handle_t const sem_hdl, uint32_t msec, tusb_error_t *p_error, int num_call)
 | 
			
		||||
{
 | 
			
		||||
  (*p_error) = TUSB_ERROR_OSAL_TIMEOUT;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void test_usbh_control_xfer_mutex_failed(void)
 | 
			
		||||
{
 | 
			
		||||
  tusb_std_request_t a_request;
 | 
			
		||||
 | 
			
		||||
  osal_mutex_wait_StubWithCallback(mutex_wait_failed_stub);
 | 
			
		||||
  osal_mutex_release_ExpectAndReturn(usbh_devices[dev_addr].control.mutex_hdl, TUSB_ERROR_NONE);
 | 
			
		||||
 | 
			
		||||
  //------------- Code Under Test -------------//
 | 
			
		||||
  usbh_control_xfer_subtask(dev_addr, &a_request, NULL);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void test_usbh_control_xfer_ok(void)
 | 
			
		||||
{
 | 
			
		||||
  tusb_std_request_t a_request;
 | 
			
		||||
 | 
			
		||||
  osal_mutex_wait_StubWithCallback(semaphore_wait_success_stub);
 | 
			
		||||
 | 
			
		||||
  hcd_pipe_control_xfer_ExpectAndReturn(dev_addr, &a_request, NULL, TUSB_ERROR_NONE);
 | 
			
		||||
  osal_semaphore_wait_StubWithCallback(semaphore_wait_success_stub);
 | 
			
		||||
 | 
			
		||||
  osal_mutex_release_ExpectAndReturn(usbh_devices[dev_addr].control.mutex_hdl, TUSB_ERROR_NONE);
 | 
			
		||||
 | 
			
		||||
  //------------- Code Under Test -------------//
 | 
			
		||||
  usbh_control_xfer_subtask(dev_addr, &a_request, NULL);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -54,11 +54,15 @@ osal_semaphore_handle_t sem_hdl;
 | 
			
		||||
OSAL_QUEUE_DEF(queue, QUEUE_DEPTH, uint32_t);
 | 
			
		||||
osal_queue_handle_t queue_hdl;
 | 
			
		||||
 | 
			
		||||
OSAL_MUTEX_DEF(mutex);
 | 
			
		||||
osal_mutex_handle_t mutex_hdl;
 | 
			
		||||
 | 
			
		||||
void setUp(void)
 | 
			
		||||
{
 | 
			
		||||
  memset(statements, 0, sizeof(statements));
 | 
			
		||||
  sem_hdl = osal_semaphore_create(OSAL_SEM_REF(sem));
 | 
			
		||||
  queue_hdl = osal_queue_create(&queue);
 | 
			
		||||
  mutex_hdl = osal_mutex_create(OSAL_MUTEX_REF(mutex));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void tearDown(void)
 | 
			
		||||
@@ -82,6 +86,21 @@ void test_semaphore_post(void)
 | 
			
		||||
}
 | 
			
		||||
// blocking service such as semaphore wait need to be invoked within a task's loop
 | 
			
		||||
 | 
			
		||||
//--------------------------------------------------------------------+
 | 
			
		||||
// Mutex
 | 
			
		||||
//--------------------------------------------------------------------+
 | 
			
		||||
void test_mutex_create(void)
 | 
			
		||||
{
 | 
			
		||||
  TEST_ASSERT_EQUAL_PTR(&mutex, mutex_hdl);
 | 
			
		||||
  TEST_ASSERT_EQUAL(1, mutex);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void test_mutex_release(void)
 | 
			
		||||
{
 | 
			
		||||
  osal_mutex_release(mutex_hdl);
 | 
			
		||||
  TEST_ASSERT_EQUAL(1, mutex);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//--------------------------------------------------------------------+
 | 
			
		||||
// Queue
 | 
			
		||||
//--------------------------------------------------------------------+
 | 
			
		||||
@@ -163,6 +182,76 @@ void test_task_with_semaphore(void)
 | 
			
		||||
  TEST_ASSERT_EQUAL(2, statements[0]);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//--------------------------------------------------------------------+
 | 
			
		||||
// TASK MUTEX
 | 
			
		||||
//--------------------------------------------------------------------+
 | 
			
		||||
tusb_error_t mutex_sample_task1(void) // occupy mutex and not release it
 | 
			
		||||
{
 | 
			
		||||
  tusb_error_t error = TUSB_ERROR_NONE;
 | 
			
		||||
 | 
			
		||||
  OSAL_TASK_LOOP_BEGIN
 | 
			
		||||
 | 
			
		||||
  statements[0]++;
 | 
			
		||||
 | 
			
		||||
  osal_mutex_wait(mutex_hdl, OSAL_TIMEOUT_WAIT_FOREVER, &error);
 | 
			
		||||
  statements[2]++;
 | 
			
		||||
 | 
			
		||||
  OSAL_TASK_LOOP_END
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
tusb_error_t mutex_sample_task2(void)
 | 
			
		||||
{
 | 
			
		||||
  tusb_error_t error = TUSB_ERROR_NONE;
 | 
			
		||||
 | 
			
		||||
  OSAL_TASK_LOOP_BEGIN
 | 
			
		||||
 | 
			
		||||
  statements[1]++;
 | 
			
		||||
 | 
			
		||||
  osal_mutex_wait(mutex_hdl, OSAL_TIMEOUT_WAIT_FOREVER, &error);
 | 
			
		||||
  statements[3]++;
 | 
			
		||||
 | 
			
		||||
  osal_mutex_wait(mutex_hdl, OSAL_TIMEOUT_NORMAL, &error);
 | 
			
		||||
  statements[5]++;
 | 
			
		||||
 | 
			
		||||
  TEST_ASSERT_EQUAL(TUSB_ERROR_OSAL_TIMEOUT, error);
 | 
			
		||||
 | 
			
		||||
  OSAL_TASK_LOOP_END
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void test_task_with_mutex(void)
 | 
			
		||||
{
 | 
			
		||||
  // several invoke before mutex is available
 | 
			
		||||
  mutex_sample_task1();
 | 
			
		||||
  for(uint32_t i=0; i<10; i++) {
 | 
			
		||||
    mutex_sample_task2();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  TEST_ASSERT_EQUAL(1, statements[0]);
 | 
			
		||||
  TEST_ASSERT_EQUAL(1, statements[2]);
 | 
			
		||||
 | 
			
		||||
  TEST_ASSERT_EQUAL(1, statements[1]);
 | 
			
		||||
  TEST_ASSERT_EQUAL(0, statements[3]);
 | 
			
		||||
 | 
			
		||||
  // invoke after posting mutex
 | 
			
		||||
  osal_mutex_release(mutex_hdl);
 | 
			
		||||
  for(uint32_t i=0; i<10; i++) {
 | 
			
		||||
    mutex_sample_task2();
 | 
			
		||||
  }
 | 
			
		||||
  TEST_ASSERT_EQUAL(1, statements[3]);
 | 
			
		||||
  TEST_ASSERT_EQUAL(0, statements[5]);
 | 
			
		||||
 | 
			
		||||
  // timeout
 | 
			
		||||
  for(uint32_t i=0; i<(OSAL_TIMEOUT_NORMAL*TUSB_CFG_OS_TICKS_PER_SECOND)/1000 ; i++){ // one tick less
 | 
			
		||||
    osal_tick_tock();
 | 
			
		||||
  }
 | 
			
		||||
  mutex_sample_task2();
 | 
			
		||||
  TEST_ASSERT_EQUAL(0, statements[5]);
 | 
			
		||||
  osal_tick_tock();
 | 
			
		||||
 | 
			
		||||
  mutex_sample_task2();
 | 
			
		||||
  TEST_ASSERT_EQUAL(1, statements[5]);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//--------------------------------------------------------------------+
 | 
			
		||||
// TASK QUEUE
 | 
			
		||||
//--------------------------------------------------------------------+
 | 
			
		||||
@@ -205,7 +294,7 @@ void test_task_with_queue(void)
 | 
			
		||||
    sample_task_with_queue();
 | 
			
		||||
  TEST_ASSERT_EQUAL(1, statements[0]);
 | 
			
		||||
 | 
			
		||||
  // invoke after posting semaphore
 | 
			
		||||
  // invoke after sending to queue
 | 
			
		||||
  item = 0x1111;
 | 
			
		||||
  osal_queue_send(queue_hdl, &item);
 | 
			
		||||
  sample_task_with_queue();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user