diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index e93c7041af..b6602de0b4 100644 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -212,6 +212,7 @@ SRC_NRFX += $(addprefix lib/nrfx/drivers/src/,\ nrfx_uart.c \ nrfx_adc.c \ nrfx_saadc.c \ + nrfx_temp.c \ nrfx_rng.c \ nrfx_twi.c \ nrfx_spi.c \ @@ -263,7 +264,6 @@ SRC_C += $(addprefix lib/tinyusb/src/,\ class/cdc/cdc_device.c \ tusb.c \ portable/nordic/nrf5x/dcd_nrf5x.c \ - portable/nordic/nrf5x/hal_nrf5x.c \ ) endif diff --git a/ports/nrf/boards/microbit/modules/modmicrobit.c b/ports/nrf/boards/microbit/modules/modmicrobit.c index 061e095f6a..b4cd3b4f18 100644 --- a/ports/nrf/boards/microbit/modules/modmicrobit.c +++ b/ports/nrf/boards/microbit/modules/modmicrobit.c @@ -31,7 +31,7 @@ #include "microbitimage.h" #include "softpwm.h" #include "ticker.h" -#include "nrf_temp.h" +#include "modules/machine/temp.h" 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); STATIC mp_obj_t microbit_temperature(void) { - int temp = nrf_temp_read(); + int temp = temp_read(); #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 - return mp_obj_new_int(temp / 4); + return mp_obj_new_int(temp / 100); #endif } MP_DEFINE_CONST_FUN_OBJ_0(microbit_temperature_obj, microbit_temperature); diff --git a/ports/nrf/drivers/rng.c b/ports/nrf/drivers/rng.c index 8234709f81..2737c74c4b 100644 --- a/ports/nrf/drivers/rng.c +++ b/ports/nrf/drivers/rng.c @@ -42,19 +42,19 @@ static inline uint32_t generate_hw_random(void) { uint32_t retval = 0; uint8_t * p_retval = (uint8_t *)&retval; - 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 (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); - p_retval[i] = nrf_rng_random_value_get(); + nrf_rng_event_clear(NRF_RNG, NRF_RNG_EVENT_VALRDY); + 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; } diff --git a/ports/nrf/drivers/usb/usb_descriptors.c b/ports/nrf/drivers/usb/usb_descriptors.c index a061302d6b..3704e5d0dd 100644 --- a/ports/nrf/drivers/usb/usb_descriptors.c +++ b/ports/nrf/drivers/usb/usb_descriptors.c @@ -107,8 +107,8 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index) { } } - // first byte is len, second byte is string type - desc_str[0] = TUD_DESC_STR_HEADER(len); + // first byte is length (including header), second byte is string type + desc_str[0] = (TUSB_DESC_STRING << 8 ) | (2 * len + 2); return desc_str; } diff --git a/ports/nrf/modules/machine/adc.c b/ports/nrf/modules/machine/adc.c index 5a02dfc274..54870d2c18 100644 --- a/ports/nrf/modules/machine/adc.c +++ b/ports/nrf/modules/machine/adc.c @@ -71,22 +71,10 @@ STATIC const machine_adc_obj_t machine_adc_obj[] = { #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) { #if defined(NRF52_SERIES) - const nrfx_saadc_config_t config = { - .resolution = NRF_SAADC_RESOLUTION_8BIT, - .oversample = NRF_SAADC_OVERSAMPLE_DISABLED, - .interrupt_priority = 6, - .low_power_mode = false - }; - - nrfx_saadc_init(&config, saadc_event_handler); + const uint8_t interrupt_priority = 6; + nrfx_saadc_init(interrupt_priority); #endif } @@ -138,19 +126,22 @@ 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]; #if defined(NRF52_SERIES) - const nrf_saadc_channel_config_t config = { - .resistor_p = NRF_SAADC_RESISTOR_DISABLED, - .resistor_n = NRF_SAADC_RESISTOR_DISABLED, - .gain = NRF_SAADC_GAIN1_4, - .reference = NRF_SAADC_REFERENCE_VDD4, - .acq_time = NRF_SAADC_ACQTIME_3US, - .mode = NRF_SAADC_MODE_SINGLE_ENDED, - .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 + const nrfx_saadc_channel_t config = { \ + .channel_config = + { + .resistor_p = NRF_SAADC_RESISTOR_DISABLED, + .resistor_n = NRF_SAADC_RESISTOR_DISABLED, + .gain = NRF_SAADC_GAIN1_4, + .reference = NRF_SAADC_REFERENCE_VDD4, + .acq_time = NRF_SAADC_ACQTIME_3US, + .mode = NRF_SAADC_MODE_SINGLE_ENDED, + .burst = NRF_SAADC_BURST_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_channel_init(self->id, &config); + nrfx_saadc_channels_config(&config, 1); #endif return MP_OBJ_FROM_PTR(self); @@ -173,7 +164,9 @@ int16_t machine_adc_value_read(machine_adc_obj_t * adc_obj) { #else // NRF52 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 return value; } @@ -261,20 +254,26 @@ mp_obj_t machine_adc_battery_level(void) { #else // NRF52 nrf_saadc_value_t value = 0; - const nrf_saadc_channel_config_t config = { - .resistor_p = NRF_SAADC_RESISTOR_DISABLED, - .resistor_n = NRF_SAADC_RESISTOR_DISABLED, - .gain = NRF_SAADC_GAIN1_6, - .reference = NRF_SAADC_REFERENCE_INTERNAL, - .acq_time = NRF_SAADC_ACQTIME_3US, - .mode = NRF_SAADC_MODE_SINGLE_ENDED, - .burst = NRF_SAADC_BURST_DISABLED, - .pin_p = NRF_SAADC_INPUT_VDD, - .pin_n = NRF_SAADC_INPUT_DISABLED + const nrfx_saadc_channel_t config = { \ + .channel_config = + { + .resistor_p = NRF_SAADC_RESISTOR_DISABLED, + .resistor_n = NRF_SAADC_RESISTOR_DISABLED, + .gain = NRF_SAADC_GAIN1_6, + .reference = NRF_SAADC_REFERENCE_INTERNAL, + .acq_time = NRF_SAADC_ACQTIME_3US, + .mode = NRF_SAADC_MODE_SINGLE_ENDED, + .burst = NRF_SAADC_BURST_DISABLED, + }, + .pin_p = NRF_SAADC_INPUT_VDD, + .pin_n = NRF_SAADC_INPUT_DISABLED, + .channel_index = 0, }; + nrfx_saadc_channels_config(&config, 1); - nrfx_saadc_channel_init(0, &config); - nrfx_saadc_sample_convert(0, &value); + nrfx_saadc_simple_mode_set((1 << 0), NRF_SAADC_RESOLUTION_8BIT, NRF_SAADC_INPUT_DISABLED, NULL); + nrfx_saadc_buffer_set(&value, 1); + nrfx_saadc_mode_trigger(); #endif uint16_t batt_lvl_in_milli_volts = BATTERY_MILLIVOLT(value) + DIODE_VOLT_DROP_MILLIVOLT; diff --git a/ports/nrf/modules/machine/i2c.c b/ports/nrf/modules/machine/i2c.c index 799d46a142..afa906c359 100644 --- a/ports/nrf/modules/machine/i2c.c +++ b/ports/nrf/modules/machine/i2c.c @@ -49,10 +49,14 @@ #define nrfx_twi_init nrfx_twim_init #define nrfx_twi_enable nrfx_twim_enable -#define nrfx_twi_rx nrfx_twim_rx -#define nrfx_twi_tx nrfx_twim_tx +#define nrfx_twi_xfer nrfx_twim_xfer #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 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; int transfer_ret = 0; 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 { - 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; } diff --git a/ports/nrf/modules/machine/pin.c b/ports/nrf/modules/machine/pin.c index 938f609303..170caf6dde 100644 --- a/ports/nrf/modules/machine/pin.c +++ b/ports/nrf/modules/machine/pin.c @@ -114,7 +114,7 @@ void pin_init0(void) { } // Initialize GPIOTE if not done yet. if (!nrfx_gpiote_is_init()) { - nrfx_gpiote_init(); + nrfx_gpiote_init(NRFX_GPIOTE_DEFAULT_CONFIG_IRQ_PRIORITY); } #if PIN_DEBUG diff --git a/ports/nrf/modules/machine/pwm.c b/ports/nrf/modules/machine/pwm.c index a5a3be89b6..a750285841 100644 --- a/ports/nrf/modules/machine/pwm.c +++ b/ports/nrf/modules/machine/pwm.c @@ -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.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); diff --git a/ports/nrf/modules/machine/rtcounter.c b/ports/nrf/modules/machine/rtcounter.c index c06bb86d53..5fb28557d8 100644 --- a/ports/nrf/modules/machine/rtcounter.c +++ b/ports/nrf/modules/machine/rtcounter.c @@ -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) - 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); } // Make sure it's uninitialized. diff --git a/ports/nrf/modules/machine/temp.c b/ports/nrf/modules/machine/temp.c index 047514967f..82b02d09e9 100644 --- a/ports/nrf/modules/machine/temp.c +++ b/ports/nrf/modules/machine/temp.c @@ -29,12 +29,13 @@ #include "py/nlr.h" #include "py/runtime.h" +#include "py/mperrno.h" #include "py/mphal.h" #if MICROPY_PY_MACHINE_TEMP #include "temp.h" -#include "nrf_temp.h" +#include "nrfx_temp.h" #if BLUETOOTH_SD #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); } +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() /// Get temperature. 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 - 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); diff --git a/ports/nrf/modules/machine/uart.c b/ports/nrf/modules/machine/uart.c index 1ff95e90d3..4f69bfc620 100644 --- a/ports/nrf/modules/machine/uart.c +++ b/ports/nrf/modules/machine/uart.c @@ -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 #if MICROPY_HW_UART1_HWFC - config.hwfc = NRF_UART_HWFC_ENABLED; + config.hal_cfg.hwfc = NRF_UART_HWFC_ENABLED; #else - config.hwfc = NRF_UART_HWFC_DISABLED; + config.hal_cfg.hwfc = NRF_UART_HWFC_DISABLED; #endif - config.parity = NRF_UART_PARITY_EXCLUDED; + config.hal_cfg.parity = NRF_UART_PARITY_EXCLUDED; #if (BLUETOOTH_SD == 100) config.interrupt_priority = 3; diff --git a/ports/nrf/nrfx_config.h b/ports/nrf/nrfx_config.h index 0e9064a9e0..19c744510f 100644 --- a/ports/nrf/nrfx_config.h +++ b/ports/nrf/nrfx_config.h @@ -53,16 +53,25 @@ // for tinyusb // #define NRFX_IRQ_IS_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 #endif #define NRFX_GPIOTE_ENABLED 1 #define NRFX_GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS 1 #if NRF51 - #define NRFX_GPIOTE_CONFIG_IRQ_PRIORITY 3 + #define NRFX_GPIOTE_DEFAULT_CONFIG_IRQ_PRIORITY 3 #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 #if defined(NRF51) || defined(NRF52_SERIES) diff --git a/ports/nrf/nrfx_glue.h b/ports/nrf/nrfx_glue.h index 153403f1e7..b4a60257a8 100644 --- a/ports/nrf/nrfx_glue.h +++ b/ports/nrf/nrfx_glue.h @@ -32,6 +32,8 @@ #define NRFX_STATIC_ASSERT(expression) #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 #if BLUETOOTH_SD