123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- #include "sys.h"
- #include "usart.h"
- #include "string.h"
- #include <stdarg.h>
- #include "delay.h"
- #include "stdio.h"
- #include "stdlib.h"
- #include "stdio.h"
- #include "oled.h"
- #define Usart3ReadLen 120
- char Usart3ReadBuff[Usart3ReadLen]={0};
- u32 Usart3ReadCnt = 0;
- u32 Usart3IdleCnt = 0;
- #define Usart1ReadLen 100
- char Usart1ReadBuff[Usart1ReadLen]={0};
- u32 Usart1ReadCnt = 0;
- #define Usart2ReadLen 100
- char Usart2ReadBuff[Usart2ReadLen]={0};
- u32 Usart2ReadCnt = 0;
- #if 1
- #pragma import(__use_no_semihosting)
- struct __FILE
- {
- int handle;
- };
- FILE __stdout;
- _sys_exit(int x)
- {
- x = x;
- }
- int fputc(int ch, FILE *f)
- {
- while((USART1->SR&0X40)==0);
- USART1->DR = (u8) ch;
- return ch;
- }
- #endif
-
- void uart_init(u32 bound){
-
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);
-
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
-
- USART_InitStructure.USART_BaudRate = bound;
- 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_ITConfig(USART1, USART_IT_IDLE, ENABLE);
- USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
- USART_Cmd(USART1, ENABLE);
- }
- void uart3_init(u32 bound)
- {
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
-
- NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0 ;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
-
- 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);
-
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
-
- USART_InitStructure.USART_BaudRate = bound;
- 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(USART3, &USART_InitStructure);
-
- USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);
- USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
- USART_Cmd(USART3,ENABLE);
- }
- void uart2_init(u32 bound)
- {
-
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
-
-
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- USART_InitStructure.USART_BaudRate = bound;
- 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(USART2, &USART_InitStructure);
-
- NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
- USART_ITConfig(USART2, USART_IT_IDLE, ENABLE);
- USART_Cmd(USART2, ENABLE);
-
- }
-
- void USART2_SendByte(u8 data)
- {
-
- while((USART2->SR&0X40)==0);
- USART2->DR = (u8) data;
- }
- void USART2_String(char *pdat,int len)
- {
- TIM_Cmd(TIM4, DISABLE);
- while(len>0)
- {
- USART2_SendByte(*pdat++);
- len--;
- }
- TIM_Cmd(TIM4, ENABLE);
- }
- void USART3_SendByte(u8 data)
- {
-
- while((USART3->SR&0X40)==0);
- USART3->DR = (u8) data;
- }
- void USART3_String(char *pdat,int len)
- {
- TIM_Cmd(TIM4, DISABLE);
- while(len>0)
- {
- USART3_SendByte(*pdat++);
- len--;
- }
- TIM_Cmd(TIM4, ENABLE);
- }
- void USART1_SendByte(u8 data)
- {
-
- while((USART1->SR&0X40)==0);
- USART1->DR = (u8) data;
- }
- void USART1_String(char *pdat,int len)
- {
- TIM_Cmd(TIM4, DISABLE);
- while(len>0)
- {
- USART1_SendByte(*pdat++);
- len--;
- }
- TIM_Cmd(TIM4, ENABLE);
- }
- char String_Sub(char *pcBuf, char *pcRes,const char *pStart,const char *pEnd);
- void USART2_IRQHandler(void)
- {
- u8 Res=0,rxes;
- if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
- {
- Res =USART_ReceiveData(USART2);
- if(Usart2ReadCnt < Usart2ReadLen-1)
- {
- Usart2ReadBuff[Usart2ReadCnt] = Res;
- }
- else
- {
- Usart2ReadCnt=0;
- }
- Usart2ReadCnt ++;
-
- }
- if(USART_GetITStatus(USART2, USART_IT_IDLE) != RESET)
- {
-
- rxes = USART2->SR;
- rxes = USART2->DR;
-
- Usart2ReadCnt=0;
- memset(Usart2ReadBuff,0,Usart2ReadLen);
- }
-
- }
- char String_Sub(char *pcBuf, char *pcRes,const char *pStart,const char *pEnd)
- {
-
- char *pcBegin = NULL;
- char *pcEnd = NULL;
- pcBegin = strstr((char*)pcBuf,pStart);
- pcEnd = strstr((char*)pcBegin+strlen(pStart)+1, pEnd);
- if(pcBegin == NULL || pcEnd == NULL || pcBegin > pcEnd)
- {
- return 0;
- }
- else
- {
- pcBegin += strlen(pStart);
-
- strncpy(pcRes, pcBegin, pcEnd-pcBegin);
- }
- return 1;
- }
- void USART3_IRQHandler(void)
- {
- u8 Res=0,rxes;
- if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
- {
- Res =USART_ReceiveData(USART3);
- if(Usart3ReadCnt < Usart3ReadLen-1)
- {
- Usart3ReadBuff[Usart3ReadCnt] = Res;
- }
- else
- {
- Usart3ReadCnt=0;
- }
- Usart3ReadCnt ++;
- Usart3IdleCnt = 0;
- }
- if(USART_GetITStatus(USART3, USART_IT_IDLE) != RESET)
- {
-
- rxes = USART3->SR;
- rxes = USART3->DR;
- USART1_String(Usart3ReadBuff,Usart3ReadCnt);
-
-
- Usart3ReadCnt=0;
- }
- }
- void USART1_IRQHandler(void)
- {
- u8 Res,rxes;
- if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
- {
- Res =USART_ReceiveData(USART1);
- if(Usart1ReadCnt < Usart1ReadLen-1)
- {
- Usart1ReadBuff[Usart1ReadCnt] = Res;
- }
- else
- {
- Usart1ReadCnt=0;
- }
- Usart1ReadCnt ++;
- }
- if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
- {
-
-
- rxes = USART1->SR;
- rxes = USART1->DR;
- printf("%s",Usart1ReadBuff);
- Usart1ReadCnt=0;
-
- }
- }
-
|