Açıklama Yok
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 8.2KB

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