NFC using app_scheduler.h and freeRTOS not working

Hi Friends,

- kit dev  NF52840-DK 

- IDE : segger studio IDE

- SDK nRF52 16.0.0 

I want to port the nfc_writable_ndef_msg NFC example to freeRTOS.

I use the NFC Tools app to write on the tag.

the concern I have a problem every time it updates the NDEF message in the flash file (error on NFC Tools application, hardfault in the ndef_file_update())

I think the problem is how I used app_scheduler and freeRTOS .

Do you have examples of using the app_schedule.h library and freeRTOS?

could you please help me

Thank you and best regards.

Lora

/**
 * Copyright (c) 2017 - 2019, Nordic Semiconductor ASA
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into a Nordic
 *    Semiconductor ASA integrated circuit in a product or a software update for
 *    such product, must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    Nordic Semiconductor ASA integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be reverse
 *    engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

/** @file
 *
 * @defgroup nfc_writable_ndef_msg_example_main main.c
 * @{
 * @ingroup nfc_writable_ndef_msg_example
 * @brief The application main file of NFC writable NDEF message example.
 *
 */

#include <stdint.h>
#include <stdbool.h>

#include "FreeRTOS.h"
#include "task.h"

#include "app_error.h"
#include "app_scheduler.h"
#include "boards.h"
#include "nfc_t4t_lib.h"
#include "nrf_log_ctrl.h"
#include "ndef_file_m.h"
#include "nfc_ndef_msg.h"

#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

#define APP_SCHED_MAX_EVENT_SIZE                            0                  /**< Maximum size of scheduler events. */
#define APP_SCHED_QUEUE_SIZE                                4                  /**< Maximum number of events in the scheduler queue. */
#define APP_DEFAULT_BTN                                     BSP_BOARD_BUTTON_0 /**< Button used to set default NDEF message. */
#define TASK_DELAY_LED0                                     200           /**< Task delay. Delays a LED0 task for 200 ms */
#define TASK_DELAY_LED1                                     1000          /**< Task delay. Delays a LED1 task for 1000 ms */

static uint8_t m_ndef_msg_buf[NDEF_FILE_SIZE];      /**< Buffer for NDEF file. */
static uint8_t m_ndef_msg_len;                      /**< Length of the NDEF message. */

TaskHandle_t  led_toggle_task_handle_0;   /**< Reference to LED0 toggling FreeRTOS task. */
TaskHandle_t  led_toggle_task_handle_1;  /**< Reference to LED1 toggling FreeRTOS task. */
TaskHandle_t  scheduler_task_handle;  /**< Reference to LED1 toggling FreeRTOS task. */



/*****************************************************************************/
/* @Function : Function used as callback Common application error handler    */
/*****************************************************************************/
void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info)
{
    //printf("Received a fault! id: 0x%08x, pc: 0x%08x, info: 0x%08x\r\n", id, pc, info);

    for (;;)
    {
        bsp_board_led_invert(3);
    }
}

/**@brief LED0 task entry function.
 *
 * @param[in] pvParameter   Pointer that will be used as the parameter for the task.
 */
static void led0_toggle_task_function (void * pvParameter)
{
    printf("led0_toggle_task_function START \n\r");
    UNUSED_PARAMETER(pvParameter);
    while (true)
    {
        bsp_board_led_invert(BSP_BOARD_LED_0);

        /* Delay a task for a given number of ticks */
        vTaskDelay(TASK_DELAY_LED0);

        /* Tasks must be implemented to never return... */
    }
}


/**@brief LED0 task entry function.
 *
 * @param[in] pvParameter   Pointer that will be used as the parameter for the task.
 */
static void led1_toggle_task_function (void * pvParameter)
{
    printf("led1_toggle_task_function START \n\r");
    UNUSED_PARAMETER(pvParameter);
    while (true)
    {
        bsp_board_led_invert(BSP_BOARD_LED_1);

        /* Delay a task for a given number of ticks */
        vTaskDelay(TASK_DELAY_LED1);

        /* Tasks must be implemented to never return... */
    }
}

/**
 * @brief Function for updating NDEF message in the flash file.
 */
static void scheduler_ndef_file_update(void * p_event_data, uint16_t event_size)
{
    //printf("scheduler_ndef_file_update START \n\r");
    ret_code_t err_code;

    UNUSED_PARAMETER(p_event_data);
    UNUSED_PARAMETER(event_size);

    // Update flash file with new NDEF message.
    err_code = ndef_file_update(m_ndef_msg_buf, m_ndef_msg_len + NLEN_FIELD_SIZE);
    APP_ERROR_CHECK(err_code);
}


