Skip to content

Commit fff9f95

Browse files
authored
Merge pull request #2836 from TerryE/dev-new-lua.c
Lua 5.1 to 5.3 realignement phase 1
2 parents a08e74d + 522b1d0 commit fff9f95

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1221
-1207
lines changed

.gdbinitlua

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ set pagination off
33
set print null-stop
44

55
define prTS
6-
set $o = &(((TString *)($arg0))->tsv)
6+
set $o = &(((TString *)(($arg0).value))->tsv)
77
printf "Common header: next = %p, marked = 0x%01x\n", $o->next, $o->marked
8-
printf "String: hash = 0x%08x, len = %u : %s\n", $o->hash, $o->len, (char *)(&$o[1])
8+
printf "String: hash = 0x%08x, len = %u : %s\n", $o->hash, $o->len, (char *)($o+1)
99
end
1010

1111
define prTnodes
@@ -24,6 +24,18 @@ define prTnodes
2424
set $i = $i +1
2525
end
2626
end
27+
28+
define prTarray
29+
set $o = (Table *)($arg0)
30+
set $n = $o->sizearray
31+
set $i = 0
32+
while $i < $n
33+
set $nd = ($o->array) + $i
34+
prTV $nd
35+
set $i = $i +1
36+
end
37+
end
38+
2739
define prTV
2840
if $arg0
2941
set $type = ($arg0).tt
@@ -78,6 +90,10 @@ define prTV
7890
end
7991
if $type == 9
8092
# UserData
93+
set $o = &($val->gc.u.uv)
94+
printf "Common header: next = %p, marked = 0x%01x\n", $o->next, $o->marked
95+
printf "UD = %p Userdata: metatable = ", ($o+1))
96+
print ($o)->metatable
8197
end
8298
if $type == 10
8399
# Thread

app/Makefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ SUBDIRS= \
3636
libc \
3737
lua \
3838
lwip \
39-
task \
4039
smart \
4140
modules \
4241
spiffs \
@@ -64,8 +63,7 @@ COMPONENTS_eagle.app.v6 = \
6463
user/libuser.a \
6564
crypto/libcrypto.a \
6665
driver/libdriver.a \
67-
platform/libplatform.a \
68-
task/libtask.a \
66+
platform/libplatform.a \
6967
libc/liblibc.a \
7068
lua/liblua.a \
7169
lwip/liblwip.a \

app/coap/endpoints.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,20 +162,18 @@ static int handle_post_function(const coap_endpoint_t *ep, coap_rw_buffer_t *scr
162162
return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE);
163163
}
164164

