📄 hal.h
字号:
do { \
CLKCON = ((CLKCON & ~0x07) | (frequency & 0x07)); \
}while (0)
// where frequency is one of
#define MHZ_24 0x00
#define MHZ_12 0x01
#define MHZ_6 0x02
#define MHZ_3 0x03
#define MHZ_1_5 0x04
#define MHZ_0_75 0x05
#define MHZ_0_37 0x06
#define MHZ_0_19 0x07
#endif
/******************************************************************************
******************* Timer macros/functions *******************
*******************************************************************************
General:
The timers/counters can be configured in a number of ways. The following
functions allow basic configuration of the timers as interrupt timers,
pulse width modulators (PWM) and capture timers. Other uses require manual
configuration of the timers/counters.
Generally 3 steps are nescessary to start a timer:
TIMERx_INIT();
bool halSetTimerxPeriod(period);
TIMERx_RUN(true);
where x is the timer number. Please see the function / macro in question for
details.
All timers can generate interrupts. The configuration of interrupts is not
included in the HAL.
******************************************************************************/
#define CLR_TIMER34_IF( bitMask )\
TIMIF = ( TIMIF & 0x40 ) | ( 0x3F & (~bitMask) )
#define CLR_TIMER1_IF( bitMask )\
T1CTL = ( T1CTL & 0x0F ) | ( 0xF0 & (~bitMask) )
/******************************************************************************
* @fn halSetTimer1Period
*
* @brief
* This function sets up timer 1 to run with a given period. If _period_ is
* set to 0, maximum period length will be used. The first time the timer
* is used the macro TIMER1_INIT() should be run to clear all settings. The
* timer is started and stopped with the macro TIMER1_RUN(true / false).
*
* Parameters:
*
* @param dword period
* The desired timer period in u-seconds.
*
* @return WORD
* The timer value written to the register if the configuration was
* successful and 0 if the period could not be achieved. This return
* value can be used for determining pulse widths when the timer is
* used in PWM mode.
*
******************************************************************************/
word halSetTimer1Period(dword period);
// Macro for initialising timer 1. Resets all involved registers and disables
// all interrupt masks
#define TIMER1_INIT() \
do { \
T1CTL = 0x00; \
T1CCTL0 = 0x00; \
T1CCTL1 = 0x00; \
T1CCTL2 = 0x00; \
TIMIF = ~0x40; \
} while (0)
// Macro for configuring a channel of timer 1 for PWM. Channel may be
// either 1 or 2
#define TIMER1_PWM_CONFIG(channel) \
do { \
T1CCTL##channel## = 0x24; \
if(PERCFG&0x40) { \
if(channel == 0x01){ \
IO_FUNC_PORT_PIN(1,1,IO_FUNC_PERIPH); \
} \
else { \
IO_FUNC_PORT_PIN(1,0,IO_FUNC_PERIPH); \
} \
} \
else { \
if(channel == 0x01){ \
IO_FUNC_PORT_PIN(0,3,IO_FUNC_PERIPH); \
} \
else { \
IO_FUNC_PORT_PIN(0,4,IO_FUNC_PERIPH); \
} \
} \
} while(0)
// Macro for changing the pulse length of a timer in PWM mode. The value is
// not scaled and the user must verify that it is correct. _channel_ is the
// channel (1 or 2) configured for PWM operation, whereas _value_ is the
// 16 bit word giving the pulse length. This argument should be shorter than
// or equal to the value returned from the function halSetTimer1Period(...).
#define TIMER1_SET_PWM_PULSE_LENGTH(channel, value) \
do { \
T1CC##channel##L = LOBYTE( value ); \
T1CC##channel##H = HIBYTE( value ); \
} while(0)
// Macro for configuring a channel of timer 1 for capture.
#define TIMER1_CAPTURE_CHANNEL(channel, edge) \
do { \
T1CCTL ##channel = edge; \
if(PERCFG&0x40) { \
if(channel == 0x01){ \
IO_FUNC_PORT_PIN(1,1,IO_FUNC_PERIPH); \
} \
else { \
IO_FUNC_PORT_PIN(1,0,IO_FUNC_PERIPH); \
} \
} \
else { \
if(channel == 0x01){ \
IO_FUNC_PORT_PIN(0,3,IO_FUNC_PERIPH); \
} \
else { \
IO_FUNC_PORT_PIN(0,4,IO_FUNC_PERIPH); \
} \
} \
} while(0)
// Where _edge_ is either
#define POS_EDGE 0x01 // Capture when a positive edge on the channel input is detected
#define NEG_EDGE 0x02 // Capture when a negative edge on the channel input is detected
#define ANY_EDGE 0x03 // Capture when either a positive or a negative edge on the
// channel input is detected.
// Macro for enabling or disabling overflow interrupts of timer 1.
#define TIMER1_ENABLE_OVERFLOW_INT(val) \
(TIMIF = (val) ? TIMIF | 0x40 : TIMIF & ~0x40)
#if (chip == 2430 || chip == 2431)
/******************************************************************************
* @fn halSetTimer2Period
*
* @brief
* This function sets the period and overflow counter value of the MAC timer
* (timer 2). The timer can be set up with 320 u-second periods according to
* IEEE 802.15.4 or as a normal counter with 1 m-second period by using the
* option TIMER2_MAC_TIMER or TIMER2_NORMAL_TIMER respectively. The value of
* _period_ gives the number of periods (320 u-seconds or 1 m-seconds) to
* generate a compare event. The timer is set up to compensate for any clock
* division. The timer is also set up to be synchronised with the 32.768 KHz
* clock when entering or leaving power mode 0. When starting synchronously
* from power mode 1 or 2, the timer value is updated by adding the time
* passed since PM 0 was left. This time is kept by the 32.768 KHz clock.
* This way the time is kept as if the chip had been in power mode 0 the
* whole time. The timer must be started with the macro
* TIMER2_RUN(true) or MAC_TIMER_RUN(true). The macro TIMER2_INIT() should be
* run in advance to reset all register values.
*
* Parameters:
*
* @param byte mode
* Determines which time period Timer 2 is to use. The period of Timer 2
* is either 320 u-seconds (TIMER2_MAC_TIMER) or 1 m-second
* (TIMER2_NORMAL_TIMER).
* @param dword period
* This value indicates how many periods (320 u-second or 1 m-second) to
* pass before an overflow compare event is generated.
*
* @return bool
Returns 0 if period is too large, 1 otherwise.
*
******************************************************************************/
bool halSetTimer2Period(byte mode, dword period);
// _mode_ may be of the following:
#define TIMER2_MAC_TIMER 0x01 // Counts 320 u-second periods
#define TIMER2_NORMAL_TIMER 0x02 // Uses the timer as a normal timer with 1 m-second period.
// Macro for initialising timer 2
#define TIMER2_INIT() \
do { \
T2THD = 0x00; \
T2TLD = 0x00; \
T2CMP = 0x00; \
T2OF0 = 0x00; \
T2OF1 = 0x00; \
T2OF2 = 0x00; \
T2CAPHPH = 0x00; \
T2CAPLPL = 0x00; \
T2PEROF0 = 0x00; \
T2PEROF1 = 0x00; \
T2PEROF2 = 0x00; \
T2CNF = 0x06; \
} while (0)
#define TIMER2_ENABLE_OVERFLOW_COMP_INT(val) (T2PEROF2 = (val) ? T2PEROF2 | 0x20 : T2PEROF2 & ~0x20)
#endif
#if (chip == 1110 || chip == 1111 || chip == 2510 || chip == 2511)
/******************************************************************************
* @fn halSetTimer2Period
*
* @brief
* This function sets the period timer 2. The values for the counter,
* prescaler and tick period is calculated and written to the corresponding
* registers.
*
* Parameters:
*
* @param uint32 period
* Period of the timer in u-seconds.
* @param uint8* cnt
* The value written to T2CT (counter). This value is returned to enable
* a fast setup (without calculation) using the SET_TIMER2_COUNTER.
* @param uint8* presc
* The value written to T2PR (prescaler). This value is returned to enable
* a fast setup (without calculation) using the SET_TIMER2_PRESCALER.
*
* @return bool
Returns false if period is too large, true otherwise.
*
******************************************************************************/
bool halSetTimer2Period(uint32 period, uint8* cnt, uint8* presc);
#define TIMER2_INIT() \
do { \
T2CTL = 0x00; \
T2CT = 0x00; \
T2PR = 0x00; \
} while (0)
#define TIMER2_SET_COUNTER(counter) do{ T2CT = counter; }while(0)
#define TIMER2_SET_PRESCALER(prescaler) do{ T2PR = prescaler; }while(0)
#define TIMER2_SET_TICK_PERIOD(tick) do{ T2CTL = ((T2CTL & ~0x03) | tick); }while(0)
#define TIMER2_ENABLE_INTERRUPT() do{ T2CTL |= 0x10; }while(0)
#define TIMER2_DISABLE_INTERRUPT() do{ T2CTL &= ~0x10; }while(0)
#define TIMER2_CLEAR_EXPIRED() do{ T2CTL &= ~0x40; }while(0)
#define TIMER2_EXPIRED (T2CTL & 0x40)
#define TIMER2_SET_MODE(mode) (T2CTL = (mode) ? T2CTL|0x04 : T2CTL&~0x04)
#define TIMER2_USE_REG false
#define TIMER2_FREE true
#endif
/******************************************************************************
* @fn halSetTimer34Period
*
* @brief
* This function sets the period of timer 3 or 4 according to the value of
* _timer_. The two timers are identical. Clock division is used to fit the
* desired period within the timer range. If the period is too short or too
* long the function returns 0. If the period is successfully set, the
* function returns the byte value written to the timer register. This
* value can be used to set the pulse length if the timer is used for PWM.
* If _period_ is set to 0, maximum timeout value will be used.
*
* Parameters:
*
* @param byte timer
* Indicates which timer to configure. Must be either 3 or 4
* (0x03 or 0x04).
* @param dword period - Describe value.
* The desired period in microseconds.
*
* @return byte
* The value written to the TxCC0 register. The timer is incremented up
* to this value before the timer is reset. This value may be used to
* set the pulse length in PWM mode.
*
******************************************************************************/
byte halSetTimer34Period(byte timer, dword period);
// Macro for initialising timer 3 or 4
#define TIMER34_INIT(timer) \
do { \
T##timer##CTL = 0x06; \
T##timer##CCTL0 = 0x00; \
T##timer##CC0 = 0x00; \
T##timer##CCTL1 = 0x00; \
T##timer##CC1 = 0x00; \
} while (0)
// Macro for enabling overflow interrupt
#define TIMER34_ENABLE_OVERFLOW_INT(timer, val) \
(T##timer##CTL = (val) ? T##timer##CTL | 0x08 : T##timer##CTL & ~0x08)
// Macro for configuring channel 1 of timer 3 or 4 for PWM mode.
#define TIMER34_PWM_CONFIG(timer) \
do{ \
T##timer##CCTL1 = 0x24; \
if(timer == 3){ \
if(PERCFG & 0x20) { \
IO_FUNC_PORT_PIN(1,7,IO_FUNC_PERIPH); \
} \
else { \
IO_FUNC_PORT_PIN(1,4,IO_FUNC_PERIPH); \
} \
} \
else { \
if(PERCFG & 0x10) { \
IO_FUNC_PORT_PIN(2,3,IO_FUNC_PERIPH);\
} \
else { \
IO_FUNC_PORT_PIN(1,1,IO_FUNC_PERIPH); \
} \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -