modpybpin.c 13.7 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 file is part of the Micro Python project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013, 2014, 2015 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.
 */

#include <stdio.h>
#include <stdint.h>
#include <string.h>

31
#include "etshal.h"
32
33
34
35
36
37
#include "c_types.h"
#include "user_interface.h"
#include "gpio.h"

#include "py/nlr.h"
#include "py/runtime.h"
38
#include "py/gc.h"
39
#include "modpyb.h"
40
41
42
43
44
45
46

#define GET_TRIGGER(phys_port) \
    GPIO_PIN_INT_TYPE_GET(GPIO_REG_READ(GPIO_PIN_ADDR(phys_port)))
#define SET_TRIGGER(phys_port, trig) \
    (GPIO_REG_WRITE(GPIO_PIN_ADDR(phys_port), \
        (GPIO_REG_READ(GPIO_PIN_ADDR(phys_port)) & ~GPIO_PIN_INT_TYPE_MASK) \
        | GPIO_PIN_INT_TYPE_SET(trig))) \
47
48
49

#define GPIO_MODE_INPUT (0)
#define GPIO_MODE_OUTPUT (1)
50
#define GPIO_MODE_OPEN_DRAIN (2) // synthesised
51
52
#define GPIO_PULL_NONE (0)
#define GPIO_PULL_UP (1)
53
54
// Removed in SDK 1.1.0
//#define GPIO_PULL_DOWN (2)
55

56
57
58
59
60
typedef struct _pin_irq_obj_t {
    mp_obj_base_t base;
    uint16_t phys_port;
} pin_irq_obj_t;

61
const pyb_pin_obj_t pyb_pin_obj[16 + 1] = {
62
63
64
65
66
67
    {{&pyb_pin_type}, 0, FUNC_GPIO0, PERIPHS_IO_MUX_GPIO0_U},
    {{&pyb_pin_type}, 1, FUNC_GPIO1, PERIPHS_IO_MUX_U0TXD_U},
    {{&pyb_pin_type}, 2, FUNC_GPIO2, PERIPHS_IO_MUX_GPIO2_U},
    {{&pyb_pin_type}, 3, FUNC_GPIO3, PERIPHS_IO_MUX_U0RXD_U},
    {{&pyb_pin_type}, 4, FUNC_GPIO4, PERIPHS_IO_MUX_GPIO4_U},
    {{&pyb_pin_type}, 5, FUNC_GPIO5, PERIPHS_IO_MUX_GPIO5_U},
68
69
70
    {{NULL}, 0, 0, 0},
    {{NULL}, 0, 0, 0},
    {{NULL}, 0, 0, 0},
71
72
    {{&pyb_pin_type}, 9, FUNC_GPIO9, PERIPHS_IO_MUX_SD_DATA2_U},
    {{&pyb_pin_type}, 10, FUNC_GPIO10, PERIPHS_IO_MUX_SD_DATA3_U},
73
    {{NULL}, 0, 0, 0},
74
75
76
77
    {{&pyb_pin_type}, 12, FUNC_GPIO12, PERIPHS_IO_MUX_MTDI_U},
    {{&pyb_pin_type}, 13, FUNC_GPIO13, PERIPHS_IO_MUX_MTCK_U},
    {{&pyb_pin_type}, 14, FUNC_GPIO14, PERIPHS_IO_MUX_MTMS_U},
    {{&pyb_pin_type}, 15, FUNC_GPIO15, PERIPHS_IO_MUX_MTDO_U},
78
79
    // GPIO16 is special, belongs to different register set, and
    // otherwise handled specially.
80
    {{&pyb_pin_type}, 16, -1, -1},
81
82
};

83
STATIC uint8_t pin_mode[16 + 1];
84

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// forward declaration
STATIC const pin_irq_obj_t pin_irq_obj[16];

void pin_init0(void) {
    ETS_GPIO_INTR_DISABLE();
    ETS_GPIO_INTR_ATTACH(pin_intr_handler_iram, NULL);
    // disable all interrupts
    memset(&MP_STATE_PORT(pin_irq_handler)[0], 0, 16 * sizeof(mp_obj_t));
    for (int p = 0; p < 16; ++p) {
        GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, 1 << p);
        SET_TRIGGER(p, 0);
    }
    ETS_GPIO_INTR_ENABLE();
}

