update nrf52840 examples

This commit is contained in:
hathach 2018-04-18 18:05:08 +07:00
parent a9ada8ea5e
commit 8c0a5f7d3b
5 changed files with 243 additions and 174 deletions

View File

@ -98,47 +98,62 @@ void msc_app_umount(uint8_t rhport)
} }
msc_csw_status_t tud_msc_scsi_cb (uint8_t rhport, uint8_t lun, uint8_t scsi_cmd[16], void const ** pp_buffer, uint16_t* p_length) bool tud_msc_scsi_cb (uint8_t rhport, uint8_t lun, uint8_t scsi_cmd[16], void* buffer, uint16_t* p_len)
{ {
// read10 & write10 has their own callback and MUST not be handled here // read10 & write10 has their own callback and MUST not be handled here
void* bufptr = NULL;
uint16_t buflen = 0;
switch (scsi_cmd[0]) switch (scsi_cmd[0])
{ {
case SCSI_CMD_INQUIRY: case SCSI_CMD_INQUIRY:
(*pp_buffer) = &mscd_inquiry_data; bufptr = &mscd_inquiry_data;
(*p_length) = sizeof(scsi_inquiry_data_t); buflen = sizeof(scsi_inquiry_data_t);
break; break;
case SCSI_CMD_READ_CAPACITY_10: case SCSI_CMD_READ_CAPACITY_10:
(*pp_buffer) = &mscd_read_capacity10_data; bufptr = &mscd_read_capacity10_data;
(*p_length) = sizeof(scsi_read_capacity10_data_t); buflen = sizeof(scsi_read_capacity10_data_t);
break; break;
case SCSI_CMD_REQUEST_SENSE: case SCSI_CMD_REQUEST_SENSE:
(*pp_buffer) = &mscd_sense_data; bufptr = &mscd_sense_data;
(*p_length) = sizeof(scsi_sense_fixed_data_t); buflen = sizeof(scsi_sense_fixed_data_t);
break; break;
case SCSI_CMD_READ_FORMAT_CAPACITY: case SCSI_CMD_READ_FORMAT_CAPACITY:
(*pp_buffer) = &mscd_format_capacity_data; bufptr = &mscd_format_capacity_data;
(*p_length) = sizeof(scsi_read_format_capacity_data_t); buflen = sizeof(scsi_read_format_capacity_data_t);
break; break;
case SCSI_CMD_MODE_SENSE_6: case SCSI_CMD_MODE_SENSE_6:
(*pp_buffer) = &msc_dev_mode_para; bufptr = &msc_dev_mode_para;
(*p_length) = sizeof(msc_dev_mode_para); buflen = sizeof(msc_dev_mode_para);
break; break;
case SCSI_CMD_TEST_UNIT_READY: case SCSI_CMD_TEST_UNIT_READY:
(*pp_buffer) = NULL; bufptr = NULL;
(*p_length) = 0; buflen= 0;
break; break;
case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL: case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
(*pp_buffer) = NULL; bufptr = NULL;
(*p_length) = 0; buflen= 0;
break; break;
default: return MSC_CSW_STATUS_FAILED; default:
(*p_len) = 0;
return false;
}
if ( bufptr && buflen )
{
// Response len must not larger than expected from host
TU_ASSERT( (*p_len) >= buflen );
memcpy(buffer, bufptr, buflen);
(*p_len) = buflen;
} }
//------------- clear sense data if it is not request sense command -------------// //------------- clear sense data if it is not request sense command -------------//
@ -149,7 +164,7 @@ msc_csw_status_t tud_msc_scsi_cb (uint8_t rhport, uint8_t lun, uint8_t scsi_cmd[
mscd_sense_data.additional_sense_qualifier = 0; mscd_sense_data.additional_sense_qualifier = 0;
} }
return MSC_CSW_STATUS_PASSED; return true;
} }
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+

View File

