Files
2026-08-05 19:07:52 +08:00

152 lines
5.8 KiB
C

/*!
* \file hal_mcu.h
*
* \brief The header of hal_mcu.c
*
* \copyright Revised BSD License, see section \ref LICENSE.
*
* \code
*
* _ __ _ ________ _
* | |/ /(_)___ / ____/ /_ (_)___
* | // / __ \/ / / __ \/ / __ \
* / |/ / / / / /___/ / / / / /_/ /
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
* /_/
* (C) 2022-2025 XinChip
*
* \endcode
*
* \author ( XinChip ) Alex-J
*
* \author ( XinChip )
*/
#ifndef _HAL_MCU_H
#define _HAL_MCU_H
/* ------------------------------------------------------------------------------------------------
* Includes
* ------------------------------------------------------------------------------------------------
*/
#include "hal_defs.h"
#include "hal_types.h"
/* ------------------------------------------------------------------------------------------------
* Target Defines
* ------------------------------------------------------------------------------------------------
*/
// #define HAL_MCU_CC2530
/* ------------------------------------------------------------------------------------------------
* Compiler Abstraction
* ------------------------------------------------------------------------------------------------
*/
/* ---------------------- IAR Compiler ---------------------- */
#ifdef __IAR_SYSTEMS_ICC__
#include <ioCC2530.h>
#define HAL_COMPILER_IAR
#define HAL_MCU_LITTLE_ENDIAN() __LITTLE_ENDIAN__
#define _PRAGMA(x) _Pragma(#x)
#define HAL_ISR_FUNC_DECLARATION(f,v) _PRAGMA(vector=v) __near_func __interrupt void f(void)
#define HAL_ISR_FUNC_PROTOTYPE(f,v) _PRAGMA(vector=v) __near_func __interrupt void f(void)
#define HAL_ISR_FUNCTION(f,v) HAL_ISR_FUNC_PROTOTYPE(f,v); HAL_ISR_FUNC_DECLARATION(f,v)
/* ---------------------- Keil Compiler ---------------------- */
#elif defined __KEIL__ // TODO
//#include <CC2530.h>
//#define HAL_COMPILER_KEIL
//#define HAL_MCU_LITTLE_ENDIAN() 0
//#define HAL_ISR_FUNC_DECLARATION(f,v) void f(void) interrupt v
//#define HAL_ISR_FUNC_PROTOTYPE(f,v) void f(void)
//#define HAL_ISR_FUNCTION(f,v) HAL_ISR_FUNC_PROTOTYPE(f,v); HAL_ISR_FUNC_DECLARATION(f,v)
/* ------------------ Unrecognized Compiler ------------------ */
#else
// #error "ERROR: Unknown compiler."
#endif
/* ------------------------------------------------------------------------------------------------
* Interrupt Macros
* ------------------------------------------------------------------------------------------------
*/
#define HAL_ENABLE_INTERRUPTS() __enable_irq()
#define HAL_DISABLE_INTERRUPTS() __disable_irq()
#define HAL_INTERRUPTS_ARE_ENABLED() \
({ \
uint32 primask; \
__asm volatile("mrs %0, primask" : "=r"(primask)); \
(primask == 0); \
})
// typedef unsigned char halIntState_t;
static uint32 criticalNesting = 0;
#define HAL_ENTER_CRITICAL_SECTION() \
do \
{ \
if (criticalNesting == 0) \
{ \
HAL_DISABLE_INTERRUPTS(); \
} \
criticalNesting++; \
} while (0)
#define HAL_EXIT_CRITICAL_SECTION() \
do \
{ \
if (criticalNesting > 0) \
{ \
criticalNesting--; \
if (criticalNesting == 0) \
{ \
HAL_ENABLE_INTERRUPTS(); \
} \
} \
} while (0)
#define HAL_CRITICAL_STATEMENT(x) \
do \
{ \
HAL_ENTER_CRITICAL_SECTION(); \
x; \
HAL_EXIT_CRITICAL_SECTION(); \
} while (0)
#ifdef __IAR_SYSTEMS_ICC__
/* IAR library uses XCH instruction with EA. It may cause the higher priority interrupt to be
* locked out, therefore, may increase interrupt latency. It may also create a lockup condition.
* This workaround should only be used with 8051 using IAR compiler. When IAR fixes this by
* removing XCH usage in its library, compile the following macros to null to disable them.
*/
#define HAL_ENTER_ISR() { halIntState_t _isrIntState = EA; HAL_ENABLE_INTERRUPTS();
#define HAL_EXIT_ISR() EA = _isrIntState; }
#else
// #define HAL_ENTER_ISR() // TODO
// #define HAL_EXIT_ISR()
#endif /* __IAR_SYSTEMS_ICC__ */
/* Dummy for this platform */
// #define HAL_AES_ENTER_WORKAROUND() // TODO
// #define HAL_AES_EXIT_WORKAROUND()
#ifdef POWER_SAVING
// extern volatile __data uint8 halSleepPconValue;
extern volatile uint8 halSleepPconValue; // TODO
/* Any ISR that is used to wake up the chip shall call this macro. This prevents the race condition
* when the PCON IDLE bit is set after such a critical ISR fires during the prep for sleep.
*/
#define CLEAR_SLEEP_MODE() st( halSleepPconValue = 0; )
#define ALLOW_SLEEP_MODE() st( halSleepPconValue = 1; ) // TODO
#else
#define CLEAR_SLEEP_MODE()
#define ALLOW_SLEEP_MODE()
#endif
/**************************************************************************************************
*/
#endif