Skip to content

upgrade nrfx API to v2 #2395

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

Merged
merged 3 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/tinyusb
Submodule tinyusb updated 269 files
2 changes: 1 addition & 1 deletion ports/nrf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ SRC_NRFX = $(addprefix nrfx/,\
drivers/src/nrfx_uarte.c \
drivers/src/nrfx_gpiote.c \
drivers/src/nrfx_rtc.c \
drivers/src/nrfx_nvmc.c \
)

ifdef EXTERNAL_FLASH_DEVICES
Expand Down Expand Up @@ -167,7 +168,6 @@ SRC_C += \
lib/utils/pyexec.c \
lib/utils/stdout_helpers.c \
lib/utils/sys_stdio_mphal.c \
nrfx/hal/nrf_nvmc.c \
nrfx/mdk/system_$(MCU_SUB_VARIANT).c \
peripherals/nrf/cache.c \
peripherals/nrf/clocks.c \
Expand Down
51 changes: 25 additions & 26 deletions ports/nrf/common-hal/analogio/AnalogIn.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@

void analogin_init(void) {
// Calibrate the ADC once, on startup.
nrf_saadc_enable();
nrf_saadc_event_clear(NRF_SAADC_EVENT_CALIBRATEDONE);
nrf_saadc_task_trigger(NRF_SAADC_TASK_CALIBRATEOFFSET);
while (nrf_saadc_event_check(NRF_SAADC_EVENT_CALIBRATEDONE) == 0) { }
nrf_saadc_event_clear(NRF_SAADC_EVENT_CALIBRATEDONE);
nrf_saadc_disable();
nrf_saadc_enable(NRF_SAADC);
nrf_saadc_event_clear(NRF_SAADC, NRF_SAADC_EVENT_CALIBRATEDONE);
nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_CALIBRATEOFFSET);
while (nrf_saadc_event_check(NRF_SAADC, NRF_SAADC_EVENT_CALIBRATEDONE) == 0) { }
nrf_saadc_event_clear(NRF_SAADC, NRF_SAADC_EVENT_CALIBRATEDONE);
nrf_saadc_disable(NRF_SAADC);
}

void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self, const mcu_pin_obj_t *pin) {
Expand Down Expand Up @@ -81,34 +81,33 @@ uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {
.reference = NRF_SAADC_REFERENCE_VDD4,
.acq_time = NRF_SAADC_ACQTIME_3US,
.mode = NRF_SAADC_MODE_SINGLE_ENDED,
.burst = NRF_SAADC_BURST_DISABLED,
.pin_p = self->pin->adc_channel,
.pin_n = self->pin->adc_channel,
.burst = NRF_SAADC_BURST_DISABLED
};

nrf_saadc_resolution_set(NRF_SAADC_RESOLUTION_14BIT);
nrf_saadc_oversample_set(NRF_SAADC_OVERSAMPLE_DISABLED);
nrf_saadc_enable();
nrf_saadc_resolution_set(NRF_SAADC, NRF_SAADC_RESOLUTION_14BIT);
nrf_saadc_oversample_set(NRF_SAADC, NRF_SAADC_OVERSAMPLE_DISABLED);
nrf_saadc_enable(NRF_SAADC);

for (uint32_t i = 0; i < NRF_SAADC_CHANNEL_COUNT; i++)
nrf_saadc_channel_input_set(i, NRF_SAADC_INPUT_DISABLED, NRF_SAADC_INPUT_DISABLED);
for (uint32_t i = 0; i < SAADC_CH_NUM; i++)
nrf_saadc_channel_input_set(NRF_SAADC, i, NRF_SAADC_INPUT_DISABLED, NRF_SAADC_INPUT_DISABLED);

nrf_saadc_channel_init(CHANNEL_NO, &config);
nrf_saadc_buffer_init(&value, 1);
nrf_saadc_channel_init(NRF_SAADC, CHANNEL_NO, &config);
nrf_saadc_channel_input_set(NRF_SAADC, CHANNEL_NO, self->pin->adc_channel, self->pin->adc_channel);
nrf_saadc_buffer_init(NRF_SAADC, &value, 1);

nrf_saadc_task_trigger(NRF_SAADC_TASK_START);
while (nrf_saadc_event_check(NRF_SAADC_EVENT_STARTED) == 0);
nrf_saadc_event_clear(NRF_SAADC_EVENT_STARTED);
nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_START);
while (nrf_saadc_event_check(NRF_SAADC, NRF_SAADC_EVENT_STARTED) == 0);
nrf_saadc_event_clear(NRF_SAADC, NRF_SAADC_EVENT_STARTED);

