timm.h 16 KB
Newer Older
李俭双's avatar
李俭双 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
#ifndef __TIMM_H__
#define __TIMM_H__
#include <stdint.h>
#include <stdbool.h>
#include "common.h"
/****************************************************************************/
/***********************TMM PARAMATERS***************************************/
/****************************************************************************/
#define _01_TMM_TM0_COUNT_START                   	(0x01U) /* TM0 Count starts */
#define _02_TMM_TM1_COUNT_START                   	(0x02U) /* TM1 Count starts */

#define _00_TMM_TM0_COUNT_STOP_MATCH              	(0x00U) /* count stops at compare match with TMGRA0 register */
#define _04_TMM_TM0_COUNT_CONTINUES               	(0x04U) /* count continues after compare match with TMGRA0 register */
#define _00_TMM_TM1_COUNT_STOP_MATCH              	(0x00U) /* count stops at compare match with TMGRA1 register */
#define _08_TMM_TM1_COUNT_CONTINUES                 (0x08U) /* count continues after compare match with TMGRA1 register */

#define _01_TMB_INTERRUPT_TBIMFA_ENABLE           	(0x01U) /* interrupt by TBIMFA bit enabled */
#define _02_TMB_INTERRUPT_TBIMFB_ENABLE           	(0x02U) /* interrupt by TBIMFB bit enabled */

#define _10_TMM_TMGRC0_BUFFER                       (0x10U) /* buffer register for TMGRA0 register */
#define _20_TMM_TMGRD0_BUFFER                       (0x20U) /* buffer register for TMGRB0 register */
#define _40_TMM_TMGRC1_BUFFER                       (0x40U) /* buffer register for TMGRA1 register */
#define _80_TMM_TMGRD1_BUFFER                       (0x80U) /* buffer register for TMGRB1 register */

#define TMM_FLAG_OVFLOW					((uint8_t)0x10)
#define TMM_FLAG_MATCHD               	((uint8_t)0x08)
#define TMM_FLAG_MATCHC               	((uint8_t)0x04)
#define TMM_FLAG_MATCHB               	((uint8_t)0x02)
#define TMM_FLAG_MATCHA					((uint8_t)0x01)
#define IS_TMM_FLAG(FLAG)			((((FLAG) & (uint8_t)0xE0) == 0x00) && ((FLAG) != (uint8_t)0x00))

/** @defgroup TMM_t 
  * @{
  */
typedef enum 
{
	TMM0 = 0x00,
	TMM1 = 0x01,	
}TMMSelect_TypeDef;
#define IS_TMM_SELECT(TMMx) (((TMMx) == TMM0) || ((TMMx) == TMM1))
/** @defgroup TMM_Clk_t 
  * @{
  */
typedef enum 
{
    TMM_FCLK_Div1		= 0x00U,
    TMM_FCLK_Div2		= 0x01U,
    TMM_FCLK_Div4		= 0x02U,
	TMM_FCLK_Div8		= 0x03U,
    TMM_FCLK_Div32		= 0x04U,
    TMM_FCLK_TMCLK		= 0x05U,
}TMM_Clk_t;
#define IS_TMM_CLOCK(CLK)	(((CLK) == TMM_FCLK_Div1) || ((CLK) == TMM_FCLK_Div2) || ((CLK) == TMM_FCLK_Div4)|| \
                                ((CLK) == TMM_FCLK_Div8)||((CLK) == TMM_FCLK_Div32) ||((CLK) == TMM_FCLK_Div1)|| \
								((CLK) == TMM_FCLK_TMCLK))
/** @defgroup TMM_Channel_t 
  * @{
  */
#define	TMM_Channel_A   	((uint8_t)0x01)
#define	TMM_Channel_B 		((uint8_t)0x02)
#define	TMM_Channel_C   	((uint8_t)0x04)
#define	TMM_Channel_D 		((uint8_t)0x08)
#define	TMM_Channel_ELC		((uint8_t)0x10)	
#define IS_TMM_CHANNEL(CHANN)			((((CHANN) & (uint8_t)0xE0) == 0x00) && ((CHANN) != (uint8_t)0x00))


/** @defgroup TMM_Counter_Clear_t 
  * @{
  */
