This repository has been archived by the owner on Mar 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bluetooth.c
137 lines (111 loc) · 3.79 KB
/
bluetooth.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <stdarg.h>
#include "bluetooth.h"
#include "printer.h"
void WaitForTransmissionComplete(USART_TypeDef* USARTx);
void BluetoothInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
// RCC Configuration
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
// GPIO Configuration
// USART Rx
GPIO_InitStructure.GPIO_Pin = USART1_RX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USART1_RX_TYPE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = USART2_RX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USART2_RX_TYPE, &GPIO_InitStructure);
// USART Tx
GPIO_InitStructure.GPIO_Pin = USART1_TX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(USART1_TX_TYPE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = USART2_TX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(USART2_TX_TYPE, &GPIO_InitStructure);
// USART Configuration
USART_InitStructure.USART_BaudRate = 9600;
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_Tx | USART_Mode_Rx);
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_InitStructure.USART_BaudRate = 9600;
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_Tx | USART_Mode_Rx);
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
// NVIC Configuration
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_ClearPendingIRQ(USART1_IRQn);
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);
NVIC_ClearPendingIRQ(USART2_IRQn);
}
void BluetoothWriteString(char *message, ...)
{
int i;
char buffer[30];
va_list vl;
va_start(vl, message);
vsnprintf(buffer, sizeof(buffer), message, vl);
va_end(vl);
for (i = 0; buffer[i] != 0; i++)
{
USART_SendData(USART2, buffer[i]);
WaitForTransmissionComplete(USART2);
}
Print("WriteString : %s", buffer);
}
void WaitForTransmissionComplete(USART_TypeDef *USARTx)
{
while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET)
{
;
}
}
void USART1_IRQHandler(void)
{
char c;
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
c = (char) USART_ReceiveData(USART1);
USART_SendData(USART2, c);
WaitForTransmissionComplete(USART2);
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}
void USART2_IRQHandler(void)
{
char c;
if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
c = (char) USART_ReceiveData(USART2);
USART_SendData(USART1, c);
WaitForTransmissionComplete(USART1);
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
}
}