diff options
Diffstat (limited to 'stmhal')
45 files changed, 215 insertions, 140 deletions
diff --git a/stmhal/adc.c b/stmhal/adc.c index c2419d543f..817b32ea89 100644 --- a/stmhal/adc.c +++ b/stmhal/adc.c @@ -202,7 +202,9 @@ STATIC mp_obj_t adc_read(mp_obj_t self_in) { STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_read_obj, adc_read); /// \method read_timed(buf, freq) -/// Read analog values into the given buffer at the given frequency. +/// Read analog values into the given buffer at the given frequency. Buffer +/// can be bytearray or array.array for example. If a buffer with 8-bit elements +/// is used, sample resolution will be reduced to 8 bits. /// /// Example: /// diff --git a/stmhal/boards/HYDRABUS/mpconfigboard.h b/stmhal/boards/HYDRABUS/mpconfigboard.h index f87a14142e..db49434b5b 100644 --- a/stmhal/boards/HYDRABUS/mpconfigboard.h +++ b/stmhal/boards/HYDRABUS/mpconfigboard.h @@ -1,6 +1,7 @@ #define HYDRABUSV10 #define MICROPY_HW_BOARD_NAME "HydraBus1.0" +#define MICROPY_HW_MCU_NAME "STM32F4" #define MICROPY_HW_HAS_SWITCH (1) #define MICROPY_HW_HAS_SDCARD (1) diff --git a/stmhal/boards/NETDUINO_PLUS_2/mpconfigboard.h b/stmhal/boards/NETDUINO_PLUS_2/mpconfigboard.h index 0e40545253..2679aee576 100644 --- a/stmhal/boards/NETDUINO_PLUS_2/mpconfigboard.h +++ b/stmhal/boards/NETDUINO_PLUS_2/mpconfigboard.h @@ -1,6 +1,7 @@ #define NETDUINO_PLUS_2 #define MICROPY_HW_BOARD_NAME "NetduinoPlus2" +#define MICROPY_HW_MCU_NAME "STM32F405RG" #define MICROPY_HW_HAS_SWITCH (1) diff --git a/stmhal/boards/PYBV10/mpconfigboard.h b/stmhal/boards/PYBV10/mpconfigboard.h index 3def531232..4ae6954a7d 100644 --- a/stmhal/boards/PYBV10/mpconfigboard.h +++ b/stmhal/boards/PYBV10/mpconfigboard.h @@ -1,6 +1,7 @@ #define PYBV10 #define MICROPY_HW_BOARD_NAME "PYBv1.0" +#define MICROPY_HW_MCU_NAME "STM32F405RG" #define MICROPY_HW_HAS_SWITCH (1) #define MICROPY_HW_HAS_SDCARD (1) diff --git a/stmhal/boards/PYBV3/mpconfigboard.h b/stmhal/boards/PYBV3/mpconfigboard.h index ac0d84ca29..43d860a0cc 100644 --- a/stmhal/boards/PYBV3/mpconfigboard.h +++ b/stmhal/boards/PYBV3/mpconfigboard.h @@ -1,6 +1,7 @@ #define PYBV3 #define MICROPY_HW_BOARD_NAME "PYBv3" +#define MICROPY_HW_MCU_NAME "STM32F405RG" #define MICROPY_HW_HAS_SWITCH (1) #define MICROPY_HW_HAS_SDCARD (1) diff --git a/stmhal/boards/PYBV4/mpconfigboard.h b/stmhal/boards/PYBV4/mpconfigboard.h index 9fedb70136..a278dea9fb 100644 --- a/stmhal/boards/PYBV4/mpconfigboard.h +++ b/stmhal/boards/PYBV4/mpconfigboard.h @@ -1,6 +1,7 @@ #define PYBV4 #define MICROPY_HW_BOARD_NAME "PYBv4" +#define MICROPY_HW_MCU_NAME "STM32F405RG" #define MICROPY_HW_HAS_SWITCH (1) #define MICROPY_HW_HAS_SDCARD (1) diff --git a/stmhal/boards/STM32F4DISC/mpconfigboard.h b/stmhal/boards/STM32F4DISC/mpconfigboard.h index e6780eacbd..10bbe45188 100644 --- a/stmhal/boards/STM32F4DISC/mpconfigboard.h +++ b/stmhal/boards/STM32F4DISC/mpconfigboard.h @@ -1,6 +1,7 @@ #define STM32F4DISC #define MICROPY_HW_BOARD_NAME "F4DISC" +#define MICROPY_HW_MCU_NAME "STM32F407" #define MICROPY_HW_HAS_SWITCH (1) #define MICROPY_HW_HAS_SDCARD (0) diff --git a/stmhal/boards/stm32f4xx-prefix.c b/stmhal/boards/stm32f4xx-prefix.c index 4d2313075a..3bbb6bda0e 100644 --- a/stmhal/boards/stm32f4xx-prefix.c +++ b/stmhal/boards/stm32f4xx-prefix.c @@ -5,8 +5,8 @@ #include "stm32f4xx_hal.h" -#include "misc.h" #include "mpconfig.h" +#include "misc.h" #include "qstr.h" #include "obj.h" #include "pin.h" diff --git a/stmhal/bufhelper.c b/stmhal/bufhelper.c index dd72655520..1f823ea963 100644 --- a/stmhal/bufhelper.c +++ b/stmhal/bufhelper.c @@ -24,8 +24,8 @@ * THE SOFTWARE. */ -#include "misc.h" #include "mpconfig.h" +#include "misc.h" #include "qstr.h" #include "obj.h" #include "bufhelper.h" diff --git a/stmhal/dac.c b/stmhal/dac.c index 81ce993a8c..725e14e906 100644 --- a/stmhal/dac.c +++ b/stmhal/dac.c @@ -237,7 +237,7 @@ STATIC const mp_arg_t pyb_dac_write_timed_args[] = { { MP_QSTR_freq, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} }, { MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DMA_NORMAL} }, }; -#define PYB_DAC_WRITE_TIMED_NUM_ARGS ARRAY_SIZE(pyb_dac_write_timed_args) +#define PYB_DAC_WRITE_TIMED_NUM_ARGS MP_ARRAY_SIZE(pyb_dac_write_timed_args) mp_obj_t pyb_dac_write_timed(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) { pyb_dac_obj_t *self = args[0]; diff --git a/stmhal/diskio.c b/stmhal/diskio.c index cde5874946..35b9eab99f 100644 --- a/stmhal/diskio.c +++ b/stmhal/diskio.c @@ -32,8 +32,8 @@ #include "stm32f4xx_hal.h" -#include "misc.h" #include "mpconfig.h" +#include "misc.h" #include "qstr.h" #include "obj.h" #include "systick.h" diff --git a/stmhal/extint.c b/stmhal/extint.c index 24d51ffb8d..591246cdd1 100644 --- a/stmhal/extint.c +++ b/stmhal/extint.c @@ -296,7 +296,7 @@ STATIC const mp_arg_t pyb_extint_make_new_args[] = { { MP_QSTR_pull, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} }, { MP_QSTR_callback, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, }; -#define PYB_EXTINT_MAKE_NEW_NUM_ARGS ARRAY_SIZE(pyb_extint_make_new_args) +#define PYB_EXTINT_MAKE_NEW_NUM_ARGS MP_ARRAY_SIZE(pyb_extint_make_new_args) STATIC mp_obj_t extint_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { // type_in == extint_obj_type diff --git a/stmhal/gccollect.c b/stmhal/gccollect.c index 79082e2f2e..c71ed13a5e 100644 --- a/stmhal/gccollect.c +++ b/stmhal/gccollect.c @@ -25,15 +25,15 @@ */ #include <stdio.h> +#include <stdint.h> -#include <stm32f4xx_hal.h> - -#include "misc.h" #include "mpconfig.h" +#include "misc.h" #include "qstr.h" #include "obj.h" #include "gc.h" #include "gccollect.h" +#include MICROPY_HAL_H machine_uint_t gc_helper_get_regs_and_sp(machine_uint_t *regs); diff --git a/stmhal/i2c.c b/stmhal/i2c.c index 00501a57da..b6ab531293 100644 --- a/stmhal/i2c.c +++ b/stmhal/i2c.c @@ -220,7 +220,7 @@ STATIC const mp_arg_t pyb_i2c_init_args[] = { { MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 400000} }, { MP_QSTR_gencall, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} }, }; -#define PYB_I2C_INIT_NUM_ARGS ARRAY_SIZE(pyb_i2c_init_args) +#define PYB_I2C_INIT_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_init_args) STATIC mp_obj_t pyb_i2c_init_helper(const pyb_i2c_obj_t *self, uint n_args, const mp_obj_t *args, mp_map_t *kw_args) { // parse args @@ -271,7 +271,7 @@ STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const machine_int_t i2c_id = mp_obj_get_int(args[0]) - 1; // check i2c number - if (!(0 <= i2c_id && i2c_id < ARRAY_SIZE(pyb_i2c_obj) && pyb_i2c_obj[i2c_id].i2c != NULL)) { + if (!(0 <= i2c_id && i2c_id < MP_ARRAY_SIZE(pyb_i2c_obj) && pyb_i2c_obj[i2c_id].i2c != NULL)) { nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "I2C bus %d does not exist", i2c_id + 1)); } @@ -363,7 +363,7 @@ STATIC const mp_arg_t pyb_i2c_send_args[] = { { MP_QSTR_addr, MP_ARG_INT, {.u_int = PYB_I2C_MASTER_ADDRESS} }, { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 5000} }, }; -#define PYB_I2C_SEND_NUM_ARGS ARRAY_SIZE(pyb_i2c_send_args) +#define PYB_I2C_SEND_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_send_args) STATIC mp_obj_t pyb_i2c_send(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) { pyb_i2c_obj_t *self = args[0]; @@ -414,7 +414,7 @@ STATIC const mp_arg_t pyb_i2c_recv_args[] = { { MP_QSTR_addr, MP_ARG_INT, {.u_int = PYB_I2C_MASTER_ADDRESS} }, { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 5000} }, }; -#define PYB_I2C_RECV_NUM_ARGS ARRAY_SIZE(pyb_i2c_recv_args) +#define PYB_I2C_RECV_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_recv_args) STATIC mp_obj_t pyb_i2c_recv(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) { pyb_i2c_obj_t *self = args[0]; @@ -470,7 +470,7 @@ STATIC const mp_arg_t pyb_i2c_mem_read_args[] = { { MP_QSTR_memaddr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} }, { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 5000} }, }; -#define PYB_I2C_MEM_READ_NUM_ARGS ARRAY_SIZE(pyb_i2c_mem_read_args) +#define PYB_I2C_MEM_READ_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_mem_read_args) STATIC mp_obj_t pyb_i2c_mem_read(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) { pyb_i2c_obj_t *self = args[0]; diff --git a/stmhal/import.c b/stmhal/import.c index abc618bef0..88ddaefacf 100644 --- a/stmhal/import.c +++ b/stmhal/import.c @@ -27,8 +27,8 @@ #include <stdio.h> #include <stdint.h> -#include "misc.h" #include "mpconfig.h" +#include "misc.h" #include "qstr.h" #include "lexer.h" #include "ff.h" diff --git a/stmhal/led.c b/stmhal/led.c index e23b30ab75..c1b298b179 100644 --- a/stmhal/led.c +++ b/stmhal/led.c @@ -61,7 +61,7 @@ STATIC const pyb_led_obj_t pyb_led_obj[] = { #endif #endif }; -#define NUM_LEDS ARRAY_SIZE(pyb_led_obj) +#define NUM_LEDS MP_ARRAY_SIZE(pyb_led_obj) void led_init(void) { /* GPIO structure */ diff --git a/stmhal/lexerfatfs.c b/stmhal/lexerfatfs.c index 6a0e83fb13..c578b13af6 100644 --- a/stmhal/lexerfatfs.c +++ b/stmhal/lexerfatfs.c @@ -27,8 +27,8 @@ #include <stdint.h> #include <stdio.h> -#include "misc.h" #include "mpconfig.h" +#include "misc.h" #include "qstr.h" #include "lexer.h" #include "lexerfatfs.h" diff --git a/stmhal/main.c b/stmhal/main.c index 9751dcac2e..0e260e18cd 100644 --- a/stmhal/main.c +++ b/stmhal/main.c @@ -29,10 +29,10 @@ #include "stm32f4xx_hal.h" +#include "mpconfig.h" #include "misc.h" #include "systick.h" #include "pendsv.h" -#include "mpconfig.h" #include "qstr.h" #include "misc.h" #include "nlr.h" @@ -40,6 +40,7 @@ #include "parse.h" #include "obj.h" #include "runtime.h" +#include "stackctrl.h" #include "gc.h" #include "gccollect.h" #include "pybstdio.h" @@ -85,7 +86,7 @@ void flash_error(int n) { led_state(PYB_LED_R2, 0); } -void __fatal_error(const char *msg) { +void NORETURN __fatal_error(const char *msg) { for (volatile uint delay = 0; delay < 10000000; delay++) { } led_state(1, 1); @@ -111,8 +112,7 @@ void nlr_jump_fail(void *val) { } #ifndef NDEBUG -void __attribute__((weak)) - __assert_func(const char *file, int line, const char *func, const char *expr) { +void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) { (void)func; printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line); __fatal_error(""); @@ -186,6 +186,10 @@ static const char fresh_readme_txt[] = int main(void) { // TODO disable JTAG + // Stack limit should be less than real stack size, so we + // had chance to recover from limit hit. + mp_stack_set_limit(&_ram_end - &_heap_end - 512); + /* STM32F4xx HAL library initialization: - Configure the Flash prefetch, instruction and Data caches - Configure the Systick to generate an interrupt each 1 msec @@ -308,7 +312,7 @@ soft_reset: MP_OBJ_NEW_SMALL_INT(115200), }; pyb_uart_global_debug = pyb_uart_type.make_new((mp_obj_t)&pyb_uart_type, - ARRAY_SIZE(args), + MP_ARRAY_SIZE(args), 0, args); } #else diff --git a/stmhal/math.c b/stmhal/math.c index 637f447cf1..91ffb2503f 100644 --- a/stmhal/math.c +++ b/stmhal/math.c @@ -72,6 +72,7 @@ float __attribute__((pcs("aapcs"))) __aeabi_d2f(double x) { fx.m = (dx.m>>(52-23)); // right justify return fx.f; } + double __aeabi_dmul(double x , double y) { return 0.0; @@ -85,6 +86,18 @@ float sqrtf(float x) { return x; } +#ifndef NDEBUG +float copysignf(float x, float y) { + float_s_t fx={.f = x}; + float_s_t fy={.f = y}; + + // copy sign bit; + fx.s = fy.s; + + return fx.f; +} +#endif + // some compilers define log2f in terms of logf #ifdef log2f #undef log2f diff --git a/stmhal/modos.c b/stmhal/modos.c index 4a6949a844..e0df05ca6b 100644 --- a/stmhal/modos.c +++ b/stmhal/modos.c @@ -194,8 +194,8 @@ STATIC const mp_obj_dict_t os_module_globals = { .map = { .all_keys_are_qstrs = 1, .table_is_fixed_array = 1, - .used = ARRAY_SIZE(os_module_globals_table), - .alloc = ARRAY_SIZE(os_module_globals_table), + .used = MP_ARRAY_SIZE(os_module_globals_table), + .alloc = MP_ARRAY_SIZE(os_module_globals_table), .table = (mp_map_elem_t*)os_module_globals_table, }, }; diff --git a/stmhal/modpyb.c b/stmhal/modpyb.c index 6879a2c1b7..9dcd0e76ca 100644 --- a/stmhal/modpyb.c +++ b/stmhal/modpyb.c @@ -426,8 +426,8 @@ STATIC const mp_obj_dict_t pyb_module_globals = { .map = { .all_keys_are_qstrs = 1, .table_is_fixed_array = 1, - .used = ARRAY_SIZE(pyb_module_globals_table), - .alloc = ARRAY_SIZE(pyb_module_globals_table), + .used = MP_ARRAY_SIZE(pyb_module_globals_table), + .alloc = MP_ARRAY_SIZE(pyb_module_globals_table), .table = (mp_map_elem_t*)pyb_module_globals_table, }, }; diff --git a/stmhal/modstm.c b/stmhal/modstm.c index 520c8e51bd..1196ff82ff 100644 --- a/stmhal/modstm.c +++ b/stmhal/modstm.c @@ -131,8 +131,8 @@ STATIC const mp_obj_dict_t stm_module_globals = { .map = { .all_keys_are_qstrs = 1, .table_is_fixed_array = 1, - .used = ARRAY_SIZE(stm_module_globals_table), - .alloc = ARRAY_SIZE(stm_module_globals_table), + .used = MP_ARRAY_SIZE(stm_module_globals_table), + .alloc = MP_ARRAY_SIZE(stm_module_globals_table), .table = (mp_map_elem_t*)stm_module_globals_table, }, }; diff --git a/stmhal/modtime.c b/stmhal/modtime.c index 4fbee31293..ea4e3210a6 100644 --- a/stmhal/modtime.c +++ b/stmhal/modtime.c @@ -122,8 +122,8 @@ STATIC const mp_obj_dict_t time_module_globals = { .map = { .all_keys_are_qstrs = 1, .table_is_fixed_array = 1, - .used = ARRAY_SIZE(time_module_globals_table), - .alloc = ARRAY_SIZE(time_module_globals_table), + .used = MP_ARRAY_SIZE(time_module_globals_table), + .alloc = MP_ARRAY_SIZE(time_module_globals_table), .table = (mp_map_elem_t*)time_module_globals_table, }, }; diff --git a/stmhal/mpconfigport.h b/stmhal/mpconfigport.h index 28cd90bb01..28e654c8a6 100644 --- a/stmhal/mpconfigport.h +++ b/stmhal/mpconfigport.h @@ -44,6 +44,7 @@ */ #define MICROPY_ENABLE_LFN (1) #define MICROPY_LFN_CODE_PAGE (437) /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */ +#define MICROPY_PY_BUILTINS_STR_UNICODE (0) #define MICROPY_PY_BUILTINS_FROZENSET (1) #define MICROPY_PY_SYS_EXIT (1) #define MICROPY_PY_SYS_STDFILES (1) @@ -104,3 +105,6 @@ typedef const void *machine_const_ptr_t; // must be of pointer size // We need to provide a declaration/definition of alloca() #include <alloca.h> + +#define MICROPY_HAL_H "mphal.h" +#define MICROPY_PIN_DEFS_PORT_H "pin_defs_stmhal.h" diff --git a/stmhal/mphal.h b/stmhal/mphal.h new file mode 100644 index 0000000000..4e9a8b2bb8 --- /dev/null +++ b/stmhal/mphal.h @@ -0,0 +1,7 @@ +// We use the ST Cube HAL library for most hardware peripherals +#include <stm32f4xx_hal.h> + +// Basic GPIO functions +#define GPIO_read_pin(gpio, pin) (((gpio)->IDR >> (pin)) & 1) +#define GPIO_set_pin(gpio, pin_mask) (((gpio)->BSRRL) = (pin_mask)) +#define GPIO_clear_pin(gpio, pin_mask) (((gpio)->BSRRH) = (pin_mask)) diff --git a/stmhal/pendsv.c b/stmhal/pendsv.c index f8e3b30072..a0eff7e5de 100644 --- a/stmhal/pendsv.c +++ b/stmhal/pendsv.c @@ -27,8 +27,8 @@ #include <stdlib.h> #include <stm32f4xx_hal.h> -#include "misc.h" #include "mpconfig.h" +#include "misc.h" #include "qstr.h" #include "obj.h" #include "pendsv.h" diff --git a/stmhal/pin.c b/stmhal/pin.c index 9806a8c49a..a9ebfa9766 100644 --- a/stmhal/pin.c +++ b/stmhal/pin.c @@ -28,14 +28,13 @@ #include <stdint.h> #include <string.h> -#include "stm32f4xx_hal.h" - #include "mpconfig.h" #include "nlr.h" #include "misc.h" #include "qstr.h" #include "obj.h" #include "runtime.h" +#include MICROPY_HAL_H #include "pin.h" /// \moduleref pyb @@ -100,8 +99,8 @@ STATIC mp_obj_t pin_class_map_dict; STATIC bool pin_class_debug; void pin_init(void) { - pin_class_mapper = MP_OBJ_NULL; - pin_class_map_dict = MP_OBJ_NULL; + pin_class_mapper = mp_const_none; + pin_class_map_dict = mp_const_none; pin_class_debug = false; } @@ -120,7 +119,7 @@ const pin_obj_t *pin_find(mp_obj_t user_obj) { return pin_obj; } - if (pin_class_mapper != MP_OBJ_NULL) { + if (pin_class_mapper != mp_const_none) { pin_obj = mp_call_function_1(pin_class_mapper, user_obj); if (pin_obj != mp_const_none) { if (!MP_OBJ_IS_TYPE(pin_obj, &pin_type)) { @@ -139,7 +138,7 @@ const pin_obj_t *pin_find(mp_obj_t user_obj) { // other lookup methods. } - if (pin_class_map_dict != MP_OBJ_NULL) { + if (pin_class_map_dict != mp_const_none) { mp_map_t *pin_map_map = mp_obj_dict_get_map(pin_class_map_dict); mp_map_elem_t *elem = mp_map_lookup(pin_map_map, user_obj, MP_MAP_LOOKUP); if (elem != NULL && elem->value != NULL) { @@ -310,13 +309,13 @@ STATIC mp_obj_t pin_value(uint n_args, mp_obj_t *args) { pin_obj_t *self = args[0]; if (n_args == 1) { // get pin - return MP_OBJ_NEW_SMALL_INT((self->gpio->IDR >> self->pin) & 1); + return MP_OBJ_NEW_SMALL_INT(GPIO_read_pin(self->gpio, self->pin)); } else { // set pin if (mp_obj_is_true(args[1])) { - self->gpio->BSRRL = self->pin_mask; + GPIO_set_pin(self->gpio, self->pin_mask); } else { - self->gpio->BSRRH = self->pin_mask; + GPIO_clear_pin(self->gpio, self->pin_mask); } return mp_const_none; } @@ -327,7 +326,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pin_value_obj, 1, 2, pin_value); /// Set the pin to a low logic level. STATIC mp_obj_t pin_low(mp_obj_t self_in) { pin_obj_t *self = self_in; - self->gpio->BSRRH = self->pin_mask; + GPIO_clear_pin(self->gpio, self->pin_mask);; return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_low_obj, pin_low); @@ -336,7 +335,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_low_obj, pin_low); /// Set the pin to a high logic level. STATIC mp_obj_t pin_high(mp_obj_t self_in) { pin_obj_t *self = self_in; - self->gpio->BSRRL = self->pin_mask; + GPIO_set_pin(self->gpio, self->pin_mask);; return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_high_obj, pin_high); diff --git a/stmhal/pin.h b/stmhal/pin.h index d60bd22fd7..8513db109b 100644 --- a/stmhal/pin.h +++ b/stmhal/pin.h @@ -24,63 +24,10 @@ * THE SOFTWARE. */ -enum { - PORT_A, - PORT_B, - PORT_C, - PORT_D, - PORT_E, - PORT_F, - PORT_G, - PORT_H, - PORT_I, - PORT_J, -}; +// This file requires pin_defs_xxx.h (which has port specific enums and +// defines, so we include it here. It should never be included directly -enum { - AF_FN_TIM, - AF_FN_I2C, - AF_FN_USART, - AF_FN_UART = AF_FN_USART, - AF_FN_SPI -}; - -enum { - AF_PIN_TYPE_TIM_CH1 = 0, - AF_PIN_TYPE_TIM_CH2, - AF_PIN_TYPE_TIM_CH3, - AF_PIN_TYPE_TIM_CH4, - AF_PIN_TYPE_TIM_CH1N, - AF_PIN_TYPE_TIM_CH2N, - AF_PIN_TYPE_TIM_CH3N, - AF_PIN_TYPE_TIM_CH1_ETR, - AF_PIN_TYPE_TIM_ETR, - AF_PIN_TYPE_TIM_BKIN, - - AF_PIN_TYPE_I2C_SDA = 0, - AF_PIN_TYPE_I2C_SCL, - - AF_PIN_TYPE_USART_TX = 0, - AF_PIN_TYPE_USART_RX, - AF_PIN_TYPE_USART_CTS, - AF_PIN_TYPE_USART_RTS, - AF_PIN_TYPE_USART_CK, - AF_PIN_TYPE_UART_TX = AF_PIN_TYPE_USART_TX, - AF_PIN_TYPE_UART_RX = AF_PIN_TYPE_USART_RX, - AF_PIN_TYPE_UART_CTS = AF_PIN_TYPE_USART_CTS, - AF_PIN_TYPE_UART_RTS = AF_PIN_TYPE_USART_RTS, - - AF_PIN_TYPE_SPI_MOSI = 0, - AF_PIN_TYPE_SPI_MISO, - AF_PIN_TYPE_SPI_SCK, - AF_PIN_TYPE_SPI_NSS, -}; - -enum { - PIN_ADC1 = (1 << 0), - PIN_ADC2 = (1 << 1), - PIN_ADC3 = (1 << 2), -}; +#include MICROPY_PIN_DEFS_PORT_H typedef struct { mp_obj_base_t base; @@ -91,24 +38,21 @@ typedef struct { union { void *reg; - TIM_TypeDef *TIM; - I2C_TypeDef *I2C; - USART_TypeDef *USART; - USART_TypeDef *UART; - SPI_TypeDef *SPI; + + PIN_DEFS_PORT_AF_UNION }; } pin_af_obj_t; typedef struct { mp_obj_base_t base; const char *name; - uint16_t port : 4; - uint16_t pin : 4; - uint16_t num_af : 4; - uint16_t adc_channel : 4; - uint16_t adc_num : 3; // 1 bit per ADC - uint16_t pin_mask; - GPIO_TypeDef *gpio; + uint32_t port : 4; + uint32_t pin : 5; // Some ARM processors use 32 bits/PORT + 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 + uint32_t pin_mask; + pin_gpio_t *gpio; const pin_af_obj_t *af; } pin_obj_t; diff --git a/stmhal/pin_defs_stmhal.h b/stmhal/pin_defs_stmhal.h new file mode 100644 index 0000000000..2b6d9da0e7 --- /dev/null +++ b/stmhal/pin_defs_stmhal.h @@ -0,0 +1,96 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013, 2014 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +// This file contains pin definitions that are specific to the stmhal port. +// This file should only ever be #included by pin.h and not directly. + +enum { + PORT_A, + PORT_B, + PORT_C, + PORT_D, + PORT_E, + PORT_F, + PORT_G, + PORT_H, + PORT_I, + PORT_J, +}; + +enum { + AF_FN_TIM, + AF_FN_I2C, + AF_FN_USART, + AF_FN_UART = AF_FN_USART, + AF_FN_SPI +}; + +enum { + AF_PIN_TYPE_TIM_CH1 = 0, + AF_PIN_TYPE_TIM_CH2, + AF_PIN_TYPE_TIM_CH3, + AF_PIN_TYPE_TIM_CH4, + AF_PIN_TYPE_TIM_CH1N, + AF_PIN_TYPE_TIM_CH2N, + AF_PIN_TYPE_TIM_CH3N, + AF_PIN_TYPE_TIM_CH1_ETR, + AF_PIN_TYPE_TIM_ETR, + AF_PIN_TYPE_TIM_BKIN, + + AF_PIN_TYPE_I2C_SDA = 0, + AF_PIN_TYPE_I2C_SCL, + + AF_PIN_TYPE_USART_TX = 0, + AF_PIN_TYPE_USART_RX, + AF_PIN_TYPE_USART_CTS, + AF_PIN_TYPE_USART_RTS, + AF_PIN_TYPE_USART_CK, + AF_PIN_TYPE_UART_TX = AF_PIN_TYPE_USART_TX, + AF_PIN_TYPE_UART_RX = AF_PIN_TYPE_USART_RX, + AF_PIN_TYPE_UART_CTS = AF_PIN_TYPE_USART_CTS, + AF_PIN_TYPE_UART_RTS = AF_PIN_TYPE_USART_RTS, + + AF_PIN_TYPE_SPI_MOSI = 0, + AF_PIN_TYPE_SPI_MISO, + AF_PIN_TYPE_SPI_SCK, + AF_PIN_TYPE_SPI_NSS, +}; + +enum { + PIN_ADC1 = (1 << 0), + PIN_ADC2 = (1 << 1), + PIN_ADC3 = (1 << 2), +}; + +#define PIN_DEFS_PORT_AF_UNION \ + TIM_TypeDef *TIM; \ + I2C_TypeDef *I2C; \ + USART_TypeDef *USART; \ + USART_TypeDef *UART; \ + SPI_TypeDef *SPI; + +typedef GPIO_TypeDef pin_gpio_t; + diff --git a/stmhal/pin_named_pins.c b/stmhal/pin_named_pins.c index 3a1794e1d9..dbf03d1a0f 100644 --- a/stmhal/pin_named_pins.c +++ b/stmhal/pin_named_pins.c @@ -28,13 +28,12 @@ #include <stdint.h> #include <string.h> -#include "stm32f4xx_hal.h" - -#include "misc.h" #include "mpconfig.h" +#include "misc.h" #include "qstr.h" #include "obj.h" #include "runtime.h" +#include MICROPY_HAL_H #include "pin.h" STATIC void pin_named_pins_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { diff --git a/stmhal/printf.c b/stmhal/printf.c index 26c552039f..c4731aa88d 100644 --- a/stmhal/printf.c +++ b/stmhal/printf.c @@ -28,10 +28,10 @@ #include <string.h> #include <stdarg.h> +#include "mpconfig.h" #include "std.h" #include "misc.h" #include "systick.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "pfenv.h" diff --git a/stmhal/pybstdio.c b/stmhal/pybstdio.c index 05ea06eb44..5447a62202 100644 --- a/stmhal/pybstdio.c +++ b/stmhal/pybstdio.c @@ -25,15 +25,15 @@ */ #include <stdio.h> +#include <stdint.h> -#include <stm32f4xx_hal.h> - -#include "misc.h" #include "mpconfig.h" +#include "misc.h" #include "qstr.h" #include "misc.h" #include "obj.h" #include "stream.h" +#include MICROPY_HAL_H #include "pybstdio.h" #include "usb.h" #include "uart.h" diff --git a/stmhal/pyexec.c b/stmhal/pyexec.c index 45928427e1..93f4d62a96 100644 --- a/stmhal/pyexec.c +++ b/stmhal/pyexec.c @@ -26,8 +26,7 @@ #include <stdlib.h> #include <stdio.h> - -#include <stm32f4xx_hal.h> +#include <stdint.h> #include "mpconfig.h" #include "nlr.h" @@ -43,6 +42,7 @@ #include "repl.h" #include "gc.h" #include "gccollect.h" +#include MICROPY_HAL_H #include "systick.h" #include "pybstdio.h" #include "readline.h" @@ -185,7 +185,7 @@ int pyexec_friendly_repl(void) { #endif friendly_repl_reset: - stdout_tx_str("Micro Python " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_HW_BOARD_NAME " with STM32F405RG\r\n"); + stdout_tx_str("Micro Python " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME "\r\n"); stdout_tx_str("Type \"help()\" for more information.\r\n"); // to test ctrl-C diff --git a/stmhal/readline.c b/stmhal/readline.c index d40bd4219b..0703dcf4ea 100644 --- a/stmhal/readline.c +++ b/stmhal/readline.c @@ -25,15 +25,15 @@ */ #include <stdio.h> +#include <stdint.h> #include <string.h> -#include <stm32f4xx_hal.h> - -#include "misc.h" #include "mpconfig.h" +#include "misc.h" #include "qstr.h" #include "misc.h" #include "obj.h" +#include MICROPY_HAL_H #include "pybstdio.h" #include "readline.h" #include "usb.h" diff --git a/stmhal/rng.c b/stmhal/rng.c index 69fcb9d6ff..ea636770c0 100644 --- a/stmhal/rng.c +++ b/stmhal/rng.c @@ -28,8 +28,8 @@ #include "stm32f4xx_hal.h" -#include "misc.h" #include "mpconfig.h" +#include "misc.h" #include "qstr.h" #include "obj.h" #include "rng.h" diff --git a/stmhal/rtc.c b/stmhal/rtc.c index 412816c396..8f0d007327 100644 --- a/stmhal/rtc.c +++ b/stmhal/rtc.c @@ -28,8 +28,8 @@ #include "stm32f4xx_hal.h" -#include "misc.h" #include "mpconfig.h" +#include "misc.h" #include "qstr.h" #include "obj.h" #include "runtime.h" diff --git a/stmhal/sdcard.c b/stmhal/sdcard.c index bd45af3a14..204dbe3b46 100644 --- a/stmhal/sdcard.c +++ b/stmhal/sdcard.c @@ -28,8 +28,8 @@ #include <stm32f4xx_hal.h> -#include "misc.h" #include "mpconfig.h" +#include "misc.h" #include "qstr.h" #include "obj.h" #include "runtime.h" diff --git a/stmhal/spi.c b/stmhal/spi.c index 10ecf7ec8e..448b8696ea 100644 --- a/stmhal/spi.c +++ b/stmhal/spi.c @@ -188,7 +188,7 @@ STATIC const pyb_spi_obj_t pyb_spi_obj[] = { {{&pyb_spi_type}, NULL}, #endif }; -#define PYB_NUM_SPI ARRAY_SIZE(pyb_spi_obj) +#define PYB_NUM_SPI MP_ARRAY_SIZE(pyb_spi_obj) STATIC void pyb_spi_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { pyb_spi_obj_t *self = self_in; @@ -242,7 +242,7 @@ STATIC const mp_arg_t pyb_spi_init_args[] = { { MP_QSTR_ti, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} }, { MP_QSTR_crc, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, }; -#define PYB_SPI_INIT_NUM_ARGS ARRAY_SIZE(pyb_spi_init_args) +#define PYB_SPI_INIT_NUM_ARGS MP_ARRAY_SIZE(pyb_spi_init_args) STATIC mp_obj_t pyb_spi_init_helper(const pyb_spi_obj_t *self, uint n_args, const mp_obj_t *args, mp_map_t *kw_args) { // parse args @@ -359,7 +359,7 @@ STATIC const mp_arg_t pyb_spi_send_args[] = { { MP_QSTR_send, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 5000} }, }; -#define PYB_SPI_SEND_NUM_ARGS ARRAY_SIZE(pyb_spi_send_args) +#define PYB_SPI_SEND_NUM_ARGS MP_ARRAY_SIZE(pyb_spi_send_args) STATIC mp_obj_t pyb_spi_send(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) { // TODO assumes transmission size is 8-bits wide @@ -401,7 +401,7 @@ STATIC const mp_arg_t pyb_spi_recv_args[] = { { MP_QSTR_recv, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 5000} }, }; -#define PYB_SPI_RECV_NUM_ARGS ARRAY_SIZE(pyb_spi_recv_args) +#define PYB_SPI_RECV_NUM_ARGS MP_ARRAY_SIZE(pyb_spi_recv_args) STATIC mp_obj_t pyb_spi_recv(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) { // TODO assumes transmission size is 8-bits wide @@ -449,7 +449,7 @@ STATIC const mp_arg_t pyb_spi_send_recv_args[] = { { MP_QSTR_recv, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 5000} }, }; -#define PYB_SPI_SEND_RECV_NUM_ARGS ARRAY_SIZE(pyb_spi_send_recv_args) +#define PYB_SPI_SEND_RECV_NUM_ARGS MP_ARRAY_SIZE(pyb_spi_send_recv_args) STATIC mp_obj_t pyb_spi_send_recv(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) { // TODO assumes transmission size is 8-bits wide diff --git a/stmhal/stm32f4xx_hal_msp.c b/stmhal/stm32f4xx_hal_msp.c index 5816249e25..90baa41f82 100644 --- a/stmhal/stm32f4xx_hal_msp.c +++ b/stmhal/stm32f4xx_hal_msp.c @@ -66,8 +66,8 @@ #include "stm32f4xx_hal.h" -#include "misc.h" #include "mpconfig.h" +#include "misc.h" #include "qstr.h" #include "obj.h" #include "servo.h" diff --git a/stmhal/stm32f4xx_it.c b/stmhal/stm32f4xx_it.c index 17cdaf5fa2..5fa7f8289d 100644 --- a/stmhal/stm32f4xx_it.c +++ b/stmhal/stm32f4xx_it.c @@ -70,8 +70,8 @@ #include "stm32f4xx_it.h" #include "stm32f4xx_hal.h" -#include "misc.h" #include "mpconfig.h" +#include "misc.h" #include "qstr.h" #include "obj.h" #include "extint.h" diff --git a/stmhal/storage.c b/stmhal/storage.c index ba7e303e21..001d87afe0 100644 --- a/stmhal/storage.c +++ b/stmhal/storage.c @@ -28,9 +28,9 @@ #include <string.h> #include <stm32f4xx_hal.h> +#include "mpconfig.h" #include "misc.h" #include "systick.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "led.h" diff --git a/stmhal/systick.c b/stmhal/systick.c index 8a8d6403dc..196f1fbcae 100644 --- a/stmhal/systick.c +++ b/stmhal/systick.c @@ -25,6 +25,7 @@ */ #include <stm32f4xx_hal.h> +#include "mpconfig.h" #include "misc.h" #include "systick.h" diff --git a/stmhal/timer.c b/stmhal/timer.c index 0ba24754b2..ec0c4dec4c 100644 --- a/stmhal/timer.c +++ b/stmhal/timer.c @@ -105,7 +105,7 @@ static uint32_t tim3_counter = 0; // Used to do callbacks to Python code on interrupt STATIC pyb_timer_obj_t *pyb_timer_obj_all[14]; -#define PYB_TIMER_OBJ_ALL_NUM ARRAY_SIZE(pyb_timer_obj_all) +#define PYB_TIMER_OBJ_ALL_NUM MP_ARRAY_SIZE(pyb_timer_obj_all) void timer_init0(void) { tim3_counter = 0; @@ -234,7 +234,7 @@ STATIC const mp_arg_t pyb_timer_init_args[] = { { MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = TIM_COUNTERMODE_UP} }, { MP_QSTR_div, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = TIM_CLOCKDIVISION_DIV1} }, }; -#define PYB_TIMER_INIT_NUM_ARGS ARRAY_SIZE(pyb_timer_init_args) +#define PYB_TIMER_INIT_NUM_ARGS MP_ARRAY_SIZE(pyb_timer_init_args) STATIC mp_obj_t pyb_timer_init_helper(pyb_timer_obj_t *self, uint n_args, const mp_obj_t *args, mp_map_t *kw_args) { // parse args diff --git a/stmhal/uart.c b/stmhal/uart.c index 5bbd9f299e..a85f7f9e3a 100644 --- a/stmhal/uart.c +++ b/stmhal/uart.c @@ -270,7 +270,7 @@ STATIC const mp_arg_t pyb_uart_init_args[] = { { MP_QSTR_stop, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} }, { MP_QSTR_parity, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, }; -#define PYB_UART_INIT_NUM_ARGS ARRAY_SIZE(pyb_uart_init_args) +#define PYB_UART_INIT_NUM_ARGS MP_ARRAY_SIZE(pyb_uart_init_args) STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, uint n_args, const mp_obj_t *args, mp_map_t *kw_args) { // parse args @@ -396,7 +396,7 @@ STATIC const mp_arg_t pyb_uart_send_args[] = { { MP_QSTR_send, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 5000} }, }; -#define PYB_UART_SEND_NUM_ARGS ARRAY_SIZE(pyb_uart_send_args) +#define PYB_UART_SEND_NUM_ARGS MP_ARRAY_SIZE(pyb_uart_send_args) STATIC mp_obj_t pyb_uart_send(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) { // TODO assumes transmission size is 8-bits wide @@ -438,7 +438,7 @@ STATIC const mp_arg_t pyb_uart_recv_args[] = { { MP_QSTR_recv, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 5000} }, }; -#define PYB_UART_RECV_NUM_ARGS ARRAY_SIZE(pyb_uart_recv_args) +#define PYB_UART_RECV_NUM_ARGS MP_ARRAY_SIZE(pyb_uart_recv_args) STATIC mp_obj_t pyb_uart_recv(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) { // TODO assumes transmission size is 8-bits wide diff --git a/stmhal/usrsw.c b/stmhal/usrsw.c index 4ed9e3abe4..8a082eac95 100644 --- a/stmhal/usrsw.c +++ b/stmhal/usrsw.c @@ -28,8 +28,8 @@ #include "stm32f4xx_hal.h" -#include "misc.h" #include "mpconfig.h" +#include "misc.h" #include "qstr.h" #include "obj.h" #include "runtime.h" |