i really need your help with the implementation of the AFE4300 chip.
Currently I only got the two values 0 or 65536, I suspected the error was in the SPI communication but I am not sure. The following code was ported from the Arduino community.
Datesheet: http://www.ti.com/lit/ds/symlink/afe4300.pdf
The first question is: do I have to set the CS pin independently?
Code: Select all
#include "AFE4300.h"
#include "SPIbus.h"
#include <cstring>
#include "sdkconfig.h"
const unsigned char ADC_DATA_RESULT = 0x00;
const unsigned char ADC_CONTROL_REGISTER = 0x01;
const unsigned char MISC1_REGISTER = 0x02;
const unsigned char MISC2_REGISTER = 0x03;
const unsigned char DEVICE_CONTROL_1 = 0x09;
const unsigned char ISW_MATRIX = 0x0A;
const unsigned char VSW_MATRIX = 0x0B;
const unsigned char IQ_MODE_ENABLE = 0x0C;
const unsigned char WEIGHT_SCALE_CONTROL = 0x0D;
const unsigned char BCM_DAC_FREQ = 0x0E;
const unsigned char DEVICE_CONTROL_2 = 0x0F;
const unsigned char ADC_CONTROL_REGISTER_2 = 0x10;
const unsigned char MISC3_REGISTER = 0x1A;
//spi_device_handle_t spi;
//spi_bus_config_t buscfg;
SPI_t &mySPI = vspi; // vspi and hspi are the default objects
spi_device_handle_t device = nullptr;
#define SPI_MODE 1
#define SPI_CLOCK 1000000 // 1 MHz
AFE4300::AFE4300(int reset, int rdy, int mosi, int miso, int sclk, int cs)
: PIN_RESET(gpio_num_t(reset))
, PIN_DRDY(gpio_num_t(rdy))
, PIN_MOSI(gpio_num_t(mosi))
, PIN_MISO(gpio_num_t(miso))
, PIN_SCLK(gpio_num_t(sclk))
, PIN_CS(gpio_num_t(cs))
, gain_(1)
, weightscale_(1)
, weightoffset(1)
{
//gpio_set_direction(PIN_CS, GPIO_MODE_OUTPUT);
gpio_set_direction(PIN_RESET, GPIO_MODE_OUTPUT);
gpio_set_direction(PIN_DRDY, GPIO_MODE_INPUT);
ESP_ERROR_CHECK( mySPI.begin(PIN_MOSI, PIN_MISO, PIN_SCLK));
ESP_ERROR_CHECK( mySPI.addDevice(SPI_MODE, SPI_CLOCK, PIN_CS, &device));
}
void AFE4300::reset()
{
gpio_set_level(PIN_RESET, 0);
delayMicroseconds(100000);
gpio_set_level(PIN_RESET, 1);
}
void AFE4300::init()
{
writeRegister(ADC_CONTROL_REGISTER, 0x4140);
writeRegister(MISC1_REGISTER, 0x0000);
writeRegister(MISC2_REGISTER, 0xFFFF);
writeRegister(DEVICE_CONTROL_1, 0x0004);
writeRegister(ISW_MATRIX, 0x0000);
writeRegister(VSW_MATRIX, 0x0000);
writeRegister(IQ_MODE_ENABLE, 0x0000);
writeRegister(WEIGHT_SCALE_CONTROL, 0x0000);
writeRegister(BCM_DAC_FREQ, 0x0040);
writeRegister(DEVICE_CONTROL_2, 0x0000);
writeRegister(ADC_CONTROL_REGISTER_2, 0x0011);
writeRegister(MISC3_REGISTER, 0x00C0);
}
void AFE4300::initWeightScale()
{
writeRegister(ADC_CONTROL_REGISTER, 0x4140); //Differential measurement mode, 32 SPS
writeRegister(DEVICE_CONTROL_1, 0x0005); //Power up weigh scale signal chain
writeRegister(ADC_CONTROL_REGISTER_2, 0x0000); //ADC selects output of weigh scale
writeRegister(WEIGHT_SCALE_CONTROL, 0x003F); //Gain = 1 DAC Offset = -1
writeRegister(BCM_DAC_FREQ, 0x0040); //Frequency = default
writeRegister(IQ_MODE_ENABLE, 0x0000); //Disable IQ mode
writeRegister(ISW_MATRIX, 0x0000); //Channels IOUTP1 and IOUTN0
writeRegister(VSW_MATRIX, 0x0000); //Channels VSENSEP1 and VSENSEN0
}
/**
* @brief Initializes the BCM Module
*/
void AFE4300::initBCM()
{
writeRegister(ADC_CONTROL_REGISTER, 0x4120); //Differential measurement mode, 32 SPS
writeRegister(DEVICE_CONTROL_1, 0x0006); //Power up BCM signal chain
writeRegister(ISW_MATRIX, 0x0804); //Channels IOUTP1 and IOUTN0
writeRegister(VSW_MATRIX, 0x0804); //Channels VSENSEP1 and VSENSEN0
writeRegister(ADC_CONTROL_REGISTER_2, 0x0063); //ADC selects output of BCM-I output
writeRegister(WEIGHT_SCALE_CONTROL, 0x0000); //Gain = 1 DAC Offset = 0
}
int AFE4300::read()
{
return readRegister(ADC_DATA_RESULT);
}
void AFE4300::writeRegister(unsigned char address, unsigned int data)
{
unsigned char firstByte = (unsigned char)(data >> 8);
unsigned char secondByte = (unsigned char)data;
uint8_t buffer[2] = {0};
buffer[0] = firstByte;
buffer[1] = secondByte;
ESP_ERROR_CHECK(mySPI.writeBytes(device, address, 2, buffer));
}
int AFE4300::readRegister(unsigned char address)
{
int spiReceive = 0;
address = address & 0x1F;
address = address | 0x20;
uint8_t buffer[2] = {0};
ESP_ERROR_CHECK(mySPI.readBytes(device, address, 2, buffer));
unsigned char spiReceiveFirst = 0;
unsigned char spiReceiveSecond = 0;
spiReceiveFirst = buffer[0];
spiReceiveSecond = buffer[1];
spiReceive = (spiReceiveFirst << 8);
spiReceive |= spiReceiveSecond;
return spiReceive;
}