blob: 3c3aebe6eac42cf1d9112fb1ea500deb02b89688 [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)
Josef Gajdusek7d8edef2015-05-26 18:54:55 +020043// Removed in SDK 1.1.0
44//#define GPIO_PULL_DOWN (2)
Damien George87c62502015-02-13 22:21:44 +000045
46typedef struct _pyb_pin_obj_t {
47 mp_obj_base_t base;
48 uint16_t pin_id;
49 uint16_t phys_port;
50 uint32_t periph;
51 uint16_t func;
52} pyb_pin_obj_t;
53
54STATIC const pyb_pin_obj_t pyb_pin_obj[] = {
55 {{&pyb_pin_type}, 0, 0, PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0},
56 {{&pyb_pin_type}, 1, 1, PERIPHS_IO_MUX_U0TXD_U, FUNC_GPIO1},
57 {{&pyb_pin_type}, 2, 2, PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2},
58 {{&pyb_pin_type}, 3, 3, PERIPHS_IO_MUX_U0RXD_U, FUNC_GPIO3},
59 {{&pyb_pin_type}, 4, 4, PERIPHS_IO_MUX_GPIO4_U, FUNC_GPIO4},
60 {{&pyb_pin_type}, 5, 5, PERIPHS_IO_MUX_GPIO5_U, FUNC_GPIO5},
61 {{&pyb_pin_type}, 9, 9, PERIPHS_IO_MUX_SD_DATA2_U, FUNC_GPIO9},
62 {{&pyb_pin_type}, 10, 10, PERIPHS_IO_MUX_SD_DATA3_U, FUNC_GPIO10},
63 {{&pyb_pin_type}, 12, 12, PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12},
64 {{&pyb_pin_type}, 13, 13, PERIPHS_IO_MUX_MTCK_U, FUNC_GPIO13},
65 {{&pyb_pin_type}, 14, 14, PERIPHS_IO_MUX_MTMS_U, FUNC_GPIO14},
66 {{&pyb_pin_type}, 15, 15, PERIPHS_IO_MUX_MTDO_U, FUNC_GPIO15},
67};
68
Damien George7f9d1d62015-04-09 23:56:15 +010069STATIC 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 +000070 pyb_pin_obj_t *self = self_in;
71
72 // pin name
Damien George7f9d1d62015-04-09 23:56:15 +010073 mp_printf(print, "Pin(%u)", self->pin_id);
Damien George87c62502015-02-13 22:21:44 +000074}
75
76// pin.init(mode, pull=Pin.PULL_NONE, af=-1)
77STATIC 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) {
78 static const mp_arg_t allowed_args[] = {
79 { MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT },
80 { MP_QSTR_pull, MP_ARG_INT, {.u_int = GPIO_PULL_NONE}},
81 };
82
83 // parse args
84 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
85 mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
86
87 // get io mode
88 uint mode = args[0].u_int;
89
90 // get pull mode
91 uint pull = args[1].u_int;
92
93 // configure the GPIO as requested
94 PIN_FUNC_SELECT(self->periph, self->func);
Josef Gajdusek7d8edef2015-05-26 18:54:55 +020095 #if 0
96 // Removed in SDK 1.1.0
Damien George87c62502015-02-13 22:21:44 +000097 if ((pull & GPIO_PULL_DOWN) == 0) {
98 PIN_PULLDWN_DIS(self->periph);
99 }
Josef Gajdusek7d8edef2015-05-26 18:54:55 +0200100 #endif
Damien George87c62502015-02-13 22:21:44 +0000101 if ((pull & GPIO_PULL_UP) == 0) {
102 PIN_PULLUP_DIS(self->periph);
103 }
Josef Gajdusek7d8edef2015-05-26 18:54:55 +0200104 #if 0
Damien George87c62502015-02-13 22:21:44 +0000105 if ((pull & GPIO_PULL_DOWN) != 0) {
106 PIN_PULLDWN_EN(self->periph);
107 }
Josef Gajdusek7d8edef2015-05-26 18:54:55 +0200108 #endif
Damien George87c62502015-02-13 22:21:44 +0000109 if ((pull & GPIO_PULL_UP) != 0) {
110 PIN_PULLUP_EN(self->periph);
111 }
112
113 // TODO input mode is not working...
114 if ((mode & GPIO_MODE_OUTPUT) == 0) {
115 GPIO_DIS_OUTPUT(self->phys_port);
116 }
117
118 return mp_const_none;
119}
120
121// constructor(id, ...)
Damien George5b3f0b72016-01-03 15:55:55 +0000122STATIC mp_obj_t pyb_pin_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
Damien George87c62502015-02-13 22:21:44 +0000123 mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
124
125 // Run an argument through the mapper and return the result.
126 int wanted_pin = mp_obj_get_int(args[0]);
127 pyb_pin_obj_t *pin = NULL;
128 for (int i = 0; i < MP_ARRAY_SIZE(pyb_pin_obj); i++) {
129 if (pyb_pin_obj[i].pin_id == wanted_pin) {
130 pin = (pyb_pin_obj_t*)&pyb_pin_obj[i];
131 break;
132 }
133 }
134 if (pin == NULL) {
135 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid pin"));
136 }
137
138 if (n_args > 1 || n_kw > 0) {
139 // pin mode given, so configure this GPIO
140 mp_map_t kw_args;
141 mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
142 pyb_pin_obj_init_helper(pin, n_args - 1, args + 1, &kw_args);
143 }
144
145 return (mp_obj_t)pin;
146}
147
148// pin.init(mode, pull)
149STATIC mp_obj_t pyb_pin_obj_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
150 return pyb_pin_obj_init_helper(args[0], n_args - 1, args + 1, kw_args);
151}
152MP_DEFINE_CONST_FUN_OBJ_KW(pyb_pin_init_obj, 1, pyb_pin_obj_init);
153
154// pin.value([value])
155STATIC mp_obj_t pyb_pin_value(mp_uint_t n_args, const mp_obj_t *args) {
156 pyb_pin_obj_t *self = args[0];
157 if (n_args == 1) {
158 // get pin
159 return MP_OBJ_NEW_SMALL_INT(GPIO_INPUT_GET(self->phys_port));
160 } else {
161 // set pin
162 if (mp_obj_is_true(args[1])) {
163 GPIO_OUTPUT_SET(self->phys_port, 1);
164 } else {
165 GPIO_OUTPUT_SET(self->phys_port, 0);
166 }
167 return mp_const_none;
168 }
169}
170STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_pin_value_obj, 1, 2, pyb_pin_value);
171
172// pin.low()
173STATIC mp_obj_t pyb_pin_low(mp_obj_t self_in) {
174 pyb_pin_obj_t *self = self_in;
175 GPIO_OUTPUT_SET(self->phys_port, 0);
176 return mp_const_none;
177}
178STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_pin_low_obj, pyb_pin_low);
179
180// pin.high()
181STATIC mp_obj_t pyb_pin_high(mp_obj_t self_in) {
182 pyb_pin_obj_t *self = self_in;
183 GPIO_OUTPUT_SET(self->phys_port, 1);
184 return mp_const_none;
185}
186STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_pin_high_obj, pyb_pin_high);
187
188STATIC const mp_map_elem_t pyb_pin_locals_dict_table[] = {
189 // instance methods
190 { MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&pyb_pin_init_obj },
191 { MP_OBJ_NEW_QSTR(MP_QSTR_value), (mp_obj_t)&pyb_pin_value_obj },
192 { MP_OBJ_NEW_QSTR(MP_QSTR_low), (mp_obj_t)&pyb_pin_low_obj },
193 { MP_OBJ_NEW_QSTR(MP_QSTR_high), (mp_obj_t)&pyb_pin_high_obj },
194
195 // class constants
196 { MP_OBJ_NEW_QSTR(MP_QSTR_IN), MP_OBJ_NEW_SMALL_INT(GPIO_MODE_INPUT) },
197 { MP_OBJ_NEW_QSTR(MP_QSTR_OUT_PP), MP_OBJ_NEW_SMALL_INT(GPIO_MODE_OUTPUT) },
198 { MP_OBJ_NEW_QSTR(MP_QSTR_PULL_NONE), MP_OBJ_NEW_SMALL_INT(GPIO_PULL_NONE) },
199 { MP_OBJ_NEW_QSTR(MP_QSTR_PULL_UP), MP_OBJ_NEW_SMALL_INT(GPIO_PULL_UP) },
Josef Gajdusek7d8edef2015-05-26 18:54:55 +0200200 //{ MP_OBJ_NEW_QSTR(MP_QSTR_PULL_DOWN), MP_OBJ_NEW_SMALL_INT(GPIO_PULL_DOWN) },
Damien George87c62502015-02-13 22:21:44 +0000201};
202
203STATIC MP_DEFINE_CONST_DICT(pyb_pin_locals_dict, pyb_pin_locals_dict_table);
204
205const mp_obj_type_t pyb_pin_type = {
206 { &mp_type_type },
207 .name = MP_QSTR_Pin,
208 .print = pyb_pin_print,
209 .make_new = pyb_pin_make_new,
210 .locals_dict = (mp_obj_t)&pyb_pin_locals_dict,
211};