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

📄 hal.h

📁 ti-Chipcon CC1010 1G以下Soc源码库。包括rf,powermodes,clockmodes,flashRW,interrupts,timer,pwm,uart...所有底层驱动源码
💻 H
📖 第 1 页 / 共 5 页
字号:
            RTCIF               :       \
        0                               \
    )


// Macro used together with the INUM_* constants defined in Reg1010.h to set
// or clear certain interrupt flags. E.g:
//          INT_SETFLAG(INUM_RF, INT_SET;
//          INT_SETFLAG(INUM_UART0_RX, INT_CLR);
#define INT_SETFLAG(inum, f)            \
    do {                                \
        if (inum==INUM_EXTERNAL0)       \
            (IE0=(f));                  \
        else if (inum==INUM_EXTERNAL1)  \
            (IE1=(f));                  \
        else if (inum==INUM_TIMER0)     \
            (TF0=(f));                  \
        else if (inum==INUM_TIMER1)     \
            (TF1=(f));                  \
        else if (inum==INUM_TIMER2)     \
            {if (f) EXIF|=0x20; else EXIF&=~0x20;} \
        else if (inum==INUM_TIMER3)     \
            {if (f) EXIF|=0x80; else EXIF&=~0x80;} \
        else if (inum==INUM_UART0_RX)   \
            (RI_0=(f));                 \
        else if (inum==INUM_UART0_TX)   \
            (TI_0=(f));                 \
        else if (inum==INUM_UART1_RX)   \
            (RI_1=(f));                 \
        else if (inum==INUM_UART1_TX)   \
            (TI_1=(f));                 \
        else if (inum==INUM_FLASH)      \
            (FDIF=(f));                 \
        else if (inum==INUM_RF)         \
            {if (f) EXIF|=0x10; else EXIF&=~0x10;} \
        else if (inum==INUM_DES_ADC)    \
            {if (f) EXIF|=0x40; else EXIF&=~0x40;} \
        else if (inum==INUM_DES)        \
            {if (f) CRPCON|=0x20; else CRPCON&=~0x20;} \
        else if (inum==INUM_ADC)        \
            {if (f) ADCON2|=0x40; else ADCON2&=~0x40;} \
        else if (inum==INUM_RTC)        \
            (RTCIF=(f));                \
    } while (0)


// Macros used to set edge/level trigging of the external interrupts.
#define INT_EXTERNAL0_TRIGGER_ON_EDGE()     (IT0=1)
#define INT_EXTERNAL0_TRIGGER_ON_LEVEL()    (IT0=0)
#define INT_EXTERNAL1_TRIGGER_ON_EDGE()     (IT1=1)
#define INT_EXTERNAL1_TRIGGER_ON_LEVEL()    (IT1=0)
//----------------------------------------------------------------------------




/*****************************************************************************
 *****************************************************************************
 *************           Serial port functions/macros            *************
 *****************************************************************************
 *****************************************************************************/

///////////////////////////////////////////////////////////////////////////////
// A number of macros have been defined which allow easy use of the serial
// ports in the CC1010:
//   UART0_SETUP(...)           - Used to configure the serial ports (baudrate,
//   UART1_SETUP(...)             8-bit / 8-bit + parity, interrupt serviced or
//                                polled, RX/TX or just TX.) These functions
//                                also configure pins and interrupts properly.
//   UART0_CLOSE()              - Used to close a serial port after use. Turns
//   UART1_CLOSE()                off baud generation, reverts pins to inputs
//                                and disables relevant interrupts.
//   UART0_SEND(...)            - Used to send a single byte with the relevant
//   UART0_SEND(...)              UART in 8-bit mode. No check is made to see 
//                                if the transmit buffer is empty.
//   UART0_RECEIVE(...)         - Used to receive a single byte with the
//   UART1_RECEIVE(...)           relevant UART in 8-bit mode. No check is made
//                                whether a new byte has been received.
//   UART0_SEND_WITH_PARITY(x)  - Used to send a single byte in the 8-bit + 
//   UART1_SEND_WITH_PARITY(x)    parity mode. No check is made to see if the
//                                transmit buffer is empty.
//   UART0_PARITY_OK()          - Checks to see if the parity of the most 
//   UART1_PARITY_OK()            recently received byte is OK. Returns TRUE or
//                                FALSE.
//   UART0_WAIT_AND_SEND(x)     - Waits until the transmit buffer has been 
//   UART1_WAIT_AND_SEND(x)       emptied (and thus sent) and then sends x 
//                                using the 8-bit mode.
//   UART0_WAIT_AND_RECEIVE(x)  - Waits until a byte is received and then
//   UART1_WAIT_AND_RECEIVE(x)    stores it in x. The 8-bit mode is used.
//
// In addition the functions printf() / scanf() can be used in conjunction
// with UART0 as long as UART0_SETUP(...) is passed the parameter UART_POLLED.
// The printf() / scanf() functions use the putchar() and _getkey() functions
// in libc for actual input and output.
///////////////////////////////////////////////////////////////////////////////
                                  
