1+ #ifndef FLASH_OS_HPP
2+ #define FLASH_OS_HPP
3+
4+ #include < cstdint>
5+
6+ // driver version. Do not modify
7+ constexpr static uint16_t flash_drv_version = 0x101 ;
8+
9+ // max amount of sectors in the flash device. Can be modified
10+ // to allow more sectors in the flash device
11+ constexpr static uint32_t max_sectors = 4 ;
12+
13+ /* *
14+ * @brief Flash device types
15+ *
16+ */
17+ enum class device_type : uint8_t {
18+ unknown = 0 ,
19+ on_chip = 1 ,
20+ external_8_bit = 2 ,
21+ external_16_bit = 3 ,
22+ external_32_bit = 4 ,
23+ external_spi = 5
24+ };
25+
26+ /* *
27+ * @brief Information about a flash sector
28+ *
29+ */
30+ struct flash_sector {
31+ // Sector size in bytes
32+ uint32_t size;
33+
34+ // Address offset on the base address
35+ uint32_t offset;
36+ };
37+
38+ // end of the sector list. Must be present at the end of the sector list
39+ constexpr static flash_sector end_of_sectors = {0xffffffff , 0xffffffff };
40+
41+ /* *
42+ * @brief Information about the flash device
43+ *
44+ */
45+ struct flash_device {
46+ // version number
47+ uint16_t version;
48+
49+ // name of the flash device.
50+ char name[128 ];
51+
52+ // type of the flash device
53+ device_type type;
54+
55+ // flash base address
56+ uint32_t base_address;
57+
58+ // total flash size of the device
59+ uint32_t size;
60+
61+ // programming page size
62+ uint32_t page_size;
63+
64+ // reserved for future extension. should be 0
65+ uint32_t reserved;
66+
67+ // value after erasing a sector/page (0xFF for most devices)
68+ uint8_t erase_value;
69+
70+ // timeout to program a page in msec
71+ uint32_t programming_timeout;
72+
73+ // timeout to erase a sector in msec
74+ uint32_t erase_timeout;
75+
76+ // flash sector layout definition
77+ flash_sector sectors[max_sectors];
78+ };
79+
80+ /* *
81+ * @brief Extern C as the Segger application is only searching the
82+ * elf for C functions. This prevents a error popup.
83+ *
84+ */
85+ extern " C" {
86+ /* *
87+ * @brief Keil / CMSIS API
88+ *
89+ */
90+
91+ /* *
92+ * @brief Initialize Flash Programming Functions
93+ *
94+ * @warning Mandatory
95+ *
96+ * @param Addr Address to init
97+ * @param Freq Clock frequency (Hz)
98+ * @param Func function code. (1 - Erase, 2 = Program, 3 = Verify)
99+ * @return int 0 = OK, 1 = Failed
100+ */
101+ int Init (const uint32_t address, const uint32_t frequency, const uint32_t function);
102+
103+ /* *
104+ * @brief De-Initialize Flash Programming Functions
105+ *
106+ * @warning Mandatory
107+ *
108+ * @param Func function code. (1 - Erase, 2 = Program, 3 = Verify)
109+ * @return int 0 = OK, 1 = Failed
110+ */
111+ int UnInit (const uint32_t function);
112+
113+ /* *
114+ * @brief Erase Sector in Flash Memory
115+ *
116+ * @warning Mandatory
117+ *
118+ * @param Addr
119+ * @return int 0 = OK, 1 = Failed
120+ */
121+ int EraseSector (const uint32_t sector_address);
122+
123+ /* *
124+ * @brief Program a page in flash memory
125+ *
126+ * @warning Mandatory
127+ *
128+ * @param Addr
129+ * @param NumBytes
130+ * @param pSrcBuff
131+ * @return int 0 = OK, 1 = Failed
132+ */
133+ int ProgramPage (const uint32_t address, const uint32_t size, const uint8_t *const data);
134+
135+ /* *
136+ * @brief Checks if the flash is "Blank". Necessary if flash is not memory mapped
137+ *
138+ * @param Addr
139+ * @param NumBytes
140+ * @param BlankData
141+ * @return int 0 = OK, 1 = OK not blank, < 0 = Error
142+ */
143+ int BlankCheck (const uint32_t address, const uint32_t size, const uint8_t blank_data);
144+
145+ /* *
146+ * @brief Erase the complete device. When not provided erase sector is called for
147+ * every sector
148+ *
149+ * @return int 0 = OK, 1 = Failed
150+ */
151+ int EraseChip (void );
152+
153+ /* *
154+ * @brief Verifies the memory. Necessary if flash is not memory mapped
155+ *
156+ * @param Addr
157+ * @param NumBytes
158+ * @param pSrcBuff
159+ * @return uint32_t == Address + Size = OK, != Address + Size = Failed
160+ */
161+ uint32_t Verify (uint32_t Addr, uint32_t NumBytes, uint8_t *pSrcBuff);
162+
163+ /* *
164+ * @brief Segger extensions
165+ *
166+ */
167+
168+ /* *
169+ * @brief Feed the watchdog of the device
170+ *
171+ */
172+ void FeedWatchdog ();
173+
174+ /* *
175+ * @brief Read from memory. Necessary if flash is not memory mapped
176+ *
177+ * @param Addr
178+ * @param NumBytes
179+ * @param pDestBuff
180+ * @return int < 0 = Error, >= 0 OK number of bytes read
181+ */
182+ int SEGGER_OPEN_Read (uint32_t Addr, uint32_t NumBytes, uint8_t *pDestBuff);
183+
184+ /* *
185+ * @brief Programs multiple pages at once in 1 ramcode call. Speeds up programming.
186+ *
187+ * @param DestAddr
188+ * @param NumBytes
189+ * @param pSrcBuff
190+ * @return int 0 = OK, 1 = Failed
191+ */
192+ int SEGGER_OPEN_Program (uint32_t DestAddr, uint32_t NumBytes, uint8_t *pSrcBuff);
193+
194+ /* *
195+ * @brief Erases multiple pages at once. Only works when uniform pages are enabled
196+ *
197+ * @param SectorAddr
198+ * @param SectorIndex
199+ * @param NumSectors
200+ * @return int 0 = OK, 1 = Failed
201+ */
202+ int SEGGER_OPEN_Erase (uint32_t SectorAddr, uint32_t SectorIndex, uint32_t NumSectors);
203+ }
204+
205+ #endif
0 commit comments