now it's possible to read and write SYSTEM.INI using bulk
This commit is contained in:
+56
-48
@@ -18,7 +18,7 @@ enum ComportSelection gActiveComport = COMPORT_USB; // start with USB so the han
|
||||
static uint32_t last_switch_time = 0; // started with USB
|
||||
static bool xfer_verify_done = false;
|
||||
|
||||
static void configure_interface(enum ComportSelection iface);
|
||||
static bool configure_interface(enum ComportSelection iface);
|
||||
|
||||
/** Switch com transfer if the current one doesnt seem to work */
|
||||
void com_switch_transfer_if_needed(void)
|
||||
@@ -30,8 +30,8 @@ void com_switch_transfer_if_needed(void)
|
||||
|
||||
if (gActiveComport == COMPORT_USB) {
|
||||
if (elapsed > 1000) {
|
||||
// USB may or may not work, depending on whether the module is plugged -
|
||||
// in or running from a battery/external supply remotely.
|
||||
// USB may or may not work, depending on whether the module is plugged in
|
||||
// or running from a battery/external supply remotely.
|
||||
|
||||
// Check if USB is enumerated
|
||||
|
||||
@@ -39,19 +39,30 @@ void com_switch_transfer_if_needed(void)
|
||||
if (0 == uadr) {
|
||||
dbg("Not enumerated, assuming USB is dead");
|
||||
|
||||
// Fallback to bare USART
|
||||
if (SystemSettings.use_comm_uart) {
|
||||
configure_interface(COMPORT_USART);
|
||||
}
|
||||
else if (SystemSettings.use_comm_nordic) {
|
||||
configure_interface(COMPORT_NORDIC); // this fallbacks to LoRa if LoRa enabled
|
||||
}
|
||||
else if (SystemSettings.use_comm_lora) {
|
||||
configure_interface(COMPORT_LORA);
|
||||
}
|
||||
else {
|
||||
dbg("No alternate com interface configured, leaving USB enabled.");
|
||||
}
|
||||
// Fallback to radio or bare USART
|
||||
do {
|
||||
if (SystemSettings.use_comm_nordic) {
|
||||
if (configure_interface(COMPORT_NORDIC)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (SystemSettings.use_comm_lora) {
|
||||
if (configure_interface(COMPORT_LORA)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (SystemSettings.use_comm_uart) {
|
||||
// after nordic/lora
|
||||
if (configure_interface(COMPORT_USART)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
dbg("No alternate com interface configured.");
|
||||
gActiveComport = COMPORT_NONE;
|
||||
} while (0);
|
||||
} else {
|
||||
dbg("USB got address 0x%02x - OK", (int)uadr);
|
||||
}
|
||||
@@ -131,7 +142,7 @@ void com_iface_flush_buffer(void)
|
||||
}
|
||||
}
|
||||
|
||||
static void configure_interface(enum ComportSelection iface)
|
||||
static bool configure_interface(enum ComportSelection iface)
|
||||
{
|
||||
// Teardown
|
||||
if (gActiveComport == COMPORT_USB) {
|
||||
@@ -146,11 +157,17 @@ static void configure_interface(enum ComportSelection iface)
|
||||
__HAL_RCC_USART2_CLK_DISABLE();
|
||||
irqd_detach(USART2, com_UsartIrqHandler);
|
||||
}
|
||||
gActiveComport = COMPORT_NONE;
|
||||
else if (gActiveComport == COMPORT_NORDIC) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
|
||||
gActiveComport = iface;
|
||||
|
||||
// Init
|
||||
if (iface == COMPORT_USB) {
|
||||
trap("illegal"); // this never happens
|
||||
return false;
|
||||
}
|
||||
else if (iface == COMPORT_USART) {
|
||||
dbg("Setting up UART transfer");
|
||||
@@ -171,37 +188,28 @@ static void configure_interface(enum ComportSelection iface)
|
||||
LL_USART_SetTransferDirection(USART2, LL_USART_DIRECTION_TX_RX);
|
||||
|
||||
LL_USART_Enable(USART2);
|
||||
|
||||
return true; // always OK (TODO check voltage on Rx if it's 3V3 when idle?)
|
||||
}
|
||||
else if (iface == COMPORT_NORDIC) {
|
||||
// Try to configure nordic
|
||||
dbg("Setting up nRF transfer");
|
||||
|
||||
// TODO set up and check nRF transport
|
||||
|
||||
|
||||
// On failure, try setting up LoRa
|
||||
dbg("nRF failed to init");
|
||||
return false;
|
||||
}
|
||||
else if (iface == COMPORT_LORA) {
|
||||
// Try to configure nordic
|
||||
dbg("Setting up LoRa transfer");
|
||||
// TODO set up and check LoRa transport
|
||||
dbg("LoRa failed to init");
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
if (iface == COMPORT_NORDIC) {
|
||||
// Try to configure nordic
|
||||
dbg("Setting up nRF transfer");
|
||||
|
||||
// TODO set up and check nRF transport
|
||||
|
||||
// On failure, try setting up LoRa
|
||||
dbg("nRF failed to init");
|
||||
if (SystemSettings.use_comm_lora) {
|
||||
iface = COMPORT_LORA;
|
||||
} else {
|
||||
iface = COMPORT_NONE; // fail
|
||||
}
|
||||
}
|
||||
|
||||
if (iface == COMPORT_LORA) {
|
||||
// Try to configure nordic
|
||||
dbg("Setting up LoRa transfer");
|
||||
|
||||
// TODO set up and check LoRa transport
|
||||
|
||||
dbg("LoRa failed to init");
|
||||
iface = COMPORT_NONE; // fail
|
||||
}
|
||||
trap("Bad iface %d", iface);
|
||||
}
|
||||
|
||||
if (iface == COMPORT_NONE) {
|
||||
dbg("NO COM PORT AVAILABLE!");
|
||||
}
|
||||
|
||||
gActiveComport = iface;
|
||||
}
|
||||
|
||||
+33
-14
@@ -58,26 +58,33 @@ static TF_Result lst_list_units(TinyFrame *tf, TF_Msg *msg)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** Callback for bulk read of the settings file */
|
||||
static void settings_bulkread_cb(BulkRead *bulk, uint32_t chunk, uint8_t *buffer)
|
||||
/** Callback for bulk read of a settings file */
|
||||
static void ini_bulkread_cb(BulkRead *bulk, uint32_t chunk, uint8_t *buffer)
|
||||
{
|
||||
// clean-up request
|
||||
if (buffer == NULL) {
|
||||
free_ck(bulk);
|
||||
iw_end();
|
||||
// dbg("INI read complete.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (bulk->offset == 0) iw_begin();
|
||||
|
||||
IniWriter iw = iw_init((char *)buffer, bulk->offset, chunk);
|
||||
iw.tag = 1;
|
||||
settings_build_units_ini(&iw);
|
||||
iw.tag = 1; // indicates this is read via the API (affects some comments)
|
||||
|
||||
uint8_t filenum = (uint8_t) (int) bulk->userdata;
|
||||
|
||||
if (filenum == 0) {
|
||||
settings_build_units_ini(&iw);
|
||||
}
|
||||
else if (filenum == 1) {
|
||||
settings_build_system_ini(&iw);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Listener: Export INI file via TF
|
||||
* Listener: Export a file via TF
|
||||
*/
|
||||
static TF_Result lst_ini_export(TinyFrame *tf, TF_Msg *msg)
|
||||
{
|
||||
@@ -86,14 +93,29 @@ static TF_Result lst_ini_export(TinyFrame *tf, TF_Msg *msg)
|
||||
BulkRead *bulk = malloc_ck(sizeof(BulkRead));
|
||||
assert_param(bulk != NULL);
|
||||
|
||||
uint8_t filenum = 0;
|
||||
|
||||
// if any payload, the first byte defines the file to read
|
||||
// 0 - units
|
||||
// 1 - system
|
||||
// (this is optional for backwards compatibility)
|
||||
if (msg->len > 0) {
|
||||
filenum = msg->data[0];
|
||||
}
|
||||
|
||||
bulk->frame_id = msg->frame_id;
|
||||
bulk->len = iw_measure_total(settings_build_units_ini, 1);
|
||||
bulk->read = settings_bulkread_cb;
|
||||
bulk->userdata = NULL;
|
||||
bulk->read = ini_bulkread_cb;
|
||||
bulk->userdata = (void *) (int)filenum;
|
||||
|
||||
if (filenum == 0) {
|
||||
bulk->len = iw_measure_total(settings_build_units_ini, 1);
|
||||
}
|
||||
else if (filenum == 1) {
|
||||
bulk->len = iw_measure_total(settings_build_system_ini, 1);
|
||||
}
|
||||
|
||||
bulkread_start(tf, bulk);
|
||||
Indicator_Effect(STATUS_DISK_BUSY_SHORT);
|
||||
|
||||
return TF_STAY;
|
||||
}
|
||||
|
||||
@@ -142,7 +164,7 @@ static TF_Result lst_ini_import(TinyFrame *tf, TF_Msg *msg)
|
||||
PayloadParser pp = pp_start(msg->data, msg->len, NULL);
|
||||
uint32_t len = pp_u32(&pp);
|
||||
if (!pp.ok) {
|
||||
com_respond_error(msg->frame_id, E_PROTOCOL_BREACH);
|
||||
com_respond_error(msg->frame_id, E_MALFORMED_COMMAND);
|
||||
goto done;
|
||||
}
|
||||
|
||||
@@ -151,11 +173,8 @@ static TF_Result lst_ini_import(TinyFrame *tf, TF_Msg *msg)
|
||||
|
||||
settings_load_ini_begin();
|
||||
ini_parse_begin(iniparser_cb, NULL);
|
||||
|
||||
bulkwrite_start(tf, bulk);
|
||||
|
||||
Indicator_Effect(STATUS_DISK_BUSY);
|
||||
|
||||
done:
|
||||
return TF_STAY;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user