nrf52dk: cpu/nrf52832 support

This commit is contained in:
Wojciech Bober 2016-01-09 14:44:18 +01:00
parent 0671640ea2
commit 20f9515ed1
20 changed files with 1918 additions and 0 deletions

171
cpu/nrf52832/dev/clock.c Normal file
View file

@ -0,0 +1,171 @@
/*
* Copyright (c) 2015 Nordic Semiconductor. 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 holder 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.
*/
/**
* \addtogroup nrf52832
* @{
*
* \addtogroup nrf52832-dev Device drivers
* @{
*
* \addtogroup nrf52832-clock Clock driver
* @{
*
* \file
* Software clock implementation for the nRF52.
* \author
* Wojciech Bober <wojciech.bober@nordicsemi.no>
*
*/
/*---------------------------------------------------------------------------*/
#include <stdint.h>
#include <stdbool.h>
#include "nrf.h"
#include "nrf_drv_config.h"
#include "nrf_drv_rtc.h"
#include "nrf_drv_clock.h"
#include "nrf_delay.h"
#include "app_error.h"
#include "contiki.h"
#include "platform-conf.h"
/*---------------------------------------------------------------------------*/
const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(PLATFORM_RTC_INSTANCE_ID); /**< RTC instance used for platform clock */
/*---------------------------------------------------------------------------*/
static volatile uint32_t ticks;
void clock_update(void);
#define TICKS (RTC1_CONFIG_FREQUENCY/CLOCK_CONF_SECOND)
/**
* \brief Function for handling the RTC0 interrupts
* \param int_type Type of interrupt to be handled
*/
static void
rtc_handler(nrf_drv_rtc_int_type_t int_type)
{
if (int_type == NRF_DRV_RTC_INT_TICK) {
clock_update();
}
}
#ifndef SOFTDEVICE_PRESENT
/** \brief Function starting the internal LFCLK XTAL oscillator.
*/
static void
lfclk_config(void)
{
ret_code_t err_code = nrf_drv_clock_init(NULL);
APP_ERROR_CHECK(err_code);
nrf_drv_clock_lfclk_request();
}
#endif
/**
* \brief Function initialization and configuration of RTC driver instance.
*/
static void
rtc_config(void)
{
uint32_t err_code;
//Initialize RTC instance
err_code = nrf_drv_rtc_init(&rtc, NULL, rtc_handler);
APP_ERROR_CHECK(err_code);
//Enable tick event & interrupt
nrf_drv_rtc_tick_enable(&rtc, true);
//Power on RTC instance
nrf_drv_rtc_enable(&rtc);
}
/*---------------------------------------------------------------------------*/
void
clock_init(void)
{
ticks = 0;
#ifndef SOFTDEVICE_PRESENT
lfclk_config();
#endif
rtc_config();
}
/*---------------------------------------------------------------------------*/
CCIF clock_time_t
clock_time(void)
{
return (clock_time_t)(ticks & 0xFFFFFFFF);
}
/*---------------------------------------------------------------------------*/
void
clock_update(void)
{
ticks++;
if (etimer_pending()) {
etimer_request_poll();
}
}
/*---------------------------------------------------------------------------*/
CCIF unsigned long
clock_seconds(void)
{
return (unsigned long)ticks/CLOCK_CONF_SECOND;
}
/*---------------------------------------------------------------------------*/
void
clock_wait(clock_time_t i)
{
clock_time_t start;
start = clock_time();
while (clock_time() - start < (clock_time_t)i) {
__WFE();
}
}
/*---------------------------------------------------------------------------*/
void
clock_delay_usec(uint16_t dt)
{
nrf_delay_us(dt);
}
/*---------------------------------------------------------------------------*/
/**
* \brief Obsolete delay function but we implement it here since some code
* still uses it
*/
void
clock_delay(unsigned int i)
{
clock_delay_usec(i);
}
/*---------------------------------------------------------------------------*/
/**
* @}
* @}
* @}
*/

67
cpu/nrf52832/dev/lpm.h Normal file
View file

