各位,我正在用C开发一个使用I2C模块的嵌入式系统。其他使用I2C的模块将I2C模块作为依赖项,或称为“子系统”。它们将消息上下文结构传递给它,当I2C模块准备就绪时,它会根据上下文的指示异步发送或读取消息,从上下文运行回调以让上覆模块知道工作已经完成、成功或出错。
我希望I2C模块能够从错误中恢复自身,即重置外部连接的硬件,然后重新初始化外部设备。这三个操作中的前两个操作可以在I2C模块内部进行处理,没有任何问题,但是I2C模块不知道哪些覆盖模块可能想使用它,并且需要向外部设备发送数据,以便在它们以前的硬件重置之后再次配置它们。
消息上下文可以对重新初始化函数进行回调,但I2C模块一次只保存对其中一个函数的引用,该模块目前正在研究如何在下一次不同模块(具有重置硬件)想要与其外部硬件对话时调用重新初始化句柄?上覆模块不知道I2C已被重置,而I2C模块不知道上覆模块是否执行了其重新初始化序列,甚至不知道是否需要。
是否有一个共同的设计模式来改善这样的问题?
以不同的方式加以说明:
I2C和外部硬件序列的正常初始化:
initExtIOExpander() {
context_t initialisationMsg = {/*...*/};
i2cSend(&initialisationMsg )
}
initExtADC() {
context_t initialisationMsg = {/*...*/};
i2cSend(&initialisationMsg )
}
i2cSend() {
// Start sending data using passed context,
}
interrupt i2c_isr() {
//If the message completed run the callback handle in the current context
// else do the next part of the message, etc.
}
main () {
//...
initI2c();
initExtIOExpander();
initExtADC();
//...
// Use external devices here.
//
}
重置和重新初始化序列:如果上述正常序列已经发生,但现在I2C ISR被更改以检测错误:
interrupt i2c_isr () {
//If there was an error reset external H/W, and own H/W
// but how to know to call initExtIOExpander() and initExtADC()??
// this module might know about the ADC if it is processing and ADC
// message but it has "forgotten" about the IO expander or any other
// device
// else if the message completed run the callback handle in the current
// context
// else do the next part of the message, etc.
}
谢谢!
发布于 2014-04-29 13:07:15
如何将所有异常初始化放在“聚合”函数中,并在需要时调用它呢?
void init_all_i2c_devices(void) {
initExtIOExpander();
initExtADC();
...
}
interrupt i2c() {
....
init_all_i2c_devices();// or i2c_init_callback()
}
main() {
init_i2c();
init_all_i2c_devices();
// set_i2c_init_callback(init_all_i2c_devices) if need
}
发布于 2014-04-29 12:03:09
对于嵌入式上下文中的设计模式,请看一下这里和这里。
特别是看门狗定时器适合你的情况。
我认为应该重设HW的不是I2C模块。
https://stackoverflow.com/questions/23362686
复制相似问题