Hi Ricardo, thanks for your interest!
I flipped the code around to work with the demo code delivered in BSP v1.1 that I got from this topic: Can someone provide me with the out-of-the-box firmware/code? It doesn't use an SDK but its functionality is complete, so I don't have to merge projects.
The code works a bit strange; Ít has a main loop and an interrupt timer loop (CT32B0) and it references to 2 different projects (lib_board_bp and lib_chip_8Nxx) to use as libraries, where the BSP V1.0 (provided on the LPC8N04) uses these projects as included libraries. Anyhow, it works.
The interrupt timer is programmed to poll the level of the button and applies debouncing. I have no idea why they didn't implement a simple rising edge interrupt on pin 0_0, but I made it work with a state machine. The state machine was needed to avoid a bug when you held down the button, the code was executed over and over again.
But now back to my problem:
I made the code work, it stores my button value (on/off text message) in the EEPROM that I can read again in the next start-up of the device.
Every time the button is pressed, it toggles between the 'button on' and 'button off' state.
while (1){ // if running on NFC power (Merijn) (True == NFC, false == battery. Use '1' for both states. This loops forever with CT32B0 timer interrupting for button press state)
//g_LPC8N04PSTAT = Chip_PMU_Switch_GetVNFC(); // get powerstate
__WFI(); // wait for clock interrupt for button press
if ((switchPressed == true) && (exFlag == false)){ // if button is pressed and this button press hasnt been executed yet
exFlag = true;
changeFont = false; //pre programmed trigger in demo code, set low to avoid weird behavior
textSize = eepromReadTag(EE_DISP_TEXT, (uint8_t *)eepromCheck, 6); // Copied from earlier in the code, checks length I think.
smallDelay(); // small delay to give extra time to modules (not crucial)
if(!strcmp(eepromCheck, btnTextOff) || (!strcmp(eepromCheck, btnHexOff))){ // check if data last time was 'off'
//BtnTextOn = Ascii, BtnHexOn = Hex Bytes for test
smallDelay();
writeNFCDisplayMsg(btnTextOn, strlen(btnTextOn)); // set to 'on', write NDEF message
smallDelay();
eepromWriteTag(EE_DISP_TEXT, (uint8_t *)btnTextOn, strlen(btnTextOn)); // store state in EEPROM
} else if ((!strcmp(eepromCheck, btnTextOn)) || (!strcmp(eepromCheck, btnTextBug)) || (!strcmp(eepromCheck, btnHexOn))){ // check if data was 'on' or bugged
smallDelay();
writeNFCDisplayMsg(btnTextOff, strlen(btnTextOff)); // set to off
smallDelay();
eepromWriteTag(EE_DISP_TEXT, (uint8_t *)btnTextOff, strlen(btnTextOff)); // store state
} else { // in any other case when data is unexpected
writeNFCDisplayMsg(btnTextBug, strlen(btnTextBug)); // send 'bugged'
smallDelay();
eepromWriteTag(EE_DISP_TEXT, (uint8_t *)btnTextBug, strlen(btnTextBug)); // store state
smallDelay();
}
//releaseMemSemaphore(); // release memory usage, just to be sure
} else if ((switchPressed == false) && (exFlag == true)){ // reset executed flag when button is low
exFlag = false;
}
}(There are some small tests I have tried in this code above, ignore those, like the small delay and MemSemaphore/PMU state)
This works beautifully on battery/USB power, but I still can't get it to work on VNFC, antenna power.
When I press the button, the old value is still read in the NFC NDEF message on my phone app. The state of the button didn't update from off to on or vice versa.
It seems like pressing the button draws too much current and it resets my MCU code before it can execute my button code. Is this correct? And if it is, how can I fix it? I have no errors in my code according to MCUXpresso.