/*
|
* Copyright (c) 2019-2023 Beijing Hanwei Innovation Technology Ltd. Co. and
|
* its subsidiaries and affiliates (collectly called MKSEMI).
|
*
|
* 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 an MKSEMI
|
* 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 MKSEMI 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
|
* MKSEMI 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 MKSEMI "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 MKSEMI 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.
|
*/
|
|
#include "mk_trace.h"
|
#include "mk_wdt.h"
|
#include "mk_reset.h"
|
#include "mk_gpio.h"
|
#include "mk_misc.h"
|
#include "mk_sleep_timer.h"
|
#include "mk_power.h"
|
#include "mk_uwb.h"
|
#include "mk_calib.h"
|
#include "mk_spi.h"
|
#include "mk_flash.h"
|
|
#include "board.h"
|
|
#include "pal_sys.h"
|
#include "wsf_os.h"
|
#include "wsf_timer.h"
|
#include "wsf_buf.h"
|
#include "wsf_nvm.h"
|
|
#include "app.h"
|
#include "ranging_custom.h"
|
#include "uwb_api.h"
|
#include "lib_ranging.h"
|
#include "se_api.h"
|
#include "libc_rom.h"
|
|
// 1=No SE; 0 = SE exist and process apdu.
|
#define TRANSACTION_SIM_EN 0
|
|
#define INITIATOR_ADDR (0xAAA1)
|
#define RESPONDER_ADDR (0xAAA2)
|
|
#define USER_RANGING_SESSION 0x0001
|
|
static uint8_t ranging_on_flag = 0;
|
static uint32_t session_id = USER_RANGING_SESSION;
|
|
static uint8_t apdu_count = 0;
|
static uint8_t try_count = 0;
|
static uint8_t se_transaction_start = 0;
|
|
static uint8_t se_master;
|
// uint8_t master_apdu_send_status;
|
// uint8_t slave_apdu_resp_send_status;
|
static uint8_t wait_rx_from_se = 0;
|
|
static uint8_t apdu_1[0x15] = {0x00, 0xA4, 0x04, 0x00, 0x10, 0xA0, 0x00, 0x00, 0x03, 0x33, 0x01, 0x01, 0x02, 0x00, 0x63, 0x48, 0x57, 0x50, 0x41, 0x59, 0x05};
|
static uint8_t apdu_1_resp[] = {0x6f, 0x32, 0x84, 0x09, 0xa0, 0x00, 0x00, 0x00, 0x03, 0x86, 0x98, 0x07, 0x01, 0xa5, 0x25, 0x9f, 0x08, 0x01,
|
0x02, 0x9f, 0x0c, 0x1e, 0x86, 0x98, 0x20, 0x00, 0x75, 0x90, 0xff, 0xff, 0x82, 0x05, 0x20, 0x00, 0xbe, 0xc2,
|
0xaf, 0x1d, 0xdc, 0x55, 0x93, 0xb8, 0x20, 0x19, 0x03, 0x18, 0x20, 0x24, 0x03, 0x17, 0x22, 0x14, 0x90, 0x00};
|
|
static uint8_t apdu_2[14] = {0x00, 0xA4, 0x04, 0x00, 0x09, 0xA0, 0x00, 0x00, 0x00, 0x03, 0x86, 0x98, 0x07, 0x01};
|
static uint8_t apdu_2_resp[] = {0x6f, 0x32, 0x84, 0x09, 0xa0, 0x00, 0x00, 0x00, 0x03, 0x86, 0x98, 0x07, 0x01, 0xa5, 0x25, 0x9f, 0x08, 0x01,
|
0x02, 0x9f, 0x0c, 0x1e, 0x86, 0x98, 0x20, 0x00, 0x75, 0x90, 0xff, 0xff, 0x82, 0x05, 0x20, 0x00, 0xbe, 0xc2,
|
0xaf, 0x1d, 0xdc, 0x55, 0x93, 0xb8, 0x20, 0x19, 0x03, 0x18, 0x20, 0x24, 0x03, 0x17, 0x22, 0x14, 0x90, 0x00};
|
|
static uint8_t apdu_3[5] = {0x00, 0xB0, 0x95, 0x00, 0x00};
|
static uint8_t apdu_3_resp[] = {0x86, 0x98, 0x20, 0x00, 0x75, 0x90, 0xff, 0xff, 0x82, 0x05, 0x20, 0x00, 0xbe, 0xc2, 0xaf, 0x1d,
|
0xdc, 0x55, 0x93, 0xb8, 0x20, 0x19, 0x03, 0x18, 0x20, 0x24, 0x03, 0x17, 0x22, 0x14, 0x90, 0x00};
|
|
static uint8_t apdu_4[5] = {0x00, 0xB0, 0x96, 0x00, 0x00};
|
static uint8_t apdu_4_resp[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x90, 0x00};
|
|
static uint8_t apdu_5[5] = {0x80, 0x5C, 0x00, 0x02, 0x04};
|
static uint8_t apdu_5_resp[] = {0x00, 0x00, 0x00, 0x00, 0x90, 0x00};
|
|
void master_process_apdu(const uint8_t *ptr, uint16_t rx_len_tmp);
|
void slave_process_apdu(const uint8_t *ptr, uint16_t rx_len_tmp, uint8_t ack);
|
|
static uint16_t get_apdu_ptr_and_len(uint8_t count, uint8_t is_apdu, uint8_t **ptr)
|
{
|
uint16_t len = 0;
|
if (is_apdu == 1)
|
{
|
LOG_INFO(TRACE_MODULE_SE, "============================================\r\n");
|
switch (count)
|
{
|
case 0x00:
|
{
|
*ptr = apdu_1;
|
len = sizeof(apdu_1);
|
LOG_INFO(TRACE_MODULE_SE, "Master send apdu command 1:select applet 1\r\n");
|
}
|
break;
|
case 0x01:
|
{
|
*ptr = apdu_2;
|
len = sizeof(apdu_2);
|
LOG_INFO(TRACE_MODULE_SE, "Master send apdu command 2:select applet 2\r\n");
|
}
|
break;
|
case 0x02:
|
{
|
*ptr = apdu_3;
|
len = sizeof(apdu_3);
|
LOG_INFO(TRACE_MODULE_SE, "Master send apdu command 3:read file 1\r\n");
|
}
|
break;
|
case 0x03:
|
{
|
*ptr = apdu_4;
|
len = sizeof(apdu_4);
|
LOG_INFO(TRACE_MODULE_SE, "Master send apdu command 4:read file 2\r\n");
|
}
|
break;
|
case 0x04:
|
{
|
*ptr = apdu_5;
|
len = sizeof(apdu_5);
|
LOG_INFO(TRACE_MODULE_SE, "Master send apdu command 5:get balance\r\n");
|
}
|
break;
|
default:
|
LOG_INFO(TRACE_MODULE_APP, "apdu_count = %02x&&&\r\n", apdu_count);
|
break;
|
}
|
}
|
else
|
{
|
switch (count)
|
{
|
case 0x00:
|
{
|
*ptr = apdu_1_resp;
|
len = sizeof(apdu_1_resp);
|
LOG_INFO(TRACE_MODULE_SE, "Master receive Response for apdu:select applet 1\r\n");
|
}
|
break;
|
case 0x01:
|
{
|
*ptr = apdu_2_resp;
|
len = sizeof(apdu_2_resp);
|
LOG_INFO(TRACE_MODULE_SE, "Master receive Response for apdu:select applet 2\r\n");
|
}
|
break;
|
case 0x02:
|
{
|
*ptr = apdu_3_resp;
|
len = sizeof(apdu_3_resp);
|
LOG_INFO(TRACE_MODULE_SE, "Master receive Response for apdu:read file 1\r\n");
|
}
|
break;
|
case 0x03:
|
{
|
*ptr = apdu_4_resp;
|
len = sizeof(apdu_4_resp);
|
LOG_INFO(TRACE_MODULE_SE, "Master receive Response for apdu:read file 2\r\n");
|
}
|
break;
|
case 0x04:
|
{
|
*ptr = apdu_5_resp;
|
len = sizeof(apdu_5_resp);
|
LOG_INFO(TRACE_MODULE_SE, "Master receive Response for apdu:get balance\r\n");
|
LOG_INFO(TRACE_MODULE_SE, "The balance is 0!!!!\r\n");
|
LOG_INFO(TRACE_MODULE_SE, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n");
|
}
|
break;
|
}
|
}
|
|
return len;
|
}
|
|
static uint8_t check_apdu_ptr_and_len(const uint8_t *data_buf, uint16_t data_len, uint8_t is_apdu)
|
{
|
uint8_t count = 255;
|
if (is_apdu)
|
{
|
LOG_INFO(TRACE_MODULE_SE, "===================================================\r\n");
|
if ((data_len == sizeof(apdu_1)) && (memcmp(data_buf, apdu_1, data_len) == 0))
|
{
|
count = 0;
|
LOG_INFO(TRACE_MODULE_SE, "Slave is ready to receive apdu command 1:select applet 1\r\n");
|
}
|
else if ((data_len == sizeof(apdu_2)) && (memcmp(data_buf, apdu_2, data_len) == 0))
|
{
|
count = 1;
|
LOG_INFO(TRACE_MODULE_SE, "Slave is ready to receive apdu command 2:select applet 2\r\n");
|
}
|
else if ((data_len == sizeof(apdu_3)) && (memcmp(data_buf, apdu_3, data_len) == 0))
|
{
|
count = 2;
|
LOG_INFO(TRACE_MODULE_SE, "Slave is ready to receive apdu command 3:read file 1\r\n");
|
}
|
else if ((data_len == sizeof(apdu_4)) && (memcmp(data_buf, apdu_4, data_len) == 0))
|
{
|
count = 3;
|
LOG_INFO(TRACE_MODULE_SE, "Slave is ready to receive apdu command 4:read file 2\r\n");
|
}
|
else if ((data_len == sizeof(apdu_5)) && (memcmp(data_buf, apdu_5, data_len) == 0))
|
{
|
count = 4;
|
LOG_INFO(TRACE_MODULE_SE, "Slave is ready to receive apdu command 5:get balance\r\n");
|
}
|
// LOG_INFO(TRACE_MODULE_SE, "[RX][%d]", rx_len_tmp);
|
// for (uint8_t i = 0; i < rx_len_tmp; i++)
|
// {
|
// LOG_INFO(TRACE_NO_OPTION | TRACE_MODULE_SE, " %02x", ptr[i]);
|
// }
|
// LOG_INFO(TRACE_NO_OPTION | TRACE_MODULE_SE, "\r\n");
|
}
|
else
|
{
|
if ((data_len == sizeof(apdu_1_resp)) && (memcmp(data_buf, apdu_1_resp, data_len) == 0))
|
{
|
count = 0;
|
LOG_INFO(TRACE_MODULE_SE, "Slave is ready to send response for apdu:select applet \r\n");
|
LOG_INFO(TRACE_MODULE_SE, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n");
|
}
|
else if ((data_len == sizeof(apdu_2_resp)) && (memcmp(data_buf, apdu_2_resp, data_len) == 0))
|
{
|
// cannot enter this branch because apdu_1_resp == apdu_2_resp
|
count = 1;
|
LOG_INFO(TRACE_MODULE_SE, "Slave is ready to send response for apdu:select applet 2\r\n");
|
LOG_INFO(TRACE_MODULE_SE, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n");
|
}
|
else if ((data_len == sizeof(apdu_3_resp)) && (memcmp(data_buf, apdu_3_resp, data_len) == 0))
|
{
|
count = 2;
|
LOG_INFO(TRACE_MODULE_SE, "Slave is ready to send response for apdu:read file 1\r\n");
|
LOG_INFO(TRACE_MODULE_SE, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n");
|
}
|
else if ((data_len == sizeof(apdu_4_resp)) && (memcmp(data_buf, apdu_4_resp, data_len) == 0))
|
{
|
count = 3;
|
LOG_INFO(TRACE_MODULE_SE, "Slave is ready to send response for apdu:read file 2\r\n");
|
LOG_INFO(TRACE_MODULE_SE, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n");
|
}
|
else if ((data_len == sizeof(apdu_5_resp)) && (memcmp(data_buf, apdu_5_resp, data_len) == 0))
|
{
|
count = 4;
|
LOG_INFO(TRACE_MODULE_SE, "Slave is ready to send response for apdu:get balance\r\n");
|
LOG_INFO(TRACE_MODULE_SE, "Get balance and all commands are OK!!!!\r\n");
|
LOG_INFO(TRACE_MODULE_SE, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n");
|
}
|
|
// LOG_INFO(TRACE_MODULE_SE, "[TX][%d]", resp_len);
|
// for (uint8_t i = 0; i < resp_len; i++)
|
// {
|
// LOG_INFO(TRACE_NO_OPTION | TRACE_MODULE_SE, " %02x", ( (uint8_t *)resp_addr)[i]);
|
// }
|
// LOG_INFO(TRACE_NO_OPTION | TRACE_MODULE_SE, "\r\n");
|
}
|
|
return count;
|
}
|
|
#if TRANSACTION_SIM_EN == 0
|
static int uwb_se_init(void)
|
{
|
int ret = 0;
|
if (se_init(ESE_MODE_NORMAL) == ESESTATUS_SUCCESS)
|
{
|
ret = 1;
|
}
|
LOG_INFO(TRACE_MODULE_SE, "uwb_se_init %d\r\n", ret);
|
return ret;
|
}
|
|
static void spi_init_on_se(void)
|
{
|
// 10000000, it's 7.8M not 10M
|
struct SPI_CFG_T usr_spi_cfg = {
|
.bit_rate = 1000000,
|
.data_bits = 8,
|
.slave = 0,
|
.clk_phase = 0,
|
.clk_polarity = 0,
|
.ti_mode = 0,
|
.dma_rx = false,
|
.dma_tx = false,
|
.int_rx = false,
|
.int_tx = false,
|
};
|
|
spi_open(SPI_ID0, &usr_spi_cfg);
|
|
uwb_se_init();
|
}
|
#endif
|
|
static void apdu_init(uint8_t master)
|
{
|
master_apdu_send_status = 0;
|
slave_apdu_resp_send_status = 0;
|
try_count = 0;
|
apdu_count = 0;
|
|
if (master)
|
{
|
// trigger SE transaction every 500ms--->5000ms
|
app_timer_set(APP_TEST_TIMER1, 2000, WSF_TIMER_PERIODIC);
|
}
|
else
|
{
|
#if TRANSACTION_SIM_EN == 0
|
spi_init_on_se();
|
#endif
|
}
|
}
|
|
void master_process_apdu(const uint8_t *ptr, uint16_t rx_len_tmp)
|
{
|
uint8_t *resp_ptr = NULL;
|
uint16_t resp_len;
|
uint8_t *tx_ptr = NULL;
|
uint16_t tx_len_tmp;
|
|
if (se_transaction_start)
|
{
|
if (master_apdu_send_status == 1)
|
{
|
// clear apdu data
|
ranging_poll_msg_set(0, NULL);
|
master_apdu_send_status = 0;
|
}
|
|
if (rx_len_tmp == 0)
|
{
|
try_count++;
|
}
|
else
|
{
|
resp_len = get_apdu_ptr_and_len(apdu_count, 0, &resp_ptr);
|
if ((rx_len_tmp == resp_len) && (memcmp(ptr, resp_ptr, rx_len_tmp) == 0))
|
{
|
LOG_INFO(TRACE_MODULE_SE, "APDU COMPLETE AND OK %d\r\n", apdu_count);
|
apdu_count++;
|
if (apdu_count == 5)
|
{
|
// transaction done
|
se_transaction_start = 0;
|
|
try_count = 0;
|
apdu_count = 0;
|
|
ranging_poll_msg_set(0, NULL);
|
}
|
else
|
{
|
try_count = 0;
|
|
tx_len_tmp = get_apdu_ptr_and_len(apdu_count, 1, &tx_ptr);
|
if (tx_ptr)
|
{
|
ranging_poll_msg_set(tx_len_tmp, tx_ptr);
|
}
|
}
|
}
|
else
|
{
|
LOG_INFO(TRACE_MODULE_SE, "APDU COMPLETE BUT ERR %d %d, resp_len = %d\r\n", apdu_count, try_count, rx_len_tmp);
|
try_count++;
|
}
|
}
|
|
if (try_count > 3)
|
{
|
try_count = 0;
|
apdu_count = 0;
|
|
tx_len_tmp = get_apdu_ptr_and_len(apdu_count, 1, &tx_ptr);
|
if (tx_ptr)
|
{
|
ranging_poll_msg_set(tx_len_tmp, tx_ptr);
|
}
|
}
|
}
|
}
|
|
#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
|
#pragma clang diagnostic push
|
#pragma clang diagnostic ignored "-Wcast-qual"
|
#endif
|
void slave_process_apdu(const uint8_t *ptr, uint16_t rx_len_tmp, uint8_t ack)
|
{
|
uint8_t count = 0;
|
uint32_t resp_addr = 0;
|
uint16_t resp_len = 0;
|
|
#if TRANSACTION_SIM_EN == 0
|
if (ack == 2)
|
{
|
uint16_t status = receive_only_resp_from_t1(&resp_addr, &resp_len);
|
if (status != ESESTATUS_NO_DATA_TO_RECEIVE)
|
{
|
wait_rx_from_se = 0;
|
|
if (resp_len != 0)
|
{
|
ranging_response_msg_set(resp_len, (uint8_t *)resp_addr);
|
}
|
|
check_apdu_ptr_and_len((uint8_t *)resp_addr, resp_len, 0);
|
}
|
}
|
else
|
#endif
|
if (ack == 1)
|
{
|
if (slave_apdu_resp_send_status == 1)
|
{
|
ranging_response_msg_set(0, NULL);
|
slave_apdu_resp_send_status = 0;
|
}
|
}
|
else
|
{
|
count = check_apdu_ptr_and_len(ptr, rx_len_tmp, 1);
|
if ((count != 255) && (wait_rx_from_se == 0))
|
{
|
#if TRANSACTION_SIM_EN == 1
|
uint8_t *tx_ptr = NULL;
|
resp_len = get_apdu_ptr_and_len(count, 0, &tx_ptr);
|
resp_addr = (uint32_t)tx_ptr;
|
ranging_response_msg_set(resp_len, (uint8_t *)resp_addr);
|
#else
|
transmit_only_apdu_to_t1((uint8_t *)ptr, rx_len_tmp);
|
wait_rx_from_se = 1;
|
#endif
|
}
|
}
|
}
|
#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
|
#pragma clang diagnostic pop
|
#endif
|
|
//*****************************************************************************
|
//
|
// WSF buffer pools.
|
//
|
//*****************************************************************************
|
#define WSF_BUF_POOLS 5
|
|
// Default pool descriptor.
|
static wsfBufPoolDesc_t poolDescriptors[WSF_BUF_POOLS] = {
|
{32, 26}, {64, 24}, {128, 4}, {256 + 32, 4}, {1024 + 32, 2},
|
};
|
|
static void sleep_timer_callback(void *dev, uint32_t time)
|
{
|
// LOG_INFO(TRACE_MODULE_APP, "Wake up by sleep timer %d\r\n", time);
|
}
|
|
//
|
// Button Handlers
|
//
|
static void GPIO_UserHandler(enum IO_PIN_T pin)
|
{
|
// LOG_INFO(TRACE_MODULE_APP, "GPIO Interrupt happen\r\n");
|
|
if (pin == BOARD_SW_1)
|
{
|
app_button_event_set(APP_BUTTON1_EVT);
|
}
|
}
|
|
static void app_ranging_report_callback(void *report)
|
{
|
struct RANGE_DATA_T *rpt = (struct RANGE_DATA_T *)report;
|
// LOG_INFO(TRACE_MODULE_APP, "Report measurements number %d\r\n", rpt->measurements_num);
|
for (uint8_t i = 0; i < RESPONDER_NUM; i++)
|
{
|
if ((rpt->mac_addr_mode == MAC_ADDR_SHORT) && (rpt->measurements[i].status == STATUS_OK))
|
{
|
LOG_INFO(TRACE_MODULE_APP, "Peer %X, Distance %ucm\r\n", READ_SHORT(rpt->measurements[i].mac_addr), rpt->measurements[i].distance);
|
}
|
}
|
}
|
|
void app_process_handle(uint8_t msg_id, const void *param)
|
{
|
uint8_t *ptr = NULL;
|
uint16_t tx_len_tmp;
|
|
switch (msg_id)
|
{
|
case APP_DEBOUNCE_TIMER1_MSG:
|
{
|
if (gpio_pin_get_val(BOARD_SW_1))
|
{
|
return;
|
}
|
|
LOG_INFO(TRACE_MODULE_APP, "DEBOUNCE TIMER1\r\n");
|
|
if (ranging_on_flag)
|
{
|
uwbapi_session_stop(session_id);
|
ranging_on_flag = 0;
|
}
|
else
|
{
|
uwbapi_session_start(session_id, app_ranging_report_callback);
|
ranging_on_flag = 1;
|
}
|
}
|
break;
|
|
case APP_TEST_TIMER1_MSG:
|
{
|
LOG_INFO(TRACE_MODULE_APP, "TEST TIMER1\r\n");
|
|
if (se_transaction_start == 0)
|
{
|
se_transaction_start = 1;
|
|
try_count = 0;
|
apdu_count = 0;
|
|
if (se_master)
|
{
|
tx_len_tmp = get_apdu_ptr_and_len(apdu_count, 1, &ptr);
|
if (ptr)
|
{
|
ranging_poll_msg_set(tx_len_tmp, ptr);
|
}
|
}
|
}
|
}
|
break;
|
|
default:
|
break;
|
}
|
}
|
|
static void board_init(void)
|
{
|
// Clock configuration
|
board_clock_run();
|
// Pin configuration
|
board_pins_config();
|
// Trace configuration
|
board_debug_console_open(TRACE_PORT_UART0);
|
// Reset reason
|
reset_cause_get();
|
reset_cause_clear();
|
|
// Load calibration parameters from NVM
|
uint32_t internal_flash = (REG_READ(0x40000018) >> 17) & 0x1;
|
uint32_t external_flash = (REG_READ(0x40010030) >> 28) & 0x3;
|
if (internal_flash || external_flash == 1)
|
{
|
WsfNvmInit();
|
board_calibration_params_load();
|
flash_close(FLASH_ID0);
|
}
|
else
|
{
|
board_calibration_params_default();
|
}
|
|
// Chip calibration
|
calib_chip();
|
|
// Configure IO_02 for role selection
|
gpio_open();
|
gpio_pin_set_dir(IO_PIN_2, GPIO_DIR_IN, 0);
|
io_pull_set(IO_PIN_2, IO_PULL_UP, IO_PULL_UP_LEVEL1);
|
|
board_led_init();
|
board_button_init(GPIO_UserHandler);
|
board_configure();
|
|
// SPI CS
|
gpio_pin_set_dir(IO_PIN_14, GPIO_DIR_OUT, 1);
|
}
|
|
int main(void)
|
{
|
// Initialize MCU system
|
board_init();
|
|
// Disable watchdog timer
|
wdt_close(WDT_ID0);
|
LOG_INFO(TRACE_MODULE_APP, "Ranging custom SE example\r\n");
|
|
// Platform init for WSF
|
PalSysInit();
|
|
// Initialize os
|
|
//
|
// Set up timers for the WSF scheduler.
|
//
|
WsfOsInit();
|
WsfTimerInit();
|
sys_tick_callback_set(WsfTimerUpdateTicks);
|
|
//
|
// Initialize a buffer pool for WSF dynamic memory needs.
|
//
|
uint32_t wsfBufMemLen = WsfBufInit(WSF_BUF_POOLS, poolDescriptors);
|
|
if (wsfBufMemLen > FREE_MEM_SIZE)
|
{
|
LOG_INFO(TRACE_MODULE_APP, "Memory pool is not enough %d\r\n", wsfBufMemLen - FREE_MEM_SIZE);
|
}
|
|
//
|
// Create app task
|
//
|
wsfHandlerId_t handlerId = WsfOsSetNextHandler(app_handler);
|
app_init(handlerId);
|
|
//
|
// Create ranging task
|
//
|
handlerId = WsfOsSetNextHandler(ranging_handler);
|
ranging_init(handlerId);
|
|
uwb_open();
|
|
// set advanced parameters
|
struct PHY_ADV_CONFIG_T adv_config =
|
{
|
.thres_fap_detect = 40,
|
.nth_scale_factor = 4,
|
.ranging_performance_mode = 0,
|
#if RX_ANT_PORTS_NUM == 4
|
.skip_weakest_port_en = 1,
|
#else
|
.skip_weakest_port_en = 0,
|
#endif
|
};
|
phy_adv_params_configure(&adv_config);
|
|
// which RX ports will be used for AoA/PDoA
|
phy_rx_ant_mode_set(RX_ANT_PORTS_COMBINATION);
|
|
uwbs_init();
|
uwb_app_config.ranging_flow_mode = (uint8_t)(RANGING_FLOW_CUSTOM);
|
uwb_app_config.filter_en = (uint8_t)(FILTER_EN);
|
uwb_app_config.session_param.tx_power_level = board_param.tx_power_fcc[CALIB_CH(uwb_app_config.ppdu_params.ch_num)];
|
uwb_app_config.ppdu_params.rx_ant_id = (uint8_t)(RX_MAIN_ANT_PORT);
|
|
// Initialize ranging session
|
if (board_param.flag & (1 << BOARD_RANGING_SESSION_ID))
|
{
|
session_id = board_param.ranging_session_id;
|
}
|
else
|
{
|
session_id = USER_RANGING_SESSION;
|
}
|
uwbapi_session_init(session_id, SESSION_TYPE_RANGING);
|
|
// Initialize ranging parameters
|
struct APP_CFG_PARAM_T param = {0};
|
param.ch_num = UWB_CH_NUM;
|
param.prf_mode = UWB_MEAN_PRF;
|
param.preamble_code_index = UWB_PREAMBLE_CODE_IDX;
|
param.preamble_duration = UWB_PREAMBLE_DURATION;
|
param.sfd_id = UWB_SFD_ID;
|
param.psdu_data_rate = UWB_PSDU_DATA_RATE;
|
param.sts_segment_num = UWB_STS_SEGMENT_NUM;
|
param.sts_segment_len = UWB_STS_SEGMENT_LEN;
|
param.aoa_result_req = 0;
|
param.rframe_config = UWB_RFRAME_TYPE;
|
param.sts_config = STS_STATIC;
|
param.ranging_round_usage = DS_TWR_DEFERRED;
|
param.mac_address_mode = ARRD_SHORT_USE_SHORT;
|
param.controlees_num = RESPONDER_NUM;
|
param.multi_node_mode = param.controlees_num > 1 ? ONE_TO_MANY : UNICAST;
|
param.result_report_config = 0x0F;
|
param.ranging_round_control = 0x3;
|
|
// Load local & peer short address
|
uint16_t local_addr = 0;
|
uint16_t peer_addr = 0;
|
|
if (gpio_pin_get_val(IO_PIN_2) == 0)
|
{
|
param.device_role = DEV_ROLE_INITIATOR;
|
local_addr = INITIATOR_ADDR;
|
peer_addr = RESPONDER_ADDR;
|
se_master = 1;
|
}
|
else
|
{
|
param.device_role = DEV_ROLE_RESPONDER;
|
local_addr = RESPONDER_ADDR;
|
peer_addr = INITIATOR_ADDR;
|
|
se_master = 0;
|
}
|
|
// apdu over UWB test
|
apdu_init(se_master);
|
|
param.device_type = param.device_role == DEV_ROLE_INITIATOR ? DEV_TYPE_CONTROLLER : DEV_TYPE_CONTROLEE;
|
param.src_dev_mac_addr[0] = local_addr & 0xff;
|
param.src_dev_mac_addr[1] = (local_addr >> 8) & 0xff;
|
param.dst_dev_mac_addr[0] = peer_addr & 0xff;
|
param.dst_dev_mac_addr[1] = (peer_addr >> 8) & 0xff;
|
param.slots_per_round = SLOT_NUM_PER_ROUND(param.controlees_num);
|
param.slot_duration = UWB_RANGING_SLOT_DURATION;
|
param.ranging_interval = UWB_RANGING_INTERVAL;
|
|
// Configure ranging parameters
|
uwbapi_session_set_app_config(session_id, ¶m);
|
|
// Start ranging
|
uwbapi_session_start(session_id, app_ranging_report_callback);
|
ranging_on_flag = 1;
|
|
// Initialize low power mode
|
power_init();
|
|
#if LOW_POWER_EN
|
power_mode_request(POWER_UNIT_USER, POWER_MODE_DEEP_POWER_DOWN);
|
#else
|
power_mode_request(POWER_UNIT_USER, POWER_MODE_SLEEP);
|
#endif
|
|
// Enable sleep timer
|
sleep_timer_open(true, SLEEP_TIMER_MODE_ONESHOT, sleep_timer_callback);
|
|
while (1)
|
{
|
wsfOsDispatcher();
|
power_manage();
|
|
#if TRANSACTION_SIM_EN == 0
|
if ((se_master == 0) && (wait_rx_from_se == 1))
|
{
|
sys_timer_delay_ms(1);
|
slave_process_apdu(NULL, 0, 2);
|
}
|
#endif
|
}
|
}
|
|
void app_restore_from_power_down(void)
|
{
|
}
|