Skip to content

changes arising from Nordic work #454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
Closed
2 changes: 1 addition & 1 deletion libraries/mbed/api/TimerEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class TimerEvent {
virtual void handler() = 0;

// insert in to linked list
void insert(unsigned int timestamp);
void insert(timestamp_t timestamp);

// remove from linked list, if in it
void remove();
Expand Down
2 changes: 1 addition & 1 deletion libraries/mbed/common/TimerEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ TimerEvent::~TimerEvent() {
}

// insert in to linked list
void TimerEvent::insert(unsigned int timestamp) {
void TimerEvent::insert(timestamp_t timestamp) {
us_ticker_insert_event(&event, timestamp, (uint32_t)this);
}

Expand Down
8 changes: 6 additions & 2 deletions libraries/mbed/common/us_ticker_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ void us_ticker_irq_handler(void) {
if (event_handler != NULL) {
event_handler(p->id); // NOTE: the handler can set new events
}
/* Note: We continue back to examining the head because calling the
* event handler may have altered the chain of pending events. */
} else {
// This event and the following ones in the list are in the future:
// set it as next interrupt and return
Expand All @@ -54,7 +56,7 @@ void us_ticker_irq_handler(void) {
}
}

void us_ticker_insert_event(ticker_event_t *obj, unsigned int timestamp, uint32_t id) {
void us_ticker_insert_event(ticker_event_t *obj, timestamp_t timestamp, uint32_t id) {
/* disable interrupts for the duration of the function */
__disable_irq();

Expand Down Expand Up @@ -95,7 +97,9 @@ void us_ticker_remove_event(ticker_event_t *obj) {
if (head == obj) {
// first in the list, so just drop me
head = obj->next;
if (obj->next != NULL) {
if (obj->next == NULL) {
us_ticker_disable_interrupt();
} else {
us_ticker_set_interrupt(head->timestamp);
}
} else {
Expand Down
10 changes: 6 additions & 4 deletions libraries/mbed/hal/us_ticker_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,26 @@
extern "C" {
#endif

typedef uint64_t timestamp_t;

uint32_t us_ticker_read(void);

typedef void (*ticker_event_handler)(uint32_t id);
void us_ticker_set_handler(ticker_event_handler handler);

typedef struct ticker_event_s {
uint32_t timestamp;
uint32_t id;
timestamp_t timestamp;
uint32_t id;
struct ticker_event_s *next;
} ticker_event_t;

void us_ticker_init(void);
void us_ticker_set_interrupt(unsigned int timestamp);
void us_ticker_set_interrupt(timestamp_t timestamp);
void us_ticker_disable_interrupt(void);
void us_ticker_clear_interrupt(void);
void us_ticker_irq_handler(void);

void us_ticker_insert_event(ticker_event_t *obj, unsigned int timestamp, uint32_t id);
void us_ticker_insert_event(ticker_event_t *obj, timestamp_t timestamp, uint32_t id);
void us_ticker_remove_event(ticker_event_t *obj);

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ static bool is_disabled_in_debug_needed(void);


#if defined ( __CC_ARM )
uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK;
uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK;
#elif defined ( __ICCARM__ )
__root uint32_t SystemCoreClock = __SYSTEM_CLOCK;
#elif defined ( __GNUC__ )
Expand All @@ -43,35 +43,36 @@ void SystemCoreClockUpdate(void)
}

void SystemInit(void)
{
{
// Prepare the peripherals for use as indicated by the PAN 26 "System: Manual setup is required
// to enable the use of peripherals" found at Product Anomaly document for your device found at
// https://www.nordicsemi.com/. The side effect of executing these instructions in the devices
// https://www.nordicsemi.com/. The side effect of executing these instructions in the devices
// that do not need it is that the new peripherals in the second generation devices (LPCOMP for
// example) will not be available.
if (is_manual_peripheral_setup_needed()){
*(uint32_t volatile *)0x40000504 = 0xC007FFDF;
*(uint32_t volatile *)0x40006C18 = 0x00008000;
}

// Disable PROTENSET registers under debug, as indicated by PAN 59 "MPU: Reset value of DISABLEINDEBUG
// register is incorrect" found at Product Anomaly document four your device found at
// https://www.nordicsemi.com/. There is no side effect of using these instruction if not needed.
// register is incorrect" found at Product Anomaly document four your device found at
// https://www.nordicsemi.com/. There is no side effect of using these instruction if not needed.
if (is_disabled_in_debug_needed()){
NRF_MPU->DISABLEINDEBUG = MPU_DISABLEINDEBUG_DISABLEINDEBUG_Disabled << MPU_DISABLEINDEBUG_DISABLEINDEBUG_Pos;
}

// Start 16 MHz crystal oscillator.
NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
NRF_CLOCK->TASKS_HFCLKSTART = 1;

// Start the external 32khz crystal oscillator.
NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos);
NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
NRF_CLOCK->TASKS_LFCLKSTART = 1;

// Wait for the external oscillator to start up.
while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) {
while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0) {
// Do nothing.
}
}

static bool is_manual_peripheral_setup_needed(void)
static bool is_manual_peripheral_setup_needed(void)
{
if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x1) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0))
{
Expand All @@ -88,11 +89,11 @@ static bool is_manual_peripheral_setup_needed(void)
return true;
}
}

return false;
}

static bool is_disabled_in_debug_needed(void)
static bool is_disabled_in_debug_needed(void)
{
if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x1) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0))
{
Expand All @@ -101,7 +102,7 @@ static bool is_disabled_in_debug_needed(void)
return true;
}
}

