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.