rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
flash_int.h
Go to the documentation of this file.
1/**
2 * @file flash_int.h
3 *
4 */
5
6#pragma once
7
8/* Error codes */
9
10/** @brief Flash operation successful */
11#define FLASH_RETURN_SUCCESS HAL_SUCCESS
12
13/** @brief Flash operation error because of denied access, corrupted memory.*/
14#define FLASH_RETURN_NO_PERMISSION -1
15
16/** @brief Flash operation error */
17#define FLASH_RETURN_OPERROR -2
18
19/** @brief Flash write protection error */
20#define FLASH_RETURN_WPERROR -3
21
22/** @brief Flash alignment error */
23#define FLASH_RETURN_ALIGNERROR -4
24
25/** @brief Flash programming parallelism error */
26#define FLASH_RETURN_PPARALLERROR -5
27
28/** @brief Flash erase sequence error */
29#define FLASH_RETURN_ESEQERROR -6
30
31/** @brief Flash programming sequence error */
32#define FLASH_RETURN_PSEQERROR -7
33
34/** @brief Flash operation error because of bad flash, corrupted memory */
35#define FLASH_RETURN_BAD_FLASH -11
36
37#define FLASH_RETURN_SECURITYERROR -12
38
39#define FLASH_RETURN_CRCERROR -13
40
41/** @brief Flash operation canceled/interrupted because of low Vdd */
42#define FLASH_RETURN_LOWVOLTAGEERROR -14
43
44/**
45 * @brief Maximum program/erase parallelism
46 *
47 * FLASH_CR_PSIZE_MASK is the mask to configure the parallelism value.
48 * FLASH_CR_PSIZE_VALUE is the parallelism value suitable for the voltage range.
49 *
50 * PSIZE(1:0) is defined as:
51 * 00 to program 8 bits per step
52 * 01 to program 16 bits per step
53 * 10 to program 32 bits per step
54 * 11 to program 64 bits per step
55 */
56// Warning, flashdata_t must be unsigned!!!
57#if defined(STM32F4XX) || defined(STM32H7XX)
58#define FLASH_CR_PSIZE_MASK (FLASH_CR_PSIZE_0 | FLASH_CR_PSIZE_1)
59#if ((STM32_VDD >= 270) && (STM32_VDD <= 360))
60#define FLASH_CR_PSIZE_VALUE FLASH_CR_PSIZE_1
61// 2.7V
62#define PWR_CR_PLS_VALUE PWR_CR_PLS_LEV5
63#define PWR_CR1_PLS_VALUE PWR_CR1_PLS_LEV5
64typedef uint32_t flashdata_t;
65#elif (STM32_VDD >= 240) && (STM32_VDD < 270)
66#define FLASH_CR_PSIZE_VALUE FLASH_CR_PSIZE_0
67// 2.5V
68#define PWR_CR_PLS_VALUE PWR_CR_PLS_LEV3
69#define PWR_CR1_PLS_VALUE PWR_CR1_PLS_LEV3
70typedef uint16_t flashdata_t;
71#elif (STM32_VDD >= 210) && (STM32_VDD < 240)
72#define FLASH_CR_PSIZE_VALUE FLASH_CR_PSIZE_0
73// 2.1V
74#define PWR_CR_PLS_VALUE PWR_CR_PLS_LEV1
75#define PWR_CR1_PLS_VALUE PWR_CR1_PLS_LEV1
76typedef uint16_t flashdata_t;
77#elif (STM32_VDD >= 180) && (STM32_VDD < 210)
78#define FLASH_CR_PSIZE_VALUE ((uint32_t)0x00000000)
79// 2.0V
80#define PWR_CR_PLS_VALUE PWR_CR_PLS_LEV0
81#define PWR_CR1_PLS_VALUE PWR_CR1_PLS_LEV0
82typedef uint8_t flashdata_t;
83#else
84#error "invalid VDD voltage specified"
85#endif
86#endif /* defined(STM32F4XX) || defined(STM32H7XX) */
87
88#if defined(STM32F7XX)
89#define FLASH_CR_PSIZE_MASK (FLASH_CR_PSIZE_0 | FLASH_CR_PSIZE_1)
90#if ((STM32_VDD >= 270) && (STM32_VDD <= 300))
91#define FLASH_CR_PSIZE_VALUE FLASH_CR_PSIZE_1
92// 2.7V
93#define PWR_CR1_PLS_VALUE PWR_CR1_PLS_LEV5
94typedef uint32_t flashdata_t;
95#elif (STM32_VDD >= 210) && (STM32_VDD < 360)
96#define FLASH_CR_PSIZE_VALUE FLASH_CR_PSIZE_0
97// 2.1V
98#define PWR_CR1_PLS_VALUE PWR_CR1_PLS_LEV1
99typedef uint16_t flashdata_t;
100#elif (STM32_VDD >= 170) && (STM32_VDD < 360)
101#define FLASH_CR_PSIZE_VALUE ((uint32_t)0x00000000)
102// 2.0V
103#define PWR_CR1_PLS_VALUE PWR_CR1_PLS_LEV0
104typedef uint8_t flashdata_t;
105#else
106#error "invalid VDD voltage specified"
107#endif
108#endif /* defined(STM32F7XX) */
109
110/** @brief Address in the flash memory */
111typedef uintptr_t flashaddr_t;
112
113/** @brief Index of a sector */
114typedef uint8_t flashsector_t;
115
116/**
117 * @brief Get the size of @p sector.
118 * @return @p sector size in bytes.
119 */
120size_t flashSectorSize(flashsector_t sector);
121
122uintptr_t getFlashAddrFirstCopy(void);
123uintptr_t getFlashAddrSecondCopy(void);
124
125/**
126 * @brief Erase the sectors containing the span of @p size bytes starting at @p address.
127 *
128 * @warning If @p address doesn't match the beginning of a sector, the
129 * data contained between the beginning of the sector and @p address will
130 * be erased too. The same applies for data contained at @p address + @p size
131 * up to the end of the sector.
132 *
133 * @param address Starting address of the span in flash memory.
134 * @param size Size of the span in bytes.
135 * @return FLASH_RETURN_SUCCESS No error erasing the flash memory.
136 * @return FLASH_RETURN_BAD_FLASH Flash cell error.
137 * @return FLASH_RETURN_NO_PERMISSION Access denied.
138 */
139int intFlashErase(flashaddr_t address, size_t size);
140
141/**
142 * @brief Check if the @p size bytes of flash memory starting at @p address are erased.
143 * @note If the memory is erased, one can write data into it safely.
144 * @param address First address in flash memory to be checked.
145 * @param size Size of the memory space to be checked in bytes.
146 * @return TRUE Memory is already erased.
147 * @return FALSE Memory is not erased.
148 */
149bool intFlashIsErased(flashaddr_t address, size_t size);
150
151/**
152 * @brief Check if the data in @p buffer are identical to the one in flash memory.
153 * @param address First address in flash memory to be checked.
154 * @param buffer Buffer containing the data to compare.
155 * @param size Size of @p buffer in bytes.
156 * @return TRUE if the flash memory and the buffer contain identical data.
157 * @return FALSE if the flash memory and the buffer don't contain identical data.
158 */
159bool intFlashCompare(flashaddr_t address, const char* buffer, size_t size);
160
161/**
162 * @brief Copy data from the flash memory to a @p destination.
163 * @warning The @p destination must be at least @p size bytes long.
164 * @param source First address of the flash memory to be copied.
165 * @param destination Buffer to copy to.
166 * @param size Size of the data to be copied in bytes.
167 * @return FLASH_RETURN_SUCCESS if successfully copied.
168 */
169int intFlashRead(flashaddr_t source, char* destination, size_t size);
170
171/**
172 * @brief Copy data from a @p buffer to the flash memory.
173 * @warning The flash memory area receiving the data must be erased.
174 * @warning The @p buffer must be at least @p size bytes long.
175 * @param address First address in the flash memory where to copy the data to.
176 * @param buffer Buffer containing the data to copy.
177 * @param size Size of the data to be copied in bytes.
178 * @return FLASH_RETURN_SUCCESS No error.
179 * @return FLASH_RETURN_NO_PERMISSION Access denied.
180 */
181int intFlashWrite(flashaddr_t address, const char* buffer, size_t size);
int intFlashErase(flashaddr_t address, size_t size)
Erase the sectors containing the span of size bytes starting at address.
uintptr_t flashaddr_t
Address in the flash memory.
Definition flash_int.h:111
int intFlashWrite(flashaddr_t address, const char *buffer, size_t size)
Copy data from a buffer to the flash memory.
uintptr_t getFlashAddrFirstCopy(void)
Definition mpu_util.cpp:253
bool intFlashIsErased(flashaddr_t address, size_t size)
Check if the size bytes of flash memory starting at address are erased.
Definition flash_int.cpp:89
int intFlashRead(flashaddr_t source, char *destination, size_t size)
Copy data from the flash memory to a destination.
uint32_t flashdata_t
Definition flash_int.h:64
uintptr_t getFlashAddrSecondCopy(void)
Definition mpu_util.cpp:257
bool intFlashCompare(flashaddr_t address, const char *buffer, size_t size)
Check if the data in buffer are identical to the one in flash memory.
size_t flashSectorSize(flashsector_t sector)
Get the size of sector.
Definition mpu_util.cpp:242
uint8_t flashsector_t
Index of a sector.
Definition flash_int.h:114
composite packet size
static BigBufferHandle buffer