⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hal.h

📁 SmartRF04EB+CC2431EM的测试代码
💻 H
📖 第 1 页 / 共 5 页
字号:
// Macro for checking status of the high frequency RC oscillator.
#define HIGH_FREQUENCY_RC_OSC_STABLE    (SLEEP & 0x20)

// Macro for setting power mode
#define SET_POWER_MODE(mode)    \
   do {                         \
      SLEEP &= ~0x03;           \
      SLEEP |= mode;            \
      PCON |= 0x01;             \
   }while (0)

// Where _mode_ is one of
#define POWER_MODE_0  0x00  // Clock oscillators on, voltage regulator on
#define POWER_MODE_1  0x01  // 32.768 KHz oscillator on, voltage regulator on
#define POWER_MODE_2  0x02  // 32.768 KHz oscillator on, voltage regulator off
#define POWER_MODE_3  0x03  // All clock oscillators off, voltage regulator off

// Macro for setting the 32 kHz clock source
// Please not that this macro only can be run when the device run on the RC osc
#define SET_32KHZ_CLOCK_SOURCE(source) \
   do {                                \
      if( source ) {                   \
         CLKCON |= 0x80;               \
      } else {                         \
         CLKCON &= ~0x80;              \
      }                                \
   } while (0)

// Where _source_ is one of
#define CRYSTAL 0x00
#define RC      0x01

// Macro for setting the main clock oscillator source,
// turns off the clock source not used. Settings TICKSPD
// equal CLKSPD
// Use this for CC2430 rev. B
#define REV_B_SET_MAIN_CLOCK_SOURCE(source)   \
   do {                                       \
      if(source) {                            \
        CLKCON |= 0x40;                       \
        while(!HIGH_FREQUENCY_RC_OSC_STABLE); \
        SLEEP |= 0x04;                        \
      }                                       \
      else {                                  \
        SLEEP &= ~0x04;                       \
        while(!XOSC_STABLE);                  \
        asm("NOP");                           \
        CLKCON &= ~0x7F;                      \
        SLEEP |= 0x04;                        \
      }                                       \
   }while (0)

// Use this for CC2430 rev. C ->
#define REV_C_SET_MAIN_CLOCK_SOURCE(source)   \
   do {                                       \
      if(source) {                            \
        CLKCON |= 0x40;                       \
        while( !(CLKCON & 0x40) );            \
      }                                       \
      else {                                  \
        CLKCON &= ~0x7F;                      \
        while( CLKCON & 0x40 );               \
      }                                       \
   }while (0)

#define SET_MAIN_CLOCK_SOURCE( source )       \
  do {                                        \
    if( CHVER < REV_C )                       \
      REV_B_SET_MAIN_CLOCK_SOURCE( source );  \
    else                                      \
      REV_C_SET_MAIN_CLOCK_SOURCE( source );  \
  }while(0)



/******************************************************************************
*******************           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) )

// 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##H = HIBYTE( value );             \
      T1CC##channel##L = LOBYTE( 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)

// _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)

// 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); \
         }                                        \
      }                                           \
   } while(0)

// Macro for setting pulse length of the timer in PWM mode
#define TIMER34_SET_PWM_PULSE_LENGTH(timer, value) \
   do {                                            \
      T##timer##CC1 = (byte)value;                 \
   } while (0)


// Macros for turning timers on or off
#define TIMER1_RUN(value)      (T1CTL = (value) ? T1CTL | 0x02 : T1CTL & ~0x03)
#define TIMER2_RUN(value)      (T2CNF = (value) ? T2CNF | 0x01  : T2CNF & ~0x01)
// MAC-timer == timer 2
#define MAC_TIMER_RUN(value)   do{ TIMER2_RUN(value); }while(0)
#define TIMER3_RUN(value)      (T3CTL = (value) ? T3CTL | 0x10 : T3CTL & ~0x10)
#define TIMER4_RUN(value)      (T4CTL = (value) ? T4CTL | 0x10 : T4CTL & ~0x10)

// Macro for enabling/ disabling interrupts from the channels of timer 1, 3 or 4.
#define TIMER_CHANNEL_INTERRUPT_ENABLE(timer, channel, value) \
   do{                                                        \
      if(value){                                              \
         T##timer##CCTL##channel## |= 0x40;                   \
      } else {                                                \
         T##timer##CCTL##channel## &= ~0x40;                  \
      }                                                       \
   } while(0)

/******************************************************************************
*******************          Watch Dog Timer (WDT)          *******************
*******************************************************************************

The WDT may be used to prevent the unit from being trapped in a system
stalemate, i.e. an endless waiting state. The WDT must be reset before it times
out. If a timeout occurs, the system is reset.

The WDT can also be configured as a normal timer which generates interrupt at
each timeout. This must be configured manually.
******************************************************************************/

// Macro for turning on the WDT
#define WDT_ENABLE()   WDCTL |= 0x08

// Macro for setting the WDT timeout interval
#define WDT_SET_TIMEOUT_PERIOD(timeout) \
   do {                                 \
       WDCTL &= ~0x03;                  \
       WDCTL |= timeout;                \
   } while (0)

// Where _timeout_ is one of
#define SEC_1          0x00     // after 1 second
#define M_SEC_250      0x01     // after 250 ms
#define M_SEC_15       0x02     // after 15 ms
#define M_SEC_2        0x03     // after 2 ms

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -