Microchip® Advanced Software Framework

unit_tests.c File Reference

Main functions to generate USB patterns.

Copyright (c) 2011-2018 Microchip Technology Inc. and its subsidiaries.

#include "compiler.h"
#include "preprocessor.h"
#include "board.h"
#include "gpio.h"
#include "sysclk.h"
#include "usbb_otg.h"
#include "usbb_device.h"
#include "usb_protocol.h"
#include "usb_protocol_hid.h"
#include "delay.h"

Data Structures

struct  main_conf_desc_t
 USB HID mouse configuration descriptor struct. More...
 
struct  udi_hid_mouse_report_desc_t
 Report descriptor for HID mouse. More...
 

Macros

#define SAML21_USB_TST
 
#define TST_15_DIS
 
#define TST_DETACH_DELAY   800
 
#define USB_DEVICE_ATTR   (USB_CONFIG_ATTR_REMOTE_WAKEUP|USB_CONFIG_ATTR_BUS_POWERED)
 
#define USB_DEVICE_MAJOR_VERSION   1
 
#define USB_DEVICE_MINOR_VERSION   0
 
#define USB_DEVICE_POWER   100
 
#define USB_DEVICE_PRODUCT_ID   USB_PID_ATMEL_ASF_HIDMOUSE
 
#define USB_DEVICE_VENDOR_ID   USB_VID_ATMEL
 

Functions

int main (void)
 Main function. Execution starts here. More...
 
Routines USB low level
static void main_otg_init (void)
 Reset and initialize USB OTG peripheral to specified speed mode. More...
 
static void main_usb_wait_reset_start (void)
 Waits the start of USB reset signal This routine waits the end of SOF. More...
 
static void main_usb_wait_end_of_reset (void)
 Waits the end of USB reset signal. More...
 
static void main_usb_wait_suspend (void)
 Waits a USB suspend mode. More...
 
static void main_usb_wait_resume (void)
 Waits a USB downstream resume signal from host. More...
 
static void main_usb_wait_wakeup (void)
 Waits a USB wakeup signal. More...
 
static void main_usb_wait_sof (void)
 Waits a USB start of frame (or 1ms for USB low speed) More...
 
static void main_detach (void)
 Detaches the USB device of the USB line (= remove USB pull_up) More...
 
static void main_usb_enable_ctrl_ep (uint8_t address, uint8_t ep_size)
 Allocation of the control endpoint. More...
 
static void main_usb_wait_setup_packet (void)
 Waits a SETUP packet and load data in main_setup_packet[]. More...
 
static void main_usb_stall_after_setup_packet (void)
 Enables the control endpoint STALL after a SETUP packet reception. More...
 
static void main_usb_send_in (uint8_t *payload, uint8_t size)
 Waits a DATA IN packets on control endpoint. More...
 
static void main_usb_wait_out (uint8_t *payload, uint8_t size)
 Sends a DATA OUT packets on control endpoint. More...
 
Routines for each step of USB enumeration from USB Host stack core
static void main_usb_enum_step1 (void)
 step1 of enumeration - wait a USB reset More...
 
static void main_usb_enum_step2 (void)
 step2 of enumeration - IDLE 20ms More...
 
static void main_usb_enum_step3 (void)
 step3 of enumeration - wait a USB reset More...
 
static void main_usb_enum_step4 (void)
 step4 of enumeration - IDLE 100ms and initialize control endpoint More...
 
static void main_usb_enum_step5 (void)
 step5 of enumeration - Get incomplete USB device descriptor More...
 
static void main_usb_enum_step6 (void)
 step6 of enumeration - IDLE 20ms More...
 
static void main_usb_enum_step7 (void)
 step7 of enumeration - wait a USB reset More...
 
static void main_usb_enum_step8 (void)
 step8 of enumeration - IDLE 100ms More...
 
static void main_usb_enum_step9 (void)
 step9 of enumeration - Reception of the Set address setup request More...
 
static void main_usb_enum_step10 (void)
 step10 of enumeration - IDLE 20ms More...
 
static void main_usb_enum_step11 (void)
 step11 of enumeration - Reception of the full Get device descriptor More...
 
static void main_usb_enum_step12 (void)
 step12 of enumeration - Get first struct of USB configuration descriptor More...
 
static void main_usb_enum_step13 (void)
 step13 of enumeration - Get all USB configuration descriptor More...
 
static void main_usb_enum_step14 (void)
 step14 of enumeration - Reception of the Set configuration setup request More...
 
Tests of USB Host stack core
static void main_test1 (void)
 Test 1 - Attach and no response. More...
 
static void main_test2 (void)
 Test 2 - Disable endpoint 0 after first setup packet. More...
 
static void main_test3 (void)
 Test 3 - Detach after first setup packet. More...
 
static void main_test4 (void)
 Test 4 - No response data (NACK IN) after first setup packet. More...
 
static void main_test5 (void)
 Test 5 - Detach after IN data phase of first setup request. More...
 
static void main_test6 (void)
 Test 6 - Wrong value in first setup request get descriptor. More...
 
static void main_test7 (void)
 Test 7 - Detach before reset. More...
 
static void main_test8 (void)
 Test 8 - Detach during reset after first setup request get descriptor. More...
 
static void main_test9 (void)
 Test 9 - Detach after reset after first setup request get descriptor. More...
 
static void main_test10 (void)
 Test 10 - No send ZLP (NAK IN) after second setup packet (set address) More...
 
static void main_test11 (void)
 Test 11 - Wrong value in first setup request get configuration 0. More...
 
static void main_test12 (void)
 Test 12 - Wrong transfer value in second setup request get configuration 0. More...
 
static void main_test13 (void)
 Test 13 - To high power consumption in setup request get configuration 0. More...
 
static void main_test14 (void)
 Test 14 - No supported interface (!=HID mouse) More...
 
static void main_test15 (void)
 Test 15 - HID mouse with too large endpoint size (USBB hardware limit) More...
 
static void main_test16 (void)
 Test 16 - Stall SET CONFIGURATION (with configuration in SELF + 300mA -> must be pass) More...
 
static void main_test17 (void)
 Test 17 - Enumeration success (with configuration in BUS + 100mA -> must be pass) More...
 
static void main_test18 (void)
 Test 18 - Test downstream resume (from USB host) More...
 
static void main_test19 (void)
 Test 19 - Test disconnection during downstream resume (from USB host) More...
 
static void main_test20 (void)
 Test 20 - Test upstream resume (from USB device) More...
 
static void main_test21 (void)
 Test 21 - Test detach during suspend. More...
 

Variables

static main_conf_desc_t main_conf_desc
 USB HID mouse configuration descriptor default definition. More...
 
COMPILER_WORD_ALIGNED
usb_dev_desc_t 
main_device_desc
 USB HID mouse device descriptor. More...
 
static usb_setup_req_t main_setup_packet
 
static uint8_t speed
 USB test speed (0:LS, 1:FS, 2:HS) More...
 

#define SAML21_USB_TST
#define TST_15_DIS
#define TST_DETACH_DELAY   800

Referenced by main_detach().

#define USB_DEVICE_MAJOR_VERSION   1
#define USB_DEVICE_MINOR_VERSION   0
#define USB_DEVICE_POWER   100
#define USB_DEVICE_PRODUCT_ID   USB_PID_ATMEL_ASF_HIDMOUSE
#define USB_DEVICE_VENDOR_ID   USB_VID_ATMEL

static void main_detach ( void  )
static
static void main_test1 ( void  )
static
static void main_test18 ( void  )
static

Test 18 - Test downstream resume (from USB host)

References main_usb_send_in(), main_usb_wait_setup_packet(), main_usb_wait_suspend(), and main_usb_wait_wakeup().

Referenced by main().

static void main_test19 ( void  )
static

Test 19 - Test disconnection during downstream resume (from USB host)

References main_detach(), main_usb_wait_suspend(), and main_usb_wait_wakeup().

Referenced by main().

static void main_test2 ( void  )
static
static void main_test21 ( void  )
static

Test 21 - Test detach during suspend.

References delay_ms, main_detach(), and main_usb_wait_suspend().

Referenced by main().

static void main_test3 ( void  )
static
static void main_test4 ( void  )
static

Test 4 - No response data (NACK IN) after first setup packet.

References main_detach(), main_otg_init(), main_usb_enum_step1(), main_usb_enum_step2(), main_usb_enum_step3(), main_usb_enum_step4(), main_usb_wait_setup_packet(), and udd_attach_device.

Referenced by main().

