mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 31 06:02:27 2019 +0000
Revision:
1:9db0e321a9f4
Parent:
0:5b88d5760320
updated based on mbed-os5.15.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 0:5b88d5760320 1 /* mbed Microcontroller Library
kenjiArai 0:5b88d5760320 2 * Copyright (c) 2018 ARM Limited
kenjiArai 0:5b88d5760320 3 *
kenjiArai 0:5b88d5760320 4 * Licensed under the Apache License, Version 2.0 (the "License");
kenjiArai 0:5b88d5760320 5 * you may not use this file except in compliance with the License.
kenjiArai 0:5b88d5760320 6 * You may obtain a copy of the License at
kenjiArai 0:5b88d5760320 7 *
kenjiArai 0:5b88d5760320 8 * http://www.apache.org/licenses/LICENSE-2.0
kenjiArai 0:5b88d5760320 9 *
kenjiArai 0:5b88d5760320 10 * Unless required by applicable law or agreed to in writing, software
kenjiArai 0:5b88d5760320 11 * distributed under the License is distributed on an "AS IS" BASIS,
kenjiArai 0:5b88d5760320 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kenjiArai 0:5b88d5760320 13 * See the License for the specific language governing permissions and
kenjiArai 0:5b88d5760320 14 * limitations under the License.
kenjiArai 0:5b88d5760320 15 */
kenjiArai 0:5b88d5760320 16
kenjiArai 0:5b88d5760320 17 #include "platform/platform.h"
kenjiArai 0:5b88d5760320 18
kenjiArai 0:5b88d5760320 19 #if (defined (DEVICE_SPI) && defined (DEVICE_INTERRUPTIN)) || defined(DOXYGEN_ONLY)
kenjiArai 0:5b88d5760320 20
kenjiArai 0:5b88d5760320 21 #include "PN512SPITransportDriver.h"
kenjiArai 0:5b88d5760320 22
kenjiArai 0:5b88d5760320 23 #include "stack/transceiver/transceiver.h"
kenjiArai 0:5b88d5760320 24 #include "platform/mbed_wait_api.h"
kenjiArai 0:5b88d5760320 25
kenjiArai 0:5b88d5760320 26 using namespace mbed;
kenjiArai 0:5b88d5760320 27 using namespace mbed::nfc;
kenjiArai 0:5b88d5760320 28
kenjiArai 0:5b88d5760320 29 PN512SPITransportDriver::PN512SPITransportDriver(PinName mosi, PinName miso, PinName sclk, PinName ssel, PinName irq, PinName rst) :
kenjiArai 0:5b88d5760320 30 _spi(mosi, miso, sclk),
kenjiArai 0:5b88d5760320 31 _ssel(ssel, 1),
kenjiArai 0:5b88d5760320 32 _irq(irq, PullNone),
kenjiArai 0:5b88d5760320 33 _rst(rst, 1)
kenjiArai 0:5b88d5760320 34 {
kenjiArai 0:5b88d5760320 35
kenjiArai 0:5b88d5760320 36 // Use SPI mode 0
kenjiArai 0:5b88d5760320 37 _spi.format(8, 0);
kenjiArai 0:5b88d5760320 38
kenjiArai 0:5b88d5760320 39 // The PN512 supports SPI clock frequencies up to 10MHz, so use this if we can
kenjiArai 0:5b88d5760320 40 _spi.frequency(10000000UL);
kenjiArai 0:5b88d5760320 41
kenjiArai 0:5b88d5760320 42 // Initialize NFC transport
kenjiArai 0:5b88d5760320 43 nfc_transport_init(&_nfc_transport, &PN512SPITransportDriver::s_transport_write, &PN512SPITransportDriver::s_transport_read, this);
kenjiArai 0:5b88d5760320 44 }
kenjiArai 0:5b88d5760320 45
kenjiArai 0:5b88d5760320 46 void PN512SPITransportDriver::initialize()
kenjiArai 0:5b88d5760320 47 {
kenjiArai 0:5b88d5760320 48 // Deactivate IRQ
kenjiArai 0:5b88d5760320 49 _irq.rise(callback<void>());
kenjiArai 0:5b88d5760320 50
kenjiArai 0:5b88d5760320 51 // Assert reset pin
kenjiArai 0:5b88d5760320 52 // According to the datasheet, it needs to be asserted for at least 100ns
kenjiArai 0:5b88d5760320 53 // Wait for 1us as that's the shortest time we can wait for
kenjiArai 0:5b88d5760320 54 _rst = 0;
kenjiArai 0:5b88d5760320 55 wait_us(1);
kenjiArai 0:5b88d5760320 56 _rst = 1;
kenjiArai 0:5b88d5760320 57
kenjiArai 0:5b88d5760320 58 // Setup IRQ pin
kenjiArai 0:5b88d5760320 59 _irq.rise(callback(this, &PN512SPITransportDriver::hw_interrupt));
kenjiArai 0:5b88d5760320 60 }
kenjiArai 0:5b88d5760320 61
kenjiArai 0:5b88d5760320 62 nfc_transport_t *PN512SPITransportDriver::get_transport()
kenjiArai 0:5b88d5760320 63 {
kenjiArai 0:5b88d5760320 64 return &_nfc_transport;
kenjiArai 0:5b88d5760320 65 }
kenjiArai 0:5b88d5760320 66
kenjiArai 0:5b88d5760320 67 void PN512SPITransportDriver::transport_write(uint8_t address, const uint8_t *outBuf, size_t outLen)
kenjiArai 0:5b88d5760320 68 {
kenjiArai 0:5b88d5760320 69 if (outLen == 0) {
kenjiArai 0:5b88d5760320 70 return;
kenjiArai 0:5b88d5760320 71 }
kenjiArai 0:5b88d5760320 72
kenjiArai 0:5b88d5760320 73 // First byte is (address << 1) | 0x00 for a write
kenjiArai 0:5b88d5760320 74 address = (address << 1) | 0x00;
kenjiArai 0:5b88d5760320 75 _ssel = 0;
kenjiArai 0:5b88d5760320 76 _spi.write(address); // First write address byte
kenjiArai 0:5b88d5760320 77 _spi.write((const char *) outBuf, outLen, (char *) NULL, 0); // Ignore read bytes
kenjiArai 0:5b88d5760320 78 _ssel = 1;
kenjiArai 0:5b88d5760320 79 }
kenjiArai 0:5b88d5760320 80
kenjiArai 0:5b88d5760320 81 void PN512SPITransportDriver::transport_read(uint8_t address, uint8_t *inBuf, size_t inLen)
kenjiArai 0:5b88d5760320 82 {
kenjiArai 0:5b88d5760320 83 if (inLen == 0) {
kenjiArai 0:5b88d5760320 84 return;
kenjiArai 0:5b88d5760320 85 }
kenjiArai 0:5b88d5760320 86
kenjiArai 0:5b88d5760320 87 // Address byte is (address << 1) | 0x80 for a read
kenjiArai 0:5b88d5760320 88 // This should be repeated accross the transfer, except for the last byte which should be 0
kenjiArai 0:5b88d5760320 89 address = (address << 1) | 0x80;
kenjiArai 0:5b88d5760320 90
kenjiArai 0:5b88d5760320 91 // Set this byte across inBuf so that it's repeated accross the transfer
kenjiArai 0:5b88d5760320 92 // Bit cheeky, but will work
kenjiArai 0:5b88d5760320 93 memset(inBuf, address, inLen - 1);
kenjiArai 0:5b88d5760320 94
kenjiArai 0:5b88d5760320 95 // Also terminate with 0 so that it's a no-op
kenjiArai 0:5b88d5760320 96 inBuf[inLen - 1] = 0;
kenjiArai 0:5b88d5760320 97
kenjiArai 0:5b88d5760320 98 _ssel = 0;
kenjiArai 0:5b88d5760320 99 _spi.write(address); // First write address byte
kenjiArai 0:5b88d5760320 100 _spi.write((const char *) inBuf, inLen, (char *) inBuf, inLen);
kenjiArai 0:5b88d5760320 101 _ssel = 1;
kenjiArai 0:5b88d5760320 102 }
kenjiArai 0:5b88d5760320 103
kenjiArai 0:5b88d5760320 104 // Callbacks from munfc
kenjiArai 0:5b88d5760320 105 void PN512SPITransportDriver::s_transport_write(uint8_t address, const uint8_t *outBuf, size_t outLen, void *pUser)
kenjiArai 0:5b88d5760320 106 {
kenjiArai 0:5b88d5760320 107 PN512SPITransportDriver *self = (PN512SPITransportDriver *)pUser;
kenjiArai 0:5b88d5760320 108 self->transport_write(address, outBuf, outLen);
kenjiArai 0:5b88d5760320 109 }
kenjiArai 0:5b88d5760320 110
kenjiArai 0:5b88d5760320 111 void PN512SPITransportDriver::s_transport_read(uint8_t address, uint8_t *inBuf, size_t inLen, void *pUser)
kenjiArai 0:5b88d5760320 112 {
kenjiArai 0:5b88d5760320 113 PN512SPITransportDriver *self = (PN512SPITransportDriver *)pUser;
kenjiArai 0:5b88d5760320 114 self->transport_read(address, inBuf, inLen);
kenjiArai 0:5b88d5760320 115 }
kenjiArai 0:5b88d5760320 116
kenjiArai 0:5b88d5760320 117 #endif