Dear Dialog Support Team;
I am building a project based on DA14583 SDK 5.0.4 sample code ble_app_peripheral.
Our project work with a SPI G-Sensor and DA14583 internal SPI Flash, but DA14583 have only one SPI controller.
So we can access only one of these SPI devices at the same time. We use G-Sensor's interrupt pin to notify DA14583 for reading sampling data.
代码是这样的(简化):
----------- Code A --------------
wkupct_register_callback(wkup_callback);
wkupct_enable_irq(...);
void wkup_callback() {
app_easy_timer(1, app_easy_timer_cb);
}
void app_easy_timer_cb() {
(access G-Sensor via SPI interface to read out sampling data)
}
And user will send command via ble characteristic's write action, that will make system save some data to internal SPI Flash.
I implement this requirement at user_custs1_ctrl_wr_ind_handler() function.
代码是这样的(简化):
----------- Code B ----------------
void user_custs1_ctrl_wr_ind_handler(...) {
if (param->value[0] == 0x01) (write data to SPI Flash)
}
I want to ask: "Will wkup_callback() function or app_easy_timer_cb() function (at Code A) interrupt the execute of user_custs1_ctrl_wr_ind_handler() function (at Code B)? vice versa." or the SDK API will make sure these callback function or handler function will execute completely without break (interrupt) by another callback or handler function? In short, do I face a race condition? Do I need to add some synchronize mechanism?
Thanks a lot.
Hi ops,
To be honest, I cannot completely understand what you are trying to accomplish. The user_custs1_ctrl_wr_ind_handler() will be triggered when the Control Point characteristic is written for the peer device. The WKUP callback function will be triggered as soon as a wake-up interrupt is detected to the wake-up controller. The app_easy_timer_cb() will be triggered if you set up a timer. Can you please provide me more inputs on that? Have you set up a timer? In addition, why don’t you use the APIs from the SPI flash driver?
Thanks, PM_Dialog
Hi, PM_Dialog;
Thanks for your quick response.
1. I do use the SPI flash driver to access the internal flash. When the Control Point characteristic is written, I can save some data to flash. This work fine. My code looks like:
void user_custs1_ctrl_wr_ind_handler(...) {
GPIO_ConfigurePin(FLASH_CS_PORT, FLASH_CS_PIN, OUTPUT, PID_SPI_EN, true);
GPIO_ConfigurePin (FLASH_CLK_PORT FLASH_CLK_PIN, OUTPUT, PID_SPI_CLK, false);
GPIO_ConfigurePin(FLASH_DO_PORT, FLASH_DO_PIN, OUTPUT, PID_SPI_DO, false);
GPIO_ConfigurePin(FLASH_DI_PORT, FLASH_DI_PIN, INPUT, PID_SPI_DI, false);
spi_init(&spi_FLASH_CS_Pad, SPI_MODE_8BIT, SPI_ROLE_MASTER, SPI_CLK_IDLE_POL_LOW, SPI_PHA_MODE_0, SPI_MINT_DISABLE, SPI_XTAL_DIV_8);
spi_flash_init(SPI_FLASH_SIZE, SPI_FLASH_PAGE);
// point A
spi_flash_block_erase(ConfigAddress, SECTOR_ERASE);
spi_flash_write_data (&configData[0], ConfigAddress, sizeof(configData));
...
}
2. I have a G-Sensor, that would generate a pulse (an external interrupt for DA14583) when it's buffer is full. When this event happens, I use WKUP callback function to read all the data out from G-Sensor's buffer vis SPI interface. This also work fine. But, somethimes I notice error happen at this WKUP callback function. It seems I do too much works at WKUP callback function, causing some kinds of timeout! That's why I am using app_easy_timer() to schedule a one-shot app_easy_timer_cb() at the WKUP callback function. In this way, WKUP callback function just do timer schedule work, so it did not causing timerout anymore. And the real work was left to the app_easy_timer_cb() function. This strategy work for me, but if not sutiable, correct me!
So, the app_easy_timer_cb() look like:
void app_easy_timer_cb(void) {
spi_init(&spi_GSensor_CS_Pad, SPI_MODE_8BIT, SPI_ROLE_MASTER, SPI_CLK_IDLE_POL_LOW, SPI_PHA_MODE_0, SPI_MINT_DISABLE, SPI_XTAL_DIV_8);
... // read G-Sensor data from SPI interface
}
3. The problem come from both Flash and G-Sensor are using SPI interface, and DA14583 had just one spi controller! when I access Flash, I must make sure
GPIO_ConfigurePin(FLASH_CS_PORT, FLASH_CS_PIN, OUTPUT, PID_SPI_EN, true);
GPIO_ConfigurePin (FLASH_CLK_PORT FLASH_CLK_PIN, OUTPUT, PID_SPI_CLK, false);
GPIO_ConfigurePin(FLASH_DO_PORT, FLASH_DO_PIN, OUTPUT, PID_SPI_DO, false);
GPIO_ConfigurePin(FLASH_DI_PORT, FLASH_DI_PIN, INPUT, PID_SPI_DI, false);
spi_init(&spi_FLASH_CS_Pad, SPI_MODE_8BIT, SPI_ROLE_MASTER, SPI_CLK_IDLE_POL_LOW, SPI_PHA_MODE_0, SPI_MINT_DISABLE, SPI_XTAL_DIV_8);
spi_flash_init(SPI_FLASH_SIZE, SPI_FLASH_PAGE);
had executed before below fucntion was called.
spi_flash_write_data (&configData[0], ConfigAddress, sizeof(configData));
In the same way, when I read G-Sensor's data, I must make sure
GPIO_ConfigurePin(GSensor_CS_PORT, GSensor_CS_PIN, OUTPUT, PID_SPI_EN, true);
GPIO_ConfigurePin(GSensor_CLK_PORT, GSensor_CLK_PIN, OUTPUT, PID_SPI_CLK, false);
GPIO_ConfigurePin(GSensor_DO_PORT, GSensor_DO_PIN, OUTPUT, PID_SPI_DO, false);
GPIO_ConfigurePin(GSensor_DI_PORT, GSensor_DI_PIN, INPUT, PID_SPI_DI, false);
spi_init(&spi_GSensor_CS_Pad, SPI_MODE_8BIT, SPI_ROLE_MASTER, SPI_CLK_IDLE_POL_LOW, SPI_PHA_MODE_0, SPI_MINT_DISABLE, SPI_XTAL_DIV_8);
had executed.
Since G-Sensor alway generate data continuously, it will causing WKUP callback to be called periodically. (In my case, app_easy_timer_cb() will also be called periodically)
What if when the Control Point characteristic is written, and the G-Sensor's buffer full at the same time?
I want to know if the following will happen?
------------------------------------------------------------------
The user_custs1_ctrl_wr_ind_handler() execute first.
Inside this handler function,
GPIO_ConfigurePin(...) was call, then spi_init() was call next, then spi_flash_init() was call.
Then before spi_flash_block_erase(...) function was call (point A), The app_easy_timer_cb() was trigger.
So, user_custs1_ctrl_wr_ind_handler() stop (pause) execute at point A. And start the exection of app_easy_timer_cb() function.
After app_easy_timer_cb() execute, user_custs1_ctrl_wr_ind_handler() resume execution from point A.
-------------------------------------------------------------------
If this happen, it will make flash write fail.
Or the SDK will execute app_easy_timer_cb() after user_custs1_ctrl_wr_ind_handler() had finished execution?
Thanks.
Hi ops,
Thanks for your detailed feedback. Let me check it and I will get back to you as soon as possible.
Thanks, PM_Dialog
Hi, PM_Dialog;
It's nice to know you are working on my question. I am looking forward to your reply.
Thanks a lot.
Hi ops,
My apologies for the late response. You will have to stop the sensor transaction with the SPI flash when the characteristic is written and then restart the transaction. Two peripherals having the SPI at the same time is not possible.
Thanks, PM_Dialog