28 #define FLASH_ACR 0x00
29 #define FLASH_PECR 0x04
30 #define FLASH_PDKEYR 0x08
31 #define FLASH_PEKEYR 0x0C
32 #define FLASH_PRGKEYR 0x10
33 #define FLASH_OPTKEYR 0x14
35 #define FLASH_OBR 0x1C
36 #define FLASH_WRPR 0x20
39 #define FLASH_ACR__LATENCY BIT(0)
40 #define FLASH_ACR__PRFTEN BIT(1)
41 #define FLASH_ACR__ACC64 BIT(2)
42 #define FLASH_ACR__SLEEP_PD BIT(3)
43 #define FLASH_ACR__RUN_PD BIT(4)
46 #define FLASH_PECR__PELOCK BIT(0)
47 #define FLASH_PECR__PRGLOCK BIT(1)
48 #define FLASH_PECR__OPTLOCK BIT(2)
49 #define FLASH_PECR__PROG BIT(3)
50 #define FLASH_PECR__DATA BIT(4)
51 #define FLASH_PECR__FTDW BIT(8)
52 #define FLASH_PECR__ERASE BIT(9)
53 #define FLASH_PECR__FPRG BIT(10)
54 #define FLASH_PECR__EOPIE BIT(16)
55 #define FLASH_PECR__ERRIE BIT(17)
56 #define FLASH_PECR__OBL_LAUNCH BIT(18)
59 #define FLASH_SR__BSY BIT(0)
60 #define FLASH_SR__EOP BIT(1)
61 #define FLASH_SR__ENDHV BIT(2)
62 #define FLASH_SR__READY BIT(3)
63 #define FLASH_SR__WRPERR BIT(8)
64 #define FLASH_SR__PGAERR BIT(9)
65 #define FLASH_SR__SIZERR BIT(10)
66 #define FLASH_SR__OPTVERR BIT(11)
69 #define PEKEY1 0x89ABCDEF
70 #define PEKEY2 0x02030405
71 #define PRGKEY1 0x8C9DAEBF
72 #define PRGKEY2 0x13141516
73 #define OPTKEY1 0xFBEAD9C8
74 #define OPTKEY2 0x24252627
77 #define DBGMCU_IDCODE 0xE0042000
78 #define DBGMCU_IDCODE_L0 0x40015800
81 #define FLASH_SECTOR_SIZE 4096
82 #define FLASH_BANK0_ADDRESS 0x08000000
85 #define OPTION_BYTES_ADDRESS 0x1FF80000
87 #define OPTION_BYTE_0_PR1 0x0000
88 #define OPTION_BYTE_0_PR0 0x00AA
101 uint16_t value, uint16_t
mask);
134 { 0x1000,
"A" }, { 0x1008,
"Y" }, { 0x1038,
"W" }, { 0x1078,
"V" },
137 { 0x1000,
"A" }, { 0x1008,
"Z" }, { 0x1018,
"Y" }, { 0x1038,
"X" }
140 { 0x1000,
"A" }, { 0x2000,
"B" }, { 0x2008,
"Y" }, { 0x2018,
"1, X" },
143 { 0x1000,
"A" }, { 0x1018,
"Y" }, { 0x1038,
"X" }, { 0x10f8,
"V" },
146 { 0x1000,
"A" }, { 0x1018,
"Z" },
149 { 0x1000,
"A" }, { 0x1008,
"Z" }, { 0x1018,
"Y" }, { 0x1038,
"X" },
155 { 0x1000,
"A" }, { 0x2000,
"B" }, { 0x2008,
"Z" },
158 { 0x1000,
"A" }, { 0x1008,
"Z" },
166 .device_str =
"STM32L1xx (Cat.1 - Low/Medium Density)",
168 .pages_per_sector = 16,
169 .max_flash_size_kb = 128,
170 .has_dual_banks =
false,
171 .flash_base = 0x40023C00,
172 .fsize_base = 0x1FF8004C,
178 .device_str =
"STM32L0xx (Cat. 3)",
180 .pages_per_sector = 32,
181 .max_flash_size_kb = 64,
182 .has_dual_banks =
false,
183 .flash_base = 0x40022000,
184 .fsize_base = 0x1FF8007C,
190 .device_str =
"STM32L0xx (Cat. 2)",
192 .pages_per_sector = 32,
193 .max_flash_size_kb = 32,
194 .has_dual_banks =
false,
195 .flash_base = 0x40022000,
196 .fsize_base = 0x1FF8007C,
202 .device_str =
"STM32L1xx (Cat.3 - Medium+ Density)",
204 .pages_per_sector = 16,
205 .max_flash_size_kb = 256,
206 .has_dual_banks =
false,
207 .flash_base = 0x40023C00,
208 .fsize_base = 0x1FF800CC,
214 .device_str =
"STM32L1xx (Cat.2)",
216 .pages_per_sector = 16,
217 .max_flash_size_kb = 128,
218 .has_dual_banks =
false,
219 .flash_base = 0x40023C00,
220 .fsize_base = 0x1FF8004C,
226 .device_str =
"STM32L1xx (Cat.4/Cat.3 - Medium+/High Density)",
228 .pages_per_sector = 16,
229 .max_flash_size_kb = 384,
230 .first_bank_size_kb = 192,
231 .has_dual_banks =
true,
232 .flash_base = 0x40023C00,
233 .fsize_base = 0x1FF800CC,
239 .device_str =
"STM32L1xx (Cat.5/Cat.6)",
241 .pages_per_sector = 16,
242 .max_flash_size_kb = 512,
243 .first_bank_size_kb = 0,
244 .has_dual_banks =
true,
245 .flash_base = 0x40023C00,
246 .fsize_base = 0x1FF800CC,
252 .device_str =
"STM32L0xx (Cat.5)",
254 .pages_per_sector = 32,
255 .max_flash_size_kb = 192,
256 .first_bank_size_kb = 0,
257 .has_dual_banks =
false,
258 .flash_base = 0x40022000,
259 .fsize_base = 0x1FF8007C,
265 .device_str =
"STM32L0xx (Cat.1)",
267 .pages_per_sector = 32,
268 .max_flash_size_kb = 16,
269 .has_dual_banks =
false,
270 .flash_base = 0x40022000,
271 .fsize_base = 0x1FF8007C,
284 stm32lx_info = calloc(1,
sizeof(*stm32lx_info));
288 LOG_ERROR(
"failed to allocate bank structure");
292 bank->driver_priv = stm32lx_info;
294 stm32lx_info->
probed =
false;
298 bank->default_padded_value =
bank->erased_value = 0x00;
438 uint16_t
mask = 0xffff;
469 for (
unsigned int i = 0; i <
bank->num_sectors; i++) {
471 bank->sectors[i].is_protected = 1;
473 bank->sectors[i].is_protected = 0;
496 for (
unsigned int i = first; i <= last; i++) {
500 bank->sectors[i].is_erased = 1;
522 static const uint8_t stm32lx_flash_write_code[] = {
523 #include "../../../contrib/loaders/flash/stm32/stm32lx.inc"
528 LOG_ERROR(
"The offset must be %" PRIu32
"B-aligned but it is %" PRIi32
"B)", hp_nb,
offset);
532 LOG_ERROR(
"The byte count must be %" PRIu32
"B-aligned but count is %" PRIu32
"B)", hp_nb,
count);
539 LOG_DEBUG(
"no working area for block memory writes");
546 sizeof(stm32lx_flash_write_code),
547 stm32lx_flash_write_code);
560 if (buffer_size <= stm32lx_info->part_info.page_size) {
565 LOG_WARNING(
"no large enough working area available, can't do block memory writes");
599 LOG_ERROR(
"unable to get armv7m target");
604 uint32_t demcr_save = armv7m->
demcr;
623 buf_set_u32(reg_params[2].value, 0, 32, this_count / hp_nb);
632 write_algorithm->
address, 0, 10000, &armv7m_info);
651 armv7m->
demcr = demcr_save;
664 LOG_WARNING(
"Couldn't use loader, falling back to page memory writes");
708 uint32_t halfpages_number;
709 uint32_t bytes_remaining = 0;
711 uint32_t bytes_written = 0;
720 LOG_ERROR(
"offset 0x%" PRIx32
" breaks required 4-byte alignment",
offset);
732 bytes_remaining = MIN(
count, hp_nb - (
offset % hp_nb));
734 while (bytes_remaining > 0) {
735 uint8_t value[4] = {0xff, 0xff, 0xff, 0xff};
738 uint32_t bytes_to_write = MIN(4, bytes_remaining);
739 memcpy(value,
buffer + bytes_written, bytes_to_write);
743 goto reset_pg_and_lock;
745 bytes_written += bytes_to_write;
746 bytes_remaining -= bytes_to_write;
751 goto reset_pg_and_lock;
755 count -= bytes_written;
758 assert((
offset % hp_nb) == 0);
761 halfpages_number =
count / hp_nb;
763 if (halfpages_number) {
767 LOG_WARNING(
"couldn't use block writes, falling back to single memory accesses");
768 halfpages_number = 0;
776 uint32_t page_bytes_written = hp_nb * halfpages_number;
777 bytes_written += page_bytes_written;
779 bytes_remaining =
count - page_bytes_written;
785 while (bytes_remaining > 0) {
786 uint8_t value[4] = {0xff, 0xff, 0xff, 0xff};
789 uint32_t bytes_to_write = MIN(4, bytes_remaining);
790 memcpy(value,
buffer + bytes_written, bytes_to_write);
794 goto reset_pg_and_lock;
796 bytes_written += bytes_to_write;
797 bytes_remaining -= bytes_to_write;
802 goto reset_pg_and_lock;
829 uint16_t flash_size_in_kb;
832 uint32_t second_bank_base;
835 stm32lx_info->
probed =
false;
841 stm32lx_info->
idcode = device_id;
843 LOG_DEBUG(
"device id = 0x%08" PRIx32, device_id);
853 LOG_ERROR(
"Cannot identify target as an STM32 L0 or L1 family device.");
868 if (retval ==
ERROR_OK && (device_id & 0xfff) == 0x436) {
869 if (flash_size_in_kb == 0)
870 flash_size_in_kb = 384;
871 else if (flash_size_in_kb == 1)
872 flash_size_in_kb = 256;
876 if (retval ==
ERROR_OK && (device_id & 0xfff) == 0x429) {
877 flash_size_in_kb &= 0xff;
882 if (retval !=
ERROR_OK || flash_size_in_kb == 0xffff || flash_size_in_kb == 0) {
883 LOG_WARNING(
"STM32L flash size failed, probe inaccurate - assuming %dk flash",
887 LOG_WARNING(
"STM32L probed flash size assumed incorrect since FLASH_SIZE=%dk > %dk, - assuming %dk flash",
902 second_bank_base = base_address +
904 if (
bank->base == second_bank_base || !
bank->base) {
906 base_address = second_bank_base;
907 flash_size_in_kb = flash_size_in_kb -
909 }
else if (
bank->base == base_address) {
913 LOG_WARNING(
"STM32L flash bank base address config is incorrect. "
916 bank->base, base_address, second_bank_base);
919 LOG_INFO(
"STM32L flash has dual banks. Bank (%u) size is %dkb, base address is 0x%" PRIx32,
920 bank->bank_number, flash_size_in_kb, base_address);
922 LOG_INFO(
"STM32L flash size is %dkb, base address is 0x%" PRIx32, flash_size_in_kb, base_address);
929 LOG_INFO(
"ignoring flash probed value, using configured bank size: %dkbytes", flash_size_in_kb);
937 bank->size = flash_size_in_kb * 1024;
938 bank->base = base_address;
939 bank->num_sectors = num_sectors;
941 if (!
bank->sectors) {
942 LOG_ERROR(
"failed to allocate bank sectors");
946 for (
unsigned int i = 0; i < num_sectors; i++) {
949 bank->sectors[i].is_erased = -1;
950 bank->sectors[i].is_protected = -1;
953 stm32lx_info->
probed =
true;
973 uint16_t rev_id = stm32lx_info->
idcode >> 16;
974 const char *rev_str =
NULL;
976 if (!stm32lx_info->
probed) {
984 for (
unsigned int i = 0; i <
info->num_revs; i++)
985 if (rev_id ==
info->revs[i].rev)
986 rev_str =
info->revs[i].str;
999 .
name =
"mass_erase",
1000 .handler = stm32lx_handle_mass_erase_command,
1003 .help =
"Erase entire flash device. including available EEPROM",
1007 .handler = stm32lx_handle_lock_command,
1010 .help =
"Increase the readout protection to Level 1.",
1014 .handler = stm32lx_handle_unlock_command,
1017 .help =
"Lower the readout protection from Level 1 to 0.",
1020 .name =
"option_load",
1021 .handler = stm32lx_handle_option_load_command,
1024 .help =
"Force re-load of device options (will cause device reset).",
1027 .name =
"option_read",
1028 .handler = stm32lx_handle_option_read_command,
1030 .usage =
"bank_id reg_offset",
1031 .help =
"Read & Display device option bytes.",
1034 .name =
"option_write",
1035 .handler = stm32lx_handle_option_write_command,
1037 .usage =
"bank_id reg_offset value [mask]",
1038 .help =
"Write device option bit fields with provided value.",
1047 .help =
"stm32lx flash command group",
1057 .flash_bank_command = stm32lx_flash_bank_command,
1234 uint32_t
addr =
bank->base +
bank->sectors[sector].offset + (page
1326 LOG_ERROR(
"timed out waiting for flash");
1333 LOG_ERROR(
"access denied / write protected");
1368 }
while (--tries > 0 &&
1375 uint16_t value, uint16_t
mask)
1384 uint32_t option_data = 0;
1391 if (
mask != 0xffff) {
1403 option_data = (option_data & ~
mask) | (value &
mask);
1405 option_data = (~(option_data & 0xffff)) << 16 | (option_data & 0xffff);
1452 stm32lx_info =
bank->driver_priv;
1487 if (flash_size_in_kb == 192 || flash_size_in_kb == 128) {
void init_reg_param(struct reg_param *param, const char *reg_name, uint32_t size, enum param_direction direction)
void destroy_reg_param(struct reg_param *param)
static struct armv7m_common * target_to_armv7m(struct target *target)
#define ARMV7M_COMMON_MAGIC
Support functions to access arbitrary bits in a byte array.
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
void command_print(struct command_invocation *cmd, const char *format,...)
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
#define ERROR_COMMAND_SYNTAX_ERROR
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
#define ERROR_COMMAND_ARGUMENT_INVALID
uint64_t buffer
Pointer to data buffer to send over SPI.
uint32_t buffer_size
Size of dw_spi_program::buffer.
uint32_t address
Starting address. Sector aligned.
#define ERROR_FLASH_OPERATION_FAILED
#define ERROR_FLASH_DST_BREAKS_ALIGNMENT
int default_flash_blank_check(struct flash_bank *bank)
Provides default erased-bank check handling.
int default_flash_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
Provides default read implementation for flash memory.
void default_flash_free_driver_priv(struct flash_bank *bank)
Deallocates bank->driver_priv.
void alive_sleep(uint64_t ms)
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
target_addr_t addr
Start address to search for the control block.
#define FLASH_SR__OPTVERR
static const struct stm32lx_rev stm32_425_revs[]
static int stm32lx_protect_check(struct flash_bank *bank)
static int stm32lx_lock_program_memory(struct flash_bank *bank)
static const struct stm32lx_rev stm32_447_revs[]
static const struct stm32lx_rev stm32_436_revs[]
static const struct stm32lx_rev stm32_437_revs[]
static int stm32lx_write_option(struct flash_bank *bank, uint32_t reg_offset, uint16_t value, uint16_t mask)
#define OPTION_BYTES_ADDRESS
static int stm32lx_update_part_info(struct flash_bank *bank, uint16_t flash_size_in_kb)
static int stm32lx_auto_probe(struct flash_bank *bank)
#define OPTION_BYTE_0_PR0
#define FLASH_PECR__OPTLOCK
#define FLASH_PECR__ERASE
static int stm32lx_read_id_code(struct target *target, uint32_t *id)
#define FLASH_BANK0_ADDRESS
static const struct stm32lx_rev stm32_416_revs[]
static int stm32lx_unlock_program_memory(struct flash_bank *bank)
static int stm32lx_probe(struct flash_bank *bank)
static const struct stm32lx_rev stm32_457_revs[]
static int stm32lx_erase_sector(struct flash_bank *bank, int sector)
static int stm32lx_mass_erase(struct flash_bank *bank)
static const struct command_registration stm32lx_exec_command_handlers[]
static const struct stm32lx_rev stm32_427_revs[]
static int stm32lx_write_half_pages(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
static const struct stm32lx_rev stm32_429_revs[]
#define FLASH_PECR__OBL_LAUNCH
static const struct command_registration stm32lx_command_handlers[]
FLASH_BANK_COMMAND_HANDLER(stm32lx_flash_bank_command)
static int stm32lx_wait_until_bsy_clear(struct flash_bank *bank)
static const struct stm32lx_rev stm32_417_revs[]
static int stm32lx_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
static const struct stm32lx_part_info stm32lx_parts[]
static int stm32lx_unlock(struct flash_bank *bank)
#define FLASH_PECR__PELOCK
static int stm32lx_get_flash_status(struct flash_bank *bank, uint32_t *status)
#define OPTION_BYTE_0_PR1
const struct flash_driver stm32lx_flash
static int stm32lx_obl_launch(struct flash_bank *bank)
COMMAND_HANDLER(stm32lx_handle_mass_erase_command)
static int stm32lx_unlock_options_bytes(struct flash_bank *bank)
static int stm32lx_wait_until_bsy_clear_timeout(struct flash_bank *bank, int timeout)
#define FLASH_SECTOR_SIZE
static int stm32lx_enable_write_half_page(struct flash_bank *bank)
static int stm32lx_lock(struct flash_bank *bank)
#define FLASH_PECR__PRGLOCK
static int stm32lx_get_info(struct flash_bank *bank, struct command_invocation *cmd)
static int stm32lx_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
enum arm_arch arch
ARM architecture version.
unsigned int common_magic
When run_command is called, a new instance will be created on the stack, filled with the proper value...
Provides details of a flash bank, available either on-chip or through a major interface.
Provides the implementation-independent structure that defines all of the callbacks required by OpenO...
const char * name
Gives a human-readable name of this flash driver, This field is used to select and initialize the dri...
Describes the geometry and status of a single flash sector within a flash bank.
struct stm32lx_part_info part_info
uint16_t first_bank_size_kb
unsigned int pages_per_sector
const struct stm32lx_rev * revs
uint16_t max_flash_size_kb
int target_halt(struct target *target)
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
int target_run_algorithm(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, target_addr_t entry_point, target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
Downloads a target-specific native code algorithm to the target, and executes it.
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
int target_poll(struct target *target)
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
int target_read_u16(struct target *target, target_addr_t address, uint16_t *value)
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
#define ERROR_TARGET_NOT_HALTED
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
static struct ublast_lowlevel_priv info