1 | /*
|
---|
2 | * TOPPERS/ASP Kernel
|
---|
3 | * Toyohashi Open Platform for Embedded Real-Time Systems/
|
---|
4 | * Advanced Standard Profile Kernel
|
---|
5 | *
|
---|
6 | * Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
|
---|
7 | * Toyohashi Univ. of Technology, JAPAN
|
---|
8 | * Copyright (C) 2004-2012 by Embedded and Real-Time Systems Laboratory
|
---|
9 | * Graduate School of Information Science, Nagoya Univ., JAPAN
|
---|
10 | *
|
---|
11 | * 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
|
---|
12 | * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
|
---|
13 | * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
|
---|
14 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
15 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
16 | * スコード中に含まれていること.
|
---|
17 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
18 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
19 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
20 | * の無保証規定を掲載すること.
|
---|
21 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
22 | * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
|
---|
23 | * と.
|
---|
24 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
25 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
26 | * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
|
---|
27 | * 報告すること.
|
---|
28 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
29 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
30 | * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
|
---|
31 | * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
|
---|
32 | * 免責すること.
|
---|
33 | *
|
---|
34 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
35 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
|
---|
36 | * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
|
---|
37 | * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
|
---|
38 | * の責任を負わない.
|
---|
39 | *
|
---|
40 | * $Id$
|
---|
41 | */
|
---|
42 |
|
---|
43 | /*
|
---|
44 | * ESP-WROOM-02テストの本体
|
---|
45 | * シールド上のプッシュスイッチがESP-WROOM-02の
|
---|
46 | * リセットスイッチです.リセット後、ATコマンドを使って、
|
---|
47 | * ESP-WROOM-02と通信ができます。
|
---|
48 | * 例えば、AT+GMRは、AT GMR(return)でESP-WROOM-02に送信され
|
---|
49 | * ESP-WROOM-02からの受信データはコンソールに表示されます.
|
---|
50 | */
|
---|
51 |
|
---|
52 | #include <stddef.h>
|
---|
53 | #include <stdbool.h>
|
---|
54 | #include <kernel.h>
|
---|
55 | #include <t_syslog.h>
|
---|
56 | #include <t_stdlib.h>
|
---|
57 | #include <target_syssvc.h>
|
---|
58 | #include <stdio.h>
|
---|
59 | #include <string.h>
|
---|
60 | #include "syssvc/serial.h"
|
---|
61 | #include "syssvc/syslog.h"
|
---|
62 | #include "kernel_cfg.h"
|
---|
63 | #include "device.h"
|
---|
64 | #include "monitor.h"
|
---|
65 | #include "main.h"
|
---|
66 | #include "esp_at_socket.h"
|
---|
67 |
|
---|
68 | /*
|
---|
69 | * サービスコールのエラーのログ出力
|
---|
70 | */
|
---|
71 | Inline void
|
---|
72 | svc_perror(const char *file, int_t line, const char *expr, ER ercd)
|
---|
73 | {
|
---|
74 | if (ercd < 0) {
|
---|
75 | t_perror(LOG_ERROR, file, line, expr, ercd);
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | #define SVC_PERROR(expr) svc_perror(__FILE__, __LINE__, #expr, (expr))
|
---|
80 |
|
---|
81 | /*static*/ uint32_t heap_area[512*1024] __attribute((section(".heap")));
|
---|
82 |
|
---|
83 | intptr_t heap_param[2] = {
|
---|
84 | (intptr_t)heap_area,
|
---|
85 | sizeof(heap_area)
|
---|
86 | };
|
---|
87 |
|
---|
88 | static char aTxBuffer[TX_BUF_SIZE];
|
---|
89 | static uint8_t aRxBuffer[RX_BUF_SIZE];
|
---|
90 |
|
---|
91 | int rx_mode = MODE_DEFAULT;
|
---|
92 | queue_t rx_queue;
|
---|
93 |
|
---|
94 | /*
|
---|
95 | * デバイスコマンド番号
|
---|
96 | */
|
---|
97 | #define BASE_CMD_LEN 4
|
---|
98 |
|
---|
99 | static int_t mode_func(int argc, char **argv);
|
---|
100 | static int_t count_func(int argc, char **argv);
|
---|
101 | static int_t at_func(int argc, char **argv);
|
---|
102 |
|
---|
103 | /*
|
---|
104 | * デバイスコマンドテーブル
|
---|
105 | */
|
---|
106 | static const COMMAND_INFO at_command_info[] = {
|
---|
107 | {"MODE", mode_func},
|
---|
108 | {"TCNT", count_func},
|
---|
109 | {"", at_func}
|
---|
110 | };
|
---|
111 |
|
---|
112 | #define NUM_DEVICE_CMD (sizeof(at_command_info)/sizeof(COMMAND_INFO))
|
---|
113 |
|
---|
114 | static const char at_name[] = "AT";
|
---|
115 | static const char at_help[] =
|
---|
116 | " AT MODE (no) echo mode\n"
|
---|
117 | " TCNT recived count\n"
|
---|
118 | " any command\n";
|
---|
119 |
|
---|
120 | static COMMAND_LINK at_command_link = {
|
---|
121 | NULL,
|
---|
122 | NUM_DEVICE_CMD,
|
---|
123 | at_name,
|
---|
124 | NULL,
|
---|
125 | at_help,
|
---|
126 | &at_command_info[0]
|
---|
127 | };
|
---|
128 |
|
---|
129 | static int a2i(char *str)
|
---|
130 | {
|
---|
131 | int num = 0;
|
---|
132 |
|
---|
133 | while(*str >= '0' && *str <= '9'){
|
---|
134 | num = num * 10 + *str++ - '0';
|
---|
135 | }
|
---|
136 | return num;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /*
|
---|
140 | * ATコマンド設定関数
|
---|
141 | */
|
---|
142 | void at_info_init(intptr_t exinf)
|
---|
143 | {
|
---|
144 | setup_command(&at_command_link);
|
---|
145 | }
|
---|
146 |
|
---|
147 | /*
|
---|
148 | * ECHOモード設定関数
|
---|
149 | */
|
---|
150 | static int_t mode_func(int argc, char **argv)
|
---|
151 | {
|
---|
152 | int mode = rx_mode;
|
---|
153 |
|
---|
154 | if(argc >= 2){
|
---|
155 | mode = a2i(argv[1]);
|
---|
156 | if(mode < 0 || mode > MODE_ECHO_HEX)
|
---|
157 | mode = MODE_DEFAULT;
|
---|
158 | rx_mode = mode;
|
---|
159 | }
|
---|
160 | printf("AT ECHO MODE(%d)\n", mode);
|
---|
161 | return mode;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /*
|
---|
165 | * 受信カウントコマンド取得関数
|
---|
166 | */
|
---|
167 | static int_t count_func(int argc, char **argv)
|
---|
168 | {
|
---|
169 | printf("AT RECIVED COUNT(%d)(%d)\n", rx_queue.size, rx_queue.clen);
|
---|
170 | return rx_queue.clen;
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | /*
|
---|
175 | * AT設定コマンド関数
|
---|
176 | */
|
---|
177 | static int_t at_func(int argc, char **argv)
|
---|
178 | {
|
---|
179 | int i, arg_count = BASE_CMD_LEN;
|
---|
180 | char *p = aTxBuffer;
|
---|
181 | char *s, c;
|
---|
182 | ER_UINT result;
|
---|
183 | int caps = 1;
|
---|
184 |
|
---|
185 | for(i = 0 ; i < argc ; i++){
|
---|
186 | arg_count++;
|
---|
187 | arg_count += strlen(argv[i]);
|
---|
188 | }
|
---|
189 | *p++ = 'A';
|
---|
190 | *p++ = 'T';
|
---|
191 | if(arg_count > BASE_CMD_LEN){
|
---|
192 | for(i = 0 ; i < argc ; i++){
|
---|
193 | s = argv[i];
|
---|
194 | *p++ = '+';
|
---|
195 | while(*s != 0){
|
---|
196 | c = *s++;
|
---|
197 | if(c == '"')
|
---|
198 | caps ^= 1;
|
---|
199 | if(caps && c >= 'a' && c <= 'z')
|
---|
200 | c -= 0x20;
|
---|
201 | *p++ = c;
|
---|
202 | }
|
---|
203 | }
|
---|
204 | }
|
---|
205 | *p++ = '\r';
|
---|
206 | *p++ = '\n';
|
---|
207 | *p++ = 0;
|
---|
208 | result = serial_wri_dat(AT_PORTID, (const char *)aTxBuffer, arg_count);
|
---|
209 | if(result < 0){
|
---|
210 | syslog_1(LOG_ERROR, "AT command send error(%d) !", result);
|
---|
211 | }
|
---|
212 | printf("%d:%s", arg_count, aTxBuffer);
|
---|
213 | return arg_count;
|
---|
214 | }
|
---|
215 |
|
---|
216 | int wolfSSL_Debugging_ON(void);
|
---|
217 | int esp_at_rx_handler(void *data, int len);
|
---|
218 |
|
---|
219 | /*
|
---|
220 | * メインタスク
|
---|
221 | */
|
---|
222 | void main_task(intptr_t exinf)
|
---|
223 | {
|
---|
224 | T_SERIAL_RPOR k_rpor;
|
---|
225 | queue_t *rxque;
|
---|
226 | ER_UINT ercd;
|
---|
227 | int i, j, len;
|
---|
228 | int dlen = 0;
|
---|
229 | uint8_t ch;
|
---|
230 |
|
---|
231 | //wolfSSL_Debugging_ON();
|
---|
232 | init_esp_at();
|
---|
233 |
|
---|
234 | SVC_PERROR(syslog_msk_log(LOG_UPTO(LOG_INFO), LOG_UPTO(LOG_EMERG)));
|
---|
235 | syslog(LOG_NOTICE, "Sample program starts (exinf = %d).", (int_t) exinf);
|
---|
236 |
|
---|
237 | /*
|
---|
238 | * シリアルポートの初期化
|
---|
239 | *
|
---|
240 | * システムログタスクと同じシリアルポートを使う場合など,シリアル
|
---|
241 | * ポートがオープン済みの場合にはここでE_OBJエラーになるが,支障は
|
---|
242 | * ない.
|
---|
243 | */
|
---|
244 | ercd = serial_opn_por(TASK_PORTID);
|
---|
245 | if (ercd < 0 && MERCD(ercd) != E_OBJ) {
|
---|
246 | syslog(LOG_ERROR, "%s (%d) reported by `serial_opn_por'.",
|
---|
247 | itron_strerror(ercd), SERCD(ercd));
|
---|
248 | }
|
---|
249 | SVC_PERROR(serial_ctl_por(TASK_PORTID,
|
---|
250 | (IOCTL_CRLF | IOCTL_FCSND | IOCTL_FCRCV)));
|
---|
251 |
|
---|
252 | /*
|
---|
253 | * キューバッファの初期化
|
---|
254 | */
|
---|
255 | rxque = &rx_queue;
|
---|
256 | rxque->size = RX_BUF_SIZE;
|
---|
257 | rxque->clen = 0;
|
---|
258 | rxque->head = 0;
|
---|
259 | rxque->tail = 0;
|
---|
260 | rxque->pbuffer = aRxBuffer;
|
---|
261 |
|
---|
262 | /*
|
---|
263 | * ESP-WROOM02用シリアルポートの初期化
|
---|
264 | */
|
---|
265 | ercd = serial_opn_por(AT_PORTID);
|
---|
266 | if (ercd < 0 && MERCD(ercd) != E_OBJ) {
|
---|
267 | syslog(LOG_ERROR, "%s (%d) reported by `serial_opn_por'.(AT)",
|
---|
268 | itron_strerror(ercd), SERCD(ercd));
|
---|
269 | slp_tsk();
|
---|
270 | }
|
---|
271 | SVC_PERROR(serial_ctl_por(AT_PORTID, 0));
|
---|
272 | while(dlen < 500000){
|
---|
273 | serial_ref_por(AT_PORTID, &k_rpor);
|
---|
274 | len = k_rpor.reacnt;
|
---|
275 | if((rxque->size - rxque->clen) < len)
|
---|
276 | len = rxque->size - rxque->clen;
|
---|
277 | if(len > 0){
|
---|
278 | if(rxque->head >= rxque->tail){
|
---|
279 | if((rxque->size - rxque->head) < len)
|
---|
280 | i = rxque->size - rxque->head;
|
---|
281 | else
|
---|
282 | i = len;
|
---|
283 | j = serial_rea_dat(AT_PORTID, (char *)&rxque->pbuffer[rxque->head], i);
|
---|
284 | rxque->head += j;
|
---|
285 | if(rxque->head >= rxque->size)
|
---|
286 | rxque->head -= rxque->size;
|
---|
287 | rxque->clen += j;
|
---|
288 | len -= j;
|
---|
289 | }
|
---|
290 | if(len > 0){
|
---|
291 | j = serial_rea_dat(AT_PORTID, (char *)&rxque->pbuffer[rxque->head], len);
|
---|
292 | rxque->head += j;
|
---|
293 | if(rxque->head >= rxque->size)
|
---|
294 | rxque->head -= rxque->size;
|
---|
295 | rxque->clen += j;
|
---|
296 | len -= j;
|
---|
297 | }
|
---|
298 | }
|
---|
299 | while(rxque->clen > 0 && rx_mode != MODE_NOECHO){
|
---|
300 | if(rx_mode == MODE_ECHO_CHAR){
|
---|
301 | ch = rxque->pbuffer[rxque->tail];
|
---|
302 | if(ch >= 0x7f)
|
---|
303 | ch = '.';
|
---|
304 | else if(ch < 0x20 && ch != '\r' && ch != '\n')
|
---|
305 | ch = '.';
|
---|
306 | putchar(ch);
|
---|
307 | }
|
---|
308 | else
|
---|
309 | printf("%02x ", rxque->pbuffer[rxque->tail]);
|
---|
310 | rxque->clen--;
|
---|
311 | rxque->tail++;
|
---|
312 | if(rxque->tail >= rxque->size)
|
---|
313 | rxque->tail -= rxque->size;
|
---|
314 | dlen++;
|
---|
315 | if((dlen % 32) == 0 && rx_mode == MODE_ECHO_HEX){
|
---|
316 | printf("\n");
|
---|
317 | dly_tsk(50);
|
---|
318 | }
|
---|
319 | }
|
---|
320 | if (rxque->clen > 0) {
|
---|
321 | len = rxque->clen;
|
---|
322 | if (rxque->tail >= rxque->head) {
|
---|
323 | if ((rxque->size - rxque->tail) < len)
|
---|
324 | i = rxque->size - rxque->tail;
|
---|
325 | else
|
---|
326 | i = len;
|
---|
327 | j = esp_at_rx_handler((char *)&rxque->pbuffer[rxque->tail], i);
|
---|
328 | rxque->tail += j;
|
---|
329 | if (rxque->tail >= rxque->size)
|
---|
330 | rxque->tail -= rxque->size;
|
---|
331 | rxque->clen -= j;
|
---|
332 | len -= j;
|
---|
333 | }
|
---|
334 | if (len > 0) {
|
---|
335 | j = esp_at_rx_handler((char *)&rxque->pbuffer[rxque->tail], len);
|
---|
336 | rxque->tail += j;
|
---|
337 | if (rxque->tail >= rxque->size)
|
---|
338 | rxque->tail -= rxque->size;
|
---|
339 | rxque->clen -= j;
|
---|
340 | len -= j;
|
---|
341 | }
|
---|
342 | }
|
---|
343 | else {
|
---|
344 | dly_tsk(20);
|
---|
345 | }
|
---|
346 | }
|
---|
347 | syslog_0(LOG_NOTICE, "## STOP ##");
|
---|
348 | slp_tsk();
|
---|
349 |
|
---|
350 | syslog(LOG_NOTICE, "Sample program ends.");
|
---|
351 | // SVC_PERROR(ext_ker());
|
---|
352 | }
|
---|
353 |
|
---|
354 | int EmbedReceive(struct WOLFSSL *ssl, char *buf, int sz, void *_ctx)
|
---|
355 | {
|
---|
356 | syslog(LOG_NOTICE, "EmbedReceive");
|
---|
357 | return 0;
|
---|
358 | }
|
---|
359 |
|
---|
360 | int EmbedSend(struct WOLFSSL* ssl, char* buf, int sz, void* _ctx)
|
---|
361 | {
|
---|
362 | syslog(LOG_NOTICE, "EmbedSend");
|
---|
363 | return 0;
|
---|
364 | }
|
---|