2014-07-02 13:42:37 +01:00
|
|
|
// We use the ST Cube HAL library for most hardware peripherals
|
2015-07-28 16:36:26 +01:00
|
|
|
#include STM32_HAL_H
|
2016-02-11 05:20:14 +00:00
|
|
|
#include "pin.h"
|
2014-07-02 13:42:37 +01:00
|
|
|
|
2015-10-29 16:03:10 +00:00
|
|
|
// 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)
|
2016-03-22 16:21:05 +00:00
|
|
|
#elif defined(MCU_SERIES_L4)
|
|
|
|
#define MP_HAL_UNIQUE_ID_ADDRESS (0x1fff7590)
|
2015-10-29 16:03:10 +00:00
|
|
|
#else
|
2015-10-30 23:03:58 +00:00
|
|
|
#error mphalport.h: Unrecognized MCU_SERIES
|
2015-10-29 16:03:10 +00:00
|
|
|
#endif
|
|
|
|
|
2014-07-02 13:42:37 +01:00
|
|
|
// Basic GPIO functions
|
|
|
|
#define GPIO_read_pin(gpio, pin) (((gpio)->IDR >> (pin)) & 1)
|
2016-03-22 16:21:05 +00:00
|
|
|
#if defined(MCU_SERIES_F7) || defined(MCU_SERIES_L4)
|
2015-07-28 19:13:33 +01:00
|
|
|
#define GPIO_set_pin(gpio, pin_mask) (((gpio)->BSRR) = (pin_mask))
|
|
|
|
#define GPIO_clear_pin(gpio, pin_mask) (((gpio)->BSRR) = ((pin_mask) << 16))
|
|
|
|
#else
|
2014-07-02 13:42:37 +01:00
|
|
|
#define GPIO_set_pin(gpio, pin_mask) (((gpio)->BSRRL) = (pin_mask))
|
|
|
|
#define GPIO_clear_pin(gpio, pin_mask) (((gpio)->BSRRH) = (pin_mask))
|
2015-07-28 19:13:33 +01:00
|
|
|
#endif
|
2014-07-22 15:57:36 +01:00
|
|
|
#define GPIO_read_output_pin(gpio, pin) (((gpio)->ODR >> (pin)) & 1)
|
2014-10-23 14:25:32 +01:00
|
|
|
|
2015-08-03 00:05:16 +01:00
|
|
|
void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio);
|
2016-04-12 13:50:47 +01:00
|
|
|
void mp_hal_gpio_config(GPIO_TypeDef *gpio, uint32_t pin, uint32_t mode, uint32_t pull, uint32_t alt);
|
2016-02-11 05:20:14 +00:00
|
|
|
bool mp_hal_gpio_set_af(const pin_obj_t *pin, GPIO_InitTypeDef *init, uint8_t fn, uint8_t unit);
|
2015-08-03 00:05:16 +01:00
|
|
|
|
2015-10-30 23:03:58 +00:00
|
|
|
extern const unsigned char mp_hal_status_to_errno_table[4];
|
2014-10-23 14:25:32 +01:00
|
|
|
|
|
|
|
NORETURN void mp_hal_raise(HAL_StatusTypeDef status);
|
2014-11-27 16:58:31 +00:00
|
|
|
void mp_hal_set_interrupt_char(int c); // -1 to disable
|
2015-02-13 15:04:53 +00:00
|
|
|
|
2016-10-06 02:12:20 +01:00
|
|
|
// timing functions
|
2016-04-12 13:51:39 +01:00
|
|
|
#include "stmhal/systick.h"
|
2016-10-06 02:12:20 +01:00
|
|
|
#define mp_hal_delay_ms HAL_Delay
|
2016-10-14 18:42:42 +01:00
|
|
|
#define mp_hal_delay_us(us) sys_tick_udelay(us)
|
2016-04-12 13:51:39 +01:00
|
|
|
#define mp_hal_delay_us_fast(us) sys_tick_udelay(us)
|
2016-10-06 02:12:20 +01:00
|
|
|
#define mp_hal_ticks_ms HAL_GetTick
|
|
|
|
#define mp_hal_ticks_us() sys_tick_get_microseconds()
|
2016-04-12 13:51:39 +01:00
|
|
|
|
2016-04-12 13:50:47 +01:00
|
|
|
// C-level pin HAL
|
|
|
|
#include "stmhal/pin.h"
|
2016-10-05 05:51:40 +01:00
|
|
|
#define mp_hal_pin_obj_t const pin_obj_t*
|
|
|
|
#define mp_hal_get_pin_obj(o) pin_find(o)
|
2016-10-03 06:44:34 +01:00
|
|
|
#define mp_hal_pin_input(p) mp_hal_gpio_config((p)->gpio, (p)->pin, 0, 0, 0)
|
|
|
|
#define mp_hal_pin_output(p) mp_hal_gpio_config((p)->gpio, (p)->pin, 1, 0, 0)
|
2016-05-26 17:06:40 +01:00
|
|
|
#define mp_hal_pin_open_drain(p) mp_hal_gpio_config((p)->gpio, (p)->pin, 5, 0, 0)
|
|
|
|
#define mp_hal_pin_od_low(p) GPIO_clear_pin((p)->gpio, (p)->pin_mask)
|
2016-04-12 13:50:47 +01:00
|
|
|
#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)
|
2016-10-03 08:06:58 +01:00
|
|
|
#define mp_hal_pin_write(p, v) do { if (v) { GPIO_set_pin((p)->gpio, (p)->pin_mask); } else { GPIO_clear_pin((p)->gpio, (p)->pin_mask); } } while (0)
|