8000 nrf: Fix non-running LFCLK by cwalther · Pull Request #13339 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

nrf: Fix non-running LFCLK #13339

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 2 commits into from
Closed
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
89 changes: 44 additions & 45 deletions ports/nrf/modules/machine/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@

typedef struct _machine_adc_obj_t {
mp_obj_base_t base;
uint8_t id;
#if NRF51
uint8_t ain;
#endif
uint8_t id;
#if NRF51
uint8_t ain;
#endif
} machine_adc_obj_t;

static const machine_adc_obj_t machine_adc_obj[] = {
#if NRF51
#if NRF51
{{&machine_adc_type}, .id = 0, .ain = NRF_ADC_CONFIG_INPUT_0},
{{&machine_adc_type}, .id = 1, .ain = NRF_ADC_CONFIG_INPUT_1},
{{&machine_adc_type}, .id = 2, .ain = NRF_ADC_CONFIG_INPUT_2},
Expand All @@ -54,7 +54,7 @@ static const machine_adc_obj_t machine_adc_obj[] = {
{{&machine_adc_type}, .id = 5, .ain = NRF_ADC_CONFIG_INPUT_5},
{{&machine_adc_type}, .id = 6, .ain = NRF_ADC_CONFIG_INPUT_6},
{{&machine_adc_type}, .id = 7, .ain = NRF_ADC_CONFIG_INPUT_7},
#else
#else
{{&machine_adc_type}, .id = 0},
{{&machine_adc_type}, .id = 1},
{{&machine_adc_type}, .id = 2},
Expand All @@ -63,14 +63,14 @@ static const machine_adc_obj_t machine_adc_obj[] = {
{{&machine_adc_type}, .id = 5},
{{&machine_adc_type}, .id = 6},
{{&machine_adc_type}, .id = 7},
#endif
#endif
};

void adc_init0(void) {
#if defined(NRF52_SERIES)
#if defined(NRF52_SERIES)
const uint8_t interrupt_priority = 6;
nrfx_saadc_init(interrupt_priority);
#endif
#endif
}

static int adc_find(mp_obj_t id) {
Expand Down Expand Up @@ -124,49 +124,49 @@ static mp_obj_t mp_machine_adc_make_new(const mp_obj_type_t *type, size_t n_args
int adc_id = adc_find(args[ARG_id].u_obj);
const machine_adc_obj_t *self = &machine_adc_obj[adc_id];

#if defined(NRF52_SERIES)
#if defined(NRF52_SERIES)
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,
.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,
.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);
#endif
#endif

return MP_OBJ_FROM_PTR(self);
}

int16_t machine_adc_value_read(machine_adc_obj_t * adc_obj) {
int16_t machine_adc_value_read(machine_adc_obj_t *adc_obj) {

#if NRF51
#if NRF51
nrf_adc_value_t value = 0;

nrfx_adc_channel_t channel_config = {
.config.resolution = NRF_ADC_CONFIG_RES_8BIT,
.config.input = NRF_ADC_CONFIG_SCALING_INPUT_TWO_THIRDS,
.config.reference = NRF_ADC_CONFIG_REF_VBG,
.config.input = adc_obj->ain,
.config.extref = ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos // Currently not defined in nrfx/hal.
.config.input = NRF_ADC_CONFIG_SCALING_INPUT_TWO_THIRDS,
.config.reference = NRF_ADC_CONFIG_REF_VBG,
.config.input = adc_obj->ain,
.config.extref = ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos // Currently not defined in nrfx/hal.
};

nrfx_adc_sample_convert(&channel_config, &value);
#else // NRF52
#else // NRF52
nrf_saadc_value_t value = 0;

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;
}

Expand Down Expand Up @@ -209,10 +209,9 @@ static MP_DEFINE_CONST_FUN_OBJ_1(mp_machine_adc_value_obj, machine_adc_value);
#define DIODE_VOLT_DROP_MILLIVOLT (270) // Voltage drop over diode.

#define BATTERY_MILLIVOLT(VALUE) \
((((VALUE) * ADC_REF_VOLTAGE_IN_MILLIVOLT) / 255) * ADC_PRE_SCALING_MULTIPLIER)
((((VALUE)*ADC_REF_VOLTAGE_IN_MILLIVOLT) / 255) * ADC_PRE_SCALING_MULTIPLIER)

static uint8_t battery_level_in_percent(const uint16_t mvolts)
{
static uint8_t battery_level_in_percent(const uint16_t mvolts) {
uint8_t battery_level;

if (mvolts >= 3000) {
Expand All @@ -236,42 +235,42 @@ static uint8_t battery_level_in_percent(const uint16_t mvolts)
/// Get battery level in percentage.
mp_obj_t machine_adc_battery_level(void) {

#if NRF51
#if NRF51
nrf_adc_value_t value = 0;

nrfx_adc_channel_t channel_config = {
.config.resolution = NRF_ADC_CONFIG_RES_8BIT,
.config.input = NRF_ADC_CONFIG_SCALING_SUPPLY_ONE_THIRD,
.config.reference = NRF_ADC_CONFIG_REF_VBG,
.config.input = NRF_ADC_CONFIG_INPUT_DISABLED,
.config.extref = ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos // Currently not defined in nrfx/hal.
.config.input = NRF_ADC_CONFIG_SCALING_SUPPLY_ONE_THIRD,
.config.reference = NRF_ADC_CONFIG_REF_VBG,
.config.input = NRF_ADC_CONFIG_INPUT_DISABLED,
.config.extref = ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos // Currently not defined in nrfx/hal.
};

nrfx_adc_sample_convert(&channel_config, &value);
#else // NRF52
#else // NRF52
nrf_saadc_value_t value = 0;

const nrfx_saadc_channel_t config = { E864 \
.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,
.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,
.pin_p = NRF_SAADC_INPUT_VDD,
.pin_n = NRF_SAADC_INPUT_DISABLED,
.channel_index = 0,
};
nrfx_saadc_channels_config(&config, 1);

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
#endif

uint16_t batt_lvl_in_milli_volts = BATTERY_MILLIVOLT(value) + DIODE_VOLT_DROP_MILLIVOLT;
uint16_t batt_in_percent = battery_level_in_percent(batt_lvl_in_milli_volts);
Expand Down
2 changes: 1 addition & 1 deletion ports/nrf/modules/machine/adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ typedef struct _machine_adc_obj_t machine_adc_obj_t;

void adc_init0(void);

int16_t machine_adc_value_read(machine_adc_obj_t * adc_obj);
int16_t machine_adc_value_read(machine_adc_obj_t *adc_obj);

#endif // ADC_H__
5 changes: 2 additions & 3 deletions ports/nrf/modules/machine/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

typedef struct _machine_hard_i2c_obj_t {
mp_obj_base_t base;
nrfx_twi_t p_twi; // Driver instance
nrfx_twi_t p_twi; // Driver instance
} machine_hard_i2c_obj_t;

static const machine_hard_i2c_obj_t machine_hard_i2c_obj[] = {
Expand Down Expand Up @@ -159,8 +159,7 @@ int machine_hard_i2c_transfer_single(mp_obj_base_t *self_in, uint16_t addr, size
if (err_code != NRFX_SUCCESS) {
if (err_code == NRFX_ERROR_DRV_TWI_ERR_ANACK) {
return -MP_ENODEV;
}
else if (err_code == NRFX_ERROR_DRV_TWI_ERR_DNACK) {
} else if (err_code == NRFX_ERROR_DRV_TWI_ERR_DNACK) {
return -MP_EIO;
}
return -MP_ETIMEDOUT;
Expand Down
20 changes: 10 additions & 10 deletions ports/nrf/modules/machine/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,16 @@ void machine_init(void) {
reset_cause = PYB_RESET_LOCKUP;
} else if (state & POWER_RESETREAS_OFF_Msk) {
reset_cause = PYB_RESET_POWER_ON;
#if !defined(NRF9160_XXAA)
#if !defined(NRF9160_XXAA)
} else if (state & POWER_RESETREAS_LPCOMP_Msk) {
reset_cause = PYB_RESET_LPCOMP;
#endif
#endif
} else if (state & POWER_RESETREAS_DIF_Msk) {
reset_cause = PYB_RESET_DIF;
#if defined(NRF52_SERIES)
#if defined(NRF52_SERIES)
} else if (state & POWER_RESETREAS_NFC_Msk) {
reset_cause = PYB_RESET_NFC;
#endif
#endif
}

// clear reset reason
Expand Down Expand Up @@ -216,22 +216,22 @@ static void mp_machine_set_freq(size_t n_args, const mp_obj_t *args) {
}

static mp_obj_t machine_enable_irq(void) {
#ifndef BLUETOOTH_SD
#ifndef BLUETOOTH_SD
__enable_irq();
#else
#else

#endif
#endif
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_0(machine_enable_irq_obj, machine_enable_irq);

// Resets the board in a manner similar to pushing the external RESET button.
static mp_obj_t machine_disable_irq(void) {
#ifndef BLUETOOTH_SD
#ifndef BLUETOOTH_SD
__disable_irq();
#else
#else

#endif
#endif
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_0(machine_disable_irq_obj, machine_disable_irq);
20 changes: 10 additions & 10 deletions ports/nrf/modules/machine/pin.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ static void pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t
}

mp_printf(print, "Pin(%d, mode=%s, pull=%s)",
self->pin,
(nrf_gpio_pin_dir_get(self->pin) == NRF_GPIO_PIN_DIR_OUTPUT) ? "OUT" : "IN",
pull);
self->pin,
(nrf_gpio_pin_dir_get(self->pin) == NRF_GPIO_PIN_DIR_OUTPUT) ? "OUT" : "IN",
pull);
}

static mp_obj_t pin_obj_init_helper(const pin_obj_t *pin, mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args);
Expand Down Expand Up @@ -375,11 +375,11 @@ static mp_obj_t pin_obj_init_helper(const pin_obj_t *self, mp_uint_t n_args, con

if (mode == NRF_GPIO_PIN_DIR_OUTPUT || mode == NRF_GPIO_PIN_DIR_INPUT) {
nrf_gpio_cfg(self->pin,
mode,
input,
pull,
NRF_GPIO_PIN_S0S1,
NRF_GPIO_PIN_NOSENSE);
mode,
input,
pull,
NRF_GPIO_PIN_S0S1,
NRF_GPIO_PIN_NOSENSE);
} else {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid pin mode: %d"), mode);
}
Expand Down Expand Up @@ -496,7 +496,7 @@ static MP_DEFINE_CONST_FUN_OBJ_1(pin_af_obj, pin_af);
static void pin_common_irq_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) {
mp_obj_t pin_handler = MP_STATE_PORT(pin_irq_handlers)[pin];
mp_obj_t pin_number = MP_OBJ_NEW_SMALL_INT(pin);
const pin_obj_t *pin_obj = pin_find(pin_number);
const pin_obj_t *pin_obj = pin_find(pin_number);

mp_call_function_1(pin_handler, (mp_obj_t)pin_obj);
}
Expand Down Expand Up @@ -591,7 +591,7 @@ static const mp_rom_map_elem_t pin_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_AF_OD), MP_ROM_INT(GPIO_MODE_AF_OD) },
{ MP_ROM_QSTR(MP_QSTR_PULL_NONE), MP_ROM_INT(GPIO_NOPULL) },
*/
#include "genhdr/pins_af_const.h"
#include "genhdr/pins_af_const.h"
};

static MP_DEFINE_CONST_DICT(pin_locals_dict, pin_locals_dict_table);
Expand Down
46 changes: 23 additions & 23 deletions ports/nrf/modules/machine/pin.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,43 +34,43 @@
#include "py/obj.h"

typedef struct {
mp_obj_base_t base;
qstr name;
uint8_t idx;
uint8_t fn;
uint8_t unit;
uint8_t type;

union {
void *reg;

PIN_DEFS_PORT_AF_UNION
};
mp_obj_base_t base;
qstr name;
uint8_t idx;
uint8_t fn;
uint8_t unit;
uint8_t type;

union {
void *reg;

PIN_DEFS_PORT_AF_UNION
};
} pin_af_obj_t;

typedef struct {
mp_obj_base_t base;
qstr name;
uint32_t pin : 8;
uint32_t num_af : 4;
uint32_t adc_channel : 5; // Some ARM processors use 32 bits/PORT
uint32_t adc_num : 3; // 1 bit per ADC
const pin_af_obj_t *af;
uint32_t pull;
mp_obj_base_t base;
qstr name;
uint32_t pin : 8;
uint32_t num_af : 4;
uint32_t adc_channel : 5; // Some ARM processors use 32 bits/PORT
uint32_t adc_num : 3; // 1 bit per ADC
const pin_af_obj_t *af;
uint32_t pull;
} pin_obj_t;

extern const mp_obj_type_t pin_type;
extern const mp_obj_type_t pin_af_type;

typedef struct {
const char *name;
const pin_obj_t *pin;
const char *name;
const pin_obj_t *pin;
} pin_named_pin_t;

extern const pin_named_pin_t pin_board_pins[];
extern const pin_named_pin_t pin_cpu_pins[];

//extern pin_map_obj_t pin_map_obj;
// extern pin_map_obj_t pin_map_obj;

typedef struct {
mp_obj_base_t base;
Expand Down
Loading
0