Skip to content

Commit

Permalink
Add BBIO READER MODE - IS0 14443A (#4)
Browse files Browse the repository at this point in the history
Co-authored-by: Guillaume VINET <guillaume.vinet@eshard.com>
  • Loading branch information
gvinet and Guillaume VINET committed Aug 15, 2020
1 parent 430b22f commit 7ddfa04
Show file tree
Hide file tree
Showing 6 changed files with 299 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ hydrafwEm.elay
*.map
*.hdr
/src/.settings
/src/.idea/
src/hydrafw_hydranfc_shield_v2 Debug.launch

11 changes: 11 additions & 0 deletions src/hydrabus/hydrabus_bbio.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
#include "hydranfc_bbio_reader.h"
#endif

#ifdef HYDRANFC_V2
#include "hydranfc_v2_bbio_reader.h"
#endif

int cmd_bbio(t_hydra_console *con)
{
mode_config_proto_t* proto = &con->mode->proto;
Expand Down Expand Up @@ -97,6 +101,13 @@ int cmd_bbio(t_hydra_console *con)
bbio_mode_hydranfc_reader(con);
break;
#endif

//#ifdef HYDRANFC_V2
case BBIO_NFC_V2_READER:
bbio_mode_hydranfc_v2_reader(con);
break;
//#endif

case BBIO_RESET_HW:
/* Needed for flashrom detection */
cprint(con, "Hydrabus\r\n", 10);
Expand Down
1 change: 1 addition & 0 deletions src/hydrabus/hydrabus_bbio.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#define BBIO_FLASH 0b00001010
#define BBIO_SMARTCARD 0b00001011
#define BBIO_NFC_READER 0b00001100
#define BBIO_NFC_V2_READER 0b00001110

#define BBIO_RESET_HW 0b00001111
#define BBIO_PWM 0b00010010
Expand Down
3 changes: 2 additions & 1 deletion src/hydranfc_v2/hydranfc_v2.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ HYDRANFC_V2_SRC = hydranfc_v2/hydranfc_v2.c \
hydranfc_v2/hydranfc_v2_ce.c \
hydranfc_v2/ce.c \
hydranfc_v2/rfal_poller.c \
hydranfc_v2/hydrabus_mode_dnfc.c
hydranfc_v2/hydrabus_mode_dnfc.c \
hydranfc_v2/hydranfc_v2_bbio_reader.c

# Required include directories
HYDRANFC_V2_INC = ./hydranfc_v2
261 changes: 261 additions & 0 deletions src/hydranfc_v2/hydranfc_v2_bbio_reader.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
/*
* HydraBus/HydraNFC
*
* Copyright (C) 2020 Guillaume VINET
* Copyright (C) 2014-2020 Benjamin VERNOUX
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "common.h"
#include "tokenline.h"
#include "hydranfc_v2.h"
#include "rfal_rf.h"
#include "hydrabus_bbio.h"
#include "hydranfc_v2_bbio_reader.h"
#include "st25r3916.h"
#include "st25r3916_irq.h"
#include "st25r3916_com.h"
#include "rfal_analogConfig.h"
#include "rfal_rf.h"

#include <string.h>

static volatile int irq_count;
static volatile int irq;
static volatile int irq_end_rx;

static void (* st25r3916_irq_fn)(void) = NULL;

/* Triggered when the Ext IRQ is pressed or released. */
static void extcb1(void * arg)
{
(void) arg;

if(st25r3916_irq_fn != NULL)
st25r3916_irq_fn();

irq_count++;
irq = 1;
}

extern t_mode_config mode_con1;

static ReturnCode hydranfc_v2_init_RFAL(t_hydra_console *con)
{
ReturnCode err;
/* RFAL initalisation */
rfalAnalogConfigInitialize();
err = rfalInitialize();
if(err != ERR_NONE)
{
cprintf(con, "hydranfc_v2_init_RFAL rfalInitialize() error=%d\r\n", err);
return err;
}

/* DPO setup */
#ifdef DPO_ENABLE
rfalDpoInitialize();
rfalDpoSetMeasureCallback( rfalChipMeasureAmplitude );
err = rfalDpoTableWrite(dpoSetup,sizeof(dpoSetup)/sizeof(rfalDpoEntry));
if(err != ERR_NONE)
{
cprintf(con, "hydranfc_v2_init_RFAL rfalDpoTableWrite() error=%d\r\n", err);
return err;
}
rfalDpoSetEnabled(true);
rfalSetPreTxRxCallback(&rfalPreTransceiveCb);
#endif
return err;
}


static bool init_gpio_spi_nfc(t_hydra_console *con)
{
/*
* Initializes the SPI driver 2. The SPI2 signals are routed as follow:
* ST25R3916 IO4_CS SPI mode / HydraBus PC1 - NSS
* ST25R3916 DATA_CLK SPI mode / HydraBus PB10 - SCK
* ST25R3916 IO6_MISO SPI mode / HydraBus PC2 - MISO
* ST25R3916 IO7_MOSI SPI mode / HydraBus PC3 - MOSI
* Used for communication with ST25R3916 in SPI mode with NSS.
*/
mode_con1.proto.config.spi.dev_gpio_pull = MODE_CONFIG_DEV_GPIO_NOPULL;
//mode_con1.proto.config.spi.dev_speed = 5; /* 5 250 000 Hz */
mode_con1.proto.config.spi.dev_speed = 6; /* 10 500 000 Hz */
mode_con1.proto.config.spi.dev_phase = 1;
mode_con1.proto.config.spi.dev_polarity = 0;
mode_con1.proto.config.spi.dev_bit_lsb_msb = DEV_FIRSTBIT_MSB;
mode_con1.proto.config.spi.dev_mode = DEV_MASTER;
bsp_spi_init(BSP_DEV_SPI2, &mode_con1.proto);

/*
* Initializes the SPI driver 1. The SPI1 signals are routed as follows:
* Shall be configured as SPI Slave for ST25R3916 NFC data sampling on MOD pin.
* NSS. (Not used use Software).
* ST25R3916 MCU_CLK pin28 output / HydraBus PA5 - SCK.(AF5) => SPI Slave CLK input (Sniffer mode/RX Transparent Mode)
* ST25R3916 ST25R3916 MOSI SPI pin31 (IN) / HydraBus PA6 - MISO.(AF5) (MCU TX Transparent Mode)
* ST25R3916 ST25R3916 MISO_SDA pin32 (OUT) / HydraBus PA7 - MOSI.(AF5) => SPI Slave MOSI input (Sniffer mode/RX Transparent Mode)
*/
/* spiStart() is done in sniffer see sniffer.c */
/* HydraBus SPI1 Slave CLK input */
palSetPadMode(GPIOA, 5, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_MID1);
/* HydraBus SPI1 Slave MISO. Not used/Not connected */
palSetPadMode(GPIOA, 6, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_MID1);
/* HydraBus SPI1 Slave MOSI. connected to ST25R3916 MOD Pin */
palSetPadMode(GPIOA, 7, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_MID1);

/* Configure K1/2 Buttons as Input */
palSetPadMode(GPIOB, 8, PAL_MODE_INPUT); /* K1 Button */
palSetPadMode(GPIOB, 9, PAL_MODE_INPUT); /* K2 Button */

/* Configure D1/2/3/4 LEDs as Output */
D1_OFF;
D2_OFF;
D3_OFF;
D4_OFF;

palSetPadMode(GPIOB, 0, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_MID1);

#ifndef MAKE_DEBUG
// can't use LED on PB3 if using SWO
palSetPadMode(GPIOB, 3, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_MID1);
#endif

palSetPadMode(GPIOB, 4, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_MID1);
palSetPadMode(GPIOB, 5, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_MID1);

palDisablePadEvent(GPIOA, 1);
/* ST25R3916 IRQ output / HydraBus PA1 input */
palClearPad(GPIOA, 1);
palSetPadMode(GPIOA, 1, PAL_MODE_INPUT | PAL_STM32_OSPEED_MID1);
/* Activates the PAL driver callback */
//palDisablePadEvent(GPIOA, 1);
palEnablePadEvent(GPIOA, 1, PAL_EVENT_MODE_RISING_EDGE);
palSetPadCallback(GPIOA, 1, &extcb1, NULL);

/* Init st25r3916 IRQ function callback */
st25r3916_irq_fn = st25r3916Isr;
hal_st25r3916_spiInit(ST25R391X_SPI_DEVICE);
if (hydranfc_v2_init_RFAL(con) != ERR_NONE)
{
cprintf(con, "HydraNFC v2 not found.\r\n");
return FALSE;
}
return TRUE;
}

static void deinit_gpio_spi_nfc(t_hydra_console *con)
{
(void)(con);
palClearPad(GPIOA, 1);
palSetPadMode(GPIOA, 1, PAL_MODE_INPUT);
palDisablePadEvent(GPIOA, 1);

bsp_spi_deinit(BSP_DEV_SPI2);

palSetPadMode(GPIOA, 5, PAL_MODE_INPUT);
palSetPadMode(GPIOA, 6, PAL_MODE_INPUT);
palSetPadMode(GPIOA, 7, PAL_MODE_INPUT);

#if 0
/* Configure K1/2 Buttons as Input */
palSetPadMode(GPIOB, 8, PAL_MODE_INPUT); /* K1 Button */
palSetPadMode(GPIOB, 9, PAL_MODE_INPUT); /* K2 Button */

/* Configure D1/2/3/4 LEDs as Input */
palSetPadMode(GPIOB, 0, PAL_MODE_INPUT);
palSetPadMode(GPIOB, 3, PAL_MODE_INPUT);
palSetPadMode(GPIOB, 4, PAL_MODE_INPUT);
palSetPadMode(GPIOB, 5, PAL_MODE_INPUT);
#endif
st25r3916_irq_fn = NULL;
}

static void bbio_mode_id(t_hydra_console *con) {
cprint(con, BBIO_HYDRANFC_READER, 4);
}

void bbio_mode_hydranfc_v2_reader(t_hydra_console *con) {
uint8_t bbio_subcommand, rlen, clen, compute_crc;
uint16_t rec_len;
uint8_t *rx_data = (uint8_t *) g_sbuf + 4096;
uint32_t flags;

init_gpio_spi_nfc(con);

bbio_mode_id(con);

while (!hydrabus_ubtn()) {

if (chnRead(con->sdu, &bbio_subcommand, 1) == 1) {
switch (bbio_subcommand) {
case BBIO_NFC_SET_MODE_ISO_14443A: {
rfalNfcaPollerInitialize(); /* Initialize RFAL for NFC-A */
break;
}
case BBIO_NFC_SET_MODE_ISO_15693: {
// TODO
break;
}
case BBIO_NFC_RF_OFF: {
rfalFieldOff();
break;
}
case BBIO_NFC_RF_ON: {
rfalFieldOnAndStartGT(); /* Turns the Field On and starts GT timer */
break;
}

case BBIO_NFC_CMD_SEND_BITS: {
chnRead(con->sdu, rx_data, 2);

rfalNfcaPollerCheckPresence(0x26, rx_data );

rlen = 2;
cprint(con, (char *) &rlen, 1);
cprint(con, (char *) rx_data, rlen);
break;
}
case BBIO_NFC_CMD_SEND_BYTES: {

chnRead(con->sdu, &compute_crc, 1);
chnRead(con->sdu, &clen, 1);
chnRead(con->sdu, rx_data, clen);

if( compute_crc){
flags = RFAL_TXRX_FLAGS_DEFAULT;
}
else{
flags = ( (uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL | (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_KEEP );
}
rfalTransceiveBlockingTxRx( &rx_data[0],
clen,
rx_data,
0xFF,
&rec_len,
flags,
216960U + 71680U );
rlen = (uint8_t) (rec_len & 0xFF);
cprint(con, (char *) &rlen, 1);
cprint(con, (char *) rx_data, rlen);
break;
}
case BBIO_RESET: {
deinit_gpio_spi_nfc(con);
return;
}
}
}
}
}
21 changes: 21 additions & 0 deletions src/hydranfc_v2/hydranfc_v2_bbio_reader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* HydraBus/HydraNFC v2
*
* Copyright (C) 2020 Guillaume VINET
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#define BBIO_HYDRANFC_READER "NFC2"

void bbio_mode_hydranfc_v2_reader(t_hydra_console *con);

1 comment on commit 7ddfa04

@bvernoux
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be modified BBIO_NFC_CMD_SEND_BITS to use a generic command to send bits as rfalNfcaPollerCheckPresence() is specific to NFC-A

Please sign in to comment.