// Use counter for timer1
extern byte halTimer1Usage;

//----------------------------------------------------------------------------
//  UART0_SETUP(baudRate, clkFreq, options) / UART1_SETUP(...)
//
//  Description:
//      Macro which does all the initialization necessary to establish a 
//      simple serial link on serial port 0/1. Timer1 is used as a baudrate
//      generator and is initialized according to _baudRate_ and _clkFreq_. 
//      The ports required for serial communication (UART0: P3.0 for RX, P3.1 
//      for TX; UART1: P2.0 for RX, P2.1 for TX) are configured as necessary. 
//      The UART is configured according to _options_, the different values 
//      of which are listed below.
//      The first byte is transmitted simply by using the UART0_SEND(...)/
//      UART1_SEND(...) macros.
//
//  Arguments:
//      word baudRate
//          Baudrate (9600, 19200, 57600, ...)
//      word clkFreq
//          Device clock frequency in kHz
//      byte options
//          One or more of the below constants. The value 0 gives the common 
//          setting UART_NO_PARITY | UART_TX_ONLY | UART_ISR.
//----------------------------------------------------------------------------
// options is one or more of these constants:
#define UART_NO_PARITY      0       // 8 data bits, no parity
#define UART_9TH_BIT_PARITY 0x40    // 8 data bits, ninth bit parity
#define UART_RX_TX          0x10    // Receive and transmit
#define UART_TX_ONLY        0       // Transmit only
#define UART_ISR            0       // Use interrupt service routines to 
                                    // signal that a byte has arrived or
                                    // that the TX buffer is empty.
                                    // The user must manually clear RI_0 and
                                    // TI_0 in the corresponding ISRs.
#define UART_POLLED         0x02    // User must poll the RI_0 and TI_0 flags
                                    // to determine when a byte arrives or when
                                    // the TX buffer is empty. The user must
                                    // manually clear RI_0 and TI_0 if not using
                                    // the printf() / scanf() functions which do
                                    // this automatically.

#define UART0_SETUP(baudRate, clkFreq, options)         \
    do {                                                \
        /* Setup pins for UART0 */                      \
        if (!((options)&UART_RX_TX))                    \
            PORTDIRBIT(3, 0, PIN);                      \
        PORTDIRBIT(3, 1, POUT);                         \
        PORTBIT(3, 1)=1;                                \
                                                        \
        /* Setup baudrate & timer 1 */                  \
        CKCON|=0x10;                /* T1M=1 */         \
        PCON|=0x80;                 /* SMOD0=1 */       \
        TH1=256 - ((((ulong)(clkFreq)*10)/((ulong)(baudRate)/100*32) +1)/2);  \
        TR1=1;                                          \
        TMOD=(TMOD&0x0F)|0x20;                          \
        halTimer1Usage++;                               \
                                                        \
        /* Setup SCON0 & interrupts */                  \
        SCON0=(options)+0x40;                           \
        if (!((options)&UART_POLLED)) {                 \
            ES0=1;                                      \
            EA=1;                                       \
        }                                               \
    } while(0)
                                                        
