我正在編寫基於MDK-ARM
環境中的狀態機的embedded C
代碼,該環境使用RealView
(基於ARMCC)編譯器。MDK-ARM C錯誤:表達式必須有一個常數值
IDE產生一個錯誤:
error: #28: expression must have a constant value.
的代碼部分如下:
struct wspace
{
struct netimer period;
struct nequeue deferred;
struct nevent *deferred_queue_storage[EPA_EEPROM_QUEUE_SIZE];
struct nepa *producer;
struct event_i2c_transfer transfer;
uint32_t i2c_retry;
uint8_t i2c_buffer[3];
uint32_t address;
uint8_t *raw_buffer;
uint32_t size;
uint32_t idx;
uint32_t max_size;
uint32_t dev_id;
};
static naction state_init_eeprom(struct nsm * sm, const struct nevent * event) {
struct wspace * ws = nsm_wspace(sm);
switch (event->id) {
case NSM_INIT : {
struct nequeue_define deferred_queue =
**NEQUEUE_DEF_INIT(ws->deferred_queue_storage, sizeof(ws->deferred_queue_storage));** // this is the line pointed by compiler
netimer_init(&ws->period);
nequeue_init(&ws->deferred, &deferred_queue);
ws->transfer.super = g_default_event;
ws->transfer.super.producer = nepa_get_current();
ws->transfer.dev_id = EEPROM_I2C_ID;
ws->transfer.timeout_ms = EEPROM_I2C_TIMEOUT_MS;
ws->max_size = 8 * 1024;
ws->dev_id = EEPROM_I2C_ID;
return (naction_transit_to(sm, state_idle));
}
default : {
return (naction_ignored());
}
}
}
重要的定義:
#define NEQUEUE_SIZEOF(elements)
(sizeof(struct nevent * [elements]))
#define NEQUEUE_DEF_INIT(storage, size)
{storage, size}
struct nequeue_define
{
struct nevent ** storage;/**<@brief Allocated memory storage */
size_t size; /**<@brief Size of queue in bytes */
};
struct nequeue
{
struct nqueue queue;
#if (CONFIG_REGISTRY == 1)
ncore_reg min;
#endif
#if (CONFIG_API_VALIDATION == 1)
unsigned int signature;
#endif
};
一切,我在這個題目中是相關事實上常數變量不能被assi即使之前被初始化,也會將其作爲初始化器的數組或成員的元素。
有沒有人有想法如何克服這個問題?
感謝,我認爲過,但不敢肯定。仍然不清楚如何去做。 – PredragN
我不確定,但我認爲我應該將指針作爲結構的第一個成員,但不知道如何去做?想法似乎沒問題,但你能更精確嗎?如果你仔細看看wspace結構,你可以看到struct nevent * deferred_queue_storage [EPA_EEPROM_QUEUE_SIZE]是指針的區域,其中每個指針代表一個事件緩衝區。我暫時做的是試圖將軟件從GCC移植到Keil RealView編譯器。這個NEQUEUE_DEF_INIT應該只是隊列初始化器。 – PredragN
我認爲你只需要避免使用NEQUEUE_DEF_INIT宏。你有沒有嘗試設置存儲和大小像user694733建議? –