@ -91,13 +91,13 @@ uint8_t msc_device_ramdisk[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] =
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// IMPLEMENTATION // IMPLEMENTATION
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
uint16_t tud_msc_read10_cb (uint8_t rhport, uint8_t lun, void** pp_buffer, uint32_t lba, uint16_t block_count) uint16_t tud_msc_read10_cb (uint8_t rhport, uint8_t lun, uint32_t lba, uint16_t block_count, void** pp_buffer)
{ {
(*pp_buffer) = msc_device_ramdisk[lba]; (*pp_buffer) = msc_device_ramdisk[lba];
return min16_of(block_count, DISK_BLOCK_NUM); return min16_of(block_count, DISK_BLOCK_NUM);
} }
uint16_t tud_msc_write10_cb(uint8_t rhport, uint8_t lun, void** pp_buffer, uint32_t lba, uint16_t block_count) uint16_t tud_msc_write10_cb(uint8_t rhport, uint8_t lun, uint32_t lba, uint16_t block_count, void** pp_buffer)
{ {
(*pp_buffer) = msc_device_ramdisk[lba]; (*pp_buffer) = msc_device_ramdisk[lba];

View File

@ -46,10 +46,14 @@
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// CONTROLLER CONFIGURATION // CONTROLLER CONFIGURATION
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
//#define CFG_TUSB_MCU will be passed from IDE/command line for easy board/mcu switching
#define CFG_TUSB_MCU OPT_MCU_NRF5X #define CFG_TUSB_MCU OPT_MCU_NRF5X
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE) #define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
#define CFG_TUSB_DEBUG 2
#define CFG_TUSB_OS OPT_OS_NONE // be passed from IDE/command line for easy project switching
//#define CFG_TUD_TASK_PRIO 0 // be passed from IDE/command line for easy project switching
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// DEVICE CONFIGURATION // DEVICE CONFIGURATION
@ -63,27 +67,24 @@
#define CFG_TUD_MSC 1 #define CFG_TUD_MSC 1
#define CFG_TUD_CDC 1 #define CFG_TUD_CDC 1
//--------------------------------------------------------------------+ /*------------------------------------------------------------------*/
// COMMON CONFIGURATION /* CLASS DRIVER
//--------------------------------------------------------------------+ *------------------------------------------------------------------*/
#define CFG_TUSB_DEBUG 2
// FIFO size of CDC TX and RX
#define CFG_TUD_CDC_BUFSIZE 64
// TX is sent automatically every Start of Frame event.
// If not enabled, application must call tud_cdc_flush() periodically
#define CFG_TUD_CDC_FLUSH_ON_SOF 1
#define CFG_TUSB_OS OPT_OS_NONE // be passed from IDE/command line for easy project switching
//#define CFG_TUD_TASK_PRIO 0 // be passed from IDE/command line for easy project switching
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// USB RAM PLACEMENT // USB RAM PLACEMENT
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
#define CFG_TUSB_ATTR_USBRAM #define CFG_TUSB_ATTR_USBRAM
// LPC11uxx and LPC13uxx requires each buffer has to be 64-byte alignment
#if CFG_TUSB_MCU == OPT_MCU_LPC11UXX || CFG_TUSB_MCU == OPT_MCU_LPC13UXX
#define CFG_TUSB_MEM_ALIGN ATTR_ALIGNED(64)
#elif CFG_TUSB_MCU == OPT_MCU_NRF5X
#define CFG_TUSB_MEM_ALIGN ATTR_ALIGNED(4) #define CFG_TUSB_MEM_ALIGN ATTR_ALIGNED(4)
#else
#define CFG_TUSB_MEM_ALIGN
#endif
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -1,5 +1,5 @@
/** /**
* Copyright (c) 2017 - 2017, Nordic Semiconductor ASA * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA
* *
* All rights reserved. * All rights reserved.
* *
@ -85,6 +85,19 @@ static inline bool nrf_drv_usbd_errata_type_52840_proto1(void)
( ((*(uint32_t *)0xF0000FEC) & 0xF0) == 0x00 ) ); ( ((*(uint32_t *)0xF0000FEC) & 0xF0) == 0x00 ) );
} }
/**
* @brief Internal auxiliary function to check if the program is running on first final product of
* NRF52840 chip
* @retval true It is NRF52480 chip and it is first final product
* @retval false It is other chip
*/
static inline bool nrf_drv_usbd_errata_type_52840_fp1(void)
{
return ( nrf_drv_usbd_errata_type_52840() &&
( ((*(uint32_t *)0xF0000FE8) & 0xF0) == 0x10 ) &&
( ((*(uint32_t *)0xF0000FEC) & 0xF0) == 0x00 ) );
}
/** /**
* @brief Function to check if chip requires errata 104 * @brief Function to check if chip requires errata 104
* *
@ -124,6 +137,32 @@ static inline bool nrf_drv_usbd_errata_166(void)
return NRF_DRV_USBD_ERRATA_ENABLE && true; return NRF_DRV_USBD_ERRATA_ENABLE && true;
} }
/**
* @brief Function to check if chip requires errata 171
*
* Errata: USBD might not reach its active state.
*
* @retval true Errata should be implemented
* @retval false Errata should not be implemented
*/
static inline bool nrf_drv_usbd_errata_171(void)
{
return NRF_DRV_USBD_ERRATA_ENABLE && true;
}
/**
* @brief Function to check if chip requires errata 187
*
* Errata: USB cannot be enabled
*
* @retval true Errata should be implemented
* @retval false Errata should not be implemented
*/
static inline bool nrf_drv_usbd_errata_187(void)
{
return NRF_DRV_USBD_ERRATA_ENABLE && nrf_drv_usbd_errata_type_52840_fp1();
}
/** /**
* @brief Function to check if chip requires errata ??? * @brief Function to check if chip requires errata ???
* *
@ -137,5 +176,18 @@ static inline bool nrf_drv_usbd_errata_sizeepout_rw(void)
return NRF_DRV_USBD_ERRATA_ENABLE && nrf_drv_usbd_errata_type_52840_proto1(); return NRF_DRV_USBD_ERRATA_ENABLE && nrf_drv_usbd_errata_type_52840_proto1();
} }
/**
* @brief Function to check if chip requires errata 199
*
* Errata: USBD cannot receive tasks during DMA
*
* @retval true Errata should be implemented
* @retval false Errata should not be implemented
*/
static inline bool nrf_drv_usb_errata_199(void)
{
return NRF_DRV_USBD_ERRATA_ENABLE && true;
}
/** @} */ /** @} */
#endif /* NRF_DRV_USBD_ERRATA_H__ */ #endif /* NRF_DRV_USBD_ERRATA_H__ */

View File

@ -60,17 +60,6 @@
#define USB_NVIC_PRIO 7 #define USB_NVIC_PRIO 7
// TODO must cover SD present but not enabled
#ifdef SOFTDEVICE_PRESENT
#define POWER_DETECT NRF_EVT_POWER_USB_DETECTED
#define POWER_READY NRF_EVT_POWER_USB_POWER_READY
#define POWER_REMOVE NRF_EVT_POWER_USB_REMOVED
#else
#define POWER_DETECT NRF_DRV_POWER_USB_EVT_DETECTED
#define POWER_READY NRF_DRV_POWER_USB_EVT_READY
#define POWER_REMOVE NRF_DRV_POWER_USB_EVT_REMOVED
#endif
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/
/* FUNCTION DECLARATION /* FUNCTION DECLARATION
*------------------------------------------------------------------*/ *------------------------------------------------------------------*/
@ -115,19 +104,16 @@ bool tusb_hal_init(void)
// power_usb_event_handler must be called by SOC event hanlder // power_usb_event_handler must be called by SOC event hanlder
return true; return true;
} }else
#endif #endif
{
#if 0
// USB Power detection // USB Power detection
const nrf_drv_power_usbevt_config_t config = const nrf_drv_power_usbevt_config_t config =
{ {
.handler = power_usb_event_handler .handler = (nrf_drv_power_usb_event_handler_t) power_usb_event_handler
}; };
return ( NRF_SUCCESS == nrf_drv_power_usbevt_init(&config) ); return ( NRF_SUCCESS == nrf_drv_power_usbevt_init(&config) );
#else }
return true; // TODO remove
#endif
} }
void tusb_hal_int_enable(uint8_t rhport) void tusb_hal_int_enable(uint8_t rhport)
@ -195,9 +181,24 @@ static void hfclk_disable(void)
*------------------------------------------------------------------*/ *------------------------------------------------------------------*/
void power_usb_event_handler(uint32_t event) void power_usb_event_handler(uint32_t event)
{ {
switch ( event ) uint32_t POWER_DETECT, POWER_READY, POWER_REMOVE;
#ifdef SOFTDEVICE_PRESENT
if ( is_sd_enabled() )
{
POWER_DETECT = NRF_EVT_POWER_USB_DETECTED;
POWER_READY = NRF_EVT_POWER_USB_POWER_READY;
POWER_REMOVE = NRF_EVT_POWER_USB_REMOVED;
}else
#endif
{
POWER_DETECT = NRF_DRV_POWER_USB_EVT_DETECTED;
POWER_READY = NRF_DRV_POWER_USB_EVT_READY;
POWER_REMOVE = NRF_DRV_POWER_USB_EVT_REMOVED;
}
if ( POWER_DETECT == event )
{ {
case POWER_DETECT:
if ( !NRF_USBD->ENABLE ) if ( !NRF_USBD->ENABLE )
{ {
/* Prepare for READY event receiving */ /* Prepare for READY event receiving */
@ -243,9 +244,9 @@ void power_usb_event_handler(uint32_t event)
// Enable HFCLK // Enable HFCLK
hfclk_enable(); hfclk_enable();
} }
break; }
else if ( POWER_READY == event )
case POWER_READY: {
/* Waiting for USBD peripheral enabled */ /* Waiting for USBD peripheral enabled */
while ( !(NRF_USBD_EVENTCAUSE_READY_MASK & NRF_USBD->EVENTCAUSE) ) { } while ( !(NRF_USBD_EVENTCAUSE_READY_MASK & NRF_USBD->EVENTCAUSE) ) { }
nrf_usbd_eventcause_clear(NRF_USBD_EVENTCAUSE_READY_MASK); nrf_usbd_eventcause_clear(NRF_USBD_EVENTCAUSE_READY_MASK);
@ -313,9 +314,9 @@ void power_usb_event_handler(uint32_t event)
// Enable pull up // Enable pull up
nrf_usbd_pullup_enable(); nrf_usbd_pullup_enable();
break; }
else if (POWER_REMOVE == event )
case POWER_REMOVE: {
if ( NRF_USBD->ENABLE ) if ( NRF_USBD->ENABLE )
{ {
// Abort all transfers // Abort all transfers
@ -332,9 +333,9 @@ void power_usb_event_handler(uint32_t event)
nrf_usbd_disable(); nrf_usbd_disable();
hfclk_disable(); hfclk_disable();
} }
break; }
else
default: break; {
} }
} }