Skip to content

Commit

Permalink
Pushing fwxv 228 for review
Browse files Browse the repository at this point in the history
  • Loading branch information
cnscwong committed Sep 30, 2023
1 parent 266fafb commit 9b5c6dc
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 0 deletions.
51 changes: 51 additions & 0 deletions projects/solar_sense/inc/temp_sense.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#pragma once

#include <stdio.h>
#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
15 changes: 15 additions & 0 deletions projects/solar_sense/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,32 @@
#include "master_task.h"
#include "solar_sense_getters.h"
#include "tasks.h"
#include "temp_sense.h"

void run_fast_cycle() {}

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();

Expand Down
103 changes: 103 additions & 0 deletions projects/solar_sense/src/temp_sense.c
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 9b5c6dc

Please sign in to comment.