#define TM_Clear_Disable        ((uint8_t)0x00)
#define TM_Clear_Match_TMGRA 	((uint8_t)0x20)
#define TM_Clear_Match_TMGRB	((uint8_t)0x40)
#define	TM_Clear_Sychronous   	((uint8_t)0x60)  // clear by the other timer under the circumstances of TMMR resister bit TMSYNC is 1
#define TM_Clear_Match_TMGRC 	((uint8_t)0xA0)
#define TM_Clear_Match_TMGRD	((uint8_t)0xC0)

/** @defgroup TMM_Combine_TransMode_t 
  * @{
  */
#define TMM_Trans_TM1_Underflow 		((uint8_t)0x02) /*data transfer from buffer register to general register when TM1 underflows */
#define TMM_Trans_TM0_Match				((uint8_t)0x03) /* data transfer from buffer register to general register when TM0 compare match TMGRA0 */

/** @defgroup TMM_Interrupt_t 
  * @{
  */
#define TMM_Int_NONE        ((uint8_t)0x00)
#define TMM_Int_IMIA 		((uint8_t)0x01)
#define TMM_Int_IMIB		((uint8_t)0x02)
#define	TMM_Int_IMIC   		((uint8_t)0x04)  
#define TMM_Int_IMID 		((uint8_t)0x08)
#define TMM_Int_OVIE 		((uint8_t)0x10) /* interrupt (OVI) by bits OVF and UDF enabled */

/** @defgroup TMM_Output_Compare_Polarity 
  * @{
  */
#define TMM_Output_Disable		((uint8_t)0x00)
#define TMM_Output_Low			((uint8_t)0x01)
#define TMM_Output_High			((uint8_t)0x02)
#define TMM_Output_Toggle		((uint8_t)0x03)

/** @defgroup TMM_Output_Compare_Initlevel 
  * @{
  */
#define TMM_OCInit_Low_Level		((uint8_t)0x00)
#define TMM_OCInit_High_Level		((uint8_t)0x01)

/** @defgroup TMM_Input_Capture_Polarity 
  * @{
  */
#define TMM_Capture_Rising		((uint8_t)0x00)
#define TMM_Capture_Falling		((uint8_t)0x01)
#define TMM_Capture_Both		((uint8_t)0x02)

/** @defgroup TMM_Input_Capture_Clock_Source 
  * @{
  */
#define TMM_Filter_FCLK_Div32		((uint8_t)0x00)
#define TMM_Filter_FCLK_Div8		((uint8_t)0x01)
#define TMM_Filter_FCLK_Div1		((uint8_t)0x02)
#define TMM_Filter_USE_CLK		    ((uint8_t)0x03) //use TMM opreation clock

/** @defgroup TMM_Input_Capture_Clock_Source 
  * @{
  */
#define TMM_GeneralRegister_Setting		0
#define TMM_BufferRegister_Setting		1

/** 
  * @brief  TMM_ICFilter_Set definition  
  */
#define TMM_ICFilter_Disable		0
#define TMM_ICFilter_Enable			1

/** @defgroup TMM_WorkMode 
  * @{
  */
typedef enum
{
	TMM_Mode_Capture 	= 0x00,
	TMM_Mode_Compare	= 0x01,
	TMM_Mode_PWM     	= 0x02,// normal PWM mode
	TMM_Mode_PWM_RSTSYN = 0x03,// RstSynchronous_PWM mode
	TMM_Mode_CompPWM 	= 0x04,//	complementary PWM mode
	TMM_Mode_PWM3 		= 0x05,//	output two PWM with same period
}TMM_Mode_t;




/** 
  * @brief  TMM Input Capture Init structure definition  
  */

typedef struct
{


  uint8_t TMM_ICPolarity;   /*!< Specifies the active edge of the input signal.
                                  This parameter can be a value of @ref TIM_Input_Capture_Polarity */

  uint8_t TMM_ICFilter;     /*!< Specifies the input capture filter flag.@ref TMM_ICFilter_Set,This parameter can be a number between 0x0 and 0x1 */
                                  
} TMM_ICInitTypeDef;