165-
extern int lua_put_line(const char *s, size_t l);
166-
167165
static const coap_endpoint_path_t path_command = {2, {"v1", "c"}};
168166
static int handle_post_command(const coap_endpoint_t *ep, coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo)
169167
{
170168
if (inpkt->payload.len == 0)
171169
return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_TEXT_PLAIN);
172170
if (inpkt->payload.len > 0)
173171
{
174-
char line[LUA_MAXINPUT];
175-
if (!coap_buffer_to_string(line, LUA_MAXINPUT, &inpkt->payload) &&
176-
lua_put_line(line, strlen(line))) {
177-
NODE_DBG("\nResult(if any):\n");
178-
system_os_post (LUA_TASK_PRIO, LUA_PROCESS_LINE_SIG, 0);
172+
char line[LUA_MAXINPUT+1];
173+
if (!coap_buffer_to_string(line, LUA_MAXINPUT, &inpkt->payload)) {
174+
int l = strlen(line);
175+
line[l] = '\n';
176+
lua_input_string(line, l+1);
179177
}
180178
return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
181179
}

app/driver/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ifndef PDIR
1515
GEN_LIBS = libdriver.a
1616
endif
1717

18-
STD_CFLAGS=-std=gnu11 -Wimplicit
18+
STD_CFLAGS=-std=gnu11 -Wimplicit -Wall
1919

2020
#############################################################
2121
# Configuration i.e. compile options etc.

app/driver/input.c

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
#include "platform.h"
2+
#include "driver/uart.h"
3+
#include "driver/input.h"
4+
#include <stdint.h>
5+
#include "mem.h"
6+
7+
/**DEBUG**/extern void dbg_printf(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
8+
9+
static void input_handler(platform_task_param_t flag, uint8 priority);
10+
11+
static struct input_state {
12+
char *data;
13+
int line_pos;
14+
size_t len;
15+
const char *prompt;
16+
uart_cb_t uart_cb;
17+
platform_task_handle_t input_sig;
18+
int data_len;
19+
bool run_input;
20+
bool uart_echo;
21+
char last_char;
22+
char end_char;
23+
uint8 input_sig_flag;
24+
} ins = {0};
25+
26+
#define NUL '\0'
27+
#define BS '\010'
28+
#define CR '\r'
29+
#define LF '\n'
30+
#define DEL 0x7f
31+
#define BS_OVER "\010 \010"
32+
33+
#define sendStr(s) uart0_sendStr(s)
34+
#define putc(c) uart0_putc(c)
35+
36+
// UartDev is defined and initialized in rom code.
37+
extern UartDevice UartDev;
38+
39+
static bool uart_getc(char *c){
40+
RcvMsgBuff *pRxBuff = &(UartDev.rcv_buff);
41+
if(pRxBuff->pWritePos == pRxBuff->pReadPos){ // empty
42+
return false;
43+
}
44+
// ETS_UART_INTR_DISABLE();
45+
ETS_INTR_LOCK();
46+
*c = (char)*(pRxBuff->pReadPos);
47+
if (pRxBuff->pReadPos == (pRxBuff->pRcvMsgBuff + RX_BUFF_SIZE)) {
48+
pRxBuff->pReadPos = pRxBuff->pRcvMsgBuff ;
49+
} else {
50+
pRxBuff->pReadPos++;
51+
}
52+
// ETS_UART_INTR_ENABLE();
53+
ETS_INTR_UNLOCK();
54+
return true;
55+
}
56+
57+
/*
58+
** input_handler at high-priority is a system post task used to process pending Rx
59+
** data on UART0. The flag is used as a latch to stop the interrupt handler posting
60+
** multiple pending requests. At low priority it is used the trigger interactive
61+
** compile.
62+
**
63+
** The ins.data check detects up the first task call which used to initialise
64+
** everything.
65+
*/
66+
int lua_main (void);
67+
static bool input_readline(void);
68+
69+
static void input_handler(platform_task_param_t flag, uint8 priority) {
70+
(void) priority;
71+
if (!ins.data) {
72+
lua_main();
73+
return;
74+
}
75+
ins.input_sig_flag = flag & 0x1;
76+
while (input_readline()) {}
77+
}
78+
79+
/*
80+
** The input state (ins) is private, so input_setup() exposes the necessary
81+
** access to public properties and is called in user_init() before the Lua
82+
** enviroment is initialised. The second routine input_setup_receive() is
83+
** called in lua.c after the Lua environment is available to bind the Lua
84+
** input handler. Any UART input before this receive setup is ignored.
85+
*/
86+
void input_setup(int bufsize, const char *prompt) {
87+
// Initialise non-zero elements
88+
ins.run_input = true;
89+
ins.uart_echo = true;
90+
ins.data = os_malloc(bufsize);
91+
ins.len = bufsize;
92+
ins.prompt = prompt;
93+
ins.input_sig = platform_task_get_id(input_handler);
94+
// pass the task CB parameters to the uart driver
95+
uart_init_task(ins.input_sig, &ins.input_sig_flag);
96+
ETS_UART_INTR_ENABLE();
97+
}
98+
99+
void input_setup_receive(uart_cb_t uart_on_data_cb, int data_len, char end_char, bool run_input) {
100+
ins.uart_cb = uart_on_data_cb;
101+
ins.data_len = data_len;
102+
ins.end_char = end_char;
103+
ins.run_input = run_input;
104+
}
105+
106+
void input_setecho (bool flag) {
107+
ins.uart_echo = flag;
108+
}
109+
110+
void input_setprompt (const char *prompt) {
111+
ins.prompt = prompt;
112+
}
113+
114+
/*
115+
** input_readline() is called from the input_handler() event routine which is
116+
** posted by the UART Rx ISR posts. This works in one of two modes depending on
117+
** the bool ins.run_input.
118+
** - TRUE: it clears the UART FIFO up to EOL, doing any callback and sending
119+
** the line to Lua.
120+
** - FALSE: it clears the UART FIFO doing callbacks according to the data_len /
121+
** end_char break.
122+
*/
123+
void lua_input_string (const char *line, int len);
124+
125+
static bool input_readline(void) {
126+
char ch = NUL;
127+
if (ins.run_input) {
128+
while (uart_getc(&ch)) {
129+
/* handle CR & LF characters and aggregate \n\r and \r\n pairs */
130+
if ((ch == CR && ins.last_char == LF) ||
131+
(ch == LF && ins.last_char == CR)) {
132+
ins.last_char = NUL;
133+
continue;
134+
}
135+
136+
/* backspace key */
137+
if (ch == DEL || ch == BS) {
138+
if (ins.line_pos > 0) {
139+
if(ins.uart_echo) sendStr(BS_OVER);
140+
ins.line_pos--;
141+
}
142+
ins.data[ins.line_pos] = 0;
143+
ins.last_char = NUL;
144+
continue;
145+
}
146+
ins.last_char = ch;
147+
148+
/* end of data */
149+
if (ch == CR || ch == LF) {
150+
if (ins.uart_echo) putc(LF);
151+
if (ins.uart_cb) ins.uart_cb(ins.data, ins.line_pos);
152+
if (ins.line_pos == 0) {
153+
/* Get a empty data, then go to get a new data */
154+
155+
sendStr(ins.prompt);
156+
continue;
157+
} else {
158+
ins.data[ins.line_pos++] = LF;
159+
lua_input_string(ins.data, ins.line_pos);
160+
ins.line_pos = 0;
161+
return true;
162+
}
163+
}
164+
165+
if(ins.uart_echo) putc(ch);
166+
167+
/* it's a large data, discard it */
168+
if ( ins.line_pos + 1 >= ins.len ){
169+
ins.line_pos = 0;
170+
}
171+
ins.data[ins.line_pos++] = ch;
172+
}
173+
174+
} else {
175+
176+
if (!ins.uart_cb) {
177+
while (uart_getc(&ch)) {}
178+
} else if (ins.data_len == 0) {
179+
while (uart_getc(&ch)) {
180+
ins.uart_cb(&ch, 1);
181+
}
182+
} else {
183+
while (uart_getc(&ch)) {
184+
ins.data[ins.line_pos++] = ch;
185+
if( ins.line_pos >= ins.len ||
186+
(ins.data_len > 0 && ins.line_pos >= ins.data_len) ||
187+
ch == ins.end_char ) {
188+
ins.uart_cb(ins.data, ins.line_pos);
189+
ins.line_pos = 0;
190+
}
191+
}
192+
}
193+
}
194+
return false;
195+
}
196+

app/driver/pwm.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "driver/pwm.h"
2121

2222
// #define PWM_DBG os_printf
23-
#define PWM_DBG
23+
#define PWM_DBG( ... )
2424

2525
// Enabling the next line will cause the interrupt handler to toggle
2626
// this output pin during processing so that the timing is obvious
@@ -253,7 +253,7 @@ pwm_set_freq(uint16 freq, uint8 channel)
253253

254254
pwm.period = PWM_1S / pwm.freq;
255255
}
256-
256+
#if 0
257257
/******************************************************************************
258258
* FunctionName : pwm_set_freq_duty
259259
* Description : set pwm frequency and each channel's duty
@@ -274,7 +274,7 @@ pwm_set_freq_duty(uint16 freq, uint16 *duty)
274274
pwm_set_duty(duty[i], pwm_out_io_num[i]);
275275
}
276276
}
277-
277+
#endif
278278
/******************************************************************************
279279
* FunctionName : pwm_get_duty
280280
* Description : get duty of each channel

app/driver/pwm2.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77

88
#include <stddef.h>
99
#include <stdint.h>
10+
#include <string.h>
1011
#include "mem.h"
1112
#include "pin_map.h"
1213
#include "platform.h"
1314
#include "hw_timer.h"
1415
#include "driver/pwm2.h"
16+
#include "user_interface.h"
1517

1618
#define PWM2_TMR_MAGIC_80MHZ 16
1719
#define PWM2_TMR_MAGIC_160MHZ 32

0 commit comments

Comments
 (0)