[nrf52] First commit for platform support

This commit is contained in:
Eric Holland
2016-06-16 10:41:04 -07:00
parent 26f1d1edb7
commit 1491809fd0
15 changed files with 18061 additions and 0 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,69 @@
/* Copyright (c) 2015, Nordic Semiconductor ASA
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * 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.
*
* * Neither the name of Nordic Semiconductor ASA 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 AND CONTRIBUTORS "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 OR CONTRIBUTORS 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.
*
*/
#ifndef SYSTEM_NRF52_H
#define SYSTEM_NRF52_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
extern uint32_t SystemCoreClock; /*!< System Clock Frequency (Core Clock) */
/**
* Initialize the system
*
* @param none
* @return none
*
* @brief Setup the microcontroller system.
* Initialize the System and update the SystemCoreClock variable.
*/
extern void SystemInit (void);
/**
* Update SystemCoreClock variable
*
* @param none
* @return none
*
* @brief Updates the SystemCoreClock with current core Clock
* retrieved from cpu registers.
*/
extern void SystemCoreClockUpdate (void);
#ifdef __cplusplus
}
#endif
#endif /* SYSTEM_NRF52_H */

12
external/platform/nrf52/rules.mk vendored Normal file
View File

@@ -0,0 +1,12 @@
LOCAL_DIR := $(GET_LOCAL_DIR)
MODULE := $(LOCAL_DIR)
GLOBAL_INCLUDES += $(LOCAL_DIR)
MODULE_SRCS += \
$(LOCAL_DIR)/system_nrf52.c \
include make/module.mk

301
external/platform/nrf52/system_nrf52.c vendored Executable file
View File

