-
-
Notifications
You must be signed in to change notification settings - Fork 676
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
base: main
Are you sure you want to change the base?
Conversation
… serial interface [no changelog]
2e6bf04
to
4c189eb
Compare
|
…rev.B [no changelog]
4c189eb
to
606d8c9
Compare
… board rev.B [no changelog]
core/embed/io/nfc/inc/io/nfc.h
Outdated
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef TREZORHAL_NFC_H |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in d0ecd9f
core/embed/io/nfc/inc/io/nfc.h
Outdated
#ifndef TREZORHAL_NFC_H | ||
#define TREZORHAL_NFC_H | ||
|
||
#include <trezor_bsp.h> |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in d0ecd9f
core/embed/io/nfc/inc/io/nfc.h
Outdated
|
||
#include <trezor_bsp.h> | ||
#include <trezor_rtl.h> | ||
#include "ndef.h" |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in d0ecd9f
core/embed/io/nfc/inc/io/nfc.h
Outdated
uint8_t uid_len; | ||
} nfc_dev_info_t; | ||
|
||
// Initialize NFC driver including supportive RFAL middlewere |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in d0ecd9f
core/embed/io/nfc/inc/io/nfc.h
Outdated
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in d0ecd9f
core/embed/io/nfc/st25r3916b/nfc.c
Outdated
|
||
// Enable clock of relevant peripherals | ||
// SPI + GPIO ports | ||
SPI_INSTANCE_3_CLK_EN(); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
core/embed/io/nfc/st25r3916b/nfc.c
Outdated
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo controled
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in d0ecd9f
core/embed/io/nfc/st25r3916b/nfc.c
Outdated
/*******************************************************************************/ | ||
|
||
// Card emulators need to promptly respond to the reader commands, so when | ||
// activated rfal worker is called seveal times untill back to back |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some typos here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in d0ecd9f
core/embed/io/nfc/st25r3916b/ndef.c
Outdated
|
||
|
||
#include "ndef.h" | ||
#include <stdint.h> |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in d0ecd9f
core/embed/io/nfc/inc/io/nfc.h
Outdated
} nfc_dev_info_t; | ||
|
||
// Initialize NFC driver including supportive RFAL middlewere | ||
nfc_status_t nfc_init(); |
There was a problem hiding this comment.
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);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in d0ecd9f
core/embed/io/nfc/st25r3916b/nfc.c
Outdated
#include "../rfal/include/rfal_t2t.h" | ||
#include "../rfal/include/rfal_utils.h" | ||
|
||
#include "stm32u5xx_hal.h" |
There was a problem hiding this comment.
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>
There was a problem hiding this comment.
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> |
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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.
core/embed/io/nfc/st25r3916b/ndef.h
Outdated
ndef_record_t records[NDEF_MAX_RECORDS]; | ||
} ndef_message_t; | ||
|
||
ndef_status_t parse_ndef_message(uint8_t *buffer, uint16_t buffer_len, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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;
}
There was a problem hiding this comment.
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
core/embed/io/nfc/st25r3916b/nfc.c
Outdated
return NFC_ERROR; | ||
} | ||
|
||
char *uid_str = hex2Str(nfcDevice->nfcid, nfcDevice->nfcidLen); |
There was a problem hiding this comment.
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.>
There was a problem hiding this comment.
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
core/embed/io/nfc/st25r3916b/nfc.c
Outdated
return NFC_OK; | ||
} | ||
|
||
// static void parse_tag_header(uint8_t *data, uint16_t dataLen) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
core/embed/io/nfc/st25r3916b/nfc.c
Outdated
return status; | ||
} | ||
|
||
uint32_t nfc_create_timer(uint16_t time) { return (systick_ms() + time); } |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in d0ecd9f
core/embed/io/nfc/st25r3916b/nfc.c
Outdated
|
||
uint32_t nfc_create_timer(uint16_t time) { return (systick_ms() + time); } | ||
|
||
bool nfc_timer_is_expired(uint32_t timer) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in d0ecd9f
09828c9
to
d0ecd9f
Compare
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.