基于 MSP430 CC1101的WOR的测试

CC1101配合MSP430 测试正常。

/******************************************************************************
  Filename:        cc1101_wor_demo.c
  
  Description: 
  
  Notes: 
  
******************************************************************************/


/*****************************************************************************
* INCLUDES
*/
#include  <msp430.h>
#include "hal_board.h"
#include "hal_mcu.h"
#include "hal_led_trxeb.h"
#include "hal_button_trxeb.h"
#include "lcd_dogm128_6.h"  
#include "hal_spi_rf_trxeb.h"
#include "hal_int_rf_trxeb.h"
#include "cc1101_spi.h"
#include "hal_timer_32k.h"

/******************************************************************************
 * CONSTANTS
 */ 

/******************************************************************************
* DEFINES
*/
#define ISR_ACTION_REQUIRED 1
#define ISR_IDLE            0
#define MAX_PACKET_LENGTH   20

/******************************************************************************
* LOCAL VARIABLES
*/
static uint8  packetSemaphore;
static uint32 packetCounter = 0;
static uint32 packetSent = 0;
static uint8 rxBuffer[MAX_PACKET_LENGTH + 3];
static uint8 txBuffer[] = {19, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};


static const registerSetting_t preferredSettings[] = {
  {CC1101_FSCTRL1,  0x0C},
  {CC1101_FSCTRL0,  0x00},
  {CC1101_FREQ2,    0x23},
  {CC1101_FREQ1,    0x31},
  {CC1101_FREQ0,    0x3B},
  {CC1101_MDMCFG4,  0x2D},
  {CC1101_MDMCFG3,  0x3B},
  {CC1101_MDMCFG2,  0x13},
  {CC1101_MDMCFG1,  0x22},
  {CC1101_MDMCFG0,  0xF8},
  {CC1101_CHANNR,   0x00},
  {CC1101_DEVIATN,  0x62},
  {CC1101_FREND1,   0xB6},
  {CC1101_FREND0,   0x10},
  {CC1101_MCSM0,    0x18},
  {CC1101_FOCCFG,   0x1D},
  {CC1101_BSCFG,    0x1C},
  {CC1101_AGCCTRL2, 0xC7},
  {CC1101_AGCCTRL1, 0x00},
  {CC1101_AGCCTRL0, 0xB0},
  {CC1101_FSCAL3,   0xEA},
  {CC1101_FSCAL2,   0x2A},
  {CC1101_FSCAL1,   0x00},
  {CC1101_FSCAL0,   0x1F},
  {CC1101_FSTEST,   0x59},
  {CC1101_TEST2,    0x88},
  {CC1101_TEST1,    0x31},
  {CC1101_TEST0,    0x09},
  {CC1101_FIFOTHR,  0x07},
  {CC1101_IOCFG2,   0x5C}, // RX
  {CC1101_IOCFG1,   0x3F}, // XOSC
  {CC1101_IOCFG0,   0x06},
  {CC1101_PKTCTRL1, 0x04},
  {CC1101_PKTCTRL0, 0x05},
  {CC1101_ADDR,	    0x00},
  {CC1101_PKTLEN,   MAX_PACKET_LENGTH},
  {CC1101_PA_TABLE0, 0x84},
};

/******************************************************************************
* STATIC FUNCTIONS
*/
static void initMCU(void);
static void registerConfig(void);
static void radioRXISR(void);
static void updateLcd(void);
/******************************************************************************
 * @fn          main
 *
 * @brief       Runs the main routine
 *                
 * @param       none
 *
 * @return      none
 */
