📄 uart_ste.c.svn-base
字号:
/******************************************************************************** **** Copyright (c) 2000 ST Microelectronics **** All rights reserved **** **** Filename : uart.c **** Author : Armando Visconti **** Revision : 1.0 **** **** **** *********************************************************************************/#include "vic_pl190.h"#include "uart.h"/*****************************************************************//********************* Comunication on port 1 *******************//************************ init function **************************/void UART1_Init (){ /*** INIT UART0 registers **/ *UART1_ASCBaudRate = UART_Baud_115200; *UART1_ASCTimeout = 0x0F; *UART1_ASCGuardtime = 100; *UART1_ASCControl = UART_Mode_7BitsDataParity | UART_IStopBits1 | UART_IParEven | UART_Run | UART_RxE | UART_FifE; *UART1_ASCTxReset = 0x00; *UART1_ASCRxReset = 0x00; return;}/*****************************************************************//******************** get and put functions **********************/unsigned int UART_getc (unsigned char *in_buf){ /* RX FIFO is not empty */ if(*UART1_ASCStatus & UART_RxNotEmpty) { *in_buf = *UART1_ASCRxBuffer; return 1; } else { return 0; }}unsigned int UART_putc (unsigned char c){ /* TX FIFO is not full */ if(!(*UART1_ASCStatus & UART_TxFull)) { *UART1_ASCTxBuffer = c; return 1; } else { return 0; }}/*****************************************************************//***************** vector tx and rx functions ********************/void UART_rx_vec (unsigned char *in_vec, unsigned int n){ int i; for (i = 0; i < n; i++) { while (!(UART_getc(&in_vec[i]))); if(in_vec[i] == '\n')return; /* check for carriage return */ while (!(UART_putc(in_vec[i]))); }}void UART_tx_vec (unsigned char *out_vec, unsigned int n){ int i; for (i = 0; i < n; i++) while (!(UART_putc(out_vec[i]))); while (!(UART_putc(13))); while (!(UART_putc(10)));}/*****************************************************************//******************** comunication function **********************/char UART_com(){ static char in_buf; while (!(UART_getc(&in_buf))); if(in_buf == 13) { while (!(UART_putc(10))); while (!(UART_putc(in_buf))); } else { while (!(UART_putc(in_buf))); } return in_buf;}/*****************************************************************//********************** get_date(yyyymmdd) ***********************/unsigned int UART_get_date(void){ unsigned char to_send[8 + 1]; unsigned int date_val; UART_tx_vec("Insert date (yyyymmdd)",strlen("Insert date (yyyymmdd)")); UART_rx_vec(to_send, 8); to_send[9] = '\0'; /* add end string */ if(sscanf(to_send, "%08x", &date_val) != 8) return 0; return date_val;}/*****************************************************************//************************* print_integer *************************/void UART_print_integer(int numb){ unsigned char to_send[6]; sprintf(to_send, "%05d", numb); UART_tx_vec(to_send, 5);}/*****************************************************************//*************************** print_time **************************/void UART_print_time(int h, int m, int s){ int i; unsigned char to_send[9]; sprintf(to_send, "%02d:%02d:%02d", h, m, s); for (i = 0; i < 8; i++) while (!(UART_putc(to_send[i]))); while (!(UART_putc(13)));}/*****************************************************************//************************ print_exadecimal ***********************/void UART_print_ex(unsigned int numb){ int i; unsigned char to_send[9]; sprintf(to_send, "%08X", numb); while (!(UART_putc('0'))); while (!(UART_putc('x'))); for (i = 0; i < 8; i++) while (!(UART_putc(to_send[i]))); while (!(UART_putc(13))); while (!(UART_putc(10)));}/*****************************************************************//************************ print_mac_address***********************/void UART_print_mac_addr(unsigned char* mac_adr){ int i; unsigned char to_send[18]; sprintf(to_send, "%02x:%02x:%02x:%02x:%02x:%02x", mac_adr[0], mac_adr[1], mac_adr[2], mac_adr[3], mac_adr[4], mac_adr[5]); UART_tx_vec(to_send, 18); }/*****************************************************************//**************************** uart_test **************************/void uart_test(void){ static char schedule; static char in_buf; int i; char message_1[38] = "-> print integer (digit a key to run)"; char message_2[36] = "-> send string (digit a key to run)"; char string[20] = "SpearLite UART test"; char message_3[35] = "-> print time (digit a key to run)"; char message_4[41] = "-> print exadecimal (digit a key to run)"; char message_5[44] = "-> char echo (digit a key to run + to exit)"; unsigned int date; schedule = 'r'; while(schedule != 's') { /* use of print_integer */ UART_tx_vec(message_1, 37); while (!(UART_getc(&in_buf))); for(i = 0; i < 16; i++) { UART_print_integer(i); } /* use of tx_vect */ UART_tx_vec(message_2, 35); while (!(UART_getc(&in_buf))); UART_tx_vec(string, 19); /* use of print time */ UART_tx_vec(message_3, 34); while (!(UART_getc(&in_buf))); UART_print_time(16, 25, 15); while (!(UART_putc(13))); while (!(UART_putc(10))); /* use of print exadecimal */ UART_tx_vec(message_4, 40); while (!(UART_getc(&in_buf))); UART_print_ex(10); /* realize echo*/ UART_tx_vec(message_5, 43); while (!(UART_getc(&in_buf))); while(UART_com() != '+'); /* realize echo*/ UART_tx_vec("Get Date (digit a key to run)", strlen("Get Date (digit a key to run)")); while (!(UART_getc(&in_buf))); date = 0; date = UART_get_date(); if(date == 0) UART_tx_vec("Error!", strlen("Error!")); else UART_print_ex(date); while (!(UART_putc(13))); while (!(UART_putc(10))); UART_tx_vec("press s to stop r to run",strlen("press s to stop r to run")); while (!(UART_getc(&schedule))); while (!(UART_putc(13))); while (!(UART_putc(10))); }}/*****************************************************************//*****************************************************************//********************* Comunication on port 2 *******************/void UART2_Init (){ /*** INIT UART0 registers **/ *UART2_ASCBaudRate = UART_Baud_115200; *UART2_ASCTimeout = 0x0F; *UART2_ASCGuardtime = 100; *UART2_ASCControl = UART_Mode_7BitsDataParity | UART_IStopBits1 | UART_IParEven | UART_Run | UART_RxE | UART_FifE; *UART2_ASCTxReset = 0x00; *UART2_ASCRxReset = 0x00; return;}/*****************************************************************//*****************************************************************//********************* Comunication on port 3 *******************/void UART3_Init (){ /*** INIT UART0 registers **/ *UART3_ASCBaudRate = UART_Baud_115200; *UART3_ASCTimeout = 0x0F; *UART3_ASCGuardtime = 100; *UART3_ASCControl = UART_Mode_7BitsDataParity | UART_IStopBits1 | UART_IParEven | UART_Run | UART_RxE | UART_FifE; *UART3_ASCTxReset = 0x00; *UART3_ASCRxReset = 0x00; return;}/*****************************************************************//************** get and put functions for uart 2,3****************/unsigned int UART23_getc (unsigned char *in_buf, unsigned int port){ switch(port) { case 2: if(*UART2_ASCStatus & UART_RxNotEmpty){*in_buf = *UART2_ASCRxBuffer; return 1;} else return 0; break; case 3: if(*UART3_ASCStatus & UART_RxNotEmpty){*in_buf = *UART3_ASCRxBuffer; return 1;} else return 0; break; default: return 0; break; }}unsigned int UART23_putc (unsigned char c, unsigned int port){ switch(port) { case 2: if(!(*UART2_ASCStatus & UART_TxFull)){*UART2_ASCTxBuffer = c;return 1;} else return 0; break; case 3: if(!(*UART3_ASCStatus & UART_TxFull)){*UART3_ASCTxBuffer = c;return 1;} else return 0; break; default: return 0; break; }}/*****************************************************************//********************* Tests for on ports 2,3 *******************/void UART_test_23 (void){ static char schedule; unsigned char in_buf; unsigned char send; unsigned char receive; schedule = 'r'; while(schedule != 's') { UART_tx_vec("LoopBack test: Select UART port (2 or 3)",strlen("LoopBack test: Select UART port (2 or 3)")); while (!(UART_getc(&in_buf))); switch(in_buf) { case '2': UART_tx_vec("Access to UART2",strlen("Access to UART2")); UART_tx_vec("Select char to send",strlen("Select char to send")); while (!(UART_getc(&send))); UART_tx_vec("Selected char is:",strlen("Selected char is:")); while (!(UART_putc(send))); while (!(UART_putc(13))); while (!(UART_putc(10))); while (!(UART23_putc(send, 2))); while (!(UART23_getc((&receive), 2))); UART_tx_vec("Received char is:",strlen("Received char is:")); while (!(UART_putc(receive))); while (!(UART_putc(13))); while (!(UART_putc(10))); break; case '3': UART_tx_vec("Access to UART3",strlen("Access to UART3")); UART_tx_vec("Select char to send",strlen("Select char to send")); while (!(UART_getc(&send))); UART_tx_vec("Selected char is:",strlen("Selected char is:")); while (!(UART_putc(send))); while (!(UART_putc(13))); while (!(UART_putc(10))); while (!(UART23_putc(send, 3))); while (!(UART23_getc((&receive), 3))); UART_tx_vec("Received char is:",strlen("Received char is:")); while (!(UART_putc(receive))); while (!(UART_putc(13))); while (!(UART_putc(10))); break; } while (!(UART_putc(13))); while (!(UART_putc(10))); UART_tx_vec("press s to stop r to run",strlen("press s to stop r to run")); while (!(UART_getc(&schedule))); while (!(UART_putc(13))); while (!(UART_putc(10))); }}/*****************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -