From 97bd0e8060bae0bcddb49a8ddbaf838afc67e771 Mon Sep 17 00:00:00 2001 From: Ayo Date: Wed, 13 Sep 2023 18:53:51 +0000 Subject: [PATCH 1/3] current sense for solar sense --- projects/solar_sense/inc/current_sense.h | 17 ++++++ projects/solar_sense/src/current_sense.c | 77 ++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 projects/solar_sense/inc/current_sense.h create mode 100644 projects/solar_sense/src/current_sense.c diff --git a/projects/solar_sense/inc/current_sense.h b/projects/solar_sense/inc/current_sense.h new file mode 100644 index 000000000..76d12befa --- /dev/null +++ b/projects/solar_sense/inc/current_sense.h @@ -0,0 +1,17 @@ +#pragma once +#include + +#include "gpio.h" +#include "status.h" + +// LTC2451 Possible conversion speeds +uint8_t set_30Hz_speed[] = { 0x80 }; +uint8_t set_60Hz_speed[] = { 0x00 }; + +StatusCode ltc2451_adc_init(); + +StatusCode drv120_relay_init(); + +StatusCode ltc2451_adc_read_converted(uint16_t *voltage_measured); + +StatusCode drv120_relay_set(GpioState state); diff --git a/projects/solar_sense/src/current_sense.c b/projects/solar_sense/src/current_sense.c new file mode 100644 index 000000000..f0db2d67a --- /dev/null +++ b/projects/solar_sense/src/current_sense.c @@ -0,0 +1,77 @@ +#include "current_sense.h" + +#include +#include + +#include "gpio.h" +#include "i2c.h" +#include "log.h" +#include "solar_sense_setters.h" +#include "status.h" + +#define LTC2451_I2C_ADDR 0x14 + +#define ADC_REFERENCE_VOLTAGE 3.3 +#define ADC_RESOLUTION 65535 +#define CURRENT_THRESHOLD 100 // TO-DO: Ask HW team +#define CURRENT_SENSOR_SENSITIVITY 0.066 + +static GpioAddress relay_en_address = { GPIO_PORT_B, 4 }; // Enable pin +static GpioAddress relay_status_address = { GPIO_PORT_A, 6 }; // Status pin + +static GpioAddress *relay_en_gpio = &relay_en_address; +static GpioAddress *relay_status_gpio = &relay_status_address; + +static GpioState relay_status; + +StatusCode ltc2451_adc_init( + uint8_t *conversion_speed) { // Initialize I2C and set conversion speed for ADC + I2CSettings i2c_settings = { + .speed = I2C_SPEED_FAST, + .scl = { .port = GPIO_PORT_B, .pin = 8 }, + .sda = { .port = GPIO_PORT_B, .pin = 9 }, + }; + + status_ok_or_return(i2c_init(I2C_PORT_1, &i2c_settings)); + + // Write to the LTC2451 to set conversion speed + status_ok_or_return(i2c_write(I2C_PORT_1, LTC2451_I2C_ADDR, conversion_speed, 1)); + + return STATUS_CODE_OK; +} + +StatusCode drv120_relay_init() { // Initialize GPIOs for relay + status_ok_or_return(gpio_init_pin(relay_en_gpio, GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_LOW)); + status_ok_or_return(gpio_init_pin(relay_status_gpio, GPIO_INPUT_PULL_DOWN, GPIO_STATE_LOW)); + status_ok_or_return(gpio_set_state(relay_status_gpio, GPIO_STATE_LOW)); + + return STATUS_CODE_OK; +} + +StatusCode ltc2451_adc_read_converted(uint16_t *voltage_measured) { // ADC reading and conversion + uint8_t read_bytes[2]; + status_ok_or_return(i2c_read(I2C_PORT_1, LTC2451_I2C_ADDR, read_bytes, 2)); + + // Convert read bytes to single value + uint16_t adc_reading = (read_bytes[0] << 8) | read_bytes[1]; + + *voltage_measured = (uint16_t)((ADC_REFERENCE_VOLTAGE * adc_reading) / ADC_RESOLUTION); + uint16_t current_measured = (uint16_t)(*voltage_measured / CURRENT_SENSOR_SENSITIVITY); + + // Open relay on fault + if (current_measured > CURRENT_THRESHOLD) drv120_relay_set(GPIO_STATE_HIGH); + + // Send info to CAN + set_current_sense_current(current_measured); + set_current_sense_voltage(*voltage_measured); + set_current_sense_relay_status(relay_status); + + return STATUS_CODE_OK; +} + +StatusCode drv120_relay_set(GpioState state) { // Relay command function + status_ok_or_return(gpio_set_state(relay_en_gpio, state)); + status_ok_or_return(gpio_get_state(relay_status_gpio, &relay_status)); + + return STATUS_CODE_OK; +} From 13b0a65328e06a7dcaaf550b3f12f73d53d2dfef Mon Sep 17 00:00:00 2001 From: Ayo Date: Thu, 14 Sep 2023 00:32:07 +0000 Subject: [PATCH 2/3] Changed function names and combined init functions into one --- projects/solar_sense/inc/current_sense.h | 10 ++++------ projects/solar_sense/src/current_sense.c | 14 ++++++-------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/projects/solar_sense/inc/current_sense.h b/projects/solar_sense/inc/current_sense.h index 76d12befa..f831170d7 100644 --- a/projects/solar_sense/inc/current_sense.h +++ b/projects/solar_sense/inc/current_sense.h @@ -4,14 +4,12 @@ #include "gpio.h" #include "status.h" -// LTC2451 Possible conversion speeds +// LTC2451 ADC Possible conversion speeds uint8_t set_30Hz_speed[] = { 0x80 }; uint8_t set_60Hz_speed[] = { 0x00 }; -StatusCode ltc2451_adc_init(); +StatusCode current_sense_init(uint8_t *conversion_speed); -StatusCode drv120_relay_init(); +StatusCode current_sense_main_cycle(uint16_t *voltage_measured); -StatusCode ltc2451_adc_read_converted(uint16_t *voltage_measured); - -StatusCode drv120_relay_set(GpioState state); +StatusCode current_sense_relay_set(GpioState state); diff --git a/projects/solar_sense/src/current_sense.c b/projects/solar_sense/src/current_sense.c index f0db2d67a..562e136c5 100644 --- a/projects/solar_sense/src/current_sense.c +++ b/projects/solar_sense/src/current_sense.c @@ -13,6 +13,7 @@ #define ADC_REFERENCE_VOLTAGE 3.3 #define ADC_RESOLUTION 65535 + #define CURRENT_THRESHOLD 100 // TO-DO: Ask HW team #define CURRENT_SENSOR_SENSITIVITY 0.066 @@ -24,7 +25,7 @@ static GpioAddress *relay_status_gpio = &relay_status_address; static GpioState relay_status; -StatusCode ltc2451_adc_init( +StatusCode current_sense_init( uint8_t *conversion_speed) { // Initialize I2C and set conversion speed for ADC I2CSettings i2c_settings = { .speed = I2C_SPEED_FAST, @@ -37,10 +38,7 @@ StatusCode ltc2451_adc_init( // Write to the LTC2451 to set conversion speed status_ok_or_return(i2c_write(I2C_PORT_1, LTC2451_I2C_ADDR, conversion_speed, 1)); - return STATUS_CODE_OK; -} - -StatusCode drv120_relay_init() { // Initialize GPIOs for relay + // Initialize GPIOs for relay status_ok_or_return(gpio_init_pin(relay_en_gpio, GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_LOW)); status_ok_or_return(gpio_init_pin(relay_status_gpio, GPIO_INPUT_PULL_DOWN, GPIO_STATE_LOW)); status_ok_or_return(gpio_set_state(relay_status_gpio, GPIO_STATE_LOW)); @@ -48,7 +46,7 @@ StatusCode drv120_relay_init() { // Initialize GPIOs for relay return STATUS_CODE_OK; } -StatusCode ltc2451_adc_read_converted(uint16_t *voltage_measured) { // ADC reading and conversion +StatusCode current_sense_main_cycle(uint16_t *voltage_measured) { // ADC reading and conversion uint8_t read_bytes[2]; status_ok_or_return(i2c_read(I2C_PORT_1, LTC2451_I2C_ADDR, read_bytes, 2)); @@ -59,7 +57,7 @@ StatusCode ltc2451_adc_read_converted(uint16_t *voltage_measured) { // ADC read uint16_t current_measured = (uint16_t)(*voltage_measured / CURRENT_SENSOR_SENSITIVITY); // Open relay on fault - if (current_measured > CURRENT_THRESHOLD) drv120_relay_set(GPIO_STATE_HIGH); + if (current_measured > CURRENT_THRESHOLD) current_sense_relay_set(GPIO_STATE_HIGH); // Send info to CAN set_current_sense_current(current_measured); @@ -69,7 +67,7 @@ StatusCode ltc2451_adc_read_converted(uint16_t *voltage_measured) { // ADC read return STATUS_CODE_OK; } -StatusCode drv120_relay_set(GpioState state) { // Relay command function +StatusCode current_sense_relay_set(GpioState state) { // Relay command function status_ok_or_return(gpio_set_state(relay_en_gpio, state)); status_ok_or_return(gpio_get_state(relay_status_gpio, &relay_status)); From 3d56c80bdae2c5e86fb9e9f9c1eaa48cd230faf8 Mon Sep 17 00:00:00 2001 From: Ayo Date: Mon, 18 Sep 2023 21:53:46 +0000 Subject: [PATCH 3/3] Made requested changes --- projects/solar_sense/inc/current_sense.h | 8 ++----- projects/solar_sense/src/current_sense.c | 28 ++++++++++++++---------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/projects/solar_sense/inc/current_sense.h b/projects/solar_sense/inc/current_sense.h index f831170d7..bf11e9a9f 100644 --- a/projects/solar_sense/inc/current_sense.h +++ b/projects/solar_sense/inc/current_sense.h @@ -4,12 +4,8 @@ #include "gpio.h" #include "status.h" -// LTC2451 ADC Possible conversion speeds -uint8_t set_30Hz_speed[] = { 0x80 }; -uint8_t set_60Hz_speed[] = { 0x00 }; +StatusCode current_sense_init(uint8_t conversion_speed); -StatusCode current_sense_init(uint8_t *conversion_speed); - -StatusCode current_sense_main_cycle(uint16_t *voltage_measured); +StatusCode current_sense_main_cycle(); StatusCode current_sense_relay_set(GpioState state); diff --git a/projects/solar_sense/src/current_sense.c b/projects/solar_sense/src/current_sense.c index 562e136c5..2d083ddd9 100644 --- a/projects/solar_sense/src/current_sense.c +++ b/projects/solar_sense/src/current_sense.c @@ -1,11 +1,9 @@ #include "current_sense.h" -#include #include #include "gpio.h" #include "i2c.h" -#include "log.h" #include "solar_sense_setters.h" #include "status.h" @@ -25,8 +23,12 @@ static GpioAddress *relay_status_gpio = &relay_status_address; static GpioState relay_status; -StatusCode current_sense_init( - uint8_t *conversion_speed) { // Initialize I2C and set conversion speed for ADC +// LTC2451 ADC Possible conversion speeds +#define CURRENT_SENSE_ADC_30_HZ 0x80; +#define CURRENT_SENSE_ADC_60_HZ 0x00; + +// Set conversion speed to either 30Hz or 60Hz defined above +StatusCode current_sense_init(uint8_t conversion_speed) { I2CSettings i2c_settings = { .speed = I2C_SPEED_FAST, .scl = { .port = GPIO_PORT_B, .pin = 8 }, @@ -36,7 +38,7 @@ StatusCode current_sense_init( status_ok_or_return(i2c_init(I2C_PORT_1, &i2c_settings)); // Write to the LTC2451 to set conversion speed - status_ok_or_return(i2c_write(I2C_PORT_1, LTC2451_I2C_ADDR, conversion_speed, 1)); + status_ok_or_return(i2c_write(I2C_PORT_1, LTC2451_I2C_ADDR, &conversion_speed, 1)); // Initialize GPIOs for relay status_ok_or_return(gpio_init_pin(relay_en_gpio, GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_LOW)); @@ -46,28 +48,32 @@ StatusCode current_sense_init( return STATUS_CODE_OK; } -StatusCode current_sense_main_cycle(uint16_t *voltage_measured) { // ADC reading and conversion +// ADC reading and conversion +StatusCode current_sense_main_cycle() { uint8_t read_bytes[2]; status_ok_or_return(i2c_read(I2C_PORT_1, LTC2451_I2C_ADDR, read_bytes, 2)); // Convert read bytes to single value uint16_t adc_reading = (read_bytes[0] << 8) | read_bytes[1]; - *voltage_measured = (uint16_t)((ADC_REFERENCE_VOLTAGE * adc_reading) / ADC_RESOLUTION); - uint16_t current_measured = (uint16_t)(*voltage_measured / CURRENT_SENSOR_SENSITIVITY); + uint16_t voltage_measured = (uint16_t)((ADC_REFERENCE_VOLTAGE * adc_reading) / ADC_RESOLUTION); + uint16_t current_measured = (uint16_t)(voltage_measured / CURRENT_SENSOR_SENSITIVITY); // Open relay on fault - if (current_measured > CURRENT_THRESHOLD) current_sense_relay_set(GPIO_STATE_HIGH); + if (current_measured > CURRENT_THRESHOLD) { + current_sense_relay_set(GPIO_STATE_HIGH); + } // Send info to CAN set_current_sense_current(current_measured); - set_current_sense_voltage(*voltage_measured); + set_current_sense_voltage(voltage_measured); set_current_sense_relay_status(relay_status); return STATUS_CODE_OK; } -StatusCode current_sense_relay_set(GpioState state) { // Relay command function +// Set state to: GPIO_LOW -> Relay close; GPIO_HIGH -> Relay open +StatusCode current_sense_relay_set(GpioState state) { status_ok_or_return(gpio_set_state(relay_en_gpio, state)); status_ok_or_return(gpio_get_state(relay_status_gpio, &relay_status));