Commit 7a37f647 authored by Damien George's avatar Damien George
Browse files

Merge branch 'teensy-new' of github.com:dhylands/micropython into dhylands-teensy-new

Conflicts:
	stmhal/pin_named_pins.c
	stmhal/readline.c

Renamed HAL_H to MICROPY_HAL_H.  Made stmhal/mphal.h which intends to
define the generic Micro Python HAL, which in stmhal sits above the ST
HAL.
parents 5fc58047 4f1b7fec
...@@ -97,4 +97,10 @@ print-cfg: ...@@ -97,4 +97,10 @@ print-cfg:
$(ECHO) "OBJ = $(OBJ)" $(ECHO) "OBJ = $(OBJ)"
.PHONY: print-cfg .PHONY: print-cfg
print-def:
@$(ECHO) "The following defines are built into the $(CC) compiler"
touch __empty__.c
@$(CC) -E -Wp,-dM __empty__.c
@$(RM) -f __empty__.c
-include $(OBJ:.o=.P) -include $(OBJ:.o=.P)
...@@ -25,8 +25,7 @@ ...@@ -25,8 +25,7 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdint.h>
#include <stm32f4xx_hal.h>
#include "mpconfig.h" #include "mpconfig.h"
#include "misc.h" #include "misc.h"
...@@ -34,6 +33,7 @@ ...@@ -34,6 +33,7 @@
#include "obj.h" #include "obj.h"
#include "gc.h" #include "gc.h"
#include "gccollect.h" #include "gccollect.h"
#include MICROPY_HAL_H
machine_uint_t gc_helper_get_regs_and_sp(machine_uint_t *regs); machine_uint_t gc_helper_get_regs_and_sp(machine_uint_t *regs);
......
...@@ -105,3 +105,6 @@ typedef const void *machine_const_ptr_t; // must be of pointer size ...@@ -105,3 +105,6 @@ typedef const void *machine_const_ptr_t; // must be of pointer size
// We need to provide a declaration/definition of alloca() // We need to provide a declaration/definition of alloca()
#include <alloca.h> #include <alloca.h>
#define MICROPY_HAL_H "mphal.h"
#define MICROPY_PIN_DEFS_PORT_H "pin_defs_stmhal.h"
// 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))
...@@ -28,14 +28,13 @@ ...@@ -28,14 +28,13 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include "stm32f4xx_hal.h"
#include "mpconfig.h" #include "mpconfig.h"
#include "nlr.h" #include "nlr.h"
#include "misc.h" #include "misc.h"
#include "qstr.h" #include "qstr.h"
#include "obj.h" #include "obj.h"
#include "runtime.h" #include "runtime.h"
#include MICROPY_HAL_H
#include "pin.h" #include "pin.h"
/// \moduleref pyb /// \moduleref pyb
...@@ -310,13 +309,13 @@ STATIC mp_obj_t pin_value(uint n_args, mp_obj_t *args) { ...@@ -310,13 +309,13 @@ STATIC mp_obj_t pin_value(uint n_args, mp_obj_t *args) {
pin_obj_t *self = args[0]; pin_obj_t *self = args[0];
if (n_args == 1) { if (n_args == 1) {
// get pin // 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 { } else {
// set pin // set pin
if (mp_obj_is_true(args[1])) { if (mp_obj_is_true(args[1])) {
self->gpio->BSRRL = self->pin_mask; GPIO_set_pin(self->gpio, self->pin_mask);
} else { } else {
self->gpio->BSRRH = self->pin_mask; GPIO_clear_pin(self->gpio, self->pin_mask);
} }
return mp_const_none; return mp_const_none;
} }
...@@ -327,7 +326,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pin_value_obj, 1, 2, pin_value); ...@@ -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. /// Set the pin to a low logic level.
STATIC mp_obj_t pin_low(mp_obj_t self_in) { STATIC mp_obj_t pin_low(mp_obj_t self_in) {
pin_obj_t *self = 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; return mp_const_none;
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_low_obj, pin_low); 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); ...@@ -336,7 +335,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_low_obj, pin_low);
/// Set the pin to a high logic level. /// Set the pin to a high logic level.
STATIC mp_obj_t pin_high(mp_obj_t self_in) { STATIC mp_obj_t pin_high(mp_obj_t self_in) {
pin_obj_t *self = 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; return mp_const_none;
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_high_obj, pin_high); STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_high_obj, pin_high);
......
...@@ -24,63 +24,10 @@ ...@@ -24,63 +24,10 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
enum { // This file requires pin_defs_xxx.h (which has port specific enums and
PORT_A, // defines, so we include it here. It should never be included directly
PORT_B,
PORT_C,
PORT_D,
PORT_E,
PORT_F,
PORT_G,
PORT_H,
PORT_I,
PORT_J,
};
enum { #include MICROPY_PIN_DEFS_PORT_H
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),
};
typedef struct { typedef struct {
mp_obj_base_t base; mp_obj_base_t base;
...@@ -91,24 +38,21 @@ typedef struct { ...@@ -91,24 +38,21 @@ typedef struct {
union { union {
void *reg; void *reg;
TIM_TypeDef *TIM;
I2C_TypeDef *I2C; PIN_DEFS_PORT_AF_UNION
USART_TypeDef *USART;
USART_TypeDef *UART;
SPI_TypeDef *SPI;
}; };
} pin_af_obj_t; } pin_af_obj_t;
typedef struct { typedef struct {
mp_obj_base_t base; mp_obj_base_t base;
const char *name; const char *name;
uint16_t port : 4; uint32_t port : 4;
uint16_t pin : 4; uint32_t pin : 5; // Some ARM processors use 32 bits/PORT
uint16_t num_af : 4; uint32_t num_af : 4;
uint16_t adc_channel : 4; uint32_t adc_channel : 5; // Some ARM processors use 32 bits/PORT
uint16_t adc_num : 3; // 1 bit per ADC uint32_t adc_num : 3; // 1 bit per ADC
uint16_t pin_mask; uint32_t pin_mask;
GPIO_TypeDef *gpio; pin_gpio_t *gpio;
const pin_af_obj_t *af; const pin_af_obj_t *af;
} pin_obj_t; } pin_obj_t;
......
/*
* 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;
...@@ -28,13 +28,12 @@ ...@@ -28,13 +28,12 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include "stm32f4xx_hal.h"
#include "mpconfig.h" #include "mpconfig.h"
#include "misc.h" #include "misc.h"
#include "qstr.h" #include "qstr.h"
#include "obj.h" #include "obj.h"
#include "runtime.h" #include "runtime.h"
#include MICROPY_HAL_H
#include "pin.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) { 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) {
......
...@@ -25,8 +25,7 @@ ...@@ -25,8 +25,7 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdint.h>
#include <stm32f4xx_hal.h>
#include "mpconfig.h" #include "mpconfig.h"
#include "misc.h" #include "misc.h"
...@@ -34,6 +33,7 @@ ...@@ -34,6 +33,7 @@
#include "misc.h" #include "misc.h"
#include "obj.h" #include "obj.h"
#include "stream.h" #include "stream.h"
#include MICROPY_HAL_H
#include "pybstdio.h" #include "pybstdio.h"
#include "usb.h" #include "usb.h"
#include "uart.h" #include "uart.h"
......
...@@ -26,8 +26,7 @@ ...@@ -26,8 +26,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdint.h>
#include <stm32f4xx_hal.h>
#include "mpconfig.h" #include "mpconfig.h"
#include "nlr.h" #include "nlr.h"
...@@ -43,6 +42,7 @@ ...@@ -43,6 +42,7 @@
#include "repl.h" #include "repl.h"
#include "gc.h" #include "gc.h"
#include "gccollect.h" #include "gccollect.h"
#include MICROPY_HAL_H
#include "systick.h" #include "systick.h"
#include "pybstdio.h" #include "pybstdio.h"
#include "readline.h" #include "readline.h"
......
...@@ -25,15 +25,15 @@ ...@@ -25,15 +25,15 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdint.h>
#include <string.h> #include <string.h>
#include <stm32f4xx_hal.h>
#include "mpconfig.h" #include "mpconfig.h"
#include "misc.h" #include "misc.h"
#include "qstr.h" #include "qstr.h"
#include "misc.h" #include "misc.h"
#include "obj.h" #include "obj.h"
#include MICROPY_HAL_H
#include "pybstdio.h" #include "pybstdio.h"
#include "readline.h" #include "readline.h"
#include "usb.h" #include "usb.h"
......
...@@ -20,12 +20,15 @@ CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mcpu=cortex-m4 -fsingle-precision-c ...@@ -20,12 +20,15 @@ CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mcpu=cortex-m4 -fsingle-precision-c
INC = -I. INC = -I.
INC += -I$(PY_SRC) INC += -I$(PY_SRC)
INC += -I../stmhal
INC += -I$(BUILD) INC += -I$(BUILD)
INC += -I$(CORE_PATH) INC += -I$(CORE_PATH)
CFLAGS = $(INC) -Wall -ansi -std=gnu99 -nostdlib $(CFLAGS_CORTEX_M4) CFLAGS = $(INC) -Wall -ansi -std=gnu99 -nostdlib $(CFLAGS_CORTEX_M4)
LDFLAGS = -nostdlib -T mk20dx256.ld LDFLAGS = -nostdlib -T mk20dx256.ld
LIBS = -L $(COMPILER_PATH)/../lib/gcc/arm-none-eabi/4.7.2/thumb2 -lgcc LIBS = -L $(COMPILER_PATH)/../arm-none-eabi/lib/thumb2 -lm
LIBS += -L $(COMPILER_PATH)/../arm-none-eabi/lib/thumb2 -lc
LIBS += -L $(COMPILER_PATH)/../lib/gcc/arm-none-eabi/4.7.2/thumb2 -lgcc
#Debugging/Optimization #Debugging/Optimization
ifdef DEBUG ifdef DEBUG
...@@ -35,23 +38,32 @@ CFLAGS += -Os #-DNDEBUG ...@@ -35,23 +38,32 @@ CFLAGS += -Os #-DNDEBUG
endif endif
SRC_C = \ SRC_C = \
hal_gpio.c \
help.c \
import.c \
main.c \ main.c \
lcd.c \ lcd.c \
led.c \ led.c \
lexerfatfs.c \
lexermemzip.c \ lexermemzip.c \
memzip.c \ memzip.c \
servo.c \ modpyb.c \
usart.c \ teensy_hal.c \
uart.c \
usb.c \ usb.c \
STM_SRC_C = $(addprefix stm/,\ STM_SRC_C = $(addprefix stmhal/,\
malloc0.c \ gccollect.c \
input.c \
pin.c \
pin_named_pins.c \
printf.c \ printf.c \
pyexec.c \
pybstdio.c \
readline.c \
string0.c \ string0.c \
) )
STM_SRC_S = $(addprefix stm/,\ STM_SRC_S = $(addprefix stmhal/,\
gchelper.s \ gchelper.s \
) )
...@@ -66,9 +78,7 @@ SRC_TEENSY = \ ...@@ -66,9 +78,7 @@ SRC_TEENSY = \
yield.c \ yield.c \
OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(STM_SRC_C:.c=.o) $(STM_SRC_S:.s=.o) $(SRC_TEENSY:.c=.o)) OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(STM_SRC_C:.c=.o) $(STM_SRC_S:.s=.o) $(SRC_TEENSY:.c=.o))
#LIB = -lreadline OBJ += $(BUILD)/pins_gen.o
# the following is needed for BSD
#LIB += -ltermcap
all: hex all: hex
hex: $(BUILD)/micropython-mz.hex hex: $(BUILD)/micropython-mz.hex
...@@ -84,7 +94,7 @@ reboot: ...@@ -84,7 +94,7 @@ reboot:
upload: post_compile reboot upload: post_compile reboot
$(BUILD)/micropython.elf: $(OBJ) $(BUILD)/micropython.elf: $(OBJ)
$(ECHO) "LINK $<" $(ECHO) "LINK $@"
$(Q)$(CC) $(LDFLAGS) -o "$@" -Wl,-Map,$(@:.elf=.map) $(OBJ) $(LIBS) $(Q)$(CC) $(LDFLAGS) -o "$@" -Wl,-Map,$(@:.elf=.map) $(OBJ) $(LIBS)
$(Q)$(SIZE) $@ $(Q)$(SIZE) $@
...@@ -103,4 +113,31 @@ $(BUILD)/%.hex: $(BUILD)/%.elf ...@@ -103,4 +113,31 @@ $(BUILD)/%.hex: $(BUILD)/%.elf
$(BUILD)/%.o: $(CORE_PATH)/%.c $(BUILD)/%.o: $(CORE_PATH)/%.c
$(call compile_c) $(call compile_c)
MAKE_PINS = make-pins.py
BOARD_PINS = teensy-pins.csv
AF_FILE = mk20dx256-af.csv
PREFIX_FILE = mk20dx256-prefix.c
GEN_PINS_SRC = $(BUILD)/pins_gen.c
GEN_PINS_HDR = $(HEADER_BUILD)/pins.h
# Making OBJ use an order-only depenedency on the generated pins.h file
# has the side effect of making the pins.h file before we actually compile
# any of the objects. The normal dependency generation will deal with the
# case when pins.h is modified. But when it doesn't exist, we don't know
# which source files might need it.
$(OBJ): | $(HEADER_BUILD)/pins.h
# Use a pattern rule here so that make will only call make-pins.py once to make
# both pins_$(BOARD).c and pins.h
$(BUILD)/%_gen.c $(HEADER_BUILD)/%.h: teensy-%.csv $(MAKE_PINS) $(AF_FILE) $(PREFIX_FILE)
$(ECHO) "Create $@"
$(Q)$(PYTHON) $(MAKE_PINS) --board $(BOARD_PINS) --af $(AF_FILE) --prefix $(PREFIX_FILE) --hdr $(GEN_PINS_HDR) > $(GEN_PINS_SRC)
$(BUILD)/pins_gen.o: $(BUILD)/pins_gen.c
$(call compile_c)
$(BUILD)/%.pp: $(BUILD)/%.c
$(ECHO) "PreProcess $<"
$(Q)$(CC) $(CFLAGS) -E -Wp,-C,-dD,-dI -o $@ $<
include ../py/mkrules.mk include ../py/mkrules.mk
#include <stdint.h>
#include <mk20dx128.h>
#include "teensy_hal.h"
#define GPIO_NUMBER 32
void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
{
/* Check the parameters */
assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
/* Configure the port pins */
for (uint32_t position = 0; position < GPIO_NUMBER; position++) {
uint32_t bitmask = 1 << position;
if ((GPIO_Init->Pin & bitmask) == 0) {
continue;
}
volatile uint32_t *port_pcr = GPIO_PIN_TO_PORT_PCR(GPIOx, position);
/*--------------------- GPIO Mode Configuration ------------------------*/
/* In case of Alternate function mode selection */
if ((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD)) {
/* Check the Alternate function parameter */
assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
/* Configure Alternate function mapped with the current IO */
*port_pcr &= ~PORT_PCR_MUX_MASK;
*port_pcr |= PORT_PCR_MUX(GPIO_Init->Alternate);
}
/* Configure IO Direction mode (Input, Output, Alternate or Analog) */
if (GPIO_Init->Mode == GPIO_MODE_INPUT || GPIO_Init->Mode == GPIO_MODE_ANALOG) {
GPIOx->PDDR &= ~bitmask;
} else {
GPIOx->PDDR |= bitmask;
}
/* In case of Output or Alternate function mode selection */
if ((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) ||
(GPIO_Init->Mode == GPIO_MODE_OUT