2014-07-02 08:42:37 -04:00
|
|
|
// We use the ST Cube HAL library for most hardware peripherals
|
2015-07-28 11:36:26 -04:00
|
|
|
#include STM32_HAL_H
|
2016-02-11 00:20:14 -05:00
|
|
|
#include "pin.h"
|
2014-07-02 08:42:37 -04:00
|
|
|
|
2023-03-22 01:38:49 -04:00
|
|
|
extern uint8_t mp_hal_unique_id_address[12];
|
|
|
|
|
2022-02-12 15:36:58 -05:00
|
|
|
// F0-1.9.0+F4-1.16.0+F7-1.7.0+G0-1.5.1+G4-1.3.0+H7-1.6.0+L0-1.11.2+L4-1.17.0+WB-1.10.0+WL-1.1.0
|
2021-09-10 08:28:41 -04:00
|
|
|
#if defined(STM32F0)
|
2021-09-27 05:17:15 -04:00
|
|
|
#define MICROPY_PLATFORM_VERSION "HAL1.9.0"
|
2021-09-10 08:28:41 -04:00
|
|
|
#elif defined(STM32F4)
|
2021-09-27 05:17:15 -04:00
|
|
|
#define MICROPY_PLATFORM_VERSION "HAL1.16.0"
|
2021-09-10 08:28:41 -04:00
|
|
|
#elif defined(STM32F7)
|
2021-09-27 05:17:15 -04:00
|
|
|
#define MICROPY_PLATFORM_VERSION "HAL1.7.0"
|
2022-02-12 15:36:58 -05:00
|
|
|
#elif defined(STM32G0)
|
|
|
|
#define MICROPY_PLATFORM_VERSION "HAL1.5.1"
|
2021-12-13 08:00:35 -05:00
|
|
|
#elif defined(STM32G4)
|
|
|
|
#define MICROPY_PLATFORM_VERSION "HAL1.3.0"
|
2023-03-22 01:38:49 -04:00
|
|
|
#elif defined(STM32H5)
|
|
|
|
#define MICROPY_PLATFORM_VERSION "HAL1.0.0"
|
2021-09-10 08:28:41 -04:00
|
|
|
#elif defined(STM32H7)
|
2022-12-20 19:53:09 -05:00
|
|
|
#define MICROPY_PLATFORM_VERSION "HAL1.11.0"
|
2021-09-10 08:28:41 -04:00
|
|
|
#elif defined(STM32L0)
|
2021-09-27 05:17:15 -04:00
|
|
|
#define MICROPY_PLATFORM_VERSION "HAL1.11.2"
|
2022-09-19 04:56:31 -04:00
|
|
|
#elif defined(STM32L1)
|
|
|
|
#define MICROPY_PLATFORM_VERSION "HAL1.10.3"
|
2021-09-10 08:28:41 -04:00
|
|
|
#elif defined(STM32L4)
|
2021-12-13 08:00:35 -05:00
|
|
|
#define MICROPY_PLATFORM_VERSION "HAL1.17.0"
|
2021-09-10 08:28:41 -04:00
|
|
|
#elif defined(STM32WB)
|
2021-09-27 05:17:15 -04:00
|
|
|
#define MICROPY_PLATFORM_VERSION "HAL1.10.0"
|
2021-12-13 08:00:35 -05:00
|
|
|
#elif defined(STM32WL)
|
|
|
|
#define MICROPY_PLATFORM_VERSION "HAL1.1.0"
|
2021-09-10 08:28:41 -04:00
|
|
|
#endif
|
|
|
|
|
2015-10-30 19:03:58 -04:00
|
|
|
extern const unsigned char mp_hal_status_to_errno_table[4];
|
2014-10-23 09:25:32 -04:00
|
|
|
|
2020-06-17 23:35:53 -04:00
|
|
|
static inline int mp_hal_status_to_neg_errno(HAL_StatusTypeDef status) {
|
|
|
|
return -mp_hal_status_to_errno_table[status];
|
|
|
|
}
|
|
|
|
|
2014-10-23 09:25:32 -04:00
|
|
|
NORETURN void mp_hal_raise(HAL_StatusTypeDef status);
|
2014-11-27 11:58:31 -05:00
|
|
|
void mp_hal_set_interrupt_char(int c); // -1 to disable
|
2015-02-13 10:04:53 -05:00
|
|
|
|
2016-10-05 21:12:20 -04:00
|
|
|
// timing functions
|
2016-10-17 23:30:00 -04:00
|
|
|
|
2017-09-06 00:09:13 -04:00
|
|
|
#include "irq.h"
|
2017-06-22 02:18:42 -04:00
|
|
|
|
2018-05-27 21:23:33 -04:00
|
|
|
#if __CORTEX_M == 0
|
|
|
|
// Don't have raise_irq_pri on Cortex-M0 so keep IRQs enabled to have SysTick timing
|
|
|
|
#define mp_hal_quiet_timing_enter() (1)
|
|
|
|
#define mp_hal_quiet_timing_exit(irq_state) (void)(irq_state)
|
|
|
|
#else
|
2017-06-22 02:18:42 -04:00
|
|
|
#define mp_hal_quiet_timing_enter() raise_irq_pri(1)
|
|
|
|
#define mp_hal_quiet_timing_exit(irq_state) restore_irq_pri(irq_state)
|
2018-05-27 21:23:33 -04:00
|
|
|
#endif
|
2017-03-01 23:32:32 -05:00
|
|
|
#define mp_hal_delay_us_fast(us) mp_hal_delay_us(us)
|
2016-04-12 08:51:39 -04:00
|
|
|
|
2016-10-17 23:30:00 -04:00
|
|
|
void mp_hal_ticks_cpu_enable(void);
|
|
|
|
static inline mp_uint_t mp_hal_ticks_cpu(void) {
|
2018-05-27 21:23:33 -04:00
|
|
|
#if __CORTEX_M == 0
|
|
|
|
return 0;
|
|
|
|
#else
|
2018-03-29 01:23:52 -04:00
|
|
|
if (!(DWT->CTRL & DWT_CTRL_CYCCNTENA_Msk)) {
|
2016-10-17 23:30:00 -04:00
|
|
|
mp_hal_ticks_cpu_enable();
|
|
|
|
}
|
|
|
|
return DWT->CYCCNT;
|
2018-05-27 21:23:33 -04:00
|
|
|
#endif
|
2016-10-17 23:30:00 -04:00
|
|
|
}
|
|
|
|
|
2016-04-12 08:50:47 -04:00
|
|
|
// C-level pin HAL
|
2016-10-17 23:32:42 -04:00
|
|
|
|
2017-09-06 00:09:13 -04:00
|
|
|
#include "pin.h"
|
2016-10-17 23:32:42 -04:00
|
|
|
|
2016-12-07 21:47:01 -05:00
|
|
|
#define MP_HAL_PIN_FMT "%q"
|
2016-11-11 01:53:45 -05:00
|
|
|
#define MP_HAL_PIN_MODE_INPUT (0)
|
|
|
|
#define MP_HAL_PIN_MODE_OUTPUT (1)
|
|
|
|
#define MP_HAL_PIN_MODE_ALT (2)
|
|
|
|
#define MP_HAL_PIN_MODE_ANALOG (3)
|
2018-05-01 22:05:45 -04:00
|
|
|
#if defined(GPIO_ASCR_ASC0)
|
|
|
|
#define MP_HAL_PIN_MODE_ADC (11)
|
|
|
|
#else
|
|
|
|
#define MP_HAL_PIN_MODE_ADC (3)
|
|
|
|
#endif
|
2016-11-11 01:53:45 -05:00
|
|
|
#define MP_HAL_PIN_MODE_OPEN_DRAIN (5)
|
|
|
|
#define MP_HAL_PIN_MODE_ALT_OPEN_DRAIN (6)
|
2016-12-21 22:51:07 -05:00
|
|
|
#define MP_HAL_PIN_PULL_NONE (GPIO_NOPULL)
|
|
|
|
#define MP_HAL_PIN_PULL_UP (GPIO_PULLUP)
|
|
|
|
#define MP_HAL_PIN_PULL_DOWN (GPIO_PULLDOWN)
|
2019-02-18 19:11:41 -05:00
|
|
|
#define MP_HAL_PIN_SPEED_LOW (GPIO_SPEED_FREQ_LOW)
|
|
|
|
#define MP_HAL_PIN_SPEED_MEDIUM (GPIO_SPEED_FREQ_MEDIUM)
|
|
|
|
#define MP_HAL_PIN_SPEED_HIGH (GPIO_SPEED_FREQ_HIGH)
|
|
|
|
#define MP_HAL_PIN_SPEED_VERY_HIGH (GPIO_SPEED_FREQ_VERY_HIGH)
|
2016-11-11 01:53:45 -05:00
|
|
|
|
2016-10-05 00:51:40 -04:00
|
|
|
#define mp_hal_pin_obj_t const pin_obj_t *
|
2016-10-17 23:32:42 -04:00
|
|
|
#define mp_hal_get_pin_obj(o) pin_find(o)
|
2016-12-07 21:47:01 -05:00
|
|
|
#define mp_hal_pin_name(p) ((p)->name)
|
2016-11-11 01:53:45 -05:00
|
|
|
#define mp_hal_pin_input(p) mp_hal_pin_config((p), MP_HAL_PIN_MODE_INPUT, MP_HAL_PIN_PULL_NONE, 0)
|
|
|
|
#define mp_hal_pin_output(p) mp_hal_pin_config((p), MP_HAL_PIN_MODE_OUTPUT, MP_HAL_PIN_PULL_NONE, 0)
|
|
|
|
#define mp_hal_pin_open_drain(p) mp_hal_pin_config((p), MP_HAL_PIN_MODE_OPEN_DRAIN, MP_HAL_PIN_PULL_NONE, 0)
|
2016-10-17 23:32:42 -04:00
|
|
|
#define mp_hal_pin_high(p) (((p)->gpio->BSRR) = (p)->pin_mask)
|
|
|
|
#define mp_hal_pin_low(p) (((p)->gpio->BSRR) = ((p)->pin_mask << 16))
|
|
|
|
#define mp_hal_pin_od_low(p) mp_hal_pin_low(p)
|
|
|
|
#define mp_hal_pin_od_high(p) mp_hal_pin_high(p)
|
|
|
|
#define mp_hal_pin_read(p) (((p)->gpio->IDR >> (p)->pin) & 1)
|
2020-01-07 19:38:20 -05:00
|
|
|
#define mp_hal_pin_write(p, v) ((v) ? mp_hal_pin_high(p) : mp_hal_pin_low(p))
|
2022-08-04 14:19:57 -04:00
|
|
|
#define mp_hal_pin_interrupt(pin, handler, trigger, hard) extint_register_pin(pin, trigger, hard, handler)
|
|
|
|
|
|
|
|
enum mp_hal_pin_interrupt_trigger {
|
|
|
|
MP_HAL_PIN_TRIGGER_NONE,
|
|
|
|
MP_HAL_PIN_TRIGGER_FALL = GPIO_MODE_IT_FALLING,
|
|
|
|
MP_HAL_PIN_TRIGGER_RISE = GPIO_MODE_IT_RISING,
|
|
|
|
};
|
2016-10-17 23:32:42 -04:00
|
|
|
|
|
|
|
void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio);
|
|
|
|
void mp_hal_pin_config(mp_hal_pin_obj_t pin, uint32_t mode, uint32_t pull, uint32_t alt);
|
2016-11-11 01:53:45 -05:00
|
|
|
bool mp_hal_pin_config_alt(mp_hal_pin_obj_t pin, uint32_t mode, uint32_t pull, uint8_t fn, uint8_t unit);
|
2018-05-01 03:31:23 -04:00
|
|
|
void mp_hal_pin_config_speed(mp_hal_pin_obj_t pin_obj, uint32_t speed);
|
2022-08-04 14:19:57 -04:00
|
|
|
void extint_register_pin(const pin_obj_t *pin, uint32_t mode, bool hard_irq, mp_obj_t callback_obj);
|
2019-02-22 05:53:41 -05:00
|
|
|
|
2022-08-10 05:04:09 -04:00
|
|
|
mp_obj_base_t *mp_hal_get_spi_obj(mp_obj_t spi_in);
|
|
|
|
|
2019-02-22 05:53:41 -05:00
|
|
|
enum {
|
|
|
|
MP_HAL_MAC_WLAN0 = 0,
|
|
|
|
MP_HAL_MAC_WLAN1,
|
|
|
|
MP_HAL_MAC_BDADDR,
|
|
|
|
MP_HAL_MAC_ETH0,
|
|
|
|
};
|
|
|
|
|
2019-08-21 09:06:56 -04:00
|
|
|
void mp_hal_generate_laa_mac(int idx, uint8_t buf[6]);
|
2019-02-22 05:53:41 -05:00
|
|
|
void mp_hal_get_mac(int idx, uint8_t buf[6]);
|
2019-06-01 02:08:40 -04:00
|
|
|
void mp_hal_get_mac_ascii(int idx, size_t chr_off, size_t chr_len, char *dest);
|