@@ -0,0 +1,301 @@
/* Copyright (c) 2015, Nordic Semiconductor ASA
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * 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.
*
* * Neither the name of Nordic Semiconductor ASA 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 AND CONTRIBUTORS "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 OR CONTRIBUTORS 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.
*
*/
#include <stdint.h>
#include <stdbool.h>
#include "nrf.h"
#include "system_nrf52.h"
/*lint ++flb "Enter library region" */
#define __SYSTEM_CLOCK_64M (64000000UL)
static bool errata_16(void);
static bool errata_31(void);
static bool errata_32(void);
static bool errata_36(void);
static bool errata_37(void);
static bool errata_57(void);
static bool errata_66(void);
#if defined ( __CC_ARM )
uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK_64M;
#elif defined ( __ICCARM__ )
__root uint32_t SystemCoreClock = __SYSTEM_CLOCK_64M;
#elif defined ( __GNUC__ )
uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK_64M;
#endif
void SystemCoreClockUpdate(void)
{
SystemCoreClock = __SYSTEM_CLOCK_64M;
}
void SystemInit(void)
{
/* Workaround for Errata 16 "System: RAM may be corrupt on wakeup from CPU IDLE" found at the Errata document
for your device located at https://infocenter.nordicsemi.com/ */
if (errata_16()){
*(volatile uint32_t *)0x4007C074 = 3131961357ul;
}
/* Workaround for Errata 31 "CLOCK: Calibration values are not correctly loaded from FICR at reset" found at the Errata document
for your device located at https://infocenter.nordicsemi.com/ */
if (errata_31()){
*(volatile uint32_t *)0x4000053C = ((*(volatile uint32_t *)0x10000244) & 0x0000E000) >> 13;
}
/* Workaround for Errata 32 "DIF: Debug session automatically enables TracePort pins" found at the Errata document
for your device located at https://infocenter.nordicsemi.com/ */
if (errata_32()){
CoreDebug->DEMCR &= ~CoreDebug_DEMCR_TRCENA_Msk;
}
/* Workaround for Errata 36 "CLOCK: Some registers are not reset when expected" found at the Errata document
for your device located at https://infocenter.nordicsemi.com/ */
if (errata_36()){
NRF_CLOCK->EVENTS_DONE = 0;
NRF_CLOCK->EVENTS_CTTO = 0;
NRF_CLOCK->CTIV = 0;
}
/* Workaround for Errata 37 "RADIO: Encryption engine is slow by default" found at the Errata document
for your device located at https://infocenter.nordicsemi.com/ */
if (errata_37()){
*(volatile uint32_t *)0x400005A0 = 0x3;
}
/* Workaround for Errata 57 "NFCT: NFC Modulation amplitude" found at the Errata document
for your device located at https://infocenter.nordicsemi.com/ */
if (errata_57()){
*(volatile uint32_t *)0x40005610 = 0x00000005;
*(volatile uint32_t *)0x40005688 = 0x00000001;
*(volatile uint32_t *)0x40005618 = 0x00000000;
*(volatile uint32_t *)0x40005614 = 0x0000003F;
}
/* Workaround for Errata 66 "TEMP: Linearity specification not met with default settings" found at the Errata document
for your device located at https://infocenter.nordicsemi.com/ */
if (errata_66()){
NRF_TEMP->A0 = NRF_FICR->TEMP.A0;
NRF_TEMP->A1 = NRF_FICR->TEMP.A1;
NRF_TEMP->A2 = NRF_FICR->TEMP.A2;
NRF_TEMP->A3 = NRF_FICR->TEMP.A3;
NRF_TEMP->A4 = NRF_FICR->TEMP.A4;
NRF_TEMP->A5 = NRF_FICR->TEMP.A5;
NRF_TEMP->B0 = NRF_FICR->TEMP.B0;
NRF_TEMP->B1 = NRF_FICR->TEMP.B1;
NRF_TEMP->B2 = NRF_FICR->TEMP.B2;
NRF_TEMP->B3 = NRF_FICR->TEMP.B3;
NRF_TEMP->B4 = NRF_FICR->TEMP.B4;
NRF_TEMP->B5 = NRF_FICR->TEMP.B5;
NRF_TEMP->T0 = NRF_FICR->TEMP.T0;
NRF_TEMP->T1 = NRF_FICR->TEMP.T1;
NRF_TEMP->T2 = NRF_FICR->TEMP.T2;
NRF_TEMP->T3 = NRF_FICR->TEMP.T3;
NRF_TEMP->T4 = NRF_FICR->TEMP.T4;
}
/* Enable the FPU if the compiler used floating point unit instructions. __FPU_USED is a MACRO defined by the
* compiler. Since the FPU consumes energy, remember to disable FPU use in the compiler if floating point unit
* operations are not used in your code. */
#if (__FPU_USED == 1)
SCB->CPACR |= (3UL << 20) | (3UL << 22);
__DSB();
__ISB();
#endif
/* Configure NFCT pins as GPIOs if NFCT is not to be used in your code. If CONFIG_NFCT_PINS_AS_GPIOS is not defined,
two GPIOs (see Product Specification to see which ones) will be reserved for NFC and will not be available as
normal GPIOs. */
#if defined (CONFIG_NFCT_PINS_AS_GPIOS)
if ((NRF_UICR->NFCPINS & UICR_NFCPINS_PROTECT_Msk) == (UICR_NFCPINS_PROTECT_NFC << UICR_NFCPINS_PROTECT_Pos)){
NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
NRF_UICR->NFCPINS &= ~UICR_NFCPINS_PROTECT_Msk;
while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
NVIC_SystemReset();
}
#endif
/* Configure GPIO pads as pPin Reset pin if Pin Reset capabilities desired. If CONFIG_GPIO_AS_PINRESET is not
defined, pin reset will not be available. One GPIO (see Product Specification to see which one) will then be
reserved for PinReset and not available as normal GPIO. */
#if defined (CONFIG_GPIO_AS_PINRESET)
if (((NRF_UICR->PSELRESET[0] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Connected << UICR_PSELRESET_CONNECT_Pos)) ||
((NRF_UICR->PSELRESET[1] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Connected << UICR_PSELRESET_CONNECT_Pos))){
NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
NRF_UICR->PSELRESET[0] = 21;
while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
NRF_UICR->PSELRESET[1] = 21;
while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
NVIC_SystemReset();
}
#endif
/* Enable SWO trace functionality. If ENABLE_SWO is not defined, SWO pin will be used as GPIO (see Product
Specification to see which one). */
#if defined (ENABLE_SWO)
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
NRF_CLOCK->TRACECONFIG |= CLOCK_TRACECONFIG_TRACEMUX_Serial << CLOCK_TRACECONFIG_TRACEMUX_Pos;
NRF_P0->PIN_CNF[18] = (GPIO_PIN_CNF_DRIVE_H0H1 << GPIO_PIN_CNF_DRIVE_Pos) | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
#endif
/* Enable Trace functionality. If ENABLE_TRACE is not defined, TRACE pins will be used as GPIOs (see Product
Specification to see which ones). */
#if defined (ENABLE_TRACE)
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
NRF_CLOCK->TRACECONFIG |= CLOCK_TRACECONFIG_TRACEMUX_Parallel << CLOCK_TRACECONFIG_TRACEMUX_Pos;
NRF_P0->PIN_CNF[14] = (GPIO_PIN_CNF_DRIVE_H0H1 << GPIO_PIN_CNF_DRIVE_Pos) | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
NRF_P0->PIN_CNF[15] = (GPIO_PIN_CNF_DRIVE_H0H1 << GPIO_PIN_CNF_DRIVE_Pos) | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
NRF_P0->PIN_CNF[16] = (GPIO_PIN_CNF_DRIVE_H0H1 << GPIO_PIN_CNF_DRIVE_Pos) | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
NRF_P0->PIN_CNF[18] = (GPIO_PIN_CNF_DRIVE_H0H1 << GPIO_PIN_CNF_DRIVE_Pos) | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
NRF_P0->PIN_CNF[20] = (GPIO_PIN_CNF_DRIVE_H0H1 << GPIO_PIN_CNF_DRIVE_Pos) | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
#endif
SystemCoreClockUpdate();
}
static bool errata_16(void)
{
if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0))
{
if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30)
{
return true;
}
}
return false;
}
static bool errata_31(void)
{
if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0))
{
if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30)
{
return true;
}
if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x40)
{
return true;
}
if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x50)
{
return true;
}
}
return false;
}
static bool errata_32(void)
{
if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0))
{
if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30)
{
return true;
}
}
return false;
}
static bool errata_36(void)
{
if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0))
{
if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30)
{
return true;
}
if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x40)
{
return true;
}
if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x50)
{
return true;
}
}
return false;
}
static bool errata_37(void)
{
if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0))
{
if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30)
{
return true;
}
}
return false;
}
static bool errata_57(void)
{
if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0))
{
if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30)
{
return true;
}
}
return false;
}
static bool errata_66(void)
{
if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0))
{
if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x50)
{
return true;
}
}
return false;
}
/*lint --flb "Leave library region" */

