-
Notifications
You must be signed in to change notification settings - Fork 1
/
gpio.c
319 lines (247 loc) · 7.71 KB
/
gpio.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* ************************************************************************** */
/** GPIO
@Company
ShimaneJohoshoriCenter.inc
@File Name
gpio.c
@Summary
GPIO class processing
@Description
mruby/c function army
*/
/* ************************************************************************** */
#include "pic32mx.h"
#include "gpio.h"
#include "mrubyc.h"
/* ================================ C codes ================================ */
/*! PIN handle setter
valが、ピン番号(数字)でもポート番号(e.g."B3")でも受け付ける。
@param pin_handle dist.
@param val src.
@retval 0 No error.
*/
int set_pin_handle( PIN_HANDLE *pin_handle, const mrbc_value *val )
{
switch( val->tt ) {
case MRBC_TT_INTEGER: {
int ch = mrbc_integer(*val);
if( ch <= 4 ) { // Rboard J9,J10,J11 mapping.
pin_handle->port = 1;
pin_handle->num = ch;
} else {
pin_handle->port = 2;
pin_handle->num = ch-5;
}
} break;
case MRBC_TT_STRING: {
const char *s = mrbc_string_cstr(val);
if( 'A' <= s[0] && s[0] <= 'G' ) {
pin_handle->port = s[0] - 'A' + 1;
} else if( 'a' <= s[0] && s[0] <= 'g' ) {
pin_handle->port = s[0] - 'a' + 1;
} else {
return -1;
}
pin_handle->num = mrbc_atoi( s+1, 10 );
} break;
default:
return -1;
}
return -(pin_handle->num < 0 || pin_handle->num > 15);
}
/*! set (change) mode
@param pin target pin.
@param mode mode. Sepcified by GPIO_* constant.
@return int zero is no error.
*/
int gpio_setmode( const PIN_HANDLE *pin, unsigned int mode )
{
if( mode & (GPIO_IN|GPIO_OUT|GPIO_ANALOG|GPIO_HIGH_Z) ) {
if( mode & GPIO_ANALOG ) {
ANSELxSET(pin->port) = (1 << pin->num);
} else {
ANSELxCLR(pin->port) = (1 << pin->num);
}
CNPUxCLR(pin->port) = (1 << pin->num);
CNPDxCLR(pin->port) = (1 << pin->num);
ODCxCLR(pin->port) = (1 << pin->num);
}
if( mode & GPIO_IN ) TRISxSET(pin->port) = (1 << pin->num);
if( mode & GPIO_OUT ) TRISxCLR(pin->port) = (1 << pin->num);
if( mode & GPIO_HIGH_Z ) return -1;
if( mode & GPIO_PULL_UP ) {
CNPDxCLR(pin->port) = (1 << pin->num);
CNPUxSET(pin->port) = (1 << pin->num);
}
if( mode & GPIO_PULL_DOWN ) {
CNPUxCLR(pin->port) = (1 << pin->num);
CNPDxSET(pin->port) = (1 << pin->num);
}
if( mode & GPIO_OPEN_DRAIN ) ODCxSET(pin->port) = (1 << pin->num);
return 0;
}
/* ============================= mruby/c codes ============================= */
/*! constructor
gpio1 = GPIO.new(pin, GPIO::IN )
*/
static void c_gpio_new(mrbc_vm *vm, mrbc_value v[], int argc)
{
v[0] = mrbc_instance_new(vm, v[0].cls, sizeof(PIN_HANDLE));
mrbc_instance_call_initialize( vm, v, argc );
}
/*! initializer
*/
static void c_gpio_initialize(mrbc_vm *vm, mrbc_value v[], int argc)
{
PIN_HANDLE *pin = (PIN_HANDLE *)v[0].instance->data;
if( argc != 2 ) goto ERROR_RETURN;
if( set_pin_handle( pin, &v[1] ) != 0 ) goto ERROR_RETURN;
if( (mrbc_integer(v[2]) & (GPIO_IN|GPIO_OUT|GPIO_HIGH_Z)) == 0 ) goto ERROR_RETURN;
if( gpio_setmode( pin, mrbc_integer(v[2]) ) < 0 ) goto ERROR_RETURN;
return;
ERROR_RETURN:
mrbc_raise(vm, MRBC_CLASS(ArgumentError), "GPIO initialize");
}
/*! setmode
GPIO.setmode( num, GPIO::IN ) # class method
gpio1.setmode( GPIO::PULL_UP ) # instance method
*/
static void c_gpio_setmode(mrbc_vm *vm, mrbc_value v[], int argc)
{
PIN_HANDLE pin;
if( v[0].tt == MRBC_TT_OBJECT ) goto INSTANCE_METHOD_MODE;
/*
Class method mode.
*/
if( argc != 2 ) goto ERROR_RETURN;
if( set_pin_handle( &pin, &v[1] ) != 0 ) goto ERROR_RETURN;
if( v[2].tt != MRBC_TT_INTEGER ) goto ERROR_RETURN;
if( gpio_setmode( &pin, mrbc_integer(v[2]) ) < 0 ) goto ERROR_RETURN;
return;
/*
Instance method mode.
*/
INSTANCE_METHOD_MODE:
pin = *(PIN_HANDLE *)v[0].instance->data;
if( v[1].tt != MRBC_TT_INTEGER ) goto ERROR_RETURN;
if( gpio_setmode( &pin, mrbc_integer(v[1]) ) < 0 ) goto ERROR_RETURN;
SET_NIL_RETURN();
return;
ERROR_RETURN:
mrbc_raise(vm, MRBC_CLASS(ArgumentError), "GPIO Can't setup");
}
/*! read_at -> Integer
v1 = GPIO.read_at( 1 ) # read from pin 1.
*/
static void c_gpio_read_at(mrbc_vm *vm, mrbc_value v[], int argc)
{
PIN_HANDLE pin;
if( set_pin_handle( &pin, &v[1] ) == 0 ) {
SET_INT_RETURN( (PORTx(pin.port) >> pin.num) & 1 );
} else {
SET_NIL_RETURN();
}
}
/*! high_at? -> bool
v1 = GPIO.read_at( 1 ) # read from pin 1.
*/
static void c_gpio_high_at(mrbc_vm *vm, mrbc_value v[], int argc)
{
PIN_HANDLE pin;
if( set_pin_handle( &pin, &v[1] ) == 0 ) {
SET_BOOL_RETURN( (PORTx(pin.port) >> pin.num) & 1 );
} else {
SET_NIL_RETURN();
}
}
/*! low_at? -> bool
v1 = GPIO.read_at( 1 ) # read from pin 1.
*/
static void c_gpio_low_at(mrbc_vm *vm, mrbc_value v[], int argc)
{
PIN_HANDLE pin;
if( set_pin_handle( &pin, &v[1] ) == 0 ) {
SET_BOOL_RETURN( ~(PORTx(pin.port) >> pin.num) & 1 );
} else {
SET_NIL_RETURN();
}
}
/*! write_at
v1 = GPIO.write_at( 1, 0 ) # output zero to pin 1.
*/
static void c_gpio_write_at(mrbc_vm *vm, mrbc_value v[], int argc)
{
PIN_HANDLE pin;
if( (set_pin_handle( &pin, &v[1] ) != 0) ||
(v[2].tt != MRBC_TT_INTEGER) ) {
mrbc_raise(vm, MRBC_CLASS(ArgumentError), 0);
return;
}
if( mrbc_integer(v[2]) == 0 ) {
LATxCLR(pin.port) = (1 << pin.num);
} else if( mrbc_integer(v[2]) == 1 ) {
LATxSET(pin.port) = (1 << pin.num);
} else {
mrbc_raise(vm, MRBC_CLASS(RangeError), 0);
}
}
/*! read
x = gpio1.read()
*/
static void c_gpio_read(mrbc_vm *vm, mrbc_value v[], int argc)
{
PIN_HANDLE *pin = (PIN_HANDLE *)v[0].instance->data;
SET_INT_RETURN( (PORTx(pin->port) >> pin->num) & 1 );
}
/*! high?
if gpio1.high?() ...
*/
static void c_gpio_high(mrbc_vm *vm, mrbc_value v[], int argc)
{
PIN_HANDLE *pin = (PIN_HANDLE *)v[0].instance->data;
SET_BOOL_RETURN( (PORTx(pin->port) >> pin->num) & 1 );
}
/*! low?
if gpio1.low?() ...
*/
static void c_gpio_low(mrbc_vm *vm, mrbc_value v[], int argc)
{
PIN_HANDLE *pin = (PIN_HANDLE *)v[0].instance->data;
SET_BOOL_RETURN( ~(PORTx(pin->port) >> pin->num) & 1 );
}
/*! write
gpio1.write( 0 or 1 )
*/
static void c_gpio_write(mrbc_vm *vm, mrbc_value v[], int argc)
{
PIN_HANDLE *pin = (PIN_HANDLE *)v[0].instance->data;
if( v[1].tt != MRBC_TT_INTEGER ) return;
if( mrbc_integer(v[1]) == 0 ) {
LATxCLR(pin->port) = (1 << pin->num);
} else {
LATxSET(pin->port) = (1 << pin->num);
}
}
/*! Initializer
*/
void mrbc_init_class_gpio( void )
{
mrbc_class *gpio = mrbc_define_class(0, "GPIO", 0);
mrbc_define_method(0, gpio, "new", c_gpio_new);
mrbc_define_method(0, gpio, "initialize", c_gpio_initialize);
mrbc_define_method(0, gpio, "setmode", c_gpio_setmode);
mrbc_define_method(0, gpio, "read_at", c_gpio_read_at);
mrbc_define_method(0, gpio, "high_at?", c_gpio_high_at);
mrbc_define_method(0, gpio, "low_at?", c_gpio_low_at);
mrbc_define_method(0, gpio, "write_at", c_gpio_write_at);
mrbc_define_method(0, gpio, "read", c_gpio_read);
mrbc_define_method(0, gpio, "high?", c_gpio_high);
mrbc_define_method(0, gpio, "low?", c_gpio_low);
mrbc_define_method(0, gpio, "write", c_gpio_write);
mrbc_set_class_const(gpio, mrbc_str_to_symid("IN"), &mrbc_integer_value(GPIO_IN));
mrbc_set_class_const(gpio, mrbc_str_to_symid("OUT"), &mrbc_integer_value(GPIO_OUT));
mrbc_set_class_const(gpio, mrbc_str_to_symid("HIGH_Z"), &mrbc_integer_value(GPIO_HIGH_Z));
mrbc_set_class_const(gpio, mrbc_str_to_symid("PULL_UP"), &mrbc_integer_value(GPIO_PULL_UP));
mrbc_set_class_const(gpio, mrbc_str_to_symid("PULL_DOWN"), &mrbc_integer_value(GPIO_PULL_DOWN));
mrbc_set_class_const(gpio, mrbc_str_to_symid("OPEN_DRAIN"), &mrbc_integer_value(GPIO_OPEN_DRAIN));
}