Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce NFC driver with RFAL middleware #4566

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

kopecdav
Copy link

@kopecdav kopecdav commented Jan 31, 2025

This PR Introduce NFC driver with RFAL middleware to control ST25R3916.

RFAL is a ST middleware which provides a low level control of the ST25R3916 + an extra layer to support several different NFC standards.

PR also introduce prodtests to READ, EMULATE and WRITE to NFC card.

@kopecdav kopecdav added the T3W1 label Jan 31, 2025
@kopecdav kopecdav requested review from TychoVrahe and cepetr January 31, 2025 12:42
@kopecdav kopecdav self-assigned this Jan 31, 2025
@kopecdav kopecdav requested a review from prusnak as a code owner January 31, 2025 12:42
@TychoVrahe TychoVrahe removed the request for review from prusnak January 31, 2025 15:06
@kopecdav kopecdav force-pushed the kopecdav/T3W1/NFC_driver branch from 2e6bf04 to 4c189eb Compare January 31, 2025 15:47
Copy link

github-actions bot commented Jan 31, 2025

core UI changes device test click test persistence test
T2T1 Model T test(screens) main(screens) test(screens) main(screens) test(screens) main(screens)
T3B1 Safe 3 test(screens) main(screens) test(screens) main(screens) test(screens) main(screens)
T3T1 Safe 5 test(screens) main(screens) test(screens) main(screens) test(screens) main(screens)
All main(screens)

@kopecdav kopecdav force-pushed the kopecdav/T3W1/NFC_driver branch from 4c189eb to 606d8c9 Compare February 3, 2025 08:56
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef TREZORHAL_NFC_H
Copy link
Contributor

Choose a reason for hiding this comment

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

please use pragma once, we are sort of in transitory period but we tend to use this in new&modified files (this applies to the other headers as well, but not the 3rd pary ones)

Copy link
Author

Choose a reason for hiding this comment

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

fixed in d0ecd9f

#ifndef TREZORHAL_NFC_H
#define TREZORHAL_NFC_H

#include <trezor_bsp.h>
Copy link
Contributor

Choose a reason for hiding this comment

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

Neither trezor_bsp nor trezor_rtl should be included in header files. use trezor_types instead. See the respective headers for instructions

Copy link
Author

Choose a reason for hiding this comment

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

fixed in d0ecd9f


#include <trezor_bsp.h>
#include <trezor_rtl.h>
#include "ndef.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

ndef seems not needed now, lets remove it and add it when necessary

Copy link
Author

Choose a reason for hiding this comment

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

fixed in d0ecd9f

uint8_t uid_len;
} nfc_dev_info_t;

// Initialize NFC driver including supportive RFAL middlewere
Copy link
Contributor

Choose a reason for hiding this comment

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

type middleware, also few other typos in comments below (drive, activly,workerwith, NDFE)

Copy link
Author

Choose a reason for hiding this comment

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

fixed in d0ecd9f

nfc_status_t nfc_register_event_callback(nfc_event_t event_type,
void (*cb_fn)(void));

// Activate the NFC RFAL state machine which will explore the registered
Copy link
Contributor

Choose a reason for hiding this comment

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

this comment needs some polishing

Copy link
Author

Choose a reason for hiding this comment

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

fixed in d0ecd9f


// Enable clock of relevant peripherals
// SPI + GPIO ports
SPI_INSTANCE_3_CLK_EN();
Copy link
Contributor

Choose a reason for hiding this comment

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

lets also add FORCE_RESET & RELEASE RESET of the SPI peripheral

Copy link
Author

Choose a reason for hiding this comment

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

make sense, fixed in d0ecd9f

GPIO_InitStruct.Pin = SPI_INSTANCE_3_SCK_PIN;
HAL_GPIO_Init(SPI_INSTANCE_3_SCK_PORT, &GPIO_InitStruct);

// NSS pin controled by software, set as classical GPIO
Copy link
Contributor

Choose a reason for hiding this comment

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

typo controled

Copy link
Author

Choose a reason for hiding this comment

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

fixed in d0ecd9f

/*******************************************************************************/

// Card emulators need to promptly respond to the reader commands, so when
// activated rfal worker is called seveal times untill back to back
Copy link
Contributor

Choose a reason for hiding this comment

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

some typos here

Copy link
Author

Choose a reason for hiding this comment

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

fixed in d0ecd9f



#include "ndef.h"
#include <stdint.h>
Copy link
Contributor

