RobotHardware-UESTC/Hardware/银星机器人底盘/PiRobot-YH_Firmware v1.0/STM32/BSPLIB/usart.c

333 lines
10 KiB
C
Raw Normal View History

2024-01-20 13:19:09 +08:00
#ifdef __cplusplus
extern "C" {
2024-01-24 22:08:32 +08:00
#endif
2024-01-20 13:19:09 +08:00
#include "usart.h"
#include "nvic.h"
2024-01-24 22:08:32 +08:00
void USART_InitGPIO(uint8_t USART_Channel, uint32_t BaudRate, uint8_t GPIO_AF)
2024-01-20 13:19:09 +08:00
{
2024-01-24 22:08:32 +08:00
// GPIO config
USART_TypeDef *USARTx;
2024-01-20 13:19:09 +08:00
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
2024-01-24 22:08:32 +08:00
if (USART_Channel == 1){
2024-01-20 13:19:09 +08:00
USARTx = USART1;
}
2024-01-24 22:08:32 +08:00
else if (USART_Channel == 2){
2024-01-20 13:19:09 +08:00
USARTx = USART2;
}
2024-01-24 22:08:32 +08:00
else if (USART_Channel == 3){
2024-01-20 13:19:09 +08:00
USARTx = USART3;
}
2024-01-24 22:08:32 +08:00
else if (USART_Channel == 4){
2024-01-20 13:19:09 +08:00
USARTx = UART4;
}
2024-01-24 22:08:32 +08:00
else if (USART_Channel == 5){
2024-01-20 13:19:09 +08:00
USARTx = UART5;
2024-01-24 22:08:32 +08:00
}else{
2024-01-20 13:19:09 +08:00
return;
}
if(USARTx==USART1){
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOB, ENABLE);
if(GPIO_AF == 0)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1_TX PA.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; //USART1_RX PA.10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
2024-01-24 22:08:32 +08:00
else if (GPIO_AF == 1)
2024-01-20 13:19:09 +08:00
{
2024-01-24 22:08:32 +08:00
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; // USART1_TX PB6
2024-01-20 13:19:09 +08:00
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
2024-01-24 22:08:32 +08:00
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; // USART1_RX PB7
2024-01-20 13:19:09 +08:00
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
2024-01-24 22:08:32 +08:00
GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE); // use Remapping
2024-01-20 13:19:09 +08:00
}
USART1_NVIC_Configuration();
}
2024-01-24 22:08:32 +08:00
else if (USARTx == USART2)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOD, ENABLE);
2024-01-20 13:19:09 +08:00
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
2024-01-24 22:08:32 +08:00
if (GPIO_AF == 0)
2024-01-20 13:19:09 +08:00
{
2024-01-24 22:08:32 +08:00
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // USART2_TX PA.2
2024-01-20 13:19:09 +08:00
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
2024-01-24 22:08:32 +08:00
2024-01-20 13:19:09 +08:00
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
2024-01-24 22:08:32 +08:00
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // USART2_RX PA.3
2024-01-20 13:19:09 +08:00
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
2024-01-24 22:08:32 +08:00
else if (GPIO_AF == 1)
2024-01-20 13:19:09 +08:00
{
2024-01-24 22:08:32 +08:00
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; // USART2_TX PD5
2024-01-20 13:19:09 +08:00
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
2024-01-24 22:08:32 +08:00
2024-01-20 13:19:09 +08:00
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
2024-01-24 22:08:32 +08:00
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // USART2_RX PD6
2024-01-20 13:19:09 +08:00
GPIO_Init(GPIOD, &GPIO_InitStructure);
2024-01-24 22:08:32 +08:00
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE); // use Remapping
2024-01-20 13:19:09 +08:00
}
2024-01-24 22:08:32 +08:00
2024-01-20 13:19:09 +08:00
USART2_NVIC_Configuration();
}
2024-01-24 22:08:32 +08:00
else if (USARTx == USART3)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE);
2024-01-20 13:19:09 +08:00
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
2024-01-24 22:08:32 +08:00
if (GPIO_AF == 0)
2024-01-20 13:19:09 +08:00
{
2024-01-24 22:08:32 +08:00
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // USART3_TX PB.10
2024-01-20 13:19:09 +08:00
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
2024-01-24 22:08:32 +08:00
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; // USART3_RX PB.11
2024-01-20 13:19:09 +08:00
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
2024-01-24 22:08:32 +08:00
else if (GPIO_AF == 1)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // USART3_TX PC.10
2024-01-20 13:19:09 +08:00
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
2024-01-24 22:08:32 +08:00
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; // USART3_RX PC.11
2024-01-20 13:19:09 +08:00
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
2024-01-24 22:08:32 +08:00
GPIO_PinRemapConfig(GPIO_FullRemap_USART3, ENABLE); // use Remapping
2024-01-20 13:19:09 +08:00
}
2024-01-24 22:08:32 +08:00
else if (GPIO_AF == 2)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; // USART3_TX PD8
2024-01-20 13:19:09 +08:00
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
2024-01-24 22:08:32 +08:00
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // USART3_RX PD9
2024-01-20 13:19:09 +08:00
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_InitStructure);
2024-01-24 22:08:32 +08:00
GPIO_PinRemapConfig(GPIO_FullRemap_USART3, ENABLE); // use Remapping
2024-01-20 13:19:09 +08:00
}
USART3_NVIC_Configuration();
}
2024-01-24 22:08:32 +08:00
else if (USARTx == UART4)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
2024-01-20 13:19:09 +08:00
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
2024-01-24 22:08:32 +08:00
if (GPIO_AF == 0)
2024-01-20 13:19:09 +08:00
{
2024-01-24 22:08:32 +08:00
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // USART4_TX PC.10
2024-01-20 13:19:09 +08:00
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
2024-01-24 22:08:32 +08:00
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; // USART4_RX PC.11
2024-01-20 13:19:09 +08:00
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
UART4_NVIC_Configuration();
}
2024-01-24 22:08:32 +08:00
else if (USARTx == UART5)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);
2024-01-20 13:19:09 +08:00
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);
2024-01-24 22:08:32 +08:00
if (GPIO_AF == 0)
2024-01-20 13:19:09 +08:00
{
2024-01-24 22:08:32 +08:00
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; // USART4_TX PC.12
2024-01-20 13:19:09 +08:00
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
2024-01-24 22:08:32 +08:00
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // USART4_RX PD.2
2024-01-20 13:19:09 +08:00
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
UART5_NVIC_Configuration();
}
2024-01-24 22:08:32 +08:00
USART_InitStructure.USART_BaudRate = BaudRate;
2024-01-20 13:19:09 +08:00
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;
2024-01-24 22:08:32 +08:00
2024-01-20 13:19:09 +08:00
USART_Init(USARTx, &USART_InitStructure);
2024-01-24 22:08:32 +08:00
// enable interrupt
USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE);
2024-01-20 13:19:09 +08:00
USART_ClearITPendingBit(USARTx, USART_IT_RXNE);
2024-01-24 22:08:32 +08:00
// enable usart
USART_Cmd(USARTx, ENABLE);
2024-01-20 13:19:09 +08:00
}
2024-01-24 22:08:32 +08:00
void USART_PutChar(uint8_t USART_Channel, unsigned char Tx_Byte)
2024-01-20 13:19:09 +08:00
{
2024-01-24 22:08:32 +08:00
USART_TypeDef *USARTx;
2024-01-20 13:19:09 +08:00
2024-01-24 22:08:32 +08:00
if (USART_Channel == 1)
{
2024-01-20 13:19:09 +08:00
USARTx = USART1;
}
2024-01-24 22:08:32 +08:00
else if (USART_Channel == 2)
{
2024-01-20 13:19:09 +08:00
USARTx = USART2;
}
2024-01-24 22:08:32 +08:00
else if (USART_Channel == 3)
{
2024-01-20 13:19:09 +08:00
USARTx = USART3;
}
2024-01-24 22:08:32 +08:00
else if (USART_Channel == 4)
{
2024-01-20 13:19:09 +08:00
USARTx = UART4;
}
2024-01-24 22:08:32 +08:00
else if (USART_Channel == 5)
{
2024-01-20 13:19:09 +08:00
USARTx = UART5;
}
2024-01-24 22:08:32 +08:00
else
{
2024-01-20 13:19:09 +08:00
return;
}
2024-01-24 22:08:32 +08:00
USART_SendData(USARTx, Tx_Byte);
while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET)
;
2024-01-20 13:19:09 +08:00
}
static char *itoa(int value, char *string, int radix)
{
2024-01-24 22:08:32 +08:00
int i, d;
int flag = 0;
char *ptr = string;
2024-01-20 13:19:09 +08:00
/* This implementation only works for decimal numbers. */
if (radix != 10)
{
*ptr = 0;
return string;
}
2024-01-24 22:08:32 +08:00
2024-01-20 13:19:09 +08:00
if (!value)
{
*ptr++ = 0x30;
*ptr = 0;
return string;
}
2024-01-24 22:08:32 +08:00
2024-01-20 13:19:09 +08:00
/* if this is a negative value insert the minus sign. */
if (value < 0)
{
*ptr++ = '-';
2024-01-24 22:08:32 +08:00
2024-01-20 13:19:09 +08:00
/* Make the value positive. */
value *= -1;
}
2024-01-24 22:08:32 +08:00
2024-01-20 13:19:09 +08:00
for (i = 10000; i > 0; i /= 10)
{
d = value / i;
2024-01-24 22:08:32 +08:00
2024-01-20 13:19:09 +08:00
if (d || flag)
{
*ptr++ = (char)(d + 0x30);
value -= (d * i);
flag = 1;
}
}
2024-01-24 22:08:32 +08:00
2024-01-20 13:19:09 +08:00
/* Null terminate the string. */
*ptr = 0;
2024-01-24 22:08:32 +08:00
2024-01-20 13:19:09 +08:00
return string;
2024-01-24 22:08:32 +08:00
2024-01-20 13:19:09 +08:00
} /* NCL_Itoa */
2024-01-24 22:08:32 +08:00
void USART_printf(USART_TypeDef *USARTx, uint8_t *Data, ...)
2024-01-20 13:19:09 +08:00
{
const char *s;
int d;
char buf[16];
2024-01-24 22:08:32 +08:00
2024-01-20 13:19:09 +08:00
va_list ap;
va_start(ap, Data);
2024-01-24 22:08:32 +08:00
while (*Data != 0) // 判断是否到达字符串结束符
2024-01-20 13:19:09 +08:00
{
2024-01-24 22:08:32 +08:00
if (*Data == 0x5c) //'\'
2024-01-20 13:19:09 +08:00
{
2024-01-24 22:08:32 +08:00
switch (*++Data)
2024-01-20 13:19:09 +08:00
{
2024-01-24 22:08:32 +08:00
case 'r': // 回车符
2024-01-20 13:19:09 +08:00
USART_SendData(USARTx, 0x0d);
2024-01-24 22:08:32 +08:00
Data++;
2024-01-20 13:19:09 +08:00
break;
2024-01-24 22:08:32 +08:00
case 'n': // 换行符
2024-01-20 13:19:09 +08:00
USART_SendData(USARTx, 0x0a);
2024-01-24 22:08:32 +08:00
Data++;
2024-01-20 13:19:09 +08:00
break;
2024-01-24 22:08:32 +08:00
2024-01-20 13:19:09 +08:00
default:
2024-01-24 22:08:32 +08:00
Data++;
2024-01-20 13:19:09 +08:00
break;
}
}
else if ( *Data == '%')
{
switch ( *++Data )
{
case 's': //字符串
s = va_arg(ap, const char *);
for ( ; *s; s++)
{
USART_SendData(USARTx,*s);
while( USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET );
}
Data++;
break;
case 'd': //十进制
d = va_arg(ap, int);
itoa(d, buf, 10);
for (s = buf; *s; s++)
{
USART_SendData(USARTx,*s);
while( USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET );
}
Data++;
break;
default:
Data++;
break;
}
} /* end of else if */
else USART_SendData(USARTx, *Data++);
while( USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET );
}
}
#ifdef __cplusplus
}
2024-01-24 22:08:32 +08:00
#endif