danieldrotos / ucsim

uCsim microcontroller simulator
https://danieldrotos.github.io/ucsim/
GNU General Public License v2.0
10 stars 0 forks source link

STM8: FLASH_FLAG_EOP not being raised #1

Closed dzid26 closed 1 week ago

dzid26 commented 4 months ago

Command: ucsim_stm8 -t stm8s105 bin/main

I have this program https://github.com/emmebrusa/TSDZ2-Smart-EBike-1/blob/ad782fd3b5691bbf9400e01387ef65ae0ceded79/src/eeprom.c#L115

It gets stuck in the while loop expecting for the FLASH_FLAG_EOP bit to be raised.

Looking at the dump of the FLASH->IAPSR register, which is 48 and so indeed the FLASH_FLAG_EOP bit is not set.

> dump 0x505F
0x0505f                                48 e1 31 00 ab 00 23 b1 H.1...#.

Again, FLASH_FLAG_EOP is third bit:

0> dump 0x505F.2
0x0505f.2                                        0b-----0-- 0x00 '.'   0

And the eeprom is never written:

> dump 0x4000
0x04000                                00 00 00 00 00 00 00 00 ........
0x04008                                00 00 00 00 00 00 00 00 ........
0x04010                                00 00 00 00 00 00 00 00 ........
0x04018                                00 00 00 00 00 00 00 00 ........
0x04020                                00 00 00 00 00 00 00 00 ........
0x04028                                00 00 00 00 00 00 00 00 ........
0x04030                                00 00 00 00 00 00 00 00 ........
0x04038                                00 00 00 00 00 00 00 00 ........
0x04040                                00 00 00 00 00 00 00 00 ........
0x04048                                00 00 00 00 00 00 00 00 ........

If I comment out the offending line the program goes further and eeprom is actually written:

> dump 0x4000
0x04000                                cc 0d 86 01 98 08 19 00 ........
0x04008                                00 00 00 00 00 01 01 00 ........
0x04010                                01 01 00 00 00 00 00 00 ........
0x04018                                00 00 00 00 00 00 00 00 ........
0x04020                                00 00 00 00 00 00 00 00 ........
0x04028                                00 00 00 00 00 00 00 00 ........
0x04030                                00 00 00 00 00 00 00 00 ........
0x04038                                00 00 00 00 00 00 00 00 ........                                                                                                                           
0x04040                                00 00 00 00 00 00 00 00 ........
0x04048                                00 00 00 00 00 00 00 00 ........

Reference:

    FLASH_FLAG_HVOFF     = (uint8_t)0x40,     /*!< End of high voltage flag */
    FLASH_FLAG_DUL       = (uint8_t)0x08,     /*!< Data EEPROM unlocked flag */
    FLASH_FLAG_EOP       = (uint8_t)0x04,     /*!< End of programming (write or erase operation) flag */
    FLASH_FLAG_PUL       = (uint8_t)0x02,     /*!< Flash Program memory unlocked flag */
    FLASH_FLAG_WR_PG_DIS = (uint8_t)0x01      /*!< Write attempted to protected page flag */
dzid26 commented 4 months ago

Overriding this register also doesn't seem to be possible: >fill io_chip 0x0005f 0x0005f 0x3 or > fill io_chip 0x0005f 0x0005f 0x4c
results in 0x08:

> dump 0x0505f
0x0505f                                08 e1 31 00 ab 00 23 b1 ..1...#
danieldrotos commented 1 week ago

Simulatin EOP bit is now fixed.