
昨天遇到一个问题,在写入数据到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+ a) a# o9 R D+ h* c 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/erase5 [- Q6 F; P- L6 @& }2 _* d: F$ ? operation is ongoing. For write and erase operations on the Flash memory (write/erase), the internal RC oscillator (HSI) must be ON.: v" e9 l+ l3 s+ F The Flash memory can be programmed and erased using in-circuit programming and in-8 O: ]$ i& ?6 U" @- j$ d/ Y; j" b application programming.) T7 T! o$ K7 j) t1 }: M - T U; q: y! d- O9 R& d( M6 X0 R. C $ S r3 l2 H. w# u2 J 中文翻译第一段话:在Flash写入操作过程中,任何试图读取Flash的操作都会锁定住总线,在完成Flash写操作之后读取Flash操作会继续执行,这意味着写入Flash期间无法访问Flash中的代码和数据。 8 h! k$ k3 g# W6 Q 这段话让人茅塞顿开,也就是在写入数据到Flash中去的时候无法读取flash中保存的任何数据,包括代码和常量,所以当在写入flash数据的时候如果发生了串口中断的话系统是无法执行中断服务代码的。如果长时间无法执行中断服务代码就会导致数据丢失(写入一个页的数据使用的时长太长了)。: V2 Z# l/ V" _+ ]* f( v% { ———————————————— 版权声明:哐哐哐 Quan3 A1 M- }% ^- P 如有侵权请联系删除 3 I, j7 o% X6 q* @3 H ' |; `$ M/ F2 l5 T z |