STM32F10x单片机Flash写操作导致中断不响应问题

昨天遇到一个问题,在写入数据到STM32F103单片机的Flash中时会出现串口中断接收数据丢失现象,但是我设置的串口接收中断优先级是最高的,并且没有哪里将全局中断关闭很长时间(除了操作系统部分内核代码执行的时候关闭全局中断,但是没有占用很长时间,不会导致丢失串口数据)。在正常情况下在没有写数据到Flash中去的时候所有串口数据都能正常接收,没有数据丢包现象,但是当执行一次写入操作(写一个页256个字的数据)就会导致串口数据丢失,导致接收帧错误。
一开始我猜测是不是在擦除和写入Flash的时候系统会屏蔽所有中断,但是没有哪个参考文档中找到这样的说明。后来查阅了一下官方文档PM0042《STM32F10xxx闪存编程手册.en》,在Page11中有一段话很重要:
During a write operation to the Flash memory, any attempt to read the Flash memory will
stall the bus. The read operation will proceed correctly once the write operation has
completed. This means that code or data fetches cannot be made while a write/erase
operation is ongoing.
For write and erase operations on the Flash memory (write/erase), the internal RC oscillator
(HSI) must be ON.
The Flash memory can be programmed and erased using in-circuit programming and in-
application programming.

中文翻译第一段话:在Flash写入操作过程中,任何试图读取Flash的操作都会锁定住总线,在完成Flash写操作之后读取Flash操作会继续执行,这意味着写入Flash期间无法访问Flash中的代码和数据。

这段话让人茅塞顿开,也就是在写入数据到Flash中去的时候无法读取flash中保存的任何数据,包括代码和常量,所以当在写入flash数据的时候如果发生了串口中断的话系统是无法执行中断服务代码的。如果长时间无法执行中断服务代码就会导致数据丢失(写入一个页的数据使用的时长太长了)。

猜你喜欢

转载自blog.csdn.net/tq384998430/article/details/80203217