/**
 @file bsp_template.h
 @brief  This files define external interface and events of bsp_template task.

 ## changelog ##
 ______
 Data        | Vesion   | Change Description
 ------------|----------|-------------------------------------
 20240327    | 1.0      | First release
 */

/********************************************************************************************
 Multi-Inlcude-Prevent Section
 ********************************************************************************************/
#ifndef __create_task_h__
#define __create_task_h__
/********************************************************************************************
 Debug Switch Definition Section
 ********************************************************************************************/

/********************************************************************************************
 Include File Section
 ********************************************************************************************/
#include <stdint.h>
#include "Platform_Types.h"
// #include "projdefs.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
/********************************************************************************************
 Macro Definition Section
 ********************************************************************************************/
#define OS_TRUE                                      ( ( base_t ) 1 )
#define OS_FALSE                                     ( ( base_t ) 0 )

#define OS_PASS                                      ( ( base_t ) 1 )
#define OS_FAIL                                      ( ( base_t ) 0 )
#define OS_COULD_NOT_ALLOCATE_REQUIRED_MEMORY        ( ( base_t )-1 )

#define SEQ_DEMO    (0U)

#define ST_MAX       32
/**
* @brief        Define transfer size
*/
#define TRANSFER_SIZE 2U

/**
* @brief        Define I2C instances
*/
/* Define I2C instance */
#define I2C_SLAVE    1U
#define I2C_MASTER   0U
#define data_lenth   4U


#define FLS_MASTER_ID                0U
#define FLS_BUF_SIZE                 80U
#define FLS_SECTOR_ADDR              0x10000000U
#define FLS_SECTOR_TEST              C40_DATA_ARRAY_0_BLOCK_2_S000


#define MASTER_EXTERNAL_DEVICE           (Lpspi_Ip_DeviceAttributes_SpiExternalDevice_0_Instance_2)
#define UART_LPUART_INTERNAL_CHANNEL  2
#define main_TASK_PRIORITY                ( tskIDLE_PRIORITY + 2 )

/*task stack section*/
#ifndef SAF_NULL
#define SAF_NULL    ( ( void * ) 0 )
#endif
#define TASK_STACK_SECTION   		__attribute( ( section( "task_stack_data" ) ) )
#define TASK_STACK					TASK_STACK_SECTION
#define MPU_UNPRIVILEGED_TASK                           ( 0U )
#define MPU_PRIVILEGED_TASK                             ( 1U )

// #define QM_DATA_SECTION   		__attribute( ( section( "QM_data" ) ) )
#define QM_DATA_SECTION
#define QM_DATA					QM_DATA_SECTION
/*os data section*/
// #define OS_DATA_SECTION   		__attribute( ( section( "OS_data" ) ) )/* 这个宏 导致ram数据到hex文件中 */
#define OS_DATA_SECTION
#define OS_DATA					OS_DATA_SECTION
#define OS_QUEUE_OVERHEAD_BYTES                         ( 140U )
#define SAFETYOS_MAX_DELAY                              ( ( tick_t ) 0xFFFFFFFFU )
#define SAFERTOSAPI_WORD_ALIGNMENT                      ( 4U )
#define SAFERTOSAPI_STACK_ALIGNMENT                     ( 8U )
#define MPU_REGION_ALIGNMENT             				( 64U )
#define MAX_TASK_NUM		50
#define pvOsMutexHandleType    uint8_t
#define pvOsSemaphoreHandle    uint8_t
#define int8_t                 uint8_t
#define xOsEventGroupType      EventBits_t
#define pvOsEventGroupHandleType   EventGroupHandle_t
#define pvOsQueueHandle            QueueHandle_t


/* mutex definition regions */
#define MUTEX_BUFFER(a) x##a##MutexBuffer
#define MUTEX_HANDLE(a) x##a##MutexHandle
#define MUTEX_CREATE(a) \
    int8_t MUTEX_BUFFER(a)[ OS_QUEUE_OVERHEAD_BYTES ] __attribute__( ( aligned ( SAFERTOSAPI_WORD_ALIGNMENT ) )) = { 0 }; \
    OS_DATA  pvOsMutexHandleType MUTEX_HANDLE(a) = SAF_NULL;
#define MUTEX_PARAM(a) \
    { \
        (int8_t *)&MUTEX_BUFFER(a), \
        (pvOsMutexHandleType *)&MUTEX_HANDLE(a) \
    }
