2023-01-16 17:55:16 -05:00
|
|
|
#include <stdio.h>
|
2023-01-17 00:07:21 -05:00
|
|
|
#include <math.h>
|
2023-01-16 17:55:16 -05:00
|
|
|
#include "esp_log.h"
|
|
|
|
|
2024-10-08 10:27:32 -05:00
|
|
|
#include "i2c_bitaxe.h"
|
2024-05-27 11:43:29 -04:00
|
|
|
#include "DS4432U.h"
|
2023-08-26 12:28:17 -04:00
|
|
|
|
2024-06-06 12:14:15 +02:00
|
|
|
// DS4432U+ -- Adjustable current DAC
|
2023-08-26 12:28:17 -04:00
|
|
|
#define DS4432U_SENSOR_ADDR 0x48 // Slave address of the DS4432U+
|
|
|
|
#define DS4432U_OUT0_REG 0xF8 // register for current output 0
|
|
|
|
#define DS4432U_OUT1_REG 0xF9 // register for current output 1
|
|
|
|
|
2024-06-06 12:14:15 +02:00
|
|
|
static const char *TAG = "DS4432U";
|
2023-01-16 17:55:16 -05:00
|
|
|
|
2024-10-08 10:27:32 -05:00
|
|
|
static i2c_master_dev_handle_t ds4432u_dev_handle;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Initialize the DS4432U+ sensor.
|
|
|
|
*
|
|
|
|
* @return esp_err_t ESP_OK on success, or an error code on failure.
|
|
|
|
*/
|
|
|
|
esp_err_t DS4432U_init(void) {
|
|
|
|
return i2c_bitaxe_add_device(DS4432U_SENSOR_ADDR, &ds4432u_dev_handle);
|
|
|
|
}
|
|
|
|
|
2023-01-17 00:07:21 -05:00
|
|
|
/**
|
2024-06-06 12:14:15 +02:00
|
|
|
* @brief Set the current DAC code for a specific DS4432U output.
|
|
|
|
*
|
|
|
|
* @param output The output channel (0 or 1).
|
|
|
|
* @param code The current code value to set.
|
|
|
|
* @return esp_err_t ESP_OK on success, or an error code on failure.
|
2023-01-17 00:07:21 -05:00
|
|
|
*/
|
2024-06-06 12:14:15 +02:00
|
|
|
esp_err_t DS4432U_set_current_code(uint8_t output, uint8_t code) {
|
|
|
|
uint8_t reg = (output == 0) ? DS4432U_OUT0_REG : DS4432U_OUT1_REG;
|
2024-10-08 10:27:32 -05:00
|
|
|
return i2c_bitaxe_register_write_byte(ds4432u_dev_handle, reg, code);
|
2024-06-06 12:14:15 +02:00
|
|
|
}
|
2023-01-17 00:07:21 -05:00
|
|
|
|
2024-06-06 12:14:15 +02:00
|
|
|
/**
|
|
|
|
* @brief Get the current DAC code value for a specific DS4432U output.
|
|
|
|
*
|
|
|
|
* @param output The output channel (0 or 1).
|
|
|
|
* @param code Pointer to store the current code value.
|
|
|
|
* @return esp_err_t ESP_OK on success, or an error code on failure.
|
|
|
|
*/
|
|
|
|
esp_err_t DS4432U_get_current_code(uint8_t output, uint8_t *code) {
|
|
|
|
uint8_t reg = (output == 0) ? DS4432U_OUT0_REG : DS4432U_OUT1_REG;
|
2024-10-08 10:27:32 -05:00
|
|
|
return i2c_bitaxe_register_read(ds4432u_dev_handle, reg, code, 1);
|
2023-01-17 00:07:21 -05:00
|
|
|
}
|
|
|
|
|
2024-03-18 00:46:59 -04:00
|
|
|
bool DS4432U_test(void)
|
|
|
|
{
|
2024-06-06 12:14:15 +02:00
|
|
|
uint8_t data;
|
2024-03-18 00:46:59 -04:00
|
|
|
|
|
|
|
/* Read the DS4432U+ WHO_AM_I register, on power up the register should have the value 0x00 */
|
2024-10-08 10:27:32 -05:00
|
|
|
esp_err_t register_result = i2c_bitaxe_register_read(ds4432u_dev_handle, DS4432U_OUT0_REG, &data, 1);
|
2024-06-06 12:14:15 +02:00
|
|
|
ESP_LOGI(TAG, "DS4432U+ OUT0 = 0x%02X", data);
|
2024-03-18 00:46:59 -04:00
|
|
|
return register_result == ESP_OK;
|
|
|
|
}
|