diff --git a/nrf5/hal/hal_temp.c b/nrf5/hal/hal_temp.c index 7d6015e1af..fa08c25279 100644 --- a/nrf5/hal/hal_temp.c +++ b/nrf5/hal/hal_temp.c @@ -30,21 +30,18 @@ #ifdef HAL_TEMP_MODULE_ENABLED -void hal_temp_init(void) -{ +void hal_temp_init(void) { // @note Workaround for PAN_028 rev2.0A anomaly 31 - TEMP: Temperature offset value has to be manually loaded to the TEMP module *(uint32_t *) 0x4000C504 = 0; } -int32_t hal_temp_read(void) -{ +int32_t hal_temp_read(void) { int32_t volatile temp; hal_temp_init(); NRF_TEMP->TASKS_START = 1; // Start the temperature measurement. - while (NRF_TEMP->EVENTS_DATARDY == 0) - { + while (NRF_TEMP->EVENTS_DATARDY == 0) { // Do nothing. } diff --git a/nrf5/modules/machine/modmachine.c b/nrf5/modules/machine/modmachine.c index fa3fbec2ff..9fc28f95e5 100644 --- a/nrf5/modules/machine/modmachine.c +++ b/nrf5/modules/machine/modmachine.c @@ -185,7 +185,7 @@ STATIC const mp_map_elem_t machine_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_PWM), (mp_obj_t)&machine_hard_pwm_type }, #endif #if MICROPY_PY_MACHINE_TEMP - { MP_OBJ_NEW_QSTR(MP_QSTR_TEMP), (mp_obj_t)&machine_temp_type }, + { MP_OBJ_NEW_QSTR(MP_QSTR_Temp), (mp_obj_t)&machine_temp_type }, #endif { MP_OBJ_NEW_QSTR(MP_QSTR_HARD_RESET), MP_OBJ_NEW_SMALL_INT(PYB_RESET_HARD) }, { MP_OBJ_NEW_QSTR(MP_QSTR_WDT_RESET), MP_OBJ_NEW_SMALL_INT(PYB_RESET_WDT) }, diff --git a/nrf5/modules/machine/temp.c b/nrf5/modules/machine/temp.c index 1f5687f6ab..01188485cd 100644 --- a/nrf5/modules/machine/temp.c +++ b/nrf5/modules/machine/temp.c @@ -35,16 +35,22 @@ #if MICROPY_PY_MACHINE_TEMP -typedef mp_obj_type_t machine_temp_type_t; +typedef struct _machine_temp_obj_t { + mp_obj_base_t base; +} machine_temp_obj_t; + +STATIC const machine_temp_obj_t machine_temp_obj; + +#define TEMP_BASE (NRF_TEMP_Type *)NRF_TEMP /// \method __str__() -/// Return a string describing the ADC object. +/// Return a string describing the Temp object. STATIC void machine_temp_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) { - machine_temp_type_t *self = o; + machine_temp_obj_t *self = o; (void)self; - mp_printf(print, "TEMP"); + mp_printf(print, "Temp.read()"); } /******************************************************************************/ @@ -58,18 +64,22 @@ STATIC mp_obj_t machine_temp_make_new(const mp_obj_type_t *type, size_t n_args, // parse args mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - - const machine_temp_type_t *self = (machine_temp_type_t*) NRF_TEMP_BASE; + + machine_temp_obj_t *self = m_new_obj(machine_temp_obj_t); + + self->base.type = &machine_temp_type; return MP_OBJ_FROM_PTR(self); } /// \method read() /// Get temperature. -mp_obj_t machine_temp_read(void) { +STATIC mp_obj_t machine_temp_read(mp_uint_t n_args, const mp_obj_t *args) { + return MP_OBJ_NEW_SMALL_INT(hal_temp_read()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_machine_temp_read_obj, machine_temp_read); + +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_temp_read_obj, 0, 1, machine_temp_read); STATIC const mp_map_elem_t machine_temp_locals_dict_table[] = { // instance methods @@ -81,7 +91,7 @@ STATIC MP_DEFINE_CONST_DICT(machine_temp_locals_dict, machine_temp_locals_dict_t const mp_obj_type_t machine_temp_type = { { &mp_type_type }, - .name = MP_QSTR_TEMP, + .name = MP_QSTR_Temp, .make_new = machine_temp_make_new, .locals_dict = (mp_obj_t)&machine_temp_locals_dict, .print = machine_temp_print,