Sin descripción
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.

main.c 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #include <avr/sleep.h>
  4. #include <util/delay.h>
  5. #include <stdint.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdbool.h>
  10. #include "led.h"
  11. #include "spi.h"
  12. #include "nrf24l01.h"
  13. #include "nrf24l01_definitions.h"
  14. #include "bme280_interface.h"
  15. #include "bme280_defs.h"
  16. #include "pin_programming.h"
  17. #include "crc.h"
  18. #include "encryption.h"
  19. uint8_t ownId;
  20. const uint8_t encryptionKey[16] = {0x9e, 0x37, 0x79, 0xb9, 0x9b, 0x97, 0x73, 0xe9, 0xb9, 0x79, 0x37, 0x9e, 0x6b, 0x69, 0x51, 0x56}; /* TODO: use exernal file with the keys */
  21. uint64_t salt;
  22. char bool_case = 0;
  23. int timer = 0;
  24. int timer_max = 0;
  25. volatile uint8_t cycle = 0;
  26. volatile bool nrfInterruptRaised;
  27. volatile uint8_t interruptCounter;
  28. volatile uint8_t executionFlag;
  29. static uint8_t messageBuffer[PACKET_LENGTH];
  30. static PACKET * packet = (PACKET*) messageBuffer;
  31. void Enter_Power_Save_Mode(void);
  32. void Exit_Power_Save_Mode(void);
  33. bool Send_Get_Salt_Message(PACKET * packet, uint64_t salt); //TODO: put into own file
  34. bool Read_Salt_Message(PACKET * packet, uint64_t * salt);
  35. void Set_Up_Power_Save_Mode(void);
  36. void Enter_Power_Save_Mode(void);
  37. ISR(TIMER2_COMPA_vect)
  38. {
  39. /* Do nothing as the interrupt is only used to wake up the MCU. */
  40. }
  41. ISR(PCINT2_vect)
  42. {
  43. nrfInterruptRaised = true;
  44. }
  45. int main (void)
  46. {
  47. struct bme280_data sensorData;
  48. uint16_t crc;
  49. bool saltReceived = false;
  50. /* Enable the debug LED */
  51. LED_DDR |= (1 << LED_PIN);
  52. /* Get the own ID */
  53. Configure_Pin_Programming_Pins();
  54. ownId = Get_Own_Identifier();
  55. /* Initialize the SPI */
  56. Initialize_SPI();
  57. /* Initialize the nrf24l01 */
  58. Initialize_NRF24L01();
  59. // The NRF24L01 is now in the mode Standby-I.
  60. /* Configure the transmission parameters (Enhanced ShockBurst)*/
  61. Configure_Transmission(ownId);
  62. /* Initialize the BME280 */
  63. Initialize_BME280();
  64. Set_Up_Power_Save_Mode();
  65. /* Initialize the salt */
  66. salt = 0x0ull;
  67. salt |= ownId;
  68. /* TODO: Request a salt from the base station:
  69. * - loop as long as no reply from the base station was received:
  70. * - send the packet that requests the salt
  71. * - if no ack is received for the packet go direclty to sleep for 100 ms
  72. * - if no salt is received within 100 ms send the message again
  73. * - keep the LED on for the duration to allow the user to see that the device is ready for operation.
  74. */
  75. LED_PORT |= (1 << LED_PIN);
  76. do
  77. {
  78. if (Send_Get_Salt_Message(packet, salt) == true)
  79. {
  80. memset(messageBuffer, 0x0F, 32);
  81. if (NRF24L01_Receive_Message(messageBuffer, 100 /*ms*/) == true)
  82. {
  83. _delay_ms(10);
  84. saltReceived = Read_Salt_Message(packet, &salt);
  85. }
  86. }
  87. else
  88. {
  89. /* TODO: enter power save mode instead to make the battery life longer */
  90. _delay_ms(100);
  91. }
  92. } while (saltReceived == false);
  93. LED_PORT &= ~(1 << LED_PIN);
  94. salt &= 0xFFFFFFFFFFFFFF00ull;
  95. salt |= ownId;
  96. /* Delay the change of the operating frequency by the function Enter_Power_Save_Mode for the
  97. * first function pass. If it is changed before the ISP can flash the MCU the clocks of the ISP
  98. * and MCU are mismatched and the flashing will fail.
  99. */
  100. _delay_ms(500);
  101. while(1)
  102. {
  103. Enter_Power_Save_Mode(); // The MCU enters the Power Save Mode here.
  104. Exit_Power_Save_Mode(); // The MCU exits the Power Save Mode here.
  105. if (cycle == 0) // TODO cycle == 7 to execute this every 60 s
  106. {
  107. /* Re-Initialize the peripherals */
  108. Set_BME280_Pins();
  109. Set_NRF24L01_Pins();
  110. /* Get measurement and send it */
  111. BME280_Get_Measurement(&sensorData);
  112. /* TODO: put the following into a Send_Report function */
  113. memset((uint8_t*)packet, 0, PACKET_LENGTH); //Reinitialize the buffer with zeros
  114. salt &= ~(1ull<<63);
  115. packet->salt = salt;
  116. packet->payload.reportData.packetIdentifier.elementCount = 3;
  117. packet->payload.reportData.packetIdentifier.packetType = PACKET_TYPE_REPORT;
  118. /* Fill in the payload */
  119. packet->payload.reportData.valueTypeTemperature = VALUE_TYPE_TEMPERATURE;
  120. packet->payload.reportData.temperature = sensorData.temperature/10;
  121. packet->payload.reportData.valueTypePressure = VALUE_TYPE_PRESSURE;
  122. packet->payload.reportData.pressure = sensorData.pressure;
  123. packet->payload.reportData.valueTypeHumidity = VALUE_TYPE_HUMIDITY;
  124. packet->payload.reportData.humidity = sensorData.humidity * 100/1024;
  125. /* Calculate the CRC */
  126. crc = Calculate_Crc(packet->payload.buffer, PACKET_PAYLOAD_BUFFER_LENGTH);
  127. packet->crc = crc;
  128. /* Encrypt the packet */
  129. /* TODO:
  130. * - increment the salt for every packet
  131. * - Receive salt from the base station
  132. */
  133. Encrypt((uint32_t*) packet->payload.buffer,
  134. PACKET_PAYLOAD_BUFFER_LENGTH + sizeof(crc),
  135. salt, (uint32_t*) encryptionKey);
  136. NRF24L01_Send_Message((uint8_t*)packet, PACKET_LENGTH);
  137. /* Increment salt */
  138. salt += (1 << 8);
  139. LED_PORT |= (1 << LED_PIN);
  140. _delay_ms(100); /* TODO: only for debugging, remove this later! */
  141. LED_PORT &= ~(1 << LED_PIN);
  142. cycle = 0;
  143. }
  144. else
  145. {
  146. cycle ++;
  147. }
  148. }
  149. }
  150. void Set_Up_Power_Save_Mode(void)
  151. {
  152. // Disable Brown-Out-Detection by setting the BODLEVEL 2:0 Fuses to 0b111 (should be default)
  153. // Disable the on-chip debug system by setting the DWEN Fuse to 1 (should be default)
  154. /* Disable some unused peripherals that are not used during operation of the weather station: */
  155. // The ADC is turned off by default
  156. ACSR &= ~(1<<ACD); // Analog comperator
  157. ACSR &= ~(1<<ACIE); // The interrupt bit has to be cleared after switchin of the analog comperator
  158. /*
  159. * The Internal voltager reference is automatically disabled if the BOD, ADC and Voltage
  160. * Reference are disabled
  161. */
  162. /* Set up Timer/Counter2 */
  163. PRR &= ~(1<<PRTIM2); // Enable the timer 2 in the Power Reduction Register
  164. TCCR2A |= (1<<COM2A1)|(1<<COM2A0)|(1<<WGM21); // Set the timer to ClearTimer on Compare Match with output compare mode for channel A
  165. /* There is a problem: The maximum time until the counter 2 overflows is 262ms at the nominal
  166. * core frquency of 1MHz and a timer prescaler of 1024.
  167. * This can be attenuated by lowering the system frequency to 31.25 kHz via the clock prescaler
  168. * (CLKPS3...0) of the register CLKPR. This gives a overflow time of 8.3s.
  169. * The cycle time of one minute can thus be accomplished by setting the output compare register
  170. * to 229. This spawns an interrupt every 7.5s which means the operation has to be executed
  171. * every 8 interrupt calls.
  172. */
  173. TCCR2B |= (1<<CS22)|(1<<CS21)|(1<<CS20); // Set the timer prescaler to 1/1024
  174. OCR2A = 229; // TODO: calculate this number from the wanted cycle time and the timer frequency.
  175. TIMSK2 |= (1<<OCIE2A); // Enable the Output Compare Match A Interrupt.
  176. /* Enable global interrupts: */
  177. sei();
  178. }
  179. void Enter_Power_Save_Mode(void)
  180. {
  181. PRR |= (1<<PRTWI) | (1<<PRTIM0) | (1<<PRTIM1) | (1<<PRSPI) | (1<<PRUSART0) | (1<<PRADC); /* Only timer 2 is needed for wake-up interrupt */
  182. /* Set the clock prescaler for a frequency of 32.25 kHz*/
  183. CLKPR = (1<<CLKPCE);
  184. CLKPR = (1<<CLKPS3);
  185. TCNT2 = 0;// Reset timer 2
  186. TIMSK2 |= (1<<OCIE2A); // Enable the Output Compare Match A Interrupt.
  187. set_sleep_mode(SLEEP_MODE_PWR_SAVE);
  188. sleep_mode();
  189. }
  190. void Exit_Power_Save_Mode(void)
  191. {
  192. TIMSK2 &= ~(1<<OCIE2A); // Disable the Output Compare Match A Interrupt.
  193. /* Set the normal operating frequency of 1 MHz */
  194. CLKPR = (1<<CLKPCE);
  195. CLKPR = (1<<CLKPS1) | (1<<CLKPS0);
  196. PRR &= ~(1<<PRSPI); // Enable SPI
  197. Initialize_SPI(); // reinitalize SPI
  198. }
  199. bool Send_Get_Salt_Message(PACKET * packet, uint64_t salt) //TODO: put into own file
  200. {
  201. bool success = false;
  202. uint16_t crc;
  203. memset((uint8_t*)packet, 0, PACKET_LENGTH); //Reinitialize the buffer with zeros
  204. salt &= ~(1ull<<63); /* Set the most significant bit to 0 to indicate a packet from the device to the base station */
  205. packet->salt = salt;
  206. packet->payload.reportData.packetIdentifier.elementCount = 0;
  207. packet->payload.reportData.packetIdentifier.packetType = PACKET_TYPE_GET_SALT;
  208. /* Calculate the CRC */
  209. crc = Calculate_Crc(packet->payload.buffer, PACKET_PAYLOAD_BUFFER_LENGTH);
  210. packet->crc = crc;
  211. Encrypt((uint32_t*) packet->payload.buffer,
  212. PACKET_PAYLOAD_BUFFER_LENGTH + sizeof(crc),
  213. salt,
  214. (uint32_t*) encryptionKey);
  215. success = NRF24L01_Send_Message((uint8_t*)packet, PACKET_LENGTH);
  216. return success;
  217. }
  218. bool Read_Salt_Message(PACKET * packet, uint64_t * salt)
  219. {
  220. uint16_t crcRemainder = 0xFFFF;
  221. uint64_t baseStationSalt = 0x0;
  222. /* TODO: check that the packet originated from the base station by checking the id */
  223. baseStationSalt = packet->salt;
  224. Decrypt((uint32_t*)packet->payload.buffer,
  225. PACKET_PAYLOAD_BUFFER_LENGTH + sizeof(packet->crc),
  226. baseStationSalt,
  227. (uint32_t*) encryptionKey);
  228. crcRemainder = Calculate_Crc(packet->payload.buffer,
  229. PACKET_PAYLOAD_BUFFER_LENGTH + sizeof(packet->crc));
  230. if (crcRemainder != 0)
  231. {
  232. return false;
  233. }
  234. if ((packet->payload.saltData.packetIdentifier.packetType != PACKET_TYPE_SALT) ||
  235. (packet->payload.saltData.packetIdentifier.elementCount != 0))
  236. {
  237. return false;
  238. }
  239. memcpy((uint8_t*)salt, packet->payload.saltData.salt, 7);
  240. *salt = (*salt) << 8;
  241. return true;
  242. }