@ -0,0 +1,67 @@
/*
* Copyright (c) 2015, Nordic Semiconductor
* 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 Institute 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 INSTITUTE 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 INSTITUTE 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.
*
*/
/**
* \addtogroup nrf52832-dev Device drivers
* @{
*
* \addtogroup nrf52832-lpm Low power mode functions
* @{
*
* \file
* A header file for low power mode functions.
* \author
* Wojciech Bober <wojciech.bober@nordicsemi.no>
*/
#ifndef LPM_H
#define LPM_H
#ifdef SOFTDEVICE_PRESENT
#include "nrf_soc.h"
#endif
/**
* \brief Stop and wait for an event
*
*/
static inline void
lpm_drop(void)
{
#ifdef SOFTDEVICE_PRESENT
sd_app_evt_wait();
#else
__WFI();
#endif
}
#endif /* DEV_LPM_H_ */
/**
* @}
* @}
*/

90
cpu/nrf52832/dev/random.c Normal file
View file

@ -0,0 +1,90 @@
/*
* Copyright (c) 2015, Nordic Semiconductor
* 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 Institute 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 INSTITUTE 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 INSTITUTE 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.
*
*/
/**
* \addtogroup nrf52832
* @{
*
* \addtogroup nrf52832-dev Device drivers
* @{
*
* \addtogroup nrf52832-rng Hardware random number generator
* @{
*
* \file
* Random number generator routines exploiting the nRF52 hardware
* capabilities.
*
* This file overrides core/lib/random.c.
*
* \author
* Wojciech Bober <wojciech.bober@nordicsemi.no>
*/
#include <stddef.h>
#include <nrf_drv_rng.h>
#include "app_error.h"
/*---------------------------------------------------------------------------*/
/**
* \brief Generates a new random number using the nRF52 RNG.
* \return a random number.
*/
unsigned short
random_rand(void)
{
unsigned short value = 42;
uint8_t available;
ret_code_t err_code;
do {
nrf_drv_rng_bytes_available(&available);
} while (available < sizeof(value));
err_code = nrf_drv_rng_rand((uint8_t *)&value, sizeof(value));
APP_ERROR_CHECK(err_code);
return value;
}
/*---------------------------------------------------------------------------*/
/**
* \brief Initialize the nRF52 random number generator.
* \param seed Ignored. It's here because the function prototype is in core.
*
*/
void
random_init(unsigned short seed)
{
(void)seed;
ret_code_t err_code = nrf_drv_rng_init(NULL);
APP_ERROR_CHECK(err_code);
}
/**
* @}
* @}
* @}
*/

118
cpu/nrf52832/dev/uart0.c Normal file
View file

