-
Notifications
You must be signed in to change notification settings - Fork 21
/
pn532_stm32f1.c
234 lines (212 loc) · 7.67 KB
/
pn532_stm32f1.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**************************************************************************
* @file pn532_stm32f1.c
* @author Yehui from Waveshare
* @license BSD
*
* This implements the peripheral interfaces.
*
* Check out the links above for our tutorials and wiring diagrams
* These chips use SPI communicate.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documnetation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
**************************************************************************/
#include "stm32f1xx_hal.h"
#include "main.h"
#include "pn532_stm32f1.h"
#define _SPI_STATREAD 0x02
#define _SPI_DATAWRITE 0x01
#define _SPI_DATAREAD 0x03
#define _SPI_READY 0x01
#define _SPI_TIMEOUT 10
// This indicates if the bits read/write should be reversed
#define _SPI_HARDWARE_LSB
#define _I2C_ADDRESS 0x48
#define _I2C_TIMEOUT 10
extern SPI_HandleTypeDef hspi1;
extern I2C_HandleTypeDef hi2c1;
/**************************************************************************
* Reset and Log implements
**************************************************************************/
int PN532_Reset(void) {
HAL_GPIO_WritePin(PN532_RST_GPIO_Port, PN532_RST_Pin, GPIO_PIN_SET);
HAL_Delay(100);
HAL_GPIO_WritePin(PN532_RST_GPIO_Port, PN532_RST_Pin, GPIO_PIN_RESET);
HAL_Delay(500);
HAL_GPIO_WritePin(PN532_RST_GPIO_Port, PN532_RST_Pin, GPIO_PIN_SET);
HAL_Delay(100);
return PN532_STATUS_OK;
}
void PN532_Log(const char* log) {
printf("%s\r\n", log);
}
void PN532_Init(PN532* pn532) {
PN532_SPI_Init(pn532);
}
/**************************************************************************
* End: Reset and Log implements
**************************************************************************/
/**************************************************************************
* SPI
**************************************************************************/
uint8_t reverse_bit(uint8_t num) {
uint8_t result = 0;
for (uint8_t i = 0; i < 8; i++) {
result <<= 1;
result += (num & 1);
num >>= 1;
}
return result;
}
void spi_rw(uint8_t* data, uint8_t count) {
HAL_GPIO_WritePin(SS_GPIO_Port, SS_Pin, GPIO_PIN_RESET);
HAL_Delay(1);
#ifndef _SPI_HARDWARE_LSB
for (uint8_t i = 0; i < count; i++) {
data[i] = reverse_bit(data[i]);
}
HAL_SPI_TransmitReceive(&hspi1, data, data, count, _SPI_TIMEOUT);
for (uint8_t i = 0; i < count; i++) {
data[i] = reverse_bit(data[i]);
}
#else
HAL_SPI_TransmitReceive(&hspi1, data, data, count, _SPI_TIMEOUT);
#endif
HAL_Delay(1);
HAL_GPIO_WritePin(SS_GPIO_Port, SS_Pin, GPIO_PIN_SET);
}
int PN532_SPI_ReadData(uint8_t* data, uint16_t count) {
uint8_t frame[count + 1];
frame[0] = _SPI_DATAREAD;
HAL_Delay(5);
spi_rw(frame, count + 1);
for (uint8_t i = 0; i < count; i++) {
data[i] = frame[i + 1];
}
return PN532_STATUS_OK;
}
int PN532_SPI_WriteData(uint8_t *data, uint16_t count) {
uint8_t frame[count + 1];
frame[0] = _SPI_DATAWRITE;
for (uint8_t i = 0; i < count; i++) {
frame[i + 1] = data[i];
}
spi_rw(frame, count + 1);
return PN532_STATUS_OK;
}
bool PN532_SPI_WaitReady(uint32_t timeout) {
uint8_t status[] = {_SPI_STATREAD, 0x00};
uint32_t tickstart = HAL_GetTick();
while (HAL_GetTick() - tickstart < timeout) {
HAL_Delay(10);
spi_rw(status, sizeof(status));
if (status[1] == _SPI_READY) {
return true;
} else {
HAL_Delay(5);
}
}
return false;
}
int PN532_SPI_Wakeup(void) {
// Send any special commands/data to wake up PN532
uint8_t data[] = {0x00};
HAL_Delay(1000);
HAL_GPIO_WritePin(SS_GPIO_Port, SS_Pin, GPIO_PIN_RESET);
HAL_Delay(2); // T_osc_start
spi_rw(data, 1);
HAL_Delay(1000);
return PN532_STATUS_OK;
}
void PN532_SPI_Init(PN532* pn532) {
// init the pn532 functions
pn532->reset = PN532_Reset;
pn532->read_data = PN532_SPI_ReadData;
pn532->write_data = PN532_SPI_WriteData;
pn532->wait_ready = PN532_SPI_WaitReady;
pn532->wakeup = PN532_SPI_Wakeup;
pn532->log = PN532_Log;
// hardware wakeup
pn532->wakeup();
}
/**************************************************************************
* End: SPI
**************************************************************************/
/**************************************************************************
* I2C
**************************************************************************/
void i2c_read(uint8_t* data, uint16_t count) {
HAL_I2C_Master_Receive(&hi2c1, _I2C_ADDRESS, data, count, _I2C_TIMEOUT);
}
void i2c_write(uint8_t* data, uint16_t count) {
HAL_I2C_Master_Transmit(&hi2c1, _I2C_ADDRESS, data, count, _I2C_TIMEOUT);
}
int PN532_I2C_ReadData(uint8_t* data, uint16_t count) {
uint8_t status[] = {0x00};
uint8_t frame[count + 1];
i2c_read(status, sizeof(status));
if (status[0] != PN532_I2C_READY) {
return PN532_STATUS_ERROR;
}
i2c_read(frame, count + 1);
for (uint8_t i = 0; i < count; i++) {
data[i] = frame[i + 1];
}
return PN532_STATUS_OK;
}
int PN532_I2C_WriteData(uint8_t *data, uint16_t count) {
i2c_write(data, count);
return PN532_STATUS_OK;
}
bool PN532_I2C_WaitReady(uint32_t timeout) {
uint8_t status[] = {0x00};
uint32_t tickstart = HAL_GetTick();
while (HAL_GetTick() - tickstart < timeout) {
i2c_read(status, sizeof(status));
if (status[0] == PN532_I2C_READY) {
return true;
} else {
HAL_Delay(5);
}
}
return false;
}
int PN532_I2C_Wakeup(void) {
// TODO
HAL_GPIO_WritePin(PN532_REQ_GPIO_Port, PN532_REQ_Pin, GPIO_PIN_SET);
HAL_Delay(100);
HAL_GPIO_WritePin(PN532_REQ_GPIO_Port, PN532_REQ_Pin, GPIO_PIN_RESET);
HAL_Delay(100);
HAL_GPIO_WritePin(PN532_REQ_GPIO_Port, PN532_REQ_Pin, GPIO_PIN_SET);
HAL_Delay(500);
return PN532_STATUS_OK;
}
void PN532_I2C_Init(PN532* pn532) {
// init the pn532 functions
pn532->reset = PN532_Reset;
pn532->read_data = PN532_I2C_ReadData;
pn532->write_data = PN532_I2C_WriteData;
pn532->wait_ready = PN532_I2C_WaitReady;
pn532->wakeup = PN532_I2C_Wakeup;
pn532->log = PN532_Log;
// hardware wakeup
pn532->wakeup();
}
/**************************************************************************
* End: I2C
**************************************************************************/