GeekIMU/2.Firmware/STM32/Firmware/system/usart/usart.c

137 lines
3.7 KiB
C
Raw Normal View History

2024-11-09 21:39:20 +08:00
/******************** (C) COPYRIGHT 2020 GEEKIMU *******************************
* File Name : usart.c
* Current Version : V2.0 & ST 3.5.0
* Author : zhanli 719901725@qq.com & JustFeng.
* Date of Issued : 2019.5.26 zhanli : Create
* Comments : STM32 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
********************************************************************************/
#include "sys.h"
#include "usart.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_usart.h"
#include "stm32f10x_rcc.h"
#if 1
#pragma import(__use_no_semihosting)
//<2F><>׼<EFBFBD><D7BC><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>֧<EFBFBD>ֺ<EFBFBD><D6BA><EFBFBD>
struct __FILE {
int handle;
};
FILE __stdout;
//<2F><><EFBFBD><EFBFBD>_sys_exit()<29>Ա<EFBFBD><D4B1><EFBFBD>ʹ<EFBFBD>ð<EFBFBD><C3B0><EFBFBD><EFBFBD><EFBFBD>ģʽ
_sys_exit(int x)
{
x = x;
}
//<2F>ض<EFBFBD><D8B6><EFBFBD>fputc<74><63><EFBFBD><EFBFBD>
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);//ѭ<><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
USART1->DR = (u8) ch;
return ch;
}
#endif
#if EN_USART1_RX //<2F><><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD>˽<EFBFBD><CBBD><EFBFBD>
//ע<><D7A2>,<2C><>ȡUSARTx->SR<53>ܱ<EFBFBD><DCB1><EFBFBD>Ī<EFBFBD><C4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>
u8 USART_RX_BUF[USART_REC_LEN]; //<2F><><EFBFBD>ջ<EFBFBD><D5BB><EFBFBD>,<2C><><EFBFBD><EFBFBD>USART_REC_LEN<45><4E><EFBFBD>ֽ<EFBFBD>.
//<2F><><EFBFBD><EFBFBD>״̬
//bit15<31><35> <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɱ<EFBFBD>־
//bit14<31><34> <09><><EFBFBD>յ<EFBFBD>0x0d
//bit13~0<><30> <09><><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>Ŀ
u16 USART_RX_STA = 0; //<2F><><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD>
/**----------------------------------------------------------------------
* Function : uart_init
* Description : STM32 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Input : bound : <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Author : zhanli&719901725@qq.com
* Date : 2019/5/26 zhanli
*---------------------------------------------------------------------**/
void uart_init(u32 bound){
//GPIO<49>˿<EFBFBD><CBBF><EFBFBD><EFBFBD><EFBFBD>
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
// ʹ<><CAB9>USART1<54><31>GPIOAʱ<41><CAB1>
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);
// <20><>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>1
USART_DeInit(USART1);
// USART1_TX PA.9 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// USART1_RX PA.10 <20><><EFBFBD>ø<EFBFBD><C3B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// USART <20><>ʼ<EFBFBD><CABC>,һ<><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ9600;
USART_InitStructure.USART_BaudRate = bound;
// <20>ֳ<EFBFBD>Ϊ8λ<38><CEBB><EFBFBD>ݸ<EFBFBD>ʽ
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
// һ<><D2BB>ֹͣλ
USART_InitStructure.USART_StopBits = USART_StopBits_1;
// <20><><EFBFBD><EFBFBD>żУ<C5BC><D0A3>λ
USART_InitStructure.USART_Parity = USART_Parity_No;
// <20><>Ӳ<EFBFBD><D3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //<2F>շ<EFBFBD>ģʽ
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
USART_Init(USART1, &USART_InitStructure);
// <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
// ʹ<>ܴ<EFBFBD><DCB4><EFBFBD>
USART_Cmd(USART1, ENABLE);
}
/**----------------------------------------------------------------------
* Function : USART1_IRQHandler
* Description : <EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD>жϷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Author : zhanli&719901725@qq.com
* Date : 2019/5/26 zhanli
*---------------------------------------------------------------------**/
void USART1_IRQHandler(void)
{
u8 Res;
//<2F><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>(<28><><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݱ<EFBFBD><DDB1><EFBFBD><EFBFBD><EFBFBD>0x0d 0x0a<30><61>β)
if(USART_GetITStatus(USART1, USART_IT_RXNE) == RESET)
return;
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Res = USART_ReceiveData(USART1);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if((USART_RX_STA & 0x8000) != 0)
return;
//<2F><><EFBFBD>յ<EFBFBD><D5B5><EFBFBD>0x0d
if(USART_RX_STA & 0x4000)
{
if(Res!=0x0a)USART_RX_STA=0;//<2F><><EFBFBD>մ<EFBFBD><D5B4><EFBFBD>,<2C><><EFBFBD>¿<EFBFBD>ʼ
else USART_RX_STA|=0x8000; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}else //<2F><>û<EFBFBD>յ<EFBFBD>0X0D
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD>,<2C><><EFBFBD>¿<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;
}
}
}
#endif