return false;
}

Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ static void ticker_isr(void) {
}
}

void us_ticker_set_interrupt(unsigned int timestamp) {
int delta = (int)(timestamp - us_ticker_read());
void us_ticker_set_interrupt(timestamp_t timestamp) {
int delta = (int)((uint32_t)timestamp - us_ticker_read());
if (delta <= 0) {
// This event was in the past:
us_ticker_irq_handler();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ static void lptmr_isr(void) {
}
}

void us_ticker_set_interrupt(unsigned int timestamp) {
int delta = (int)(timestamp - us_ticker_read());
void us_ticker_set_interrupt(timestamp_t timestamp) {
int delta = (int)((uint32_t)timestamp - us_ticker_read());
if (delta <= 0) {
// This event was in the past:
us_ticker_irq_handler();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ static void lptmr_isr(void) {
}
}

void us_ticker_set_interrupt(unsigned int timestamp) {
int delta = (int)(timestamp - us_ticker_read());
void us_ticker_set_interrupt(timestamp_t timestamp) {
int delta = (int)((uint32_t)timestamp - us_ticker_read());
if (delta <= 0) {
// This event was in the past:
us_ticker_irq_handler();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,45 +29,47 @@ static const PinMap PinMap_ADC[] = {
{p4, ADC0_0, 32},
{p5, ADC0_0, 64},
{p6, ADC0_0, 128},
{NC , NC , 0}
{NC, NC, 0}
};

void analogin_init(analogin_t *obj, PinName pin) {
int analogInputPin=0;
const PinMap *map = PinMap_ADC;

void analogin_init(analogin_t *obj, PinName pin)
{
int analogInputPin = 0;
const PinMap *map = PinMap_ADC;

obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); //(NRF_ADC_Type *)
MBED_ASSERT(obj->adc != (ADCName)NC);

while (map->pin != NC) {
if (map->pin == pin){
if (map->pin == pin) {
analogInputPin = map->function;
break;
}
map++;
}
obj->adc_pin = (uint8_t)analogInputPin;

NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled;
NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) |
(ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling<< ADC_CONFIG_INPSEL_Pos) |
(ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) |
(ADC_CONFIG_REFSEL_SupplyOneThirdPrescaling << ADC_CONFIG_REFSEL_Pos) |
(analogInputPin << ADC_CONFIG_PSEL_Pos) |
(ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos);
}

uint16_t analogin_read_u16(analogin_t *obj) {
NRF_ADC->CONFIG &= ~ADC_CONFIG_PSEL_Msk;
NRF_ADC->CONFIG |= obj->adc_pin << ADC_CONFIG_PSEL_Pos;
uint16_t analogin_read_u16(analogin_t *obj)
{
NRF_ADC->CONFIG &= ~ADC_CONFIG_PSEL_Msk;
NRF_ADC->CONFIG |= obj->adc_pin << ADC_CONFIG_PSEL_Pos;
NRF_ADC->TASKS_START = 1;
while ( ( (NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy)
{
while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) {
}

return (uint16_t)NRF_ADC->RESULT; // 10 bit
}

float analogin_read(analogin_t *obj) {
float analogin_read(analogin_t *obj)
{
uint16_t value = analogin_read_u16(obj);
return (float)value * (1.0f / (float)ADC_RANGE);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
#include "gpio_api.h"
#include "pinmap.h"

void gpio_init(gpio_t *obj, PinName pin) {
void gpio_init(gpio_t *obj, PinName pin)
{
obj->pin = pin;
if (pin == (PinName)NC)
if (pin == (PinName)NC) {
return;
}

obj->mask = (1ul << pin);

Expand All @@ -30,26 +32,27 @@ void gpio_init(gpio_t *obj, PinName pin) {
obj->reg_dir = &NRF_GPIO->DIR;
}

void gpio_mode(gpio_t *obj, PinMode mode) {
void gpio_mode(gpio_t *obj, PinMode mode)
{
pin_mode(obj->pin, mode);
}

void gpio_dir(gpio_t *obj, PinDirection direction) {
void gpio_dir(gpio_t *obj, PinDirection direction)
{
MBED_ASSERT(obj->pin != (PinName)NC);
switch (direction) {
case PIN_INPUT :
NRF_GPIO->PIN_CNF[obj->pin] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
| (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
| (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
| (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
case PIN_INPUT:
NRF_GPIO->PIN_CNF[obj->pin] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) |
(GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) |
(GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
(GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
break;
case PIN_OUTPUT:
NRF_GPIO->PIN_CNF[obj->pin] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
| (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
| (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
| (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
| (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
NRF_GPIO->PIN_CNF[obj->pin] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) |
(GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) |
(GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
(GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
(GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
break;
}
}

Loading