Skip to content

Commit

Permalink
LPM3.5 firmware now runs ok
Browse files Browse the repository at this point in the history
420nA current draw has been verified.
  • Loading branch information
malcolmmackay committed Feb 7, 2021
1 parent 9603c99 commit f0528e7
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 17 deletions.
36 changes: 30 additions & 6 deletions wscodec/encoder/pyencoder/c_encoder/demi.c
Expand Up @@ -28,14 +28,26 @@
#define DEMI_TO_BLK(demi) (_startblk + (demi >> 1)) /*!< Maps a demi to its EEPROM block. */
#define IS_ODD(x) ((x & 0x01) > 0) /*!< Returns 1 if x is ODD and 0 if x is EVEN. */

extern void fram_write_enable(void); /*!< Enable writes to FRAM. Should be defined in the processor-specific cuplTag project. */
extern void fram_write_disable(void); /*!< Disable writes to FRAM. Should be defined in the processor-specific cuplTag project. */

static int _endblk = 0; /*!< Last EEPROM block in the circular buffer. */
static int _startblk = 0; /*!< First EEPROM block in the circular buffer. */
static int _cursorblk; /*!< Cursor address in terms of 16-byte EEPROM blocks. Must be >= #_startblk and <= #_endblk. */
static int _nextblk; /*!< Address of the next EEPROM block after cursor block. The buffer is circular, so it can be < #_cursorblk. */
#pragma PERSISTENT(_endblk)
int _endblk = 0; /*!< Last EEPROM block in the circular buffer. */

static int _enddemi = 0; /*!< Largest possible value of _cursordemi. Always an odd integer. */
static int _cursordemi = 0; /*!< Cursor in terms of 8-byte demis. Must be >= 0 and <= #_enddemi. */
#pragma PERSISTENT(_startblk)
int _startblk = 0; /*!< First EEPROM block in the circular buffer. */

#pragma PERSISTENT(_cursorblk)
int _cursorblk = 0; /*!< Cursor address in terms of 16-byte EEPROM blocks. Must be >= #_startblk and <= #_endblk. */

#pragma PERSISTENT(_nextblk)
int _nextblk = 0; /*!< Address of the next EEPROM block after cursor block. The buffer is circular, so it can be < #_cursorblk. */

#pragma PERSISTENT(_enddemi)
int _enddemi = 0; /*!< Largest possible value of _cursordemi. Always an odd integer. */

#pragma PERSISTENT(_cursordemi)
int _cursordemi = 0; /*!< Cursor in terms of 8-byte demis. Must be >= 0 and <= #_enddemi. */

/*!
* @brief Copy 4 demis from EEPROM into RAM.
Expand Down Expand Up @@ -107,6 +119,9 @@ void demi_init(const int startblk, const int lenblks)
{
int lendemis;

// --------- Enable FRAM writes -------------//
fram_write_enable();

_startblk = startblk;
_endblk = startblk+lenblks-1;

Expand All @@ -118,6 +133,9 @@ void demi_init(const int startblk, const int lenblks)
_enddemi = lendemis - 1;

_cursordemi = 0;

// --------- Disable FRAM writes -------------//
fram_write_disable();
}

/*!
Expand Down Expand Up @@ -153,6 +171,9 @@ DemiState_t demi_movecursor(void)
{
DemiState_t demistate = ds_consecutive;

// --------- Enable FRAM writes -------------//
fram_write_enable();

// Increment _cursordemi
if (_cursordemi == _enddemi)
{
Expand All @@ -175,6 +196,9 @@ DemiState_t demi_movecursor(void)
_nextblk = _cursorblk + 1;
}

// --------- Disable FRAM writes -------------//
fram_write_disable();

return demistate;
}

Expand Down
14 changes: 13 additions & 1 deletion wscodec/encoder/pyencoder/c_encoder/eep.c
Expand Up @@ -28,6 +28,10 @@
#define BUFSIZE_BLKS 4
#define BUFSIZE_BYTES (BUFSIZE_BLKS * BLKSIZE)

extern void fram_write_enable(void); /*!< Enable writes to FRAM. Should be defined in the processor-specific cuplTag project. */
extern void fram_write_disable(void); /*!< Disable writes to FRAM. Should be defined in the processor-specific cuplTag project. */

