nrf: Update to work with nrfx v2.0.0, to match TinyUSB.

Commit 6cea369b89 updated the TinyUSB
submodule to a version based on nrfx v2.0.0.  This commit updates the nrf
port to work with the latest TinyUSB and nrfx v2.0.0.
This commit is contained in:
Jim Mussared 2020-03-25 17:01:38 +11:00 committed by Damien George
parent 9ee5aff334
commit e7f8c7d9a3
13 changed files with 97 additions and 68 deletions

View File

@ -212,6 +212,7 @@ SRC_NRFX += $(addprefix lib/nrfx/drivers/src/,\
nrfx_uart.c \ nrfx_uart.c \
nrfx_adc.c \ nrfx_adc.c \
nrfx_saadc.c \ nrfx_saadc.c \
nrfx_temp.c \
nrfx_rng.c \ nrfx_rng.c \
nrfx_twi.c \ nrfx_twi.c \
nrfx_spi.c \ nrfx_spi.c \
@ -263,7 +264,6 @@ SRC_C += $(addprefix lib/tinyusb/src/,\
class/cdc/cdc_device.c \ class/cdc/cdc_device.c \
tusb.c \ tusb.c \
portable/nordic/nrf5x/dcd_nrf5x.c \ portable/nordic/nrf5x/dcd_nrf5x.c \
portable/nordic/nrf5x/hal_nrf5x.c \
) )
endif endif

View File

@ -31,7 +31,7 @@
#include "microbitimage.h" #include "microbitimage.h"
#include "softpwm.h" #include "softpwm.h"
#include "ticker.h" #include "ticker.h"
#include "nrf_temp.h" #include "modules/machine/temp.h"
extern uint32_t ticks; extern uint32_t ticks;
@ -96,11 +96,11 @@ STATIC mp_obj_t microbit_panic(mp_uint_t n_args, const mp_obj_t *args) {
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(microbit_panic_obj, 0, 1, microbit_panic); MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(microbit_panic_obj, 0, 1, microbit_panic);
STATIC mp_obj_t microbit_temperature(void) { STATIC mp_obj_t microbit_temperature(void) {
int temp = nrf_temp_read(); int temp = temp_read();
#if MICROPY_PY_BUILTINS_FLOAT #if MICROPY_PY_BUILTINS_FLOAT
return mp_obj_new_float(temp / 4); return mp_obj_new_float((mp_float_t)temp / MICROPY_FLOAT_CONST(100.0));
#else #else
return mp_obj_new_int(temp / 4); return mp_obj_new_int(temp / 100);
#endif #endif
} }
MP_DEFINE_CONST_FUN_OBJ_0(microbit_temperature_obj, microbit_temperature); MP_DEFINE_CONST_FUN_OBJ_0(microbit_temperature_obj, microbit_temperature);

View File

@ -42,19 +42,19 @@ static inline uint32_t generate_hw_random(void) {
uint32_t retval = 0; uint32_t retval = 0;
uint8_t * p_retval = (uint8_t *)&retval; uint8_t * p_retval = (uint8_t *)&retval;
nrf_rng_event_clear(NRF_RNG_EVENT_VALRDY); nrf_rng_event_clear(NRF_RNG, NRF_RNG_EVENT_VALRDY);
nrf_rng_task_trigger(NRF_RNG_TASK_START); nrf_rng_task_trigger(NRF_RNG, NRF_RNG_TASK_START);
for (uint16_t i = 0; i < 4; i++) { for (uint16_t i = 0; i < 4; i++) {
while (!nrf_rng_event_get(NRF_RNG_EVENT_VALRDY)) { while (!nrf_rng_event_check(NRF_RNG, NRF_RNG_EVENT_VALRDY)) {
; ;
} }
nrf_rng_event_clear(NRF_RNG_EVENT_VALRDY); nrf_rng_event_clear(NRF_RNG, NRF_RNG_EVENT_VALRDY);
p_retval[i] = nrf_rng_random_value_get(); p_retval[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 retval; return retval;
} }

View File

@ -107,8 +107,8 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index) {
} }
} }
// first byte is len, second byte is string type // first byte is length (including header), second byte is string type
desc_str[0] = TUD_DESC_STR_HEADER(len); desc_str[0] = (TUSB_DESC_STRING << 8 ) | (2 * len + 2);
return desc_str; return desc_str;
} }

