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.

MyHwTeensy3.cpp 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #include "MyHwTeensy3.h"
  20. /*
  21. int8_t pinIntTrigger = 0;
  22. void wakeUp() //place to send the interrupts
  23. {
  24. pinIntTrigger = 1;
  25. }
  26. void wakeUp2() //place to send the second interrupts
  27. {
  28. pinIntTrigger = 2;
  29. }
  30. // Watchdog Timer interrupt service routine. This routine is required
  31. // to allow automatic WDIF and WDIE bit clearance in hardware.
  32. ISR (WDT_vect)
  33. {
  34. // WDIE & WDIF is cleared in hardware upon entering this ISR
  35. wdt_disable();
  36. }
  37. */
  38. bool hwInit(void)
  39. {
  40. #if !defined(MY_DISABLED_SERIAL)
  41. MY_SERIALDEVICE.begin(MY_BAUD_RATE);
  42. #if defined(MY_GATEWAY_SERIAL)
  43. while (!MY_SERIALDEVICE) {}
  44. #endif
  45. #endif
  46. return true;
  47. }
  48. void hwWatchdogReset(void)
  49. {
  50. // TODO: Not supported!
  51. }
  52. void hwReboot(void)
  53. {
  54. SCB_AIRCR = 0x05FA0004;
  55. while (true);
  56. }
  57. int8_t hwSleep(uint32_t ms)
  58. {
  59. // TODO: Not supported!
  60. (void)ms;
  61. return MY_SLEEP_NOT_POSSIBLE;
  62. }
  63. int8_t hwSleep(uint8_t interrupt, uint8_t mode, uint32_t ms)
  64. {
  65. // TODO: Not supported!
  66. (void)interrupt;
  67. (void)mode;
  68. (void)ms;
  69. return MY_SLEEP_NOT_POSSIBLE;
  70. }
  71. int8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2,
  72. uint32_t ms)
  73. {
  74. // TODO: Not supported!
  75. (void)interrupt1;
  76. (void)mode1;
  77. (void)interrupt2;
  78. (void)mode2;
  79. (void)ms;
  80. return MY_SLEEP_NOT_POSSIBLE;
  81. }
  82. bool hwUniqueID(unique_id_t *uniqueID)
  83. {
  84. #if defined(__MKL26Z64__)
  85. (void)memcpy((uint8_t *)uniqueID, &SIM_UIDMH, 12);
  86. (void)memset((uint8_t *)uniqueID + 12, MY_HWID_PADDING_BYTE, 4);
  87. #else
  88. (void)memcpy((uint8_t *)uniqueID, &SIM_UIDH, 16);
  89. #endif
  90. return true;
  91. }
  92. uint16_t hwCPUVoltage(void)
  93. {
  94. analogReference(DEFAULT);
  95. analogReadResolution(12);
  96. analogReadAveraging(32);
  97. #if defined(__MK20DX128__) || defined(__MK20DX256__)
  98. // Teensy 3.0/3.1/3.2
  99. return 1195 * 4096 / analogRead(39);
  100. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  101. // Teensy 3.6
  102. return 1195 * 4096 / analogRead(71);
  103. #elif defined(__MKL26Z64__)
  104. // Teensy LC
  105. // not supported
  106. return FUNCTION_NOT_SUPPORTED;
  107. #else
  108. // not supported
  109. return FUNCTION_NOT_SUPPORTED;
  110. #endif
  111. }
  112. uint16_t hwCPUFrequency(void)
  113. {
  114. // TODO: currently reporting compile time frequency (in 1/10MHz)
  115. return F_CPU / 100000UL;
  116. }
  117. int8_t hwCPUTemperature(void)
  118. {
  119. return -127; // not implemented yet
  120. }
  121. uint16_t hwFreeMem(void)
  122. {
  123. // TODO: Not supported!
  124. return FUNCTION_NOT_SUPPORTED;
  125. }
  126. #if defined(MY_HW_HAS_GETENTROPY)
  127. ssize_t hwGetentropy(void *__buffer, const size_t __length)
  128. {
  129. SIM_SCGC6 |= SIM_SCGC6_RNGA; // enable RNG
  130. RNG_CR &= ~RNG_CR_SLP_MASK;
  131. RNG_CR |= RNG_CR_HA_MASK; //high assurance, not needed
  132. size_t pos = 0;
  133. while (pos < __length) {
  134. RNG_CR |= RNG_CR_GO_MASK;
  135. while (!(RNG_SR & RNG_SR_OREG_LVL(0xF)));
  136. const uint32_t rndVar = RNG_OR;
  137. const uint8_t bsize = (__length - pos) > sizeof(rndVar) ? sizeof(rndVar) : (__length - pos);
  138. (void)memcpy((uint8_t *)__buffer + pos, &rndVar, bsize);
  139. pos += bsize;
  140. }
  141. SIM_SCGC6 &= ~SIM_SCGC6_RNGA; // disable RNG
  142. return pos;
  143. }
  144. #endif
  145. void hwRandomNumberInit(void)
  146. {
  147. #if defined(MY_HW_HAS_GETENTROPY)
  148. // use HW RNG present on Teensy3.5/3.6
  149. // init RNG
  150. uint32_t seed = 0;
  151. hwGetentropy(&seed, sizeof(seed));
  152. randomSeed(seed);
  153. #else
  154. randomSeed(analogRead(MY_SIGNING_SOFT_RANDOMSEED_PIN));
  155. #endif
  156. }
  157. void hwDebugPrint(const char *fmt, ...)
  158. {
  159. #ifndef MY_DISABLED_SERIAL
  160. char fmtBuffer[MY_SERIAL_OUTPUT_SIZE];
  161. #ifdef MY_GATEWAY_SERIAL
  162. // prepend debug message to be handled correctly by controller (C_INTERNAL, I_LOG_MESSAGE)
  163. snprintf_P(fmtBuffer, sizeof(fmtBuffer), PSTR("0;255;%" PRIu8 ";0;%" PRIu8 ";%" PRIu32 " "),
  164. C_INTERNAL, I_LOG_MESSAGE, hwMillis());
  165. MY_DEBUGDEVICE.print(fmtBuffer);
  166. #else
  167. // prepend timestamp
  168. MY_DEBUGDEVICE.print(hwMillis());
  169. MY_DEBUGDEVICE.print(F(" "));
  170. #endif
  171. va_list args;
  172. va_start(args, fmt);
  173. vsnprintf_P(fmtBuffer, sizeof(fmtBuffer), fmt, args);
  174. #ifdef MY_GATEWAY_SERIAL
  175. // Truncate message if this is gateway node
  176. fmtBuffer[sizeof(fmtBuffer) - 2] = '\n';
  177. fmtBuffer[sizeof(fmtBuffer) - 1] = '\0';
  178. #endif
  179. va_end(args);
  180. MY_DEBUGDEVICE.print(fmtBuffer);
  181. #else
  182. (void)fmt;
  183. #endif
  184. }