backup_sram.cpp
Go to the documentation of this file.
1 #include <cstring> //For memcpy
2 #include "stm32f4xx_pwr.h"
3 #include "stm32f4xx_rcc.h"
4 #include "backup_sram.h"
5 
7 {
8  //The checksum function depends on the BackupData struct being a multiple of 32 bits
9  //If you change the BackupData struct, and it trips on this static_assert,
10  //either make the BackupData struct a multiple of 32 bits, or change the checksum method
11  static_assert(sizeof(rosflight_firmware::BackupData)%4==0,"Error with debug_info_t: Size is not multiple of 32 bits");
12 #pragma GCC diagnostic push //Ignore old style cast from included library
13 #pragma GCC diagnostic ignored "-Wold-style-cast"
17 #pragma GCC diagnostic pop
19 }
20 
22 {
24 #pragma GCC diagnostic push //Ignore old style cast from included library
25 #pragma GCC diagnostic ignored "-Wold-style-cast"
26  std::memcpy(reinterpret_cast<void*>(BKPSRAM_BASE),&data,sizeof(rosflight_firmware::BackupData));
27 #pragma GCC diagnostic pop
29 }
30 
31 //This method actually reads the backup sram, and then clears it.
32 //This is used by the backup_sram_read() function, and should not
33 //be used elsewhere.
35 #pragma GCC diagnostic push //Ignore old style cast from included library
36 #pragma GCC diagnostic ignored "-Wold-style-cast"
38 #pragma GCC diagnostic pop
39 #pragma GCC diagnostic push //Ignore blank fields in struct
40 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
41  rosflight_firmware::BackupData blank_data = {0};
42 #pragma GCC diagnostic pop
43  if(!check_backup_checksum(data))
44  data=blank_data;
45  backup_sram_write(blank_data);
46  return data;
47 }
48 
50 {
52  return data;
53 }
54 
56 {
57  return (data.checksum==generate_backup_checksum(data));
58 }
59 
60 //Generates a checksum by XORing 32 bit chunks of the backup data together,
61 //excluding the last 32 bits, which stores the checksum itself
63 {
64  const uint32_t* pointer = reinterpret_cast<const uint32_t*>(&data);
65  uint32_t checksum = 0;
66  for(uint8_t i=0;i<(sizeof (rosflight_firmware::BackupData)/4)-1;i++)
67  checksum^=*(pointer+i);
68  return checksum;
69 }
void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
Enables or disables the Low Speed APB (APB1) peripheral clock.
void backup_sram_write(const rosflight_firmware::BackupData &data)
Definition: backup_sram.cpp:21
void backup_sram_init()
Definition: backup_sram.cpp:6
This file contains all the functions prototypes for the PWR firmware library.
void RCC_AHB1PeriphClockCmd(uint32_t RCC_AHB1Periph, FunctionalState NewState)
Enables or disables the AHB1 peripheral clock.
void PWR_BackupAccessCmd(FunctionalState NewState)
Enables or disables access to the RTC and backup registers.
void RCC_APB1PeriphResetCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
Forces or releases Low Speed APB (APB1) peripheral reset.
rosflight_firmware::BackupData backup_sram_read()
Definition: backup_sram.cpp:49
bool check_backup_checksum(const rosflight_firmware::BackupData &data)
Definition: backup_sram.cpp:55
rosflight_firmware::BackupData do_first_read()
Definition: backup_sram.cpp:34
uint32_t generate_backup_checksum(const rosflight_firmware::BackupData &data)
Definition: backup_sram.cpp:62
void PWR_BackupRegulatorCmd(FunctionalState NewState)
Enables or disables the Backup Regulator.
#define BKPSRAM_BASE
Definition: stm32f4xx.h:1849


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Thu Oct 24 2019 03:17:18