暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

main.c 6.1KB

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