#define DEVENT_MUTEX_HANDLE(a) pvOsMutexHandleType MUTEX_HANDLE(a)
#define DECLARATION_MUTEX(a)    extern DEVENT_MUTEX_HANDLE(a)
#define MUTEX_TAKE(a,b) xQueueTakeMutexRecursive(MUTEX_HANDLE(a),b)
#define MUTEX_GIVE(a) xQueueGiveMutexRecursive(MUTEX_HANDLE(a))

/* semaphore definition regions */
#define SEMAPHORE_BUFFER(a) x##a##SemaphoreBuffer
#define SEMAPHORE_HANDLE(a) x##a##SemaphoreHandle
#define SEMAPHORE_CREATE(a) \
     int8_t SEMAPHORE_BUFFER(a)[ OS_QUEUE_OVERHEAD_BYTES ] __attribute__( ( aligned ( SAFERTOSAPI_WORD_ALIGNMENT ) )) = { 0 }; \
     OS_DATA pvOsSemaphoreHandle SEMAPHORE_HANDLE(a) = SAF_NULL;
#define SEMAPHORE_PARAM(a) \
    { \
        (int8_t *)&SEMAPHORE_BUFFER(a), \
        (pvOsSemaphoreHandle *)&SEMAPHORE_HANDLE(a) \
    }
#define DEVENT_SEMAPHORE_HANDLE(a)         extern pvOsSemaphoreHandle SEMAPHORE_HANDLE(a)
#define DECLARATION_SEMAPHORE(a)    DEVENT_SEMAPHORE_HANDLE(a)
#define SEMAPHORE_TAKE(a,b)  xSemaphoreTake(SEMAPHORE_HANDLE(a),b)
#define SEMAPHORE_GIVE(a)    xSemaphoreGive(SEMAPHORE_HANDLE(a))
#define SEMAPHORE_TAKEISR(a,b)  xSemaphoreTakeFromISR(SEMAPHORE_HANDLE(a),b)
#define SEMAPHORE_GIVEISR(a)    xSemaphoreGiveFromISR(SEMAPHORE_HANDLE(a))

/* event group definition regions */
#define EVENT_GROUP(a)  x##a##EventGroup
#define EVENT_GROUP_HANDLE(a)  x##a##EventGroupHandle

#define DEVENT_GROUP(a) \
    xOsEventGroupType EVENT_GROUP(a)

#define DEVENT_GROUP_HANDLE(a) \
    OS_DATA pvOsEventGroupHandleType EVENT_GROUP_HANDLE(a)

#define TASK_EVENT_GROUP(a) \
    DEVENT_GROUP( a ); \
    DEVENT_GROUP_HANDLE( a );

#define MEM_TASK_EVENT_GROUP(a) \
    { \
        &EVENT_GROUP(a), \
        &EVENT_GROUP_HANDLE(a) \
    }

#define DECLARATION_EGROUP(a)    extern DEVENT_GROUP_HANDLE(a);

/* queue definition regions */
#define QUEUE_NAME(a)       #a
#define QUEUE_HANDLE(a)     pvOs##a##queueHandle
#define QUEUE_BUFFER(a)     pvOs##a##queueBuffer
#define QUEUE_BUFFERX(a)    pvConst##a##queueBx
#define QUEUE_DEF(a,x) \
    CMsg_t pvOs##a##queueBuffer[x]; \
    pvOsQueueHandle QUEUE_HANDLE(a);
#define DQUEUE_HANDLE(a)    pvOsQueueHandle QUEUE_HANDLE(a);
#define QUEUE_CREATE(a,x,y) \
    { \
        &QUEUE_BUFFER(a), \
        x * y + OS_QUEUE_OVERHEAD_BYTES, \
        x, \
        y, \
        &QUEUE_HANDLE(a) \
    }


/* task definition regions */
#define TASK_FUNC(a)		psv##a##Task
#define TASK_STR(a)		(const char *)#a
#define TASK_TCB(a)		x##a##TCB
#define TASK_STK(a)		ac##a##Stack
#define TASK_STK_SZ_1K		(0x400)
#define TASK_STK_SZ(a)		sizeof(TASK_STK( a  ))
// #define TASK_PARAM(a)		pv##a##Param
#define TASK_PARAM(a)		SAF_NULL
#define TASK_PRI(a)		a##TaskPriority

#define TASK_OBJ(a)		pv##a##Obj
#define TASK_USE_FPU(a)	OS_FALSE
#define TASK_HANDLE(a)  pv##a##Handle
#define TASK_PRIVILEGED(a)	MPU_PRIVILEGED_TASK
#define TASK_DEFAULT_MPU_REGION	{ SAF_NULL, 0U, 0U, 0U }

