不含stm32 底层的代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
MyStm32Code/device/Src/temperature.c

407 lines
13 KiB

#include "temperature.h"
osThreadId_t TEMP_Handle;
const osThreadAttr_t TEMP_attributes = {
.name = "TEMP",
.stack_size = 1024,
.priority = (osPriority_t) osPriorityBelowNormal,
};
// 事件
osEventFlagsId_t TEMP_EventHandle;
const osEventFlagsAttr_t TEMP_Event_attributes = {
.name = "TEMP_Event"
};
extern UART_HandleTypeDef huart3;
UART_HandleTypeDef *pTEMPUart = &huart3;
uint8_t TEMP_TEST_BUF[8] ={0x01, 0x03, 0x00, 0x00, 0x00, 0x04, 0x44, 0x09};
uint8_t TEMP_TEST_BUF2[8] ={0x01, 0x03, 0x00, 0x00, 0x00, 0x04, 0x44, 0x09};
uint8_t TEMP_TRANS_BUF[8] ={0x01, 0x03, 0x00, 0x00, 0x00, 0x04, 0x44, 0x09};
uint8_t TEMP_RCV_BUF[14] ={0};
UART_HELPER_TypeDef *temp_uart_helper;
TEMP_TypeDef temp ={
TEMP_Init,
TEMP_Port,
TEMP_Test,
TEMP_Start,
TEMP_Stop,
NULL,
0, // state
0, // data_ok
{0}, // buf
0, // size_received
0, // time base
1000, // timeout ticks, 1000ms = 1S
0, // event_flag
NULL, // transRecord[3]
};
int TEMP_Init( )
{
temp_uart_helper = UART_HELPER_Init( );
if (temp_uart_helper ==NULL) return -1;
// TODO 接口
UART_HELPER_Set_Huart( temp_uart_helper, pTEMPUart );
UART_HELPER_Set_Interface_Type(temp_uart_helper, Uart_Interface_Max3160_485);
// 传输
UART_HELPER_Setup_Trans_mode( temp_uart_helper, Uart_Trans_DMA);
// UART_HELPER_Setup_Rcv_mode( temp_uart_helper, Uart_Trans_IT );
UART_HELPER_Set_enable_idle( temp_uart_helper, Uart_IDLE_IT_ENABLE);
// 回调GPIO 操作 数据操作
UART_HELPER_Set_trans_GPIO( temp_uart_helper, TEMP_Trans_GPIO ); // enbale rcv
UART_HELPER_Set_trans_cplt_GPIO( temp_uart_helper, TEMP_Trans_Cplt_GPIO );
UART_HELPER_Set_Callback( temp_uart_helper,&temp, TEMP_CallBack );
// 设置 Buf
UART_HELPER_Set_Rcv_Buf(temp_uart_helper, TEMP_RCV_BUF, sizeof(TEMP_RCV_BUF));
UART_HELPER_Set_Trans_Buf( temp_uart_helper, TEMP_TRANS_BUF, sizeof(TEMP_TRANS_BUF) );
// GPIO 操作
switch (temp_uart_helper->interface_type)
{
case Uart_Interface_Default:
break;
case Uart_Interface_485:
usart6_send_enable();
break;
case Uart_Interface_Max3160_232:
TEMP_MAX3160_232();
break;
case Uart_Interface_Max3160_485:
max3160_485_receive_mode();
break;
default:
break;
}
temp.state = TEMP_State_Waiting;
temp.timebase_ticks = osKernelGetTickCount( );
// TODO enable trans queue
// 最大队列数 8, 消息类型, 值传递(内含的buf为指针,如何包含大空间的buf,可选择指针传递 )
temp.transQueue = osMessageQueueNew( 8, sizeof(Record_Trans_Rcv_TypeDef), NULL);
}
void TEMP_Port( )
{
TEMP_Handle = osThreadNew( TEMP_Task, NULL, &TEMP_attributes );
// TEMP_EventHandle = osEventFlagsNew( &TEMP_Event_attributes );
}
int TEMP_Test( )
{
// 装载要发送的消息队列
temp.transRecord[0].size = sizeof(TEMP_TEST_BUF);
temp.transRecord[0].buf = TEMP_TEST_BUF;
// log_i( " temp.transRecord[0] buf %d, addr %d " , temp.transRecord[0].buf[0], temp.transRecord[0].buf );
// log_i( " TEMP_TEST_BUF buf %d, addr %d " ,TEMP_TEST_BUF[0], TEMP_TEST_BUF );
osMessageQueuePut( temp.transQueue, (void*)&temp.transRecord[0], 0, osWaitForever );
// 改变操作状态
temp.state = TEMP_State_Test; // 操作状态
return 0;
}
void TEMP_Start( )
{
// 装载要发送的消息队列 , 注意队列大小及
temp.transRecord[0].size = sizeof(TEMP_TEST_BUF);
temp.transRecord[0].buf = TEMP_TEST_BUF;
osMessageQueuePut( temp.transQueue, (void*)&temp.transRecord[0], 0, osWaitForever );
temp.transRecord[1].size = sizeof(TEMP_TEST_BUF2);
temp.transRecord[1].buf = TEMP_TEST_BUF2;
osMessageQueuePut( temp.transQueue, (void*)&temp.transRecord[1], 0, osWaitForever );
temp.state = TEMP_State_Get_DATA; // 操作状态
// osEventFlagsSet( TEMP_EventHandle, TEMP_Event_Get_Data ); // 事件
}
void TEMP_Stop( )
{
temp.state = TEMP_State_Stop;
// TODO stop task?
}
void TEMP_Task( )
{
// log_i( " ph tran mode : %d ", temp.->trans_mode );
// memset( TEMP_RCV_Buf, 0, 2*TEMP_Rcv_Buf_Size ); /* ph*3 ph *2 否则内存泄漏 */
temp.event_flag = 0;
uint64_t ticks;
int err_con = 0;
int st;
Record_Trans_Rcv_TypeDef msgQueue;
int count_msg;
for ( ; ; )
{
switch (temp.state)
{
case TEMP_State_Waiting:
log_i( " temp task..... : %d %d " , temp.state , temp.state );
osDelay(20);
break;
case TEMP_State_Test:
// log_i( " temp test..... : %d QueueGetCount %d " , temp.state , osMessageQueueGetCount(temp.transQueue) );
if (osMessageQueueGetCount(temp.transQueue) == 0){
temp.state = TEMP_State_Error;
break;
}
// 取队列 值传递, 第三个参数为NULL 否则为消息长度
osMessageQueueGet( temp.transQueue, &msgQueue, sizeof(Record_Trans_Rcv_TypeDef), osWaitForever );
// log_i( " msgQueue %d %d " , msgQueue.size, msgQueue.buf );
// log_i( " msgQueue %d " , TEMP_TEST_BUF );
temp.state = TEMP_State_Test_Start;
continue;
case TEMP_State_Test_Start:
// log_i( " temp TEMP_State_Test_Start..... : %d %d " , temp.state , temp.state );
UART_HELPER_Set_Trans_Buf( temp_uart_helper, msgQueue.buf, msgQueue.size );
TEMP_Receive();
TEMP_Transmit();
temp.state = TEMP_State_Testing;
continue;
case TEMP_State_Testing:
if ( osKernelGetTickCount() - ticks > 2000 )
{
err_con++;
if( err_con >3)
{
log_w( " TEMP testing ******** error " );
temp.state = TEMP_State_Error;
}
temp.state = TEMP_State_Test;
}
osDelay(1);
continue;
case TEMP_State_Test_OK:
log_i( " temp test ok..... : %d %d " , temp.state , temp.state );
// TODO 判断队列是否有元素
if (osMessageQueueGetCount(temp.transQueue) == 0)
{
temp.state = TEMP_State_Ready;
// TODO 停止DMA
if (temp_uart_helper->rcv_mode == Uart_Trans_DMA)
{
HAL_UART_DMAStop( temp_uart_helper->huart );
}
osEventFlagsSet(TEMP_EventHandle,TEMP_Event_Ready);
continue;
}else{
osMessageQueueGet( temp.transQueue, &msgQueue, sizeof(Record_Trans_Rcv_TypeDef), osWaitForever );
// osEventFlagsClear(TEMP_EventHandle,TEMP_Event_Test_OK);
temp.state = TEMP_State_Testing;
continue;
}
break;
case TEMP_State_Ready:
// log_i( " temp TEMP_State_Ready ..... " );
osDelay(1);
continue;
case TEMP_State_Get_DATA:
log_i( " temp TEMP_State_Get_DATA ..... " );
temp.data_ok = 0 ;
temp.size_received = 0 ;
memset( temp.result_buf, 0, sizeof(temp.result_buf) );
osEventFlagsClear(TEMP_EventHandle,TEMP_Event_Get_Data);
// 队列是否有值
if (osMessageQueueGetCount(temp.transQueue) == 0){
temp.state = TEMP_State_Error;
break;
}
osMessageQueueGet( temp.transQueue, &msgQueue, sizeof(Record_Trans_Rcv_TypeDef), osWaitForever );
UART_HELPER_Set_Trans_Buf( temp_uart_helper, msgQueue.buf, msgQueue.size );
TEMP_Receive();
TEMP_Transmit();
temp.state = TEMP_State_Get_DATA_OK;
continue;
case TEMP_State_Get_DATA_Wait:
if ( osKernelGetTickCount() - ticks > 2000 )
{
err_con++;
if( err_con >3)
{
log_w( " TEMP testing ******** error " );
temp.state = TEMP_State_Error;
}
temp.state = TEMP_State_Get_DATA;
}
osDelay(1);
continue;
case TEMP_State_Get_DATA_OK:
log_w( " TEMP data ok ******** " );
// TODO 停止DMA
// if (ph_uart_helper->rcv_mode == Uart_Trans_DMA)
// {
// HAL_UART_DMAStop( ph_uart_helper->huart );
// }
temp.data_ok = 0 ;
temp.state = TEMP_State_Get_DATA_Check ;
continue;
case TEMP_State_Get_DATA_Check:
log_w( " PH data check ******** " );
if(TEMP_Validate( )== 0)
{
temp.state = TEMP_State_Stop;
}else{
temp.state = TEMP_State_Error;
}
osDelay(10);
continue;
case TEMP_State_Stop:
osDelay(10);
continue;
case TEMP_State_Timeout:
log_e( " pH timeout ..... " );
temp.state = TEMP_State_Waiting ;
// TODO 停止DMA
if (temp_uart_helper->rcv_mode == Uart_Trans_DMA)
{
HAL_UART_DMAStop( temp_uart_helper->huart );
}
osDelay(5);
break;
case TEMP_State_Error:
log_e( " temp error ..... " );
temp.state = TEMP_State_Waiting ;
// TODO 停止DMA
if (temp_uart_helper->rcv_mode == Uart_Trans_DMA)
{
HAL_UART_DMAStop( temp_uart_helper->huart );
}
osDelay(5);
break;
}
osDelay(20);
}
}
void TEMP_Trans_GPIO(void)
{
// HAL_GPIO_WritePin(R_W_GPIO_Port,R_W_Pin, SET);
// HAL_GPIO_WritePin(NULL,0, SET);
HAL_GPIO_WritePin(SEL_232_485_GPIO_Port,SEL_232_485_Pin, SET);
HAL_GPIO_WritePin(HDPLX_GPIO_Port,HDPLX_Pin, RESET);
HAL_GPIO_WritePin(DE485_GPIO_Port, DE485_Pin, SET);
}
void TEMP_Trans_Cplt_GPIO(void)
{
// HAL_GPIO_WritePin(R_W_GPIO_Port,R_W_Pin, RESET);
// HAL_GPIO_WritePin(NULL,0, RESET);
HAL_GPIO_WritePin(SEL_232_485_GPIO_Port,SEL_232_485_Pin, SET);
HAL_GPIO_WritePin(HDPLX_GPIO_Port,HDPLX_Pin, SET);
HAL_GPIO_WritePin(DE485_GPIO_Port, DE485_Pin, RESET);
}
int TEMP_Transmit()
{
// TODO 结合队列
temp.size_received =0;
if( temp_uart_helper->transferring == 0)
{
return UART_HELPER_Trans(temp_uart_helper, temp_uart_helper->trans_record->buf, temp_uart_helper->trans_record->size );
}
return 0;
}
int TEMP_Receive()
{
return UART_HELPER_Start_Rcv(temp_uart_helper, temp_uart_helper->rcv_buf, temp_uart_helper->rcv_size);
// return 0;
}
void TEMP_Set_Timeout_ms( uint64_t ms_ticks )
{
temp.timeout_ticks = ms_ticks;
}
int TEMP_Validate( )
{
return 0;
}
int TEMP_CallBack( TEMP_TypeDef *pPH, uint8_t *buf, uint16_t size )
{
log_i( " TEMP_CallBack -- state %d " , temp.state);
uint16_t size_tmp;
size_tmp =size;
switch (temp.state)
{
case TEMP_State_Testing:
if (size>0)
{
temp.state++;
}
break;
case TEMP_State_Get_DATA_Wait:
if (size == 0) return 0;
if ((size + temp.size_received) >= sizeof(temp.result_buf) )
{
size_tmp = sizeof(temp.result_buf) - temp.size_received;
}
memcpy( (uint32_t)(temp.result_buf+temp.size_received), temp_uart_helper->rcv_buf,size_tmp);
temp.size_received += size_tmp;
if ( temp_uart_helper->enable_idle_it == 0 )
{
// Modbus 长度校验,拷贝数据?
if( temp.size_received == ( temp.result_buf[2]+ 5 ) )
{
temp.state++;
}
return 0;
}
if ( temp_uart_helper->enable_idle_it == 1 )
{
// 长度校验,拷贝数据?
if( temp.size_received == ( temp.result_buf[2]+ 5 ) )
{
temp.state++;
}
return 0;
}
break;
default:
break;
}
return 0;
}
// void TEMP_Set_Interface( TEMP_TypeDef *ph, UartInterface_TypeDef * interface )
// {
// UartHelper_Set_Interface( temp.uarthelper, interface );
// }
// void TEMP_Set_Huart( TEMP_TypeDef *ph, UART_HandleTypeDef * huart )
// {
// temp.uarthelper->huart = huart;
// // UartHelper_Set_Huart( temp.uarthelper, huart );
// }
// void TEMP_Set_Interface_Type( TEMP_TypeDef *ph, Uart_Interface_Type_Typedef interface_type )
// {
// temp.interface_type = interface_type;
// temp.uarthelper->interface_type = interface_type;
// UartInterface_Setup_Interface_type( temp.uarthelper->interface ,temp.interface_type);
// }