Commit ca5444e6 authored by Dave Hylands's avatar Dave Hylands
Browse files

stmhal - add pin mapping, gpio, exti, usrsw

parent 0a64c92a
......@@ -45,6 +45,10 @@ vpath %.c . $(TOP)
$(BUILD)/%.o: %.c
$(call compile_c)
$(BUILD)/%.pp: %.c
$(ECHO) "PreProcess $<"
$(Q)$(CC) $(CFLAGS) -E -Wp,-C,-dD,-dI -o $@ $<
# The following rule uses | to create an order only prereuisite. Order only
# prerequisites only get built if they don't exist. They don't cause timestamp
# checkng to be performed.
......
......@@ -62,6 +62,8 @@ SRC_C = \
systick.c \
led.c \
pin.c \
pin_map.c \
pin_named_pins.c \
usart.c \
usb.c \
printf.c \
......@@ -72,8 +74,10 @@ SRC_C = \
pybmodule.c \
import.c \
lexerfatfs.c \
gpio.c \
exti.c \
usrsw.c \
# gpio.c \
# lcd.c \
# servo.c \
# flash.c \
......@@ -86,10 +90,6 @@ SRC_C = \
# adc.c \
# rtc.c \
# file.c \
# pin_named_pins.c \
# pin_map.c \
# exti.c \
# usrsw.c \
# pybwlan.c \
SRC_S = \
......
......@@ -20,8 +20,8 @@
// USRSW is pulled low. Pressing the button makes the input go high.
#define USRSW_PIN (pin_B11)
#define USRSW_PUPD (GPIO_PuPd_NOPULL)
#define USRSW_EXTI_EDGE (EXTI_Trigger_Rising)
#define USRSW_PULL (GPIO_NOPULL)
#define USRSW_EXTI_MODE (GPIO_MODE_IT_RISING)
#define USRSW_PRESSED (1)
/* LED */
......
......@@ -16,8 +16,8 @@
// USRSW has no pullup or pulldown, and pressing the switch makes the input go low
#define USRSW_PIN (pin_A13)
#define USRSW_PUPD (GPIO_PuPd_UP)
#define USRSW_EXTI_EDGE (EXTI_Trigger_Falling)
#define USRSW_PULL (GPIO_PULLUP)
#define USRSW_EXTI_MODE (GPIO_MODE_IT_FALLING)
#define USRSW_PRESSED (0)
/* LED */
......
......@@ -16,8 +16,8 @@
// USRSW has no pullup or pulldown, and pressing the switch makes the input go low
#define USRSW_PIN (pin_B3)
#define USRSW_PUPD (GPIO_PuPd_UP)
#define USRSW_EXTI_EDGE (EXTI_Trigger_Falling)
#define USRSW_PULL (GPIO_PULLUP)
#define USRSW_EXTI_MODE (GPIO_MODE_IT_FALLING)
#define USRSW_PRESSED (0)
/* LED */
......
......@@ -16,8 +16,8 @@
// USRSW is pulled low. Pressing the button makes the input go high.
#define USRSW_PIN (pin_A0)
#define USRSW_PUPD (GPIO_PuPd_NOPULL)
#define USRSW_EXTI_EDGE (EXTI_Trigger_Rising)
#define USRSW_PULL (GPIO_NOPULL)
#define USRSW_EXTI_MODE (GPIO_MODE_IT_RISING)
#define USRSW_PRESSED (1)
/* LED */
......
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <stm32f4xx_hal.h>
#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
#include "nlr.h"
#include "pin.h"
#include "exti.h"
// Usage Model:
//
// There are a total of 22 interrupt lines. 16 of these can come from GPIO pins
// and the remaining 6 are from internal sources.
//
// For lines 0 thru 15, a given line can map to the corresponding line from an
// arbitrary port. So line 0 can map to Px0 where x is A, B, C, ... and
// line 1 can map to Px1 where x is A, B, C, ...
//
// def callback(line):
// print("line =", line)
//
// # Configure the pin as a GPIO input.
// pin = pyb.Pin.board.X1
// pyb.gpio_in(pin, pyb.PULL_UP)
// exti = pyb.Exti(pin, pyb.Exti.MODE_IRQ_FALLING, pyb.PULLUP, callback)
//
// Now every time a falling edge is seen on the X1 pin, the callback will be
// called. Caution: mechanical pushbuttons have "bounce" and pushing or
// releasing a switch will often generate multiple edges.
// See: http://www.eng.utah.edu/~cs5780/debouncing.pdf for a detailed
// explanation, along with various techniques for debouncing.
//
// Trying to register 2 callbacks onto the same pin will throw an exception.
//
// If pin is passed as an integer, then it is assumed to map to one of the
// internal interrupt sources, and must be in the range 16 thru 22.
//
// All other pin objects go through the pin mapper to come up with one of the
// gpio pins.
//
// exti = pyb.Exti(pin, mode, pull, callback)
//
// Valid modes are pyb.Exti.MODE_IRQ_RISING, pyb.Exti.MODE_IRQ_FALLING,
// pyb.Exti.MODE_IRQ_RISING_FALLING, pyb.Exti.MODE_EVT_RISING,
// pyb.Exti.MODE_EVT_FALLING, and pyb.Exti.MODE_EVT_RISING_FALLING.
//
// Only the MODE_IRQ_xxx modes have been tested. The MODE_EVENT_xxx modes have
// something to do with sleep mode and he WFE instruction.
//
// Valid pull values are pyb.PULL_UP, pyb.PULL_DOWN, pyb.PULL_NONE.
//
// exti.line() will return the line number that pin was mapped to.
// exti.disable() can be use to disable the interrupt associated with a given
// exti object. This could be useful for debouncing.
// exti.enable() enables a disabled interrupt
// exti.swint() will allow the callback to be triggered from software.
//
// pyb.Exti.regs() will dump the values of the EXTI registers.
//
// There is also a C API, so that drivers which require EXTI interrupt lines
// can also use this code. See exti.h for the available functions and
// usrsw.h for an example of using this.
#define EXTI_OFFSET (EXTI_BASE - PERIPH_BASE)
// Macro used to set/clear the bit corresponding to the line in the IMR/EMR
// register in an atomic fashion by using bitband addressing.
#define EXTI_MODE_BB(mode, line) (*(__IO uint32_t *)(PERIPH_BB_BASE + ((EXTI_OFFSET + (mode)) * 32) + ((line) * 4)))
#define EXTI_Mode_Interrupt offsetof(EXTI_TypeDef, IMR)
#define EXTI_Mode_Event offsetof(EXTI_TypeDef, EMR)
#define EXTI_SWIER_BB(line) (*(__IO uint32_t *)(PERIPH_BB_BASE + ((EXTI_OFFSET + offsetof(EXTI_TypeDef, SWIER)) * 32) + ((line) * 4)))
typedef struct {
mp_obj_base_t base;
mp_small_int_t line;
} exti_obj_t;
typedef struct {
mp_obj_t callback_obj;
void *param;
uint32_t mode;
} exti_vector_t;
static exti_vector_t exti_vector[EXTI_NUM_VECTORS];
#if !defined(ETH)
#define ETH_WKUP_IRQn 62 // The 405 doesn't have ETH, but we want a value to put in our table
#endif
static const uint8_t nvic_irq_channel[EXTI_NUM_VECTORS] = {
EXTI0_IRQn, EXTI1_IRQn, EXTI2_IRQn, EXTI3_IRQn, EXTI4_IRQn,
EXTI9_5_IRQn, EXTI9_5_IRQn, EXTI9_5_IRQn, EXTI9_5_IRQn, EXTI9_5_IRQn,
EXTI15_10_IRQn, EXTI15_10_IRQn, EXTI15_10_IRQn, EXTI15_10_IRQn, EXTI15_10_IRQn,
EXTI15_10_IRQn, PVD_IRQn, RTC_Alarm_IRQn, OTG_FS_WKUP_IRQn, ETH_WKUP_IRQn,
OTG_HS_WKUP_IRQn, TAMP_STAMP_IRQn, RTC_WKUP_IRQn
};
// NOTE: param is for C callers. Python can use closure to get an object bound
// with the function.
uint exti_register(mp_obj_t pin_obj, mp_obj_t mode_obj, mp_obj_t pull_obj, mp_obj_t callback_obj, void *param) {
const pin_obj_t *pin = NULL;
uint v_line;
if (MP_OBJ_IS_INT(pin_obj)) {
// If an integer is passed in, then use it to identify lines 16 thru 22
// We expect lines 0 thru 15 to be passed in as a pin, so that we can
// get both the port number and line number.
v_line = mp_obj_get_int(pin_obj);
if (v_line < 16) {
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "EXTI vector %d < 16, use a Pin object", v_line));
}
if (v_line >= EXTI_NUM_VECTORS) {
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "EXTI vector %d >= max of %d", v_line, EXTI_NUM_VECTORS));
}
} else {
pin = pin_map_user_obj(pin_obj);
v_line = pin->pin;
}
int mode = mp_obj_get_int(mode_obj);
if (mode != GPIO_MODE_IT_RISING &&
mode != GPIO_MODE_IT_FALLING &&
mode != GPIO_MODE_IT_RISING_FALLING &&
mode != GPIO_MODE_EVT_RISING &&
mode != GPIO_MODE_EVT_FALLING &&
mode != GPIO_MODE_EVT_RISING_FALLING) {
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Invalid EXTI Mode: %d", mode));
}
int pull = mp_obj_get_int(pull_obj);
if (pull != GPIO_NOPULL &&
pull != GPIO_PULLUP &&
pull != GPIO_PULLDOWN) {
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Invalid EXTI Pull: %d", pull));
}
exti_vector_t *v = &exti_vector[v_line];
if (v->callback_obj != mp_const_none && callback_obj != mp_const_none) {
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "EXTI vector %d is already in use", v_line));
}
// We need to update callback and param atomically, so we disable the line
// before we update anything.
exti_disable(v_line);
v->callback_obj = callback_obj;
v->param = param;
v->mode = (mode & 0x00010000) ? // GPIO_MODE_IT == 0x00010000
EXTI_Mode_Interrupt : EXTI_Mode_Event;
if (v->callback_obj != mp_const_none) {
GPIO_InitTypeDef exti;
exti.Pin = pin->pin_mask;
exti.Mode = mode;
exti.Pull = pull;
exti.Speed = GPIO_SPEED_FAST;
HAL_GPIO_Init(pin->gpio, &exti);
// Calling HAL_GPIO_Init does an implicit exti_enable
/* Enable and set NVIC Interrupt to the lowest priority */
HAL_NVIC_SetPriority(nvic_irq_channel[v_line], 0x0F, 0x0F);
HAL_NVIC_EnableIRQ(nvic_irq_channel[v_line]);
}
return v_line;
}
void exti_enable(uint line) {
if (line >= EXTI_NUM_VECTORS) {
return;
}
// Since manipulating IMR/EMR is a read-modify-write, and we want this to
// be atomic, we use the bit-band area to just affect the bit we're
// interested in.
EXTI_MODE_BB(exti_vector[line].mode, line) = 1;
}
void exti_disable(uint line) {
if (line >= EXTI_NUM_VECTORS) {
return;
}
// Since manipulating IMR/EMR is a read-modify-write, and we want this to
// be atomic, we use the bit-band area to just affect the bit we're
// interested in.
EXTI_MODE_BB(EXTI_Mode_Interrupt, line) = 0;
EXTI_MODE_BB(EXTI_Mode_Event, line) = 0;
}
void exti_swint(uint line) {
if (line >= EXTI_NUM_VECTORS) {
return;
}
EXTI->SWIER = (1 << line);
}
static mp_obj_t exti_obj_line(mp_obj_t self_in) {
exti_obj_t *self = self_in;
return MP_OBJ_NEW_SMALL_INT(self->line);
}
static mp_obj_t exti_obj_enable(mp_obj_t self_in) {
exti_obj_t *self = self_in;
exti_enable(self->line);
return mp_const_none;
}
static mp_obj_t exti_obj_disable(mp_obj_t self_in) {
exti_obj_t *self = self_in;
exti_disable(self->line);
return mp_const_none;
}
static mp_obj_t exti_obj_swint(mp_obj_t self_in) {
exti_obj_t *self = self_in;
exti_swint(self->line);
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(exti_obj_line_obj, exti_obj_line);
static MP_DEFINE_CONST_FUN_OBJ_1(exti_obj_enable_obj, exti_obj_enable);
static MP_DEFINE_CONST_FUN_OBJ_1(exti_obj_disable_obj, exti_obj_disable);
static MP_DEFINE_CONST_FUN_OBJ_1(exti_obj_swint_obj, exti_obj_swint);
static const mp_method_t exti_methods[] = {
{ "line", &exti_obj_line_obj },
{ "enable", &exti_obj_enable_obj },
{ "disable", &exti_obj_disable_obj },
{ "swint", &exti_obj_swint_obj },
{ NULL, NULL },
};
static mp_obj_t exti_regs(void) {
printf("EXTI_IMR %08lx\n", EXTI->IMR);
printf("EXTI_EMR %08lx\n", EXTI->EMR);
printf("EXTI_RTSR %08lx\n", EXTI->RTSR);
printf("EXTI_FTSR %08lx\n", EXTI->FTSR);
printf("EXTI_SWIER %08lx\n", EXTI->SWIER);
printf("EXTI_PR %08lx\n", EXTI->PR);
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_0(exti_regs_obj, exti_regs);
typedef struct {
const char *name;
uint val;
} exti_const_t;
static const exti_const_t exti_const[] = {
{ "MODE_IRQ_RISING", GPIO_MODE_IT_RISING },
{ "MODE_IRQ_FALLING", GPIO_MODE_IT_FALLING },
{ "MODE_IRQ_RISING_FALLING", GPIO_MODE_IT_RISING_FALLING },
{ "MODE_EVT_RISING", GPIO_MODE_EVT_RISING },
{ "MODE_EVT_FALLING", GPIO_MODE_EVT_FALLING },
{ "MODE_EVT_RISING_FALLING", GPIO_MODE_EVT_RISING_FALLING },
};
#define EXTI_NUM_CONST (sizeof(exti_const) / sizeof(exti_const[0]))
static void exti_load_attr(mp_obj_t self_in, qstr attr_qstr, mp_obj_t *dest) {
(void)self_in;
const char *attr = qstr_str(attr_qstr);
if (strcmp(attr, "regs") == 0) {
dest[0] = (mp_obj_t)&exti_regs_obj;
return;
}
const exti_const_t *entry = &exti_const[0];
for (; entry < &exti_const[EXTI_NUM_CONST]; entry++) {
if (strcmp(attr, entry->name) == 0) {
dest[0] = MP_OBJ_NEW_SMALL_INT(entry->val);
dest[1] = MP_OBJ_NULL;
return;
}
}
}
// line_obj = pyb.Exti(pin, mode, trigger, callback)
static mp_obj_t exti_call(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
// type_in == exti_obj_type
rt_check_nargs(n_args, 4, 4, n_kw, 0);
exti_obj_t *self = m_new_obj(exti_obj_t);
self->base.type = type_in;
mp_obj_t line_obj = args[0];
mp_obj_t mode_obj = args[1];
mp_obj_t trigger_obj = args[2];
mp_obj_t callback_obj = args[3];
self->line = exti_register(line_obj, mode_obj, trigger_obj, callback_obj, NULL);
return self;
}
static void exti_meta_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
(void) self_in;
print(env, "<Exti meta>");
}
static void exti_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
exti_obj_t *self = self_in;
print(env, "<Exti line=%u>", self->line);
}
static const mp_obj_type_t exti_meta_obj_type = {
{ &mp_type_type },
.name = MP_QSTR_ExtiMeta,
.print = exti_meta_obj_print,
.call = exti_call,
.load_attr = exti_load_attr,
};
const mp_obj_type_t exti_obj_type = {
{ &exti_meta_obj_type },
.name = MP_QSTR_Exti,
.print = exti_obj_print,
.methods = exti_methods,
};
void exti_init(void) {
for (exti_vector_t *v = exti_vector; v < &exti_vector[EXTI_NUM_VECTORS]; v++) {
v->callback_obj = mp_const_none;
v->param = NULL;
v->mode = EXTI_Mode_Interrupt;
}
}
void Handle_EXTI_Irq(uint32_t line) {
if (__HAL_GPIO_EXTI_GET_FLAG(1 << line)) {
__HAL_GPIO_EXTI_CLEAR_FLAG(1 << line);
if (line < EXTI_NUM_VECTORS) {
exti_vector_t *v = &exti_vector[line];
if (v->callback_obj != mp_const_none) {
rt_call_function_1(v->callback_obj, MP_OBJ_NEW_SMALL_INT(line));
}
}
}
}
// Vectors 0-15 are for regular pins
// Vectors 16-22 are for internal sources.
//
// Use the following constants for the internal sources:
#define EXTI_PVD_OUTPUT (16)
#define EXTI_RTC_ALARM (17)
#define EXTI_USB_OTG_FS_WAKEUP (18)
#define EXTI_ETH_WAKEUP (19)
#define EXTI_USB_OTG_HS_WAKEUP (20)
#define EXTI_RTC_TIMESTAMP (21)
#define EXTI_RTC_WAKEUP (22)
#define EXTI_NUM_VECTORS (23)
#define EXTI_MODE_INTERRUPT (offsetof(EXTI_TypeDef, IMR))
#define EXTI_MODE_EVENT (offsetof(EXTI_TypeDef, EMR))
#define EXTI_TRIGGER_RISING (offsetof(EXTI_TypeDef, RTSR))
#define EXTI_TRIGGER_FALLING (offsetof(EXTI_TypeDef, FTSR))
#define EXTI_TRIGGER_RISING_FALLING (EXTI_TRIGGER_RISING + EXTI_TRIGGER_FALLING) // just different from RISING or FALLING
void exti_init(void);
uint exti_register(mp_obj_t pin_obj, mp_obj_t mode_obj, mp_obj_t trigger_obj, mp_obj_t callback_obj, void *param);
void exti_enable(uint line);
void exti_disable(uint line);
void exti_swint(uint line);
void Handle_EXTI_Irq(uint32_t line);
typedef struct {
mp_obj_t callback;
void *param;
} exti_t;
extern const mp_obj_type_t exti_obj_type;
// This is a woefully inadequate set of bindings for GPIO control, and
// needs to be replaced with something much better.
#include <stdio.h>
#include <string.h>
#include <stm32f4xx_hal.h>
#include "nlr.h"
#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "misc.h"
#include "parse.h"
#include "obj.h"
#include "compile.h"
#include "runtime0.h"
#include "runtime.h"
#include "systick.h"
#include "gpio.h"
#include "pin.h"
mp_obj_t pyb_gpio(uint n_args, mp_obj_t *args) {
const pin_obj_t *pin = pin_map_user_obj(args[0]);
if (n_args == 1) {
// get pin
return MP_OBJ_NEW_SMALL_INT(HAL_GPIO_ReadPin(pin->gpio, pin->pin_mask));
}
// set pin
HAL_GPIO_WritePin(pin->gpio, pin->pin_mask, rt_is_true(args[1]));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_gpio_obj, 1, 2, pyb_gpio);
mp_obj_t pyb_gpio_input(uint n_args, mp_obj_t *args) {
const pin_obj_t *pin = pin_map_user_obj(args[0]);
uint32_t pull = GPIO_NOPULL;
if (n_args > 1) {
pull = mp_obj_get_int(args[1]);
}
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Pin = pin->pin_mask;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.Pull = pull;
HAL_GPIO_Init(pin->gpio, &GPIO_InitStructure);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_gpio_input_obj, 1, 2, pyb_gpio_input);
mp_obj_t pyb_gpio_output(uint n_args, mp_obj_t *args) {
const pin_obj_t *pin = pin_map_user_obj(args[0]);
uint32_t mode = GPIO_MODE_OUTPUT_PP;
if (n_args > 1) {
mode = mp_obj_get_int(args[1]) == GPIO_MODE_OUTPUT_OD ?
GPIO_MODE_OUTPUT_OD: GPIO_MODE_OUTPUT_PP;
}
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Pin = pin->pin_mask;
GPIO_InitStructure.Mode = mode;
GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
GPIO_InitStructure.Pull = GPIO_NOPULL;
HAL_GPIO_Init(pin->gpio, &GPIO_InitStructure);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_gpio_output_obj, 1, 2, pyb_gpio_output);
mp_obj_t pyb_gpio(uint n_args, mp_obj_t *args);
mp_obj_t pyb_gpio_output(uint n_args, mp_obj_t *args);
mp_obj_t pyb_gpio_input(uint n_args, mp_obj_t *args);
MP_DECLARE_CONST_FUN_OBJ(pyb_gpio_obj);
MP_DECLARE_CONST_FUN_OBJ(pyb_gpio_input_obj);
MP_DECLARE_CONST_FUN_OBJ(pyb_gpio_output_obj);
......@@ -38,6 +38,8 @@
#include "pyexec.h"
#include "pybmodule.h"
#include "led.h"
#include "exti.h"
#include "usrsw.h"
#include "usb.h"
#if 0
#include "ff.h"
......@@ -49,11 +51,9 @@
#include "accel.h"
#include "timer.h"
#include "pybwlan.h"
#include "usrsw.h"
#include "rtc.h"
#include "file.h"
#include "pin.h"
#include "exti.h"
#endif
void SystemClock_Config(void);
......@@ -309,13 +309,13 @@ soft_reset:
def_path[2] = MP_OBJ_NEW_QSTR(MP_QSTR_0_colon__slash_lib);
sys_path = mp_obj_new_list(3, def_path);
#if 0
exti_init();
#if MICROPY_HW_HAS_SWITCH
switch_init();
#endif
#if 0
#if MICROPY_HW_HAS_LCD
// LCD init (just creates class, init hardware by calling LCD())
lcd_init();
......
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stm32f4xx_hal.h>
#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
#include "nlr.h"
#include "map.h"
#include "pin.h"
// Usage Model:
//
// All Board Pins are predefined as pyb.Pin.board.Name
//
// x1_pin = pyb.Pin.board.X1
//
// g = pyb.gpio(pyb.Pin.board.X1, 0)
//
// CPU pins which correspond to the board pins are available
// as pyb.cpu.Name. For the CPU pins, the names are the port letter
// followed by the pin number. On the PYBOARD4, pyb.Pin.board.X1 and
// pyb.Pin.cpu.B6 are the same pin.
//
// You can also use strings:
//
// g = pyb.gpio('X1', 0)
//
// Users can add their own names:
//
// pyb.Pin("LeftMotorDir", pyb.Pin.cpu.C12)
// g = pyb.gpio("LeftMotorDir", 0)
//
// and can query mappings
//