#pragma PERSISTENT(_blkbuffer)
char _blkbuffer[BUFSIZE_BLKS * BLKSIZE] = {0};

/*! \brief Checks if a byte index is within the bounds the buffer array.
Expand Down Expand Up @@ -81,7 +85,9 @@ int eep_read(const int eepblk, const unsigned int bufblk)
int startbyte = bufblk * BLKSIZE;
if (bufblk < BUFSIZE_BLKS)
{
fram_write_enable();
nt3h_readtag(eepblk+1, &_blkbuffer[startbyte]);
fram_write_disable();
errflag = 0;
}

Expand All @@ -102,7 +108,9 @@ int eep_swap(const unsigned int srcblk, const unsigned int destblk)
{
for (i=0; i<BUFSIZE_BLKS; i++)
{
fram_write_enable();
_blkbuffer[destblk + i] = _blkbuffer[srcblk + i];
fram_write_disable();
}
errflag = 0;
}
Expand All @@ -129,7 +137,9 @@ int eep_cp(int * indexptr, const char * dataptr, const int lenbytes)
{
for (i=0; i<lenbytes; i++)
{
_blkbuffer[startbyte + i] = *(dataptr + i);
fram_write_enable();
_blkbuffer[startbyte + i] = *(dataptr + i);
fram_write_disable();
}
*indexptr = endbyte + 1;
errflag = 0;
Expand All @@ -151,7 +161,9 @@ int eep_cpbyte(int * indexptr, const char bytedata)

if (inbounds(*indexptr))
{
fram_write_enable();
_blkbuffer[*indexptr] = bytedata;
fram_write_disable();
(*indexptr) = (*indexptr) + 1;
errflag = 0;
}
Expand Down
1 change: 0 additions & 1 deletion wscodec/encoder/pyencoder/c_encoder/nvtype.c
Expand Up @@ -35,6 +35,5 @@
nv_t nv = {.serial="AAAACCCC", .seckey="AAAACCCC"};

void fram_write_enable() {}

void fram_write_disable() {}
#endif
26 changes: 23 additions & 3 deletions wscodec/encoder/pyencoder/c_encoder/pairhist.c
Expand Up @@ -32,11 +32,17 @@
#define MD5BLKLEN_BYTES 64 /*!< Length of the MD5 input message block in bytes. */

extern nv_t nv;
extern void fram_write_enable(void); /*!< Enable writes to FRAM. Should be defined in the processor-specific cuplTag project. */
extern void fram_write_disable(void); /*!< Disable writes to FRAM. Should be defined in the processor-specific cuplTag project. */

#pragma PERSISTENT(pairhistory)
pair_t pairhistory[BUFLEN_PAIRS] = {0}; /*!< Array of unencoded pairs. This mirrors the circular buffer of base64 encoded pairs stored in EEPROM. */

#pragma PERSISTENT(cursorindex)
int cursorindex = -1; /*!< Index marking the end of the circular buffer. The most recent sample is stored here. The next index contains the oldest sample. */

unsigned char msgblock[MD5BLKLEN_BYTES]; /*!< Block to hold message data as an input to the MD5 algorithm. */
const int buflenpairs= BUFLEN_PAIRS; /*!< Length of the circular buffer in pairs. */
static pair_t pairhistory[BUFLEN_PAIRS]; /*!< Array of unencoded pairs. This mirrors the circular buffer of base64 encoded pairs stored in EEPROM. */
static int cursorindex = -1; /*!< Index marking the end of the circular buffer. The most recent sample is stored here. The next index contains the oldest sample. */
static const char ipadchar = 0x36; /*!< Inner padding byte for HMAC as defined in <a href="https://tools.ietf.org/html/rfc2104#section-2">RFC 2104</a>.*/
static const char opadchar = 0x5C; /*!< Outer padding byte for HMAC as defined in <a href="https://tools.ietf.org/html/rfc2104#section-2">RFC 2104</a>. */
static MD5_CTX ctx; /*!< MD5 context. */
Expand All @@ -51,7 +57,19 @@ static MD5_CTX ctx; /*!< MD5 context. */
*/
void pairhist_ovr(pair_t pair)
{
pairhistory[cursorindex] = pair;
fram_write_enable();
pairhistory[cursorindex] = pair;
fram_write_disable();
}

