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

📄 wm430_transmitter.c

📁 MSP430单片机的数据收发程序
💻 C
📖 第 1 页 / 共 2 页
字号:
//*******************************************************************************
//   TI/Agilent/Cypress Wireless Mouse Reference Design
//
//   File: "wm430_transmitter.c"
//
//   Description: Implements the RF transmitter protocol
//
//  Author: Randy Wu
//  Company: Texas Instruments, Inc.
//  Date: May 2006
//  IDE: Built with IAR Embedded Workbench Version 3.41A
//
// THIS PROGRAM IS PROVIDED "AS IS". TI MAKES NO WARRANTIES OR
// REPRESENTATIONS, EITHER EXPRESS, IMPLIED OR STATUTORY,
// INCLUDING ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR
// COMPLETENESS OF RESPONSES, RESULTS AND LACK OF NEGLIGENCE.
// TI DISCLAIMS ANY WARRANTY OF TITLE, QUIET ENJOYMENT, QUIET
// POSSESSION, AND NON-INFRINGEMENT OF ANY THIRD PARTY
// INTELLECTUAL PROPERTY RIGHTS WITH REGARD TO THE PROGRAM OR
// YOUR USE OF THE PROGRAM.
//
// IN NO EVENT SHALL TI BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
// CONSEQUENTIAL OR INDIRECT DAMAGES, HOWEVER CAUSED, ON ANY
// THEORY OF LIABILITY AND WHETHER OR NOT TI HAS BEEN ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGES, ARISING IN ANY WAY OUT
// OF THIS AGREEMENT, THE PROGRAM, OR YOUR USE OF THE PROGRAM.
// EXCLUDED DAMAGES INCLUDE, BUT ARE NOT LIMITED TO, COST OF
// REMOVAL OR REINSTALLATION, COMPUTER TIME, LABOR COSTS, LOSS
// OF GOODWILL, LOSS OF PROFITS, LOSS OF SAVINGS, OR LOSS OF
// USE OR INTERRUPTION OF BUSINESS. IN NO EVENT WILL TI'S
// AGGREGATE LIABILITY UNDER THIS AGREEMENT OR ARISING OUT OF
// YOUR USE OF THE PROGRAM EXCEED FIVE HUNDRED DOLLARS
// (U.S.$500).
//
// Unless otherwise stated, the Program written and copyrighted
// by Texas Instruments is distributed as "freeware".  You may,
// only under TI's copyright in the Program, use and modify the
// Program without any charge or restriction.  You may
// distribute to third parties, provided that you transfer a
// copy of this license to the third party and the third party
// agrees to these terms by its first use of the Program. You
// must reproduce the copyright notice and any other legend of
// ownership on each copy or partial copy, of the Program.
//
// You acknowledge and agree that the Program contains
// copyrighted material, trade secrets and other TI proprietary
// information and is protected by copyright laws,
// international copyright treaties, and trade secret laws, as
// well as other intellectual property laws.  To protect TI's
// rights in the Program, you agree not to decompile, reverse
// engineer, disassemble or otherwise translate any object code
// versions of the Program to a human-readable form.  You agree
// that in no event will you alter, remove or destroy any
// copyright notice included in the Program.  TI reserves all
// rights not specifically granted under this license. Except
// as specifically provided herein, nothing in this agreement
// shall be construed as conferring by implication, estoppel,
// or otherwise, upon you, any license or other right under any
// TI patents, copyrights or trade secrets.
//
// You may not use the Program in non-TI devices.
//*******************************************************************************
#include "wm430_transmitter.h"
#include "CRC-8.h"
#include "wm430_led.h"
#include "MSP430_TI_TRF7950.h"
#include "MSP430_AGILENT_ADNS-3040.h"

unsigned int  WM430_TX_pendingMsgs = 0;
#ifdef _FSKDATAPLUSENCODING
unsigned char WM430_TX_rfState = 1;         // State of TX Data for next pulse
unsigned int  WM430_TX_rfWidth =            // Width of next TX Data pulse
              TIMERA0_SMCLK_BITPERIOD_10;
