Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pn512_cmd.c Source File

pn512_cmd.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2013-2018, ARM Limited, All Rights Reserved
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00006  * not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  * http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00013  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 /**
00018  * \file pn512_cmd.c
00019  * \copyright Copyright (c) ARM Ltd 2013
00020  * \author Donatien Garnier
00021  * \details Format and execute PN512 commands
00022  * \internal
00023  */
00024 
00025 #define __DEBUG__ 0
00026 #ifndef __MODULE__
00027 #define __MODULE__ "pn512_cmd.c"
00028 #endif
00029 #include "stack/nfc_errors.h"
00030 
00031 #include "pn512_cmd.h"
00032 
00033 #define PN512_FIFO_SIZE 64
00034 
00035 #include "pn512.h"
00036 #include "pn512_registers.h"
00037 #include "pn512_irq.h"
00038 #include "pn512_hw.h"
00039 
00040 /** \addtogroup PN512
00041  *  \internal
00042  *  @{
00043  *  \name Commands
00044  *  @{
00045  */
00046 
00047 /** \internal Initialize underlying pn512_cmd_t structure
00048  * \param pPN512 pointer to pn512_t structure
00049  */
00050 void pn512_cmd_init(pn512_t *pPN512)
00051 {
00052     (void) pPN512;
00053 }
00054 
00055 //Fifo read / write
00056 /** \internal Write bytes to FIFO
00057  * \param pPN512 pointer to pn512_t structure
00058  * \param pData buffer to write
00059  */
00060 void pn512_fifo_write(pn512_t *pPN512, ac_buffer_t *pData)
00061 {
00062     uint8_t fifo_space = pn512_fifo_space(pPN512); //Do not call this fn twice
00063     size_t len = ac_buffer_reader_readable(pData);
00064     len = MIN(fifo_space, len);
00065 
00066     pn512_register_switch_page(pPN512, PN512_REG_FIFODATA);
00067     pn512_hw_write_buffer(pPN512, PN512_REG_FIFODATA, pData, len);
00068 }
00069 
00070 /** \internal Read bytes from FIFO
00071  * \param pPN512 pointer to pn512_t structure
00072  * \param pData buffer in which to read
00073  */
00074 void pn512_fifo_read(pn512_t *pPN512, ac_buffer_builder_t *pData)
00075 {
00076     uint8_t fifo_len = pn512_fifo_length(pPN512); //Do not call this fn twice
00077     size_t len = ac_buffer_builder_writable(pData);
00078     len = MIN(fifo_len, len);
00079 
00080     pn512_register_switch_page(pPN512, PN512_REG_FIFODATA);
00081     pn512_hw_read_buffer(pPN512, PN512_REG_FIFODATA, pData, len);
00082 }
00083 
00084 /** \internal Clear FIFO
00085  * Removes any bytes left in FIFO
00086  * \param pPN512 pointer to pn512_t structure
00087  */
00088 void pn512_fifo_clear(pn512_t *pPN512)
00089 {
00090     pn512_register_write(pPN512, PN512_REG_FIFOLEVEL, 0x80); //Flush FIFO
00091 }
00092 
00093 /** \internal Get space in FIFO
00094  * \param pPN512 pointer to pn512_t structure
00095  * \return number of bytes that can be written to FIFO
00096  */
00097 size_t pn512_fifo_space(pn512_t *pPN512)
00098 {
00099     return PN512_FIFO_SIZE - pn512_register_read(pPN512, PN512_REG_FIFOLEVEL);
00100 }
00101 
00102 /** \internal Get FIFO length
00103  * \param pPN512 pointer to pn512_t structure
00104  * \return number of bytes that can be read from FIFO
00105  */
00106 size_t pn512_fifo_length(pn512_t *pPN512)
00107 {
00108     return pn512_register_read(pPN512, PN512_REG_FIFOLEVEL);
00109 }
00110 
00111 /** \internal Execute command
00112  * \param pPN512 pointer to pn512_t structure
00113  * \param cmd PN512 command to execute
00114  */
00115 void pn512_cmd_exec(pn512_t *pPN512, uint8_t cmd)
00116 {
00117     pn512_register_write(pPN512, PN512_REG_COMMAND, cmd);
00118 }
00119 
00120 /** \internal Wait for command completion
00121  * \param pPN512 pointer to pn512_t structure
00122  * \param timeout timeout in milliseconds or -1 for blocking mode
00123  * \return NFC_OK on success or NFC_ERR_TIMEOUT on timeout
00124  */
00125 nfc_err_t pn512_cmd_wait_idle(pn512_t *pPN512, int timeout)
00126 {
00127     (void) timeout;
00128     while (pn512_cmd_get(pPN512) != PN512_CMD_IDLE) {
00129 
00130     }
00131     return NFC_OK;
00132 }
00133 
00134 
00135 /** \internal Read executed command
00136  * \param pPN512 pointer to pn512_t structure
00137  * \return PN512 command being executed
00138  */
00139 uint8_t pn512_cmd_get(pn512_t *pPN512)
00140 {
00141     return pn512_register_read(pPN512, PN512_REG_COMMAND) & PN512_CMD_REG_MASK;
00142 }
00143 
00144 /**
00145  * @}
00146  * @}
00147  * */
00148