【发布时间】:2019-05-02 23:34:59
【问题描述】:
我正在尝试优化对我制作的一些跳转表的访问,它们如下:
int (*const usart_ctrl_table[USART_READ_WRITE_CLEAR])() =
{zg_usartCtrlRead, zg_usartCtrlWrite, zg_usartCtrlClr};
int (*const usart_frame_table[USART_READ_WRITE_CLEAR])() =
{zg_usartFrameRead, zg_usartFrameWrite, zg_usartFrameClr};
int (*const usart_trig_ctrl_table[USART_READ_WRITE_CLEAR])() =
{zg_usartTrigctrlRead, zg_usartTrigctrlWrite, zg_usartTrigctrlClr};
如您所见,这些函数用于在硬件级别访问 usart 外围设备,并按读/写/清除的顺序排列在表中。
我想要做的是有另一个跳转表的跳转表,这样我可以在启动时初始化所有 usart 的寄存器,或者如果需要,稍后只需更改单个寄存器。
即
<datatype> (*usart_peripheral_table[<number of jump tables>])() =
{usart_ctrl_table, usart_frame_table, usart_trig_ctrl_table};
这样我可以将该表暴露给我的中间件层,这将有助于在更改 HAL 时保持标准,并且我可以使用定义来索引该表,即
fn_ptr = usart_peripheral_table[CTRL_TABLE]
fn_ptr[WRITE](bitmask);
fn_ptr[READ](buffer);
正如您可能已经猜到的那样,我正在努力弄清楚如何构建这张表。我认为这是两件事之一:
-
另一个简单的指针数组,因为即使是跳转表本身也只是一个指针数组。因此我的初始化将是:
const int* (*usart_peripheral_table[<number of jump tables])() = {usart_ctrl_table, usart_frame_table, usart_trig_ctrl_table};
但是这似乎不起作用。然后我想:
-
指向指针的指针数组。于是我尝试了各种连击:
const int**(*usart_perip... const int**(usart_perip... const int** (*usart_peripheral_table[<number of jump tables])() = {&usart_ctrl_table, &usart_frame_table[0], usart_trig_ctrl_table};
似乎没有任何效果。在将该变量分配给指针到指针数组之前,是否需要将较低跳转表的地址存储在另一个指针中?即
int* fn_ptr = usart_ctrl_table;
<dataytype>(*const usart_periph[<number>])() = {fn_ptr};
在此先感谢,任何帮助将不胜感激。
MM25
编辑:
const int** (*const peripheral_table[1])() =
{&usart_ctrl_table[0]};
const int** (*const peripheral_table[1])() =
{usart_ctrl_table};
以上都给出错误“从不兼容的指针类型初始化”,我尝试过的所有其他组合也是如此
【问题讨论】:
-
关于“什么是正确的方法” - 你回答了你的问题。您需要多个并行表。没关系。因此,最好的方法可能是定义一个指针,该指针可以配置为在运行时指向这些表中的一个或另一个。问:当您尝试这种方法时,究竟遇到了什么问题?问:您能否显示相关代码,以及确切的错误信息(如果适用)?
-
添加了错误编辑
标签: c function-pointers jump-table