forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 171
/
machine_pin_cyw43.c
86 lines (76 loc) · 2.97 KB
/
machine_pin_cyw43.c
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016-2022 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 <string.h>
#include "py/runtime.h"
#include "py/mphal.h"
#if defined(MICROPY_PY_NETWORK_CYW43) && defined(MICROPY_HW_PIN_EXT_COUNT)
#include "modmachine.h"
#include "machine_pin.h"
#include "lib/cyw43-driver/src/cyw43.h"
void machine_pin_ext_init(void) {
}
void machine_pin_ext_set(machine_pin_obj_t *self, bool value) {
if (value != self->last_output_value || !self->is_output) {
cyw43_gpio_set(&cyw43_state, self->id, value);
}
self->last_output_value = value;
}
bool machine_pin_ext_get(machine_pin_obj_t *self) {
bool value = false;
cyw43_gpio_get(&cyw43_state, self->id, &value);
return value;
}
void machine_pin_ext_config(machine_pin_obj_t *self, int mode, int value) {
if (mode == MACHINE_PIN_MODE_IN) {
if (self->is_output) {
// TODO need to disable output
}
self->is_output = false;
} else if (mode == MACHINE_PIN_MODE_OUT) {
if (!self->is_output) {
// TODO need to enable output
// for now we just set the value
if (value == -1) {
value = self->last_output_value;
}
self->last_output_value = !self->last_output_value; // defeat shortcircuit
machine_pin_ext_set(self, value);
self->is_output = true;
}
} else {
mp_raise_ValueError("only Pin.OUT and Pin.IN are supported for this pin");
}
if (value != -1) {
if (self->is_output) {
machine_pin_ext_set(self, value);
} else {
// figure if you pass a value to IN it should still remember it (this is what regular GPIO does)
self->last_output_value = value;
}
}
}
#endif // defined(MICROPY_PY_NETWORK_CYW43) && defined(MICROPY_HW_PIN_EXT_COUNT)