Skip to content

Commit

Permalink
NFC: add GPO interrupt
Browse files Browse the repository at this point in the history
  • Loading branch information
polesskiy-dev committed Oct 25, 2023
1 parent 8fb79b8 commit 36712db
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 233 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
#Sat Oct 21 15:14:08 TRT 2023
#Tue Oct 24 22:29:15 TRT 2023
default.languagetoolchain.version=4.30
default.Pack.dfplocation=/Applications/microchip/mplabx/v6.10/packs/Microchip/SAMD21_DFP/3.6.144
default.com-microchip-mplab-mdbcore-PK5Tool-PK5ToolImpl.md5=03e12bb97e05706420942b3ce207925c
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
<file>file:/Users/artempolisskyi/projects/iot-risk-data-logger-nfc-samd21/firmware/src/config/default/usb/src/usb_device.c</file>
<file>file:/Users/artempolisskyi/projects/iot-risk-data-logger-nfc-samd21/firmware/src/nfc/nfc_fsm.c</file>
<file>file:/Users/artempolisskyi/projects/iot-risk-data-logger-nfc-samd21/firmware/src/nfc/nfc.h</file>
<file>file:/Users/artempolisskyi/projects/iot-risk-data-logger-nfc-samd21/firmware/src/storage/storage_manager.h</file>
<file>file:/Users/artempolisskyi/projects/iot-risk-data-logger-nfc-samd21/firmware/src/config/default/exceptions.c</file>
<file>file:/Users/artempolisskyi/projects/iot-risk-data-logger-nfc-samd21/firmware/src/config/default/initialization.c</file>
<file>file:/Users/artempolisskyi/projects/iot-risk-data-logger-nfc-samd21/firmware/src/sensors/sht3x-temperature-humidity/sht3x.c</file>
<file>file:/Users/artempolisskyi/projects/iot-risk-data-logger-nfc-samd21/firmware/src/sensors/sht3x-temperature-humidity/sht3x.h</file>
<file>file:/Users/artempolisskyi/projects/iot-risk-data-logger-nfc-samd21/firmware/src/main.c</file>
<file>file:/Users/artempolisskyi/projects/iot-risk-data-logger-nfc-samd21/firmware/src/nfc/nfc.c</file>
<file>file:/Users/artempolisskyi/projects/iot-risk-data-logger-nfc-samd21/firmware/src/config/default/driver/memory/src/drv_memory.c</file>
<file>file:/Users/artempolisskyi/projects/iot-risk-data-logger-nfc-samd21/firmware/src/config/default/system/console/src/sys_console.c</file>
</group>
</open-files>
</project-private>
145 changes: 15 additions & 130 deletions firmware/iot-risk-data-logger-nfc-samd21.X/queuelogs/debugtool

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion firmware/src/init_manager/init_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void INIT_Initialize(uintptr_t context) {
ActiveObject_Initialize(&initAO.super, INIT_AO_ID, events, INIT_QUEUE_MAX_CAPACITY);

// init storage on next cycle
ActiveObject_Dispatch(&initAO.super, (TEvent) {.sig = INIT_SIG_STORAGE});
// ActiveObject_Dispatch(&initAO.super, (TEvent) {.sig = INIT_SIG_STORAGE});
// init sensors on next cycle
// ActiveObject_Dispatch(&initAO.super, (TEvent) {.sig = INIT_SIG_SENSORS});
// init NFC on next cycle
Expand Down
14 changes: 12 additions & 2 deletions firmware/src/nfc/nfc.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ TActiveObject *NFC_Initialize(void) {
(uintptr_t) &nfcAO
);

// Register callback for NFC GPO rise and fall events (RF presence / unpresence)
// Register callback for NFC GPO fall events (RF presence / absence)
EIC_CallbackRegister(EIC_PIN_3, _onNFCGPOPinChange, (uintptr_t) &nfcAO);

return (TActiveObject *) &nfcAO;
Expand All @@ -63,6 +63,13 @@ void NFC_Tasks(void) {
NFC_SIG_MAX, nfcTransitionTable);

if (FSM_IsValidState(nextState)) FSM_TraverseAOToNextState(&nfcAO.super, nextState);

#ifdef __DEBUG
// TODO replace by simplified macro: #STRINGIFY_VAR(VAR) ("#VAR")
// int sig = event.sig;
// int name = nextState->name;
// SYS_DEBUG_PRINT(SYS_ERROR_INFO, "NFC Event: %d, Next State: %d\n", sig, name);
#endif
}