#define UART1_SETUP(baudRate, clkFreq, options)         \
    do {                                                \
        /* Setup pins for UART1 */                      \
        if (!((options)&UART_RX_TX))                    \
            PORTDIRBIT(2, 0, PIN);                      \
        PORTDIRBIT(2, 1, POUT);                         \
        PORTBIT(2, 1)=1;                                \
                                                        \
        /* Setup baudrate & timer 1 */                  \
        CKCON|=0x10;                /* T1M=1 */         \
        EICON|=0x80;                /* SMOD1=1 */       \
        TH1=256 - ((((ulong)(clkFreq)*10)/((ulong)(baudRate)/100*32) +1)/2);  \
        TR1=1;                                          \
        TMOD=(TMOD&0x0F)|0x20;                          \
        halTimer1Usage++;                               \
                                                        \
        /* Setup SCON1 & interrupts */                  \
        SCON1=(options)+0x40;                           \
        if (!((options)&UART_POLLED)) {                 \
            ES1=1;                                      \
            EA=1;                                       \
        }                                               \
    } while(0)
//----------------------------------------------------------------------------


//----------------------------------------------------------------------------
//  void UART0_CLOSE()/UART1_CLOSE()
//
//  Description:
//      Macro which closes the serial link established by the
//      HAL_SETUP_SERIAL0(...)/HAL_SETUP_SERIAL1(...) macros and performs all 
//      the necessary cleanup: disabling serial interrupts, reverting the pins 
//      used for RX and TX to inputs and turning off timer1 baudgeneration (if 
//      both UART0 and UART1 are turned off.)
//
//  Arguments:
//      None.
//----------------------------------------------------------------------------
#define UART0_CLOSE()                                   \ 
    do {                                                \
        /* Restore pins for UART0 */                    \
        PORTDIRBIT(3, 1, PIN);                          \
                                                        \
        /* Turn off baud generation */                  \
        if (!(--halTimer1Usage))                        \
            TR1=0;                                      \
                                                        \
        /* Turn off interrupts */                       \
        ES0=0;                                          \
    } while (0)

#define UART1_CLOSE()                                   \ 
    do {                                                \
        /* Restore pins for UART1 */                    \
        PORTDIRBIT(2, 1, PIN);                          \
                                                        \
        /* Turn off baud generation */                  \
        if (!(--halTimer1Usage))                        \
            TR1=0;                                      \
                                                        \
        /* Turn off interrupts */                       \
        ES1=0;                                          \
    } while (0)


//-----------------------------------------------------------------------------
// Macros which are helful when transmitting data over a serial interface.
// Usage example:
//      UART0_SEND(data);
//      UART1_SEND_WITH_PARITY(data);
//      for (i=0; i<len; i++)
//          UART0_WAIT_AND_SEND(data[i]);
#define UART0_SEND(x) do { SBUF0=(x); } while (0)
#define UART1_SEND(x) do { SBUF1=(x); } while (0)
#define UART0_SEND_WITH_PARITY(x) do { ACC=(x); TB8_0=P; SBUF0=(x); } while (0)
#define UART1_SEND_WITH_PARITY(x) do { ACC=(x); TB8_1=P; SBUF1=(x); } while (0)
#define UART0_WAIT_AND_SEND(x) do { while (!TI_0); TI_0=0; SBUF0=(x); } while (0)
#define UART1_WAIT_AND_SEND(x) do { while (!TI_1); TI_1=0; SBUF1=(x); } while (0)
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Macros which are helful when receiving data over a serial interface.
// Usage example:
//      len=UART0_RECEIVE();
//      while (len-- > 0) {
//          UART0_WAIT_AND_RECEIVE(data[i++]);
//          if (!UART0_PARITY_OK())
//              break;
//      }
#define UART0_RECEIVE() SBUF0
#define UART1_RECEIVE() SBUF1
#define UART0_PARITY_OK() (ACC=SBUF0, RB8_0==P)
#define UART1_PARITY_OK() (ACC=SBUF1, RB8_1==P)
#define UART0_WAIT_AND_RECEIVE(x) do { while (!RI_0); RI_0=0; (x)=SBUF0; } while (0)
#define UART1_WAIT_AND_RECEIVE(x) do { while (!RI_1); RI_1=0; (x)=SBUF1; } while (0)
//-----------------------------------------------------------------------------



/*****************************************************************************

⌨️ 快捷键说明

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