-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpio.cpp
More file actions
591 lines (494 loc) · 15.2 KB
/
pio.cpp
File metadata and controls
591 lines (494 loc) · 15.2 KB
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/**************************************************************************
* Copyright (C), AirM2M Tech. Co., Ltd.
*
* Name: pio.cpp
* Author: panjun
* Version: V0.1
* Date: 2016/09/13
*
* Description:
* Register all functions of 'pio' into LUA's stack.
* History:
* panjun 16/09/13 Initially create file.
**************************************************************************/
#include "stdafx.h"
#include "lua.hpp"
#include "platform.h"
#include "platform_conf.h"
#ifdef AM_SLI3108_SUPPORT
#include "platform_SLI3108.h"
#endif //AM_SLI3108_SUPPORT
#if defined(TOUCH_PANEL_SUPPORT)
#include "sim_tp.h"
#endif //TOUCH_PANEL_SUPPORT
#include "pio.h"
// PIO public constants
#define PIO_DIR_OUTPUT 0
#define PIO_DIR_INPUT 1
#define PIO_DIR_OUTPUT1 2
// PIO private constants
#define PIO_PORT_OP 0
#define PIO_PIN_OP 1
// Local operation masks for all the ports
static pio_type pio_masks[ PLATFORM_IO_PORTS ];
// ****************************************************************************
// Generic helper functions
// Helper function: clear all masks
static void pioh_clear_masks()
{
int i;
for( i = 0; i < PLATFORM_IO_PORTS; i ++ )
pio_masks[ i ] = 0;
}
// Helper function: pin operations
// Gets the stack index of the first pin and the operation
static int pioh_set_pins( lua_State* L, int stackidx, int op )
{
int total = lua_gettop( L );
int i, v, port, pin;
pioh_clear_masks();
// Get all masks
for( i = stackidx; i <= total; i ++ )
{
v = luaL_checkinteger( L, i );
port = PLATFORM_IO_GET_PORT( v );
pin = PLATFORM_IO_GET_PIN( v );
if( PLATFORM_IO_IS_PORT( v ) || !platform_pio_has_port( port ) || !platform_pio_has_pin( port, pin ) )
return luaL_error( L, "invalid pin" );
pio_masks[ port ] |= 1 << pin;
}
// Ask platform to execute the given operation
for( i = 0; i < PLATFORM_IO_PORTS; i ++ )
if( pio_masks[ i ] )
if( !platform_pio_op( i, pio_masks[ i ], op ) )
return luaL_error( L, "invalid PIO operation" );
return 0;
}
// Helper function: port operations
// Gets the stack index of the first port and the operation (also the mask)
static int pioh_set_ports( lua_State* L, int stackidx, int op, pio_type mask )
{
int total = lua_gettop( L );
int i, v, port;
UINT32 port_mask = 0;
// Get all masks
for( i = stackidx; i <= total; i ++ )
{
v = luaL_checkinteger( L, i );
port = PLATFORM_IO_GET_PORT( v );
if( !PLATFORM_IO_IS_PORT( v ) || !platform_pio_has_port( port ) )
return luaL_error( L, "invalid port" );
port_mask |= ( 1UL << port );
}
// Ask platform to execute the given operation
for( i = 0; i < PLATFORM_IO_PORTS; i ++ )
if( port_mask & ( 1UL << i ) )
if( !platform_pio_op( i, mask, op ) )
return luaL_error( L, "invalid PIO operation" );
return 0;
}
// ****************************************************************************
// Pin/port helper functions
static int pio_gen_setdir( lua_State *L, int optype )
{
int op = luaL_checkinteger( L, 1 );
if( op == PIO_DIR_INPUT )
op = optype == PIO_PIN_OP ? PLATFORM_IO_PIN_DIR_INPUT : PLATFORM_IO_PORT_DIR_INPUT;
else if( op == PIO_DIR_OUTPUT )
op = optype == PIO_PIN_OP ? PLATFORM_IO_PIN_DIR_OUTPUT : PLATFORM_IO_PORT_DIR_OUTPUT;
else if( op == PIO_DIR_OUTPUT1 )
op = optype == PIO_PIN_OP ? PLATFORM_IO_PIN_DIR_OUTPUT1 : PLATFORM_IO_PORT_DIR_OUTPUT;
else if(!(optype == PIO_PIN_OP && op == PLATFORM_IO_PIN_DIR_INT))
return luaL_error( L, "invalid direction" );
if( optype == PIO_PIN_OP )
pioh_set_pins( L, 2, op );
else
pioh_set_ports( L, 2, op, PLATFORM_IO_ALL_PINS );
return 0;
}
static int pio_gen_setpull( lua_State *L, int optype )
{
int op = luaL_checkinteger( L, 1 );
if( ( op != PLATFORM_IO_PIN_PULLUP ) &&
( op != PLATFORM_IO_PIN_PULLDOWN ) &&
( op != PLATFORM_IO_PIN_NOPULL ) )
return luaL_error( L, "invalid pull type" );
if( optype == PIO_PIN_OP )
pioh_set_pins( L, 2, op );
else
pioh_set_ports( L, 2, op, PLATFORM_IO_ALL_PINS );
return 0;
}
static int pio_gen_setval( lua_State *L, int optype, pio_type val, int stackidx )
{
if( ( optype == PIO_PIN_OP ) && ( val != 1 ) && ( val != 0 ) )
return luaL_error( L, "invalid pin value" );
if( optype == PIO_PIN_OP )
pioh_set_pins( L, stackidx, val == 1 ? PLATFORM_IO_PIN_SET : PLATFORM_IO_PIN_CLEAR );
else
pioh_set_ports( L, stackidx, PLATFORM_IO_PORT_SET_VALUE, val );
return 0;
}
// ****************************************************************************
// Pin operations
static int pio_pin_close( lua_State *L)
{
int total = lua_gettop( L );
int i, v, port, pin;
pioh_clear_masks();
// Get all masks
for( i = 1; i <= total; i ++ )
{
v = luaL_checkinteger( L, i );
port = PLATFORM_IO_GET_PORT( v );
pin = PLATFORM_IO_GET_PIN( v );
if( PLATFORM_IO_IS_PORT( v ) || !platform_pio_has_port( port ) || !platform_pio_has_pin( port, pin ) )
return luaL_error( L, "invalid pin" );
pio_masks[ port ] |= 1 << pin;
}
// Ask platform to execute the given operation
for( i = 0; i < PLATFORM_IO_PORTS; i ++ )
if( pio_masks[ i ] )
if( !platform_pio_op( i, pio_masks[ i ], PLATFORM_IO_PIN_CLOSE ) )
return luaL_error( L, "invalid PIO operation" );
return 0;
}
// Lua: pio.pin.setdir( pio.INPUT | pio.OUTPUT, pin1, pin2, ..., pinn )
static int pio_pin_setdir( lua_State *L )
{
return pio_gen_setdir( L, PIO_PIN_OP );
}
// Lua: pio.pin.setpull( pio.PULLUP | pio.PULLDOWN | pio.NOPULL, pin1, pin2, ..., pinn )
static int pio_pin_setpull( lua_State *L )
{
return pio_gen_setpull( L, PIO_PIN_OP );
}
// Lua: pio.pin.setval( 0|1, pin1, pin2, ..., pinn )
static int pio_pin_setval( lua_State *L )
{
pio_type val = ( pio_type )luaL_checkinteger( L, 1 );
return pio_gen_setval( L, PIO_PIN_OP, val, 2 );
}
// Lua: pio.pin.sethigh( pin1, pin2, ..., pinn )
static int pio_pin_sethigh( lua_State *L )
{
return pio_gen_setval( L, PIO_PIN_OP, 1, 1 );
}
// Lua: pio.pin.setlow( pin1, pin2, ..., pinn )
static int pio_pin_setlow( lua_State *L )
{
return pio_gen_setval( L, PIO_PIN_OP, 0, 1 );
}
// Lua: pin1, pin2, ..., pinn = pio.pin.getval( pin1, pin2, ..., pinn )
static int pio_pin_getval( lua_State *L )
{
pio_type value;
int v, i, port, pin;
int total = lua_gettop( L );
for( i = 1; i <= total; i ++ )
{
v = luaL_checkinteger( L, i );
port = PLATFORM_IO_GET_PORT( v );
pin = PLATFORM_IO_GET_PIN( v );
if( PLATFORM_IO_IS_PORT( v ) || !platform_pio_has_port( port ) || !platform_pio_has_pin( port, pin ) )
return luaL_error( L, "invalid pin" );
else
{
value = platform_pio_op( port, 1 << pin, PLATFORM_IO_PIN_GET );
if(value == 0xff)
{
return luaL_error( L, "pin.getval failed for port(%d) pin(%d)", port, pin );
}
lua_pushinteger( L, value );
}
}
return total;
}
// ****************************************************************************
// Port operations
// Lua: pio.port.setdir( pio.INPUT | pio.OUTPUT, port1, port2, ..., portn )
static int pio_port_setdir( lua_State *L )
{
return pio_gen_setdir( L, PIO_PORT_OP );
}
// Lua: pio.port.setpull( pio.PULLUP | pio.PULLDOWN | pio.NOPULL, port1, port2, ..., portn )
static int pio_port_setpull( lua_State *L )
{
return pio_gen_setpull( L, PIO_PORT_OP );
}
// Lua: pio.port.setval( value, port1, port2, ..., portn )
static int pio_port_setval( lua_State *L )
{
pio_type val = ( pio_type )luaL_checkinteger( L, 1 );
return pio_gen_setval( L, PIO_PORT_OP, val, 2 );
}
// Lua: pio.port.sethigh( port1, port2, ..., portn )
static int pio_port_sethigh( lua_State *L )
{
return pio_gen_setval( L, PIO_PORT_OP, PLATFORM_IO_ALL_PINS, 1 );
}
// Lua: pio.port.setlow( port1, port2, ..., portn )
static int pio_port_setlow( lua_State *L )
{
return pio_gen_setval( L, PIO_PORT_OP, 0, 1 );
}
// Lua: val1, val2, ..., valn = pio.port.getval( port1, port2, ..., portn )
static int pio_port_getval( lua_State *L )
{
pio_type value;
int v, i, port;
int total = lua_gettop( L );
for( i = 1; i <= total; i ++ )
{
v = luaL_checkinteger( L, i );
port = PLATFORM_IO_GET_PORT( v );
if( !PLATFORM_IO_IS_PORT( v ) || !platform_pio_has_port( port ) )
return luaL_error( L, "invalid port" );
else
{
value = platform_pio_op( port, PLATFORM_IO_ALL_PINS, PLATFORM_IO_PORT_GET_VALUE );
lua_pushinteger( L, value );
}
}
return total;
}
// ****************************************************************************
// The __index metamethod will return pin/port numeric identifiers
static int pio_mt_index( lua_State* L )
{
const char *key = luaL_checkstring( L ,2 );
int port = 0xFFFF, pin = 0xFFFF, isport = 0, sz;
if( !key || *key != 'P' )
return 0;
if( isupper( key[ 1 ] ) ) // PA, PB, ...
{
if( PIO_PREFIX != 'A' )
return 0;
port = key[ 1 ] - 'A';
if( key[ 2 ] == '\0' )
isport = 1;
else if( key[ 2 ] == '_' )
if( sscanf( key + 3, "%d%n", &pin, &sz ) != 1 || sz != strlen( key ) - 3 )
return 0;
}
else // P0, P1, ...
{
if( PIO_PREFIX != '0' )
return 0;
if( !strchr( key, '_' ) ) // parse port
{
if( sscanf( key + 1, "%d%n", &port, &sz ) != 1 || sz != strlen( key ) - 1 )
return 0;
isport = 1;
}
else // parse port_pin
if( sscanf( key + 1, "%d_%d%n", &port, &pin, &sz ) != 2 || sz != strlen( key ) - 1 )
return 0;
}
sz = -1;
if( isport )
{
if( platform_pio_has_port( port ) )
sz = PLATFORM_IO_ENCODE( port, 0, 1 );
}
else
{
if( platform_pio_has_port( port ) && platform_pio_has_pin( port, pin ) )
sz = PLATFORM_IO_ENCODE( port, pin, 0 );
}
if( sz == -1 )
return 0;
else
{
lua_pushinteger( L, sz );
return 1;
}
}
// *****************************************************************************
// The 'decode' functions returns a port/pin pair from a platform code
static int pio_decode( lua_State *L )
{
int code = ( int )luaL_checkinteger( L, 1 );
lua_pushinteger( L, PLATFORM_IO_GET_PORT( code ) );
lua_pushinteger( L, PLATFORM_IO_GET_PIN( code ) );
return 2;
}
#ifdef AM_AW9523B_GPIO_CHIP_SUPPORT
static int pio_aw9523b_pin_display( lua_State *L )
{
u8 arg_index = 1;
u16 slave_addr = I2C_NULL_SLAVE_ADDR;
u8 regAddr;
u32 wrote = 0;
u8 numdata1 = (u8) luaL_checkinteger(L, arg_index++);
u8 numdata2 = (u8) luaL_checkinteger(L, arg_index++);
u8 numdata3 = (u8) luaL_checkinteger(L, arg_index);
platform_AW9523B_display(numdata1, numdata2, numdata3);
return 0;
}
static int pio_aw9523b_pin_setval( lua_State *L )
{
u8 arg_index = 1;
u8 pin_num;
u8 value;
pin_num = (u8)luaL_checkinteger(L, arg_index++);
value = (u8)luaL_checkinteger(L, arg_index++);
platform_AW9523B_set_gpio(pin_num, value);
return 0;
}
#endif
#ifdef AM_SLI3108_SUPPORT
static int pio_sli3108_setworkmode( lua_State *L )
{
UINT8 arg_index = 1;
UINT8 work_mode;
work_mode = (UINT8)luaL_checkinteger(L, 1);
platform_SLI3108_set_work_mode((BOOL)work_mode);
return 0;
}
static int pio_sli3108_open( lua_State *L )
{
LogWriter::LOGX("pio_sli3108_open");
platform_SLI3108_open();
return 0;
}
static int pio_sli3108_close( lua_State *L )
{
LogWriter::LOGX("pio_sli3108_close");
platform_SLI3108_close();
return 0;
}
#endif
#if defined(TOUCH_PANEL_SUPPORT)
static int pio_tp_sleep_in( lua_State *L )
{
platform_tp_sleep_in();
return 0;
}
static int pio_tp_sleep_out( lua_State *L )
{
platform_tp_sleep_out();
return 0;
}
#endif
// *****************************************************************************
// Pin function map
#define MIN_OPT_LEVEL 2
static const luaL_Reg pio_pin_map[] =
{
{ "setdir", pio_pin_setdir},
{ "setpull", pio_pin_setpull},
{ "setval", pio_pin_setval},
{ "sethigh", pio_pin_sethigh},
{ "setlow", pio_pin_setlow},
{ "getval", pio_pin_getval},
{ "close", pio_pin_close},
{ NULL, NULL }
};
static const luaL_Reg pio_port_map[] =
{
{ "setdir", pio_port_setdir},
{ "setpull", pio_port_setpull},
{ "setval", pio_port_setval},
{ "sethigh", pio_port_sethigh},
{ "setlow", pio_port_setlow},
{ "getval", pio_port_getval},
{ NULL, NULL }
};
#ifdef AM_AW9523B_GPIO_CHIP_SUPPORT
static const LUA_REG_TYPE pio_aw9523b_pin_map[] =
{
{ LSTRKEY( "display" ), LFUNCVAL( pio_aw9523b_pin_display ) },
{ LSTRKEY( "setpinval" ), LFUNCVAL( pio_aw9523b_pin_setval ) },
{ LNILKEY, LNILVAL }
};
#endif
#ifdef AM_SLI3108_SUPPORT
static const luaL_Reg pio_sli3108_pin_map[] =
{
{"setworkmode", pio_sli3108_setworkmode},
{"open", pio_sli3108_open},
{"close", pio_sli3108_close},
{NULL, NULL}
};
#endif
#if defined(TOUCH_PANEL_SUPPORT)
static const luaL_Reg pio_tp_map[] =
{
{"sleepin", pio_tp_sleep_in},
{"sleepout", pio_tp_sleep_out},
{NULL, NULL}
};
#endif
const luaL_Reg pio_map[] =
{
#if LUA_OPTIMIZE_MEMORY > 0
{ LSTRKEY( "pin" ), LROVAL( pio_pin_map ) },
{ LSTRKEY( "port" ), LROVAL( pio_port_map ) },
#ifdef AM_AW9523B_GPIO_CHIP_SUPPORT
{ LSTRKEY( "aw9523b_pin" ), LROVAL( pio_aw9523b_pin_map ) },
#endif
#ifdef AM_SLI3108_SUPPORT
{ LSTRKEY( "sli3108" ), LROVAL( pio_sli3108_pin_map ) },
#endif
#if defined(TOUCH_PANEL_SUPPORT)
{ LSTRKEY( "tp" ), LROVAL( pio_tp_map ) },
#endif
{ LSTRKEY( "decode" ), LFUNCVAL( pio_decode ) },
{ LSTRKEY( "INPUT" ), LNUMVAL( PIO_DIR_INPUT ) },
{ LSTRKEY( "OUTPUT" ), LNUMVAL( PIO_DIR_OUTPUT ) },
{ LSTRKEY( "PULLUP" ), LNUMVAL( PLATFORM_IO_PIN_PULLUP ) },
{ LSTRKEY( "PULLDOWN" ), LNUMVAL( PLATFORM_IO_PIN_PULLDOWN ) },
{ LSTRKEY( "NOPULL" ), LNUMVAL( PLATFORM_IO_PIN_NOPULL ) },
{ LSTRKEY( "__metatable" ), LROVAL( pio_map ) },
#endif
{ "decode", pio_decode},
{ "__index", pio_mt_index},
{ NULL, NULL }
};
LUALIB_API int luaopen_pio( lua_State *L )
{
#if LUA_OPTIMIZE_MEMORY > 0
return 0;
#else // #if LUA_OPTIMIZE_MEMORY > 0
luaL_register( L, AUXLIB_PIO, pio_map );
// Set it as its own metatable
lua_pushvalue( L, -1 );
lua_setmetatable( L, -2 );
// Set constants for direction/pullups
MOD_REG_NUMBER( L, "INPUT", PIO_DIR_INPUT );
MOD_REG_NUMBER( L, "OUTPUT", PIO_DIR_OUTPUT );
MOD_REG_NUMBER( L, "OUTPUT1", PIO_DIR_OUTPUT1 );
MOD_REG_NUMBER( L, "INT", PLATFORM_IO_PIN_DIR_INT );
MOD_REG_NUMBER( L, "PULLUP", PLATFORM_IO_PIN_PULLUP );
MOD_REG_NUMBER( L, "PULLDOWN", PLATFORM_IO_PIN_PULLDOWN );
MOD_REG_NUMBER( L, "NOPULL", PLATFORM_IO_PIN_NOPULL );
MOD_REG_NUMBER( L, "NORMAL_MODE", 0 );
MOD_REG_NUMBER( L, "FACTORY_MODE", 1 );
// Setup the new tables (pin and port) inside pio
lua_newtable( L );
luaL_register( L, NULL, pio_pin_map );
lua_setfield( L, -2, "pin" );
lua_newtable( L );
luaL_register( L, NULL, pio_port_map );
lua_setfield( L, -2, "port" );
#ifdef AM_AW9523B_GPIO_CHIP_SUPPORT
lua_newtable( L );
luaL_register( L, NULL, pio_aw9523b_pin_map );
lua_setfield( L, -2, "aw9523b_pin" );
platform_AW9523B_init();
#endif
#if defined(TOUCH_PANEL_SUPPORT)
lua_newtable( L );
luaL_register( L, NULL, pio_tp_map );
lua_setfield( L, -2, "tp" );
#endif
#ifdef AM_SLI3108_SUPPORT
lua_newtable( L );
luaL_register( L, NULL, pio_sli3108_pin_map );
lua_setfield( L, -2, "sli3108" );
platform_SLI3108_init();
#endif
return 1;
#endif // #if LUA_OPTIMIZE_MEMORY > 0
}