You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
4.7 KiB

#include "sys.h"
#include "usart.h"
//////////////////////////////////////////////////////////////////////////////////
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>ucos,<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
#if SYSTEM_SUPPORT_OS
#include "includes.h" //ucos ʹ<EFBFBD><EFBFBD>
#endif
//////////////////////////////////////////////////////////////////////////////////
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD>ѧϰʹ<EFBFBD>ã<EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>κ<EFBFBD><EFBFBD><EFBFBD>;
//ALIENTEK STM32<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><EFBFBD>@ALIENTEK
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̳:www.openedv.com
//<EFBFBD>޸<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:2012/8/18
//<EFBFBD><EFBFBD><EFBFBD>V1.5
//<EFBFBD><EFBFBD>Ȩ<EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ؾ<EFBFBD><EFBFBD><EFBFBD>
//Copyright(C) <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿƼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>޹<EFBFBD>˾ 2009-2019
//All rights reserved
//********************************************************************************
//V1.3<EFBFBD>޸<EFBFBD>˵<EFBFBD><EFBFBD>
//֧<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ͬƵ<EFBFBD><EFBFBD><EFBFBD>µĴ<EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˶<EFBFBD>printf<EFBFBD><EFBFBD>֧<EFBFBD><EFBFBD>
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˴<EFBFBD><EFBFBD>ڽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>printf<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>bug
//V1.4<EFBFBD>޸<EFBFBD>˵<EFBFBD><EFBFBD>
//1,<EFBFBD>޸Ĵ<EFBFBD><EFBFBD>ڳ<EFBFBD>ʼ<EFBFBD><EFBFBD>IO<EFBFBD><EFBFBD>bug
//2,<EFBFBD>޸<EFBFBD><EFBFBD><EFBFBD>USART_RX_STA,ʹ<EFBFBD>ô<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><EFBFBD><EFBFBD>Ϊ2<EFBFBD><EFBFBD>14<EFBFBD>η<EFBFBD>
//3,<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>USART_REC_LEN,<EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><EFBFBD>ֽ<EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2<EFBFBD><EFBFBD>14<EFBFBD>η<EFBFBD>)
//4,<EFBFBD>޸<EFBFBD><EFBFBD><EFBFBD>EN_USART1_RX<EFBFBD><EFBFBD>ʹ<EFBFBD>ܷ<EFBFBD>ʽ
//V1.5<EFBFBD>޸<EFBFBD>˵<EFBFBD><EFBFBD>
//1,<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˶<EFBFBD>UCOSII<EFBFBD><EFBFBD>֧<EFBFBD><EFBFBD>
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>´<EFBFBD><EFBFBD><EFBFBD>,֧<EFBFBD><EFBFBD>printf<EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫѡ<EFBFBD><EFBFBD>use MicroLIB
#if 1
#pragma import(__use_no_semihosting)
//<EFBFBD><EFBFBD>׼<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD>֧<EFBFBD>ֺ<EFBFBD><EFBFBD><EFBFBD>
struct __FILE
{
int handle;
};
FILE __stdout;
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>_sys_exit()<EFBFBD>Ա<EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ð<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
_sys_exit(int x)
{
x = x;
}
//<EFBFBD>ض<EFBFBD><EFBFBD><EFBFBD>fputc<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);//ѭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,ֱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
USART1->DR = (u8) ch;
return ch;
}
#endif
void UART1_SendString(char* s, int length)
{
int i = 0;
for(i = 0; i < length; i++){
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
USART_SendData(USART1 ,*(s + i));//<EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>ǰ<EFBFBD>ַ<EFBFBD>
}
}
#if EN_USART1_RX //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD>˽<EFBFBD><EFBFBD><EFBFBD>
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD>жϷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//ע<EFBFBD><EFBFBD>,<EFBFBD><EFBFBD>ȡUSARTx->SR<EFBFBD>ܱ<EFBFBD><EFBFBD><EFBFBD>Ī<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>
u8 USART_RX_BUF[USART_REC_LEN]; //<EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD>USART_REC_LEN<EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>.
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
//bit15<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɱ<EFBFBD>־
//bit14<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD>0x0d
//bit13~0<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD>ֽ<EFBFBD><EFBFBD><EFBFBD>Ŀ
u16 USART_RX_STA=0; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void uart_init(u32 bound){
//GPIO<EFBFBD>˿<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //ʹ<EFBFBD><EFBFBD>USART1<EFBFBD><EFBFBD>GPIOAʱ<EFBFBD><EFBFBD>
//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; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_Init(GPIOA, &GPIO_InitStructure);//<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>GPIOA.9
//USART1_RX GPIOA.10<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_Init(GPIOA, &GPIO_InitStructure);//<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>GPIOA.10
//Usart1 NVIC <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//<EFBFBD><EFBFBD>ռ<EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨ<EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>
NVIC_Init(&NVIC_InitStructure); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>VIC<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>
//USART <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
USART_InitStructure.USART_BaudRate = bound;//<EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//<EFBFBD>ֳ<EFBFBD>Ϊ8λ<EFBFBD><EFBFBD><EFBFBD>ݸ<EFBFBD>ʽ
USART_InitStructure.USART_StopBits = USART_StopBits_1;//һ<EFBFBD><EFBFBD>ֹͣλ
USART_InitStructure.USART_Parity = USART_Parity_No;//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>żУ<EFBFBD><EFBFBD>λ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//<EFBFBD><EFBFBD>Ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //<EFBFBD>շ<EFBFBD>ģʽ
USART_Init(USART1, &USART_InitStructure); //<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
USART_Cmd(USART1, ENABLE); //ʹ<EFBFBD>ܴ<EFBFBD><EFBFBD><EFBFBD>1
}
void USART1_IRQHandler(void) //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD>жϷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
u8 Res;
#if SYSTEM_SUPPORT_OS //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>SYSTEM_SUPPORT_OSΪ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ֧<EFBFBD><EFBFBD>OS.
OSIntEnter();
#endif
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>(<EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0x0d 0x0a<EFBFBD><EFBFBD>β)
{
Res =USART_ReceiveData(USART1); //<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if((USART_RX_STA&0x8000)==0)//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
if(USART_RX_STA&0x4000)//<EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><EFBFBD><EFBFBD>0x0d
{
if(Res!=0x0a)USART_RX_STA=0;//<EFBFBD><EFBFBD><EFBFBD>մ<EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD>¿<EFBFBD>ʼ
else USART_RX_STA|=0x8000; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
else //<EFBFBD><EFBFBD>û<EFBFBD>յ<EFBFBD>0X0D
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD>¿<EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
}
}
}
#if SYSTEM_SUPPORT_OS //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>SYSTEM_SUPPORT_OSΪ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ֧<EFBFBD><EFBFBD>OS.
OSIntExit();
#endif
}
#endif