Ingen beskrivning
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 5.4KB

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