unsigned char WM430_TX_keyOnPeriodBitNum = 0;
unsigned char WM430_TX_startPatternBitNum = 0;
unsigned char WM430_TX_syncPatternBitNum = 0;
unsigned char WM430_TX_stopPatternBitNum = 0;
signed int    WM430_TX_currentFreeMesgPtr = -1;
unsigned char debug_dropped_messages = 0;   // Keep track of how many times we couldn't xmit a report
WM430_TX_MesgBuffer WM430_TX_mesgQueue[WM430_TX_QUEUEMSGMAX];
static unsigned char TA0V_shiftCounter = 0;
static unsigned char TA0V_bytePtr = 0;
static unsigned char TA0V_currentTxPtr = 0;
static unsigned char TA0V_currentTxBit = WM430_TX_CURRENTTXBITDEFAULT;
static unsigned char TA0V_nextTxBit;
#endif /* _FSKDATAPLUSENCODING */


void WM430_TX_dataIdle(void)
{
#ifdef _FSKDATAPLUSENCODING
  P2OUT &= ~TX_CTRL2;                       // RF Data in IDLE state = LOW
#endif /* _FSKDATAPLUSENCODING */
}

void WM430_TX_init(void)
{
  WM430_TX_dataIdle();                      // RF Data in IDLE state
#ifndef _FSKDATAPLUSENCODING
  TACTL |= MC1;                             // Start continuous timer mode
#endif
}

void WM430_TX_disableRF(void)
{
#ifdef _TI_TRF7950_TX
  TRF7950_disableTX();                      // Send the command to the TRF7950
#endif /* _TI_TRF7950_TX */
}

void WM430_TX_enableRF(void)
{
#ifdef _TI_TRF7950_TX
  TRF7950_enableTX();                       // Send the command to the TRF7950
#endif /* _TI_TRF7950_TX */
}

void WM430_TX_sendBindingData(void)
{
  WM430_LED_setLowBatt();                   // Show start of binding sequence with LED
  for (unsigned char temp = 0; temp < WM430_TX_NUMBINDPACKETS; temp++) {
#ifdef _FSKDATAPLUSENCODING
    WM430_TX_updateFreeBufPtr();            // Get pointer to next free buffer space
    WM430_TX_queueDataPacketBIND(&WM430_TX_mesgQueue[WM430_TX_currentFreeMesgPtr].field, WM430_SYS_currID, WM430_TX_FREQCODEDEFAULT);
    WM430_TX_signalMessage();               // Signal that message is pending
#endif /* _FSKDATAPLUSENCODING */
    WM430_SYS_delaymsec(5);                 // Delay between each packet in the bind packet burst sequence
  }
  WM430_LED_clearLowBatt();                 // Show end of binding sequence with LED
}

void WM430_TX_sendButtonData(int numPackets)
{
  WM430_TX_flushBuffer();                   // Reset buffer so that only button packet(s) will be queued up

  for (int counter = 0; counter < numPackets; counter++) {
#ifdef _FSKDATAPLUSENCODING
    WM430_TX_updateFreeBufPtr();            // Get pointer location of next free buffer
    WM430_TX_queueDataPacketBUTTONZDISP(&WM430_TX_mesgQueue[WM430_TX_currentFreeMesgPtr].field,
                                       WM430_BUTTONS_data, 0);
    WM430_TX_signalMessage();               // Signal that a message is ready and pending
#else /* _FSKDATAPLUSENCODING */
    WM430_TX_sendDataPacket(WM430_BUTTONS_data, 0, 0, 0);
#endif /* _FSKDATAPLUSENCODING */
                                            // Delay to prevent button packets from being overwritten
    WM430_SYS_delaymsec(WM430_TX_SHIFTPACKETMSEC);
  }
}

#ifdef _FSKDATAPLUSENCODING

void WM430_TX_queueDataPacketBIND(WM430_TX_MesgFields *fields, unsigned char newID, unsigned char freqCode)
{
  fields->ID = WM430_TX_BINDINGID;          // Set to the old ID
  fields->header = WM430_TX_IDCHANGE;       // Set header ID
  fields->data2 = freqCode;                 // Set frequency code
  fields->data1 = newID;                    // Set new ID value
  fields->CRC = CRC8_process(&fields->ID, CRC8_MESGLENBYTES);
}

