_stateMachine.ino 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. // 1-channel LoRa Gateway for ESP8266
  2. // Copyright (c) 2016, 2017, 2018, 2019 Maarten Westenberg version for ESP8266
  3. // Version 6.1.0
  4. // Date: 2019-10-20
  5. //
  6. // based on work done by Thomas Telkamp for Raspberry PI 1ch gateway
  7. // and many others.
  8. //
  9. // All rights reserved. This program and the accompanying materials
  10. // are made available under the terms of the MIT License
  11. // which accompanies this distribution, and is available at
  12. // https://opensource.org/licenses/mit-license.php
  13. //
  14. // NO WARRANTY OF ANY KIND IS PROVIDED
  15. //
  16. // Author: Maarten Westenberg (mw12554@hotmail.com)
  17. //
  18. // This file contains the state machine code enabling to receive
  19. // and transmit packages/messages.
  20. // ============================================================================================
  21. //
  22. // --------------------------------------------------------------------------------------------
  23. // stateMachine handler of the state machine.
  24. // We use ONE state machine for all kind of interrupts. This assures that we take
  25. // the correct action upon receiving an interrupt.
  26. //
  27. // _event is the software interrupt: If set this function is executed from loop(),
  28. // the function should itself take care of setting or resetting the variable.
  29. //
  30. // STATE MACHINE
  31. // The program uses the following state machine (in _state), all states
  32. // are done in interrupt routine, only the follow-up of S-RXDONE is done
  33. // in the main loop() program. This is because otherwise the interrupt processing
  34. // would take too long to finish
  35. //
  36. // So _state has one of the following state values:
  37. //
  38. // S-INIT=0, The commands in this state are executed only once
  39. // - Goto S_SCAN
  40. //
  41. // S-SCAN, CadScanner() part
  42. // - Upon CDDECT (int1) goto S_RX,
  43. // - upon CDDONE (int0) goto S_CAD, walk through all SF until CDDETD
  44. // - Else stay in SCAN state
  45. //
  46. // S-CAD,
  47. // - Upon CDDECT (int1) goto S_RX,
  48. // - Upon CDDONE (int0) goto S_SCAN, start with SF7 recognition again
  49. //
  50. // S-RX, Received CDDECT so message detected, RX cycle started.
  51. // - Upon RXDONE (int0) package read. If read ok continue to read message
  52. // - upon RXTOUT (int1) error, goto S_SCAN
  53. //
  54. // S-TX Transmitting a message
  55. // - Upon TXDONE goto S_SCAN
  56. //
  57. // S-TXDONE Transmission complete by loop() now again in interrupt
  58. // - Set the Mask
  59. // - reset the Flags
  60. // - Goto either SCAN or RX
  61. //
  62. // This interrupt routine has been kept as simple and short as possible.
  63. // If we receive an interrupt that does not belong to a _state then print error.
  64. // _event is a special variable which indicate that an interrupt event has happened
  65. // and we need to take action OR that we generate a soft interrupt for state machine.
  66. //
  67. // NOTE: We may clear the interrupt but leave the flag for the moment.
  68. // The eventHandler should take care of repairing flags between interrupts.
  69. // --------------------------------------------------------------------------------------------
  70. void stateMachine()
  71. {
  72. // Determine what interrupt flags are set
  73. //
  74. uint8_t flags = readRegister(REG_IRQ_FLAGS);
  75. uint8_t mask = readRegister(REG_IRQ_FLAGS_MASK);
  76. uint8_t intr = flags & ( ~ mask ); // Only react on non masked interrupts
  77. uint8_t rssi;
  78. _event=0; // Reset the interrupt detector
  79. #if DUSB>=1
  80. if (intr != flags) {
  81. Serial.print(F("FLAG ::"));
  82. SerialStat(intr);
  83. }
  84. #endif
  85. // If Hopping is selected AND if there is NO event interrupt detected
  86. // and the state machine is called anyway
  87. // then we know its a soft interrupt and we do nothing and return to main loop.
  88. //
  89. if ((_hop) && (intr == 0x00) )
  90. {
  91. // eventWait is the time since we have had a CDDETD event (preamble detected)
  92. // If we are not in scanning state, and there will be an interrupt coming,
  93. // In state S_RX it could be RXDONE in which case allow kernel time
  94. //
  95. if ((_state == S_SCAN) || (_state == S_CAD)) {
  96. _event=0;
  97. uint32_t eventWait = EVENT_WAIT;
  98. switch (_state) {
  99. case S_INIT: eventWait = 0; break;
  100. // Next two are most important
  101. case S_SCAN: eventWait = EVENT_WAIT * 1; break;
  102. case S_CAD: eventWait = EVENT_WAIT * 1; break;
  103. case S_RX: eventWait = EVENT_WAIT * 8; break;
  104. case S_TX: eventWait = EVENT_WAIT * 1; break;
  105. case S_TXDONE: eventWait = EVENT_WAIT * 4; break;
  106. default:
  107. eventWait=0;
  108. #if DUSB>=1
  109. Serial.print(F("DEFAULT :: "));
  110. SerialStat(intr);
  111. #endif
  112. }
  113. // doneWait is the time that we received CDDONE interrupt
  114. // So we init the wait time for RXDONE based on the current SF.
  115. // As for highter CF it takes longer to receive symbols
  116. // Assume symbols in SF8 take twice the time of SF7
  117. //
  118. uint32_t doneWait = DONE_WAIT; // Initial value
  119. switch (sf) {
  120. case SF7: break;
  121. case SF8: doneWait *= 2; break;
  122. case SF9: doneWait *= 4; break;
  123. case SF10: doneWait *= 8; break;
  124. case SF11: doneWait *= 16; break;
  125. case SF12: doneWait *= 32; break;
  126. default:
  127. doneWait *= 1;
  128. #if DUSB>=1
  129. if (( debug>=0 ) && ( pdebug & P_PRE )) {
  130. Serial.print(F("PRE:: DEF set"));
  131. Serial.println();
  132. }
  133. #endif
  134. break;
  135. }
  136. // If micros is starting over again after 51 minutes
  137. // it's value is smaller than an earlier value of eventTime/doneTime
  138. //
  139. if (eventTime > micros()) eventTime=micros();
  140. if (doneTime > micros()) doneTime=micros();
  141. if (((micros() - doneTime) > doneWait ) &&
  142. (( _state == S_SCAN ) || ( _state == S_CAD )))
  143. {
  144. _state = S_SCAN;
  145. hop(); // increment ifreq = (ifreq + 1) % NUM_HOPS ;
  146. cadScanner(); // Reset to initial SF, leave frequency "freqs[ifreq]"
  147. #if DUSB>=1
  148. if (( debug >= 1 ) && ( pdebug & P_PRE )) {
  149. Serial.print(F("DONE :: "));
  150. SerialStat(intr);
  151. }
  152. #endif
  153. eventTime=micros(); // reset the timer on timeout
  154. doneTime=micros(); // reset the timer on timeout
  155. return;
  156. }
  157. // If timeout occurs and still no _event, then hop
  158. // and start scanning again
  159. //
  160. if ((micros() - eventTime) > eventWait )
  161. {
  162. _state = S_SCAN;
  163. hop(); // increment ifreq = (ifreq + 1) % NUM_HOPS ;
  164. cadScanner(); // Reset to initial SF, leave frequency "freqs[ifreq]"
  165. #if DUSB>=1
  166. if (( debug >= 2 ) && ( pdebug & P_PRE )) {
  167. Serial.print(F("HOP :: "));
  168. SerialStat(intr);
  169. }
  170. #endif
  171. eventTime=micros(); // reset the timer on timeout
  172. doneTime=micros(); // reset the timer on timeout
  173. return;
  174. }
  175. // If we are here, NO timeout has occurred
  176. // So we need to return to the main State Machine
  177. // as there was NO interrupt
  178. #if DUSB>=1
  179. if (( debug>=3 ) && ( pdebug & P_PRE )) {
  180. Serial.print(F("PRE:: eventTime="));
  181. Serial.print(eventTime);
  182. Serial.print(F(", micros="));
  183. Serial.print(micros());
  184. Serial.print(F(": "));
  185. SerialStat(intr);
  186. }
  187. #endif
  188. } // if SCAN or CAD
  189. // else, S_RX of S_TX for example
  190. else {
  191. //yield(); // May take too much time for RX
  192. } // else S_RX or S_TX, TXDONE
  193. yield();
  194. }// intr==0 && _hop
  195. // ========================================================================================
  196. // This is the actual state machine of the gateway
  197. // and its next actions are depending on the state we are in.
  198. // For hop situations we do not get interrupts, so we have to
  199. // simulate and generate events ourselves.
  200. //
  201. switch (_state)
  202. {
  203. // ----------------------------------------------------------------------------------------
  204. // If the state is init, we are starting up.
  205. // The initLoraModem() function is already called in setup();
  206. //
  207. case S_INIT:
  208. #if DUSB>=1
  209. if (( debug>=1 ) && ( pdebug & P_PRE )) {
  210. Serial.println(F("S_INIT"));
  211. }
  212. #endif
  213. // new state, needed to startup the radio (to S_SCAN)
  214. writeRegister(REG_IRQ_FLAGS, (uint8_t) 0xFF ); // Clear ALL interrupts
  215. writeRegister(REG_IRQ_FLAGS_MASK, (uint8_t) 0x00 ); // Clear ALL interrupts
  216. _event=0;
  217. break;
  218. // ----------------------------------------------------------------------------------------
  219. // In S_SCAN we measure a high RSSI this means that there (probably) is a message
  220. // coming in at that freq. But not necessarily on the current SF.
  221. // If so find the right SF with CDDETD.
  222. //
  223. case S_SCAN:
  224. //
  225. // Intr==IRQ_LORA_CDDETD_MASK
  226. // We detected a message on this frequency and SF when scanning
  227. // We clear both CDDETD and swich to reading state to read the message
  228. //
  229. if (intr & IRQ_LORA_CDDETD_MASK) {
  230. _state = S_RX; // Set state to receiving
  231. // Set RXDONE interrupt to dio0, RXTOUT to dio1
  232. writeRegister(REG_DIO_MAPPING_1, (
  233. MAP_DIO0_LORA_RXDONE |
  234. MAP_DIO1_LORA_RXTOUT |
  235. MAP_DIO2_LORA_NOP |
  236. MAP_DIO3_LORA_CRC));
  237. // Since new state is S_RX, accept no interrupts except RXDONE or RXTOUT
  238. // HEADER and CRCERR
  239. writeRegister(REG_IRQ_FLAGS_MASK, (uint8_t) ~(
  240. IRQ_LORA_RXDONE_MASK |
  241. IRQ_LORA_RXTOUT_MASK |
  242. IRQ_LORA_HEADER_MASK |
  243. IRQ_LORA_CRCERR_MASK));
  244. // Starting with version 5.0.1 the waittime is dependent on the SF
  245. // So for SF12 we wait longer (2^7 == 128 uSec) and for SF7 4 uSec.
  246. //delayMicroseconds( (0x01 << ((uint8_t)sf - 5 )) );
  247. //if (_cad) // XXX 180520 make sure we start reading asap in hop
  248. // delayMicroseconds( RSSI_WAIT ); // Wait some microseconds less
  249. rssi = readRegister(REG_RSSI); // Read the RSSI
  250. _rssi = rssi; // Read the RSSI in the state variable
  251. _event = 0; // Make 0, as soon as we have an interrupt
  252. detTime = micros(); // mark time that preamble detected
  253. #if DUSB>=1
  254. if (( debug>=1 ) && ( pdebug & P_SCAN )) {
  255. Serial.print(F("SCAN:: "));
  256. SerialStat(intr);
  257. }
  258. #endif
  259. writeRegister(REG_IRQ_FLAGS, (uint8_t) 0xFF ); // reset all interrupt flags
  260. opmode(OPMODE_RX_SINGLE); // set reg 0x01 to 0x06 for receiving
  261. }//if
  262. // CDDONE
  263. // We received a CDDONE int telling us that we received a message on this
  264. // frequency and possibly on one of its SF. Only when the incoming message
  265. // matches the SF then also CDDETD is raised.
  266. // If so, we switch to CAD state where we will wait for CDDETD event.
  267. //
  268. else if (intr & IRQ_LORA_CDDONE_MASK) {
  269. opmode(OPMODE_CAD);
  270. rssi = readRegister(REG_RSSI); // Read the RSSI
  271. #if DUSB>=1
  272. if (( debug>=2 ) && ( pdebug & P_SCAN )) {
  273. Serial.print(F("SCAN:: CDDONE: "));
  274. SerialStat(intr);
  275. }
  276. #endif
  277. // We choose the generic RSSI as a sorting mechanism for packages/messages
  278. // The pRSSI (package RSSI) is calculated upon successful reception of message
  279. // So we expect that this value makes little sense for the moment with CDDONE.
  280. // Set the rssi as low as the noise floor. Lower values are not recognized then.
  281. // Every cycle starts with ifreq==0 and sf=SF7 (or the set init SF)
  282. //
  283. //if ( rssi > RSSI_LIMIT ) // Is set to 35
  284. if ( rssi > (RSSI_LIMIT - (_hop * 7))) // Is set to 35, or 29 for HOP
  285. {
  286. #if DUSB>=1
  287. if (( debug>=2 ) && ( pdebug & P_SCAN )) {
  288. Serial.print(F("SCAN:: -> CAD: "));
  289. SerialStat(intr);
  290. }
  291. #endif
  292. _state = S_CAD; // promote to next level
  293. _event=0;
  294. }
  295. // If the RSSI is not big enough we skip the CDDONE
  296. // and go back to scanning
  297. else {
  298. #if DUSB>=1
  299. if (( debug>=2 ) && ( pdebug & P_SCAN )) {
  300. Serial.print("SCAN:: rssi=");
  301. Serial.print(rssi);
  302. Serial.print(F(": "));
  303. SerialStat(intr);
  304. }
  305. #endif
  306. _state = S_SCAN;
  307. //_event=1; // loop() scan until CDDONE
  308. }
  309. // Clear the CADDONE flag
  310. writeRegister(REG_IRQ_FLAGS_MASK, (uint8_t) 0x00);
  311. writeRegister(REG_IRQ_FLAGS, (uint8_t) 0xFF);
  312. doneTime = micros(); // Need CDDONE or other intr to reset timeout
  313. }//SCAN CDDONE
  314. // So if we are here then we are in S_SCAN and the interrupt is not
  315. // CDDECT or CDDONE. it is probably soft interrupt _event==1
  316. // So if _hop we change the frequency and restart the
  317. // interrupt in order to check for CDONE on other frequencies
  318. // if _hop we start at the next frequency, hop () sets the sf to SF7.
  319. // If we are at the end of all frequencies, reset frequencies and sf
  320. // and go to S_SCAN state.
  321. //
  322. // Note:: We should make sure that all frequencies are scanned in a row
  323. // and when we switch to ifreq==0 we should stop for a while
  324. // to allow system processing.
  325. // We should make sure that we enable webserver etc every once in a while.
  326. // We do this by changing _event to 1 in loop() only for _hop and
  327. // use _event=0 for non hop.
  328. //
  329. else if (intr == 0x00)
  330. {
  331. _event=0; // XXX 26/12/2017 !!! NEED
  332. }
  333. // Unkown Interrupt, so we have an error
  334. //
  335. else {
  336. #if DUSB>=1
  337. if (( debug>=0 ) && ( pdebug & P_SCAN )) {
  338. Serial.print(F("SCAN unknown:: "));
  339. SerialStat(intr);
  340. }
  341. #endif
  342. _state=S_SCAN;
  343. //_event=1; // XXX 06/03 loop until interrupt
  344. writeRegister(REG_IRQ_FLAGS_MASK, (uint8_t) 0x00);
  345. writeRegister(REG_IRQ_FLAGS, (uint8_t) 0xFF);
  346. }
  347. break; // S_SCAN
  348. // ----------------------------------------------------------------------------------------
  349. // S_CAD: In CAD mode we scan every SF for high RSSI until we have a DETECT.
  350. // Reason is the we received a CADDONE interrupt so we know a message is received
  351. // on the frequency but may be on another SF.
  352. //
  353. // If message is of the right frequency and SF, IRQ_LORA_CDDETD_MSAK interrupt
  354. // is raised, indicating that we can start beging reading the message from SPI.
  355. //
  356. // DIO0 interrupt IRQ_LORA_CDDONE_MASK in state S_CAD==2 means that we might have
  357. // a lock on the Freq but not the right SF. So we increase the SF
  358. //
  359. case S_CAD:
  360. // Intr=IRQ_LORA_CDDETD_MASK
  361. // We have to set the sf based on a strong RSSI for this channel
  362. // Also we set the state to S_RX and start receiving the message
  363. //
  364. if (intr & IRQ_LORA_CDDETD_MASK) {
  365. // Set RXDONE interrupt to dio0, RXTOUT to dio1
  366. writeRegister(REG_DIO_MAPPING_1, (
  367. MAP_DIO0_LORA_RXDONE |
  368. MAP_DIO1_LORA_RXTOUT |
  369. MAP_DIO2_LORA_NOP |
  370. MAP_DIO3_LORA_CRC ));
  371. // Accept no interrupts except RXDONE or RXTOUT
  372. _event=0;
  373. // if CDECT, make state S_RX so we wait for RXDONE intr
  374. writeRegister(REG_IRQ_FLAGS_MASK, (uint8_t) ~(
  375. IRQ_LORA_RXDONE_MASK |
  376. IRQ_LORA_RXTOUT_MASK |
  377. IRQ_LORA_HEADER_MASK |
  378. IRQ_LORA_CRCERR_MASK ));
  379. // Reset all interrupts as soon as possible
  380. // But listen ONLY to RXDONE and RXTOUT interrupts
  381. //writeRegister(REG_IRQ_FLAGS, IRQ_LORA_CDDETD_MASK | IRQ_LORA_RXDONE_MASK);
  382. // If we want to reset CRC, HEADER and RXTOUT flags as well
  383. writeRegister(REG_IRQ_FLAGS, (uint8_t) 0xFF ); // XXX 180326, reset all CAD Detect interrupt flags
  384. //_state = S_RX; // XXX 180521 Set state to start receiving
  385. opmode(OPMODE_RX_SINGLE); // set reg 0x01 to 0x06, initiate READ
  386. delayMicroseconds( RSSI_WAIT ); // Wait some microseconds less
  387. //delayMicroseconds( (0x01 << ((uint8_t)sf - 5 )) );
  388. rssi = readRegister(REG_RSSI); // Read the RSSI
  389. _rssi = rssi; // Read the RSSI in the state variable
  390. detTime = micros();
  391. #if DUSB>=1
  392. if (( debug>=1 ) && ( pdebug & P_CAD )) {
  393. Serial.print(F("CAD:: "));
  394. SerialStat(intr);
  395. }
  396. #endif
  397. _state = S_RX; // Set state to start receiving
  398. }// CDDETD
  399. // Intr == CADDONE
  400. // So we scan this SF and if not high enough ... next
  401. //
  402. else if (intr & IRQ_LORA_CDDONE_MASK) {
  403. // If this is not the max SF, increment the SF and try again
  404. // Depending on the frequency scheme this is for example SF8, SF10 or SF12
  405. // We expect on other SF get CDDETD
  406. //
  407. if (((uint8_t)sf) < freqs[ifreq].upHi) {
  408. sf = (sf_t)((uint8_t)sf+1); // Increment sf
  409. setRate(sf, 0x04); // Set SF with CRC==on
  410. // reset interrupt flags for CAD Done
  411. _event=0; // XXX 180324, when increasing SF loop, ws 0x00
  412. writeRegister(REG_IRQ_FLAGS_MASK, (uint8_t) 0x00); // Reset the interrupt mask
  413. //writeRegister(REG_IRQ_FLAGS, IRQ_LORA_CDDONE_MASK | IRQ_LORA_CDDETD_MASK);
  414. writeRegister(REG_IRQ_FLAGS, (uint8_t) 0xFF ); // This will prevent the CDDETD from being read
  415. opmode(OPMODE_CAD); // Scanning mode
  416. delayMicroseconds(RSSI_WAIT);
  417. rssi = readRegister(REG_RSSI); // Read the RSSI
  418. #if DUSB>=1
  419. if (( debug>=2 ) && ( pdebug & P_CAD )) {
  420. Serial.print(F("S_CAD:: CDONE, SF="));
  421. Serial.println(sf);
  422. }
  423. #endif
  424. }
  425. // If we reach the highest SF for the frequency plan,
  426. // we should go back to SCAN state
  427. //
  428. else {
  429. // Reset Interrupts
  430. _event=1; // reset soft intr, to state machine again
  431. writeRegister(REG_IRQ_FLAGS_MASK, (uint8_t) 0x00); // Reset the interrupt mask
  432. writeRegister(REG_IRQ_FLAGS, (uint8_t) 0xFF ); // or IRQ_LORA_CDDONE_MASK
  433. _state = S_SCAN; // As soon as we reach SF12 do something
  434. sf = (sf_t) freqs[ifreq].upLo;
  435. cadScanner(); // Which will reset SF to lowest SF
  436. #if DUSB>=1
  437. if (( debug>=2 ) && ( pdebug & P_CAD )) {
  438. Serial.print(F("CAD->SCAN:: "));
  439. SerialStat(intr);
  440. }
  441. #endif
  442. }
  443. doneTime = micros(); // We need CDDONE or other intr to reset timeout
  444. } //CAD CDDONE
  445. // if this interrupt is not CDECT or CDDONE then probably is 0x00
  446. // This means _event was set but there was no real interrupt (yet).
  447. // So we clear _event and wait for next (soft) interrupt.
  448. // We stay in the CAD state because CDDONE means something is
  449. // coming on this frequency so we wait on CDECT.
  450. //
  451. else if (intr == 0x00) {
  452. #if DUSB>=0
  453. if (( debug>=3 ) && ( pdebug & P_CAD )) {
  454. Serial.println("Err CAD:: intr is 0x00");
  455. }
  456. #endif
  457. _event=1; // Stay in CAD _state until real interrupt
  458. }
  459. // else we do not recognize the interrupt. We print an error
  460. // and restart scanning. If hop we even start at ifreq==1
  461. //
  462. else {
  463. #if DUSB>=1
  464. if (( debug>=0) && ( pdebug & P_CAD )) {
  465. Serial.print(F("Err CAD: Unknown::"));
  466. SerialStat(intr);
  467. }
  468. #endif
  469. _state = S_SCAN;
  470. sf = SF7;
  471. cadScanner(); // Scan and set SF7
  472. // Reset Interrupts
  473. _event=1; // If unknown interrupt, restarts
  474. writeRegister(REG_IRQ_FLAGS_MASK, (uint8_t) 0x00); // Reset the interrupt mask
  475. writeRegister(REG_IRQ_FLAGS, (uint8_t) 0xFF); // Reset all interrupts
  476. }
  477. break; //S_CAD
  478. // ----------------------------------------------------------------------------------------
  479. // If we receive an RXDONE interrupt on dio0 with state==S_RX
  480. // So we should handle the received message
  481. // Else if it is RXTOUT interrupt
  482. // So we handle this, and get modem out of standby
  483. // Else
  484. // Go back to SCAN
  485. //
  486. case S_RX:
  487. if (intr & IRQ_LORA_RXDONE_MASK) {
  488. #if CRCCHECK==1
  489. // We have to check for CRC error which will be visible AFTER RXDONE is set.
  490. // CRC errors might indicate that the reception is not OK.
  491. // Could be CRC error or message too large.
  492. // CRC error checking requires DIO3
  493. //
  494. if (intr & IRQ_LORA_CRCERR_MASK) {
  495. #if DUSB>=1
  496. if (( debug>=0 ) && ( pdebug & P_RX )) {
  497. Serial.print(F("Rx CRC err: "));
  498. SerialStat(intr);
  499. }
  500. #endif
  501. if (_cad) {
  502. sf = SF7;
  503. _state = S_SCAN;
  504. cadScanner();
  505. }
  506. else {
  507. _state = S_RX;
  508. rxLoraModem();
  509. }
  510. // Reset interrupts
  511. _event=0; // CRC error
  512. writeRegister(REG_IRQ_FLAGS_MASK, (uint8_t) 0x00); // Reset the interrupt mask
  513. writeRegister(REG_IRQ_FLAGS, (uint8_t)(
  514. IRQ_LORA_RXDONE_MASK |
  515. IRQ_LORA_RXTOUT_MASK |
  516. IRQ_LORA_HEADER_MASK |
  517. IRQ_LORA_CRCERR_MASK ));
  518. break;
  519. }// RX-CRC
  520. #endif // CRCCHECK
  521. // If we are here, no CRC error occurred, start timer
  522. #if DUSB>=1
  523. unsigned long ffTime = micros();
  524. #endif
  525. // There should not be an error in the message
  526. LoraUp.payLoad[0]= 0x00; // Empty the message
  527. // If receive S_RX error,
  528. // - print Error message
  529. // - Set _state to SCAN
  530. // - Set _event=1 so that we loop until we have an interrupt
  531. // - Reset the interrupts
  532. // - break
  533. if((LoraUp.payLength = receivePkt(LoraUp.payLoad)) <= 0) {
  534. #if DUSB>=1
  535. if (( debug>=1 ) && ( pdebug & P_RX )) {
  536. Serial.print(F("sMachine:: Error S-RX: "));
  537. Serial.print(F("payLength="));
  538. Serial.print(LoraUp.payLength);
  539. Serial.println();
  540. }
  541. #endif
  542. _event=1;
  543. writeRegister(REG_IRQ_FLAGS_MASK, (uint8_t) 0x00); // Reset the interrupt mask
  544. //writeRegister(REG_IRQ_FLAGS, (uint8_t)(
  545. // IRQ_LORA_RXDONE_MASK |
  546. // IRQ_LORA_RXTOUT_MASK |
  547. // IRQ_LORA_HEADER_MASK |
  548. // IRQ_LORA_CRCERR_MASK ));
  549. writeRegister(REG_IRQ_FLAGS, (uint8_t) 0xFF);
  550. _state = S_SCAN;
  551. break;
  552. }
  553. #if DUSB>=1
  554. if (( debug>=1 ) && ( pdebug & P_RX )) {
  555. Serial.print(F("R RXDONE in dT="));
  556. Serial.print(ffTime - detTime);
  557. Serial.print(F(": "));
  558. SerialStat(intr);
  559. }
  560. #endif
  561. // Do all register processing in this section
  562. uint8_t value = readRegister(REG_PKT_SNR_VALUE); // 0x19;
  563. if ( value & 0x80 ) { // The SNR sign bit is 1
  564. value = ( ( ~value + 1 ) & 0xFF ) >> 2; // Invert and divide by 4
  565. LoraUp.snr = -value;
  566. }
  567. else {
  568. // Divide by 4
  569. LoraUp.snr = ( value & 0xFF ) >> 2;
  570. }
  571. // Packet RSSI
  572. LoraUp.prssi = readRegister(REG_PKT_RSSI); // read register 0x1A, packet rssi
  573. // Correction of RSSI value based on chip used.
  574. if (sx1272) { // Is it a sx1272 radio?
  575. LoraUp.rssicorr = 139;
  576. } else { // Probably SX1276 or RFM95
  577. LoraUp.rssicorr = 157;
  578. }
  579. LoraUp.sf = readRegister(REG_MODEM_CONFIG2) >> 4;
  580. // If read was successful, read the package from the LoRa bus
  581. //
  582. if (receivePacket() <= 0) { // read is not successful
  583. #if DUSB>=1
  584. if (( debug>=0 ) && ( pdebug & P_RX )) {
  585. Serial.println(F("sMach:: Error receivePacket"));
  586. }
  587. #endif
  588. }
  589. // Set the modem to receiving BEFORE going back to user space.
  590. //
  591. if ((_cad) || (_hop)) {
  592. _state = S_SCAN;
  593. sf = SF7;
  594. cadScanner();
  595. }
  596. else {
  597. _state = S_RX;
  598. rxLoraModem();
  599. }
  600. writeRegister(REG_IRQ_FLAGS_MASK, (uint8_t) 0x00);
  601. writeRegister(REG_IRQ_FLAGS, (uint8_t) 0xFF); // Reset the interrupt mask
  602. eventTime=micros(); //There was an event for receive
  603. _event=0;
  604. }// RXDONE
  605. // RXOUT:
  606. // We did receive message receive timeout
  607. // This is the most common event in hop mode, possibly due to the fact
  608. // that receiving has started too late in the middle of a message
  609. // (according to the documentation). So is there a way to start receiving
  610. // immediately without delay.
  611. //
  612. else if (intr & IRQ_LORA_RXTOUT_MASK) {
  613. // Make sure we reset all interrupts
  614. // and get back to scanning
  615. _event=0; // Is set by interrupt handlers
  616. writeRegister(REG_IRQ_FLAGS_MASK, (uint8_t) 0x00 );
  617. writeRegister(REG_IRQ_FLAGS, (uint8_t) 0xFF); // reset all interrupts
  618. // If RXTOUT we put the modem in cad state and reset to SF7
  619. // If a timeout occurs here we reset the cadscanner
  620. //
  621. if ((_cad) || (_hop)) {
  622. // Set the state to CAD scanning
  623. #if DUSB>=1
  624. if (( debug>=2 ) && ( pdebug & P_RX )) {
  625. Serial.print(F("RXTOUT:: "));
  626. SerialStat(intr);
  627. }
  628. #endif
  629. sf = SF7;
  630. cadScanner(); // Start the scanner after RXTOUT
  631. _state = S_SCAN; // New state is scan
  632. }
  633. // If not in cad mode we are in single channel single sf mode.
  634. //
  635. else {
  636. _state = S_RX; // Receive when interrupted
  637. rxLoraModem();
  638. }
  639. eventTime=micros(); //There was an event for receive
  640. doneTime = micros(); // We need CDDONE or other intr to reset timeout
  641. }// RXTOUT
  642. else if (intr & IRQ_LORA_HEADER_MASK) {
  643. // This interrupt means we received an header successfully
  644. // which is normall an indication of RXDONE
  645. //writeRegister(REG_IRQ_FLAGS, IRQ_LORA_HEADER_MASK);
  646. #if DUSB>=1
  647. if (( debug>=3 ) && ( pdebug & P_RX )) {
  648. Serial.print(F("RX HEADER:: "));
  649. SerialStat(intr);
  650. }
  651. #endif
  652. //_event=1;
  653. }
  654. // If we did not receive a hard interrupt
  655. // Then probably do not do anything, because in the S_RX
  656. // state there always comes a RXTOUT or RXDONE interrupt
  657. //
  658. else if (intr == 0x00) {
  659. #if DUSB>=1
  660. if (( debug>=3) && ( pdebug & P_RX )) {
  661. Serial.print(F("S_RX no INTR:: "));
  662. SerialStat(intr);
  663. }
  664. #endif
  665. }
  666. // The interrupt received is not RXDONE, RXTOUT or HEADER
  667. // therefore we wait. Make sure to clear the interrupt
  668. // as HEADER interrupt comes just before RXDONE
  669. else {
  670. #if DUSB>=1
  671. if (( debug>=0 ) && ( pdebug & P_RX )) {
  672. Serial.print(F("R S_RX:: no RXDONE, RXTOUT, HEADER:: "));
  673. SerialStat(intr);
  674. }
  675. #endif
  676. //writeRegister(REG_IRQ_FLAGS_MASK, (uint8_t) 0x00 );
  677. //writeRegister(REG_IRQ_FLAGS, (uint8_t) 0xFF);
  678. }// int not RXDONE or RXTOUT
  679. break; // S_RX
  680. // ----------------------------------------------------------------------------------------
  681. // Start the transmission of a message in state S-TX
  682. // This is not an interrupt state, we use this state to start transmission
  683. // the interrupt TX-DONE tells us that the transmission was successful.
  684. // It therefore is no use to set _event==1 as transmission might
  685. // not be finished in the next loop iteration
  686. //
  687. case S_TX:
  688. // We need a timeout for this case. In case there does not come an interrupt,
  689. // then there will nog be a TXDONE but probably another CDDONE/CDDETD before
  690. // we have a timeout in the main program (Keep Alive)
  691. if (intr == 0x00) {
  692. #if DUSB>=1
  693. if (( debug>=2 ) && ( pdebug & P_TX )) {
  694. Serial.println(F("TX:: 0x00"));
  695. }
  696. #endif
  697. _event=1;
  698. _state=S_TXDONE;
  699. }
  700. // Set state to transmit
  701. _state = S_TXDONE;
  702. // Clear interrupt flags and masks
  703. writeRegister(REG_IRQ_FLAGS_MASK, (uint8_t) 0x00);
  704. writeRegister(REG_IRQ_FLAGS, (uint8_t) 0xFF); // reset interrupt flags
  705. // Initiate the transmission of the buffer (in Interrupt space)
  706. // We react on ALL interrupts if we are in TX state.
  707. txLoraModem(
  708. LoraDown.payLoad,
  709. LoraDown.payLength,
  710. LoraDown.tmst,
  711. LoraDown.sfTx,
  712. LoraDown.powe,
  713. LoraDown.fff,
  714. LoraDown.crc,
  715. LoraDown.iiq
  716. );
  717. // After filling the buffer we only react on TXDONE interrupt
  718. #if DUSB>=1
  719. if (( debug>=1 ) && ( pdebug & P_TX )) {
  720. Serial.print(F("T TX done:: "));
  721. SerialStat(intr);
  722. }
  723. #endif
  724. // More or less start at the "case TXDONE:" below
  725. _state=S_TXDONE;
  726. _event=1; // Or remove the break below
  727. break; // S_TX
  728. // ----------------------------------------------------------------------------------------
  729. // After the transmission is completed by the hardware,
  730. // the interrupt TXDONE is raised telling us that the tranmission
  731. // was successful.
  732. // If we receive an interrupt on dio0 _state==S_TX it is a TxDone interrupt
  733. // Do nothing with the interrupt, it is just an indication.
  734. // send Packet switch back to scanner mode after transmission finished OK
  735. //
  736. case S_TXDONE:
  737. if (intr & IRQ_LORA_TXDONE_MASK) {
  738. #if DUSB>=1
  739. if (( debug>=0 ) && ( pdebug & P_TX )) {
  740. Serial.print(F("T TXDONE:: rcvd="));
  741. Serial.print(micros());
  742. Serial.print(F(", diff="));
  743. Serial.println(micros()-LoraDown.tmst);
  744. if (debug>=2) Serial.flush();
  745. }
  746. #endif
  747. // After transmission reset to receiver
  748. if ((_cad) || (_hop)) { // XXX 26/02
  749. // Set the state to CAD scanning
  750. _state = S_SCAN;
  751. sf = SF7;
  752. cadScanner(); // Start the scanner after TX cycle
  753. }
  754. else {
  755. _state = S_RX;
  756. rxLoraModem();
  757. }
  758. _event=0;
  759. writeRegister(REG_IRQ_FLAGS_MASK, (uint8_t) 0x00);
  760. writeRegister(REG_IRQ_FLAGS, (uint8_t) 0xFF); // reset interrupt flags
  761. #if DUSB>=1
  762. if (( debug>=1 ) && ( pdebug & P_TX )) {
  763. Serial.println(F("T TXDONE:: done OK"));
  764. }
  765. #endif
  766. }
  767. // If a soft _event==0 interrupt and no transmission finished:
  768. else if ( intr != 0 ) {
  769. #if DUSB>=1
  770. if (( debug>=0 ) && ( pdebug & P_TX )) {
  771. Serial.print(F("T TXDONE:: unknown int:"));
  772. SerialStat(intr);
  773. }
  774. #endif
  775. writeRegister(REG_IRQ_FLAGS_MASK, (uint8_t) 0x00);
  776. writeRegister(REG_IRQ_FLAGS, (uint8_t) 0xFF); // reset interrupt flags
  777. _event=0;
  778. _state=S_SCAN;
  779. }
  780. // intr == 0
  781. else {
  782. // Increase timer. If timer exceeds certain value (7 seconds!), reset
  783. // After sending a message with S_TX, we have to receive a TXDONE interrupt
  784. // within 7 seconds according to spec, of here is a problem.
  785. if ( sendTime > micros() ) sendTime = 0; // This could be omitted for usigned ints
  786. if (( _state == S_TXDONE ) && (( micros() - sendTime) > 7000000 )) {
  787. #if DUSB>=1
  788. if (( debug>=1 ) && ( pdebug & P_TX )) {
  789. Serial.println(F("T TXDONE:: reset TX"));
  790. Serial.flush();
  791. }
  792. #endif
  793. startReceiver();
  794. }
  795. #if DUSB>=1
  796. if (( debug>=3 ) && ( pdebug & P_TX )) {
  797. Serial.println(F("T TXDONE:: No Interrupt"));
  798. }
  799. #endif
  800. }
  801. break; // S_TXDONE
  802. // ----------------------------------------------------------------------------------------
  803. // If _STATE is in an undefined state
  804. // If such a thing happens, we should re-init the interface and
  805. // make sure that we pick up next interrupt
  806. default:
  807. #if DUSB>=1
  808. if (( debug>=0) && ( pdebug & P_PRE )) {
  809. Serial.print("ERR state=");
  810. Serial.println(_state);
  811. }
  812. #endif
  813. if ((_cad) || (_hop)) {
  814. #if DUSB>=1
  815. if (debug>=0) {
  816. Serial.println(F("default:: Unknown _state "));
  817. SerialStat(intr);
  818. }
  819. #endif
  820. _state = S_SCAN;
  821. sf = SF7;
  822. cadScanner(); // Restart the state machine
  823. _event=0;
  824. }
  825. else // Single channel AND single SF
  826. {
  827. _state = S_RX;
  828. rxLoraModem();
  829. _event=0;
  830. }
  831. writeRegister(REG_IRQ_FLAGS_MASK, (uint8_t) 0x00);
  832. writeRegister(REG_IRQ_FLAGS, (uint8_t) 0xFF); // Reset all interrupts
  833. eventTime=micros(); // Reset event for unkonwn state
  834. break;// default
  835. }// switch(_state)
  836. return;
  837. }