| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009 |
- /**
- ******************************** STM32F10x *********************************
- * @文件名 : device.c
- * @作者 : sun
- * @库版本 : V3.5.0
- * @文件版本 : V1.0.0
- * @日期 : 2016年05月09日
- * @摘要 : device源文件
- ******************************************************************************/
- /*----------------------------------------------------------------------------
- 更新日志:
- 2016-05-09 V1.0.0:初始版本
- ----------------------------------------------------------------------------*/
- /* 包含的头文件 --------------------------------------------------------------*/
- #include "usart.h"
- #include "led.h"
- #include "queue.h"
- #include <string.h>
- /********************uart1****************************/
- uint8_t USART1_receive_buf[USART1_BUF_SIZE],USART1_ready_buf[USART1_BUF_SIZE];
- static int32_t USART1_ReceiveTimeCounter = 0;
- volatile uint16_t USART1_receive_index=0;
- volatile uint8_t USART1_ready_buf_ok = 0;
- volatile uint16_t USART1_ready_buf_len = 0;
- uint8_t USART2_receive_buf[USART2_BUF_SIZE],USART2_ready_buf[USART2_BUF_SIZE];
- static int32_t USART2_ReceiveTimeCounter = 0;
- volatile uint16_t USART2_receive_index=0;
- volatile uint8_t USART2_ready_buf_ok = 0;
- volatile uint16_t USART2_ready_buf_len = 0;
- uint8_t UART4_receive_buf[UART4_BUF_SIZE],UART4_ready_buf[UART4_BUF_SIZE];
- static int32_t UART4_ReceiveTimeCounter = 0;
- volatile uint16_t UART4_receive_index=0;
- volatile uint8_t UART4_ready_buf_ok = 0;
- volatile uint8_t UART4_ready_buf_len = 0;
- sequeue_t *usart2_rx_sq;
- sequeue_t *uart4_rx_sq;
- sequeue_t *uart4_tx_sq;
- sequeue_t *usart1_rx_sq;
- sequeue_t *usart1_tx_sq;
- sequeue_t *uart5_rx_sq;
- sequeue_t *uart5_tx_sq;
- uint8_t UART5_receive_buf[UART5_BUF_SIZE],UART5_ready_buf[UART5_BUF_SIZE];
- static int32_t UART5_ReceiveTimeCounter = 0;
- volatile uint16_t UART5_receive_index=0;
- volatile uint8_t UART5_ready_buf_ok = 0;
- volatile uint16_t UART5_ready_buf_len = 0;
- volatile uint8_t m_state_code=M_STATE_IDLE;
- #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)&((USART2->SR&0X40)==0))
- {
- temp--;
- };//循环发送,直到发送完毕
- //while((USART2->SR & USART_FLAG_TXE) == RESET);
- USART2->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_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(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_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();
-
- USART2_GPIO_Configuration();
- USART2_Configuration();
-
-
- USART4_GPIO_Configuration();
- USART4_Configuration();
-
-
- USART5_GPIO_Configuration();
- USART5_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++;
- }
- }
- /*UART1中断处理函数*/
- void USART1_IRQHandler(void)
- {
- if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
- {
- USART1_ReceiveTimeCounter = USART1_RECEIVE_OVERTIME;
- if(USART1_receive_index >= USART1_BUF_SIZE)
- USART1_receive_index = 0;
-
- USART1_receive_buf[USART1_receive_index++] = (uint8_t)USART_ReceiveData(USART1);
-
- }
-
- if (USART_GetFlagStatus(USART1, USART_FLAG_ORE) != RESET)
- {
- (void)USART_ReceiveData(USART1);
- }
- USART_ClearFlag(USART1,USART_FLAG_TC);
- }
- /************************************************
- 函数名称 : 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++;
- }
- }
- /*UART1中断处理函数*/
- void USART2_IRQHandler(void)
- {
- uint8_t temp=0;
- //LED0=!LED0;
- if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
- {
- USART2_ReceiveTimeCounter = USART2_RECEIVE_OVERTIME;
- if(USART2_receive_index >= USART2_BUF_SIZE)
- USART2_receive_index = 0;
-
- temp = (uint8_t)USART_ReceiveData(USART2);
- USART2_receive_buf[USART2_receive_index++] = temp;
- Update_Queue(usart2_rx_sq,0,temp);
- if(temp==0x0d)
- Update_Queue(usart2_rx_sq,1,0);
-
- }
-
- if (USART_GetFlagStatus(USART2, USART_FLAG_ORE) != RESET)
- {
- (void)USART_ReceiveData(USART2);
- }
- USART_ClearFlag(USART2,USART_FLAG_TC);
- }
-
- /************************************************
- 函数名称 : 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++;
- }
- }
- /*UART4中断处理函数*/
- void UART4_IRQHandler(void)
- {
- //LED0=!LED0;
- if (USART_GetITStatus(UART4, USART_IT_RXNE) != RESET)
- {
- UART4_ReceiveTimeCounter = UART4_RECEIVE_OVERTIME;
- if(UART4_receive_index >= UART4_BUF_SIZE)
- UART4_receive_index = 0;
-
- UART4_receive_buf[UART4_receive_index++] = (uint8_t)USART_ReceiveData(UART4);
-
- }
-
- if (USART_GetFlagStatus(UART4, USART_FLAG_ORE) != RESET)
- {
- (void)USART_ReceiveData(UART4);
- }
- USART_ClearFlag(UART4,USART_FLAG_TC);
- }
-
- /************************************************
- 函数名称 : 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++;
- }
- }
- /*UART5中断处理函数*/
- void UART5_IRQHandler(void)
- {
- //LED0=!LED0;
- if (USART_GetITStatus(UART5, USART_IT_RXNE) != RESET)
- {
- UART5_ReceiveTimeCounter = UART5_RECEIVE_OVERTIME;
- if(UART5_receive_index >= UART5_BUF_SIZE)
- UART5_receive_index = 0;
-
- UART5_receive_buf[UART5_receive_index++] = (uint8_t)USART_ReceiveData(UART5);
-
- }
-
- if (USART_GetFlagStatus(UART5, USART_FLAG_ORE) != RESET)
- {
- (void)USART_ReceiveData(UART5);
- }
- USART_ClearFlag(UART5,USART_FLAG_TC);
- }
- /************************************************
- 函数名称 : USART_ReceiveOvertimeProcess
- 功 能 : 串口超时接收
- 参 数 :
- 返 回 值 : 无
- 作 者 : sun
- *************************************************/
- void USART_ReceiveOvertimeProcess(void)
- {
- uint16_t i;
- // uint16_t j;
- // uint16_t h,t;
- uint8_t frame_num=0;
- uint8_t frame_head=0;
- uint8_t frame_len=0;
- /********************uart1****************************/
- if(USART1_ReceiveTimeCounter>=SYSTEMTICK_PERIOD_MS)
- {
- USART1_ReceiveTimeCounter -= SYSTEMTICK_PERIOD_MS;
- if(USART1_ReceiveTimeCounter<SYSTEMTICK_PERIOD_MS)
- {
- //若1包里面含有多个帧,拆开入队
- frame_head = 0;
- frame_num = 0;
- frame_len = 0;
- //
- start_led(3,USART1_receive_index,99,10*10);//(led,delay*0.1ms,duty,period*0.1ms)
- for(i = 0;i < USART1_receive_index; i ++){
- if(USART1_receive_buf[i] == 0xBB){
- if(frame_len>0)
- En_Queue(usart1_rx_sq,USART1_receive_buf+frame_head,sizeof(USART1_receive_buf)-frame_head,frame_len);//数据入队,可多次调用
-
- frame_head = i;
- frame_num++;
- frame_len=1;
- }
- else
- frame_len++;
- }
- if(frame_num > 0)
- En_Queue(usart1_rx_sq,USART1_receive_buf+frame_head,sizeof(USART1_receive_buf)-frame_head,frame_len);//数据入队,可多次调用
-
-
- DEBUG1("\r\nUSART1 rx=");
- DEBUG2(USART1_receive_buf,USART1_receive_index);
- //DEBUG("\r\n");
-
- USART1_receive_index = 0;
- USART1_ReceiveTimeCounter = 0;
-
- }
- }
- if(USART2_ReceiveTimeCounter>=SYSTEMTICK_PERIOD_MS)
- {
- USART2_ReceiveTimeCounter -= SYSTEMTICK_PERIOD_MS;
- if(USART2_ReceiveTimeCounter<SYSTEMTICK_PERIOD_MS)
- {
- if(USART2_ready_buf_ok == 0)
- {
- USART2_ready_buf_len = USART2_receive_index;
- for(i = 0;i <= USART2_ready_buf_len; i ++)
- USART2_ready_buf[i] = USART2_receive_buf[i];
- USART2_ready_buf[USART2_ready_buf_len] = '\0';
- USART2_ready_buf_ok = 1;
- USART2_receive_index=0;
- USART2_ReceiveTimeCounter = 0;
- }
- else
- {
- USART2_ReceiveTimeCounter = 0;
- }
- }
-
- }
- if(UART4_ReceiveTimeCounter>=SYSTEMTICK_PERIOD_MS)
- {
- UART4_ReceiveTimeCounter -= SYSTEMTICK_PERIOD_MS;
- if(UART4_ReceiveTimeCounter<SYSTEMTICK_PERIOD_MS)
- {
- //若1包里面含有多个帧,拆开入队
- frame_head = 0;
- frame_num = 0;
- frame_len = 0;
-
- for(i = 0;i < UART4_receive_index; i ++){
- if(UART4_receive_buf[i] == 0xBB){
- if(frame_len>0)
- En_Queue(uart4_rx_sq,UART4_receive_buf+frame_head,sizeof(UART4_receive_buf)-frame_head,frame_len);//数据入队,可多次调用
-
- frame_head = i;
- frame_num++;
- frame_len=1;
- }
- else
- frame_len++;
- }
- if(frame_num > 0)
- En_Queue(uart4_rx_sq,UART4_receive_buf+frame_head,sizeof(UART4_receive_buf)-frame_head,frame_len);//数据入队,可多次调用
-
-
- DEBUG1("\r\nUART4 rx=");
- DEBUG2(UART4_receive_buf,UART4_receive_index);
- //DEBUG("\r\n");
-
- UART4_receive_index = 0;
- UART4_ReceiveTimeCounter = 0;
-
- }
- }
-
-
- if(UART5_ReceiveTimeCounter>=SYSTEMTICK_PERIOD_MS)
- {
- UART5_ReceiveTimeCounter -= SYSTEMTICK_PERIOD_MS;
- if(UART5_ReceiveTimeCounter<SYSTEMTICK_PERIOD_MS)
- {
- //若1包里面含有多个帧,拆开入队
- frame_head = 0;
- frame_num = 0;
- frame_len = 0;
- //
- start_led(2,UART5_receive_index,99,10*10);//(led,delay*0.1ms,duty,period*0.1ms)
- for(i = 0;i < UART5_receive_index; i ++){
- if(UART5_receive_buf[i] == 0xBB){
- if(frame_len>0)
- En_Queue(uart5_rx_sq,UART5_receive_buf+frame_head,sizeof(UART5_receive_buf)-frame_head,frame_len);//数据入队,可多次调用
-
- frame_head = i;
- frame_num++;
- frame_len=1;
- }
- else
- frame_len++;
- }
- if(frame_num > 0)
- En_Queue(uart5_rx_sq,UART5_receive_buf+frame_head,sizeof(UART5_receive_buf)-frame_head,frame_len);//数据入队,可多次调用
-
-
- DEBUG1("\r\nUART5 rx=");
- DEBUG2(UART5_receive_buf,UART5_receive_index);
- //DEBUG("\r\n");
-
- UART5_receive_index = 0;
- UART5_ReceiveTimeCounter = 0;
-
- }
- }
- }
- 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;
- }
- sequeue_t *usart2_Rcv_sq_init(void)
- {
- usart2_rx_sq = Create_Empty_Sequeue(2,128);
- DEBUG_F("usart2_rx_sq: SQ=0x%08x,depth=%d,width=%d\r\n",(uint32_t)usart2_rx_sq,usart2_rx_sq->depth,usart2_rx_sq->width);
- if(usart2_rx_sq==NULL)
- DEBUG_F("error,usart2_rx_sq\r\n");
- return usart2_rx_sq;
- }
- sequeue_t *uart4_Rcv_sq_init(void)
- {
- uart4_rx_sq = Create_Empty_Sequeue(2,128);
- DEBUG_F("uart4_rx_sq: SQ=0x%08x,depth=%d,width=%d\r\n",(uint32_t)uart4_rx_sq,uart4_rx_sq->depth,uart4_rx_sq->width);
- if(uart4_rx_sq==NULL)
- DEBUG_F("error,uart4_rx_sq\r\n");
- return uart4_rx_sq;
- }
- sequeue_t *uart4_Send_sq_init(void)
- {
- uart4_tx_sq = Create_Empty_Sequeue(2,128);
- DEBUG_F("uart4_rx_sq: SQ=0x%08x,depth=%d,width=%d\r\n",(uint32_t)uart4_rx_sq,uart4_rx_sq->depth,uart4_rx_sq->width);
- if(uart4_tx_sq==NULL)
- DEBUG_F("error,uart4_tx_sq\r\n");
- return uart4_rx_sq;
- }
- sequeue_t *usart1_Rcv_sq_init(void)
- {
- usart1_rx_sq = Create_Empty_Sequeue(2,128);
- DEBUG_F("usart1_rx_sq: SQ=0x%08x,depth=%d,width=%d\r\n",(uint32_t)usart1_rx_sq,usart1_rx_sq->depth,usart1_rx_sq->width);
- if(usart1_rx_sq==NULL)
- DEBUG_F("error,usart1_rx_sq\r\n");
- return uart4_rx_sq;
- }
- sequeue_t *usart1_Send_sq_init(void)
- {
- usart1_tx_sq = Create_Empty_Sequeue(2,128);
- DEBUG_F("usart1_tx_sq: SQ=0x%08x,depth=%d,width=%d\r\n",(uint32_t)usart1_tx_sq,usart1_tx_sq->depth,usart1_tx_sq->width);
- if(usart1_tx_sq==NULL)
- DEBUG_F("error,usart1_tx_sq\r\n");
- return uart4_rx_sq;
- }
- sequeue_t *uart5_Rcv_sq_init(void)
- {
- uart5_rx_sq = Create_Empty_Sequeue(2,128);
- DEBUG_F("uart5_rx_sq: SQ=0x%08x,depth=%d,width=%d\r\n",(uint32_t)uart5_rx_sq,uart5_rx_sq->depth,uart5_rx_sq->width);
- if(uart5_rx_sq==NULL)
- DEBUG_F("error,uart5_rx_sq\r\n");
- return uart4_rx_sq;
- }
- sequeue_t *uart5_Send_sq_init(void)
- {
- uart5_tx_sq = Create_Empty_Sequeue(2,128);
- DEBUG_F("uart5_tx_sq: SQ=0x%08x,depth=%d,width=%d\r\n",(uint32_t)uart5_tx_sq,uart5_tx_sq->depth,uart5_tx_sq->width);
- if(uart5_tx_sq==NULL)
- DEBUG_F("error,uart5_Send_sq_init\r\n");
- return uart4_rx_sq;
- }
- /************************************************
- 函数名称 : 应用层函数,UsartToLora(非阻塞函数)
- 功 能 : 串口与LOra透出啊数据
- 参 数 :
- 返 回 值 :
- 作 者 : sun
- *************************************************/
- void UsartToLora(void )
- {
- // uint8_t BUFFER[MAXLOADLEN] ;
- uint8_t sout[128];
- int8_t lout=0;
- // uint8_t tmp;
- // uint8_t i;
- // int16_t bufferLen = 0;
-
- if(!Check_Seqeue_Empty(usart2_rx_sq))
- {
- lout=0;
- memset(sout,0,128);
-
- De_Queue(usart2_rx_sq,sout,sizeof(sout),(uint8_t *)&lout);
-
- if(strstr((char *)sout,"help")||strstr((char *)sout,"?")){
- printf("指令后面必须加回车:\r\n");
- printf("输入\"help or ?\",打印帮助信息\r\n");
-
- printf("输入\"erase app1\",擦除,app1\r\n");
- printf("输入\"erase app2\",擦除,app2\r\n");
- printf("输入\"erase app3\",擦除,app3\r\n");
-
- printf("输入\"copy app1->app2\",复制并校验,从app1 到app2\r\n");
- printf("输入\"copy app3->app1\",复制并校验,从app3 到app1\r\n");
- printf("输入\"copy app2->app1\",复制并校验,从app2 到app1\r\n");
-
- printf("输入\"run app1\",运行,run app1\r\n");
- printf("输入\"get info\",获取信息\r\n");
- printf("输入\"reset\",重启\r\n");
-
- }
- if(strstr((char *)sout,"erase app1")){
- printf("擦除,app1\r\n");
- m_state_code = M_STATE_ERASE_APP1;
- }
- if(strstr((char *)sout,"erase app2")){
- printf("擦除,app2\r\n");
- m_state_code = M_STATE_ERASE_APP2;
- }
- if(strstr((char *)sout,"erase app3")){
- printf("擦除,app3\r\n");
- m_state_code = M_STATE_ERASE_APP3;
- }
-
- if(strstr((char *)sout,"copy app1->app2")){
- printf("复制,copy app1->app2\r\n");
- m_state_code = M_STATE_CP_APP1_APP2;
- }
-
-
- if(strstr((char *)sout,"copy app3->app1")){
- printf("复制,copy app3->app1\r\n");
- m_state_code = M_STATE_CP_APP3_APP1;
- }
-
- if(strstr((char *)sout,"copy app2->app1")){
- printf("复制,copy app2->app1\r\n");
- m_state_code = M_STATE_CP_APP2_APP1;
- }
-
- if(strstr((char *)sout,"run app1")){
- printf("运行,run app1\r\n");
- m_state_code = M_STATE_RUN_APP1;
- }
-
- if(strstr((char *)sout,"get info")){
- printf("获取信息,get info\r\n");
- m_state_code = M_STATE_GET_INFO;
- }
-
- if(strstr((char *)sout,"reset")){
- printf("重启,reset\r\n");
- m_state_code = M_STATE_RST;
- }
- }
-
- }
- /**** Copyright (C)2016 sun. All Rights Reserved **** END OF FILE ****/
|