/*******************************************************************************************************
 *                                                                                                     *
 *        **********                                                                                   *
 *       ************                                                                                  *
 *      ***        ***                                                                                 *
 *      ***   +++   ***                                                                                *
 *      ***   + +   ***     Medium Access Control layer                                                *
 *      ***   +                                                                                        *
 *      ***   + +   ***                                                                                *
 *      ***   +++   ***     tw_mac.c                                                                   *
 *      ***        ***                                                                                 *
 *       ************                                                                                  *
 *        **********                                                                                   *
 *                                                                                                     *
 *******************************************************************************************************
 * Compiler:                IAR C Compiler for 8051                                                    *
 * Target platform:         Chipcon CC2510                                                             *
 * Author:                  TBR, KHO, ESY, MJB                                                         *
 *******************************************************************************************************
 * Revision history:
 * Last Update:             15 July 2008 MJB
 ******************************************************************************************************/


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

#include "twoway.h"


/***********************************************************************************
* @fn            macTimer3Init
*
* @brief	Initialize timer3
*
*   Initialize for use as RX timeout timer.
*   Note: Timer 3 is clocked by the 26.000 MHz XTAL divided by the 'Prescale Divider', defined
*   in bits 7:5 of T3CTL as 128. This sets the 'tic' period as 128/26 = 4.923076923 usec
*
* @param        none
*
* @return       void
*/

void macTimer3Init(void) {
  T3CCTL0 = 0;
  T3CCTL1 = 0;
  T3CTL = 0xE5;               // Set the Prescale Divider to 7 (divide by 128). One count = 4.923 usec
                              // interrupt on overflow disabled
                              // Clear the counter and set mode to Down (count from T3CCO to 0)
}


/***********************************************************************************
* @fn           macsetT3TimeoutAndWait
*
* @brief	Set the T3 timeout and start timer.
*
* @param[in]
*               timeout       Must have a value between 1 and 255
*               multiplier    Must have a value between 1 and 255
*
*   Note: The timeout period can be calculated from the formula:
*   timeout period (usec) = multiplier*timeout*4.923076923
*
* @return       void
*/

void macsetT3TimeoutAndWait(UINT8 timeout, UINT8 multiplier) {

int i;

  TIMIF &= ~T3OVFIF;            // Clear RX_TIMEOUT_TIMER_FLAG

  for (i = 1; i <= multiplier; i++) {
    T3CC0 = timeout;            // Set timeout period
    TIMER3_RUN(TRUE);           // Start timer

    while(!(TIMIF & T3OVFIF));  // Wait for timeout

    TIMIF &= ~T3OVFIF;          // Clear RX_TIMEOUT_TIMER_FLAG
    TIMER3_RUN(FALSE);          // Stop timer
  }
}


////////////////////////////////////////////////////////////////////////////////
/// @brief	Set next channel after RX/TX completes
////////////////////////////////////////////////////////////////////////////////

void setChannel (UINT8 ch) {

// If radio state is not IDLE, force it into the IDLE state

  if (MARCSTATE != MARCSTATE_IDLE)
    SIDLE();

  CHANNR = ch;
}

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


