Skip to content

Commit

Permalink
fix #5: added feature to load FW via Serial
Browse files Browse the repository at this point in the history
  • Loading branch information
dhineshkumarmcci committed Jun 30, 2023
1 parent b4bfa08 commit e5e242c
Showing 1 changed file with 135 additions and 1 deletion.
136 changes: 135 additions & 1 deletion model4841-production-lorawan.ino
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Author:
#include <sgpc3.h>
#include <mcciadk_baselib.h>
#include <stdlib.h>
#include <Catena_Download.h>
#include <Catena_BootloaderApi.h>

#include <cstdint>

Expand Down Expand Up @@ -82,6 +84,15 @@ SPIClass gSPI2(
Catena_Mx25v8035f gFlash;
bool gfFlash;

/* instantiate a serial object */
cSerial<decltype(Serial)> gSerial(Serial);

/* instantiate the bootloader API */
cBootloaderApi gBootloaderApi;

/* instantiate the downloader */
cDownload gDownload;

// The Temperature/humidity sensor
cSHT3x gTempRh { Wire };

Expand Down Expand Up @@ -132,6 +143,26 @@ sMyExtraCommands_top(
nullptr /* this is no "first word" for all the commands in this table */
);

// forward reference to the command function
static cCommandStream::CommandFn cmdUpdate;

// the individual commmands are put in this table
static const cCommandStream::cEntry sMyFWUpdateCommmands[] =
{
{ "fallback", cmdUpdate },
{ "update", cmdUpdate },
// other commands go here....
};

/* a top-level structure wraps the above and connects to the system table */
/* it optionally includes a "first word" so you can for sure avoid name clashes */
static cCommandStream::cDispatch
sMyFWUpdateCommmands_top(
sMyFWUpdateCommmands, /* this is the pointer to the table */
sizeof(sMyFWUpdateCommmands), /* this is the size of the table */
"system" /* this is the "first word" for all the commands in this table*/
);

/****************************************************************************\
|
| Setup
Expand All @@ -145,9 +176,9 @@ void setup()

setup_flash();
setup_radio();
setup_commands();
setup_sensors();
setup_pms7003();
setup_commands();
setup_measurement();
}

Expand Down Expand Up @@ -304,6 +335,109 @@ void setup_commands()
*/
nullptr
);
/* add our application-specific commands */
gCatena.addCommands(
/* name of app dispatch table, passed by reference */
sMyFWUpdateCommmands_top,
/*
|| optionally a context pointer using static_cast<void *>().
|| normally only libraries (needing to be reentrant) need
|| to use the context pointer.
*/
nullptr
);

gDownload.begin(gFlash, gBootloaderApi);
}

/* process "system" "update" / "system" "fallback" -- args are ignored */
// argv[0] is "update" or "fallback"
// argv[1..argc-1] are the (ignored) arguments
static cCommandStream::CommandStatus cmdUpdate(
cCommandStream *pThis,
void *pContext,
int argc,
char **argv
)
{
cCommandStream::CommandStatus result;

pThis->printf(
"Update firmware: echo off, timeout %d seconds\n",
(cDownload::kTransferTimeoutMs + 500) / 1000
);

if (! gfFlash)
{
pThis->printf(
"** flash not found at init time, can't update **\n"
);
return cCommandStream::CommandStatus::kIoError;
}

gSPI2.begin();
gFlash.begin(&gSPI2, Catena::PIN_SPI2_FLASH_SS);

struct context_t
{
cCommandStream *pThis;
bool fWorking;
cDownload::Status_t status;
cCommandStream::CommandStatus cmdStatus;
cDownload::Request_t request;
};

context_t context { pThis, true };

auto doneFn =
[](void *pUserData, cDownload::Status_t status) -> void
{
context_t * const pCtx = (context_t *)pUserData;

cCommandStream * const pThis = pCtx->pThis;
cCommandStream::CommandStatus cmdStatus;

cmdStatus = cCommandStream::CommandStatus::kSuccess;

if (status != cDownload::Status_t::kSuccessful)
{
pThis->printf(
"download error, status %u\n",
unsigned(status)
);
cmdStatus = cCommandStream::CommandStatus::kIoError;
}

pCtx->cmdStatus = cmdStatus;
pCtx->fWorking = false;
};

if (gDownload.evStartSerialDownload(
argv[0][0] == 'u' ? cDownload::DownloadRq_t::GetUpdate
: cDownload::DownloadRq_t::GetFallback,
gSerial,
context.request,
doneFn,
(void *) &context)
)
{
while (context.fWorking)
gCatena.poll();

result = context.cmdStatus;
}
else
{
pThis->printf(
"download launch failure\n"
);
result = cCommandStream::CommandStatus::kInternalError;
}

gFlash.powerDown();
gSPI2.end();

return result;
}

void setup_measurement()
Expand Down

0 comments on commit e5e242c

Please sign in to comment.