Europalab Devices produces a LoRaWAN transmitting client node, specialised for higher research of actuator and sensor assisted IoT networks. https://dev.europalab.com/nlnet/20200000/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
6.5KB

  1. /**
  2. * \file main.c
  3. *
  4. * \brief Serial Provisioning of LoRaWAN Demo Application
  5. *
  6. * Copyright (c) 2020 Europalab Devices ApS
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * This file is part of Sendcomm.
  13. *
  14. * Sendcomm is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation, either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * Sendcomm is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with Sendcomm. If not, see <https://www.gnu.org/licenses/>.
  26. *
  27. * \asf_license_stop
  28. *
  29. */
  30. /****************************** INCLUDES **************************************/
  31. #include "system_low_power.h"
  32. #include "radio_driver_hal.h"
  33. #include "lorawan.h"
  34. #include "sys.h"
  35. #include "system_init.h"
  36. #include "system_assert.h"
  37. #include "aes_engine.h"
  38. #include "enddevice_demo.h"
  39. #include "sio2host.h"
  40. #include "extint.h"
  41. #include "conf_app.h"
  42. #include "sw_timer.h"
  43. #ifdef CONF_PMM_ENABLE
  44. #include "pmm.h"
  45. #include "conf_pmm.h"
  46. #include "sleep_timer.h"
  47. #include "sleep.h"
  48. #endif
  49. #include "conf_sio2host.h"
  50. #if (ENABLE_PDS == 1)
  51. #include "pds_interface.h"
  52. #endif
  53. #if (CERT_APP == 1)
  54. #include "conf_certification.h"
  55. #include "enddevice_cert.h"
  56. #endif
  57. /************************** Macro definition ***********************************/
  58. /* Button debounce time in ms */
  59. #define APP_DEBOUNCE_TIME 50
  60. /************************** Global variables ***********************************/
  61. bool button_pressed = false;
  62. bool factory_reset = false;
  63. bool bandSelected = false;
  64. uint32_t longPress = 0;
  65. uint8_t demoTimerId = 0xFF;
  66. uint8_t lTimerId = 0xFF;
  67. extern bool certAppEnabled;
  68. #ifdef CONF_PMM_ENABLE
  69. bool deviceResetsForWakeup = false;
  70. #endif
  71. /************************** Extern variables ***********************************/
  72. /************************** Function Prototypes ********************************/
  73. static void driver_init(void);
  74. #if (_DEBUG_ == 1)
  75. static void assertHandler(SystemAssertLevel_t level, uint16_t code);
  76. #endif /* #if (_DEBUG_ == 1) */
  77. /*********************************************************************//**
  78. \brief Uninitializes app resources before going to low power mode
  79. *************************************************************************/
  80. #ifdef CONF_PMM_ENABLE
  81. static void app_resources_uninit(void);
  82. #endif
  83. /****************************** FUNCTIONS **************************************/
  84. static void print_reset_causes(void)
  85. {
  86. enum system_reset_cause rcause = system_get_reset_cause();
  87. printf("Last reset cause: ");
  88. if(rcause & (1 << 6)) {
  89. printf("System Reset Request\r\n");
  90. }
  91. if(rcause & (1 << 5)) {
  92. printf("Watchdog Reset\r\n");
  93. }
  94. if(rcause & (1 << 4)) {
  95. printf("External Reset\r\n");
  96. }
  97. if(rcause & (1 << 2)) {
  98. printf("Brown Out 33 Detector Reset\r\n");
  99. }
  100. if(rcause & (1 << 1)) {
  101. printf("Brown Out 12 Detector Reset\r\n");
  102. }
  103. if(rcause & (1 << 0)) {
  104. printf("Power-On Reset\r\n");
  105. }
  106. }
  107. #ifdef CONF_PMM_ENABLE
  108. static void appWakeup(uint32_t sleptDuration)
  109. {
  110. HAL_Radio_resources_init();
  111. sio2host_init();
  112. printf("\r\nsleep_ok %ld ms\r\n", sleptDuration);
  113. }
  114. #endif
  115. #if (_DEBUG_ == 1)
  116. static void assertHandler(SystemAssertLevel_t level, uint16_t code)
  117. {
  118. printf("\r\n%04x\r\n", code);
  119. (void)level;
  120. }
  121. #endif /* #if (_DEBUG_ == 1) */
  122. /**
  123. * \mainpage
  124. * \section preface Preface
  125. * This is the reference manual for the LORAWAN Demo Application of EU Band
  126. */
  127. int main(void)
  128. {
  129. /* System Initialization */
  130. system_init();
  131. /* Initialize the delay driver */
  132. delay_init();
  133. /* Initialize the board target resources */
  134. board_init();
  135. INTERRUPT_GlobalInterruptEnable();
  136. /* Initialize Hardware and Software Modules */
  137. driver_init();
  138. /* Initialize the Serial Interface */
  139. sio2host_init();
  140. print_reset_causes();
  141. #if (_DEBUG_ == 1)
  142. SYSTEM_AssertSubscribe(assertHandler);
  143. #endif
  144. /* Initialize demo application */
  145. Stack_Init();
  146. SwTimerCreate(&demoTimerId);
  147. SwTimerCreate(&lTimerId);
  148. mote_demo_init();
  149. while (1)
  150. {
  151. serial_data_handler();
  152. SYSTEM_RunTasks();
  153. #ifdef CONF_PMM_ENABLE
  154. if (false == certAppEnabled)
  155. {
  156. if(bandSelected == true)
  157. {
  158. PMM_SleepReq_t sleepReq;
  159. /* Put the application to sleep */
  160. sleepReq.sleepTimeMs = DEMO_CONF_DEFAULT_APP_SLEEP_TIME_MS;
  161. sleepReq.pmmWakeupCallback = appWakeup;
  162. sleepReq.sleep_mode = CONF_PMM_SLEEPMODE_WHEN_IDLE;
  163. if (CONF_PMM_SLEEPMODE_WHEN_IDLE == SLEEP_MODE_STANDBY)
  164. {
  165. deviceResetsForWakeup = false;
  166. }
  167. if (true == LORAWAN_ReadyToSleep(deviceResetsForWakeup))
  168. {
  169. app_resources_uninit();
  170. if (PMM_SLEEP_REQ_DENIED == PMM_Sleep(&sleepReq))
  171. {
  172. HAL_Radio_resources_init();
  173. sio2host_init();
  174. /*printf("\r\nsleep_not_ok\r\n");*/
  175. }
  176. }
  177. }
  178. }
  179. #endif
  180. }
  181. }
  182. /* Initializes all the hardware and software modules used for Stack operation */
  183. static void driver_init(void)
  184. {
  185. /* Initialize the Radio Hardware */
  186. HAL_RadioInit();
  187. /* Initialize the AES Hardware Engine */
  188. AESInit();
  189. /* Initialize the Software Timer Module */
  190. SystemTimerInit();
  191. #ifdef CONF_PMM_ENABLE
  192. /* Initialize the Sleep Timer Module */
  193. SleepTimerInit();
  194. #endif
  195. #if (ENABLE_PDS == 1)
  196. /* PDS Module Init */
  197. PDS_Init();
  198. #endif
  199. }
  200. #ifdef CONF_PMM_ENABLE
  201. static void app_resources_uninit(void)
  202. {
  203. /* Disable USART TX and RX Pins */
  204. struct port_config pin_conf;
  205. port_get_config_defaults(&pin_conf);
  206. pin_conf.powersave = true;
  207. port_pin_set_config(HOST_SERCOM_PAD0_PIN, &pin_conf);
  208. port_pin_set_config(HOST_SERCOM_PAD1_PIN, &pin_conf);
  209. /* Disable UART module */
  210. sio2host_deinit();
  211. /* Disable Transceiver SPI Module */
  212. HAL_RadioDeInit();
  213. }
  214. #endif
  215. /**
  216. End of File
  217. */