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/**
42 * @brief Maximum program/erase parallelism
43 *
44 * FLASH_CR_PSIZE_MASK is the mask to configure the parallelism value.
45 * FLASH_CR_PSIZE_VALUE is the parallelism value suitable for the voltage range.
46 *
47 * PSIZE(1:0) is defined as:
48 * 00 to program 8 bits per step
49 * 01 to program 16 bits per step
50 * 10 to program 32 bits per step
51 * 11 to program 64 bits per step
52 */
53// Warning, flashdata_t must be unsigned!!!
54#if defined(STM32F4XX) || defined(STM32H7XX)
55#define FLASH_CR_PSIZE_MASK (FLASH_CR_PSIZE_0 | FLASH_CR_PSIZE_1)
56#if ((STM32_VDD >= 270) && (STM32_VDD <= 360))
57#define FLASH_CR_PSIZE_VALUE FLASH_CR_PSIZE_1
58typedef uint32_t flashdata_t;
59#elif (STM32_VDD >= 240) && (STM32_VDD < 270)
60#define FLASH_CR_PSIZE_VALUE FLASH_CR_PSIZE_0
61typedef uint16_t flashdata_t;
62#elif (STM32_VDD >= 210) && (STM32_VDD < 240)
63#define FLASH_CR_PSIZE_VALUE FLASH_CR_PSIZE_0
64typedef uint16_t flashdata_t;
65#elif (STM32_VDD >= 180) && (STM32_VDD < 210)
66#define FLASH_CR_PSIZE_VALUE ((uint32_t)0x00000000)
67typedef uint8_t flashdata_t;
68#else
69#error "invalid VDD voltage specified"
70#endif
71#endif /* defined(STM32F4XX) || defined(STM32H7XX) */
72
73#if defined(STM32F7XX)
74#define FLASH_CR_PSIZE_MASK (FLASH_CR_PSIZE_0 | FLASH_CR_PSIZE_1)
75#if ((STM32_VDD >= 270) && (STM32_VDD <= 300))
76#define FLASH_CR_PSIZE_VALUE FLASH_CR_PSIZE_1
77typedef uint32_t flashdata_t;
78#elif (STM32_VDD >= 210) && (STM32_VDD < 360)
79#define FLASH_CR_PSIZE_VALUE FLASH_CR_PSIZE_0
80typedef uint16_t flashdata_t;
81#elif (STM32_VDD >= 170) && (STM32_VDD < 360)
82#define FLASH_CR_PSIZE_VALUE ((uint32_t)0x00000000)
83typedef uint8_t flashdata_t;
84#else
85#error "invalid VDD voltage specified"
86#endif
87#endif /* defined(STM32F7XX) */
88
89/** @brief Address in the flash memory */
90typedef uintptr_t flashaddr_t;
91
92/** @brief Index of a sector */
93typedef uint8_t flashsector_t;
94
95/**
96 * @brief Get the size of @p sector.
97 * @return @p sector size in bytes.
98 */
99size_t flashSectorSize(flashsector_t sector);
100
101uintptr_t getFlashAddrFirstCopy(void);
102uintptr_t getFlashAddrSecondCopy(void);
103
104/**
105 * @brief Erase the sectors containing the span of @p size bytes starting at @p address.
106 *
107 * @warning If @p address doesn't match the beginning of a sector, the
108 * data contained between the beginning of the sector and @p address will
109 * be erased too. The same applies for data contained at @p address + @p size
110 * up to the end of the sector.
111 *
112 * @param address Starting address of the span in flash memory.
113 * @param size Size of the span in bytes.
114 * @return FLASH_RETURN_SUCCESS No error erasing the flash memory.
115 * @return FLASH_RETURN_BAD_FLASH Flash cell error.
116 * @return FLASH_RETURN_NO_PERMISSION Access denied.
117 */
118int intFlashErase(flashaddr_t address, size_t size);
119
120/**
121 * @brief Check if the @p size bytes of flash memory starting at @p address are erased.
122 * @note If the memory is erased, one can write data into it safely.
123 * @param address First address in flash memory to be checked.
124 * @param size Size of the memory space to be checked in bytes.
125 * @return TRUE Memory is already erased.
126 * @return FALSE Memory is not erased.
127 */
128bool intFlashIsErased(flashaddr_t address, size_t size);
129
130/**
131 * @brief Check if the data in @p buffer are identical to the one in flash memory.
132 * @param address First address in flash memory to be checked.
133 * @param buffer Buffer containing the data to compare.
134 * @param size Size of @p buffer in bytes.
135 * @return TRUE if the flash memory and the buffer contain identical data.
136 * @return FALSE if the flash memory and the buffer don't contain identical data.
137 */
138bool intFlashCompare(flashaddr_t address, const char* buffer, size_t size);
139
140/**
141 * @brief Copy data from the flash memory to a @p destination.
142 * @warning The @p destination must be at least @p size bytes long.
143 * @param source First address of the flash memory to be copied.
144 * @param destination Buffer to copy to.
145 * @param size Size of the data to be copied in bytes.
146 * @return FLASH_RETURN_SUCCESS if successfully copied.
147 */
148int intFlashRead(flashaddr_t source, char* destination, size_t size);
149
150/**
151 * @brief Copy data from a @p buffer to the flash memory.
152 * @warning The flash memory area receiving the data must be erased.
153 * @warning The @p buffer must be at least @p size bytes long.
154 * @param address First address in the flash memory where to copy the data to.
155 * @param buffer Buffer containing the data to copy.
156 * @param size Size of the data to be copied in bytes.
157 * @return FLASH_RETURN_SUCCESS No error.
158 * @return FLASH_RETURN_NO_PERMISSION Access denied.
159 */
160int 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:90
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:248
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:58
uintptr_t getFlashAddrSecondCopy(void)
Definition mpu_util.cpp:252
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:237
uint8_t flashsector_t
Index of a sector.
Definition flash_int.h:93
composite packet size
static BigBufferHandle buffer