nrf_saadc_task_trigger(NRF_SAADC_TASK_SAMPLE);
while (nrf_saadc_event_check(NRF_SAADC_EVENT_END) == 0);
nrf_saadc_event_clear(NRF_SAADC_EVENT_END);
nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_SAMPLE);
while (nrf_saadc_event_check(NRF_SAADC, NRF_SAADC_EVENT_END) == 0);
nrf_saadc_event_clear(NRF_SAADC, NRF_SAADC_EVENT_END);

nrf_saadc_task_trigger(NRF_SAADC_TASK_STOP);
while (nrf_saadc_event_check(NRF_SAADC_EVENT_STOPPED) == 0);
nrf_saadc_event_clear(NRF_SAADC_EVENT_STOPPED);
nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_STOP);
while (nrf_saadc_event_check(NRF_SAADC, NRF_SAADC_EVENT_STOPPED) == 0);
nrf_saadc_event_clear(NRF_SAADC, NRF_SAADC_EVENT_STOPPED);

nrf_saadc_disable();
nrf_saadc_disable(NRF_SAADC);

if (value < 0)
value = 0;
Expand Down
12 changes: 7 additions & 5 deletions ports/nrf/common-hal/busio/I2C.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,8 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, const mcu_pin_obj_t *
mp_raise_RuntimeError(translate("SDA or SCL needs a pull up"));
}

nrfx_twim_config_t config = NRFX_TWIM_DEFAULT_CONFIG;
config.scl = scl->number;
config.sda = sda->number;
nrfx_twim_config_t config = NRFX_TWIM_DEFAULT_CONFIG(scl->number, sda->number);

