int main(void) { //定义串口初始化结构 UART_InitTypeDef UART_InitStruct1; //初始化系统时钟 使用外部50M晶振 PLL倍频到100M SystemClockSetup(ClockSource_EX50M,CoreClock_100M); DelayInit(); //初始化LED LED_Init(LED_PinLookup_CHKATOM, kNumOfLED); //KBI 初始化 KBI_Init(KBI_PinLookup_CHKATOM, kNumOfKEY); UART_InitStruct1.UARTxMAP = UART4_RX_C14_TX_C15; //UART4模块 映射引脚:PTC14 PTC15 UART_InitStruct1.UART_BaudRate = 115200; //波特率 115200 UART_Init(&UART_InitStruct1); //初始化串口 UART_SendData(UART4, 'H'); UART_SendData(UART4, 'e'); UART_SendData(UART4, 'l'); UART_SendData(UART4, 'l'); UART_SendData(UART4, 'o'); //一次发送多个字符串 UART_SendBytes(UART4, "12345678", 8); while(1) { } }
/******************************************************************************* * Function Name : SenderFunc * Description : Sender interrupt handler. * Input : None * Output : None * Return : None *******************************************************************************/ static void SenderFunc(void) { for (; (send_data_pos < send_data_length && (UART_GetFlagStatus (UART, UART_FLAG_TXFF) == RESET)); send_data_pos++) { UART_SendData(UART, SendBuffer[send_data_pos]); } }
int_t fputc(int_t c, FILE *stream) { //Standard output? if(stream == stdout) { //Display current character lcdPutChar(c); //On success, the character written is returned return c; } //Standard error output? else if(stream == stderr) { //Wait for the transmitter to be ready while(UART_GetFlagStatus(UART0, UART_FLAG_TxFIFOFull) != RESET); //Send character UART_SendData(UART0, c); //Wait for the transfer to complete while(UART_GetFlagStatus(UART0, UART_FLAG_TxFIFOFull) != RESET); //On success, the character written is returned return c; } //Unknown output? else { //If a writing error occurs, EOF is returned return EOF; } }
int main(void) { uint8_t ch; //定义串口初始化结构 UART_InitTypeDef UART_InitStruct1; //初始化系统时钟 使用外部50M晶振 PLL倍频到100M SystemClockSetup(ClockSource_EX50M,CoreClock_100M); DelayInit(); //初始化LED LED_Init(LED_PinLookup_CHKATOM, kNumOfLED); //KBI 初始化 KBI_Init(KBI_PinLookup_CHKATOM, kNumOfKEY); UART_InitStruct1.UARTxMAP = UART4_RX_C14_TX_C15; //UART4模块 映射引脚:PTC14 PTC15 UART_InitStruct1.UART_BaudRate = 115200; //波特率 115200 UART_Init(&UART_InitStruct1); //初始化串口 while(1) { //接收成功 if(UART_ReceiveData(UART4, &ch) == TRUE) { //echo UART_SendData(UART4, ch); } } }
//发送多个字符串 static void UART_SendBytes(UART_Type* UARTx, uint8_t* pBuffer, uint32_t size) { while(size--) { UART_SendData(UARTx, *(pBuffer++)); } }
/************************************************************************* * 函数名称:SendHex * 功能说明:采集发数程序 * 参数说明: * 函数返回:无 * 修改时间:2012-10-20 * 备 注: *************************************************************************/ void SendHex(unsigned char hex) { unsigned char temp; temp = hex >> 4; if(temp < 10) { UART_SendData(UART0, temp + '0'); } else { UART_SendData(UART0, temp - 10 + 'A'); } temp = hex & 0x0F; if(temp < 10) { UART_SendData(UART0, temp + '0'); } else { UART_SendData(UART0, temp - 10 + 'A'); } }
/*********************************************************************************************** 功能:UART 格式化输出 形参:fmt 输入字符串指针 返回:0 详解:类似于C标准库中的printf 但是只支持 %d %l %o %x %s ************************************************************************************************/ void UART_printf(char *fmt, ...) { char c; unsigned int *adx = (unsigned int*)(void*)&fmt + 1; _loop: while((c = *fmt++) != '%') { if (c == '\0') return; UART_SendData(UART_DebugPort,c); } c = *fmt++; if (c == 'd' || c == 'l') { printn(*adx, 10); } if (c == 'o' || c == 'x') { printn(*adx, c=='o'? 8:16 ); } if (c == 's') { UART_puts((char*)*adx); } adx++; goto _loop; }
void platform_uart_send( unsigned id, u8 data ) { id = id; // while( UART_GetFlagStatus( STR9_UART, UART_FLAG_TxFIFOFull ) == SET ); UART_SendData( STR9_UART, data ); while( UART_GetFlagStatus( STR9_UART, UART_FLAG_TxFIFOFull ) != RESET ); }
/*********************************************************************//** * @brief Send a block of data via UART peripheral * @param[in] UARTx Selected UART peripheral used to send data, * should be UART0, UART1, UART2 or UART3. * @param[in] txbuf Pointer to Transmit buffer * @param[in] buflen Length of Transmit buffer * @param[in] flag Flag used in UART transfer, should be * NONE_BLOCKING or BLOCKING * @return Number of bytes sent. * * Note: when using UART in BLOCKING mode, a time-out condition is used * via defined symbol UART_BLOCKING_TIMEOUT. **********************************************************************/ uint32_t UART_Send(UART_TypeDef *UARTx, uint8_t *txbuf, uint32_t buflen, TRANSFER_BLOCK_Type flag) { uint32_t bToSend, bSent, timeOut, fifo_cnt; uint8_t *pChar = txbuf; bToSend = buflen; // blocking mode if (flag == BLOCKING) { bSent = 0; while (bToSend){ timeOut = UART_BLOCKING_TIMEOUT; // Wait for THR empty with timeout while (!(UARTx->LSR & UART_LSR_THRE)) { if (timeOut == 0) break; timeOut--; } // Time out! if(timeOut == 0) break; fifo_cnt = UART_TX_FIFO_SIZE; while (fifo_cnt && bToSend){ UART_SendData(UARTx, (*pChar++)); fifo_cnt--; bToSend--; bSent++; } } } // None blocking mode else { bSent = 0; while (bToSend) { if (!(UARTx->LSR & UART_LSR_THRE)){ break; } fifo_cnt = UART_TX_FIFO_SIZE; while (fifo_cnt && bToSend) { UART_SendData(UARTx, (*pChar++)); bToSend--; fifo_cnt--; bSent++; } } } return bSent; }
void platform_s_uart_send( unsigned id, u8 data ) { UART_TypeDef* p_uart = ( UART_TypeDef* )uarts[ id ]; // while( UART_GetFlagStatus( STR9_UART, UART_FLAG_TxFIFOFull ) == SET ); UART_SendData( p_uart, data ); while( UART_GetFlagStatus( p_uart, UART_FLAG_TxFIFOFull ) != RESET ); }
//内部函数为实现UART_printf static void UART_puts(char *pch) { while(*pch != '\0') { UART_SendData(UART_DebugPort,*pch); pch++; } }
uint8_t UartPutc(UART_TypeDef* UARTx, uint8_t ch) { UART_SendData(UARTx,ch); while(UARTx->FR & UART_FR_BUSY); return (ch); }
// ----------------------------------------------------------------------------- // This function sends the contents of buffer to the u-blox device. This // function blocks program execution until the entire buffer is sent. static void UBloxTxBuffer(const uint8_t * buffer, size_t length) { while (length--) { while(UART_GetFlagStatus(UART0, UART_FLAG_TxFIFOFull)) continue; UART_SendData(UART0, *buffer++); } }
void UART4_RX_TX_IRQHandler(void) { uint8_t ch; if(UART_ReceiveData(UART4,&ch)) { UART_SendData(UART4,ch+1); } }
/******************************************************************************* * Function Name : USB_To_UART_Send_Data. * Description : send the received data from USB to the UART 0. * Input : data_buffer: data address. Nb_bytes: number of bytes to send. * Return : none. *******************************************************************************/ void USB_To_UART_Send_Data(u8* data_buffer, u8 Nb_bytes) { u32 i; for(i=0;i<Nb_bytes;i++) { UART_SendData(UART0, *(data_buffer + i)); } }
void UART1_IRQHandler(void) { uint8_t ch; if(UART_ReceiveData(UART1,&ch) == TRUE) { UART_SendData(UART1,ch); } }
void UART4_RX_TX_IRQHandler(void) { uint8_t ch; //中断发送处理过程 UART_SendDataIntProcess(UART4); //如果成功接收到了数据 if(UART_ReceiveData(UART4, &ch)) { //把接收到的信息发送回去 echo UART_SendData(UART4,ch); } }
//================================================================================== void UART1_send_byte(uint8_t byte) { UART_SendData(MDR_UART1,byte); //time_out_byte=0; //while(!UART_GetFlagStatus (MDR_UART1,UART_FLAG_RXFF)) //while(UART_GetFlagStatus (MDR_UART1,UART_FLAG_BUSY) || !UART_GetFlagStatus (MDR_UART1,UART_FLAG_RXFF)) while(UART_GetFlagStatus (MDR_UART1,UART_FLAG_BUSY)) { } UART_ReceiveData (MDR_UART1); UART_ClearITPendingBit(MDR_UART1,UART_IT_RX); }
//内部函数为实现UART_printf static void printn(unsigned int n, unsigned int b) { static char *ntab = "0123456789ABCDEF"; unsigned int a, m; if (n / b) { a = n / b; printn(a, b); } m = n % b; UART_SendData(UART_DebugPort,ntab[m]); }
/************************************************************************* * 函数名称:SendImageData * 功能说明: * 参数说明: * 函数返回:无 * 修改时间:2012-10-20 * 备 注: *************************************************************************/ void SendImageData(unsigned char * ImageData) { unsigned char i; unsigned char crc = 0; /* Send Data */ UART_SendData(UART0, '*'); UART_SendData(UART0, 'L'); UART_SendData(UART0, 'D'); SendHex(0); SendHex(0); SendHex(0); SendHex(0); for(i=0; i<128; i++) { SendHex(*ImageData++); } SendHex(crc); UART_SendData(UART0,'#'); }
void INT_UART0_Handler(void) { uint32_t temp_1; if (UART_GetITStatusMasked(MDR_UART0, UART_IT_RX) == SET) { temp_1 = UART_ReceiveData(MDR_UART0); UART_ClearITPendingBit(MDR_UART0, UART_IT_RX); while (UART_GetFlagStatus (MDR_UART0, UART_FLAG_TXFE)!= SET) { } UART_SendData (MDR_UART0,temp_1); } }
int uartEcho() { uartPut("Echo keypress mode:"); do { if((UART_GetFlagStatus(UART0, UART_FLAG_RxFIFOEmpty) != SET)&&(RxCounter < RxBufferSize)) { RxBuffer[RxCounter] = UART0->DR; UART_SendData(UART0, RxBuffer[RxCounter++]); } }while((RxBuffer[RxCounter - 1] != '@')&&(RxBuffer[RxCounter - 1] != '\r')&&(RxCounter != RxBufferSize)); return 1; }
int main() { /*System clock configuration*/ SystemInit(); // *(volatile uint32_t *)(0x41001014) = 0x0060100; //clock setting 48MHz /* CLK OUT Set */ // PAD_AFConfig(PAD_PA,GPIO_Pin_2, PAD_AF2); // PAD Config - CLKOUT used 3nd Function /* UART0 and UART1 configuration*/ UART_StructInit(&UART_InitStructure); /* Configure UART0 */ UART_Init(UART0,&UART_InitStructure); /* Configure UART1 */ UART_Init(UART1,&UART_InitStructure); while(TxCounter < TxBufferSize) { /* Send one byte from UART0 to UART1 */ UART_SendData(UART0,TxBuffer[TxCounter++]); /* Loop until UART0 TX FIFO Register is empty */ while(UART_GetFlagStatus(UART0,UART_FLAG_TXFE) == RESET) { } /* Loop until the UART1 Receive FIFO Register is not empty */ while(UART_GetFlagStatus(UART1,UART_FLAG_RXFE) == SET) { } /* Store the received byte in RxBuffer */ RxBuffer[RxCounter++] = (UART_ReceiveData(UART1) & 0xFF); } /* Check the received data with the send ones */ TransferStatus = Buffercmp(TxBuffer, RxBuffer, TxBufferSize); /* TransferStatus = PASSED, if the data transmitted from USARTy and received by USARTz are the same */ /* TransferStatus = FAILED, if the data transmitted from USARTy and received by USARTz are different */ while(1) { } }
void UART0_IRQHandler(void) { u8 c; // if receive irq (FIFO is over trigger level) or receive timeout irq (FIFO is not empty for longer times) has occured if((UART_GetITStatus(UART0, UART_IT_Receive) != RESET) || (UART_GetITStatus(UART0, UART_IT_ReceiveTimeOut) != RESET) ) { UART_ClearITPendingBit(UART0, UART_IT_Receive); // clear receive interrupt flag UART_ClearITPendingBit(UART0, UART_IT_ReceiveTimeOut); // clear receive timeout interrupt flag // if debug UART is UART0 if (DebugUART == UART0) { // forward received data to the UART1 tx buffer while(UART_GetFlagStatus(UART0, UART_FLAG_RxFIFOEmpty) != SET) { // wait for space in the tx buffer of the UART1 while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) == SET) {}; // move the byte from the rx buffer of UART0 to the tx buffer of UART1 UART_SendData(UART1, UART_ReceiveData(UART0)); } } else // UART0 is not the DebugUART (normal operation) { // repeat until no byte is in the RxFIFO while (UART_GetFlagStatus(UART0, UART_FLAG_RxFIFOEmpty) != SET) { c = UART_ReceiveData(UART0); // get byte from rx fifo switch(UART0_Muxer) { case UART0_MKGPS: UBX_RxParser(c); // if connected to GPS forward byte to ubx parser MKProtocol_CollectSerialFrame(&UART0_rx_buffer, c); // ckeck for MK-Frames also break; case UART0_MK3MAG: // ignore any byte send from MK3MAG break; case UART0_UNDEF: default: // ignore the byte from unknown source break; } // eof switch(UART0_Muxer) } // eof while } // eof UART0 is not the DebugUART } // eof receive irq or receive timeout irq }
void TIMER1_IRQHandler(void) { uint32_t temp_2 = 0; if (TIMER_GetITStatus(MDR_TIMER1, TIMER_STATUS_CNT_ARR) == SET) { //TIMER_ClearITPendingBit(MDR_TIMER1, TIMER_STATUS_CNT_ARR); MDR_TIMER1->STATUS &= ~TIMER_STATUS_CNT_ARR; while (UART_GetFlagStatus (MDR_UART1, UART_FLAG_TXFE)!= SET) { } UART_SendData (MDR_UART1,0x35); } }
void UART1_IRQHandler(void) { uint32_t temp_1; //UARTx_HandlerWork(MDR_UART2); if (UART_GetITStatusMasked(MDR_UART1, UART_IT_RX) == SET) { temp_1 = MDR_UART1->DR; UART_ClearITPendingBit(MDR_UART1, UART_IT_RX); while (UART_GetFlagStatus (MDR_UART1, UART_FLAG_TXFE)!= SET) { } UART_SendData (MDR_UART1,0x44); } }
void UART1_Handler(void) { if(UART_GetITStatus(UART1, UART_IT_FLAG_RXI)) { UART_ClearITPendingBit(UART1, UART_IT_FLAG_RXI); if( (u1rx_wr > u1rx_rd && u1rx_wr-u1rx_rd >= U1RX_BUF_SIZE-1) ||(u1rx_wr < u1rx_rd && u1rx_rd == u1rx_wr+1) ) // Buffer Overflow { UART_SendData(UART1, (uint8_t)'@'); return; } u1rx_buf[u1rx_wr] = (uint8_t)UART_ReceiveData(UART1); if(u1rx_wr < U1RX_BUF_SIZE-1) u1rx_wr++; else u1rx_wr = 0; } }
void UART1_send_byte(uint8_t byte) { UART_SendData(MDR_UART1,byte); time_out_byte = 0; /*while(!UART_GetFlagStatus (MDR_UART1,UART_FLAG_RXFF)) { if( time_out_byte>3) break; } UART_ReceiveData (MDR_UART1); */ while(UART_GetFlagStatus (MDR_UART1,UART_FLAG_BUSY) || !UART_GetFlagStatus (MDR_UART1,UART_FLAG_RXFF)) { // if(time_out_byte>3) // break; } UART_ReceiveData (MDR_UART1); UART_ClearITPendingBit(MDR_UART1,UART_IT_RX); }
int main() { /*System clock configuration*/ SystemInit(); /* UART0 and UART1 configuration*/ UART_StructInit(&UART_InitStructure); /* Configure UART0 */ UART_Init(UART0,&UART_InitStructure); /* Configure UART1 */ UART_Init(UART1,&UART_InitStructure); while(TxCounter < TxBufferSize) { /* Send one byte from UART0 to UART1 */ UART_SendData(UART0,TxBuffer[TxCounter++]); /* Loop until UART0 TX FIFO Register is empty */ while(UART_GetFlagStatus(UART0,UART_FLAG_TXFE) == RESET) { } /* Loop until the UART1 Receive FIFO Register is not empty */ while(UART_GetFlagStatus(UART1,UART_FLAG_RXFE) == SET) { } /* Store the received byte in RxBuffer */ RxBuffer[RxCounter++] = (UART_ReceiveData(UART1) & 0xFF); } /* Check the received data with the send ones */ TransferStatus = Buffercmp(TxBuffer, RxBuffer, TxBufferSize); /* TransferStatus = PASSED, if the data transmitted from USARTy and received by USARTz are the same */ /* TransferStatus = FAILED, if the data transmitted from USARTy and received by USARTz are different */ while(1) { } }
void UART0_Transmit(void) { u8 tmp_tx; IENABLE; if(DebugUART == UART0) return; // no data output if debug uart is rederected to UART0 // if something has to be send and the txd fifo is not full if((UART0_tx_buffer.Locked == TRUE) && (UART_GetFlagStatus(UART0, UART_FLAG_TxFIFOFull) == RESET)) { tmp_tx = UART0_tx_buffer.pData[UART0_tx_buffer.Position++]; // read next byte from txd buffer UART_SendData(UART0, tmp_tx); // put character to txd fifo // if terminating character or end of txd buffer reached if((tmp_tx == '\r') || (UART0_tx_buffer.Position == UART0_tx_buffer.Size)) { Buffer_Clear(&UART0_tx_buffer); } } IDISABLE; }