gpio.c 2.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 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"
Dave Hylands's avatar
Dave Hylands committed
31
#include "pin.h"
32
33
34
35

mp_obj_t pyb_gpio(uint n_args, mp_obj_t *args) {
    //assert(1 <= n_args && n_args <= 2);

Dave Hylands's avatar
Dave Hylands committed
36
37
38
    const pin_obj_t *pin = pin_map_user_obj(args[0]);
    GPIO_TypeDef *port = pin->gpio;
    uint16_t pin_mask = pin->pin_mask;
39
40
41

    if (n_args == 1) {
        // get pin
Dave Hylands's avatar
Dave Hylands committed
42
        if ((port->IDR & pin_mask) != (uint32_t)Bit_RESET) {
43
44
45
46
47
48
49
50
            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
Dave Hylands's avatar
Dave Hylands committed
51
            port->BSRRL = pin_mask;
52
53
        } else {
            // set pin low
Dave Hylands's avatar
Dave Hylands committed
54
            port->BSRRH = pin_mask;
55
56
57
58
59
60
61
        }
        return mp_const_none;
    }
}

MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_gpio_obj, 1, 2, pyb_gpio);

Dave Hylands's avatar
Dave Hylands committed
62
63
64
65
mp_obj_t pyb_gpio_input(mp_obj_t arg_pin, mp_obj_t arg_mode) {
    const pin_obj_t *pin = pin_map_user_obj(arg_pin);
    GPIO_TypeDef *port = pin->gpio;
    uint16_t pin_mask = pin->pin_mask;
66
67

    GPIO_InitTypeDef GPIO_InitStructure;
Dave Hylands's avatar
Dave Hylands committed
68
    GPIO_InitStructure.GPIO_Pin = pin_mask;
69
70
71
72
73
74
75
76
77
    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);

Dave Hylands's avatar
Dave Hylands committed
78
79
80
81
mp_obj_t pyb_gpio_output(mp_obj_t arg_pin, mp_obj_t arg_mode) {
    const pin_obj_t *pin = pin_map_user_obj(arg_pin);
    GPIO_TypeDef *port = pin->gpio;
    uint16_t pin_mask = pin->pin_mask;
82
83

    GPIO_InitTypeDef GPIO_InitStructure;
Dave Hylands's avatar
Dave Hylands committed
84
    GPIO_InitStructure.GPIO_Pin = pin_mask;
85
86
87
88
89
90
91
92
93
94
    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);