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 "revo_f4.h"
33 
34 #include "spi.h"
35 #include "M25P16.h"
36 #include "led.h"
37 #include "vcp.h"
38 #include "printf.h"
39 
40 #include <cstdlib>
41 
43 
44 static void _putc(void *p, char c)
45 {
46  (void)p; // avoid compiler warning about unused variable
47  vcp.put_byte(c);
48 }
49 
50 typedef struct
51 {
52  uint8_t magic_BE = 0xBE;
53  uint8_t big_array[2048];
54  uint8_t magic_AC = 0xAC;
55  uint8_t big_array2[2048];
56  uint8_t magic_D3 = 0xD3;
57  uint8_t crc;
58 } config_t;
59 
60 
61 
62 int main()
63 {
64  static config_t config_file;
65  static uint8_t config_buffer[sizeof(config_t)];
66 
67  systemInit();
68 
69  vcp.init();
71 
72  config_file.magic_AC = 0xAC;
73  config_file.magic_BE = 0xBE;
74  config_file.magic_D3 = 0xD3;
75 
76  for (int i = 0; i < 2048; i++)
77  {
78  config_file.big_array[i] = (uint8_t)std::rand();
79  config_file.big_array2[i] = (uint8_t)std::rand();
80  }
81 
82  LED warn;
83  warn.init(LED1_GPIO, LED1_PIN);
84  LED info;
85  info.init(LED2_GPIO, LED2_PIN);
86 
87  SPI spi;
88  spi.init(&spi_config[FLASH_SPI]);
89 
90  M25P16 flash;
91  flash.init(&spi);
92 
93  // calculate crc
94  uint8_t crc = 0;
95  for (uint8_t* p = (uint8_t*)&config_file; p < (uint8_t*)&config_file + sizeof(config_file); p++)
96  {
97  crc ^= *p;
98  }
99  config_file.crc = crc;
100 
101  info.on();
102 
103  bool success = false;
104 
105  // write the config to flash
106  flash.write_config((uint8_t*)&config_file, sizeof(config_t));
107 
108  // Read config from flash
109  flash.read_config(config_buffer, sizeof(config_t));
110 
111  // See if it is valid
112  config_t* config_ptr = (config_t*) config_buffer;
113 
114  // Calculate crc of new data
115  crc = 0;
116  for (uint8_t* p = (uint8_t*) config_ptr; p < (uint8_t*)config_ptr + sizeof(config_file); p++)
117  {
118  crc ^= *p;
119  }
120 
121  if (config_ptr->magic_AC == 0xAC &&
122  config_ptr->magic_BE == 0xBE &&
123  config_ptr->magic_D3 == 0xD3 &&
124  crc == 0)
125  {
126  warn.on();
127  success = true;
128  }
129  else
130  {
131  warn.off();
132  success = false;
133  }
134 
135  while(1)
136  {
137  uint32_t size = sizeof(config_file);
138  info.toggle();
139  delay(1000);
140  if (success)
141  printf("successfully wrote, read and validated %d.%dKB worth of data\n", size/1000, size%1000);
142  else
143  printf("failed\n");
144  }
145 }
146 
void systemInit(void)
Definition: system.c:55
void toggle(void)
Definition: gpio.cpp:52
#define printf
Definition: printf.h:119
Definition: spi.h:38
void init(GPIO_TypeDef *gpio_port, uint16_t pin)
Definition: led.cpp:34
#define LED2_PIN
Definition: revo_f4.h:134
void init()
Definition: vcp.cpp:24
#define LED2_GPIO
Definition: revo_f4.h:133
Definition: led.h:38
static void _putc(void *p, char c)
#define LED1_GPIO
Definition: revo_f4.h:131
#define FLASH_SPI
Definition: revo_f4.h:114
bool read_config(uint8_t *data, uint32_t len)
Definition: M25P16.cpp:59
#define LED1_PIN
Definition: revo_f4.h:132
bool write_config(const uint8_t *data, const uint32_t len)
Definition: M25P16.cpp:73
Definition: M25P16.h:36
void put_byte(uint8_t ch) override
Definition: vcp.cpp:106
void on()
Definition: led.cpp:39
void init(SPI *_spi)
Definition: M25P16.cpp:37
Definition: vcp.h:20
void init_printf(void *putp, void(*putf)(void *, char))
Definition: printf.cpp:245
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:101
void off()
Definition: led.cpp:44
void delay(uint32_t ms)
Definition: system.c:98


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