2 ******************************************************************************
3 * @addtogroup PIOS PIOS Core hardware abstraction layer
5 * @addtogroup PIOS_EEPROM EEPROM reading/writing functions
6 * @brief PIOS EEPROM reading/writing functions
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
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
33 #ifdef PIOS_INCLUDE_FLASH_EEPROM
35 #include <stm32f10x_flash.h>
36 #include <pios_board_info.h>
38 static struct pios_eeprom_cfg config
;
41 * Initialize the flash eeprom device
42 * \param cfg The configuration structure.
44 void PIOS_EEPROM_Init(const struct pios_eeprom_cfg
*cfg
)
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.
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
) {
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
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)) {
79 // TODO: Check that the area isn't already erased
82 FLASH_Status fs
= FLASH_ErasePage(config
.base_address
);
83 if (fs
!= FLASH_COMPLETE
) { // error
88 // write 4 bytes at a time into program flash area (emulated EEPROM area)
90 uint32_t *p3
= (uint32_t *)config
.base_address
;
91 for (uint32_t i
= 0; i
< size
; p3
++) {
94 if (i
== (size
- 4)) {
100 value
|= (uint32_t)*p1
++ << 0;
101 } else { value
|= 0x000000ff; }
104 value
|= (uint32_t)*p1
++ << 8;
105 } else { value
|= 0x0000ff00; }
108 value
|= (uint32_t)*p1
++ << 16;
109 } else { value
|= 0x00ff0000; }
112 value
|= (uint32_t)*p1
++ << 24;
113 } else { value
|= 0xff000000; }
117 // write a 32-bit value
118 fs
= FLASH_ProgramWord((uint32_t)p3
, value
);
119 if (fs
!= FLASH_COMPLETE
) {
125 // Lock the Flash Program Erase controller
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
) {
149 // Read the data from flash.
150 memcpy(data
, (uint8_t *)config
.base_address
, len
);
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
) {
164 #endif /* PIOS_INCLUDE_FLASH_EEPROM */