View File

@ -71,22 +71,10 @@ STATIC const machine_adc_obj_t machine_adc_obj[] = {
#endif #endif
}; };
#if defined(NRF52_SERIES)
STATIC void saadc_event_handler(nrfx_saadc_evt_t const * p_event) {
(void)p_event;
}
#endif
void adc_init0(void) { void adc_init0(void) {
#if defined(NRF52_SERIES) #if defined(NRF52_SERIES)
const nrfx_saadc_config_t config = { const uint8_t interrupt_priority = 6;
.resolution = NRF_SAADC_RESOLUTION_8BIT, nrfx_saadc_init(interrupt_priority);
.oversample = NRF_SAADC_OVERSAMPLE_DISABLED,
.interrupt_priority = 6,
.low_power_mode = false
};
nrfx_saadc_init(&config, saadc_event_handler);
#endif #endif
} }
@ -138,7 +126,9 @@ STATIC mp_obj_t machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, s
const machine_adc_obj_t *self = &machine_adc_obj[adc_id]; const machine_adc_obj_t *self = &machine_adc_obj[adc_id];
#if defined(NRF52_SERIES) #if defined(NRF52_SERIES)
const nrf_saadc_channel_config_t config = { const nrfx_saadc_channel_t config = { \
.channel_config =
{
.resistor_p = NRF_SAADC_RESISTOR_DISABLED, .resistor_p = NRF_SAADC_RESISTOR_DISABLED,
.resistor_n = NRF_SAADC_RESISTOR_DISABLED, .resistor_n = NRF_SAADC_RESISTOR_DISABLED,
.gain = NRF_SAADC_GAIN1_4, .gain = NRF_SAADC_GAIN1_4,
@ -146,11 +136,12 @@ STATIC mp_obj_t machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, s
.acq_time = NRF_SAADC_ACQTIME_3US, .acq_time = NRF_SAADC_ACQTIME_3US,
.mode = NRF_SAADC_MODE_SINGLE_ENDED, .mode = NRF_SAADC_MODE_SINGLE_ENDED,
.burst = NRF_SAADC_BURST_DISABLED, .burst = NRF_SAADC_BURST_DISABLED,
.pin_p = 1 + self->id, // pin_p=0 is AIN0, pin_p=8 is AIN7 },
.pin_n = NRF_SAADC_INPUT_DISABLED .pin_p = (nrf_saadc_input_t)(1 + self->id), // pin_p=0 is AIN0, pin_p=8 is AIN7
.pin_n = NRF_SAADC_INPUT_DISABLED,
.channel_index = self->id,
}; };
nrfx_saadc_channels_config(&config, 1);
nrfx_saadc_channel_init(self->id, &config);
#endif #endif
return MP_OBJ_FROM_PTR(self); return MP_OBJ_FROM_PTR(self);
@ -173,7 +164,9 @@ int16_t machine_adc_value_read(machine_adc_obj_t * adc_obj) {
#else // NRF52 #else // NRF52
nrf_saadc_value_t value = 0; nrf_saadc_value_t value = 0;
nrfx_saadc_sample_convert(adc_obj->id, &value); nrfx_saadc_simple_mode_set((1 << adc_obj->id), NRF_SAADC_RESOLUTION_8BIT, NRF_SAADC_INPUT_DISABLED, NULL);
nrfx_saadc_buffer_set(&value, 1);
nrfx_saadc_mode_trigger();
#endif #endif
return value; return value;
} }
@ -261,7 +254,9 @@ mp_obj_t machine_adc_battery_level(void) {
#else // NRF52 #else // NRF52
nrf_saadc_value_t value = 0; nrf_saadc_value_t value = 0;
const nrf_saadc_channel_config_t config = { const nrfx_saadc_channel_t config = { \
.channel_config =
{
.resistor_p = NRF_SAADC_RESISTOR_DISABLED, .resistor_p = NRF_SAADC_RESISTOR_DISABLED,
.resistor_n = NRF_SAADC_RESISTOR_DISABLED, .resistor_n = NRF_SAADC_RESISTOR_DISABLED,
.gain = NRF_SAADC_GAIN1_6, .gain = NRF_SAADC_GAIN1_6,
@ -269,12 +264,16 @@ mp_obj_t machine_adc_battery_level(void) {
.acq_time = NRF_SAADC_ACQTIME_3US, .acq_time = NRF_SAADC_ACQTIME_3US,
.mode = NRF_SAADC_MODE_SINGLE_ENDED, .mode = NRF_SAADC_MODE_SINGLE_ENDED,
.burst = NRF_SAADC_BURST_DISABLED, .burst = NRF_SAADC_BURST_DISABLED,
},
.pin_p = NRF_SAADC_INPUT_VDD, .pin_p = NRF_SAADC_INPUT_VDD,
.pin_n = NRF_SAADC_INPUT_DISABLED .pin_n = NRF_SAADC_INPUT_DISABLED,
.channel_index = 0,
}; };
nrfx_saadc_channels_config(&config, 1);
nrfx_saadc_channel_init(0, &config); nrfx_saadc_simple_mode_set((1 << 0), NRF_SAADC_RESOLUTION_8BIT, NRF_SAADC_INPUT_DISABLED, NULL);
nrfx_saadc_sample_convert(0, &value); nrfx_saadc_buffer_set(&value, 1);
nrfx_saadc_mode_trigger();
#endif #endif
uint16_t batt_lvl_in_milli_volts = BATTERY_MILLIVOLT(value) + DIODE_VOLT_DROP_MILLIVOLT; uint16_t batt_lvl_in_milli_volts = BATTERY_MILLIVOLT(value) + DIODE_VOLT_DROP_MILLIVOLT;

View File

@ -49,10 +49,14 @@
#define nrfx_twi_init nrfx_twim_init #define nrfx_twi_init nrfx_twim_init
#define nrfx_twi_enable nrfx_twim_enable #define nrfx_twi_enable nrfx_twim_enable
#define nrfx_twi_rx nrfx_twim_rx #define nrfx_twi_xfer nrfx_twim_xfer
#define nrfx_twi_tx nrfx_twim_tx
#define nrfx_twi_disable nrfx_twim_disable #define nrfx_twi_disable nrfx_twim_disable
#define nrfx_twi_xfer_desc_t nrfx_twim_xfer_desc_t
#define NRFX_TWI_XFER_DESC_RX NRFX_TWIM_XFER_DESC_RX
#define NRFX_TWI_XFER_DESC_TX NRFX_TWIM_XFER_DESC_TX
#define NRFX_TWI_INSTANCE NRFX_TWIM_INSTANCE #define NRFX_TWI_INSTANCE NRFX_TWIM_INSTANCE
#define NRF_TWI_FREQ_400K NRF_TWIM_FREQ_400K #define NRF_TWI_FREQ_400K NRF_TWIM_FREQ_400K
@ -129,9 +133,11 @@ int machine_hard_i2c_transfer_single(mp_obj_base_t *self_in, uint16_t addr, size
nrfx_err_t err_code; nrfx_err_t err_code;
int transfer_ret = 0; int transfer_ret = 0;
if (flags & MP_MACHINE_I2C_FLAG_READ) { if (flags & MP_MACHINE_I2C_FLAG_READ) {
err_code = nrfx_twi_rx(&self->p_twi, addr, buf, len); nrfx_twi_xfer_desc_t desc = NRFX_TWI_XFER_DESC_RX(addr, buf, len);
err_code = nrfx_twi_xfer(&self->p_twi, &desc, 0);
} else { } else {
err_code = nrfx_twi_tx(&self->p_twi, addr, buf, len, (flags & MP_MACHINE_I2C_FLAG_STOP) == 0); nrfx_twi_xfer_desc_t desc = NRFX_TWI_XFER_DESC_TX(addr, buf, len);
err_code = nrfx_twi_xfer(&self->p_twi, &desc, (flags & MP_MACHINE_I2C_FLAG_STOP) == 0);
transfer_ret = len; transfer_ret = len;
} }

View File

@ -114,7 +114,7 @@ void pin_init0(void) {
} }
// Initialize GPIOTE if not done yet. // Initialize GPIOTE if not done yet.
if (!nrfx_gpiote_is_init()) { if (!nrfx_gpiote_is_init()) {
nrfx_gpiote_init(); nrfx_gpiote_init(NRFX_GPIOTE_DEFAULT_CONFIG_IRQ_PRIORITY);
} }
#if PIN_DEBUG #if PIN_DEBUG

View File

@ -290,7 +290,7 @@ STATIC void machine_hard_pwm_init(mp_obj_t self_in, mp_arg_val_t *args) {
config.load_mode = NRF_PWM_LOAD_INDIVIDUAL; config.load_mode = NRF_PWM_LOAD_INDIVIDUAL;
config.step_mode = NRF_PWM_STEP_AUTO; config.step_mode = NRF_PWM_STEP_AUTO;
nrfx_pwm_init(self->p_pwm, &config, NULL); nrfx_pwm_init(self->p_pwm, &config, NULL, NULL);
uint16_t pulse_width = ((self->p_config->period * self->p_config->duty) / 100); uint16_t pulse_width = ((self->p_config->period * self->p_config->duty) / 100);

View File

@ -175,8 +175,8 @@ STATIC mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, s
} }
// Start the low-frequency clock (if it hasn't been started already) // Start the low-frequency clock (if it hasn't been started already)
if (!nrf_clock_lf_is_running()) { if (!nrf_clock_lf_is_running(NRF_CLOCK)) {
nrf_clock_task_trigger(NRF_CLOCK_TASK_LFCLKSTART); nrf_clock_task_trigger(NRF_CLOCK, NRF_CLOCK_TASK_LFCLKSTART);
} }
// Make sure it's uninitialized. // Make sure it's uninitialized.

View File

@ -29,12 +29,13 @@
#include "py/nlr.h" #include "py/nlr.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "py/mperrno.h"
#include "py/mphal.h" #include "py/mphal.h"
#if MICROPY_PY_MACHINE_TEMP #if MICROPY_PY_MACHINE_TEMP
#include "temp.h" #include "temp.h"
#include "nrf_temp.h" #include "nrfx_temp.h"
#if BLUETOOTH_SD #if BLUETOOTH_SD
#include "py/nlr.h" #include "py/nlr.h"
@ -76,6 +77,18 @@ STATIC mp_obj_t machine_temp_make_new(const mp_obj_type_t *type, size_t n_args,
return MP_OBJ_FROM_PTR(self); return MP_OBJ_FROM_PTR(self);
} }
int32_t temp_read(void) {
const nrfx_temp_config_t config = NRFX_TEMP_DEFAULT_CONFIG;
nrfx_temp_init(&config, NULL); // Blocking mode.
if (nrfx_temp_measure() != NRFX_SUCCESS) {
mp_raise_OSError(MP_EIO);
}
int32_t raw_temp = nrfx_temp_result_get();
int32_t temp_c = nrfx_temp_calculate(raw_temp);
nrfx_temp_uninit();
return temp_c;
}
/// \method read() /// \method read()
/// Get temperature. /// Get temperature.
STATIC mp_obj_t machine_temp_read(mp_uint_t n_args, const mp_obj_t *args) { STATIC mp_obj_t machine_temp_read(mp_uint_t n_args, const mp_obj_t *args) {
@ -88,7 +101,7 @@ STATIC mp_obj_t machine_temp_read(mp_uint_t n_args, const mp_obj_t *args) {
} }
#endif // BLUETOOTH_SD #endif // BLUETOOTH_SD
return MP_OBJ_NEW_SMALL_INT(nrf_temp_read()); return MP_OBJ_NEW_SMALL_INT(temp_read());
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_temp_read_obj, 0, 1, machine_temp_read); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_temp_read_obj, 0, 1, machine_temp_read);

