Pinconfig für Endbadge.
[hackover2013-badge-firmware.git] / drivers / rf / pn532 / pn532.c
1 /**************************************************************************/
2 /*!
3 @file pn532.c
4 */
5 /**************************************************************************/
6 #include <string.h>
7
8 #include "pn532.h"
9 #include "pn532_bus.h"
10 #include "core/systick/systick.h"
11 #include "core/uart/uart.h"
12
13 static pn532_pcb_t pcb;
14
15 /**************************************************************************/
16 /*!
17 @brief Prints a hexadecimal value in plain characters
18
19 @param pbtData Pointer to the byte data
20 @param szBytes Data length in bytes
21 */
22 /**************************************************************************/
23 void pn532PrintHex(const byte_t * pbtData, const size_t szBytes)
24 {
25 size_t szPos;
26 for (szPos=0; szPos < szBytes; szPos++)
27 {
28 printf("%02x ", pbtData[szPos]);
29 }
30 printf(CFG_PRINTF_NEWLINE);
31 }
32
33 /**************************************************************************/
34 /*!
35 @brief Prints a hexadecimal value in plain characters, along with
36 the char equivalents in the following format
37
38 AA BB CC DD EE FF ......
39
40 @param pbtData Pointer to the byte data
41 @param szBytes Data length in bytes
42 */
43 /**************************************************************************/
44 void pn532PrintHexChar(const byte_t * pbtData, const size_t szBytes)
45 {
46 size_t szPos;
47 for (szPos=0; szPos < szBytes; szPos++)
48 {
49 printf("%02x", pbtData[szPos]);
50 }
51 printf(" ");
52 for (szPos=0; szPos < szBytes; szPos++)
53 {
54 printf("%c", pbtData[szPos] <= 0x1F ? '.' : pbtData[szPos]);
55 }
56 printf(CFG_PRINTF_NEWLINE);
57 }
58
59 /**************************************************************************/
60 /*!
61 @brief Gets a reference to the PN532 peripheral control block,
62 which can be used to determine that state of the PN532
63 IC, buffers, etc.
64 */
65 /**************************************************************************/
66 pn532_pcb_t * pn532GetPCB()
67 {
68 return &pcb;
69 }
70
71 /**************************************************************************/
72 /*!
73 @brief Initialises the appropriate serial bus (UART, etc.),and
74 sets up any buffers or peripherals required by the PN532.
75 */
76 /**************************************************************************/
77 void pn532Init(void)
78 {
79 // Clear protocol control blocks
80 memset(&pcb, 0, sizeof(pn532_pcb_t));
81
82 // Initialise the underlying HW
83 pn532_bus_HWInit();
84
85 // Set the PCB flags to an appropriate state
86 pcb.initialised = TRUE;
87 }
88
89 /**************************************************************************/
90 /*!
91 @brief Reads the response buffer from the PN532
92
93 @param pbtResponse
94 The byte array that will hold the response data
95 @param pszLen
96 Pointer to the number of bytes in pbtCommand
97 */
98 /**************************************************************************/
99 pn532_error_t pn532Read(byte_t * pbtResponse, size_t * pszLen)
100 {
101 if (!pcb.initialised) pn532Init();
102
103 // Try to wake the device up if it's in sleep mode
104 if (pcb.state == PN532_STATE_SLEEP)
105 {
106 pn532_error_t wakeupError = pn532_bus_Wakeup();
107 if (wakeupError)
108 return wakeupError;
109 }
110
111 // Read the response if the device is in an appropriate state
112 if (pcb.state == PN532_STATE_READY)
113 {
114 return pn532_bus_ReadResponse(pbtResponse, pszLen);
115 }
116 else
117 {
118 #ifdef PN532_DEBUGMODE
119 PN532_DEBUG("Init Failed%s", CFG_PRINTF_NEWLINE);
120 #endif
121 return PN532_ERROR_UNABLETOINIT;
122 }
123 }
124
125 /**************************************************************************/
126 /*!
127 @brief Sends a byte array of command and parameter data to the
128 PN532, starting with the command byte. The frame's
129 preamble, checksums, postamble and frame identifier (0xD4)
130 will all be automatically added.
131
132 @param abtCommand
133 The byte array containg the command and any
134 optional parameters
135 @param szLen
136 The number of bytes in abtCommand
137 */
138 /**************************************************************************/
139 pn532_error_t pn532Write(byte_t * abtCommand, size_t szLen)
140 {
141 if (!pcb.initialised) pn532Init();
142
143 // Try to wake the device up if it's in sleep mode
144 if (pcb.state == PN532_STATE_SLEEP)
145 {
146 pn532_error_t wakeupError = pn532_bus_Wakeup();
147 if (wakeupError)
148 return wakeupError;
149 }
150
151 // Send the command if the device is in an appropriate state
152 if (pcb.state == PN532_STATE_READY)
153 {
154 return pn532_bus_SendCommand(abtCommand, szLen);
155 }
156 else
157 {
158 #ifdef PN532_DEBUGMODE
159 PN532_DEBUG("Init Failed%s", CFG_PRINTF_NEWLINE);
160 #endif
161 return PN532_ERROR_UNABLETOINIT;
162 }
163 }
This page took 0.054413 seconds and 5 git commands to generate.