📄 hal.h
字号:
// Macro for resetting the WDT. If this is not done before the WDT times out,
// the system is reset
#define WDT_RESET() \
do { \
WDCTL = (WDCTL & ~0xF0) | 0xA0; \
WDCTL = (WDCTL & ~0xF0) | 0x50; \
} while (0)
/******************************************************************************
******************* ADC macros/functions *******************
*******************************************************************************
These functions/macros simplifies usage of the ADC.
******************************************************************************/
// Macro for setting up a single conversion. If ADCCON1.STSEL = 11, using this
// macro will also start the conversion.
#define ADC_SINGLE_CONVERSION(settings) \
do{ \
ADCCON3 = (settings); \
}while(0)
// Macro for setting up a single conversion
#define ADC_SEQUENCE_SETUP(settings) \
do{ \
ADCCON2 = (settings); \
}while(0)
// Where _settings_ are the following:
// Reference voltage:
#define ADC_REF_1_25_V 0x00 // Internal 1.25V reference
#define ADC_REF_P0_7 0x40 // External reference on AIN7 pin
#define ADC_REF_AVDD 0x80 // AVDD_SOC pin
#define ADC_REF_P0_6_P0_7 0xC0 // External reference on AIN6-AIN7 differential input
// Resolution (decimation rate):
#define ADC_8_BIT 0x00 // 64 decimation rate
#define ADC_10_BIT 0x10 // 128 decimation rate
#define ADC_12_BIT 0x20 // 256 decimation rate
#define ADC_14_BIT 0x30 // 512 decimation rate
// Input channel:
#define ADC_AIN0 0x00 // single ended P0_0
#define ADC_AIN1 0x01 // single ended P0_1
#define ADC_AIN2 0x02 // single ended P0_2
#define ADC_AIN3 0x03 // single ended P0_3
#define ADC_AIN4 0x04 // single ended P0_4
#define ADC_AIN5 0x05 // single ended P0_5
#define ADC_AIN6 0x06 // single ended P0_6
#define ADC_AIN7 0x07 // single ended P0_7
#define ADC_GND 0x0C // Ground
#define ADC_TEMP_SENS 0x0E // on-chip temperature sensor
#define ADC_VDD_3 0x0F // (vdd/3)
// Macro for starting the ADC in continuous conversion mode
#define ADC_SAMPLE_CONTINUOUS() \
do { \
ADCCON1 &= ~0x30; \
ADCCON1 |= 0x10; \
} while (0)
// Macro for stopping the ADC in continuous mode
#define ADC_STOP() \
do { \
ADCCON1 |= 0x30; \
} while (0)
// Macro for initiating a single sample in single-conversion mode (ADCCON1.STSEL = 11).
#define ADC_SAMPLE_SINGLE() \
do{ \
ADC_STOP(); \
ADCCON1 |= 0x40; \
} while (0)
// Macro for configuring the ADC to be started from T1 channel 0. (T1 ch 0 must be in compare mode!!)
#define ADC_TRIGGER_FROM_TIMER1() \
do { \
ADC_STOP(); \
ADCCON1 &= ~0x10; \
} while (0)
// Expression indicating whether a conversion is finished or not.
#define ADC_SAMPLE_READY() (ADCCON1 & 0x80)
// Macro for setting/clearing a channel as input of the ADC
#define ADC_ENABLE_CHANNEL(ch) ADCCFG |= (0x01 << ch)
#define ADC_DISABLE_CHANNEL(ch) ADCCFG &= ~(0x01 << ch)
#define ADC_GET_VALUE( v ) GET_WORD( ADCH, ADCL, v )
/******************************************************************************
******************* RF communication functions/macros *******************
*******************************************************************************
The functions in this section are designed to simplify usage of the radio.
A function for setup, transmitting and receiption are included. In addition,
macros for writing instructions to the Command Stobe Processor are included.
******************************************************************************/
#define STOP_RADIO() ISRFOFF;
// RF interrupt flags
#define IRQ_RREG_ON 0x80
#define IRQ_TXDONE 0x40
#define IRQ_FIFOP 0x20
#define IRQ_SFD 0x10
#define IRQ_CCA 0x08
#define IRQ_CSP_WT 0x04
#define IRQ_CSP_STOP 0x02
#define IRQ_CSP_INT 0x01
// RF status flags
#define TX_ACTIVE_FLAG 0x10
#define FIFO_FLAG 0x08
#define FIFOP_FLAG 0x04
#define SFD_FLAG 0x02
#define CCA_FLAG 0x01
// Radio status states
#define TX_ACTIVE (RFSTATUS & TX_ACTIVE_FLAG)
#define FIFO (RFSTATUS & FIFO_FLAG)
#define FIFOP (RFSTATUS & FIFOP_FLAG)
#define SFD (RFSTATUS & SFD_FLAG)
#define CCA (RFSTATUS & CCA_FLAG)
// Various radio settings
#define ADR_DECODE 0x08
#define AUTO_CRC 0x20
#define AUTO_TX2RX_OFF 0x08
#define RX2RX_TIME_OFF 0x04
#define ACCEPT_ACKPKT 0x01
//-----------------------------------------------------------------------------
// Command Strobe Processor (CSP) instructions
//-----------------------------------------------------------------------------
#define DECZ do{RFST = 0xBF; }while(0)
#define DECY do{RFST = 0xBE; }while(0)
#define INCY do{RFST = 0xBD; }while(0)
#define INCMAXY(m) do{RFST = (0xB8 | m); }while(0) // m < 8 !!
#define RANDXY do{RFST = 0xBC; }while(0)
#define INT do{RFST = 0xB9; }while(0)
#define WAITX do{RFST = 0xBB; }while(0)
#define WAIT(w) do{RFST = (0x80 | w); }while(0) // w < 64 !!
#define WEVENT do{RFST = 0xB8; }while(0)
#define LABEL do{RFST = 0xBA; }while(0)
#define RPT(n,c) do{RFST = (0xA0 | (n << 3) | c); }while(0) // n = TRUE/FALSE && (c < 8)
#define SKIP(s,n,c) do{RFST = ((s << 4) | (n << 3) | c); }while(0) // && (s < 8)
#define STOP do{RFST = 0xDF; }while(0)
#define SNOP do{RFST = 0xC0; }while(0)
#define STXCALN do{RFST = 0xC1; }while(0)
#define SRXON do{RFST = 0xC2; }while(0)
#define STXON do{RFST = 0xC3; }while(0)
#define STXONCCA do{RFST = 0xC4; }while(0)
#define SRFOFF do{RFST = 0xC5; }while(0)
#define SFLUSHRX do{RFST = 0xC6; }while(0)
#define SFLUSHTX do{RFST = 0xC7; }while(0)
#define SACK do{RFST = 0xC8; }while(0)
#define SACKPEND do{RFST = 0xC9; }while(0)
#define ISSTOP do{RFST = 0xFF; }while(0)
#define ISSTART do{RFST = 0xFE; }while(0)
#define ISTXCALN do{RFST = 0xE1; }while(0)
#define ISRXON do{RFST = 0xE2; }while(0)
#define ISTXON do{RFST = 0xE3; }while(0)
#define ISTXONCCA do{RFST = 0xE4; }while(0)
#define ISRFOFF do{RFST = 0xE5; }while(0)
#define ISFLUSHRX do{RFST = 0xE6; }while(0)
#define ISFLUSHTX do{RFST = 0xE7; }while(0)
#define ISACK do{RFST = 0xE8; }while(0)
#define ISACKPEND do{RFST = 0xE9; }while(0)
// Conditions _c_ for the RPT and SKIP instructions of the CSP
#define CCA_TRUE 0x00;
#define RECEIVING 0x01;
#define MCU_BIT_IS_1 0x02;
#define COMMAND_BUF_EMPT 0x03;
#define REGX_IS_0 0x04;
#define REGY_IS_0 0x05;
#define REGZ_IS_0 0x06;
#define NO_OP 0x07;
/******************************************************************************
******************* Memory space mapping macros/functions ********************
*******************************************************************************/
// Macros for enabling or disabling unified code space.
// Unified code space is generally used when executing programs from RAM.
#define ENABLE_UINIFIED_CODE_SPACE() do { MEMCTR |= 0x40; } while (0)
#define DISABLE_UINIFIED_CODE_SPACE() do { MEMCTR &= ~0x40; } while (0)
/******************************************************************************
******************* AES encryption / decryption functions *******************
*******************************************************************************
Functions for performing a encryption or decryption using the Advanced
Encryption Standard. A unique 16 byte key must be loaded prior to the
encryption or decryption process.
The Direct Memory Access (DMA) may also be used for data transfer to and from
the AES module.
******************************************************************************/
#define AES_BUSY 0x08
#define ENCRYPT 0x00
#define DECRYPT 0x01
// Macro for setting the mode of the AES operation
#define AES_SETMODE(mode) \
do { \
ENCCS &= ~0x70; \
ENCCS |= mode; \
} while (0)
// _mode_ is one of
#define CBC 0x00
#define CFB 0x10
#define OFB 0x20
#define CTR 0x30
#define ECB 0x40
#define CBC_MAC 0x50
// Macro for starting or stopping encryption or decryption
#define AES_SET_ENCR_DECR_KEY_IV(mode) \
do { \
ENCCS &= ~0x07; \
ENCCS |= mode; \
} while(0)
// Where _mode_ is one of
#define AES_ENCRYPT 0x00
#define AES_DECRYPT 0x02
#define AES_LOAD_KEY 0x04
#define AES_LOAD_IV 0x06
// Macro for starting the AES module for either encryption, decryption,
// key or initialisation vector loading.
#define AES_START() ENCCS |= 0x01
/******************************************************************************
************* Random Generator macros / functions ************
*******************************************************************************
Example usage:
First initialise the random generator:
halInitRandomGenerator();
Each time the random generator is read, the following sequence should
be used (clock the random generator for each read):
CLOCK_RANDOM_GENERATOR();
rnd = GET_RANDOM_VALUE();
******************************************************************************/
// Macro for enabling the random generator
#define ENABLE_RANDOM_GENERATOR() \
do{ ADCCON1 &= ~0x0C; } while(0)
// Macro for clocking the random generator
#define CLOCK_RANDOM_GENERATOR() \
do{ ADCCON1 |= 0x04; } while(0)
// Macro for getting a random byte
#define GET_RANDOM_BYTE(a) \
do{ \
CLOCK_RANDOM_GENERATOR(); \
a = RNDH; \
}while(0)
// Macro for getting a random word
#define GET_RANDOM_WORD(a) \
do{ \
CLOCK_RANDOM_GENERATOR(); \
GET_WORD( RNDH, RNDL, a ); \
}while(0)
#endif //HAL_H
/*------------------------------------------------------------------------------
0ooo
ooo0 ( )
( ) ) /
\ ( (_/
\_) By:cuiqingwei [gary]
------------------------------------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -