最小化堆栈使用:
编写程序以便调用是并行的,而不是函数调用子函数调用子函数调用子函数.... IE 顶级函数调用子函数,其中子函数立即返回,并带有状态信息。顶级函数然后调用下一个子函数......等
程序架构的(不利于堆栈限制)嵌套方法:
top level function
second level function
third level function
forth level function
在嵌入式系统中应该避免
嵌入式系统程序架构的首选方法是:
top level function (the reset event handler)
(variations in the following depending on if 'warm' or 'cold' start)
initialize hardware
initialize peripherals
initialize communication I/O
initialize interrupts
initialize status info
enable interrupts
enter background processing
interrupt handler
re-enable the interrupt
using 'scheduler'
select a foreground function
trigger dispatch for selected foreground function
return from interrupt
background processing
(this can be, and often is implemented as a 'state' machine rather than a loop)
loop:
if status info indicates need to call second level function 1
second level function 1, which updates status info
if status info indicates need to call second level function 2
second level function 2, which updates status info
etc
end loop:
请注意,尽可能不存在“第三级函数 x”
请注意,前台功能必须在再次调度之前完成。
注意:我在上面省略了很多其他细节,比如
kicking the watchdog,
the other interrupt events,
'critical' code sections and use of mutex(),
considerations between 'soft real-time' and 'hard real-time',
context switching
continuous BIT, commanded BIT, and error handling
etc