static void main_test7 ( void  )
static
static void main_test8 ( void  )
static
static void main_test9 ( void  )
static
static void main_usb_enable_ctrl_ep ( uint8_t  address,
uint8_t  ep_size 
)
static

Allocation of the control endpoint.

Parameters
addressUSB address of endpoint
ep_sizecontrol endpoint size

References udd_allocate_memory, udd_configure_address, udd_configure_endpoint, udd_disable_address, udd_disable_endpoint, udd_enable_address, udd_enable_endpoint, udd_unallocate_memory, and USB_EP_TYPE_CONTROL.

Referenced by main_test10(), main_usb_enum_step4(), and main_usb_enum_step9().

static void main_usb_enum_step10 ( void  )
static

step10 of enumeration - IDLE 20ms

References main_usb_wait_sof().

Referenced by main_test11(), main_test12(), main_test13(), main_test14(), main_test15(), main_test16(), main_test17(), and main_test20().

static void main_usb_enum_step11 ( void  )
static
static void main_usb_enum_step14 ( void  )
static

step14 of enumeration - Reception of the Set configuration setup request

References main_usb_send_in(), and main_usb_wait_setup_packet().

Referenced by main_test17(), and main_test20().

static void main_usb_enum_step4 ( void  )
static
static void main_usb_enum_step6 ( void  )
static
static void main_usb_enum_step7 ( void  )
static
static void main_usb_enum_step8 ( void  )
static
static void main_usb_enum_step9 ( void  )
static
static void main_usb_send_in ( uint8_t *  payload,
uint8_t  size 
)
static
static void main_usb_stall_after_setup_packet ( void  )
static

Enables the control endpoint STALL after a SETUP packet reception.

References udd_ack_setup_received, and udd_enable_stall_handshake.

Referenced by main_test16().

static void main_usb_wait_end_of_reset ( void  )
static

Waits the end of USB reset signal.

References Is_udd_reset, and udd_ack_reset.

Referenced by main_usb_enum_step1(), main_usb_enum_step3(), and main_usb_enum_step7().

static void main_usb_wait_out ( uint8_t *  payload,
uint8_t  size 
)
static
static void main_usb_wait_reset_start ( void  )
static

Waits the start of USB reset signal This routine waits the end of SOF.

References delay_ms, Is_udd_sof, and udd_ack_sof.

Referenced by main_test8().

static void main_usb_wait_resume ( void  )
static

Waits a USB downstream resume signal from host.

References Is_udd_resume, and udd_ack_resume.

static void main_usb_wait_sof ( void  )
static
static void main_usb_wait_suspend ( void  )
static
static void main_usb_wait_wakeup ( void  )
static

Waits a USB wakeup signal.

References Is_udd_wake_up, and udd_ack_wake_up.

Referenced by main_test18(), main_test19(), and main_test20().

main_conf_desc_t main_conf_desc
static

USB HID mouse configuration descriptor default definition.

Initial value:
= {
.bLength = sizeof(usb_dev_desc_t),
.bDescriptorType = USB_DT_DEVICE,
.bcdUSB = LE16(USB_V2_0),
.bDeviceClass = 0,
.bDeviceSubClass = 0,
.bDeviceProtocol = 0,
.bMaxPacketSize0 = 0,
.idVendor = LE16(USB_DEVICE_VENDOR_ID),
.idProduct = LE16(USB_DEVICE_PRODUCT_ID),
.bcdDevice = LE16((USB_DEVICE_MAJOR_VERSION << 8)
.iManufacturer = 0,
.iProduct = 0,
.iSerialNumber = 0,
.bNumConfigurations = 1
}
#define USB_V2_0
Value for field bcdUSB.
Definition: usb_protocol.h:56
Standard USB device descriptor structure.
Definition: usb_protocol.h:313
#define LE16(x)
Definition: compiler.h:1105
Definition: usb_protocol.h:190
#define USB_DEVICE_PRODUCT_ID
Definition: unit_tests.c:98
#define USB_DEVICE_MINOR_VERSION
Definition: unit_tests.c:100
#define USB_DEVICE_MAJOR_VERSION
Definition: unit_tests.c:99
#define USB_DEVICE_VENDOR_ID
Definition: unit_tests.c:97

USB HID mouse device descriptor.

usb_setup_req_t main_setup_packet
static
uint8_t speed
static