typedef struct
{
	uint8_t TMM_Filter_Clk;     /*!< Specifies the clock source of input capture filter .
									This parameter can be a number between 0x0 and 0x03, it provides clock source when using filter */

	TMM_ICInitTypeDef 	TMM_CHA;	/*!< TMM input capture channelA, for TMM channelA, it has Capture Compare Register TMGRA. 
												input capture parameter can be  a value of @ref TMM_ICInitTypeDef */

	TMM_ICInitTypeDef 	TMM_CHB;	/*!< TMM input capture channelB, for TMM channelB, it has Capture Compare Register TMGRB. 
												input capture parameter can be  a value of @ref TMM_ICInitTypeDef */

	TMM_ICInitTypeDef 	TMM_CHC;	/*!< TMM input capture channelC, for TMM channelC, it has Capture Compare Register TMGRC. 
												input capture parameter can be  a value of @ref TMM_ICInitTypeDef */
	uint8_t TMM_CHC_AS_BufferRegister;/*the flag of channelC worked as buffer Register for channelA,it @ref TMM_BufferRegister_Set*/
	
	TMM_ICInitTypeDef 	TMM_CHD;	/*!< TMM input capture channelD, for TMM channelD, it has Capture Compare Register TMGRD. 
												input capture parameter can be  a value of @ref TMM_ICInitTypeDef */
	uint8_t TMM_CHD_AS_BufferRegister;/*the flag of channelD worked as buffer Register for channelB,it @ref TMM_BufferRegister_Set*/

}TMM_IC_Channel_t;

/** 
  * @brief  TMM Output Compare Init structure definition  
  */

typedef struct
{
	uint8_t TMM_OCPolarity;    /*!< Specifies the output polarity.
								   This parameter can be a value of @ref TIM_Output_Compare_Polarity */
	uint8_t TMM_OCInitLevel;    /*!< Specifies the output init level.
								   This parameter can be a value of @ref TIM_Output_Compare_InitLevel */

} TMM_OCInitTypeDef;

/** 
  * @brief  TMM_OC_Output_PinCtrl  
  */
#define TMM_OC_Output_PinCtrl_Disable   ((uint8_t)0x00)
#define TMM_OC_Output_PinCtrl_Enable    ((uint8_t)0x01)

typedef struct
{

	TMM_OCInitTypeDef TMM_CHA;     /*!< TMM output compare channelA, for TMM channelA, it has Compare Register TMGRA. 
												output compare parameter can be  a value of @ref TMM_OCInitTypeDef */

	TMM_OCInitTypeDef TMM_CHB;   /*!< TMM output compare channelB, for TMM channelB, it has Compare Register TMGRB. 
												output compare parameter can be  a value of @ref TMM_OCInitTypeDef */

	TMM_OCInitTypeDef TMM_CHC;     /*!< TMM output compare channelC, for TMM channelC, it has Compare Register TMGRC. 
											output compare parameter can be  a value of @ref TMM_OCInitTypeDef */
	
	uint8_t TMM_CHC_OutPin_Ctrl;     /*!< TMM output compare channelC, Compare Register TMGRC, combined with Compare Register TMGRA,
											used to control TMIOA output pin. if TMM_OC_Output_PinCtrl_Enable is setted, TMIOA output pin can output PWM wave
											TMM_CHD_OutPin_Ctrl parameter can be  a value of @ref TMM_OC_Output_PinCtrl*/
	
	TMM_OCInitTypeDef TMM_CHD;   /*!< TMM output compare channelD, for TMM channelD, it has Compare Register TMGRD. 
											output compare parameter can be  a value of @ref TMM_OCInitTypeDef */
	
	uint8_t TMM_CHD_OutPin_Ctrl;     /*!< TMM output compare channelD, Compare Register TMGRD, combined with Compare Register TMGRB,
											used to control TMIOB output pin. if TMM_OC_Output_PinCtrl_Enable is setted, TMIOB output pin can output PWM wave
											TMM_CHD_OutPin_Ctrl parameter can be  a value of @ref TMM_OC_Output_PinCtrl*/
								

} TMM_OC_Channel_t;


/** 
  * @brief  TMM_PWM_InitLevel
  */