/**
Expand Down Expand Up @@ -110,5 +117,8 @@ static DRV_HANDLE _openI2CDriver(void) {

/** @brief FIELD_CHANGE_EN: A pulse is emitted on GPO, when RF field appears or disappears */
static void _onNFCGPOPinChange(uintptr_t context) {
ActiveObject_Dispatch(&nfcAO.super, (TEvent) {.sig = NFC_GPO_PULSE});
// static volatile uint8_t gpo = 0;
SYS_DEBUG_PRINT(SYS_ERROR_INFO, "GPO %d\n");
TNFCActiveObject *nfcAO = (TNFCActiveObject *) context;
ActiveObject_Dispatch(&nfcAO->super, (TEvent) {.sig = NFC_GPO_PULSE});
};
2 changes: 1 addition & 1 deletion firmware/src/nfc/nfc.config.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extern "C" {

#define NFC_QUEUE_MAX_CAPACITY (16)

#define NFC_TRANSFER_RETRIES_MAX (0x08)
#define NFC_TRANSFER_RETRIES_MAX (0x20)

/* all SIZE is in Bytes */
#define NFC_UID_SIZE (0x08)
Expand Down
21 changes: 12 additions & 9 deletions firmware/src/nfc/nfc.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,18 @@ typedef struct {
struct {
uint8_t uid[NFC_UID_SIZE];
uint8_t pwd[NFC_PASSWORD_SIZE];
struct {
unsigned RF_USER:1;
unsigned RF_ACTIVITY:1;
unsigned RF_INTERRUPT:1;
unsigned FIELD_FALLING:1;
unsigned FIELD_RISING:1;
unsigned RF_PUT_MSG:1;
unsigned RF_GET_MSG:1;
unsigned RF_WRITE:1;
union {
uint8_t raw;
struct {
unsigned RF_USER:1;
unsigned RF_ACTIVITY:1;
unsigned RF_INTERRUPT:1;
unsigned FIELD_FALLING:1;
unsigned FIELD_RISING:1;
unsigned RF_PUT_MSG:1;
unsigned RF_GET_MSG:1;
unsigned RF_WRITE:1;
} bitFields;
} interruptStatus;
} st25dvRegs;
} TNFCActiveObject;
Expand Down
47 changes: 16 additions & 31 deletions firmware/src/nfc/nfc_fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ const TState nfcStatesList[NFC_STATES_MAX] = {
[NFC_ST_INIT] = {.name = NFC_ST_INIT},
[NFC_ST_IDLE] = {.name = NFC_ST_IDLE},
[NFC_ST_READ_UID] = {.name = NFC_ST_READ_UID},
[NFC_ST_READ_INTERRUPT_STATUS] = {.name = NFC_ST_READ_INTERRUPT_STATUS},
[NFC_SUPER_ST_PREPARE_MAILBOX] = {.name = NFC_SUPER_ST_PREPARE_MAILBOX, .onEnter = _refreshRetries},
[NFC_ST_READ_INTERRUPT_STATUS] = {.name = NFC_ST_READ_INTERRUPT_STATUS},
[NFC_SUPER_ST_PREPARE_MAILBOX] = {.name = NFC_SUPER_ST_PREPARE_MAILBOX, .onExit = _refreshRetries},
[NFC_ST_WRITE_MAILBOX] = {.name = NFC_ST_WRITE_MAILBOX, .onExit = _refreshRetries},
[NFC_ST_READ_MAILBOX] = {.name = NFC_ST_READ_MAILBOX, .onExit = _refreshRetries},
[NFC_ST_ERROR] = {.name = NFC_ST_ERROR}
Expand All @@ -47,7 +47,7 @@ const TState nfcStatesList[NFC_STATES_MAX] = {
const TEventHandler nfcTransitionTable[NFC_STATES_MAX][NFC_SIG_MAX] = {
[NFC_ST_INIT]= {[NFC_READ_UID]=_readUID, [NFC_ERROR]=_error},
[NFC_ST_IDLE]= {[NFC_GPO_PULSE]=_readInterruptStatus, [NFC_WRITE_MAILBOX]=_writeMailbox, [NFC_ERROR]=_error},
[NFC_ST_READ_INTERRUPT_STATUS]= {[NFC_TRANSFER_SUCCESS]=_handleInterruptStatus, [NFC_TRANSFER_FAIL]=_error, [NFC_ERROR]=_error},
[NFC_ST_READ_INTERRUPT_STATUS]= {[NFC_TRANSFER_SUCCESS]=_handleInterruptStatus, /*[NFC_GPO_PULSE]=_readInterruptStatus*/ /*[NFC_TRANSFER_FAIL]=_error TODO */ [NFC_ERROR]=_error},
[NFC_ST_READ_UID]= {[NFC_TRANSFER_SUCCESS]=_prepareMailbox, [NFC_TRANSFER_FAIL]=_error, [NFC_ERROR]=_error},
/* Prepare mailbox (enable Fast Transfer mode) */
[NFC_SUPER_ST_PREPARE_MAILBOX]= {[NFC_PREPARE_MAILBOX_SUCCESS]=_idle, [NFC_TRANSFER_SUCCESS]=_prepareMailbox, [NFC_TRANSFER_FAIL]=_prepareMailbox, [NFC_TRANSFER_MAX_RETRIES]=_error, [NFC_ERROR]=_error},/* Check RF field */
Expand Down Expand Up @@ -110,12 +110,14 @@ static const TState *_writeMailbox(TActiveObject *const AO, TEvent event) {
static const TState *_readInterruptStatus(TActiveObject *const AO, TEvent event) {
TNFCActiveObject *nfcAO = (TNFCActiveObject *) AO;

nfcAO->st25dvRegs.interruptStatus.raw = 0x00;

DRV_I2C_WriteReadTransferAdd(
nfcAO->drvI2CHandle,
ST25DV_ADDR_DATA_I2C,
(void *const) &ST25DV_ITSTS_DYN_REG,
NFC_CMD_SIZE,
&(nfcAO->st25dvRegs.interruptStatus),
&(nfcAO->st25dvRegs.interruptStatus.raw),
NFC_ITSTS_SIZE,
&(nfcAO->transferHandle)
);
Expand All @@ -126,36 +128,39 @@ static const TState *_readInterruptStatus(TActiveObject *const AO, TEvent event)
}

static const TState *_handleInterruptStatus(TActiveObject *const AO, TEvent event) {
static uint8_t invoked = 0;
TNFCActiveObject *nfcAO = (TNFCActiveObject *) AO;

if (nfcAO->st25dvRegs.interruptStatus.RF_ACTIVITY) {
if (nfcAO->st25dvRegs.interruptStatus.bitFields.RF_ACTIVITY) {
// TODO handle RF activity
};

if (nfcAO->st25dvRegs.interruptStatus.RF_INTERRUPT) {
if (nfcAO->st25dvRegs.interruptStatus.bitFields.RF_INTERRUPT) {
// TODO handle RF interrupt
};

if (nfcAO->st25dvRegs.interruptStatus.FIELD_FALLING) {
if (nfcAO->st25dvRegs.interruptStatus.bitFields.FIELD_FALLING) {
// TODO handle FIELD falling
};

if (nfcAO->st25dvRegs.interruptStatus.FIELD_RISING) {
if (nfcAO->st25dvRegs.interruptStatus.bitFields.FIELD_RISING) {
// TODO handle FIELD rising
};

if (nfcAO->st25dvRegs.interruptStatus.RF_PUT_MSG) {
if (nfcAO->st25dvRegs.interruptStatus.bitFields.RF_PUT_MSG) {
// TODO handle RF put message
};

if (nfcAO->st25dvRegs.interruptStatus.RF_GET_MSG) {
if (nfcAO->st25dvRegs.interruptStatus.bitFields.RF_GET_MSG) {
// TODO handle RF get message
};

if (nfcAO->st25dvRegs.interruptStatus.RF_WRITE) {
if (nfcAO->st25dvRegs.interruptStatus.bitFields.RF_WRITE) {
// TODO handle RF write
};

SYS_DEBUG_PRINT(SYS_ERROR_INFO, "GPO IT_STS_Dyn: %X, invoked %d\n", nfcAO->st25dvRegs.interruptStatus.raw, invoked++);

return &(nfcStatesList[NFC_ST_IDLE]);
}

Expand Down Expand Up @@ -196,23 +201,3 @@ void NFC_VerifyRetries(TNFCActiveObject *const nfcAO) {
ActiveObject_Dispatch(&(nfcAO->super), (TEvent) {.sig = NFC_TRANSFER_MAX_RETRIES});
}
};

//
//static inline void validateTransferHandle(NFC_ACT_OBJ *me) {
// // error on i2c transfer queuing
// if (DRV_I2C_TRANSFER_HANDLE_INVALID == me->transferHandle) {
// NFC_ACT_Dispatch((QUEUE_EVENT) {.nfcSig = NFC_ERROR});
// };
//};
//
//static inline void checkRemainingRetries(NFC_ACT_OBJ *me, QUEUE_EVENT event) {
// // decrease remaining retries on transfer fail
// if (NFC_TRANSFER_FAIL == event.nfcSig) {
// if (NO_RETRIES_LEFT == --me->retriesLeft) {
// NFC_ACT_Dispatch((QUEUE_EVENT) {.nfcSig = NFC_TRANSFER_MAX_RETRIES});
// }
// } else {
// // or refresh counter on non fail event
// me->retriesLeft = NFC_TRANSFER_RETRIES_MAX;
// };
//};

0 comments on commit 36712db

Please sign in to comment.