app.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /**
  2. ******************************************************************************
  3. * Tranceiver Project
  4. ******************************************************************************
  5. * This project works as a 433MHz tranceiver between a Raspberry Pi and a
  6. * 433MHz tranceiver module. Target to run on a slower low power STM32L-CPU
  7. * System clock is 32MHz
  8. *
  9. * SysTick-counter is setup to handle the LED-flashing
  10. * The systick int is 1mS and it calles the LED-Handler
  11. *
  12. * TIM2 is set to sample the Rx-input in a 10uS period. TIM2_IRQHandler()
  13. * HW is set to 32 prescaler + 10 counter period
  14. *
  15. * TIM3 handles the micros() function by counting a 1mS variable.
  16. * HW is set to 32 prescaler + 1000 counter period
  17. *
  18. * The USART1 is used for comm to R-Pi. Also handled with interrupts.
  19. *
  20. * IO-Pins:
  21. * PA1 Output ENABLE
  22. * PA2 Output TX_ENABLE
  23. * PA3 Output TX_DATA
  24. * PB3 Input RX_DATA
  25. * PB6 Output LED_BLUE
  26. * PB7 Output LED_GREEN
  27. *
  28. ******************************************************************************
  29. */
  30. #include <stdio.h>
  31. #include "sw_fifo.h"
  32. #include "app.h"
  33. #include "main.h"
  34. #include "queue.h"
  35. #include "led_blink.h"
  36. #include "nexa.h"
  37. #include "temp1.h"
  38. #include "temp2.h"
  39. #include "oregon.h"
  40. #include "transmit.h"
  41. #include "stm32l1xx_hal.h"
  42. #include "stm32l1xx_hal_gpio.h"
  43. volatile unsigned long width;
  44. boolean disableRx = true;
  45. void setup() {
  46. disableRx = true;
  47. digitalWrite(enablePin, LOW);
  48. digitalWrite(txEnablePin, LOW);
  49. digitalWrite(txPin, LOW);
  50. // ------------------ INIT THE TRANCEIVER -------------------------
  51. // From powerdown mode (pin 4-5-6 low),
  52. // 1. Drive high pin 6 (ENABLE)
  53. // 2. After 20us drive high pin 5 (RX/TX)200us, hold on 40us
  54. // 3. Drive down 20us pin 6 (ENABLE).
  55. digitalWrite(enablePin, HIGH); delayMicroseconds( 20 );
  56. digitalWrite(txEnablePin, HIGH); delayMicroseconds( 200 ); digitalWrite(txEnablePin, LOW);
  57. delayMicroseconds( 40 );
  58. digitalWrite(enablePin, LOW); delayMicroseconds( 20 ); digitalWrite(enablePin, HIGH);
  59. delayMicroseconds( 200 );
  60. // ------------------ INIT THE TRANCEIVER -------------------------
  61. rcvInitQueue();
  62. disableRx = false;
  63. }
  64. /*
  65. //button = (int)HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);
  66. digitalWrite(enablePin, blue);
  67. //HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,(GPIO_PinState)blue); //blue);
  68. */
  69. // Debug
  70. __IO unsigned long long data[11] = {0,0,0,0,0,0,0,0,0,0,0};
  71. __IO int data_cnt=0;
  72. //********************************************************************
  73. // LOOP
  74. //
  75. // Test-code: <NT1B5C1D83>
  76. //********************************************************************
  77. #define SER_RX_MODE_WAIT 0
  78. #define SER_RX_MODE_RCV 1
  79. #define SER_RX_MODE_END 2
  80. void loop() {
  81. static int rxMode = SER_RX_MODE_WAIT;
  82. static int rxNum = 0;
  83. static unsigned char rxChars[20];
  84. static unsigned long last_rx_time = 0;
  85. static unsigned char txDataReady = 0;
  86. static unsigned long previous_x_data = 0;
  87. static unsigned long old_time=0;
  88. static unsigned long previous_temp1_x_data = 0;
  89. static unsigned long temp1_old_time=0;
  90. static char hex[100];
  91. static boolean firstRun = true;
  92. static unsigned int width;
  93. //static OregonDecoderV2 orscV2;
  94. //static OregonDecoderV3 orscV3;
  95. static unsigned long previous_now = 0; // Get the current time
  96. unsigned long now = millis(); // Get the current time
  97. // Check millis() for wrapping (after 49 days)
  98. if( previous_now > now ) {
  99. last_rx_time = 0; // Reset last rx time
  100. }
  101. // Display version information 5 sec after startup
  102. if( firstRun == true && now > 5000 ) {
  103. printf("\n\r433MHz STM32L Receiver module. Ver: 140111 08:02\n\r"); // **** VERSION ****
  104. firstRun = false;
  105. }
  106. if( rcvDeQueue(&width) ) {
  107. // Oregon V3
  108. // ********** TEMP1 ***********
  109. if( nextPulseTemp1(width) ) {
  110. last_rx_time = now; // Set last rx time to now
  111. if( temp1_x_data != previous_temp1_x_data || now > (temp1_old_time+1000) ) {
  112. sprintf(hex,"<TR%08lX>",temp1_x_data);
  113. printf(hex);
  114. previous_temp1_x_data = temp1_x_data;
  115. blinkTheLED();
  116. }
  117. temp1_old_time = now;
  118. temp1ResetDecoder();
  119. }
  120. // ********** TEMP2 ***********
  121. if( nextPulseTemp2(width) ) {
  122. last_rx_time = now; // Set last rx time to now
  123. sprintf(hex,"<Tr%016llX>",temp2_x_data);
  124. printf(hex);
  125. blinkTheLED();
  126. temp2ResetDecoder();
  127. }
  128. // ***************** NEXA *********************
  129. if( nextPulseNexa(width) ) {
  130. last_rx_time = now; // Set last rx time to now
  131. if( x_data != previous_x_data || now > (old_time+1000) ) {
  132. sprintf(hex,"<NR%08lX>\n\r",x_data);
  133. printf(hex);
  134. previous_x_data = x_data;
  135. }
  136. old_time = now;
  137. resetDecoder();
  138. blinkTheLED();
  139. }
  140. }
  141. // Serial receive of the available buffer, only if we are not waiting to transmit something
  142. if( serialAvailable() > 0 && txDataReady == 0 ) {
  143. unsigned char c = serialRead();
  144. // Reset if we receive an '%'
  145. if( rxMode == SER_RX_MODE_WAIT && c == '%' ) {
  146. HAL_NVIC_SystemReset();
  147. }
  148. switch( rxMode ) {
  149. case SER_RX_MODE_WAIT:
  150. if( c == '<' ) {
  151. rxMode = SER_RX_MODE_RCV;
  152. rxNum = 0;
  153. }
  154. break;
  155. case SER_RX_MODE_RCV:
  156. if( (c >= '0' && c<= '9') || (c >= 'A' && c<= 'Z') ) {
  157. rxChars[rxNum++] = c;
  158. if( rxNum == 1 && rxChars[0] != 'N' ) {
  159. rxMode = SER_RX_MODE_WAIT;
  160. rxNum = 0;
  161. }
  162. else if( rxNum == 2 && rxChars[1] != 'T' ) {
  163. rxMode = SER_RX_MODE_WAIT;
  164. rxNum = 0;
  165. }
  166. else if( rxNum == 10 ) {
  167. rxMode = SER_RX_MODE_END;
  168. }
  169. }
  170. else {
  171. rxMode = SER_RX_MODE_WAIT;
  172. rxNum = 0;
  173. }
  174. break;
  175. case SER_RX_MODE_END:
  176. if( c == '>' ) {
  177. rxChars[rxNum] = '\0';
  178. txDataReady = 1; // Flag that we have something to transmit
  179. }
  180. rxMode = SER_RX_MODE_WAIT;
  181. rxNum = 0;
  182. break;
  183. }
  184. }
  185. if( txDataReady == 1 && (now > (last_rx_time+100)) ) { // Transmit if it has passed more than 50mS since last rx
  186. txDataReady = 0;
  187. blinkTheTxLED();
  188. sendNexaCodeStr(rxChars);
  189. }
  190. previous_now = now; // Store to check for wrapping of millis()
  191. }
  192. // ********************* ARCHIVE **********************
  193. /*
  194. void loop() {
  195. __IO int button;
  196. static unsigned int width;
  197. static unsigned long last_rx_time = 0;
  198. static unsigned long old_time=0;
  199. static unsigned long previous_x_data = 0;
  200. static unsigned char txDataReady = 0;
  201. static unsigned long txData = 0;
  202. static unsigned long previous_now = 0; // Get the current time
  203. unsigned long now = millis();
  204. // Check millis() for wrapping (after 49 days)
  205. if( previous_now > now ) {
  206. last_rx_time = 0; // Reset last rx time
  207. }
  208. // NEXA RX Loop
  209. if( rcvDeQueue(&width) ) {
  210. // ***************** TEMP1 *********************
  211. if( nextPulseTemp1(width) ) {
  212. static unsigned long long previous_temp1_x_data = 0;
  213. //if( previous_temp1_x_data != temp1_x_data ) {
  214. temp1_x_data = (temp1_x_data & 0x0FFF); // Just keep the temperature
  215. if( data_cnt<10 ) {
  216. data[data_cnt] = temp1_x_data;
  217. data_cnt++;
  218. }
  219. else {
  220. data_cnt=0;
  221. }
  222. blinkTheLED();
  223. //}
  224. previous_temp1_x_data = temp1_x_data;
  225. temp1ResetDecoder();
  226. }
  227. // ***************** NEXA *********************
  228. if( nextPulseNexa(width) ) {
  229. last_rx_time = now; // Set last rx time to now
  230. if( x_data != previous_x_data || now > (old_time+1000) ) {
  231. previous_x_data = x_data;
  232. // We have received a nexa code.
  233. }
  234. old_time = now;
  235. resetDecoder();
  236. blinkTheLED();
  237. }
  238. }
  239. if( txDataReady == 1 && (now > (last_rx_time+100)) ) { // Transmit if it has passed more than 50mS since last rx
  240. txDataReady = 0;
  241. sendNexaCode(txData);
  242. }
  243. previous_now = now; // Store to check for wrapping of millis()
  244. }
  245. */
  246. /*
  247. button = (int)HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);
  248. if( button == 1 ) {
  249. sendNexaCode(708298896UL);
  250. }
  251. */
  252. /*
  253. volatile NEXA_QUEUE_DATA qd;
  254. qd.remote_id = (x_data & 0xFFFFFFC0) >>6;
  255. qd.group = (x_data & 0x00000020) >>5;
  256. qd.onOff = (x_data & 0x00000010) >>4;
  257. qd.channel = (x_data & 0x0000000C) >>2;
  258. qd.button = (x_data & 0x00000003);
  259. if( qd.remote_id == 11067170 && // This is the id of the white newer 8-button Nexa remote
  260. qd.group == 0 &&
  261. qd.channel == 0 &&
  262. qd.button == 0 ) {
  263. // We got the trigger code ! Yohoo
  264. // Now resend the same code (with one higher button value)
  265. qd.button++;
  266. //qd.remote_id -= 120;
  267. txData = ((qd.remote_id << 6) & 0xFFFFFFC0 );
  268. txData |= ((qd.group << 5) & 0x00000020 );
  269. txData |= ((qd.onOff << 4) & 0x00000010 );
  270. txData |= ((qd.channel << 2) & 0x0000000C );
  271. txData |= ((qd.button ) & 0x00000003 );
  272. txDataReady = 1; // Flag that we have something to transmit
  273. }
  274. */