#include “sys.h”
#include “usart.h”
#include “stm32f10x_usart.h”
#include “LedControl.h”
//
//Èç¹ûʹÓÃucos,Ôò°üÀ¨ÏÂÃæµÄÍ·Îļþ¼´¿É.
#if SYSTEM_SUPPORT_OS
#include “includes.h” //ucos ʹÓÃ
#endif
//
//¼ÓÈëÒÔÏ´úÂë,Ö§³Öprintfº¯Êý,¶ø²»ÐèҪѡÔñuse MicroLIB
#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)
{
while((USART1->SR&0X40)==0);//Ñ»··¢ËÍ,Ö±µ½·¢ËÍÍê±Ï
USART1->DR = (u8) ch;
return ch;
}
#endif
/ʹÓÃmicroLibµÄ·½·¨/
/*
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (uint8_t) ch);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}
return ch;
}
int GetKey (void) {
while (!(USART1->SR & USART_FLAG_RXNE));
return ((int)(USART1->DR & 0x1FF));
}
*/
//#if EN_USART1_RX //Èç¹ûʹÄÜÁ˽ÓÊÕ
//´®¿Ú1ÖжϷþÎñ³ÌÐò
//×¢Òâ,¶ÁÈ¡USARTx->SRÄܱÜÃâĪÃûÆäÃîµÄ´íÎó
u8 USART_RX_BUF[USART_REC_LEN]; //½ÓÊÕ»º³å,×î´óUSART_REC_LEN¸ö×Ö½Ú.
//½ÓÊÕ״̬
//bit15£¬ ½ÓÊÕÍê³É±êÖ¾
//bit14£¬ ½ÓÊÕµ½0x0d
//bit13~0£¬ ½ÓÊÕµ½µÄÓÐЧ×Ö½ÚÊýÄ¿
u16 USART_RX_STA=0; //½ÓÊÕ״̬±ê¼Ç
void uart_init(u32 bound){
//GPIO¶Ë¿ÚÉèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //ʹÄÜUSART1£¬GPIOAʱÖÓ
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
//*************´®¿ÚDMA·¢ËÍ¡¢½ÓÊÕ
//DMA·¢ËÍÖжÏÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//DMA1ͨµÀ4ÅäÖÃ
DMA_DeInit(DMA1_Channel4);
//ÍâÉèµØÖ·
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);
//ÄÚ´æµØÖ·
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Uart_Send_Buffer;
//dma´«Êä·½Ïòµ¥Ïò
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
//ÉèÖÃDMAÔÚ´«Êäʱ»º³åÇøµÄ³¤¶È
DMA_InitStructure.DMA_BufferSize = 100;
//ÉèÖÃDMAµÄÍâÉèµÝÔöģʽ£¬Ò»¸öÍâÉè
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
//ÉèÖÃDMAµÄÄÚ´æµÝÔöģʽ
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
//ÍâÉèÊý¾Ý×Ö³¤
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
//ÄÚ´æÊý¾Ý×Ö³¤
DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;
//ÉèÖÃDMAµÄ´«Êäģʽ
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
//ÉèÖÃDMAµÄÓÅÏȼ¶±ð
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
//ÉèÖÃDMAµÄ2¸ömemoryÖеıäÁ¿»¥Ïà·ÃÎÊ
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4,&DMA_InitStructure);
DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE);
//ʹÄÜͨµÀ4
//DMA_Cmd(DMA1_Channel4, ENABLE);
//´®¿ÚÊÕDMAÅäÖÃ
//Æô¶¯DMAʱÖÓ
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
//DMA1ͨµÀ5ÅäÖÃ
DMA_DeInit(DMA1_Channel5);
//ÍâÉèµØÖ·
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);
//ÄÚ²ãµØÖ·
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Uart_Rx;
//dma´«Êä·½Ïòµ¥Ïò
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
//ÉèÖÃDMAÔÚ´«Êäʱ»º³åÇøµÄ³¤¶È
DMA_InitStructure.DMA_BufferSize = UART_RX_LEN;
//ÉèÖÃDMAµÄÍâÉèµÝÔöģʽ£¬Ò»¸öÍâÉè
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
//ÉèÖÃDMAÄÚ´æµÝÔöģʽ
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
//ÍâÉèÊý¾Ý×Ö³¤
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
//ÄÚ´æÊý¾Ý×Ö³¤
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
//ÉèÖÃDMAµÄ´«Êäģʽ
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
//ÉèÖÃDMAµÄÓÅÏȼ¶±ð
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
//ÉèÖÃDMAµÄ2¸ömemoryÖеıäÁ¿»¥Ïà·ÃÎ
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel5,&DMA_InitStructure);
//ʹÄÜͨµÀ5
DMA_Cmd(DMA1_Channel5,ENABLE);
//ÖжÏÅäÖÃ
USART_ITConfig(USART1,USART_IT_TC,DISABLE);
USART_ITConfig(USART1,USART_IT_RXNE,DISABLE);
USART_ITConfig(USART1,USART_IT_IDLE,ENABLE);
//ʹÄÜDMA·½Ê½·¢ËÍ
USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);
//ʹÄÜDMA·½Ê½½ÓÊÕ
USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
//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; //¸´ÓÃÍÆÍìÊä³ö
GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA.9
//USART1_RX GPIOA.10³õʼ»¯
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë
GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA.10
//Usart1 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//ÇÀÕ¼ÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×ÓÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷
//USART ³õʼ»¯ÉèÖÃ
USART_InitStructure.USART_BaudRate = bound;//´®¿Ú²¨ÌØÂÊ
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö³¤Îª8λÊý¾Ý¸ñʽ
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); //³õʼ»¯´®¿Ú1
// USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//¿ªÆô´®¿Ú½ÓÊÜÖжÏ
// USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_Cmd(USART1, ENABLE); //ʹÄÜ´®¿Ú1
// USART_ITConfig(USART1, USART_IT_TC, ENABLE);//Tramsimssion Completeºó£¬²Å²úÉúÖжϣ¬¿ªTCÖжϱØÐë·ÅÓÚ´Ë£¬·ñÔò»¹ÊǻᶪʧµÚÒ»×Ö½Ú
}
void Uart1_Send_PVID(int Order)
{
u8 SendData[4];
int i;
SendData[0] = 0xFE;
SendData[1] = 0xEF;
switch(Order)
{
case KBUart1: SendData[0] = 0xFE;
SendData[1] = 0xEF;
SendData[2] = 0x7F;
SendData[3] = 0x68;
for(i= 0;i < 4;i++)
{
Uart1_Send_Byte(SendData[i]);
UartTimeDelay(2);
}
break;
case KBVersion:
SendData[0] = 0x02;
SendData[1] = 0x27;
SendData[2] = 0xB1;
SendData[3] = 0x02;
for(i= 0;i < 4;i++)
{
Uart1_Send_Byte(SendData[i]);
UartTimeDelay(2);
}
break;
}
}
void Uart1_Send_Byte(u8 Data)
{
// USART_ClearFlag(USART1, USART_FLAG_TC); //·¢ËÍÍê³É±ê־룬·¢ËÍÍêµÚÒ»¸ö×Ö½Úºó²Å½øÈëÖжϣ¬ÐèÒªÏÈÇå³ý£¬·ñÔò¿ÉÄܻᶪʧµÚÒ»¸ö×Ö½ÚÊý¾Ý
USART_SendData(USART1,(u8)Data);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
// USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
// if( USART_GetITStatus(USART1, USART_IT_TXE) == SET )
// {
// if( Data == ‘\0’ )
// {
// USART_ClearFlag(USART1, USART_FLAG_TC);
// }
// else
// USART_SendData(USART1,(u8)Data);
// }
// USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
// while( Data != ‘\0’ )
// {
// while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
// USART_SendData(USART1,(u8)Data);
// }
}
void USART1_IRQHandler(void) //´®¿Ú1ÖжϷþÎñ³ÌÐò
{
int Data;
// int Uart1_Tx=0;
// int Uart1_Rx=0;
// int Uart1_Sta=0;
// u8 Uart1_Buffer[4]; //ÓÉÓÚ´®¿Úÿ´ÎÖ»½ÓÊÕ1¸ö×Ö½Ú£¬Ã¿À´Ò»¸ö×Ö½Ú½øÒ»´ÎÖжϣ¬ËùÒÔÒªÏë¶à´Î½ÓÊÕ£¬buffer¼°Ïà¹Ø±äÁ¿¶¨Òå Ðë·Åµ½ÖжϺ¯ÊýÍâÃæ
/********************** DMA·¢Ëͻضà´Î·¢ËÍ ,»¹ÐèÒªµ÷ÊÔ ***************************/
uint32_t temp = 0;
uint16_t i = 0;
// printf("%s\r\n",“enter int”);
if (USART_GetFlagStatus(USART1, USART_FLAG_PE) != RESET)
{
USART_ReceiveData(USART1);
USART_ClearFlag(USART1, USART_FLAG_PE);
}
if (USART_GetFlagStatus(USART1, USART_FLAG_ORE) != RESET)
{
USART_ReceiveData(USART1);
USART_ClearFlag(USART1, USART_FLAG_ORE);
}
if (USART_GetFlagStatus(USART1, USART_FLAG_FE) != RESET)
{
USART_ReceiveData(USART1);
USART_ClearFlag(USART1, USART_FLAG_FE);
}
if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
{
USART_ClearFlag(USART1,USART_IT_IDLE);
temp = USART1->SR;
temp = USART1->DR; //ÇåUSART_IT_IDLE±ê×¢
DMA_Cmd(DMA1_Channel5,DISABLE);
temp = UART_RX_LEN - DMA_GetCurrDataCounter(DMA1_Channel5);
for (i = 0;i < temp;i++)
{
Data = Uart_Rx[i];
//Æô¶¯´®¿Ú״̬»ú
// usart_state_run(); //ÒÑÏ¿ÉÒÔµ¥¶À·âװΪ״̬»ú³ÌÐò£¬ÓÃÀ´½ÓÊÕ
switch(ReceiveState)
{
// Data =USART_ReceiveData(USART1); //¶ÁÈ¡½ÓÊÕµ½µÄÊý¾Ý
case RecSta1:
if(Data == 0xFE)
{
ReceiveState = RecSta2;
// Uart1_Send_Byte(0xEF);
// printf("%s\r\n",“EF”);
}
else
ReceiveState = RecSta1;
break;
case RecSta2:
if(Data == 0xEF)
{
ReceiveState = RecSta3;
// Uart1_Send_Byte(0xFE);
// printf("%s\r\n",“FE”);
}
else
ReceiveState = RecSta1;
break;
case RecSta3:
if(Data == 0x7F)
{
// printf("%s\r\n",“7F”);
// Uart1_Send_Byte(0x7F);
ReceiveState = RecSta5;
}
else
{
LedChannel = Data;
ReceiveState = RecSta4;
}
break;
case RecSta4:
ReceiveFlag = 1;
LedState = Data;
ReceiveState = RecSta1;
break;
case RecSta5:
if(Data == 0x8F)
{
KBUartPIDAck=1;
// printf("%s\r\n",“8F”);
// Uart1_Send_PVID(KBUart1);
// Uart1_Send_Byte(0xEF);
// Uart1_Send_Byte(0xFE);
// Uart1_Send_Byte(0x7F);
// Uart1_Send_Byte(0x68);
// Uart_Send_Buffer[0]=0xFE;
// Uart_Send_Buffer[1]=0xEF;
// Uart_Send_Buffer[2]=0x7F;
// Uart_Send_Buffer[3]=0x68;
}
else if(Data == 0xF8)
{
KBUartVIDAck=1;
// printf("%s\r\n",“F8”);
// Uart1_Send_PVID(KBVersion);
// Uart1_Send_Byte(0x02);
// Uart1_Send_Byte(0x25);
// Uart1_Send_Byte(0xB1);
// Uart1_Send_Byte(0x01);
// Uart_Send_Buffer[0]=0x03;
// Uart_Send_Buffer[1]=0x02;
// Uart_Send_Buffer[2]=0xB1;
// Uart_Send_Buffer[3]=0x03;
}
ReceiveState = RecSta1; //·¢ËÍÍê4×Ö½ÚÒԺ󣬱ØÐë»Øµ½sta1,·ñÔòºóÐøÎÞ·¨½øÈë½ÓÊÕ
break;
default:
ReceiveState = RecSta1;
break;
}
}
// temp=0;
//ÉèÖô«ÊäÊý¾Ý³¤¶È
// DMA_SetCurrDataCounter(DMA1_Channel5,UART_RX_LEN);
//´ò¿ªDMA
DMA_Cmd(DMA1_Channel5,ENABLE);
}
/********************* ·¢ËÍÐèÒª·Åµ½½ÓÊÕÒÔÍ⣬·ñÔò»á¶à´Î·¢ËÍ **************************/
if(KBUartPIDAck==1)
{
KBUartPIDAck=0;
// printf("%s\r\n",“EFFE7F68”);
// Uart1_Send_PVID(KBUart1);
Uart1_Send_Byte(0xEF);
Uart1_Send_Byte(0xFE);
Uart1_Send_Byte(0x7F);
Uart1_Send_Byte(0x68);
}
if(KBUartVIDAck==1)
{
KBUartVIDAck=0;
// printf("%s\r\n",“0223B100”);
// Uart1_Send_PVID(KBVersion);
Uart1_Send_Byte(0x02);
Uart1_Send_Byte(0x25);
Uart1_Send_Byte(0xB1);
Uart1_Send_Byte(0x01);
}
__nop();
//
// if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //½ÓÊÕÖжÏ
// {
USART_ClearFlag(USART1, USART_FLAG_RXNE);
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
printf("%s\r\n",“Enter_receive”);
//
// Data =USART_ReceiveData(USART1); //¶ÁÈ¡½ÓÊÕµ½µÄÊý¾Ý
// switch(ReceiveState)
// {
Data =USART_ReceiveData(USART1); //¶ÁÈ¡½ÓÊÕµ½µÄÊý¾Ý
// case RecSta1:
// if(Data == 0xFE)
// {
// ReceiveState = RecSta2;
Uart1_Send_Byte(0xEF);
printf("%s\r\n",“EF”);
// }
// else
// ReceiveState = RecSta1;
// break;
// case RecSta2:
// if(Data == 0xEF)
// {
// ReceiveState = RecSta3;
Uart1_Send_Byte(0xFE);
printf("%s\r\n",“FE”);
// }
// else
// ReceiveState = RecSta1;
// break;
// case RecSta3:
// if(Data == 0x7F)
// {
printf("%s\r\n",“7F”);
Uart1_Send_Byte(0x7F);
// ReceiveState = RecSta5;
// }
// else
// {
// LedChannel = Data;
// ReceiveState = RecSta4;
// }
// break;
// case RecSta4:
// ReceiveFlag = 1;
// LedState = Data;
// ReceiveState = RecSta1;
//
// break;
// case RecSta5:
// if(Data == 0x8F)
// {
KBUartPIDAck=1;
printf("%s\r\n",“8F”);
Uart1_Send_PVID(KBUart1);
// Uart1_Send_Byte(0xFE);
// Uart1_Send_Byte(0xEF);
// Uart1_Send_Byte(0x7F);
// Uart1_Send_Byte(0x68);
//
// }
// else if(Data == 0xF8)
// {
KBUartVIDAck=1;
printf("%s\r\n",“F8”);
Uart1_Send_PVID(KBVersion);
// Uart1_Send_Byte(0x02);
// Uart1_Send_Byte(0x27);
// Uart1_Send_Byte(0xB1);
// Uart1_Send_Byte(0x02);
// }
//
// ReceiveState = RecSta1; //·¢ËÍÍê4×Ö½ÚÒԺ󣬱ØÐë»Øµ½sta1,·ñÔòºóÐøÎÞ·¨½øÈë½ÓÊÕ
// break;
//
// default:
// ReceiveState = RecSta1;
// break;
// }
// }
LedConfigure.X86_ControlLed(); //X86µãµÆ
// if(KBUartPIDAck1)
// {
// KBUartPIDAck=0;
printf("%s\r\n",“EFFE7F68”);
Uart1_Send_PVID(KBUart1);
// Uart1_Send_Byte(0xEF);
// Uart1_Send_Byte(0xFE);
// Uart1_Send_Byte(0x7F);
// Uart1_Send_Byte(0x68);
// }
// if(KBUartVIDAck1)
// {
// KBUartVIDAck=0;
printf("%s\r\n",“0223B100”);
Uart1_Send_PVID(KBVersion);
// Uart1_Send_Byte(0x02);
// Uart1_Send_Byte(0x25);
// Uart1_Send_Byte(0xB1);
// Uart1_Send_Byte(0x01);
// }
}
void UartTimeDelay(int ms)
{
UartSendTime = 0;
while(UartSendTime < ms);
}
//#endif