Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions wscodec/encoder/pyencoder/c_encoder/demi.c
Original file line number Diff line number Diff line change
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
19 changes: 15 additions & 4 deletions wscodec/encoder/pyencoder/c_encoder/eep.c
Original file line number Diff line number Diff line change
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 All @@ -47,16 +51,15 @@ static inline int inbounds(int byteindex)
* \param eepblk Block of the EEPROM to write to.
* \param bufblk Block of the buffer to write from.
*
* \returns 1 if the block to be written greater than the buffer size. Otherwise 0.
* \returns 1 if the block to be written greater than the buffer size. 0 on success and -1 on write error.
*/
int eep_write(const int eepblk, const unsigned int bufblk)
{
int errflag = 1;
int startbyte = bufblk * BLKSIZE;
if (bufblk < BUFSIZE_BLKS)
{
nt3h_writetag(eepblk+1, &_blkbuffer[startbyte]);
errflag = 0;
errflag = nt3h_writetag(eepblk+1, &_blkbuffer[startbyte]);
}

return errflag;
Expand All @@ -81,7 +84,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 +107,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 +136,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 +160,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
4 changes: 2 additions & 2 deletions wscodec/encoder/pyencoder/c_encoder/nt3h.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
#ifndef _NT3H_H_
#define _NT3H_H_

void nt3h_init(void);
int nt3h_writetag(int eepromBlock, char * blkdata);
int nt3h_readtag(int eepromBlock, char * blkdata);
int nt3h_eepromwritedone(void);
void nt3h_check_address(void);
int nt3h_check_address(void);
void nt3h_update_cc(void);
void nt3h_init_wrongaddress(void);

int printint(int myint);
Expand Down
3 changes: 3 additions & 0 deletions wscodec/encoder/pyencoder/c_encoder/nvtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@

#ifndef NOT_CFFI
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading