added ADC functions to read BM1397 Vcore. Cleaned up DS4432U set_vcore() function

This commit is contained in:
Skot Croshere 2023-01-19 23:45:31 -05:00
parent 6dae0e2434
commit b69e0a54b4
8 changed files with 58 additions and 26 deletions

View File

@ -1,2 +1,2 @@
idf_component_register(SRCS "INA260.c" "EMC2101.c" "i2c_simple_main.c" "led_controller.c" "DS4432U.c" "EMC2101.c" "INA260.c"
idf_component_register(SRCS "adc.c" "INA260.c" "EMC2101.c" "i2c_simple_main.c" "led_controller.c" "DS4432U.c" "EMC2101.c" "INA260.c" "adc.c"
INCLUDE_DIRS ".")

View File

@ -34,7 +34,7 @@ static const char *TAG = "DS4432U.c";
* @brief voltage_to_reg takes a voltage and returns a register setting for the DS4432U to get that voltage on the TPS40305
* careful with this one!!
*/
uint8_t voltage_to_reg(float vout) {
static uint8_t voltage_to_reg(float vout) {
float change;
uint8_t reg;
@ -110,7 +110,19 @@ void DS4432U_read(void) {
ESP_LOGI(TAG, "DS4432U+ OUT1 = 0x%02X", data[0]);
}
void DS4432U_set(uint8_t val) {
static void DS4432U_set(uint8_t val) {
ESP_LOGI(TAG, "Writing 0x%02X", val);
ESP_ERROR_CHECK(register_write_byte(DS4432U_OUT0_REG, val));
}
bool DS4432U_set_vcore(float core_voltage) {
uint8_t reg_setting;
reg_setting = voltage_to_reg(core_voltage);
ESP_LOGI(TAG, "Test set %.3fV = 0x%02X", core_voltage, reg_setting);
DS4432U_set(reg_setting); ///eek!
return true;
}

View File

@ -6,7 +6,7 @@
esp_err_t i2c_master_init(void);
esp_err_t i2c_master_delete(void);
void DS4432U_read(void);
void DS4432U_set(uint8_t);
uint8_t voltage_to_reg(float vout);
bool DS4432U_set_vcore(float);
#endif /* DS4432U_H_ */

View File

@ -33,7 +33,7 @@ static esp_err_t register_write_byte(uint8_t reg_addr, uint8_t data) {
return ret;
}
float INA260_read_current(void) {
uint16_t INA260_read_current(void) {
uint8_t data[2];
ESP_ERROR_CHECK(register_read(INA260_REG_CURRENT, data, 2));
@ -42,7 +42,7 @@ float INA260_read_current(void) {
return (uint16_t)(data[0] | (data[1] << 8)) * 1.25;
}
float INA260_read_voltage(void) {
uint16_t INA260_read_voltage(void) {
uint8_t data[2];
ESP_ERROR_CHECK(register_read(INA260_REG_BUSVOLTAGE, data, 2));
@ -51,11 +51,11 @@ float INA260_read_voltage(void) {
return (uint16_t)(data[0] | (data[1] << 8)) * 1.25;
}
float INA260_read_power(void) {
uint16_t INA260_read_power(void) {
uint8_t data[2];
ESP_ERROR_CHECK(register_read(INA260_REG_POWER, data, 2));
//ESP_LOGI(TAG, "Raw Power = %02X %02X", data[1], data[0]);
return (uint16_t)(data[0] | (data[1] << 8)) * 10;
return (data[0] | (data[1] << 8)) * 10;
}

View File

@ -98,8 +98,8 @@ typedef enum _alert_latch {
} INA260_AlertLatch;
float INA260_read_current(void);
float INA260_read_voltage(void);
float INA260_read_power(void);
uint16_t INA260_read_current(void);
uint16_t INA260_read_voltage(void);
uint16_t INA260_read_power(void);
#endif /* INA260_H_ */

17
main/adc.c Normal file
View File

@ -0,0 +1,17 @@
#include "driver/adc.h"
#include "esp_adc_cal.h"
static const char *TAG = "adc.c";
static esp_adc_cal_characteristics_t adc1_chars;
//Sets up the ADC to read Vcore. Run this before ADC_get_vcore()
void ADC_init(void) {
adc1_config_channel_atten(ADC1_CHANNEL_1, ADC_ATTEN_DB_11);
esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_DEFAULT, 0, &adc1_chars);
}
//returns the ADC voltage in mV
uint16_t ADC_get_vcore(void) {
adc1_config_width(ADC_WIDTH_BIT_DEFAULT);
return esp_adc_cal_raw_to_voltage(adc1_get_raw(ADC1_CHANNEL_1), &adc1_chars);
}

7
main/adc.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef ADC_H_
#define ADC_H_
void ADC_init(void);
uint16_t ADC_get_vcore(void);
#endif /* ADC_H_ */

View File

@ -26,6 +26,7 @@
#include "DS4432U.h"
#include "EMC2101.h"
#include "INA260.h"
#include "adc.h"
static const char *TAG = "i2c-test";
@ -39,18 +40,10 @@ void app_main(void) {
ESP_ERROR_CHECK(i2c_master_init());
ESP_LOGI(TAG, "I2C initialized successfully");
// DS4432U_read();
// DS4432U_set(0x00);
// float core_voltage = 1.0;
// uint8_t reg_setting;
// reg_setting = voltage_to_reg(core_voltage);
// ESP_LOGI(TAG, "Test set %.3fV = 0x%02X", core_voltage, reg_setting);
// DS4432U_set(reg_setting); ///eek!
ADC_init();
//DS4432U tests
DS4432U_set_vcore(1.25);
//Fan Tests
EMC2101_set_config(0x04); //set the tach input
@ -60,9 +53,12 @@ void app_main(void) {
ESP_LOGI(TAG, "Fan Speed: %d RPM", EMC2101_get_fan_speed());
//Current Sensor tests
ESP_LOGI(TAG, "Current: %.1f mA", INA260_read_current());
ESP_LOGI(TAG, "Voltage: %.1f mV", INA260_read_voltage());
ESP_LOGI(TAG, "Power: %.1f mW", INA260_read_power());
ESP_LOGI(TAG, "Current: %d mA", INA260_read_current());
ESP_LOGI(TAG, "Voltage: %d mV", INA260_read_voltage());
ESP_LOGI(TAG, "Power: %d mW", INA260_read_power());
//ESP32 ADC tests
ESP_LOGI(TAG, "Vcore: %d mV", ADC_get_vcore());
ESP_ERROR_CHECK(i2c_master_delete());
ESP_LOGI(TAG, "I2C unitialized successfully");