Skip to content

Commit 860984f

Browse files
authored
Merge pull request #2 from cj8scrambler/master
Add non-blocking AFE calibration method
2 parents 72a32d5 + 57b0895 commit 860984f

File tree

3 files changed

+81
-22
lines changed

3 files changed

+81
-22
lines changed

keywords.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ setLDO KEYWORD2
3333
setSampleRate KEYWORD2
3434
setChannel KEYWORD2
3535
calibrateAFE KEYWORD2
36+
beginCalibrateAFE KEYWORD2
37+
calAFEStatus KEYWORD2
38+
waitForCalibrateAFE KEYWORD2
39+
3640
reset KEYWORD2
3741
powerUp KEYWORD2
3842
powerDown KEYWORD2
@@ -124,3 +128,6 @@ NAU7802_PGA_PWR_PGA_CURR LITERAL1
124128
NAU7802_PGA_PWR_ADC_CURR LITERAL1
125129
NAU7802_PGA_PWR_MSTR_BIAS_CURR LITERAL1
126130
NAU7802_PGA_PWR_PGA_CAP_EN LITERAL1
131+
NAU7802_CAL_SUCCESS LITERAL1
132+
NAU7802_CAL_IN_PROGRESS LITERAL1
133+
NAU7802_CAL_FAILURE LITERAL1

src/SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.cpp

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ NAU7802::NAU7802()
2828
}
2929

3030
//Sets up the NAU7802 for basic function
31+
//If initialize is true (or not specified), default init and calibration is performed
32+
//If initialize is false, then it's up to the caller to initalize and calibrate
3133
//Returns true upon completion
32-
bool NAU7802::begin(TwoWire &wirePort)
34+
bool NAU7802::begin(TwoWire &wirePort, bool initialize)
3335
{
3436
//Get user's options
3537
_i2cPort = &wirePort;
@@ -44,21 +46,24 @@ bool NAU7802::begin(TwoWire &wirePort)
4446

4547
bool result = true; //Accumulate a result as we do the setup
4648

47-
result &= reset(); //Reset all registers
49+
if (initialize)
50+
{
51+
result &= reset(); //Reset all registers
4852

49-
result &= powerUp(); //Power on analog and digital sections of the scale
53+
result &= powerUp(); //Power on analog and digital sections of the scale
5054

51-
result &= setLDO(NAU7802_LDO_3V3); //Set LDO to 3.3V
55+
result &= setLDO(NAU7802_LDO_3V3); //Set LDO to 3.3V
5256

53-
result &= setGain(NAU7802_GAIN_128); //Set gain to 128
57+
result &= setGain(NAU7802_GAIN_128); //Set gain to 128
5458

55-
result &= setSampleRate(NAU7802_SPS_80); //Set samples per second to 10
59+
result &= setSampleRate(NAU7802_SPS_80); //Set samples per second to 10
5660

57-
result &= setRegister(NAU7802_ADC, 0x30); //Turn off CLK_CHP. From 9.1 power on sequencing.
61+
result &= setRegister(NAU7802_ADC, 0x30); //Turn off CLK_CHP. From 9.1 power on sequencing.
5862

59-
result &= setBit(NAU7802_PGA_PWR_PGA_CAP_EN, NAU7802_PGA_PWR); //Enable 330pF decoupling cap on chan 2. From 9.14 application circuit note.
63+
result &= setBit(NAU7802_PGA_PWR_PGA_CAP_EN, NAU7802_PGA_PWR); //Enable 330pF decoupling cap on chan 2. From 9.14 application circuit note.
6064

61-
result &= calibrateAFE(); //Re-cal analog front end when we change gain, sample rate, or channel
65+
result &= calibrateAFE(); //Re-cal analog front end when we change gain, sample rate, or channel
66+
}
6267

6368
return (result);
6469
}
@@ -80,25 +85,60 @@ bool NAU7802::available()
8085
}
8186

8287
//Calibrate analog front end of system. Returns true if CAL_ERR bit is 0 (no error)
83-
//Takes approximately 344ms to calibrate
88+
//Takes approximately 344ms to calibrate; wait up to 1000ms.
8489
//It is recommended that the AFE be re-calibrated any time the gain, SPS, or channel number is changed.
8590
bool NAU7802::calibrateAFE()
8691
{
87-
setBit(NAU7802_CTRL2_CALS, NAU7802_CTRL2); //Begin calibration
92+
beginCalibrateAFE();
93+
return waitForCalibrateAFE(1000);
94+
}
8895

89-
uint16_t counter = 0;
90-
while (1)
96+
//Begin asynchronous calibration of the analog front end.
97+
// Poll for completion with calAFEStatus() or wait with waitForCalibrateAFE()
98+
void NAU7802::beginCalibrateAFE()
99+
{
100+
setBit(NAU7802_CTRL2_CALS, NAU7802_CTRL2);
101+
}
102+
103+
//Check calibration status.
104+
NAU7802_Cal_Status NAU7802::calAFEStatus()
105+
{
106+
if (getBit(NAU7802_CTRL2_CALS, NAU7802_CTRL2))
107+
{
108+
return NAU7802_CAL_IN_PROGRESS;
109+
}
110+
111+
if (getBit(NAU7802_CTRL2_CAL_ERROR, NAU7802_CTRL2))
91112
{
92-
if (getBit(NAU7802_CTRL2_CALS, NAU7802_CTRL2) == false)
93-
break; //Goes to 0 once cal is complete
113+
return NAU7802_CAL_FAILURE;
114+
}
115+
116+
// Calibration passed
117+
return NAU7802_CAL_SUCCESS;
118+
}
119+
120+
//Wait for asynchronous AFE calibration to complete with optional timeout.
121+
//If timeout is not specified (or set to 0), then wait indefinitely.
122+
//Returns true if calibration completes succsfully, otherwise returns false.
123+
bool NAU7802::waitForCalibrateAFE(uint32_t timeout_ms)
124+
{
125+
uint32_t begin = millis();
126+
NAU7802_Cal_Status cal_ready;
127+
128+
while ((cal_ready = calAFEStatus()) == NAU7802_CAL_IN_PROGRESS)
129+
{
130+
if ((timeout_ms > 0) && ((millis() - begin) > timeout_ms))
131+
{
132+
break;
133+
}
94134
delay(1);
95-
if (counter++ > 1000)
96-
return (false);
97135
}
98136

99-
if (getBit(NAU7802_CTRL2_CAL_ERROR, NAU7802_CTRL2) == false)
100-
return (true); //No error! Cal is good.
101-
return (false); //Cal error
137+
if (cal_ready == NAU7802_CAL_SUCCESS)
138+
{
139+
return (true);
140+
}
141+
return (false);
102142
}
103143

104144
//Set the readings per second

src/SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,19 @@ typedef enum
153153
NAU7802_CHANNEL_2 = 1,
154154
} NAU7802_Channels;
155155

156+
//Calibration state
157+
typedef enum
158+
{
159+
NAU7802_CAL_SUCCESS = 0,
160+
NAU7802_CAL_IN_PROGRESS = 1,
161+
NAU7802_CAL_FAILURE = 2,
162+
} NAU7802_Cal_Status;
163+
156164
class NAU7802
157165
{
158166
public:
159167
NAU7802(); //Default constructor
160-
bool begin(TwoWire &wirePort = Wire); //Check communication and initialize sensor
168+
bool begin(TwoWire &wirePort = Wire, bool reset = true); //Check communication and initialize sensor
161169
bool isConnected(); //Returns true if device acks at the I2C address
162170

163171
bool available(); //Returns true if Cycle Ready bit is set (conversion is complete)
@@ -179,7 +187,11 @@ class NAU7802
179187
bool setSampleRate(uint8_t rate); //Set the readings per second. 10, 20, 40, 80, and 320 samples per second is available
180188
bool setChannel(uint8_t channelNumber); //Select between 1 and 2
181189

182-
bool calibrateAFE(); //Calibrate the analog from end of the NAU7802. Returns true if CAL_ERR bit is 0 (no error)
190+
bool calibrateAFE(); //Synchronous calibration of the analog front end of the NAU7802. Returns true if CAL_ERR bit is 0 (no error)
191+
void beginCalibrateAFE(); //Begin asynchronous calibration of the analog front end of the NAU7802. Poll for completion with calAFEStatus() or wait with waitForCalibrateAFE().
192+
bool waitForCalibrateAFE(uint32_t timeout_ms = 0); //Wait for asynchronous AFE calibration to complete with optional timeout.
193+
NAU7802_Cal_Status calAFEStatus(); //Check calibration status.
194+
183195
bool reset(); //Resets all registers to Power Of Defaults
184196

185197
bool powerUp(); //Power up digital and analog sections of scale, ~2mA

0 commit comments

Comments
 (0)