| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573 |
- /**
- ******************************** STM32F10x *********************************
- * @文件名 : usart.c
- * @作者 : sun
- * @库版本 : V3.5.0
- * @文件版本 : V1.0.0
- * @日期 : 2016年05月09日
- * @摘要 : USART源文件
- ******************************************************************************/
- /*----------------------------------------------------------------------------
- 更新日志:
- 2016-05-09 V1.0.0:初始版本
- ----------------------------------------------------------------------------*/
- /* 包含的头文件 --------------------------------------------------------------*/
- #include "usart.h"
- #include "led.h"
- #include "systick.h"
- #include <string.h>
- /********************uart1****************************/
- volatile uint8_t m_state_code=M_STATE_IDLE;
- uint32_t usart2_cmd_time_out=0;
- uint32_t usart2_cmd_time_counter=0;
- #if 1
- #pragma import(__use_no_semihosting)
- //标准库需要的支持函数
- struct __FILE
- {
- int handle;
- };
- FILE __stdout;
- //定义_sys_exit()以避免使用半主机模式
- void _sys_exit(int x)
- {
- x = x;
- }
- //重定义fputc函数
- int fputc(int ch, FILE *f)
- {
- uint16_t temp=1000;
-
- while((temp>0)&((USART1->SR&0X40)==0))
- {
- temp--;
- };//循环发送,直到发送完毕
- //while((USART2->SR & USART_FLAG_TXE) == RESET);
- USART1->DR = (u8) ch;
-
- return ch;
-
- }
- #endif
-
- /************************************************
- 函数名称 : USART1_GPIO_Configuration
- 功 能 : USART所使用管脚输出输入定义
- 参 数 : 无
- 返 回 值 : 无
- 作 者 : sun
- *************************************************/
- void USART1_GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* 定义 USART1-TX 引脚为复用输出 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //IO口的第A9脚
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //IO口复用推挽输出
- GPIO_Init(GPIOA, &GPIO_InitStructure); //USART输出IO口
- /* 定义 USART1-Rx 引脚为悬空输入 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //IO口的第A10脚
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //IO口悬空输入
- GPIO_Init(GPIOA, &GPIO_InitStructure); //USART输入IO口
- }
- /************************************************
- 函数名称 : USART_Configuration
- 功 能 : 配置USART
- 参 数 : 无
- 返 回 值 : 无
- 作 者 : sun
- *************************************************/
- void USART1_Configuration(void)
- {
- USART_InitTypeDef USART_InitStructure;
- /******************************************************************
- USART参数初始化: 波特率 传输位数 停止位数 校验位数
- 115200 8 1 0(NO)
- *******************************************************************/
- USART_InitStructure.USART_BaudRate = USART1_BAUDRATE; //设定传输速率
- USART_InitStructure.USART_WordLength = USART_WordLength_9b; //设定传输数据位数,USART_WordLength_8b USART_WordLength_9b
- USART_InitStructure.USART_StopBits = USART_StopBits_1; //设定停止位个数
- USART_InitStructure.USART_Parity = USART_Parity_No ; //不用校验位USART_Parity_No,偶校验USART_Parity_Even,奇校验USART_Parity_Odd
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//不用流量控制
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //使用接收和发送功能
- USART_Init(USART1, &USART_InitStructure); //初始化USART1
- USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //使能USART1接收中断
- USART_Cmd(USART1, ENABLE); //使能USART1
- }
- /************************************************
- 函数名称 : USART1_GPIO_Configuration
- 功 能 : USART所使用管脚输出输入定义
- 参 数 : 无
- 返 回 值 : 无
- 作 者 : sun
- *************************************************/
- void USART2_GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* 定义 USART1-TX 引脚为复用输出 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //IO口的第A9脚
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //IO口复用推挽输出
- GPIO_Init(GPIOA, &GPIO_InitStructure); //USART输出IO口
- /* 定义 USART1-Rx 引脚为悬空输入 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //IO口的第A10脚
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//GPIO_Mode_IPU GPIO_Mode_IN_FLOATING; //IO口悬空输入
- GPIO_Init(GPIOA, &GPIO_InitStructure); //USART输入IO口
- }
- /************************************************
- 函数名称 : USART_Configuration
- 功 能 : 配置USART
- 参 数 : 无
- 返 回 值 : 无
- 作 者 : sun
- *************************************************/
- void USART2_Configuration(void)
- {
- USART_InitTypeDef USART_InitStructure;
- /******************************************************************
- USART参数初始化: 波特率 传输位数 停止位数 校验位数
- 115200 8 1 0(NO)
- *******************************************************************/
- USART_InitStructure.USART_BaudRate = USART2_BAUDRATE; //设定传输速率
- USART_InitStructure.USART_WordLength = USART_WordLength_8b; //设定传输数据位数,USART_WordLength_8b USART_WordLength_9b
- USART_InitStructure.USART_StopBits = USART_StopBits_1; //设定停止位个数
- USART_InitStructure.USART_Parity = USART_Parity_No ; //不用校验位USART_Parity_No,偶校验USART_Parity_Even,奇校验USART_Parity_Odd
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//不用流量控制
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //使用接收和发送功能
- USART_Init(USART2, &USART_InitStructure); //初始化USART1
- USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //使能USART1接收中断
- USART_ClearFlag(USART2, USART_FLAG_TC);
- USART_Cmd(USART2, ENABLE); //使能USART1
- }
- /************************************************
- 函数名称 : USART1_GPIO_Configuration
- 功 能 : USART所使用管脚输出输入定义
- 参 数 : 无
- 返 回 值 : 无
- 作 者 : sun
- *************************************************/
- void USART4_GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //使能PB,PE端口时钟 | RCC_APB2Periph_AFIO
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
- /* 定义 USART1-TX 引脚为复用输出 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //IO口的第C10脚
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //IO口复用推挽输出
- GPIO_Init(GPIOC, &GPIO_InitStructure); //USART输出IO口
- /* 定义 USART1-Rx 引脚为悬空输入 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //IO口的第C11脚
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //IO口悬空输入
- GPIO_Init(GPIOC, &GPIO_InitStructure); //USART输入IO口
- }
- /************************************************
- 函数名称 : USART_Configuration
- 功 能 : 配置USART
- 参 数 : 无
- 返 回 值 : 无
- 作 者 : sun
- *************************************************/
- void USART4_Configuration(void)
- {
- USART_InitTypeDef USART_InitStructure;
- /******************************************************************
- USART参数初始化: 波特率 传输位数 停止位数 校验位数
- 115200 8 1 0(NO)
- *******************************************************************/
- USART_InitStructure.USART_BaudRate = UART4_BAUDRATE; //设定传输速率
- USART_InitStructure.USART_WordLength = USART_WordLength_9b; //设定传输数据位数
- USART_InitStructure.USART_StopBits = USART_StopBits_1; //设定停止位个数
- USART_InitStructure.USART_Parity = USART_Parity_Even ; //不用校验位USART_Parity_No,偶校验USART_Parity_Even,奇校验USART_Parity_Odd
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//不用流量控制
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //使用接收和发送功能
- USART_Init(UART4, &USART_InitStructure); //初始化UART4
- USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); //使能UART4接收中断
- USART_Cmd(UART4, ENABLE); //使能UART4
- }
- /************************************************
- 函数名称 : USART1_GPIO_Configuration
- 功 能 : USART所使用管脚输出输入定义
- 参 数 : 无
- 返 回 值 : 无
- 作 者 : sun
- *************************************************/
- void USART5_GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //使能PB,PE端口时钟 | RCC_APB2Periph_AFIO
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); //使能PB,PE端口时钟 | RCC_APB2Periph_AFIO
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);
- /* 定义 USART1-TX 引脚为复用输出 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //IO口的第PC12脚
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //IO口复用推挽输出
- GPIO_Init(GPIOC, &GPIO_InitStructure); //USART输出IO口
- /* 定义 USART1-Rx 引脚为悬空输入 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //IO口的第PD2脚
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //IO口悬空输入
- GPIO_Init(GPIOD, &GPIO_InitStructure); //USART输入IO口
- }
- /************************************************
- 函数名称 : USART_Configuration
- 功 能 : 配置USART
- 参 数 : 无
- 返 回 值 : 无
- 作 者 : sun
- *************************************************/
- void USART5_Configuration(void)
- {
- USART_InitTypeDef USART_InitStructure;
- /******************************************************************
- USART参数初始化: 波特率 传输位数 停止位数 校验位数
- 115200 8 1 0(NO)
- *******************************************************************/
- USART_InitStructure.USART_BaudRate = UART5_BAUDRATE; //设定传输速率
- USART_InitStructure.USART_WordLength = USART_WordLength_9b; //设定传输数据位数
- USART_InitStructure.USART_StopBits = USART_StopBits_1; //设定停止位个数
- USART_InitStructure.USART_Parity = USART_Parity_Even ; //不用校验位USART_Parity_No,偶校验USART_Parity_Even,奇校验USART_Parity_Odd
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//不用流量控制
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //使用接收和发送功能
- USART_Init(UART5, &USART_InitStructure); //初始化USART5
- USART_ITConfig(UART5, USART_IT_RXNE, ENABLE); //使能USART5接收中断
- USART_Cmd(UART5, ENABLE); //使能USART5
- }
- /************************************************
- 函数名称 : USART_Initializes
- 功 能 : 串口初始化
- 参 数 : 无
- 返 回 值 : 无
- 作 者 : sun
- *************************************************/
- void USART_Initializes(void)
- {
- USART1_GPIO_Configuration();
- USART1_Configuration();
-
- }
- /************************************************
- 函数名称 : USART1_SendChar
- 功 能 : 串口1发送一个字符
- 参 数 : Data --- 数据
- 返 回 值 : 无
- 作 者 : sun
- *************************************************/
- void USART1_SendByte(uint8_t Data)
- {
- while((USART1->SR & USART_FLAG_TXE) == RESET);
- USART1->DR = (Data & (uint16_t)0x01FF);
- }
- /************************************************
- 函数名称 : USART1_SendNByte
- 功 能 : 串口1发送N个字符
- 参 数 : pData ----- 字符串
- Length --- 长度
- 返 回 值 : 无
- 作 者 : sun
- *************************************************/
- void USART1_SendNByte(uint8_t *pData, uint16_t Length)
- {
- while(Length--)
- {
- USART1_SendByte(*pData);
- pData++;
- }
- }
- /************************************************
- 函数名称 : USART1_Printf
- 功 能 : 串口1打印输出
- 参 数 : string --- 字符串
- 返 回 值 : 无
- 作 者 : sun
- *************************************************/
- void USART1_Printf(uint8_t *String)
- {
- while((*String) != '\0')
- {
- USART1_SendByte(*String);
- String++;
- }
- }
- /************************************************
- 函数名称 : USART1_SendChar
- 功 能 : 串口1发送一个字符
- 参 数 : Data --- 数据
- 返 回 值 : 无
- 作 者 : sun
- *************************************************/
- void USART2_SendByte(uint8_t Data)
- {
- while((USART2->SR & USART_FLAG_TXE) == RESET);
- USART2->DR = (Data & (uint16_t)0x01FF);
- }
- /************************************************
- 函数名称 : USART1_SendNByte
- 功 能 : 串口1发送N个字符
- 参 数 : pData ----- 字符串
- Length --- 长度
- 返 回 值 : 无
- 作 者 : sun
- *************************************************/
- void USART2_SendNByte(uint8_t *pData, uint16_t Length)
- {
- while(Length--)
- {
- USART2_SendByte(*pData);
- pData++;
- }
- }
- /************************************************
- 函数名称 : USART1_Printf
- 功 能 : 串口1打印输出
- 参 数 : string --- 字符串
- 返 回 值 : 无
- 作 者 : sun
- *************************************************/
- void USART2_Printf(uint8_t *String)
- {
- while((*String) != '\0')
- {
- USART2_SendByte(*String);
- String++;
- }
- }
-
- /************************************************
- 函数名称 : UART4_SendChar
- 功 能 : 串口4发送一个字符
- 参 数 : Data --- 数据
- 返 回 值 : 无
- 作 者 : sun
- *************************************************/
- void UART4_SendByte(uint8_t Data)
- {
- while((UART4->SR & USART_FLAG_TXE) == RESET);
- UART4->DR = (Data & (uint16_t)0x01FF);
- }
- /************************************************
- 函数名称 : USART4_SendNByte
- 功 能 : 串口4发送N个字符
- 参 数 : pData ----- 字符串
- Length --- 长度
- 返 回 值 : 无
- 作 者 : sun
- *************************************************/
- void UART4_SendNByte(uint8_t *pData, uint16_t Length)
- {
- while(Length--)
- {
- UART4_SendByte(*pData);
- pData++;
- }
- }
- /************************************************
- 函数名称 : USART4_Printf
- 功 能 : 串口4打印输出
- 参 数 : string --- 字符串
- 返 回 值 : 无
- 作 者 : sun
- *************************************************/
- void UART4_Printf(uint8_t *String)
- {
- while((*String) != '\0')
- {
- UART4_SendByte(*String);
- String++;
- }
- }
-
- /************************************************
- 函数名称 : UART5_SendChar
- 功 能 : 串口5发送一个字符
- 参 数 : Data --- 数据
- 返 回 值 : 无
- 作 者 : sun
- *************************************************/
- void UART5_SendByte(uint8_t Data)
- {
- while((UART5->SR & USART_FLAG_TXE) == RESET);
- UART5->DR = (Data & (uint16_t)0x01FF);
- }
- /************************************************
- 函数名称 : USART4_SendNByte
- 功 能 : 串口5发送N个字符
- 参 数 : pData ----- 字符串
- Length --- 长度
- 返 回 值 : 无
- 作 者 : sun
- *************************************************/
- void UART5_SendNByte(uint8_t *pData, uint16_t Length)
- {
- while(Length--)
- {
- UART5_SendByte(*pData);
- pData++;
- }
- }
- /************************************************
- 函数名称 : USART4_Printf
- 功 能 : 串口5打印输出
- 参 数 : string --- 字符串
- 返 回 值 : 无
- 作 者 : sun
- *************************************************/
- void UART5_Printf(uint8_t *String)
- {
- while((*String) != '\0')
- {
- UART5_SendByte(*String);
- String++;
- }
- }
- void USART_printfHex(const uint8_t* SendChars,uint16_t len)
- {
- uint16_t i = 0;
- // uint8_t temp = 0;
- for(i=0;i<len;i++)
- {
- printf("%02X",SendChars[i]);
- }
- printf("\r\n");
- }
- #define CHAR_TO_UPPER(ch) ((ch >= 'a' && ch <= 'z')?(ch-0x20):ch)
- /**
- * @brief ascii convert hex
- * @par param[in] *hex:hex data
- * @par param[in] *ascii:ascii data
- * @par param[in] asciiLen:length of ascii
- * @retval length
- */
- uint8_t Ascii2Hex(uint8_t *hex, uint8_t *ascii, uint8_t asciiLen)
- {
- uint8_t i,ch,value;
- value = 0;
- for(i=0;i<(asciiLen>>1);i++) {
- ch = CHAR_TO_UPPER(ascii[i*2]);
- if(ch >= '0' && ch <= '9') {
- value = ch -'0';
- }
- else if(ch >= 'A' && ch <= 'F') {
- value = ch - 'A' + 0x0A;
- }
- else {
- return i;
- }
- hex[i] = (value<<4);
- ch = CHAR_TO_UPPER(ascii[i*2+1]);
- if(ch >= '0' && ch <= '9') {
- value = ch -'0';
- }
- else if(ch >= 'A' && ch <= 'F') {
- value = ch - 'A' + 0x0A;
- }
- else {
- return i;
- }
- hex[i] += value;
- }
- return i;
- }
- /**
- * @brief hex convert ascii
- * @par param[in] *ascii:ascii data
- * @par param[in] *hex:hex data
- * @par param[in] hexLen:length of hex
- * @retval length
- */
- uint8_t Hex2Ascii(uint8_t *ascii, uint8_t *hex, uint8_t hexLen)
- {
- uint8_t i, value;
- for(i=0;i<hexLen;i++) {
- value = (hex[i]>>4);
- if(value > 9) {
- value += 0x07;
- }
- ascii[2*i] = value+0x30;
- value = (hex[i]&0x0F);
- if(value > 9) {
- value += 0x07;
- }
- ascii[2*i+1] = value+0x30;
- }
- return hexLen*2;
- }
- /**** Copyright (C)2016 sun. All Rights Reserved **** END OF FILE ****/
|