BLE TX pending queue 확장 및 ADC 패킷 재시도 처리, Ver116 업데이트
- BLE TX pending slot을 1개에서 8개로 확장 - dr_binary_tx_safe()에서 TX 결과 반환 - maa_async TX 실패 시 상태 유지 후 TX ready에서 재시도
This commit is contained in:
+60
-19
@@ -605,7 +605,7 @@ uint32_t dr_adc_get_vref(void)
|
||||
*============================================================================*/
|
||||
|
||||
/* External BLE NUS functions and variables from main.c */
|
||||
extern void dr_binary_tx_safe(uint8_t const *ble_bin_buff, uint16_t length);
|
||||
extern uint32_t dr_binary_tx_safe(uint8_t const *ble_bin_buff, uint16_t length);
|
||||
extern void dr_sd_delay_ms(uint32_t ms);
|
||||
|
||||
/* Platform interface (log function) */
|
||||
@@ -1163,6 +1163,17 @@ static maa_async_ctx_t g_maa_ctx = { .state = MAA_ASYNC_IDLE };
|
||||
|
||||
/* (reb+red merged protocol) */
|
||||
|
||||
typedef enum {
|
||||
MAA_ASYNC_TX_RETRY = 0,
|
||||
MAA_ASYNC_TX_DONE,
|
||||
MAA_ASYNC_TX_MORE
|
||||
} maa_async_tx_result_t;
|
||||
|
||||
static bool maa_async_tx_accepted(uint32_t err_code)
|
||||
{
|
||||
return (err_code == NRF_SUCCESS || err_code == NRF_ERROR_RESOURCES);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Capture one channel (internal helper)
|
||||
*/
|
||||
@@ -1192,10 +1203,11 @@ static dr_adc_err_t maa_async_capture_channel(uint8_t ch)
|
||||
* reb: tag(4) + num_samples(2) + data(up to 238 bytes)
|
||||
* If <= 100 samples (200B), the channel completes in this single packet
|
||||
*/
|
||||
static void maa_async_send_header(void)
|
||||
static bool maa_async_send_header(void)
|
||||
{
|
||||
dr_maa_channel_t *ch = &g_maa_ctx.channels[g_maa_ctx.current_ch];
|
||||
uint8_t *buf = g_maa_ctx.ble_buffer;
|
||||
uint32_t tx_err;
|
||||
|
||||
uint16_t total_data_bytes = ch->num_samples * 2;
|
||||
|
||||
@@ -1214,7 +1226,12 @@ static void maa_async_send_header(void)
|
||||
buf[dst_idx++] = (uint8_t)(sample & 0xFF);
|
||||
}
|
||||
|
||||
dr_binary_tx_safe(buf, dst_idx / 2);
|
||||
tx_err = dr_binary_tx_safe(buf, dst_idx / 2);
|
||||
if (!maa_async_tx_accepted(tx_err))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
dr_sd_delay_ms(MAA_ASYNC_POST_REB_MS);
|
||||
|
||||
g_maa_ctx.current_pkt = 0;
|
||||
@@ -1229,21 +1246,24 @@ static void maa_async_send_header(void)
|
||||
{
|
||||
g_maa_ctx.state = MAA_ASYNC_TX_DATA;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Send next red: data packet
|
||||
* @return true if more packets to send, false if done with current channel
|
||||
* @return TX result: retry, current channel done, or more packets remain
|
||||
*/
|
||||
static bool maa_async_send_data_packet(void)
|
||||
static maa_async_tx_result_t maa_async_send_data_packet(void)
|
||||
{
|
||||
dr_maa_channel_t *ch = &g_maa_ctx.channels[g_maa_ctx.current_ch];
|
||||
uint8_t *buf = g_maa_ctx.ble_buffer;
|
||||
uint16_t total_data_bytes = ch->num_samples * 2;
|
||||
uint32_t tx_err;
|
||||
|
||||
if (g_maa_ctx.data_offset >= total_data_bytes)
|
||||
{
|
||||
return false; /* All data sent */
|
||||
return MAA_ASYNC_TX_DONE; /* All data sent */
|
||||
}
|
||||
|
||||
uint16_t pkt_idx = g_maa_ctx.current_pkt;
|
||||
@@ -1265,7 +1285,12 @@ static bool maa_async_send_data_packet(void)
|
||||
buf[dst_idx++] = (uint8_t)(sample & 0xFF);
|
||||
}
|
||||
|
||||
dr_binary_tx_safe(buf, dst_idx / 2);
|
||||
tx_err = dr_binary_tx_safe(buf, dst_idx / 2);
|
||||
if (!maa_async_tx_accepted(tx_err))
|
||||
{
|
||||
return MAA_ASYNC_TX_RETRY;
|
||||
}
|
||||
|
||||
dr_sd_delay_ms(MAA_ASYNC_POST_RED_MS);
|
||||
|
||||
g_maa_ctx.data_offset += chunk_size;
|
||||
@@ -1273,15 +1298,16 @@ static bool maa_async_send_data_packet(void)
|
||||
|
||||
//ADC_LOG("maa_async: CH%u red:%u (%u/%u bytes)", g_maa_ctx.current_ch, pkt_idx, g_maa_ctx.data_offset, total_data_bytes);
|
||||
|
||||
return (g_maa_ctx.data_offset < total_data_bytes);
|
||||
return (g_maa_ctx.data_offset < total_data_bytes) ? MAA_ASYNC_TX_MORE : MAA_ASYNC_TX_DONE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Send raa: completion marker
|
||||
*/
|
||||
static void maa_async_send_completion(uint16_t status)
|
||||
static bool maa_async_send_completion(uint16_t status)
|
||||
{
|
||||
uint8_t *buf = g_maa_ctx.ble_buffer;
|
||||
uint32_t tx_err;
|
||||
|
||||
/* Wait for previous TX to complete before sending raa: */
|
||||
dr_sd_delay_ms(MAA_ASYNC_PRE_RAA_MS);
|
||||
@@ -1290,7 +1316,11 @@ static void maa_async_send_completion(uint16_t status)
|
||||
buf[4] = (uint8_t)(status >> 8);
|
||||
buf[5] = (uint8_t)(status & 0xFF);
|
||||
|
||||
dr_binary_tx_safe(buf, 3);
|
||||
tx_err = dr_binary_tx_safe(buf, 3);
|
||||
if (!maa_async_tx_accepted(tx_err))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//if (g_plat.log) g_plat.log("-------------------------------------------------------------------------------------\r\n");
|
||||
|
||||
@@ -1307,6 +1337,8 @@ static void maa_async_send_completion(uint16_t status)
|
||||
g_maa_ctx.on_complete_cb = NULL; /* one-shot: prevent re-entry */
|
||||
cb();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*==============================================================================
|
||||
@@ -1397,7 +1429,8 @@ dr_adc_err_t maa_async_start(uint8_t freq_option, uint16_t delay_us, uint16_t nu
|
||||
dr_piezo_power_off();
|
||||
|
||||
/* Send CH0 header - this will trigger TX_RDY for subsequent packets */
|
||||
maa_async_send_header();
|
||||
g_maa_ctx.state = MAA_ASYNC_TX_HEADER;
|
||||
(void)maa_async_send_header();
|
||||
|
||||
return DR_ADC_OK;
|
||||
}
|
||||
@@ -1412,8 +1445,16 @@ bool maa_async_on_tx_ready(void)
|
||||
switch (g_maa_ctx.state)
|
||||
{
|
||||
case MAA_ASYNC_TX_DATA:
|
||||
{
|
||||
maa_async_tx_result_t tx_result;
|
||||
|
||||
/* Send next data packet */
|
||||
if (!maa_async_send_data_packet())
|
||||
tx_result = maa_async_send_data_packet();
|
||||
if (tx_result == MAA_ASYNC_TX_RETRY)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (tx_result == MAA_ASYNC_TX_DONE)
|
||||
{
|
||||
/* Current channel done, move to next */
|
||||
g_maa_ctx.current_ch++;
|
||||
@@ -1422,22 +1463,20 @@ bool maa_async_on_tx_ready(void)
|
||||
{
|
||||
/* All channels done */
|
||||
g_maa_ctx.state = MAA_ASYNC_COMPLETE;
|
||||
maa_async_send_completion(0x0000);
|
||||
return false;
|
||||
return maa_async_send_completion(0x0000) ? false : true;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* all channels already captured, send header directly */
|
||||
g_maa_ctx.state = MAA_ASYNC_CAPTURING;
|
||||
maa_async_send_header();
|
||||
g_maa_ctx.state = MAA_ASYNC_TX_HEADER;
|
||||
(void)maa_async_send_header();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
case MAA_ASYNC_TX_HEADER:
|
||||
/* Header sent, start sending data */
|
||||
g_maa_ctx.state = MAA_ASYNC_TX_DATA;
|
||||
maa_async_send_data_packet();
|
||||
(void)maa_async_send_header();
|
||||
return true;
|
||||
|
||||
case MAA_ASYNC_CAPTURING:
|
||||
@@ -1445,6 +1484,8 @@ bool maa_async_on_tx_ready(void)
|
||||
return true;
|
||||
|
||||
case MAA_ASYNC_COMPLETE:
|
||||
return maa_async_send_completion(0x0000) ? false : true;
|
||||
|
||||
case MAA_ASYNC_IDLE:
|
||||
default:
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user