//////////////////////////////////////////////////////////////////////////////// /// @file uart.c /// @author AE TEAM /// @brief THIS FILE PROVIDES ALL THE SYSTEM FUNCTIONS. //////////////////////////////////////////////////////////////////////////////// /// @attention /// /// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE /// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE /// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR /// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH /// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN /// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS. /// ///

© COPYRIGHT MINDMOTION

//////////////////////////////////////////////////////////////////////////////// // Define to prevent recursive inclusion #define _UART_C_ // Files includes #include #include "uart.h" #include "hal_conf.h" #include "public.h" #include "CRC.h" #include "sim_eeprom.h" #define Item_Number "C2C5" // 项目编号 #define Software_version "N_C2C5_V1_3" // 软件版本 #define Hardware_version "N_C2C5_V1_3" // 硬件版本 //u8 message_0[] = {0x00,0x01,0x10,0x58,0x59,0x4C,0x4C,0x30,0x31,0x31,0x32,0x78,0x78,0x78,0x78,0x78,0x78,0x78,0x78,0xCB,0x7E}; //u8 message_1[] = {0x00,0x01,0x10,0x58,0x59,0x4C,0x4C,0x30,0x31,0x31,0x32,0x78,0x78,0x78,0x78,0x78,0x78,0x78,0x78,0x04,0x01,0x32,0x05,0x05,0x4C,0x4C,0x30,0x31 // ,0x31,0x06,0x14,0x59,0x41,0x32,0x30,0x30,0x35,0x32,0x38,0x48,0x34,0x35,0x30,0x34,0x32,0x41,0x78,0x78,0x78,0x78,0x78,0x7D,0x5D,0x7E}; typedef enum receive_conditon { HEAD_1, HEAD_2, MODE, ADDRESS, RECEIVE_NUM, RECEIVE_VALUE, END_0, END_1, }R_condtion; R_condtion m_receive; typedef struct _get_ { u16 count; u8 receive_num; char num_count; u8 add; u8 spec_flag; u8 receive_arr[256]; }Get_Value; Get_Value m_get_value; typedef union By16 { u16 byte_16; u8 arr[2]; }Byte_16; //////////////////////////////////////////////////////////////////////////////// /// @addtogroup MM32_Hardware_Abstract_Layer /// @{ //////////////////////////////////////////////////////////////////////////////// /// @addtogroup UART /// @{ //////////////////////////////////////////////////////////////////////////////// /// @addtogroup UART_Exported_Functions /// @{ #ifdef __GNUC__ #define PUTCHAR_PROTOTYPE s32 __io_putchar(s32 ch) #else #define PUTCHAR_PROTOTYPE s32 fputc(s32 ch, FILE *f) #endif #ifdef USE_IAR PUTCHAR_PROTOTYPE { while((UART1->CSR & UART_IT_TXIEN) == 0); //The loop is sent until it is finished UART1->TDR = (ch & (u16)0x00FF); return ch; } #else void _sys_exit(s32 x) { x = x; } //redefine fputcfunction //s32 fputc(s32 ch, FILE* f) //{ // while((UART1->CSR & UART_IT_TXIEN) == 0); //The loop is sent until it is finished // UART1->TDR = (ch & (u16)0x00FF); // return ch; //} #endif u16 get_short(u8 Value_H,u8 Value_L) { u16 value = 0; u16 V_1 =Value_H; u16 V_2 = Value_L; value = ((V_1 << 8 )&0xFF00)| V_2; return value; } //////////////////////////////////////////////////////////////////////////////// /// @brief UART send byte. /// @note None. /// @param dat(A byte data). /// @retval None. //////////////////////////////////////////////////////////////////////////////// void UART1_Send_Byte(u8 dat) { UART_SendData(UART1, dat); while(!UART_GetFlagStatus(UART1, UART_FLAG_TXEPT)); } //////////////////////////////////////////////////////////////////////////////// /// @brief UART send byte. /// @note None. /// @param buf:buffer address. /// @param len:data length. /// @retval None. //////////////////////////////////////////////////////////////////////////////// void UART1_Send_Group(u8* buf, u16 len) { WWDG_SetCounter(0x7e); while(len--) UART1_Send_Byte(*buf++); } void send_buffer(u8* buf, u16 len) { WWDG_SetCounter(0x7e); while(len--) UART1_Send_Byte(*buf++); } void send_temp( u16 time, u16 temp, u16 temp_1 ) { u8 arr[64] = {0}; u16 crc = 0; arr[0] = 0xAA; arr[1] = 0xFB; arr[2] = 0x02; arr[3] = 0x06; arr[4] = 0x06; arr[5] = ((time& 0xFF00) >> 8); arr[6] = time&0x00FF; arr[7] = ((temp& 0xFF00) >> 8); arr[8] = temp&0x00FF; arr[9] = ((temp_1& 0xFF00) >> 8); arr[10] = temp_1&0x00FF; crc = CRC16(arr,11); arr[11] = ((crc & 0xFF00) >> 8); arr[12] = crc & 0x00FF; UART1_Send_Group( arr,13 ); } void send_ack(u8 value) { u8 arr[128] = {0}; u16 crc = 0; arr[0] = 0xAA; arr[1] = 0xFB; arr[2] = 0x02; arr[3] = 0x05; arr[4] = 0x02; arr[5] = ((value& 0xFF00) >> 8); arr[6] = value&0x00FF; crc = CRC16(arr,7); arr[7] = ((crc & 0xFF00) >> 8); arr[8] = crc & 0x00FF; UART1_Send_Group( arr,9 ); } void send_TCR(u16 value) { u8 arr[128] = {0}; u16 crc = 0; arr[0] = 0xAA; arr[1] = 0xFB; arr[2] = 0x02; arr[3] = 0x01; arr[4] = 0x02; arr[5] = ((value& 0xFF00) >> 8); arr[6] = value&0x00FF; crc = CRC16(arr,7); arr[7] = ((crc & 0xFF00) >> 8); arr[8] = crc & 0x00FF; UART1_Send_Group( arr,9 ); } void send_TCR_value(u16 value, u16 low_r, u16 low_temp) { u8 arr[13] = {0}; u16 crc = 0; arr[0] = 0xAA; arr[1] = 0xFB; arr[2] = 0x01; arr[3] = 0xA7; arr[4] = 0x02; arr[5] = ((value& 0xFF00) >> 8); arr[6] = value&0x00FF; arr[7] = ((low_r& 0xFF00) >> 8); arr[8] = low_r&0x00FF; arr[9] = ((low_temp& 0xFF00) >> 8); arr[10] = low_temp&0x00FF; crc = CRC16(arr,11); arr[11] = ((crc & 0xFF00) >> 8); arr[12] = crc & 0x00FF; UART1_Send_Group( arr,13 ); } void send_now_time_value(u16 time,u16 value ,u16 r_value) { u8 arr[13] = {0}; u16 crc = 0; arr[0] = 0xAA; arr[1] = 0xFB; arr[2] = 0x01; arr[3] = 0xA8; arr[4] =0x03; arr[5] = ((value& 0xFF00) >> 8); arr[6] = value&0x00FF; arr[7] = ((time& 0xFF00) >> 8); arr[8] = time&0x00FF; arr[9] = ((r_value& 0xFF00) >> 8); arr[10] = r_value&0x00FF; crc = CRC16(arr,11); arr[11] = ((crc & 0xFF00) >> 8); arr[12] = crc & 0x00FF; UART1_Send_Group( arr,13 ); } void send_TCR_result(u16 value) { u8 arr[128] = {0}; u16 crc = 0; arr[0] = 0xAA; arr[1] = 0xFB; arr[2] = 0x01; arr[3] = 0xA6; arr[4] = 0x02; arr[5] = ((value& 0xFF00) >> 8); arr[6] = value&0x00FF; crc = CRC16(arr,7); arr[7] = ((crc & 0xFF00) >> 8); arr[8] = crc & 0x00FF; UART1_Send_Group( arr,9 ); } void send_Set_Value() { unsigned char count = 0 ; unsigned char i = 0; unsigned char send_buff[128] = {0}; unsigned short crc_value; send_buff[0] = 0xAA; send_buff[1] = 0xFB; send_buff[2] = 0x01; send_buff[3] = 0xA5; send_buff[4] = g_work.m_set_other_hot.set_up_num * 4; // 一个时间一个温度总共4个字节 memcpy(&send_buff[5],g_work.m_set_other_hot.m_set_up,send_buff[4]); crc_value = CRC16(send_buff,send_buff[4] + 5); send_buff[send_buff[4] + 5] = crc_value >> 8; send_buff[send_buff[4] + 6] = crc_value ; UART1_Send_Group( send_buff,send_buff[4] + 7 ); } void send_Version() { unsigned char count = 0 ; unsigned char i = 0; unsigned char send_buff[128] = {0}; unsigned short crc_value; char *P_item = NULL; send_buff[0] = 0xAA; send_buff[1] = 0xFB; send_buff[2] = 0x01; send_buff[3] = 0xA1; send_buff[4] = strlen(Software_version); // 一个时间一个温度总共4个字节 P_item = (char *) &Software_version; memcpy(&send_buff[5],P_item,send_buff[4] ); crc_value = CRC16(send_buff,send_buff[4] + 5); send_buff[send_buff[4] + 5] = crc_value >> 8; send_buff[send_buff[4] + 6] = crc_value ; UART1_Send_Group( send_buff,send_buff[4] + 7 ); } void analysis() { u16 nor_temp_set = 0; if( g_work.m_work_flag._get_uart_receive_flag == 1 ) { if( g_work.m_time._uart_receive_time > 1000)//超时设置500ms { g_work.m_work_flag._get_uart_receive_flag = 0; m_get_value.count = 0; m_receive = HEAD_1; } } // g_work.m_work_flag._get_uart_scuess = 1; if( g_work.m_work_flag._get_uart_scuess == 1 ) { switch( m_get_value.receive_arr[2] ) { case 0x03: // 匹配 if( g_work.work_conditon == STANDY ) { if( g_work.m_work_flag._match_scuess == 0 ) { g_work.m_work_flag._match_scuess = 1; } } g_work.m_time._Uart_match_time = 0; break; case 0x01: // 写数据 switch (m_get_value.receive_arr[3]) // 地址 { case 0x00: // 进入boot程序 g_work.m_time._Uart_match_time = 0; g_work.m_work_flag._Uart_match = RESET_MATH; UART_ITConfig(UART1, UART_IT_RXIEN, DISABLE); g_work.m_time._Sleep_time = 0; /* code */ break; case 0x01: // 常温标定温度 g_work.m_time._Uart_match_time = 0; if( m_get_value.receive_arr[4] == 1 ) { if( m_get_value.receive_arr[6] < 40 && m_get_value.receive_arr[6] > 10 ) { g_work.m_set_other_hot.normal_T_Value = m_get_value.receive_arr[6]; g_work.m_work_flag._nor_stand_flag = 1; g_work.m_work_flag._temp_open_flag = 1; //send_ack(2); // 开始标定标定标志 } } else if( m_get_value.receive_arr[4] == 2) { // AA FB 01 01 02 00 19 58 B7 nor_temp_set = get_short(m_get_value.receive_arr[5],m_get_value.receive_arr[6]); if( nor_temp_set < 40 && nor_temp_set > 10 ) { g_work.m_set_other_hot.normal_T_Value = nor_temp_set; g_work.m_work_flag._nor_stand_flag = 1; g_work.m_work_flag._temp_open_flag = 1; } } else { return; } break; case 0x02:// 上端加热时间 g_work.m_time._Uart_match_time = 0; if( m_get_value.receive_arr[4] == 2 ) { if ( get_short(m_get_value.receive_arr[5],m_get_value.receive_arr[6]) == 1 ) { g_work.work_conditon = HOT_2; // printf("\nHOT_1\n"); } else if ( get_short(m_get_value.receive_arr[5],m_get_value.receive_arr[6]) == 0 ) { g_work.work_conditon = STANDY; if( g_work.m_work_flag._high_stand_flag == 1 ) { //printf("write"); flash_wite(); g_work.m_work_flag._high_stand_flag = 0; } } } break; case 0x03: // 高温标定 if( m_get_value.receive_arr[4] == 2 ) { g_work.m_set_other_hot.TCR_vlaue = get_short(m_get_value.receive_arr[5],m_get_value.receive_arr[6]); g_work.m_work_flag._high_stand_flag = 1; send_TCR_result(0); //flash_wite(); } else { send_TCR_result(1); } break; case 0x05:// 下端加热温度 设置 break; case 0x06: // 总时间 g_work.m_time._Uart_match_time = 0; g_work.m_set_other_hot.set_all_time = get_short(m_get_value.receive_arr[5],m_get_value.receive_arr[6]); send_ack(1); break; case 0x07: // 设置 预加热 时间 g_work.m_time._Uart_match_time = 0; g_work.m_set_other_hot.set_order_time = get_short(m_get_value.receive_arr[5],m_get_value.receive_arr[6] ); send_ack(3); break; case 0x08: // 设置 预加热 温度 g_work.m_time._Uart_match_time = 0; //g_work.m_set_other_hot.set_order_temp = get_short(m_get_value.receive_arr[5],m_get_value.receive_arr[6] ); send_ack(2); break; case 0x09: // 设置上段加热数值 g_work.m_time._Uart_match_time = 0; if( m_get_value.receive_arr[4]%4 == 0 ) // 收到的数值是四的倍数 两个short 类型 一个表示时间 一个表示 温度 { g_work.m_set_other_hot.set_up_num = m_get_value.receive_arr[4]/4; if( g_work.m_set_other_hot.set_up_num < 28 ) // 接收个数小于最大值 { for( int i = 0 ; i < g_work.m_set_other_hot.set_up_num; i ++ ) { //0 g_work.m_set_other_hot.m_set_up[i]._time = get_short(m_get_value.receive_arr[5 + i * 4],m_get_value.receive_arr[6 + i * 4]); // printf("up_time: %d",g_work.m_set_other_hot.m_set_up[i]._time); g_work.m_set_other_hot.m_set_up[i]._temp = get_short(m_get_value.receive_arr[7 + i * 4],m_get_value.receive_arr[8 + i * 4]); //printf("up_temp: %d",g_work.m_set_other_hot.m_set_up[i]._temp); } } } send_ack(4); flash_wite(); break; case 0x0A: // 设置下段加热数值 send_ack(5); flash_wite(); break; break; case 0xA1: // 版本号 send_Version(); break; case 0xA5: // 读取数据 send_Set_Value(); break; case 0xAA: // 开始实时检测 g_work.real_time_flag = 1; break; case 0xAB: // 读取标定数据 g_work.check_info = 1; break; case 0xB1: break; /////////////////////预留出来用以给上位机补偿NTC或者解析要保存的图片 default: break; } break; } g_work.m_work_flag._get_uart_scuess = 0; } } void get_char( u8 receive ) { u16 CRC_16_byte = 0; g_work.m_work_flag._get_uart_receive_flag = 1; g_work.m_time._uart_receive_time = 0; if( m_get_value.count > 250 ) { m_get_value.count = 0; m_receive = HEAD_1; } switch ( m_receive) { case HEAD_1: if( receive == 0xAA ) { m_receive = HEAD_2; m_get_value.receive_arr[m_get_value.count++] = receive; m_get_value.num_count = 0; } if(receive == 0xBB) { //LDO_OFF(); //强制关机 } /* code */ break; case HEAD_2: if( receive == 0xFB ) { m_get_value.receive_arr[m_get_value.count++] = receive; m_receive = MODE; } break; case MODE: // 模式 0x01 // 写 0x00 // 读 0x03 匹配 if( receive == 0x01 ) { m_receive = ADDRESS; } else if( receive == 0x03 ) { m_receive = END_0; } m_get_value.receive_arr[m_get_value.count++] = receive; break; case ADDRESS: m_get_value.receive_arr[m_get_value.count++] = receive; m_receive = RECEIVE_NUM; break; case RECEIVE_NUM: m_get_value.receive_num = receive; m_get_value.receive_arr[m_get_value.count++] = receive; m_receive =RECEIVE_VALUE; break; case RECEIVE_VALUE: if( ++ m_get_value.num_count >= m_get_value.receive_num ) { m_receive = END_0; } m_get_value.receive_arr[m_get_value.count++] = receive; break; case END_0: m_get_value.receive_arr[m_get_value.count++] = receive; m_receive = END_1; break; case END_1: m_get_value.receive_arr[m_get_value.count++] = receive; // if( 0 == CRC16( m_get_value.receive_arr,m_get_value.count) ) { g_work.m_work_flag._get_uart_receive_flag = 0; g_work.m_work_flag._get_uart_scuess = 1; m_receive = HEAD_1; m_get_value.count = 0; } if( m_get_value.count > 250 ) { m_get_value.count = 0; m_receive = HEAD_1; } break; default: break; } } void CONSOLE_Init(u32 baudrate) { //GPIO port set GPIO_InitTypeDef GPIO_InitStruct; UART_InitTypeDef UART_InitStruct; NVIC_InitTypeDef NVIC_InitStruct; RCC_APB2PeriphClockCmd(RCC_APB2ENR_UART1, ENABLE); //enableUART1,GPIOAclock RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOB, ENABLE); // //UART initialset GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_3); GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_3); UART_StructInit(&UART_InitStruct); UART_InitStruct.UART_BaudRate = baudrate; UART_InitStruct.UART_WordLength = UART_WordLength_8b; UART_InitStruct.UART_StopBits = UART_StopBits_1;//one stopbit UART_InitStruct.UART_Parity = UART_Parity_No;//none odd-even verify bit UART_InitStruct.UART_HardwareFlowControl = UART_HardwareFlowControl_None;//No hardware flow control UART_InitStruct.UART_Mode = UART_Mode_Rx | UART_Mode_Tx; // receive and sent mode UART_Init(UART1, &UART_InitStruct); //initial uart 1 UART_Cmd(UART1, ENABLE); //enable uart 1 //UART1_TX GPIOB.3 GPIO_StructInit(&GPIO_InitStruct); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOB, &GPIO_InitStruct); //UART1_RX GPIOB.4 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOB, &GPIO_InitStruct); //UART1 NVIC NVIC_InitStruct.NVIC_IRQChannel = UART1_IRQn; NVIC_InitStruct.NVIC_IRQChannelPriority = 3; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruct); UART_ITConfig(UART1, UART_IT_RXIEN, ENABLE); } //////////////////////////////////////////////////////////////////////////////// /// @brief Serial port Interrupt Handler /// @note /// @param None. /// @retval None. //////////////////////////////////////////////////////////////////////////////// void UART1_IRQHandler(void) { u8 recvbyte; // Recv packet if (UART_GetITStatus(UART1, UART_ISR_RX) != RESET) { recvbyte = UART_ReceiveData(UART1); if( g_work.m_work_flag._get_uart_scuess == 0 ) get_char(recvbyte); UART_ClearITPendingBit(UART1, UART_ISR_RX); } } /// @} /// @} /// @}