void pin_intr_handler(uint32_t status) {
    gc_lock();
    status &= 0xffff;
    for (int p = 0; status; ++p, status >>= 1) {
        if (status & 1) {
            mp_obj_t handler = MP_STATE_PORT(pin_irq_handler)[p];
            if (handler != MP_OBJ_NULL) {
107
                mp_call_function_1_protected(handler, MP_OBJ_FROM_PTR(&pyb_pin_obj[p]));
108
109
110
111
112
113
            }
        }
    }
    gc_unlock();
}

114
pyb_pin_obj_t *mp_obj_get_pin_obj(mp_obj_t pin_in) {
115
116
117
118
    if (mp_obj_get_type(pin_in) != &pyb_pin_type) {
        nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "expecting a pin"));
    }
    pyb_pin_obj_t *self = pin_in;
119
120
121
122
123
    return self;
}

uint mp_obj_get_pin(mp_obj_t pin_in) {
    return mp_obj_get_pin_obj(pin_in)->phys_port;
124
125
126
}

int pin_get(uint pin) {
127
128
129
    if (pin == 16) {
        return READ_PERI_REG(RTC_GPIO_IN_DATA) & 1;
    }
130
131
132
133
    return GPIO_INPUT_GET(pin);
}

void pin_set(uint pin, int value) {
134
135
136
137
138
139
140
141
142
    if (pin == 16) {
        int out_en = (pin_mode[pin] == GPIO_MODE_OUTPUT);
        WRITE_PERI_REG(PAD_XPD_DCDC_CONF, (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | 1);
        WRITE_PERI_REG(RTC_GPIO_CONF, READ_PERI_REG(RTC_GPIO_CONF) & ~1);
        WRITE_PERI_REG(RTC_GPIO_ENABLE, (READ_PERI_REG(RTC_GPIO_ENABLE) & ~1) | out_en);
        WRITE_PERI_REG(RTC_GPIO_OUT, (READ_PERI_REG(RTC_GPIO_OUT) & ~1) | value);
        return;
    }

143
144
    uint32_t enable = 0;
    uint32_t disable = 0;
145
    switch (pin_mode[pin]) {
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
        case GPIO_MODE_INPUT:
            value = -1;
            disable = 1;
            break;

        case GPIO_MODE_OUTPUT:
            enable = 1;
            break;

        case GPIO_MODE_OPEN_DRAIN:
            if (value == -1) {
                return;
            } else if (value == 0) {
                enable = 1;
            } else {
                value = -1;
                disable = 1;
            }
            break;
    }

    enable <<= pin;
    disable <<= pin;
    if (value == -1) {
        gpio_output_set(0, 0, enable, disable);
    } else {
        gpio_output_set(value << pin, (1 - value) << pin, enable, disable);
    }
}

176
STATIC void pyb_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
177
178
179
    pyb_pin_obj_t *self = self_in;

    // pin name
180
    mp_printf(print, "Pin(%u)", self->phys_port);
181
182
183
184
}

// pin.init(mode, pull=Pin.PULL_NONE, af=-1)
STATIC 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) {
185
    enum { ARG_mode, ARG_pull, ARG_value };
186
187
    static const mp_arg_t allowed_args[] = {
        { MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT },
188
189
        { MP_QSTR_pull, MP_ARG_INT, {.u_int = GPIO_PULL_NONE}},
        { MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}},
190
191
192
    };

    // parse args
193
194
    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
    mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
195
196

    // get io mode
197
    uint mode = args[ARG_mode].u_int;
198
199

    // get pull mode
200
    uint pull = args[ARG_pull].u_int;
201
202
203

    // get initial value
    int value;
204
    if (args[ARG_value].u_obj == MP_OBJ_NULL) {
205
206
        value = -1;
    } else {
207
        value = mp_obj_is_true(args[ARG_value].u_obj);
208
209
210
211
    }

    // save the mode
    pin_mode[self->phys_port] = mode;
212
213

    // configure the GPIO as requested
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
    if (self->phys_port == 16) {
        // TODO: Set pull up/pull down
    } else {
        PIN_FUNC_SELECT(self->periph, self->func);
        #if 0
        // Removed in SDK 1.1.0
        if ((pull & GPIO_PULL_DOWN) == 0) {
            PIN_PULLDWN_DIS(self->periph);
        }
        #endif
        if ((pull & GPIO_PULL_UP) == 0) {
            PIN_PULLUP_DIS(self->periph);
        }
        #if 0
        if ((pull & GPIO_PULL_DOWN) != 0) {
            PIN_PULLDWN_EN(self->periph);
        }
        #endif
        if ((pull & GPIO_PULL_UP) != 0) {
            PIN_PULLUP_EN(self->periph);
        }
235
236
    }

