fix WhatsNew for release
[librepilot.git] / flight / pios / stm32f10x / pios_eeprom.c
blob75fbced049c9e8e0e900080fa8054c75efdf37d8
1 /**
2 ******************************************************************************
3 * @addtogroup PIOS PIOS Core hardware abstraction layer
4 * @{
5 * @addtogroup PIOS_EEPROM EEPROM reading/writing functions
6 * @brief PIOS EEPROM reading/writing functions
7 * @{
9 * @file pios_eeprom.c
10 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
11 * @brief COM layer functions
12 * @see The GNU Public License (GPL) Version 3
14 *****************************************************************************/
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * for more details.
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <pios.h>
33 #ifdef PIOS_INCLUDE_FLASH_EEPROM
35 #include <stm32f10x_flash.h>
36 #include <pios_board_info.h>
38 static struct pios_eeprom_cfg config;
40 /**
41 * Initialize the flash eeprom device
42 * \param cfg The configuration structure.
44 void PIOS_EEPROM_Init(const struct pios_eeprom_cfg *cfg)
46 config = *cfg;
49 /**
50 * Save a block of data to the flash eeprom device.
51 * \param data A pointer to the data to write.
52 * \param len The length of data to write.
53 * \return 0 on sucess
55 int32_t PIOS_EEPROM_Save(uint8_t *data, uint32_t len)
57 // We need to write 32 bit words, so extend the length to be an even multiple of 4 bytes,
58 // and include 4 bytes for the 32 bit CRC.
59 uint32_t nwords = (len / 4) + 1 + (len % 4 ? 1 : 0);
60 uint32_t size = nwords * 4;
62 // Ensure that the length is not longer than the max size.
63 if (size > config.max_size) {
64 return -1;
67 // Calculate a 32 bit CRC of the data.
68 uint32_t crc = PIOS_CRC32_updateCRC(0xffffffff, data, len);
70 // Unlock the Flash Program Erase controller
71 FLASH_Unlock();
73 // See if we have to write the data.
74 if ((memcmp(data, (uint8_t *)config.base_address, len) == 0) &&
75 (memcmp((uint8_t *)&crc, (uint8_t *)config.base_address + size - 4, 4) == 0)) {
76 return 0;
79 // TODO: Check that the area isn't already erased
81 // Erase page
82 FLASH_Status fs = FLASH_ErasePage(config.base_address);
83 if (fs != FLASH_COMPLETE) { // error
84 FLASH_Lock();
85 return -2;
88 // write 4 bytes at a time into program flash area (emulated EEPROM area)
89 uint8_t *p1 = data;
90 uint32_t *p3 = (uint32_t *)config.base_address;
91 for (uint32_t i = 0; i < size; p3++) {
92 uint32_t value = 0;
94 if (i == (size - 4)) {
95 // write the CRC.
96 value = crc;
97 i += 4;
98 } else {
99 if (i < len) {
100 value |= (uint32_t)*p1++ << 0;
101 } else { value |= 0x000000ff; }
102 i++;
103 if (i < len) {
104 value |= (uint32_t)*p1++ << 8;
105 } else { value |= 0x0000ff00; }
106 i++;
107 if (i < len) {
108 value |= (uint32_t)*p1++ << 16;
109 } else { value |= 0x00ff0000; }
110 i++;
111 if (i < len) {
112 value |= (uint32_t)*p1++ << 24;
113 } else { value |= 0xff000000; }
114 i++;
117 // write a 32-bit value
118 fs = FLASH_ProgramWord((uint32_t)p3, value);
119 if (fs != FLASH_COMPLETE) {
120 FLASH_Lock();
121 return -3;
125 // Lock the Flash Program Erase controller
126 FLASH_Lock();
128 return 0;
132 * Reads a block of data from the flash eeprom device.
133 * \param data A pointer to the output data buffer.
134 * \param len The length of data to read.
135 * \return 0 on sucess
137 int32_t PIOS_EEPROM_Load(uint8_t *data, uint32_t len)
139 // We need to write 32 bit words, so the length should have been extended
140 // to an even multiple of 4 bytes, and should include 4 bytes for the 32 bit CRC.
141 uint32_t nwords = (len / 4) + 1 + (len % 4 ? 1 : 0);
142 uint32_t size = nwords * 4;
144 // Ensure that the length is not longer than the max size.
145 if (size > config.max_size) {
146 return -1;
149 // Read the data from flash.
150 memcpy(data, (uint8_t *)config.base_address, len);
152 // Read the CRC.
153 uint32_t crc_flash = *((uint32_t *)(config.base_address + size - 4));
155 // Calculate a 32 bit CRC of the data.
156 uint32_t crc = PIOS_CRC32_updateCRC(0xffffffff, data, len);
157 if (crc != crc_flash) {
158 return -2;
161 return 0;
164 #endif /* PIOS_INCLUDE_FLASH_EEPROM */