94 lines
2.3 KiB
C
94 lines
2.3 KiB
C
#include "main.h"
|
|
#include "cmsis_os.h"
|
|
#include "osu.h"
|
|
|
|
extern UART_HandleTypeDef huart3;
|
|
static uint8_t gausRcvBuf[100];
|
|
static int rx_packet_ready = 0;
|
|
|
|
static uint16_t CRC16(uint8_t *buf, int len)
|
|
{
|
|
uint16_t crc = 0xFFFF;
|
|
for(int pos=0; pos<len; pos++){
|
|
crc ^= (uint16_t)buf[pos];
|
|
for(int i=8; i!=0; i--){
|
|
if((crc & 0x0001) != 0){
|
|
crc >>= 1;
|
|
crc ^= 0xA001; // Modbus CRC多项?
|
|
}else{
|
|
crc >>= 1;
|
|
}
|
|
}
|
|
}
|
|
return crc;
|
|
}
|
|
|
|
|
|
static void osuDataProcess(void) {
|
|
if ((gausRcvBuf[0] != 0x10) || (gausRcvBuf[1] != 0x03) || (gausRcvBuf[2] != 0x04)) {
|
|
printf("osu packet header error, 0x%02X, 0x%02X, 0x%02X\r\n", gausRcvBuf[0], gausRcvBuf[1], gausRcvBuf[2]);
|
|
return;
|
|
}
|
|
|
|
uint16_t crc = CRC16(gausRcvBuf, 7);
|
|
if (crc != (gausRcvBuf[8] << 8 | gausRcvBuf[7])) {
|
|
printf("osu packet crc error, 0x%04X, %02X %02X\r\n", crc, gausRcvBuf[7], gausRcvBuf[8]);
|
|
return;
|
|
}
|
|
|
|
int level = gausRcvBuf[4];
|
|
int temp = gausRcvBuf[5] * 256 + gausRcvBuf[6];
|
|
printf("osu packet level, %d, temp, %.1f\r\n", level, temp / 10.0);
|
|
}
|
|
|
|
void HAL_UARTEx_RxEventCallback_UART3(UART_HandleTypeDef *huart, uint16_t Size)
|
|
{
|
|
if (huart->Instance == huart3.Instance) {
|
|
rx_packet_ready = 1;
|
|
}
|
|
}
|
|
|
|
static void osuThread(void *arg)
|
|
{
|
|
int iCnt = 0;
|
|
|
|
while (1) {
|
|
osDelay(10);
|
|
|
|
if (rx_packet_ready == 1) {
|
|
rx_packet_ready = 0;
|
|
osuDataProcess();
|
|
}
|
|
|
|
iCnt++;
|
|
|
|
if (iCnt < 500) {
|
|
continue;
|
|
}
|
|
iCnt = 0;
|
|
|
|
printf("send osu cmd\n");
|
|
HAL_UART_AbortReceive(&huart3);
|
|
HAL_UART_Transmit(&huart3, (uint8_t *)"\x10\x03\x00\x00\x00\x02\xC7\x4A", 8, 0xFFFF);
|
|
HAL_UARTEx_ReceiveToIdle_DMA(&huart3, (uint8_t *)gausRcvBuf, 9);
|
|
}
|
|
}
|
|
|
|
void osuInit(void)
|
|
{
|
|
// 创建任务
|
|
const osThreadAttr_t osu_task_attributes = {
|
|
.name = "osu_task",
|
|
.stack_size = 4096 * 2,
|
|
.priority = (osPriority_t) osPriorityNormal,
|
|
};
|
|
if (osThreadNew(osuThread, NULL, &osu_task_attributes) == NULL)
|
|
{
|
|
printf("failed to create osu_task\n");
|
|
}
|
|
else
|
|
{
|
|
printf("create osu_task success\n");
|
|
}
|
|
}
|