Рефакторинг, доработка проекта STM (в релиз)
This commit is contained in:
25
STM32_CO2_NODE/Core/Src/uart_callbacks.cpp
Normal file
25
STM32_CO2_NODE/Core/Src/uart_callbacks.cpp
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#include "FreeRTOS.h"
|
||||||
|
#include "task.h"
|
||||||
|
#include "semphr.h"
|
||||||
|
|
||||||
|
#include "usart.h"
|
||||||
|
|
||||||
|
#include "mhz19c.hpp"
|
||||||
|
|
||||||
|
extern MHZ19C* g_mhz19c;
|
||||||
|
|
||||||
|
extern "C"
|
||||||
|
void HAL_UART_RxCpltCallback(
|
||||||
|
UART_HandleTypeDef* huart)
|
||||||
|
{
|
||||||
|
extern UART_HandleTypeDef huart1;
|
||||||
|
|
||||||
|
if(huart == &huart1)
|
||||||
|
{
|
||||||
|
BaseType_t hpw = pdFALSE;
|
||||||
|
|
||||||
|
xSemaphoreGiveFromISR( g_mhz19c->GetRxSemaphore(), &hpw);
|
||||||
|
|
||||||
|
portYIELD_FROM_ISR(hpw);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,8 @@
|
|||||||
MHZ19C::MHZ19C(UART_HandleTypeDef* huart)
|
MHZ19C::MHZ19C(UART_HandleTypeDef* huart)
|
||||||
: uart(huart)
|
: uart(huart)
|
||||||
{
|
{
|
||||||
|
rxDoneSem = xSemaphoreCreateBinary();
|
||||||
|
|
||||||
BuildReadFrame();
|
BuildReadFrame();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,22 +35,11 @@ uint8_t MHZ19C::Checksum(uint8_t* data)
|
|||||||
|
|
||||||
bool MHZ19C::ReadCO2(uint16_t& ppm)
|
bool MHZ19C::ReadCO2(uint16_t& ppm)
|
||||||
{
|
{
|
||||||
if(HAL_UART_Transmit(
|
HAL_UART_Transmit(uart, txFrame, 9, 100);
|
||||||
uart,
|
|
||||||
txFrame,
|
|
||||||
9,
|
|
||||||
100)
|
|
||||||
!= HAL_OK)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(HAL_UART_Receive(
|
HAL_UART_Receive_DMA(uart, rxFrame, 9);
|
||||||
uart,
|
|
||||||
rxFrame,
|
if(xSemaphoreTake(rxDoneSem, pdMS_TO_TICKS(200)) != pdTRUE)
|
||||||
9,
|
|
||||||
100)
|
|
||||||
!= HAL_OK)
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -58,9 +49,7 @@ bool MHZ19C::ReadCO2(uint16_t& ppm)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ppm =
|
ppm = (rxFrame[2] << 8) | rxFrame[3];
|
||||||
(rxFrame[2] << 8) |
|
|
||||||
rxFrame[3];
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -82,10 +71,5 @@ void MHZ19C::CalibrateZero()
|
|||||||
|
|
||||||
cmd[8] = Checksum(cmd);
|
cmd[8] = Checksum(cmd);
|
||||||
|
|
||||||
HAL_UART_Transmit(
|
HAL_UART_Transmit(uart, cmd, 9, 100);
|
||||||
uart,
|
|
||||||
cmd,
|
|
||||||
9,
|
|
||||||
100
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "stm32f1xx_hal.h"
|
#include "stm32f1xx_hal.h"
|
||||||
|
#include "FreeRTOS.h"
|
||||||
|
#include "semphr.h"
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
class MHZ19C
|
class MHZ19C
|
||||||
@@ -14,6 +15,11 @@ public:
|
|||||||
|
|
||||||
void CalibrateZero();
|
void CalibrateZero();
|
||||||
|
|
||||||
|
SemaphoreHandle_t GetRxSemaphore()
|
||||||
|
{
|
||||||
|
return rxDoneSem;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
UART_HandleTypeDef* uart;
|
UART_HandleTypeDef* uart;
|
||||||
@@ -21,6 +27,8 @@ private:
|
|||||||
uint8_t txFrame[9];
|
uint8_t txFrame[9];
|
||||||
uint8_t rxFrame[9];
|
uint8_t rxFrame[9];
|
||||||
|
|
||||||
|
SemaphoreHandle_t rxDoneSem;
|
||||||
|
|
||||||
void BuildReadFrame();
|
void BuildReadFrame();
|
||||||
|
|
||||||
uint8_t Checksum(uint8_t* data);
|
uint8_t Checksum(uint8_t* data);
|
||||||
|
|||||||
@@ -8,40 +8,25 @@ extern "C"
|
|||||||
|
|
||||||
bool SDCard::Init()
|
bool SDCard::Init()
|
||||||
{
|
{
|
||||||
FATFS fs;
|
|
||||||
|
|
||||||
return f_mount(&fs, "", 1) == FR_OK;
|
return f_mount(&fs, "", 1) == FR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SDCard::AppendCO2(uint32_t timestamp,
|
bool SDCard::AppendCO2(uint32_t timestamp, uint16_t ppm)
|
||||||
uint16_t ppm)
|
|
||||||
{
|
{
|
||||||
FIL file;
|
FIL file;
|
||||||
|
|
||||||
if (f_open(
|
if (f_open(&file, "co2_log.csv", FA_OPEN_APPEND | FA_WRITE) != FR_OK)
|
||||||
&file,
|
|
||||||
"co2_log.csv",
|
|
||||||
FA_OPEN_APPEND | FA_WRITE) != FR_OK)
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
char buffer[64];
|
char buffer[64];
|
||||||
|
|
||||||
int len = snprintf(
|
int len = snprintf(buffer, sizeof(buffer), "%lu,%u\r\n", timestamp, ppm);
|
||||||
buffer,
|
|
||||||
sizeof(buffer),
|
|
||||||
"%lu,%u\r\n",
|
|
||||||
timestamp,
|
|
||||||
ppm);
|
|
||||||
|
|
||||||
UINT written;
|
UINT written;
|
||||||
|
|
||||||
f_write(
|
f_write( &file, buffer, len, &written);
|
||||||
&file,
|
|
||||||
buffer,
|
|
||||||
len,
|
|
||||||
&written);
|
|
||||||
|
|
||||||
f_close(&file);
|
f_close(&file);
|
||||||
|
|
||||||
|
|||||||
@@ -2,12 +2,20 @@
|
|||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#include "ff.h"
|
||||||
|
}
|
||||||
|
|
||||||
class SDCard
|
class SDCard
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
bool Init();
|
bool Init();
|
||||||
|
|
||||||
bool AppendCO2(uint32_t timestamp,
|
bool AppendCO2(uint32_t timestamp, uint16_t ppm);
|
||||||
uint16_t ppm);
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
FATFS fs;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -17,14 +17,8 @@ void CANTask_RunOnce()
|
|||||||
{
|
{
|
||||||
CO2Data data;
|
CO2Data data;
|
||||||
|
|
||||||
if(xQueueReceive(
|
if(xQueueReceive(g_canQueue, &data, pdMS_TO_TICKS(100)) == pdTRUE)
|
||||||
g_canQueue,
|
|
||||||
&data,
|
|
||||||
pdMS_TO_TICKS(100))
|
|
||||||
== pdTRUE)
|
|
||||||
{
|
{
|
||||||
canProtocol.sendCO2(
|
canProtocol.sendCO2(data.ppm);
|
||||||
data.ppm
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ extern UART_HandleTypeDef huart1;
|
|||||||
|
|
||||||
static MHZ19C sensor(&huart1);
|
static MHZ19C sensor(&huart1);
|
||||||
|
|
||||||
|
MHZ19C* g_mhz19c = &sensor;
|
||||||
|
|
||||||
void CO2Task_RunOnce()
|
void CO2Task_RunOnce()
|
||||||
{
|
{
|
||||||
CO2Data data;
|
CO2Data data;
|
||||||
|
|||||||
@@ -12,20 +12,11 @@ void SDTask_RunOnce()
|
|||||||
{
|
{
|
||||||
CO2Data data;
|
CO2Data data;
|
||||||
|
|
||||||
if(xQueueReceive(
|
if(xQueueReceive(g_sdQueue, &data, pdMS_TO_TICKS(100)) == pdTRUE)
|
||||||
g_sdQueue,
|
|
||||||
&data,
|
|
||||||
pdMS_TO_TICKS(100))
|
|
||||||
== pdTRUE)
|
|
||||||
{
|
{
|
||||||
char line[64];
|
char line[64];
|
||||||
|
|
||||||
sprintf(
|
sprintf(line, "CO2:%u t:%lu\r\n", data.ppm, data.timestamp);
|
||||||
line,
|
|
||||||
"CO2:%u t:%lu\r\n",
|
|
||||||
data.ppm,
|
|
||||||
data.timestamp
|
|
||||||
);
|
|
||||||
|
|
||||||
sd.write(line);
|
sd.write(line);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user