airbourne/airbourne/examples/flash/main.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, James Jackson
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "M25P16.h"
33 #include "led.h"
34 #include "printf.h"
35 #include "revo_f4.h"
36 #include "spi.h"
37 #include "vcp.h"
38 
39 #include <cstdlib>
40 
42 
43 static void _putc(void* p, char c)
44 {
45  (void)p; // avoid compiler warning about unused variable
46  vcp.put_byte(c);
47 }
48 
49 typedef struct
50 {
51  uint8_t magic_BE = 0xBE;
52  uint8_t big_array[2048];
53  uint8_t magic_AC = 0xAC;
54  uint8_t big_array2[2048];
55  uint8_t magic_D3 = 0xD3;
56  uint8_t crc;
57 } config_t;
58 
59 int main()
60 {
61  static config_t config_file;
62  static uint8_t config_buffer[sizeof(config_t)];
63 
64  systemInit();
65 
66  vcp.init();
68 
69  config_file.magic_AC = 0xAC;
70  config_file.magic_BE = 0xBE;
71  config_file.magic_D3 = 0xD3;
72 
73  for (int i = 0; i < 2048; i++)
74  {
75  config_file.big_array[i] = (uint8_t)std::rand();
76  config_file.big_array2[i] = (uint8_t)std::rand();
77  }
78 
79  LED warn;
80  warn.init(LED1_GPIO, LED1_PIN);
81  LED info;
82  info.init(LED2_GPIO, LED2_PIN);
83 
84  SPI spi;
85  spi.init(&spi_config[FLASH_SPI]);
86 
87  M25P16 flash;
88  flash.init(&spi);
89 
90  // calculate crc
91  uint8_t crc = 0;
92  for (uint8_t* p = (uint8_t*)&config_file; p < (uint8_t*)&config_file + sizeof(config_file); p++)
93  {
94  crc ^= *p;
95  }
96  config_file.crc = crc;
97 
98  info.on();
99 
100  bool success = false;
101 
102  // write the config to flash
103  flash.write_config((uint8_t*)&config_file, sizeof(config_t));
104 
105  // Read config from flash
106  flash.read_config(config_buffer, sizeof(config_t));
107 
108  // See if it is valid
109  config_t* config_ptr = (config_t*)config_buffer;
110 
111  // Calculate crc of new data
112  crc = 0;
113  for (uint8_t* p = (uint8_t*)config_ptr; p < (uint8_t*)config_ptr + sizeof(config_file); p++)
114  {
115  crc ^= *p;
116  }
117 
118  if (config_ptr->magic_AC == 0xAC && config_ptr->magic_BE == 0xBE && config_ptr->magic_D3 == 0xD3 && crc == 0)
119  {
120  warn.on();
121  success = true;
122  }
123  else
124  {
125  warn.off();
126  success = false;
127  }
128 
129  while (1)
130  {
131  uint32_t size = sizeof(config_file);
132  info.toggle();
133  delay(1000);
134  if (success)
135  printf("successfully wrote, read and validated %d.%dKB worth of data\n", size / 1000, size % 1000);
136  else
137  printf("failed\n");
138  }
139 }
void systemInit(void)
Definition: system.c:55
void toggle(void)
Definition: gpio.cpp:52
Definition: spi.h:37
void init(GPIO_TypeDef *gpio_port, uint16_t pin)
Definition: led.cpp:34
#define LED2_PIN
Definition: revo_f4.h:129
void init()
Definition: vcp.cpp:24
#define LED2_GPIO
Definition: revo_f4.h:128
Definition: led.h:38
static void _putc(void *p, char c)
#define LED1_GPIO
Definition: revo_f4.h:126
#define FLASH_SPI
Definition: revo_f4.h:110
bool read_config(uint8_t *data, uint32_t len)
Definition: M25P16.cpp:60
#define LED1_PIN
Definition: revo_f4.h:127
bool write_config(const uint8_t *data, const uint32_t len)
Definition: M25P16.cpp:77
Definition: M25P16.h:36
void put_byte(uint8_t ch) override
Definition: vcp.cpp:102
void on()
Definition: led.cpp:39
void init(SPI *_spi)
Definition: M25P16.cpp:38
Definition: vcp.h:19
void init(const spi_hardware_struct_t *conf)
Definition: spi.cpp:40
#define NULL
Definition: usbd_def.h:50
const spi_hardware_struct_t spi_config[NUM_SPI]
Definition: revo_f4.h:99
void init_printf(void *putp, void(*putf)(void *, char))
Definition: printf.cpp:264
void off()
Definition: led.cpp:44
void delay(uint32_t ms)
Definition: system.c:101


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Thu Apr 15 2021 05:07:46