View File

@ -200,12 +200,12 @@ STATIC mp_obj_t machine_hard_uart_make_new(const mp_obj_type_t *type, size_t n_a
// flow control // flow control
#if MICROPY_HW_UART1_HWFC #if MICROPY_HW_UART1_HWFC
config.hwfc = NRF_UART_HWFC_ENABLED; config.hal_cfg.hwfc = NRF_UART_HWFC_ENABLED;
#else #else
config.hwfc = NRF_UART_HWFC_DISABLED; config.hal_cfg.hwfc = NRF_UART_HWFC_DISABLED;
#endif #endif
config.parity = NRF_UART_PARITY_EXCLUDED; config.hal_cfg.parity = NRF_UART_PARITY_EXCLUDED;
#if (BLUETOOTH_SD == 100) #if (BLUETOOTH_SD == 100)
config.interrupt_priority = 3; config.interrupt_priority = 3;

View File

@ -53,16 +53,25 @@
// for tinyusb // for tinyusb
// #define NRFX_IRQ_IS_ENABLED 1 // #define NRFX_IRQ_IS_ENABLED 1
#define NRFX_POWER_ENABLED 1 #define NRFX_POWER_ENABLED 1
#define NRFX_POWER_CONFIG_IRQ_PRIORITY 2 #define NRFX_POWER_DEFAULT_CONFIG_IRQ_PRIORITY 2
#define NRFX_SYSTICK_ENABLED 1 #define NRFX_SYSTICK_ENABLED 1
#endif #endif
#define NRFX_GPIOTE_ENABLED 1 #define NRFX_GPIOTE_ENABLED 1
#define NRFX_GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS 1 #define NRFX_GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS 1
#if NRF51 #if NRF51
#define NRFX_GPIOTE_CONFIG_IRQ_PRIORITY 3 #define NRFX_GPIOTE_DEFAULT_CONFIG_IRQ_PRIORITY 3
#else #else
#define NRFX_GPIOTE_CONFIG_IRQ_PRIORITY 6 #define NRFX_GPIOTE_DEFAULT_CONFIG_IRQ_PRIORITY 6
#endif
#if defined(NRF51) || defined(NRF52_SERIES)
#define NRFX_TEMP_ENABLED 1
#if NRF51
#define NRFX_TEMP_DEFAULT_CONFIG_IRQ_PRIORITY 3
#else
#define NRFX_TEMP_DEFAULT_CONFIG_IRQ_PRIORITY 7
#endif
#endif #endif
#if defined(NRF51) || defined(NRF52_SERIES) #if defined(NRF51) || defined(NRF52_SERIES)

View File

@ -32,6 +32,8 @@
#define NRFX_STATIC_ASSERT(expression) #define NRFX_STATIC_ASSERT(expression)
#define NRFX_ASSERT(expression) do { bool res = expression; (void)res; } while (0) #define NRFX_ASSERT(expression) do { bool res = expression; (void)res; } while (0)
void mp_hal_delay_us(mp_uint_t us);
#define NRFX_DELAY_US mp_hal_delay_us #define NRFX_DELAY_US mp_hal_delay_us
#if BLUETOOTH_SD #if BLUETOOTH_SD