mphalport.h 1.8 KB
Newer Older
1
// We use the ST Cube HAL library for most hardware peripherals
2
#include STM32_HAL_H
3
#include "pin.h"
4

5
6
7
8
9
10
11
// The unique id address differs per MCU.  Ideally this define should
// go in some MCU-specific header, but for now it lives here.
#if defined(MCU_SERIES_F4)
#define MP_HAL_UNIQUE_ID_ADDRESS (0x1fff7a10)
#elif defined(MCU_SERIES_F7)
#define MP_HAL_UNIQUE_ID_ADDRESS (0x1ff0f420)
#else
12
#error mphalport.h: Unrecognized MCU_SERIES
13
14
#endif

15
16
// Basic GPIO functions
#define GPIO_read_pin(gpio, pin)        (((gpio)->IDR >> (pin)) & 1)
17
#if defined(MCU_SERIES_F7)
18
19
20
#define GPIO_set_pin(gpio, pin_mask)    (((gpio)->BSRR) = (pin_mask))
#define GPIO_clear_pin(gpio, pin_mask)  (((gpio)->BSRR) = ((pin_mask) << 16))
#else
21
22
#define GPIO_set_pin(gpio, pin_mask)    (((gpio)->BSRRL) = (pin_mask))
#define GPIO_clear_pin(gpio, pin_mask)  (((gpio)->BSRRH) = (pin_mask))
23
#endif
24
#define GPIO_read_output_pin(gpio, pin) (((gpio)->ODR >> (pin)) & 1)
25

26
void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio);
27
void mp_hal_gpio_config(GPIO_TypeDef *gpio, uint32_t pin, uint32_t mode, uint32_t pull, uint32_t alt);
28
bool mp_hal_gpio_set_af(const pin_obj_t *pin, GPIO_InitTypeDef *init, uint8_t fn, uint8_t unit);
29

30
extern const unsigned char mp_hal_status_to_errno_table[4];
31
32

NORETURN void mp_hal_raise(HAL_StatusTypeDef status);
33
void mp_hal_set_interrupt_char(int c); // -1 to disable
34

35
36
#define mp_hal_delay_ms HAL_Delay
#define mp_hal_ticks_ms HAL_GetTick
37
38
39
40
41
42
43
44
45

// C-level pin HAL
#include "stmhal/pin.h"
#define mp_hal_pin_obj_t pin_obj_t
#define mp_hal_get_pin_obj(o) (pin_obj_t*)pin_find(o)
#define mp_hal_pin_config_od(p) mp_hal_gpio_config((p)->gpio, (p)->pin, 5, 0, 0)
#define mp_hal_pin_low(p) GPIO_clear_pin((p)->gpio, (p)->pin_mask)
#define mp_hal_pin_od_high(p) GPIO_set_pin((p)->gpio, (p)->pin_mask)
#define mp_hal_pin_read(p) GPIO_read_pin((p)->gpio, (p)->pin)