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