/*******************************************************************************************************
 *                                                                                                     *
 *        **********                                                                                   *
 *       ************                                                                                  *
 *      ***        ***                                                                                 *
 *      ***   +++   ***                                                                                *
 *      ***   + +   ***     Direct Memory Access functions                                             *
 *      ***   +                                                                                        *
 *      ***   + +   ***                                                                                *
 *      ***   +++   ***     tw_dma.c                                                                   *
 *      ***        ***                                                                                 *
 *       ************                                                                                  *
 *        **********                                                                                   *
 *                                                                                                     *
 *******************************************************************************************************
 * Compiler:                IAR C Compiler for 8051                                                    *
 * Target platform:         Chipcon CC2510                                                             *
 * Author:                  MJB                                                                        *
 *******************************************************************************************************
 * Revision history:
 * Last Update:             15 July 2008
 ******************************************************************************************************/

/***********************************************************************************
* INCLUDES
*/

#include "twoway.h"


/***********************************************************************************
* GLOBAL VARIABLES
*/

DMA_DESC __xdata DmaDesc1_4[4];


/***********************************************************************************
* @fn           initDma
*
* @brief        This function clears all interrupt flags and disarms all DMA channels 1 to 4.
*
* @parm         none
*
* @return       void
*/

void initDma(void) {
  DMAARM &= DMA_CHANNEL_0;                                   // Disarm ch 1-4 (leave ch 0 unchanged)
  DMAREQ &= DMA_CHANNEL_0;                                   // Clear start flag for ch 1-4
  DMAIRQ &= DMA_CHANNEL_0;                                   // Clear DMA interrupt flags for ch 1-4

  // Clearing the interrupt flag of the DMA and enabling DMA interrupt.
  INT_SETFLAG(INUM_DMA, INT_CLR);
  INT_ENABLE(INUM_DMA, INT_ON);

  // Setting the address of the DMA descriptors
  DMA_SET_ADDR_DESC1234(DmaDesc1_4);
}

/***********************************************************************************
* @fn         dmaFromRadio
*
* @brief      This function configures DMA transfer from the radio to an RX buffer.
*
* @param[in]  length
*             Number of bytes to be transferred
*
* @param[in]  dstAddr
*             The start address in __xdata space to where the data is to be stored.
*
*
* @return     void
*
* Note: Uses DMA Channel DMA_RX
*
*/

void dmaFromRadio(WORD length, WORD dstAddr){
  SET_WORD(DmaDesc1_4[DMA_RX-1].SRCADDRH, DmaDesc1_4[DMA_RX-1].SRCADDRL,   &X_RFD);   // Set RFD register as source
  SET_WORD(DmaDesc1_4[DMA_RX-1].DESTADDRH, DmaDesc1_4[DMA_RX-1].DESTADDRL, dstAddr);  // RX buffer address
  SET_WORD(DmaDesc1_4[DMA_RX-1].LENH, DmaDesc1_4[DMA_RX-1].LENL, length);             // Maximum length of RX buffer
  DmaDesc1_4[DMA_RX-1].VLEN       = VLEN_1_P_VALOFFIRST_P_2;                          // The first byte indicates transfer length
  DmaDesc1_4[DMA_RX-1].PRIORITY   = PRI_LOW;
  DmaDesc1_4[DMA_RX-1].M8         = M8_USE_8_BITS;
  DmaDesc1_4[DMA_RX-1].IRQMASK        = IRQMASK_DISABLE;                       // Do not generate an interrupt on DMA completion.
  DmaDesc1_4[DMA_RX-1].DESTINC    = DESTINC_1;                                 // The destination is constant
  DmaDesc1_4[DMA_RX-1].SRCINC     = SRCINC_0;                                  // The source address increments by 1 byte for each transfer.
  DmaDesc1_4[DMA_RX-1].TRIG       = DMATRIG_RADIO;                             // DMA is started by the radio receiver.
  DmaDesc1_4[DMA_RX-1].TMODE      = TMODE_SINGLE;                              // One byte is transferred at each DMA trigger.
  DmaDesc1_4[DMA_RX-1].WORDSIZE   = WORDSIZE_BYTE;                             // Byte transfer.

  return;
}


/***********************************************************************************
* @fn         dmaToRadio
*
* @brief      This function configures DMA transfer to the radio from a TX buffer.
*
* @param[in]  length
*             The length (bytes) of packet to move from TX buffer
*
* @param[in]  srcAddr
*             The start address in __xdata space of the data to be transferred.
*
* @return     void
*
* Note: Uses DMA Channel DMA_TX
*
*/

void dmaToRadio(WORD length, WORD srcAddr){
   SET_WORD(DmaDesc1_4[DMA_TX-1].SRCADDRH, DmaDesc1_4[DMA_TX-1].SRCADDRL,   srcAddr);  // TX buffer address
   SET_WORD(DmaDesc1_4[DMA_TX-1].DESTADDRH, DmaDesc1_4[DMA_TX-1].DESTADDRL, &X_RFD);   // Set RFD register as destination
   SET_WORD(DmaDesc1_4[DMA_TX-1].LENH, DmaDesc1_4[DMA_TX-1].LENL, length);             // Setting the length of the transfer (bytes)
   DmaDesc1_4[DMA_TX-1].VLEN           = VLEN_USE_LEN;                          // Using the length field
   DmaDesc1_4[DMA_TX-1].PRIORITY       = PRI_LOW;
   DmaDesc1_4[DMA_TX-1].M8             = M8_USE_8_BITS;                         // Transferring all 8 bits in each byte.
   DmaDesc1_4[DMA_TX-1].IRQMASK        = IRQMASK_DISABLE;                       // Do not generate an interrupt on DMA completion.
   DmaDesc1_4[DMA_TX-1].DESTINC        = DESTINC_0;                             // The destination address is constant
   DmaDesc1_4[DMA_TX-1].SRCINC         = SRCINC_1;                              // The address for data fetch is inremented by 1 byte
                                                                                // after each transfer.
   DmaDesc1_4[DMA_TX-1].TRIG           = DMATRIG_RADIO;                         // DMA is started by writing the radio
   DmaDesc1_4[DMA_TX-1].TMODE          = TMODE_SINGLE;                          // Byte for byte is transferred.
   DmaDesc1_4[DMA_TX-1].WORDSIZE       = WORDSIZE_BYTE;                         // One byte is transferred each time.

   return;
}


/***********************************************************************************
  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.
***********************************************************************************/





