/*******************************************************************************************************
 *                                                                                                     *
 *        **********                                                                                   *
 *       ************                                                                                  *
 *      ***        ***                                                                                 *
 *      ***   +++   ***                                                                                *
 *      ***   + +   ***     Functions for transfering stored audio samples to the Delta-Sigma          *
 *      ***   +             Modulator/Pulse Width Modulator output                                     *
 *      ***   + +   ***                                                                                *
 *      ***   +++   ***     tw_dsm.c                                                                   *
 *      ***        ***                                                                                 *
 *       ************                                                                                  *
 *        **********                                                                                   *
 *                                                                                                     *
 *******************************************************************************************************
 * Compiler:                IAR C Compiler for 8051                                                    *
 * Target platform:         Chipcon CC2510                                                             *
 * Author:                  TBR, KHO, ESY                                                              *
 *******************************************************************************************************
 * Revision history:
 *
 * Last Update:             15 July 2008 MJB
 ******************************************************************************************************/

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

#include "twoway.h"


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

UINT16 timerSampleRate;


/***********************************************************************************
* @fn         initDsm
*
* @brief      This function enables the Delta-Sigma Modulator for 8 kHz operation
*             with 26 MHz clock.
*
* The DSM is described in the CC2510/CC2511 data sheet, section 12.6.8
* on pages 115 - 116 (SWRS055F):
*
* 1. Suspend timer 1 (T1CTL:MODE = 0)
* 2. Clear timer counter by writing any value to T1CNTL. (T1CNT = 0x0000)
* 3. Set the sample rate by writing the Timer 1 channel 0 count period, T1CC0.
*    (From Table 43 in data sheet: T1CC0 = 0x0CB1 for 8 kHz @ 26 MHz)
* 4. Set Timer 1 channel 0 compare mode (T1CCTL0.MODE = 1)
* 5. Load first sample if available (or zero if no sample available) into T1CC1H:T1CC1L
* 6. Enable timer counter to modulo mode (T1CTL.MODE = 2)
* 7. Configure the DSM by setting the MODE and CAP fields of the T1CCTL1 register
* 8. Enable DSM mode (T1CCTL1.CMP = 7)
*
* On each Timer 1 IRQ or Timer 1 DMA trigger, write a new sample to the T1CC1H:T1CC1L
* registers. The least significant bits must be written to T1CC1L before the most
* significant bits are written to T1CC1H.
*
* The samples written mustbe signed 2's complement values. The 2 least significant
* bits will always be treated as 0, thus the effective sample size is 14 bits.
*
* @param      none
*
* @return     void
*/

void initDsm(void) {
  // Suspend timer 1 (T1CTL:MODE = 0)
  T1CTL &= ~0x03;
  // Clear timer counter by writing any value to T1CNTL. (T1CNT = 0x0000)
  T1CNTL = 0x00;

  // Set the sample rate by writing the Timer 1 channel 0 count period, T1CC0.
  timerSampleRate = NORMAL_SAMPLE_PERIOD;
  T1CC0H = ((timerSampleRate >> 8) & 0xFF);
  T1CC0L = (timerSampleRate & 0xFF);

  // Set Timer 1 channel 0 compare mode (T1CCTL0.MODE = 1)
  T1CCTL0 |= 0x04;
  // Load first sample if available (or zero if no sample available) into T1CC1H:T1CC1L
  T1CC1L = 0;
  T1CC1H = 0;
  // Enable timer counter to modulo mode (T1CTL.MODE = 2)
  T1CTL |= 0x02;
  // Configure the DSM by setting the MODE and CAP fields of the T1CCT1 register
  T1CCTL1 &= 0xC0;

  // Enable interrupt on timer 1 overflow
  TIMER1_ENABLE_OVERFLOW_INT(TRUE);
  INT_ENABLE(INUM_T1, INT_ON);

  // Enable DSM mode (T1CCTL1.CMP = 7)
  T1CCTL1 |= 0x3A; // interpolator off, shaping on
}


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