void main(void) {
  uint8 writeByte;
  uint8 rxBytes;
  
  // Initialize MCU and peripherals
  initMCU();
    
  // Write radio registers
  registerConfig();
  
  // Disable interrupt from GPIO_0
  trxDisableInt(GPIO_0);
    
  // Connect ISR function to GPIO0, interrupt on falling edge
  trxIsrConnect(GPIO_0, FALLING_EDGE, &radioRXISR);
    
  trxClearIntFlag(GPIO_0);
  
  // Enable interrupt from GPIO_0
  trxEnableInt(GPIO_0);
  
  // TX Code (include this code for the transmitter)
  /*while (TRUE) {
    
    //while (!halButtonsPushed());
    
    writeByte = 0x5B; cc1101SpiWriteReg(CC1101_IOCFG2,    &writeByte, 1);
    
    trxSpiCmdStrobe(CC1101_STX);
    
    for (uint16 i = 0; i < 65000; i++);
    for (uint16 i = 0; i < 16500; i++);
    
    cc1101SpiWriteTxFifo(txBuffer,sizeof(txBuffer));
    trxSpiCmdStrobe(CC1101_STX);
    
    while (packetSemaphore == ISR_IDLE);
    packetSemaphore = ISR_IDLE;
    
    lcdBufferClear(0);
    lcdBufferPrintString(0, "WOR Demo", 0, 0);
    lcdBufferSetHLine(0, 0, LCD_COLS - 1, 7); 
    lcdBufferPrintString(0, "Received:", 0, 3);
    lcdBufferPrintInt(0, ++packetSent, 65, 3);
    lcdBufferPrintString(0,"TX", 0, 7);
    lcdBufferSetHLine(0, 0, LCD_COLS - 1, 55);
    lcdBufferInvertPage(0, 0, LCD_COLS, 7);
    lcdSendBuffer(0);
    
    halMcuWaitMs(1);
  }*/
  
  writeByte = 0x64; cc1101SpiWriteReg(CC1101_PKTCTRL1,  &writeByte, 1); // PQT = 3
  writeByte = 0x0C; cc1101SpiWriteReg(CC1101_MCSM2,     &writeByte, 1); // RX_TIME_QUAL = 1, RX_TIME = 4 ()
  writeByte = 0x30; cc1101SpiWriteReg(CC1101_MCSM1,     &writeByte, 1);
  writeByte = 0x18; cc1101SpiWriteReg(CC1101_MCSM0,     &writeByte, 1);
  writeByte = 0x38; cc1101SpiWriteReg(CC1101_WORCTRL,   &writeByte, 1); // EVENT1 = 3, WOR_RES = 0
  writeByte = 0x06; cc1101SpiWriteReg(CC1101_WOREVT1,   &writeByte, 1); // tEVENT0 = 50 ms, RX timeout = 390 us
  writeByte = 0xC5; cc1101SpiWriteReg(CC1101_WOREVT0,   &writeByte, 1);
  writeByte = 0x06; cc1101SpiWriteReg(CC1101_IOCFG0,    &writeByte, 1);
  
 
  // Infinite loop
  while (TRUE) {
    
    //--------------------------------------------------------------------------
    // Enter WOR mode
      
    
      
    trxSpiCmdStrobe(CC1101_SWORRST); 
    trxSpiCmdStrobe(CC1101_SWOR);
    
    while (packetSemaphore == ISR_IDLE);
    packetSemaphore = ISR_IDLE;
    
    cc1101SpiReadReg(CC1101_RXBYTES, &rxBytes,1);
        
    if (rxBytes) {
      cc1101SpiReadRxFifo(rxBuffer,(rxBytes));
        
      // check CRC ok (CRC_OK: bit7 in second status byte)
      if(rxBuffer[rxBytes-1] | 0x80) {
        updateLcd();
      }
    }
  }
}


/*******************************************************************************
* @fn          radioRXISR
*
* @brief       ISR for packet handling in RX. Sets packet semaphore, puts radio
*              in idle state and clears isr flag.
*
* @param       none
*
* @return      none
*/
static void radioRXISR(void) {

  // Set packet semaphore
  packetSemaphore = ISR_ACTION_REQUIRED;

  // Clear isr flag
  trxClearIntFlag(GPIO_0);
}


/******************************************************************************
 * @fn          initMCU
 *
 * @brief       Initialize MCU and board peripherals
 *                
 * @param       input, output parameters
 *
 * @return      describe return value, if any
 */
static void initMCU(void)
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;
  
  // Initialize all GPIO configurations
  halBoardInit();
  
  // Care must be taken when handling power modes
  // - Peripheral units can request clocks and have them granted even if
  //   the system is in a power mode. Peripheral clock request is enabled
  //   as default.
  // - Simple Link only needs ACLK to be enabled to timers
  //   during power mode operation
  halMcuDisablePeripheralClockRequest((MCLKREQEN+SMCLKREQEN));

  // SPI flash uses same SPI interface as LCD -- we'll disable the SPI flash
  P8SEL &= BIT6; // ioflash_csn = gp. 
  P8DIR |= BIT6; // tpflash_csn = ouut.
  P8OUT |= BIT6; // flash_csn = 1.     
  
  // Init leds 
  halLedInit();

  // Init Buttons
  halButtonsInit();
  halButtonsInterruptEnable();

  // Init LCD
  lcdInit();

  // Instantiate tranceiver RF spi interface to SCLK ~ 4 MHz */
  //input clockDivider - SMCLK/clockDivider gives SCLK frequency
  trxRfSpiInterfaceInit(0x02);

  // Enable global interrupt
  _BIS_SR(GIE);
  
}
/*******************************************************************************
* @fn          registerConfig
*
* @brief       Write register settings as given by SmartRF Studio
*
* @param       none
*
* @return      none
*/
static void registerConfig(void) {
  
  uint8 writeByte;
  
  // Reset radio
  trxSpiCmdStrobe(CC1101_SRES);
  
  // Write registers to radio
  for(uint16 i = 0; i < (sizeof  preferredSettings/sizeof(registerSetting_t)); i++) {
    writeByte =  preferredSettings[i].data;
    cc1101SpiWriteReg( preferredSettings[i].addr, &writeByte, 1);
  }
}
/******************************************************************************
 * @fn          updateLcd
 *
 * @brief       updates LCD buffer and sends bufer to LCD.
 *                
 * @param       none
 *
 * @return      none
 */
static void updateLcd(void) {

  lcdBufferClear(0);
  lcdBufferPrintString(0, "WOR Demo", 0, 0);
  lcdBufferSetHLine(0, 0, LCD_COLS - 1, 7); 
  lcdBufferPrintString(0, "Received:", 0, 3);
  lcdBufferPrintInt(0, ++packetCounter, 65, 3);
  lcdBufferPrintString(0,"RX", 0, 7);
  lcdBufferSetHLine(0, 0, LCD_COLS - 1, 55);
  lcdBufferInvertPage(0, 0, LCD_COLS, 7);
  lcdSendBuffer(0);
}
/***********************************************************************************
  Copyright 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 揂S 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.
***********************************************************************************/



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值