Advertisement
Guest User

Untitled

a guest
May 24th, 2017
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.79 KB | None | 0 0
  1. /*
  2.  * esp8266.c
  3.  *
  4.  *  Created on: 22/05/2017
  5.  *      Author: Dolence
  6.  */
  7. #include "ch.h"
  8. #include "hal.h"
  9. #include "chprintf.h"
  10. #include "esp8266.h"
  11.  
  12. #define READ_TIMEOUT 1000  // uart read timeout on 1000 ticks
  13. #define WRITE_TIMEOUT 1000 // uart write timeout on 1000 ticks
  14.  
  15. static BaseSequentialStream * espStream = NULL;
  16. static BaseSequentialStream * dbgStream = NULL;
  17.  
  18.  
  19. binary_semaphore_t uart_bsem;
  20. static thread_t *tWifiRx;
  21. static thread_t *tWifiTx;
  22.  
  23.  
  24. static const SerialConfig esp8266cfg = {
  25.     115200, //9600, // baud rate
  26.     0,
  27.     0,
  28.     0,
  29. };
  30.  
  31. static const SerialConfig debugcfg = {
  32.     115200, //9600, // baud rate
  33.     0,
  34.     0,
  35.     0,
  36. };
  37.  
  38. static THD_WORKING_AREA(waWifiRx, 128);
  39. static THD_FUNCTION(WifiRx, arg) {
  40.  
  41.   (void)arg;
  42.  
  43.   event_listener_t elWifidata;
  44.  
  45.   eventflags_t flags;
  46.   chEvtRegisterMask((event_source_t *)chnGetEventSource((SerialDriver *) espStream), &elWifidata, EVENT_MASK(1));
  47.  
  48.   while (true) {
  49.     chEvtWaitOneTimeout(EVENT_MASK(1), MS2ST(10));
  50.  
  51.     chSysLock();
  52.     flags = chEvtGetAndClearFlags(&elWifidata);
  53.     chSysUnlock();
  54.  
  55.     if (flags & CHN_INPUT_AVAILABLE) {
  56.       msg_t charbuf;
  57.       do {
  58.         charbuf = chnGetTimeout((SerialDriver *) espStream, TIME_IMMEDIATE);
  59.             if (charbuf != Q_TIMEOUT) {
  60.               chSequentialStreamPut(dbgStream, charbuf);
  61.             }
  62.       }
  63.       while (charbuf != Q_TIMEOUT);
  64.     }
  65.   }
  66.   //return 0;
  67. }
  68.  
  69.  
  70. static THD_WORKING_AREA(waWifiTx, 128);
  71. static THD_FUNCTION(WifiTx, arg) {
  72.  
  73.   (void)arg;
  74.  
  75.   while (true) {
  76.     thread_t *tp = chMsgWait();
  77.  
  78.     msg_t msg = chMsgGet(tp);
  79.  
  80.     chprintf(espStream, "%s", msg);
  81.  
  82.     chMsgRelease(tp, MSG_OK);
  83.   }
  84. }
  85.  
  86.  
  87. int esp8266Init(SerialDriver * espDriver, SerialDriver * dbgDriver, int mode)
  88. {
  89.   espStream = (BaseSequentialStream *) espDriver;
  90.   dbgStream = (BaseSequentialStream *) dbgDriver;
  91.  
  92.   sdStart((SerialDriver *) espStream, &debugcfg);
  93.   sdStart((SerialDriver *) dbgStream, &esp8266cfg);
  94.  
  95.   //cria o thread de recebimento
  96.   tWifiRx = chThdCreateStatic(waWifiRx, sizeof(waWifiRx), NORMALPRIO, WifiRx, NULL);
  97.   tWifiTx = chThdCreateStatic(waWifiTx, sizeof(waWifiTx), NORMALPRIO, WifiTx, NULL);
  98.  
  99.   esp8266Cmd("AT+RST\r\n", "teste", 1000);
  100.  
  101.   return 0;
  102. }
  103.  
  104.  
  105. bool esp8266Cmd(const char * cmd, const char * rsp, int cmddelay)
  106. {
  107.   char buffer[200];
  108.   chsnprintf(buffer, sizeof(buffer), "%s", cmd);
  109.  
  110.   chMsgSend(tWifiTx, (msg_t)buffer);
  111.   if (cmddelay > 0) chThdSleepMilliseconds(cmddelay);
  112.  
  113.   //return esp8266ReadUntil(rsp, READ_TIMEOUT);
  114.   return 0;
  115. }
  116.  
  117.  
  118. bool esp8266ReadUntil(const char * resp, int timeout)
  119. {
  120.   //to be implemented
  121.   thread_t *tp = chMsgWait();
  122.  
  123.   //do while response doesn't arrive or timeout occurs
  124.   msg_t msg = chMsgGet(tp);
  125.   //
  126.   // ...
  127.   //
  128.   chMsgRelease(tp, MSG_OK);
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement