Geen omschrijving
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 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 PACKET reportPacket;
  30. void Enter_Power_Save_Mode(void);
  31. void Exit_Power_Save_Mode(void);
  32. /* TODO Notes for power saving:
  33. * - Power-save-mode needed -> SM2...0 bits written to 011
  34. * - Entering by issuing the SLEEP instruction -> What call in C?
  35. * - Before executing the SLEEP instruction, write bit SE of the SMCR to 1
  36. * - Let Timer/Counter2 run with the necessary period and enable an interrupt.
  37. * -> The Global Interrupt Enable bit in SREG has to be set.
  38. * - asynchronous/synchronous clock source?
  39. * - When waking up
  40. * - Set PRR bits for needed peripherals
  41. * -
  42. */
  43. void Set_Up_Power_Save_Mode(void);
  44. void Enter_Power_Save_Mode(void);
  45. ISR(TIMER2_COMPA_vect)
  46. {
  47. /* Do nothing as the interrupt is only used to wake up the MCU. */
  48. }
  49. ISR(PCINT2_vect)
  50. {
  51. nrfInterruptRaised = true;
  52. }
  53. int main (void)
  54. {
  55. struct bme280_data sensorData;
  56. uint16_t crc;
  57. /* Enable the debug LED */
  58. LED_DDR |= (1 << LED_PIN);
  59. /* Get the own ID */
  60. Configure_Pin_Programming_Pins();
  61. ownId = Get_Own_Identifier();
  62. /* Initialize the SPI */
  63. Initialize_SPI();
  64. /* Initialize the nrf24l01 */
  65. Initialize_NRF24L01();
  66. // The NRF24L01 is now in the mode Standby-I.
  67. /* Configure the transmission parameters (Enhanced ShockBurst)*/
  68. Configure_Transmission(ownId);
  69. /* Initialize the BME280 */
  70. Initialize_BME280();
  71. Set_Up_Power_Save_Mode();
  72. /* Initialize the salt */
  73. salt = 0xFFFFFFFFFFFFFFFFull;
  74. salt &= ~ownId;
  75. /* Delay the change of the operating frequency by the function Enter_Power_Save_Mode for the
  76. * first function pass. If it is changed before the ISP can flash the MCU the clocks of the ISP
  77. * and MCU are mismatched and the flashing will fail.
  78. */
  79. _delay_ms(500);
  80. while(1)
  81. {
  82. Enter_Power_Save_Mode(); // The MCU enters the Power Save Mode here.
  83. Exit_Power_Save_Mode(); // The MCU exits the Power Save Mode here.
  84. if (cycle == 0) // TODO cycle == 7 to execute this every 60 s
  85. {
  86. /* Re-Initialize the peripherals */
  87. Set_BME280_Pins();
  88. Set_NRF24L01_Pins();
  89. /* Get measurement and send it */
  90. BME280_Get_Measurement(&sensorData);
  91. memset((uint8_t*)&reportPacket, 0, sizeof(reportPacket)); //Reinitialize the buffer with zeros
  92. salt &= ~(1ull<<55);
  93. reportPacket.salt = salt;
  94. reportPacket.payload.values.packetIdentifier.elementCount = 3;
  95. reportPacket.payload.values.packetIdentifier.packetType = PACKET_TYPE_REPORT;
  96. /* Fill in the payload */
  97. reportPacket.payload.values.valueTypeTemperature = VALUE_TYPE_TEMPERATURE;
  98. reportPacket.payload.values.temperature = sensorData.temperature/10;
  99. reportPacket.payload.values.valueTypePressure = VALUE_TYPE_PRESSURE;
  100. reportPacket.payload.values.pressure = sensorData.pressure;
  101. reportPacket.payload.values.valueTypeHumidity = VALUE_TYPE_HUMIDITY;
  102. reportPacket.payload.values.humidity = sensorData.humidity * 100/1024;
  103. /* Calculate the CRC */
  104. crc = Calculate_Crc(reportPacket.payload.buffer, PACKET_BUFFER_LENGTH);
  105. reportPacket.crc = crc;
  106. /* Encrypt the packet */
  107. /* TODO:
  108. * - increment the salt for every packet
  109. * - Receive salt from the base station
  110. */
  111. Encrypt((uint32_t*) &reportPacket.payload.buffer,
  112. PACKET_BUFFER_LENGTH + sizeof(crc),
  113. salt, (uint32_t*) encryptionKey);
  114. NRF24L01_Send_Message((uint8_t*)&reportPacket, sizeof(reportPacket));
  115. _delay_ms(100); /* TODO: only for debugging, remove this later! */
  116. LED_PORT &= ~(1 << LED_PIN);
  117. cycle = 0;
  118. }
  119. else
  120. {
  121. cycle ++;
  122. }
  123. }
  124. }
  125. void Set_Up_Power_Save_Mode(void)
  126. {
  127. // Disable Brown-Out-Detection by setting the BODLEVEL 2:0 Fuses to 0b111 (should be default)
  128. // Disable the on-chip debug system by setting the DWEN Fuse to 1 (should be default)
  129. /* Disable some unused peripherals that are not used during operation of the weather station: */
  130. // The ADC is turned off by default
  131. ACSR &= ~(1<<ACD); // Analog comperator
  132. ACSR &= ~(1<<ACIE); // The interrupt bit has to be cleared after switchin of the analog comperator
  133. /*
  134. * The Internal voltager reference is automatically disabled if the BOD, ADC and Voltage
  135. * Reference are disabled
  136. */
  137. /* Set up Timer/Counter2 */
  138. PRR &= ~(1<<PRTIM2); // Enable the timer 2 in the Power Reduction Register
  139. TCCR2A |= (1<<COM2A1)|(1<<COM2A0)|(1<<WGM21); // Set the timer to ClearTimer on Compare Match with output compare mode for channel A
  140. /* There is a problem: The maximum time until the counter 2 overflows is 262ms at the nominal
  141. * core frquency of 1MHz and a timer prescaler of 1024.
  142. * This can be attenuated by lowering the system frequency to 31.25 kHz via the clock prescaler
  143. * (CLKPS3...0) of the register CLKPR. This gives a overflow time of 8.3s.
  144. * The cycle time of one minute can thus be accomplished by setting the output compare register
  145. * to 229. This spawns an interrupt every 7.5s which means the operation has to be executed
  146. * every 8 interrupt calls.
  147. */
  148. TCCR2B |= (1<<CS22)|(1<<CS21)|(1<<CS20); // Set the timer prescaler to 1/1024
  149. OCR2A = 229; // TODO: calculate this number from the wanted cycle time and the timer frequency.
  150. TIMSK2 |= (1<<OCIE2A); // Enable the Output Compare Match A Interrupt.
  151. /* Enable global interrupts: */
  152. sei();
  153. }
  154. void Enter_Power_Save_Mode(void)
  155. {
  156. PRR |= (1<<PRTWI) | (1<<PRTIM0) | (1<<PRTIM1) | (1<<PRSPI) | (1<<PRUSART0) | (1<<PRADC); /* Only timer 2 is needed for wake-up interrupt */
  157. /* Set the clock prescaler for a frequency of 32.25 kHz*/
  158. CLKPR = (1<<CLKPCE);
  159. CLKPR = (1<<CLKPS3);
  160. TCNT2 = 0;// Reset timer 2
  161. TIMSK2 |= (1<<OCIE2A); // Enable the Output Compare Match A Interrupt.
  162. set_sleep_mode(SLEEP_MODE_PWR_SAVE);
  163. sleep_mode();
  164. }
  165. void Exit_Power_Save_Mode(void)
  166. {
  167. TIMSK2 &= ~(1<<OCIE2A); // Disable the Output Compare Match A Interrupt.
  168. /* Set the normal operating frequency of 1 MHz */
  169. CLKPR = (1<<CLKPCE);
  170. CLKPR = (1<<CLKPS1) | (1<<CLKPS0);
  171. PRR &= ~(1<<PRSPI); // Enable SPI
  172. Initialize_SPI(); // reinitalize SPI
  173. }