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.

AltSoftSerial.cpp 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /* An Alternative Software Serial Library
  2. * http://www.pjrc.com/teensy/td_libs_AltSoftSerial.html
  3. * Copyright (c) 2014 PJRC.COM, LLC, Paul Stoffregen, paul@pjrc.com
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. // Revisions are now tracked on GitHub
  24. // https://github.com/PaulStoffregen/AltSoftSerial
  25. //
  26. // Version 1.2: Support Teensy 3.x
  27. //
  28. // Version 1.1: Improve performance in receiver code
  29. //
  30. // Version 1.0: Initial Release
  31. #include "AltSoftSerial.h"
  32. #include "config/AltSoftSerial_Boards.h"
  33. #include "config/AltSoftSerial_Timers.h"
  34. /****************************************/
  35. /** Initialization **/
  36. /****************************************/
  37. static uint16_t ticks_per_bit=0;
  38. bool AltSoftSerial::timing_error=false;
  39. static uint8_t rx_state;
  40. static uint8_t rx_byte;
  41. static uint8_t rx_bit = 0;
  42. static uint16_t rx_target;
  43. static uint16_t rx_stop_ticks=0;
  44. static volatile uint8_t rx_buffer_head;
  45. static volatile uint8_t rx_buffer_tail;
  46. #define RX_BUFFER_SIZE 80
  47. static volatile uint8_t rx_buffer[RX_BUFFER_SIZE];
  48. static volatile uint8_t tx_state=0;
  49. static uint8_t tx_byte;
  50. static uint8_t tx_bit;
  51. static volatile uint8_t tx_buffer_head;
  52. static volatile uint8_t tx_buffer_tail;
  53. #define TX_BUFFER_SIZE 68
  54. static volatile uint8_t tx_buffer[TX_BUFFER_SIZE];
  55. #ifndef INPUT_PULLUP
  56. #define INPUT_PULLUP INPUT
  57. #endif
  58. #define MAX_COUNTS_PER_BIT 6241 // 65536 / 10.5
  59. void AltSoftSerial::init(uint32_t cycles_per_bit)
  60. {
  61. //Serial.printf("cycles_per_bit = %d\n", cycles_per_bit);
  62. if (cycles_per_bit < MAX_COUNTS_PER_BIT) {
  63. CONFIG_TIMER_NOPRESCALE();
  64. } else {
  65. cycles_per_bit /= 8;
  66. //Serial.printf("cycles_per_bit/8 = %d\n", cycles_per_bit);
  67. if (cycles_per_bit < MAX_COUNTS_PER_BIT) {
  68. CONFIG_TIMER_PRESCALE_8();
  69. } else {
  70. #if defined(CONFIG_TIMER_PRESCALE_256)
  71. cycles_per_bit /= 32;
  72. //Serial.printf("cycles_per_bit/256 = %d\n", cycles_per_bit);
  73. if (cycles_per_bit < MAX_COUNTS_PER_BIT) {
  74. CONFIG_TIMER_PRESCALE_256();
  75. } else {
  76. return; // baud rate too low for AltSoftSerial
  77. }
  78. #elif defined(CONFIG_TIMER_PRESCALE_128)
  79. cycles_per_bit /= 16;
  80. //Serial.printf("cycles_per_bit/128 = %d\n", cycles_per_bit);
  81. if (cycles_per_bit < MAX_COUNTS_PER_BIT) {
  82. CONFIG_TIMER_PRESCALE_128();
  83. } else {
  84. return; // baud rate too low for AltSoftSerial
  85. }
  86. #else
  87. return; // baud rate too low for AltSoftSerial
  88. #endif
  89. }
  90. }
  91. ticks_per_bit = cycles_per_bit;
  92. rx_stop_ticks = cycles_per_bit * 37 / 4;
  93. hwPinMode(INPUT_CAPTURE_PIN, INPUT_PULLUP);
  94. hwDigitalWrite(OUTPUT_COMPARE_A_PIN, HIGH);
  95. hwPinMode(OUTPUT_COMPARE_A_PIN, OUTPUT);
  96. rx_state = 0;
  97. rx_buffer_head = 0;
  98. rx_buffer_tail = 0;
  99. tx_state = 0;
  100. tx_buffer_head = 0;
  101. tx_buffer_tail = 0;
  102. ENABLE_INT_INPUT_CAPTURE();
  103. }
  104. void AltSoftSerial::end(void)
  105. {
  106. DISABLE_INT_COMPARE_B();
  107. DISABLE_INT_INPUT_CAPTURE();
  108. flushInput();
  109. flushOutput();
  110. DISABLE_INT_COMPARE_A();
  111. // TODO: restore timer to original settings?
  112. }
  113. /****************************************/
  114. /** Transmission **/
  115. /****************************************/
  116. void AltSoftSerial::writeByte(uint8_t b)
  117. {
  118. uint8_t intr_state, head;
  119. head = tx_buffer_head + 1;
  120. if (head >= TX_BUFFER_SIZE) {
  121. head = 0;
  122. }
  123. while (tx_buffer_tail == head) ; // wait until space in buffer
  124. intr_state = SREG;
  125. cli();
  126. if (tx_state) {
  127. tx_buffer[head] = b;
  128. tx_buffer_head = head;
  129. } else {
  130. tx_state = 1;
  131. tx_byte = b;
  132. tx_bit = 0;
  133. ENABLE_INT_COMPARE_A();
  134. CONFIG_MATCH_CLEAR();
  135. SET_COMPARE_A(GET_TIMER_COUNT() + 16);
  136. }
  137. SREG = intr_state;
  138. }
  139. ISR(COMPARE_A_INTERRUPT)
  140. {
  141. uint8_t state, byte, bit, head, tail;
  142. uint16_t target;
  143. state = tx_state;
  144. byte = tx_byte;
  145. target = GET_COMPARE_A();
  146. while (state < 10) {
  147. target += ticks_per_bit;
  148. if (state < 9) {
  149. bit = byte & 1;
  150. } else {
  151. bit = 1; // stopbit
  152. }
  153. byte >>= 1;
  154. state++;
  155. if (bit != tx_bit) {
  156. if (bit) {
  157. CONFIG_MATCH_SET();
  158. } else {
  159. CONFIG_MATCH_CLEAR();
  160. }
  161. SET_COMPARE_A(target);
  162. tx_bit = bit;
  163. tx_byte = byte;
  164. tx_state = state;
  165. // TODO: how to detect timing_error?
  166. return;
  167. }
  168. }
  169. head = tx_buffer_head;
  170. tail = tx_buffer_tail;
  171. if (head == tail) {
  172. if (state == 10) {
  173. // Wait for final stop bit to finish
  174. tx_state = 11;
  175. SET_COMPARE_A(target + ticks_per_bit);
  176. } else {
  177. tx_state = 0;
  178. CONFIG_MATCH_NORMAL();
  179. DISABLE_INT_COMPARE_A();
  180. }
  181. } else {
  182. if (++tail >= TX_BUFFER_SIZE) {
  183. tail = 0;
  184. }
  185. tx_buffer_tail = tail;
  186. tx_byte = tx_buffer[tail];
  187. tx_bit = 0;
  188. CONFIG_MATCH_CLEAR();
  189. if (state == 10) {
  190. SET_COMPARE_A(target + ticks_per_bit);
  191. } else {
  192. SET_COMPARE_A(GET_TIMER_COUNT() + 16);
  193. }
  194. tx_state = 1;
  195. // TODO: how to detect timing_error?
  196. }
  197. }
  198. void AltSoftSerial::flushOutput(void)
  199. {
  200. while (tx_state) /* wait */ ;
  201. }
  202. /****************************************/
  203. /** Reception **/
  204. /****************************************/
  205. ISR(CAPTURE_INTERRUPT)
  206. {
  207. uint8_t state, bit;
  208. uint16_t capture;
  209. capture = GET_INPUT_CAPTURE();
  210. bit = rx_bit;
  211. if (bit) {
  212. CONFIG_CAPTURE_FALLING_EDGE();
  213. rx_bit = 0;
  214. } else {
  215. CONFIG_CAPTURE_RISING_EDGE();
  216. rx_bit = 0x80;
  217. }
  218. state = rx_state;
  219. if (state == 0) {
  220. if (!bit) {
  221. uint16_t end = capture + rx_stop_ticks;
  222. SET_COMPARE_B(end);
  223. ENABLE_INT_COMPARE_B();
  224. rx_target = capture + ticks_per_bit + ticks_per_bit/2;
  225. rx_state = 1;
  226. }
  227. } else {
  228. uint16_t target = rx_target;
  229. const uint16_t offset_overflow = 65535 - ticks_per_bit;
  230. while (1) {
  231. const uint16_t offset = capture - target;
  232. if (offset > offset_overflow) {
  233. break;
  234. }
  235. rx_byte = (rx_byte >> 1) | rx_bit;
  236. target += ticks_per_bit;
  237. state++;
  238. if (state >= 9) {
  239. DISABLE_INT_COMPARE_B();
  240. uint8_t head = rx_buffer_head + 1;
  241. if (head >= RX_BUFFER_SIZE) {
  242. head = 0;
  243. }
  244. if (head != rx_buffer_tail) {
  245. rx_buffer[head] = rx_byte;
  246. rx_buffer_head = head;
  247. }
  248. CONFIG_CAPTURE_FALLING_EDGE();
  249. rx_bit = 0;
  250. rx_state = 0;
  251. return;
  252. }
  253. }
  254. rx_target = target;
  255. rx_state = state;
  256. }
  257. //if (GET_TIMER_COUNT() - capture > ticks_per_bit) AltSoftSerial::timing_error = true;
  258. }
  259. ISR(COMPARE_B_INTERRUPT)
  260. {
  261. uint8_t head, state, bit;
  262. DISABLE_INT_COMPARE_B();
  263. CONFIG_CAPTURE_FALLING_EDGE();
  264. state = rx_state;
  265. bit = rx_bit ^ 0x80;
  266. while (state < 9) {
  267. rx_byte = (rx_byte >> 1) | bit;
  268. state++;
  269. }
  270. head = rx_buffer_head + 1;
  271. if (head >= RX_BUFFER_SIZE) {
  272. head = 0;
  273. }
  274. if (head != rx_buffer_tail) {
  275. rx_buffer[head] = rx_byte;
  276. rx_buffer_head = head;
  277. }
  278. rx_state = 0;
  279. CONFIG_CAPTURE_FALLING_EDGE();
  280. rx_bit = 0;
  281. }
  282. int AltSoftSerial::read(void)
  283. {
  284. uint8_t head, tail, out;
  285. head = rx_buffer_head;
  286. tail = rx_buffer_tail;
  287. if (head == tail) {
  288. return -1;
  289. }
  290. if (++tail >= RX_BUFFER_SIZE) {
  291. tail = 0;
  292. }
  293. out = rx_buffer[tail];
  294. rx_buffer_tail = tail;
  295. return out;
  296. }
  297. int AltSoftSerial::peek(void)
  298. {
  299. uint8_t head, tail;
  300. head = rx_buffer_head;
  301. tail = rx_buffer_tail;
  302. if (head == tail) {
  303. return -1;
  304. }
  305. if (++tail >= RX_BUFFER_SIZE) {
  306. tail = 0;
  307. }
  308. return rx_buffer[tail];
  309. }
  310. int AltSoftSerial::available(void)
  311. {
  312. uint8_t head, tail;
  313. head = rx_buffer_head;
  314. tail = rx_buffer_tail;
  315. if (head >= tail) {
  316. return head - tail;
  317. }
  318. return RX_BUFFER_SIZE + head - tail;
  319. }
  320. void AltSoftSerial::flushInput(void)
  321. {
  322. rx_buffer_head = rx_buffer_tail;
  323. }
  324. #ifdef ALTSS_USE_FTM0
  325. void ftm0_isr(void)
  326. {
  327. uint32_t flags = FTM0_STATUS;
  328. FTM0_STATUS = 0;
  329. if (flags & (1<<0) && (FTM0_C0SC & 0x40)) {
  330. altss_compare_b_interrupt();
  331. }
  332. if (flags & (1<<5)) {
  333. altss_capture_interrupt();
  334. }
  335. if (flags & (1<<6) && (FTM0_C6SC & 0x40)) {
  336. altss_compare_a_interrupt();
  337. }
  338. }
  339. #endif