1. 问题
在调试I2C外设的demo时,按照官方文档的描述调用相关API,烧录程序后发现程序会不断崩溃,系统log如下。
初步分析log,原因是访问到了不存在的地址。一开始我以为是自己的代码问题,反反复复改了几次都会出现同样的问题。
后面使用backtrace追踪出现问题的代码位置,代码在执行到i2c_slave.c的77行的时候崩溃了。
这是I2C中断服务函数内调用的,仔细观察发现,这个函数的处理就有问题。i2c_ll_read_rxfifo函数中传入的这个t->rcv_fifo_cnt参数是我们在上层调用i2c_slave_receive函数时传进去的buffer_size,这个参数的官方描述如下。
/**
* @brief Read bytes from I2C internal buffer. Start a job to receive I2C data.
*
* @note This function is non-blocking, it initiates a new receive job and then returns.
* User should check the received data from the `on_recv_done` callback t