237
    pin_set(self->phys_port, value);
238
239
240
241
242

    return mp_const_none;
}

// constructor(id, ...)
243
STATIC 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) {
244
245
    mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);

246
    // get the wanted pin object
247
248
    int wanted_pin = mp_obj_get_int(args[0]);
    pyb_pin_obj_t *pin = NULL;
249
250
    if (0 <= wanted_pin && wanted_pin < MP_ARRAY_SIZE(pyb_pin_obj)) {
        pin = (pyb_pin_obj_t*)&pyb_pin_obj[wanted_pin];
251
    }
252
    if (pin == NULL || pin->base.type == NULL) {
253
254
255
256
257
258
259
260
261
262
263
264
265
        nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid pin"));
    }

    if (n_args > 1 || n_kw > 0) {
        // pin mode given, so configure this GPIO
        mp_map_t kw_args;
        mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
        pyb_pin_obj_init_helper(pin, n_args - 1, args + 1, &kw_args);
    }

    return (mp_obj_t)pin;
}

266
267
268
269
270
271
272
273
274
// fast method for getting/setting pin value
STATIC mp_obj_t pyb_pin_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
    mp_arg_check_num(n_args, n_kw, 0, 1, false);
    pyb_pin_obj_t *self = self_in;
    if (n_args == 0) {
        // get pin
        return MP_OBJ_NEW_SMALL_INT(GPIO_INPUT_GET(self->phys_port));
    } else {
        // set pin
275
        pin_set(self->phys_port, mp_obj_is_true(args[0]));
276
277
278
279
        return mp_const_none;
    }
}

280
281
282
283
284
285
286
287
// pin.init(mode, pull)
STATIC mp_obj_t pyb_pin_obj_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
    return pyb_pin_obj_init_helper(args[0], n_args - 1, args + 1, kw_args);
}
MP_DEFINE_CONST_FUN_OBJ_KW(pyb_pin_init_obj, 1, pyb_pin_obj_init);

// pin.value([value])
STATIC mp_obj_t pyb_pin_value(mp_uint_t n_args, const mp_obj_t *args) {
288
    return pyb_pin_call(args[0], n_args - 1, 0, args + 1);
289
290
291
292
293
294
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_pin_value_obj, 1, 2, pyb_pin_value);

// pin.low()
STATIC mp_obj_t pyb_pin_low(mp_obj_t self_in) {
    pyb_pin_obj_t *self = self_in;
295
    pin_set(self->phys_port, 0);
296
297
298
299
300
301
302
    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_pin_low_obj, pyb_pin_low);

// pin.high()
STATIC mp_obj_t pyb_pin_high(mp_obj_t self_in) {
    pyb_pin_obj_t *self = self_in;
303
    pin_set(self->phys_port, 1);
304
305
306
307
    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_pin_high_obj, pyb_pin_high);

308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// pin.irq()
STATIC mp_obj_t pyb_pin_irq(size_t n_args, const mp_obj_t *args) {
    pyb_pin_obj_t *self = MP_OBJ_TO_PTR(args[0]);
    if (self->phys_port >= 16) {
        nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "pin does not have IRQ capabilities"));
    }

    if (n_args > 1) {
        // configure irq
        int trig = mp_obj_get_int(args[1]);
        mp_obj_t handler = args[2];
        if (handler == mp_const_none) {
            handler = MP_OBJ_NULL;
        }
        ETS_GPIO_INTR_DISABLE();
        MP_STATE_PORT(pin_irq_handler)[self->phys_port] = handler;
        SET_TRIGGER(self->phys_port, trig);
        GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, 1 << self->phys_port);
        ETS_GPIO_INTR_ENABLE();
    }

    // return the irq object
    return MP_OBJ_FROM_PTR(&pin_irq_obj[self->phys_port]);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_pin_irq_obj, 1, 3, pyb_pin_irq);

334
335
336
337
338
339
STATIC const mp_map_elem_t pyb_pin_locals_dict_table[] = {
    // instance methods
    { MP_OBJ_NEW_QSTR(MP_QSTR_init),    (mp_obj_t)&pyb_pin_init_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_value),   (mp_obj_t)&pyb_pin_value_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_low),     (mp_obj_t)&pyb_pin_low_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_high),    (mp_obj_t)&pyb_pin_high_obj },
