第二弹:i.MX6ULL的GPT外设的驱动

头文件:

```头文件
#ifndef __MY_GPT_H
#define __MY_GPT_H

#include "MCIMX6Y2.h"


// general purpose timer
/**
 * @brief   GPT is a 32-bit up-counting conter.
 * 
 */ 

#define GPT_SoftWare_Reset                              ((uint32_t)0x00008000)


typedef struct{
    uint32_t GPT_ClockSource;       /*!< Specifies the clock source.
                                         This parameter can be a value of @ref GPT_CLKSRC */
    uint32_t GPT_Enable24MHz;       /*!< Enable 24MHz clock input from crystal 
                                         This parameter can be a value of @ref GPT_CLK24MHz */
    uint32_t GPT_Prescaler;         /*!< Specifies the prescaler value used to divide the GPT clock.
                                       This parameter can be a number between 0x000 and 0xFFF */
    uint32_t GPT_Prescaler24M;      /*!< Specifies the prescaler value used to divide the GPT 24MHz clock  
                                       This parameter can be a number between 0x0 and 0xF*/
    uint32_t GPT_EnableMode;        /*!< Specifies the GPT enable mode. 
                                       This parameter can be a value of @ref GPT_ENMODE.  */ 


    uint32_t GPT_Compare1Mode;      /*!< Specifies the behavior of the GPT when a compare event in channel 1 occurs.
                                       This parameter can be a value of @ref GPT_FRR */
}GPT_InitTypeDef;

/** @defgroup GPT_CLKSRC 
  * @{
  */

#define GPT_CLKSRC_NoClock                              ((uint32_t)0x00000000)
#define GPT_CLKSRC_PeriphralClock                       ((uint32_t)0x00000040)
#define GPT_CLKSRC_HighFrequencyRefClock                ((uint32_t)0x00000080)
#define GPT_CLKSRC_ExternalClock                        ((uint32_t)0x000000C0)
#define GPT_CLKSRC_LowFrequencyRefClock                 ((uint32_t)0x00000100)
#define GPT_CLKSRC_CrystalOSCClock                      ((uint32_t)0x00000140)

/** @defgroup GPT_CLK24MHz 
  * @{
  */

#define GPT_CLK24MHz_Disable                            ((uint32_t)0x00000000)
#define GPT_CLK24MHz_Enable                             ((uint32_t)0x00000400)

/** @defgroup GPT_ENMODE 
  * @{
  */

#define GPT_ENMODE_Continue                             ((uint32_t)0x00000000)
#define GPT_ENMODE_Restart                              ((uint32_t)0x00000002)

/** @defgroup GPT_FRR 
  * @{
  */

#define GPT_FRR_Restart                                 ((uint32_t)0x00000000)
#define GPT_FRR_FreeRun                                 ((uint32_t)0x00000200)


/** @defgroup GPT_Enable 
  * @{
  */

#define GPT_CR1_EN                                      ((uint32_t)0x00000001)



/* Output Compare management **************************************************/
#define GPT_OutputMode1_Disconnected                    ((uint32_t)0x00000000)
#define GPT_OutputMode1_TogglePin                       ((uint32_t)0x00100000)
#define GPT_OutputMode1_ClearPin                        ((uint32_t)0x00200000)
#define GPT_OutputMode1_SetPin                          ((uint32_t)0x00300000)
#define GPT_OutputMode1_LowPulse                        ((uint32_t)0x00400000)

#define GPT_OutputMode2_Disconnected                    ((uint32_t)0x00000000)
#define GPT_OutputMode2_TogglePin                       ((uint32_t)0x00800000)
#define GPT_OutputMode2_ClearPin                        ((uint32_t)0x01000000)
#define GPT_OutputMode2_SetPin                          ((uint32_t)0x01800000)
#define GPT_OutputMode2_LowPulse                        ((uint32_t)0x02000000)

#define GPT_OutputMode3_Disconnected                    ((uint32_t)0x00000000)
#define GPT_OutputMode3_TogglePin                       ((uint32_t)0x04000000)
#define GPT_OutputMode3_ClearPin                        ((uint32_t)0x08000000)
#define GPT_OutputMode3_SetPin                          ((uint32_t)0x0C000000)
#define GPT_OutputMode3_LowPulse                        ((uint32_t)0x10000000)

#define GPT_ForceOutput1_Enable                         ((uint32_t)0x20000000)
#define GPT_ForceOutput2_Enable                         ((uint32_t)0x40000000)
#define GPT_ForceOutput3_Enable                         ((uint32_t)0x80000000)


/* Input Capture management ***************************************************/
#define GPT_InputCapture1_Disabled                      ((uint32_t)0x00000000)
#define GPT_InputCapture1_RisingEdge                    ((uint32_t)0x00010000)
#define GPT_InputCapture1_FallingEdge                   ((uint32_t)0x00020000)
#define GPT_InputCapture1_BothEdge                      ((uint32_t)0x00030000)

#define GPT_InputCapture2_Disabled                      ((uint32_t)0x00000000)
#define GPT_InputCapture2_RisingEdge                    ((uint32_t)0x00040000)
#define GPT_InputCapture2_FallingEdge                   ((uint32_t)0x00080000)
#define GPT_InputCapture2_BothEdge                      ((uint32_t)0x000C0000)


/* Input Capture management ***************************************************/

#define GPT_IT_Rollover                                 ((uint32_t)0x00000020)
#define GPT_IT_IF2                                      ((uint32_t)0x00000010)
#define GPT_IT_IF1                                      ((uint32_t)0x00000008)
#define GPT_IT_OF3                                      ((uint32_t)0x00000004)
#define GPT_IT_OF2                                      ((uint32_t)0x00000002)
#define GPT_IT_OF1                                      ((uint32_t)0x00000001)


void GPT_Init(GPT_Type* GPTx, GPT_InitTypeDef* GPT_InitStruct);
void GPT_PrescalerConfig(GPT_Type* GPTx, uint16_t Prescaler);
uint32_t GPT_GetCounter(GPT_Type* GPTx);
void GPT_Cmd(GPT_Type* GPTx, FunctionalState NewState);

/* Output Compare management **************************************************/
void GPT_SetCompare1Value(GPT_Type* GPTx, uint32_t Compare1);
void GPT_SetCompare2Value(GPT_Type* GPTx, uint32_t Compare2);
void GPT_SetCompare3Value(GPT_Type* GPTx, uint32_t Compare3);

void GPT_SetCompare1Mode(GPT_Type* GPTx, uint32_t OCMode);
void GPT_SetCompare2Mode(GPT_Type* GPTx, uint32_t OCMode);
void GPT_SetCompare3Mode(GPT_Type* GPTx, uint32_t OCMode);

void GPT_ForcedOutputCmd(GPT_Type* GPTx, uint8_t channel, FunctionalState NewState);

/* Input Capture management ***************************************************/
void GPT_SetCapture1Mode(GPT_Type* GPTx, uint32_t IMMode);
void GPT_SetCapture2Mode(GPT_Type* GPTx, uint32_t IMMode);

uint32_t GPT_GetCapture1(GPT_Type* GPTx);
uint32_t GPT_GetCapture2(GPT_Type* GPTx);

/* Interrupts and flags management ***************************************/
void GPT_ITConfig(GPT_Type* GPTx, uint32_t GPT_IT, FunctionalState NewState);
ITStatus GPT_GetITStatus(GPT_Type* GPTx, uint32_t GPT_IT);
void GPT_ClearITPendingBit(GPT_Type* GPTx, uint32_t GPT_IT);


#endif // !__MY_GPT_H

源文件:

```源文件
#include "myGpt.h"

