Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

main.c 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "radio.h"
  18. #include "encryption.h"
  19. uint8_t ownId;
  20. uint64_t salt;
  21. char bool_case = 0;
  22. int timer = 0;
  23. int timer_max = 0;
  24. volatile uint8_t cycle = 0;
  25. volatile bool nrfInterruptRaised;
  26. volatile uint8_t interruptCounter;
  27. volatile uint8_t executionFlag;
  28. static uint8_t messageBuffer[PACKET_LENGTH];
  29. static PACKET * packet = (PACKET*) messageBuffer;
  30. void Enter_Power_Save_Mode(void);
  31. void Exit_Power_Save_Mode(void);
  32. void Set_Up_Power_Save_Mode(void);
  33. void Enter_Power_Save_Mode(void);
  34. ISR(TIMER2_COMPA_vect)
  35. {
  36. /* Do nothing as the interrupt is only used to wake up the MCU. */
  37. }
  38. ISR(PCINT2_vect)
  39. {
  40. nrfInterruptRaised = true;
  41. }
  42. int main (void)
  43. {
  44. struct bme280_data sensorData;
  45. bool saltReceived = false;
  46. /* Enable the debug LED */
  47. LED_DDR |= (1 << LED_PIN);
  48. /* Get the own ID */
  49. Configure_Pin_Programming_Pins();
  50. ownId = Get_Own_Identifier();
  51. /* Set the encryption key */
  52. Set_Encryption_Key(ownId);
  53. /* Initialize the SPI */
  54. Initialize_SPI();
  55. /* Initialize the nrf24l01 */
  56. Initialize_NRF24L01();
  57. // The NRF24L01 is now in the mode Standby-I.
  58. /* Configure the transmission parameters (Enhanced ShockBurst)*/
  59. Configure_Transmission(ownId);
  60. /* Initialize the BME280 */
  61. Initialize_BME280();
  62. Set_Up_Power_Save_Mode();
  63. /* Initialize the salt */
  64. salt = 0x0ull;
  65. salt |= ownId;
  66. /* Request a salt from the base station */
  67. LED_PORT |= (1 << LED_PIN);
  68. do
  69. {
  70. if (Send_Get_Salt_Message(packet, &salt) == true)
  71. {
  72. memset(messageBuffer, 0x0F, 32);
  73. if (NRF24L01_Receive_Message(messageBuffer, 100 /*ms*/) == true)
  74. {
  75. _delay_ms(10);
  76. saltReceived = Read_Salt_Message(packet, &salt);
  77. }
  78. }
  79. else
  80. {
  81. _delay_ms(100);
  82. }
  83. } while (saltReceived == false);
  84. LED_PORT &= ~(1 << LED_PIN);
  85. salt &= 0xFFFFFFFFFFFFFF00ull;
  86. salt |= ownId;
  87. /* Delay the change of the operating frequency by the function Enter_Power_Save_Mode for the
  88. * first function pass. If it is changed before the ISP can flash the MCU the clocks of the ISP
  89. * and MCU are mismatched and the flashing will fail.
  90. */
  91. _delay_ms(500);
  92. while(1)
  93. {
  94. Enter_Power_Save_Mode(); // The MCU enters the Power Save Mode here.
  95. Exit_Power_Save_Mode(); // The MCU exits the Power Save Mode here.
  96. if (cycle == 0) // TODO cycle == 7 to execute this every 60 s
  97. {
  98. /* Re-Initialize the peripherals */
  99. Set_BME280_Pins();
  100. Set_NRF24L01_Pins();
  101. /* Get measurement and send it */
  102. BME280_Get_Measurement(&sensorData);
  103. /* Send the report */
  104. Send_Report_Message(packet, &salt, &sensorData);
  105. LED_PORT |= (1 << LED_PIN);
  106. _delay_ms(100); /* TODO: only for debugging, remove this later! */
  107. LED_PORT &= ~(1 << LED_PIN);
  108. cycle = 0;
  109. }
  110. else
  111. {
  112. cycle ++;
  113. }
  114. }
  115. }
  116. void Set_Up_Power_Save_Mode(void)
  117. {
  118. // Disable Brown-Out-Detection by setting the BODLEVEL 2:0 Fuses to 0b111 (should be default)
  119. // Disable the on-chip debug system by setting the DWEN Fuse to 1 (should be default)
  120. /* Disable some unused peripherals that are not used during operation of the weather station: */
  121. // The ADC is turned off by default
  122. ACSR &= ~(1<<ACD); // Analog comperator
  123. ACSR &= ~(1<<ACIE); // The interrupt bit has to be cleared after switchin of the analog comperator
  124. /*
  125. * The Internal voltager reference is automatically disabled if the BOD, ADC and Voltage
  126. * Reference are disabled
  127. */
  128. /* Set up Timer/Counter2 */
  129. PRR &= ~(1<<PRTIM2); // Enable the timer 2 in the Power Reduction Register
  130. TCCR2A |= (1<<COM2A1)|(1<<COM2A0)|(1<<WGM21); // Set the timer to ClearTimer on Compare Match with output compare mode for channel A
  131. /* There is a problem: The maximum time until the counter 2 overflows is 262ms at the nominal
  132. * core frquency of 1MHz and a timer prescaler of 1024.
  133. * This can be attenuated by lowering the system frequency to 31.25 kHz via the clock prescaler
  134. * (CLKPS3...0) of the register CLKPR. This gives a overflow time of 8.3s.
  135. * The cycle time of one minute can thus be accomplished by setting the output compare register
  136. * to 229. This spawns an interrupt every 7.5s which means the operation has to be executed
  137. * every 8 interrupt calls.
  138. */
  139. TCCR2B |= (1<<CS22)|(1<<CS21)|(1<<CS20); // Set the timer prescaler to 1/1024
  140. OCR2A = 229; // TODO: calculate this number from the wanted cycle time and the timer frequency.
  141. TIMSK2 |= (1<<OCIE2A); // Enable the Output Compare Match A Interrupt.
  142. /* Enable global interrupts: */
  143. sei();
  144. }
  145. void Enter_Power_Save_Mode(void)
  146. {
  147. PRR |= (1<<PRTWI) | (1<<PRTIM0) | (1<<PRTIM1) | (1<<PRSPI) | (1<<PRUSART0) | (1<<PRADC); /* Only timer 2 is needed for wake-up interrupt */
  148. /* Set the clock prescaler for a frequency of 32.25 kHz*/
  149. CLKPR = (1<<CLKPCE);
  150. CLKPR = (1<<CLKPS3);
  151. TCNT2 = 0;// Reset timer 2
  152. TIMSK2 |= (1<<OCIE2A); // Enable the Output Compare Match A Interrupt.
  153. set_sleep_mode(SLEEP_MODE_PWR_SAVE);
  154. sleep_mode();
  155. }
  156. void Exit_Power_Save_Mode(void)
  157. {
  158. TIMSK2 &= ~(1<<OCIE2A); // Disable the Output Compare Match A Interrupt.
  159. /* Set the normal operating frequency of 1 MHz */
  160. CLKPR = (1<<CLKPCE);
  161. CLKPR = (1<<CLKPS1) | (1<<CLKPS0);
  162. PRR &= ~(1<<PRSPI); // Enable SPI
  163. Initialize_SPI(); // reinitalize SPI
  164. }