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 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD>,ʹ<EFBFBD><EFBFBD>HC05<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD>飬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܣ<EFBFBD>
|
|||
|
<EFBFBD><EFBFBD>ʹ<EFBFBD>ô<EFBFBD><EFBFBD>ڲ<EFBFBD>࣬ʹ<EFBFBD><EFBFBD>stm32 <EFBFBD><EFBFBD>uart2<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
<EFBFBD><EFBFBD><EFBFBD>ӿڣ<EFBFBD>TXD<EFBFBD><EFBFBD>RXD<EFBFBD><EFBFBD>5V<EFBFBD><EFBFBD>GND
|
|||
|
1. PA2 -> <EFBFBD><EFBFBD><EFBFBD><EFBFBD>RXD 2.PA3 -> <EFBFBD><EFBFBD><EFBFBD><EFBFBD>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 : <EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,PA2->RXD, PA3->TXD, boundΪ<EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* 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);
|
|||
|
|
|||
|
/* ʹ<>ܴ<EFBFBD><DCB4><EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD> */
|
|||
|
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
|
|||
|
USART_Cmd(USART2, ENABLE);
|
|||
|
}
|
|||
|
|
|||
|
/**----------------------------------------------------------------------
|
|||
|
* Function : USART2_Send
|
|||
|
* Description : <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD>USART2<EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD>ֻ<EFBFBD>
|
|||
|
* 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 : <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD>USART2<EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD>ֻ<EFBFBD>
|
|||
|
* 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 : <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* 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 : <EFBFBD><EFBFBD><EFBFBD><EFBFBD>2<EFBFBD>жϺ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* Author : zhanli&719901725@qq.com
|
|||
|
* Date : 2023/05/20 zhanli
|
|||
|
*---------------------------------------------------------------------**/
|
|||
|
void USART2_IRQHandler(void)
|
|||
|
{
|
|||
|
unsigned char Res;
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>(<28><><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݱ<EFBFBD><DDB1><EFBFBD><EFBFBD><EFBFBD>0x0d 0x0a<30><61>β)
|
|||
|
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);
|
|||
|
}
|
|||
|
}
|