void WM430_TX_queueDataPacketYXDISP(WM430_TX_MesgFields *fields, signed char YDISP, signed char XDISP)
{
  fields->ID = WM430_SYS_currID;            // Set current ID
  fields->header = WM430_TX_XYDISP;         // Set header ID
  fields->data2 = XDISP;                    // Transfer X displacement value
  fields->data1 = YDISP;                    // Transfer Y displacement value
  fields->CRC = CRC8_process(&fields->ID, CRC8_MESGLENBYTES);
}

void WM430_TX_queueDataPacketBUTTONZDISP(WM430_TX_MesgFields *fields, unsigned char BUTTON, signed char ZDISP)
{
  fields->ID = WM430_SYS_currID;            // Set current ID
  fields->header = WM430_TX_BUTTONZDISP;    // Set header ID
  fields->data2 = BUTTON;                   // Transfer button data bits
  fields->data1 = ZDISP;                    // Transfer Z displacement value
  fields->CRC = CRC8_process(&fields->ID, CRC8_MESGLENBYTES);
}

void WM430_TX_dataHI(void)
{
  WM430_TX_rfState = 1;                     // Set bit state to 0
}

void WM430_TX_dataLO(void)
{
  WM430_TX_rfState = 0;                     // Reset bit state to 0
}

void WM430_TX_dataToggle(void)
{
  WM430_TX_rfState ^= 1;                    // Exclusive OR the current bit state
}

void WM430_TX_enableInt(void)
{
  TACCTL0 |= CCIE;
}

void WM430_TX_disableInt(void)
{
  TACCTL0 &= ~CCIE;
}

void WM430_TX_flushBuffer(void)
{
  WM430_TX_pendingMsgs = 0;                 // Signal no pending messages
  WM430_TX_currentFreeMesgPtr = 0;          // Reset free message pointer (wrap-around)
  WM430_TX_startPatternBitNum = WM430_TX_STARTPATTERNPREAMBLENUMBITS;
  WM430_TX_syncPatternBitNum = WM430_TX_SYNCPATTERNNUMBITS;
  WM430_TX_stopPatternBitNum = WM430_TX_STOPPATTERNNUMBITS;
  TA0V_shiftCounter = 0;                    // Reset all Timer_A ISR state variables
  TA0V_bytePtr = 0;
  TA0V_currentTxPtr = 0;
  TA0V_currentTxBit = WM430_TX_CURRENTTXBITDEFAULT;
}

void WM430_TX_updateFreeBufPtr(void)
{
                                            // Wait for buffer space to become available (if needed)
  while (WM430_TX_pendingMsgs >= WM430_TX_QUEUEMSGMAX)
    WM430_TX_enableInt();                   // Keep transmitter interrupt enabled
                                            // Advance pointer of circular buffer of messages
                                            // Note that these variables may also be changing in TIMERA0_ISR
                                            // in between next few lines
  WM430_TX_currentFreeMesgPtr++;            // Increment the free message pointer
  if (WM430_TX_currentFreeMesgPtr >= WM430_TX_QUEUEMSGMAX)
    WM430_TX_currentFreeMesgPtr = 0;        // Reset free message pointer (wrap-around)
}

void WM430_TX_signalMessage(void)
{
  WM430_SYS_disableSensorInt();             // Disable sensor motion interrupt until RF activity finished
  if (WM430_TX_pendingMsgs == 0) {          // Should not be currently transmitting if nothing pending
    WM430_TX_dataIdle();                    // Avoid runt pulse at beginning
                                            // Set initial timer value
    TACCR0 = TAR + TIMERA0_SMCLK_BITPERIOD_10;
                                            // Prepare upcoming timer value for first preamble pulse
    WM430_TX_rfWidth = TIMERA0_SMCLK_BITPERIOD_10;
    WM430_TX_keyOnPeriodBitNum = WM430_TX_KEYONPERIODPREAMBLENUMBITS;
    WM430_TX_startPatternBitNum = WM430_TX_STARTPATTERNPREAMBLENUMBITS;
    WM430_TX_syncPatternBitNum = WM430_TX_SYNCPATTERNNUMBITS;
    WM430_TX_stopPatternBitNum = WM430_TX_STOPPATTERNNUMBITS;
    WM430_TX_enableRF();                    // Turn the transmitter on
    WM430_TX_enableInt();                   // Enable the Tx Interrupt
    TACTL |= MC1;                           // Start continuous timer mode

⌨️ 快捷键说明

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