@ -0,0 +1,118 @@
/*
* Copyright (c) 2015, Nordic Semiconductor
* 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 Institute 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 INSTITUTE 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 INSTITUTE 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.
*
*/
/**
* \addtogroup nrf52832-dev Device drivers
* @{
*
* \addtogroup nrf52832-uart UART driver
* @{
*
* \file
* Contiki compatible UART driver.
* \author
* Wojciech Bober <wojciech.bober@nordicsemi.no>
*/
#include <stdlib.h>
#include "nrf.h"
#include "nrf_drv_config.h"
#include "nrf_drv_uart.h"
#include "app_util_platform.h"
#include "app_error.h"
#include "contiki.h"
#include "dev/uart0.h"
#include "dev/watchdog.h"
#include "lib/ringbuf.h"
#define TXBUFSIZE 128
static uint8_t rx_buffer[1];
static int (*uart0_input_handler)(unsigned char c);
static struct ringbuf txbuf;
static uint8_t txbuf_data[TXBUFSIZE];
/*---------------------------------------------------------------------------*/
static void
uart_event_handler(nrf_drv_uart_event_t * p_event, void * p_context)
{
ENERGEST_ON(ENERGEST_TYPE_IRQ);
if (p_event->type == NRF_DRV_UART_EVT_RX_DONE) {
if (uart0_input_handler != NULL) {
uart0_input_handler(p_event->data.rxtx.p_data[0]);
}
(void)nrf_drv_uart_rx(rx_buffer, 1);
} else if (p_event->type == NRF_DRV_UART_EVT_TX_DONE) {
if (ringbuf_elements(&txbuf) > 0) {
uint8_t c = ringbuf_get(&txbuf);
nrf_drv_uart_tx(&c, 1);
}
}
ENERGEST_OFF(ENERGEST_TYPE_IRQ);
}
/*---------------------------------------------------------------------------*/
void
uart0_set_input(int (*input)(unsigned char c))
{
uart0_input_handler = input;
}
/*---------------------------------------------------------------------------*/
void
uart0_writeb(unsigned char c)
{
if (nrf_drv_uart_tx(&c, 1) == NRF_ERROR_BUSY) {
while (ringbuf_put(&txbuf, c) == 0) {
__WFE();
}
}
}
/*---------------------------------------------------------------------------*/
/**
* Initialize the RS232 port.
*
*/
void
uart0_init(unsigned long ubr)
{
nrf_drv_uart_config_t config = NRF_DRV_UART_DEFAULT_CONFIG;
ret_code_t retcode = nrf_drv_uart_init(&config, uart_event_handler);
APP_ERROR_CHECK(retcode);
ringbuf_init(&txbuf, txbuf_data, sizeof(txbuf_data));
nrf_drv_uart_rx_enable();
nrf_drv_uart_rx(rx_buffer, 1);
}
/**
* @}
* @}
*/

57
cpu/nrf52832/dev/uart0.h Normal file
View file

@ -0,0 +1,57 @@
/*
* Copyright (c) 2015, Nordic Semiconductor
* 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 Institute 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 INSTITUTE 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 INSTITUTE 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.
*
*/
/**
* \addtogroup nrf52832-dev Device drivers
* @{
*
* \addtogroup nrf52832-uart UART driver
* @{
*
* \file
* A header file for Contiki compatible UART driver.
* \author
* Wojciech Bober <wojciech.bober@nordicsemi.no>
*/
#ifndef UART_0_H
#define UART_0_H
#include <stdint.h>
#include "contiki-conf.h"
void uart0_init();
void uart0_writeb(uint8_t byte);
void uart0_set_input(int (* input)(unsigned char c));
#endif /* UART_0_H */
/**
* @}
* @}
*/

View file

@ -0,0 +1,93 @@
/*
* Copyright (c) 2015, Nordic Semiconductor
* 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 Institute 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 INSTITUTE 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 INSTITUTE 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.
*
*/
/**
* \addtogroup nrf52832-dev Device drivers
* @{
*
* \addtogroup nrf52832-watchdog Watchdog driver
* @{
*
* \file
* Contiki compatible watchdog driver implementation.
* \author
* Wojciech Bober <wojciech.bober@nordicsemi.no>
*/
#include <nrf_drv_wdt.h>
#include "app_error.h"
#include "contiki-conf.h"
static nrf_drv_wdt_channel_id wdt_channel_id;
static uint8_t wdt_initialized = 0;
/**
* \brief WDT events handler.
*/
static void wdt_event_handler(void)
{
LEDS_OFF(LEDS_MASK);
}
/*---------------------------------------------------------------------------*/
void
watchdog_init(void)
{
ret_code_t err_code;
err_code = nrf_drv_wdt_init(NULL, &wdt_event_handler);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_wdt_channel_alloc(&wdt_channel_id);
APP_ERROR_CHECK(err_code);
wdt_initialized = 1;
}
/*---------------------------------------------------------------------------*/
void
watchdog_start(void)
{
if(wdt_initialized) {
nrf_drv_wdt_enable();
}
}
/*---------------------------------------------------------------------------*/
void
watchdog_periodic(void)
{
if(wdt_initialized) {
nrf_drv_wdt_channel_feed(wdt_channel_id);
}
}
/*---------------------------------------------------------------------------*/
void
watchdog_reboot(void)
{
NVIC_SystemReset();
}
/**
* @}
* @}
*/