Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
src/SOF_dev_stm32_f4xx.cpp@57:5afd7c1b9acf, 2019-11-25 (annotated)
- Committer:
- Slord2142
- Date:
- Mon Nov 25 23:20:54 2019 +0000
- Revision:
- 57:5afd7c1b9acf
- Parent:
- 33:6c7364ea360f
Lut base variables updated
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Slord2142 | 33:6c7364ea360f | 1 | /** |
| Slord2142 | 33:6c7364ea360f | 2 | * @file SOF_dev_stm32.c |
| Slord2142 | 33:6c7364ea360f | 3 | * |
| Slord2142 | 33:6c7364ea360f | 4 | * @brief Flash device access interface for STM32 F4xx series |
| Slord2142 | 33:6c7364ea360f | 5 | * |
| Slord2142 | 33:6c7364ea360f | 6 | * |
| Slord2142 | 33:6c7364ea360f | 7 | * History: |
| Slord2142 | 33:6c7364ea360f | 8 | */ |
| Slord2142 | 33:6c7364ea360f | 9 | |
| Slord2142 | 33:6c7364ea360f | 10 | #include <stdio.h> |
| Slord2142 | 33:6c7364ea360f | 11 | #include "SOF_dev.h" |
| Slord2142 | 33:6c7364ea360f | 12 | #include <string.h> |
| Slord2142 | 33:6c7364ea360f | 13 | |
| Slord2142 | 33:6c7364ea360f | 14 | #include "mbed.h" |
| Slord2142 | 33:6c7364ea360f | 15 | #include "stm32f4xx_hal_flash.h" |
| Slord2142 | 33:6c7364ea360f | 16 | |
| Slord2142 | 33:6c7364ea360f | 17 | #define DCRLF "\r\n" |
| Slord2142 | 33:6c7364ea360f | 18 | |
| Slord2142 | 33:6c7364ea360f | 19 | #if 1 |
| Slord2142 | 33:6c7364ea360f | 20 | #define DPRINTF printf |
| Slord2142 | 33:6c7364ea360f | 21 | #define DASSERT(cond) \ |
| Slord2142 | 33:6c7364ea360f | 22 | if (!(cond)) { \ |
| Slord2142 | 33:6c7364ea360f | 23 | printf("%s:%d assertion failed! '%s\r\n"\ |
| Slord2142 | 33:6c7364ea360f | 24 | , __FILE__, __LINE__, #cond); \ |
| Slord2142 | 33:6c7364ea360f | 25 | } |
| Slord2142 | 33:6c7364ea360f | 26 | #else |
| Slord2142 | 33:6c7364ea360f | 27 | #define DPRINTF(...) |
| Slord2142 | 33:6c7364ea360f | 28 | #define DASSERT(...) |
| Slord2142 | 33:6c7364ea360f | 29 | #endif |
| Slord2142 | 33:6c7364ea360f | 30 | |
| Slord2142 | 33:6c7364ea360f | 31 | |
| Slord2142 | 33:6c7364ea360f | 32 | #if defined(STM32F401xE) |
| Slord2142 | 33:6c7364ea360f | 33 | static const SOF_SectorSpec_t _sec_spec[] = { |
| Slord2142 | 33:6c7364ea360f | 34 | {FLASH_SECTOR_0, 0x08000000, 16*1024}, |
| Slord2142 | 33:6c7364ea360f | 35 | {FLASH_SECTOR_1, 0x08004000, 16*1024}, |
| Slord2142 | 33:6c7364ea360f | 36 | {FLASH_SECTOR_2, 0x08008000, 16*1024}, |
| Slord2142 | 33:6c7364ea360f | 37 | {FLASH_SECTOR_3, 0x0800C000, 16*1024}, |
| Slord2142 | 33:6c7364ea360f | 38 | {FLASH_SECTOR_4, 0x08010000, 64*1024}, |
| Slord2142 | 33:6c7364ea360f | 39 | {FLASH_SECTOR_5, 0x08020000, 128*1024}, |
| Slord2142 | 33:6c7364ea360f | 40 | {FLASH_SECTOR_6, 0x08040000, 128*1024}, |
| Slord2142 | 33:6c7364ea360f | 41 | {FLASH_SECTOR_7, 0x08060000, 128*1024}, |
| Slord2142 | 33:6c7364ea360f | 42 | }; |
| Slord2142 | 33:6c7364ea360f | 43 | #elif defined(STM32F411xE) |
| Slord2142 | 33:6c7364ea360f | 44 | static const SOF_SectorSpec_t _sec_spec[] = { |
| Slord2142 | 33:6c7364ea360f | 45 | {FLASH_SECTOR_0, 0x08000000, 16*1024}, |
| Slord2142 | 33:6c7364ea360f | 46 | {FLASH_SECTOR_1, 0x08004000, 16*1024}, |
| Slord2142 | 33:6c7364ea360f | 47 | {FLASH_SECTOR_2, 0x08008000, 16*1024}, |
| Slord2142 | 33:6c7364ea360f | 48 | {FLASH_SECTOR_3, 0x0800C000, 16*1024}, |
| Slord2142 | 33:6c7364ea360f | 49 | {FLASH_SECTOR_4, 0x08010000, 64*1024}, |
| Slord2142 | 33:6c7364ea360f | 50 | {FLASH_SECTOR_5, 0x08020000, 128*1024}, |
| Slord2142 | 33:6c7364ea360f | 51 | {FLASH_SECTOR_6, 0x08040000, 128*1024}, |
| Slord2142 | 33:6c7364ea360f | 52 | {FLASH_SECTOR_7, 0x08060000, 128*1024}, |
| Slord2142 | 33:6c7364ea360f | 53 | }; |
| Slord2142 | 33:6c7364ea360f | 54 | #else |
| Slord2142 | 33:6c7364ea360f | 55 | #error "Not supported device" |
| Slord2142 | 33:6c7364ea360f | 56 | #endif |
| Slord2142 | 33:6c7364ea360f | 57 | |
| Slord2142 | 33:6c7364ea360f | 58 | #define N_SECTOR_SPEC (sizeof(_sec_spec)/sizeof(_sec_spec[0])) |
| Slord2142 | 33:6c7364ea360f | 59 | |
| Slord2142 | 33:6c7364ea360f | 60 | #define SECTOR_NO(sector) _sec_spec[sector].sec_no |
| Slord2142 | 33:6c7364ea360f | 61 | #define SECTOR_ADDR(sector) _sec_spec[sector].sec_addr |
| Slord2142 | 33:6c7364ea360f | 62 | #define SECTOR_SIZE(sector) _sec_spec[sector].sec_size |
| Slord2142 | 33:6c7364ea360f | 63 | |
| Slord2142 | 33:6c7364ea360f | 64 | |
| Slord2142 | 33:6c7364ea360f | 65 | static inline size_t handle_to_sector_index(SOF_DevHandle_t hdev) |
| Slord2142 | 33:6c7364ea360f | 66 | { |
| Slord2142 | 33:6c7364ea360f | 67 | DASSERT(hdev < N_SECTOR_SPEC); |
| Slord2142 | 33:6c7364ea360f | 68 | return hdev; |
| Slord2142 | 33:6c7364ea360f | 69 | } |
| Slord2142 | 33:6c7364ea360f | 70 | |
| Slord2142 | 33:6c7364ea360f | 71 | const SOF_SectorSpec_t *SOF_dev_info(uint8_t sector_index) |
| Slord2142 | 33:6c7364ea360f | 72 | { |
| Slord2142 | 33:6c7364ea360f | 73 | DASSERT(sector_index < N_SECTOR_SPEC); |
| Slord2142 | 33:6c7364ea360f | 74 | return &_sec_spec[sector_index]; |
| Slord2142 | 33:6c7364ea360f | 75 | } |
| Slord2142 | 33:6c7364ea360f | 76 | |
| Slord2142 | 33:6c7364ea360f | 77 | int SOF_dev_is_valid_sector(uint8_t sector_index) |
| Slord2142 | 33:6c7364ea360f | 78 | { |
| Slord2142 | 33:6c7364ea360f | 79 | return sector_index < N_SECTOR_SPEC; |
| Slord2142 | 33:6c7364ea360f | 80 | } |
| Slord2142 | 33:6c7364ea360f | 81 | |
| Slord2142 | 33:6c7364ea360f | 82 | const SOF_SectorSpec_t *SOF_dev_info_by_index(uint8_t sector_index) |
| Slord2142 | 33:6c7364ea360f | 83 | { |
| Slord2142 | 33:6c7364ea360f | 84 | DASSERT(SOF_dev_is_valid_sector(sector_index)); |
| Slord2142 | 33:6c7364ea360f | 85 | return &_sec_spec[sector_index]; |
| Slord2142 | 33:6c7364ea360f | 86 | } |
| Slord2142 | 33:6c7364ea360f | 87 | |
| Slord2142 | 33:6c7364ea360f | 88 | const SOF_SectorSpec_t *SOF_dev_info(SOF_DevHandle_t hdev) |
| Slord2142 | 33:6c7364ea360f | 89 | { |
| Slord2142 | 33:6c7364ea360f | 90 | uint8_t sector_index = handle_to_sector_index(hdev); |
| Slord2142 | 33:6c7364ea360f | 91 | |
| Slord2142 | 33:6c7364ea360f | 92 | return SOF_dev_info_by_index(sector_index); |
| Slord2142 | 33:6c7364ea360f | 93 | } |
| Slord2142 | 33:6c7364ea360f | 94 | |
| Slord2142 | 33:6c7364ea360f | 95 | SOF_DevHandle_t SOF_dev_open(uint8_t sector_index) |
| Slord2142 | 33:6c7364ea360f | 96 | { |
| Slord2142 | 33:6c7364ea360f | 97 | DASSERT(sector_index < N_SECTOR_SPEC); |
| Slord2142 | 33:6c7364ea360f | 98 | return (SOF_DevHandle_t)sector_index; |
| Slord2142 | 33:6c7364ea360f | 99 | } |
| Slord2142 | 33:6c7364ea360f | 100 | |
| Slord2142 | 33:6c7364ea360f | 101 | void SOF_dev_close(SOF_DevHandle_t hdev) |
| Slord2142 | 33:6c7364ea360f | 102 | { |
| Slord2142 | 33:6c7364ea360f | 103 | } |
| Slord2142 | 33:6c7364ea360f | 104 | |
| Slord2142 | 33:6c7364ea360f | 105 | uint8_t *SOF_dev_get_hw_addr(SOF_DevHandle_t hdev) |
| Slord2142 | 33:6c7364ea360f | 106 | { |
| Slord2142 | 33:6c7364ea360f | 107 | uint8_t sector_index = handle_to_sector_index(hdev); |
| Slord2142 | 33:6c7364ea360f | 108 | |
| Slord2142 | 33:6c7364ea360f | 109 | return (uint8_t *)SECTOR_ADDR(sector_index); |
| Slord2142 | 33:6c7364ea360f | 110 | } |
| Slord2142 | 33:6c7364ea360f | 111 | |
| Slord2142 | 33:6c7364ea360f | 112 | |
| Slord2142 | 33:6c7364ea360f | 113 | void SOF_dev_erase(SOF_DevHandle_t hdev) |
| Slord2142 | 33:6c7364ea360f | 114 | { |
| Slord2142 | 33:6c7364ea360f | 115 | uint8_t sector_index = handle_to_sector_index(hdev); |
| Slord2142 | 33:6c7364ea360f | 116 | FLASH_EraseInitTypeDef ei; |
| Slord2142 | 33:6c7364ea360f | 117 | uint32_t error = 0; |
| Slord2142 | 33:6c7364ea360f | 118 | |
| Slord2142 | 33:6c7364ea360f | 119 | DPRINTF("FLASH_Erase_Sector %d"DCRLF, SECTOR_NO(sector_index)); |
| Slord2142 | 33:6c7364ea360f | 120 | HAL_FLASH_Unlock(); |
| Slord2142 | 33:6c7364ea360f | 121 | DPRINTF("FLASH_Erase_Sector_Unlocked %d"DCRLF, SECTOR_NO(sector_index)); |
| Slord2142 | 33:6c7364ea360f | 122 | |
| Slord2142 | 33:6c7364ea360f | 123 | ei.TypeErase = TYPEERASE_SECTORS; |
| Slord2142 | 33:6c7364ea360f | 124 | ei.Sector = SECTOR_NO(sector_index); |
| Slord2142 | 33:6c7364ea360f | 125 | ei.NbSectors = 1; |
| Slord2142 | 33:6c7364ea360f | 126 | ei.Banks = 0; |
| Slord2142 | 33:6c7364ea360f | 127 | ei.VoltageRange = VOLTAGE_RANGE_3; |
| Slord2142 | 33:6c7364ea360f | 128 | HAL_FLASHEx_Erase(&ei, &error); |
| Slord2142 | 33:6c7364ea360f | 129 | DPRINTF("FLASHEx_Erase %d"DCRLF, SECTOR_NO(sector_index)); |
| Slord2142 | 33:6c7364ea360f | 130 | HAL_FLASH_Lock(); |
| Slord2142 | 33:6c7364ea360f | 131 | DPRINTF("FLASH_Erase_Sector ok"DCRLF); |
| Slord2142 | 33:6c7364ea360f | 132 | } |
| Slord2142 | 33:6c7364ea360f | 133 | |
| Slord2142 | 33:6c7364ea360f | 134 | |
| Slord2142 | 33:6c7364ea360f | 135 | int SOF_dev_write_word(SOF_DevHandle_t hdev, uint32_t offset_addr, uint32_t data) |
| Slord2142 | 33:6c7364ea360f | 136 | { |
| Slord2142 | 33:6c7364ea360f | 137 | uint8_t sector_index = handle_to_sector_index(hdev); |
| Slord2142 | 33:6c7364ea360f | 138 | uint32_t dst = SECTOR_ADDR(sector_index) + offset_addr; |
| Slord2142 | 33:6c7364ea360f | 139 | |
| Slord2142 | 33:6c7364ea360f | 140 | DASSERT((offset_addr%sizeof(uint32_t)) == 0); |
| Slord2142 | 33:6c7364ea360f | 141 | HAL_FLASH_Unlock(); |
| Slord2142 | 33:6c7364ea360f | 142 | if (HAL_FLASH_Program(TYPEPROGRAM_WORD, dst, data) != HAL_OK) |
| Slord2142 | 33:6c7364ea360f | 143 | { |
| Slord2142 | 33:6c7364ea360f | 144 | DPRINTF("FLASH_ProgramWord failed: %#x"DCRLF, dst); |
| Slord2142 | 33:6c7364ea360f | 145 | HAL_FLASH_Lock(); |
| Slord2142 | 33:6c7364ea360f | 146 | return -1; |
| Slord2142 | 33:6c7364ea360f | 147 | } |
| Slord2142 | 33:6c7364ea360f | 148 | |
| Slord2142 | 33:6c7364ea360f | 149 | HAL_FLASH_Lock(); |
| Slord2142 | 33:6c7364ea360f | 150 | |
| Slord2142 | 33:6c7364ea360f | 151 | if (data != SOF_dev_read_word(hdev, offset_addr)) |
| Slord2142 | 33:6c7364ea360f | 152 | { |
| Slord2142 | 33:6c7364ea360f | 153 | DPRINTF("addr=%x %#04x %#04x"DCRLF, dst, data, SOF_dev_read_word(hdev, offset_addr)); |
| Slord2142 | 33:6c7364ea360f | 154 | return -1; |
| Slord2142 | 33:6c7364ea360f | 155 | } |
| Slord2142 | 33:6c7364ea360f | 156 | |
| Slord2142 | 33:6c7364ea360f | 157 | return 0; |
| Slord2142 | 33:6c7364ea360f | 158 | } |
| Slord2142 | 33:6c7364ea360f | 159 | |
| Slord2142 | 33:6c7364ea360f | 160 | uint32_t SOF_dev_read_word(SOF_DevHandle_t hdev, uint32_t offset_addr) |
| Slord2142 | 33:6c7364ea360f | 161 | { |
| Slord2142 | 33:6c7364ea360f | 162 | uint8_t sector_index = handle_to_sector_index(hdev); |
| Slord2142 | 33:6c7364ea360f | 163 | uint32_t src = SECTOR_ADDR(sector_index) + offset_addr; |
| Slord2142 | 33:6c7364ea360f | 164 | |
| Slord2142 | 33:6c7364ea360f | 165 | DASSERT((offset_addr%sizeof(uint32_t)) == 0); |
| Slord2142 | 33:6c7364ea360f | 166 | |
| Slord2142 | 33:6c7364ea360f | 167 | return *(volatile uint32_t*)src; |
| Slord2142 | 33:6c7364ea360f | 168 | } |
| Slord2142 | 33:6c7364ea360f | 169 | |
| Slord2142 | 33:6c7364ea360f | 170 | int SOF_dev_write_byte(SOF_DevHandle_t hdev, uint32_t offset_addr, uint8_t data) |
| Slord2142 | 33:6c7364ea360f | 171 | { |
| Slord2142 | 33:6c7364ea360f | 172 | uint8_t sector_index = handle_to_sector_index(hdev); |
| Slord2142 | 33:6c7364ea360f | 173 | uint32_t dst = SECTOR_ADDR(sector_index) + offset_addr; |
| Slord2142 | 33:6c7364ea360f | 174 | |
| Slord2142 | 33:6c7364ea360f | 175 | HAL_FLASH_Unlock(); |
| Slord2142 | 33:6c7364ea360f | 176 | if (HAL_FLASH_Program(TYPEPROGRAM_BYTE, dst, data) != HAL_OK) |
| Slord2142 | 33:6c7364ea360f | 177 | { |
| Slord2142 | 33:6c7364ea360f | 178 | DPRINTF("FLASH_ProgramWord failed: %#x"DCRLF, dst); |
| Slord2142 | 33:6c7364ea360f | 179 | HAL_FLASH_Lock(); |
| Slord2142 | 33:6c7364ea360f | 180 | return -1; |
| Slord2142 | 33:6c7364ea360f | 181 | } |
| Slord2142 | 33:6c7364ea360f | 182 | |
| Slord2142 | 33:6c7364ea360f | 183 | HAL_FLASH_Lock(); |
| Slord2142 | 33:6c7364ea360f | 184 | |
| Slord2142 | 33:6c7364ea360f | 185 | if (data != SOF_dev_read_byte(hdev, offset_addr)) |
| Slord2142 | 33:6c7364ea360f | 186 | { |
| Slord2142 | 33:6c7364ea360f | 187 | DPRINTF("addr=%x %#02x %#02x"DCRLF, dst, data, SOF_dev_read_byte(hdev, offset_addr)); |
| Slord2142 | 33:6c7364ea360f | 188 | return -1; |
| Slord2142 | 33:6c7364ea360f | 189 | } |
| Slord2142 | 33:6c7364ea360f | 190 | |
| Slord2142 | 33:6c7364ea360f | 191 | return 0; |
| Slord2142 | 33:6c7364ea360f | 192 | } |
| Slord2142 | 33:6c7364ea360f | 193 | |
| Slord2142 | 33:6c7364ea360f | 194 | uint8_t SOF_dev_read_byte(SOF_DevHandle_t hdev, uint32_t offset_addr) |
| Slord2142 | 33:6c7364ea360f | 195 | { |
| Slord2142 | 33:6c7364ea360f | 196 | uint8_t sector_index = handle_to_sector_index(hdev); |
| Slord2142 | 33:6c7364ea360f | 197 | uint32_t src = SECTOR_ADDR(sector_index) + offset_addr; |
| Slord2142 | 33:6c7364ea360f | 198 | |
| Slord2142 | 33:6c7364ea360f | 199 | return *(volatile uint8_t*)src; |
| Slord2142 | 33:6c7364ea360f | 200 | } |
| Slord2142 | 33:6c7364ea360f | 201 |