 /*

	PWM.h - PWM API header file.
   includes:
      PWM_Output_Init, PWM_Enable, PWM_Disable, PWM_SetFreq, PWM_SetDC, PWM_SetDT

   Copyright (C) 2011 Houtouridis Christos (http://houtouridis.blogspot.com/)

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

   Author:     Houtouridis Christos <houtouridis.ch@gmail.com>
   Date:       11/2011
   Version:    0.1

*/

#ifndef  __PWM_h__
#define  __PWM_h__


#include <stm32f10x.h>
#include <stm32f10x_systick.h>
#include <stm32f10x_clock.h>
#include <math.h>

/* ================     User Defines       ======================*/

#define  PWM_DATA_SIZE              (4)
#define  PWM_MINIMUM_STEPS          (100)

#define  PWM_STD_DC                 (40)
#define  PWM_STD_FREQ               (10000)

#define  PWM_DEBUG_MODE             (1)

/* =========     General Defines       ========*/
#define  PWM_MAXIMUM_STEPS          (0xFFFF)
#define  PWM_DT_A_MAX               (0x7F)
#define  PWM_DT_B_MAX               (0xFE)
#define  PWM_DT_C_MAX               (0x1F8)
#define  PWM_DT_D_MAX               (0x3F0)

#define  PWM_DTG_A_MASK             (0)
#define  PWM_DTG_B_MASK             (0x80)
#define  PWM_DTG_C_MASK             (0xC0)
#define  PWM_DTG_D_MASK             (0xE0)


#define  PWM_TIM_CCMR1_1_MODE1      ((uint16_t)0x0060)
#define  PWM_TIM_CCMR1_1_MODE2      ((uint16_t)0x0070)

#define  PWM_TIM_CCMR1_2_MODE1      ((uint16_t)0x6000)
#define  PWM_TIM_CCMR1_2_MODE2      ((uint16_t)0x7000)

#define  PWM_TIM_CCMR2_3_MODE1      ((uint16_t)0x0060)
#define  PWM_TIM_CCMR2_3_MODE2      ((uint16_t)0x0060)

#define  PWM_TIM_CCMR2_4_MODE1      ((uint16_t)0x6000)
#define  PWM_TIM_CCMR2_4_MODE2      ((uint16_t)0x7000)

/*================  General  Data Types   ======================*/
typedef  enum
{
   TIM_CH1, TIM_CH1N, TIM_CH1CMP,
   TIM_CH2, TIM_CH2N, TIM_CH2CMP,
   TIM_CH3, TIM_CH3N, TIM_CH3CMP,
   TIM_CH4
}PWM_Channel_t;


/*
   See User manual page 257 and 227
 */
typedef enum 
{
   PWM_MODE_1_UP, PWM_MODE_1_DOWN,
   PWM_MODE_2_UP, PWM_MODE_2_DOWN
}PWM_Mode_t;


typedef  uint16_t       PWM_PSC_t;

// Timer typedefs
typedef  TIM_TypeDef    PWM_Timer_t;
typedef  uint32_t       PWM_Timer_Freq_t;

// PWM typedefs
typedef  float          PWM_Freq_t;
typedef  float          PWM_DC_t;
typedef  float          PWM_DT_t;

typedef  uint8_t        PWM_DTG_t;

typedef volatile struct
{
   PWM_Timer_t       *tim;
   PWM_Timer_Freq_t  tim_freq;
   PWM_Channel_t     ch;
   PWM_Mode_t        mode;
   PWM_Freq_t        freq;
   PWM_DC_t          dc;
   PWM_DT_t          dt;
}PWM_t;



typedef  enum
{
   PWM_EXIT_OK, PWM_EXIT_ERROR
}PWM_EXIT_t;



/*================   Static Functions    ======================*/

#define  PWM_DTG_A(__dt, __tfreq)      ( (PWM_DTG_t)(__dt * __tfreq) )
#define  PWM_DTG_B(__dt, __tfreq)      ( (PWM_DTG_t)(((__dt * __tfreq) / 2 ) - 64 ) )
#define  PWM_DTG_C(__dt, __tfreq)      ( (PWM_DTG_t)(((__dt * __tfreq) / 8 ) - 32 ) )
#define  PWM_DTG_D(__dt, __tfreq)      ( (PWM_DTG_t)(((__dt * __tfreq) / 16) - 32 ) )


/*================  Exported Functions   ======================*/

PWM_EXIT_t  PWM_Output_Init (PWM_t *pwm);
PWM_EXIT_t  PWM_Enable (PWM_t *pwm);
PWM_EXIT_t  PWM_Disable (PWM_t *pwm);
PWM_EXIT_t  PWM_SetFreq (PWM_t *pwm, PWM_Freq_t f);
PWM_EXIT_t  PWM_SetDC (PWM_t *pwm, PWM_DC_t dc);
PWM_EXIT_t  PWM_SetDT (PWM_t *pwm, PWM_DT_t dt);


#endif   //#ifndef  __PWM_h__
