diff --git a/App/Inc/tasks/relay.hpp b/App/Inc/tasks/relay.hpp new file mode 100644 index 0000000..35d20f9 --- /dev/null +++ b/App/Inc/tasks/relay.hpp @@ -0,0 +1,29 @@ +/* + * can_task.hpp + * + * Created on: 10 Dec 2023 + * Author: Carl + */ + +#ifndef INC_TASKS_RELAYTASK_HPP_ +#define INC_TASKS_RELAYTASK_HPP_ + +#include +#include + +namespace tasks +{ + class RelayTask : public sta::tacos::TacosThread { + public: + RelayTask(uint32_t canID); + + void init() override; + + void func() override; + + private: + + }; +} // namespace demo + +#endif /* INC_TASKS_RELAYTASK_HPP_ */ diff --git a/App/Inc/tasks/thermo.hpp b/App/Inc/tasks/thermo.hpp index 3080d83..13f3b91 100644 --- a/App/Inc/tasks/thermo.hpp +++ b/App/Inc/tasks/thermo.hpp @@ -16,11 +16,11 @@ #include #include -namespace demo +namespace tasks { class ThermoTask : public sta::tacos::TacosThread { public: - ThermoTask(); + ThermoTask(uint32_t canID); void init() override; @@ -28,12 +28,14 @@ namespace demo private: - sta::STM32GpioPin cs_pin = sta::STM32GpioPin(GPIOE, GPIO_PIN_13); + sta::STM32GpioPin* cs_pin[5]; + sta::RtosMutex* mutex; sta::STM32SPI* spi2; - sta::STM32SPIDevice* device_; - sta::MAX31855* tc; + sta::STM32SPIDevice* device_[5]; + + sta::MAX31855* tc_[5]; }; } // namespace demo diff --git a/App/Src/tasks/relay.cpp b/App/Src/tasks/relay.cpp new file mode 100644 index 0000000..9e06000 --- /dev/null +++ b/App/Src/tasks/relay.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include "can.h" +#include + +#include + +namespace tasks { +RelayTask::RelayTask(uint32_t canID) : + TacosThread("RELAY", osPriorityNormal) { + setCanID(canID); +} + +void RelayTask::init() { +} + +void RelayTask::func() { + // Receiving polling message + CanSysMsg msg; + + if (CAN_queue_.get(&msg, osWaitForever)) { + + uint8_t dev = msg.payload[0]; + uint8_t value = msg.payload[1]; + switch (dev) { + case 1: + if (value == 1) { + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_SET); + } else if (value == 0) { + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_RESET); + } + break; + + case 2: + if (value == 1) { + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET); + } else if (value == 0) { + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET); + } + break; + + case 3: + if (value == 1) { + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_SET); + } else if (value == 0) { + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET); + } + break; + + case 4: + if (value == 1) { + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_SET); + } else if (value == 0) { + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET); + } + break; + + case 5: + if (value == 1) { + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_SET); + } else if (value == 0) { + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET); + } + break; + } + + } +} +} // namespace tasks + diff --git a/App/Src/tasks/thermo.cpp b/App/Src/tasks/thermo.cpp index 4c855a9..ebf60b0 100644 --- a/App/Src/tasks/thermo.cpp +++ b/App/Src/tasks/thermo.cpp @@ -7,51 +7,86 @@ #include #include -#include #include #include #include #include +#include #include #include -namespace demo +namespace tasks { - ThermoTask::ThermoTask() + ThermoTask::ThermoTask(uint32_t canID) : TacosThread("Thermo", osPriorityNormal) { - + setCanID(canID); } void ThermoTask::init() { mutex = new sta::RtosMutex("spi2"); spi2 = new sta::STM32SPI(&hspi2, 16000000, mutex); - device_ = new sta::STM32SPIDevice(spi2, &cs_pin); - tc = new sta::MAX31855(device_); //create driver object + // Init cs pins + cs_pin[0] = new sta::STM32GpioPin(GPIOE, GPIO_PIN_12); + cs_pin[1] = new sta::STM32GpioPin(GPIOB, GPIO_PIN_10); + cs_pin[2] = new sta::STM32GpioPin(GPIOE, GPIO_PIN_15); + cs_pin[3] = new sta::STM32GpioPin(GPIOE, GPIO_PIN_14); + cs_pin[4] = new sta::STM32GpioPin(GPIOE, GPIO_PIN_13); + + // init devices + for (uint8_t i = 0; i < 5; i++){ + device_[i] = new sta::STM32SPIDevice(spi2, cs_pin[i]); + } + + // init drivers + for (uint8_t i = 0; i < 5; i++){ + tc_[i] = new sta::MAX31855(device_[i]); //create driver object + } } void ThermoTask::func() { - //STA_DEBUG_HEAP_STATS(); - //canController.sendFrame(txHeader, payload); - //STA_DEBUG_HEAP_STATS(); + // Receiving polling message + CanSysMsg msg; - //fuck off other pins - HAL_GPIO_WritePin(GPIOE,GPIO_PIN_12, GPIO_PIN_SET); - HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10, GPIO_PIN_SET); - HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15, GPIO_PIN_SET); - HAL_GPIO_WritePin(GPIOE,GPIO_PIN_14, GPIO_PIN_SET); + if (CAN_queue_.get(&msg, osWaitForever)) + { - tc->update(); // update internal values - float temperature = tc->getTemp(); //read out temperature in degrees Celsius - float referenceTemperature = tc->getReferenceTemp(); // read out reference temperature in degrees Celsius - uint8_t status = tc->getStatus(); //read out status of the update + //fuck off other pins + /*HAL_GPIO_WritePin(GPIOE,GPIO_PIN_12, GPIO_PIN_SET); + HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10, GPIO_PIN_SET); + HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15, GPIO_PIN_SET); + HAL_GPIO_WritePin(GPIOE,GPIO_PIN_14, GPIO_PIN_SET);*/ + + uint8_t dev = msg.payload[0] - 1; // -1 to offset start from 0 + + //float temp = tc_[dev]->measureTemp(); + tc_[dev]->update(); // update internal values + float temp = tc_[dev]->getTemp(); //read out temperature in degrees Celsius + + uint8_t* bytePointer = reinterpret_cast(&temp); + + for(uint8_t i = 0; i < 4; i++){ + msg.payload[i] = bytePointer[i]; // first four bytes are float of Temperature in Celcius + } + + + //msg.payload[4] = tc_[dev]->getStatus(); // Followed by status + + msg.header.sid = getCanID(); + msg.header.payloadLength = 5; + msg.header.eid = 0; + msg.header.format = 0; + + // Send it out + sta::tacos::queueCanBusMsg(msg, 0); + + } - HAL_Delay(1000); } } // namespace demo