0
\$\begingroup\$

I can't program the STM32 Flash with 8 bits of data. I have a STM32F103C8T6 and I'm trying to write in flash, I saw in HAL_FLASH libraries and I took some of the code to write in a single byte of the flash.

HAL_FLASH_Unlock();
/* Proceed to erase the page */
SET_BIT(FLASH->CR, FLASH_CR_PER);
WRITE_REG(FLASH->AR, FLASH_START_ADDR);
SET_BIT(FLASH->CR, FLASH_CR_STRT);

HAL_Delay(200);
CLEAR_BIT(FLASH->CR, FLASH_CR_PER);

SET_BIT(FLASH->CR, FLASH_CR_PG);
*((__IO uint8_t *)(FLASH_START_ADDR)) = 0x9;

HAL_Delay(200);
CLEAR_BIT(FLASH->CR, FLASH_CR_PG);

HAL_FLASH_Lock();

The loop is a simple LED blinking to see if STM get an error trying to do the flash thing. I can't write in flash with the line:

*((__IO uint8_t *)(FLASH_START_ADDR)) = 0x9;

When I change it to:

*((__IO uint16_t *)(FLASH_START_ADDR)) = 0x9;

It works, but I don't know why.

\$\endgroup\$
2
  • \$\begingroup\$ "The Flash memory can be programmed 16 bits (half words) at a time. " in reference manual for stm32f103xxxx, but what defines it? \$\endgroup\$
    – Tiago
    Commented Aug 2, 2022 at 14:27
  • \$\begingroup\$ A very warm welcome to the site. Please don't add new information in comments - edit it into your Question instead and delete the comment. Otherwise, readers have to piece all of this together to understand it. (Please rewrite the question text as needed to contain this info, don't just dump it at the end under an 'EDIT' heading as the question becomes more confusing then.) Thanks and, again, welcome. \$\endgroup\$
    – TonyM
    Commented Aug 2, 2022 at 15:07

1 Answer 1

2
\$\begingroup\$

The flash hardware is built so that it writes 16 bits at a time, presumably with no option to write more or fewer.

The processor hardware is built so that it knows how wide a write is being requested, depending on the flavor of the store instruction (STR, STRB, STRH). It communicates the width of the write to the memory bus that is connected to the flash hardware.

Somewhere in the hardware -- I think it's the responsibility of the peripheral (i.e, the flash hardware) something detects that you're trying to do a byte write rather than a half word (16-bit) write, and flags an error. I'm not going to dig for the details, but I'm 99.44% sure that it gets 'seen' by the processor as an illegal memory access.

So, the bottom line is -- the flash hardware won't let you do that no way no how, so don't bother trying with that processor.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ (In confirmation to this answer) Reference to documentation: STM32F103xx Reference manual, page 59. "Programming and erasing the Flash memory The Flash memory can be programmed 16 bits (half words) at a time. " No ways of writing/erasing flash other than with 16-bit half words are mentioned. \$\endgroup\$
    – Ilya
    Commented Aug 3, 2022 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.