📄 hal.h
字号:
/*******************************************************************************************************
* *
* All HAL library macros and constants are defined here. *
*******************************************************************************************************
* Compiler: AVR-GCC *
* Target platform: CCxx00 + any ATMEGA MCU *
* Author: WangChaoyan
*******************************************************************************************************
* Revision history: *
*
*
*******************************************************************************************************/
#ifndef HAL_H
#define HAL_H
/*******************************************************************************************************
*******************************************************************************************************
************************** AVR<->CCxx00 SPI INTERFACE **************************
*******************************************************************************************************
*******************************************************************************************************/
//-------------------------------------------------------------------------------------------------------
// Initialization
//SPI ENABLE MSTR置位主机模式 SPR1=0,SPR0=0,Focr/4
#define SPI_INIT() \
do { \
SPCR = BM(SPE) | BM(MSTR);
} while (0)
//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------
// FAST SPI: Low level functions
// x = value (BYTE or WORD)
// p = pointer to the byte array to operate on
// c = the byte count
//
// SPI_ENABLE() and SPI_DISABLE() are located in the devboard header file (CS_N uses GPIO)
#define FASTSPI_WAIT() \
do { \
while (!(SPSR & BM(SPIF))); \
} while (0)
#define FASTSPI_TX(x) \
do { \
SPDR = x; \
FASTSPI_WAIT(); \
} while (0)
#define FASTSPI_RX(x) \
do { \
SPDR = 0; \
FASTSPI_WAIT(); \
x = SPDR; \
} while (0)
//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------
// FAST SPI: Register access
// s = command strobe
// a = register address
// v = register value
#define FASTSPI_STROBE(s) \
do { \
SPI_ENABLE(); \
FASTSPI_TX_ADDR(s); \
SPI_DISABLE(); \
} while (0)
//-------------------------------------------------------------------------------------------------------
/*******************************************************************************************************
*******************************************************************************************************
************************** INTERRUPTS **************************
*******************************************************************************************************
*******************************************************************************************************/
//-------------------------------------------------------------------------------------------------------
// General
#define ENABLE_GLOBAL_INT() do { asm ("sei\n\t" ::); } while (0)
#define DISABLE_GLOBAL_INT() do { asm ("cli\n\t" ::); } while (0)
//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------
// UART1 interrupts
#define ENABLE_UART1_INT() do { UCSR1B |= (BM(UDRIE1) | BM(RXCIE1)); } while (0)
#define DISABLE_UART1_INT() do { UCSR1B &= ~(BM(UDRIE1) | BM(RXCIE1)); } while (0)
#define ENABLE_UART1_TX_INT() do { UCSR1B |= BM(UDRIE1); } while (0)
#define DISABLE_UART1_TX_INT() do { UCSR1B &= ~BM(UDRIE1); } while (0)
#define CLEAR_UART1_TX_INT() do { UCSR1A &= ~BM(UDRE1); } while (0)
#define SET_UART1_TX_INT() do { UCSR1A |= BM(UDRE1); } while (0)
#define ENABLE_UART1_RX_INT() do { UCSR1A &= ~BM(RXC1); UCSR1B |= BM(RXCIE1); } while (0)
//#define ENABLE_UART1_RX_INT() do { UCSR1B |= BM(RXCIE1); } while (0)
#define DISABLE_UART1_RX_INT() do { UCSR1B &= ~BM(RXCIE1); } while (0)
#define CLEAR_UART1_RX_INT() do { UCSR1A &= ~BM(RXC1); } while (0)
//-------------------------------------------------------------------------------------------------------
/*******************************************************************************************************
*******************************************************************************************************
************************** SERIAL PORT (UART1) **************************
*******************************************************************************************************
*******************************************************************************************************/
//-------------------------------------------------------------------------------------------------------
// INIT_UART1(baudRate,options)
//
// DESCRIPTION:
// A macro which does all the initialization necessary to communicate on UART 1. The UART is
// configured according to options (defined below). Note that this macro does not call
// ENABLE_UART1().
//
// ARGUMENTS:
// baudRate
// One of the UART_BAUDRATE_... constants defined below
// options
// One or more of the UART_OPT constants defined below. The value 0 gives one stop bit, no
// parity and 5 bits per char.
//-------------------------------------------------------------------------------------------------------
#define INIT_UART1(baudRate,options) \
do \
{ \
UBRR1H=(baudRate)>>8; \
UBRR1L=(baudRate); \
UCSR1C= (options | 0x80);\
} while (0)
// Baud rate codes for use with the INIT_UART1 macro
#define UART_BAUDRATE_9K6 0x33 // Normal speed, Clk = 8.0MHz
// Options for use with the INIT_UART1 macro
#define UART_OPT_ONE_STOP_BIT 0
#define UART_OPT_TWO_STOP_BITS 0x08
#define UART_OPT_NO_PARITY 0
#define UART_OPT_EVEN_PARITY 0x20
#define UART_OPT_ODD_PARITY 0x30
#define UART_OPT_5_BITS_PER_CHAR 0
#define UART_OPT_6_BITS_PER_CHAR 0x02
#define UART_OPT_7_BITS_PER_CHAR 0x04
#define UART_OPT_8_BITS_PER_CHAR 0x06
//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------
// Enable/disable macros
// Enable/disable UART1
#define ENABLE_UART1() (UCSR1B |= (BM(RXEN1) | BM(TXEN1)))
#define DISABLE_UART1() (UCSR1B &= ~(BM(RXEN1) | BM(TXEN1)))
#define ENABLE_UART1_RX() (UCSR1B |= (BM(RXEN1)))
#define DISABLE_UART1_RX() (UCSR1B &= ~(BM(RXEN1)))
//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------
// Macros which are helful when transmitting and receiving data over the serial interface.
//
// Example of usage:
// UART1_SEND(pData[0]);
// for (i = 1; i < len; i++) {
// UART1_WAIT_AND_SEND(pData[i]);
// }
#define UART1_WAIT() do { while (!(UCSR1A & BM(UDRIE1))); CLEAR_UART1_TX_INT(); } while (0)
#define UART1_SEND(x) do { UDR1 = (x); } while (0)
#define UART1_WAIT_AND_SEND(x) do { UART1_WAIT(); UART1_SEND(x); } while (0)
#define UART1_RECEIVE(x) do { (x) = UDR1; } while (0)
#define UART1_WAIT_AND_RECEIVE(x) do { UDR1 = 0; UART1_WAIT(); UART1_RX(x); } while (0)
//-------------------------------------------------------------------------------------------------------
/*******************************************************************************************************
*******************************************************************************************************
************************** USEFUL STUFF **************************
*******************************************************************************************************
*******************************************************************************************************/
//-------------------------------------------------------------------------------------------------------
// Useful stuff
#define NOP() asm volatile ("nop\n\t" ::)
//-------------------------------------------------------------------------------------------------------
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -