Skip to content

Commit

Permalink
Update wiring_analog.c (#190)
Browse files Browse the repository at this point in the history
- Add analogSet() function
- Change from int to float for gain, offset, ret and mv
- Modify analogRead function
  • Loading branch information
pammyleong authored Jul 27, 2023
1 parent a2d8022 commit 56c6f3b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
70 changes: 38 additions & 32 deletions Arduino_package/hardware/cores/ambd/wiring_analog.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ static int _readResolution = 10;
extern void *gpio_pin_struct[];
static int _writeResolution = 8;
static int _writePeriod = 1000;
static uint16_t _offset = 0;
static uint16_t _gain = 0;
static float _offset = 0;
static float _gain = 0;
static int _calibrate_en = 0;

void analogReadResolution(int res) {
if (res > 12) {
Expand Down Expand Up @@ -88,42 +89,47 @@ void analogReference(eAnalogReference ulMode) {
analog_reference = ulMode;
}

uint32_t analogRead(uint32_t ulPin) {
uint16_t ret = 0;
// float voltage;
uint32_t mv;
void analogSet(float gain, float offset){
_offset = offset;
_gain = gain;
_calibrate_en = 1;
}

uint32_t analogRead(uint32_t ulPin) {
//float voltage;
float ret = 0;
float mv;
if ((g_APinDescription[ulPin].ulPinType & TYPE_ANALOG) != TYPE_ANALOG) {
printf("%s : ulPin %d wrong\n", __FUNCTION__, ((int)ulPin));
return 0;
}
if (_calibrate_en == 0) {
if ((_offset == 0) || (_gain == 0)) {
u8 EfuseBuf[2];
u32 index;
u32 addressOffset = 0x1D0;
u32 addressGain = 0x1D2;

// Read pre-calibrated values from EFUSE
for (index = 0; index < 2; index++) {
EFUSE_PMAP_READ8(0, (addressOffset + index), (EfuseBuf + index), L25EOUTVOLTAGE);
}
_offset = EfuseBuf[1]<<8|EfuseBuf[0];

if ((_offset == 0) || (_gain == 0)) {
u8 EfuseBuf[2];
u32 index;
u32 addressOffset = 0x1D0;
u32 addressGain = 0x1D2;

// Read pre-calibrated values from EFUSE
for (index = 0; index < 2; index++) {
EFUSE_PMAP_READ8(0, (addressOffset + index), (EfuseBuf + index), L25EOUTVOLTAGE);
}
_offset = EfuseBuf[1]<<8|EfuseBuf[0];

for (index = 0; index < 2; index++) {
EFUSE_PMAP_READ8(0, (addressGain + index), (EfuseBuf + index), L25EOUTVOLTAGE);
}
_gain = EfuseBuf[1] << 8 | EfuseBuf[0];
for (index = 0; index < 2; index++) {
EFUSE_PMAP_READ8(0, (addressGain + index), (EfuseBuf + index), L25EOUTVOLTAGE);
}
_gain = EfuseBuf[1] << 8 | EfuseBuf[0];

// Use default values if invalid values obtained from EFUSE
if (_offset == 0xFFFF) {
_offset = 0x9B0;
}
if (_gain == 0xFFFF) {
_gain = 0x2F12;
// Use default values if invalid values obtained from EFUSE
if (_offset == 0xFFFF) {
_offset = 0x9B0;
}
if (_gain == 0xFFFF) {
_gain = 0x2F12;
}
}
}

pinRemoveMode(ulPin);

switch (g_APinDescription[ulPin].pinname) {
Expand Down Expand Up @@ -159,14 +165,14 @@ uint32_t analogRead(uint32_t ulPin) {
printf("%s : ulPin %d wrong\n", __FUNCTION__, ((int)ulPin));
return 0;
}

if (ret < 0xfa) {
mv = 0; // Ignore persistent low voltage measurement error
} else {
mv = ((10 * ret - _offset) * 1000 / _gain); // Convert measured ADC value to millivolts
}
}

ret = (mv/3300.0) * (1 << _readResolution); // Return user required resolution

return ret;
}

Expand Down
2 changes: 2 additions & 0 deletions Arduino_package/hardware/cores/ambd/wiring_analog.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ extern void noTone(uint32_t ulPin);

extern void analogWritePeriod(int us);

extern void analogSet(float gain, float offset);

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit 56c6f3b

Please sign in to comment.