Skip to content

Commit

Permalink
oops I2C problem. working now. still need to mutex wrap TPS546 smbus …
Browse files Browse the repository at this point in the history
…commands..
  • Loading branch information
skot committed Sep 6, 2024
1 parent 63a8b5d commit dcfbc46
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 34 deletions.
27 changes: 7 additions & 20 deletions main/i2c_master.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include <string.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_err.h"
#include "esp_log.h"

#include "i2c_master.h"

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion main/i2c_master.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */
8 changes: 5 additions & 3 deletions main/oled.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
20 changes: 10 additions & 10 deletions main/tasks/power_management_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit dcfbc46

Please sign in to comment.