From 6e65c6eccc223dade5c58f74d17c4aa6bd415fe5 Mon Sep 17 00:00:00 2001 From: Jose Date: Sat, 22 Feb 2025 22:07:20 +0100 Subject: [PATCH] new file: include/_hal_uart_dma.c new file: include/_hal_uart_isr.c new file: include/hal_adc.c new file: include/hal_adc.h new file: include/hal_aes.h new file: include/hal_assert.c new file: include/hal_assert.h new file: include/hal_board.h new file: include/hal_board_cfg.h new file: include/hal_ccm.h new file: include/hal_defs.h new file: include/hal_dma.c new file: include/hal_dma.h new file: include/hal_drivers.c new file: include/hal_drivers.h new file: include/hal_flash.c new file: include/hal_flash.h new file: include/hal_key.c new file: include/hal_key.h new file: include/hal_lcd.c new file: include/hal_lcd.h new file: include/hal_led.c new file: include/hal_led.h new file: include/hal_mac_cfg.h new file: include/hal_mcu.h new file: include/hal_oad.c new file: include/hal_oad.h new file: include/hal_ota.c new file: include/hal_ota.h new file: include/hal_rpc.h new file: include/hal_sleep.c new file: include/hal_sleep.h new file: include/hal_startup.c new file: include/hal_timer.c new file: include/hal_timer.h new file: include/hal_types.h new file: include/hal_uart.c new file: include/hal_uart.h --- include/_hal_uart_dma.c | 809 ++++++++++++++++++++++++++++++++++++++++ include/_hal_uart_isr.c | 587 +++++++++++++++++++++++++++++ include/hal_adc.c | 349 +++++++++++++++++ include/hal_adc.h | 149 ++++++++ include/hal_aes.h | 126 +++++++ include/hal_assert.c | 311 +++++++++++++++ include/hal_assert.h | 110 ++++++ include/hal_board.h | 1 + include/hal_board_cfg.h | 607 ++++++++++++++++++++++++++++++ include/hal_ccm.h | 51 +++ include/hal_defs.h | 146 ++++++++ include/hal_dma.c | 167 +++++++++ include/hal_dma.h | 302 +++++++++++++++ include/hal_drivers.c | 297 +++++++++++++++ include/hal_drivers.h | 105 ++++++ include/hal_flash.c | 171 +++++++++ include/hal_flash.h | 122 ++++++ include/hal_key.c | 560 +++++++++++++++++++++++++++ include/hal_key.h | 152 ++++++++ include/hal_lcd.c | 763 +++++++++++++++++++++++++++++++++++++ include/hal_lcd.h | 151 ++++++++ include/hal_led.c | 518 +++++++++++++++++++++++++ include/hal_led.h | 127 +++++++ include/hal_mac_cfg.h | 62 +++ include/hal_mcu.h | 194 ++++++++++ include/hal_oad.c | 577 ++++++++++++++++++++++++++++ include/hal_oad.h | 130 +++++++ include/hal_ota.c | 532 ++++++++++++++++++++++++++ include/hal_ota.h | 128 +++++++ include/hal_rpc.h | 136 +++++++ include/hal_sleep.c | 600 +++++++++++++++++++++++++++++ include/hal_sleep.h | 84 +++++ include/hal_startup.c | 103 +++++ include/hal_timer.c | 45 +++ include/hal_timer.h | 240 ++++++++++++ include/hal_types.h | 117 ++++++ include/hal_uart.c | 311 +++++++++++++++ include/hal_uart.h | 235 ++++++++++++ 38 files changed, 10175 insertions(+) create mode 100644 include/_hal_uart_dma.c create mode 100644 include/_hal_uart_isr.c create mode 100644 include/hal_adc.c create mode 100644 include/hal_adc.h create mode 100644 include/hal_aes.h create mode 100644 include/hal_assert.c create mode 100644 include/hal_assert.h create mode 100644 include/hal_board.h create mode 100644 include/hal_board_cfg.h create mode 100644 include/hal_ccm.h create mode 100644 include/hal_defs.h create mode 100644 include/hal_dma.c create mode 100644 include/hal_dma.h create mode 100644 include/hal_drivers.c create mode 100644 include/hal_drivers.h create mode 100644 include/hal_flash.c create mode 100644 include/hal_flash.h create mode 100644 include/hal_key.c create mode 100644 include/hal_key.h create mode 100644 include/hal_lcd.c create mode 100644 include/hal_lcd.h create mode 100644 include/hal_led.c create mode 100644 include/hal_led.h create mode 100644 include/hal_mac_cfg.h create mode 100644 include/hal_mcu.h create mode 100644 include/hal_oad.c create mode 100644 include/hal_oad.h create mode 100644 include/hal_ota.c create mode 100644 include/hal_ota.h create mode 100644 include/hal_rpc.h create mode 100644 include/hal_sleep.c create mode 100644 include/hal_sleep.h create mode 100644 include/hal_startup.c create mode 100644 include/hal_timer.c create mode 100644 include/hal_timer.h create mode 100644 include/hal_types.h create mode 100644 include/hal_uart.c create mode 100644 include/hal_uart.h diff --git a/include/_hal_uart_dma.c b/include/_hal_uart_dma.c new file mode 100644 index 0000000..81a09aa --- /dev/null +++ b/include/_hal_uart_dma.c @@ -0,0 +1,809 @@ +/************************************************************************************************** + Filename: _hal_uart_dma.c + Revised: $Date: 2010-04-01 18:14:33 -0700 (Thu, 01 Apr 2010) $ + Revision: $Revision: 22068 $ + + Description: This file contains the interface to the H/W UART driver by DMA. + + + Copyright 2006-2010 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +/********************************************************************* + * INCLUDES + */ + +#include "hal_types.h" +#include "hal_assert.h" +#include "hal_board.h" +#include "hal_defs.h" +#include "hal_dma.h" +#include "hal_mcu.h" +#include "hal_uart.h" +#if defined MT_TASK +#include "mt_uart.h" +#endif +#include "osal.h" + +/********************************************************************* + * MACROS + */ + +//#define HAL_UART_ASSERT(expr) HAL_ASSERT((expr)) +#define HAL_UART_ASSERT(expr) + +#if defined HAL_BOARD_CC2430EB || defined HAL_BOARD_CC2430DB || defined HAL_BOARD_CC2430BB +#define HAL_UART_DMA_NEW_RX_BYTE(IDX) (DMA_PAD == LO_UINT16(dmaCfg.rxBuf[(IDX)])) +#define HAL_UART_DMA_GET_RX_BYTE(IDX) (HI_UINT16(dmaCfg.rxBuf[(IDX)])) +#define HAL_UART_DMA_CLR_RX_BYTE(IDX) (dmaCfg.rxBuf[(IDX)] = BUILD_UINT16((DMA_PAD ^ 0xFF), 0)) +#else +#define HAL_UART_DMA_NEW_RX_BYTE(IDX) (DMA_PAD == HI_UINT16(dmaCfg.rxBuf[(IDX)])) +#define HAL_UART_DMA_GET_RX_BYTE(IDX) (LO_UINT16(dmaCfg.rxBuf[(IDX)])) +#define HAL_UART_DMA_CLR_RX_BYTE(IDX) (dmaCfg.rxBuf[(IDX)] = BUILD_UINT16(0, (DMA_PAD ^ 0xFF))) +#endif + +/********************************************************************* + * CONSTANTS + */ + +// UxCSR - USART Control and Status Register. +#define CSR_MODE 0x80 +#define CSR_RE 0x40 +#define CSR_SLAVE 0x20 +#define CSR_FE 0x10 +#define CSR_ERR 0x08 +#define CSR_RX_BYTE 0x04 +#define CSR_TX_BYTE 0x02 +#define CSR_ACTIVE 0x01 + +// UxUCR - USART UART Control Register. +#define UCR_FLUSH 0x80 +#define UCR_FLOW 0x40 +#define UCR_D9 0x20 +#define UCR_BIT9 0x10 +#define UCR_PARITY 0x08 +#define UCR_SPB 0x04 +#define UCR_STOP 0x02 +#define UCR_START 0x01 + +#define UTX0IE 0x04 +#define UTX1IE 0x08 + +#define P2DIR_PRIPO 0xC0 + +// Incompatible redefinitions result with more than one UART driver sub-module. +#undef PxOUT +#undef PxDIR +#undef PxSEL +#undef UxCSR +#undef UxUCR +#undef UxDBUF +#undef UxBAUD +#undef UxGCR +#undef URXxIE +#undef URXxIF +#undef UTXxIE +#undef UTXxIF +#undef HAL_UART_PERCFG_BIT +#undef HAL_UART_Px_RTS +#undef HAL_UART_Px_CTS +#undef HAL_UART_Px_RX_TX +#if (HAL_UART_DMA == 1) +#define PxOUT P0 +#define PxIN P0 +#define PxDIR P0DIR +#define PxSEL P0SEL +#define UxCSR U0CSR +#define UxUCR U0UCR +#define UxDBUF U0DBUF +#define UxBAUD U0BAUD +#define UxGCR U0GCR +#define URXxIE URX0IE +#define URXxIF URX0IF +#define UTXxIE UTX0IE +#define UTXxIF UTX0IF +#else +#define PxOUT P1 +#define PxIN P1 +#define PxDIR P1DIR +#define PxSEL P1SEL +#define UxCSR U1CSR +#define UxUCR U1UCR +#define UxDBUF U1DBUF +#define UxBAUD U1BAUD +#define UxGCR U1GCR +#define URXxIE URX1IE +#define URXxIF URX1IF +#define UTXxIE UTX1IE +#define UTXxIF UTX1IF +#endif + +#if (HAL_UART_DMA == 1) +#define HAL_UART_PERCFG_BIT 0x01 // USART0 on P0, Alt-1; so clear this bit. +#define HAL_UART_Px_RX_TX 0x0C // Peripheral I/O Select for Rx/Tx. +#define HAL_UART_Px_RTS 0x20 // Peripheral I/O Select for RTS. +#define HAL_UART_Px_CTS 0x10 // Peripheral I/O Select for CTS. +#else +#define HAL_UART_PERCFG_BIT 0x02 // USART1 on P1, Alt-2; so set this bit. +#define HAL_UART_Px_RTS 0x20 // Peripheral I/O Select for RTS. +#define HAL_UART_Px_CTS 0x10 // Peripheral I/O Select for CTS. +#define HAL_UART_Px_RX_TX 0xC0 // Peripheral I/O Select for Rx/Tx. +#endif + +// The timeout tick is at 32-kHz, so multiply msecs by 33. +#define HAL_UART_MSECS_TO_TICKS 33 + +#if defined MT_TASK +#define HAL_UART_DMA_TX_MAX MT_UART_DEFAULT_MAX_TX_BUFF +#define HAL_UART_DMA_RX_MAX MT_UART_DEFAULT_MAX_RX_BUFF +#define HAL_UART_DMA_HIGH MT_UART_DEFAULT_THRESHOLD +#define HAL_UART_DMA_IDLE (MT_UART_DEFAULT_IDLE_TIMEOUT * HAL_UART_MSECS_TO_TICKS) +#else +#if !defined HAL_UART_DMA_RX_MAX +#define HAL_UART_DMA_RX_MAX 256 +#endif +#if !defined HAL_UART_DMA_TX_MAX +#define HAL_UART_DMA_TX_MAX HAL_UART_DMA_RX_MAX +#endif +#if !defined HAL_UART_DMA_HIGH +#define HAL_UART_DMA_HIGH (HAL_UART_DMA_RX_MAX / 2 - 16) +#endif +#if !defined HAL_UART_DMA_IDLE +#define HAL_UART_DMA_IDLE (1 * HAL_UART_MSECS_TO_TICKS) +#endif +#endif +#if !defined HAL_UART_DMA_FULL +#define HAL_UART_DMA_FULL (HAL_UART_DMA_RX_MAX - 16) +#endif + +#if defined HAL_BOARD_CC2430EB || defined HAL_BOARD_CC2430DB || defined HAL_BOARD_CC2430BB +#define HAL_DMA_U0DBUF 0xDFC1 +#define HAL_DMA_U1DBUF 0xDFF9 +#else /* CC2530 */ +#define HAL_DMA_U0DBUF 0x70C1 +#define HAL_DMA_U1DBUF 0x70F9 +#endif + +#if (HAL_UART_DMA == 1) +#define DMATRIG_RX HAL_DMA_TRIG_URX0 +#define DMATRIG_TX HAL_DMA_TRIG_UTX0 +#define DMA_UDBUF HAL_DMA_U0DBUF +#define DMA_PAD U0BAUD +#else +#define DMATRIG_RX HAL_DMA_TRIG_URX1 +#define DMATRIG_TX HAL_DMA_TRIG_UTX1 +#define DMA_UDBUF HAL_DMA_U1DBUF +#define DMA_PAD U1BAUD +#endif + +/********************************************************************* + * TYPEDEFS + */ + +#if HAL_UART_DMA_RX_MAX <= 256 +typedef uint8 rxIdx_t; +#else +typedef uint16 rxIdx_t; +#endif + +#if HAL_UART_DMA_TX_MAX <= 256 +typedef uint8 txIdx_t; +#else +typedef uint16 txIdx_t; +#endif + +typedef struct +{ + uint16 rxBuf[HAL_UART_DMA_RX_MAX]; + rxIdx_t rxHead; + rxIdx_t rxTail; + uint8 rxTick; + uint8 rxShdw; + + uint8 txBuf[2][HAL_UART_DMA_TX_MAX]; + txIdx_t txIdx[2]; + volatile uint8 txSel; + uint8 txMT; + uint8 txTick; // 1-character time in 32kHz ticks according to baud rate, + // to be used in calculating time lapse since DMA ISR + // to allow delay margin before start firing DMA, so that + // DMA does not overwrite UART DBUF of previous packet + + volatile uint8 txShdw; // Sleep Timer LSB shadow. + volatile uint8 txShdwValid; // TX shadow value is valid + uint8 txDMAPending; // UART TX DMA is pending + + halUARTCBack_t uartCB; +} uartDMACfg_t; + +/********************************************************************* + * GLOBAL VARIABLES + */ + +/********************************************************************* + * GLOBAL FUNCTIONS + */ + +void HalUARTIsrDMA(void); + +/********************************************************************* + * LOCAL VARIABLES + */ + +static uartDMACfg_t dmaCfg; + +/********************************************************************* + * LOCAL FUNCTIONS + */ + +static rxIdx_t findTail(void); + +// Invoked by functions in hal_uart.c when this file is included. +static void HalUARTInitDMA(void); +static void HalUARTOpenDMA(halUARTCfg_t *config); +static uint16 HalUARTReadDMA(uint8 *buf, uint16 len); +static uint16 HalUARTWriteDMA(uint8 *buf, uint16 len); +static void HalUARTPollDMA(void); +static uint16 HalUARTRxAvailDMA(void); +static void HalUARTSuspendDMA(void); +static void HalUARTResumeDMA(void); + +/***************************************************************************** + * @fn findTail + * + * @brief Find the rxBuf index where the DMA RX engine is working. + * + * @param None. + * + * @return Index of tail of rxBuf. + *****************************************************************************/ +static rxIdx_t findTail(void) +{ + rxIdx_t idx = dmaCfg.rxHead; + + do + { + if (!HAL_UART_DMA_NEW_RX_BYTE(idx)) + { + break; + } + +#if HAL_UART_DMA_RX_MAX == 256 + idx++; +#else + if (++idx >= HAL_UART_DMA_RX_MAX) + { + idx = 0; + } +#endif + } while (idx != dmaCfg.rxHead); + + return idx; +} + +/****************************************************************************** + * @fn HalUARTInitDMA + * + * @brief Initialize the UART + * + * @param none + * + * @return none + *****************************************************************************/ +static void HalUARTInitDMA(void) +{ + halDMADesc_t *ch; + + P2DIR &= ~P2DIR_PRIPO; + P2DIR |= HAL_UART_PRIPO; + +#if (HAL_UART_DMA == 1) + PERCFG &= ~HAL_UART_PERCFG_BIT; // Set UART0 I/O to Alt. 1 location on P0. +#else + PERCFG |= HAL_UART_PERCFG_BIT; // Set UART1 I/O to Alt. 2 location on P1. +#endif + PxSEL |= HAL_UART_Px_RX_TX; // Enable Tx and Rx on P1. + ADCCFG &= ~HAL_UART_Px_RX_TX; // Make sure ADC doesnt use this. + UxCSR = CSR_MODE; // Mode is UART Mode. + UxUCR = UCR_FLUSH; // Flush it. + + // Setup Tx by DMA. + ch = HAL_DMA_GET_DESC1234( HAL_DMA_CH_TX ); + + // The start address of the destination. + HAL_DMA_SET_DEST( ch, DMA_UDBUF ); + + // Using the length field to determine how many bytes to transfer. + HAL_DMA_SET_VLEN( ch, HAL_DMA_VLEN_USE_LEN ); + + // One byte is transferred each time. + HAL_DMA_SET_WORD_SIZE( ch, HAL_DMA_WORDSIZE_BYTE ); + + // The bytes are transferred 1-by-1 on Tx Complete trigger. + HAL_DMA_SET_TRIG_MODE( ch, HAL_DMA_TMODE_SINGLE ); + HAL_DMA_SET_TRIG_SRC( ch, DMATRIG_TX ); + + // The source address is incremented by 1 byte after each transfer. + HAL_DMA_SET_SRC_INC( ch, HAL_DMA_SRCINC_1 ); + + // The destination address is constant - the Tx Data Buffer. + HAL_DMA_SET_DST_INC( ch, HAL_DMA_DSTINC_0 ); + + // The DMA Tx done is serviced by ISR in order to maintain full thruput. + HAL_DMA_SET_IRQ( ch, HAL_DMA_IRQMASK_ENABLE ); + + // Xfer all 8 bits of a byte xfer. + HAL_DMA_SET_M8( ch, HAL_DMA_M8_USE_8_BITS ); + + // DMA has highest priority for memory access. + HAL_DMA_SET_PRIORITY( ch, HAL_DMA_PRI_HIGH ); + + // Setup Rx by DMA. + ch = HAL_DMA_GET_DESC1234( HAL_DMA_CH_RX ); + + // The start address of the source. + HAL_DMA_SET_SOURCE( ch, DMA_UDBUF ); + + // Using the length field to determine how many bytes to transfer. + HAL_DMA_SET_VLEN( ch, HAL_DMA_VLEN_USE_LEN ); + + /* The trick is to cfg DMA to xfer 2 bytes for every 1 byte of Rx. + * The byte after the Rx Data Buffer is the Baud Cfg Register, + * which always has a known value. So init Rx buffer to inverse of that + * known value. DMA word xfer will flip the bytes, so every valid Rx byte + * in the Rx buffer will be preceded by a DMA_PAD char equal to the + * Baud Cfg Register value. + */ + HAL_DMA_SET_WORD_SIZE( ch, HAL_DMA_WORDSIZE_WORD ); + + // The bytes are transferred 1-by-1 on Rx Complete trigger. + HAL_DMA_SET_TRIG_MODE( ch, HAL_DMA_TMODE_SINGLE_REPEATED ); + HAL_DMA_SET_TRIG_SRC( ch, DMATRIG_RX ); + + // The source address is constant - the Rx Data Buffer. + HAL_DMA_SET_SRC_INC( ch, HAL_DMA_SRCINC_0 ); + + // The destination address is incremented by 1 word after each transfer. + HAL_DMA_SET_DST_INC( ch, HAL_DMA_DSTINC_1 ); + HAL_DMA_SET_DEST( ch, dmaCfg.rxBuf ); + HAL_DMA_SET_LEN( ch, HAL_UART_DMA_RX_MAX ); + + // The DMA is to be polled and shall not issue an IRQ upon completion. + HAL_DMA_SET_IRQ( ch, HAL_DMA_IRQMASK_DISABLE ); + + // Xfer all 8 bits of a byte xfer. + HAL_DMA_SET_M8( ch, HAL_DMA_M8_USE_8_BITS ); + + // DMA has highest priority for memory access. + HAL_DMA_SET_PRIORITY( ch, HAL_DMA_PRI_HIGH ); +} + +/****************************************************************************** + * @fn HalUARTOpenDMA + * + * @brief Open a port according tp the configuration specified by parameter. + * + * @param config - contains configuration information + * + * @return none + *****************************************************************************/ +static void HalUARTOpenDMA(halUARTCfg_t *config) +{ + dmaCfg.uartCB = config->callBackFunc; + // Only supporting subset of baudrate for code size - other is possible. + HAL_UART_ASSERT((config->baudRate == HAL_UART_BR_9600) || + (config->baudRate == HAL_UART_BR_19200) || + (config->baudRate == HAL_UART_BR_38400) || + (config->baudRate == HAL_UART_BR_57600) || + (config->baudRate == HAL_UART_BR_115200)); + + if (config->baudRate == HAL_UART_BR_57600 || + config->baudRate == HAL_UART_BR_115200) + { + UxBAUD = 216; + } + else + { + UxBAUD = 59; + } + + switch (config->baudRate) + { + case HAL_UART_BR_9600: + UxGCR = 8; + dmaCfg.txTick = 35; // (32768Hz / (9600bps / 10 bits)) + // 10 bits include start and stop bits. + break; + case HAL_UART_BR_19200: + UxGCR = 9; + dmaCfg.txTick = 18; + break; + case HAL_UART_BR_38400: + UxGCR = 10; + dmaCfg.txTick = 9; + break; + case HAL_UART_BR_57600: + UxGCR = 10; + dmaCfg.txTick = 6; + break; + default: + // HAL_UART_BR_115200 + UxGCR = 11; + dmaCfg.txTick = 3; + break; + } + + // 8 bits/char; no parity; 1 stop bit; stop bit hi. + if (config->flowControl) + { + UxUCR = UCR_FLOW | UCR_STOP; + PxSEL |= HAL_UART_Px_CTS; + // DMA Rx is always on (self-resetting). So flow must be controlled by the S/W polling the Rx + // buffer level. Start by allowing flow. + PxOUT &= ~HAL_UART_Px_RTS; + PxDIR |= HAL_UART_Px_RTS; + } + else + { + UxUCR = UCR_STOP; + } + + dmaCfg.rxBuf[0] = *(volatile uint8 *)DMA_UDBUF; // Clear the DMA Rx trigger. + HAL_DMA_CLEAR_IRQ(HAL_DMA_CH_RX); + HAL_DMA_ARM_CH(HAL_DMA_CH_RX); + osal_memset(dmaCfg.rxBuf, (DMA_PAD ^ 0xFF), HAL_UART_DMA_RX_MAX*2); + + UxCSR |= CSR_RE; + + // Initialize that TX DMA is not pending + dmaCfg.txDMAPending = FALSE; + dmaCfg.txShdwValid = FALSE; +} + +/***************************************************************************** + * @fn HalUARTReadDMA + * + * @brief Read a buffer from the UART + * + * @param buf - valid data buffer at least 'len' bytes in size + * len - max length number of bytes to copy to 'buf' + * + * @return length of buffer that was read + *****************************************************************************/ +static uint16 HalUARTReadDMA(uint8 *buf, uint16 len) +{ + uint16 cnt; + + for (cnt = 0; cnt < len; cnt++) + { + if (!HAL_UART_DMA_NEW_RX_BYTE(dmaCfg.rxHead)) + { + break; + } + *buf++ = HAL_UART_DMA_GET_RX_BYTE(dmaCfg.rxHead); + HAL_UART_DMA_CLR_RX_BYTE(dmaCfg.rxHead); +#if HAL_UART_DMA_RX_MAX == 256 + (dmaCfg.rxHead)++; +#else + if (++(dmaCfg.rxHead) >= HAL_UART_DMA_RX_MAX) + { + dmaCfg.rxHead = 0; + } +#endif + } + PxOUT &= ~HAL_UART_Px_RTS; // Re-enable the flow on any read. + + return cnt; +} + +/****************************************************************************** + * @fn HalUARTWriteDMA + * + * @brief Write a buffer to the UART. + * + * @param buf - pointer to the buffer that will be written, not freed + * len - length of + * + * @return length of the buffer that was sent + *****************************************************************************/ +static uint16 HalUARTWriteDMA(uint8 *buf, uint16 len) +{ + uint16 cnt; + halIntState_t his; + uint8 txSel; + txIdx_t txIdx; + + // Enforce all or none. + if ((len + dmaCfg.txIdx[dmaCfg.txSel]) > HAL_UART_DMA_TX_MAX) + { + return 0; + } + + HAL_ENTER_CRITICAL_SECTION(his); + txSel = dmaCfg.txSel; + txIdx = dmaCfg.txIdx[txSel]; + HAL_EXIT_CRITICAL_SECTION(his); + + for (cnt = 0; cnt < len; cnt++) + { + dmaCfg.txBuf[txSel][txIdx++] = buf[cnt]; + } + + HAL_ENTER_CRITICAL_SECTION(his); + if (txSel != dmaCfg.txSel) + { + HAL_EXIT_CRITICAL_SECTION(his); + txSel = dmaCfg.txSel; + txIdx = dmaCfg.txIdx[txSel]; + + for (cnt = 0; cnt < len; cnt++) + { + dmaCfg.txBuf[txSel][txIdx++] = buf[cnt]; + } + HAL_ENTER_CRITICAL_SECTION(his); + } + + dmaCfg.txIdx[txSel] = txIdx; + + if (dmaCfg.txIdx[(txSel ^ 1)] == 0) + { + // TX DMA is expected to be fired + dmaCfg.txDMAPending = TRUE; + } + HAL_EXIT_CRITICAL_SECTION(his); + return cnt; +} + +/****************************************************************************** + * @fn HalUARTPollDMA + * + * @brief Poll a USART module implemented by DMA. + * + * @param none + * + * @return none + *****************************************************************************/ +static void HalUARTPollDMA(void) +{ + uint16 cnt = 0; + uint8 evt = 0; + + if (HAL_UART_DMA_NEW_RX_BYTE(dmaCfg.rxHead)) + { + rxIdx_t tail = findTail(); + + // If the DMA has transferred in more Rx bytes, reset the Rx idle timer. + if (dmaCfg.rxTail != tail) + { + dmaCfg.rxTail = tail; + + // Re-sync the shadow on any 1st byte(s) received. + if (dmaCfg.rxTick == 0) + { + dmaCfg.rxShdw = ST0; + } + dmaCfg.rxTick = HAL_UART_DMA_IDLE; + } + else if (dmaCfg.rxTick) + { + // Use the LSB of the sleep timer (ST0 must be read first anyway). + uint8 decr = ST0 - dmaCfg.rxShdw; + + if (dmaCfg.rxTick > decr) + { + dmaCfg.rxTick -= decr; + dmaCfg.rxShdw = ST0; + } + else + { + dmaCfg.rxTick = 0; + } + } + cnt = HalUARTRxAvailDMA(); + } + else + { + dmaCfg.rxTick = 0; + } + + if (cnt >= HAL_UART_DMA_FULL) + { + evt = HAL_UART_RX_FULL; + } + else if (cnt >= HAL_UART_DMA_HIGH) + { + evt = HAL_UART_RX_ABOUT_FULL; + PxOUT |= HAL_UART_Px_RTS; // Disable Rx flow. + } + else if (cnt && !dmaCfg.rxTick) + { + evt = HAL_UART_RX_TIMEOUT; + } + + if (dmaCfg.txMT) + { + dmaCfg.txMT = FALSE; + evt |= HAL_UART_TX_EMPTY; + } + + if (dmaCfg.txShdwValid) + { + uint8 decr = ST0; + decr -= dmaCfg.txShdw; + if (decr > dmaCfg.txTick) + { + // No protection for txShdwValid is required + // because while the shadow was valid, DMA ISR cannot be triggered + // to cause concurrent access to this variable. + dmaCfg.txShdwValid = FALSE; + } + } + + if (dmaCfg.txDMAPending && !dmaCfg.txShdwValid) + { + // UART TX DMA is expected to be fired and enough time has lapsed since last DMA ISR + // to know that DBUF can be overwritten + halDMADesc_t *ch = HAL_DMA_GET_DESC1234(HAL_DMA_CH_TX); + halIntState_t intState; + + // Clear the DMA pending flag + dmaCfg.txDMAPending = FALSE; + + HAL_DMA_SET_SOURCE(ch, dmaCfg.txBuf[dmaCfg.txSel]); + HAL_DMA_SET_LEN(ch, dmaCfg.txIdx[dmaCfg.txSel]); + dmaCfg.txSel ^= 1; + HAL_ENTER_CRITICAL_SECTION(intState); + HAL_DMA_ARM_CH(HAL_DMA_CH_TX); + do + { + asm("NOP"); + } while (!HAL_DMA_CH_ARMED(HAL_DMA_CH_TX)); + HAL_DMA_CLEAR_IRQ(HAL_DMA_CH_TX); + HAL_DMA_MAN_TRIGGER(HAL_DMA_CH_TX); + HAL_EXIT_CRITICAL_SECTION(intState); + } + else + { + halIntState_t his; + + HAL_ENTER_CRITICAL_SECTION(his); + if ((dmaCfg.txIdx[dmaCfg.txSel] != 0) && !HAL_DMA_CH_ARMED(HAL_DMA_CH_TX) + && !HAL_DMA_CHECK_IRQ(HAL_DMA_CH_TX)) + { + HAL_EXIT_CRITICAL_SECTION(his); + HalUARTIsrDMA(); + } + else + { + HAL_EXIT_CRITICAL_SECTION(his); + } + } + + if (evt && (dmaCfg.uartCB != NULL)) + { + dmaCfg.uartCB(HAL_UART_DMA-1, evt); + } +} + +/************************************************************************************************** + * @fn HalUARTRxAvailDMA() + * + * @brief Calculate Rx Buffer length - the number of bytes in the buffer. + * + * @param none + * + * @return length of current Rx Buffer + **************************************************************************************************/ +static uint16 HalUARTRxAvailDMA(void) +{ + uint16 cnt = 0; + + if (HAL_UART_DMA_NEW_RX_BYTE(dmaCfg.rxHead)) + { + uint16 idx; + + for (idx = 0; idx < HAL_UART_DMA_RX_MAX; idx++) + { + if (HAL_UART_DMA_NEW_RX_BYTE(idx)) + { + cnt++; + } + } + } + + return cnt; +} + +/****************************************************************************** + * @fn HalUARTSuspendDMA + * + * @brief Suspend UART hardware before entering PM mode 1, 2 or 3. + * + * @param None + * + * @return None + *****************************************************************************/ +static void HalUARTSuspendDMA( void ) +{ + PxOUT |= HAL_UART_Px_RTS; // Disable Rx flow. + UxCSR &= ~CSR_RE; + P0IEN |= HAL_UART_Px_CTS; // Enable the CTS ISR. +} + +/****************************************************************************** + * @fn HalUARTResumeDMA + * + * @brief Resume UART hardware after exiting PM mode 1, 2 or 3. + * + * @param None + * + * @return None + *****************************************************************************/ +static void HalUARTResumeDMA( void ) +{ + P0IEN &= ~HAL_UART_Px_CTS; // Disable the CTS ISR. + UxUCR |= UCR_FLUSH; + UxCSR |= CSR_RE; + PxOUT &= ~HAL_UART_Px_RTS; // Re-enable Rx flow. +} + +/****************************************************************************** + * @fn HalUARTIsrDMA + * + * @brief Handle the Tx done DMA ISR. + * + * @param none + * + * @return none + *****************************************************************************/ +void HalUARTIsrDMA(void); +void HalUARTIsrDMA(void) +{ + HAL_DMA_CLEAR_IRQ(HAL_DMA_CH_TX); + + // Indicate that the other buffer is free now. + dmaCfg.txIdx[(dmaCfg.txSel ^ 1)] = 0; + dmaCfg.txMT = TRUE; + + // Set TX shadow + dmaCfg.txShdw = ST0; + dmaCfg.txShdwValid = TRUE; + + // If there is more Tx data ready to go, re-start the DMA immediately on it. + if (dmaCfg.txIdx[dmaCfg.txSel]) + { + // UART TX DMA is expected to be fired + dmaCfg.txDMAPending = TRUE; + } +} + +/****************************************************************************** +******************************************************************************/ diff --git a/include/_hal_uart_isr.c b/include/_hal_uart_isr.c new file mode 100644 index 0000000..a29dfd7 --- /dev/null +++ b/include/_hal_uart_isr.c @@ -0,0 +1,587 @@ +/************************************************************************************************** + Filename: _hal_uart_isr.c + Revised: $Date: 2013-11-21 16:22:23 -0800 (Thu, 21 Nov 2013) $ + Revision: $Revision: 36195 $ + + Description: This file contains the interface to the H/W UART driver by ISR. + + + Copyright 2006-2012 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +/********************************************************************* + * INCLUDES + */ + +#include "hal_types.h" +#include "hal_assert.h" +#include "hal_board.h" +#include "hal_defs.h" +#include "hal_mcu.h" +#include "hal_uart.h" +#if defined MT_TASK +#include "MT_UART.h" +#endif + +/********************************************************************* + * MACROS + */ + +//#define HAL_UART_ASSERT(expr) HAL_ASSERT((expr)) +#define HAL_UART_ASSERT(expr) + +/********************************************************************* + * CONSTANTS + */ + +// UxCSR - USART Control and Status Register. +#define CSR_MODE 0x80 +#define CSR_RE 0x40 +#define CSR_SLAVE 0x20 +#define CSR_FE 0x10 +#define CSR_ERR 0x08 +#define CSR_RX_BYTE 0x04 +#define CSR_TX_BYTE 0x02 +#define CSR_ACTIVE 0x01 + +// UxUCR - USART UART Control Register. +#define UCR_FLUSH 0x80 +#define UCR_FLOW 0x40 +#define UCR_D9 0x20 +#define UCR_BIT9 0x10 +#define UCR_PARITY 0x08 +#define UCR_SPB 0x04 +#define UCR_STOP 0x02 +#define UCR_START 0x01 + +#define UTX0IE 0x04 +#define UTX1IE 0x08 + +#define P2DIR_PRIPO 0xC0 + +// Incompatible redefinitions result with more than one UART driver sub-module. +#undef PxOUT +#undef PxDIR +#undef PxSEL +#undef UxCSR +#undef UxUCR +#undef UxDBUF +#undef UxBAUD +#undef UxGCR +#undef URXxIE +#undef URXxIF +#undef UTXxIE +#undef UTXxIF +#undef HAL_UART_PERCFG_BIT +#undef HAL_UART_Px_RTS +#undef HAL_UART_Px_CTS +#undef HAL_UART_Px_RX_TX +#if (HAL_UART_ISR == 1) +#define PxOUT P0 +#define PxDIR P0DIR +#define PxSEL P0SEL +#define UxCSR U0CSR +#define UxUCR U0UCR +#define UxDBUF U0DBUF +#define UxBAUD U0BAUD +#define UxGCR U0GCR +#define URXxIE URX0IE +#define URXxIF URX0IF +#define UTXxIE UTX0IE +#define UTXxIF UTX0IF +#else +#define PxOUT P1 +#define PxDIR P1DIR +#define PxSEL P1SEL +#define UxCSR U1CSR +#define UxUCR U1UCR +#define UxDBUF U1DBUF +#define UxBAUD U1BAUD +#define UxGCR U1GCR +#define URXxIE URX1IE +#define URXxIF URX1IF +#define UTXxIE UTX1IE +#define UTXxIF UTX1IF +#endif + +#if (HAL_UART_ISR == 1) +#define HAL_UART_PERCFG_BIT 0x01 // USART0 on P0, Alt-1; so clear this bit. +#define HAL_UART_Px_RX_TX 0x0C // Peripheral I/O Select for Rx/Tx. +#define HAL_UART_Px_RTS 0x20 // Peripheral I/O Select for RTS. +#define HAL_UART_Px_CTS 0x10 // Peripheral I/O Select for CTS. +#else +#define HAL_UART_PERCFG_BIT 0x02 // USART1 on P1, Alt-2; so set this bit. +#define HAL_UART_Px_RTS 0x20 // Peripheral I/O Select for RTS. +#define HAL_UART_Px_CTS 0x10 // Peripheral I/O Select for CTS. +#define HAL_UART_Px_RX_TX 0xC0 // Peripheral I/O Select for Rx/Tx. +#endif + +// The timeout tick is at 32-kHz, so multiply msecs by 33. +#define HAL_UART_MSECS_TO_TICKS 33 + +#if defined MT_TASK +#define HAL_UART_ISR_TX_MAX MT_UART_DEFAULT_MAX_TX_BUFF +#define HAL_UART_ISR_RX_MAX MT_UART_DEFAULT_MAX_RX_BUFF +#define HAL_UART_ISR_HIGH MT_UART_DEFAULT_THRESHOLD +#define HAL_UART_ISR_IDLE (MT_UART_DEFAULT_IDLE_TIMEOUT * HAL_UART_MSECS_TO_TICKS) +#else +#if !defined HAL_UART_ISR_RX_MAX +#define HAL_UART_ISR_RX_MAX 128 +#endif +#if !defined HAL_UART_ISR_TX_MAX +#define HAL_UART_ISR_TX_MAX HAL_UART_ISR_RX_MAX +#endif +#if !defined HAL_UART_ISR_HIGH +#define HAL_UART_ISR_HIGH (HAL_UART_ISR_RX_MAX / 2 - 16) +#endif +#if !defined HAL_UART_ISR_IDLE +#define HAL_UART_ISR_IDLE (6 * HAL_UART_MSECS_TO_TICKS) +#endif +#endif + +/********************************************************************* + * TYPEDEFS + */ + +typedef struct +{ + uint8 rxBuf[HAL_UART_ISR_RX_MAX]; +#if HAL_UART_ISR_RX_MAX < 256 + uint8 rxHead; + volatile uint8 rxTail; +#else + uint16 rxHead; + volatile uint16 rxTail; +#endif + uint8 rxTick; + uint8 rxShdw; + + uint8 txBuf[HAL_UART_ISR_TX_MAX]; +#if HAL_UART_ISR_TX_MAX < 256 + volatile uint8 txHead; + uint8 txTail; +#else + volatile uint16 txHead; + uint16 txTail; +#endif + uint8 txMT; + + halUARTCBack_t uartCB; +} uartISRCfg_t; + +/********************************************************************* + * GLOBAL VARIABLES + */ + +/********************************************************************* + * GLOBAL FUNCTIONS + */ + +/********************************************************************* + * LOCAL VARIABLES + */ + +static uartISRCfg_t isrCfg; + +/********************************************************************* + * LOCAL FUNCTIONS + */ + +static void HalUARTInitISR(void); +static void HalUARTOpenISR(halUARTCfg_t *config); +uint16 HalUARTReadISR(uint8 *buf, uint16 len); +uint16 HalUARTWriteISR(uint8 *buf, uint16 len); +static void HalUARTPollISR(void); +static uint16 HalUARTRxAvailISR(void); +static uint16 HalUARTTxAvailISR(void); +static void HalUARTSuspendISR(void); +static void HalUARTResumeISR(void); + +/****************************************************************************** + * @fn HalUARTInitISR + * + * @brief Initialize the UART + * + * @param none + * + * @return none + *****************************************************************************/ +static void HalUARTInitISR(void) +{ + // Set P2 priority - USART0 over USART1 if both are defined. + P2DIR &= ~P2DIR_PRIPO; + P2DIR |= HAL_UART_PRIPO; + +#if (HAL_UART_ISR == 1) + PERCFG &= ~HAL_UART_PERCFG_BIT; // Set UART0 I/O location to P0. +#else + PERCFG |= HAL_UART_PERCFG_BIT; // Set UART1 I/O location to P1. +#endif + PxSEL |= HAL_UART_Px_RX_TX; // Enable Tx and Rx on P1. + ADCCFG &= ~HAL_UART_Px_RX_TX; // Make sure ADC doesnt use this. + UxCSR = CSR_MODE; // Mode is UART Mode. + UxUCR = UCR_FLUSH; // Flush it. +} + +/****************************************************************************** + * @fn HalUARTUnInitISR + * + * @brief UnInitialize the UART. + * + * @param none + * + * @return none + *****************************************************************************/ +static void HalUARTUnInitISR(void) +{ + UxCSR = 0; + URXxIE = 0; + URXxIF = 0; + IEN2 &= ~UTXxIE; + UTXxIF = 0; +} + +/****************************************************************************** + * @fn HalUARTOpenISR + * + * @brief Open a port according tp the configuration specified by parameter. + * + * @param config - contains configuration information + * + * @return none + *****************************************************************************/ +static void HalUARTOpenISR(halUARTCfg_t *config) +{ + isrCfg.uartCB = config->callBackFunc; + // Only supporting subset of baudrate for code size - other is possible. + HAL_UART_ASSERT((config->baudRate == HAL_UART_BR_9600) || + (config->baudRate == HAL_UART_BR_19200) || + (config->baudRate == HAL_UART_BR_38400) || + (config->baudRate == HAL_UART_BR_57600) || + (config->baudRate == HAL_UART_BR_115200)); + + if (config->baudRate == HAL_UART_BR_57600 || + config->baudRate == HAL_UART_BR_115200) + { + UxBAUD = 216; + } + else + { + UxBAUD = 59; + } + + switch (config->baudRate) + { + case HAL_UART_BR_9600: + UxGCR = 8; + break; + case HAL_UART_BR_19200: + UxGCR = 9; + break; + case HAL_UART_BR_38400: + case HAL_UART_BR_57600: + UxGCR = 10; + break; + default: + UxGCR = 11; + break; + } + + // 8 bits/char; no parity; 1 stop bit; stop bit hi. + if (config->flowControl) + { + UxUCR = UCR_FLOW | UCR_STOP; + PxSEL |= HAL_UART_Px_RTS | HAL_UART_Px_CTS; + } + else + { + UxUCR = UCR_STOP; + } + + UxCSR |= CSR_RE; + URXxIE = 1; + UTXxIF = 1; // Prime the ISR pump. +} + +/***************************************************************************** + * @fn HalUARTReadISR + * + * @brief Read a buffer from the UART + * + * @param buf - valid data buffer at least 'len' bytes in size + * len - max length number of bytes to copy to 'buf' + * + * @return length of buffer that was read + *****************************************************************************/ +uint16 HalUARTReadISR(uint8 *buf, uint16 len) +{ + uint16 cnt = 0; + + while ((isrCfg.rxHead != isrCfg.rxTail) && (cnt < len)) + { + *buf++ = isrCfg.rxBuf[isrCfg.rxHead++]; + if (isrCfg.rxHead >= HAL_UART_ISR_RX_MAX) + { + isrCfg.rxHead = 0; + } + cnt++; + } + + return cnt; +} + +/****************************************************************************** + * @fn HalUARTWriteISR + * + * @brief Write a buffer to the UART. + * + * @param buf - pointer to the buffer that will be written, not freed + * len - length of + * + * @return length of the buffer that was sent + *****************************************************************************/ +uint16 HalUARTWriteISR(uint8 *buf, uint16 len) +{ + uint16 cnt; + + // Enforce all or none. + if (HalUARTTxAvailISR() < len) + { + return 0; + } + + for (cnt = 0; cnt < len; cnt++) + { + isrCfg.txBuf[isrCfg.txTail] = *buf++; + isrCfg.txMT = 0; + + if (isrCfg.txTail >= HAL_UART_ISR_TX_MAX-1) + { + isrCfg.txTail = 0; + } + else + { + isrCfg.txTail++; + } + + // Keep re-enabling ISR as it might be keeping up with this loop due to other ints. + IEN2 |= UTXxIE; + } + + return cnt; +} + +/****************************************************************************** + * @fn HalUARTPollISR + * + * @brief Poll a USART module implemented by ISR. + * + * @param none + * + * @return none + *****************************************************************************/ +static void HalUARTPollISR(void) +{ + if (isrCfg.uartCB != NULL) + { + uint16 cnt = HalUARTRxAvailISR(); + uint8 evt = 0; + + if (isrCfg.rxTick) + { + // Use the LSB of the sleep timer (ST0 must be read first anyway). + uint8 decr = ST0 - isrCfg.rxShdw; + + if (isrCfg.rxTick > decr) + { + isrCfg.rxTick -= decr; + } + else + { + isrCfg.rxTick = 0; + } + } + isrCfg.rxShdw = ST0; + + if (cnt >= HAL_UART_ISR_RX_MAX-1) + { + evt = HAL_UART_RX_FULL; + } + else if (cnt >= HAL_UART_ISR_HIGH) + { + evt = HAL_UART_RX_ABOUT_FULL; + } + else if (cnt && !isrCfg.rxTick) + { + evt = HAL_UART_RX_TIMEOUT; + } + + if (isrCfg.txMT) + { + isrCfg.txMT = 0; + evt |= HAL_UART_TX_EMPTY; + } + + if (evt) + { + isrCfg.uartCB(HAL_UART_ISR-1, evt); + } + } +} + +/************************************************************************************************** + * @fn HalUARTRxAvailISR() + * + * @brief Calculate Rx Buffer length - the number of bytes in the buffer. + * + * @param none + * + * @return length of current Rx Buffer + **************************************************************************************************/ +static uint16 HalUARTRxAvailISR(void) +{ + uint8 tmp = isrCfg.rxTail; + + return (tmp >= isrCfg.rxHead) ? (tmp - isrCfg.rxHead) : (HAL_UART_ISR_RX_MAX - isrCfg.rxHead + tmp); +} + +/************************************************************************************************** + * @fn HalUARTTxAvailISR() + * + * @brief Calculate Tx Buffer length - the number of bytes in the buffer. + * + * @param none + * + * @return length of current Tx Buffer + **************************************************************************************************/ +static uint16 HalUARTTxAvailISR(void) +{ + uint16 tmp = isrCfg.txHead; + + return (tmp > isrCfg.txTail) ? (isrCfg.txHead - isrCfg.txTail - 1) : (HAL_UART_ISR_TX_MAX - isrCfg.txTail + isrCfg.txHead - 1); +} + +/****************************************************************************** + * @fn HalUARTSuspendISR + * + * @brief Suspend UART hardware before entering PM mode 1, 2 or 3. + * + * @param None + * + * @return None + *****************************************************************************/ +static void HalUARTSuspendISR( void ) +{ + UxCSR &= ~CSR_RE; +} + +/****************************************************************************** + * @fn HalUARTResumeISR + * + * @brief Resume UART hardware after exiting PM mode 1, 2 or 3. + * + * @param None + * + * @return None + *****************************************************************************/ +static void HalUARTResumeISR( void ) +{ + UxUCR |= UCR_FLUSH; + UxCSR |= CSR_RE; +} + +/*************************************************************************************************** + * @fn halUartRxIsr + * + * @brief UART Receive Interrupt + * + * @param None + * + * @return None + ***************************************************************************************************/ +#if (HAL_UART_ISR == 1) +HAL_ISR_FUNCTION( halUart0RxIsr, URX0_VECTOR ) +#else +HAL_ISR_FUNCTION( halUart1RxIsr, URX1_VECTOR ) +#endif + +{ + uint8 tmp = UxDBUF; + isrCfg.rxBuf[isrCfg.rxTail] = tmp; + + // Re-sync the shadow on any 1st byte received. + if (isrCfg.rxHead == isrCfg.rxTail) + { + isrCfg.rxShdw = ST0; + } + + if (++isrCfg.rxTail >= HAL_UART_ISR_RX_MAX) + { + isrCfg.rxTail = 0; + } + + isrCfg.rxTick = HAL_UART_ISR_IDLE; +} + +/*************************************************************************************************** + * @fn halUartTxIsr + * + * @brief UART Transmit Interrupt + * + * @param None + * + * @return None + ***************************************************************************************************/ +#if (HAL_UART_ISR == 1) +HAL_ISR_FUNCTION( halUart0TxIsr, UTX0_VECTOR ) +#else +HAL_ISR_FUNCTION( halUart1TxIsr, UTX1_VECTOR ) +#endif +{ + if (isrCfg.txHead == isrCfg.txTail) + { + IEN2 &= ~UTXxIE; + isrCfg.txMT = 1; + } + else + { + UTXxIF = 0; + UxDBUF = isrCfg.txBuf[isrCfg.txHead++]; + + if (isrCfg.txHead >= HAL_UART_ISR_TX_MAX) + { + isrCfg.txHead = 0; + } + } +} + +/****************************************************************************** +******************************************************************************/ diff --git a/include/hal_adc.c b/include/hal_adc.c new file mode 100644 index 0000000..63e9b66 --- /dev/null +++ b/include/hal_adc.c @@ -0,0 +1,349 @@ +/************************************************************************************************** + Filename: hal_adc.c + Revised: $Date: 2010-03-12 16:10:36 -0800 (Fri, 12 Mar 2010) $ + Revision: $Revision: 21910 $ + + Description: This file contains the interface to the HAL ADC. + + + Copyright 2006-2010 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS?WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +/************************************************************************************************** + * INCLUDES + **************************************************************************************************/ + +#include "hal_adc.h" +#include "hal_defs.h" +#include "hal_mcu.h" +#include "hal_types.h" +#include + +/************************************************************************************************** + * CONSTANTS + **************************************************************************************************/ +#define HAL_ADC_EOC 0x80 /* End of Conversion bit */ +#define HAL_ADC_START 0x40 /* Starts Conversion */ + +#define HAL_ADC_STSEL_EXT 0x00 /* External Trigger */ +#define HAL_ADC_STSEL_FULL 0x10 /* Full Speed, No Trigger */ +#define HAL_ADC_STSEL_T1C0 0x20 /* Timer1, Channel 0 Compare Event Trigger */ +#define HAL_ADC_STSEL_ST 0x30 /* ADCCON1.ST =1 Trigger */ + +#define HAL_ADC_RAND_NORM 0x00 /* Normal Operation */ +#define HAL_ADC_RAND_LFSR 0x04 /* Clock LFSR */ +#define HAL_ADC_RAND_SEED 0x08 /* Seed Modulator */ +#define HAL_ADC_RAND_STOP 0x0c /* Stop Random Generator */ +#define HAL_ADC_RAND_BITS 0x0c /* Bits [3:2] */ + +#define HAL_ADC_DEC_064 0x00 /* Decimate by 64 : 8-bit resolution */ +#define HAL_ADC_DEC_128 0x10 /* Decimate by 128 : 10-bit resolution */ +#define HAL_ADC_DEC_256 0x20 /* Decimate by 256 : 12-bit resolution */ +#define HAL_ADC_DEC_512 0x30 /* Decimate by 512 : 14-bit resolution */ +#define HAL_ADC_DEC_BITS 0x30 /* Bits [5:4] */ + +#define HAL_ADC_STSEL HAL_ADC_STSEL_ST +#define HAL_ADC_RAND_GEN HAL_ADC_RAND_STOP +#define HAL_ADC_REF_VOLT HAL_ADC_REF_AVDD +#define HAL_ADC_DEC_RATE HAL_ADC_DEC_064 +#define HAL_ADC_SCHN HAL_ADC_CHN_VDD3 +#define HAL_ADC_ECHN HAL_ADC_CHN_GND + +#ifdef ADC_CAL +#define INFO_CAL_H (*((uint8 *)0x7826)) //calibration values stored here +#define INFO_CAL_L (*((uint8 *)0x7827)) +#endif //ADC_CAL + +/* ------------------------------------------------------------------------------------------------ + * Local Variables + * ------------------------------------------------------------------------------------------------ + */ + +#if (HAL_ADC == TRUE) +static uint8 adcRef; +#endif + +#ifdef ADC_CAL +uint32 IDEAL_VDD3_CODE = 1780; // this is ideal value for Vdd/3 at 3.0V input and 1.15V V_ref at 12 bit resolution +uint16 vdd3InfoPg; +int16 compensation; +#endif //ADC_CAL + +/*************************************************************************************************** + * LOCAL FUNCTION + ***************************************************************************************************/ +#ifdef ADC_CAL +static uint16 HalAdcCompensate ( uint16 rawAdcVal, uint8 resolution ); +#endif //ADC_CAL + +/************************************************************************************************** + * @fn HalAdcInit + * + * @brief Initialize ADC Service + * + * @param None + * + * @return None + **************************************************************************************************/ +void HalAdcInit(void) +{ +#if (HAL_ADC == TRUE) + adcRef = HAL_ADC_REF_VOLT; +#ifdef ADC_CAL + { + vdd3InfoPg = ((INFO_CAL_H << 8) | INFO_CAL_L) >> 4; + compensation = IDEAL_VDD3_CODE - vdd3InfoPg; + } +#endif //ADC_CAL +#endif +} + +#ifdef ADC_CAL +/************************************************************************************************** + * @fn HalAdcRead + * + * @brief Compensate a raw ADC value + * + * @param rawAdcVal - channel where ADC will be read + * + * @param voltValue - Pointer that will be set to the converted voltage value +* Note: Pass NULL if convertion to voltage is not needed + * + * @param resolution - Resolution of the rawAdcVal, only needed if converting + * to voltageValue + * + * @return 16 bit value of the Compensated ADC value. + * + * + **************************************************************************************************/ +uint16 HalAdcCompensate ( uint16 rawAdcVal, uint8 resolution ) +{ +{ + uint16 compAdcVal; + int16 comp ; + + switch (resolution) + { + case HAL_ADC_RESOLUTION_8: + comp = compensation >> 4; + break; + case HAL_ADC_RESOLUTION_10: + comp = compensation >> 2; + break; + case HAL_ADC_RESOLUTION_14: + comp = compensation << 2; + break; + case HAL_ADC_RESOLUTION_12: + default: + break; + } + compAdcVal = rawAdcVal + comp; + + return compAdcVal; +} +#endif //ADC_CAL + +/************************************************************************************************** + * @fn HalAdcRead + * + * @brief Read the ADC based on given channel and resolution + * + * @param channel - channel where ADC will be read + * @param resolution - the resolution of the value + * + * @return 16 bit value of the ADC in offset binary format. + * + * Note that the ADC is "bipolar", which means the GND (0V) level is mid-scale. + * Note2: This function assumes that ADCCON3 contains the voltage reference. + **************************************************************************************************/ +uint16 HalAdcRead (uint8 channel, uint8 resolution) +{ + int16 reading = 0; + +#if (HAL_ADC == TRUE) + uint8 i, resbits; + uint8 adcChannel = 1; + + /* + * If Analog input channel is AIN0..AIN7, make sure corresponing P0 I/O pin is enabled. The code + * does NOT disable the pin at the end of this function. I think it is better to leave the pin + * enabled because the results will be more accurate. Because of the inherent capacitance on the + * pin, it takes time for the voltage on the pin to charge up to its steady-state level. If + * HalAdcRead() has to turn on the pin for every conversion, the results may show a lower voltage + * than actuality because the pin did not have time to fully charge. + */ + if (channel < 8) + { + for (i=0; i < channel; i++) + { + adcChannel <<= 1; + } + } + + /* Enable channel */ + ADCCFG |= adcChannel; + + /* Convert resolution to decimation rate */ + switch (resolution) + { + case HAL_ADC_RESOLUTION_8: + resbits = HAL_ADC_DEC_064; + break; + case HAL_ADC_RESOLUTION_10: + resbits = HAL_ADC_DEC_128; + break; + case HAL_ADC_RESOLUTION_12: + resbits = HAL_ADC_DEC_256; + break; + case HAL_ADC_RESOLUTION_14: + default: + resbits = HAL_ADC_DEC_512; + break; + } + + /* writing to this register starts the extra conversion */ + ADCCON3 = channel | resbits | adcRef; + + /* Wait for the conversion to be done */ + while (!(ADCCON1 & HAL_ADC_EOC)); + + /* Disable channel after done conversion */ + ADCCFG &= (adcChannel ^ 0xFF); + + /* Read the result */ + reading = (int16) (ADCL); + reading |= (int16) (ADCH << 8); + + /* Treat small negative as 0 */ + if (reading < 0) + reading = 0; + + switch (resolution) + { + case HAL_ADC_RESOLUTION_8: + reading >>= 8; + break; + case HAL_ADC_RESOLUTION_10: + reading >>= 6; + break; + case HAL_ADC_RESOLUTION_12: + reading >>= 4; + break; + case HAL_ADC_RESOLUTION_14: + default: + reading >>= 2; + break; + } + + +#else + // unused arguments + (void) channel; + (void) resolution; +#endif + + return ((uint16)reading); +} + +/************************************************************************************************** + * @fn HalAdcSetReference + * + * @brief Sets the reference voltage for the ADC and initializes the service + * + * @param reference - the reference voltage to be used by the ADC + * + * @return none + * + **************************************************************************************************/ +void HalAdcSetReference ( uint8 reference ) +{ +#if (HAL_ADC == TRUE) + adcRef = reference; +#endif +} + +/********************************************************************* + * @fn HalAdcCheckVdd + * + * @brief Check for minimum Vdd specified. + * + * @param vdd - The board-specific Vdd reading to check for. + * + * @return TRUE if the Vdd measured is greater than the 'vdd' minimum parameter; + * FALSE if not. + * + *********************************************************************/ +bool HalAdcCheckVdd(uint8 vdd) +{ +#ifdef ADC_CAL + uint16 compAdcVal; +#endif //ADC_CAL + + ADCCON3 = 0x0F; + while (!(ADCCON1 & 0x80)); + +#ifdef ADC_CAL + compAdcVal = HalAdcCompensate (ADCH, HAL_ADC_RESOLUTION_8); + return (compAdcVal > vdd); +#else + return (ADCH > vdd); +#endif //ADC_CAL +} + +/********************************************************************* + * @fn HalAdcCheckVddRaw + * + * @brief Check Vdd. + * + * @param none. + * + * @return value measured + * + *********************************************************************/ +uint8 HalAdcCheckVddRaw( void ) +{ +#ifdef ADC_CAL + uint16 compAdcVal; +#endif //ADC_CAL + + ADCCON3 = 0x0F; + while (!(ADCCON1 & 0x80)); + +#ifdef ADC_CAL + compAdcVal = HalAdcCompensate (ADCH, HAL_ADC_RESOLUTION_8); + return compAdcVal; +#else + return ADCH; +#endif +} + +/************************************************************************************************** +**************************************************************************************************/ diff --git a/include/hal_adc.h b/include/hal_adc.h new file mode 100644 index 0000000..9fb37d3 --- /dev/null +++ b/include/hal_adc.h @@ -0,0 +1,149 @@ +/************************************************************************************************** + Filename: hal_adc.h + Revised: $Date: 2013-03-06 13:50:31 -0800 (Wed, 06 Mar 2013) $ + Revision: $Revision: 33395 $ + + Description: This file contains the interface to the ADC Service. + + + Copyright 2005-2010 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +#ifndef HAL_ADC_H +#define HAL_ADC_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +/************************************************************************************************** + * INCLUDES + **************************************************************************************************/ + +#include "hal_types.h" +#include "hal_board.h" + +/************************************************************************************************** + * CONSTANTS + **************************************************************************************************/ + +/* Resolution */ +#define HAL_ADC_RESOLUTION_8 0x01 +#define HAL_ADC_RESOLUTION_10 0x02 +#define HAL_ADC_RESOLUTION_12 0x03 +#define HAL_ADC_RESOLUTION_14 0x04 + +/* Channels */ +#define HAL_ADC_CHANNEL_0 0x00 +#define HAL_ADC_CHANNEL_1 0x01 +#define HAL_ADC_CHANNEL_2 0x02 +#define HAL_ADC_CHANNEL_3 0x03 +#define HAL_ADC_CHANNEL_4 0x04 +#define HAL_ADC_CHANNEL_5 0x05 +#define HAL_ADC_CHANNEL_6 0x06 +#define HAL_ADC_CHANNEL_7 0x07 + +#define HAL_ADC_CHN_AIN0 0x00 /* AIN0 */ +#define HAL_ADC_CHN_AIN1 0x01 /* AIN1 */ +#define HAL_ADC_CHN_AIN2 0x02 /* AIN2 */ +#define HAL_ADC_CHN_AIN3 0x03 /* AIN3 */ +#define HAL_ADC_CHN_AIN4 0x04 /* AIN4 */ +#define HAL_ADC_CHN_AIN5 0x05 /* AIN5 */ +#define HAL_ADC_CHN_AIN6 0x06 /* AIN6 */ +#define HAL_ADC_CHN_AIN7 0x07 /* AIN7 */ +#define HAL_ADC_CHN_A0A1 0x08 /* AIN0,AIN1 */ +#define HAL_ADC_CHN_A2A3 0x09 /* AIN2,AIN3 */ +#define HAL_ADC_CHN_A4A5 0x0a /* AIN4,AIN5 */ +#define HAL_ADC_CHN_A6A7 0x0b /* AIN6,AIN7 */ +#define HAL_ADC_CHN_GND 0x0c /* GND */ +#define HAL_ADC_CHN_VREF 0x0d /* Positive voltage reference */ +#define HAL_ADC_CHN_TEMP 0x0e /* Temperature sensor */ +#define HAL_ADC_CHN_VDD3 0x0f /* VDD/3 */ +#define HAL_ADC_CHN_BITS 0x0f /* Bits [3:0] */ + +#define HAL_ADC_CHANNEL_TEMP HAL_ADC_CHN_TEMP +#define HAL_ADC_CHANNEL_VDD HAL_ADC_CHN_VDD3 /* channel VDD divided by 3 */ + +/* Vdd Limits */ +#define HAL_ADC_VDD_LIMIT_0 0x00 +#define HAL_ADC_VDD_LIMIT_1 0x01 +#define HAL_ADC_VDD_LIMIT_2 0x02 +#define HAL_ADC_VDD_LIMIT_3 0x03 +#define HAL_ADC_VDD_LIMIT_4 0x04 +#define HAL_ADC_VDD_LIMIT_5 0x05 +#define HAL_ADC_VDD_LIMIT_6 0x06 +#define HAL_ADC_VDD_LIMIT_7 0x07 + +/* Reference Voltages */ +#define HAL_ADC_REF_125V 0x00 /* Internal Reference (1.25V-CC2430)(1.15V-CC2530) */ +#define HAL_ADC_REF_AIN7 0x40 /* AIN7 Reference */ +#define HAL_ADC_REF_AVDD 0x80 /* AVDD_SOC Pin Reference */ +#define HAL_ADC_REF_DIFF 0xc0 /* AIN7,AIN6 Differential Reference */ +#define HAL_ADC_REF_BITS 0xc0 /* Bits [7:6] */ + +/************************************************************************************************** + * FUNCTIONS - API + **************************************************************************************************/ + +/* + * Initialize ADC Service with reference set to default value + */ +extern void HalAdcInit ( void ); + +/* + * Read value from a specified ADC Channel at the given resolution + */ +extern uint16 HalAdcRead ( uint8 channel, uint8 resolution ); + +/* + * Set the reference voltage for the ADC + */ +extern void HalAdcSetReference ( uint8 reference ); + +/* + * Check for minimum Vdd specified. + */ +extern bool HalAdcCheckVdd(uint8 vdd); + +/* + * Check Vdd. + */ +extern uint8 HalAdcCheckVddRaw( void ); + +/************************************************************************************************** +**************************************************************************************************/ + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/hal_aes.h b/include/hal_aes.h new file mode 100644 index 0000000..85a9ea5 --- /dev/null +++ b/include/hal_aes.h @@ -0,0 +1,126 @@ +/************************************************************************************************** + Filename: hal_aes.h + Revised: $Date: 2011-11-08 11:26:03 -0800 (Tue, 08 Nov 2011) $ + Revision: $Revision: 28264 $ + + Description: Support for HW/SW AES encryption. + + + Copyright 2007-2011 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +#ifndef HAL_AES_H_ +#define HAL_AES_H_ + +#include "ZComDef.h" + +#define STATE_BLENGTH 16 // Number of bytes in State +#define KEY_BLENGTH 16 // Number of bytes in Key +#define KEY_EXP_LENGTH 176 // Nb * (Nr+1) * 4 + +/* AES Engine is default to hardware AES. To turn on software AES, #define one of the followings: + * #define SOFTWARE_AES TRUE, uses software aes ( slowest setting ) + * #define SW_AES_AND_KEY_EXP TRUE, enables software aes with key expansion ( improves speed at the cost of 176 bytes of data (RAM) ) + */ +#if ((defined SOFTWARE_AES) && (SOFTWARE_AES == TRUE)) && ((defined SW_AES_AND_KEY_EXP) && (SW_AES_AND_KEY_EXP == TRUE)) +#error "SOFTWARE_AES and SW_AES_AND_KEY_EXP cannot be both defined." +#endif + +extern void HalAesInit( void ); +extern void AesLoadBlock( uint8 * ); +extern void AesStartBlock( uint8 *, uint8 * ); +extern void AesStartShortBlock( uint8 *, uint8 * ); +extern void AesLoadIV(uint8 *); +extern void AesDmaSetup( uint8 *, uint16, uint8 *, uint16 ); +extern void AesLoadKey( uint8 * ); + +extern void (*pSspAesEncrypt)( uint8 *, uint8 * ); +extern void ssp_HW_KeyInit (uint8 *); +extern void sspKeyExpansion (uint8 *, uint8 *); + +extern void sspAesEncryptHW (uint8 *, uint8 *); +extern void sspAesEncryptKeyExp (uint8 *, uint8 *); +extern void sspAesEncryptBasic (uint8 *, uint8 *); +extern void sspAesEncrypt( uint8 *key, uint8 *buf ); + +extern void sspAesDecryptHW( uint8 *, uint8 * ); +extern void sspAesDecrypt( uint8 *key, uint8 *buf ); + +#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 = (ENCCS & ~0x07) | 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 + +/* Used by DMA macros to shift 1 to create a mask for DMA registers. */ +#define HAL_DMA_AES_IN 1 +#define HAL_DMA_AES_OUT 2 + +/* AES registers */ +#define HAL_AES_IN_ADDR 0x70B1 +#define HAL_AES_OUT_ADDR 0x70B2 + +#if !defined (HAL_AES_DMA) || (HAL_AES_DMA == FALSE) +#define HAL_AES_DELAY() \ + do { \ + uint8 delay = 15; \ + while(delay--); \ + } while(0) +#endif + +// End of CC2530 hardware AES engine definitions + +#endif // HAL_AES_H_ diff --git a/include/hal_assert.c b/include/hal_assert.c new file mode 100644 index 0000000..ff5781c --- /dev/null +++ b/include/hal_assert.c @@ -0,0 +1,311 @@ +/************************************************************************************************** + Filename: hal_assert.c + Revised: $Date: 2014-07-23 12:14:30 -0700 (Wed, 23 Jul 2014) $ + Revision: $Revision: 39492 $ + + Description: Describe the purpose and contents of the file. + + + Copyright 2006-2014 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + + +/* ------------------------------------------------------------------------------------------------ + * Includes + * ------------------------------------------------------------------------------------------------ + */ +#include "hal_assert.h" +#include "hal_types.h" +#include "hal_board.h" +#include "hal_defs.h" +#include "hal_mcu.h" + +#if (defined HAL_MCU_AVR) || (defined HAL_MCU_CC2430) || (defined HAL_MCU_CC2530) || \ + (defined HAL_MCU_CC2533) || (defined HAL_MCU_MSP430) + /* for access to debug data */ +#include "mac_rx.h" +#include "mac_tx.h" +#endif + +/* ------------------------------------------------------------------------------------------------ + * Local Prototypes + * ------------------------------------------------------------------------------------------------ + */ +void halAssertHazardLights(void); + + +/************************************************************************************************** + * @fn halAssertHandler + * + * @brief Logic to handle an assert. + * + * @param none + * + * @return none + ************************************************************************************************** + */ +void halAssertHandler( void ) +{ +#if defined( HAL_ASSERT_RESET ) + HAL_SYSTEM_RESET(); +#elif defined ( HAL_ASSERT_LIGHTS ) + halAssertHazardLights(); +#elif defined( HAL_ASSERT_SPIN ) + volatile uint8 i = 1; + HAL_DISABLE_INTERRUPTS(); + while(i); +#endif + + return; +} + +#if !defined ASSERT_WHILE +/************************************************************************************************** + * @fn halAssertHazardLights + * + * @brief Blink LEDs to indicate an error. + * + * @param none + * + * @return none + ************************************************************************************************** + */ +void halAssertHazardLights(void) +{ + enum + { + DEBUG_DATA_RSTACK_HIGH_OFS, + DEBUG_DATA_RSTACK_LOW_OFS, + DEBUG_DATA_TX_ACTIVE_OFS, + DEBUG_DATA_RX_ACTIVE_OFS, + +#if (defined HAL_MCU_AVR) || (defined HAL_MCU_CC2430) + DEBUG_DATA_INT_MASK_OFS, +#elif (defined HAL_MCU_CC2530) || (defined HAL_MCU_CC2533) + DEBUG_DATA_INT_MASK0_OFS, + DEBUG_DATA_INT_MASK1_OFS, +#endif + + DEBUG_DATA_SIZE + }; + + uint8 buttonHeld; + uint8 debugData[DEBUG_DATA_SIZE]; + + /* disable all interrupts before anything else */ + HAL_DISABLE_INTERRUPTS(); + + /*------------------------------------------------------------------------------- + * Initialize LEDs and turn them off. + */ + HAL_BOARD_INIT(); + + HAL_TURN_OFF_LED1(); + HAL_TURN_OFF_LED2(); + HAL_TURN_OFF_LED3(); + HAL_TURN_OFF_LED4(); + + /*------------------------------------------------------------------------------- + * Master infinite loop. + */ + for (;;) + { + buttonHeld = 0; + + /*------------------------------------------------------------------------------- + * "Hazard lights" loop. A held keypress will exit this loop. + */ + do + { + HAL_LED_BLINK_DELAY(); + + /* toggle LEDS, the #ifdefs are in case HAL has logically remapped non-existent LEDs */ +#if (HAL_NUM_LEDS >= 1) + HAL_TOGGLE_LED1(); +#if (HAL_NUM_LEDS >= 2) + HAL_TOGGLE_LED2(); +#if (HAL_NUM_LEDS >= 3) + HAL_TOGGLE_LED3(); +#if (HAL_NUM_LEDS >= 4) + HAL_TOGGLE_LED4(); +#endif +#endif +#endif +#endif + + /* escape hatch to continue execution, set escape to '1' to continue execution */ + { + static uint8 escape = 0; + if (escape) + { + escape = 0; + return; + } + } + + /* break out of loop if button is held long enough */ + if (HAL_PUSH_BUTTON1()) + { + buttonHeld++; + } + else + { + buttonHeld = 0; + } + } + while (buttonHeld != 10); /* loop until button is held specified number of loops */ + + /*------------------------------------------------------------------------------- + * Just exited from "hazard lights" loop. + */ + + /* turn off all LEDs */ + HAL_TURN_OFF_LED1(); + HAL_TURN_OFF_LED2(); + HAL_TURN_OFF_LED3(); + HAL_TURN_OFF_LED4(); + + /* wait for button release */ + HAL_DEBOUNCE(!HAL_PUSH_BUTTON1()); + + /*------------------------------------------------------------------------------- + * Load debug data into memory. + */ +#ifdef HAL_MCU_AVR + { + uint8 * pStack; + pStack = (uint8 *) SP; + pStack++; /* point to return address on stack */ + debugData[DEBUG_DATA_RSTACK_HIGH_OFS] = *pStack; + pStack++; + debugData[DEBUG_DATA_RSTACK_LOW_OFS] = *pStack; + } + debugData[DEBUG_DATA_INT_MASK_OFS] = EIMSK; +#endif + +#if (defined HAL_MCU_CC2430) + debugData[DEBUG_DATA_INT_MASK_OFS] = RFIM; +#elif (defined HAL_MCU_CC2530) || (defined HAL_MCU_CC2533) + debugData[DEBUG_DATA_INT_MASK0_OFS] = RFIRQM0; + debugData[DEBUG_DATA_INT_MASK1_OFS] = RFIRQM1; +#endif + + +#if (defined HAL_MCU_AVR) || (defined HAL_MCU_CC2430) || (defined HAL_MCU_CC2530) || \ + (defined HAL_MCU_CC2533) || (defined HAL_MCU_MSP430) + debugData[DEBUG_DATA_TX_ACTIVE_OFS] = macTxActive; + debugData[DEBUG_DATA_RX_ACTIVE_OFS] = macRxActive; +#endif + + /* initialize for data dump loop */ + { + uint8 iBit; + uint8 iByte; + + iBit = 0; + iByte = 0; + + /*------------------------------------------------------------------------------- + * Data dump loop. A button press cycles data bits to an LED. + */ + while (iByte < DEBUG_DATA_SIZE) + { + /* wait for key press */ + while(!HAL_PUSH_BUTTON1()); + + /* turn on all LEDs for first bit of byte, turn on three LEDs if not first bit */ + HAL_TURN_ON_LED1(); + HAL_TURN_ON_LED2(); + HAL_TURN_ON_LED3(); + if (iBit == 0) + { + HAL_TURN_ON_LED4(); + } + else + { + HAL_TURN_OFF_LED4(); + } + + /* wait for debounced key release */ + HAL_DEBOUNCE(!HAL_PUSH_BUTTON1()); + + /* turn off all LEDs */ + HAL_TURN_OFF_LED1(); + HAL_TURN_OFF_LED2(); + HAL_TURN_OFF_LED3(); + HAL_TURN_OFF_LED4(); + + /* output value of data bit to LED1 */ + if (debugData[iByte] & (1 << (7 - iBit))) + { + HAL_TURN_ON_LED1(); + } + else + { + HAL_TURN_OFF_LED1(); + } + + /* advance to next bit */ + iBit++; + if (iBit == 8) + { + iBit = 0; + iByte++; + } + } + } + + /* + * About to enter "hazard lights" loop again. Turn off LED1 in case the last bit + * displayed happened to be one. This guarantees all LEDs are off at the start of + * the flashing loop which uses a toggle operation to change LED states. + */ + HAL_TURN_OFF_LED1(); + } +} +#endif + +/* ------------------------------------------------------------------------------------------------ + * Compile Time Assertions + * ------------------------------------------------------------------------------------------------ + */ + +/* integrity check of type sizes */ +HAL_ASSERT_SIZE( int8, 1); +HAL_ASSERT_SIZE( uint8, 1); +HAL_ASSERT_SIZE( int16, 2); +HAL_ASSERT_SIZE(uint16, 2); +HAL_ASSERT_SIZE( int32, 4); +HAL_ASSERT_SIZE(uint32, 4); + + +/************************************************************************************************** +*/ diff --git a/include/hal_assert.h b/include/hal_assert.h new file mode 100644 index 0000000..ddfb126 --- /dev/null +++ b/include/hal_assert.h @@ -0,0 +1,110 @@ +/************************************************************************************************** + Filename: hal_assert.h + Revised: $Date: 2009-02-16 18:03:22 -0800 (Mon, 16 Feb 2009) $ + Revision: $Revision: 19172 $ + + Description: Describe the purpose and contents of the file. + + + Copyright 2006-2007 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +#ifndef HAL_ASSERT_H +#define HAL_ASSERT_H + +/* ------------------------------------------------------------------------------------------------ + * Macros + * ------------------------------------------------------------------------------------------------ + */ + +/* + * HAL_ASSERT( expression ) - The given expression must evaluate as "true" or else the assert + * handler is called. From here, the call stack feature of the debugger can pinpoint where + * the problem occurred. + * + * HAL_ASSERT_FORCED( ) - If asserts are in use, immediately calls the assert handler. + * + * HAL_ASSERT_STATEMENT( statement ) - Inserts the given C statement but only when asserts + * are in use. This macros allows debug code that is not part of an expression. + * + * HAL_ASSERT_DECLARATION( declaration ) - Inserts the given C declaration but only when asserts + * are in use. This macros allows debug code that is not part of an expression. + * + * Asserts can be disabled for optimum performance and minimum code size (ideal for + * finalized, debugged production code). To disable, define the preprocessor + * symbol HALNODEBUG at the project level. + */ + + +#ifdef HALNODEBUG +#define HAL_ASSERT(expr) +#define HAL_ASSERT_FORCED() +#define HAL_ASSERT_STATEMENT(statement) +#define HAL_ASSERT_DECLARATION(declaration) +#else +#define HAL_ASSERT(expr) st( if (!( expr )) halAssertHandler(); ) +#define HAL_ASSERT_FORCED() halAssertHandler() +#define HAL_ASSERT_STATEMENT(statement) st( statement ) +#define HAL_ASSERT_DECLARATION(declaration) declaration +#endif + + +/* + * This macro compares the size of the first parameter to the integer value + * of the second parameter. If they do not match, a compile time error for + * negative array size occurs (even gnu chokes on negative array size). + * + * This compare is done by creating a typedef for an array. No variables are + * created and no memory is consumed with this check. The created type is + * used for checking only and is not for use by any other code. The value + * of 10 in this macro is arbitrary, it just needs to be a value larger + * than one to result in a positive number for the array size. + */ +#define HAL_ASSERT_SIZE(x,y) typedef char x ## _assert_size_t[-1+10*(sizeof(x) == (y))] + + +/* ------------------------------------------------------------------------------------------------ + * Prototypes + * ------------------------------------------------------------------------------------------------ + */ +void halAssertHandler(void); + + +/************************************************************************************************** + */ + +/************************************************************************************************** + * FUNCTIONS - API + **************************************************************************************************/ + + +extern void halAssertHazardLights(void); +#endif diff --git a/include/hal_board.h b/include/hal_board.h new file mode 100644 index 0000000..506e100 --- /dev/null +++ b/include/hal_board.h @@ -0,0 +1 @@ +#include "hal_board_cfg.h" diff --git a/include/hal_board_cfg.h b/include/hal_board_cfg.h new file mode 100644 index 0000000..3240f07 --- /dev/null +++ b/include/hal_board_cfg.h @@ -0,0 +1,607 @@ +/************************************************************************************************** + Filename: hal_board_cfg.h + Revised: $Date: 2015-02-17 14:17:44 -0800 (Tue, 17 Feb 2015) $ + Revision: $Revision: 42683 $ + + Description: Declarations for the CC2530EM used on the SmartRF05EB. + + + Copyright 2009-2015 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +#ifndef HAL_BOARD_CFG_H +#define HAL_BOARD_CFG_H + + +/* ------------------------------------------------------------------------------------------------ + * Includes + * ------------------------------------------------------------------------------------------------ + */ + +#include "hal_mcu.h" +#include "hal_defs.h" +#include "hal_types.h" + +/* ------------------------------------------------------------------------------------------------ + * CC2590/CC2591 support + * + * Define HAL_PA_LNA_CC2590 if CC2530+CC2590EM is used + * Define HAL_PA_LNA if CC2530+CC2591EM is used + * Note that only one of them can be defined + * ------------------------------------------------------------------------------------------------ + */ +#define xHAL_PA_LNA +#define xHAL_PA_LNA_CC2590 +#define xHAL_PA_LNA_SE2431L +#define xHAL_PA_LNA_CC2592 + + +/* ------------------------------------------------------------------------------------------------ + * Board Indentifier + * + * Define the Board Identifier to HAL_BOARD_CC2530EB_REV13 for SmartRF05 EB 1.3 board + * ------------------------------------------------------------------------------------------------ + */ + +#if !defined (HAL_BOARD_CC2530EB_REV17) && !defined (HAL_BOARD_CC2530EB_REV13) +#define HAL_BOARD_CC2530EB_REV17 +#endif + +/* ------------------------------------------------------------------------------------------------ + * Clock Speed + * ------------------------------------------------------------------------------------------------ + */ + +#define HAL_CPU_CLOCK_MHZ 32 + +/* This flag should be defined if the SoC uses the 32MHz crystal + * as the main clock source (instead of DCO). + */ +#define HAL_CLOCK_CRYSTAL + +/* 32 kHz clock source select in CLKCONCMD */ +#if !defined (OSC32K_CRYSTAL_INSTALLED) || (defined (OSC32K_CRYSTAL_INSTALLED) && (OSC32K_CRYSTAL_INSTALLED == TRUE)) + #define OSC_32KHZ 0x00 /* external 32 KHz xosc */ +#else + #define OSC_32KHZ 0x80 /* internal 32 KHz rcosc */ +#endif + +#define HAL_CLOCK_STABLE() st( while (CLKCONSTA != (CLKCONCMD_32MHZ | OSC_32KHZ)); ) + +/* ------------------------------------------------------------------------------------------------ + * LED Configuration + * ------------------------------------------------------------------------------------------------ + */ + +#if defined (HAL_BOARD_CC2530EB_REV17) && !defined (HAL_PA_LNA) && \ + !defined (HAL_PA_LNA_CC2590) && !defined (HAL_PA_LNA_SE2431L) && \ + !defined (HAL_PA_LNA_CC2592) + #define HAL_NUM_LEDS 3 +#elif defined (HAL_BOARD_CC2530EB_REV13) || defined (HAL_PA_LNA) || \ + defined (HAL_PA_LNA_CC2590) || defined (HAL_PA_LNA_CC2592) || \ + defined (HAL_PA_LNA_SE2431L) + #define HAL_NUM_LEDS 1 +#else + #error Unknown Board Indentifier +#endif + +#define HAL_LED_BLINK_DELAY() st( { volatile uint32 i; for (i=0; i<0x5800; i++) { }; } ) + +/* 1 - Green */ +#define LED1_BV BV(0) +#define LED1_SBIT P1_0 +#define LED1_DDR P1DIR +#define LED1_POLARITY ACTIVE_HIGH + +#if defined (HAL_BOARD_CC2530EB_REV17) + /* 2 - Red */ + #define LED2_BV BV(1) + #define LED2_SBIT P1_1 + #define LED2_DDR P1DIR + #define LED2_POLARITY ACTIVE_HIGH + + /* 3 - Yellow */ + #define LED3_BV BV(4) + #define LED3_SBIT P1_4 + #define LED3_DDR P1DIR + #define LED3_POLARITY ACTIVE_HIGH + +#ifdef ENABLE_LED4_DISABLE_S1 + /* 4 - Orange */ + #define LED4_BV BV(1) + #define LED4_SBIT P0_1 + #define LED4_DDR P0DIR + #define LED4_POLARITY ACTIVE_HIGH + #define LED4_SET_DIR() do {LED4_DDR |= LED4_BV;} while (0) +#else + #define LED4_SET_DIR() +#endif +#endif + +/* ------------------------------------------------------------------------------------------------ + * Push Button Configuration + * ------------------------------------------------------------------------------------------------ + */ + +#define ACTIVE_LOW ! +#define ACTIVE_HIGH !! /* double negation forces result to be '1' */ + +/* S1 */ +#define PUSH1_BV BV(1) +#define PUSH1_SBIT P0_1 + +#if defined (HAL_BOARD_CC2530EB_REV17) + #define PUSH1_POLARITY ACTIVE_HIGH +#elif defined (HAL_BOARD_CC2530EB_REV13) + #define PUSH1_POLARITY ACTIVE_LOW +#else + #error Unknown Board Indentifier +#endif + +/* Joystick Center Press */ +#define PUSH2_BV BV(0) +#define PUSH2_SBIT P2_0 +#define PUSH2_POLARITY ACTIVE_HIGH + +/* ------------------------------------------------------------------------------------------------ + * LCD Configuration + * ------------------------------------------------------------------------------------------------ + */ + +/* LCD Max Chars and Buffer */ +#define HAL_LCD_MAX_CHARS 16 +#define HAL_LCD_MAX_BUFF 25 + +/* ------------------------------------------------------------------------------------------------ + * OSAL NV implemented by internal flash pages. + * ------------------------------------------------------------------------------------------------ + */ + +// Flash is partitioned into 8 banks of 32 KB or 16 pages. +#define HAL_FLASH_PAGE_PER_BANK 16 +// Flash is constructed of 128 pages of 2 KB. +#define HAL_FLASH_PAGE_SIZE 2048 +#define HAL_FLASH_WORD_SIZE 4 + +// CODE banks get mapped into the XDATA range 8000-FFFF. +#define HAL_FLASH_PAGE_MAP 0x8000 + +// The last 16 bytes of the last available page are reserved for flash lock bits. +// NV page definitions must coincide with segment declaration in project *.xcl file. +#if defined NON_BANKED +#define HAL_FLASH_LOCK_BITS 16 +#define HAL_NV_PAGE_END 30 +#define HAL_NV_PAGE_CNT 2 +#else +#define HAL_FLASH_LOCK_BITS 16 +#define HAL_NV_PAGE_END 126 +#define HAL_NV_PAGE_CNT 6 +#endif + +// Re-defining Z_EXTADDR_LEN here so as not to include a Z-Stack .h file. +#define HAL_FLASH_IEEE_SIZE 8 +#define HAL_FLASH_IEEE_PAGE (HAL_NV_PAGE_END+1) +#define HAL_FLASH_IEEE_OSET (HAL_FLASH_PAGE_SIZE - HAL_FLASH_LOCK_BITS - HAL_FLASH_IEEE_SIZE) +#define HAL_INFOP_IEEE_OSET 0xC + +#define HAL_FLASH_DEV_PRIVATE_KEY_OSET 0x7D2 +#define HAL_FLASH_CA_PUBLIC_KEY_OSET 0x7BC +#define HAL_FLASH_IMPLICIT_CERT_OSET 0x78C + +#define HAL_NV_PAGE_BEG (HAL_NV_PAGE_END-HAL_NV_PAGE_CNT+1) + +// Used by DMA macros to shift 1 to create a mask for DMA registers. +#define HAL_NV_DMA_CH 0 +#define HAL_DMA_CH_RX 3 +#define HAL_DMA_CH_TX 4 + +#define HAL_NV_DMA_GET_DESC() HAL_DMA_GET_DESC0() +#define HAL_NV_DMA_SET_ADDR(a) HAL_DMA_SET_ADDR_DESC0((a)) + +/* ------------------------------------------------------------------------------------------------ + * Serial Boot Loader: reserving the first 4 pages of flash and other memory in cc2530-sb.xcl. + * ------------------------------------------------------------------------------------------------ + */ + +#define HAL_SB_IMG_ADDR 0x2000 +#define HAL_SB_CRC_ADDR 0x2090 +// Size of internal flash less 4 pages for boot loader, 6 pages for NV, & 1 page for lock bits. +#define HAL_SB_IMG_SIZE (0x40000 - 0x2000 - 0x3000 - 0x0800) + +/* ------------------------------------------------------------------------------------------------ + * Macros + * ------------------------------------------------------------------------------------------------ + */ + +/* ----------- RF-frontend Connection Initialization ---------- */ +#if defined HAL_PA_LNA || defined HAL_PA_LNA_CC2590 || \ + defined HAL_PA_LNA_SE2431L || defined HAL_PA_LNA_CC2592 +extern void MAC_RfFrontendSetup(void); +#define HAL_BOARD_RF_FRONTEND_SETUP() MAC_RfFrontendSetup() +#else +#define HAL_BOARD_RF_FRONTEND_SETUP() +#endif + +/* ----------- Cache Prefetch control ---------- */ +#define PREFETCH_ENABLE() st( FCTL = 0x08; ) +#define PREFETCH_DISABLE() st( FCTL = 0x04; ) + +/* ----------- Board Initialization ---------- */ +#if defined (HAL_BOARD_CC2530EB_REV17) && !defined (HAL_PA_LNA) && \ + !defined (HAL_PA_LNA_CC2590) && !defined (HAL_PA_LNA_SE2431L) && \ + !defined (HAL_PA_LNA_CC2592) + +#define HAL_BOARD_INIT() \ +{ \ + uint16 i; \ + \ + SLEEPCMD &= ~OSC_PD; /* turn on 16MHz RC and 32MHz XOSC */ \ + while (!(SLEEPSTA & XOSC_STB)); /* wait for 32MHz XOSC stable */ \ + asm("NOP"); /* chip bug workaround */ \ + for (i=0; i<504; i++) asm("NOP"); /* Require 63us delay for all revs */ \ + CLKCONCMD = (CLKCONCMD_32MHZ | OSC_32KHZ); /* Select 32MHz XOSC and the source for 32K clock */ \ + while (CLKCONSTA != (CLKCONCMD_32MHZ | OSC_32KHZ)); /* Wait for the change to be effective */ \ + SLEEPCMD |= OSC_PD; /* turn off 16MHz RC */ \ + \ + /* Turn on cache prefetch mode */ \ + PREFETCH_ENABLE(); \ + \ + HAL_TURN_OFF_LED1(); \ + LED1_DDR |= LED1_BV; \ + HAL_TURN_OFF_LED2(); \ + LED2_DDR |= LED2_BV; \ + HAL_TURN_OFF_LED3(); \ + LED3_DDR |= LED3_BV; \ + HAL_TURN_OFF_LED4(); \ + LED4_SET_DIR(); \ + \ + /* configure tristates */ \ + P0INP |= PUSH2_BV; \ +} + +#elif defined (HAL_BOARD_CC2530EB_REV13) || defined (HAL_PA_LNA) || \ + defined (HAL_PA_LNA_CC2590) + +#define HAL_BOARD_INIT() \ +{ \ + uint16 i; \ + \ + SLEEPCMD &= ~OSC_PD; /* turn on 16MHz RC and 32MHz XOSC */ \ + while (!(SLEEPSTA & XOSC_STB)); /* wait for 32MHz XOSC stable */ \ + asm("NOP"); /* chip bug workaround */ \ + for (i=0; i<504; i++) asm("NOP"); /* Require 63us delay for all revs */ \ + CLKCONCMD = (CLKCONCMD_32MHZ | OSC_32KHZ); /* Select 32MHz XOSC and the source for 32K clock */ \ + while (CLKCONSTA != (CLKCONCMD_32MHZ | OSC_32KHZ)); /* Wait for the change to be effective */ \ + SLEEPCMD |= OSC_PD; /* turn off 16MHz RC */ \ + \ + /* Turn on cache prefetch mode */ \ + PREFETCH_ENABLE(); \ + \ + /* set direction for GPIO outputs */ \ + LED1_DDR |= LED1_BV; \ + \ + /* Set PA/LNA HGM control P0_7 */ \ + P0DIR |= BV(7); \ + \ + /* configure tristates */ \ + P0INP |= PUSH2_BV; \ + \ + /* setup RF frontend if necessary */ \ + HAL_BOARD_RF_FRONTEND_SETUP(); \ +} + +#elif defined (HAL_PA_LNA_CC2592) || defined (HAL_PA_LNA_SE2431L) + +#define HAL_BOARD_INIT() \ +{ \ + uint16 i; \ + \ + SLEEPCMD &= ~OSC_PD; /* turn on 16MHz RC and 32MHz XOSC */ \ + while (!(SLEEPSTA & XOSC_STB)); /* wait for 32MHz XOSC stable */ \ + asm("NOP"); /* chip bug workaround */ \ + for (i=0; i<504; i++) asm("NOP"); /* Require 63us delay for all revs */ \ + CLKCONCMD = (CLKCONCMD_32MHZ | OSC_32KHZ); /* Select 32MHz XOSC and the source for 32K clock */ \ + while (CLKCONSTA != (CLKCONCMD_32MHZ | OSC_32KHZ)); /* Wait for the change to be effective */ \ + SLEEPCMD |= OSC_PD; /* turn off 16MHz RC */ \ + \ + /* Turn on cache prefetch mode */ \ + PREFETCH_ENABLE(); \ + \ + /* set direction for GPIO outputs */ \ + /* For SE2431L PA LNA this sets ANT_SEL to output */ \ + /* For CC2592 this enables LNA */ \ + P1DIR |= BV(0); \ + \ + LED3_DDR |= LED3_BV; \ + \ + /* Set PA/LNA HGM control P0_7 */ \ + P0DIR |= BV(7); \ + \ + /* configure tristates */ \ + P0INP |= PUSH2_BV; \ + \ + /* setup RF frontend if necessary */ \ + HAL_BOARD_RF_FRONTEND_SETUP(); \ +} +#endif + +/* ----------- Debounce ---------- */ +#define HAL_DEBOUNCE(expr) { int i; for (i=0; i<500; i++) { if (!(expr)) i = 0; } } + +/* ----------- Push Buttons ---------- */ +#define HAL_PUSH_BUTTON1() (PUSH1_POLARITY (PUSH1_SBIT)) +#define HAL_PUSH_BUTTON2() (PUSH2_POLARITY (PUSH2_SBIT)) +#define HAL_PUSH_BUTTON3() (0) +#define HAL_PUSH_BUTTON4() (0) +#define HAL_PUSH_BUTTON5() (0) +#define HAL_PUSH_BUTTON6() (0) + +/* ----------- LED's ---------- */ +#if defined (HAL_BOARD_CC2530EB_REV17) && !defined (HAL_PA_LNA) && \ + !defined (HAL_PA_LNA_CC2590) && !defined (HAL_PA_LNA_SE2431L) && \ + !defined (HAL_PA_LNA_CC2592) + + #define HAL_TURN_OFF_LED1() st( LED1_SBIT = LED1_POLARITY (0); ) + #define HAL_TURN_OFF_LED2() st( LED2_SBIT = LED2_POLARITY (0); ) + #define HAL_TURN_OFF_LED3() st( LED3_SBIT = LED3_POLARITY (0); ) +#ifdef ENABLE_LED4_DISABLE_S1 + #define HAL_TURN_OFF_LED4() st( LED4_SBIT = LED4_POLARITY (0); ) +#else + #define HAL_TURN_OFF_LED4() HAL_TURN_OFF_LED1() +#endif + + #define HAL_TURN_ON_LED1() st( LED1_SBIT = LED1_POLARITY (1); ) + #define HAL_TURN_ON_LED2() st( LED2_SBIT = LED2_POLARITY (1); ) + #define HAL_TURN_ON_LED3() st( LED3_SBIT = LED3_POLARITY (1); ) +#ifdef ENABLE_LED4_DISABLE_S1 + #define HAL_TURN_ON_LED4() st( LED4_SBIT = LED4_POLARITY (1); ) +#else + #define HAL_TURN_ON_LED4() HAL_TURN_ON_LED1() +#endif + + #define HAL_TOGGLE_LED1() st( if (LED1_SBIT) { LED1_SBIT = 0; } else { LED1_SBIT = 1;} ) + #define HAL_TOGGLE_LED2() st( if (LED2_SBIT) { LED2_SBIT = 0; } else { LED2_SBIT = 1;} ) + #define HAL_TOGGLE_LED3() st( if (LED3_SBIT) { LED3_SBIT = 0; } else { LED3_SBIT = 1;} ) +#ifdef ENABLE_LED4_DISABLE_S1 + #define HAL_TOGGLE_LED4() st( if (LED4_SBIT) { LED4_SBIT = 0; } else { LED4_SBIT = 1;} ) +#else + #define HAL_TOGGLE_LED4() HAL_TOGGLE_LED1() +#endif + + #define HAL_STATE_LED1() (LED1_POLARITY (LED1_SBIT)) + #define HAL_STATE_LED2() (LED2_POLARITY (LED2_SBIT)) + #define HAL_STATE_LED3() (LED3_POLARITY (LED3_SBIT)) +#ifdef ENABLE_LED4_DISABLE_S1 + #define HAL_STATE_LED4() (LED4_POLARITY (LED4_SBIT)) +#else + #define HAL_STATE_LED4() HAL_STATE_LED1() +#endif + +#elif defined (HAL_PA_LNA_SE2431L) || defined (HAL_PA_LNA_CC2592) + #define HAL_TURN_OFF_LED3() st( LED3_SBIT = LED3_POLARITY (0); ) + #define HAL_TURN_OFF_LED1() HAL_TURN_OFF_LED3() + #define HAL_TURN_OFF_LED2() HAL_TURN_OFF_LED3() + #define HAL_TURN_OFF_LED4() HAL_TURN_OFF_LED3() + + #define HAL_TURN_ON_LED3() st( LED3_SBIT = LED3_POLARITY (1); ) + #define HAL_TURN_ON_LED1() HAL_TURN_ON_LED3() + #define HAL_TURN_ON_LED2() HAL_TURN_ON_LED3() + #define HAL_TURN_ON_LED4() HAL_TURN_ON_LED3() + + #define HAL_TOGGLE_LED3() st( if (LED3_SBIT) { LED3_SBIT = 0; } else { LED3_SBIT = 1;} ) + #define HAL_TOGGLE_LED1() HAL_TOGGLE_LED3() + #define HAL_TOGGLE_LED2() HAL_TOGGLE_LED3() + #define HAL_TOGGLE_LED4() HAL_TOGGLE_LED3() + + #define HAL_STATE_LED3() (LED3_POLARITY (LED3_SBIT)) + #define HAL_STATE_LED1() HAL_STATE_LED3() + #define HAL_STATE_LED2() HAL_STATE_LED3() + #define HAL_STATE_LED4() HAL_STATE_LED3() + +#elif defined (HAL_BOARD_CC2530EB_REV13) || defined (HAL_PA_LNA) || \ + defined (HAL_PA_LNA_CC2590) + #define HAL_TURN_OFF_LED1() st( LED1_SBIT = LED1_POLARITY (0); ) + #define HAL_TURN_OFF_LED2() HAL_TURN_OFF_LED1() + #define HAL_TURN_OFF_LED3() HAL_TURN_OFF_LED1() + #define HAL_TURN_OFF_LED4() HAL_TURN_OFF_LED1() + + #define HAL_TURN_ON_LED1() st( LED1_SBIT = LED1_POLARITY (1); ) + #define HAL_TURN_ON_LED2() HAL_TURN_ON_LED1() + #define HAL_TURN_ON_LED3() HAL_TURN_ON_LED1() + #define HAL_TURN_ON_LED4() HAL_TURN_ON_LED1() + + #define HAL_TOGGLE_LED1() st( if (LED1_SBIT) { LED1_SBIT = 0; } else { LED1_SBIT = 1;} ) + #define HAL_TOGGLE_LED2() HAL_TOGGLE_LED1() + #define HAL_TOGGLE_LED3() HAL_TOGGLE_LED1() + #define HAL_TOGGLE_LED4() HAL_TOGGLE_LED1() + + #define HAL_STATE_LED1() (LED1_POLARITY (LED1_SBIT)) + #define HAL_STATE_LED2() HAL_STATE_LED1() + #define HAL_STATE_LED3() HAL_STATE_LED1() + #define HAL_STATE_LED4() HAL_STATE_LED1() + +#endif + +/* ----------- XNV ---------- */ +#define XNV_SPI_BEGIN() st(P1_3 = 0;) +#define XNV_SPI_TX(x) st(U1CSR &= ~0x02; U1DBUF = (x);) +#define XNV_SPI_RX() U1DBUF +#define XNV_SPI_WAIT_RXRDY() st(while (!(U1CSR & 0x02));) +#define XNV_SPI_END() st(P1_3 = 1;) + +// The TI reference design uses UART1 Alt. 2 in SPI mode. +#define XNV_SPI_INIT() \ +st( \ + /* Mode select UART1 SPI Mode as master. */\ + U1CSR = 0; \ + \ + /* Setup for 115200 baud. */\ + U1GCR = 11; \ + U1BAUD = 216; \ + \ + /* Set bit order to MSB */\ + U1GCR |= BV(5); \ + \ + /* Set UART1 I/O to alternate 2 location on P1 pins. */\ + PERCFG |= 0x02; /* U1CFG */\ + \ + /* Select peripheral function on I/O pins but SS is left as GPIO for separate control. */\ + P1SEL |= 0xE0; /* SELP1_[7:4] */\ + /* P1.1,2,3: reset, LCD CS, XNV CS. */\ + P1SEL &= ~0x0E; \ + P1 |= 0x0E; \ + P1_1 = 0; \ + P1DIR |= 0x0E; \ + \ + /* Give UART1 priority over Timer3. */\ + P2SEL &= ~0x20; /* PRI2P1 */\ + \ + /* When SPI config is complete, enable it. */\ + U1CSR |= 0x40; \ + /* Release XNV reset. */\ + P1_1 = 1; \ +) + +/* ----------- Minimum safe bus voltage ---------- */ + +// Vdd/3 / Internal Reference X ENOB --> (Vdd / 3) / 1.15 X 127 +#define VDD_2_0 74 // 2.0 V required to safely read/write internal flash. +#define VDD_2_7 100 // 2.7 V required for the Numonyx device. +#define VDD_MIN_RUN (VDD_2_0+4) // VDD_MIN_RUN = VDD_MIN_NV +#define VDD_MIN_NV (VDD_2_0+4) // 5% margin over minimum to survive a page erase and compaction. +#define VDD_MIN_GOOD (VDD_2_0+8) // 10% margin over minimum to survive a page erase and compaction. +#define VDD_MIN_XNV (VDD_2_7+5) // 5% margin over minimum to survive a page erase and compaction. + +/* ------------------------------------------------------------------------------------------------ + * Driver Configuration + * ------------------------------------------------------------------------------------------------ + */ + +/* Set to TRUE enable H/W TIMER usage, FALSE disable it */ +#ifndef HAL_TIMER +#define HAL_TIMER FALSE +#endif + +/* Set to TRUE enable ADC usage, FALSE disable it */ +#ifndef HAL_ADC +#define HAL_ADC TRUE +#endif + +/* Set to TRUE enable DMA usage, FALSE disable it */ +#ifndef HAL_DMA +#define HAL_DMA TRUE +#endif + +/* Set to TRUE enable Flash access, FALSE disable it */ +#ifndef HAL_FLASH +#define HAL_FLASH TRUE +#endif + +/* Set to TRUE enable AES usage, FALSE disable it */ +#ifndef HAL_AES +#define HAL_AES TRUE +#endif + +#ifndef HAL_AES_DMA +#define HAL_AES_DMA TRUE +#endif + +/* Set to TRUE enable LCD usage, FALSE disable it */ +#ifndef HAL_LCD +#define HAL_LCD TRUE +#endif + +/* Set to TRUE enable LED usage, FALSE disable it */ +#ifndef HAL_LED +#define HAL_LED TRUE +#endif +#if (!defined BLINK_LEDS) && (HAL_LED == TRUE) +#define BLINK_LEDS +#endif + +/* Set to TRUE enable KEY usage, FALSE disable it */ +#ifndef HAL_KEY +#define HAL_KEY TRUE +#endif + +/* Set to TRUE enable UART usage, FALSE disable it */ +#ifndef HAL_UART +#if (defined ZAPP_P1) || (defined ZAPP_P2) || (defined ZTOOL_P1) || (defined ZTOOL_P2) +#define HAL_UART TRUE +#else +#define HAL_UART FALSE +#endif +#endif + +#if HAL_UART +#ifndef HAL_UART_DMA +#if HAL_DMA +#if (defined ZAPP_P2) || (defined ZTOOL_P2) +#define HAL_UART_DMA 2 +#else +#define HAL_UART_DMA 1 +#endif +#else +#define HAL_UART_DMA 0 +#endif +#endif + +#ifndef HAL_UART_ISR +#if HAL_UART_DMA // Default preference for DMA over ISR. +#define HAL_UART_ISR 0 +#elif (defined ZAPP_P2) || (defined ZTOOL_P2) +#define HAL_UART_ISR 2 +#else +#define HAL_UART_ISR 1 +#endif +#endif + +#if (HAL_UART_DMA && (HAL_UART_DMA == HAL_UART_ISR)) +#error HAL_UART_DMA & HAL_UART_ISR must be different. +#endif + +// Used to set P2 priority - USART0 over USART1 if both are defined. +#if ((HAL_UART_DMA == 1) || (HAL_UART_ISR == 1)) +#define HAL_UART_PRIPO 0x00 +#else +#define HAL_UART_PRIPO 0x40 +#endif + +#else +#define HAL_UART_DMA 0 +#define HAL_UART_ISR 0 +#endif + +/* USB is not used for CC2530 configuration */ +#define HAL_UART_USB 0 +#endif +/******************************************************************************************************* +*/ diff --git a/include/hal_ccm.h b/include/hal_ccm.h new file mode 100644 index 0000000..2403d09 --- /dev/null +++ b/include/hal_ccm.h @@ -0,0 +1,51 @@ +/************************************************************************************************** + Filename: hal_ccm.h + Revised: $Date: 2007-07-06 10:42:24 -0700 (Fri, 06 Jul 2007) $ + Revision: $Revision: 13579 $ + + Description: Describe the purpose and contents of the file. + + + Copyright 2007-2012 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +#ifndef HAL_CCM_H_ +#define HAL_CCM_H_ + +Status_t SSP_CCM_Auth (uint8, uint8 *, uint8 *, uint16, uint8 *, uint16, uint8 *, uint8 *, uint8); +Status_t SSP_CCM_Encrypt (uint8, uint8 *, uint8 *, uint16, uint8 *, uint8 *, uint8); +Status_t SSP_CCM_Decrypt (uint8, uint8 *, uint8 *, uint16, uint8 *, uint8 *, uint8); +Status_t SSP_CCM_InvAuth (uint8, uint8 *, uint8 *, uint16, uint8 *, uint16, uint8 *, uint8 *, uint8); +Status_t SSP_CCM_Auth_Encrypt (bool, uint8, uint8 *, uint8 *, uint16, uint8 *, uint16, uint8 *, uint8 *, uint8); +Status_t SSP_CCM_InvAuth_Decrypt (bool, uint8, uint8 *, uint8 *, uint16, uint8 *, uint16, uint8 *, uint8 *, uint8); + +#endif // HAL_CCM_H_ + diff --git a/include/hal_defs.h b/include/hal_defs.h new file mode 100644 index 0000000..65070b5 --- /dev/null +++ b/include/hal_defs.h @@ -0,0 +1,146 @@ +/************************************************************************************************** + Filename: hal_defs.h + Revised: $Date: 2014-11-19 13:29:24 -0800 (Wed, 19 Nov 2014) $ + Revision: $Revision: 41175 $ + + Description: This file contains useful macros and data types + + + Copyright 2005-2014 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +#ifndef HAL_DEFS_H +#define HAL_DEFS_H + + +/* ------------------------------------------------------------------------------------------------ + * Macros + * ------------------------------------------------------------------------------------------------ + */ + +#ifndef BV +#define BV(n) (1 << (n)) +#endif + +#ifndef BF +#define BF(x,b,s) (((x) & (b)) >> (s)) +#endif + +#ifndef MIN +#define MIN(n,m) (((n) < (m)) ? (n) : (m)) +#endif + +#ifndef MAX +#define MAX(n,m) (((n) < (m)) ? (m) : (n)) +#endif + +#ifndef ABS +#define ABS(n) (((n) < 0) ? -(n) : (n)) +#endif + + +/* takes a byte out of a uint32 : var - uint32, ByteNum - byte to take out (0 - 3) */ +#define BREAK_UINT32( var, ByteNum ) \ + (uint8)((uint32)(((var) >>((ByteNum) * 8)) & 0x00FF)) + +#define BUILD_UINT32(Byte0, Byte1, Byte2, Byte3) \ + ((uint32)((uint32)((Byte0) & 0x00FF) \ + + ((uint32)((Byte1) & 0x00FF) << 8) \ + + ((uint32)((Byte2) & 0x00FF) << 16) \ + + ((uint32)((Byte3) & 0x00FF) << 24))) + +#define BUILD_UINT16(loByte, hiByte) \ + ((uint16)(((loByte) & 0x00FF) + (((hiByte) & 0x00FF) << 8))) + +#define HI_UINT16(a) (((a) >> 8) & 0xFF) +#define LO_UINT16(a) ((a) & 0xFF) + +#define BUILD_UINT8(hiByte, loByte) \ + ((uint8)(((loByte) & 0x0F) + (((hiByte) & 0x0F) << 4))) + +#define HI_UINT8(a) (((a) >> 4) & 0x0F) +#define LO_UINT8(a) ((a) & 0x0F) + +// Write the 32bit value of 'val' in little endian format to the buffer pointed +// to by pBuf, and increment pBuf by 4 +#define UINT32_TO_BUF_LITTLE_ENDIAN(pBuf,val) \ + do { \ + *(pBuf)++ = ((((uint32)(val)) >> 0) & 0xFF); \ + *(pBuf)++ = ((((uint32)(val)) >> 8) & 0xFF); \ + *(pBuf)++ = ((((uint32)(val)) >> 16) & 0xFF); \ + *(pBuf)++ = ((((uint32)(val)) >> 24) & 0xFF); \ + } while (0) + +// Return the 32bit little-endian formatted value pointed to by pBuf, and increment pBuf by 4 +#define BUF_TO_UINT32_LITTLE_ENDIAN(pBuf) (((pBuf) += 4), BUILD_UINT32((pBuf)[-4], (pBuf)[-3], (pBuf)[-2], (pBuf)[-1])) + +#ifndef CHECK_BIT +#define CHECK_BIT(DISCS, IDX) ((DISCS) & (1<<(IDX))) +#endif +#ifndef GET_BIT +#define GET_BIT(DISCS, IDX) (((DISCS)[((IDX) / 8)] & BV((IDX) % 8)) ? TRUE : FALSE) +#endif +#ifndef SET_BIT +#define SET_BIT(DISCS, IDX) (((DISCS)[((IDX) / 8)] |= BV((IDX) % 8))) +#endif +#ifndef CLR_BIT +#define CLR_BIT(DISCS, IDX) (((DISCS)[((IDX) / 8)] &= (BV((IDX) % 8) ^ 0xFF))) +#endif + +/* + * This macro is for use by other macros to form a fully valid C statement. + * Without this, the if/else conditionals could show unexpected behavior. + * + * For example, use... + * #define SET_REGS() st( ioreg1 = 0; ioreg2 = 0; ) + * instead of ... + * #define SET_REGS() { ioreg1 = 0; ioreg2 = 0; } + * or + * #define SET_REGS() ioreg1 = 0; ioreg2 = 0; + * The last macro would not behave as expected in the if/else construct. + * The second to last macro will cause a compiler error in certain uses + * of if/else construct + * + * It is not necessary, or recommended, to use this macro where there is + * already a valid C statement. For example, the following is redundant... + * #define CALL_FUNC() st( func(); ) + * This should simply be... + * #define CALL_FUNC() func() + * + * (The while condition below evaluates false without generating a + * constant-controlling-loop type of warning on most compilers.) + */ +#define st(x) do { x } while (__LINE__ == -1) + + +/************************************************************************************************** + */ +#endif diff --git a/include/hal_dma.c b/include/hal_dma.c new file mode 100644 index 0000000..d2f8a1d --- /dev/null +++ b/include/hal_dma.c @@ -0,0 +1,167 @@ +/************************************************************************************************** + Filename: hal_dma.c + Revised: $Date: 2010-07-21 16:20:44 -0700 (Wed, 21 Jul 2010) $ + Revision: $Revision: 23090 $ + + Description: This file contains the interface to the DMA. + + + Copyright 2007-2010 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +/********************************************************************* + * INCLUDES + */ + +#include "hal_types.h" +#include "hal_defs.h" +#include "hal_dma.h" +#include "hal_mcu.h" +#include "hal_uart.h" + +#if (defined HAL_IRGEN) && (HAL_IRGEN == TRUE) +#include "hal_irgen.h" +#endif + +#if (defined HAL_SPI) && (HAL_SPI == TRUE) +#include "hal_spi.h" +#endif + + +#if ((defined HAL_DMA) && (HAL_DMA == TRUE)) + +/********************************************************************* + * MACROS + */ + +/********************************************************************* + * CONSTANTS + */ + +/********************************************************************* + * TYPEDEFS + */ + +/********************************************************************* + * GLOBAL VARIABLES + */ + +halDMADesc_t dmaCh0; +halDMADesc_t dmaCh1234[4]; + +/********************************************************************* + * GLOBAL FUNCTIONS + */ + +/********************************************************************* + * LOCAL VARIABLES + */ + +/********************************************************************* + * LOCAL FUNCTIONS + */ + +/****************************************************************************** + * @fn HalDMAInit + * + * @brief DMA Interrupt Service Routine + * + * @param None + * + * @return None + *****************************************************************************/ +void HalDmaInit( void ) +{ + HAL_DMA_SET_ADDR_DESC0( &dmaCh0 ); + HAL_DMA_SET_ADDR_DESC1234( dmaCh1234 ); +#if (HAL_UART_DMA || \ + ((defined HAL_SPI) && (HAL_SPI == TRUE)) || \ + ((defined HAL_IRGEN) && (HAL_IRGEN == TRUE))) + DMAIE = 1; +#endif +} + +#if (HAL_UART_DMA || \ + ((defined HAL_SPI) && (HAL_SPI == TRUE)) || \ + ((defined HAL_IRGEN) && (HAL_IRGEN == TRUE))) +/****************************************************************************** + * @fn HalDMAInit + * + * @brief DMA Interrupt Service Routine + * + * @param None + * + * @return None + *****************************************************************************/ +HAL_ISR_FUNCTION( halDmaIsr, DMA_VECTOR ) +{ + extern void HalUARTIsrDMA(void); + + HAL_ENTER_ISR(); + + DMAIF = 0; + +#if HAL_UART_DMA + if (HAL_DMA_CHECK_IRQ(HAL_DMA_CH_TX)) + { + HalUARTIsrDMA(); + } +#endif // HAL_UART_DMA + +#if (defined HAL_SPI) && (HAL_SPI == TRUE) + if ( HAL_DMA_CHECK_IRQ( HAL_DMA_CH_RX ) ) + { + HAL_DMA_CLEAR_IRQ( HAL_DMA_CH_RX ); + HalSpiRxIsr(); + } + + if ( HAL_DMA_CHECK_IRQ( HAL_DMA_CH_TX ) ) + { + HAL_DMA_CLEAR_IRQ( HAL_DMA_CH_TX ); + HalSpiTxIsr(); + } +#endif // (defined HAL_SPI) && (HAL_SPI == TRUE) + +#if (defined HAL_IRGEN) && (HAL_IRGEN == TRUE) + if ( HAL_IRGEN == TRUE && HAL_DMA_CHECK_IRQ( HAL_IRGEN_DMA_CH ) ) + { + HAL_DMA_CLEAR_IRQ( HAL_IRGEN_DMA_CH ); + HalIrGenDmaIsr(); + } +#endif // (defined HAL_IRGEN) && (HAL_IRGEN == TRUE) + + CLEAR_SLEEP_MODE(); + HAL_EXIT_ISR(); +} +#endif +#endif // #if ((defined HAL_DMA) && (HAL_DMA == TRUE)) +/****************************************************************************** +******************************************************************************/ diff --git a/include/hal_dma.h b/include/hal_dma.h new file mode 100644 index 0000000..98c9e25 --- /dev/null +++ b/include/hal_dma.h @@ -0,0 +1,302 @@ +/************************************************************************************************** + Filename: hal_dma.h + Revised: $Date: 2011-05-31 11:57:28 -0700 (Tue, 31 May 2011) $ + Revision: $Revision: 26163 $ + + Description: This file contains the interface to the DMA Service. + + + Copyright 2007-2011 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +#ifndef HAL_DMA_H +#define HAL_DMA_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +/********************************************************************* + * INCLUDES + */ + +#include "hal_board.h" +#include "hal_types.h" + +#if ((defined HAL_DMA) && (HAL_DMA == TRUE)) + +/********************************************************************* + * MACROS + */ + +#define HAL_DMA_SET_ADDR_DESC0( a ) \ + st( \ + DMA0CFGH = (uint8)( (uint16)(a) >> 8 ); \ + DMA0CFGL = (uint8)( (uint16)(a) & 0xFF ); \ + ) + +#define HAL_DMA_SET_ADDR_DESC1234( a ) \ + st( \ + DMA1CFGH = (uint8)( (uint16)(a) >> 8 ); \ + DMA1CFGL = (uint8)( (uint16)(a) & 0xFF ); \ + ) + +#define HAL_DMA_GET_DESC0() &dmaCh0 + +#define HAL_DMA_GET_DESC1234( a ) (dmaCh1234+((a)-1)) + +#define HAL_DMA_ARM_CH( ch ) DMAARM = (0x01 << (ch)) + +#define HAL_DMA_CH_ARMED( ch ) (DMAARM & (0x01 << (ch))) + +#define HAL_DMA_ABORT_CH( ch ) DMAARM = (0x80 | (0x01 << (ch))) +#define HAL_DMA_MAN_TRIGGER( ch ) DMAREQ = (0x01 << (ch)) +#define HAL_DMA_START_CH( ch ) HAL_DMA_MAN_TRIGGER( (ch) ) + +#define HAL_DMA_CLEAR_IRQ( ch ) DMAIRQ = ((0x01 << (ch)) ^ 0xFF) + +#define HAL_DMA_CHECK_IRQ( ch ) (DMAIRQ & (0x01 << (ch))) + +// Macro for quickly setting the source address of a DMA structure. +#define HAL_DMA_SET_SOURCE( pDesc, src ) \ + st( \ + pDesc->srcAddrH = (uint8)( (uint16)(src) >> 8 ); \ + pDesc->srcAddrL = (uint8)( (uint16)(src) & 0xFF ); \ + ) + +// Macro for quickly setting the destination address of a DMA structure. +#define HAL_DMA_SET_DEST( pDesc, dst ) \ + st( \ + pDesc->dstAddrH = (uint8)( (uint16)(dst) >> 8 ); \ + pDesc->dstAddrL = (uint8)( (uint16)(dst) & 0xFF ); \ + ) + +// Macro for quickly setting the number of bytes to be transferred by the DMA, +// max length is 0x1FFF. +#define HAL_DMA_SET_LEN( pDesc, len ) \ + st( \ + pDesc->xferLenL = (uint8)( (uint16)(len) & 0xFF); \ + pDesc->xferLenV &= ~HAL_DMA_LEN_H; \ + pDesc->xferLenV |= (uint8)((uint16)(len) >> 8); \ + ) + +#define HAL_DMA_GET_LEN( pDesc ) \ + (((uint16)(pDesc->xferLenV & HAL_DMA_LEN_H) << 8) | pDesc->xferLenL) + +#define HAL_DMA_SET_VLEN( pDesc, vMode ) \ + st( \ + pDesc->xferLenV &= ~HAL_DMA_LEN_V; \ + pDesc->xferLenV |= (vMode << 5); \ + ) + +#define HAL_DMA_SET_WORD_SIZE( pDesc, xSz ) \ + st( \ + pDesc->ctrlA &= ~HAL_DMA_WORD_SIZE; \ + pDesc->ctrlA |= (xSz << 7); \ + ) + +#define HAL_DMA_SET_TRIG_MODE( pDesc, tMode ) \ + st( \ + pDesc->ctrlA &= ~HAL_DMA_TRIG_MODE; \ + pDesc->ctrlA |= (tMode << 5); \ + ) + +#define HAL_DMA_GET_TRIG_MODE( pDesc ) ((pDesc->ctrlA >> 5) & 0x3) + +#define HAL_DMA_SET_TRIG_SRC( pDesc, tSrc ) \ + st( \ + pDesc->ctrlA &= ~HAL_DMA_TRIG_SRC; \ + pDesc->ctrlA |= tSrc; \ + ) + +#define HAL_DMA_SET_SRC_INC( pDesc, srcInc ) \ + st( \ + pDesc->ctrlB &= ~HAL_DMA_SRC_INC; \ + pDesc->ctrlB |= (srcInc << 6); \ + ) + +#define HAL_DMA_SET_DST_INC( pDesc, dstInc ) \ + st( \ + pDesc->ctrlB &= ~HAL_DMA_DST_INC; \ + pDesc->ctrlB |= (dstInc << 4); \ + ) + +#define HAL_DMA_SET_IRQ( pDesc, enable ) \ + st( \ + pDesc->ctrlB &= ~HAL_DMA_IRQ_MASK; \ + pDesc->ctrlB |= (enable << 3); \ + ) + +#define HAL_DMA_SET_M8( pDesc, m8 ) \ + st( \ + pDesc->ctrlB &= ~HAL_DMA_M8; \ + pDesc->ctrlB |= (m8 << 2); \ + ) + +#define HAL_DMA_SET_PRIORITY( pDesc, pri ) \ + st( \ + pDesc->ctrlB &= ~HAL_DMA_PRIORITY; \ + pDesc->ctrlB |= pri; \ + ) + +/********************************************************************* + * CONSTANTS + */ + +// Use LEN for transfer count +#define HAL_DMA_VLEN_USE_LEN 0x00 +// Transfer the first byte + the number of bytes indicated by the first byte +#define HAL_DMA_VLEN_1_P_VALOFFIRST 0x01 +// Transfer the number of bytes indicated by the first byte (starting with the first byte) +#define HAL_DMA_VLEN_VALOFFIRST 0x02 +// Transfer the first byte + the number of bytes indicated by the first byte + 1 more byte +#define HAL_DMA_VLEN_1_P_VALOFFIRST_P_1 0x03 +// Transfer the first byte + the number of bytes indicated by the first byte + 2 more bytes +#define HAL_DMA_VLEN_1_P_VALOFFIRST_P_2 0x04 + +#define HAL_DMA_WORDSIZE_BYTE 0x00 /* Transfer a byte at a time. */ +#define HAL_DMA_WORDSIZE_WORD 0x01 /* Transfer a 16-bit word at a time. */ + +#define HAL_DMA_TMODE_SINGLE 0x00 /* Transfer a single byte/word after each DMA trigger. */ +#define HAL_DMA_TMODE_BLOCK 0x01 /* Transfer block of data (length len) after each DMA trigger. */ +#define HAL_DMA_TMODE_SINGLE_REPEATED 0x02 /* Transfer single byte/word (after len transfers, rearm DMA). */ +#define HAL_DMA_TMODE_BLOCK_REPEATED 0x03 /* Transfer block of data (after len transfers, rearm DMA). */ + +#define HAL_DMA_TRIG_NONE 0 /* No trigger, setting DMAREQ.DMAREQx bit starts transfer. */ +#define HAL_DMA_TRIG_PREV 1 /* DMA channel is triggered by completion of previous channel. */ +#define HAL_DMA_TRIG_T1_CH0 2 /* Timer 1, compare, channel 0. */ +#define HAL_DMA_TRIG_T1_CH1 3 /* Timer 1, compare, channel 1. */ +#define HAL_DMA_TRIG_T1_CH2 4 /* Timer 1, compare, channel 2. */ +#define HAL_DMA_TRIG_T2_COMP 5 /* Timer 2, compare. */ +#define HAL_DMA_TRIG_T2_OVFL 6 /* Timer 2, overflow. */ +#define HAL_DMA_TRIG_T3_CH0 7 /* Timer 3, compare, channel 0. */ +#define HAL_DMA_TRIG_T3_CH1 8 /* Timer 3, compare, channel 1. */ +#define HAL_DMA_TRIG_T4_CH0 9 /* Timer 4, compare, channel 0. */ +#define HAL_DMA_TRIG_T4_CH1 10 /* Timer 4, compare, channel 1. */ +#define HAL_DMA_TRIG_ST 11 /* Sleep Timer compare. */ +#define HAL_DMA_TRIG_IOC_0 12 /* Port 0 I/O pin input transition. */ +#define HAL_DMA_TRIG_IOC_1 13 /* Port 1 I/O pin input transition. */ +#define HAL_DMA_TRIG_URX0 14 /* USART0 RX complete. */ +#define HAL_DMA_TRIG_UTX0 15 /* USART0 TX complete. */ +#define HAL_DMA_TRIG_URX1 16 /* USART1 RX complete. */ +#define HAL_DMA_TRIG_UTX1 17 /* USART1 TX complete. */ +#define HAL_DMA_TRIG_FLASH 18 /* Flash data write complete. */ +#define HAL_DMA_TRIG_RADIO 19 /* RF packet byte received/transmit. */ +#define HAL_DMA_TRIG_ADC_CHALL 20 /* ADC end of a conversion in a sequence, sample ready. */ +#define HAL_DMA_TRIG_ADC_CH0 21 /* ADC end of conversion channel 0 in sequence, sample ready. */ +#define HAL_DMA_TRIG_ADC_CH1 22 /* ADC end of conversion channel 1 in sequence, sample ready. */ +#define HAL_DMA_TRIG_ADC_CH2 23 /* ADC end of conversion channel 2 in sequence, sample ready. */ +#define HAL_DMA_TRIG_ADC_CH3 24 /* ADC end of conversion channel 3 in sequence, sample ready. */ +#define HAL_DMA_TRIG_ADC_CH4 25 /* ADC end of conversion channel 4 in sequence, sample ready. */ +#define HAL_DMA_TRIG_ADC_CH5 26 /* ADC end of conversion channel 5 in sequence, sample ready. */ +#define HAL_DMA_TRIG_ADC_CH6 27 /* ADC end of conversion channel 6 in sequence, sample ready. */ +#define HAL_DMA_TRIG_ADC_CH7 28 /* ADC end of conversion channel 7 in sequence, sample ready. */ +#define HAL_DMA_TRIG_ENC_DW 29 /* AES encryption processor requests download input data. */ +#define HAL_DMA_TRIG_ENC_UP 30 /* AES encryption processor requests upload output data. */ + +#define HAL_DMA_SRCINC_0 0x00 /* Increment source pointer by 0 bytes/words after each transfer. */ +#define HAL_DMA_SRCINC_1 0x01 /* Increment source pointer by 1 bytes/words after each transfer. */ +#define HAL_DMA_SRCINC_2 0x02 /* Increment source pointer by 2 bytes/words after each transfer. */ +#define HAL_DMA_SRCINC_M1 0x03 /* Decrement source pointer by 1 bytes/words after each transfer. */ + +#define HAL_DMA_DSTINC_0 0x00 /* Increment destination pointer by 0 bytes/words after each transfer. */ +#define HAL_DMA_DSTINC_1 0x01 /* Increment destination pointer by 1 bytes/words after each transfer. */ +#define HAL_DMA_DSTINC_2 0x02 /* Increment destination pointer by 2 bytes/words after each transfer. */ +#define HAL_DMA_DSTINC_M1 0x03 /* Decrement destination pointer by 1 bytes/words after each transfer. */ + +#define HAL_DMA_IRQMASK_DISABLE 0x00 /* Disable interrupt generation. */ +#define HAL_DMA_IRQMASK_ENABLE 0x01 /* Enable interrupt generation upon DMA channel done. */ + +#define HAL_DMA_M8_USE_8_BITS 0x00 /* Use all 8 bits for transfer count. */ +#define HAL_DMA_M8_USE_7_BITS 0x01 /* Use 7 LSB for transfer count. */ + +#define HAL_DMA_PRI_LOW 0x00 /* Low, CPU has priority. */ +#define HAL_DMA_PRI_GUARANTEED 0x01 /* Guaranteed, DMA at least every second try. */ +#define HAL_DMA_PRI_HIGH 0x02 /* High, DMA has priority. */ +#define HAL_DMA_PRI_ABSOLUTE 0x03 /* Highest, DMA has priority. Reserved for DMA port access.. */ + +#define HAL_DMA_MAX_ARM_CLOCKS 45 // Maximum number of clocks required if arming all 5 at once. + +/********************************************************************* + * TYPEDEFS + */ + +// Bit fields of the 'lenModeH' +#define HAL_DMA_LEN_V 0xE0 +#define HAL_DMA_LEN_H 0x1F + +// Bit fields of the 'ctrlA' +#define HAL_DMA_WORD_SIZE 0x80 +#define HAL_DMA_TRIG_MODE 0x60 +#define HAL_DMA_TRIG_SRC 0x1F + +// Bit fields of the 'ctrlB' +#define HAL_DMA_SRC_INC 0xC0 +#define HAL_DMA_DST_INC 0x30 +#define HAL_DMA_IRQ_MASK 0x08 +#define HAL_DMA_M8 0x04 +#define HAL_DMA_PRIORITY 0x03 + +typedef struct { + uint8 srcAddrH; + uint8 srcAddrL; + uint8 dstAddrH; + uint8 dstAddrL; + uint8 xferLenV; + uint8 xferLenL; + uint8 ctrlA; + uint8 ctrlB; +} halDMADesc_t; + +/********************************************************************* + * GLOBAL VARIABLES + */ + +extern halDMADesc_t dmaCh0; +extern halDMADesc_t dmaCh1234[4]; + +/********************************************************************* + * FUNCTIONS - API + */ + +void HalDmaInit( void ); + +#endif // #if (defined HAL_DMA) && (HAL_DMA == TRUE) + +#ifdef __cplusplus +} +#endif + +#endif // #ifndef HAL_DMA_H + +/****************************************************************************** +******************************************************************************/ diff --git a/include/hal_drivers.c b/include/hal_drivers.c new file mode 100644 index 0000000..535dd10 --- /dev/null +++ b/include/hal_drivers.c @@ -0,0 +1,297 @@ +/************************************************************************************************** + Filename: hal_drivers.c + Revised: $Date: 2007-07-06 10:42:24 -0700 (Fri, 06 Jul 2007) $ + Revision: $Revision: 13579 $ + + Description: This file contains the interface to the Drivers Service. + + + Copyright 2005-2013 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +/************************************************************************************************** + * INCLUDES + **************************************************************************************************/ + +#include "hal_adc.h" +#if (defined HAL_AES) && (HAL_AES == TRUE) +#include "hal_aes.h" +#endif +#if (defined HAL_BUZZER) && (HAL_BUZZER == TRUE) +#include "hal_buzzer.h" +#endif +#if (defined HAL_DMA) && (HAL_DMA == TRUE) +#include "hal_dma.h" +#endif +#include "hal_drivers.h" +#include "hal_key.h" +#include "hal_lcd.h" +#include "hal_led.h" +#include "hal_sleep.h" +#include "hal_timer.h" +#include "hal_types.h" +#include "hal_uart.h" +#ifdef CC2591_COMPRESSION_WORKAROUND +#include "mac_rx.h" +#endif +#include "OSAL.h" +#if defined POWER_SAVING +#include "OSAL_PwrMgr.h" +#endif +#if (defined HAL_HID) && (HAL_HID == TRUE) +#include "usb_hid.h" +#endif +#if (defined HAL_SPI) && (HAL_SPI == TRUE) +#include "hal_spi.h" +#endif + +/************************************************************************************************** + * GLOBAL VARIABLES + **************************************************************************************************/ +uint8 Hal_TaskID; + +extern void HalLedUpdate( void ); /* Notes: This for internal only so it shouldn't be in hal_led.h */ + +/************************************************************************************************** + * @fn Hal_Init + * + * @brief Hal Initialization function. + * + * @param task_id - Hal TaskId + * + * @return None + **************************************************************************************************/ +void Hal_Init( uint8 task_id ) +{ + /* Register task ID */ + Hal_TaskID = task_id; + +#ifdef CC2591_COMPRESSION_WORKAROUND + osal_start_reload_timer( Hal_TaskID, PERIOD_RSSI_RESET_EVT, PERIOD_RSSI_RESET_TIMEOUT ); +#endif +} + +/************************************************************************************************** + * @fn Hal_DriverInit + * + * @brief Initialize HW - These need to be initialized before anyone. + * + * @param task_id - Hal TaskId + * + * @return None + **************************************************************************************************/ +void HalDriverInit (void) +{ + /* TIMER */ +#if (defined HAL_TIMER) && (HAL_TIMER == TRUE) +#endif + + /* ADC */ +#if (defined HAL_ADC) && (HAL_ADC == TRUE) + HalAdcInit(); +#endif + + /* DMA */ +#if (defined HAL_DMA) && (HAL_DMA == TRUE) + // Must be called before the init call to any module that uses DMA. + HalDmaInit(); +#endif + + /* AES */ +#if (defined HAL_AES) && (HAL_AES == TRUE) + HalAesInit(); +#endif + + /* LCD */ +#if (defined HAL_LCD) && (HAL_LCD == TRUE) + HalLcdInit(); +#endif + + /* LED */ +#if (defined HAL_LED) && (HAL_LED == TRUE) + HalLedInit(); +#endif + + /* UART */ +#if (defined HAL_UART) && (HAL_UART == TRUE) + HalUARTInit(); +#endif + + /* KEY */ +#if (defined HAL_KEY) && (HAL_KEY == TRUE) + HalKeyInit(); +#endif + + /* SPI */ +#if (defined HAL_SPI) && (HAL_SPI == TRUE) + HalSpiInit(); +#endif + + /* HID */ +#if (defined HAL_HID) && (HAL_HID == TRUE) + usbHidInit(); +#endif +} + +/************************************************************************************************** + * @fn Hal_ProcessEvent + * + * @brief Hal Process Event + * + * @param task_id - Hal TaskId + * events - events + * + * @return None + **************************************************************************************************/ +uint16 Hal_ProcessEvent( uint8 task_id, uint16 events ) +{ + uint8 *msgPtr; + + (void)task_id; // Intentionally unreferenced parameter + + if ( events & SYS_EVENT_MSG ) + { + msgPtr = osal_msg_receive(Hal_TaskID); + + while (msgPtr) + { + /* Do something here - for now, just deallocate the msg and move on */ + + /* De-allocate */ + osal_msg_deallocate( msgPtr ); + /* Next */ + msgPtr = osal_msg_receive( Hal_TaskID ); + } + return events ^ SYS_EVENT_MSG; + } + +#if (defined HAL_BUZZER) && (HAL_BUZZER == TRUE) + if (events & HAL_BUZZER_EVENT) + { + HalBuzzerStop(); + return events ^ HAL_BUZZER_EVENT; + } +#endif + +#ifdef CC2591_COMPRESSION_WORKAROUND + if ( events & PERIOD_RSSI_RESET_EVT ) + { + macRxResetRssi(); + return (events ^ PERIOD_RSSI_RESET_EVT); + } +#endif + + if ( events & HAL_LED_BLINK_EVENT ) + { +#if (defined (BLINK_LEDS)) && (HAL_LED == TRUE) + HalLedUpdate(); +#endif /* BLINK_LEDS && HAL_LED */ + return events ^ HAL_LED_BLINK_EVENT; + } + + if (events & HAL_KEY_EVENT) + { +#if (defined HAL_KEY) && (HAL_KEY == TRUE) + /* Check for keys */ + HalKeyPoll(); + + /* if interrupt disabled, do next polling */ + if (!Hal_KeyIntEnable) + { + osal_start_timerEx( Hal_TaskID, HAL_KEY_EVENT, 100); + } +#endif + return events ^ HAL_KEY_EVENT; + } + +#if defined POWER_SAVING + if ( events & HAL_SLEEP_TIMER_EVENT ) + { + halRestoreSleepLevel(); + return events ^ HAL_SLEEP_TIMER_EVENT; + } + + if ( events & HAL_PWRMGR_HOLD_EVENT ) + { + (void)osal_pwrmgr_task_state(Hal_TaskID, PWRMGR_HOLD); + + (void)osal_stop_timerEx(Hal_TaskID, HAL_PWRMGR_CONSERVE_EVENT); + (void)osal_clear_event(Hal_TaskID, HAL_PWRMGR_CONSERVE_EVENT); + + return (events & ~(HAL_PWRMGR_HOLD_EVENT | HAL_PWRMGR_CONSERVE_EVENT)); + } + + if ( events & HAL_PWRMGR_CONSERVE_EVENT ) + { + (void)osal_pwrmgr_task_state(Hal_TaskID, PWRMGR_CONSERVE); + return events ^ HAL_PWRMGR_CONSERVE_EVENT; + } +#endif + + return 0; +} + +/************************************************************************************************** + * @fn Hal_ProcessPoll + * + * @brief This routine will be called by OSAL to poll UART, TIMER... + * + * @param task_id - Hal TaskId + * + * @return None + **************************************************************************************************/ +void Hal_ProcessPoll () +{ +#if defined( POWER_SAVING ) + /* Allow sleep before the next OSAL event loop */ + ALLOW_SLEEP_MODE(); +#endif + + /* UART Poll */ +#if (defined HAL_UART) && (HAL_UART == TRUE) + HalUARTPoll(); +#endif + + /* SPI Poll */ +#if (defined HAL_SPI) && (HAL_SPI == TRUE) + HalSpiPoll(); +#endif + + /* HID poll */ +#if (defined HAL_HID) && (HAL_HID == TRUE) + usbHidProcessEvents(); +#endif + +} + +/************************************************************************************************** +**************************************************************************************************/ + diff --git a/include/hal_drivers.h b/include/hal_drivers.h new file mode 100644 index 0000000..847a12d --- /dev/null +++ b/include/hal_drivers.h @@ -0,0 +1,105 @@ +/************************************************************************************************** + Filename: hal_drivers.h + Revised: $Date: 2012-07-09 13:23:30 -0700 (Mon, 09 Jul 2012) $ + Revision: $Revision: 30873 $ + + Description: This file contains the interface to the Drivers service. + + + Copyright 2005-2012 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ +#ifndef HAL_DRIVER_H +#define HAL_DRIVER_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +/************************************************************************************************** + * INCLUDES + **************************************************************************************************/ + +#include "hal_types.h" + +/************************************************************************************************** + * CONSTANTS + **************************************************************************************************/ + +#define HAL_BUZZER_EVENT 0x0080 +#define PERIOD_RSSI_RESET_EVT 0x0040 +#define HAL_LED_BLINK_EVENT 0x0020 +#define HAL_KEY_EVENT 0x0010 + +#if defined POWER_SAVING +#define HAL_SLEEP_TIMER_EVENT 0x0004 +#define HAL_PWRMGR_HOLD_EVENT 0x0002 +#define HAL_PWRMGR_CONSERVE_EVENT 0x0001 +#endif + +#define HAL_PWRMGR_CONSERVE_DELAY 10 +#define PERIOD_RSSI_RESET_TIMEOUT 10 + +/************************************************************************************************** + * GLOBAL VARIABLES + **************************************************************************************************/ + +extern uint8 Hal_TaskID; + +/************************************************************************************************** + * FUNCTIONS - API + **************************************************************************************************/ + +extern void Hal_Init ( uint8 task_id ); + +/* + * Process Serial Buffer + */ +extern uint16 Hal_ProcessEvent ( uint8 task_id, uint16 events ); + +/* + * Process Polls + */ +extern void Hal_ProcessPoll (void); + +/* + * Initialize HW + */ +extern void HalDriverInit (void); + +#ifdef __cplusplus +} +#endif + +#endif + +/************************************************************************************************** +**************************************************************************************************/ diff --git a/include/hal_flash.c b/include/hal_flash.c new file mode 100644 index 0000000..37f757a --- /dev/null +++ b/include/hal_flash.c @@ -0,0 +1,171 @@ +/************************************************************************************************** + Filename: hal_flash.c + Revised: $Date: 2010-10-07 02:19:52 -0700 (Thu, 07 Oct 2010) $ + Revision: $Revision: 24049 $ + + Description: This file contains the interface to the H/W Flash driver. + + + Copyright 2006-2010 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +/* ------------------------------------------------------------------------------------------------ + * Includes + * ------------------------------------------------------------------------------------------------ + */ + +#include "hal_board_cfg.h" +#include "hal_dma.h" +#include "hal_flash.h" +#include "hal_mcu.h" +#include "hal_types.h" + +/************************************************************************************************** + * @fn HalFlashRead + * + * @brief This function reads 'cnt' bytes from the internal flash. + * + * input parameters + * + * @param pg - A valid flash page number. + * @param offset - A valid offset into the page. + * @param buf - A valid buffer space at least as big as the 'cnt' parameter. + * @param cnt - A valid number of bytes to read. + * + * output parameters + * + * None. + * + * @return None. + ************************************************************************************************** + */ +void HalFlashRead(uint8 pg, uint16 offset, uint8 *buf, uint16 cnt) +{ + // Calculate the offset into the containing flash bank as it gets mapped into XDATA. + uint8 *pData = (uint8 *)(offset + HAL_FLASH_PAGE_MAP) + + ((pg % HAL_FLASH_PAGE_PER_BANK) * HAL_FLASH_PAGE_SIZE); + uint8 memctr = MEMCTR; // Save to restore. + +#if (!defined HAL_OAD_BOOT_CODE) && (!defined HAL_OTA_BOOT_CODE) + halIntState_t is; +#endif + + pg /= HAL_FLASH_PAGE_PER_BANK; // Calculate the flash bank from the flash page. + +#if (!defined HAL_OAD_BOOT_CODE) && (!defined HAL_OTA_BOOT_CODE) + HAL_ENTER_CRITICAL_SECTION(is); +#endif + + // Calculate and map the containing flash bank into XDATA. + MEMCTR = (MEMCTR & 0xF8) | pg; + + while (cnt--) + { + *buf++ = *pData++; + } + + MEMCTR = memctr; + +#if (!defined HAL_OAD_BOOT_CODE) && (!defined HAL_OTA_BOOT_CODE) + HAL_EXIT_CRITICAL_SECTION(is); +#endif +} + +/************************************************************************************************** + * @fn HalFlashWrite + * + * @brief This function writes 'cnt' bytes to the internal flash. + * + * input parameters + * + * @param addr - Valid HAL flash write address: actual addr / 4 and quad-aligned. + * @param buf - Valid buffer space at least as big as 'cnt' X 4. + * @param cnt - Number of 4-byte blocks to write. + * + * output parameters + * + * None. + * + * @return None. + ************************************************************************************************** + */ +void HalFlashWrite(uint16 addr, uint8 *buf, uint16 cnt) +{ +#if (defined HAL_DMA) && (HAL_DMA == TRUE) + halDMADesc_t *ch = HAL_NV_DMA_GET_DESC(); + + HAL_DMA_SET_SOURCE(ch, buf); + HAL_DMA_SET_DEST(ch, &FWDATA); + HAL_DMA_SET_VLEN(ch, HAL_DMA_VLEN_USE_LEN); + HAL_DMA_SET_LEN(ch, (cnt * HAL_FLASH_WORD_SIZE)); + HAL_DMA_SET_WORD_SIZE(ch, HAL_DMA_WORDSIZE_BYTE); + HAL_DMA_SET_TRIG_MODE(ch, HAL_DMA_TMODE_SINGLE); + HAL_DMA_SET_TRIG_SRC(ch, HAL_DMA_TRIG_FLASH); + HAL_DMA_SET_SRC_INC(ch, HAL_DMA_SRCINC_1); + HAL_DMA_SET_DST_INC(ch, HAL_DMA_DSTINC_0); + // The DMA is to be polled and shall not issue an IRQ upon completion. + HAL_DMA_SET_IRQ(ch, HAL_DMA_IRQMASK_DISABLE); + HAL_DMA_SET_M8( ch, HAL_DMA_M8_USE_8_BITS); + HAL_DMA_SET_PRIORITY(ch, HAL_DMA_PRI_HIGH); + HAL_DMA_CLEAR_IRQ(HAL_NV_DMA_CH); + HAL_DMA_ARM_CH(HAL_NV_DMA_CH); + + FADDRL = (uint8)addr; + FADDRH = (uint8)(addr >> 8); + FCTL |= 0x02; // Trigger the DMA writes. + while (FCTL & 0x80); // Wait until writing is done. +#endif +} + +/************************************************************************************************** + * @fn HalFlashErase + * + * @brief This function erases the specified page of the internal flash. + * + * input parameters + * + * @param pg - A valid flash page number to erase. + * + * output parameters + * + * None. + * + * @return None. + ************************************************************************************************** + */ +void HalFlashErase(uint8 pg) +{ + FADDRH = pg * (HAL_FLASH_PAGE_SIZE / HAL_FLASH_WORD_SIZE / 256); + FCTL |= 0x01; +} + +/************************************************************************************************** +*/ diff --git a/include/hal_flash.h b/include/hal_flash.h new file mode 100644 index 0000000..209358b --- /dev/null +++ b/include/hal_flash.h @@ -0,0 +1,122 @@ +/************************************************************************************************** + Filename: hal_flash.h + Revised: $Date:$ + Revision: $Revision:$ + + Description: This file contains the interface to the Flash Service. + + + Copyright 2005-2010 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +#ifndef HAL_FLASH_H +#define HAL_FLASH_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +/* ------------------------------------------------------------------------------------------------ + * Includes + * ------------------------------------------------------------------------------------------------ + */ + +#include "hal_board.h" +#include "hal_types.h" + +/************************************************************************************************** + * @fn HalFlashRead + * + * @brief This function reads 'cnt' bytes from the internal flash. + * + * input parameters + * + * @param pg - Valid HAL flash page number (ie < 128). + * @param offset - Valid offset into the page (so < HAL_NV_PAGE_SIZE and byte-aligned is ok). + * @param buf - Valid buffer space at least as big as the 'cnt' parameter. + * @param cnt - Valid number of bytes to read: a read cannot cross into the next 32KB bank. + * + * output parameters + * + * None. + * + * @return None. + ************************************************************************************************** + */ +void HalFlashRead(uint8 pg, uint16 offset, uint8 *buf, uint16 cnt); + +/************************************************************************************************** + * @fn HalFlashWrite + * + * @brief This function reads 'cnt' bytes from the internal flash. + * + * input parameters + * + * @param addr - Valid HAL flash write address: actual addr / 4 and quad-aligned. + * @param buf - Valid buffer space at least as big as the 'cnt' parameter. + * @param cnt - Valid number of bytes to write: a write cannot cross into the next 32KB bank. + * + * output parameters + * + * None. + * + * @return None. + ************************************************************************************************** + */ +void HalFlashWrite(uint16 addr, uint8 *buf, uint16 cnt); + +/************************************************************************************************** + * @fn HalFlashErase + * + * @brief This function erases 'cnt' pages of the internal flash. + * + * input parameters + * + * @param pg - Valid HAL flash page number (ie < 128) to erase. + * + * output parameters + * + * None. + * + * @return None. + ************************************************************************************************** + */ +void HalFlashErase(uint8 pg); + +#ifdef __cplusplus +}; +#endif + +#endif + +/************************************************************************************************** +*/ diff --git a/include/hal_key.c b/include/hal_key.c new file mode 100644 index 0000000..10c90da --- /dev/null +++ b/include/hal_key.c @@ -0,0 +1,560 @@ +/************************************************************************************************** + Filename: hal_key.c + Revised: $Date: 2010-09-15 19:02:45 -0700 (Wed, 15 Sep 2010) $ + Revision: $Revision: 23815 $ + + Description: This file contains the interface to the HAL KEY Service. + + + Copyright 2006-2010 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ +/********************************************************************* + NOTE: If polling is used, the hal_driver task schedules the KeyRead() + to occur every 100ms. This should be long enough to naturally + debounce the keys. The KeyRead() function remembers the key + state of the previous poll and will only return a non-zero + value if the key state changes. + + NOTE: If interrupts are used, the KeyRead() function is scheduled + 25ms after the interrupt occurs by the ISR. This delay is used + for key debouncing. The ISR disables any further Key interrupt + until KeyRead() is executed. KeyRead() will re-enable Key + interrupts after executing. Unlike polling, when interrupts + are enabled, the previous key state is not remembered. This + means that KeyRead() will return the current state of the keys + (not a change in state of the keys). + + NOTE: If interrupts are used, the KeyRead() fucntion is scheduled by + the ISR. Therefore, the joystick movements will only be detected + during a pushbutton interrupt caused by S1 or the center joystick + pushbutton. + + NOTE: When a switch like S1 is pushed, the S1 signal goes from a normally + high state to a low state. This transition is typically clean. The + duration of the low state is around 200ms. When the signal returns + to the high state, there is a high likelihood of signal bounce, which + causes a unwanted interrupts. Normally, we would set the interrupt + edge to falling edge to generate an interrupt when S1 is pushed, but + because of the signal bounce, it is better to set the edge to rising + edge to generate an interrupt when S1 is released. The debounce logic + can then filter out the signal bounce. The result is that we typically + get only 1 interrupt per button push. This mechanism is not totally + foolproof because occasionally, signal bound occurs during the falling + edge as well. A similar mechanism is used to handle the joystick + pushbutton on the DB. For the EB, we do not have independent control + of the interrupt edge for the S1 and center joystick pushbutton. As + a result, only one or the other pushbuttons work reasonably well with + interrupts. The default is the make the S1 switch on the EB work more + reliably. + +*********************************************************************/ + +/************************************************************************************************** + * INCLUDES + **************************************************************************************************/ +#include "hal_mcu.h" +#include "hal_defs.h" +#include "hal_types.h" +#include "hal_board.h" +#include "hal_drivers.h" +#include "hal_adc.h" +#include "hal_key.h" +#include "osal.h" + +#if (defined HAL_KEY) && (HAL_KEY == TRUE) + +/************************************************************************************************** + * MACROS + **************************************************************************************************/ + +/************************************************************************************************** + * CONSTANTS + **************************************************************************************************/ +#define HAL_KEY_RISING_EDGE 0 +#define HAL_KEY_FALLING_EDGE 1 + +#define HAL_KEY_DEBOUNCE_VALUE 25 + +/* CPU port interrupt */ +#define HAL_KEY_CPU_PORT_0_IF P0IF +#define HAL_KEY_CPU_PORT_2_IF P2IF + +/* SW_6 is at P0.1 */ +#define HAL_KEY_SW_6_PORT P0 +#define HAL_KEY_SW_6_BIT BV(1) +#define HAL_KEY_SW_6_SEL P0SEL +#define HAL_KEY_SW_6_DIR P0DIR + +/* edge interrupt */ +#define HAL_KEY_SW_6_EDGEBIT BV(0) +#define HAL_KEY_SW_6_EDGE HAL_KEY_FALLING_EDGE + + +/* SW_6 interrupts */ +#define HAL_KEY_SW_6_IEN IEN1 /* CPU interrupt mask register */ +#define HAL_KEY_SW_6_IENBIT BV(5) /* Mask bit for all of Port_0 */ +#define HAL_KEY_SW_6_ICTL P0IEN /* Port Interrupt Control register */ +#define HAL_KEY_SW_6_ICTLBIT BV(1) /* P0IEN - P0.1 enable/disable bit */ +#define HAL_KEY_SW_6_PXIFG P0IFG /* Interrupt flag at source */ + +/* Joy stick move at P2.0 */ +#define HAL_KEY_JOY_MOVE_PORT P2 +#define HAL_KEY_JOY_MOVE_BIT BV(0) +#define HAL_KEY_JOY_MOVE_SEL P2SEL +#define HAL_KEY_JOY_MOVE_DIR P2DIR + +/* edge interrupt */ +#define HAL_KEY_JOY_MOVE_EDGEBIT BV(3) +#define HAL_KEY_JOY_MOVE_EDGE HAL_KEY_FALLING_EDGE + +/* Joy move interrupts */ +#define HAL_KEY_JOY_MOVE_IEN IEN2 /* CPU interrupt mask register */ +#define HAL_KEY_JOY_MOVE_IENBIT BV(1) /* Mask bit for all of Port_2 */ +#define HAL_KEY_JOY_MOVE_ICTL P2IEN /* Port Interrupt Control register */ +#define HAL_KEY_JOY_MOVE_ICTLBIT BV(0) /* P2IENL - P2.0<->P2.3 enable/disable bit */ +#define HAL_KEY_JOY_MOVE_PXIFG P2IFG /* Interrupt flag at source */ + +#define HAL_KEY_JOY_CHN HAL_ADC_CHANNEL_6 + + +/************************************************************************************************** + * TYPEDEFS + **************************************************************************************************/ + + +/************************************************************************************************** + * GLOBAL VARIABLES + **************************************************************************************************/ +static uint8 halKeySavedKeys; /* used to store previous key state in polling mode */ +static halKeyCBack_t pHalKeyProcessFunction; +static uint8 HalKeyConfigured; +bool Hal_KeyIntEnable; /* interrupt enable/disable flag */ + +/************************************************************************************************** + * FUNCTIONS - Local + **************************************************************************************************/ +void halProcessKeyInterrupt(void); +uint8 halGetJoyKeyInput(void); + + + +/************************************************************************************************** + * FUNCTIONS - API + **************************************************************************************************/ + + +/************************************************************************************************** + * @fn HalKeyInit + * + * @brief Initilize Key Service + * + * @param none + * + * @return None + **************************************************************************************************/ +void HalKeyInit( void ) +{ + /* Initialize previous key to 0 */ + halKeySavedKeys = 0; + + HAL_KEY_SW_6_SEL &= ~(HAL_KEY_SW_6_BIT); /* Set pin function to GPIO */ + +#if ! defined ENABLE_LED4_DISABLE_S1 + HAL_KEY_SW_6_DIR &= ~(HAL_KEY_SW_6_BIT); /* Set pin direction to Input */ +#endif + + HAL_KEY_JOY_MOVE_SEL &= ~(HAL_KEY_JOY_MOVE_BIT); /* Set pin function to GPIO */ + HAL_KEY_JOY_MOVE_DIR &= ~(HAL_KEY_JOY_MOVE_BIT); /* Set pin direction to Input */ + + + /* Initialize callback function */ + pHalKeyProcessFunction = NULL; + + /* Start with key is not configured */ + HalKeyConfigured = FALSE; +} + + +/************************************************************************************************** + * @fn HalKeyConfig + * + * @brief Configure the Key serivce + * + * @param interruptEnable - TRUE/FALSE, enable/disable interrupt + * cback - pointer to the CallBack function + * + * @return None + **************************************************************************************************/ +void HalKeyConfig (bool interruptEnable, halKeyCBack_t cback) +{ + /* Enable/Disable Interrupt or */ + Hal_KeyIntEnable = interruptEnable; + + /* Register the callback fucntion */ + pHalKeyProcessFunction = cback; + + /* Determine if interrupt is enable or not */ + if (Hal_KeyIntEnable) + { + /* Rising/Falling edge configuratinn */ + + PICTL &= ~(HAL_KEY_SW_6_EDGEBIT); /* Clear the edge bit */ + /* For falling edge, the bit must be set. */ + #if (HAL_KEY_SW_6_EDGE == HAL_KEY_FALLING_EDGE) + PICTL |= HAL_KEY_SW_6_EDGEBIT; + #endif + + + /* Interrupt configuration: + * - Enable interrupt generation at the port + * - Enable CPU interrupt + * - Clear any pending interrupt + */ + HAL_KEY_SW_6_ICTL |= HAL_KEY_SW_6_ICTLBIT; + HAL_KEY_SW_6_IEN |= HAL_KEY_SW_6_IENBIT; + HAL_KEY_SW_6_PXIFG = ~(HAL_KEY_SW_6_BIT); + + + + /* Rising/Falling edge configuratinn */ + + HAL_KEY_JOY_MOVE_ICTL &= ~(HAL_KEY_JOY_MOVE_EDGEBIT); /* Clear the edge bit */ + /* For falling edge, the bit must be set. */ + #if (HAL_KEY_JOY_MOVE_EDGE == HAL_KEY_FALLING_EDGE) + HAL_KEY_JOY_MOVE_ICTL |= HAL_KEY_JOY_MOVE_EDGEBIT; + #endif + + + /* Interrupt configuration: + * - Enable interrupt generation at the port + * - Enable CPU interrupt + * - Clear any pending interrupt + */ + HAL_KEY_JOY_MOVE_ICTL |= HAL_KEY_JOY_MOVE_ICTLBIT; + HAL_KEY_JOY_MOVE_IEN |= HAL_KEY_JOY_MOVE_IENBIT; + HAL_KEY_JOY_MOVE_PXIFG = ~(HAL_KEY_JOY_MOVE_BIT); + + + /* Do this only after the hal_key is configured - to work with sleep stuff */ + if (HalKeyConfigured == TRUE) + { + osal_stop_timerEx(Hal_TaskID, HAL_KEY_EVENT); /* Cancel polling if active */ + } + } + else /* Interrupts NOT enabled */ + { + HAL_KEY_SW_6_ICTL &= ~(HAL_KEY_SW_6_ICTLBIT); /* don't generate interrupt */ + HAL_KEY_SW_6_IEN &= ~(HAL_KEY_SW_6_IENBIT); /* Clear interrupt enable bit */ + + osal_set_event(Hal_TaskID, HAL_KEY_EVENT); + } + + /* Key now is configured */ + HalKeyConfigured = TRUE; +} + + +/************************************************************************************************** + * @fn HalKeyRead + * + * @brief Read the current value of a key + * + * @param None + * + * @return keys - current keys status + **************************************************************************************************/ +uint8 HalKeyRead ( void ) +{ + uint8 keys = 0; + + if (HAL_PUSH_BUTTON1()) + { + keys |= HAL_KEY_SW_6; + } + + if ((HAL_KEY_JOY_MOVE_PORT & HAL_KEY_JOY_MOVE_BIT)) /* Key is active low */ + { + keys |= halGetJoyKeyInput(); + } + + return keys; +} + + +/************************************************************************************************** + * @fn HalKeyPoll + * + * @brief Called by hal_driver to poll the keys + * + * @param None + * + * @return None + **************************************************************************************************/ +void HalKeyPoll (void) +{ + uint8 keys = 0; + + if ((HAL_KEY_JOY_MOVE_PORT & HAL_KEY_JOY_MOVE_BIT)) /* Key is active HIGH */ + { + keys = halGetJoyKeyInput(); + } + + /* If interrupts are not enabled, previous key status and current key status + * are compared to find out if a key has changed status. + */ + if (!Hal_KeyIntEnable) + { + if (keys == halKeySavedKeys) + { + /* Exit - since no keys have changed */ + return; + } + /* Store the current keys for comparation next time */ + halKeySavedKeys = keys; + } + else + { + /* Key interrupt handled here */ + } + + if (HAL_PUSH_BUTTON1()) + { + keys |= HAL_KEY_SW_6; + } + + /* Invoke Callback if new keys were depressed */ + if (pHalKeyProcessFunction +#ifdef HAL_LEGACY_KEYS + && keys //in legacy modes, only report key presses and do not report when a key is released +#endif + ) + { + (pHalKeyProcessFunction) (keys, HAL_KEY_STATE_NORMAL); + } +} + +/************************************************************************************************** + * @fn halGetJoyKeyInput + * + * @brief Map the ADC value to its corresponding key. + * + * @param None + * + * @return keys - current joy key status + **************************************************************************************************/ +uint8 halGetJoyKeyInput(void) +{ + /* The joystick control is encoded as an analog voltage. + * Read the JOY_LEVEL analog value and map it to joy movement. + */ + uint8 adc; + uint8 ksave0 = 0; + uint8 ksave1; + + /* Keep on reading the ADC until two consecutive key decisions are the same. */ + do + { + ksave1 = ksave0; /* save previouse key reading */ + + adc = HalAdcRead (HAL_KEY_JOY_CHN, HAL_ADC_RESOLUTION_8); + + if ((adc >= 2) && (adc <= 38)) + { + ksave0 |= HAL_KEY_UP; + } + else if ((adc >= 74) && (adc <= 88)) + { + ksave0 |= HAL_KEY_RIGHT; + } + else if ((adc >= 60) && (adc <= 73)) + { + ksave0 |= HAL_KEY_LEFT; + } + else if ((adc >= 39) && (adc <= 59)) + { + ksave0 |= HAL_KEY_DOWN; + } + else if ((adc >= 89) && (adc <= 100)) + { + ksave0 |= HAL_KEY_CENTER; + } + } while (ksave0 != ksave1); + + return ksave0; +} + + + + + +/************************************************************************************************** + * @fn halProcessKeyInterrupt + * + * @brief Checks to see if it's a valid key interrupt, saves interrupt driven key states for + * processing by HalKeyRead(), and debounces keys by scheduling HalKeyRead() 25ms later. + * + * @param + * + * @return + **************************************************************************************************/ +void halProcessKeyInterrupt (void) +{ + bool valid=FALSE; + + if (HAL_KEY_SW_6_PXIFG & HAL_KEY_SW_6_BIT) /* Interrupt Flag has been set */ + { + HAL_KEY_SW_6_PXIFG = ~(HAL_KEY_SW_6_BIT); /* Clear Interrupt Flag */ + valid = TRUE; + } + + if (HAL_KEY_JOY_MOVE_PXIFG & HAL_KEY_JOY_MOVE_BIT) /* Interrupt Flag has been set */ + { + HAL_KEY_JOY_MOVE_PXIFG = ~(HAL_KEY_JOY_MOVE_BIT); /* Clear Interrupt Flag */ + valid = TRUE; + } + + if (valid) + { + osal_start_timerEx (Hal_TaskID, HAL_KEY_EVENT, HAL_KEY_DEBOUNCE_VALUE); + } +} + +/************************************************************************************************** + * @fn HalKeyEnterSleep + * + * @brief - Get called to enter sleep mode + * + * @param + * + * @return + **************************************************************************************************/ +void HalKeyEnterSleep ( void ) +{ +} + +/************************************************************************************************** + * @fn HalKeyExitSleep + * + * @brief - Get called when sleep is over + * + * @param + * + * @return - return saved keys + **************************************************************************************************/ +uint8 HalKeyExitSleep ( void ) +{ + /* Wake up and read keys */ + return ( HalKeyRead () ); +} + +/*************************************************************************************************** + * INTERRUPT SERVICE ROUTINE + ***************************************************************************************************/ + +/************************************************************************************************** + * @fn halKeyPort0Isr + * + * @brief Port0 ISR + * + * @param + * + * @return + **************************************************************************************************/ +HAL_ISR_FUNCTION( halKeyPort0Isr, P0INT_VECTOR ) +{ + HAL_ENTER_ISR(); + + if (HAL_KEY_SW_6_PXIFG & HAL_KEY_SW_6_BIT) + { + halProcessKeyInterrupt(); + } + + /* + Clear the CPU interrupt flag for Port_0 + PxIFG has to be cleared before PxIF + */ + HAL_KEY_SW_6_PXIFG = 0; + HAL_KEY_CPU_PORT_0_IF = 0; + + CLEAR_SLEEP_MODE(); + HAL_EXIT_ISR(); +} + + +/************************************************************************************************** + * @fn halKeyPort2Isr + * + * @brief Port2 ISR + * + * @param + * + * @return + **************************************************************************************************/ +HAL_ISR_FUNCTION( halKeyPort2Isr, P2INT_VECTOR ) +{ + HAL_ENTER_ISR(); + + if (HAL_KEY_JOY_MOVE_PXIFG & HAL_KEY_JOY_MOVE_BIT) + { + halProcessKeyInterrupt(); + } + + /* + Clear the CPU interrupt flag for Port_2 + PxIFG has to be cleared before PxIF + Notes: P2_1 and P2_2 are debug lines. + */ + HAL_KEY_JOY_MOVE_PXIFG = 0; + HAL_KEY_CPU_PORT_2_IF = 0; + + CLEAR_SLEEP_MODE(); + HAL_EXIT_ISR(); +} + +#else + + +void HalKeyInit(void){} +void HalKeyConfig(bool interruptEnable, halKeyCBack_t cback){} +uint8 HalKeyRead(void){ return 0;} +void HalKeyPoll(void){} + +#endif /* HAL_KEY */ + + + + + +/************************************************************************************************** +**************************************************************************************************/ + + + diff --git a/include/hal_key.h b/include/hal_key.h new file mode 100644 index 0000000..6ba1b61 --- /dev/null +++ b/include/hal_key.h @@ -0,0 +1,152 @@ +/************************************************************************************************** + Filename: hal_key.h + Revised: $Date: 2007-07-06 10:42:24 -0700 (Fri, 06 Jul 2007) $ + Revision: $Revision: 13579 $ + + Description: This file contains the interface to the KEY Service. + + + Copyright 2005-2012 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +#ifndef HAL_KEY_H +#define HAL_KEY_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +/************************************************************************************************** + * INCLUDES + **************************************************************************************************/ +#include "hal_board.h" + +/************************************************************************************************** + * MACROS + **************************************************************************************************/ + +/************************************************************************************************** + * CONSTANTS + **************************************************************************************************/ + +/* Interrupt option - Enable or disable */ +#define HAL_KEY_INTERRUPT_DISABLE 0x00 +#define HAL_KEY_INTERRUPT_ENABLE 0x01 + +/* Key state - shift or nornal */ +#define HAL_KEY_STATE_NORMAL 0x00 +#define HAL_KEY_STATE_SHIFT 0x01 + +#define HAL_KEY_SW_1 0x01 // Joystick up +#define HAL_KEY_SW_2 0x02 // Joystick right +#define HAL_KEY_SW_5 0x04 // Joystick center +#define HAL_KEY_SW_4 0x08 // Joystick left +#define HAL_KEY_SW_3 0x10 // Joystick down + +#define HAL_KEY_SW_6 0x20 // Button S1 if available +#define HAL_KEY_SW_7 0x40 // Button S2 if available + +/* Joystick */ +#define HAL_KEY_UP 0x01 // Joystick up +#define HAL_KEY_RIGHT 0x02 // Joystick right +#define HAL_KEY_CENTER 0x04 // Joystick center +#define HAL_KEY_LEFT 0x08 // Joystick left +#define HAL_KEY_DOWN 0x10 // Joystick down + +/* Buttons */ +#define HAL_PUSH_BUTTON_RIGHT 0x01 // Button right +#define HAL_PUSH_BUTTON_LEFT 0x02 // Button left +#define HAL_PUSH_BUTTON_SELECT 0x04 // Button select +#define HAL_KEY_BUTTON_UP 0x40 // Button up +#define HAL_KEY_BUTTON_DOWN 0x80 // Button down + +/************************************************************************************************** + * TYPEDEFS + **************************************************************************************************/ +typedef void (*halKeyCBack_t) (uint8 keys, uint8 state); + +/************************************************************************************************** + * GLOBAL VARIABLES + **************************************************************************************************/ +extern bool Hal_KeyIntEnable; + +/************************************************************************************************** + * FUNCTIONS - API + **************************************************************************************************/ + +/* + * Initialize the Key Service + */ +extern void HalKeyInit( void ); + +/* + * Configure the Key Service + */ +extern void HalKeyConfig( bool interruptEnable, const halKeyCBack_t cback); + +/* + * Read the Key status + */ +extern uint8 HalKeyRead( void); + +/* + * Enter sleep mode, store important values + */ +extern void HalKeyEnterSleep ( void ); + +/* + * Exit sleep mode, retore values + */ +extern uint8 HalKeyExitSleep ( void ); + +/* + * This is for internal used by hal_driver + */ +extern void HalKeyPoll ( void ); + +/* + * This is for internal used by hal_sleep + */ +extern bool HalKeyPressed( void ); + +extern uint8 hal_key_keys(void); + +extern uint8 hal_key_int_keys(void); + +/************************************************************************************************** +**************************************************************************************************/ + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/hal_lcd.c b/include/hal_lcd.c new file mode 100644 index 0000000..beb9d42 --- /dev/null +++ b/include/hal_lcd.c @@ -0,0 +1,763 @@ +/************************************************************************************************** + Filename: hal_lcd.c + Revised: $Date: 2012-09-30 16:36:36 -0700 (Sun, 30 Sep 2012) $ + Revision: $Revision: 31658 $ + + Description: This file contains the interface to the HAL LCD Service. + + + Copyright 2007-2012 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +/************************************************************************************************** + * INCLUDES + **************************************************************************************************/ +#include "hal_types.h" +#include "hal_lcd.h" +#include "OSAL.h" +#include "OnBoard.h" +#include "hal_assert.h" + +#if defined (ZTOOL_P1) || defined (ZTOOL_P2) + #include "DebugTrace.h" +#endif + +/************************************************************************************************** + * CONSTANTS + **************************************************************************************************/ +/* + LCD pins + + //control + P0.0 - LCD_MODE + P1.1 - LCD_FLASH_RESET + P1.2 - LCD_CS + + //spi + P1.5 - CLK + P1.6 - MOSI + P1.7 - MISO +*/ + +/* LCD Control lines */ +#define HAL_LCD_MODE_PORT 0 +#define HAL_LCD_MODE_PIN 0 + +#define HAL_LCD_RESET_PORT 1 +#define HAL_LCD_RESET_PIN 1 + +#define HAL_LCD_CS_PORT 1 +#define HAL_LCD_CS_PIN 2 + +/* LCD SPI lines */ +#define HAL_LCD_CLK_PORT 1 +#define HAL_LCD_CLK_PIN 5 + +#define HAL_LCD_MOSI_PORT 1 +#define HAL_LCD_MOSI_PIN 6 + +#define HAL_LCD_MISO_PORT 1 +#define HAL_LCD_MISO_PIN 7 + +/* SPI settings */ +#define HAL_SPI_CLOCK_POL_LO 0x00 +#define HAL_SPI_CLOCK_PHA_0 0x00 +#define HAL_SPI_TRANSFER_MSB_FIRST 0x20 + +/* LCD lines */ +#define LCD_MAX_LINE_COUNT 3 + +/* Defines for HW LCD */ + +/* Set power save mode */ +#define OSC_OFF 0x00 +#define OSC_ON 0x01 +#define POWER_SAVE_OFF 0x00 +#define POWER_SAVE_ON 0x02 +#define SET_POWER_SAVE_MODE(options) HalLcd_HW_Control(0x0C | (options)) + +/* Function Set */ +#define CGROM 0x00 +#define CGRAM 0x01 +#define COM_FORWARD 0x00 +#define COM_BACKWARD 0x02 +#define TWO_LINE 0x00 +#define THREE_LINE 0x04 +#define FUNCTION_SET(options) HalLcd_HW_Control(0x10 | (options)) + +/* Set Display Start Line */ +#define LINE1 0x00 +#define LINE2 0x01 +#define LINE3 0x02 +#define LINE4 0x03 +#define SET_DISPLAY_START_LINE(line) HalLcd_HW_Control(0x18 | (line)) + +/* Bias control */ +#define BIAS_1_5 0x00 +#define BIAS_1_4 0x01 +#define SET_BIAS_CTRL(bias) HalLcd_HW_Control(0x1C | (bias)) + +/* Power control */ +#define VOLTAGE_DIVIDER_OFF 0x00 +#define VOLTAGE_DIVIDER_ON 0x01 +#define CONVERTER_AND_REG_OFF 0x00 +#define CONVERTER_AND_REG_ON 0x04 +#define SET_POWER_CTRL(options) HalLcd_HW_Control(0x20 | (options)) + +// Set display control +#define DISPLAY_CTRL_ON 0x01 +#define DISPLAY_CTRL_OFF 0x00 +#define DISPLAY_CTRL_BLINK_ON 0x02 +#define DISPLAY_CTRL_BLINK_OFF 0x00 +#define DISPLAY_CTRL_CURSOR_ON 0x04 +#define DISPLAY_CTRL_CURSOR_OFF 0x00 +#define SET_DISPLAY_CTRL(options) HalLcd_HW_Control(0x28 | (options)) + +/* Set DD/ CGRAM address */ +#define SET_DDRAM_ADDR(charIndex) HalLcd_HW_Control(0x80 | (charIndex)) +#define SET_GCRAM_CHAR(specIndex) HalLcd_HW_Control(0xC0 | (specIndex)) + +/* Set ICONRAM address */ +#define CONTRAST_CTRL_REGISTER 0x10 +#define SET_ICONRAM_ADDR(addr) HalLcd_HW_Control(0x40 | (addr)) + +/* Set double height */ +#define LINE_1_AND_2 0x01 +#define LINE_2_AND_3 0x02 +#define NORMAL_DISPLAY 0x00 +#define SET_DOUBLE_HEIGHT(options) HalLcd_HW_Control(0x08 | (options)) + +/************************************************************************************************** + * MACROS + **************************************************************************************************/ + +#define HAL_IO_SET(port, pin, val) HAL_IO_SET_PREP(port, pin, val) +#define HAL_IO_SET_PREP(port, pin, val) st( P##port##_##pin = val; ) + +#define HAL_CONFIG_IO_OUTPUT(port, pin, val) HAL_CONFIG_IO_OUTPUT_PREP(port, pin, val) +#define HAL_CONFIG_IO_OUTPUT_PREP(port, pin, val) st( P##port##SEL &= ~BV(pin); \ + P##port##_##pin = val; \ + P##port##DIR |= BV(pin); ) + +#define HAL_CONFIG_IO_PERIPHERAL(port, pin) HAL_CONFIG_IO_PERIPHERAL_PREP(port, pin) +#define HAL_CONFIG_IO_PERIPHERAL_PREP(port, pin) st( P##port##SEL |= BV(pin); ) + + + +/* SPI interface control */ +#define LCD_SPI_BEGIN() HAL_IO_SET(HAL_LCD_CS_PORT, HAL_LCD_CS_PIN, 0); /* chip select */ +#define LCD_SPI_END() \ +{ \ + asm("NOP"); \ + asm("NOP"); \ + asm("NOP"); \ + asm("NOP"); \ + HAL_IO_SET(HAL_LCD_CS_PORT, HAL_LCD_CS_PIN, 1); /* chip select */ \ +} +/* clear the received and transmit byte status, write tx data to buffer, wait till transmit done */ +#define LCD_SPI_TX(x) { U1CSR &= ~(BV(2) | BV(1)); U1DBUF = x; while( !(U1CSR & BV(1)) ); } +#define LCD_SPI_WAIT_RXRDY() { while(!(U1CSR & BV(1))); } + + +/* Control macros */ +#define LCD_DO_WRITE() HAL_IO_SET(HAL_LCD_MODE_PORT, HAL_LCD_MODE_PIN, 1); +#define LCD_DO_CONTROL() HAL_IO_SET(HAL_LCD_MODE_PORT, HAL_LCD_MODE_PIN, 0); + +#define LCD_ACTIVATE_RESET() HAL_IO_SET(HAL_LCD_RESET_PORT, HAL_LCD_RESET_PIN, 0); +#define LCD_RELEASE_RESET() HAL_IO_SET(HAL_LCD_RESET_PORT, HAL_LCD_RESET_PIN, 1); + +#if (HAL_LCD == TRUE) +/************************************************************************************************** + * LOCAL VARIABLES + **************************************************************************************************/ + +static uint8 *Lcd_Line1; + +/************************************************************************************************** + * FUNCTIONS - API + **************************************************************************************************/ + +void HalLcd_HW_Init(void); +void HalLcd_HW_WaitUs(uint16 i); +void HalLcd_HW_ClearAllSpecChars(void); +void HalLcd_HW_Control(uint8 cmd); +void HalLcd_HW_Write(uint8 data); +void HalLcd_HW_SetContrast(uint8 value); +void HalLcd_HW_WriteChar(uint8 line, uint8 col, char text); +void HalLcd_HW_WriteLine(uint8 line, const char *pText); +#endif //LCD + +/************************************************************************************************** + * @fn HalLcdInit + * + * @brief Initilize LCD Service + * + * @param init - pointer to void that contains the initialized value + * + * @return None + **************************************************************************************************/ +void HalLcdInit(void) +{ +#if (HAL_LCD == TRUE) + Lcd_Line1 = NULL; + HalLcd_HW_Init(); +#endif +} + +/************************************************************************************************* + * LCD EMULATION FUNCTIONS + * + * Some evaluation boards are equipped with Liquid Crystal Displays + * (LCD) which may be used to display diagnostic information. These + * functions provide LCD emulation, sending the diagnostic strings + * to Z-Tool via the RS232 serial port. These functions are enabled + * when the "LCD_SUPPORTED" compiler flag is placed in the makefile. + * + * Most applications update both lines (1 and 2) of the LCD whenever + * text is posted to the device. This emulator assumes that line 1 is + * updated first (saved locally) and the formatting and send operation + * is triggered by receipt of line 2. Nothing will be transmitted if + * only line 1 is updated. + * + *************************************************************************************************/ + + +/************************************************************************************************** + * @fn HalLcdWriteString + * + * @brief Write a string to the LCD + * + * @param str - pointer to the string that will be displayed + * option - display options + * + * @return None + **************************************************************************************************/ +void HalLcdWriteString ( char *str, uint8 option) +{ +#if (HAL_LCD == TRUE) + + uint8 strLen = 0; + uint8 totalLen = 0; + uint8 *buf; + uint8 tmpLen; + + if ( Lcd_Line1 == NULL ) + { + Lcd_Line1 = osal_mem_alloc( HAL_LCD_MAX_CHARS+1 ); + HalLcdWriteString( "TexasInstruments", 1 ); + } + + strLen = (uint8)osal_strlen( (char*)str ); + + /* Check boundries */ + if ( strLen > HAL_LCD_MAX_CHARS ) + strLen = HAL_LCD_MAX_CHARS; + + if ( option == HAL_LCD_LINE_1 ) + { + /* Line 1 gets saved for later */ + osal_memcpy( Lcd_Line1, str, strLen ); + Lcd_Line1[strLen] = '\0'; + } + else + { + /* Line 2 triggers action */ + tmpLen = (uint8)osal_strlen( (char*)Lcd_Line1 ); + totalLen = tmpLen + 1 + strLen + 1; + buf = osal_mem_alloc( totalLen ); + if ( buf != NULL ) + { + /* Concatenate strings */ + osal_memcpy( buf, Lcd_Line1, tmpLen ); + buf[tmpLen++] = ' '; + osal_memcpy( &buf[tmpLen], str, strLen ); + buf[tmpLen+strLen] = '\0'; + + /* Send it out */ +#if defined (ZTOOL_P1) || defined (ZTOOL_P2) + +#if defined(SERIAL_DEBUG_SUPPORTED) + debug_str( (uint8*)buf ); +#endif //LCD_SUPPORTED + +#endif //ZTOOL_P1 + + /* Free mem */ + osal_mem_free( buf ); + } + } + + if (option != HAL_LCD_SERIAL_DEBUG_ONLY) + { + /* Display the string */ + HalLcd_HW_WriteLine (option, str); + } + +#endif //HAL_LCD + +} + +/************************************************************************************************** + * @fn HalLcdWriteValue + * + * @brief Write a value to the LCD + * + * @param value - value that will be displayed + * radix - 8, 10, 16 + * option - display options + * + * @return None + **************************************************************************************************/ +void HalLcdWriteValue ( uint32 value, const uint8 radix, uint8 option) +{ +#if (HAL_LCD == TRUE) + uint8 buf[HAL_LCD_MAX_BUFF]; + + _ltoa( value, &buf[0], radix ); + HalLcdWriteString( (char*)buf, option ); +#endif +} + +/************************************************************************************************** + * @fn HalLcdWriteScreen + * + * @brief Write a value to the LCD + * + * @param line1 - string that will be displayed on line 1 + * line2 - string that will be displayed on line 2 + * + * @return None + **************************************************************************************************/ +void HalLcdWriteScreen( char *line1, char *line2 ) +{ +#if (HAL_LCD == TRUE) + HalLcdWriteString( line1, HAL_LCD_DEBUG_LINE_1 ); + HalLcdWriteString( line2, HAL_LCD_DEBUG_LINE_2 ); +#endif +} + +/************************************************************************************************** + * @fn HalLcdWriteStringValue + * + * @brief Write a string followed by a value to the LCD + * + * @param title - Title that will be displayed before the value + * value - value + * format - redix + * line - line number + * + * @return None + **************************************************************************************************/ +void HalLcdWriteStringValue( char *title, uint16 value, uint8 format, uint8 line ) +{ +#if (HAL_LCD == TRUE) + uint8 tmpLen; + uint8 buf[HAL_LCD_MAX_BUFF]; + uint32 err; + + tmpLen = (uint8)osal_strlen( (char*)title ); + osal_memcpy( buf, title, tmpLen ); + buf[tmpLen] = ' '; + err = (uint32)(value); + _ltoa( err, &buf[tmpLen+1], format ); + HalLcdWriteString( (char*)buf, line ); +#endif +} + +/************************************************************************************************** + * @fn HalLcdWriteStringValue + * + * @brief Write a string followed by a value to the LCD + * + * @param title - Title that will be displayed before the value + * value1 - value #1 + * format1 - redix of value #1 + * value2 - value #2 + * format2 - redix of value #2 + * line - line number + * + * @return None + **************************************************************************************************/ +void HalLcdWriteStringValueValue( char *title, uint16 value1, uint8 format1, + uint16 value2, uint8 format2, uint8 line ) +{ + +#if (HAL_LCD == TRUE) + + uint8 tmpLen; + uint8 buf[HAL_LCD_MAX_BUFF]; + uint32 err; + + tmpLen = (uint8)osal_strlen( (char*)title ); + if ( tmpLen ) + { + osal_memcpy( buf, title, tmpLen ); + buf[tmpLen++] = ' '; + } + + err = (uint32)(value1); + _ltoa( err, &buf[tmpLen], format1 ); + tmpLen = (uint8)osal_strlen( (char*)buf ); + + buf[tmpLen++] = ','; + buf[tmpLen++] = ' '; + err = (uint32)(value2); + _ltoa( err, &buf[tmpLen], format2 ); + + HalLcdWriteString( (char *)buf, line ); + +#endif +} + +/************************************************************************************************** + * @fn HalLcdDisplayPercentBar + * + * @brief Display percentage bar on the LCD + * + * @param title - + * value - + * + * @return None + **************************************************************************************************/ +void HalLcdDisplayPercentBar( char *title, uint8 value ) +{ +#if (HAL_LCD == TRUE) + + uint8 percent; + uint8 leftOver; + uint8 buf[17]; + uint32 err; + uint8 x; + + /* Write the title: */ + HalLcdWriteString( title, HAL_LCD_LINE_1 ); + + if ( value > 100 ) + value = 100; + + /* convert to blocks */ + percent = (uint8)(value / 10); + leftOver = (uint8)(value % 10); + + /* Make window */ + osal_memcpy( buf, "[ ] ", 15 ); + + for ( x = 0; x < percent; x ++ ) + { + buf[1+x] = '>'; + } + + if ( leftOver >= 5 ) + buf[1+x] = '+'; + + err = (uint32)value; + _ltoa( err, (uint8*)&buf[13], 10 ); + + HalLcdWriteString( (char*)buf, HAL_LCD_LINE_2 ); + +#endif + +} + +#if (HAL_LCD == TRUE) +/************************************************************************************************** + * HARDWARE LCD + **************************************************************************************************/ + +/************************************************************************************************** + * @fn halLcd_ConfigIO + * + * @brief Configure IO lines needed for LCD control. + * + * @param None + * + * @return None + **************************************************************************************************/ +static void halLcd_ConfigIO(void) +{ + /* GPIO configuration */ + HAL_CONFIG_IO_OUTPUT(HAL_LCD_MODE_PORT, HAL_LCD_MODE_PIN, 1); + HAL_CONFIG_IO_OUTPUT(HAL_LCD_RESET_PORT, HAL_LCD_RESET_PIN, 1); + HAL_CONFIG_IO_OUTPUT(HAL_LCD_CS_PORT, HAL_LCD_CS_PIN, 1); +} + +/************************************************************************************************** + * @fn halLcd_ConfigSPI + * + * @brief Configure SPI lines needed for talking to LCD. + * + * @param None + * + * @return None + **************************************************************************************************/ +static void halLcd_ConfigSPI(void) +{ + /* UART/SPI Peripheral configuration */ + + uint8 baud_exponent; + uint8 baud_mantissa; + + /* Set SPI on UART 1 alternative 2 */ + PERCFG |= 0x02; + + /* Configure clk, master out and master in lines */ + HAL_CONFIG_IO_PERIPHERAL(HAL_LCD_CLK_PORT, HAL_LCD_CLK_PIN); + HAL_CONFIG_IO_PERIPHERAL(HAL_LCD_MOSI_PORT, HAL_LCD_MOSI_PIN); + HAL_CONFIG_IO_PERIPHERAL(HAL_LCD_MISO_PORT, HAL_LCD_MISO_PIN); + + + /* Set SPI speed to 1 MHz (the values assume system clk of 32MHz) + * Confirm on board that this results in 1MHz spi clk. + */ + baud_exponent = 15; + baud_mantissa = 0; + + /* Configure SPI */ + U1UCR = 0x80; /* Flush and goto IDLE state. 8-N-1. */ + U1CSR = 0x00; /* SPI mode, master. */ + U1GCR = HAL_SPI_TRANSFER_MSB_FIRST | HAL_SPI_CLOCK_PHA_0 | HAL_SPI_CLOCK_POL_LO | baud_exponent; + U1BAUD = baud_mantissa; +} + +/************************************************************************************************** + * @fn HalLcd_HW_Init + * + * @brief Initilize HW LCD Driver. + * + * @param None + * + * @return None + **************************************************************************************************/ +void HalLcd_HW_Init(void) +{ + /* Initialize LCD IO lines */ + halLcd_ConfigIO(); + + /* Initialize SPI */ + halLcd_ConfigSPI(); + + /* Perform reset */ + LCD_ACTIVATE_RESET(); + HalLcd_HW_WaitUs(15000); // 15 ms + LCD_RELEASE_RESET(); + HalLcd_HW_WaitUs(15); // 15 us + + /* Perform the initialization sequence */ + FUNCTION_SET(CGRAM | COM_FORWARD | THREE_LINE); + + /* Set contrast */ + HalLcd_HW_SetContrast(15); + + /* Set power */ + SET_POWER_SAVE_MODE(OSC_OFF | POWER_SAVE_ON); + SET_POWER_CTRL(VOLTAGE_DIVIDER_ON | CONVERTER_AND_REG_ON); + SET_BIAS_CTRL(BIAS_1_5); + HalLcd_HW_WaitUs(21000);// 21 ms + + /* Clear the display */ + HalLcd_HW_Clear(); + HalLcd_HW_ClearAllSpecChars(); + SET_DISPLAY_CTRL(DISPLAY_CTRL_ON | DISPLAY_CTRL_BLINK_OFF | DISPLAY_CTRL_CURSOR_OFF); +} + +/************************************************************************************************** + * @fn HalLcd_HW_Control + * + * @brief Write 1 command to the LCD + * + * @param uint8 cmd - command to be written to the LCD + * + * @return None + **************************************************************************************************/ +void HalLcd_HW_Control(uint8 cmd) +{ + LCD_SPI_BEGIN(); + LCD_DO_CONTROL(); + LCD_SPI_TX(cmd); + LCD_SPI_WAIT_RXRDY(); + LCD_SPI_END(); +} + +/************************************************************************************************** + * @fn HalLcd_HW_Write + * + * @brief Write 1 byte to the LCD + * + * @param uint8 data - data to be written to the LCD + * + * @return None + **************************************************************************************************/ +void HalLcd_HW_Write(uint8 data) +{ + LCD_SPI_BEGIN(); + LCD_DO_WRITE(); + LCD_SPI_TX(data); + LCD_SPI_WAIT_RXRDY(); + LCD_SPI_END(); +} + +/************************************************************************************************** + * @fn HalLcd_HW_SetContrast + * + * @brief Set display contrast + * + * @param uint8 value - contrast value + * + * @return none + **************************************************************************************************/ +void HalLcd_HW_SetContrast(uint8 value) +{ + SET_ICONRAM_ADDR(CONTRAST_CTRL_REGISTER); + HalLcd_HW_Write(value); +} + +/************************************************************************************************** + * @fn HalLcd_HW_Clear + * + * @brief Clear the HW LCD + * + * @param None + * + * @return None + **************************************************************************************************/ +void HalLcd_HW_Clear(void) +{ + uint8 n; + + SET_DDRAM_ADDR(0x00); + for (n = 0; n < (LCD_MAX_LINE_COUNT * HAL_LCD_MAX_CHARS); n++) + { + HalLcd_HW_Write(' '); + } +} + +/************************************************************************************************** + * @fn HalLcd_HW_ClearAllSpecChars + * + * @brief Clear all special chars + * + * @param None + * + * @return None + **************************************************************************************************/ +void HalLcd_HW_ClearAllSpecChars(void) +{ + uint8 n = 0; + + SET_GCRAM_CHAR(0); + for (n = 0; n < (8 * 8); n++) + { + HalLcd_HW_Write(0x00); + } +} + +/************************************************************************************************** + * @fn HalLcd_HW_WriteChar + * + * @brief Write one char to the display + * + * @param uint8 line - line number that the char will be displayed + * uint8 col - colum where the char will be displayed + * + * @return None + **************************************************************************************************/ +void HalLcd_HW_WriteChar(uint8 line, uint8 col, char text) +{ + if (col < HAL_LCD_MAX_CHARS) + { + SET_DDRAM_ADDR((line - 1) * HAL_LCD_MAX_CHARS + col); + HalLcd_HW_Write(text); + } + else + { + return; + } +} + +/************************************************************************************************** + * @fn halLcdWriteLine + * + * @brief Write one line on display + * + * @param uint8 line - display line + * char *pText - text buffer to write + * + * @return none + **************************************************************************************************/ +void HalLcd_HW_WriteLine(uint8 line, const char *pText) +{ + uint8 count; + uint8 totalLength = (uint8)osal_strlen( (char *)pText ); + + /* Write the content first */ + for (count=0; countmode = mode; /* ON or OFF */ + } + else + { + sts->mode ^= HAL_LED_MODE_ON; /* Toggle */ + } + HalLedOnOff (led, sts->mode); + leds ^= led; + } + led <<= 1; + sts++; + } + break; + + default: + break; + } + +#elif (HAL_LED == TRUE) + LedOnOff(leds, mode); +#else + // HAL LED is disabled, suppress unused argument warnings + (void) leds; + (void) mode; +#endif /* BLINK_LEDS && HAL_LED */ + + return ( HalLedState ); + +} + +/*************************************************************************************************** + * @fn HalLedBlink + * + * @brief Blink the leds + * + * @param leds - bit mask value of leds to be blinked + * numBlinks - number of blinks + * percent - the percentage in each period where the led + * will be on + * period - length of each cycle in milliseconds + * + * @return None + ***************************************************************************************************/ +void HalLedBlink (uint8 leds, uint8 numBlinks, uint8 percent, uint16 period) +{ +#if (defined (BLINK_LEDS)) && (HAL_LED == TRUE) + uint8 led; + HalLedControl_t *sts; + + if (leds && percent && period) + { + if (percent < 100) + { + led = HAL_LED_1; + leds &= HAL_LED_ALL; + sts = HalLedStatusControl.HalLedControlTable; + + while (leds) + { + if (leds & led) + { + /* Store the current state of the led before going to blinking if not already blinking */ + if(sts->mode < HAL_LED_MODE_BLINK ) + preBlinkState |= (led & HalLedState); + + sts->mode = HAL_LED_MODE_OFF; /* Stop previous blink */ + sts->time = period; /* Time for one on/off cycle */ + sts->onPct = percent; /* % of cycle LED is on */ + sts->todo = numBlinks; /* Number of blink cycles */ + if (!numBlinks) sts->mode |= HAL_LED_MODE_FLASH; /* Continuous */ + sts->next = osal_GetSystemClock(); /* Start now */ + sts->mode |= HAL_LED_MODE_BLINK; /* Enable blinking */ + leds ^= led; + } + led <<= 1; + sts++; + } + // Cancel any overlapping timer for blink events + osal_stop_timerEx(Hal_TaskID, HAL_LED_BLINK_EVENT); + osal_set_event (Hal_TaskID, HAL_LED_BLINK_EVENT); + } + else + { + HalLedSet (leds, HAL_LED_MODE_ON); /* >= 100%, turn on */ + } + } + else + { + HalLedSet (leds, HAL_LED_MODE_OFF); /* No on time, turn off */ + } +#elif (HAL_LED == TRUE) + percent = (leds & HalLedState) ? HAL_LED_MODE_OFF : HAL_LED_MODE_ON; + HalLedOnOff (leds, percent); /* Toggle */ +#else + // HAL LED is disabled, suppress unused argument warnings + (void) leds; + (void) numBlinks; + (void) percent; + (void) period; +#endif /* BLINK_LEDS && HAL_LED */ +} + +#if (HAL_LED == TRUE) +/*************************************************************************************************** + * @fn HalLedUpdate + * + * @brief Update leds to work with blink + * + * @param none + * + * @return none + ***************************************************************************************************/ +void HalLedUpdate (void) +{ + uint8 led; + uint8 pct; + uint8 leds; + HalLedControl_t *sts; + uint32 time; + uint16 next; + uint16 wait; + + next = 0; + led = HAL_LED_1; + leds = HAL_LED_ALL; + sts = HalLedStatusControl.HalLedControlTable; + + /* Check if sleep is active or not */ + if (!HalLedStatusControl.sleepActive) + { + while (leds) + { + if (leds & led) + { + if (sts->mode & HAL_LED_MODE_BLINK) + { + time = osal_GetSystemClock(); + if (time >= sts->next) + { + if (sts->mode & HAL_LED_MODE_ON) + { + pct = 100 - sts->onPct; /* Percentage of cycle for off */ + sts->mode &= ~HAL_LED_MODE_ON; /* Say it's not on */ + HalLedOnOff (led, HAL_LED_MODE_OFF); /* Turn it off */ + + if (!(sts->mode & HAL_LED_MODE_FLASH)) + { + sts->todo--; /* Not continuous, reduce count */ + } + } + else if ( (!sts->todo) && !(sts->mode & HAL_LED_MODE_FLASH) ) + { + sts->mode ^= HAL_LED_MODE_BLINK; /* No more blinks */ + } + else + { + pct = sts->onPct; /* Percentage of cycle for on */ + sts->mode |= HAL_LED_MODE_ON; /* Say it's on */ + HalLedOnOff (led, HAL_LED_MODE_ON); /* Turn it on */ + } + if (sts->mode & HAL_LED_MODE_BLINK) + { + wait = (((uint32)pct * (uint32)sts->time) / 100); + sts->next = time + wait; + } + else + { + /* no more blink, no more wait */ + wait = 0; + /* After blinking, set the LED back to the state before it blinks */ + HalLedSet (led, ((preBlinkState & led)!=0)?HAL_LED_MODE_ON:HAL_LED_MODE_OFF); + /* Clear the saved bit */ + preBlinkState &= (led ^ 0xFF); + } + } + else + { + wait = sts->next - time; /* Time left */ + } + + if (!next || ( wait && (wait < next) )) + { + next = wait; + } + } + leds ^= led; + } + led <<= 1; + sts++; + } + + if (next) + { + osal_start_timerEx(Hal_TaskID, HAL_LED_BLINK_EVENT, next); /* Schedule event */ + } + } +} + +/*************************************************************************************************** + * @fn HalLedOnOff + * + * @brief Turns specified LED ON or OFF + * + * @param leds - LED bit mask + * mode - LED_ON,LED_OFF, + * + * @return none + ***************************************************************************************************/ +void HalLedOnOff (uint8 leds, uint8 mode) +{ + if (leds & HAL_LED_1) + { + if (mode == HAL_LED_MODE_ON) + { + HAL_TURN_ON_LED1(); + } + else + { + HAL_TURN_OFF_LED1(); + } + } + + if (leds & HAL_LED_2) + { + if (mode == HAL_LED_MODE_ON) + { + HAL_TURN_ON_LED2(); + } + else + { + HAL_TURN_OFF_LED2(); + } + } + + if (leds & HAL_LED_3) + { + if (mode == HAL_LED_MODE_ON) + { + HAL_TURN_ON_LED3(); + } + else + { + HAL_TURN_OFF_LED3(); + } + } + + if (leds & HAL_LED_4) + { + if (mode == HAL_LED_MODE_ON) + { + HAL_TURN_ON_LED4(); + } + else + { + HAL_TURN_OFF_LED4(); + } + } + + /* Remember current state */ + if (mode) + { + HalLedState |= leds; + } + else + { + HalLedState &= (leds ^ 0xFF); + } +} +#endif /* HAL_LED */ + +/*************************************************************************************************** + * @fn HalGetLedState + * + * @brief Dim LED2 - Dim (set level) of LED2 + * + * @param none + * + * @return led state + ***************************************************************************************************/ +uint8 HalLedGetState () +{ +#if (HAL_LED == TRUE) + return HalLedState; +#else + return 0; +#endif +} + +/*************************************************************************************************** + * @fn HalLedEnterSleep + * + * @brief Store current LEDs state before sleep + * + * @param none + * + * @return none + ***************************************************************************************************/ +void HalLedEnterSleep( void ) +{ +#ifdef BLINK_LEDS + /* Sleep ON */ + HalLedStatusControl.sleepActive = TRUE; +#endif /* BLINK_LEDS */ + +#if (HAL_LED == TRUE) + /* Save the state of each led */ + HalSleepLedState = 0; + HalSleepLedState |= HAL_STATE_LED1(); + HalSleepLedState |= HAL_STATE_LED2() << 1; + HalSleepLedState |= HAL_STATE_LED3() << 2; + HalSleepLedState |= HAL_STATE_LED4() << 3; + + /* TURN OFF all LEDs to save power */ + HalLedOnOff (HAL_LED_ALL, HAL_LED_MODE_OFF); +#endif /* HAL_LED */ + +} + +/*************************************************************************************************** + * @fn HalLedExitSleep + * + * @brief Restore current LEDs state after sleep + * + * @param none + * + * @return none + ***************************************************************************************************/ +void HalLedExitSleep( void ) +{ +#if (HAL_LED == TRUE) + /* Load back the saved state */ + HalLedOnOff(HalSleepLedState, HAL_LED_MODE_ON); + + /* Restart - This takes care BLINKING LEDS */ + HalLedUpdate(); +#endif /* HAL_LED */ + +#ifdef BLINK_LEDS + /* Sleep OFF */ + HalLedStatusControl.sleepActive = FALSE; +#endif /* BLINK_LEDS */ +} + +/*************************************************************************************************** +***************************************************************************************************/ + + + + diff --git a/include/hal_led.h b/include/hal_led.h new file mode 100644 index 0000000..bb327f1 --- /dev/null +++ b/include/hal_led.h @@ -0,0 +1,127 @@ +/************************************************************************************************** + Filename: hal_led.h + Revised: $Date: 2007-07-06 10:42:24 -0700 (Fri, 06 Jul 2007) $ + Revision: $Revision: 13579 $ + + Description: This file contains the interface to the LED Service. + + + Copyright 2005-2007 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +#ifndef HAL_LED_H +#define HAL_LED_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +/********************************************************************* + * INCLUDES + */ +#include "hal_board.h" + +/********************************************************************* + * MACROS + */ + +/********************************************************************* + * CONSTANTS + */ + +/* LEDS - The LED number is the same as the bit position */ +#define HAL_LED_1 0x01 +#define HAL_LED_2 0x02 +#define HAL_LED_3 0x04 +#define HAL_LED_4 0x08 +#define HAL_LED_ALL (HAL_LED_1 | HAL_LED_2 | HAL_LED_3 | HAL_LED_4) + +/* Modes */ +#define HAL_LED_MODE_OFF 0x00 +#define HAL_LED_MODE_ON 0x01 +#define HAL_LED_MODE_BLINK 0x02 +#define HAL_LED_MODE_FLASH 0x04 +#define HAL_LED_MODE_TOGGLE 0x08 + +/* Defaults */ +#define HAL_LED_DEFAULT_MAX_LEDS 4 +#define HAL_LED_DEFAULT_DUTY_CYCLE 5 +#define HAL_LED_DEFAULT_FLASH_COUNT 50 +#define HAL_LED_DEFAULT_FLASH_TIME 1000 + +/********************************************************************* + * TYPEDEFS + */ + + +/********************************************************************* + * GLOBAL VARIABLES + */ + +/* + * Initialize LED Service. + */ +extern void HalLedInit( void ); + +/* + * Set the LED ON/OFF/TOGGLE. + */ +extern uint8 HalLedSet( uint8 led, uint8 mode ); + +/* + * Blink the LED. + */ +extern void HalLedBlink( uint8 leds, uint8 cnt, uint8 duty, uint16 time ); + +/* + * Put LEDs in sleep state - store current values + */ +extern void HalLedEnterSleep( void ); + +/* + * Retore LEDs from sleep state + */ +extern void HalLedExitSleep( void ); + +/* + * Return LED state + */ +extern uint8 HalLedGetState ( void ); + +/********************************************************************* +*********************************************************************/ + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/hal_mac_cfg.h b/include/hal_mac_cfg.h new file mode 100644 index 0000000..387d9ed --- /dev/null +++ b/include/hal_mac_cfg.h @@ -0,0 +1,62 @@ +/************************************************************************************************** + Filename: hal_mac_cfg.h + Revised: $Date: 2010-04-12 09:38:02 -0700 (Mon, 12 Apr 2010) $ + Revision: $Revision: 22158 $ + + Description: Describe the purpose and contents of the file. + + + Copyright 2007-2009 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +#ifndef HAL_MAC_CFG_H +#define HAL_MAC_CFG_H + +/* + * Board Configuration File for low-level MAC + * -------------------------------------------- + * Manufacturer : Texas Instruments + * Part Number : CC2530EB + * Processor : Texas Instruments CC2530 + * + */ + + +/* ------------------------------------------------------------------------------------------------ + * Board Specific Configuration + * ------------------------------------------------------------------------------------------------ + */ +#define HAL_MAC_RSSI_OFFSET -73 /* no units */ + + +/************************************************************************************************** +*/ +#endif diff --git a/include/hal_mcu.h b/include/hal_mcu.h new file mode 100644 index 0000000..c80c312 --- /dev/null +++ b/include/hal_mcu.h @@ -0,0 +1,194 @@ +/************************************************************************************************** + Filename: hal_mcu.h + Revised: $Date: 2013-10-07 09:18:37 -0700 (Mon, 07 Oct 2013) $ + Revision: $Revision: 35584 $ + + Description: Describe the purpose and contents of the file. + + + Copyright 2006-2013 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +#ifndef _HAL_MCU_H +#define _HAL_MCU_H + +/* + * Target : Texas Instruments CC2530 (8051 core) + * + */ + + +/* ------------------------------------------------------------------------------------------------ + * Includes + * ------------------------------------------------------------------------------------------------ + */ +#include "hal_defs.h" +#include "hal_types.h" + + +/* ------------------------------------------------------------------------------------------------ + * Target Defines + * ------------------------------------------------------------------------------------------------ + */ +#define HAL_MCU_CC2530 + + +/* ------------------------------------------------------------------------------------------------ + * Compiler Abstraction + * ------------------------------------------------------------------------------------------------ + */ + +/* ---------------------- IAR Compiler ---------------------- */ +#ifdef __IAR_SYSTEMS_ICC__ +#include +#define HAL_COMPILER_IAR +#define HAL_MCU_LITTLE_ENDIAN() __LITTLE_ENDIAN__ +#define _PRAGMA(x) _Pragma(#x) +#define HAL_ISR_FUNC_DECLARATION(f,v) _PRAGMA(vector=v) __near_func __interrupt void f(void) +#define HAL_ISR_FUNC_PROTOTYPE(f,v) _PRAGMA(vector=v) __near_func __interrupt void f(void) +#define HAL_ISR_FUNCTION(f,v) HAL_ISR_FUNC_PROTOTYPE(f,v); HAL_ISR_FUNC_DECLARATION(f,v) + +/* ---------------------- Keil Compiler ---------------------- */ +#elif defined __KEIL__ +#include +#define HAL_COMPILER_KEIL +#define HAL_MCU_LITTLE_ENDIAN() 0 +#define HAL_ISR_FUNC_DECLARATION(f,v) void f(void) interrupt v +#define HAL_ISR_FUNC_PROTOTYPE(f,v) void f(void) +#define HAL_ISR_FUNCTION(f,v) HAL_ISR_FUNC_PROTOTYPE(f,v); HAL_ISR_FUNC_DECLARATION(f,v) + +/* ------------------ Unrecognized Compiler ------------------ */ +#else +#error "ERROR: Unknown compiler." +#endif + + +/* ------------------------------------------------------------------------------------------------ + * Interrupt Macros + * ------------------------------------------------------------------------------------------------ + */ +#define HAL_ENABLE_INTERRUPTS() st( EA = 1; ) +#define HAL_DISABLE_INTERRUPTS() st( EA = 0; ) +#define HAL_INTERRUPTS_ARE_ENABLED() (EA) + +typedef unsigned char halIntState_t; +#define HAL_ENTER_CRITICAL_SECTION(x) st( x = EA; HAL_DISABLE_INTERRUPTS(); ) +#define HAL_EXIT_CRITICAL_SECTION(x) st( EA = x; ) +#define HAL_CRITICAL_STATEMENT(x) st( halIntState_t _s; HAL_ENTER_CRITICAL_SECTION(_s); x; HAL_EXIT_CRITICAL_SECTION(_s); ) + +#ifdef __IAR_SYSTEMS_ICC__ + /* IAR library uses XCH instruction with EA. It may cause the higher priority interrupt to be + * locked out, therefore, may increase interrupt latency. It may also create a lockup condition. + * This workaround should only be used with 8051 using IAR compiler. When IAR fixes this by + * removing XCH usage in its library, compile the following macros to null to disable them. + */ + #define HAL_ENTER_ISR() { halIntState_t _isrIntState = EA; HAL_ENABLE_INTERRUPTS(); + #define HAL_EXIT_ISR() EA = _isrIntState; } +#else + #define HAL_ENTER_ISR() + #define HAL_EXIT_ISR() +#endif /* __IAR_SYSTEMS_ICC__ */ + +/* Dummy for this platform */ +#define HAL_AES_ENTER_WORKAROUND() +#define HAL_AES_EXIT_WORKAROUND() + +/* ------------------------------------------------------------------------------------------------ + * Reset Macro + * ------------------------------------------------------------------------------------------------ + */ +#define WD_EN BV(3) +#define WD_MODE BV(2) +#define WD_INT_1900_USEC (BV(0) | BV(1)) +#define WD_RESET1 (0xA0 | WD_EN | WD_INT_1900_USEC) +#define WD_RESET2 (0x50 | WD_EN | WD_INT_1900_USEC) +#define WD_KICK() st( WDCTL = (0xA0 | WDCTL & 0x0F); WDCTL = (0x50 | WDCTL & 0x0F); ) + +/* disable interrupts, set watchdog timer, wait for reset */ +#define HAL_SYSTEM_RESET() st( HAL_DISABLE_INTERRUPTS(); WDCTL = WD_RESET1; WDCTL = WD_RESET2; for(;;); ) + +/* ------------------------------------------------------------------------------------------------ + * CC2530 rev numbers + * ------------------------------------------------------------------------------------------------ + */ +#define REV_A 0x00 /* workaround turned off */ +#define REV_B 0x11 /* PG1.1 */ +#define REV_C 0x20 /* PG2.0 */ +#define REV_D 0x21 /* PG2.1 */ + +/* ------------------------------------------------------------------------------------------------ + * CC2530 sleep common code + * ------------------------------------------------------------------------------------------------ + */ + +/* PCON bit definitions */ +#define PCON_IDLE BV(0) /* Writing 1 to force CC2530 to enter sleep mode */ + +/* SLEEPCMD bit definitions */ +#define OSC_PD BV(2) /* Idle Osc: powered down=1 */ +#define PMODE (BV(1) | BV(0)) /* Power mode bits */ + +/* SLEEPSTA bit definitions */ +#define XOSC_STB BV(6) /* XOSC: powered, stable=1 */ +#define HFRC_STB BV(5) /* HFRCOSC: powered, stable=1 */ + +/* SLEEPCMD and SLEEPSTA bit definitions */ +#define OSC_PD BV(2) /* 0: Both oscillators powered up and stable + * 1: oscillators not stable */ + +/* CLKCONCMD bit definitions */ +#define OSC BV(6) +#define TICKSPD(x) (x << 3) +#define CLKSPD(x) (x << 0) +#define CLKCONCMD_32MHZ (0) +#define CLKCONCMD_16MHZ (CLKSPD(1) | TICKSPD(1) | OSC) + +/* STLOAD */ +#define LDRDY BV(0) /* Load Ready. This bit is 0 while the sleep timer + * loads the 24-bit compare value and 1 when the sleep + * timer is ready to start loading a newcompare value. */ + +#ifdef POWER_SAVING +extern volatile __data uint8 halSleepPconValue; + +/* Any ISR that is used to wake up the chip shall call this macro. This prevents the race condition + * when the PCON IDLE bit is set after such a critical ISR fires during the prep for sleep. + */ +#define CLEAR_SLEEP_MODE() st( halSleepPconValue = 0; ) +#define ALLOW_SLEEP_MODE() st( halSleepPconValue = PCON_IDLE; ) +#else +#define CLEAR_SLEEP_MODE() +#define ALLOW_SLEEP_MODE() +#endif + +/************************************************************************************************** + */ +#endif diff --git a/include/hal_oad.c b/include/hal_oad.c new file mode 100644 index 0000000..ecfe956 --- /dev/null +++ b/include/hal_oad.c @@ -0,0 +1,577 @@ +/************************************************************************************************** + Filename: _hal_oad.c + Revised: $Date: 2010-07-08 18:39:25 -0700 (Thu, 08 Jul 2010) $ + Revision: $Revision: 22957 $ + + Description: This module contains optionally-compiled Boot Code to support OAD. + The rest of the functionality is the H/W specific drivers to read/write + the flash/NV containing the ACTIVE and the DOWNLOADED images. + Notes: This version targets the Texas Instruments CC253x family of processors. + + + Copyright 2008-2010 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +/* ------------------------------------------------------------------------------------------------ + * Includes + * ------------------------------------------------------------------------------------------------ + */ + +#include "comdef.h" +#include "hal_board_cfg.h" +#include "hal_dma.h" +#include "hal_flash.h" +#include "hal_oad.h" +#include "hal_types.h" + +/* ------------------------------------------------------------------------------------------------ + * Local Variables + * ------------------------------------------------------------------------------------------------ + */ + +#if HAL_OAD_BOOT_CODE +halDMADesc_t dmaCh0; +#endif + +/* ------------------------------------------------------------------------------------------------ + * Local Functions + * ------------------------------------------------------------------------------------------------ + */ + +static uint16 runPoly(uint16 crc, uint8 val); +#if HAL_OAD_XNV_IS_SPI +static void HalSPIRead(uint32 addr, uint8 *pBuf, uint16 len); +static void HalSPIWrite(uint32 addr, uint8 *pBuf, uint16 len); +#endif + +#if HAL_OAD_BOOT_CODE +static void vddWait(uint8 vdd); +static void dl2rc(void); +static uint16 crcCalc(void); + +/************************************************************************************************** + * @fn main + * + * @brief ISR for the reset vector. + * + * input parameters + * + * None. + * + * output parameters + * + * None. + * + * @return None. + ************************************************************************************************** + */ +#pragma location="NEAR_CODE" +void main(void) +{ + HAL_BOARD_INIT(); + vddWait(VDD_MIN_RUN); + +#if HAL_OAD_XNV_IS_SPI + XNV_SPI_INIT(); +#endif + /* This is in place of calling HalDmaInit() which would require init of the other 4 DMA + * descriptors in addition to just Channel 0. + */ + HAL_DMA_SET_ADDR_DESC0( &dmaCh0 ); + + while (1) + { + uint16 crc[2]; + + HalFlashRead(HAL_OAD_CRC_ADDR / HAL_FLASH_PAGE_SIZE, + HAL_OAD_CRC_ADDR % HAL_FLASH_PAGE_SIZE, + (uint8 *)crc, sizeof(crc)); + + if (crc[0] == crc[1]) + { + break; + } + else if ((crc[0] != 0) && (crc[0] == crcCalc())) + { + crc[1] = crc[0]; + HalFlashWrite((HAL_OAD_CRC_ADDR / HAL_FLASH_WORD_SIZE), (uint8 *)crc, 1); + } + else + { + dl2rc(); + } + } + + // Simulate a reset for the Application code by an absolute jump to location 0x0800. + asm("LJMP 0x800\n"); +} + +/********************************************************************* + * @fn vddWait + * + * @brief Loop waiting for 256 reads of the Vdd over the requested limit. + * + * @param vdd - Vdd level to wait for. + * + * @return None. + *********************************************************************/ +static void vddWait(uint8 vdd) +{ + uint8 cnt = 16; + + do { + do { + ADCCON3 = 0x0F; + while (!(ADCCON1 & 0x80)); + } while (ADCH < vdd); + } while (--cnt); +} + +/********************************************************************* + * @fn dl2rc + * + * @brief Copy the DL image to the RC image location. + * + * NOTE: Assumes that DL image ends on a flash word boundary. + * + * @param None. + * + * @return None. + *********************************************************************/ +static void dl2rc(void) +{ + preamble_t preamble; + uint32 oset; + uint16 addr = HAL_OAD_RC_START / HAL_FLASH_WORD_SIZE; + uint8 buf[4]; + + vddWait(VDD_MIN_OAD); + HalOADRead(PREAMBLE_OFFSET, (uint8 *)&preamble, sizeof(preamble_t), HAL_OAD_DL); + + for (oset = 0; oset < preamble.len; oset += HAL_FLASH_WORD_SIZE) + { + HalOADRead(oset, buf, HAL_FLASH_WORD_SIZE, HAL_OAD_DL); + if ((addr % (HAL_FLASH_PAGE_SIZE / HAL_FLASH_WORD_SIZE)) == 0) + { + HalFlashErase(addr / (HAL_FLASH_PAGE_SIZE / HAL_FLASH_WORD_SIZE)); + } + HalFlashWrite(addr++, buf, 1); + } +} + +/********************************************************************* + * @fn crcCalc + * + * @brief Run the CRC16 Polynomial calculation over the RC image. + * + * @param None. + * + * @return The CRC16 calculated. + */ +static uint16 crcCalc(void) +{ + preamble_t preamble; + uint32 oset; + uint16 crc = 0; + + HalOADRead(PREAMBLE_OFFSET, (uint8 *)&preamble, sizeof(preamble_t), HAL_OAD_RC); + if (preamble.len > HAL_OAD_DL_SIZE) + { + return 0; + } + + // Run the CRC calculation over the active body of code. + for (oset = 0; oset < preamble.len; oset++) + { + if (oset == HAL_OAD_CRC_OSET) + { + oset += 3; + } + else + { + uint8 buf; + HalOADRead(oset, &buf, 1, HAL_OAD_RC); + crc = runPoly(crc, buf); + } + } + + // IAR note explains that poly must be run with value zero for each byte of crc. + crc = runPoly(crc, 0); + crc = runPoly(crc, 0); + + return crc; +} +#endif + +/********************************************************************* + * @fn runPoly + * + * @brief Run the CRC16 Polynomial calculation over the byte parameter. + * + * @param crc - Running CRC calculated so far. + * @param val - Value on which to run the CRC16. + * + * @return crc - Updated for the run. + */ +static uint16 runPoly(uint16 crc, uint8 val) +{ + const uint16 poly = 0x1021; + uint8 cnt; + + for (cnt = 0; cnt < 8; cnt++, val <<= 1) + { + uint8 msb = (crc & 0x8000) ? 1 : 0; + + crc <<= 1; + if (val & 0x80) crc |= 0x0001; + if (msb) crc ^= poly; + } + + return crc; +} + +/********************************************************************* + * @fn HalOADChkDL + * + * @brief Run the CRC16 Polynomial calculation over the DL image. + * + * @param dlImagePreambleOffset - Offset into the monolithic DL image to read the preamble. + * + * @return SUCCESS or FAILURE. + *********************************************************************/ +uint8 HalOADChkDL(uint8 dlImagePreambleOffset) +{ + preamble_t preamble; + uint32 oset; + uint16 crc = 0, crc2; + + HalOADRead(dlImagePreambleOffset, (uint8 *)&preamble, sizeof(preamble_t), HAL_OAD_DL); + + // Run the CRC calculation over the downloaded image. + for (oset = 0; oset < preamble.len; oset++) + { + if ((oset < HAL_OAD_CRC_OSET) || (oset >= HAL_OAD_CRC_OSET+4)) + { + uint8 buf; + HalOADRead(oset, &buf, 1, HAL_OAD_DL); + crc = runPoly(crc, buf); + } + } + + // IAR note explains that poly must be run with value zero for each byte of crc. + crc = runPoly(crc, 0); + crc = runPoly(crc, 0); + + HalOADRead(HAL_OAD_CRC_OSET, (uint8 *)&crc2, sizeof(crc2), HAL_OAD_DL); + return (crc2 == crc) ? SUCCESS : FAILURE; +} + +/********************************************************************* + * @fn HalOADInvRC + * + * @brief Invalidate the active image so that the boot code will instantiate the DL image on the + * next reset. + * + * @param None. + * + * @return None. + *********************************************************************/ +void HalOADInvRC(void) +{ + uint16 crc[2] = {0,0xFFFF}; + HalFlashWrite((HAL_OAD_CRC_ADDR / HAL_FLASH_WORD_SIZE), (uint8 *)crc, 1); +} + +/********************************************************************* + * @fn HalOADRead + * + * @brief Read from the storage medium according to image type. + * + * @param oset - Offset into the monolithic image. + * @param pBuf - Pointer to the buffer in which to copy the bytes read. + * @param len - Number of bytes to read. + * @param type - Which image: HAL_OAD_RC or HAL_OAD_DL. + * + * @return None. + *********************************************************************/ +void HalOADRead(uint32 oset, uint8 *pBuf, uint16 len, image_t type) +{ + if (HAL_OAD_RC != type) + { +#if HAL_OAD_XNV_IS_INT + preamble_t preamble; + + HalOADRead(PREAMBLE_OFFSET, (uint8 *)&preamble, sizeof(preamble_t), HAL_OAD_RC); + //oset += HAL_OAD_RC_START + preamble.len; + oset += HAL_OAD_RC_START + HAL_OAD_DL_OSET; +#elif HAL_OAD_XNV_IS_SPI + oset += HAL_OAD_DL_OSET; + HalSPIRead(oset, pBuf, len); + return; +#endif + } + else + { + oset += HAL_OAD_RC_START; + } + + HalFlashRead(oset / HAL_FLASH_PAGE_SIZE, oset % HAL_FLASH_PAGE_SIZE, pBuf, len); +} + +/********************************************************************* + * @fn HalOADWrite + * + * @brief Write to the storage medium according to the image type. + * + * NOTE: Destructive write on page boundary! When writing to the first flash word + * of a page boundary, the page is erased without saving/restoring the bytes not written. + * Writes anywhere else on a page assume that the location written to has been erased. + * + * @param oset - Offset into the monolithic image, aligned to HAL_FLASH_WORD_SIZE. + * @param pBuf - Pointer to the buffer in from which to write. + * @param len - Number of bytes to write. If not an even multiple of HAL_FLASH_WORD_SIZE, + * remainder bytes are overwritten with garbage. + * @param type - Which image: HAL_OAD_RC or HAL_OAD_DL. + * + * @return None. + *********************************************************************/ +void HalOADWrite(uint32 oset, uint8 *pBuf, uint16 len, image_t type) +{ + if (HAL_OAD_RC != type) + { +#if HAL_OAD_XNV_IS_INT + preamble_t preamble; + + HalOADRead(PREAMBLE_OFFSET, (uint8 *)&preamble, sizeof(preamble_t), HAL_OAD_RC); + //oset += HAL_OAD_RC_START + preamble.len; + oset += HAL_OAD_RC_START + HAL_OAD_DL_OSET; +#elif HAL_OAD_XNV_IS_SPI + oset += HAL_OAD_DL_OSET; + HalSPIWrite(oset, pBuf, len); + return; +#endif + } + else + { + oset += HAL_OAD_RC_START; + } + + if ((oset % HAL_FLASH_PAGE_SIZE) == 0) + { + HalFlashErase(oset / HAL_FLASH_PAGE_SIZE); + } + + HalFlashWrite(oset / HAL_FLASH_WORD_SIZE, pBuf, len / HAL_FLASH_WORD_SIZE); +} + +#if HAL_OAD_XNV_IS_INT +/********************************************************************* + * @fn HalOADAvail + * + * @brief Determine the space available for downloading an image. + * + * @param None. + * + * @return Number of bytes available for storing an OAD image. + *********************************************************************/ +uint32 HalOADAvail(void) +{ + /* + preamble_t preamble; + + HalOADRead(PREAMBLE_OFFSET, (uint8 *)&preamble, sizeof(preamble_t), HAL_OAD_RC); + return HAL_OAD_DL_MAX - preamble.len; + */ + return HAL_OAD_DL_MAX - HAL_OAD_DL_OSET; +} + +#elif HAL_OAD_XNV_IS_SPI + +/********************************************************************* + * CONSTANTS + */ + +#define XNV_STAT_CMD 0x05 +#define XNV_WREN_CMD 0x06 +#define XNV_WRPG_CMD 0x0A +#define XNV_READ_CMD 0x0B + +#define XNV_STAT_WIP 0x01 + +/********************************************************************* + * @fn xnvSPIWrite + * + * @brief SPI write sequence for code size savings. + * + * @param ch - The byte to write to the SPI. + * + * @return None. + *********************************************************************/ +static void xnvSPIWrite(uint8 ch); +static void xnvSPIWrite(uint8 ch) +{ + XNV_SPI_TX(ch); + XNV_SPI_WAIT_RXRDY(); +} + +/********************************************************************* + * @fn HalOADAvail + * + * @brief Determine the space available for downloading an image. + * + * @param None. + * + * @return Number of bytes available for storing an OAD image. + *********************************************************************/ +uint32 HalOADAvail(void) +{ + return HAL_OAD_DL_MAX - HAL_OAD_DL_OSET; +} + +/********************************************************************* + * @fn HalSPIRead + * + * @brief Read from the external NV storage via SPI. + * + * @param addr - Offset into the external NV. + * @param pBuf - Pointer to the buffer in which to copy the bytes read from external NV. + * @param len - Number of bytes to read from external NV. + * + * @return None. + *********************************************************************/ +static void HalSPIRead(uint32 addr, uint8 *pBuf, uint16 len) +{ +#if !HAL_OAD_BOOT_CODE + uint8 shdw = P1DIR; + halIntState_t his; + HAL_ENTER_CRITICAL_SECTION(his); + P1DIR |= BV(3); +#endif + + XNV_SPI_BEGIN(); + do { + xnvSPIWrite(XNV_STAT_CMD); + } while (XNV_SPI_RX() & XNV_STAT_WIP); + XNV_SPI_END(); + asm("NOP"); asm("NOP"); + + XNV_SPI_BEGIN(); + xnvSPIWrite(XNV_READ_CMD); + xnvSPIWrite(addr >> 16); + xnvSPIWrite(addr >> 8); + xnvSPIWrite(addr); + xnvSPIWrite(0); + + while (len--) + { + xnvSPIWrite(0); + *pBuf++ = XNV_SPI_RX(); + } + XNV_SPI_END(); + +#if !HAL_OAD_BOOT_CODE + P1DIR = shdw; + HAL_EXIT_CRITICAL_SECTION(his); +#endif +} + +/********************************************************************* + * @fn HalSPIWrite + * + * @brief Write to the external NV storage via SPI. + * + * @param addr - Offset into the external NV. + * @param pBuf - Pointer to the buffer in from which to write bytes to external NV. + * @param len - Number of bytes to write to external NV. + * + * @return None. + *********************************************************************/ +static void HalSPIWrite(uint32 addr, uint8 *pBuf, uint16 len) +{ + uint8 cnt; +#if !HAL_OAD_BOOT_CODE + uint8 shdw = P1DIR; + halIntState_t his; + HAL_ENTER_CRITICAL_SECTION(his); + P1DIR |= BV(3); +#endif + + while (len) + { + XNV_SPI_BEGIN(); + do { + xnvSPIWrite(XNV_STAT_CMD); + } while (XNV_SPI_RX() & XNV_STAT_WIP); + XNV_SPI_END(); + asm("NOP"); asm("NOP"); + + XNV_SPI_BEGIN(); + xnvSPIWrite(XNV_WREN_CMD); + XNV_SPI_END(); + asm("NOP"); asm("NOP"); + + XNV_SPI_BEGIN(); + xnvSPIWrite(XNV_WRPG_CMD); + xnvSPIWrite(addr >> 16); + xnvSPIWrite(addr >> 8); + xnvSPIWrite(addr); + + // Can only write within any one page boundary, so prepare for next page write if bytes remain. + cnt = 0 - (uint8)addr; + if (cnt) + { + addr += cnt; + } + else + { + addr += 256; + } + + do + { + xnvSPIWrite(*pBuf++); + cnt--; + len--; + } while (len && cnt); + XNV_SPI_END(); + } + +#if !HAL_OAD_BOOT_CODE + P1DIR = shdw; + HAL_EXIT_CRITICAL_SECTION(his); +#endif +} +#else +#error Invalid Xtra-NV for OAD. +#endif + +/************************************************************************************************** +*/ diff --git a/include/hal_oad.h b/include/hal_oad.h new file mode 100644 index 0000000..fafbddf --- /dev/null +++ b/include/hal_oad.h @@ -0,0 +1,130 @@ +/************************************************************************************************** + Filename: hal_oad.h + Revised: $Date:$ + Revision: $Revision:$ + + Description: This module defines optionally-compiled Boot Code parameters for the CC2x3x. + + Copyright 2008-2010 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +#ifndef HAL_OAD_H +#define HAL_OAD_H + +/********************************************************************* + * INCLUDES + */ + +#include "hal_board_cfg.h" +#include "hal_types.h" + +/********************************************************************* + * MACROS + */ + +#if !defined HAL_OAD_BOOT_CODE +#define HAL_OAD_BOOT_CODE FALSE +#endif + +// MSP430 needs to explicitly pack OAD data structures - 8051 is automatic with byte alignment. +#define PACK_1 + +/********************************************************************* + * CONSTANTS + */ + +// Placement controlled by oad.xcl. +#define HAL_OAD_RC_START 0x0800 +#define HAL_OAD_CRC_ADDR 0x0888 +#define HAL_OAD_CRC_OSET (HAL_OAD_CRC_ADDR - HAL_OAD_RC_START) + +/* Note that corresponding changes must be made to oad.xcl when changing the source of Xtra-NV. + * When using internal flash for XNV, (HAL_OAD_BOOT_PG_CNT + HAL_NV_PAGE_CNT) must be even. + */ +#define HAL_OAD_XNV_IS_INT FALSE +#define HAL_OAD_XNV_IS_SPI !HAL_OAD_XNV_IS_INT + +/* The oad/oad-boot.xcl files only need 1 page of boot code (located on the first flash page), + * but the Lock Bits Page is lost with OAD since it is not programmable. + * So discard it here by faking that boot needs 2 pages. + */ +#define HAL_OAD_BOOT_PG_CNT 2 + +/* To reduce the binary image size due to padding un-used code space, reduce HAL_OAD_DL_SIZE + * to the minimum required for your Application and make the corresponding changes to oad_app.xcl. + * This size must be an even multiple of HAL_FLASH_PAGE_SIZE. + */ +#if HAL_OAD_XNV_IS_SPI && !defined HAL_BOARD_CC2530EB_REV13 +#define HAL_OAD_DL_MAX 0x40000 +#define HAL_OAD_DL_SIZE (0x40000 - ((HAL_NV_PAGE_CNT+HAL_OAD_BOOT_PG_CNT)*HAL_FLASH_PAGE_SIZE)) +#define HAL_OAD_DL_OSET 0x0 // Configurable offset into an external NV. +#else +#define HAL_OAD_DL_MAX (0x40000 - ((HAL_NV_PAGE_CNT+HAL_OAD_BOOT_PG_CNT)*HAL_FLASH_PAGE_SIZE)) +#define HAL_OAD_DL_SIZE (HAL_OAD_DL_MAX / 2) +#define HAL_OAD_DL_OSET (HAL_OAD_DL_MAX / 2) +#endif + +// To run OAD with the legacy ZOAD.exe PC tool, place the preamble in this legacy location. +#define PREAMBLE_OFFSET 0x8C + +#if HAL_OAD_XNV_IS_INT +#define VDD_MIN_OAD VDD_MIN_NV +#else +#define VDD_MIN_OAD VDD_MIN_XNV +#endif + +/********************************************************************* + * TYPEDEFS + */ + +typedef enum { + HAL_OAD_RC, /* Run code / active image. */ + HAL_OAD_DL /* Downloaded code to be activated later. */ +} image_t; + +typedef struct { + uint8 magic[2]; + uint32 len; + uint16 vers; + uint16 manu; + uint16 prod; +} preamble_t; + +/********************************************************************* + * FUNCTIONS + */ + +uint8 HalOADChkDL(uint8 dlImagePreambleOffset); +void HalOADInvRC(void); +uint32 HalOADAvail(void); +void HalOADRead(uint32 oset, uint8 *pBuf, uint16 len, image_t type); +void HalOADWrite(uint32 oset, uint8 *pBuf, uint16 len, image_t type); +#endif diff --git a/include/hal_ota.c b/include/hal_ota.c new file mode 100644 index 0000000..7ea4b41 --- /dev/null +++ b/include/hal_ota.c @@ -0,0 +1,532 @@ +/****************************************************************************** + Filename: hal_ota.c + Revised: $Date: 2010-11-18 08:22:50 -0800 (Thu, 18 Nov 2010) $ + Revision: $Revision: 24438 $ + + Description: This module contains optionally-compiled Boot Code to support + OTA. The rest of the functionality is the H/W specific drivers + to read/write the flash/NV containing the ACTIVE and the + DOWNLOADED images. + Notes: Targets the Texas Instruments CC253x family of processors. + + + Copyright 2010 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +******************************************************************************/ + +/****************************************************************************** + * INCLUDES + */ +#include "comdef.h" +#include "hal_board_cfg.h" +#include "hal_dma.h" +#include "hal_flash.h" +#include "hal_ota.h" +#include "hal_types.h" + +#include "ota_common.h" + +/****************************************************************************** + * CONSTANTS + */ +#if HAL_OTA_XNV_IS_SPI +#define XNV_STAT_CMD 0x05 +#define XNV_WREN_CMD 0x06 +#define XNV_WRPG_CMD 0x0A +#define XNV_READ_CMD 0x0B + +#define XNV_STAT_WIP 0x01 +#endif + +/****************************************************************************** + * TYPEDEFS + */ +typedef struct +{ + uint16 crc[2]; + uint32 programSize; +} OTA_CrcControl_t; + +/****************************************************************************** + * LOCAL VARIABLES + */ +OTA_CrcControl_t OTA_crcControl; + +#if HAL_OTA_BOOT_CODE +halDMADesc_t dmaCh0; +#endif + +/****************************************************************************** + * LOCAL FUNCTIONS + */ +static uint16 runPoly(uint16 crc, uint8 val); + +#if HAL_OTA_XNV_IS_SPI +static void HalSPIRead(uint32 addr, uint8 *pBuf, uint16 len); +static void HalSPIWrite(uint32 addr, uint8 *pBuf, uint16 len); +static void xnvSPIWrite(uint8 ch); +#endif + +#if HAL_OTA_BOOT_CODE +static void dl2rc(void); +static uint16 crcCalc(void); + +/****************************************************************************** + * @fn main + * + * @brief ISR for the reset vector. + * + * @param None. + * + * @return None. + */ +#pragma location="NEAR_CODE" +void main(void) +{ + HAL_BOARD_INIT(); +#if HAL_OTA_XNV_IS_SPI + XNV_SPI_INIT(); +#endif + /* This is in place of calling HalDmaInit() which would require init of the + * other 4 DMA descriptors in addition to just Channel 0. + */ + HAL_DMA_SET_ADDR_DESC0( &dmaCh0 ); + + while (1) + { + HalFlashRead(HAL_OTA_CRC_ADDR / HAL_FLASH_PAGE_SIZE, + HAL_OTA_CRC_ADDR % HAL_FLASH_PAGE_SIZE, + (uint8 *)&OTA_crcControl, sizeof(OTA_crcControl)); + + if (OTA_crcControl.crc[0] == OTA_crcControl.crc[1]) + { + break; + } + else if ((OTA_crcControl.crc[0] != 0) && (OTA_crcControl.crc[0] == crcCalc())) + { + OTA_crcControl.crc[1] = OTA_crcControl.crc[0]; + HalFlashWrite((HAL_OTA_CRC_ADDR / HAL_FLASH_WORD_SIZE), (uint8 *)OTA_crcControl.crc, 1); + } + else + { + dl2rc(); + } + } + + // Simulate a reset for the Application code by an absolute jump to location 0x0800. + asm("LJMP 0x800\n"); +} + +/****************************************************************************** + * @fn dl2rc + * + * @brief Copy the DL image to the RC image location. + * + * NOTE: Assumes that DL image ends on a flash word boundary. + * + * @param None. + * + * @return None. + */ +static void dl2rc(void) +{ + uint32 oset; + OTA_SubElementHdr_t subElement; + OTA_ImageHeader_t header; + uint16 addr = HAL_OTA_RC_START / HAL_FLASH_WORD_SIZE; + uint8 buf[4]; + + // Determine the length and starting point of the upgrade image + HalOTARead(0, (uint8 *)&header, sizeof(OTA_ImageHeader_t), HAL_OTA_DL); + HalOTARead(header.headerLength, (uint8*)&subElement, OTA_SUB_ELEMENT_HDR_LEN, HAL_OTA_DL); + + for (oset = 0; oset < subElement.length; oset += HAL_FLASH_WORD_SIZE) + { + HalOTARead(oset + header.headerLength + OTA_SUB_ELEMENT_HDR_LEN, buf, HAL_FLASH_WORD_SIZE, HAL_OTA_DL); + if ((addr % (HAL_FLASH_PAGE_SIZE / HAL_FLASH_WORD_SIZE)) == 0) + { + HalFlashErase(addr / (HAL_FLASH_PAGE_SIZE / HAL_FLASH_WORD_SIZE)); + } + HalFlashWrite(addr++, buf, 1); + } +} + +/****************************************************************************** + * @fn crcCalc + * + * @brief Run the CRC16 Polynomial calculation over the RC image. + * + * @param None. + * + * @return The CRC16 calculated. + */ +static uint16 crcCalc() +{ + uint32 oset; + uint16 crc = 0; + + // Run the CRC calculation over the active body of code. + for (oset = 0; oset < OTA_crcControl.programSize; oset++) + { + if ((oset < HAL_OTA_CRC_OSET) || (oset >= HAL_OTA_CRC_OSET + 4)) + { + uint8 buf; + HalOTARead(oset, &buf, 1, HAL_OTA_RC); + crc = runPoly(crc, buf); + } + } + + return crc; +} +#endif //HAL_OTA_BOOT_CODE + +/****************************************************************************** + * @fn runPoly + * + * @brief Run the CRC16 Polynomial calculation over the byte parameter. + * + * @param crc - Running CRC calculated so far. + * @param val - Value on which to run the CRC16. + * + * @return crc - Updated for the run. + */ +static uint16 runPoly(uint16 crc, uint8 val) +{ + const uint16 poly = 0x1021; + uint8 cnt; + + for (cnt = 0; cnt < 8; cnt++, val <<= 1) + { + uint8 msb = (crc & 0x8000) ? 1 : 0; + + crc <<= 1; + if (val & 0x80) crc |= 0x0001; + if (msb) crc ^= poly; + } + + return crc; +} + +/****************************************************************************** + * @fn HalOTAChkDL + * + * @brief Run the CRC16 Polynomial calculation over the DL image. + * + * @param None + * + * @return SUCCESS or FAILURE. + */ +uint8 HalOTAChkDL(uint8 dlImagePreambleOffset) +{ + (void)dlImagePreambleOffset; // Intentionally unreferenced parameter + + uint32 oset; + uint16 crc = 0; + OTA_CrcControl_t crcControl; + OTA_ImageHeader_t header; + uint32 programStart; + +#if HAL_OTA_XNV_IS_SPI + XNV_SPI_INIT(); +#endif + + // Read the OTA File Header + HalOTARead(0, (uint8 *)&header, sizeof(OTA_ImageHeader_t), HAL_OTA_DL); + + // Calculate the update image start address + programStart = header.headerLength + OTA_SUB_ELEMENT_HDR_LEN; + + // Get the CRC Control structure + HalOTARead(programStart + HAL_OTA_CRC_OSET, (uint8 *)&crcControl, sizeof(crcControl), HAL_OTA_DL); + + if ((crcControl.programSize > HAL_OTA_DL_MAX) || (crcControl.programSize == 0)) + { + return FAILURE; + } + + // Run the CRC calculation over the downloaded image. + for (oset = 0; oset < crcControl.programSize; oset++) + { + if ((oset < HAL_OTA_CRC_OSET) || (oset >= HAL_OTA_CRC_OSET+4)) + { + uint8 buf; + HalOTARead(oset + programStart, &buf, 1, HAL_OTA_DL); + crc = runPoly(crc, buf); + } + } + + return (crcControl.crc[0] == crc) ? SUCCESS : FAILURE; +} + +/****************************************************************************** + * @fn HalOTAInvRC + * + * @brief Invalidate the active image so that the boot code will instantiate + * the DL image on the next reset. + * + * @param None. + * + * @return None. + */ +void HalOTAInvRC(void) +{ + uint16 crc[2] = {0,0xFFFF}; + HalFlashWrite((HAL_OTA_CRC_ADDR / HAL_FLASH_WORD_SIZE), (uint8 *)crc, 1); +} + +/****************************************************************************** + * @fn HalOTARead + * + * @brief Read from the storage medium according to image type. + * + * @param oset - Offset into the monolithic image. + * @param pBuf - Pointer to the buffer in which to copy the bytes read. + * @param len - Number of bytes to read. + * @param type - Which image: HAL_OTA_RC or HAL_OTA_DL. + * + * @return None. + */ +void HalOTARead(uint32 oset, uint8 *pBuf, uint16 len, image_t type) +{ + if (HAL_OTA_RC != type) + { +#if HAL_OTA_XNV_IS_INT + preamble_t preamble; + + HalOTARead(PREAMBLE_OFFSET, (uint8 *)&preamble, sizeof(preamble_t), HAL_OTA_RC); + oset += HAL_OTA_RC_START + HAL_OTA_DL_OSET; +#elif HAL_OTA_XNV_IS_SPI + oset += HAL_OTA_DL_OSET; + HalSPIRead(oset, pBuf, len); + return; +#endif + } + else + { + oset += HAL_OTA_RC_START; + } + + HalFlashRead(oset / HAL_FLASH_PAGE_SIZE, oset % HAL_FLASH_PAGE_SIZE, pBuf, len); +} + +/****************************************************************************** + * @fn HalOTAWrite + * + * @brief Write to the storage medium according to the image type. + * + * NOTE: Destructive write on page boundary! When writing to the first flash word + * of a page boundary, the page is erased without saving/restoring the bytes not written. + * Writes anywhere else on a page assume that the location written to has been erased. + * + * @param oset - Offset into the monolithic image, aligned to HAL_FLASH_WORD_SIZE. + * @param pBuf - Pointer to the buffer in from which to write. + * @param len - Number of bytes to write. If not an even multiple of HAL_FLASH_WORD_SIZE, + * remainder bytes are overwritten with garbage. + * @param type - Which image: HAL_OTA_RC or HAL_OTA_DL. + * + * @return None. + */ +void HalOTAWrite(uint32 oset, uint8 *pBuf, uint16 len, image_t type) +{ + if (HAL_OTA_RC != type) + { +#if HAL_OTA_XNV_IS_INT + oset += HAL_OTA_RC_START + HAL_OTA_DL_OSET; +#elif HAL_OTA_XNV_IS_SPI + oset += HAL_OTA_DL_OSET; + HalSPIWrite(oset, pBuf, len); + return; +#endif + } + else + { + oset += HAL_OTA_RC_START; + } + + if ((oset % HAL_FLASH_PAGE_SIZE) == 0) + { + HalFlashErase(oset / HAL_FLASH_PAGE_SIZE); + } + + HalFlashWrite(oset / HAL_FLASH_WORD_SIZE, pBuf, len / HAL_FLASH_WORD_SIZE); +} + +/****************************************************************************** + * @fn HalOTAAvail + * + * @brief Determine the space available for downloading an image. + * + * @param None. + * + * @return Number of bytes available for storing an OTA image. + */ +uint32 HalOTAAvail(void) +{ + return HAL_OTA_DL_MAX - HAL_OTA_DL_OSET; +} + +#if HAL_OTA_XNV_IS_SPI +/****************************************************************************** + * @fn xnvSPIWrite + * + * @brief SPI write sequence for code size savings. + * + * @param ch - The byte to write to the SPI. + * + * @return None. + */ +static void xnvSPIWrite(uint8 ch) +{ + XNV_SPI_TX(ch); + XNV_SPI_WAIT_RXRDY(); +} + +/****************************************************************************** + * @fn HalSPIRead + * + * @brief Read from the external NV storage via SPI. + * + * @param addr - Offset into the external NV. + * @param pBuf - Pointer to buffer to copy the bytes read from external NV. + * @param len - Number of bytes to read from external NV. + * + * @return None. + *****************************************************************************/ +static void HalSPIRead(uint32 addr, uint8 *pBuf, uint16 len) +{ +#if !HAL_OTA_BOOT_CODE + uint8 shdw = P1DIR; + halIntState_t his; + HAL_ENTER_CRITICAL_SECTION(his); + P1DIR |= BV(3); +#endif + + XNV_SPI_BEGIN(); + do + { + xnvSPIWrite(XNV_STAT_CMD); + } while (XNV_SPI_RX() & XNV_STAT_WIP); + XNV_SPI_END(); + asm("NOP"); asm("NOP"); + + XNV_SPI_BEGIN(); + xnvSPIWrite(XNV_READ_CMD); + xnvSPIWrite(addr >> 16); + xnvSPIWrite(addr >> 8); + xnvSPIWrite(addr); + xnvSPIWrite(0); + + while (len--) + { + xnvSPIWrite(0); + *pBuf++ = XNV_SPI_RX(); + } + XNV_SPI_END(); + +#if !HAL_OTA_BOOT_CODE + P1DIR = shdw; + HAL_EXIT_CRITICAL_SECTION(his); +#endif +} + +/****************************************************************************** + * @fn HalSPIWrite + * + * @brief Write to the external NV storage via SPI. + * + * @param addr - Offset into the external NV. + * @param pBuf - Pointer to the buffer in from which to write bytes to external NV. + * @param len - Number of bytes to write to external NV. + * + * @return None. + *****************************************************************************/ +static void HalSPIWrite(uint32 addr, uint8 *pBuf, uint16 len) +{ + uint8 cnt; +#if !HAL_OTA_BOOT_CODE + uint8 shdw = P1DIR; + halIntState_t his; + HAL_ENTER_CRITICAL_SECTION(his); + P1DIR |= BV(3); +#endif + + while (len) + { + XNV_SPI_BEGIN(); + do + { + xnvSPIWrite(XNV_STAT_CMD); + } while (XNV_SPI_RX() & XNV_STAT_WIP); + XNV_SPI_END(); + asm("NOP"); asm("NOP"); + + XNV_SPI_BEGIN(); + xnvSPIWrite(XNV_WREN_CMD); + XNV_SPI_END(); + asm("NOP"); asm("NOP"); + + XNV_SPI_BEGIN(); + xnvSPIWrite(XNV_WRPG_CMD); + xnvSPIWrite(addr >> 16); + xnvSPIWrite(addr >> 8); + xnvSPIWrite(addr); + + // Can only write within any one page boundary, so prepare for next page write if bytes remain. + cnt = 0 - (uint8)addr; + if (cnt) + { + addr += cnt; + } + else + { + addr += 256; + } + + do + { + xnvSPIWrite(*pBuf++); + cnt--; + len--; + } while (len && cnt); + XNV_SPI_END(); + } + +#if !HAL_OTA_BOOT_CODE + P1DIR = shdw; + HAL_EXIT_CRITICAL_SECTION(his); +#endif +} + +#elif !HAL_OTA_XNV_IS_INT +#error Invalid Xtra-NV for OTA. +#endif + +/****************************************************************************** +*/ diff --git a/include/hal_ota.h b/include/hal_ota.h new file mode 100644 index 0000000..3b43bca --- /dev/null +++ b/include/hal_ota.h @@ -0,0 +1,128 @@ +/****************************************************************************** + Filename: hal_ota.h + Revised: $Date: 2010-11-18 08:22:50 -0800 (Thu, 18 Nov 2010) $ + Revision: $Revision: 24438 $ + + Description: This module defines optionally-compiled Boot Code parameters + for the CC253x. + + Copyright 2010 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +******************************************************************************/ + +#ifndef HAL_OTA_H +#define HAL_OTA_H + +/****************************************************************************** + * INCLUDES + */ + +#include "hal_board_cfg.h" +#include "hal_types.h" + +/****************************************************************************** + * MACROS + */ + +#if !defined HAL_OTA_BOOT_CODE +#define HAL_OTA_BOOT_CODE FALSE +#endif + +// MSP430 needs to explicitly pack OTA data structures - 8051 is automatic with byte alignment. +#define PACK_1 + +/****************************************************************************** + * CONSTANTS + */ + +// Placement controlled by ota.xcl. +#define HAL_OTA_RC_START 0x0800 +#define HAL_OTA_CRC_ADDR 0x0888 +#define HAL_OTA_CRC_OSET (HAL_OTA_CRC_ADDR - HAL_OTA_RC_START) + +/* Note that corresponding changes must be made to ota.xcl when changing the source of Xtra-NV. + * When using internal flash for XNV, (HAL_OTA_BOOT_PG_CNT + HAL_NV_PAGE_CNT) must be even. + */ +#define HAL_OTA_XNV_IS_INT FALSE +#define HAL_OTA_XNV_IS_SPI !HAL_OTA_XNV_IS_INT + +/* The ota/ota-boot.xcl files only need 1 page of boot code (located on the first flash page), + * but the Lock Bits Page is lost with OTA since it is not programmable. + * So discard it here by faking that boot needs 2 pages. + */ +#define HAL_OTA_BOOT_PG_CNT 2 + +/* To reduce the binary image size due to padding un-used code space, reduce HAL_OTA_DL_SIZE + * to the minimum required for your Application and make the corresponding changes to ota_app.xcl. + * This size must be an even multiple of HAL_FLASH_PAGE_SIZE. + */ +#if HAL_OTA_XNV_IS_SPI && !defined HAL_BOARD_CC2530EB_REV13 +#define HAL_OTA_DL_MAX 0x40000 +#define HAL_OTA_DL_SIZE (0x40000 - ((HAL_NV_PAGE_CNT+HAL_OTA_BOOT_PG_CNT)*HAL_FLASH_PAGE_SIZE)) +#define HAL_OTA_DL_OSET 0x0 // Configurable offset into an external NV. +#else +#define HAL_OTA_DL_MAX (0x40000 - ((HAL_NV_PAGE_CNT+HAL_OTA_BOOT_PG_CNT)*HAL_FLASH_PAGE_SIZE)) +#define HAL_OTA_DL_SIZE (HAL_OTA_DL_MAX / 2) +#define HAL_OTA_DL_OSET (HAL_OTA_DL_MAX / 2) +#endif + +#define PREAMBLE_OFFSET 0x8C + +/********************************************************************* + * TYPEDEFS + */ + +typedef enum { + HAL_OTA_RC, /* Run code / active image. */ + HAL_OTA_DL /* Downloaded code to be activated later. */ +} image_t; + +typedef struct { + uint16 crc; + uint16 crc_shadow; +} otaCrc_t; + +typedef struct { + uint32 programLength; + uint16 manufacturerId; + uint16 imageType; + uint32 imageVersion; +} preamble_t; + +/********************************************************************* + * FUNCTIONS + */ + +uint8 HalOTAChkDL(uint8 dlImagePreambleOffset); +void HalOTAInvRC(void); +uint32 HalOTAAvail(void); +void HalOTARead(uint32 oset, uint8 *pBuf, uint16 len, image_t type); +void HalOTAWrite(uint32 oset, uint8 *pBuf, uint16 len, image_t type); +#endif diff --git a/include/hal_rpc.h b/include/hal_rpc.h new file mode 100644 index 0000000..cf8118e --- /dev/null +++ b/include/hal_rpc.h @@ -0,0 +1,136 @@ +/************************************************************************************************** + Filename: hal_rpc.h + Revised: $Date: 2007-07-06 10:42:24 -0700 (Fri, 06 Jul 2007) $ + Revision: $Revision: 13579 $ + + Description: This file contains information required by services that + use the Remote Procedure Call (RPC) standard. + + Copyright 2008 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +#ifndef HAL_RPC_H +#define HAL_RPC_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* ------------------------------------------------------------------------------------------------ + * Includes + * ------------------------------------------------------------------------------------------------ + */ + +/* ------------------------------------------------------------------------------------------------ + * Constants + * ------------------------------------------------------------------------------------------------ + */ + +/////////////////////////////////////////////////////////////////////////////////////////////////// +// Common +/////////////////////////////////////////////////////////////////////////////////////////////////// + +// RPC Command Field Type +#define RPC_CMD_POLL 0x00 +#define RPC_CMD_SREQ 0x20 +#define RPC_CMD_AREQ 0x40 +#define RPC_CMD_SRSP 0x60 +#define RPC_CMD_RES4 0x80 +#define RPC_CMD_RES5 0xA0 +#define RPC_CMD_RES6 0xC0 +#define RPC_CMD_RES7 0xE0 + +// RPC Command Field Subsystem +#define RPC_SYS_RES0 0 +#define RPC_SYS_SYS 1 +#define RPC_SYS_MAC 2 +#define RPC_SYS_NWK 3 +#define RPC_SYS_AF 4 +#define RPC_SYS_ZDO 5 +#define RPC_SYS_SAPI 6 // Simple API +#define RPC_SYS_UTIL 7 +#define RPC_SYS_DBG 8 +#define RPC_SYS_APP 9 +#define RPC_SYS_RCAF 10 // Remote Control Application Framework +#define RPC_SYS_RCN 11 // Remote Control Network Layer +#define RPC_SYS_RCN_CLIENT 12 // Remote Control Network Layer Client +#define RPC_SYS_BOOT 13 // Serial Bootloader +#define RPC_SYS_MAX 14 // Maximum value, must be last + +/* 1st byte is the length of the data field, 2nd/3rd bytes are command field. */ +#define RPC_FRAME_HDR_SZ 3 + +/* The 3 MSB's of the 1st command field byte are for command type. */ +#define RPC_CMD_TYPE_MASK 0xE0 + +/* The 5 LSB's of the 1st command field byte are for the subsystem. */ +#define RPC_SUBSYSTEM_MASK 0x1F + +/* position of fields in the general format frame */ +#define RPC_POS_LEN 0 +#define RPC_POS_CMD0 1 +#define RPC_POS_CMD1 2 +#define RPC_POS_DAT0 3 + +/* Error codes */ +#define RPC_SUCCESS 0 /* success */ +#define RPC_ERR_SUBSYSTEM 1 /* invalid subsystem */ +#define RPC_ERR_COMMAND_ID 2 /* invalid command ID */ +#define RPC_ERR_PARAMETER 3 /* invalid parameter */ +#define RPC_ERR_LENGTH 4 /* invalid length */ + +/////////////////////////////////////////////////////////////////////////////////////////////////// +// UART Specific +/////////////////////////////////////////////////////////////////////////////////////////////////// + +#define RPC_UART_FRAME_OVHD 2 + +// Start of frame character value +#define RPC_UART_SOF 0xFE + +/////////////////////////////////////////////////////////////////////////////////////////////////// +// SPI Specific +/////////////////////////////////////////////////////////////////////////////////////////////////// + + +/* ------------------------------------------------------------------------------------------------ + * Typedefs + * ------------------------------------------------------------------------------------------------ + */ + +/************************************************************************************************** +*/ + +#ifdef __cplusplus +}; +#endif + +#endif /* HAL_RPC_H */ diff --git a/include/hal_sleep.c b/include/hal_sleep.c new file mode 100644 index 0000000..88d3c36 --- /dev/null +++ b/include/hal_sleep.c @@ -0,0 +1,600 @@ +/************************************************************************************** + Filename: hal_sleep.c + Revised: $Date: 2014-12-19 13:07:30 -0800 (Fri, 19 Dec 2014) $ + Revision: $Revision: 41556 $ + + Description: This module contains the HAL power management procedures for the CC2530. + + + Copyright 2006-2014 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +/* ------------------------------------------------------------------------------------------------ + * Includes + * ------------------------------------------------------------------------------------------------ + */ +#include "hal_types.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "hal_sleep.h" +#include "hal_led.h" +#include "hal_key.h" +#include "mac_api.h" +#include "OSAL.h" +#include "OSAL_Timers.h" +#include "OSAL_Tasks.h" +#include "OSAL_PwrMgr.h" +#include "OnBoard.h" +#include "hal_drivers.h" +#include "hal_assert.h" +#include "mac_mcu.h" + +#ifndef ZG_BUILD_ENDDEVICE_TYPE +# define ZG_BUILD_ENDDEVICE_TYPE FALSE +#endif + +#if ZG_BUILD_ENDDEVICE_TYPE && defined (NWK_AUTO_POLL) +#include "nwk_globals.h" +#include "ZGlobals.h" +#endif + +/* ------------------------------------------------------------------------------------------------ + * Macros + * ------------------------------------------------------------------------------------------------ + */ + +/* POWER CONSERVATION DEFINITIONS + * Sleep mode H/W definitions (enabled with POWER_SAVING compile option) + */ +#define CC2530_PM0 0 /* PM0, Clock oscillators on, voltage regulator on */ +#define CC2530_PM1 1 /* PM1, 32.768 kHz oscillators on, voltage regulator on */ +#define CC2530_PM2 2 /* PM2, 32.768 kHz oscillators on, voltage regulator off */ +#define CC2530_PM3 3 /* PM3, All clock oscillators off, voltage regulator off */ + +/* HAL power management mode is set according to the power management state. The default + * setting is HAL_SLEEP_OFF. The actual value is tailored to different HW platform. Both + * HAL_SLEEP_TIMER and HAL_SLEEP_DEEP selections will: + * 1. turn off the system clock, and + * 2. halt the MCU. + * HAL_SLEEP_TIMER can be woken up by sleep timer interrupt, I/O interrupt and reset. + * HAL_SLEEP_DEEP can be woken up by I/O interrupt and reset. + */ +#define HAL_SLEEP_OFF CC2530_PM0 +#define HAL_SLEEP_TIMER CC2530_PM2 +#define HAL_SLEEP_DEEP CC2530_PM3 + +/* MAX_SLEEP_TIME calculation: + * Sleep timer maximum duration = 0xFFFF7F / 32768 Hz = 511.996 seconds + * Round it to 510 seconds or 510000 ms + */ +#define MAX_SLEEP_TIME 510000 /* maximum time to sleep allowed by ST */ + +/* + * Choosing value to be lower than MAC_BACKOFF_TIMER_DEFAULT_NONBEACON_ROLLOVER + * The unit is in ms. The back off timer rollover should be greater + * than the value below + */ +#define MAX_SLEEP_LOOP_TIME 0x510000 /* ~84 minutes */ +#define TICKS_SUBTRACTED 2 + +/* minimum time to sleep, this macro is to: + * 1. avoid thrashing in-and-out of sleep with short OSAL timer (~2ms) + * 2. define minimum safe sleep period + */ +#if !defined (PM_MIN_SLEEP_TIME) +#define PM_MIN_SLEEP_TIME 2 /* default to minimum safe sleep time minimum CAP */ +#endif + +/* The PCON instruction must be 4-byte aligned. The following code may cause excessive power + * consumption if not aligned. See linker file ".xcl" for actual placement. + */ +#pragma location = "SLEEP_CODE" +void halSetSleepMode(void); + +/* This value is used to adjust the sleep timer compare value such that the sleep timer + * compare takes into account the amount of processing time spent in function halSleep(). + * The first value is determined by measuring the number of sleep timer ticks it from + * the beginning of the function to entering sleep mode or more precisely, when + * MAC_PwrNextTimeout() is called. The second value is determined by measuring the number + * of sleep timer ticks from exit of sleep mode to the call to MAC_PwrOnReq() where the + * MAC timer is restarted. + */ +#define HAL_SLEEP_ADJ_TICKS (11 + 12) + +#ifndef HAL_SLEEP_DEBUG_POWER_MODE +/* set CC2530 power mode; always use PM2 */ +#define HAL_SLEEP_PREP_POWER_MODE(mode) st( SLEEPCMD &= ~PMODE; /* clear mode bits */ \ + SLEEPCMD |= mode; /* set mode bits */ \ + while (!(STLOAD & LDRDY)); \ + halSleepPconValue = PCON_IDLE; \ + ) +#define HAL_SLEEP_SET_POWER_MODE() halSetSleepMode() +#else +/* Debug: don't set power mode, just block until sleep timer interrupt */ +#define HAL_SLEEP_PREP_POWER_MODE(mode) /* nothing */ +#define HAL_SLEEP_SET_POWER_MODE() st( while(halSleepInt == FALSE); \ + halSleepInt = FALSE; \ + HAL_DISABLE_INTERRUPTS(); \ + ) +#endif + +/* sleep and external interrupt port masks */ +#define STIE_BV BV(5) +#define P0IE_BV BV(5) +#define P1IE_BV BV(4) +#define P2IE_BV BV(1) + +/* sleep timer interrupt control */ +#define HAL_SLEEP_TIMER_ENABLE_INT() st(IEN0 |= STIE_BV;) /* enable sleep timer interrupt */ +#define HAL_SLEEP_TIMER_DISABLE_INT() st(IEN0 &= ~STIE_BV;) /* disable sleep timer interrupt */ +#define HAL_SLEEP_TIMER_CLEAR_INT() st(STIF = 0;) /* clear sleep interrupt flag */ + +/* backup interrupt enable registers before sleep */ +#define HAL_SLEEP_IE_BACKUP_AND_DISABLE(ien0, ien1, ien2) st(ien0 = IEN0; /* backup IEN0 register */ \ + ien1 = IEN1; /* backup IEN1 register */ \ + ien2 = IEN2; /* backup IEN2 register */ \ + IEN0 &= STIE_BV; /* disable IEN0 except STIE */ \ + IEN1 &= P0IE_BV; /* disable IEN1 except P0IE */ \ + IEN2 &= (P1IE_BV|P2IE_BV);) /* disable IEN2 except P1IE, P2IE */ + +/* restore interrupt enable registers before sleep */ +#define HAL_SLEEP_IE_RESTORE(ien0, ien1, ien2) st(IEN0 = ien0; /* restore IEN0 register */ \ + IEN1 = ien1; /* restore IEN1 register */ \ + IEN2 = ien2;) /* restore IEN2 register */ + +/* convert msec to 320 usec units with round */ +#define HAL_SLEEP_MS_TO_320US(ms) (((((uint32) (ms)) * 100) + 31) / 32) + +/* for optimized indexing of uint32's */ +#if HAL_MCU_LITTLE_ENDIAN() +#define UINT32_NDX0 0 +#define UINT32_NDX1 1 +#define UINT32_NDX2 2 +#define UINT32_NDX3 3 +#else +#define UINT32_NDX0 3 +#define UINT32_NDX1 2 +#define UINT32_NDX2 1 +#define UINT32_NDX3 0 +#endif + +/* ------------------------------------------------------------------------------------------------ + * Global Variables + * ------------------------------------------------------------------------------------------------ + */ +/* PCON register value to program when setting power mode */ +volatile __data uint8 halSleepPconValue = PCON_IDLE; + +static uint32 maxSleepLoopTime = HAL_SLEEP_MS_TO_320US(MAX_SLEEP_LOOP_TIME); + +/* ------------------------------------------------------------------------------------------------ + * Local Variables + * ------------------------------------------------------------------------------------------------ + */ + +/* HAL power management mode is set according to the power management state. + */ +static uint8 halPwrMgtMode = HAL_SLEEP_OFF; + +#ifdef HAL_SLEEP_DEBUG_POWER_MODE +static bool halSleepInt = FALSE; +#endif + +/* ------------------------------------------------------------------------------------------------ + * Function Prototypes + * ------------------------------------------------------------------------------------------------ + */ + +void halSleepSetTimer(uint32 timeout); + +/************************************************************************************************** + * @fn halSetSleepMode + * + * @brief This function put the CC2530 to sleep. The PCON instruction must be 4-byte aligned. + * The following code may cause excessive power consumption if not aligned. See linker + * file ".xcl" for actual placement. + * + * input parameters + * + * @param None. + * + * output parameters + * + * None. + * + * @return None. + ************************************************************************************************** + */ +void halSetSleepMode(void) +{ + PCON = halSleepPconValue; + HAL_DISABLE_INTERRUPTS(); +} + +/************************************************************************************************** + * @fn halSetMaxSleepLoopTime + * + * @brief This function is to used to setup the maximum sleep loop time. This sleep loop time + * should be lesser than T2 rollover so that a maximum of only one rollover occurs + * when cc2530 is in sleep. This function should be called whenever rolloverTime is + * changed using the function macBackoffTimerSetRollover(macTimerRollover); + * + * input parameters + * + * @param rolloverTime. + * + * output parameters + * + * None. + * + * @return None. + ************************************************************************************************** + */ +void halSetMaxSleepLoopTime(uint32 rolloverTime) +{ + if( rolloverTime > HAL_SLEEP_MS_TO_320US(MAX_SLEEP_TIME) ) + { + rolloverTime = HAL_SLEEP_MS_TO_320US(MAX_SLEEP_TIME); + } + maxSleepLoopTime = (rolloverTime - TICKS_SUBTRACTED); +} + +/************************************************************************************************** + * @fn halSleep + * + * @brief This function is called from the OSAL task loop using and existing OSAL + * interface. It sets the low power mode of the MAC and the CC2530. + * + * input parameters + * + * @param osal_timeout - Next OSAL timer timeout. + * + * output parameters + * + * None. + * + * @return None. + ************************************************************************************************** + */ +void halSleep( uint32 osal_timeout ) +{ + uint32 timeout; + uint32 macTimeout = 0; + + /* get next OSAL timer expiration converted to 320 usec units */ + timeout = HAL_SLEEP_MS_TO_320US(osal_timeout); + if (timeout == 0) + { + timeout = MAC_PwrNextTimeout(); + } + else + { + /* get next MAC timer expiration */ + macTimeout = MAC_PwrNextTimeout(); + + /* get lesser of two timeouts */ + if ((macTimeout != 0) && (macTimeout < timeout)) + { + timeout = macTimeout; + } + } + + /* HAL_SLEEP_PM2 is entered only if the timeout is zero and + * the device is a stimulated device. + */ + halPwrMgtMode = (timeout == 0) ? HAL_SLEEP_DEEP : HAL_SLEEP_TIMER; + + /* DEEP sleep can only be entered when zgPollRate == 0. + * This is to eliminate any possibility of entering PM3 between + * two network timers. + */ +#if ZG_BUILD_ENDDEVICE_TYPE && defined (NWK_AUTO_POLL) + if ((timeout > HAL_SLEEP_MS_TO_320US(PM_MIN_SLEEP_TIME)) || + (timeout == 0 && zgPollRate == 0)) +#else + if ((timeout > HAL_SLEEP_MS_TO_320US(PM_MIN_SLEEP_TIME)) || + (timeout == 0)) +#endif + { + halIntState_t ien0, ien1, ien2; + + HAL_ASSERT(HAL_INTERRUPTS_ARE_ENABLED()); + HAL_DISABLE_INTERRUPTS(); + + /* always use "deep sleep" to turn off radio VREG on CC2530 */ + if (halSleepPconValue != 0 && MAC_PwrOffReq(MAC_PWR_SLEEP_DEEP) == MAC_SUCCESS) + { + /* The PCON value is not zero. There is no interrupt overriding the + * sleep decision. Also, the radio granted the sleep request. + */ + +#if ((defined HAL_KEY) && (HAL_KEY == TRUE)) + /* get peripherals ready for sleep */ + HalKeyEnterSleep(); +#endif + +#ifdef HAL_SLEEP_DEBUG_LED + HAL_TURN_OFF_LED3(); +#else + /* use this to turn LEDs off during sleep */ + HalLedEnterSleep(); +#endif + + if(timeout > maxSleepLoopTime) + { + timeout = maxSleepLoopTime; + } + + do + { + /* enable sleep timer interrupt */ + if(timeout != 0) + { + if (timeout > HAL_SLEEP_MS_TO_320US( MAX_SLEEP_TIME )) + { + timeout -= HAL_SLEEP_MS_TO_320US( MAX_SLEEP_TIME ); + halSleepSetTimer(HAL_SLEEP_MS_TO_320US( MAX_SLEEP_TIME )); + } + else + { + /* set sleep timer */ + halSleepSetTimer(timeout); + timeout = 0; + } + + /* set up sleep timer interrupt */ + HAL_SLEEP_TIMER_CLEAR_INT(); + HAL_SLEEP_TIMER_ENABLE_INT(); + } + +#ifdef HAL_SLEEP_DEBUG_LED + if (halPwrMgtMode == CC2530_PM1) + { + HAL_TURN_ON_LED1(); + } + else + { + HAL_TURN_OFF_LED1(); + } +#endif + /* Prep CC2530 power mode */ + HAL_SLEEP_PREP_POWER_MODE(halPwrMgtMode); + + /* save interrupt enable registers and disable all interrupts */ + HAL_SLEEP_IE_BACKUP_AND_DISABLE(ien0, ien1, ien2); + HAL_ENABLE_INTERRUPTS(); + + /* set CC2530 power mode, interrupt is disabled after this function + * Note that an ISR (that could wake up from power mode) which runs + * between the previous instruction enabling interrupts and before + * power mode is set would switch the halSleepPconValue so that + * power mode shall not be entered in such a case. + */ + + HAL_SLEEP_SET_POWER_MODE(); + + /* the interrupt is disabled - see halSetSleepMode() */ + + /* restore interrupt enable registers */ + HAL_SLEEP_IE_RESTORE(ien0, ien1, ien2); + + /* disable sleep timer interrupt */ + HAL_SLEEP_TIMER_DISABLE_INT(); + +#ifdef HAL_SLEEP_DEBUG_LED + HAL_TURN_ON_LED3(); +#else + /* use this to turn LEDs back on after sleep */ + HalLedExitSleep(); +#endif + +#if ((defined HAL_KEY) && (HAL_KEY == TRUE)) + /* handle peripherals */ + if(HalKeyExitSleep()) + { + break; + } +#endif + + } while(timeout != 0); + + /* power on the MAC; blocks until completion */ + MAC_PwrOnReq(); + + HAL_ENABLE_INTERRUPTS(); + + /* For CC2530, T2 interrupt won’t be generated when the current count is greater than + * the comparator. The interrupt is only generated when the current count is equal to + * the comparator. When the CC2530 is waking up from sleep, there is a small window + * that the count may be grater than the comparator, therefore, missing the interrupt. + * This workaround will call the T2 ISR when the current T2 count is greater than the + * comparator. The problem only occurs when POWER_SAVING is turned on, i.e. the 32KHz + * drives the chip in sleep and SYNC start is used. + */ + macMcuTimer2OverflowWorkaround(); + } + else + { + /* An interrupt may have changed the sleep decision. Do not sleep at all. Turn on + * the interrupt, exit normally, and the next sleep will be allowed. + */ + HAL_ENABLE_INTERRUPTS(); + } + } +} + +/************************************************************************************************** + * @fn halSleepSetTimer + * + * @brief This function sets the CC2530 sleep timer compare value. First it reads and + * stores the value of the sleep timer; this value is used later to update OSAL + * timers. Then the timeout value is converted from 320 usec units to 32 kHz + * period units and the compare value is set to the timeout. + * + * input parameters + * + * @param timeout - Timeout value in 320 usec units. The sleep timer compare is set to + * this value. + * + * output parameters + * + * None. + * + * @return None. + ************************************************************************************************** + */ +void halSleepSetTimer(uint32 timeout) +{ + uint32 ticks; + + /* read the sleep timer; ST0 must be read first */ + ((uint8 *) &ticks)[UINT32_NDX0] = ST0; + ((uint8 *) &ticks)[UINT32_NDX1] = ST1; + ((uint8 *) &ticks)[UINT32_NDX2] = ST2; + ((uint8 *) &ticks)[UINT32_NDX3] = 0; + + /* Compute sleep timer compare value. The ratio of 32 kHz ticks to 320 usec ticks + * is 32768/3125 = 10.48576. This is nearly 671/64 = 10.484375. + */ + ticks += (timeout * 671) / 64; + + /* subtract the processing time spent in function halSleep() */ + ticks -= HAL_SLEEP_ADJ_TICKS; + + /* set sleep timer compare; ST0 must be written last */ + ST2 = ((uint8 *) &ticks)[UINT32_NDX2]; + ST1 = ((uint8 *) &ticks)[UINT32_NDX1]; + ST0 = ((uint8 *) &ticks)[UINT32_NDX0]; +} + +/************************************************************************************************** + * @fn TimerElapsed + * + * @brief Determine the number of OSAL timer ticks elapsed during sleep. + * Deprecated for CC2530 and CC2430 SoC. + * + * input parameters + * + * @param None. + * + * output parameters + * + * None. + * + * @return Number of timer ticks elapsed during sleep. + ************************************************************************************************** + */ +uint32 TimerElapsed( void ) +{ + /* Stubs */ + return (0); +} + +/************************************************************************************************** + * @fn halRestoreSleepLevel + * + * @brief Restore the deepest timer sleep level. + * + * input parameters + * + * @param None + * + * output parameters + * + * None. + * + * @return None. + ************************************************************************************************** + */ +void halRestoreSleepLevel( void ) +{ + /* Stubs */ +} + +/************************************************************************************************** + * @fn halSleepTimerIsr + * + * @brief Sleep timer ISR. + * + * input parameters + * + * None. + * + * output parameters + * + * None. + * + * @return None. + ************************************************************************************************** + */ +HAL_ISR_FUNCTION(halSleepTimerIsr, ST_VECTOR) +{ + HAL_ENTER_ISR(); + HAL_SLEEP_TIMER_CLEAR_INT(); + +#ifdef HAL_SLEEP_DEBUG_POWER_MODE + halSleepInt = TRUE; +#endif + + CLEAR_SLEEP_MODE(); + HAL_EXIT_ISR(); +} + +/************************************************************************************************** + * @fn halSleepWait + * + * @brief Perform a blocking wait for the specified number of microseconds. + * Use assumptions about number of clock cycles needed for the various instructions. + * This function assumes a 32 MHz clock. + * NB! This function is highly dependent on architecture and compiler! + * + * input parameters + * + * @param duration - Duration of wait in microseconds. + * + * output parameters + * + * None. + * + * @return None. + ************************************************************************************************** + */ +#pragma optimize=none +void halSleepWait(uint16 duration) +{ + duration >>= 1; + + while (duration-- > 0) + { + ASM_NOP; ASM_NOP; ASM_NOP; ASM_NOP; ASM_NOP; ASM_NOP; ASM_NOP; ASM_NOP; ASM_NOP; ASM_NOP; ASM_NOP; ASM_NOP; + ASM_NOP; ASM_NOP; ASM_NOP; ASM_NOP; ASM_NOP; ASM_NOP; ASM_NOP; ASM_NOP; ASM_NOP; ASM_NOP; ASM_NOP; ASM_NOP; + } +} diff --git a/include/hal_sleep.h b/include/hal_sleep.h new file mode 100644 index 0000000..837c42c --- /dev/null +++ b/include/hal_sleep.h @@ -0,0 +1,84 @@ +/************************************************************************************************** + Filename: hal_sleep.h + Revised: $Date: 2013-02-27 11:32:02 -0800 (Wed, 27 Feb 2013) $ + Revision: $Revision: 33315 $ + + Description: This file contains the interface to the power management service. + + + Copyright 2006-2012 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +#ifndef HAL_SLEEP_H +#define HAL_SLEEP_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +/********************************************************************* + * FUNCTIONS + */ + +/* + * Execute power management procedure + */ +extern void halSleep( uint32 osal_timer ); + +/* + * Used in mac_mcu + */ +extern void halSleepWait(uint16 duration); + +/* + * Used in hal_drivers, AN044 - DELAY EXTERNAL INTERRUPTS + */ +extern void halRestoreSleepLevel( void ); + +/* + * Used by the interrupt routines to exit from sleep. + */ +extern void halSleepExit(void); + +/* + * Set the max sleep loop time lesser than the T2 rollover period. + */ +extern void halSetMaxSleepLoopTime(uint32 rolloverTime); + +/********************************************************************* +*********************************************************************/ + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/hal_startup.c b/include/hal_startup.c new file mode 100644 index 0000000..fb24b6d --- /dev/null +++ b/include/hal_startup.c @@ -0,0 +1,103 @@ +/************************************************************************************************** + Filename: hal_startup.c + Revised: $Date: 2010-01-28 16:31:53 -0800 (Thu, 28 Jan 2010) $ + Revision: $Revision: 21613 $ + + Description: Contains code that needs to run before main() + + Copyright 2008 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +/************************************************************************************************** + * INCLUDES + *************************************************************************************************/ +#include "hal_board.h" +#include "hal_types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#pragma language=extended + +// +// Locate low_level_init in the CSTART module +// +#pragma location="CSTART" +// +// If the code model is banked, low_level_init must be declared +// __near_func elsa a ?BRET is performed +// +#if (__CODE_MODEL__ == 2) +__near_func __root char +#else +__root char +#endif +__low_level_init(void); + +/************************************************************************************************** + * @fn __low_level_init + * + * @brief The function __low_level_init is called by the start-up code before doing + * the normal initialization of data segments. If the return value is zero, + * initialization is not performed. + * + * @param None + * + * @return 0 - don't intialize data segments / 1 - do initialization + **************************************************************************************************/ +#if (__CODE_MODEL__ == 2) +__near_func __root char +#else +__root char +#endif +__low_level_init(void) +{ + /*==================================*/ + /* Initialize hardware. */ + /*==================================*/ + // Map flash bank #1 into XDATA for access to "ROM mapped as data". + MEMCTR = (MEMCTR & 0xF8) | 0x01; + + /*==================================*/ + /* Choose if segment initialization */ + /* should be done or not. */ + /* Return: 0 to omit seg_init */ + /* 1 to run seg_init */ + /*==================================*/ + return 1; +} + +#pragma language=default + +#ifdef __cplusplus +} +#endif diff --git a/include/hal_timer.c b/include/hal_timer.c new file mode 100644 index 0000000..cf81743 --- /dev/null +++ b/include/hal_timer.c @@ -0,0 +1,45 @@ +/************************************************************************************************** + Filename: hal_timer.c + Revised: $Date: 2010-05-28 15:26:34 -0700 (Fri, 28 May 2010) $ + Revision: $Revision: 22676 $ + + Description: This file contains the interface to the Timer Service. + + + Copyright 2006-2010 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +/********************************************************************* + NOTE: Z-Stack and TIMAC no longer use CC2530 Timer 1, Timer 3, and + Timer 4. The supporting timer driver module is removed and left + for the users to implement their own application timer + functions. +*********************************************************************/ \ No newline at end of file diff --git a/include/hal_timer.h b/include/hal_timer.h new file mode 100644 index 0000000..517b1e6 --- /dev/null +++ b/include/hal_timer.h @@ -0,0 +1,240 @@ +/************************************************************************************************** + Filename: hal_timer.h + Revised: $Date: 2007-07-06 10:42:24 -0700 (Fri, 06 Jul 2007) $ + Revision: $Revision: 13579 $ + + Description: This file contains the interface to the Timer Service. + + + Copyright 2005-2007 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +#ifndef HAL_TIMER_H +#define HAL_TIMER_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +/*************************************************************************************************** + * INCLUDES + ***************************************************************************************************/ +#include "hal_board.h" + +/*************************************************************************************************** + * MACROS + ***************************************************************************************************/ + + + +/*************************************************************************************************** + * CONSTANTS + ***************************************************************************************************/ +/* Timer ID definitions */ +#define HAL_TIMER_0 0x00 // 8bit timer +#define HAL_TIMER_1 0x01 // 16bit Mac timer +#define HAL_TIMER_2 0x02 // 8bit timer +#define HAL_TIMER_3 0x03 // 16bit timer +#define HAL_TIMER_MAX 4 // Max number of timer + +/* Operation Modes for timer */ +#define HAL_TIMER_MODE_NORMAL 0x01 // Normal Mode +#define HAL_TIMER_MODE_CTC 0x02 // Clear Timer On Compare +#define HAL_TIMER_MODE_MASK (HAL_TIMER_MODE_NORMAL | HAL_TIMER_MODE_CTC) + +/* Timer1 channels */ +#define HAL_T1_CH0 0 +#define HAL_T1_CH1 1 +#define HAL_T1_CH2 2 +#define HAL_T1_CH3 3 +#define HAL_T1_CH4 4 + +/* Channel definitions */ +#define HAL_TIMER_CHANNEL_SINGLE 0x01 // Single Channel - default +#define HAL_TIMER_CHANNEL_A 0x02 // Channel A +#define HAL_TIMER_CHANNEL_B 0x04 // Channel B +#define HAL_TIMER_CHANNEL_C 0x08 // Channel C +#define HAL_TIMER_CHANNEL_D 0x10 // Channel D +#define HAL_TIMER_CHANNEL_E 0x20 // Channel E +#define HAL_TIMER_CHANNEL_MASK (HAL_TIMER_CHANNEL_SINGLE | \ + HAL_TIMER_CHANNEL_A | \ + HAL_TIMER_CHANNEL_B | \ + HAL_TIMER_CHANNEL_C | \ + HAL_TIMER_CHANNEL_D | \ + HAL_TIMER_CHANNEL_E) +#define HAL_TIMER_CH_MAX 0x06 + +/* Timer 1 Channel 0 Channel compare mode definitions */ +#define HAL_TIMER1_CH0_CMP_MODE_SET_ON_COMP 0x00 // 000: Set output on compare +#define HAL_TIMER1_CH0_CMP_MODE_CLR_ON_COMP 0x01 // 001: Clear output on compare +#define HAL_TIMER1_CH0_CMP_MODE_TOG_ON_COMP 0x02 // 010: Toggle output on compare +#define HAL_TIMER1_CH0_CMP_MODE_SET_ON_COMP_CLR_ON_0 0x03 // 011: Set output on compare-up, clear on 0 +#define HAL_TIMER1_CH0_CMP_MODE_CLR_ON_COMP_SET_ON_0 0x04 // 100: Clear output on compare-up, set on 0 +#define HAL_TIMER1_CH0_CMP_MODE_INIT 0x07 // 111: Initialize output pin. CMP[2:0] is not changed + +/* Timer 1 Channel 1-5 channel compare mode definitions */ +#define HAL_TIMER1_CHn_CMP_MODE_SET_ON_COMP 0x00 // 000: Set output on compare +#define HAL_TIMER1_CHn_CMP_MODE_CLR_ON_COMP 0x01 // 001: Clear output on compare +#define HAL_TIMER1_CHn_CMP_MODE_TOG_ON_COMP 0x02 // 010: Toggle output on compare +#define HAL_TIMER1_CHn_CMP_MODE_SET_ON_COMP_CLR_ON_0 0x03 // 011: Set output on compare-up, clear on compare down in up-down mode. Otherwise set output on + // compare, clear on 0. +#define HAL_TIMER1_CHn_CMP_MODE_CLR_ON_COMP_SET_ON_0 0x04 // 100: Clear output on compare-up, set on compare down in up-down mode. Otherwise clear output + // on compare, set on 0 +#define HAL_TIMER1_CHn_CMP_MODE_CLR_ON_CH0_SET_ON_CHn 0x05 // 101: Clear when equal T1CC0, set when equal T1CC2 +#define HAL_TIMER1_CHn_CMP_MODE_SET_ON_CH0_CLR_ON_CHn 0x06 // 110: Set when equal T1CC0, clear when equal T1CC2 +#define HAL_TIMER1_CHn_CMP_MODE_INIT 0x07 // 111: Initialize output pin. CMP[2:0] is not changed. +#define HAL_TIMER1_CH_CMP_MODE_BITS 0x38 // bits 5:3 + +/* Timer 1 Capture mode */ +#define HAL_TIMER1_CH_CAP_MODE_NO 0x00 // 00: No capture +#define HAL_TIMER1_CH_CAP_MODE_RISING 0x01 // 01: Capture on rising edge +#define HAL_TIMER1_CH_CAP_MODE_FALLING 0x02 // 10: Capture on falling edge +#define HAL_TIMER1_CH_CAP_MODE_ALL 0x03 // 11: Capture on all edges +#define HAL_TIMER1_CH_CAP_BITS 0x03 // bits 1:0 + +/* Channel mode definitions */ +#define HAL_TIMER_CH_MODE_INPUT_CAPTURE 0x01 // Channel Mode Input-Capture +#define HAL_TIMER_CH_MODE_OUTPUT_COMPARE 0x02 // Channel Mode Output_Compare +#define HAL_TIMER_CH_MODE_OVERFLOW 0x04 // Channel Mode Overflow +#define HAL_TIMER_CH_MODE_MASK (HAL_TIMER_CH_MODE_INPUT_CAPTURE | \ + HAL_TIMER_CH_MODE_OUTPUT_COMPARE | \ + HAL_TIMER_CH_MODE_OVERFLOW) + +/* Error Code */ +#define HAL_TIMER_OK 0x00 +#define HAL_TIMER_NOT_OK 0x01 +#define HAL_TIMER_PARAMS_ERROR 0x02 +#define HAL_TIMER_NOT_CONFIGURED 0x03 +#define HAL_TIMER_INVALID_ID 0x04 +#define HAL_TIMER_INVALID_CH_MODE 0x05 +#define HAL_TIMER_INVALID_OP_MODE 0x06 + +/* Timer clock pre-scaler definitions for 16bit timer1 and timer3 */ +#define HAL_TIMER3_16_TC_STOP 0x00 // No clock, timer stopped +#define HAL_TIMER3_16_TC_DIV1 0x01 // No clock pre-scaling +#define HAL_TIMER3_16_TC_DIV8 0x02 // Clock pre-scaled by 8 +#define HAL_TIMER3_16_TC_DIV64 0x03 // Clock pre-scaled by 64 +#define HAL_TIMER3_16_TC_DIV256 0x04 // Clock pre-scaled by 256 +#define HAL_TIMER3_16_TC_DIV1024 0x05 // Clock pre-scaled by 1024 +#define HAL_TIMER3_16_TC_EXTFE 0x06 // External clock (T2), falling edge +#define HAL_TIMER3_16_TC_EXTRE 0x07 // External clock (T2), rising edge + +/* Timer clock pre-scaler definitions for 8bit timer0 and timer2 */ +#define HAL_TIMER0_8_TC_STOP 0x00 // No clock, timer stopped +#define HAL_TIMER0_8_TC_DIV1 0x01 // No clock pre-scaling +#define HAL_TIMER0_8_TC_DIV8 0x02 // Clock pre-scaled by 8 +#define HAL_TIMER0_8_TC_DIV32 0x03 // Clock pre-scaled by 32 +#define HAL_TIMER0_8_TC_DIV64 0x04 // Clock pre-scaled by 64 +#define HAL_TIMER0_8_TC_DIV128 0x05 // Clock pre-scaled by 128 +#define HAL_TIMER0_8_TC_DIV256 0x06 // Clock pre-scaled by 256 +#define HAL_TIMER0_8_TC_DIV1024 0x07 // Clock pre-scaled by 1024 + +/* Timer clock pre-scaler definitions for 8bit timer2 */ +#define HAL_TIMER2_8_TC_STOP 0x00 // No clock, timer stopped +#define HAL_TIMER2_8_TC_DIV1 0x01 // No clock pre-scaling +#define HAL_TIMER2_8_TC_DIV8 0x02 // Clock pre-scaled by 8 +#define HAL_TIMER2_8_TC_DIV64 0x03 // Clock pre-scaled by 32 +#define HAL_TIMER2_8_TC_DIV256 0x04 // Clock pre-scaled by 64 +#define HAL_TIMER2_8_TC_DIV1024 0x05 // Clock pre-scaled by 128 +#define HAL_TIMER2_8_TC_EXTFE 0x06 // External clock (T2), falling edge +#define HAL_TIMER2_8_TC_EXTRE 0x07 // External clock (T2), rising edge + + +/*************************************************************************************************** + * TYPEDEFS + ***************************************************************************************************/ +typedef void (*halTimerCBack_t) (uint8 timerId, uint8 channel, uint8 channelMode); + +/*************************************************************************************************** + * GLOBAL VARIABLES + ***************************************************************************************************/ + + +/*************************************************************************************************** + * FUNCTIONS - API + ***************************************************************************************************/ + +/* + * Initialize Timer Service + */ +extern void HalTimerInit ( void ); + +/* + * Configure channel in different modes + */ +extern uint8 HalTimerConfig ( uint8 timerId, + uint8 opMode, + uint8 channel, + uint8 channelMode, + bool intEnable, + halTimerCBack_t cback ); + +/* + * Start a Timer + */ +extern uint8 HalTimerStart ( uint8 timerId, uint32 timePerTick ); + +/* + * Stop a Timer + */ +extern uint8 HalTimerStop ( uint8 timerId ); + + +/* + * This is used for polling, provide the tick increment + */ +extern void HalTimerTick ( void ); + +/* + * Enable and disable particular timer + */ +extern uint8 HalTimerInterruptEnable (uint8 timerId, uint8 channelMode, bool enable); + +/* + * Configures timer 1 to control 4 PWM outputs + */ +void HalTimer1Init (halTimerCBack_t cBack); + + +/* + * Set dutycycle on timer 1 PWM output channel + */ +void halTimer1SetChannelDuty (uint8 channel, uint16 promill); + +/*************************************************************************************************** +***************************************************************************************************/ + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/hal_types.h b/include/hal_types.h new file mode 100644 index 0000000..5d41603 --- /dev/null +++ b/include/hal_types.h @@ -0,0 +1,117 @@ +/************************************************************************************************** + Filename: hal_types.h + Revised: $Date: 2012-08-27 10:59:53 -0700 (Mon, 27 Aug 2012) $ + Revision: $Revision: 31378 $ + + Description: Describe the purpose and contents of the file. + + + Copyright 2006-2007 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +#ifndef _HAL_TYPES_H +#define _HAL_TYPES_H + +/* Texas Instruments CC2530 */ + +/* ------------------------------------------------------------------------------------------------ + * Types + * ------------------------------------------------------------------------------------------------ + */ +typedef signed char int8; +typedef unsigned char uint8; + +typedef signed short int16; +typedef unsigned short uint16; + +typedef signed long int32; +typedef unsigned long uint32; + +typedef unsigned char bool; + +typedef uint8 halDataAlign_t; + + +/* ------------------------------------------------------------------------------------------------ + * Memory Attributes and Compiler Macros + * ------------------------------------------------------------------------------------------------ + */ + +/* ----------- IAR Compiler ----------- */ +#ifdef __IAR_SYSTEMS_ICC__ +#define CODE __code +#define XDATA __xdata +#define ASM_NOP asm("NOP") + +/* ----------- KEIL Compiler ----------- */ +#elif defined __KEIL__ +#define CODE code +#define XDATA xdata +#define ASM_NOP __nop() + +/* ----------- CCS Compiler ----------- */ +#elif defined __TI_COMPILER_VERSION +#define ASM_NOP asm(" NOP") + +/* ----------- GNU Compiler ----------- */ +#elif defined __GNUC__ +#define ASM_NOP __asm__ __volatile__ ("nop") + +/* ---------- MSVC compiler ---------- */ +#elif _MSC_VER +#define ASM_NOP __asm NOP + +/* ----------- Unrecognized Compiler ----------- */ +#else +#error "ERROR: Unknown compiler." +#endif + + +/* ------------------------------------------------------------------------------------------------ + * Standard Defines + * ------------------------------------------------------------------------------------------------ + */ +#ifndef TRUE +#define TRUE 1 +#endif + +#ifndef FALSE +#define FALSE 0 +#endif + +#ifndef NULL +#define NULL 0 +#endif + + +/************************************************************************************************** + */ +#endif diff --git a/include/hal_uart.c b/include/hal_uart.c new file mode 100644 index 0000000..31e0e2f --- /dev/null +++ b/include/hal_uart.c @@ -0,0 +1,311 @@ +/************************************************************************************************** + Filename: _hal_uart.c + Revised: $Date: 2009-06-12 09:16:43 -0700 (Fri, 12 Jun 2009) $ + Revision: $Revision: 20142 $ + + Description: This file contains the interface to the H/W UART driver. + + + Copyright 2006-2009 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +/********************************************************************* + * INCLUDES + */ + +#include "hal_board_cfg.h" +#include "hal_defs.h" +#include "hal_types.h" +#include "hal_uart.h" + +/********************************************************************* + * MACROS + */ + +/********************************************************************* + * TYPEDEFS + */ + +/********************************************************************* + * CONSTANTS + */ + +/********************************************************************* + * GLOBAL VARIABLES + */ + +/********************************************************************* + * GLOBAL FUNCTIONS + */ + +/********************************************************************* + * LOCAL VARIABLES + */ + +/********************************************************************* + * LOCAL FUNCTIONS + */ + +#if HAL_UART_DMA +#include "_hal_uart_dma.c" +#endif +#if HAL_UART_ISR +#include "_hal_uart_isr.c" +#endif +#if HAL_UART_USB +#include "_hal_uart_usb.c" +#endif + +/****************************************************************************** + * @fn HalUARTInit + * + * @brief Initialize the UART + * + * @param none + * + * @return none + *****************************************************************************/ +void HalUARTInit(void) +{ +#if HAL_UART_DMA + HalUARTInitDMA(); +#endif +#if HAL_UART_ISR + HalUARTInitISR(); +#endif +#if HAL_UART_USB + HalUARTInitUSB(); +#endif +} + +/****************************************************************************** + * @fn HalUARTOpen + * + * @brief Open a port according tp the configuration specified by parameter. + * + * @param port - UART port + * config - contains configuration information + * + * @return Status of the function call + *****************************************************************************/ +uint8 HalUARTOpen(uint8 port, halUARTCfg_t *config) +{ + (void)port; + (void)config; + +#if (HAL_UART_DMA == 1) + if (port == HAL_UART_PORT_0) HalUARTOpenDMA(config); +#endif +#if (HAL_UART_DMA == 2) + if (port == HAL_UART_PORT_1) HalUARTOpenDMA(config); +#endif +#if (HAL_UART_ISR == 1) + if (port == HAL_UART_PORT_0) HalUARTOpenISR(config); +#endif +#if (HAL_UART_ISR == 2) + if (port == HAL_UART_PORT_1) HalUARTOpenISR(config); +#endif +#if (HAL_UART_USB) + HalUARTOpenUSB(config); +#endif + + return HAL_UART_SUCCESS; +} + +/***************************************************************************** + * @fn HalUARTRead + * + * @brief Read a buffer from the UART + * + * @param port - USART module designation + * buf - valid data buffer at least 'len' bytes in size + * len - max length number of bytes to copy to 'buf' + * + * @return length of buffer that was read + *****************************************************************************/ +uint16 HalUARTRead(uint8 port, uint8 *buf, uint16 len) +{ + (void)port; + (void)buf; + (void)len; + +#if (HAL_UART_DMA == 1) + if (port == HAL_UART_PORT_0) return HalUARTReadDMA(buf, len); +#endif +#if (HAL_UART_DMA == 2) + if (port == HAL_UART_PORT_1) return HalUARTReadDMA(buf, len); +#endif +#if (HAL_UART_ISR == 1) + if (port == HAL_UART_PORT_0) return HalUARTReadISR(buf, len); +#endif +#if (HAL_UART_ISR == 2) + if (port == HAL_UART_PORT_1) return HalUARTReadISR(buf, len); +#endif + +#if HAL_UART_USB + return HalUARTRx(buf, len); +#else + return 0; +#endif +} + +/****************************************************************************** + * @fn HalUARTWrite + * + * @brief Write a buffer to the UART. + * + * @param port - UART port + * buf - pointer to the buffer that will be written, not freed + * len - length of + * + * @return length of the buffer that was sent + *****************************************************************************/ +uint16 HalUARTWrite(uint8 port, uint8 *buf, uint16 len) +{ + (void)port; + (void)buf; + (void)len; + +#if (HAL_UART_DMA == 1) + if (port == HAL_UART_PORT_0) return HalUARTWriteDMA(buf, len); +#endif +#if (HAL_UART_DMA == 2) + if (port == HAL_UART_PORT_1) return HalUARTWriteDMA(buf, len); +#endif +#if (HAL_UART_ISR == 1) + if (port == HAL_UART_PORT_0) return HalUARTWriteISR(buf, len); +#endif +#if (HAL_UART_ISR == 2) + if (port == HAL_UART_PORT_1) return HalUARTWriteISR(buf, len); +#endif + +#if HAL_UART_USB + HalUARTTx(buf, len); + return len; +#else + return 0; +#endif +} + +/****************************************************************************** + * @fn HalUARTSuspend + * + * @brief Suspend UART hardware before entering PM mode 1, 2 or 3. + * + * @param None + * + * @return None + *****************************************************************************/ +void HalUARTSuspend( void ) +{ +#if HAL_UART_DMA + HalUARTSuspendDMA(); +#endif +#if HAL_UART_ISR + HalUARTSuspendISR(); +#endif +} + +/****************************************************************************** + * @fn HalUARTResume + * + * @brief Resume UART hardware after exiting PM mode 1, 2 or 3. + * + * @param None + * + * @return None + *****************************************************************************/ +void HalUARTResume( void ) +{ +#if HAL_UART_DMA + HalUARTResumeDMA(); +#endif +#if HAL_UART_ISR + HalUARTResumeISR(); +#endif +} + +/*************************************************************************************************** + * @fn HalUARTPoll + * + * @brief Poll the UART. + * + * @param none + * + * @return none + *****************************************************************************/ +void HalUARTPoll(void) +{ +#if HAL_UART_DMA + HalUARTPollDMA(); +#endif +#if HAL_UART_ISR + HalUARTPollISR(); +#endif +#if HAL_UART_USB + HalUARTPollUSB(); +#endif +} + +/************************************************************************************************** + * @fn Hal_UART_RxBufLen() + * + * @brief Calculate Rx Buffer length - the number of bytes in the buffer. + * + * @param port - UART port + * + * @return length of current Rx Buffer + **************************************************************************************************/ +uint16 Hal_UART_RxBufLen( uint8 port ) +{ + (void)port; + +#if (HAL_UART_DMA == 1) + if (port == HAL_UART_PORT_0) return HalUARTRxAvailDMA(); +#endif +#if (HAL_UART_DMA == 2) + if (port == HAL_UART_PORT_1) return HalUARTRxAvailDMA(); +#endif +#if (HAL_UART_ISR == 1) + if (port == HAL_UART_PORT_0) return HalUARTRxAvailISR(); +#endif +#if (HAL_UART_ISR == 2) + if (port == HAL_UART_PORT_1) return HalUARTRxAvailISR(); +#endif +#if HAL_UART_USB + return HalUARTRxAvailUSB(); +#else + return 0; +#endif +} + +/****************************************************************************** +******************************************************************************/ diff --git a/include/hal_uart.h b/include/hal_uart.h new file mode 100644 index 0000000..df5184c --- /dev/null +++ b/include/hal_uart.h @@ -0,0 +1,235 @@ +/************************************************************************************************** + Filename: hal_uart.h + Revised: $Date: 2012-10-17 09:45:25 -0700 (Wed, 17 Oct 2012) $ + Revision: $Revision: 31844 $ + + Description: This file contains the interface to the UART Service. + + + Copyright 2005-2012 Texas Instruments Incorporated. All rights reserved. + + IMPORTANT: Your use of this Software is limited to those specific rights + granted under the terms of a software license agreement between the user + who downloaded the software, his/her employer (which must be your employer) + and Texas Instruments Incorporated (the "License"). You may not use this + Software unless you agree to abide by the terms of the License. The License + limits your use, and you acknowledge, that the Software may not be modified, + copied or distributed unless embedded on a Texas Instruments microcontroller + or used solely and exclusively in conjunction with a Texas Instruments radio + frequency transceiver, which is integrated into your product. Other than for + the foregoing purpose, you may not use, reproduce, copy, prepare derivative + works of, modify, distribute, perform, display or sell this Software and/or + its documentation for any purpose. + + YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE + PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, + INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, + NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL + TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, + NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER + LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES + INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE + OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT + OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES + (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. + + Should you have any questions regarding your right to use this Software, + contact Texas Instruments Incorporated at www.TI.com. +**************************************************************************************************/ + +#ifndef HAL_UART_H +#define HAL_UART_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +/*************************************************************************************************** + * INCLUDES + ***************************************************************************************************/ +#include "hal_board.h" + +/*************************************************************************************************** + * MACROS + ***************************************************************************************************/ + +/*************************************************************************************************** + * CONSTANTS + ***************************************************************************************************/ + + +/* UART Ports */ + +/* + Serial Port Baudrate Settings + Have to match with baudrate table +*/ +#define HAL_UART_BR_9600 0x00 +#define HAL_UART_BR_19200 0x01 +#define HAL_UART_BR_38400 0x02 +#define HAL_UART_BR_57600 0x03 +#define HAL_UART_BR_115200 0x04 + +/* Frame Format constant */ + +/* Stop Bits */ +#define HAL_UART_ONE_STOP_BIT 0x00 +#define HAL_UART_TWO_STOP_BITS 0x01 + +/* Parity settings */ +#define HAL_UART_NO_PARITY 0x00 +#define HAL_UART_EVEN_PARITY 0x01 +#define HAL_UART_ODD_PARITY 0x02 + +/* Character Size */ +#define HAL_UART_8_BITS_PER_CHAR 0x00 +#define HAL_UART_9_BITS_PER_CHAR 0x01 + +/* Flow control */ +#define HAL_UART_FLOW_OFF 0x00 +#define HAL_UART_FLOW_ON 0x01 + +/* Ports */ +#define HAL_UART_PORT_0 0x00 +#define HAL_UART_PORT_1 0x01 +#define HAL_UART_PORT_MAX 0x02 + +/* UART Status */ +#define HAL_UART_SUCCESS 0x00 +#define HAL_UART_UNCONFIGURED 0x01 +#define HAL_UART_NOT_SUPPORTED 0x02 +#define HAL_UART_MEM_FAIL 0x03 +#define HAL_UART_BAUDRATE_ERROR 0x04 + +/* UART Events */ +#define HAL_UART_RX_FULL 0x01 +#define HAL_UART_RX_ABOUT_FULL 0x02 +#define HAL_UART_RX_TIMEOUT 0x04 +#define HAL_UART_TX_FULL 0x08 +#define HAL_UART_TX_EMPTY 0x10 + +/*************************************************************************************************** + * TYPEDEFS + ***************************************************************************************************/ + +typedef void (*halUARTCBack_t) (uint8 port, uint8 event); + +typedef struct +{ + // The head or tail is updated by the Tx or Rx ISR respectively, when not polled. + volatile uint16 bufferHead; + volatile uint16 bufferTail; + uint16 maxBufSize; + uint8 *pBuffer; +} halUARTBufControl_t; + +typedef struct +{ + bool configured; + uint8 baudRate; + bool flowControl; + uint16 flowControlThreshold; + uint8 idleTimeout; + halUARTBufControl_t rx; + halUARTBufControl_t tx; + bool intEnable; + uint32 rxChRvdTime; + halUARTCBack_t callBackFunc; +}halUARTCfg_t; + +typedef union +{ + bool paramCTS; + bool paramRTS; + bool paramDSR; + bool paramDTR; + bool paramCD; + bool paramRI; + uint16 baudRate; + bool flowControl; + bool flushControl; +}halUARTIoctl_t; + + +/*************************************************************************************************** + * GLOBAL VARIABLES + ***************************************************************************************************/ + + +/*************************************************************************************************** + * FUNCTIONS - API + ***************************************************************************************************/ +/* + * Initialize UART at the startup + */ +extern void HalUARTInit ( void ); + +/* + * Open a port based on the configuration + */ +extern uint8 HalUARTOpen ( uint8 port, halUARTCfg_t *config ); + +/* + * Close a port + */ +extern void HalUARTClose ( uint8 port ); + +/* + * Read a buffer from the UART + */ +extern uint16 HalUARTRead ( uint8 port, uint8 *pBuffer, uint16 length ); + +/* + * Write a buff to the uart * + */ +extern uint16 HalUARTWrite ( uint8 port, uint8 *pBuffer, uint16 length ); + +/* + * Write a buffer to the UART + */ +extern uint8 HalUARTIoctl ( uint8 port, uint8 cmd, halUARTIoctl_t *pIoctl ); + +/* + * This to support polling + */ +extern void HalUARTPoll( void ); + +/* + * Return the number of bytes in the Rx buffer + */ +extern uint16 Hal_UART_RxBufLen ( uint8 port ); + +/* + * Return the number of bytes in the Tx buffer + */ +extern uint16 Hal_UART_TxBufLen ( uint8 port ); + +/* + * This enable/disable flow control + */ +extern void Hal_UART_FlowControlSet ( uint8 port, bool status ); + +/* + * Initialize hardware for UART + */ +extern uint8 HalUART_HW_Init(uint8 port); + +/* + * Abort UART when entering sleep mode + */ +extern void HalUARTSuspend(void); + +/* + * Resume UART after wakeup from sleep + */ +extern void HalUARTResume(void); + +/*************************************************************************************************** +***************************************************************************************************/ + +#ifdef __cplusplus +} +#endif + +#endif