270 lines
7.7 KiB
C
270 lines
7.7 KiB
C
/******************** (C) COPYRIGHT 2023 GeekRebot *****************************
|
||
* File Name : bluetooth.c
|
||
* Current Version : V1.0 & ST 3.5.0
|
||
* Author : zhanli 719901725@qq.com
|
||
* Date of Issued : 2023.04.06 zhanli: Create
|
||
* Comments : GeekRebot 蓝牙驱动模块,使用HC05蓝牙模块,串口透传功能,
|
||
和使用串口差不多,使用stm32 的uart2。蓝牙模块只需要接四
|
||
个接口,TXD,RXD,5V,GND
|
||
1. PA2 -> 蓝牙RXD 2.PA3 -> 蓝牙TXD
|
||
********************************************************************************/
|
||
#include "stdarg.h"
|
||
#include "stdio.h"
|
||
#include "bluetooth.h"
|
||
#include <stdio.h>
|
||
|
||
|
||
volatile unsigned char *rx2_address;
|
||
volatile unsigned int rx2_count;
|
||
volatile unsigned int rx2_length;
|
||
|
||
/**----------------------------------------------------------------------
|
||
* Function : Bluetooth_Init
|
||
* Description : 串口1初始化函数,PA2->RXD, PA3->TXD, bound为设置波特率
|
||
* Author : zhanli&719901725@qq.com
|
||
* Date : 2023/05/20 zhanli
|
||
*---------------------------------------------------------------------**/
|
||
void Bluetooth_Init(u32 bound)
|
||
{
|
||
GPIO_InitTypeDef GPIO_InitStructure;
|
||
USART_InitTypeDef USART_InitStructure;
|
||
NVIC_InitTypeDef NVIC_InitStructure;
|
||
|
||
/* Config USART2 clock */
|
||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
||
|
||
/* USART2 GPIO config */
|
||
/* Configure USART2 Tx (PA.02) as alternate function push-pull */
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||
/* Configure USART2 Rx (PA.03) as input floating */
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
||
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||
|
||
/* USART2 mode config */
|
||
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);
|
||
|
||
|
||
/* Configure the NVIC Preemption Priority Bits */
|
||
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
|
||
|
||
/* Enable the USARTy Interrupt */
|
||
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
|
||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||
NVIC_Init(&NVIC_InitStructure);
|
||
|
||
/* 使能串口1接收中断 */
|
||
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
|
||
USART_Cmd(USART2, ENABLE);
|
||
}
|
||
|
||
/**----------------------------------------------------------------------
|
||
* Function : USART2_Send
|
||
* Description : 蓝牙通过USART2发送到手机
|
||
* Author : zhanli&719901725@qq.com
|
||
* Date : 2023/05/20 zhanli
|
||
*---------------------------------------------------------------------**/
|
||
void USART2_Send(unsigned char *tx_buf, int len)
|
||
{
|
||
USART_ClearFlag(USART2, USART_FLAG_TC);
|
||
USART_ClearITPendingBit(USART2, USART_FLAG_TXE);
|
||
while(len--)
|
||
{
|
||
USART_SendData(USART2, *tx_buf);
|
||
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) != 1);
|
||
USART_ClearFlag(USART2, USART_FLAG_TC);
|
||
USART_ClearITPendingBit(USART2, USART_FLAG_TXE);
|
||
tx_buf++;
|
||
}
|
||
}
|
||
|
||
|
||
/**----------------------------------------------------------------------
|
||
* Function : USART2_Send
|
||
* Description : 蓝牙通过USART2发送到手机
|
||
* Author : zhanli&719901725@qq.com
|
||
* Date : 2023/05/20 zhanli
|
||
*---------------------------------------------------------------------**/
|
||
void USART2_SendStr(unsigned char *tx_buf)
|
||
{
|
||
USART_ClearFlag(USART2, USART_FLAG_TC);
|
||
USART_ClearITPendingBit(USART2, USART_FLAG_TXE);
|
||
while(*tx_buf != 0)
|
||
{
|
||
USART_SendData(USART2, *tx_buf++);
|
||
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) != 1);
|
||
USART_ClearFlag(USART2, USART_FLAG_TC);
|
||
USART_ClearITPendingBit(USART2, USART_FLAG_TXE);
|
||
}
|
||
}
|
||
|
||
static char *itoa(int value, char *string, int radix)
|
||
{
|
||
int i, d;
|
||
int flag = 0;
|
||
char *ptr = string;
|
||
|
||
/* This implementation only works for decimal numbers. */
|
||
if (radix != 10)
|
||
{
|
||
*ptr = 0;
|
||
return string;
|
||
}
|
||
|
||
if (!value)
|
||
{
|
||
*ptr++ = 0x30;
|
||
*ptr = 0;
|
||
return string;
|
||
}
|
||
|
||
/* if this is a negative value insert the minus sign. */
|
||
if (value < 0)
|
||
{
|
||
*ptr++ = '-';
|
||
|
||
/* Make the value positive. */
|
||
value *= -1;
|
||
}
|
||
|
||
for (i = 10000; i > 0; i /= 10)
|
||
{
|
||
d = value / i;
|
||
|
||
if (d || flag)
|
||
{
|
||
*ptr++ = (char)(d + 0x30);
|
||
value -= (d * i);
|
||
flag = 1;
|
||
}
|
||
}
|
||
|
||
/* Null terminate the string. */
|
||
*ptr = 0;
|
||
|
||
return string;
|
||
|
||
} /* NCL_Itoa */
|
||
|
||
void PB_USART_printf(USART_TypeDef* USARTx, uint8_t *Data,...)
|
||
{
|
||
const char *s;
|
||
int d;
|
||
char buf[18];
|
||
|
||
va_list ap;
|
||
va_start(ap, Data);
|
||
|
||
USART_ClearFlag(USART2, USART_FLAG_TC);
|
||
USART_ClearITPendingBit(USART2, USART_FLAG_TXE);
|
||
|
||
while ( *Data != 0)
|
||
{
|
||
if ( *Data == 0x5c ) //'\'
|
||
{
|
||
switch ( *++Data )
|
||
{
|
||
case 'r':
|
||
USART_SendData(USARTx, 0x0d);
|
||
Data ++;
|
||
break;
|
||
|
||
case 'n':
|
||
USART_SendData(USARTx, 0x0a);
|
||
Data ++;
|
||
break;
|
||
|
||
default:
|
||
Data ++;
|
||
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 );
|
||
USART_ClearFlag(USART2, USART_FLAG_TC);
|
||
USART_ClearITPendingBit(USART2, USART_FLAG_TXE);
|
||
}
|
||
}
|
||
|
||
/**----------------------------------------------------------------------
|
||
* Function : USART2_Receive
|
||
* Description : 蓝牙接受,从手机接收
|
||
* Author : zhanli&719901725@qq.com
|
||
* Date : 2023/05/20 zhanli
|
||
*---------------------------------------------------------------------**/
|
||
void USART2_Receive(unsigned char *rx_buf, int len)
|
||
{
|
||
rx2_count = 0;
|
||
rx2_length = len;
|
||
rx2_address = rx_buf;
|
||
}
|
||
|
||
|
||
/**----------------------------------------------------------------------
|
||
* Function : USART2_Receive
|
||
* Description : 串口2中断函数
|
||
* Author : zhanli&719901725@qq.com
|
||
* Date : 2023/05/20 zhanli
|
||
*---------------------------------------------------------------------**/
|
||
void USART2_IRQHandler(void)
|
||
{
|
||
unsigned char Res;
|
||
// 接收中断(接收到的数据必须是0x0d 0x0a结尾)
|
||
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
|
||
{
|
||
Res = USART_ReceiveData(USART2);
|
||
|
||
if(rx2_length > rx2_count)
|
||
{
|
||
*rx2_address = Res;
|
||
rx2_address++;
|
||
rx2_count++;
|
||
}
|
||
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
|
||
}
|
||
}
|