hpw422移植新的sdk
This commit is contained in:
@@ -0,0 +1,269 @@
|
||||
/*!
|
||||
* \file hal_assert.c
|
||||
*
|
||||
* \brief Target xc6xxx hal spi implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Includes
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
#include "hal_assert.h"
|
||||
#include "hal_types.h"
|
||||
#include "hal_board.h"
|
||||
#include "hal_defs.h"
|
||||
#include "hal_mcu.h"
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Local Prototypes
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
void halAssertHazardLights(void);
|
||||
|
||||
|
||||
/**************************************************************************************************
|
||||
* @fn halAssertHandler
|
||||
*
|
||||
* @brief Logic to handle an assert.
|
||||
*
|
||||
* @param none
|
||||
*
|
||||
* @return none
|
||||
**************************************************************************************************
|
||||
*/
|
||||
void halAssertHandler( void )
|
||||
{
|
||||
#if defined( HAL_ASSERT_RESET )
|
||||
HAL_SYSTEM_RESET();
|
||||
#elif defined ( HAL_ASSERT_LIGHTS )
|
||||
halAssertHazardLights();
|
||||
#elif defined( HAL_ASSERT_SPIN )
|
||||
volatile uint8 i = 1;
|
||||
HAL_DISABLE_INTERRUPTS();
|
||||
while(i);
|
||||
#endif
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
#if !defined ASSERT_WHILE
|
||||
/**************************************************************************************************
|
||||
* @fn halAssertHazardLights
|
||||
*
|
||||
* @brief Blink LEDs to indicate an error.
|
||||
*
|
||||
* @param none
|
||||
*
|
||||
* @return none
|
||||
**************************************************************************************************
|
||||
*/
|
||||
void halAssertHazardLights(void)
|
||||
{
|
||||
enum
|
||||
{
|
||||
DEBUG_DATA_RSTACK_HIGH_OFS,
|
||||
DEBUG_DATA_RSTACK_LOW_OFS,
|
||||
DEBUG_DATA_TX_ACTIVE_OFS,
|
||||
DEBUG_DATA_RX_ACTIVE_OFS,
|
||||
DEBUG_DATA_SIZE
|
||||
};
|
||||
|
||||
uint8 buttonHeld;
|
||||
uint8 debugData[DEBUG_DATA_SIZE] = {0};
|
||||
|
||||
/* disable all interrupts before anything else */
|
||||
HAL_DISABLE_INTERRUPTS();
|
||||
|
||||
/*-------------------------------------------------------------------------------
|
||||
* Initialize LEDs and turn them off.
|
||||
*/
|
||||
HAL_BOARD_INIT();
|
||||
|
||||
HAL_TURN_OFF_LED1();
|
||||
HAL_TURN_OFF_LED2();
|
||||
HAL_TURN_OFF_LED3();
|
||||
HAL_TURN_OFF_LED4();
|
||||
|
||||
/*-------------------------------------------------------------------------------
|
||||
* Master infinite loop.
|
||||
*/
|
||||
for (;;)
|
||||
{
|
||||
buttonHeld = 0;
|
||||
|
||||
/*-------------------------------------------------------------------------------
|
||||
* "Hazard lights" loop. A held keypress will exit this loop.
|
||||
*/
|
||||
do
|
||||
{
|
||||
HAL_LED_BLINK_DELAY();
|
||||
|
||||
/* toggle LEDS, the #ifdefs are in case HAL has logically remapped non-existent LEDs */
|
||||
#if (HAL_NUM_LEDS >= 1)
|
||||
HAL_TOGGLE_LED1();
|
||||
#if (HAL_NUM_LEDS >= 2)
|
||||
HAL_TOGGLE_LED2();
|
||||
#if (HAL_NUM_LEDS >= 3)
|
||||
HAL_TOGGLE_LED3();
|
||||
#if (HAL_NUM_LEDS >= 4)
|
||||
HAL_TOGGLE_LED4();
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* escape hatch to continue execution, set escape to '1' to continue execution */
|
||||
{
|
||||
static uint8 escape = 0;
|
||||
if (escape)
|
||||
{
|
||||
escape = 0;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* break out of loop if button is held long enough */
|
||||
// if (HAL_PUSH_BUTTON1()) // TODO
|
||||
// {
|
||||
// buttonHeld++;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// buttonHeld = 0;
|
||||
// }
|
||||
}
|
||||
while (buttonHeld != 10); /* loop until button is held specified number of loops */
|
||||
|
||||
/*-------------------------------------------------------------------------------
|
||||
* Just exited from "hazard lights" loop.
|
||||
*/
|
||||
|
||||
/* turn off all LEDs */
|
||||
HAL_TURN_OFF_LED1();
|
||||
HAL_TURN_OFF_LED2();
|
||||
HAL_TURN_OFF_LED3();
|
||||
HAL_TURN_OFF_LED4();
|
||||
|
||||
/* wait for button release */
|
||||
// HAL_DEBOUNCE(!HAL_PUSH_BUTTON1()); // TODO
|
||||
|
||||
/*-------------------------------------------------------------------------------
|
||||
* Load debug data into memory.
|
||||
*/
|
||||
#ifdef HAL_MCU_AVR
|
||||
{
|
||||
uint8 * pStack;
|
||||
pStack = (uint8 *) SP;
|
||||
pStack++; /* point to return address on stack */
|
||||
debugData[DEBUG_DATA_RSTACK_HIGH_OFS] = *pStack;
|
||||
pStack++;
|
||||
debugData[DEBUG_DATA_RSTACK_LOW_OFS] = *pStack;
|
||||
}
|
||||
debugData[DEBUG_DATA_INT_MASK_OFS] = EIMSK;
|
||||
#endif
|
||||
|
||||
/* initialize for data dump loop */
|
||||
{
|
||||
uint8 iBit;
|
||||
uint8 iByte;
|
||||
|
||||
iBit = 0;
|
||||
iByte = 0;
|
||||
|
||||
/*-------------------------------------------------------------------------------
|
||||
* Data dump loop. A button press cycles data bits to an LED.
|
||||
*/
|
||||
while (iByte < DEBUG_DATA_SIZE)
|
||||
{
|
||||
/* wait for key press */
|
||||
// while(!HAL_PUSH_BUTTON1()); // TODO
|
||||
|
||||
/* turn on all LEDs for first bit of byte, turn on three LEDs if not first bit */
|
||||
HAL_TURN_ON_LED1();
|
||||
HAL_TURN_ON_LED2();
|
||||
HAL_TURN_ON_LED3();
|
||||
if (iBit == 0)
|
||||
{
|
||||
HAL_TURN_ON_LED4();
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_TURN_OFF_LED4();
|
||||
}
|
||||
|
||||
/* wait for debounced key release */
|
||||
// HAL_DEBOUNCE(!HAL_PUSH_BUTTON1()); // TODO
|
||||
|
||||
/* turn off all LEDs */
|
||||
HAL_TURN_OFF_LED1();
|
||||
HAL_TURN_OFF_LED2();
|
||||
HAL_TURN_OFF_LED3();
|
||||
HAL_TURN_OFF_LED4();
|
||||
|
||||
/* output value of data bit to LED1 */
|
||||
if (debugData[iByte] & (1 << (7 - iBit)))
|
||||
{
|
||||
HAL_TURN_ON_LED1();
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_TURN_OFF_LED1();
|
||||
}
|
||||
|
||||
/* advance to next bit */
|
||||
iBit++;
|
||||
if (iBit == 8)
|
||||
{
|
||||
iBit = 0;
|
||||
iByte++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* About to enter "hazard lights" loop again. Turn off LED1 in case the last bit
|
||||
* displayed happened to be one. This guarantees all LEDs are off at the start of
|
||||
* the flashing loop which uses a toggle operation to change LED states.
|
||||
*/
|
||||
HAL_TURN_OFF_LED1();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Compile Time Assertions
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/* integrity check of type sizes */
|
||||
HAL_ASSERT_SIZE( int8, 1);
|
||||
HAL_ASSERT_SIZE( uint8, 1);
|
||||
HAL_ASSERT_SIZE( int16, 2);
|
||||
HAL_ASSERT_SIZE(uint16, 2);
|
||||
HAL_ASSERT_SIZE( int32, 4);
|
||||
HAL_ASSERT_SIZE(uint32, 4);
|
||||
#pragma message("note: compile depend")
|
||||
//HAL_ASSERT_SIZE( int32, 8); // TODO
|
||||
//HAL_ASSERT_SIZE(uint32, 8); // TODO
|
||||
|
||||
|
||||
/**************************************************************************************************
|
||||
*/
|
||||
@@ -0,0 +1,295 @@
|
||||
/*!
|
||||
* \file hal_drivers.c
|
||||
*
|
||||
* \brief Target xc6xxx hal spi implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/**************************************************************************************************
|
||||
* INCLUDES
|
||||
**************************************************************************************************/
|
||||
|
||||
// #include "hal_adc.h"
|
||||
#if (defined HAL_AES) && (HAL_AES == TRUE)
|
||||
#include "hal_aes.h"
|
||||
#endif
|
||||
#if (defined HAL_BUZZER) && (HAL_BUZZER == TRUE)
|
||||
#include "hal_buzzer.h"
|
||||
#endif
|
||||
#if (defined HAL_DMA) && (HAL_DMA == TRUE)
|
||||
#include "hal_dma.h"
|
||||
#endif
|
||||
#include "hal_drivers.h"
|
||||
// #include "hal_key.h"
|
||||
// #include "hal_lcd.h"
|
||||
// #include "hal_led.h"
|
||||
#include "hal_sleep.h"
|
||||
#include "hal_timer.h"
|
||||
#include "hal_types.h"
|
||||
// #include "hal_uart.h"
|
||||
#ifdef CC2591_COMPRESSION_WORKAROUND
|
||||
#include "mac_rx.h"
|
||||
#endif
|
||||
#include "OSAL.h"
|
||||
#if defined POWER_SAVING
|
||||
#include "OSAL_PwrMgr.h"
|
||||
#endif
|
||||
#if (defined HAL_HID) && (HAL_HID == TRUE)
|
||||
#include "usb_hid.h"
|
||||
#endif
|
||||
#if (defined HAL_SPI) && (HAL_SPI == TRUE)
|
||||
#include "hal_spi.h"
|
||||
#endif
|
||||
#include "hal_mcu.h"
|
||||
|
||||
/**************************************************************************************************
|
||||
* GLOBAL VARIABLES
|
||||
**************************************************************************************************/
|
||||
uint8 Hal_TaskID;
|
||||
|
||||
extern void HalLedUpdate( void ); /* Notes: This for internal only so it shouldn't be in hal_led.h */
|
||||
|
||||
/**************************************************************************************************
|
||||
* @fn Hal_Init
|
||||
*
|
||||
* @brief Hal Initialization function.
|
||||
*
|
||||
* @param task_id - Hal TaskId
|
||||
*
|
||||
* @return None
|
||||
**************************************************************************************************/
|
||||
void Hal_Init( uint8 task_id )
|
||||
{
|
||||
/* Register task ID */
|
||||
Hal_TaskID = task_id;
|
||||
|
||||
osal_start_reload_timer( Hal_TaskID, PERIOD_RSSI_RESET_EVT, 1000 );
|
||||
#ifdef CC2591_COMPRESSION_WORKAROUND
|
||||
osal_start_reload_timer( Hal_TaskID, PERIOD_RSSI_RESET_EVT, PERIOD_RSSI_RESET_TIMEOUT );
|
||||
#endif
|
||||
}
|
||||
|
||||
/**************************************************************************************************
|
||||
* @fn Hal_DriverInit
|
||||
*
|
||||
* @brief Initialize HW - These need to be initialized before anyone.
|
||||
*
|
||||
* @param task_id - Hal TaskId
|
||||
*
|
||||
* @return None
|
||||
**************************************************************************************************/
|
||||
void HalDriverInit (void)
|
||||
{
|
||||
/* TIMER */
|
||||
#if (defined HAL_TIMER) && (HAL_TIMER == TRUE)
|
||||
#endif
|
||||
|
||||
/* ADC */
|
||||
#if (defined HAL_ADC) && (HAL_ADC == TRUE)
|
||||
HalAdcInit();
|
||||
#endif
|
||||
|
||||
/* DMA */
|
||||
#if (defined HAL_DMA) && (HAL_DMA == TRUE)
|
||||
// Must be called before the init call to any module that uses DMA.
|
||||
HalDmaInit();
|
||||
#endif
|
||||
|
||||
/* AES */
|
||||
#if (defined HAL_AES) && (HAL_AES == TRUE)
|
||||
HalAesInit();
|
||||
#endif
|
||||
|
||||
/* LCD */
|
||||
#if (defined HAL_LCD) && (HAL_LCD == TRUE)
|
||||
HalLcdInit();
|
||||
#endif
|
||||
|
||||
/* LED */
|
||||
#if (defined HAL_LED) && (HAL_LED == TRUE)
|
||||
HalLedInit();
|
||||
#endif
|
||||
|
||||
/* UART */
|
||||
#if (defined HAL_UART) && (HAL_UART == TRUE)
|
||||
HalUARTInit();
|
||||
#endif
|
||||
|
||||
/* KEY */
|
||||
#if (defined HAL_KEY) && (HAL_KEY == TRUE)
|
||||
HalKeyInit();
|
||||
#endif
|
||||
|
||||
/* SPI */
|
||||
#if (defined HAL_SPI) && (HAL_SPI == TRUE)
|
||||
HalSpiInit();
|
||||
#endif
|
||||
|
||||
/* HID */
|
||||
#if (defined HAL_HID) && (HAL_HID == TRUE)
|
||||
usbHidInit();
|
||||
#endif
|
||||
}
|
||||
|
||||
/**************************************************************************************************
|
||||
* @fn Hal_ProcessEvent
|
||||
*
|
||||
* @brief Hal Process Event
|
||||
*
|
||||
* @param task_id - Hal TaskId
|
||||
* events - events
|
||||
*
|
||||
* @return None
|
||||
**************************************************************************************************/
|
||||
uint16 Hal_ProcessEvent( uint8 task_id, uint16 events )
|
||||
{
|
||||
uint8 *msgPtr;
|
||||
|
||||
(void)task_id; // Intentionally unreferenced parameter
|
||||
|
||||
if ( events & SYS_EVENT_MSG )
|
||||
{
|
||||
msgPtr = osal_msg_receive(Hal_TaskID);
|
||||
|
||||
while (msgPtr)
|
||||
{
|
||||
/* Do something here - for now, just deallocate the msg and move on */
|
||||
|
||||
/* De-allocate */
|
||||
osal_msg_deallocate( msgPtr );
|
||||
/* Next */
|
||||
msgPtr = osal_msg_receive( Hal_TaskID );
|
||||
}
|
||||
return events ^ SYS_EVENT_MSG;
|
||||
}
|
||||
|
||||
#if (defined HAL_BUZZER) && (HAL_BUZZER == TRUE)
|
||||
if (events & HAL_BUZZER_EVENT)
|
||||
{
|
||||
HalBuzzerStop();
|
||||
return events ^ HAL_BUZZER_EVENT;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef CC2591_COMPRESSION_WORKAROUND
|
||||
if ( events & PERIOD_RSSI_RESET_EVT )
|
||||
{
|
||||
macRxResetRssi();
|
||||
return (events ^ PERIOD_RSSI_RESET_EVT);
|
||||
}
|
||||
#endif
|
||||
|
||||
if ( events & HAL_LED_BLINK_EVENT )
|
||||
{
|
||||
#if (defined (BLINK_LEDS)) && (HAL_LED == TRUE)
|
||||
HalLedUpdate();
|
||||
#endif /* BLINK_LEDS && HAL_LED */
|
||||
return events ^ HAL_LED_BLINK_EVENT;
|
||||
}
|
||||
|
||||
if ( events & PERIOD_RSSI_RESET_EVT )
|
||||
{
|
||||
#if (defined (BLINK_LEDS)) && (HAL_LED == TRUE)
|
||||
HalLedUpdate();
|
||||
#endif /* BLINK_LEDS && HAL_LED */
|
||||
// TODO only for test
|
||||
// printf("hal task system_clock = %ld\r\n", osal_GetSystemClock());
|
||||
delay_ms(5);
|
||||
return events ^ PERIOD_RSSI_RESET_EVT;
|
||||
}
|
||||
|
||||
if (events & HAL_KEY_EVENT)
|
||||
{
|
||||
#if (defined HAL_KEY) && (HAL_KEY == TRUE)
|
||||
/* Check for keys */
|
||||
HalKeyPoll();
|
||||
|
||||
/* if interrupt disabled, do next polling */
|
||||
if (!Hal_KeyIntEnable)
|
||||
{
|
||||
osal_start_timerEx( Hal_TaskID, HAL_KEY_EVENT, 100);
|
||||
}
|
||||
#endif
|
||||
return events ^ HAL_KEY_EVENT;
|
||||
}
|
||||
|
||||
#if defined POWER_SAVING
|
||||
if ( events & HAL_SLEEP_TIMER_EVENT )
|
||||
{
|
||||
halRestoreSleepLevel();
|
||||
return events ^ HAL_SLEEP_TIMER_EVENT;
|
||||
}
|
||||
|
||||
if ( events & HAL_PWRMGR_HOLD_EVENT )
|
||||
{
|
||||
(void)osal_pwrmgr_task_state(Hal_TaskID, PWRMGR_HOLD);
|
||||
|
||||
(void)osal_stop_timerEx(Hal_TaskID, HAL_PWRMGR_CONSERVE_EVENT);
|
||||
(void)osal_clear_event(Hal_TaskID, HAL_PWRMGR_CONSERVE_EVENT);
|
||||
|
||||
return (events & ~(HAL_PWRMGR_HOLD_EVENT | HAL_PWRMGR_CONSERVE_EVENT));
|
||||
}
|
||||
|
||||
if ( events & HAL_PWRMGR_CONSERVE_EVENT )
|
||||
{
|
||||
(void)osal_pwrmgr_task_state(Hal_TaskID, PWRMGR_CONSERVE);
|
||||
return events ^ HAL_PWRMGR_CONSERVE_EVENT;
|
||||
}
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**************************************************************************************************
|
||||
* @fn Hal_ProcessPoll
|
||||
*
|
||||
* @brief This routine will be called by OSAL to poll UART, TIMER...
|
||||
*
|
||||
* @param task_id - Hal TaskId
|
||||
*
|
||||
* @return None
|
||||
**************************************************************************************************/
|
||||
void Hal_ProcessPoll ()
|
||||
{
|
||||
#if defined( POWER_SAVING )
|
||||
/* Allow sleep before the next OSAL event loop */
|
||||
ALLOW_SLEEP_MODE();
|
||||
#endif
|
||||
|
||||
/* UART Poll */
|
||||
#if (defined HAL_UART) && (HAL_UART == TRUE)
|
||||
HalUARTPoll();
|
||||
#endif
|
||||
|
||||
/* SPI Poll */
|
||||
#if (defined HAL_SPI) && (HAL_SPI == TRUE)
|
||||
HalSpiPoll();
|
||||
#endif
|
||||
|
||||
/* HID poll */
|
||||
#if (defined HAL_HID) && (HAL_HID == TRUE)
|
||||
usbHidProcessEvents();
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
/**************************************************************************************************
|
||||
**************************************************************************************************/
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
/*!
|
||||
* \file hal_assert.h
|
||||
*
|
||||
* \brief The header of hal_assert.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
#ifndef HAL_ASSERT_H
|
||||
#define HAL_ASSERT_H
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Macros
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/*
|
||||
* HAL_ASSERT( expression ) - The given expression must evaluate as "true" or else the assert
|
||||
* handler is called. From here, the call stack feature of the debugger can pinpoint where
|
||||
* the problem occurred.
|
||||
*
|
||||
* HAL_ASSERT_FORCED( ) - If asserts are in use, immediately calls the assert handler.
|
||||
*
|
||||
* HAL_ASSERT_STATEMENT( statement ) - Inserts the given C statement but only when asserts
|
||||
* are in use. This macros allows debug code that is not part of an expression.
|
||||
*
|
||||
* HAL_ASSERT_DECLARATION( declaration ) - Inserts the given C declaration but only when asserts
|
||||
* are in use. This macros allows debug code that is not part of an expression.
|
||||
*
|
||||
* Asserts can be disabled for optimum performance and minimum code size (ideal for
|
||||
* finalized, debugged production code). To disable, define the preprocessor
|
||||
* symbol HALNODEBUG at the project level.
|
||||
*/
|
||||
|
||||
|
||||
#ifdef HALNODEBUG
|
||||
#define HAL_ASSERT(expr)
|
||||
#define HAL_ASSERT_FORCED()
|
||||
#define HAL_ASSERT_STATEMENT(statement)
|
||||
#define HAL_ASSERT_DECLARATION(declaration)
|
||||
#else
|
||||
#define HAL_ASSERT(expr) st( if (!( expr )) halAssertHandler(); )
|
||||
#define HAL_ASSERT_FORCED() halAssertHandler()
|
||||
#define HAL_ASSERT_STATEMENT(statement) st( statement )
|
||||
#define HAL_ASSERT_DECLARATION(declaration) declaration
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* This macro compares the size of the first parameter to the integer value
|
||||
* of the second parameter. If they do not match, a compile time error for
|
||||
* negative array size occurs (even gnu chokes on negative array size).
|
||||
*
|
||||
* This compare is done by creating a typedef for an array. No variables are
|
||||
* created and no memory is consumed with this check. The created type is
|
||||
* used for checking only and is not for use by any other code. The value
|
||||
* of 10 in this macro is arbitrary, it just needs to be a value larger
|
||||
* than one to result in a positive number for the array size.
|
||||
*/
|
||||
#define HAL_ASSERT_SIZE(x,y) typedef char x ## _assert_size_t[-1+10*(sizeof(x) == (y))]
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Prototypes
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
void halAssertHandler(void);
|
||||
|
||||
|
||||
/**************************************************************************************************
|
||||
*/
|
||||
|
||||
/**************************************************************************************************
|
||||
* FUNCTIONS - API
|
||||
**************************************************************************************************/
|
||||
|
||||
|
||||
extern void halAssertHazardLights(void);
|
||||
#endif
|
||||
@@ -0,0 +1 @@
|
||||
#include "hal_board_cfg.h"
|
||||
@@ -0,0 +1,131 @@
|
||||
/*!
|
||||
* \file hal_defs.h
|
||||
*
|
||||
* \brief The header of hal_defs.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
#ifndef HAL_DEFS_H
|
||||
#define HAL_DEFS_H
|
||||
#include "xc6xxx.h"
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Macros
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#ifndef BV
|
||||
#define BV(n) (1 << (n))
|
||||
#endif
|
||||
|
||||
#ifndef BF
|
||||
#define BF(x,b,s) (((x) & (b)) >> (s))
|
||||
#endif
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(n,m) (((n) < (m)) ? (n) : (m))
|
||||
#endif
|
||||
|
||||
#ifndef MAX
|
||||
#define MAX(n,m) (((n) < (m)) ? (m) : (n))
|
||||
#endif
|
||||
|
||||
#ifndef ABS
|
||||
#define ABS(n) (((n) < 0) ? -(n) : (n))
|
||||
#endif
|
||||
|
||||
|
||||
/* takes a byte out of a uint32 : var - uint32, ByteNum - byte to take out (0 - 3) */
|
||||
#define BREAK_UINT32( var, ByteNum ) \
|
||||
(uint8)((uint32)(((var) >>((ByteNum) * 8)) & 0x00FF))
|
||||
|
||||
#define BUILD_UINT32(Byte0, Byte1, Byte2, Byte3) \
|
||||
((uint32)((uint32)((Byte0) & 0x00FF) \
|
||||
+ ((uint32)((Byte1) & 0x00FF) << 8) \
|
||||
+ ((uint32)((Byte2) & 0x00FF) << 16) \
|
||||
+ ((uint32)((Byte3) & 0x00FF) << 24)))
|
||||
|
||||
#define BUILD_UINT16(loByte, hiByte) \
|
||||
((uint16)(((loByte) & 0x00FF) + (((hiByte) & 0x00FF) << 8)))
|
||||
|
||||
#define HI_UINT16(a) (((a) >> 8) & 0xFF)
|
||||
#define LO_UINT16(a) ((a) & 0xFF)
|
||||
|
||||
#define BUILD_UINT8(hiByte, loByte) \
|
||||
((uint8)(((loByte) & 0x0F) + (((hiByte) & 0x0F) << 4)))
|
||||
|
||||
#define HI_UINT8(a) (((a) >> 4) & 0x0F)
|
||||
#define LO_UINT8(a) ((a) & 0x0F)
|
||||
|
||||
// Write the 32bit value of 'val' in little endian format to the buffer pointed
|
||||
// to by pBuf, and increment pBuf by 4
|
||||
#define UINT32_TO_BUF_LITTLE_ENDIAN(pBuf,val) \
|
||||
do { \
|
||||
*(pBuf)++ = ((((uint32)(val)) >> 0) & 0xFF); \
|
||||
*(pBuf)++ = ((((uint32)(val)) >> 8) & 0xFF); \
|
||||
*(pBuf)++ = ((((uint32)(val)) >> 16) & 0xFF); \
|
||||
*(pBuf)++ = ((((uint32)(val)) >> 24) & 0xFF); \
|
||||
} while (0)
|
||||
|
||||
// Return the 32bit little-endian formatted value pointed to by pBuf, and increment pBuf by 4
|
||||
#define BUF_TO_UINT32_LITTLE_ENDIAN(pBuf) (((pBuf) += 4), BUILD_UINT32((pBuf)[-4], (pBuf)[-3], (pBuf)[-2], (pBuf)[-1]))
|
||||
|
||||
#ifndef CHECK_BIT
|
||||
#define CHECK_BIT(DISCS, IDX) ((DISCS) & (1<<(IDX)))
|
||||
#endif
|
||||
#ifndef GET_BIT
|
||||
#define GET_BIT(DISCS, IDX) (((DISCS)[((IDX) / 8)] & BV((IDX) % 8)) ? TRUE : FALSE)
|
||||
#endif
|
||||
#ifndef SET_BIT
|
||||
#define SET_BIT(DISCS, IDX) (((DISCS)[((IDX) / 8)] |= BV((IDX) % 8)))
|
||||
#endif
|
||||
#ifndef CLR_BIT
|
||||
#define CLR_BIT(DISCS, IDX) (((DISCS)[((IDX) / 8)] &= (BV((IDX) % 8) ^ 0xFF)))
|
||||
#endif
|
||||
|
||||
/*
|
||||
* This macro is for use by other macros to form a fully valid C statement.
|
||||
* Without this, the if/else conditionals could show unexpected behavior.
|
||||
*
|
||||
* For example, use...
|
||||
* #define SET_REGS() st( ioreg1 = 0; ioreg2 = 0; )
|
||||
* instead of ...
|
||||
* #define SET_REGS() { ioreg1 = 0; ioreg2 = 0; }
|
||||
* or
|
||||
* #define SET_REGS() ioreg1 = 0; ioreg2 = 0;
|
||||
* The last macro would not behave as expected in the if/else construct.
|
||||
* The second to last macro will cause a compiler error in certain uses
|
||||
* of if/else construct
|
||||
*
|
||||
* It is not necessary, or recommended, to use this macro where there is
|
||||
* already a valid C statement. For example, the following is redundant...
|
||||
* #define CALL_FUNC() st( func(); )
|
||||
* This should simply be...
|
||||
* #define CALL_FUNC() func()
|
||||
*
|
||||
* (The while condition below evaluates false without generating a
|
||||
* constant-controlling-loop type of warning on most compilers.)
|
||||
*/
|
||||
#define st(x) do { x } while (__LINE__ == -1)
|
||||
|
||||
|
||||
/**************************************************************************************************
|
||||
*/
|
||||
#endif
|
||||
@@ -0,0 +1,91 @@
|
||||
/*!
|
||||
* \file hal_drivers.h
|
||||
*
|
||||
* \brief The header of hal_drivers.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
#ifndef HAL_DRIVER_H
|
||||
#define HAL_DRIVER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/**************************************************************************************************
|
||||
* INCLUDES
|
||||
**************************************************************************************************/
|
||||
|
||||
#include "hal_types.h"
|
||||
|
||||
/**************************************************************************************************
|
||||
* CONSTANTS
|
||||
**************************************************************************************************/
|
||||
|
||||
#define HAL_BUZZER_EVENT 0x0080
|
||||
#define PERIOD_RSSI_RESET_EVT 0x0040
|
||||
#define HAL_LED_BLINK_EVENT 0x0020
|
||||
#define HAL_KEY_EVENT 0x0010
|
||||
|
||||
#if defined POWER_SAVING
|
||||
#define HAL_SLEEP_TIMER_EVENT 0x0004
|
||||
#define HAL_PWRMGR_HOLD_EVENT 0x0002
|
||||
#define HAL_PWRMGR_CONSERVE_EVENT 0x0001
|
||||
#endif
|
||||
|
||||
#define HAL_PWRMGR_CONSERVE_DELAY 10
|
||||
#define PERIOD_RSSI_RESET_TIMEOUT 10
|
||||
|
||||
/**************************************************************************************************
|
||||
* GLOBAL VARIABLES
|
||||
**************************************************************************************************/
|
||||
|
||||
extern uint8 Hal_TaskID;
|
||||
|
||||
/**************************************************************************************************
|
||||
* FUNCTIONS - API
|
||||
**************************************************************************************************/
|
||||
|
||||
extern void Hal_Init ( uint8 task_id );
|
||||
|
||||
/*
|
||||
* Process Serial Buffer
|
||||
*/
|
||||
extern uint16 Hal_ProcessEvent ( uint8 task_id, uint16 events );
|
||||
|
||||
/*
|
||||
* Process Polls
|
||||
*/
|
||||
extern void Hal_ProcessPoll (void);
|
||||
|
||||
/*
|
||||
* Initialize HW
|
||||
*/
|
||||
extern void HalDriverInit (void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/**************************************************************************************************
|
||||
**************************************************************************************************/
|
||||
@@ -0,0 +1,71 @@
|
||||
/*!
|
||||
* \file hal_sleep.h
|
||||
*
|
||||
* \brief The header of hal_sleep.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
#ifndef HAL_SLEEP_H
|
||||
#define HAL_SLEEP_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include "hal_types.h"
|
||||
|
||||
/*********************************************************************
|
||||
* FUNCTIONS
|
||||
*/
|
||||
extern void system_sleep_init(void);
|
||||
/*
|
||||
* Execute power management procedure
|
||||
*/
|
||||
extern void halSleep( uint32 osal_timer );
|
||||
|
||||
/*
|
||||
* Used in mac_mcu
|
||||
*/
|
||||
extern void halSleepWait(uint16 duration);
|
||||
|
||||
/*
|
||||
* Used in hal_drivers, AN044 - DELAY EXTERNAL INTERRUPTS
|
||||
*/
|
||||
extern void halRestoreSleepLevel( void );
|
||||
|
||||
/*
|
||||
* Used by the interrupt routines to exit from sleep.
|
||||
*/
|
||||
extern void halSleepExit(void);
|
||||
|
||||
/*
|
||||
* Set the max sleep loop time lesser than the T2 rollover period.
|
||||
*/
|
||||
extern void halSetMaxSleepLoopTime(uint32 rolloverTime);
|
||||
|
||||
/*********************************************************************
|
||||
*********************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,37 @@
|
||||
/*!
|
||||
* \file hal_timer.h
|
||||
*
|
||||
* \brief The header of hal_timer.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
#ifndef TIMER_H
|
||||
#define TIMER_H
|
||||
|
||||
#include "hal_types.h"
|
||||
|
||||
#define OSAL_TIMER (TIMER1_IDX)
|
||||
|
||||
void osal_timer_init(void);
|
||||
void osal_timer_start(void);
|
||||
void osal_timer_tick_set(uint32 tick);
|
||||
uint32 getMcuPrecisionCount(void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,78 @@
|
||||
/*!
|
||||
* \file hal_board_cfg.h
|
||||
*
|
||||
* \brief The header of xc_drv_aotimer.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
#ifndef HAL_BOARD_CFG_H
|
||||
#define HAL_BOARD_CFG_H
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Includes
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#include "hal_mcu.h"
|
||||
#include "hal_defs.h"
|
||||
#include "hal_types.h"
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* LED Configuration
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#define HAL_LED_BLINK_DELAY() st( { volatile uint32 i; for (i=0; i<0x5800; i++) { }; } )
|
||||
|
||||
/* ----------- Debounce ---------- */
|
||||
#define HAL_DEBOUNCE(expr) \
|
||||
{ \
|
||||
int i; \
|
||||
for (i = 0; i < 500; i++) \
|
||||
{ \
|
||||
if (!(expr)) \
|
||||
i = 0; \
|
||||
} \
|
||||
}
|
||||
|
||||
/* ----------- Push Buttons ---------- */
|
||||
#define HAL_PUSH_BUTTON1()
|
||||
#define HAL_PUSH_BUTTON2()
|
||||
#define HAL_PUSH_BUTTON3()
|
||||
#define HAL_PUSH_BUTTON4()
|
||||
#define HAL_PUSH_BUTTON5()
|
||||
#define HAL_PUSH_BUTTON6()
|
||||
|
||||
/* ----------- LED's ---------- */
|
||||
#define HAL_TURN_ON_LED1()
|
||||
#define HAL_TURN_ON_LED2()
|
||||
#define HAL_TURN_ON_LED3()
|
||||
#define HAL_TURN_ON_LED4()
|
||||
|
||||
#define HAL_TURN_OFF_LED1()
|
||||
#define HAL_TURN_OFF_LED2()
|
||||
#define HAL_TURN_OFF_LED3()
|
||||
#define HAL_TURN_OFF_LED4()
|
||||
|
||||
#define HAL_BOARD_INIT()
|
||||
|
||||
#endif
|
||||
/*******************************************************************************************************
|
||||
*/
|
||||
@@ -0,0 +1,151 @@
|
||||
/*!
|
||||
* \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
|
||||
@@ -0,0 +1,304 @@
|
||||
/*!
|
||||
* \file hal_sleep.c
|
||||
*
|
||||
* \brief Target xc6xxx hal spi implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Includes
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
#include "hal_types.h"
|
||||
#include "hal_mcu.h"
|
||||
#include "hal_board.h"
|
||||
#include "hal_sleep.h"
|
||||
#include "OSAL.h"
|
||||
#include "OSAL_Timers.h"
|
||||
#include "OSAL_Tasks.h"
|
||||
#include "OSAL_PwrMgr.h"
|
||||
#include "OnBoard.h"
|
||||
#include "hal_drivers.h"
|
||||
#include "hal_assert.h"
|
||||
#include "hal_timer.h"
|
||||
#include "xc_drv_pwr.h"
|
||||
#include "xc_drv_clock.h"
|
||||
#include "xc6xxx.h"
|
||||
|
||||
#define WAKEUP_TIMER (TIMER0_IDX)
|
||||
#define OSASL_MIN_SLEEP_TIME (5)
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Global Variables
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
volatile uint8 halSleepPconValue;
|
||||
uint32 rtc_time_mod = 0;
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Function Prototypes
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
#if (0)
|
||||
__RAM_CODE static uint32_t wakeup_timer_set(uint32_t ms)
|
||||
{
|
||||
uint32_t ticks;
|
||||
cprao_aon_reg1__timer_ao_sleep_clksw__setf(0);
|
||||
|
||||
/* aotimer0 init */
|
||||
if (cprao_aon_clken_grctl__timer_ao_pclk_en__getf() != ENABLE)
|
||||
{
|
||||
cprao_aon_clken_grctl__timer_ao_pclk_en__setf(ENABLE);
|
||||
}
|
||||
cprao_aon_clken_grctl__timer0_ao_pclk_en__setf(ENABLE);
|
||||
cprao_aon_clken_grctl__timer1_ao_pclk_en__setf(ENABLE);
|
||||
cpr_lp_ctl__timer_sysclk_sel__setf(ENABLE);
|
||||
aotimer_tcr__tes__setf(WAKEUP_TIMER, DISABLE);
|
||||
aotimer_tcr__tms__setf(WAKEUP_TIMER, (uint8_t)AOTIMER_MODE_SINGLE);
|
||||
(void)aotimer_tic__tic__getf(WAKEUP_TIMER);
|
||||
|
||||
/* aotimer0 set value */
|
||||
if (clock_cb.lfclk_in == CLOCK_LFCLK_IN_32768) {
|
||||
ticks = 32 * ms + (768 * ms) / 1000;}
|
||||
else if (clock_cb.lfclk_in == CLOCK_LFCLK_IN_32K) {
|
||||
ticks = 32 * ms;}
|
||||
ticks = ticks < AOTIMER_MIN_TLC_VAL ? AOTIMER_MIN_TLC_VAL : ticks;
|
||||
aotimer_tcr__tes__setf(WAKEUP_TIMER, AOTIMER_TCR_TES_DISABLE);
|
||||
aotimer_tlc_set(WAKEUP_TIMER, ticks);
|
||||
|
||||
/* aotimer0 start */
|
||||
NVIC_EnableIRQ(TIMER_AO0_IRQn);
|
||||
aotimer_tcr__tim__setf(WAKEUP_TIMER, DISABLE);
|
||||
aotimer_tcr__tes__setf(WAKEUP_TIMER, ENABLE);
|
||||
|
||||
// Wait for timer enable
|
||||
while (aotimer_tcr__tes__getf(WAKEUP_TIMER) != ENABLE)
|
||||
{
|
||||
__nop();
|
||||
}
|
||||
|
||||
return ticks;
|
||||
}
|
||||
#endif
|
||||
|
||||
__RAM_CODE static uint32_t wakeup_timer_set(uint32_t ms)
|
||||
{
|
||||
uint32_t ticks;
|
||||
/* wakeup timer init */
|
||||
if (cpr_ctlapbclken_grctl__timer_pclk_en__getf() != ENABLE) {
|
||||
cpr_ctlapbclken_grctl__timer_pclk_en__setf(ENABLE);}
|
||||
cpr_timer0_clk_ctl__timer_clksel__setf((uint8_t)TIMER_CLK_SRC_32K);
|
||||
cpr_timer0_clk_ctl__timer_clk0_div__setf((uint8_t)TIMER_DIV_CLK_32000Hz);
|
||||
cpr_timer0_clk_ctl__timer_clk1_div__setf((uint8_t)TIMER_DIV_CLK_32000Hz);
|
||||
if (cpr_lp_ctl__timer_sysclk_sel__getf() != ENABLE) {
|
||||
cpr_lp_ctl__timer_sysclk_sel__setf(ENABLE);}
|
||||
timer_tcr__tes__setf(WAKEUP_TIMER, TIMER_TCR_TES_DISABLE);
|
||||
timer_tcr__tms__setf(WAKEUP_TIMER, (uint8_t)TIMER_MODE_SINGLE);
|
||||
|
||||
#if !defined(NO_SUPPORT_LFCLK_XTAL_32768)
|
||||
/* wakeup timer set value */
|
||||
if (clock_cb.lfclk_in == CLOCK_LFCLK_IN_32768) {
|
||||
ticks = 32 * ms + (768 * ms) / 1000;}
|
||||
else if (clock_cb.lfclk_in == CLOCK_LFCLK_IN_32K) {
|
||||
ticks = 32 * ms;}
|
||||
#else
|
||||
ticks = 32 * ms;
|
||||
#endif
|
||||
|
||||
ticks = ticks < TIMER_MIN_TLC_VAL ? TIMER_MIN_TLC_VAL : ticks;
|
||||
// timer_tcr__tes__setf(WAKEUP_TIMER, TIMER_TCR_TES_DISABLE);
|
||||
timer_tlc_set(WAKEUP_TIMER, ticks);
|
||||
|
||||
/* wakeup timer start */
|
||||
NVIC_EnableIRQ(TIMER0_IRQn);
|
||||
timer_tcr__tim__setf(WAKEUP_TIMER, TIMER_TCR_TIM_DISABLE);
|
||||
timer_tcr__tes__setf(WAKEUP_TIMER, TIMER_TCR_TES_ENABLE);
|
||||
|
||||
// Wait for timer enable
|
||||
while (timer_tcr__tes__getf(WAKEUP_TIMER) != ENABLE)
|
||||
{
|
||||
__nop();
|
||||
}
|
||||
|
||||
return ticks;
|
||||
}
|
||||
|
||||
static void bor_init(void)
|
||||
{
|
||||
cprao_aon_bor_ctr_reg0__bor_ctrl__setf(0x0);
|
||||
cprao_aon_bor_ctr_reg0__bor_rstn_mask__setf(1);
|
||||
cprao_aon_bor_ctr_reg0__bor_intr_mask__setf(0);
|
||||
NVIC_EnableIRQ(MPU_IRQn);
|
||||
}
|
||||
|
||||
static void system_lightsleep_cfg(void)
|
||||
{
|
||||
#if (USE_XIP != 1)
|
||||
cprao_aon_puctrl1_set(0x4); /* puctrl1= 0x4 , SSI0RX must pulldown*/
|
||||
#endif
|
||||
cprao_aon_sys_time_set((RST_READY_TIME << 12) | (OSC32_STABLE_TIME));
|
||||
|
||||
xc_pwr_pd_lightsleep_set();
|
||||
xc_pwr_sleepsrc_mask_set(0x1e001e);
|
||||
xc_pwr_osc_off();
|
||||
}
|
||||
|
||||
void system_sleep_init(void)
|
||||
{
|
||||
uint32_t wake_it_src;
|
||||
bor_init();
|
||||
#if (0)
|
||||
#if !defined(NO_SUPPORT_ROM_FMC_SPI)
|
||||
#if (USE_XIP == 1)
|
||||
xc_fmc_spi_init_oprt();
|
||||
#endif
|
||||
#else
|
||||
#if (USE_XIP == 1)
|
||||
SPI_InitCfg_t spi_cfg = {0};
|
||||
spi_cfg.Mode = SPI_MODE_MASTER;
|
||||
spi_cfg.DataSize = SSI_CTRL0_DFS_LEN_8BIT;
|
||||
spi_cfg.Direction = SSI_CTRL0_TMOD_WR;
|
||||
spi_cfg.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
|
||||
spi_cfg.CLKPolarity = SSI_CTRL0_SCPOL_LOW;
|
||||
spi_cfg.CLKPhase = SPI_CPHA_LEAD;
|
||||
spi_cfg.FirstBit = SPI_FirstBit_MSB;
|
||||
|
||||
xc_spi_init(XC_SPI0, &spi_cfg);
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
xc_pwr_gpio_sleep_config();
|
||||
wake_it_src = GPIO_IRQn_WAKE | RTC_IRQn_WAKE | TIMER_AO0_IRQn_WAKE | TIMER0_IRQn_WAKE;
|
||||
xc_pwr_wake_it_set(wake_it_src);
|
||||
|
||||
system_lightsleep_cfg();
|
||||
}
|
||||
|
||||
__RAM_CODE void halSleep(uint32 osal_timeout)
|
||||
{
|
||||
uint32 before_sleep_tick, wakeup_timer_set_tick, wakeup_timer_current_tick, wakeup_timer_dlt_tick, wakeup_timer_dlt_us, temp_tick;
|
||||
if (osal_timeout <= OSASL_MIN_SLEEP_TIME)
|
||||
return;
|
||||
|
||||
NVIC_DisableIRQ(TIMER1_IRQn);
|
||||
HAL_DISABLE_INTERRUPTS();
|
||||
/* stop OSAL timer */
|
||||
timer_tcr__tes__setf(OSAL_TIMER, TIMER_TCR_TES_DISABLE);
|
||||
/* get before sleep tick */
|
||||
before_sleep_tick = getMcuPrecisionCount();
|
||||
/* set sleep time:ms */
|
||||
wakeup_timer_set_tick = wakeup_timer_set(osal_timeout);
|
||||
delay_us(100);
|
||||
/* sleep */
|
||||
xc_sleep();
|
||||
xc_rc32k_soft_calib_enable();
|
||||
wakeup_timer_current_tick = timer_tcv_get(WAKEUP_TIMER);
|
||||
/* stop wakeup timer */
|
||||
timer_tcr__tes__setf(WAKEUP_TIMER, TIMER_TCR_TES_DISABLE);
|
||||
if (wakeup_timer_current_tick < wakeup_timer_set_tick) {
|
||||
wakeup_timer_dlt_tick = wakeup_timer_set_tick - wakeup_timer_current_tick;}
|
||||
else {
|
||||
wakeup_timer_dlt_tick = 0xffffffff - wakeup_timer_current_tick + wakeup_timer_set_tick;}
|
||||
#if !defined(NO_SUPPORT_LFCLK_XTAL_32768)
|
||||
if (clock_cb.lfclk_in == CLOCK_LFCLK_IN_32768) {
|
||||
wakeup_timer_dlt_us = (((wakeup_timer_dlt_tick << 7) - (wakeup_timer_dlt_tick << 2) - (wakeup_timer_dlt_tick << 1)) >> 2) + (((wakeup_timer_dlt_tick << 3) + wakeup_timer_dlt_tick) >> 9);}
|
||||
else {
|
||||
wakeup_timer_dlt_us = (wakeup_timer_dlt_tick * 125) >> 2;}
|
||||
#else
|
||||
wakeup_timer_dlt_us = (wakeup_timer_dlt_tick * 125) >> 2;
|
||||
#endif
|
||||
|
||||
temp_tick = before_sleep_tick + wakeup_timer_dlt_us / 625;
|
||||
rtc_time_mod += wakeup_timer_dlt_us % 625;
|
||||
if (rtc_time_mod > 625)
|
||||
{
|
||||
temp_tick++;
|
||||
rtc_time_mod = rtc_time_mod % 625;
|
||||
}
|
||||
/* update system tick */
|
||||
osal_timer_tick_set(temp_tick);
|
||||
osal_timer_start();
|
||||
HAL_ENABLE_INTERRUPTS();
|
||||
xc_rc32k_soft_calib_disable();
|
||||
}
|
||||
|
||||
__RAM_CODE void MPU_Handler(void)
|
||||
{
|
||||
cprao_aon_slp_pd_mask_set(0x001);
|
||||
|
||||
#if XTAL_32K
|
||||
#if !defined(NO_SUPPORT_LFCLK_XTAL_32768)
|
||||
cprao_aon_ctl_clk32k_set(0x109);
|
||||
#endif
|
||||
#endif
|
||||
printf("MPU_Handler\n");
|
||||
WDT_InitCfg_t wdt_cfg;
|
||||
|
||||
wdt_cfg.WorkMode = WDT_WORK_MODE0;
|
||||
wdt_cfg.ReloadValue = WDT_CLK_32M_RESET_MODE0_4096US;
|
||||
wdt_cfg.PclkSel = WDT_WORK_32M;
|
||||
xc_wdt_init(&wdt_cfg);
|
||||
xc_wdt_start();
|
||||
while (1);
|
||||
}
|
||||
|
||||
/**************************************************************************************************
|
||||
* @fn TimerElapsed
|
||||
*
|
||||
* @brief Determine the number of OSAL timer ticks elapsed during sleep.
|
||||
* Deprecated for CC2538 and CC2430 SoC.
|
||||
*
|
||||
* input parameters
|
||||
*
|
||||
* @param None.
|
||||
*
|
||||
* output parameters
|
||||
*
|
||||
* None.
|
||||
*
|
||||
* @return Number of timer ticks elapsed during sleep.
|
||||
**************************************************************************************************
|
||||
*/
|
||||
uint32 TimerElapsed( void )
|
||||
{
|
||||
/* Stubs */
|
||||
return (0);
|
||||
}
|
||||
|
||||
/**************************************************************************************************
|
||||
* @fn halRestoreSleepLevel
|
||||
*
|
||||
* @brief Restore the deepest timer sleep level.
|
||||
*
|
||||
* input parameters
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* output parameters
|
||||
*
|
||||
* None.
|
||||
*
|
||||
* @return None.
|
||||
**************************************************************************************************
|
||||
*/
|
||||
void halRestoreSleepLevel( void )
|
||||
{
|
||||
/* Stubs */
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/*!
|
||||
* \file hal_sleep.c
|
||||
*
|
||||
* \brief Target xc6xxx hal spi implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Includes
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
#include "hal_timer.h"
|
||||
#include "OSAL_Clock.h"
|
||||
#include "xc_drv_timer.h"
|
||||
|
||||
uint32 sys_tick_cnt;
|
||||
|
||||
__RAM_CODE void timer1_callback(void *context)
|
||||
{
|
||||
sys_tick_cnt++;
|
||||
}
|
||||
|
||||
__RAM_CODE void osal_timer_init(void)
|
||||
{
|
||||
/* timer1 init */
|
||||
if (cpr_ctlapbclken_grctl__timer_pclk_en__getf() != ENABLE)
|
||||
cpr_ctlapbclken_grctl__timer_pclk_en__setf(ENABLE);
|
||||
|
||||
cpr_timer1_clk_ctl__timer_clksel__setf(TIMER_CLK_SRC_32M_DIV);
|
||||
cpr_timer1_clk_ctl__timer_clk0_div__setf(TIMER_DIV_CLK_16MHzOr16K);
|
||||
cpr_timer1_clk_ctl__timer_clk1_div__setf(TIMER_DIV_CLK_16MHzOr16K);
|
||||
|
||||
cpr_lp_ctl__timer_sysclk_sel__setf(ENABLE);
|
||||
timer_tcr__tes__setf(OSAL_TIMER, TIMER_TCR_TES_DISABLE);
|
||||
timer_tcr__tms__setf(OSAL_TIMER, TIMER_MODE_CYCLE);
|
||||
|
||||
/* value set */
|
||||
timer_tlc_set(OSAL_TIMER, 10000); //625us = 10000tick
|
||||
|
||||
/* timer1 start */
|
||||
NVIC_EnableIRQ(TIMER1_IRQn);
|
||||
NVIC_SetPriority(TIMER1_IRQn,1);
|
||||
timer_tcr__tim__setf(OSAL_TIMER, TIMER_TCR_TIM_DISABLE);
|
||||
timer_tcr__tes__setf(OSAL_TIMER, TIMER_TCR_TES_ENABLE);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
__RAM_CODE void osal_timer_start(void)
|
||||
{
|
||||
NVIC_EnableIRQ(TIMER1_IRQn);
|
||||
timer_tcr__tim__setf(OSAL_TIMER, TIMER_TCR_TIM_DISABLE);
|
||||
timer_tcr__tes__setf(OSAL_TIMER, TIMER_TCR_TES_ENABLE);
|
||||
}
|
||||
|
||||
__RAM_CODE void osal_timer_tick_set(uint32 tick)
|
||||
{
|
||||
sys_tick_cnt = tick;
|
||||
}
|
||||
|
||||
__RAM_CODE uint32 getMcuPrecisionCount(void)
|
||||
{
|
||||
return sys_tick_cnt;
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
/*!
|
||||
* \file hal_types.h
|
||||
*
|
||||
* \brief The header of hal_types.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
#ifndef _HAL_TYPES_H
|
||||
#define _HAL_TYPES_H
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Types
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
typedef signed char int8;
|
||||
typedef unsigned char uint8;
|
||||
|
||||
typedef signed short int16;
|
||||
typedef unsigned short uint16;
|
||||
|
||||
typedef signed long int32;
|
||||
typedef unsigned long uint32;
|
||||
|
||||
//typedef unsigned char bool;
|
||||
|
||||
typedef uint32 halDataAlign_t;
|
||||
#define POWER_SAVING
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Memory Attributes and Compiler Macros
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/* ----------- IAR Compiler ----------- */
|
||||
#ifdef __IAR_SYSTEMS_ICC__
|
||||
#define CODE __code
|
||||
#define XDATA __xdata
|
||||
#define ASM_NOP asm("NOP")
|
||||
|
||||
/* ----------- KEIL Compiler ----------- */
|
||||
#elif defined __KEIL__
|
||||
#define CODE code
|
||||
#define XDATA xdata
|
||||
#define ASM_NOP __nop()
|
||||
|
||||
/* ----------- CCS Compiler ----------- */
|
||||
#elif defined __TI_COMPILER_VERSION
|
||||
#define ASM_NOP asm(" NOP")
|
||||
|
||||
/* ----------- GNU Compiler ----------- */
|
||||
#elif defined __GNUC__
|
||||
#define ASM_NOP __asm__ __volatile__ ("nop")
|
||||
|
||||
/* ---------- MSVC compiler ---------- */
|
||||
#elif _MSC_VER
|
||||
#define ASM_NOP __asm NOP
|
||||
|
||||
/* ----------- Unrecognized Compiler ----------- */
|
||||
#else
|
||||
#error "ERROR: Unknown compiler."
|
||||
#endif
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Standard Defines
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
#ifndef TRUE
|
||||
#define TRUE 1
|
||||
#endif
|
||||
|
||||
#ifndef FALSE
|
||||
#define FALSE 0
|
||||
#endif
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL 0
|
||||
#endif
|
||||
|
||||
|
||||
/**************************************************************************************************
|
||||
*/
|
||||
#endif
|
||||
Reference in New Issue
Block a user