64
platform/nrf52xxx/debug.c Normal file
View File

@@ -0,0 +1,64 @@
/*
* Copyright (c) 2016 Eric Holland
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdarg.h>
#include <reg.h>
#include <debug.h>
#include <stdio.h>
#include <kernel/thread.h>
#include <dev/uart.h>
#include <arch/ops.h>
#include <arch/arm/cm.h>
#include <platform/debug.h>
#include <target/debugconfig.h>
void nrf52_debug_early_init(void)
{
uart_init_early();
}
/* later in the init process */
void nrf52_debug_init(void)
{
uart_init();
}
void platform_dputc(char c)
{
if (c == '\n')
uart_putc(DEBUG_UART, '\r');
uart_putc(DEBUG_UART, c);
}
int platform_dgetc(char *c, bool wait)
{
int ret = uart_getc(DEBUG_UART, wait);
if (ret == -1)
return -1;
*c = ret;
return 0;
}

77
platform/nrf52xxx/gpio.c Normal file
View File

@@ -0,0 +1,77 @@
/*
* Copyright (c) 2016 Eric Holland
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <debug.h>
#include <assert.h>
#include <dev/gpio.h>
#include <platform/nrf52.h>
#include <platform/gpio.h>
int gpio_config(unsigned nr, unsigned flags)
{
DEBUG_ASSERT(nr <= NRF_MAX_PIN_NUMBER);
unsigned init;
if (flags & GPIO_OUTPUT) {
NRF_GPIO->PIN_CNF[nr] = GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos | \
GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos | \
GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos | \
GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos;
} else { // GPIO_INPUT
if (flags & GPIO_PULLUP) {
init = GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos;
} else if (flags & GPIO_PULLDOWN) {
init = GPIO_PIN_CNF_PULL_Pulldown << GPIO_PIN_CNF_PULL_Pos;
} else {
init = GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos;
}
NRF_GPIO->PIN_CNF[nr] = GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos | \
GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos | \
init;
}
return 0;
}
void gpio_set(unsigned nr, unsigned on)
{
DEBUG_ASSERT(nr <= NRF_MAX_PIN_NUMBER);
if (on > 0) {
NRF_GPIO->OUTSET = 1 << nr;
} else {
NRF_GPIO->OUTCLR = 1 << nr;
}
}
int gpio_get(unsigned nr)
{
DEBUG_ASSERT( nr <= NRF_MAX_PIN_NUMBER );
if ( NRF_GPIO->IN & ( 1 << nr) ) {
return 1;
} else {
return 0;
}
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright (c) 2016 Eric Holland
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef __PLATFORM_NRF52_GPIO_H
#define __PLATFORM_NRF52_GPIO_H
#define NRF_MAX_PIN_NUMBER 31
#endif

View File

@@ -0,0 +1,37 @@
/*
* Copyright (c) 2016 Eric Holland
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef __PLATFORM_NRF52_H
#define __PLATFORM_NRF52_H
#include <platform/nrf52xxx.h>
void nrf52_debug_early_init(void);
void nrf52_debug_init(void);
void nrf52_timer_early_init(void);
void nrf52_timer_init(void);
void nrf52_gpio_early_init(void);
void nrf52_flash_nor_early_init(void);
void nrf52_flash_nor_init(void);
#endif

View File

@@ -0,0 +1,29 @@
/*
* Copyright (c) 2016 Eric Holland
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef __PLATFORM_CM_H
#define __PLATFORM_CM_H
#include <platform/nrf52xxx.h>
#endif

58
platform/nrf52xxx/init.c Normal file
View File

@@ -0,0 +1,58 @@
/*
* Copyright (c) 2016 Eric Holland
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <err.h>
#include <debug.h>
#include <arch/arm/cm.h>
#include <dev/uart.h>
#include <platform.h>
#include <platform/nrf52.h>
#include <platform/system_nrf52.h>
void platform_early_init(void)
{
// Crank up the clock before initing timers.
SystemInit();
arm_cm_systick_init(32768);
}
void platform_init(void)
{
dprintf(SPEW, "Nordic nrf51xxx platform for lk...\n");
dprintf(SPEW, "\tFlash: %d pages of %d bytes each (%dk bytes total)\n", \
NRF_FICR->CODESIZE, NRF_FICR->CODEPAGESIZE, \
(NRF_FICR->CODESIZE * NRF_FICR->CODEPAGESIZE)>>10);
dprintf(SPEW, "\tRAM: %d blocks of %d bytes each (%dk bytes total)\n", \
NRF_FICR->NUMRAMBLOCK, NRF_FICR->SIZERAMBLOCKS, \
(NRF_FICR->NUMRAMBLOCK * NRF_FICR->SIZERAMBLOCKS)>>10);
dprintf(SPEW, "\tRadio MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", \
(NRF_FICR->DEVICEADDR[1] >> 8) & 0xFF, \
(NRF_FICR->DEVICEADDR[1]) & 0xFF, \
(NRF_FICR->DEVICEADDR[0] >> 24) & 0xFF, \
(NRF_FICR->DEVICEADDR[0] >> 16) & 0xFF, \
(NRF_FICR->DEVICEADDR[0] >> 8) & 0xFF, \
(NRF_FICR->DEVICEADDR[0] >> 0) & 0xFF);
dprintf(SPEW, "\tHWID: 0x%04x\n",NRF_FICR->CONFIGID & 0x0000ffff);
}

View File

@@ -0,0 +1,59 @@
LOCAL_DIR := $(GET_LOCAL_DIR)
MODULE := $(LOCAL_DIR)
# ROMBASE, MEMBASE, and MEMSIZE are required for the linker script
ROMBASE := 0x0
MEMBASE := 0x20000000
# can be overridden by target
ARCH := arm
ARM_CPU := cortex-m4
ifeq ($(NRF51_CHIP),nrf51822-qfaa)
GLOBAL_DEFINES +=
MEMSIZE ?= 16384
endif
ifeq ($(NRF51_CHIP),nrf51822-ceaa)
GLOBAL_DEFINES +=
MEMSIZE ?= 16384
endif
ifeq ($(NRF51_CHIP),nrf51822-qfab)
GLOBAL_DEFINES +=
MEMSIZE ?= 16384
endif
ifeq ($(NRF51_CHIP),nrf51822-cdab)
GLOBAL_DEFINES +=
MEMSIZE ?= 16384
endif
ifeq ($(NRF51_CHIP),nrf51822-qfac)
GLOBAL_DEFINES +=
MEMSIZE ?= 32768
endif
ifeq ($(NRF51_CHIP),nrf51822-cfac)
GLOBAL_DEFINES +=
MEMSIZE ?= 32768
endif
GLOBAL_INCLUDES += $(LOCAL_DIR)
GLOBAL_DEFINES += \
MEMSIZE=$(MEMSIZE)
MODULE_SRCS += \
$(LOCAL_DIR)/init.c \
$(LOCAL_DIR)/debug.c \
$(LOCAL_DIR)/uart.c \
$(LOCAL_DIR)/vectab.c \
$(LOCAL_DIR)/gpio.c \
$(LOCAL_DIR)/timer.c \
LINKER_SCRIPT += \
$(BUILDDIR)/system-twosegment.ld
MODULE_DEPS += \
platform/nrf52 \
lib/cbuf
include make/module.mk

104
platform/nrf52xxx/timer.c Normal file
View File

@@ -0,0 +1,104 @@
/*
* Copyright (c) 2015 Eric Holland
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <err.h>
#include <debug.h>
#include <trace.h>
#include <arch/arm/cm.h>
#include <platform.h>
#include <platform/nrf52.h>
#include <platform/system_nrf52.h>
#include <platform/timer.h>
#include <sys/types.h>
static volatile uint64_t ticks;
static uint32_t tick_rate = 0;
static uint32_t tick_rate_mhz = 0;
static lk_time_t tick_interval_ms;
static platform_timer_callback cb;
static void *cb_args;
typedef enum handler_return (*platform_timer_callback)(void *arg, lk_time_t now);
status_t platform_set_periodic_timer(platform_timer_callback callback, void *arg, lk_time_t interval)
{
cb = callback;
cb_args = arg;
tick_interval_ms = interval;
uint32_t ticks = tick_rate / ( 1000 / interval );
NRF_CLOCK->LFCLKSRC = CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos;
NRF_CLOCK->TASKS_LFCLKSTART = 1;
NRF_RTC1->PRESCALER = ticks;
NRF_RTC1->INTENSET = RTC_INTENSET_TICK_Enabled << RTC_INTENSET_TICK_Pos;
NRF_RTC1->EVENTS_TICK = 0;
NRF_RTC1->TASKS_START = 1;
NVIC_EnableIRQ(RTC1_IRQn);
return NO_ERROR;
}
lk_time_t current_time(void)
{
uint64_t t;
do {
t = ticks;
} while (ticks != t);
return t * tick_interval_ms;
}
lk_bigtime_t current_time_hires(void)
{
return current_time() * 1000;
}
void nrf52_RTC1_IRQ(void)
{
ticks++;
arm_cm_irq_entry();
NRF_RTC1->EVENTS_TICK = 0;
bool resched = false;
if (cb) {
lk_time_t now = current_time();
if (cb(cb_args, now) == INT_RESCHEDULE)
resched = true;
}
arm_cm_irq_exit(resched);
}
void arm_cm_systick_init(uint32_t mhz)
{
tick_rate = mhz;
tick_rate_mhz = mhz / 1000000;
}

128
platform/nrf52xxx/uart.c Normal file
View File

@@ -0,0 +1,128 @@
/*
* Copyright (c) 2015 Eric Holland
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdarg.h>
#include <reg.h>
#include <debug.h>
#include <stdio.h>
#include <assert.h>
#include <err.h>
#include <lib/cbuf.h>
#include <arch/arm/cm.h>
#include <arch/ops.h>
#include <dev/uart.h>
#include <dev/gpio.h>
#include <kernel/thread.h>
#include <platform/debug.h>
#include <platform/gpio.h>
#include <target/debugconfig.h>
#include <target/gpioconfig.h>
#define RXBUF_SIZE 16
//cbuf_t uart0_rx_buf;
void uart_init_early(void)
{
#ifdef ENABLE_UART0
#ifdef UART0_TX_PIN
gpio_config(UART0_TX_PIN,GPIO_OUTPUT);
NRF_UART0->PSELTXD = UART0_TX_PIN;
#endif
#ifdef UART0_RX_PIN
gpio_config(UART0_RX_PIN,GPIO_INPUT);
NRF_UART0->PSELRXD = UART0_RX_PIN;
#endif
NRF_UART0->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud115200 << UART_BAUDRATE_BAUDRATE_Pos;
NRF_UART0->CONFIG = UART_CONFIG_HWFC_Disabled << UART_CONFIG_HWFC_Pos | \
UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos;
NVIC_DisableIRQ(UART0_IRQn);
NRF_UART0->ENABLE = UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos;
NRF_UART0->TXD = 'E';
NRF_UART0->TASKS_STARTTX=1;
NRF_UART0->TASKS_STARTRX=1;
#endif //ENABLE_UART0
}
void uart_init(void)
{
#ifdef ENABLE_UART0
// cbuf_initialize(&uart0_rx_buf, RXBUF_SIZE);
// NRF_UART0->INTENSET = UART_INTENSET_RXDRDY_Enabled << UART_INTENSET_RXDRDY_Pos;
NRF_UART0->EVENTS_RXDRDY = 0;
// NVIC_EnableIRQ(UART0_IRQn);
char c = NRF_UART0->RXD;
(void)c;
#endif //ENABLE_UART0
}
void nrf52_UART0_IRQ(void)
{
// char c;
arm_cm_irq_entry();
/*
bool resched = false;
while ( NRF_UART0->EVENTS_RXDRDY > 0 ) {
NRF_UART0->EVENTS_RXDRDY = 0;
c = NRF_UART0->RXD;
if (!cbuf_space_avail(&uart0_rx_buf)) {
break;
}
cbuf_write_char(&uart0_rx_buf, c, false);
resched = true;
}
*/
arm_cm_irq_exit(false);
}
int uart_putc(int port, char c)
{
while (NRF_UART0->EVENTS_TXDRDY == 0);
NRF_UART0->EVENTS_TXDRDY = 0;
NRF_UART0->TXD = c;
return 1;
}
int uart_getc(int port, bool wait)
{
do {
if (NRF_UART0->EVENTS_RXDRDY > 0) {
NRF_UART0->EVENTS_RXDRDY=0;
return NRF_UART0->RXD;
}
} while (wait);
return -1;
}
void uart_flush_tx(int port) {}
void uart_flush_rx(int port) {}
void uart_init_port(int port, uint baud)
{
// TODO - later
PANIC_UNIMPLEMENTED;
}