#define TMM_PWMInitLevel_Low		((uint8_t)0x00)	
#define TMM_PWMInitLevel_High		((uint8_t)0x01)	

/** 
  * @brief  TMM_PWM_ActiveLevel
  */
#define TMM_PWMActiveLevel_Low		((uint8_t)0x00)	
#define TMM_PWMActiveLevel_High		((uint8_t)0x01)	

/** 
  * @brief  TMM PWM Init structure definition  
  */
typedef struct
{
	uint8_t TMM_PWMInitLevel;    /*!< Specifies the output init level.
								   This parameter can be a value of @ref TIM_PWM_InitLevel */
	
	uint8_t TMM_PWMActiveLevel;    /*!< Specifies the output active level.
								   This parameter can be a value of @ref TIM_PWM_ActiveLevel */

} TMM_PWMInitTypeDef;




typedef struct
{

	TMM_PWMInitTypeDef TMM_CHA;     /*!< TMM output compare channelA, for TMM channelA, it has Compare Register TMGRA. 
												output compare parameter can be  a value of @ref TMM_OCInitTypeDef */

	TMM_PWMInitTypeDef TMM_CHB;   /*!< TMM output compare channelB, for TMM channelB, it has Compare Register TMGRB. 
												output compare parameter can be  a value of @ref TMM_OCInitTypeDef */

	TMM_PWMInitTypeDef TMM_CHC;     /*!< TMM output compare channelC, for TMM channelC, it has Compare Register TMGRC. 
											output compare parameter can be  a value of @ref TMM_OCInitTypeDef */
		
	TMM_PWMInitTypeDef TMM_CHD;   /*!< TMM output compare channelD, for TMM channelD, it has Compare Register TMGRD. 
											output compare parameter can be  a value of @ref TMM_OCInitTypeDef */
									

} TMM_PWM_Channel_t;

/** 
  * @brief TMM_Output_State_Selection 
  */
#define TMM_InitHigh_ActiveLow             (0x00U) /*  initial high level and active level is low level */
#define TMM_InitLow_ActiveHigh             (0x01U) /* initial low level and active level is high level */


/** 
  * @brief  BDTR structure definition 
  */

typedef struct
{

	uint8_t TMM_PostiveState;        /*!< Specifies the Postive output level selection .
									  This parameter can be a value of @ref TMM_Output_State_Selection */

	uint8_t TMM_NegativeState;        /*!< Specifies the Negative output level selection .
									  This parameter can be a value of @ref TMM_Output_State_Selection */

	uint8_t TMM_Break;            /*!< Specifies whether the TIM Break input is enabled or not. 
									  This parameter can be a value of @ref TIM_Break_Input_enable_disable */

	uint16_t TMM_DeadTime;         /*!< Specifies the delay time between the switching-off and the
								  switching-on of the outputs.
								  This parameter can be a number between 0x00 and 0xFF  */

	uint16_t TMIOA_BreakPolarity;    /*!< Specifies the TMIOA Break Output pin polarity.
                                      This parameter can be a value of @ref TMM_Break_Polarity */

	uint16_t TMIOB_BreakPolarity;    /*!< Specifies the TMIOB Break Output pin polarity.
                                      This parameter can be a value of @ref TMM_Break_Polarity */

	uint16_t TMIOC_BreakPolarity;    /*!< Specifies the TMIOC Break Output pin polarity.
                                      This parameter can be a value of @ref TMM_Break_Polarity */

	uint16_t TMIOD_BreakPolarity;    /*!< Specifies the TMIOD Break Output pin polarity.
                                      This parameter can be a value of @ref TMM_Break_Polarity */

} TMM_BDTRInitTypeDef;

