350d4cb98441ba12cb3a2925d8444a27be76cf6d
[hackover2013-badge-firmware.git] / drivers / sensors / pn532 / pn532.c
1 /**************************************************************************/
2 /*!
3 @file pn532.c
4 */
5 /**************************************************************************/
6 #include <string.h>
7
8 #include "pn532.h"
9 #include "pn532_drvr.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 PN532_DEBUG("%02x ", pbtData[szPos]);
29 }
30 PN532_DEBUG(CFG_PRINTF_NEWLINE);
31 }
32
33 /**************************************************************************/
34 /*!
35 @brief Gets a reference to the PN532 peripheral control block,
36 which can be used to determine that state of the PN532
37 IC, buffers, etc.
38 */
39 /**************************************************************************/
40 pn532_pcb_t * pn532GetPCB()
41 {
42 return &pcb;
43 }
44
45 /**************************************************************************/
46 /*!
47 @brief Initialises the appropriate serial bus (UART, etc.),and
48 sets up any buffers or peripherals required by the PN532.
49 */
50 /**************************************************************************/
51 void pn532Init(void)
52 {
53 // Clear protocol control blocks
54 memset(&pcb, 0, sizeof(pn532_pcb_t));
55
56 // Initialise the underlying HW
57 pn532HWInit();
58
59 // Set the PCB flags to an appropriate state
60 pcb.initialised = TRUE;
61 }
62
63 /**************************************************************************/
64 /*!
65 @brief Configures the PN532 for a specific modulation and
66 baud rate
67 */
68 /**************************************************************************/
69 pn532_error_t pn532Configure(pn532_modulation_t mod)
70 {
71 // ToDo
72
73 return PN532_ERROR_NONE;
74 }
75
76 /**************************************************************************/
77 /*!
78 @brief Reads the response buffer from the PN532
79
80 @param abtCommand
81 The byte array containg the command and any
82 optional paramaters
83 @param szLen
84 The number of bytes in abtCommand
85 */
86 /**************************************************************************/
87 pn532_error_t pn532Read(byte_t * abtResponse, size_t * pszLen)
88 {
89 if (!pcb.initialised) pn532Init();
90
91 // Try to wake the device up if it's in sleep mode
92 if (pcb.state == PN532_STATE_SLEEP)
93 {
94 pn532_error_t wakeupError = pn532Wakeup();
95 if (wakeupError)
96 {
97 return wakeupError;
98 }
99 }
100
101 // Read the response if the device is in an appropriate state
102 if (pcb.state == PN532_STATE_READY)
103 {
104 return pn532ReadResponse(abtResponse, pszLen);
105 }
106 else
107 {
108 PN532_DEBUG("Init Failed%s", CFG_PRINTF_NEWLINE);
109 return PN532_ERROR_UNABLETOINIT;
110 }
111 }
112
113 /**************************************************************************/
114 /*!
115 @brief Sends a byte array of command and parameter data to the
116 PN532, starting with the command byte. The frame's
117 preamble, checksums, postamble and frame identifier (0xD4)
118 will all be automatically added.
119
120 @param abtCommand
121 The byte array containg the command and any
122 optional paramaters
123 @param szLen
124 The number of bytes in abtCommand
125 */
126 /**************************************************************************/
127 pn532_error_t pn532Write(byte_t * abtCommand, size_t szLen)
128 {
129 if (!pcb.initialised) pn532Init();
130
131 // Try to wake the device up if it's in sleep mode
132 if (pcb.state == PN532_STATE_SLEEP)
133 {
134 pn532_error_t wakeupError = pn532Wakeup();
135 if (wakeupError)
136 {
137 return wakeupError;
138 }
139 }
140
141 // Send the command if the device is in an appropriate state
142 if (pcb.state == PN532_STATE_READY)
143 {
144 return pn532SendCommand(abtCommand, szLen);
145 }
146 else
147 {
148 PN532_DEBUG("Init Failed%s", CFG_PRINTF_NEWLINE);
149 return PN532_ERROR_UNABLETOINIT;
150 }
151 }
152
This page took 0.046951 seconds and 3 git commands to generate.