static void scheduler_init()
{
    /* Initialize App Scheduler. */
    APP_SCHED_INIT(APP_SCHED_MAX_EVENT_SIZE, APP_SCHED_QUEUE_SIZE);
}

static void scheduler_thread(void * pvParameter)
{
    NRF_LOG_INFO("Starting scheduler thread")

    while(1) 
    {
        app_sched_execute();

        //vTaskSuspend(NULL);
    }
}

/**
 * @brief Callback function for handling NFC events.
 */
static void nfc_callback(void          * context,
                         nfc_t4t_event_t event,
                         const uint8_t * data,
                         size_t          dataLength,
                         uint32_t        flags)
{
    (void)context;

    switch (event)
    {
        case NFC_T4T_EVENT_FIELD_ON:
            bsp_board_led_on(BSP_BOARD_LED_0);
            break;

        case NFC_T4T_EVENT_FIELD_OFF:
            bsp_board_leds_off();
            break;

        case NFC_T4T_EVENT_NDEF_READ:
            bsp_board_led_on(BSP_BOARD_LED_3);
            break;

        case NFC_T4T_EVENT_NDEF_UPDATED:
            if (dataLength > 0)
            {
                ret_code_t err_code;

                bsp_board_led_on(BSP_BOARD_LED_1);

                // Schedule update of NDEF message in the flash file.
                m_ndef_msg_len = dataLength;
                err_code       = app_sched_event_put(NULL, 0, scheduler_ndef_file_update);
                APP_ERROR_CHECK(err_code);
            }
            break;

        default:
            break;
    }
}


/**
 *@brief Function for initializing logging.
 */
static void log_init(void)
{
    ret_code_t err_code = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(err_code);

    NRF_LOG_DEFAULT_BACKENDS_INIT();
}


/**
 * @brief   Function for application main entry.
 */
int main(void)
{
    ret_code_t err_code;

    log_init();

    /* Configure LED-pins as outputs */
    bsp_board_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS);

    scheduler_init();

    /* Initialize FDS. */
    err_code = ndef_file_setup();
    APP_ERROR_CHECK(err_code);

    /* Load NDEF message from the flash file. */
    err_code = ndef_file_load(m_ndef_msg_buf, sizeof(m_ndef_msg_buf));
    APP_ERROR_CHECK(err_code);

    // Restore default NDEF message.
    if (bsp_board_button_state_get(APP_DEFAULT_BTN))
    {
        uint32_t size = sizeof(m_ndef_msg_buf);
        err_code = ndef_file_default_message(m_ndef_msg_buf, &size);
        APP_ERROR_CHECK(err_code);
        err_code = ndef_file_update(m_ndef_msg_buf, NDEF_FILE_SIZE);
        APP_ERROR_CHECK(err_code);
        NRF_LOG_DEBUG("Default NDEF message restored!");
    }

    /* Set up NFC */
    err_code = nfc_t4t_setup(nfc_callback, NULL);
    APP_ERROR_CHECK(err_code);

    /* Run Read-Write mode for Type 4 Tag platform */
    err_code = nfc_t4t_ndef_rwpayload_set(m_ndef_msg_buf, sizeof(m_ndef_msg_buf));
    APP_ERROR_CHECK(err_code);

    NRF_LOG_INFO("Writable NDEF message example started.");

    /* Start sensing NFC field */
    err_code = nfc_t4t_emulation_start();
    APP_ERROR_CHECK(err_code);

        /* Create task for LED0 blinking with priority set to 2 */
    //UNUSED_VARIABLE(xTaskCreate(led0_toggle_task_function, "LED0", configMINIMAL_STACK_SIZE + 200, NULL, 2, &led_toggle_task_handle_0));

    /* Create task for LED1 blinking with priority set to 3 */
    //UNUSED_VARIABLE(xTaskCreate(led1_toggle_task_function, "LED1", configMINIMAL_STACK_SIZE + 200, NULL, 3, &led_toggle_task_handle_1));

    /* Create task for Scheduler with priority set to 1 */
    UNUSED_VARIABLE(xTaskCreate(scheduler_thread, "Scheduler", configMINIMAL_STACK_SIZE + 200, NULL, 1, &scheduler_task_handle));

    /* Start FreeRTOS scheduler. */
    vTaskStartScheduler();

    while (1)
    {
    }
}

void vApplicationIdleHook()
{
   //vTaskResume(scheduler_task_handle);
}


/** @} */

 

Parents Reply Children
Related