// #define TASK_PRIVILEGED(x) x

#define TASK_CREATE(a, b) \
    TaskHandle_t TASK_HANDLE(a); \
    void TASK_FUNC( a )( void *pvParameters ); \
    const int TASK_PRI( a )	=	b; \
    int TASK_OBJ( a ); \
    OS_DATA pvOsQueueHandle QUEUE_HANDLE(a); \
    DEVENT_GROUP( a )= { 0 }; \
    DEVENT_GROUP_HANDLE( a ); \
    SemaphoreHandle_t SEMAPHORE_HANDLE(a);

#define CTASK_PARAMS(a,b,c)	\
	{ \
        { \
            (unsigned_base_t)(b), \
            (unsigned_base_t)sizeof(CMsg_t), \
            (pvOsQueueHandle)&QUEUE_HANDLE(a) \
        }, \
        { \
            (pfvTASK_CODE)&TASK_FUNC(a), \
            (const char *)TASK_STR(a), \
            (configSTACK_DEPTH_TYPE)(c), \
            (void *)TASK_PARAM(a), \
            (unsigned_base_t)TASK_PRI(a), \
            (TaskHandle_t*)&TASK_HANDLE(a) \
        }, \
            (SemaphoreHandle_t *)&SEMAPHORE_HANDLE(a) \
	}


#define MAIL_SIZE    10

// #define GET_TSK_NAME (((xOsTCB*)(xSafetyOSTaskGetCurrentTaskHandle()))->pcNameOfTask)
#define MAX_TASK_STACK_SIZE     (5*1024)
#define MAX_QUEUE_SIZE          (512 + OS_QUEUE_OVERHEAD_BYTES)


/*==================================================================================================
 *                                 QUEUE DECLARATION
 *================================================================================================*/
#define DECLARATION_QUEUE(a)    extern DQUEUE_HANDLE(a)
#define RECV_MSG(a,b,c)         xQueueReceive(QUEUE_HANDLE(a),b,c)
#define RECV_ISR_MSG(a,b,c)     xQueueReceiveFromISR(QUEUE_HANDLE(a),b,c)
#define SEND_MSG(a,b,c)         xQueueSend(QUEUE_HANDLE(a),b,c)
#define SEND_ISR_MSG(a,b,c)     xQueueSendFromISR(QUEUE_HANDLE(a),b,c)
/********************************************************************************************
 Structure Type Declaration Section
 ********************************************************************************************/
/* task type */
typedef void ( *pfvTASK_CODE )( void *pvParameters );
// typedef void *pvOsEventGroupHandleType;
typedef unsigned long           unsigned_base_t;
typedef signed long             base_t;
typedef int8_t                msg_buffer_t;
typedef unsigned long           tick_t;

typedef union
{
    unsigned_base_t uldata[data_lenth];
    struct
    {
        unsigned_base_t uld0;
        unsigned_base_t uld1;
        unsigned_base_t* ulBuffer_p;
        unsigned_base_t ullen;
    }data;
}  xMsgData_t;

typedef struct xMsg
{
    unsigned_base_t dst;
    unsigned_base_t src;
    unsigned_base_t code;
    xMsgData_t data;
} CMsg_t;

typedef struct xCREATE_TASK_PARAMETERS
{
    TaskFunction_t pvTaskCode;
    const char * pcName; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
    configSTACK_DEPTH_TYPE usStackDepth;
    void * pvParameters;
    unsigned_base_t uxPriority;
    TaskHandle_t * pxCreatedTask; /* return value */
} CTaskParameters_t;

typedef struct xQueueCreate
{
    unsigned_base_t uxQueueLength; 
    unsigned_base_t uxItemSize;
    QueueHandle_t pvQueueHandle; /* return value */
}CQueueParameters_t;

typedef struct xOsCreate
{
    CQueueParameters_t queue;
    CTaskParameters_t tsk;
    SemaphoreHandle_t *semphrHandle; /* return value */

}xOsCreateType;

typedef struct xOsMutexCreate
{
    int8_t *buffer;
    pvOsMutexHandleType *handle;
}xOsMutexCreateType;

typedef struct xOsSemaphoreCreate
{
    int8_t *buffer;
    pvOsSemaphoreHandle *handle;
}xOsSemaphoreCreateType;
/********************************************************************************************
 Prototype Declaration Section
 ********************************************************************************************/

extern base_t xAllTaskCreate( void );
extern int iMemCpyStr(char *d, const char *s, int len);
extern void vAllTaskCreateProcess( void );
#endif /* _BSPDEMO_ */