Choose a reason for hiding this comment

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

use trezor_types, add license

Copy link
Author

Choose a reason for hiding this comment

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

fixed in d0ecd9f


// clang-format off

PRODTEST_CLI_CMD(
Copy link
Contributor

Choose a reason for hiding this comment

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

add readme entries for nrf commands in prodtest/README.md

Copy link
Author

Choose a reason for hiding this comment

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

fixed in d0ecd9f

} nfc_dev_info_t;

// Initialize NFC driver including supportive RFAL middlewere
nfc_status_t nfc_init();
Copy link
Contributor

Choose a reason for hiding this comment

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

In general it's better to explicitly specify that functions have no arguments, so in this case:

nfc_status_t nfc_init(void);

Copy link
Author

Choose a reason for hiding this comment

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

fixed in d0ecd9f

#include "../rfal/include/rfal_t2t.h"
#include "../rfal/include/rfal_utils.h"

#include "stm32u5xx_hal.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

This header is already included in <trezor_bsp.h>

Copy link
Author

Choose a reason for hiding this comment

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

fixed in d0ecd9f


#include <sys/irq.h>
#include <sys/systick.h>
#include <trezor_bsp.h>
Copy link
Contributor

Choose a reason for hiding this comment

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

There is no strict rule on how to order #include statements, but in new code, we follow this convention:

#include <trezor_xxx.h>.  <-- comes first
// empty line
#include <module/header.h> <-- comes next
// empty line
#include "local_header.h" <--- comes last

There is no strict rule on how to order #include statements, but in new code, we follow this convention:

Copy link
Author

Choose a reason for hiding this comment

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

Hope to fix correctly in d0ecd9f

*/
0x00, 0x00}; /* RD */

static ReturnCode nfc_transcieve_blocking(uint8_t *txBuf, uint16_t txBufSize,
Copy link
Contributor

Choose a reason for hiding this comment

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

In new code, we prefer camelCase over snake_case. camelCase is also used in other parts of the file.

ndef_record_t records[NDEF_MAX_RECORDS];
} ndef_message_t;

ndef_status_t parse_ndef_message(uint8_t *buffer, uint16_t buffer_len,
Copy link
Contributor

Choose a reason for hiding this comment

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

In these functions it would be better to define input arguments as const pointers.

Copy link
Author

Choose a reason for hiding this comment

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

Agree, fixed in d0ecd9f


uint32_t tick = systick_ms();

while (!nfc_dev_activated) { // Todo, while timeout or device found
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider this patter (that also some the problem with uint32_t wrapping):

uint32_t expire_time = ticks_timeout(timeout);

if (tick_expired(expire_time)) {
  cli_error(...);
  return;
}

Copy link
Author

Choose a reason for hiding this comment

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

good idea, fixed in d0ecd9f

return NFC_ERROR;
}

char *uid_str = hex2Str(nfcDevice->nfcid, nfcDevice->nfcidLen);
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider replacing hex2Str with cstr_encode_hex from <rtl/strutils.>

Copy link
Author

@kopecdav kopecdav Feb 6, 2025

Choose a reason for hiding this comment

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

Didnt know we already have it 😮 , fixed d0ecd9f but not tested yet, please leave open

return NFC_OK;
}

// static void parse_tag_header(uint8_t *data, uint16_t dataLen) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We usually remove any commented-out code before merging a pull request.

Copy link
Author

Choose a reason for hiding this comment

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

I wanted to keep the parser for the future, but i agree. removed in d0ecd9f

return status;
}

uint32_t nfc_create_timer(uint16_t time) { return (systick_ms() + time); }
Copy link
Contributor

Choose a reason for hiding this comment

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

see ticks_timeout() in sys/systick.h

Copy link
Author

Choose a reason for hiding this comment

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

fixed in d0ecd9f


uint32_t nfc_create_timer(uint16_t time) { return (systick_ms() + time); }

bool nfc_timer_is_expired(uint32_t timer) {
Copy link
Contributor

Choose a reason for hiding this comment

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

see ticks_expired() in sys/systick.h

Copy link
Author

Choose a reason for hiding this comment

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

fixed in d0ecd9f

@kopecdav kopecdav force-pushed the kopecdav/T3W1/NFC_driver branch from 09828c9 to d0ecd9f Compare February 6, 2025 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: 🏃‍♀️ In progress
Development

Successfully merging this pull request may close these issues.

3 participants