void GPT_Init(GPT_Type* GPTx, GPT_InitTypeDef* GPT_InitStruct){
    uint32_t reg=0x0;
    /* reset GPT peripheral */
    GPTx->CR = GPT_SoftWare_Reset;
    while( GPTx->CR & GPT_SoftWare_Reset){

    }
    /* configure clock */
    if( (GPT_InitStruct->GPT_Enable24MHz & GPT_CLK24MHz_Enable) != 0 ){   /* if use crystal OSI clock */
        reg |= GPT_CLK24MHz_Enable;
        if(GPT_InitStruct->GPT_Prescaler24M > 0xF){
            GPT_InitStruct->GPT_Prescaler24M = 0xF;
        }
        GPTx->PR = (GPT_InitStruct->GPT_Prescaler24M<<12);
    }
    else{
        /* set clock and prescaler */
        reg |= GPT_InitStruct->GPT_ClockSource;
        if(GPT_InitStruct->GPT_Prescaler > 0xFFF){
            GPT_InitStruct->GPT_Prescaler24M = 0xFFF;
        }
        GPTx->PR = GPT_InitStruct->GPT_Prescaler;
    }
    reg |= (GPT_InitStruct->GPT_Compare1Mode | GPT_InitStruct->GPT_EnableMode);
    GPTx->CR = reg;
}


void GPT_PrescalerConfig(GPT_Type* GPTx, uint16_t Prescaler){
    Prescaler = (Prescaler>0xFFF) ? 0xFFF : Prescaler;
    GPTx->PR = Prescaler;
}

uint32_t GPT_GetCounter(GPT_Type* GPTx){
    return GPTx->CNT;
}

void GPT_Cmd(GPT_Type* GPTx, FunctionalState NewState){
    if( NewState!=DISABLE ){
        GPTx->CR |= GPT_CR1_EN;
    }
    else{
        GPTx->CR &= ~GPT_CR1_EN;    
    }
}




