diff --git a/NAU7802/NAU7802.cpp b/NAU7802/NAU7802.cpp new file mode 100644 index 0000000..6771907 --- /dev/null +++ b/NAU7802/NAU7802.cpp @@ -0,0 +1,167 @@ +/** + ******************************************************************************** + * @file nau7802.cpp + * @author Javier + * @date 2025-11-25 + * @brief Implementation of the NAU7802 driver. + ******************************************************************************** + */ + +#include "NAU7802.hpp" +#include "main.h" +#include + +NAU7802::NAU7802(I2C_Wrapper* i2c_pointer, std::uint8_t address) + : _i2c(i2c_pointer), _deviceAddress(address) {} + +// Begin NAU7802 Startup Sequence +NauStatus NAU7802::begin(uint8_t initialGain) { + + // 1. Send a reset command + if (reset() != NauStatus::OK) { + return NauStatus::ERR_I2C; // Failed to reset + } + HAL_Delay(10); + + // 2. Power up the analog and digital sections + uint8_t pu_ctrl = NAU7802_PU_CTRL_PUA | NAU7802_PU_CTRL_PUD; + writeRegister(NAU7802_REG_PU_CTRL, pu_ctrl); + + // 3. Wait for the Power Up Ready bit + uint8_t attempts = 100; + bool powerReady = false; + while (attempts > 0) { + if (readRegister(NAU7802_REG_PU_CTRL, &pu_ctrl) == NauStatus::OK) { + if (pu_ctrl & NAU7802_PU_CTRL_PUR) { + powerReady = true; + break; + } + } + HAL_Delay(1); + attempts--; + } + return setGain(initialGain); +} + +bool NAU7802::isReady() { + uint8_t status; + if (readRegister(NAU7802_REG_PU_CTRL, &status) == NauStatus::OK) { + return (status & NAU7802_PU_CTRL_CR) != 0; + } + return false; +} + +NauStatus NAU7802::readSensor(NAU7802_OUT *dest) { + uint8_t buffer[3]; + + if (readRegisters(NAU7802_REG_ADC_B2, buffer, 3 ) != NauStatus::OK) { + dest -> raw_reading = 0; + return NauStatus::ERR_I2C; // Read failed + } + + // Combine the three bytes + int32_t value = ((int32_t)buffer[0] << 16) | \ + ((int32_t)buffer[1] << 8) | \ + (buffer[2]); + + // Sign-extend the 24-bit value to a 32-bit integer + if (value & 0x00800000) { + value |= 0xFF000000; + } + + dest->raw_reading = value; + return NauStatus::OK; +} + +NauStatus NAU7802::reset() { + // RR bit + modifyRegisterBit(NAU7802_REG_PU_CTRL, NAU7802_PU_CTRL_RR, true); + + HAL_Delay(1); // Small delay to ensure reset is processed + + // Clear RR bit + return modifyRegisterBit(NAU7802_REG_PU_CTRL, NAU7802_PU_CTRL_RR, false); +} + +NauStatus NAU7802::setGain(uint8_t gain) { + if (gain > NAU7802_GAIN_128X) { + return NauStatus::ERR_INVALID_ARG; // Invalid gain setting + } + + // Read current CTRL1 register state + uint8_t ctrl1; + if (readRegister(NAU7802_REG_CTRL1, &ctrl1) != NauStatus::OK) { + return NauStatus::ERR_I2C; // Failed to read CTRL1 register + } + + // Modify gain bits + ctrl1 &= 0b11111000; // Clear existing gain + ctrl1 |= gain; // Set new gain + + // Write back modified CTRL1 register + if (writeRegister(NAU7802_REG_CTRL1, ctrl1) != NauStatus::OK) { + return NauStatus::ERR_I2C; // Failed to write CTRL1 register + } + + // Verify the gain setting + uint8_t verifyCtrl1; + if (readRegister(NAU7802_REG_CTRL1, &verifyCtrl1) != NauStatus::OK) { + return NauStatus::ERR_I2C; // Failed to read back CTRL1 register + } + + // Check if gain bits match + if ((verifyCtrl1 & 0b00000111) == gain) { + return NauStatus::OK; // Success + } + else { + return NauStatus::ERR_I2C; // Verification failed + } +} + +/* -- Private Helpers -- */ +NauStatus NAU7802::writeRegister(std::uint8_t reg, std::uint8_t value) { + if (_i2c->writeByte(_deviceAddress, reg, value)) { + return NauStatus::OK; + } + return NauStatus::ERR_I2C; +} + +NauStatus NAU7802::readRegister(std::uint8_t reg, std::uint8_t* value) { + if (_i2c->readByte(_deviceAddress, reg, value)) { + return NauStatus::OK; + } + return NauStatus::ERR_I2C; +} + +NauStatus NAU7802::readRegisters(std::uint8_t reg, std::uint8_t* buffer, std::uint8_t len) { + if (_i2c->readBytes(_deviceAddress, reg, buffer, len)) { + return NauStatus::OK; + } + return NauStatus::ERR_I2C; +} + +NauStatus NAU7802::modifyRegisterBit(std::uint8_t reg, std::uint8_t bitMask, bool state) { + uint8_t val; + + // 1. Check Read Status + NauStatus status = readRegister(reg, &val); + if (status != NauStatus::OK) { + return status; // Propagate error + } + + // 2. Modify + if (state) { + val |= bitMask; + } else { + val &= ~bitMask; + } + + // 3. Return Write Status + return writeRegister(reg, val); +} + +NauStatus NAU7802::calibrate() { + // TODO: Implement calibration logic based on datasheet + // Usually involves setting CALS bit in REG0x02 + return modifyRegisterBit(NAU7802_REG_CTRL2, NAU7802_CTRL2_CALS, true); +} \ No newline at end of file diff --git a/NAU7802/NAU7802.hpp b/NAU7802/NAU7802.hpp new file mode 100644 index 0000000..68445a4 --- /dev/null +++ b/NAU7802/NAU7802.hpp @@ -0,0 +1,101 @@ + /** + ******************************************************************************** + * @file NAU7802.hpp + * @author Javier + * @date 2025-11-25 + * @brief Header for the NAU7802 24-bit ADC Driver. + ******************************************************************************** + */ + +#ifndef NAU7802_HPP +#define NAU7802_HPP + +/************************************ + * INCLUDES + ************************************/ +#include +#include "i2c_wrapper.hpp" +#include "NAU7802_regs.hpp" + +/** + * @brief Output data structure for the NAU7802. + */ +typedef struct NAU7802_OUTPUT { + std::int32_t raw_reading; +} NAU7802_OUT; + +/************************************ + * CLASS DEFINITIONS + ************************************/ +// New Status Enum +enum class NauStatus : uint8_t { + OK = 0, + ERR_I2C, + ERR_TIMEOUT, + ERR_NOT_READY, + ERR_INVALID_ARG +}; + + +class NAU7802 { +public: + /** + * @brief Constructs the NAU7802 Driver. + * @param configs Configuration settings for the sensor. + * @param i2c_pointer I2C Wrapper for communication. + */ + NAU7802(I2C_Wrapper* i2c_pointer, std::uint8_t address = NAU7802_I2C_ADDRESS); + + + + /** + * @brief Initializes the sensor, resets and waits for power up ready. + * @return true if sensor detected and powered up, false otherwise. + */ + NauStatus begin(uint8_t initialGain = NAU7802_GAIN_1X); + + /** + * @brief Checks the Cycle Ready (CR) bit. + * @return true if new data is available. + */ + bool isReady(); + + /** + * @brief Reads the 24-bit signed ADC value. + * @param dest Pointer to the output struct. + * @return true if read was successful. + */ + NauStatus readSensor(NAU7802_OUT *dest); + + /** + * @brief Performs a software reset of the device. + */ + NauStatus reset(); + + /** + * @brief Sets the Programmable Gain Amplifier (PGA) setting. + * @param gain One of NAU7802_GAIN_xxx constants. + * @return true if write successful. + */ + NauStatus setGain(std::uint8_t gain); + + /** + * @brief Calibrates the internal offsets (Optional usage). + */ + NauStatus calibrate(); + +private: + + // Private Helpers + NauStatus writeRegister(std::uint8_t reg, std::uint8_t value); + NauStatus readRegister(std::uint8_t reg, std::uint8_t* value); + NauStatus readRegisters(std::uint8_t reg, std::uint8_t* buffer, std::uint8_t len); + NauStatus modifyRegisterBit(std::uint8_t reg, std::uint8_t bitMask, bool state); + + // Member Variables + I2C_Wrapper* _i2c; + std::uint8_t _deviceAddress; + +}; + +#endif /* NAU7802_HPP */ \ No newline at end of file diff --git a/NAU7802/NAU7802Task.cpp b/NAU7802/NAU7802Task.cpp new file mode 100644 index 0000000..abe9602 --- /dev/null +++ b/NAU7802/NAU7802Task.cpp @@ -0,0 +1,136 @@ + /** + ******************************************************************************** + * @file NAU7802Task.cpp + * @author Javier + * @date 2026-01-10 + * @brief Implementation of the NAU7802 task handling. + ******************************************************************************** + */ + + /************************************ + * INCLUDES + ************************************/ +#include "NAU7802Task.hpp" +#include "main.h" + +// FreeRTOS includes +#include "FreeRTOS.h" +#include "task.h" + + /************************************ + * MACROS AND DEFINES + ************************************/ +/* temp definitions if not SystemDefines.hpp*/ +#ifndef NAU_TASK_QUEUE_DEPTH +#define NAU_TASK_QUEUE_DEPTH 5 +#endif + +#ifndef NAU_TASK_STACK_DEPTH +#define NAU_TASK_STACK_DEPTH 256 // Adjust.. +#endif + +#ifndef NAU_TASK_PRIORITY +#define NAU_TASK_PRIORITY 3 +#endif + + /************************************ + * FUNCTION DEFINITIONS + ************************************/ + + // Constructor + NAU7802Task::NAU7802Task() : Task(NAU_TASK_QUEUE_DEPTH) +{ + _i2c_wrapper = nullptr; + _adc = nullptr; +} + +void NAU7802Task::Init(I2C_HandleTypeDef* hi2c) +{ + SOAR_ASSERT(hi2c != nullptr, "NAU7802Task: Received Null I2C Handle"); + + _i2c_wrapper = new I2C_Wrapper(hi2c); + _adc = new NAU7802(_i2c_wrapper); +} + + +void NAU7802Task::InitTask() // RTOS Task Init +{ + // Make sure dependencies are set + SOAR_ASSERT(_adc != nullptr, "NAU7802Task: Driver not initialized. Call Init(hi2c) first."); + + // Assert Task not already created + SOAR_ASSERT(rtTaskHandle == nullptr, "Cannot initialize NAU7802 task twice"); + BaseType_t rtValue = + xTaskCreate((TaskFunction_t)NAU7802Task::RunTask, + (const char*)"NAU7802Task", + (uint16_t)NAU_TASK_STACK_DEPTH, + (void*)this, + (UBaseType_t)NAU_TASK_PRIORITY, + (TaskHandle_t*)&rtTaskHandle); + + SOAR_ASSERT(rtValue == pdPASS, "NAU7802Task::InitTask() - xTaskCreate() failed"); +} + +void NAU7802Task::Run(void * pvParams) // Instance Run loop for task +{ + /* -Driver Setup- */ + + // Initialize 1x Gain + if (_adc->begin(NAU7802_GAIN_1X) != NauStatus::OK){ + // Handle initialization error + SOAR_PRINT("NAU7802Task: Sensor initialization failed.\n"); + } + else{ + SOAR_PRINT("NAU7802Task: Sensor initialized successfully.\n"); + } + + NAU7802_OUT adcData; + + /* ==Main Loop== */ + while (1) + { + // Check if new data is ready + if (_adc->isReady()){ + // Read sensor data + if (_adc->readSensor(&adcData) == NauStatus::OK){ + // SOAR_PRINT("NAU7802Task: ADC Reading: %ld\n", adc_data.raw_reading); + + // TODO: Send data somewhere + } + else{ + SOAR_PRINT("NAU7802Task: Failed to read sensor data.\n"); + } + } + + // Handle incoming commands (optional) + // receive with 0 timeout to poll + Command cm; + if (qEvtQueue->Receive(cm, 0)) { + HandleCommand(cm); + } + + // Yield / Delay to allow other tasks to run + // TODO: Adjust delay as needed + osDelay(10); + } +} + +void NAU7802Task::HandleCommand(Command & cm) +{ + switch (cm.GetTaskCommand()) + { + // TODO: Add command cases (Gain change, calibration, etc.) IF NEEDED + + /* + -- Example command -- + case 0: // + SOAR_PRINT("NAU7802Task: Received command 0.\n"); + break; + */ + default: + SOAR_PRINT("NAU7802Task: Received Unsupported Command {%d}.\n", cm.GetTaskCommand()); + break; + } + + cm.Reset(); +} diff --git a/NAU7802/NAU7802Task.hpp b/NAU7802/NAU7802Task.hpp new file mode 100644 index 0000000..a91de50 --- /dev/null +++ b/NAU7802/NAU7802Task.hpp @@ -0,0 +1,62 @@ +/** + ******************************************************************************** + * @file NAU7802Task.hpp + * @author Javier + * @brief FreeRTOS Task Wrapper for the NAU7802 ADC Driver + ******************************************************************************** + */ + +#ifndef NAU7802_TASK_HPP_ +#define NAU7802_TASK_HPP_ + +/************************************ + * INCLUDES + ************************************/ +#include "main.h" + +// FreeRTOS includes +#include "FreeRTOS.h" +#include "task.h" +#include "queue.h" +#include "semphr.h" + +// system includes +#include "SystemDefines.hpp" +#include "Task.hpp" + +// Driver Includes +#include "NAU7802.hpp" +#include "i2c_wrapper.hpp" + +/************************************ + * CLASS DEFINITIONS + ************************************/ +class NAU7802Task : public Task +{ +public: + static NAU7802Task& Inst(){ + static NAU7802Task inst; + return inst; + } + + void Init(I2C_HandleTypeDef* hi2c); + + void InitTask(); + +protected: + static void RunTask(void* pvParams) { NAU7802Task::Inst().Run(pvParams); } + void Run(void* pvParams); + void HandleCommand(Command& cm); + +private: + // Constructors + NAU7802Task(); + NAU7802Task(const NAU7802Task&); + NAU7802Task& operator=(const NAU7802Task&); + + // Obj to allow delayed inits + I2C_Wrapper* _i2c_wrapper; + NAU7802* _adc; +}; + +#endif /* NAU7802_TASK_HPP_ */ diff --git a/NAU7802/NAU7802_regs.hpp b/NAU7802/NAU7802_regs.hpp new file mode 100644 index 0000000..376829a --- /dev/null +++ b/NAU7802/NAU7802_regs.hpp @@ -0,0 +1,61 @@ + /** + ******************************************************************************** + * @file nau7802_regs.hpp + * @author Javier + * @date 2025-11-25 + * @brief Register definitions and constants for the NAU7802 ADC. + ******************************************************************************** + */ + + + +/************************************ + * MACROS AND DEFINES + ************************************/ + +#ifndef NAU7802_REGS_HPP +#define NAU7802_REGS_HPP + +// Fixed 7-bit I2C address of the NAU7802. [left-shifted for HAL] +#define NAU7802_I2C_ADDRESS (0x2A << 1) + +// Register Map +#define NAU7802_REG_PU_CTRL 0x00 +#define NAU7802_REG_CTRL1 0x01 +#define NAU7802_REG_CTRL2 0x02 +#define NAU7802_REG_ADC_B2 0x12 // ADC Result MSB +#define NAU7802_REG_ADC_B1 0x13 // ADC Result Mid-byte +#define NAU7802_REG_ADC_B0 0x14 // ADC Result LSB +#define NAU7802_REG_REVISION_ID 0x1F + +// Gain [in] settings for register +#define NAU7802_GAIN_1X 0b000 +#define NAU7802_GAIN_2X 0b001 +#define NAU7802_GAIN_4X 0b010 +#define NAU7802_GAIN_8X 0b011 +#define NAU7802_GAIN_16X 0b100 +#define NAU7802_GAIN_32X 0b101 +#define NAU7802_GAIN_64X 0b110 +#define NAU7802_GAIN_128X 0b111 + +// PU_CTRL Register Bits +#define NAU7802_PU_CTRL_RR (1 << 0) // Register Reset +#define NAU7802_PU_CTRL_PUD (1 << 1) // Power Up Digital +#define NAU7802_PU_CTRL_PUA (1 << 2) // Power Up Analog +#define NAU7802_PU_CTRL_PUR (1 << 3) // Power Up Ready (Read Only) +#define NAU7802_PU_CTRL_CR (1 << 5) // Cycle Ready (Read Only) +#define NAU7802_PU_CTRL_OSCS (1 << 6) // Select clock source +#define NAU7802_PU_CTRL_AVDDS (1 << 7) // Select internal LDO + +// CTRL2 Register Bits +#define NAU7802_CTRL2_CHS (1 << 7) // Channel Select +#define NAU7802_CTRL2_CRS_MASK (0b111 << 4) // Conversion Rate Select Mask +#define NAU7802_CTRL2_CAL_ERR (1 << 3) // Calibration Error (Read Only) +#define NAU7802_CTRL2_CALS (1 << 2) // Start Calibration + +// Calibration Modes (CALMOD bits 1:0) +#define NAU7802_CALMOD_OFFSET_INT (0b00) // Internal Offset Calibration (Default) +#define NAU7802_CALMOD_OFFSET_SYS (0b10) // System Offset Calibration +#define NAU7802_CALMOD_GAIN_SYS (0b11) // System Gain Calibration + +#endif // NAU7802_REGS_HPP \ No newline at end of file diff --git a/NAU7802/README.md b/NAU7802/README.md new file mode 100644 index 0000000..6fd9ebb --- /dev/null +++ b/NAU7802/README.md @@ -0,0 +1,43 @@ + +# STM32 C++ Driver for NAU7802 + +This is a C++ driver for the Nuvoton NAU7802 24-bit ADC, designed to be used with the STM32 HAL library in a C++ environment + +### Features + +- Handles sensor initialization and power-up sequence +- Software reset +- Sets PGA gain (from 1x to 128x) +- Checks if new conversion data is ready +- Reads the 24-bit signed ADC result + +### Project Structure + +The driver is split into logical components to make it easy to understand and port: + +- `i2c_wrapper.hpp` / `.cpp` + - A small C++ class that wraps the C-style STM32 HAL I2C functions (`HAL_I2C_Mem_Write` / `HAL_I2C_Mem_Read`). + - Acts as the low-level communication handler for register reads/writes. +- `NAU7802_regs.hpp` + - Register addresses, bit masks, and gain values for the NAU7802 (datasheet-based definitions). +- `NAU7802.hpp` / `.cpp` + - The main driver with logic for communicating with the sensor. + - Defines `NAU7802_OUT` for output data. + - Uses `I2C_Wrapper` to perform all register operations. +- `main_read_test.cpp` / `ExampleUsage.cpp` + - Example `main()` files demonstrating driver initialization and reading. +- `NAU7802Task.hpp` / `.cpp` + - Initializes Wrapper and Tasks and handles task specific commands + +### Complete example + +See `main_read_test.cpp` in this driver folder for a complete example that initializes the sensor at 1x gain and continuously reads raw ADC values. + +### Dependencies + +- STM32 HAL (driver uses HAL I2C functions) +- C++11 or newer + +### Datasheet + +For full register details and calibration notes, consult the NAU7802 datasheet (e.g. en-us--DS_NAU7802_DataSheet_EN_Rev2.6.pdf). \ No newline at end of file diff --git a/NAU7802/i2c_wrapper.cpp b/NAU7802/i2c_wrapper.cpp new file mode 100644 index 0000000..e0c3a36 --- /dev/null +++ b/NAU7802/i2c_wrapper.cpp @@ -0,0 +1,20 @@ +/* + * i2c_wrapper.cpp + */ +#include "i2c_wrapper.hpp" + +I2C_Wrapper::I2C_Wrapper(I2C_HandleTypeDef* hi2c) : _hi2c(hi2c) { +} + +bool I2C_Wrapper::writeByte(std::uint8_t devAddr, std::uint8_t regAddr, std::uint8_t data) { + return (HAL_I2C_Mem_Write(_hi2c, devAddr, regAddr, I2C_MEMADD_SIZE_8BIT, &data, 1, 100) == HAL_OK); + +} + +bool I2C_Wrapper::readByte(std::uint8_t devAddr, std::uint8_t regAddr, std::uint8_t* dest) { + return (HAL_I2C_Mem_Read(_hi2c, devAddr, regAddr, I2C_MEMADD_SIZE_8BIT, dest, 1, 100) == HAL_OK); +} + +bool I2C_Wrapper::readBytes(std::uint8_t devAddr, std::uint8_t regAddr, std::uint8_t *data, std::uint8_t len) { + return HAL_I2C_Mem_Read(_hi2c, devAddr, regAddr, I2C_MEMADD_SIZE_8BIT, data, len, 100) == HAL_OK; +} \ No newline at end of file diff --git a/NAU7802/i2c_wrapper.hpp b/NAU7802/i2c_wrapper.hpp new file mode 100644 index 0000000..fc284e9 --- /dev/null +++ b/NAU7802/i2c_wrapper.hpp @@ -0,0 +1,67 @@ + /** + ******************************************************************************** + * @file i2c_wrapper.hpp + * @author Javier + * @date 2025-11-25 + * @brief I2C communication wrapper. + ******************************************************************************** + */ + +#ifndef I2C_WRAPPER_HPP +#define I2C_WRAPPER_HPP + + +/************************************ + * INCLUDES + ************************************/ +#include + +extern "C" { + #include "main.h" +} + + + +/************************************ + * CLASS DEFINITIONS + ************************************/ +class I2C_Wrapper { +public: + /** + * @brief Constructor. + * @param hi2c Pointer to the HAL I2C handle + */ + + I2C_Wrapper(I2C_HandleTypeDef* hi2c); + + /** + * @brief Writes a single byte to a specific register. + * @param devAddr The 8-bit device address (left-shifted) + * @param regAddr The register address to write to. + * @param data The byte to write. + * @return True if successful (HAL_OK), false otherwise. + */ + bool writeByte(std::uint8_t devAddr, std::uint8_t regAddr, std::uint8_t data); + + /** + * @brief Reads a single byte from a specific register. + * @param devAddr The 8-bit device address (left-shifted) + * @param regAddr The register address to read from. + * @return 1 if successful, 0 otherwise. + */ + bool readByte(std::uint8_t devAddr, std::uint8_t regAddr, std::uint8_t* dest); + + /** + * @brief Reads Multiple bytes from a specific register. + * @param devAddr The 8-bit device address (left-shifted) + * @param regAddr The register address to read from. + * @param data Pointer to the buffer to store read data. + * @param len Number of bytes to read. + * @return True if successful (HAL_OK), false otherwise. + */ + bool readBytes(std::uint8_t devAddr, std::uint8_t regAddr, std::uint8_t data[], std::uint8_t len); +private: + I2C_HandleTypeDef* _hi2c; +}; + +#endif // I2C_WRAPPER_HPP \ No newline at end of file diff --git a/NAU7802/main_read_test.cpp b/NAU7802/main_read_test.cpp new file mode 100644 index 0000000..207bc51 --- /dev/null +++ b/NAU7802/main_read_test.cpp @@ -0,0 +1,75 @@ +/* + * main_read_test.cpp + * + * Example usage file for the refactored NAU7802 driver. + * This example initializes the sensor with 1x gain + * and continuously reads the raw ADC value. + */ + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" +#include "i2c_wrapper.hpp" +#include "NAU7802.hpp" +#include // For printf debugging + +/* Private variables ---------------------------------------------------------*/ +// Assume hi2c1 is generated by STM32CubeIDE and initialized in main() +extern I2C_HandleTypeDef hi2c1; + +/* Function Prototypes -------------------------------------------------------*/ +// These are assumed to be in main.h or defined in main.c +void SystemClock_Config(void); +static void MX_GPIO_Init(void); +static void MX_I2C1_Init(void); +extern void Error_Handler(void); + +/** + * @brief The application entry point. + */ +int main(void) +{ + /* MCU Configuration--------------------------------------------------------*/ + HAL_Init(); + SystemClock_Config(); + MX_GPIO_Init(); + MX_I2C1_Init(); // This initializes hi2c1 + + /* --- SETUP STAGE --- */ + // 1. Create wrapper instance + I2C_Wrapper i2c_bus_1(&hi2c1); + + // 2. Create data output struct + NAU7802_OUT adc_data; + + // 3. Create the driver object + // Pass the address of the wrapper and the HAL_Delay function pointer + NAU7802 adc(&i2c_bus_1); + + // 4. Initialize the sensor + // Initializing with 1x Gain. Options: NAU7802_GAIN_1X through _128X + if (adc.begin(NAU7802_GAIN_1X) != NauStatus::OK) + { + // ADC not found or failed to configure + Error_Handler(); + } + + // 5. Calibration (Optional) + // Recommended to run after power-up and stabilization. + // adc.calibrate(); // Un-comment when calibration logic is fully verified + + /* --- MAIN LOOP --- */ + while (1) + { + // 6. Check if data is ready + if (adc.isReady()) + { + // 7. Read data + if (adc.readSensor(&adc_data) == NauStatus::OK) + { + // Debug print usage: + // printf("Raw ADC Value: %ld\r\n", adc_data.raw_reading); + } + } + HAL_Delay(100); // Wait 100ms before checking again + } +} \ No newline at end of file