Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristophKarlHeck committed Dec 13, 2024
1 parent 8e9ca64 commit 3e01bc9
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 124 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ add_executable(PhytoNode ${SOURCES})
# Enable logging with the desired log level
target_compile_definitions(PhytoNode PRIVATE
ENABLE_LOGGING # Enable logging system
LOG_LEVEL_TRACE # Set logging level to INFO
LOG_LEVEL_NOLOG # Set logging level to INFO
)

target_link_libraries(PhytoNode PUBLIC
Expand Down
230 changes: 112 additions & 118 deletions src/adc/AD7124.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,125 +6,15 @@
#include "interfaces/ReadingQueue.h"


/**
* @brief Constructs an AD7124 object and initializes SPI communication.
* @param spi_frequency The SPI clock frequency in Hz.
*/
AD7124::AD7124(int spi_frequency):
m_spi(PA_7, PA_6, PA_5), m_drdy(PA_6), m_cs(PA_4), m_sync(PA_1),
m_spi_frequency(spi_frequency), m_flag_0(false), m_flag_1(false),
m_read(1), m_write(0){

INFO("AD7124::AD7124(int spi_frequency)");

m_spi.format(8, 3);
m_spi.frequency(m_spi_frequency);

init(true,true);
}

/**
* @brief Gets the singleton instance of the AD7124 class.
* @param spi_frequency The SPI clock frequency in Hz.
* @return Reference to the singleton instance of the AD7124 class.
*/
AD7124& AD7124::getInstance(int spi_frequency) {
static AD7124 instance(spi_frequency);
return instance;
}

void AD7124::init(bool f0, bool f1){

m_flag_0 = f0;
m_flag_1 = f1;
m_sync = 1;
m_cs=0;

AD7124::reset();
AD7124::status();

AD7124::channel_reg(m_read); //activate 2 channels
AD7124::channel_reg(m_write); //activate 2 channels
AD7124::channel_reg(m_read); //activate 2 channels

//flags for if you want to have channel 0, or 1, or both active
if(m_flag_0 == true){
//config reg 0
AD7124::config_reg(AD7124_CFG0_REG, m_read); // read configuration register
AD7124::config_reg(AD7124_CFG0_REG, m_write); // write configuration register
AD7124::config_reg(AD7124_CFG0_REG, m_read); // proof writing by reading again
//filter reg 0
AD7124::filter_reg(AD7124_FILT0_REG, m_read); // same with filter register
AD7124::filter_reg(AD7124_FILT0_REG, m_write);
AD7124::filter_reg(AD7124_FILT0_REG, m_read);
}

if(m_flag_1 == true){
//config reg 1
//AD7124::config_reg(AD7124_CFG1_REG, read);
AD7124::config_reg(AD7124_CFG1_REG, m_write);
AD7124::config_reg(AD7124_CFG1_REG, m_read);
//filter reg 1
//AD7124::filter_reg(AD7124_FILT1_REG, read);
AD7124::filter_reg(AD7124_FILT1_REG, m_write);
AD7124::filter_reg(AD7124_FILT1_REG, m_read);
}
//xAD7124::calibrate(1,1,0,0);

AD7124::ctrl_reg(m_read); // same with control register
AD7124::ctrl_reg(m_write);
AD7124::ctrl_reg(m_read);
//AD7124::calibrate(1,0,0,0);
}

void AD7124::reset(){
/* reset the ADC */
INFO("Reset ADC\n");
for (int i = 0; i<=8; i++){
m_spi.write(0xFF);
}
}

char AD7124::status(){
/* read the status register */
m_spi.write(AD7124_R | AD7124_STATUS_REG);
char status = m_spi.write(0x00);
TRACE("ADC status = 0x%X, %s\n", status, byte_to_binary(status).c_str());
return status;
}


void AD7124::config_reg(uint8_t address ,char RW){
/* read/ write the configuration register */

if(RW == m_read){
m_spi.write(AD7124_R | address);
TRACE("ADC conf = ");
for (int i = 0; i<=1; i++){
TRACE("%s", byte_to_binary(m_spi.write(0x00)).c_str());
}
TRACE("\n");
}
else{
m_spi.write(address);
char my_config[]={(AD7124_CFG_REG_BIPOLAR ) >>8 ,
AD7124_CFG_REG_AIN_BUFP | AD7124_CFG_REG_AINN_BUFM | AD7124_CFG_REG_REF_SEL(2) |AD7124_CFG_REG_PGA(2)};
//original 0x08, 0x71
for (int i = 0; i<=1; i++){
m_spi.write(my_config[i]);
}
}
}


void AD7124::ctrl_reg(char RW){
/* read/write the control register */

if(RW == m_read){
m_spi.write(AD7124_R | AD7124_ADC_CTRL_REG);
TRACE("ADC contr_reg =");
for (int i = 0; i<=1; i++){
TRACE("%s", byte_to_binary(m_spi.write(0x00)).c_str());
int byte = m_spi.write(0x00);
TRACE("%s", byte_to_binary(byte).c_str());
}
TRACE("\n");
} else {
Expand All @@ -146,12 +36,12 @@ void AD7124::ctrl_reg(char RW){
*/
void AD7124::channel_reg(char RW){
//RW=1 -> read else write

if(RW == m_read){
m_spi.write(AD7124_R | AD7124_CH1_MAP_REG);
TRACE("Channel register =");
for (int i = 0; i<=1; i++){
TRACE("%s", byte_to_binary(m_spi.write(0x00)).c_str());
int byte = m_spi.write(0x00);
TRACE("%s", byte_to_binary(byte).c_str());
}
TRACE("\n");

Expand Down Expand Up @@ -185,16 +75,14 @@ void AD7124::channel_reg(char RW){
}
}



void AD7124::filter_reg(uint8_t filt, char RW){
//default FS: 384, Post filter = 011

if(RW == m_read){
m_spi.write(AD7124_R | filt);
TRACE("Filter register =");
for (int i = 0; i<=2; i++){
TRACE("%s", byte_to_binary(m_spi.write(0x00)).c_str());
int byte = m_spi.write(0x00);
TRACE("%s", byte_to_binary(byte).c_str());
}
TRACE("\n");
}
Expand All @@ -208,6 +96,112 @@ void AD7124::filter_reg(uint8_t filt, char RW){
}
}
}
void AD7124::config_reg(uint8_t address ,char RW){
/* read/ write the configuration register */
if(RW == m_read){
m_spi.write(AD7124_R | address);
TRACE("ADC conf = ");
for (int i = 0; i<=1; i++){
int byte = m_spi.write(0x00);
TRACE("%s", byte_to_binary(byte).c_str());
}
TRACE("\n");
}
else{
m_spi.write(address);
char my_config[]={(AD7124_CFG_REG_BIPOLAR ) >>8 ,
AD7124_CFG_REG_AIN_BUFP | AD7124_CFG_REG_AINN_BUFM | AD7124_CFG_REG_REF_SEL(2) |AD7124_CFG_REG_PGA(2)};
//original 0x08, 0x71
for (int i = 0; i<=1; i++){
m_spi.write(my_config[i]);
}
}
}

void AD7124::reset(){
/* reset the ADC */
//INFO("Reset ADC\n");
for (int i = 0; i< 8; i++){
m_spi.write(0xFF);
}
}

char AD7124::status(){
/* read the status register */
m_spi.write(AD7124_R | AD7124_STATUS_REG);
char status = m_spi.write(0x00);
TRACE("ADC status = 0x%X, %s\n", status, byte_to_binary(status).c_str());
return status;
}

void AD7124::init(bool f0, bool f1){
m_flag_0 = f0;
m_flag_1 = f1;
m_sync = 1;
m_cs=0;

reset();
status();

channel_reg(m_read); //activate 2 channels
channel_reg(m_write); //activate 2 channels
channel_reg(m_read); //activate 2 channels

//flags for if you want to have channel 0, or 1, or both active
if(m_flag_0 == true){
//config reg 0
config_reg(AD7124_CFG0_REG, m_read); // read configuration register
config_reg(AD7124_CFG0_REG, m_write); // write configuration register
config_reg(AD7124_CFG0_REG, m_read); // proof writing by reading again
//filter reg 0
filter_reg(AD7124_FILT0_REG, m_read); // same with filter register
filter_reg(AD7124_FILT0_REG, m_write);
filter_reg(AD7124_FILT0_REG, m_read);
}

if(m_flag_1 == true){
//config reg 1
//AD7124::config_reg(AD7124_CFG1_REG, read);
config_reg(AD7124_CFG1_REG, m_write);
config_reg(AD7124_CFG1_REG, m_read);
//filter reg 1
//AD7124::filter_reg(AD7124_FILT1_REG, read);
filter_reg(AD7124_FILT1_REG, m_write);
filter_reg(AD7124_FILT1_REG, m_read);
}
//xAD7124::calibrate(1,1,0,0);

ctrl_reg(m_read); // same with control register
ctrl_reg(m_write);
ctrl_reg(m_read);
//AD7124::calibrate(1,0,0,0);
}

/**
* @brief Constructs an AD7124 object and initializes SPI communication.
* @param spi_frequency The SPI clock frequency in Hz.
*/
AD7124::AD7124(int spi_frequency):
m_spi(PA_7, PA_6, PA_5), m_drdy(PA_6), m_cs(PA_4), m_sync(PA_1),
m_spi_frequency(spi_frequency), m_flag_0(false), m_flag_1(false),
m_read(1), m_write(0){

m_spi.format(8, 3);
m_spi.frequency(m_spi_frequency);

init(true,true);
}

/**
* @brief Gets the singleton instance of the AD7124 class.
* @param spi_frequency The SPI clock frequency in Hz.
* @return Reference to the singleton instance of the AD7124 class.
*/
AD7124& AD7124::getInstance(int spi_frequency) {
static AD7124 instance(spi_frequency);
return instance;
}


/**
* @brief Sends ADC data to the main thread for further processing.
Expand Down
11 changes: 6 additions & 5 deletions src/serial_mail_sender/SerialMailSender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* - RX -> GPIO 14 (TX)
* - GND -> GND
*/
BufferedSerial SerialMailSender::m_serial_port(PC_1/*USBTX*/,PC_0/*USBRX*/, BAUDRATE);
BufferedSerial SerialMailSender::m_serial_port(/*PC_1*/USBTX,/*PC_0*/USBRX, BAUDRATE);

/**
* @brief Access the singleton instance of SerialMailSender.
Expand Down Expand Up @@ -91,16 +91,17 @@ void SerialMailSender::sendMail(
uint16_t sync_marker = 0xAAAA;
m_serial_port.write(reinterpret_cast<const char*>(&sync_marker), sizeof(sync_marker));


// Send the size (4 bytes)
m_serial_port.write(reinterpret_cast<const char*>(&size), sizeof(size));

// Send the FlatBuffers buffer
m_serial_port.write(reinterpret_cast<const char*>(buf), size);

// Log message details
auto now = Kernel::Clock::now().time_since_epoch();
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(now).count();
auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(now).count() % 1000;
// auto now = Kernel::Clock::now().time_since_epoch();
// auto seconds = std::chrono::duration_cast<std::chrono::seconds>(now).count();
// auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(now).count() % 1000;

printf("Serial Mail(size:%lu) sent at: %lld seconds and %lld milliseconds\n", size, seconds, milliseconds);
// printf("Serial Mail(size:%lu) sent at: %lld seconds and %lld milliseconds\n", size, seconds, milliseconds);
}

0 comments on commit 3e01bc9

Please sign in to comment.