108
platform/nrf52xxx/vectab.c Normal file
View File

@@ -0,0 +1,108 @@
/*
* Copyright (c) 2012 Travis Geiselbrecht
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <debug.h>
#include <compiler.h>
#include <arch/arm/cm.h>
#include <lib/cbuf.h>
#include <platform/nrf52.h>
#include <target/debugconfig.h>
/* un-overridden irq handler */
void nrf52_dummy_irq(void)
{
arm_cm_irq_entry();
panic("unhandled irq\n");
}
/* a list of default handlers that are simply aliases to the dummy handler */
#define DEFAULT_HANDLER(x) \
void nrf52_##x(void) __WEAK_ALIAS("nrf52_dummy_irq");
DEFAULT_HANDLER(POWER_CLOCK_IRQ);
DEFAULT_HANDLER(RADIO_IRQ);
DEFAULT_HANDLER(UART0_IRQ);
DEFAULT_HANDLER(SPI0_TWI0_IRQ);
DEFAULT_HANDLER(SPI1_TWI1_IRQ);
DEFAULT_HANDLER(RESERVED_IRQ);
DEFAULT_HANDLER(GPIOTE_IRQ);
DEFAULT_HANDLER(ADC_IRQ);
DEFAULT_HANDLER(TIMER0_IRQ);
DEFAULT_HANDLER(TIMER1_IRQ);
DEFAULT_HANDLER(TIMER2_IRQ);
DEFAULT_HANDLER(RTC0_IRQ);
DEFAULT_HANDLER(TEMP_IRQ);
DEFAULT_HANDLER(RNG_IRQ);
DEFAULT_HANDLER(ECB_IRQ);
DEFAULT_HANDLER(CCM_AAR_IRQ);
DEFAULT_HANDLER(WDT_IRQ);
DEFAULT_HANDLER(RTC1_IRQ);
DEFAULT_HANDLER(QDEC_IRQ);
DEFAULT_HANDLER(LPCOMP_IRQ);
DEFAULT_HANDLER(SWI0_IRQ);
DEFAULT_HANDLER(SWI1_IRQ);
DEFAULT_HANDLER(SWI2_IRQ);
DEFAULT_HANDLER(SWI3_IRQ);
DEFAULT_HANDLER(SWI4_IRQ);
DEFAULT_HANDLER(SWI5_IRQ);
#define VECTAB_ENTRY(x) [x##n] = nrf51_##x
/* appended to the end of the main vector table */
const void *const __SECTION(".text.boot.vectab2") vectab2[] = {
VECTAB_ENTRY(POWER_CLOCK_IRQ),
VECTAB_ENTRY(RADIO_IRQ),
VECTAB_ENTRY(UART0_IRQ),
VECTAB_ENTRY(SPI0_TWI0_IRQ),
VECTAB_ENTRY(SPI1_TWI1_IRQ),
VECTAB_ENTRY(RESERVED_IRQ),
VECTAB_ENTRY(GPIOTE_IRQ),
VECTAB_ENTRY(ADC_IRQ),
VECTAB_ENTRY(TIMER0_IRQ),
VECTAB_ENTRY(TIMER1_IRQ),
VECTAB_ENTRY(TIMER2_IRQ),
VECTAB_ENTRY(RTC0_IRQ),
VECTAB_ENTRY(TEMP_IRQ),
VECTAB_ENTRY(RNG_IRQ),
VECTAB_ENTRY(ECB_IRQ),
VECTAB_ENTRY(CCM_AAR_IRQ),
VECTAB_ENTRY(WDT_IRQ),
VECTAB_ENTRY(RTC1_IRQ),
VECTAB_ENTRY(QDEC_IRQ),
VECTAB_ENTRY(LPCOMP_IRQ),
VECTAB_ENTRY(SWI0_IRQ),
VECTAB_ENTRY(SWI1_IRQ),
VECTAB_ENTRY(SWI2_IRQ),
VECTAB_ENTRY(SWI3_IRQ),
VECTAB_ENTRY(SWI4_IRQ),
VECTAB_ENTRY(SWI5_IRQ),
};