📄 hal.h
字号:
#define INUM_T3 11
#define INUM_T4 12
#define INUM_P0INT 13
#define INUM_UTX1 14
#define INUM_P1INT 15
#define INUM_RF 16
#define INUM_WDT 17
#define NBR_OF_INTERRUPTS 18
#define INT_ENABLE_RF(on) { (on) ? (IEN2 |= 0x01) : (IEN2 &= ~0x01); }
#define INT_ENABLE_RFERR(on) { RFERRIE = on; }
#define INT_ENABLE_T2(on) { T2IE = on; }
#define INT_ENABLE_URX0(on) { URX0IE = on; }
#define INT_GETFLAG_RFERR() RFERRIF
#define INT_GETFLAG_RF() S1CON &= ~0x03
#define INT_SETFLAG_RFERR(f) RFERRIF= f
#define INT_SETFLAG_RF(f) { (f) ? (S1CON |= 0x03) : (S1CON &= ~0x03); }
#define INT_SETFLAG_T2(f) { T2IF = f; }
/******************************************************************************
******************* Common USART functions/macros *******************
******************************************************************************/
// The macros in this section are available for both SPI and UART operation.
//*****************************************************************************
// Example usage:
// USART0_FLUSH();
#define USART_FLUSH(num) (U##num##UCR |= 0x80)
#define USART0_FLUSH() USART_FLUSH(0)
#define USART1_FLUSH() USART_FLUSH(1)
// Example usage:
// if (USART0_BUSY())
// ...
#define USART_BUSY(num) (U##num##CSR & 0x01 == 0x01)
#define USART0_BUSY() USART_BUSY(0)
#define USART1_BUSY() USART_BUSY(1)
// Example usage:
// while(!USART1_BYTE_RECEIVED())
// ...
#define USART_BYTE_RECEIVED(num) ((U##num##CSR & 0x04) == 0x04)
#define USART0_BYTE_RECEIVED() USART_BYTE_RECEIVED(0)
#define USART1_BYTE_RECEIVED() USART_BYTE_RECEIVED(1)
// Example usage:
// if(USART1_BYTE_TRANSMITTED())
// ...
#define USART_BYTE_TRANSMITTED(num) ((U##num##CSR & 0x02) == 0x02)
#define USART0_BYTE_TRANSMITTED() USART_BYTE_TRANSMITTED(0)
#define USART1_BYTE_TRANSMITTED() USART_BYTE_TRANSMITTED(1)
/******************************************************************************
******************* USART-UART specific functions/macros *******************
******************************************************************************/
// The macros in this section simplify UART operation.
#define BAUD_E(baud, clkDivPow) ( \
(baud==2400) ? 6 +clkDivPow : \
(baud==4800) ? 7 +clkDivPow : \
(baud==9600) ? 8 +clkDivPow : \
(baud==14400) ? 8 +clkDivPow : \
(baud==19200) ? 9 +clkDivPow : \
(baud==28800) ? 9 +clkDivPow : \
(baud==38400) ? 10 +clkDivPow : \
(baud==57600) ? 10 +clkDivPow : \
(baud==76800) ? 11 +clkDivPow : \
(baud==115200) ? 11 +clkDivPow : \
(baud==153600) ? 12 +clkDivPow : \
(baud==230400) ? 12 +clkDivPow : \
(baud==307200) ? 13 +clkDivPow : \
0 )
#define BAUD_M(baud) ( \
(baud==2400) ? 59 : \
(baud==4800) ? 59 : \
(baud==9600) ? 59 : \
(baud==14400) ? 216 : \
(baud==19200) ? 59 : \
(baud==28800) ? 216 : \
(baud==38400) ? 59 : \
(baud==57600) ? 216 : \
(baud==76800) ? 59 : \
(baud==115200) ? 216 : \
(baud==153600) ? 59 : \
(baud==230400) ? 216 : \
(baud==307200) ? 59 : \
0)
//*****************************************************************************
// Macro for setting up a UART transfer channel. The macro sets the appropriate
// pins for peripheral operation, sets the baudrate, and the desired options of
// the selected uart. _uart_ indicates which uart to configure and must be
// either 0 or 1. _baudRate_ must be one of 2400, 4800, 9600, 14400, 19200,
// 28800, 38400, 57600, 76800, 115200, 153600, 230400 or 307200. Possible
// options are defined below.
//
// Example usage:
//
// UART_SETUP(0,115200,HIGH_STOP);
//
// This configures uart 0 for contact with "hyperTerminal", setting:
// Baudrate: 115200
// Data bits: 8
// Parity: None
// Stop bits: 1
// Flow control: None
//
#define UART_SETUP(uart, baudRate, options) \
do { \
if((uart) == 0){ \
if(PERCFG & 0x01){ \
P1SEL |= 0x30; \
} else { \
P0SEL |= 0x0C; \
} \
} \
else { \
if(PERCFG & 0x02){ \
P1SEL |= 0xC0; \
} else { \
P0SEL |= 0x30; \
} \
} \
\
U##uart##GCR = BAUD_E((baudRate),CLKSPD); \
U##uart##BAUD = BAUD_M(baudRate); \
\
U##uart##CSR |= 0x80; \
\
\
U##uart##UCR |= ((options) | 0x80); \
\
if((options) & TRANSFER_MSB_FIRST){ \
U##uart##GCR |= 0x20; \
} \
U##uart##CSR |= 0x40; \
} while(0)
//can do this via a macro
#define halSetBaud(baud) UART_SETUP(0, baud, HIGH_STOP);
// Options for UART_SETUP macro
#define FLOW_CONTROL_ENABLE 0x40
#define FLOW_CONTROL_DISABLE 0x00
#define EVEN_PARITY 0x20
#define ODD_PARITY 0x00
#define NINE_BIT_TRANSFER 0x10
#define EIGHT_BIT_TRANSFER 0x00
#define PARITY_ENABLE 0x08
#define PARITY_DISABLE 0x00
#define TWO_STOP_BITS 0x04
#define ONE_STOP_BITS 0x00
#define HIGH_STOP 0x02
#define LOW_STOP 0x00
#define HIGH_START 0x01
#define TRANSFER_MSB_FIRST 0x80
#define TRANSFER_MSB_LAST 0x00
#define UART_ENABLE_RECEIVE 0x40
// Example usage:
// if(UART0_PARERR())
// ...
#define UART_PARERR(num) ((U##num##CSR & 0x08) == 0x08)
#define UART0_PARERR() UART_PARERR(0)
#define UART1_PARERR() UART_PARERR(1)
// Example usage:
// if(UART1_FRAMEERR())
// ...
#define UART_FRAMEERR(num) ((U ##num## CSR & 0x10) == 0x10)
#define UART0_FRAMEERR() UART_FRAMEERR(0)
#define UART1_FRAMEERR() UART_FRAMEERR(1)
// Example usage:
// char ch = 'A';
// UART1_SEND(ch);
// ...
// UART1_RECEIVE(ch);
#define UART_SEND(num, x) U##num##DBUF = x
#define UART0_SEND(x) UART_SEND(0, x)
#define UART1_SEND(x) UART_SEND(1, x)
#define UART_RECEIVE(num, x) x = U##num##DBUF
#define UART0_RECEIVE(x) UART_RECEIVE(0, x)
#define UART1_RECEIVE(x) UART_RECEIVE(1, x)
/******************************************************************************
******************* Power and clock management ********************
*******************************************************************************
These macros are used to set power-mode, clock source and clock speed.
******************************************************************************/
// Macro for getting the clock division factor
#define CLKSPD (CLKCON & 0x07)
// Macro for getting the timer tick division factor.
#define TICKSPD ((CLKCON & 0x38) >> 3)
// Macro for checking status of the crystal oscillator
#define XOSC_STABLE (SLEEP & 0x40)
// Macro for checking status of the high frequency RC oscillator.
#define HIGH_FREQUENCY_RC_OSC_STABLE (SLEEP & 0x20)
// Macro for setting the 32 KHz clock source
#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
//changing to XOSC will take approx 150 us
#define SET_MAIN_CLOCK_SOURCE(source) \
do { \
if(source) { \
CLKCON |= 0x40; \
while(!HIGH_FREQUENCY_RC_OSC_STABLE); \
if(TICKSPD == 0){ \
CLKCON |= 0x08; \
} \
SLEEP |= 0x04; \
} \
else { \
SLEEP &= ~0x04; \
while(!XOSC_STABLE); \
asm("NOP"); \
CLKCON &= ~0x47; \
SLEEP |= 0x04; \
} \
}while (0)
/*
#### RADIO Support
*/
#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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -