/** * @UART uart loopback transfer demo * @param none * @return none */ void UartLBModeDemo(void) { UINT8 recbuf[0x10]; UINT8 Writebuf[0x10]; UINT8 i; for(i = 0; i < 0x10; i++) { recbuf[i] = i; Writebuf[i] = 0; } UartInit(); UartLBEn(); UartSend(recbuf, 10); UartReceive(Writebuf, 10); UartInit(); UartLBDis(); for(i = 0; i < 10; i++) { if(Writebuf[i] != i) { printf("UartLoopBack Error!\r\n"); } else { printf("UartLoopBack Ok!\r\n"); } } }
static void UartCbf(uint8_t byte) { if (byte != 0) { LedToggle(&led_green); byte = byte + 1; (void) UartSend(&byte, 1); } }
/******************************************************************************* * Function Name : ModbusHandle * Description : Modbus从机处理函数 ,在主函数中收到一帧数据后被调用 * Input : * Output : * Return : 0 无错误 1帧地址错 2校验错 3读数据命令错 *******************************************************************************/ uint8_t ModbusHandle(unsigned char* RevBuf,uint32_t RevLen) { uint32_t i; uint32_t len; uint8_t SendBuf[64] = {0x00}; uint16_t CRC16[1]; /*判断帧地址 */ if(RevBuf[0] == ModbusAddr) { } else { return 1; } /*CRC计算*/ CalCRC(RevBuf,RevLen-2,CRC16); if(*(uint16_t *)(CRC16)!=*(uint16_t *)(RevBuf+RevLen-2)) { return 2; /*("校验错误");*/ } /*判断读数据功能码 0x03 */ if(RevBuf[1] == 0x03) { } else { return 3; } len = RevBuf[4]*256 + RevBuf[5]; len = len * 2; //字节数要*2 /*填充发送字节 表计地址 功能码 长度高 长度低 数据1 数据2 数据3 数据4 ... CRC高 CRC低 */ { SendBuf[0] = ModbusAddr; SendBuf[1] = 0x03;//功能码 SendBuf[2] = len;//长度 for(i=0; i<len; i++) { SendBuf[i+3] = modbusdata[i]; } /*CRC计算*/ CalCRC(SendBuf,len+3,(uint16_t *)&SendBuf[len+3]); } /*发送数据*/ UartSend(SendBuf, len+5); return 0; }
void SendDebugInfo(unsigned int debug_info) { for(unsigned char debug_info_counter = 0; debug_info_counter < 5; debug_info_counter++) { send_info[debug_info_counter] = debug_info % 10; debug_info = ( debug_info - send_info[debug_info_counter]) / 10; } for(signed char debug_send_counter = 4; debug_send_counter >= 0; debug_send_counter--) { unsigned char send = send_info[debug_send_counter]; UartSendAscii(send); } UartSend(';'); }
/** * @UART uart transfer whith pc demo * @param none * @return none */ void UartSendReviceDemo(void) { UINT8 buf[0x10]; UartInit(); printf("Uart test start\r\nRreceive data:\r\n"); UartReceive(buf, 10); printf("\r\nSend data:\r\n"); UartSend(buf, 10); printf("\r\n"); }
static void UartTransmitNumber(uint16_t num) { uint8_t buf[5]; buf[0]='0'; //+ num/1000; buf[1]='2'; //+ ((num/100) % 10); buf[2]='5'; //+ ((num/10) % 10); buf[3]='6';// + ((num) % 10); buf[4]=' '; if (UartSend(buf,1) == false) { LedToggle(&led_green); } }
/* ********************************************************************************************************* * 函 数 名: comSendBuf * 功能说明: 向串口发送一组数据。数据放到发送缓冲区后立即返回,由中断服务程序在后台完成发送 * 形 参: _ucPort: 端口号(COM1 - COM6) * _ucaBuf: 待发送的数据缓冲区 * _usLen : 数据长度 * 返 回 值: 无 ********************************************************************************************************* */ void comSendBuf(COM_PORT_E _ucPort, uint8_t *_ucaBuf, uint16_t _usLen) { UART_T *pUart; pUart = ComToUart(_ucPort); if (pUart == 0) { return; } if (pUart->SendBefor != 0) { pUart->SendBefor(); /* 如果是RS485通信,可以在这个函数中将RS485设置为发送模式 */ } UartSend(pUart, _ucaBuf, _usLen); }
//发送串口通讯数据包 static inline void CommSend(BYTE size) { WORD sum; BYTE i; UartSend(0x46); UartSend(0xb9); UartSend(0x6a); UartSend(0x00); sum = size + 6 + 0x6a; UartSend(size + 6); serialport_write(handle_download_port,TxBuffer,size); for (i=0; i<size; i++) { sum += TxBuffer[i]; } UartSend(HIBYTE(sum)); UartSend(LOBYTE(sum)); UartSend(0x16); CommInit(); }
/******************************************************************************* * Function Name : T188Handle * Description : T188从机处理函数 ,在主函数中收到一帧数据后被调用 * Input : * Output : * Return : 0 无错误 1帧起始错 2校验错 3读数据命令错 *******************************************************************************/ uint8_t T188Handle(unsigned char* RevBuf,uint32_t RevLen) { uint32_t i; unsigned char BcdData[4]={0x00}; unsigned char SendBuf[64] = {0x00}; /*删除前导字符 0xFE */ while(RevBuf[0] == 0xFE) { RevLen = RevLen - 1; for(i=0 ;i<RevLen ; i++) { RevBuf[i] = RevBuf[i+1]; } } /*判断帧开始标志 0x68 */ if(RevBuf[0] == 0x68) { } else { return 1; } /*判断读数据标志 0x01 */ if(RevBuf[9] == 0x01) { } else { return 3; } /*判断校验位是否正确 */ if(RevBuf[14] == CalSum(RevBuf, 14)) { } else { return 2; } /*填充发送字节 帧开始 表计类型 表计地址 控制码 长度 数据标识 序列号 累计流量 状态0 状态1 校验和 帧结束 0x68 0x10 A0-A6 0x81 0x09 0x901F 0x00 D0-D3 0x00 0xFF CRC 0x16 */ { SendBuf[0] = 0x68; SendBuf[1] = 0x10;//类型 SendBuf[2] = A0;//地址 SendBuf[3] = A1; SendBuf[4] = A2; SendBuf[5] = A3; SendBuf[6] = A4; SendBuf[7] = A5; SendBuf[8] = A6; SendBuf[9] = 0x81; //控制码 SendBuf[10] = 0x09; //长度 SendBuf[11] = 0x90; //数据标识 SendBuf[12] = 0x1F; SendBuf[13] = 0x00; //序号 /*将数据转换成BCD码*/ Dec2BCD(data, BcdData, 4); SendBuf[14] = BcdData[3]; //流量 低位在前 SendBuf[15] = BcdData[2]; SendBuf[16] = BcdData[1]; SendBuf[17] = BcdData[0]; SendBuf[18] = 0x00; //状态0 SendBuf[19] = 0xFF; //状态1 SendBuf[20] = CalSum(SendBuf,20); //CRC SendBuf[21] = 0x68; } /*发送数据*/ UartSend(SendBuf, 22); return 0; }
static void UartSendByte(uint8_t byte) { UartSend(&byte, 1); }
static uint32_t Send_Byte(uint8_t c) { UartSend(c); return 0; }
void UartSendError(void) { UartSend(0x15); /* NAK, error while receiving such as incorrect * number of cc etc; data needs to be resent */ }
/*--------------------------------------------------------------*/ void UartSendOverflow(void) { UartSend(0x11); // Device control 1; Used to indicate Overflow }
/* * fill INS_delay_buffer * wait GPS signal */ void vINSAligTask(void* pvParameters) { char printf_buffer[100]; /*odometry sensor data*/ float direction; u8 temp; float *p_insBuffer; GPSDataType gdt; u16 GPS_validate_cnt=0; float uw_height; portBASE_TYPE xstatus; /*Enable ultrasonic sensor TIMER*/ TIM2_Config(); TIM2_IT_Config(); /**/ xQueueReceive(AHRSToINSQueue,&p_insBuffer,portMAX_DELAY); //capture an INS frame p_insBuffer[INDEX_DT]=0.0; //the last number in buffer represent time interval, not time Blinks(LED1,2); #ifdef INS_DEBUG /*GPS data is not needed in debug mode*/ while(1) { /*receive ins data and fill the IMU_delay_buffer*/ xQueueReceive(AHRSToINSQueue,&p_insBuffer,portMAX_DELAY); PutToBuffer(p_insBuffer); /*clear time interval*/ p_insBuffer[INDEX_DT]=0.0; /*INS_delay_buffer is full filled*/ if(buffer_header == 0) break; } navParamK[0] = 0.0; navParamK[1] = 0.0; navParamK[2] = 0.0; navParamK[3] = 0.0; navParamK[4] = 0.0; navParamK[5] = 0.0; navParamK[6] = 0.0; navParamK[7] = 0.0; navParamK[8] = 0.0; x[0]=0.0; x[1]=0.0; x[2]=0.0; x[3]=0.0; x[4]=0.0; x[5]=0.0; x[6]=0.0; x[7]=0.0; x[8]=0.0; #else //normol mode /*wait while GPS signal not stable*/ while(GPS_validate_cnt<=100) { xstatus = xQueueReceive(xUartGPSQueue,&gdt,0); if(xstatus == pdPASS) { GPS_validate_cnt ++; } if(GetUltraSonicMeasure(&uw_height)) { sprintf(printf_buffer,"%1.3f\r\n",uw_height); UartSend(printf_buffer,7); } /*receive ins data and fill the IMU_delay_buffer*/ xQueueReceive(AHRSToINSQueue,&p_insBuffer,portMAX_DELAY); PutToBuffer(p_insBuffer); /*clear time interval*/ p_insBuffer[INDEX_DT]=0.0; } /************initialize navParamK*********************/ temp=(u8)(gdt.Lati*0.01); initPos[0]=0.01745329*(temp+(gdt.Lati-temp*100.0)*0.0166667); temp=(u8)(gdt.Long*0.01); initPos[1]=0.01745329*(temp+(gdt.Long-temp*100.0)*0.0166667); initPos[2]=gdt.Alti; if(gdt.type != GPGMV) { direction = gdt.COG*0.0174533; gdt.speedN = gdt.SPD*0.51444*arm_cos_f32(direction); gdt.speedE = gdt.SPD*0.51444*arm_sin_f32(direction); } navParamK[0] = 0.0; navParamK[1] = 0.0; navParamK[2] = 0.0; navParamK[3] = gdt.speedN; navParamK[4] = gdt.speedE; navParamK[5] = 0.0; navParamK[6] = 0.0; navParamK[7] = 0.0; navParamK[8] = 0.0; /*initialize filter state param x*/ x[0]=0.0; x[1]=0.0; x[2]=0.0; x[3]=0.0; x[4]=0.0; x[5]=0.0; x[6]=0.0; x[7]=0.0; x[8]=0.0; #endif xstatus=xTaskCreate(vIEKFProcessTask,(signed portCHAR *)"ins_ekf",configMINIMAL_STACK_SIZE+1024,(void *)NULL,tskIDLE_PRIORITY+1,NULL); if(xstatus!=pdTRUE) { sprintf(printf_buffer, "failed to initialize\r\n"); UartSend(printf_buffer, strlen(printf_buffer)); } vTaskDelete(NULL); }
/**************************************************************************** * * NAME : EdenProtocolSend * * INPUT : Data buffer, data length, transaction id, reply/solicited message * * OUTPUT : EDEN_PROTOCOL_STATUS. * * DESCRIPTION : build a message (header + footer) and send it by the UART. * ****************************************************************************/ EDEN_PROTOCOL_STATUS OHDBEdenProtocolSend(BYTE *Data, WORD DataLength, BYTE DestId ,BYTE Transaction, BOOL Reply) { BYTE xdata BufIndex, i, CheckSum = 0, TransactionId; if (Reply) TransactionId = Transaction; else TransactionId = EdenProtocolTransactionId; // check if message length > 30 // ---------------------------- if (DataLength > EDEN_PROTOCOL_MAX_MSG_LENGTH) return EDEN_PROTOCOL_MSG_TOO_LONG; // build the msg header // -------------------- EdenProtocolBuffer[MSG_SYNC_1_INDEX] = MSG_SYNC_1; EdenProtocolBuffer[MSG_SYNC_2_INDEX] = MSG_SYNC_2; // insert the source id, destination id and transaction id to the buffer // add filler un case one of them equals to the header byte (0x55) // --------------------------------------------------------------- BufIndex = SOURCE_ID_INDEX; EdenProtocolBuffer[BufIndex++] = OHDB_SOURCE_ID; if (OHDB_SOURCE_ID == MSG_SYNC_1) EdenProtocolBuffer[BufIndex++] = MSG_FILLER; EdenProtocolBuffer[BufIndex++] = DestId; if (DestId == MSG_SYNC_1) EdenProtocolBuffer[BufIndex++] = MSG_FILLER; EdenProtocolBuffer[BufIndex++] = TransactionId; if (TransactionId == MSG_SYNC_1) EdenProtocolBuffer[BufIndex++] = MSG_FILLER; if (!Reply) { if (++EdenProtocolTransactionId == 0) EdenProtocolTransactionId = MIN_TRASACTION_ID; } // check if there are header bytes (0x55) in the msg and add filler for every // header byte found // ----------------- for (i = 0;i < DataLength ; i++) { EdenProtocolBuffer[BufIndex++] = Data[i]; if (Data[i] == MSG_SYNC_1) EdenProtocolBuffer[BufIndex++] = MSG_FILLER; } // insert the length to the msg after adding all the fillers // --------------------------------------------------------- EdenProtocolBuffer[MSG_LENGTH_LSB_INDEX] = (BufIndex - EDEN_HEADER_SIZE) & LSB_MASK; EdenProtocolBuffer[MSG_LENGTH_FILLER_1_INDEX] = MSG_LENGTH_FILLER; EdenProtocolBuffer[MSG_LENGTH_MSB_INDEX] = ((BufIndex - EDEN_HEADER_SIZE) & MSB_MASK) >> 8; EdenProtocolBuffer[MSG_LENGTH_FILLER_2_INDEX] = MSG_LENGTH_FILLER; // calculate the check sum and add it to the msg // --------------------------------------------- for(i = SOURCE_ID_INDEX; i < BufIndex; i++) CheckSum += EdenProtocolBuffer[i]; EdenProtocolBuffer[BufIndex++] = CheckSum; // send the msg through the uart // ----------------------------- if (UartSend(EdenProtocolBuffer, BufIndex) != UART_NO_ERROR) return EDEN_PROTOCOL_SEND_FAILED; return EDEN_PROTOCOL_NO_ERROR; }
//对STC15系列的芯片进行数据下载程序 static int Firmware_Download(BYTE *pdat, long size) { BYTE arg; BYTE cnt; WORD addr; Initial(); //握手 CommInit(); while (1) { if (UartRecvStep == 0) { UartSend(0x7f); DelayXms(10); } if (UartReceived) { arg = RxBuffer[4]; if (RxBuffer[0] == 0x50) break; return eDownloadErrCodeHandshake; } if (TimeOut == 0) { printf("waiting for mcu isp timeout\n"); return eDownloadErrCodeTimeout; } } printf("set comm param\n"); //设置参数 TxBuffer[0] = 0x01; TxBuffer[1] = arg; TxBuffer[2] = 0x40; TxBuffer[3] = HIBYTE(RL(MAXBAUD)); TxBuffer[4] = LOBYTE(RL(MAXBAUD)); TxBuffer[5] = 0x00; TxBuffer[6] = 0x00; TxBuffer[7] = 0xc3; CommSend(8); while (1) { if (TimeOut == 0) return eDownloadErrCodeTimeout; if (UartReceived) { if (RxBuffer[0] == 0x01) break; return eDownloadErrCodeSetCommParam; } } printf("download prepare \n"); //准备 serialport_config(handle_download_port,MAXBAUD,8,1,'e'); DelayXms(10); TxBuffer[0] = 0x05; CommSend(1); while (1) { if (TimeOut == 0) return eDownloadErrCodeTimeout; if (UartReceived) { if (RxBuffer[0] == 0x05) break; return eDownloadErrCodeSetCommParam; } } printf("erase flash ...\n"); //擦除 DelayXms(10); TxBuffer[0] = 0x03; TxBuffer[1] = 0x00; CommSend(2); TimeOut = 100; while (1) { if (TimeOut == 0) return eDownloadErrCodeTimeout; if (UartReceived) { if (RxBuffer[0] == 0x03) break; return eDownloadErrCodeEraseFlash; } } printf("program flash size[0x%lx]...\n",size); //写用户代码 DelayXms(10); addr = 0; TxBuffer[0] = 0x22; while (addr < size) { TxBuffer[1] = HIBYTE(addr); TxBuffer[2] = LOBYTE(addr); cnt = 0; while (addr < size) { TxBuffer[cnt+3] = pdat[addr]; addr++; cnt++; if (cnt >= 128) break; } CommSend(cnt + 3); while (1) { if (TimeOut == 0) return eDownloadErrCodeTimeout; if (UartReceived) { if ((RxBuffer[0] == 0x02) && (RxBuffer[1] == 'T')) break; return eDownloadErrCodeProgramFlash; } } TxBuffer[0] = 0x02; } printf("program hardware option ... skipped\n"); #if 0 //写硬件选项(如果不需要修改硬件选项,此步骤可直接跳过) DelayXms(10); for (cnt=0; cnt<128; cnt++) { TxBuffer[cnt] = 0xff; } //Do we know details of hardware options??? TxBuffer[0] = 0x04; TxBuffer[1] = 0x00; TxBuffer[2] = 0x00; TxBuffer[34] = 0xfd; TxBuffer[62] = arg; TxBuffer[63] = 0x7f; TxBuffer[64] = 0xf7; TxBuffer[65] = 0x7b; TxBuffer[66] = 0x1f; CommSend(67); while (1) { if (TimeOut == 0) return eDownloadErrCodeTimeout; if (UartReceived) { if ((RxBuffer[0] == 0x04) && (RxBuffer[1] == 'T')) break; return eDownloadErrCodeProgramOption; } } #endif //下载完成 return eDownloadErrCodeSuccess; }
void UartSendString(const char *str) { do { UartSend(*str); } while (*str++); }