typedef struct
{
	TMMSelect_TypeDef	TMM_Select;		/*!< Specifie the TMM uint of TMM, it can be TMM0 or TMM1
	                                  This parameter can be a value of @ref TMM_t */
	
	uint8_t TMM_Channel;     /*!< Specifie the channel of TMM
	                                  This parameter can be a value of @ref TMM_Channel_t */
										   	
	uint8_t TMM_Clk;     /*!< Specifie the count clock source of TMM
	                                  This parameter can be a value of @ref TMM_Clk_t */
	
	TMM_Mode_t TMM_Mode; 	         /*!< Specifies the TIM work mode of different TIM channel
									      This parameter can be a value of @ref TMM_Mode_t */
	
	uint16_t TMM_CHA_Pulse;         /*!< Specifies the pulse value to be loaded into the Capture Compare RegisterTMGRA. 
										   This parameter can be a number between 0x0000 and 0xFFFF */

	uint16_t TMM_CHB_Pulse;         /*!< Specifies the pulse value to be loaded into the Capture Compare Register TMGRB. 
										   This parameter can be a number between 0x0000 and 0xFFFF */

	uint16_t TMM_CHC_Pulse;         /*!< Specifies the pulse value to be loaded into the Capture Compare RegisterTMGRC. 
										   This parameter can be a number between 0x0000 and 0xFFFF */

	uint16_t TMM_CHD_Pulse;         /*!< Specifies the pulse value to be loaded into the Capture Compare Register TBMGRD. 
										   This parameter can be a number between 0x0000 and 0xFFFF */

	TMM_IC_Channel_t 	TMM_IC;	 /*!< TMM input capture mode channel This parameter can be a value of @ref TMM_IC_Channel_t */

	
	TMM_OC_Channel_t	TMM_OC	;	/*!< TMM output compare channel This parameter can be a value of @ref TMM_OC_Channel_t */
	
	TMM_PWM_Channel_t	TMM_PWM	;	/*!< TMM used as PWM channel This parameter can be a value of @ref TMM_PWM_Channel_t */
	
	TMM_BDTRInitTypeDef TMM_BDTR;	/*!< TMM used to set PWM break and deadtime, This parameter can be a value of @ref TMM_BDTRInitTypeDef */
	
	uint8_t		TMM_CounterClear;	/*!< TMM counter TM Register clear conditon This parameter can be a value of @ref TMM_Counter_Clear_t */
	
357 358
	uint8_t		TMM_Combine_TransMode;	/*!< TMM counter TM buffer Register transfer mode: 1��buffer register copyed to general register when TM1 
										underflows 2��buffer register copyed to general register when TM0 compare match to  TMGRA0; it only used 
李俭双's avatar
李俭双 committed
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
											in complementary PWM mode This parameter can be a value of @ref TMM_Combine_TransMode_t */

	uint8_t TMM_Intp;         /*!< Specifies the whether open the interrupt flag of Capture Compare Register TBGRA.TBGRB/TBGRC/TBGRD 
										   when ocurs TM match Capture Compare Register,it will generates interrupt
										   This parameter can be  @ref TMM_Interrupt_t */	
}TMM_InitTypeDef; 

typedef struct 
{
	volatile uint8_t			*TMMR;
	volatile uint8_t			*TMPMR;
	volatile uint8_t			*TMFCR;
	volatile uint8_t            *TMOER[2];	
	volatile uint8_t			*TMOCR;	
	volatile uint8_t			*TMCR[2];	
	volatile uint8_t			*TMIORA[2];	
	volatile uint8_t			*TMIORC[2];			
	volatile uint8_t			*TMIER[2];	
	volatile uint8_t            *TMPOCR[2];
    volatile uint8_t            *TMSR;

	volatile uint8_t            *TMDF[2];
	volatile uint8_t            *TMOER1;
    volatile uint8_t            *OPTL0;
	
	volatile uint16_t            *TMGRA;
	volatile uint16_t            *TMGRB;
	volatile uint16_t            *TMGRC;
	volatile uint16_t            *TMGRD;

}TMM_REG_t;

void TMM_Init(TMM_InitTypeDef *TMM_InitStruct);
void TMM1_Start(FunctionalState match_flag);
void TMM0_Start(FunctionalState match_flag);
void TMM0_Stop(void);
395
void TMM1_Stop(void);
李俭双's avatar
李俭双 committed
396 397 398 399 400 401 402
void TMM_All_Start(void);
void TMM_All_Stop(void);
FlagStatus TMM_GetFlagStatus(TMMSelect_TypeDef TMMx, uint8_t TMM_FLAG);

uint32_t TMM_GetPulseWidth(TMMSelect_TypeDef TMMx, uint8_t channel);

#endif