2023-10-31 19:40:22 -04:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include <stdio.h>
|
2023-01-18 00:34:51 -05:00
|
|
|
|
2023-01-19 22:17:36 -05:00
|
|
|
#include "EMC2101.h"
|
|
|
|
|
2023-08-26 12:28:17 -04:00
|
|
|
// static const char *TAG = "EMC2101.c";
|
2023-01-18 00:34:51 -05:00
|
|
|
|
2023-08-26 12:28:17 -04:00
|
|
|
// run this first. sets up the config register
|
2023-10-31 19:40:22 -04:00
|
|
|
void EMC2101_init(bool invertPolarity)
|
2023-08-26 12:28:17 -04:00
|
|
|
{
|
2023-01-20 00:04:00 -05:00
|
|
|
|
2023-08-26 12:28:17 -04:00
|
|
|
// set the TACH input
|
2024-06-06 12:14:15 +02:00
|
|
|
ESP_ERROR_CHECK(i2c_master_register_write_byte(EMC2101_I2CADDR_DEFAULT, EMC2101_REG_CONFIG, 0x04));
|
2023-10-31 19:40:22 -04:00
|
|
|
|
|
|
|
if (invertPolarity) {
|
2024-06-06 12:14:15 +02:00
|
|
|
ESP_ERROR_CHECK(i2c_master_register_write_byte(EMC2101_I2CADDR_DEFAULT, EMC2101_FAN_CONFIG, 0b00100011));
|
2023-10-31 19:40:22 -04:00
|
|
|
}
|
2023-01-18 00:34:51 -05:00
|
|
|
}
|
|
|
|
|
2023-08-26 12:28:17 -04:00
|
|
|
// takes a fan speed percent
|
|
|
|
void EMC2101_set_fan_speed(float percent)
|
|
|
|
{
|
2023-01-18 00:34:51 -05:00
|
|
|
uint8_t speed;
|
|
|
|
|
2023-10-31 19:40:22 -04:00
|
|
|
speed = (uint8_t) (63.0 * percent);
|
2024-06-06 12:14:15 +02:00
|
|
|
ESP_ERROR_CHECK(i2c_master_register_write_byte(EMC2101_I2CADDR_DEFAULT, EMC2101_REG_FAN_SETTING, speed));
|
2023-01-18 00:34:51 -05:00
|
|
|
}
|
|
|
|
|
2023-08-26 12:28:17 -04:00
|
|
|
// RPM = 5400000/reading
|
|
|
|
uint16_t EMC2101_get_fan_speed(void)
|
|
|
|
{
|
2023-01-19 22:17:36 -05:00
|
|
|
uint8_t tach_lsb, tach_msb;
|
2023-01-18 00:34:51 -05:00
|
|
|
uint16_t reading;
|
2023-01-19 22:25:19 -05:00
|
|
|
uint16_t RPM;
|
2023-01-18 00:34:51 -05:00
|
|
|
|
2024-06-06 12:14:15 +02:00
|
|
|
ESP_ERROR_CHECK(i2c_master_register_read(EMC2101_I2CADDR_DEFAULT, EMC2101_TACH_LSB, &tach_lsb, 1));
|
|
|
|
ESP_ERROR_CHECK(i2c_master_register_read(EMC2101_I2CADDR_DEFAULT, EMC2101_TACH_MSB, &tach_msb, 1));
|
2023-01-18 00:34:51 -05:00
|
|
|
|
2023-08-26 12:28:17 -04:00
|
|
|
// ESP_LOGI(TAG, "Raw Fan Speed = %02X %02X", tach_msb, tach_lsb);
|
2023-01-18 00:34:51 -05:00
|
|
|
|
2023-01-19 22:17:36 -05:00
|
|
|
reading = tach_lsb | (tach_msb << 8);
|
2023-08-26 12:28:17 -04:00
|
|
|
RPM = 5400000 / reading;
|
2023-01-18 00:34:51 -05:00
|
|
|
|
2023-08-26 12:28:17 -04:00
|
|
|
// ESP_LOGI(TAG, "Fan Speed = %d RPM", RPM);
|
2023-11-14 23:02:42 -05:00
|
|
|
if (RPM == 82) {
|
|
|
|
return 0;
|
|
|
|
}
|
2023-01-18 00:34:51 -05:00
|
|
|
return RPM;
|
|
|
|
}
|
|
|
|
|
2023-09-06 17:22:08 -04:00
|
|
|
float EMC2101_get_external_temp(void)
|
2023-08-26 12:28:17 -04:00
|
|
|
{
|
2023-01-20 00:04:00 -05:00
|
|
|
uint8_t temp_msb, temp_lsb;
|
|
|
|
uint16_t reading;
|
|
|
|
|
2024-06-06 12:14:15 +02:00
|
|
|
ESP_ERROR_CHECK(i2c_master_register_read(EMC2101_I2CADDR_DEFAULT, EMC2101_EXTERNAL_TEMP_MSB, &temp_msb, 1));
|
|
|
|
ESP_ERROR_CHECK(i2c_master_register_read(EMC2101_I2CADDR_DEFAULT, EMC2101_EXTERNAL_TEMP_LSB, &temp_lsb, 1));
|
2023-01-20 00:04:00 -05:00
|
|
|
|
|
|
|
reading = temp_lsb | (temp_msb << 8);
|
|
|
|
reading >>= 5;
|
2023-01-17 00:07:21 -05:00
|
|
|
|
2023-10-31 19:40:22 -04:00
|
|
|
return (float) reading / 8.0;
|
2023-09-06 17:22:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t EMC2101_get_internal_temp(void)
|
|
|
|
{
|
|
|
|
uint8_t temp;
|
2024-06-06 12:14:15 +02:00
|
|
|
ESP_ERROR_CHECK(i2c_master_register_read(EMC2101_I2CADDR_DEFAULT, EMC2101_INTERNAL_TEMP, &temp, 1));
|
2023-09-06 17:22:08 -04:00
|
|
|
return temp;
|
|
|
|
}
|