flash.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, James Jackson and Daniel Koch, BYU MAGICC Lab
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 #include <string.h>
32 
33 #include <breezystm32.h>
34 
35 #include "flash.h"
36 
37 void initEEPROM(void)
38 {
39  // make sure (at compile time) that config struct doesn't overflow allocated flash pages
40  // ct_assert(sizeof(_params) < CONFIG_SIZE);
41 }
42 
43 static uint8_t compute_checksum(const void * addr, size_t len)
44 {
45  const uint8_t *p;
46  uint8_t chk = 0;
47 
48  for (p = (const uint8_t *)addr; p < ((const uint8_t *)addr + len); p++)
49  chk ^= *p;
50 
51  return chk;
52 }
53 
54 bool readEEPROM(void * dest, size_t len)
55 {
56  memcpy(dest, (char *)FLASH_WRITE_ADDR, len);
57  return true;
58 }
59 
60 bool writeEEPROM(const void * src, size_t len)
61 {
63 
64  // write it
65  FLASH_Unlock();
66  for (unsigned int tries = 3; tries; tries--)
67  {
69 
70  for(int i = 0; i < NUM_PAGES; i++)
72 
73  for (unsigned int i = 0; i < len && status == FLASH_COMPLETE; i += 4)
74  status = FLASH_ProgramWord(FLASH_WRITE_ADDR + i, *(uint32_t *)((char *)src + i));
75 
76  if(status == FLASH_COMPLETE)
77  break;
78  }
79  FLASH_Lock();
80 
81  // Flash write failed - just die now
82  if (status != FLASH_COMPLETE || compute_checksum(src, len) != compute_checksum((uint32_t*)FLASH_WRITE_ADDR, len))
83  return false;
84 
85  return true;
86 }
#define FLASH_FLAG_EOP
void FLASH_Unlock(void)
Unlocks the FLASH control register access.
#define FLASH_FLAG_WRPRTERR
static volatile uint8_t * status
Definition: drv_i2c.c:102
void FLASH_ClearFlag(uint32_t FLASH_FLAG)
Clears the FLASH&#39;s pending flags.
static volatile uint8_t addr
Definition: drv_i2c.c:95
void initEEPROM(void)
Initialize Flash.
Definition: flash.c:37
#define NUM_PAGES
Definition: flash.h:49
bool readEEPROM(void *dest, size_t len)
Read data from Flash.
Definition: flash.c:54
bool writeEEPROM(const void *src, size_t len)
Write data to Flash.
Definition: flash.c:60
static uint8_t compute_checksum(const void *addr, size_t len)
Definition: flash.c:43
static const uint32_t FLASH_WRITE_ADDR
Definition: flash.h:57
#define FLASH_FLAG_PGERR
FLASH_Status FLASH_ErasePage(uint32_t Page_Address)
Erases a specified FLASH page.
FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
Programs a word at a specified address.
FLASH_Status
FLASH Status.
void FLASH_Lock(void)
Locks the FLASH control register access.
#define FLASH_PAGE_SIZE
Definition: flash.h:48


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Wed Jul 3 2019 19:59:24