/*
 * Copyright (c) 2021 Pimax. All rights reserved.
 * only used for factory test
 * Author    Date          ChangeLog
 * Master    2021-03-18    First version
 */
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
#include "alps_config.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "sensor_register.h"
#include "twim.h"

TaskHandle_t  check_board_task_handle;
#define TWI_ADDRESSES      127
 
extern const nrf_drv_twi_t cs_twi_instance;
extern const nrf_drv_twi_t haptic_twi_instance;


static void check_scan_i2c_device(const nrf_drv_twi_t *twi)
{
    volatile ret_code_t err_code;
    uint8_t address;
    uint8_t sample_data;
    bool detected_device = false;

    for (address = 1; address <= TWI_ADDRESSES; address++)
    {
        err_code = twi_read_reg(twi, address, 0x75, &sample_data, sizeof(sample_data));
        //err_code = nrf_drv_twi_rx(twi, address, &sample_data, sizeof(sample_data));
        if (err_code == NRF_SUCCESS)
        {
            detected_device = true;
            NRF_LOG_INFO("TWI(%x) device detected at address 0x%x.", twi, address);
        }
        vTaskDelay(10);
    }
    if (!detected_device)
    {
        NRF_LOG_INFO("twi(%x)No device was found.", twi);
    }
}
 
static void check_board_task_entry(void *args)
{
    static uint32_t count = 0;
//    da7280_dev_t *da7280_dev = da7280_init();
//    sgm4151x_dev_t *sgm4151x_dev = sgm4151x_init();
//    aw2013_dev_t *aw2013_dev = aw2013_init();
    inv_icm4x6xx_t *icm42688_dev = icm42686_init();
    
    while(1)
    {
        //check_scan_i2c_device(&cs_twi_instance);
        //check_scan_i2c_device(&haptic_twi_instance);
#if 0
        if(sgm4151x_check_part_number(sgm4151x_dev))
        {
            NRF_LOG_INFO("%s id check success", sgm4151x_dev->name);
        }else
        {
            NRF_LOG_ERROR("%s id check failure", sgm4151x_dev->name);
        }
        
        if(da7280_check_chip_version(da7280_dev))
        {
            NRF_LOG_INFO("%s version check success", da7280_dev->name);
        }else
        {
            NRF_LOG_ERROR("%s version check failure", da7280_dev->name);
        }
        
        if(aw2013_check_id(aw2013_dev))
        {
            NRF_LOG_INFO("%s id check success", aw2013_dev->name);
        }else
        {
            NRF_LOG_ERROR("% id check failure", aw2013_dev->name);
        }
#endif

        //icm4x6xx_set_bank_sel(icm42688_dev, BANK_SEL_BANK3);
        //icm4x6xx_device_set_config(icm42688_dev, DEVICE_CONFIG_SPI_MODE_0_3, DEVICE_CONFIG_RESET_EN);
#if 1
        vTaskDelay(5);
        if(icm4x6xx_get_who_am_i(icm42688_dev))
        {
            NRF_LOG_INFO("%s who am i check success", icm42688_dev->name);
        }else
        {
            NRF_LOG_ERROR("%s  who am i check failure", icm42688_dev->name);
        }

        count ++;
        NRF_LOG_INFO("board check count=%d\r\n", count);
#endif
        vTaskDelay(1000);

    }
}

void check_board_init(void)
{
    /* Create task for LED0 blinking with priority set to 2 */
    BaseType_t ret = xTaskCreate(check_board_task_entry, 
                                "check board task", 
                                1024, NULL, 
                                5, &check_board_task_handle);
    if(ret != pdPASS)
    {
        NRF_LOG_ERROR("task creat failure:%d", ret);
    }
}


