diff --git a/projects/solar_sense/inc/temp_sense.h b/projects/solar_sense/inc/temp_sense.h new file mode 100644 index 000000000..655967834 --- /dev/null +++ b/projects/solar_sense/inc/temp_sense.h @@ -0,0 +1,51 @@ +#pragma once + +#include +#include "adc.h" +#include "can.h" +#include "gpio.h" +#include "log.h" +#include "master_task.h" +#include "solar_sense_getters.h" +#include "solar_sense_setters.h" +#include "tasks.h" + +#define MAX_TEMP 80 +#define MIN_TEMP 0 + +// Voltages for A0 and A1 +#define MAX_VOLTS1 3.334 +#define MIN_VOLTS1 2.570 + +// Voltages for A2 - A5 +#define MAX_VOLTS2 0.758 +#define MIN_VOLTS2 2.760 + +// GPIO pin index +typedef enum { + TEMP_SENSE0 = 0, + TEMP_SENSE1, + TEMP_SENSE2, + TEMP_SENSE3, + TEMP_SENSE4, + TEMP_SENSE5, + TEMP_SENSE_OVERTEMP, + TEMP_SENSE_FULLSPEED, + TEMP_SENSE_FANFAIL, + NUM_TEMP_SENSE_PINS, +} TempSensePinsIndex; + +// GPIO pins +#define TEMP_SENSE_GPIO0 { .port = GPIO_PORT_A, .pin = 0 } +#define TEMP_SENSE_GPIO1 { .port = GPIO_PORT_A, .pin = 1 } +#define TEMP_SENSE_GPIO2 { .port = GPIO_PORT_A, .pin = 2 } +#define TEMP_SENSE_GPIO3 { .port = GPIO_PORT_A, .pin = 3 } +#define TEMP_SENSE_GPIO4 { .port = GPIO_PORT_A, .pin = 4 } +#define TEMP_SENSE_GPIO5 { .port = GPIO_PORT_A, .pin = 5 } +#define TEMP_SENSE_OVERTEMP_GPIO { .port = GPIO_PORT_B, .pin = 5 } +#define TEMP_SENSE_FULLSPEED_GPIO { .port = GPIO_PORT_B, .pin = 6 } +#define TEMP_SENSE_FANFAIL_GPIO { .port = GPIO_PORT_B, .pin = 7 } + +uint16_t calculateTempDigital1(uint16_t temp_analog); // Calculates the temperature based on the analog input for A0 and A1 +uint16_t calculateTempDigital2(uint16_t temp_analog); // Calculates the temperature based on the analog input for A2 - A5 +StatusCode temp_sense_adc_init(); // Initializes the temp sense gpio pins and adc channels diff --git a/projects/solar_sense/src/main.c b/projects/solar_sense/src/main.c index ccac8a30d..e1c3be8ea 100644 --- a/projects/solar_sense/src/main.c +++ b/projects/solar_sense/src/main.c @@ -4,6 +4,7 @@ #include "master_task.h" #include "solar_sense_getters.h" #include "tasks.h" +#include "temp_sense.h" void run_fast_cycle() {} @@ -11,10 +12,24 @@ void run_medium_cycle() {} void run_slow_cycle() {} +#define DEVICE_ID 0x04 + +static CanStorage s_can_storage = { 0 }; +const CanSettings can_settings = { + .device_id = 0x1, + .bitrate = CAN_HW_BITRATE_500KBPS, + .tx = { GPIO_PORT_A, 12 }, + .rx = { GPIO_PORT_A, 11 }, + .loopback = true, +}; + int main() { tasks_init(); log_init(); + gpio_init(); LOG_DEBUG("Welcome to TEST!"); + can_init(&s_can_storage, &can_settings); + temp_sense_adc_init(); init_master_task(); diff --git a/projects/solar_sense/src/temp_sense.c b/projects/solar_sense/src/temp_sense.c new file mode 100644 index 000000000..645a64418 --- /dev/null +++ b/projects/solar_sense/src/temp_sense.c @@ -0,0 +1,103 @@ +#include "temp_sense.h" + +GpioState curr_state; + +static GpioAddress temp_sense_pins[NUM_TEMP_SENSE_PINS] = { + [TEMP_SENSE0] = TEMP_SENSE_GPIO0, + [TEMP_SENSE1] = TEMP_SENSE_GPIO1, + [TEMP_SENSE2] = TEMP_SENSE_GPIO2, + [TEMP_SENSE3] = TEMP_SENSE_GPIO3, + [TEMP_SENSE4] = TEMP_SENSE_GPIO4, + [TEMP_SENSE5] = TEMP_SENSE_GPIO5, + [TEMP_SENSE_OVERTEMP] = TEMP_SENSE_OVERTEMP_GPIO, + [TEMP_SENSE_FULLSPEED] = TEMP_SENSE_FULLSPEED_GPIO, + [TEMP_SENSE_FANFAIL] = TEMP_SENSE_FANFAIL_GPIO, +}; + +uint16_t calculateTempDigital1(uint16_t temp_analog){ + if(temp_analog > MAX_VOLTS1){ + temp_analog = MAX_VOLTS1; + } + else if (temp_analog < MIN_VOLTS1) + { + temp_analog = MIN_VOLTS1; + } + + return (temp_analog-MIN_VOLTS1)/((MAX_VOLTS1-MIN_VOLTS1)/(MAX_TEMP - MIN_TEMP)); +} + +uint16_t calculateTempDigital2(uint16_t temp_analog){ + if(temp_analog > MIN_VOLTS2){ + temp_analog = MIN_VOLTS2; + } + else if (temp_analog < MAX_VOLTS2) + { + temp_analog = MAX_VOLTS2; + } + + return (temp_analog-MIN_VOLTS2)/((MAX_VOLTS2-MIN_VOLTS2)/(MAX_TEMP - MIN_TEMP)); +} + +TASK(temp_sense_task, TASK_STACK_512){ + LOG_DEBUG("Test"); + int i; + uint16_t temp_digital; + uint16_t temp_analog; + + while(true){ + // Overtemp, Full speed, and Fan fail + gpio_get_state(&(temp_sense_pins[TEMP_SENSE_OVERTEMP]), &curr_state); + set_thermal_status_overtemp(curr_state); + gpio_get_state(&(temp_sense_pins[TEMP_SENSE_FULLSPEED]), &curr_state); + set_thermal_status_fullspeed(curr_state); + gpio_get_state(&(temp_sense_pins[TEMP_SENSE_FANFAIL]), &curr_state); + set_thermal_status_fan_fail(curr_state); + + // A0 + adc_read_converted(temp_sense_pins[0], &temp_analog); + temp_digital = calculateTempDigital1(temp_analog); + set_thermal_status_temp_1(temp_digital); + + // A1 + adc_read_converted(temp_sense_pins[1], &temp_analog); + temp_digital = calculateTempDigital1(temp_analog); + set_thermal_status_temp_2(temp_digital); + + // A2 + adc_read_converted(temp_sense_pins[2], &temp_analog); + temp_digital = calculateTempDigital2(temp_analog); + set_thermal_temps_temp_3(temp_digital); + + // A3 + adc_read_converted(temp_sense_pins[3], &temp_analog); + temp_digital = calculateTempDigital2(temp_analog); + set_thermal_temps_temp_4(temp_digital); + + // A4 + adc_read_converted(temp_sense_pins[4], &temp_analog); + temp_digital = calculateTempDigital2(temp_analog); + set_thermal_temps_temp_5(temp_digital); + + // A5 + adc_read_converted(temp_sense_pins[5], &temp_analog); + temp_digital = calculateTempDigital2(temp_analog); + set_thermal_temps_temp_6(temp_digital); + } +} + +StatusCode temp_sense_adc_init() { + int p; + for(p = 0; p < NUM_TEMP_SENSE_PINS; p++){ + if(p < TEMP_SENSE_OVERTEMP){ + gpio_init_pin(&(temp_sense_pins[p]), GPIO_ANALOG, GPIO_STATE_LOW); + adc_add_channel(temp_sense_pins[p]); + } + else{ + gpio_init_pin(&temp_sense_pins[p], GPIO_INPUT_PULL_UP, GPIO_STATE_LOW); + } + } + + tasks_init_task(temp_sense_task, TASK_PRIORITY(2), NULL); + + return STATUS_CODE_OK; +} \ No newline at end of file