📄 hal.h
字号:
(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; \
} \
} while(0)
// 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)
/******************************************************************************
******************* USART-SPI specific functions/macros *******************
******************************************************************************/
// The macros in this section simplify SPI operation.
//*****************************************************************************
// Macro for setting up an SPI connection. The macro configures the appropriate
// pins for peripheral operation, sets the baudrate if the chip is configured
// to be SPI master, and sets the desired clock polarity and phase. Whether to
// transfer MSB or LSB first is also determined. _spi_ indicates whether
// to use spi 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.
#define SPI_SETUP(spi, baudRate, options) \
do { \
U##spi##UCR = 0x80; \
U##spi##CSR = 0x00; \
\
if(spi == 0){ \
if(PERCFG & 0x01){ \
P1SEL |= 0x3C; \
} else { \
P0SEL |= 0x3C; \
} \
} \
else { \
if(PERCFG & 0x02){ \
P1SEL |= 0xF0; \
} else { \
P0SEL |= 0x3C; \
} \
} \
\
if(options & SPI_SLAVE){ \
U##spi##CSR = 0x20; \
} \
else { \
U##spi##GCR = BAUD_E(baudRate, CLKSPD); \
U##spi##BAUD = BAUD_M(baudRate); \
} \
U##spi##GCR |= (options & 0xE0); \
} while(0)
// Options for the SPI_SETUP macro.
#define SPI_SLAVE 0x01
#define SPI_MASTER 0x00
#define SPI_CLOCK_POL_LO 0x00
#define SPI_CLOCK_POL_HI 0x80
#define SPI_CLOCK_PHA_0 0x00
#define SPI_CLOCK_PHA_1 0x40
#define SPI_TRANSFER_MSB_FIRST 0x20
#define SPI_TRANSFER_MSB_LAST 0x00
/******************************************************************************
******************* FLASH programming functions *******************
*******************************************************************************
_halFlashWritePage(...)_ writes a whole flash page. Because code memory cannot
be read during flash write, the writing routines are copied to XDATA RAM. The
function is implemented in assembly code with file extensions .s51 rather than .c
The Direct Memory Access (DMA) may also be used for flash write.
******************************************************************************/
//Macro for erasing a given flash page
#define FLASH_ERASE_PAGE(page) \
do{ \
FADDRH = (page) << 1; \
FADDRL = 0x00; \
FLASH_CONFIG(ERASE); \
}while (0)
// Macro for configuring flash access and setting flash access mode.
#define FLASH_CONFIG(options) \
do { \
FWT = ( 0x2A >> CLKSPD ); \
FCTL = options; \
} while (0)
// _options_ may be the following:
#define READ_WHEN_NEED 0x00
#define CONTINOUS_READ 0x10
#define WRITE 0x02
#define ERASE 0x01
#define FLASH_BUSY 0x80
/******************************************************************************
* @fn halFlashWritePage
*
* @brief
* This function writes a byte field in XDATA RAM to a given flash
* page. Normal program execution is run from flash. However during flash
* write, flash memory is not available for reading. To circumvent this
* problem the core operation of this procedure, namely the actual flash
* write procedure, is copied to XDATA RAM and run from there. The flash
* write procedure is copied to a 35 byte XDATA RAM buffer.
* Prior to a write the page is erased.
*
* This function disables interrupts when running, and re-enables interrupt
* if interrupts were enabled at function entry.
*
* Parameters:
*
* @param BYTE* pSrcAddr
* Pointer to first byte in xdata space which is to be written to
* flash. The number of bytes a flash page consists of starting from
* this address will be written to the page _page_.
* @param BYTE* pBuffer
* Pointer to a buffer of 35 bytes in XDATA RAM to which the flash
* write procedure
* can be copied.
* @param BYTE page
* Indicates which of the flash pages the data is to be written to.
*
* @return void
*
******************************************************************************/
void halFlashWritePage(BYTE *pSrcAddr, BYTE *pBuffer, BYTE page);
/******************************************************************************
* @fn halFlashErasePage
*
* @brief
* This function erases a given flash page.
*
* This function disables interrupts when running, and re-enables interrupt
* if interrupts were enabled at function entry.
*
* Parameters:
*
* @param BYTE* pBuffer
* Pointer to a buffer of 10 bytes in XDATA RAM to which the flash
* erase procedure can be copied.
* @param BYTE page
* Indicates which of the flash pages is to be erased.
*
* @return void
*
******************************************************************************/
void halFlashErasePage(BYTE* buffer, BYTE page);
/******************************************************************************
******************* 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 power mode
#define SET_POWER_MODE(mode) \
do { \
if(mode == 0) { SLEEP &= ~0x03; } \
else if (mode == 3) { SLEEP |= 0x03; } \
else { SLEEP &= ~0x03; SLEEP |= mode; } \
PCON |= 0x01; \
asm("NOP"); \
}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
#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; \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -