void InitializeVibration(void)
{
  // Make sure the vibe motor is off
  SetVibeMotorState(pdFALSE);
  
  // Initialize the timer for the vibe motor with the right PWM params.
  SetupVibrationMotorTimerAndPwm();

  // Vibe motor duration timer.
  VibeEventTimerCount = 0;    
}
/* Handle the message from the host that starts a vibration event */
void SetVibrateModeHandler(tHostMsg* pMsg)
{

  // overlay a structure pointer on the data section
  tSetVibrateModePayload* pMsgData;
  pMsgData = (tSetVibrateModePayload*) pMsg->pPayload;

  // set it active or cancel it
  VibeEventActive = pMsgData->Enable;

  // save the parameters from the message
  motorOn = pMsgData->Enable;

  tWordByteUnion temp;
  temp.byte0 = pMsgData->OnDurationLsb; 
  temp.byte1 = pMsgData->OnDurationMsb;
  timeOn = temp.word / RTC_TIMER_MS_PER_TICK;

  temp.byte0 = pMsgData->OffDurationLsb; 
  temp.byte1 = pMsgData->OffDurationMsb;
  timeOff = temp.word / RTC_TIMER_MS_PER_TICK;

  cycleCount = pMsgData->NumberOfCycles;

  // Start or stop the RTC PS timer based on whether the motor is being turned
  // on or off
  if ( VibeEventActive )
  {
    EnableRtcPrescaleInterruptUser(RTC_TIMER_VIBRATION);
    EnableVibratorPwm();
  }
  else
  {
    DisableRtcPrescaleInterruptUser(RTC_TIMER_VIBRATION);
    DisableVibratorPwm();
  }

  // Use a separate timer that counts Rtc_Prescale_Timer_Static messages to
  // get the time for the vibe motor.
  if(VibeEventActive)
  {
    VibeEventTimerCount = 0;
    timeStart =  0;
  }

  // the next event is to turn
  nextActionTime =  timeStart + timeOn;

  // Set/clear  the port bit that controls the motor
  SetVibeMotorState(motorOn);

}
/* 
 * Once the phone has started a vibration event this controls the pulsing
 * on and off.
 * 
 * This requires < 7 us and is done in the ISR
*/
void VibrationMotorStateMachineIsr(void)
{
  VibeEventTimerCount++;
  
  // If we have an active event
  if( VibeEventActive )
  {
    // is it time for the next action
    if(VibeEventTimerCount >= nextActionTime)
    {
      // if the motor is currently on
      if(motorOn)
      {
        motorOn = pdFALSE;
        nextActionTime +=  timeOff;
        
        if ( cycleCount > 1 )
        {
          cycleCount --;
        }
        else /* last cycle */
        {
          VibeEventActive = pdFALSE;
          DisableRtcPrescaleInterruptUser(RTC_TIMER_VIBRATION);
          DisableVibratorPwm();
        }
      
      }
      else
      {
        motorOn = pdTRUE;
        nextActionTime +=  timeOn;
      }
  
    }
  
    // Set/clean the port bit that controls the motor
    SetVibeMotorState(motorOn);
  
  }
  else
  {
    DisableRtcPrescaleInterruptUser(RTC_TIMER_VIBRATION);
    DisableVibratorPwm();   
  }
  
}