blob: 8b09c7f655cccfec7ad91f3eb5d92bd70e8229a2 [file] [log] [blame]
Damien George87c62502015-02-13 22:21:44 +00001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014, 2015 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27#include <stdio.h>
28#include <stdint.h>
29#include <string.h>
30
31#include "c_types.h"
32#include "user_interface.h"
33#include "gpio.h"
34
35#include "py/nlr.h"
36#include "py/runtime.h"
37#include "modpyb.h"
38
39#define GPIO_MODE_INPUT (0)
40#define GPIO_MODE_OUTPUT (1)
41#define GPIO_PULL_NONE (0)
42#define GPIO_PULL_UP (1)
43#define GPIO_PULL_DOWN (2)
44
45typedef struct _pyb_pin_obj_t {
46 mp_obj_base_t base;
47 uint16_t pin_id;
48 uint16_t phys_port;
49 uint32_t periph;
50 uint16_t func;
51} pyb_pin_obj_t;
52
53STATIC const pyb_pin_obj_t pyb_pin_obj[] = {
54 {{&pyb_pin_type}, 0, 0, PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0},
55 {{&pyb_pin_type}, 1, 1, PERIPHS_IO_MUX_U0TXD_U, FUNC_GPIO1},
56 {{&pyb_pin_type}, 2, 2, PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2},
57 {{&pyb_pin_type}, 3, 3, PERIPHS_IO_MUX_U0RXD_U, FUNC_GPIO3},
58 {{&pyb_pin_type}, 4, 4, PERIPHS_IO_MUX_GPIO4_U, FUNC_GPIO4},
59 {{&pyb_pin_type}, 5, 5, PERIPHS_IO_MUX_GPIO5_U, FUNC_GPIO5},
60 {{&pyb_pin_type}, 9, 9, PERIPHS_IO_MUX_SD_DATA2_U, FUNC_GPIO9},
61 {{&pyb_pin_type}, 10, 10, PERIPHS_IO_MUX_SD_DATA3_U, FUNC_GPIO10},
62 {{&pyb_pin_type}, 12, 12, PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12},
63 {{&pyb_pin_type}, 13, 13, PERIPHS_IO_MUX_MTCK_U, FUNC_GPIO13},
64 {{&pyb_pin_type}, 14, 14, PERIPHS_IO_MUX_MTMS_U, FUNC_GPIO14},
65 {{&pyb_pin_type}, 15, 15, PERIPHS_IO_MUX_MTDO_U, FUNC_GPIO15},
66};
67
Damien George7f9d1d62015-04-09 23:56:15 +010068STATIC void pyb_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
Damien George87c62502015-02-13 22:21:44 +000069 pyb_pin_obj_t *self = self_in;
70
71 // pin name
Damien George7f9d1d62015-04-09 23:56:15 +010072 mp_printf(print, "Pin(%u)", self->pin_id);
Damien George87c62502015-02-13 22:21:44 +000073}
74
75// pin.init(mode, pull=Pin.PULL_NONE, af=-1)
76STATIC mp_obj_t pyb_pin_obj_init_helper(pyb_pin_obj_t *self, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
77 static const mp_arg_t allowed_args[] = {
78 { MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT },
79 { MP_QSTR_pull, MP_ARG_INT, {.u_int = GPIO_PULL_NONE}},
80 };
81
82 // parse args
83 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
84 mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
85
86 // get io mode
87 uint mode = args[0].u_int;
88
89 // get pull mode
90 uint pull = args[1].u_int;
91
92 // configure the GPIO as requested
93 PIN_FUNC_SELECT(self->periph, self->func);
94 if ((pull & GPIO_PULL_DOWN) == 0) {
95 PIN_PULLDWN_DIS(self->periph);
96 }
97 if ((pull & GPIO_PULL_UP) == 0) {
98 PIN_PULLUP_DIS(self->periph);
99 }
100 if ((pull & GPIO_PULL_DOWN) != 0) {
101 PIN_PULLDWN_EN(self->periph);
102 }
103 if ((pull & GPIO_PULL_UP) != 0) {
104 PIN_PULLUP_EN(self->periph);
105 }
106
107 // TODO input mode is not working...
108 if ((mode & GPIO_MODE_OUTPUT) == 0) {
109 GPIO_DIS_OUTPUT(self->phys_port);
110 }
111
112 return mp_const_none;
113}
114
115// constructor(id, ...)
116STATIC mp_obj_t pyb_pin_make_new(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
117 mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
118
119 // Run an argument through the mapper and return the result.
120 int wanted_pin = mp_obj_get_int(args[0]);
121 pyb_pin_obj_t *pin = NULL;
122 for (int i = 0; i < MP_ARRAY_SIZE(pyb_pin_obj); i++) {
123 if (pyb_pin_obj[i].pin_id == wanted_pin) {
124 pin = (pyb_pin_obj_t*)&pyb_pin_obj[i];
125 break;
126 }
127 }
128 if (pin == NULL) {
129 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid pin"));
130 }
131
132 if (n_args > 1 || n_kw > 0) {
133 // pin mode given, so configure this GPIO
134 mp_map_t kw_args;
135 mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
136 pyb_pin_obj_init_helper(pin, n_args - 1, args + 1, &kw_args);
137 }
138
139 return (mp_obj_t)pin;
140}
141
142// pin.init(mode, pull)
143STATIC mp_obj_t pyb_pin_obj_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
144 return pyb_pin_obj_init_helper(args[0], n_args - 1, args + 1, kw_args);
145}
146MP_DEFINE_CONST_FUN_OBJ_KW(pyb_pin_init_obj, 1, pyb_pin_obj_init);
147
148// pin.value([value])
149STATIC mp_obj_t pyb_pin_value(mp_uint_t n_args, const mp_obj_t *args) {
150 pyb_pin_obj_t *self = args[0];
151 if (n_args == 1) {
152 // get pin
153 return MP_OBJ_NEW_SMALL_INT(GPIO_INPUT_GET(self->phys_port));
154 } else {
155 // set pin
156 if (mp_obj_is_true(args[1])) {
157 GPIO_OUTPUT_SET(self->phys_port, 1);
158 } else {
159 GPIO_OUTPUT_SET(self->phys_port, 0);
160 }
161 return mp_const_none;
162 }
163}
164STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_pin_value_obj, 1, 2, pyb_pin_value);
165
166// pin.low()
167STATIC mp_obj_t pyb_pin_low(mp_obj_t self_in) {
168 pyb_pin_obj_t *self = self_in;
169 GPIO_OUTPUT_SET(self->phys_port, 0);
170 return mp_const_none;
171}
172STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_pin_low_obj, pyb_pin_low);
173
174// pin.high()
175STATIC mp_obj_t pyb_pin_high(mp_obj_t self_in) {
176 pyb_pin_obj_t *self = self_in;
177 GPIO_OUTPUT_SET(self->phys_port, 1);
178 return mp_const_none;
179}
180STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_pin_high_obj, pyb_pin_high);
181
182STATIC const mp_map_elem_t pyb_pin_locals_dict_table[] = {
183 // instance methods
184 { MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&pyb_pin_init_obj },
185 { MP_OBJ_NEW_QSTR(MP_QSTR_value), (mp_obj_t)&pyb_pin_value_obj },
186 { MP_OBJ_NEW_QSTR(MP_QSTR_low), (mp_obj_t)&pyb_pin_low_obj },
187 { MP_OBJ_NEW_QSTR(MP_QSTR_high), (mp_obj_t)&pyb_pin_high_obj },
188
189 // class constants
190 { MP_OBJ_NEW_QSTR(MP_QSTR_IN), MP_OBJ_NEW_SMALL_INT(GPIO_MODE_INPUT) },
191 { MP_OBJ_NEW_QSTR(MP_QSTR_OUT_PP), MP_OBJ_NEW_SMALL_INT(GPIO_MODE_OUTPUT) },
192 { MP_OBJ_NEW_QSTR(MP_QSTR_PULL_NONE), MP_OBJ_NEW_SMALL_INT(GPIO_PULL_NONE) },
193 { MP_OBJ_NEW_QSTR(MP_QSTR_PULL_UP), MP_OBJ_NEW_SMALL_INT(GPIO_PULL_UP) },
194 { MP_OBJ_NEW_QSTR(MP_QSTR_PULL_DOWN), MP_OBJ_NEW_SMALL_INT(GPIO_PULL_DOWN) },
195};
196
197STATIC MP_DEFINE_CONST_DICT(pyb_pin_locals_dict, pyb_pin_locals_dict_table);
198
199const mp_obj_type_t pyb_pin_type = {
200 { &mp_type_type },
201 .name = MP_QSTR_Pin,
202 .print = pyb_pin_print,
203 .make_new = pyb_pin_make_new,
204 .locals_dict = (mp_obj_t)&pyb_pin_locals_dict,
205};