340
    { MP_OBJ_NEW_QSTR(MP_QSTR_irq),     (mp_obj_t)&pyb_pin_irq_obj },
341
342
343

    // class constants
    { MP_OBJ_NEW_QSTR(MP_QSTR_IN),        MP_OBJ_NEW_SMALL_INT(GPIO_MODE_INPUT) },
344
345
    { MP_OBJ_NEW_QSTR(MP_QSTR_OUT),       MP_OBJ_NEW_SMALL_INT(GPIO_MODE_OUTPUT) },
    { MP_OBJ_NEW_QSTR(MP_QSTR_OPEN_DRAIN), MP_OBJ_NEW_SMALL_INT(GPIO_MODE_OPEN_DRAIN) },
346
347
    { MP_OBJ_NEW_QSTR(MP_QSTR_PULL_NONE), MP_OBJ_NEW_SMALL_INT(GPIO_PULL_NONE) },
    { MP_OBJ_NEW_QSTR(MP_QSTR_PULL_UP),   MP_OBJ_NEW_SMALL_INT(GPIO_PULL_UP) },
348
    //{ MP_OBJ_NEW_QSTR(MP_QSTR_PULL_DOWN), MP_OBJ_NEW_SMALL_INT(GPIO_PULL_DOWN) },
349
350
351
352

    // IRG triggers, can be or'd together
    { MP_OBJ_NEW_QSTR(MP_QSTR_IRQ_RISING), MP_OBJ_NEW_SMALL_INT(GPIO_PIN_INTR_POSEDGE) },
    { MP_OBJ_NEW_QSTR(MP_QSTR_IRQ_FALLING), MP_OBJ_NEW_SMALL_INT(GPIO_PIN_INTR_NEGEDGE) },
353
354
355
356
357
358
359
360
361
};

STATIC MP_DEFINE_CONST_DICT(pyb_pin_locals_dict, pyb_pin_locals_dict_table);

const mp_obj_type_t pyb_pin_type = {
    { &mp_type_type },
    .name = MP_QSTR_Pin,
    .print = pyb_pin_print,
    .make_new = pyb_pin_make_new,
362
    .call = pyb_pin_call,
363
364
    .locals_dict = (mp_obj_t)&pyb_pin_locals_dict,
};
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420

/******************************************************************************/
// Pin IRQ object

STATIC const mp_obj_type_t pin_irq_type;

STATIC const pin_irq_obj_t pin_irq_obj[16] = {
    {{&pin_irq_type}, 0},
    {{&pin_irq_type}, 1},
    {{&pin_irq_type}, 2},
    {{&pin_irq_type}, 3},
    {{&pin_irq_type}, 4},
    {{&pin_irq_type}, 5},
    {{&pin_irq_type}, 6},
    {{&pin_irq_type}, 7},
    {{&pin_irq_type}, 8},
    {{&pin_irq_type}, 9},
    {{&pin_irq_type}, 10},
    {{&pin_irq_type}, 11},
    {{&pin_irq_type}, 12},
    {{&pin_irq_type}, 13},
    {{&pin_irq_type}, 14},
    {{&pin_irq_type}, 15},
};

STATIC mp_obj_t pin_irq_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
    pin_irq_obj_t *self = self_in;
    mp_arg_check_num(n_args, n_kw, 0, 0, false);
    pin_intr_handler(1 << self->phys_port);
    return mp_const_none;
}

STATIC mp_obj_t pin_irq_trigger(size_t n_args, const mp_obj_t *args) {
    pin_irq_obj_t *self = args[0];
    uint32_t orig_trig = GET_TRIGGER(self->phys_port);
    if (n_args == 2) {
        // set trigger
        SET_TRIGGER(self->phys_port, mp_obj_get_int(args[1]));
    }
    // return original trigger value
    return MP_OBJ_NEW_SMALL_INT(orig_trig);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pin_irq_trigger_obj, 1, 2, pin_irq_trigger);

STATIC const mp_rom_map_elem_t pin_irq_locals_dict_table[] = {
    { MP_ROM_QSTR(MP_QSTR_trigger),  MP_ROM_PTR(&pin_irq_trigger_obj) },
};

STATIC MP_DEFINE_CONST_DICT(pin_irq_locals_dict, pin_irq_locals_dict_table);

STATIC const mp_obj_type_t pin_irq_type = {
    { &mp_type_type },
    .name = MP_QSTR_IRQ,
    .call = pin_irq_call,
    .locals_dict = (mp_obj_dict_t*)&pin_irq_locals_dict,
};