/* Output Compare management **************************************************/
void GPT_SetCompare1Value(GPT_Type* GPTx, uint32_t Compare1){
    GPTx->OCR[0] = Compare1;
}

void GPT_SetCompare2Value(GPT_Type* GPTx, uint32_t Compare2){
    GPTx->OCR[1] = Compare2;
}

void GPT_SetCompare3Value(GPT_Type* GPTx, uint32_t Compare3){
    GPTx->OCR[2] = Compare3;
}



/**
  * @brief  Configure the GPTx output compare channel 1 mode.
  * @param  GPTx: where x can be 1 to 2.
  * @param  OCMode: specifies the mode to be set to the output compare.
  *          This parameter can be one of the following values:
  *            @arg GPT_OutputMode1_Disconnected: No response on pin.
  *            @arg GPT_OutputMode1_TogglePin: Toggle output pin.
  *            @arg GPT_OutputMode1_ClearPin: Clear output pin.
  *            @arg GPT_OutputMode1_SetPin:  Set output pin.
  *            @arg GPT_OutputMode1_LowPulse: generate an active low pulse on the output pin.
  * @retval NoneOC
  */
void GPT_SetCompare1Mode(GPT_Type* GPTx, uint32_t OCMode){
    GPTx->CR |= OCMode;
}

/**
  * @brief  Configure the GPTx output compare channel 2 mode.
  * @param  GPTx: where x can be 1 to 2.
  * @param  OCMode: specifies the mode to be set to the output compare.
  *          This parameter can be one of the following values:
  *            @arg GPT_OutputMode2_Disconnected: No response on pin.
  *            @arg GPT_OutputMode2_TogglePin: Toggle output pin.
  *            @arg GPT_OutputMode2_ClearPin: Clear output pin.
  *            @arg GPT_OutputMode2_SetPin:  Set output pin.
  *            @arg GPT_OutputMode2_LowPulse: generate an active low pulse on the output pin.
  * @retval NoneOC
  */
void GPT_SetCompare2Mode(GPT_Type* GPTx, uint32_t OCMode){
    GPTx->CR |= OCMode;
}

/**
  * @brief  Configure the GPTx output compare channel 3 mode.
  * @param  GPTx: where x can be 1 to 2.
  * @param  OCMode: specifies the mode to be set to the output compare.
  *          This parameter can be one of the following values:
  *            @arg GPT_OutputMode3_Disconnected: No response on pin.
  *            @arg GPT_OutputMode3_TogglePin: Toggle output pin.
  *            @arg GPT_OutputMode3_ClearPin: Clear output pin.
  *            @arg GPT_OutputMode3_SetPin:  Set output pin.
  *            @arg GPT_OutputMode3_LowPulse: generate an active low pulse on the output pin.
  * @retval NoneOC
  */
void GPT_SetCompare3Mode(GPT_Type* GPTx, uint32_t OCMode){
    GPTx->CR |= OCMode;
}

/**
  * @brief  Force output compare channel generate an action programmed by OMx.
  * @param  GPTx: where x can be 1 to 2.
  * @param  OCMode: specifies the mode to be set to the output compare.
  * @param  channel: specifies the channel.
  *          This parameter can be 1..3.
  * @param  NewState: enable or disable
  *         @arg ENABLE: generate.
  *         @arg DISABLE: don't generate.
  * @retval NoneOC
  */
void GPT_ForcedOutputCmd(GPT_Type* GPTx, uint8_t channel, FunctionalState NewState){
    if( NewState!=DISABLE ){
        switch(channel){
            case 1: GPTx->CR |= GPT_ForceOutput1_Enable; break;
            case 2: GPTx->CR |= GPT_ForceOutput2_Enable; break;
            case 3: GPTx->CR |= GPT_ForceOutput3_Enable; break;
            default: break;
        }
    }
    else{
        switch(channel){
            case 1: GPTx->CR &= ~GPT_ForceOutput1_Enable; break;
            case 2: GPTx->CR &= ~GPT_ForceOutput2_Enable; break;
            case 3: GPTx->CR &= ~GPT_ForceOutput3_Enable; break;
            default: break;
        }
    }
}



/* Input Capture management ***************************************************/
/**
  * @brief  Configure the GPTx input capture channel 1 mode.
  * @param  GPTx: where x can be 1 to 2.
  * @param  IMMode: specifies the mode to be set to the input capture.
  *          This parameter can be one of the following values:
  *            @arg GPT_InputCapture1_Disabled: capture disabled.
  *            @arg GPT_InputCapture1_RisingEdge: capture on rising edge only.
  *            @arg GPT_InputCapture1_FallingEdge: capture on falling edge only.
  *            @arg GPT_InputCapture1_BothEdge:  capture on both edge only.
  * @retval NoneOC
  */
