Skip to content

Commit f4ccb30

Browse files
Merge pull request #27 from cuplsensor/dev
Update from dev.
2 parents 836185d + 99112d4 commit f4ccb30

7 files changed

Lines changed: 124 additions & 29 deletions

File tree

wscodec/encoder/pyencoder/c_encoder/demi.c

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,26 @@
2828
#define DEMI_TO_BLK(demi) (_startblk + (demi >> 1)) /*!< Maps a demi to its EEPROM block. */
2929
#define IS_ODD(x) ((x & 0x01) > 0) /*!< Returns 1 if x is ODD and 0 if x is EVEN. */
3030

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

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

37-
static int _enddemi = 0; /*!< Largest possible value of _cursordemi. Always an odd integer. */
38-
static int _cursordemi = 0; /*!< Cursor in terms of 8-byte demis. Must be >= 0 and <= #_enddemi. */
37+
#pragma PERSISTENT(_startblk)
38+
int _startblk = 0; /*!< First EEPROM block in the circular buffer. */
39+
40+
#pragma PERSISTENT(_cursorblk)
41+
int _cursorblk = 0; /*!< Cursor address in terms of 16-byte EEPROM blocks. Must be >= #_startblk and <= #_endblk. */
42+
43+
#pragma PERSISTENT(_nextblk)
44+
int _nextblk = 0; /*!< Address of the next EEPROM block after cursor block. The buffer is circular, so it can be < #_cursorblk. */
45+
46+
#pragma PERSISTENT(_enddemi)
47+
int _enddemi = 0; /*!< Largest possible value of _cursordemi. Always an odd integer. */
48+
49+
#pragma PERSISTENT(_cursordemi)
50+
int _cursordemi = 0; /*!< Cursor in terms of 8-byte demis. Must be >= 0 and <= #_enddemi. */
3951

4052
/*!
4153
* @brief Copy 4 demis from EEPROM into RAM.
@@ -107,6 +119,9 @@ void demi_init(const int startblk, const int lenblks)
107119
{
108120
int lendemis;
109121

122+
// --------- Enable FRAM writes -------------//
123+
fram_write_enable();
124+
110125
_startblk = startblk;
111126
_endblk = startblk+lenblks-1;
112127

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

120135
_cursordemi = 0;
136+
137+
// --------- Disable FRAM writes -------------//
138+
fram_write_disable();
121139
}
122140

123141
/*!
@@ -153,6 +171,9 @@ DemiState_t demi_movecursor(void)
153171
{
154172
DemiState_t demistate = ds_consecutive;
155173

174+
// --------- Enable FRAM writes -------------//
175+
fram_write_enable();
176+
156177
// Increment _cursordemi
157178
if (_cursordemi == _enddemi)
158179
{
@@ -175,6 +196,9 @@ DemiState_t demi_movecursor(void)
175196
_nextblk = _cursorblk + 1;
176197
}
177198

199+
// --------- Disable FRAM writes -------------//
200+
fram_write_disable();
201+
178202
return demistate;
179203
}
180204

wscodec/encoder/pyencoder/c_encoder/eep.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
#define BUFSIZE_BLKS 4
2929
#define BUFSIZE_BYTES (BUFSIZE_BLKS * BLKSIZE)
3030

31+
extern void fram_write_enable(void); /*!< Enable writes to FRAM. Should be defined in the processor-specific cuplTag project. */
32+
extern void fram_write_disable(void); /*!< Disable writes to FRAM. Should be defined in the processor-specific cuplTag project. */
33+
34+
#pragma PERSISTENT(_blkbuffer)
3135
char _blkbuffer[BUFSIZE_BLKS * BLKSIZE] = {0};
3236

3337
/*! \brief Checks if a byte index is within the bounds the buffer array.
@@ -47,16 +51,15 @@ static inline int inbounds(int byteindex)
4751
* \param eepblk Block of the EEPROM to write to.
4852
* \param bufblk Block of the buffer to write from.
4953
*
50-
* \returns 1 if the block to be written greater than the buffer size. Otherwise 0.
54+
* \returns 1 if the block to be written greater than the buffer size. 0 on success and -1 on write error.
5155
*/
5256
int eep_write(const int eepblk, const unsigned int bufblk)
5357
{
5458
int errflag = 1;
5559
int startbyte = bufblk * BLKSIZE;
5660
if (bufblk < BUFSIZE_BLKS)
5761
{
58-
nt3h_writetag(eepblk+1, &_blkbuffer[startbyte]);
59-
errflag = 0;
62+
errflag = nt3h_writetag(eepblk+1, &_blkbuffer[startbyte]);
6063
}
6164

6265
return errflag;
@@ -81,7 +84,9 @@ int eep_read(const int eepblk, const unsigned int bufblk)
8184
int startbyte = bufblk * BLKSIZE;
8285
if (bufblk < BUFSIZE_BLKS)
8386
{
87+
fram_write_enable();
8488
nt3h_readtag(eepblk+1, &_blkbuffer[startbyte]);
89+
fram_write_disable();
8590
errflag = 0;
8691
}
8792

@@ -102,7 +107,9 @@ int eep_swap(const unsigned int srcblk, const unsigned int destblk)
102107
{
103108
for (i=0; i<BUFSIZE_BLKS; i++)
104109
{
110+
fram_write_enable();
105111
_blkbuffer[destblk + i] = _blkbuffer[srcblk + i];
112+
fram_write_disable();
106113
}
107114
errflag = 0;
108115
}
@@ -129,7 +136,9 @@ int eep_cp(int * indexptr, const char * dataptr, const int lenbytes)
129136
{
130137
for (i=0; i<lenbytes; i++)
131138
{
132-
_blkbuffer[startbyte + i] = *(dataptr + i);
139+
fram_write_enable();
140+
_blkbuffer[startbyte + i] = *(dataptr + i);
141+
fram_write_disable();
133142
}
134143
*indexptr = endbyte + 1;
135144
errflag = 0;
@@ -151,7 +160,9 @@ int eep_cpbyte(int * indexptr, const char bytedata)
151160

152161
if (inbounds(*indexptr))
153162
{
163+
fram_write_enable();
154164
_blkbuffer[*indexptr] = bytedata;
165+
fram_write_disable();
155166
(*indexptr) = (*indexptr) + 1;
156167
errflag = 0;
157168
}

wscodec/encoder/pyencoder/c_encoder/nt3h.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131
#ifndef _NT3H_H_
3232
#define _NT3H_H_
3333

34-
void nt3h_init(void);
3534
int nt3h_writetag(int eepromBlock, char * blkdata);
3635
int nt3h_readtag(int eepromBlock, char * blkdata);
3736
int nt3h_eepromwritedone(void);
38-
void nt3h_check_address(void);
37+
int nt3h_check_address(void);
38+
void nt3h_update_cc(void);
3939
void nt3h_init_wrongaddress(void);
4040

4141
int printint(int myint);

wscodec/encoder/pyencoder/c_encoder/nvtype.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,7 @@
3333

3434
#ifndef NOT_CFFI
3535
nv_t nv = {.serial="AAAACCCC", .seckey="AAAACCCC"};
36+
37+
void fram_write_enable() {}
38+
void fram_write_disable() {}
3639
#endif

wscodec/encoder/pyencoder/c_encoder/pairhist.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,17 @@
3232
#define MD5BLKLEN_BYTES 64 /*!< Length of the MD5 input message block in bytes. */
3333

3434
extern nv_t nv;
35+
extern void fram_write_enable(void); /*!< Enable writes to FRAM. Should be defined in the processor-specific cuplTag project. */
36+
extern void fram_write_disable(void); /*!< Disable writes to FRAM. Should be defined in the processor-specific cuplTag project. */
37+
38+
#pragma PERSISTENT(pairhistory)
39+
pair_t pairhistory[BUFLEN_PAIRS] = {0}; /*!< Array of unencoded pairs. This mirrors the circular buffer of base64 encoded pairs stored in EEPROM. */
40+
41+
#pragma PERSISTENT(cursorindex)
42+
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. */
3543

3644
unsigned char msgblock[MD5BLKLEN_BYTES]; /*!< Block to hold message data as an input to the MD5 algorithm. */
3745
const int buflenpairs= BUFLEN_PAIRS; /*!< Length of the circular buffer in pairs. */
38-
static pair_t pairhistory[BUFLEN_PAIRS]; /*!< Array of unencoded pairs. This mirrors the circular buffer of base64 encoded pairs stored in EEPROM. */
39-
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. */
4046
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>.*/
4147
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>. */
4248
static MD5_CTX ctx; /*!< MD5 context. */
@@ -51,7 +57,19 @@ static MD5_CTX ctx; /*!< MD5 context. */
5157
*/
5258
void pairhist_ovr(pair_t pair)
5359
{
54-
pairhistory[cursorindex] = pair;
60+
fram_write_enable();
61+
pairhistory[cursorindex] = pair;
62+
fram_write_disable();
63+
}
64+
65+
/*!
66+
* @brief Initialise the cursorindex
67+
* The circular buffer itself (pairhistory) does not need to be initialised.
68+
*/
69+
void pairhist_init() {
70+
fram_write_enable();
71+
cursorindex = -1;
72+
fram_write_disable();
5573
}
5674

5775
/*!
@@ -62,6 +80,7 @@ void pairhist_ovr(pair_t pair)
6280
*/
6381
void pairhist_push(pair_t pair)
6482
{
83+
fram_write_enable();
6584
if (cursorindex == BUFLEN_PAIRS-1)
6685
{
6786
cursorindex = 0; // Write next pair to the start of the buffer.
@@ -72,6 +91,7 @@ void pairhist_push(pair_t pair)
7291
}
7392

7493
pairhistory[cursorindex] = pair;
94+
fram_write_disable();
7595
}
7696

7797
/*!

wscodec/encoder/pyencoder/c_encoder/pairhist.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ typedef struct
6969

7070
/// Have doxygen ignore this
7171
/// @cond
72+
void pairhist_init(void);
7273
void pairhist_ovr(pair_t pair);
7374
void pairhist_push(pair_t pair);
7475
hashn_t pairhist_hash(int npairs, int usehmac, unsigned int loopcount, unsigned int resetsalltime, unsigned int batv_resetcause, int cursorpos);

0 commit comments

Comments
 (0)