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

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