Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mcux: align imu adapter with SDK #513

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mcux/hal_nxp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ endif()
if(CONFIG_NXP_RF_IMU)
if(CONFIG_SOC_SERIES_RW6XX)
list(APPEND CMAKE_MODULE_PATH
${CMAKE_CURRENT_LIST_DIR}/mcux-sdk/components/rpmsg
${CMAKE_CURRENT_LIST_DIR}/mcux-sdk/components/imu_adapter
${CMAKE_CURRENT_LIST_DIR}/mcux-sdk/drivers/imu
${CMAKE_CURRENT_LIST_DIR}/mcux-sdk/drivers/gdma
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ include_guard(GLOBAL)
message("component_wireless_imu_adapter component is included.")

target_sources(${MCUX_SDK_PROJECT_NAME} PRIVATE
${CMAKE_CURRENT_LIST_DIR}/fsl_adapter_rfimu.c
${CMAKE_CURRENT_LIST_DIR}/fsl_adapter_imu.c
)

target_include_directories(${MCUX_SDK_PROJECT_NAME} PUBLIC
${CMAKE_CURRENT_LIST_DIR}/.
)

target_compile_definitions(${MCUX_SDK_PROJECT_NAME} PRIVATE
FSL_OSA_TASK_ENABLE=1
)

#OR Logic component
if(CONFIG_USE_component_osa_free_rtos_RW612)
include(component_osa_free_rtos_RW612)
Expand Down
123 changes: 6 additions & 117 deletions mcux/mcux-sdk/components/imu_adapter/fsl_adapter_imu.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
* Copyright 2021 NXP
* All rights reserved.
* Copyright 2021-2025 NXP
*
*
* SPDX-License-Identifier: BSD-3-Clause
Expand Down Expand Up @@ -61,9 +60,6 @@
/*******************************************************************************
* Declaration
******************************************************************************/
#ifdef __ZEPHYR__
const uint8_t gUseRtos_c = 1;
#endif

/*! IMU handle instance for each IMU link */
static hal_imu_handle_t imuHandleCh[kIMU_LinkMax];
Expand Down Expand Up @@ -91,20 +87,16 @@ static uint8_t imu_task_flag = 0;
#ifndef CPU2
/*! Sleep flag address between CPU1 and CPU3 or CPU2 and CPU3 */
#define IMU_SLEEP_FLAG13 0x4138248C
#define IMU_SLEEP_FLAG23 0x443CFE88
#define IMU_SLEEP_FLAG23 0x443C2588
#define IMU_SLEEP_FLAG_ADDR(imuLinkId) \
(((imuLinkId) == kIMU_LinkCpu1Cpu3) ? ((uint32_t *)IMU_SLEEP_FLAG13) : ((uint32_t *)IMU_SLEEP_FLAG23))
#endif

/*! wait imu task lock
*/
#if (defined(USE_RTOS) && (USE_RTOS > 0U))
#ifdef __ZEPHYR__
struct k_mutex imu_task_lock;
#else
static OSA_MUTEX_HANDLE_DEFINE(imu_task_lock);
#endif
#endif
/*******************************************************************************
* Code
******************************************************************************/
Expand Down Expand Up @@ -132,23 +124,6 @@ imumcTxBuf13[IMUMC_TXQ13_BUFSIZE][IMUMC_TXQ13_BUFLENGTH];
__attribute__((section(".smu_cpu32_txq"))) static volatile uint8_t __attribute__((aligned(4)))
imumcTxBuf23[IMUMC_TXQ23_BUFSIZE][IMUMC_TXQ23_BUFLENGTH];

/* seperate CPU2 to CPU3 and CPU1 to CPU3 IMU RX tasks */
#ifdef __ZEPHYR__
static void HAL_ImuMainCpu13(void *argv1, void *argv2, void *argv3);
static void HAL_ImuMainCpu23(void *argv1, void *argv2, void *argv3);

K_THREAD_STACK_DEFINE(ImuTaskStackCpu13, IMU_TASK_STACK_SIZE);
K_THREAD_STACK_DEFINE(ImuTaskStackCpu23, IMU_TASK_STACK_SIZE);

k_tid_t ImuTaskCpu13;
k_tid_t ImuTaskCpu23;

K_EVENT_DEFINE(ImuQ13FlagsRef);
K_EVENT_DEFINE(ImuQ23FlagsRef);

struct k_thread ImuTaskHandleCpu13;
struct k_thread ImuTaskHandleCpu23;
#else
static void HAL_ImuMainCpu13(void *argv);
static void HAL_ImuMainCpu23(void *argv);

Expand All @@ -158,14 +133,9 @@ static OSA_TASK_DEFINE(HAL_ImuMainCpu13, IMU_TASK_PRIORITY, 1, IMU_TASK_STACK_SI
static OSA_TASK_DEFINE(HAL_ImuMainCpu23, IMU_TASK_PRIORITY, 1, IMU_TASK_STACK_SIZE, 0);
OSA_EVENT_HANDLE_DEFINE(ImuQ13FlagsRef);
OSA_EVENT_HANDLE_DEFINE(ImuQ23FlagsRef);
#endif /* __ZEPHYR__ */
#endif

#ifdef __ZEPHYR__
struct k_event imumcQFlagsRef;
#else
OSA_EVENT_HANDLE_DEFINE(imumcQFlagsRef);
#endif /* __ZEPHYR__ */

#if defined(IMU_GDMA_ENABLE) && (IMU_GDMA_ENABLE == 1)
static gdma_handle_t gdmaHandle;
Expand Down Expand Up @@ -1160,12 +1130,8 @@ static hal_imumc_status_t HAL_ImuCtrlHandler(hal_imu_handle_t *imuHandle, IMU_Ms
/* Confirm the local endpoint is ready too */
imumcStatus = HAL_ImuSendImumcEptQuiryRsp(imuHandle, pImuMsg->PayloadPtr[0], TRUE);
/* Make sure to unblock the task calling HAL_ImumcInit */
#ifdef __ZEPHYR__
k_event_set(&imumcQFlagsRef, IMUMC_EVENT_ENDPOINT_QUERY_RSP << imuHandle->imuLink);
#else
(void)OSA_EventSet((osa_event_handle_t)imumcQFlagsRef,
IMUMC_EVENT_ENDPOINT_QUERY_RSP << imuHandle->imuLink);
#endif
}
else
{
Expand Down Expand Up @@ -1197,12 +1163,8 @@ static hal_imumc_status_t HAL_ImuCtrlHandler(hal_imu_handle_t *imuHandle, IMU_Ms
if (eptQuiryRspAck != 0U)
{
/* Make sure to unblock the task calling HAL_ImumcInit */
#ifdef __ZEPHYR__
k_event_set(&imumcQFlagsRef, IMUMC_EVENT_ENDPOINT_QUERY_RSP << imuHandle->imuLink);
#else
(void)OSA_EventSet((osa_event_handle_t)imumcQFlagsRef,
IMUMC_EVENT_ENDPOINT_QUERY_RSP << imuHandle->imuLink);
#endif
}
break;
case IMU_MSG_CONTROL_IMUMC_BUF_FREE:
Expand Down Expand Up @@ -1387,13 +1349,7 @@ static void HAL_ImuMain(void *argv)
}
#else

static void HAL_ImuMainCpu13(void *argv
#ifdef __ZEPHYR__
,
void *argv2,
void *argv3
#endif
)
static void HAL_ImuMainCpu13(void *argv)
{
uint32_t Events = 0;

Expand All @@ -1403,13 +1359,7 @@ static void HAL_ImuMainCpu13(void *argv
(void)HAL_ImuPutTaskLock();
#endif

#ifdef __ZEPHYR__
Events = k_event_wait(&ImuQ13FlagsRef, IMU_EVENT_TRIGGERS, 0, K_FOREVER);
k_event_clear(&ImuQ13FlagsRef, (1U << kIMU_LinkCpu1Cpu3));
#else
(void)OSA_EventWait((osa_event_handle_t)ImuQ13FlagsRef, IMU_EVENT_TRIGGERS, 0, osaWaitForever_c, &Events);
#endif

if (Events == 0U)
{
if (gUseRtos_c == 0U)
Expand All @@ -1436,13 +1386,7 @@ static void HAL_ImuMainCpu13(void *argv
}
}

static void HAL_ImuMainCpu23(void *argv
#ifdef __ZEPHYR__
,
void *argv2,
void *argv3
#endif
)
static void HAL_ImuMainCpu23(void *argv)
{
uint32_t Events = 0;

Expand All @@ -1452,12 +1396,7 @@ static void HAL_ImuMainCpu23(void *argv
HAL_ImuPutTaskLock();
#endif

#ifdef __ZEPHYR__
Events = k_event_wait(&ImuQ23FlagsRef, IMU_EVENT_TRIGGERS, 0, K_FOREVER);
k_event_clear(&ImuQ23FlagsRef, (1U << kIMU_LinkCpu2Cpu3));
#else
(void)OSA_EventWait((osa_event_handle_t)ImuQ23FlagsRef, IMU_EVENT_TRIGGERS, 0, osaWaitForever_c, &Events);
#endif

if (Events == 0U)
{
Expand Down Expand Up @@ -1501,26 +1440,14 @@ static void HAL_ImuTaskInit(uint8_t link)
#else
if (link == kIMU_LinkCpu1Cpu3 && (imu_task_flag & (1U << link)) == 0)
{
#ifdef __ZEPHYR__
ImuTaskCpu13 = k_thread_create(&ImuTaskHandleCpu13, ImuTaskStackCpu13, K_THREAD_STACK_SIZEOF(ImuTaskStackCpu13),
HAL_ImuMainCpu13, NULL, NULL, NULL, IMU_TASK_PRIORITY, 0, K_NO_WAIT);
k_thread_name_set(ImuTaskCpu13, "HAL_ImuMainCpu13");
#else
(void)OSA_TaskCreate((osa_task_handle_t)ImuTaskHandleCpu13, OSA_TASK(HAL_ImuMainCpu13), NULL);
(void)OSA_EventCreate((osa_event_handle_t)ImuQ13FlagsRef, 1U);
#endif
imu_task_flag |= (1U << link);
}
else if (link == kIMU_LinkCpu2Cpu3 && (imu_task_flag & (1U << link)) == 0)
{
#ifdef __ZEPHYR__
ImuTaskCpu23 = k_thread_create(&ImuTaskHandleCpu23, ImuTaskStackCpu23, K_THREAD_STACK_SIZEOF(ImuTaskStackCpu23),
HAL_ImuMainCpu23, NULL, NULL, NULL, IMU_TASK_PRIORITY, 0, K_NO_WAIT);
k_thread_name_set(ImuTaskCpu23, "HAL_ImuMainCpu23");
#else
(void)OSA_TaskCreate((osa_task_handle_t)ImuTaskHandleCpu23, OSA_TASK(HAL_ImuMainCpu23), NULL);
(void)OSA_EventCreate((osa_event_handle_t)ImuQ23FlagsRef, 1U);
#endif
imu_task_flag |= (1U << link);
}
else
Expand All @@ -1544,19 +1471,15 @@ static void HAL_ImuTaskDeinit(uint8_t link)
#else
if (link == kIMU_LinkCpu1Cpu3 && (imu_task_flag & (1U << link)) != 0)
{
#ifndef __ZEPHYR__
(void)OSA_EventDestroy((osa_event_handle_t)ImuQ13FlagsRef);
(void)OSA_TaskDestroy(ImuTaskHandleCpu13);
imu_task_flag &= ~(1U << link);
#endif
}
else if (link == kIMU_LinkCpu2Cpu3 && (imu_task_flag & (1U << link)) != 0)
{
#ifndef __ZEPHYR__
(void)OSA_EventDestroy((osa_event_handle_t)ImuQ23FlagsRef);
(void)OSA_TaskDestroy(ImuTaskHandleCpu23);
imu_task_flag &= ~(1U << link);
#endif
}
else
{
Expand Down Expand Up @@ -1720,11 +1643,7 @@ hal_imumc_status_t HAL_ImumcInit(hal_imumc_handle_t handle, hal_imumc_config_t *

if (imumc_init_flag == 0U)
{
#ifdef __ZEPHYR__
k_event_init(&imumcQFlagsRef);
#else
(void)OSA_EventCreate((osa_event_handle_t)imumcQFlagsRef, 1U);
#endif
imumc_init_flag = 1U;
}

Expand Down Expand Up @@ -1771,13 +1690,8 @@ hal_imumc_status_t HAL_ImumcInit(hal_imumc_handle_t handle, hal_imumc_config_t *
OSA_EXIT_CRITICAL();

#if defined(USE_RTOS) && (USE_RTOS == 1U)
#ifdef __ZEPHYR__
Events = k_event_wait(&imumcQFlagsRef, IMUMC_EVENT_ENDPOINT_QUERY_RSP << imuHandle->imuLink, 0, K_FOREVER);
k_event_clear(&imumcQFlagsRef, IMUMC_EVENT_ENDPOINT_QUERY_RSP << imuHandle->imuLink);
#else
(void)OSA_EventWait((osa_event_handle_t)imumcQFlagsRef, IMUMC_EVENT_ENDPOINT_QUERY_RSP << imuHandle->imuLink, 0,
osaWaitForever_c, &Events);
#endif
#else
while (TRUE) /* Wait for IMUMC_EVENT_ENDPOINT_QUERY_RSP */
{
Expand Down Expand Up @@ -1971,19 +1885,11 @@ void HAL_ImumcSetEvent(uint32_t Event)
#else
if ((Event & (1U << kIMU_LinkCpu1Cpu3)) != 0U)
{
#ifdef __ZEPHYR__
k_event_post(&ImuQ13FlagsRef, Event);
#else
(void)OSA_EventSet((osa_event_handle_t)ImuQ13FlagsRef, Event);
#endif
}
else
{
#ifdef __ZEPHYR__
k_event_post(&ImuQ23FlagsRef, Event);
#else
(void)OSA_EventSet((osa_event_handle_t)ImuQ23FlagsRef, Event);
#endif
}
#endif
}
Expand Down Expand Up @@ -2142,9 +2048,6 @@ bool HAL_ImuIsTxBufQueueEmpty(uint8_t imuLink)
#if (defined(USE_RTOS) && (USE_RTOS > 0U))
hal_imumc_status_t HAL_ImuCreateTaskLock(void)
{
#ifdef __ZEPHYR__
k_mutex_init(&imu_task_lock);
#else
osa_status_t status;

if ((*(uint32_t *)(osa_mutex_handle_t)imu_task_lock) == 0)
Expand All @@ -2155,28 +2058,21 @@ hal_imumc_status_t HAL_ImuCreateTaskLock(void)
return kStatus_HAL_ImumcError;
}
}
#endif

return kStatus_HAL_ImumcSuccess;
}

void HAL_ImuDeleteTaskLock(void)
{
#ifndef __ZEPHYR__
if ((*(uint32_t *)(osa_mutex_handle_t)imu_task_lock) != 0)
{
(void)OSA_MutexDestroy((osa_mutex_handle_t)imu_task_lock);
(*(uint32_t *)(osa_mutex_handle_t)imu_task_lock) = 0;
}
#endif
}

hal_imumc_status_t HAL_ImuGetTaskLock(void)
{
#ifdef __ZEPHYR__
int ret = k_mutex_lock(&imu_task_lock, K_FOREVER);

return (ret == 0) ? kStatus_HAL_ImumcSuccess : kStatus_HAL_ImumcError;
#else
osa_status_t status;

if ((*(uint32_t *)(osa_mutex_handle_t)imu_task_lock) == 0)
Expand All @@ -2186,17 +2082,10 @@ hal_imumc_status_t HAL_ImuGetTaskLock(void)

status = OSA_MutexLock((osa_mutex_handle_t)imu_task_lock, osaWaitForever_c);
return status == KOSA_StatusSuccess ? kStatus_HAL_ImumcSuccess : kStatus_HAL_ImumcError;
#endif
}

hal_imumc_status_t HAL_ImuPutTaskLock(void)
{
#ifdef __ZEPHYR__
int ret = k_mutex_unlock(&imu_task_lock);

return (ret == 0) ? kStatus_HAL_ImumcSuccess : kStatus_HAL_ImumcError;
#else

osa_status_t status;

if ((*(uint32_t *)(osa_mutex_handle_t)imu_task_lock) == 0)
Expand All @@ -2206,6 +2095,6 @@ hal_imumc_status_t HAL_ImuPutTaskLock(void)

status = OSA_MutexUnlock((osa_mutex_handle_t)imu_task_lock);
return status == KOSA_StatusSuccess ? kStatus_HAL_ImumcSuccess : kStatus_HAL_ImumcError;
#endif

}
#endif
10 changes: 7 additions & 3 deletions mcux/mcux-sdk/components/imu_adapter/fsl_adapter_imu.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
* Copyright 2021 NXP
* All rights reserved.
* Copyright 2021-2025 NXP
*
*
* SPDX-License-Identifier: BSD-3-Clause
Expand Down Expand Up @@ -283,11 +282,16 @@ hal_imumc_status_t HAL_ImumcExitLowpower(hal_imumc_handle_t handle);
#elif defined(CPU2)
#define IMU_TASK_PRIORITY (2U)
#else
#ifndef configMAX_PRIORITIES
#define IMU_TASK_PRIORITY (6U)
#else
#define IMU_TASK_PRIORITY \
(6U) /* makesure the calculated priority of HAL_ImuMainCpu13 task is the same with tcp/ip task(priority 2), \
(PRIORITY_RTOS_TO_OSA((configMAX_PRIORITIES - 3)))
/* makesure the calculated priority of HAL_ImuMainCpu13 task is the same with tcp/ip task(priority 2), \
otherwise the throughput of udp rx will be very low */
#endif
#endif
#endif

#ifndef IMU_TASK_STACK_SIZE
#define IMU_TASK_STACK_SIZE (2048U)
Expand Down
Loading