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 {
36 #pragma GCC diagnostic push //Ignore old style cast from included library
37 #pragma GCC diagnostic ignored "-Wold-style-cast"
39 #pragma GCC diagnostic pop
40 #pragma GCC diagnostic push //Ignore blank fields in struct
41 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
42  rosflight_firmware::BackupData blank_data = {0};
43 #pragma GCC diagnostic pop
44  if (!check_backup_checksum(data))
45  data=blank_data;
46  backup_sram_write(blank_data);
47  return data;
48 }
49 
51 {
53  return data;
54 }
55 
57 {
58  return (data.checksum==generate_backup_checksum(data));
59 }
60 
61 //Generates a checksum by XORing 32 bit chunks of the backup data together,
62 //excluding the last 32 bits, which stores the checksum itself
64 {
65  const uint32_t *pointer = reinterpret_cast<const uint32_t *>(&data);
66  uint32_t checksum = 0;
67  for (uint8_t i=0; i<(sizeof(rosflight_firmware::BackupData)/4)-1; i++)
68  checksum^=*(pointer+i);
69  return checksum;
70 }
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:50
bool check_backup_checksum(const rosflight_firmware::BackupData &data)
Definition: backup_sram.cpp:56
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:63
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 Wed Jul 3 2019 19:59:24