123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- #include "uart1.h"
- #include "queue.h"
- static uint8_t uart1_tx_buf[UART1_TX_LEN] = {0};
- static uint8_t uart1_rx_buf[UART1_REC_LEN] = {0};
- __IO uint8_t rx_index = 0x00;
- __IO uint8_t tx_index = 0x00;
- __IO uint8_t TimeOut = 0x00;
- #ifdef __GNUC__
- int _write(int fd, char *buffer, int size)
- {
-
- for (int i = 0; i < size; i++)
- {
- USART_SendData(USART1, buffer[i]);
-
- while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
- ;
- }
- return size;
- }
- int _read(int fd, char *buffer, int size)
- {
- for (int i = 0; i < size; i++)
- {
-
- while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
- ;
- buffer[i] = USART_ReceiveData(USART1);
- }
- return size;
- }
- #endif
- void uart1_nvic_config(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
-
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- void uart1_config(void)
- {
- USART_InitTypeDef USART_InitStructure;
- USART_InitStructure.USART_BaudRate = 115200;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(USART1, &USART_InitStructure);
- USART_Cmd(USART1, ENABLE);
- }
- void uart1_init(void)
- {
- uart1_config();
- uart1_nvic_config();
-
- USART_ClearFlag(USART1, USART_FLAG_TC);
- }
- void USART1_IRQHandler(void)
- {
-
- if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
- {
- if (rx_index < UART1_REC_LEN)
- {
-
- uart1_rx_buf[rx_index++] = USART_ReceiveData(USART1);
- }
- else
- {
-
- USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
- }
- }
-
- if (USART_GetITStatus(USART1, USART_IT_TXE) == SET)
- {
- if (tx_index < UART1_TX_LEN)
- {
-
- USART_SendData(USART1, uart1_tx_buf[tx_index++]);
- }
- else
- {
-
- USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
- }
- }
- }
|