espressif_tinyusb/src/portable/nordic/nrf5x/hal_nrf5x.c

350 lines
9.9 KiB
C
Raw Normal View History

/**************************************************************************/
/*!
2018-03-26 17:48:53 +02:00
@file hal_nrf5x.c
@author hathach
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2018, hathach (tinyusb.org)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**************************************************************************/
2018-04-12 08:14:59 +02:00
#include "tusb_option.h"
2018-07-23 10:25:45 +02:00
#if TUSB_OPT_DEVICE_ENABLED && CFG_TUSB_MCU == OPT_MCU_NRF5X
2018-03-26 17:54:34 +02:00
#include "nrf.h"
#include "nrf_gpio.h"
2018-03-20 14:57:49 +01:00
#include "nrf_clock.h"
2018-04-04 11:48:52 +02:00
#include "nrf_usbd.h"
2018-04-04 15:02:54 +02:00
#ifdef SOFTDEVICE_PRESENT
#include "nrf_sdm.h"
#include "nrf_soc.h"
2018-07-02 06:09:22 +02:00
// TODO fully move to nrfx
enum {
NRFX_POWER_USB_EVT_DETECTED, /**< USB power detected on the connector (plugged in). */
NRFX_POWER_USB_EVT_REMOVED, /**< USB power removed from the connector. */
NRFX_POWER_USB_EVT_READY /**< USB power regulator ready. */
};
2018-04-04 15:02:54 +02:00
#else
#include "nrfx_power.h"
2018-04-04 15:02:54 +02:00
#endif
#include "tusb_hal.h"
/*------------------------------------------------------------------*/
/* MACRO TYPEDEF CONSTANT ENUM
*------------------------------------------------------------------*/
2018-04-17 12:06:40 +02:00
#define USB_NVIC_PRIO 7
/*------------------------------------------------------------------*/
/* FUNCTION DECLARATION
*------------------------------------------------------------------*/
2018-05-14 08:40:41 +02:00
void tusb_hal_nrf_power_event(uint32_t event);
/*------------------------------------------------------------------*/
/* Errata check from nrf_drv_usbd_errata.h
*------------------------------------------------------------------*/
/**
* @brief Internal auxiliary function to check if the program is running on NRF52840 chip
* @retval true It is NRF52480 chip
* @retval false It is other chip
*/
static inline bool nrf_drv_usbd_errata_type_52840(void)
{
return ((((*(uint32_t *)0xF0000FE0) & 0xFF) == 0x08) &&
(((*(uint32_t *)0xF0000FE4) & 0x0F) == 0x0));
}
/**
* @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) == 0x20 ) &&
( ((*(uint32_t *)0xF0000FEC) & 0xF0) == 0x00 ) );
}
static inline bool nrf_drv_usbd_errata_166(void)
{
return true;
}
static inline bool nrf_drv_usbd_errata_171(void)
{
return true;
}
static inline bool nrf_drv_usbd_errata_187(void)
{
return nrf_drv_usbd_errata_type_52840_fp1();
}
2018-05-14 08:40:41 +02:00
/*------------------------------------------------------------------*/
/* HFCLK helper
*------------------------------------------------------------------*/
2018-04-04 15:02:54 +02:00
2018-07-02 12:32:09 +02:00
#ifdef SOFTDEVICE_PRESENT
2018-04-04 15:02:54 +02:00
// check if SD is present and enabled
static bool is_sd_enabled(void)
{
uint8_t sd_en = false;
(void) sd_softdevice_is_enabled(&sd_en);
return sd_en;
}
2018-07-02 12:32:09 +02:00
#endif
2018-04-04 15:02:54 +02:00
2018-04-04 11:48:52 +02:00
static bool hfclk_running(void)
{
#ifdef SOFTDEVICE_PRESENT
if ( is_sd_enabled() )
{
uint32_t is_running;
(void) sd_clock_hfclk_is_running(&is_running);
return (is_running ? true : false);
}
#endif
return nrf_clock_hf_is_running(NRF_CLOCK_HFCLK_HIGH_ACCURACY);
}
static void hfclk_enable(void)
{
// already running, nothing to do
if ( hfclk_running() ) return;
#ifdef SOFTDEVICE_PRESENT
if ( is_sd_enabled() )
{
(void)sd_clock_hfclk_request();
return;
}
#endif
nrf_clock_event_clear(NRF_CLOCK_EVENT_HFCLKSTARTED);
nrf_clock_task_trigger(NRF_CLOCK_TASK_HFCLKSTART);
}
static void hfclk_disable(void)
{
#ifdef SOFTDEVICE_PRESENT
if ( is_sd_enabled() )
{
(void)sd_clock_hfclk_release();
return;
}
#endif
nrf_clock_task_trigger(NRF_CLOCK_TASK_HFCLKSTOP);
}
2018-05-14 08:40:41 +02:00
2018-04-04 15:02:54 +02:00
/*------------------------------------------------------------------*/
2018-05-14 08:40:41 +02:00
/* TUSB HAL
2018-04-04 15:02:54 +02:00
*------------------------------------------------------------------*/
2018-05-14 08:40:41 +02:00
bool tusb_hal_init(void)
2018-04-04 11:48:52 +02:00
{
return true;
2018-05-14 08:40:41 +02:00
}
2018-04-04 11:48:52 +02:00
2018-05-14 08:40:41 +02:00
void tusb_hal_int_enable(uint8_t rhport)
{
(void) rhport;
NVIC_EnableIRQ(USBD_IRQn);
}
2018-05-14 08:40:41 +02:00
void tusb_hal_int_disable(uint8_t rhport)
{
(void) rhport;
NVIC_DisableIRQ(USBD_IRQn);
}
2018-04-18 13:05:08 +02:00
2018-05-14 08:40:41 +02:00
/*------------------------------------------------------------------*/
/* Controller Start up Sequence (USBD 51.4 specs)
*------------------------------------------------------------------*/
// tusb_hal_nrf_power_event must be called by SOC event handler
2018-06-06 08:40:02 +02:00
void tusb_hal_nrf_power_event (uint32_t event)
2018-05-14 08:40:41 +02:00
{
switch ( event )
{
case NRFX_POWER_USB_EVT_DETECTED:
if ( !NRF_USBD->ENABLE )
2018-04-18 13:05:08 +02:00
{
2018-05-14 08:40:41 +02:00
/* Prepare for READY event receiving */
nrf_usbd_eventcause_clear(NRF_USBD_EVENTCAUSE_READY_MASK);
/* Enable the peripheral */
2018-06-06 08:40:02 +02:00
// ERRATA 171, 187, 166
// Somehow Errata 187 check failed for pca10056 1.0.0 (2018.19)
if ( nrf_drv_usbd_errata_187() )
2018-04-18 13:05:08 +02:00
{
// CRITICAL_REGION_ENTER();
2018-06-06 08:40:02 +02:00
if ( *((volatile uint32_t *) (0x4006EC00)) == 0x00000000 )
2018-05-14 08:40:41 +02:00
{
2018-06-06 08:40:02 +02:00
*((volatile uint32_t *) (0x4006EC00)) = 0x00009375;
*((volatile uint32_t *) (0x4006ED14)) = 0x00000003;
*((volatile uint32_t *) (0x4006EC00)) = 0x00009375;
2018-05-14 08:40:41 +02:00
}
else
{
2018-06-06 08:40:02 +02:00
*((volatile uint32_t *) (0x4006ED14)) = 0x00000003;
2018-05-14 08:40:41 +02:00
}
// CRITICAL_REGION_EXIT();
}
2018-05-14 08:40:41 +02:00
2018-06-06 08:40:02 +02:00
if ( nrf_drv_usbd_errata_171() )
{
// CRITICAL_REGION_ENTER();
2018-06-06 08:40:02 +02:00
if ( *((volatile uint32_t *) (0x4006EC00)) == 0x00000000 )
2018-05-14 08:40:41 +02:00
{
2018-06-06 08:40:02 +02:00
*((volatile uint32_t *) (0x4006EC00)) = 0x00009375;
*((volatile uint32_t *) (0x4006EC14)) = 0x000000C0;
*((volatile uint32_t *) (0x4006EC00)) = 0x00009375;
2018-05-14 08:40:41 +02:00
}
else
{
2018-06-06 08:40:02 +02:00
*((volatile uint32_t *) (0x4006EC14)) = 0x000000C0;
2018-05-14 08:40:41 +02:00
}
// CRITICAL_REGION_EXIT();
}
2018-05-14 08:40:41 +02:00
nrf_usbd_enable();
// Enable HFCLK
hfclk_enable();
2018-04-04 11:48:52 +02:00
}
2018-05-14 08:40:41 +02:00
break;
case NRFX_POWER_USB_EVT_READY:
/* Waiting for USBD peripheral enabled */
while ( !(USBD_EVENTCAUSE_READY_Msk & NRF_USBD->EVENTCAUSE) ) { }
nrf_usbd_eventcause_clear(USBD_EVENTCAUSE_READY_Msk);
nrf_usbd_event_clear(USBD_EVENTCAUSE_READY_Msk);
2018-04-04 11:48:52 +02:00
2018-06-06 08:40:02 +02:00
if ( nrf_drv_usbd_errata_171() )
{
// CRITICAL_REGION_ENTER();
2018-06-06 08:40:02 +02:00
if ( *((volatile uint32_t *) (0x4006EC00)) == 0x00000000 )
{
*((volatile uint32_t *) (0x4006EC00)) = 0x00009375;
*((volatile uint32_t *) (0x4006EC14)) = 0x00000000;
*((volatile uint32_t *) (0x4006EC00)) = 0x00009375;
}
else
{
*((volatile uint32_t *) (0x4006EC14)) = 0x00000000;
}
2018-05-14 08:40:41 +02:00
// CRITICAL_REGION_EXIT();
}
// Somehow Errata 187 check failed for pca10056 1.0.0 (2018.19)
if ( nrf_drv_usbd_errata_187() )
2018-05-14 08:40:41 +02:00
{
// CRITICAL_REGION_ENTER();
2018-06-06 08:40:02 +02:00
if ( *((volatile uint32_t *) (0x4006EC00)) == 0x00000000 )
{
*((volatile uint32_t *) (0x4006EC00)) = 0x00009375;
*((volatile uint32_t *) (0x4006ED14)) = 0x00000000;
*((volatile uint32_t *) (0x4006EC00)) = 0x00009375;
}
else
{
*((volatile uint32_t *) (0x4006ED14)) = 0x00000000;
}
// CRITICAL_REGION_EXIT();
2018-05-14 08:40:41 +02:00
}
2018-05-14 08:40:41 +02:00
if ( nrf_drv_usbd_errata_166() )
{
*((volatile uint32_t *) (NRF_USBD_BASE + 0x800)) = 0x7E3;
*((volatile uint32_t *) (NRF_USBD_BASE + 0x804)) = 0x40;
2018-04-04 11:48:52 +02:00
2018-06-06 08:40:02 +02:00
__ISB();
__DSB();
2018-05-14 08:40:41 +02:00
}
2018-04-04 11:48:52 +02:00
2018-05-14 08:40:41 +02:00
nrf_usbd_isosplit_set(NRF_USBD_ISOSPLIT_Half);
2018-04-04 11:48:52 +02:00
2018-05-14 08:40:41 +02:00
// Enable interrupt. SOF is used as CDC auto flush
2018-07-02 12:32:09 +02:00
NRF_USBD->INTENSET = USBD_INTEN_USBRESET_Msk | USBD_INTEN_USBEVENT_Msk | USBD_INTEN_EPDATA_Msk |
USBD_INTEN_EP0SETUP_Msk | USBD_INTEN_EP0DATADONE_Msk | USBD_INTEN_ENDEPIN0_Msk | USBD_INTEN_ENDEPOUT0_Msk;
#if CFG_TUD_CDC && CFG_TUD_CDC_FLUSH_ON_SOF
NRF_USBD->INTENSET |= USBD_INTEN_SOF_Msk;
#endif
2018-04-04 11:48:52 +02:00
2018-05-14 08:40:41 +02:00
// Enable interrupt, Priorities 0,1,4,5 (nRF52) are reserved for SoftDevice
NVIC_SetPriority(USBD_IRQn, USB_NVIC_PRIO);
NVIC_ClearPendingIRQ(USBD_IRQn);
NVIC_EnableIRQ(USBD_IRQn);
2018-04-04 11:48:52 +02:00
2018-05-14 08:40:41 +02:00
// Wait for HFCLK
2018-06-06 08:40:02 +02:00
while ( !hfclk_running() )
{
}
2018-04-05 13:36:59 +02:00
2018-05-14 08:40:41 +02:00
// Enable pull up
nrf_usbd_pullup_enable();
break;
2018-04-04 11:48:52 +02:00
2018-05-14 08:40:41 +02:00
case NRFX_POWER_USB_EVT_REMOVED:
if ( NRF_USBD->ENABLE )
{
// Abort all transfers
2018-04-04 11:48:52 +02:00
2018-05-14 08:40:41 +02:00
// Disable pull up
nrf_usbd_pullup_disable();
2018-04-04 11:48:52 +02:00
2018-05-14 08:40:41 +02:00
// Disable Interrupt
NVIC_DisableIRQ(USBD_IRQn);
2018-04-04 11:48:52 +02:00
2018-05-14 08:40:41 +02:00
// disable all interrupt
NRF_USBD->INTENCLR = NRF_USBD->INTEN;
2018-04-04 11:48:52 +02:00
2018-05-14 08:40:41 +02:00
nrf_usbd_disable();
hfclk_disable();
}
break;
2018-04-04 11:48:52 +02:00
2018-05-14 08:40:41 +02:00
default: break;
2018-04-04 11:48:52 +02:00
}
}
2018-03-26 17:54:34 +02:00
#endif