/*!
* @brief Initialise the cursorindex
* The circular buffer itself (pairhistory) does not need to be initialised.
*/
void pairhist_init() {
fram_write_enable();
cursorindex = -1;
fram_write_disable();
}

/*!
Expand All @@ -62,6 +80,7 @@ void pairhist_ovr(pair_t pair)
*/
void pairhist_push(pair_t pair)
{
fram_write_enable();
if (cursorindex == BUFLEN_PAIRS-1)
{
cursorindex = 0; // Write next pair to the start of the buffer.
Expand All @@ -72,6 +91,7 @@ void pairhist_push(pair_t pair)
}

pairhistory[cursorindex] = pair;
fram_write_disable();
}

/*!
Expand Down
1 change: 1 addition & 0 deletions wscodec/encoder/pyencoder/c_encoder/pairhist.h
Expand Up @@ -69,6 +69,7 @@ typedef struct

/// Have doxygen ignore this
/// @cond
void pairhist_init(void);
void pairhist_ovr(pair_t pair);
void pairhist_push(pair_t pair);
hashn_t pairhist_hash(int npairs, int usehmac, unsigned int loopcount, unsigned int resetsalltime, unsigned int batv_resetcause, int cursorpos);
Expand Down
11 changes: 5 additions & 6 deletions wscodec/encoder/pyencoder/c_encoder/sample.c
Expand Up @@ -38,10 +38,9 @@
#define BATV_RESETCAUSE(BATV, RSTC) ((BATV << 8) | (RSTC & 0xFF)) /*!< Macro for creating a 16-bit batv_resetcause value from 8-bit CODEC_FEAT_30 and CODEC_SPEC_16 values. */


extern nv_t nv; /*!< Externally defined parameters stored in non-volatile memory. */
extern void fram_write_enable(void);
extern void fram_write_disable(void);

extern nv_t nv; /*!< Externally defined parameters stored in non-volatile memory. */
extern void fram_write_enable(void); /*!< Enable writes to FRAM. Should be defined in the processor-specific cuplTag project. */
extern void fram_write_disable(void); /*!< Disable writes to FRAM. Should be defined in the processor-specific cuplTag project. */


typedef enum {
Expand Down Expand Up @@ -81,7 +80,7 @@ pair_t pairbuf[2] = {0}; /*!< Stores two unencoded 3-byte pairs. */
unsigned int npairs = 0; /*!< Number of base64 encoded pairs in the circular buffer, starting from the endstop and counting backwards. */

#pragma PERSISTENT(state)
pairbufstate_t state = 0; /*!< Pair buffer write state. */
pairbufstate_t state = pairbuf_initial; /*!< Pair buffer write state. */

#pragma PERSISTENT(endstop)
endstop_t endstop = {0}; /*!< The 16 byte end stop. */
Expand All @@ -91,7 +90,6 @@ stat_t status = {0}; /*!< Structure to hold unencoded status data




static bool one_reading_per_sample(void)
{
return (nv.format == HDC2021_TEMPONLY);
Expand Down Expand Up @@ -219,6 +217,7 @@ void enc_init(unsigned int resetcause, bool err, unsigned int batv)
}

ndef_writeblankurl(buflenblks, statusb64, &startblk);
pairhist_init();
demi_init(startblk, buflenblks);
}

Expand Down

0 comments on commit f0528e7

Please sign in to comment.