From dcfbc46ea08f905c47d0ad02bea32f6e32a5a1c5 Mon Sep 17 00:00:00 2001 From: Skot Date: Fri, 6 Sep 2024 16:21:58 -0400 Subject: [PATCH] oops I2C problem. working now. still need to mutex wrap TPS546 smbus commands.. --- main/i2c_master.c | 27 +++++++-------------------- main/i2c_master.h | 2 +- main/oled.c | 8 +++++--- main/tasks/power_management_task.c | 20 ++++++++++---------- 4 files changed, 23 insertions(+), 34 deletions(-) diff --git a/main/i2c_master.c b/main/i2c_master.c index 212cea785..8d3680f47 100644 --- a/main/i2c_master.c +++ b/main/i2c_master.c @@ -1,5 +1,9 @@ +#include + #include "freertos/FreeRTOS.h" #include "freertos/task.h" +#include "esp_err.h" +#include "esp_log.h" #include "i2c_master.h" @@ -89,35 +93,18 @@ esp_err_t i2c_master_register_write_byte(uint8_t device_address, uint8_t reg_add /** * @brief Write multiple bytes to a I2C register */ -esp_err_t i2c_master_register_write_bytes(uint8_t device_address, uint8_t reg_addr, uint8_t * data, uint16_t len) +esp_err_t i2c_master_register_write_bytes(uint8_t device_address, uint8_t * data, uint16_t len) { esp_err_t return_value; - //allocate a uint8_t array to store the data - uint8_t *write_buf = (uint8_t *)malloc(len+1); - - //check if the allocation was successful - if (write_buf == NULL) { - return ESP_FAIL; - } - - write_buf[0] = reg_addr; //set the first byte to the register address - - //fill the data buffer with the data - for (int i = 0; i < len; i++) { - write_buf[i+1] = data; - } - //wait for I2C access if (xSemaphoreTake(i2c_sem, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS) != pdTRUE) { + ESP_LOGE("I2C", "Failed to take I2C semaphore"); return ESP_FAIL; } - return_value = i2c_master_write_to_device(I2C_MASTER_NUM, device_address, write_buf, sizeof(write_buf), I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS); - - //free the allocated memory - free(write_buf); + return_value = i2c_master_write_to_device(I2C_MASTER_NUM, device_address, data, len, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS); //release I2C access xSemaphoreGive(i2c_sem); diff --git a/main/i2c_master.h b/main/i2c_master.h index 3adb1e931..3e65379bd 100644 --- a/main/i2c_master.h +++ b/main/i2c_master.h @@ -10,7 +10,7 @@ esp_err_t i2c_master_init(void); esp_err_t i2c_master_delete(void); esp_err_t i2c_master_register_read(uint8_t device_address, uint8_t reg_addr, uint8_t * data, size_t len); esp_err_t i2c_master_register_write_byte(uint8_t device_address, uint8_t reg_addr, uint8_t data); -esp_err_t i2c_master_register_write_bytes(uint8_t device_address, uint8_t reg_addr, uint8_t * data, uint16_t len); +esp_err_t i2c_master_register_write_bytes(uint8_t device_address, uint8_t * data, uint16_t len); esp_err_t i2c_master_register_write_word(uint8_t device_address, uint8_t reg_addr, uint16_t data); #endif /* I2C_MASTER_H_ */ diff --git a/main/oled.c b/main/oled.c index be93ed538..8215be11a 100644 --- a/main/oled.c +++ b/main/oled.c @@ -25,6 +25,8 @@ #include "oled.h" +#define DEVICE_ADDRESS 0x3C + extern unsigned char ucSmallFont[]; static int iScreenOffset; // current write offset of screen data static unsigned char ucScreen[1024]; // local copy of the image buffer @@ -283,12 +285,12 @@ static esp_err_t write(uint8_t * data, uint8_t len) { int ret; - //ret = i2c_master_write_to_device(I2C_MASTER_NUM, 0x3C, data, len, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS); - ret = i2c_master_register_write_bytes(I2C_MASTER_NUM, I2C_MASTER_NUM, data, len); + //ret = i2c_master_write_to_device(I2C_MASTER_NUM, DEVICE_ADDRESS, data, len, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS); + ret = i2c_master_register_write_bytes(DEVICE_ADDRESS, data, len); if (ret != ESP_OK) { ESP_LOGE("OLED", "Failed to write to device: %d", ret); } - + return ret; } diff --git a/main/tasks/power_management_task.c b/main/tasks/power_management_task.c index 6657fa330..de9aaa7fa 100644 --- a/main/tasks/power_management_task.c +++ b/main/tasks/power_management_task.c @@ -31,15 +31,15 @@ static const char * TAG = "power_management"; -static float _fbound(float value, float lower_bound, float upper_bound) -{ - if (value < lower_bound) - return lower_bound; - if (value > upper_bound) - return upper_bound; +// static float _fbound(float value, float lower_bound, float upper_bound) +// { +// if (value < lower_bound) +// return lower_bound; +// if (value > upper_bound) +// return upper_bound; - return value; -} +// return value; +// } // Set the fan speed between 20% min and 100% max based on chip temperature as input. // The fan speed increases from 20% to 100% proportionally to the temperature increase from 50 and THROTTLE_TEMP @@ -84,9 +84,9 @@ void POWER_MANAGEMENT_task(void * pvParameters) power_management->HAS_POWER_EN = GLOBAL_STATE->board_version == 202 || GLOBAL_STATE->board_version == 203 || GLOBAL_STATE->board_version == 204; power_management->HAS_PLUG_SENSE = GLOBAL_STATE->board_version == 204; - int last_frequency_increase = 0; + //int last_frequency_increase = 0; - uint16_t frequency_target = nvs_config_get_u16(NVS_CONFIG_ASIC_FREQ, CONFIG_ASIC_FREQUENCY); + //uint16_t frequency_target = nvs_config_get_u16(NVS_CONFIG_ASIC_FREQ, CONFIG_ASIC_FREQUENCY); uint16_t auto_fan_speed = nvs_config_get_u16(NVS_CONFIG_AUTO_FAN_SPEED, 1);