summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--stm/Makefile1
-rw-r--r--stm/gpio.c130
-rw-r--r--stm/gpio.h1
-rw-r--r--stm/main.c54
-rw-r--r--stm/qstrdefsport.h2
-rw-r--r--stm/stmperiph/stm324x7i_eval.c3
-rw-r--r--stm/usart.c6
7 files changed, 146 insertions, 51 deletions
diff --git a/stm/Makefile b/stm/Makefile
index bf56fc9bc1..479d93ec7a 100644
--- a/stm/Makefile
+++ b/stm/Makefile
@@ -51,6 +51,7 @@ SRC_C = \
lexerfatfs.c \
pyexec.c \
led.c \
+ gpio.c \
lcd.c \
servo.c \
flash.c \
diff --git a/stm/gpio.c b/stm/gpio.c
new file mode 100644
index 0000000000..99fb49be4f
--- /dev/null
+++ b/stm/gpio.c
@@ -0,0 +1,130 @@
+// 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.h>
+#include <stm32f4xx_rcc.h>
+#include <stm32f4xx_syscfg.h>
+#include <stm32f4xx_gpio.h>
+#include <stm32f4xx_exti.h>
+#include <stm32f4xx_tim.h>
+#include <stm32f4xx_pwr.h>
+#include <stm32f4xx_rtc.h>
+#include <stm32f4xx_usart.h>
+#include <stm32f4xx_rng.h>
+#include <usbd_storage_msd.h>
+#include <stm_misc.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"
+
+void parse_pin_name(mp_obj_t name, GPIO_TypeDef **port, uint *pin) {
+ const char *pin_name = mp_obj_str_get_str(name);
+ switch (pin_name[0]) {
+ case 'A': case 'a': *port = GPIOA; break;
+ case 'B': case 'b': *port = GPIOB; break;
+ case 'C': case 'c': *port = GPIOC; break;
+ case 'D': case 'd': *port = GPIOD; break;
+ default: goto pin_error;
+ }
+ *pin = 0;
+ for (const char *s = pin_name + 1; *s; s++) {
+ if (!('0' <= *s && *s <= '9')) {
+ goto pin_error;
+ }
+ *pin = 10 * (*pin) + *s - '0';
+ }
+ if (!(0 <= *pin && *pin <= 15)) {
+ goto pin_error;
+ }
+
+ return;
+
+pin_error:
+ nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "pin %s does not exist", pin_name));
+}
+
+mp_obj_t pyb_gpio(uint n_args, mp_obj_t *args) {
+ //assert(1 <= n_args && n_args <= 2);
+
+ GPIO_TypeDef *port;
+ uint pin;
+ parse_pin_name(args[0], &port, &pin);
+
+ if (n_args == 1) {
+ // get pin
+ if ((port->IDR & (1 << pin)) != (uint32_t)Bit_RESET) {
+ return MP_OBJ_NEW_SMALL_INT(1);
+ } else {
+ return MP_OBJ_NEW_SMALL_INT(0);
+ }
+ } else {
+ // set pin
+ if (rt_is_true(args[1])) {
+ // set pin high
+ port->BSRRL = 1 << pin;
+ } else {
+ // set pin low
+ port->BSRRH = 1 << pin;
+ }
+ return mp_const_none;
+ }
+}
+
+MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_gpio_obj, 1, 2, pyb_gpio);
+
+mp_obj_t pyb_gpio_input(mp_obj_t arg_port_pin, mp_obj_t arg_mode) {
+ GPIO_TypeDef *port;
+ uint pin;
+ parse_pin_name(arg_port_pin, &port, &pin);
+
+ GPIO_InitTypeDef GPIO_InitStructure;
+ GPIO_InitStructure.GPIO_Pin = 1 << pin;
+ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
+ GPIO_InitStructure.GPIO_PuPd = mp_obj_get_int(arg_mode);
+ GPIO_Init(port, &GPIO_InitStructure);
+
+ return mp_const_none;
+}
+
+MP_DEFINE_CONST_FUN_OBJ_2(pyb_gpio_input_obj, pyb_gpio_input);
+
+mp_obj_t pyb_gpio_output(mp_obj_t arg_port_pin, mp_obj_t arg_mode) {
+ GPIO_TypeDef *port;
+ uint pin;
+ parse_pin_name(arg_port_pin, &port, &pin);
+
+ GPIO_InitTypeDef GPIO_InitStructure;
+ GPIO_InitStructure.GPIO_Pin = 1 << pin;
+ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
+ GPIO_InitStructure.GPIO_Speed = GPIO_Fast_Speed;
+ GPIO_InitStructure.GPIO_OType = mp_obj_get_int(arg_mode);
+ GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
+ GPIO_Init(port, &GPIO_InitStructure);
+
+ return mp_const_none;
+}
+
+MP_DEFINE_CONST_FUN_OBJ_2(pyb_gpio_output_obj, pyb_gpio_output);
+
+void gpio_init(mp_obj_t mod) {
+ rt_store_attr(mod, MP_QSTR_gpio, (mp_obj_t)&pyb_gpio_obj);
+ rt_store_attr(mod, MP_QSTR_gpio_in, (mp_obj_t)&pyb_gpio_input_obj);
+ rt_store_attr(mod, MP_QSTR_gpio_out, (mp_obj_t)&pyb_gpio_output_obj);
+ rt_store_attr(mod, qstr_from_str("PULL_NONE"), MP_OBJ_NEW_SMALL_INT(GPIO_PuPd_NOPULL));
+ rt_store_attr(mod, qstr_from_str("PULL_UP"), MP_OBJ_NEW_SMALL_INT(GPIO_PuPd_UP));
+ rt_store_attr(mod, qstr_from_str("PULL_DOWN"), MP_OBJ_NEW_SMALL_INT(GPIO_PuPd_DOWN));
+ rt_store_attr(mod, qstr_from_str("PUSH_PULL"), MP_OBJ_NEW_SMALL_INT(GPIO_OType_PP));
+ rt_store_attr(mod, qstr_from_str("OPEN_DRAIN"), MP_OBJ_NEW_SMALL_INT(GPIO_OType_OD));
+}
diff --git a/stm/gpio.h b/stm/gpio.h
new file mode 100644
index 0000000000..2f81ac6658
--- /dev/null
+++ b/stm/gpio.h
@@ -0,0 +1 @@
+void gpio_init(mp_obj_t mod);
diff --git a/stm/main.c b/stm/main.c
index 33af85351e..b283821bba 100644
--- a/stm/main.c
+++ b/stm/main.c
@@ -34,6 +34,7 @@
#include "pendsv.h"
#include "pyexec.h"
#include "led.h"
+#include "gpio.h"
#include "servo.h"
#include "lcd.h"
#include "storage.h"
@@ -53,7 +54,9 @@
int errno;
static FATFS fatfs0;
+#if MICROPY_HW_HAS_SDCARD
static FATFS fatfs1;
+#endif
void flash_error(int n) {
for (int i = 0; i < n; i++) {
@@ -259,53 +262,6 @@ static mp_obj_t pyb_standby(void) {
return mp_const_none;
}
-mp_obj_t pyb_gpio(uint n_args, mp_obj_t *args) {
- //assert(1 <= n_args && n_args <= 2);
-
- const char *pin_name = mp_obj_str_get_str(args[0]);
- GPIO_TypeDef *port;
- switch (pin_name[0]) {
- case 'A': case 'a': port = GPIOA; break;
- case 'B': case 'b': port = GPIOB; break;
- case 'C': case 'c': port = GPIOC; break;
- default: goto pin_error;
- }
- uint pin_num = 0;
- for (const char *s = pin_name + 1; *s; s++) {
- if (!('0' <= *s && *s <= '9')) {
- goto pin_error;
- }
- pin_num = 10 * pin_num + *s - '0';
- }
- if (!(0 <= pin_num && pin_num <= 15)) {
- goto pin_error;
- }
-
- if (n_args == 1) {
- // get pin
- if ((port->IDR & (1 << pin_num)) != (uint32_t)Bit_RESET) {
- return MP_OBJ_NEW_SMALL_INT(1);
- } else {
- return MP_OBJ_NEW_SMALL_INT(0);
- }
- } else {
- // set pin
- if (rt_is_true(args[1])) {
- // set pin high
- port->BSRRL = 1 << pin_num;
- } else {
- // set pin low
- port->BSRRH = 1 << pin_num;
- }
- return mp_const_none;
- }
-
-pin_error:
- nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "pin %s does not exist", pin_name));
-}
-
-MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_gpio_obj, 1, 2, pyb_gpio);
-
mp_obj_t pyb_hid_send_report(mp_obj_t arg) {
mp_obj_t *items = mp_obj_get_array_fixed_n(arg, 4);
uint8_t data[4];
@@ -484,11 +440,13 @@ soft_reset:
rt_store_attr(m, MP_QSTR_Servo, rt_make_function_n(1, pyb_Servo));
#endif
rt_store_attr(m, MP_QSTR_I2C, rt_make_function_n(2, pyb_I2C));
- rt_store_attr(m, MP_QSTR_gpio, (mp_obj_t)&pyb_gpio_obj);
rt_store_attr(m, MP_QSTR_Usart, rt_make_function_n(2, pyb_Usart));
rt_store_attr(m, qstr_from_str("ADC_all"), (mp_obj_t)&pyb_ADC_all_obj);
rt_store_attr(m, MP_QSTR_ADC, (mp_obj_t)&pyb_ADC_obj);
rt_store_attr(m, qstr_from_str("millis"), rt_make_function_n(0, pyb_millis));
+
+ gpio_init(m);
+
rt_store_name(MP_QSTR_pyb, m);
rt_store_name(MP_QSTR_open, rt_make_function_n(2, pyb_io_open));
diff --git a/stm/qstrdefsport.h b/stm/qstrdefsport.h
index 02208875d3..65c4f81a94 100644
--- a/stm/qstrdefsport.h
+++ b/stm/qstrdefsport.h
@@ -26,6 +26,8 @@ Q(Servo)
Q(SDcard)
Q(I2C)
Q(gpio)
+Q(gpio_in)
+Q(gpio_out)
Q(Usart)
Q(ADC)
Q(open)
diff --git a/stm/stmperiph/stm324x7i_eval.c b/stm/stmperiph/stm324x7i_eval.c
index 2a8943936d..c5a9d425c8 100644
--- a/stm/stmperiph/stm324x7i_eval.c
+++ b/stm/stmperiph/stm324x7i_eval.c
@@ -89,7 +89,6 @@ void SD_LowLevel_DeInit(void)
* yet still detect when a card is inserted.
*/
void SD_LowLevel_Init_Detect(void) {
- GPIO_InitTypeDef GPIO_InitStructure;
/* Periph clock enable */
RCC_AHB1PeriphClockCmd(SD_DETECT_GPIO_CLK, ENABLE);
@@ -97,6 +96,7 @@ void SD_LowLevel_Init_Detect(void) {
/*!< Configure SD_SPI_DETECT_PIN pin: SD Card detect pin */
#if defined(PYBOARD3)
// dpgeorge: PYBv2-v3: switch is normally open, connected to VDD when card inserted
+ GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = SD_DETECT_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; // needs to be 2MHz due to restrictions on PC13
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
@@ -104,6 +104,7 @@ void SD_LowLevel_Init_Detect(void) {
GPIO_Init(SD_DETECT_GPIO_PORT, &GPIO_InitStructure);
#elif defined(PYBOARD4)
// dpgeorge: PYBv4: switch is normally open, connected to GND when card inserted
+ GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = SD_DETECT_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
diff --git a/stm/usart.c b/stm/usart.c
index 6478f01152..78d9a0fee4 100644
--- a/stm/usart.c
+++ b/stm/usart.c
@@ -205,8 +205,10 @@ static mp_obj_t usart_obj_rx_char(mp_obj_t self_in) {
static mp_obj_t usart_obj_tx_char(mp_obj_t self_in, mp_obj_t c) {
pyb_usart_obj_t *self = self_in;
- if (self->is_enabled) {
- usart_tx_char(self->usart_id, mp_obj_get_int(c));
+ uint len;
+ const char *str = mp_obj_str_get_data(c, &len);
+ if (len == 1 && self->is_enabled) {
+ usart_tx_char(self->usart_id, str[0]);
}
return mp_const_none;
}