Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
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.

MyMainESP8266.cpp 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * The MySensors Arduino library handles the wireless radio link and protocol
  3. * between your home built sensors/actuators and HA controller of choice.
  4. * The sensors forms a self healing radio network with optional repeaters. Each
  5. * repeater and gateway builds a routing tables in EEPROM which keeps track of the
  6. * network topology allowing messages to be routed to nodes.
  7. *
  8. * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
  9. * Copyright (C) 2013-2018 Sensnology AB
  10. * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
  11. *
  12. * Documentation: http://www.mysensors.org
  13. * Support Forum: http://forum.mysensors.org
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * version 2 as published by the Free Software Foundation.
  18. */
  19. //This may be used to change user task stack size:
  20. //#define CONT_STACKSIZE 4096
  21. #include <Arduino.h>
  22. #include "Schedule.h"
  23. extern "C" {
  24. #include "ets_sys.h"
  25. #include "os_type.h"
  26. #include "osapi.h"
  27. #include "mem.h"
  28. #include "user_interface.h"
  29. #include "cont.h"
  30. }
  31. #include <core_version.h>
  32. #include "gdb_hooks.h"
  33. #define LOOP_TASK_PRIORITY 1
  34. #define LOOP_QUEUE_SIZE 1
  35. #define OPTIMISTIC_YIELD_TIME_US 16000
  36. extern "C" void call_user_start();
  37. extern void loop();
  38. extern void setup();
  39. extern void(*__init_array_start)(void);
  40. extern void(*__init_array_end)(void);
  41. /* Not static, used in Esp.cpp */
  42. struct rst_info resetInfo;
  43. /* Not static, used in core_esp8266_postmortem.c.
  44. * Placed into noinit section because we assign value to this variable
  45. * before .bss is zero-filled, and need to preserve the value.
  46. */
  47. cont_t* g_pcont __attribute__((section(".noinit")));
  48. /* Event queue used by the main (arduino) task */
  49. static os_event_t s_loop_queue[LOOP_QUEUE_SIZE];
  50. /* Used to implement optimistic_yield */
  51. static uint32_t s_micros_at_task_start;
  52. extern "C" {
  53. extern const uint32_t __attribute__((section(".ver_number"))) core_version =
  54. ARDUINO_ESP8266_GIT_VER;
  55. const char* core_release =
  56. #ifdef ARDUINO_ESP8266_RELEASE
  57. ARDUINO_ESP8266_RELEASE;
  58. #else
  59. NULL;
  60. #endif
  61. } // extern "C"
  62. void initVariant() __attribute__((weak));
  63. void initVariant()
  64. {
  65. }
  66. void preloop_update_frequency() __attribute__((weak));
  67. void preloop_update_frequency()
  68. {
  69. #if defined(F_CPU) && (F_CPU == 160000000L)
  70. REG_SET_BIT(0x3ff00014, BIT(0));
  71. ets_update_cpu_frequency(160);
  72. #endif
  73. }
  74. extern "C" void esp_yield()
  75. {
  76. if (cont_can_yield(g_pcont)) {
  77. cont_yield(g_pcont);
  78. }
  79. }
  80. extern "C" void esp_schedule()
  81. {
  82. ets_post(LOOP_TASK_PRIORITY, 0, 0);
  83. }
  84. extern "C" void __yield()
  85. {
  86. if (cont_can_yield(g_pcont)) {
  87. esp_schedule();
  88. esp_yield();
  89. } else {
  90. panic();
  91. }
  92. }
  93. extern "C" void yield(void) __attribute__((weak, alias("__yield")));
  94. extern "C" void optimistic_yield(uint32_t interval_us)
  95. {
  96. if (cont_can_yield(g_pcont) &&
  97. (system_get_time() - s_micros_at_task_start) > interval_us) {
  98. yield();
  99. }
  100. }
  101. static void loop_wrapper()
  102. {
  103. static bool setup_done = false;
  104. preloop_update_frequency();
  105. if (!setup_done) {
  106. _begin(); // Startup MySensors library
  107. setup_done = true;
  108. }
  109. _process(); // Process incoming data
  110. loop();
  111. run_scheduled_functions();
  112. esp_schedule();
  113. }
  114. static void loop_task(os_event_t *events)
  115. {
  116. (void)events;
  117. s_micros_at_task_start = system_get_time();
  118. cont_run(g_pcont, &loop_wrapper);
  119. if (cont_check(g_pcont) != 0) {
  120. panic();
  121. }
  122. }
  123. static void do_global_ctors(void)
  124. {
  125. void(**p)(void) = &__init_array_end;
  126. while (p != &__init_array_start) {
  127. (*--p)();
  128. }
  129. }
  130. void init_done()
  131. {
  132. system_set_os_print(1);
  133. gdb_init();
  134. do_global_ctors();
  135. esp_schedule();
  136. }
  137. /* This is the entry point of the application.
  138. * It gets called on the default stack, which grows down from the top
  139. * of DRAM area.
  140. * .bss has not been zeroed out yet, but .data and .rodata are in place.
  141. * Cache is not enabled, so only ROM and IRAM functions can be called.
  142. * Peripherals (except for SPI0 and UART0) are not initialized.
  143. * This function does not return.
  144. */
  145. /*
  146. A bit of explanation for this entry point:
  147. SYS is the SDK task/context used by the upperlying system to run its
  148. administrative tasks (at least WLAN and lwip's receive callbacks and
  149. Ticker). NONOS-SDK is designed to run user's non-threaded code in
  150. another specific task/context with its own stack in BSS.
  151. Some clever fellows found that the SYS stack was a large and quite unused
  152. piece of ram that we could use for the user's stack instead of using user's
  153. main memory, thus saving around 4KB on ram/heap.
  154. A problem arose later, which is that this stack can heavily be used by
  155. the SDK for some features. One of these features is WPS. We still don't
  156. know if other features are using this, or if this memory is going to be
  157. used in future SDK releases.
  158. WPS beeing flawed by its poor security, or not beeing used by lots of
  159. users, it has been decided that we are still going to use that memory for
  160. user's stack and disable the use of WPS, with an option to revert that
  161. back at the user's discretion. This selection can be done with the
  162. global define NO_EXTRA_4K_HEAP. An option has been added to the board
  163. generator script.
  164. References:
  165. https://github.com/esp8266/Arduino/pull/4553
  166. https://github.com/esp8266/Arduino/pull/4622
  167. https://github.com/esp8266/Arduino/issues/4779
  168. https://github.com/esp8266/Arduino/pull/4889
  169. */
  170. #ifdef NO_EXTRA_4K_HEAP
  171. /* this is the default NONOS-SDK user's heap location */
  172. cont_t g_cont __attribute__((aligned(16)));
  173. #endif
  174. extern "C" void ICACHE_RAM_ATTR app_entry(void)
  175. {
  176. #ifdef NO_EXTRA_4K_HEAP
  177. /* this is the default NONOS-SDK user's heap location */
  178. g_pcont = &g_cont;
  179. #else
  180. /* Allocate continuation context on this SYS stack,
  181. and save pointer to it. */
  182. cont_t s_cont __attribute__((aligned(16)));
  183. g_pcont = &s_cont;
  184. #endif
  185. /* Call the entry point of the SDK code. */
  186. call_user_start();
  187. }
  188. extern "C" void user_init(void)
  189. {
  190. struct rst_info *rtc_info_ptr = system_get_rst_info();
  191. memcpy((void *)&resetInfo, (void *)rtc_info_ptr, sizeof(resetInfo));
  192. uart_div_modify(0, UART_CLK_FREQ / (115200));
  193. init();
  194. initVariant();
  195. cont_init(g_pcont);
  196. ets_task(loop_task,
  197. LOOP_TASK_PRIORITY, s_loop_queue,
  198. LOOP_QUEUE_SIZE);
  199. system_init_done_cb(&init_done);
  200. }