app.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. resetOV3Decoder();
  63. disableRx = false;
  64. }
  65. /*
  66. //button = (int)HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);
  67. digitalWrite(enablePin, blue);
  68. //HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,(GPIO_PinState)blue); //blue);
  69. */
  70. // Debug
  71. __IO unsigned long long data[11] = {0,0,0,0,0,0,0,0,0,0,0};
  72. __IO int data_cnt=0;
  73. //********************************************************************
  74. // LOOP
  75. //
  76. // Test-code: <NT1B5C1D83>
  77. //********************************************************************
  78. #define SER_RX_MODE_WAIT 0
  79. #define SER_RX_MODE_RCV 1
  80. #define SER_RX_MODE_END 2
  81. void loop() {
  82. static int rxMode = SER_RX_MODE_WAIT;
  83. static int rxNum = 0;
  84. static unsigned char rxChars[20];
  85. static unsigned long last_rx_time = 0;
  86. static unsigned char txDataReady = 0;
  87. static unsigned long previous_x_data = 0;
  88. static unsigned long old_time=0;
  89. static unsigned long previous_temp1_x_data = 0;
  90. static unsigned long temp1_old_time=0;
  91. static char hex[100];
  92. static boolean firstRun = true;
  93. static unsigned int width;
  94. //static OregonDecoderV2 orscV2;
  95. //static OregonDecoderV3 orscV3;
  96. static unsigned long previous_now = 0; // Get the current time
  97. unsigned long now = millis(); // Get the current time
  98. // Check millis() for wrapping (after 49 days)
  99. if( previous_now > now ) {
  100. last_rx_time = 0; // Reset last rx time
  101. }
  102. // Display version information 5 sec after startup
  103. if( firstRun == true && now > 5000 ) {
  104. printf("\n\r433MHz STM32L Receiver module. Ver: 140111 08:02\n\r"); // **** VERSION ****
  105. firstRun = false;
  106. }
  107. if( rcvDeQueue(&width) ) {
  108. // ---------- Oregon V3 --------------
  109. if( nextPulseoregonV3(width) ) {
  110. unsigned char count;
  111. const unsigned char* data = getOV3Data(&count);
  112. sprintf(hex,"<RR");
  113. if( count == 10 ) {
  114. for( int i=0; i<count; i++ ) {
  115. sprintf(hex+3+(i*2),"%02X",data[i] );
  116. }
  117. sprintf(hex+3+(10*2),">" );
  118. printf(hex);
  119. }
  120. resetOV3Decoder();
  121. }
  122. // ********** TEMP1 ***********
  123. if( nextPulseTemp1(width) ) {
  124. last_rx_time = now; // Set last rx time to now
  125. if( temp1_x_data != previous_temp1_x_data || now > (temp1_old_time+1000) ) {
  126. sprintf(hex,"<TR%08lX>",temp1_x_data);
  127. printf(hex);
  128. previous_temp1_x_data = temp1_x_data;
  129. blinkTheLED();
  130. }
  131. temp1_old_time = now;
  132. temp1ResetDecoder();
  133. }
  134. // ********** TEMP2 ***********
  135. if( nextPulseTemp2(width) ) {
  136. last_rx_time = now; // Set last rx time to now
  137. sprintf(hex,"<Tr%016llX>",temp2_x_data);
  138. printf(hex);
  139. blinkTheLED();
  140. temp2ResetDecoder();
  141. }
  142. // ***************** NEXA *********************
  143. if( nextPulseNexa(width) ) {
  144. last_rx_time = now; // Set last rx time to now
  145. if( x_data != previous_x_data || now > (old_time+1000) ) {
  146. sprintf(hex,"<NR%08lX>\n\r",x_data);
  147. printf(hex);
  148. previous_x_data = x_data;
  149. }
  150. old_time = now;
  151. resetDecoder();
  152. blinkTheLED();
  153. }
  154. }
  155. // Serial receive of the available buffer, only if we are not waiting to transmit something
  156. if( serialAvailable() > 0 && txDataReady == 0 ) {
  157. unsigned char c = serialRead();
  158. // Reset if we receive an '%'
  159. if( rxMode == SER_RX_MODE_WAIT && c == '%' ) {
  160. HAL_NVIC_SystemReset();
  161. }
  162. switch( rxMode ) {
  163. case SER_RX_MODE_WAIT:
  164. if( c == '<' ) {
  165. rxMode = SER_RX_MODE_RCV;
  166. rxNum = 0;
  167. }
  168. break;
  169. case SER_RX_MODE_RCV:
  170. if( (c >= '0' && c<= '9') || (c >= 'A' && c<= 'Z') ) {
  171. rxChars[rxNum++] = c;
  172. if( rxNum == 1 && rxChars[0] != 'N' ) {
  173. rxMode = SER_RX_MODE_WAIT;
  174. rxNum = 0;
  175. }
  176. else if( rxNum == 2 && rxChars[1] != 'T' ) {
  177. rxMode = SER_RX_MODE_WAIT;
  178. rxNum = 0;
  179. }
  180. else if( rxNum == 10 ) {
  181. rxMode = SER_RX_MODE_END;
  182. }
  183. }
  184. else {
  185. rxMode = SER_RX_MODE_WAIT;
  186. rxNum = 0;
  187. }
  188. break;
  189. case SER_RX_MODE_END:
  190. if( c == '>' ) {
  191. rxChars[rxNum] = '\0';
  192. txDataReady = 1; // Flag that we have something to transmit
  193. }
  194. rxMode = SER_RX_MODE_WAIT;
  195. rxNum = 0;
  196. break;
  197. }
  198. }
  199. if( txDataReady == 1 && (now > (last_rx_time+100)) ) { // Transmit if it has passed more than 50mS since last rx
  200. txDataReady = 0;
  201. blinkTheTxLED();
  202. sendNexaCodeStr(rxChars);
  203. }
  204. previous_now = now; // Store to check for wrapping of millis()
  205. }
  206. // ********************* ARCHIVE **********************
  207. /*
  208. void loop() {
  209. __IO int button;
  210. static unsigned int width;
  211. static unsigned long last_rx_time = 0;
  212. static unsigned long old_time=0;
  213. static unsigned long previous_x_data = 0;
  214. static unsigned char txDataReady = 0;
  215. static unsigned long txData = 0;
  216. static unsigned long previous_now = 0; // Get the current time
  217. unsigned long now = millis();
  218. // Check millis() for wrapping (after 49 days)
  219. if( previous_now > now ) {
  220. last_rx_time = 0; // Reset last rx time
  221. }
  222. // NEXA RX Loop
  223. if( rcvDeQueue(&width) ) {
  224. // ***************** TEMP1 *********************
  225. if( nextPulseTemp1(width) ) {
  226. static unsigned long long previous_temp1_x_data = 0;
  227. //if( previous_temp1_x_data != temp1_x_data ) {
  228. temp1_x_data = (temp1_x_data & 0x0FFF); // Just keep the temperature
  229. if( data_cnt<10 ) {
  230. data[data_cnt] = temp1_x_data;
  231. data_cnt++;
  232. }
  233. else {
  234. data_cnt=0;
  235. }
  236. blinkTheLED();
  237. //}
  238. previous_temp1_x_data = temp1_x_data;
  239. temp1ResetDecoder();
  240. }
  241. // ***************** NEXA *********************
  242. if( nextPulseNexa(width) ) {
  243. last_rx_time = now; // Set last rx time to now
  244. if( x_data != previous_x_data || now > (old_time+1000) ) {
  245. previous_x_data = x_data;
  246. // We have received a nexa code.
  247. }
  248. old_time = now;
  249. resetDecoder();
  250. blinkTheLED();
  251. }
  252. }
  253. if( txDataReady == 1 && (now > (last_rx_time+100)) ) { // Transmit if it has passed more than 50mS since last rx
  254. txDataReady = 0;
  255. sendNexaCode(txData);
  256. }
  257. previous_now = now; // Store to check for wrapping of millis()
  258. }
  259. */
  260. /*
  261. button = (int)HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);
  262. if( button == 1 ) {
  263. sendNexaCode(708298896UL);
  264. }
  265. */
  266. /*
  267. volatile NEXA_QUEUE_DATA qd;
  268. qd.remote_id = (x_data & 0xFFFFFFC0) >>6;
  269. qd.group = (x_data & 0x00000020) >>5;
  270. qd.onOff = (x_data & 0x00000010) >>4;
  271. qd.channel = (x_data & 0x0000000C) >>2;
  272. qd.button = (x_data & 0x00000003);
  273. if( qd.remote_id == 11067170 && // This is the id of the white newer 8-button Nexa remote
  274. qd.group == 0 &&
  275. qd.channel == 0 &&
  276. qd.button == 0 ) {
  277. // We got the trigger code ! Yohoo
  278. // Now resend the same code (with one higher button value)
  279. qd.button++;
  280. //qd.remote_id -= 120;
  281. txData = ((qd.remote_id << 6) & 0xFFFFFFC0 );
  282. txData |= ((qd.group << 5) & 0x00000020 );
  283. txData |= ((qd.onOff << 4) & 0x00000010 );
  284. txData |= ((qd.channel << 2) & 0x0000000C );
  285. txData |= ((qd.button ) & 0x00000003 );
  286. txDataReady = 1; // Flag that we have something to transmit
  287. }
  288. */