// change freq. only if it's less than the default 400K
if (frequency < 100000) {
config.frequency = NRF_TWIM_FREQ_100K;
Expand Down Expand Up @@ -248,8 +247,10 @@ uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr, const u
// break into MAX_XFER_LEN transaction
while ( len ) {
const size_t xact_len = MIN(len, I2C_MAX_XFER_LEN);
nrfx_twim_xfer_desc_t xfer_desc = NRFX_TWIM_XFER_DESC_TX(addr, (uint8_t*) data, xact_len);
uint32_t const flags = (stopBit ? 0 : NRFX_TWIM_FLAG_TX_NO_STOP);

if ( NRFX_SUCCESS != (err = nrfx_twim_tx(&self->twim_peripheral->twim, addr, data, xact_len, !stopBit)) ) {
if ( NRFX_SUCCESS != (err = nrfx_twim_xfer(&self->twim_peripheral->twim, &xfer_desc, flags)) ) {
break;
}

Expand All @@ -274,8 +275,9 @@ uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr, uint8_t
// break into MAX_XFER_LEN transaction
while ( len ) {
const size_t xact_len = MIN(len, I2C_MAX_XFER_LEN);
nrfx_twim_xfer_desc_t xfer_desc = NRFX_TWIM_XFER_DESC_RX(addr, data, xact_len);

if ( NRFX_SUCCESS != (err = nrfx_twim_rx(&self->twim_peripheral->twim, addr, data, xact_len)) ) {
if ( NRFX_SUCCESS != (err = nrfx_twim_xfer(&self->twim_peripheral->twim, &xfer_desc, 0)) ) {
break;
}

Expand Down
3 changes: 2 additions & 1 deletion ports/nrf/common-hal/busio/SPI.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self, const mcu_pin_obj_t *
mp_raise_ValueError(translate("All SPI peripherals are in use"));
}

nrfx_spim_config_t config = NRFX_SPIM_DEFAULT_CONFIG;
nrfx_spim_config_t config = NRFX_SPIM_DEFAULT_CONFIG(NRFX_SPIM_PIN_NOT_USED, NRFX_SPIM_PIN_NOT_USED,
NRFX_SPIM_PIN_NOT_USED, NRFX_SPIM_PIN_NOT_USED);
config.frequency = NRF_SPIM_FREQ_8M;

config.sck_pin = clock->number;
Expand Down
8 changes: 5 additions & 3 deletions ports/nrf/common-hal/busio/UART.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,12 @@ void common_hal_busio_uart_construct (busio_uart_obj_t *self,
.pselcts = NRF_UARTE_PSEL_DISCONNECTED,
.pselrts = NRF_UARTE_PSEL_DISCONNECTED,
.p_context = self,
.hwfc = NRF_UARTE_HWFC_DISABLED,
.parity = (parity == PARITY_NONE) ? NRF_UARTE_PARITY_EXCLUDED : NRF_UARTE_PARITY_INCLUDED,
.baudrate = get_nrf_baud(baudrate),
.interrupt_priority = 7
.interrupt_priority = 7,
.hal_cfg = {
.hwfc = NRF_UARTE_HWFC_DISABLED,
.parity = (parity == PARITY_NONE) ? NRF_UARTE_PARITY_EXCLUDED : NRF_UARTE_PARITY_INCLUDED
}
};

nrfx_uarte_uninit(self->uarte);
Expand Down
39 changes: 19 additions & 20 deletions ports/nrf/common-hal/microcontroller/Processor.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,35 +75,34 @@ float common_hal_mcu_processor_get_voltage(void) {
.reference = NRF_SAADC_REFERENCE_INTERNAL,
.acq_time = NRF_SAADC_ACQTIME_10US,
.mode = NRF_SAADC_MODE_SINGLE_ENDED,
.burst = NRF_SAADC_BURST_DISABLED,
.pin_p = NRF_SAADC_INPUT_VDD,
.pin_n = NRF_SAADC_INPUT_VDD,
.burst = NRF_SAADC_BURST_DISABLED
};

nrf_saadc_resolution_set(NRF_SAADC_RESOLUTION_14BIT);
nrf_saadc_oversample_set(NRF_SAADC_OVERSAMPLE_DISABLED);
nrf_saadc_enable();
nrf_saadc_resolution_set(NRF_SAADC, NRF_SAADC_RESOLUTION_14BIT);
nrf_saadc_oversample_set(NRF_SAADC, NRF_SAADC_OVERSAMPLE_DISABLED);
nrf_saadc_enable(NRF_SAADC);

for (uint32_t i = 0; i < NRF_SAADC_CHANNEL_COUNT; i++) {
nrf_saadc_channel_input_set(i, NRF_SAADC_INPUT_DISABLED, NRF_SAADC_INPUT_DISABLED);
for (uint32_t i = 0; i < SAADC_CH_NUM; i++) {
nrf_saadc_channel_input_set(NRF_SAADC, i, NRF_SAADC_INPUT_DISABLED, NRF_SAADC_INPUT_DISABLED);
}

nrf_saadc_channel_init(0, &config);
nrf_saadc_buffer_init(&value, 1);
nrf_saadc_channel_init(NRF_SAADC, 0, &config);
nrf_saadc_channel_input_set(NRF_SAADC, 0, NRF_SAADC_INPUT_VDD, NRF_SAADC_INPUT_VDD);
nrf_saadc_buffer_init(NRF_SAADC, &value, 1);

nrf_saadc_task_trigger(NRF_SAADC_TASK_START);
while (nrf_saadc_event_check(NRF_SAADC_EVENT_STARTED) == 0) { }
nrf_saadc_event_clear(NRF_SAADC_EVENT_STARTED);
nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_START);
while (nrf_saadc_event_check(NRF_SAADC, NRF_SAADC_EVENT_STARTED) == 0) { }
nrf_saadc_event_clear(NRF_SAADC, NRF_SAADC_EVENT_STARTED);

nrf_saadc_task_trigger(NRF_SAADC_TASK_SAMPLE);
while (nrf_saadc_event_check(NRF_SAADC_EVENT_END) == 0) { }
nrf_saadc_event_clear(NRF_SAADC_EVENT_END);
nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_SAMPLE);
while (nrf_saadc_event_check(NRF_SAADC, NRF_SAADC_EVENT_END) == 0) { }
nrf_saadc_event_clear(NRF_SAADC, NRF_SAADC_EVENT_END);

nrf_saadc_task_trigger(NRF_SAADC_TASK_STOP);
while (nrf_saadc_event_check(NRF_SAADC_EVENT_STOPPED) == 0) { }
nrf_saadc_event_clear(NRF_SAADC_EVENT_STOPPED);
nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_STOP);
while (nrf_saadc_event_check(NRF_SAADC, NRF_SAADC_EVENT_STOPPED) == 0) { }
nrf_saadc_event_clear(NRF_SAADC, NRF_SAADC_EVENT_STOPPED);

nrf_saadc_disable();
nrf_saadc_disable(NRF_SAADC);

if (value < 0) {
value = 0;
Expand Down
12 changes: 6 additions & 6 deletions ports/nrf/common-hal/os/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ bool common_hal_os_urandom(uint8_t *buffer, uint32_t length) {
return NRF_SUCCESS == sd_rand_application_vector_get(buffer, length);
#endif

nrf_rng_event_clear(NRF_RNG_EVENT_VALRDY);
nrf_rng_task_trigger(NRF_RNG_TASK_START);
nrf_rng_event_clear(NRF_RNG, NRF_RNG_EVENT_VALRDY);
nrf_rng_task_trigger(NRF_RNG, NRF_RNG_TASK_START);

for (uint32_t i = 0; i < length; i++) {
while (nrf_rng_event_get(NRF_RNG_EVENT_VALRDY) == 0);
nrf_rng_event_clear(NRF_RNG_EVENT_VALRDY);
while (nrf_rng_event_check(NRF_RNG, NRF_RNG_EVENT_VALRDY) == 0);
nrf_rng_event_clear(NRF_RNG, NRF_RNG_EVENT_VALRDY);

buffer[i] = nrf_rng_random_value_get();
buffer[i] = nrf_rng_random_value_get(NRF_RNG);
}

nrf_rng_task_trigger(NRF_RNG_TASK_STOP);
nrf_rng_task_trigger(NRF_RNG, NRF_RNG_TASK_STOP);

return true;
}
2 changes: 1 addition & 1 deletion ports/nrf/common-hal/pulseio/PulseIn.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void pulsein_reset(void) {
if ( nrfx_gpiote_is_init() ) {
nrfx_gpiote_uninit();
}
nrfx_gpiote_init();
nrfx_gpiote_init(NRFX_GPIOTE_CONFIG_IRQ_PRIORITY);

memset(_objs, 0, sizeof(_objs));
}
Expand Down
4 changes: 2 additions & 2 deletions ports/nrf/common-hal/rtc/RTC.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ void rtc_handler(nrfx_rtc_int_type_t int_type) {
}

void rtc_init(void) {
if (!nrf_clock_lf_is_running()) {
nrf_clock_task_trigger(NRF_CLOCK_TASK_LFCLKSTART);
if (!nrf_clock_lf_is_running(NRF_CLOCK)) {
nrf_clock_task_trigger(NRF_CLOCK, NRF_CLOCK_TASK_LFCLKSTART);
}
nrfx_rtc_counter_clear(&rtc_instance);
nrfx_rtc_init(&rtc_instance, &rtc_config, rtc_handler);
Expand Down
2 changes: 1 addition & 1 deletion ports/nrf/nrfx
Submodule nrfx updated 317 files
5 changes: 4 additions & 1 deletion ports/nrf/nrfx_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// Power
#define NRFX_POWER_ENABLED 1
#define NRFX_POWER_CONFIG_IRQ_PRIORITY 7
#define NRFX_POWER_DEFAULT_CONFIG_IRQ_PRIORITY 7

// NOTE: THIS WORKAROUND CAUSES BLE CODE TO CRASH.
// It doesn't work with the SoftDevice.
Expand Down Expand Up @@ -116,4 +116,7 @@
#define NRFX_GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS 1
#define NRFX_GPIOTE_CONFIG_IRQ_PRIORITY 7

// NVM controller
#define NRFX_NVMC_ENABLED 1

#endif // NRFX_CONFIG_H__
4 changes: 2 additions & 2 deletions ports/nrf/peripherals/nrf/nrf52840/power.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
*/

#include "nrfx.h"
#include "nrf_nvmc.h"
#include "nrfx_nvmc.h"

void nrf_peripherals_power_init(void) {
// Set GPIO reference voltage to 3.3V if it isn't already. REGOUT0 will get reset to 0xfffffff
// if flash is erased, which sets the default to 1.8V
// This matters only when "high voltage mode" is enabled, which is true on the PCA10059,
// and might be true on other boards.
if (NRF_UICR->REGOUT0 == 0xffffffff) {
nrf_nvmc_write_word((uint32_t) &NRF_UICR->REGOUT0, UICR_REGOUT0_VOUT_3V3 << UICR_REGOUT0_VOUT_Pos);
nrfx_nvmc_word_write((uint32_t) &NRF_UICR->REGOUT0, UICR_REGOUT0_VOUT_3V3 << UICR_REGOUT0_VOUT_Pos);
// Must reset to make enable change.
NVIC_SystemReset();
}
Expand Down
6 changes: 3 additions & 3 deletions ports/nrf/peripherals/nrf/nvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include <stdio.h>
#include <string.h>

#include "nrf_nvmc.h"
#include "nrfx_nvmc.h"

#define FLASH_PAGE_SIZE (4096)

Expand Down Expand Up @@ -90,7 +90,7 @@ bool nrf_nvm_safe_flash_page_write(uint32_t page_addr, uint8_t *data) {
}
#endif

nrf_nvmc_page_erase(page_addr);
nrf_nvmc_write_bytes(page_addr, data, FLASH_PAGE_SIZE);
nrfx_nvmc_page_erase(page_addr);
nrfx_nvmc_bytes_write(page_addr, data, FLASH_PAGE_SIZE);
return true;
}