void GPT_SetCapture1Mode(GPT_Type* GPTx, uint32_t IMMode){
    GPTx->CR |= IMMode;
}

/**
  * @brief  Configure the GPTx input capture channel 1 mode.
  * @param  GPTx: where x can be 1 to 2.
  * @param  IMMode: specifies the mode to be set to the input capture.
  *          This parameter can be one of the following values:
  *            @arg GPT_InputCapture2_Disabled: capture disabled.
  *            @arg GPT_InputCapture2_RisingEdge: capture on rising edge only.
  *            @arg GPT_InputCapture2_FallingEdge: capture on falling edge only.
  *            @arg GPT_InputCapture2_BothEdge:  capture on both edge only.
  * @retval NoneOC
  */
void GPT_SetCapture2Mode(GPT_Type* GPTx, uint32_t IMMode){
    GPTx->CR |= IMMode;
}

/**
  * @brief  Gets the GPTx Input Capture 1 value.
  * @param  GPTx: where x can be 1 to 2.
  * @retval Input Capture 1 Register value.
  */
uint32_t GPT_GetCapture1(GPT_Type* GPTx){
    return GPTx->ICR[0];
}

/**
  * @brief  Gets the GPTx Input Capture 2 value.
  * @param  GPTx: where x can be 1 to 2.
  * @retval Input Capture 2 Register value.
  */
uint32_t GPT_GetCapture2(GPT_Type* GPTx){
    return GPTx->ICR[1];
}


/**
  * @brief  Enables or disables the specified GPT interrupts.
  * @param  GPTx: where x can be 1 to 2 to select the GPTx peripheral.
  * @param  GPT_IT: specifies the GPT interrupts sources to be enabled or disabled.
  *          This parameter can be any combination of the following values:
  *            @arg GPT_IT_Rollover: GPT rollover Interrupt source
  *            @arg GPT_IT_IF2: GPT Input Capture 2 Interrupt source
  *            @arg GPT_IT_IF1: GPT Input Capture 1 Interrupt source
  *            @arg GPT_IT_OF3: GPT Output Compare 3 Interrupt source
  *            @arg GPT_IT_OF2: GPT Output Compare 2 Interrupt source
  *            @arg GPT_IT_OF1: GPT Output Compare 1 Interrupt source
  *  
  * @param  NewState: new state of the GPT interrupts.
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void GPT_ITConfig(GPT_Type* GPTx, uint32_t GPT_IT, FunctionalState NewState){
    if(NewState != DISABLE){
        /* Enanle the interrupt source */
        GPTx->IR |= GPT_IT;
    }
    else{
        /* Disable the interrupt source */
        GPTx->IR &= ~GPT_IT;
    }
}

/**
  * @brief  Checks whether the GPT interrupt has occurred or not.
  * @param  GPTx: where x can be 1 to 2 to select the GPT peripheral.
  * @param  GPT_IT: specifies the GPT interrupt source to check.
  *          This parameter can be one of the following values:
  *            @arg GPT_IT_IF2: GPT Input Capture 2 Interrupt source
  *            @arg GPT_IT_IF1: GPT Input Capture 1 Interrupt source
  *            @arg GPT_IT_OF3: GPT Output Compare 3 Interrupt source
  *            @arg GPT_IT_OF2: GPT Output Compare 2 Interrupt source
  *            @arg GPT_IT_OF1: GPT Output Compare 1 Interrupt source
  * 
  * @retval The new state of the GPT_IT(SET or RESET).
  */
ITStatus GPT_GetITStatus(GPT_Type* GPTx, uint32_t GPT_IT){
    ITStatus bitstatus = RESET;
    uint32_t itstatus = 0x0, itenable = 0x0;

    itstatus = GPTx->SR & GPT_IT;

    itenable = GPTx->IR & GPT_IT;
    if( (itstatus != (uint32_t)RESET) && (itenable) != (uint32_t)(RESET) ){
        bitstatus = SET;
    }
    else{
        bitstatus = RESET;
    }
    return bitstatus;
}

/**
  * @brief  Clears the GPTx's interrupt pending bits.
  * @param  GPTx: where x can be 1 to 2 to select the GPT peripheral.
  * @param  GPT_IT: specifies the pending bit to clear.
  *          This parameter can be any combination of the following values:
  *            @arg GPT_IT_IF2: GPT Input Capture 2 Interrupt source
  *            @arg GPT_IT_IF1: GPT Input Capture 1 Interrupt source
  *            @arg GPT_IT_OF3: GPT Output Compare 3 Interrupt source
  *            @arg GPT_IT_OF2: GPT Output Compare 2 Interrupt source
  *            @arg GPT_IT_OF1: GPT Output Compare 1 Interrupt source
  *  
  * @retval None
  */
void GPT_ClearITPendingBit(GPT_Type* GPTx, uint32_t GPT_IT){
    GPTx->SR |= GPT_IT;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值