123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- #include "myUart.h"
- #include "stm32f10x_rcc.h"
- #include "stm32f10x_usart.h"
- #include "stm32f10x_it.h"
- #include "misc.h"
- uint8_t printf_uartX = US_UART1;
- UART_CALLBACK uartCallBack;
-
- uint8_t USART_RX_BUF[USART_REC_LEN]; //
- uint16_t USART_RX_STA=0; //接收状态标记
- static irqCallback_ts myIrqCallback_uart1;
- void uart1_callback(uint8_t status, uint32_t param)
- {
- uint8_t Res;
- switch (status)
- {
- case 0:
- {
- uartCallBack(USART_RX_BUF, USART_RX_STA);
- memset(USART_RX_BUF, 0, sizeof(USART_RX_BUF));
- USART_RX_STA=0;
- }
- break;
- case 1:
- {
- //读取接收到的数据
- USART_RX_BUF[USART_RX_STA] = param;
- USART_RX_STA++;
- if(USART_RX_STA > (USART_REC_LEN - 1))
- {
- USART_RX_STA=0;//接收数据错误,重新开始接收
- }
- }
- break;
-
- default:
- break;
- }
- }
- void myUart1_init(uint32_t baudrate, UART_CALLBACK cb)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- myIrqCallback_uart1.thisCb = uart1_callback;
- USART1_callbackRegiste(&myIrqCallback_uart1);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //使能USART1,GPIOA时钟
- //USART1_TX GPIOA.9
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
- GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9
- //USART1_RX GPIOA.10初始化
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
- GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10
- //Usart1 NVIC 配置
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
- NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
- //USART 初始化设置
- USART_InitStructure.USART_BaudRate = baudrate;//串口波特率
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
- 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); //初始化串口1
- USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断
- USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);//开启串口接受中断
- // USART_ITConfig(USART1, USART_IT_TC, ENABLE);// 使能串口发送完成中断
- USART_Cmd(USART1, ENABLE); //使能串口1
- uartCallBack = cb;
- }
- void myUart1_sendByte(uint8_t src)
- {
- while(!USART_GetFlagStatus(USART1, USART_FLAG_TC));
- USART_SendData(USART1, src);
- }
- void myUart1_sendArray(uint8_t *src, uint16_t srclen)
- {
- while(srclen --)
- {
- myUart1_sendByte(*src);
- src ++;
- }
- }
- static uint16_t USART3_RX_STA; //接收状态标记
- static UART_CALLBACK uart3RxCallBack;
- static uint8_t USART3_RX_BUF[USART_REC_LEN];
- static irqCallback_ts myIrqCallback_uart3;
- static irqCallback_ts myIrqCallback_uart3Timeout;
- static uint32_t timeout;
- /**
- * @brief uart3中断函数
- * 通过API@USART1_callbackRegiste注册到@stm32f10x_it.c中的真正的中断函数
- *
- * @param status 串口接收状态,
- * =0,接收超时完成
- * =1,接收到一个字节数据
- * @param param
- */
- void uart3_callback(uint8_t status, uint32_t param)
- {
- uint8_t Res;
- switch (status)
- {
- // case 0:
- // {
- // uart3RxCallBack(USART3_RX_BUF, USART3_RX_STA);
- // memset(USART3_RX_BUF, 0, sizeof(USART3_RX_BUF));
- // USART3_RX_STA=0;
- // }
- // break;
- case 1:
- {
- //读取接收到的数据
- USART3_RX_BUF[USART3_RX_STA] = param;
- USART3_RX_STA++;
- if(USART3_RX_STA > (USART_REC_LEN - 1))
- {
- USART3_RX_STA=0;//接收数据错误,重新开始接收
- }
- timeout = 5*100;
- }
- break;
-
- default:
- break;
- }
- }
- void uart3Timeout_callback(uint8_t status, uint32_t param)
- {
- if (timeout)
- {
- if (--timeout == 0)
- {
- uart3RxCallBack(USART3_RX_BUF, USART3_RX_STA);
- memset(USART3_RX_BUF, 0, sizeof(USART3_RX_BUF));
- USART3_RX_STA=0;
- }
- }
- }
- void myUart3_init(uint32_t baudrate, UART_CALLBACK cb)
- {
- //GPIO端口设置
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- myIrqCallback_uart3.thisCb = uart3_callback;
- USART3_callbackRegiste(&myIrqCallback_uart3);
- myIrqCallback_uart3Timeout.thisCb = uart3Timeout_callback;
- TIM1_callbackRegiste(&myIrqCallback_uart3Timeout);
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
- //USART3_TX
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
- GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化
- //USART3_RX初始化
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;//
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
- GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化
- //Usart3 NVIC 配置
- NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
- NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
- //USART 初始化设置
- USART_InitStructure.USART_BaudRate = baudrate;//串口波特率
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
- 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(USART3, &USART_InitStructure); //初始化串口
- USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//开启串口接受中断
- USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);//开启串口接受中断
- // USART_ITConfig(USART3, USART_IT_TC, ENABLE);// 使能串口发送完成中断
- uart3RxCallBack = cb;
- USART_Cmd(USART3, ENABLE); //使能串口
- }
- void myUart3_sendByte(uint8_t sendByte)
- {
- while(!USART_GetFlagStatus(USART3, USART_FLAG_TC))
- {
- ;
- }
- USART_SendData(USART3, sendByte);
- }
- void myUart3_sendArray(uint8_t *buffer, uint16_t sendLen)
- {
- int i = 0;
- for (i = 0; i < sendLen; i++)
- {
- myUart3_sendByte(*buffer);
- buffer ++;
- }
-
- }
- //////////////////////////////////////////////////////////////////
- //加入以下代码,支持printf函数,而不需要选择use MicroLIB
- #ifdef __GNUC__
- #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
- #else
- #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
- #endif
- #ifdef USE_IAR
- PUTCHAR_PROTOTYPE
- {
- myUart_sendByte(ch);
- return ch;
- }
- #else
- #pragma import(__use_no_semihosting)
- //标准库需要的支持函数
- struct __FILE
- {
- int handle;
- };
- FILE __stdout;
- //定义_sys_exit()以避免使用半主机模式
- void _sys_exit(int x)
- {
- x = x;
- }
- //redefine fputcfunction
- s32 fputc(s32 ch, FILE* f)
- {
- switch (printf_uartX)
- {
- case US_UART1:
- myUart1_sendByte(ch);
- break;
- case US_UART3:
- myUart3_sendByte(ch);
- break;
-
- default:
- break;
- }
- return ch;
- }
- #endif
|