📄 bluetooth.c
字号:
#include <msp430x14x.h>
#include "FreeRTOS.h"
#include "task.h"
#include "F2M_Bluetooth.h"
#include "Menu.h"
#include "LCD_Library.h"
#include "global.h"
#include "TimerTask.h"
#include <string.h>
#include <stdio.h>
//static unsigned char F2M_RED_CMD[][]={
// }
#define F2MBT_STACK_SIZE 100
unsigned char PairingSetting[]={0x00, // No Encryption
0x00, // Authentication
0x00}; // no effect for master
volatile unsigned char F2M_RSP_BUFFER[0x2a];
volatile unsigned char UART_BUFFER[F2M_BUFFER_LENGTH]; // Serial Communication Buffer
unsigned char INQUIRY_FILTER_STR[12] = {'C','a','r','d','i','o','A','n','g','e','l',0x00};
static volatile unsigned char BUFFER_FLAG = 0;
static char F2M_PAIR_NAME[0x20];
xTaskHandle F2MBT_Handle; // Pairing name
//static unsigned char F2M_PAIR_ADDRESS[6]; // Pairing address
//static unsigned char F2M_PAIR_PASSKEY[17] = "Philips123";
//unsigned char debugg;
//volatile unsigned char debuff = 1;
//static union
union
{
unsigned char ADD_PASSKEY[0x17];
struct
{
unsigned char ADDRESS[6]; // Pairing address
char PASSKEY[17];
}_;
}F2M_PAIR;
/*
* BUFFER_FLAG
* BIT0 F2M_OVERFLOW_FLAG 1-OVERFLOW
* BIT1 F2M_LOOP_FLAG 1-pBUF_WRT rewrites the Buffer
* BIT2 F2M_RENEW_FLAG 1-Buffer data is renewed
*/
static unsigned char pBUF_WRT = 0; // Buffer Writing Point
static unsigned char pBUF_RED = 0; // Buffer Reading Point
char ICHAR;
static void F2M_Delay(unsigned int i)
{
for(;i>0;i--);
}
unsigned char F2M_GetBufferFlag(unsigned char Bit)
{
return (BUFFER_FLAG & Bit)? 1:0;
}
void F2M_SetBufferFlag(unsigned char Bit)
{
BUFFER_FLAG |= Bit;
}
void F2M_ClrBufferFlag(unsigned char Bit)
{
BUFFER_FLAG &= ~Bit;
}
void F2M_SetMspUartBR(unsigned int BaudrateConst)
{
portENTER_CRITICAL();
{
#ifdef __F2M_UART1
UBR01 = (unsigned char)(BaudrateConst&0x00ff);
UBR11 = (unsigned char)((BaudrateConst&0xff00)>>8);
#else
UBR00 = (char)(BaudrateConst&0x00ff);
UBR10 = (char)((BaudrateConst&0xff00)>>8);
#endif
}
portEXIT_CRITICAL();
}
void F2M_InitUART()
{
unsigned char i;
_F2M_INIT_PORTS(); // Init F2M Ports
//Init UART
// portENTER_CRITICAL();
// {
#ifdef __F2M_UART1 // Using the UART1
P3SEL |= BIT6|BIT7; // P3.6,7 = USART1 TXD/RXD
ME2 |= UTXE1 + URXE1; // Enabled USART1 TXD/RXD
UCTL1 |= CHAR; // 8-bit character
UTCTL1 |= SSEL1; // UCLK = SMCLK
F2M_SetMspUartBR(F2M_USART_COMM); // Set Baudrate
UMCTL1 = 0x00; // no modulation
UCTL1 &= ~SWRST; // Initialize USART state machine
IE2 |= URXIE1; // Enable USART1 RX interrupt
#else // Using the UART0
P3SEL |= BIT5|BIT4; // P3.4,5 = USART0 TXD/RXD
ME1 |= UTXE0 + URXE0; // Enabled USART1 TXD/RXD
UCTL0 |= CHAR; // 8-bit character
UTCTL0 |= SSEL1; // UCLK = SMCLK
F2M_SetMspUartBR(F2M_USART_COMM); // Set Baudrate
UMCTL0 = 0x00; // no modulation
UCTL0 &= ~SWRST; // Initialize USART state machine
IE1 |= URXIE0; // Enable USART1 RX interrupt
#endif
//}
//portEXIT_CRITICAL();
}
unsigned char F2M_ReadBuffer()
{
unsigned char Buffer_value;
Buffer_value = UART_BUFFER[pBUF_RED];
pBUF_RED++;
if(pBUF_RED == F2M_BUFFER_LENGTH)
{
pBUF_RED = 0; // Rewriting the Buffer
BUFFER_FLAG &= ~F2M_LOOP_FLAG; // Clear Rewirting flag
}
if((pBUF_RED == pBUF_WRT) && (!F2M_GetBufferFlag(F2M_LOOP_FLAG)))
{
BUFFER_FLAG &= ~F2M_RENEW_FLAG;
}
if(BUFFER_FLAG & F2M_OVERFLOW_FLAG)
{
BUFFER_FLAG &= ~F2M_OVERFLOW_FLAG; // Clear the overflow flag
_F2M_CLR_CTS(); // Flow control
}
return Buffer_value;
}
void F2M_Command(unsigned char Command, unsigned char Length, unsigned char* pParameters)
{
pBUF_WRT = 0;
pBUF_RED = 0; // Override the uart buffer
BUFFER_FLAG &= ~(BIT0|BIT1|BIT2);
#ifdef __F2M_UART1
while (!(IFG2 & UTXIFG1)); // USART1 TX buffer ready?
TXBUF1 = Command; // Send Byte
while (!(IFG2 & UTXIFG1)); // USART1 TX buffer ready?
TXBUF1 = Length; // Send Byte
while (!(IFG2 & UTXIFG1)); // USART1 TX buffer ready?
while(Length--)
{
TXBUF1 = *pParameters++; // Send Byte
while (!(IFG2 & UTXIFG1)); // USART1 TX buffer ready?
}
#else
while (!(IFG1 & UTXIFG0)); // USART0 TX buffer ready?
TXBUF0 = Command; // Send Byte
while (!(IFG1 & UTXIFG0)); // USART0 TX buffer ready?
TXBUF0 = Length; // Send Byte
while (!(IFG1 & UTXIFG0)); // USART0 TX buffer ready?
while(Length--)
{
TXBUF0 = *pParameters++; // Send Byte
while (!(IFG1 & UTXIFG0)); // USART0 TX buffer ready?
}
#endif
}
/*
unsigned char F2M_OverFlow()
{
return (BUFFER_FLAG & F2M_OVERFLOW_FLAG);
}
*/
/*
unsigned char F2M_BufRenew()
{
return (BUFFER_FLAG & F2M_RENEW_FLAG);
}
*/
void F2M_GetResponse()
{
//unsigned char* pB = F2M_RSP_BUFFER;
unsigned char i = 0;
F2M_RSP_BUFFER[1] = 0x01;
while(i<2+F2M_RSP_BUFFER[1])
{
while( !F2M_GetBufferFlag(F2M_RENEW_FLAG) );
F2M_RSP_BUFFER[i++] = F2M_ReadBuffer();
}
/*
if(F2M_RSP_BUFFER[2] == 0xfc )
{
while(1)
{
i = 15;
}
}
*/
}
unsigned char F2M_EnterCommandMode()
{
unsigned char i;
const unsigned char F2M_CMD_ENTHCM[] = {0xff,0x00,0x55,0xaa}; //HCM Enter Code
// ONLY FOR F2M03AC2!!!!!
// Set CTS high to close down the probable Bluetooth connection
_F2M_SET_PIO2(); // Before Commands can be sent to firmware,
//if(_F2M_CHK_PIO3())
//{ // the current Bluetooth connection must be closed down.
//LCD_RollingDisplay("Waiting for PIO3");
while(_F2M_CHK_PIO3()!=0); // Waiting for stopping Bluetooth connection if necessary
//}
F2M_SetMspUartBR(F2M_USART_38400); //HCM works only with Baudrate 38400!
vTaskDelay(2); //Delay 200ms for preparation entering the Command Mode
F2M_Command(0x01,0x04,(unsigned char*)F2M_CMD_ENTHCM); // Enter HCM Command
F2M_GetResponse(); // Get Response from the F2M Bluetooth Plug
// ONLY FOR F2M03AC2!!!!!
_F2M_CLR_PIO2(); //
return F2M_RSP_BUFFER[2]; // Return Status Byte
}
unsigned char F2M_SetF2MBaudrate()
{
// Default UART Setting
unsigned char BRstr[] = {0x04, // Baudrate 19200
0x01, // Hardware flow control on
0x00, // No parity
0x08, // 8bit
0x01}; // 1 stop bit
switch(F2M_USART_COMM) // Translation F2M Baudrate Code
{
case F2M_USART_2400:
BRstr[0] = 0x01;
break;
case F2M_USART_4800:
BRstr[0] = 0x02;
break;
case F2M_USART_9600:
BRstr[0] = 0x03;
break;
case F2M_USART_19200:
BRstr[0] = 0x04;
break;
case F2M_USART_38400:
BRstr[0] = 0x05;
break;
case F2M_USART_57600:
BRstr[0] = 0x06;
break;
case F2M_USART_115200:
BRstr[0] = 0x07;
break;
default:
BRstr[0] = 0x04;
}
F2M_Command(0x15,0x05,BRstr); // Set F2M Communication Baudrate
F2M_GetResponse(); // Get Response from the F2M Bluetooth Plug
return F2M_RSP_BUFFER[2]; // Return Status Byte
}
unsigned char F2M_SetOperationMode(unsigned char Mode)
{
// Mode: 0x01 Bluetooth Master
// Mode: 0x02 Bluetooth Slave
if((Mode!=0x01) && (Mode!=0x02)) return 0; // Error!
F2M_Command(0x13,0x01,&Mode); // Set Operation Mode
F2M_GetResponse(); // Get Response from the F2M Bluetooth Plug
return F2M_RSP_BUFFER[2]; // Return Status Byte
}
unsigned char F2M_Pairing()
{
unsigned char PairingSetting[]={0x00, // No Encryption
0x01, // Authentication
0x00}; // no effect for master
//unsigned char pStrBuffer[0x16]; // longest str: 0x16 bytes(address + passkey)
//strcpy(pStrBuffer,F2M_PAIR._.ADDRESS);
//strcat(pStrBuffer,F2M_PAIR._.PASSKEY);
LCD_RollingDisplay("Try to pair off with");
LCD_RollingDisplay(F2M_PAIR_NAME);
strcpy(F2M_PAIR._.PASSKEY,"1111"); // Set passkey as "1111" (ASCII)
F2M_Command(0x67,0x03,(unsigned char*)PairingSetting); // Set Pairing Parameters
F2M_GetResponse(); // Get Response from the F2M Bluetooth Plug
if(F2M_RSP_BUFFER[2]!= 0x01)
return F2M_RSP_BUFFER[2]; // Error
F2M_Command(0x64,0x06+strlen(F2M_PAIR._.PASSKEY),F2M_PAIR.ADD_PASSKEY); // Pairing
F2M_GetResponse(); // Get Response from the F2M Bluetooth Plug
if(F2M_RSP_BUFFER[2]!= 0x01)
return F2M_RSP_BUFFER[2]; // Error
F2M_GetResponse(); //Waiting for pairng
return F2M_RSP_BUFFER[2];
}
unsigned char F2M_SelSlave(char* pDesName)
{
//const unsigned char pDesName[] = F2M_SLAVE_NAME;
unsigned char i;
char* pSname = (char*)F2M_RSP_BUFFER + 9; // Get point to the slave name
if(pDesName == NULL) return 0; // Null point , return error
// just a compare
while(*pDesName)
if(*pDesName++ != *pSname++) return 0;
return 1;
// end
//LCD_RollingDisplay(pSname);
//LCD_RollingDisplay(pDesName); // for Debug
/*
if(!(BUFFER_FLAG & F2M_PAIRSEL_FLAG)) // Still no suitable slave in response
{
while(*pDesName)
if(*pDesName++ != *pSname++) return 0;
// Here the comparing is safe, if the pDesName's length < pSname's
// obviously no problem
// if pDesName's length > pSname's
// the parser will return at the last char of pSname '/0'
}
else
return 0; // Slave has benn confirmed
// if it is allowed to pick one of slaves, this parser should be modified
BUFFER_FLAG |= F2M_PAIRSEL_FLAG; // Set Pairsel flag, stop further comparing
for(i=0;i<6;i++) // Get pairing Bluetooth address
F2M_PAIR._.ADDRESS[i] = F2M_RSP_BUFFER[i+3];
pDesName = (char*) F2M_PAIR_NAME; // Get pairing Bluetooth name
pSname = (char*)F2M_RSP_BUFFER + 9;
while(*pSname)
*pDesName++ = *pSname++;
*pDesName = 0x00; //'/0' !!!! // Add a null for end of string
*/
return 1; // Finish pairing
}
unsigned char F2M_Inquiry()
{
unsigned char IQstr[] = {F2M_INQUIRY_SCAN_TIMEOUT, // Inquiry scan timeout
F2M_INQUIRY_DEVICE_CLASS, // Inquiry class of device
F2M_INQUIRY_NAME_REQUEST}; // Inquiry name request
F2M_Command(0x60,0x06,IQstr); // Set inquiry scan parameters
F2M_GetResponse(); // Get Response from the F2M Bluetooth Plug
while(F2M_RSP_BUFFER[1]!= 0x00)
{
if(F2M_RSP_BUFFER[2]== 0x01) // Check Response Status
//LCD_RollingDisplay(pBName); // Display
F2M_SelSlave(F2M_SLAVE_NAME); //Searching for aimed Slave
else //if(F2M_RSP_BUFFER[2]!= 0xfc)
return F2M_RSP_BUFFER[2]; // Error
F2M_GetResponse(); // Get Response from the F2M Bluetooth Plug
}
if(BUFFER_FLAG & F2M_PAIRSEL_FLAG) // Suitable slave found
{
LCD_RollingDisplay(" Aim found! It's:"); // Just display
LCD_RollingDisplay(F2M_PAIR_NAME);
}
else LCD_RollingDisplay(" Aim not found!");
return 1; // Return Status Byte
}
unsigned char F2M_SetConnectRule(unsigned char *BluetoothAdr)
{
if(BluetoothAdr == NULL) return 0; // Null point, return error
F2M_Command(0x17,0x06,BluetoothAdr); // Exit HCM Command
F2M_GetResponse(); // Get Response from the F2M Bluetooth Plug
return F2M_RSP_BUFFER[2]; // Return Status Byte
}
unsigned char F2M_GetBluetoothName()
{
F2M_Command(0x1c,0x00,NULL); // Exit HCM Command
F2M_GetResponse(); // Get Response from the F2M Bluetooth Plug
return F2M_RSP_BUFFER[2]; // Return Status Byte
}
unsigned char F2M_SetBluetoothName(unsigned char *BluetoothName)
{
if(BluetoothName == NULL) return 0; // Null point, return error
F2M_Command(0x1d,strlen((char*)BluetoothName)+1,BluetoothName); // Exit HCM Command
F2M_GetResponse(); // Get Response from the F2M Bluetooth Plug
return F2M_RSP_BUFFER[2]; // Return Status Byte
}
unsigned char F2M_ExitCommandMode()
{
F2M_Command(0x50,0x00,0x00); // Exit HCM Command
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -