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