123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- /**
- * @file pwm.h
- * @author chipsea
- * @brief
- * @version 0.1
- * @date 2020-11-30
- * @copyright Copyright (c) 2020, CHIPSEA Co., Ltd.
- * @note
- */
- /*******************************************************************************
- * @file pwm.h
- * @brief Contains all functions support for pwm driver
- * @version 0.0
- * @date 30. Oct. 2017
- * @author Ding
- *
- *
- *******************************************************************************/
- #ifndef __PWM__H__
- #define __PWM__H__
- #ifdef __cplusplus
- extern "C" {
- #endif
- #include "types.h"
- #include "gpio.h"
- #define PWM_ENABLE_ALL do{\
- AP_PWM->pwmen |= BIT(0);\
- AP_PWM->pwmen |= BIT(4);\
- }while(0)
- #define PWM_DISABLE_ALL do{\
- AP_PWM->pwmen &= ~BIT(0);\
- AP_PWM->pwmen &= ~BIT(4);\
- }while(0)
- #define PWM_ENABLE_CH_012 do{\
- AP_PWM->pwmen |= BIT(8);\
- AP_PWM->pwmen |= BIT(9);\
- }while(0)
- #define PWM_DISABLE_CH_012 do{\
- AP_PWM->pwmen &= ~BIT(8);\
- AP_PWM->pwmen &= ~BIT(9);\
- }while(0)
- #define PWM_ENABLE_CH_345 do{\
- AP_PWM->pwmen |= BIT(10);\
- AP_PWM->pwmen |= BIT(11);\
- }while(0)
- #define PWM_DISABLE_CH_345 do{\
- AP_PWM->pwmen &= ~BIT(10);\
- AP_PWM->pwmen &= ~BIT(11);\
- }while(0)
- #define PWM_ENABLE_CH_01 do{\
- AP_PWM->pwmen |= BIT(12);\
- AP_PWM->pwmen |= BIT(13);\
- }while(0)
- #define PWM_DISABLE_CH_01 do{\
- AP_PWM->pwmen &= ~BIT(12);\
- AP_PWM->pwmen &= ~BIT(13);\
- }while(0)
- #define PWM_ENABLE_CH_23 do{\
- AP_PWM->pwmen |= BIT(14);\
- AP_PWM->pwmen |= BIT(15);\
- }while(0)
- #define PWM_DISABLE_CH_23 do{\
- AP_PWM->pwmen &= ~BIT(14);\
- AP_PWM->pwmen &= ~BIT(15);\
- }while(0)
- #define PWM_ENABLE_CH_45 do{\
- AP_PWM->pwmen |= BIT(16);\
- AP_PWM->pwmen |= BIT(17);\
- }while(0)
- #define PWM_DISABLE_CH_45 do{\
- AP_PWM->pwmen &= ~BIT(16);\
- AP_PWM->pwmen &= ~BIT(17);\
- }while(0)
- #define PWM_INSTANT_LOAD_CH(n) subWriteReg(&(AP_PWM_CTRL(n)->ctrl0),31,31,1)
- #define PWM_NO_INSTANT_LOAD_CH(n) subWriteReg(&(AP_PWM_CTRL(n)->ctrl0),31,31,0)
- #define PWM_LOAD_CH(n) subWriteReg(&(AP_PWM_CTRL(n)->ctrl0),16,16,1)
- #define PWM_NO_LOAD_CH(n) subWriteReg(&(AP_PWM_CTRL(n)->ctrl0),16,16,0)
- #define PWM_SET_DIV(n,v) subWriteReg(&(AP_PWM_CTRL(n)->ctrl0),14,12,v)
- #define PWM_SET_MODE(n,v) subWriteReg(&(AP_PWM_CTRL(n)->ctrl0),8,8,v)
- #define PWM_SET_POL(n,v) subWriteReg(&(AP_PWM_CTRL(n)->ctrl0),4,4,v)
- #define PWM_ENABLE_CH(n) subWriteReg(&(AP_PWM_CTRL(n)->ctrl0),0,0,1)
- #define PWM_DISABLE_CH(n) subWriteReg(&(AP_PWM_CTRL(n)->ctrl0),0,0,0)
-
- #define PWM_SET_CMP_VAL(n,v) subWriteReg(&(AP_PWM_CTRL(n)->ctrl1),31,16,v)
- #define PWM_SET_TOP_VAL(n,v) subWriteReg(&(AP_PWM_CTRL(n)->ctrl1),15,0,v)
- #define PWM_GET_CMP_VAL(n) ((AP_PWM_CTRL(n)->ctrl1 & 0xFFFF0000) >> 8)
- #define PWM_GET_TOP_VAL(n) AP_PWM_CTRL(n)->ctrl1 & 0x0000FFFF
- /*************************************************************
- * @brief enum variable, the number of PWM channels supported
- *
- */
- typedef enum{
- PWM_CH0 = 0,
- PWM_CH1 = 1,
- PWM_CH2 = 2,
- PWM_CH3 = 3,
- PWM_CH4 = 4,
- PWM_CH5 = 5
- }PwmCh_t;
- /*************************************************************
- * @brief enum variable used for PWM clock prescaler
- *
- */
- typedef enum{
- PWM_CLK_NO_DIV = 0,
- PWM_CLK_DIV_2 = 1,
- PWM_CLK_DIV_4 = 2,
- PWM_CLK_DIV_8 = 3,
- PWM_CLK_DIV_16 = 4,
- PWM_CLK_DIV_32 = 5,
- PWM_CLK_DIV_64 = 6,
- PWM_CLK_DIV_128 = 7
- }PwmClkDiv_t;
- /*************************************************************
- * @brief enum variable used for PWM work mode setting
- *
- */
- typedef enum{
- PWM_CNT_UP = 0, //!<!< pwm mode up
- PWM_CNT_UP_AND_DOWN = 1 //!< pwm mode up and down
- }PwmMode_t;
- /*************************************************************
- * @brief enum variable used for PWM output polarity setting
- *
- */
- typedef enum{
- PWM_POLARITY_RISING = 0, //!< pwm polarity rising
- PWM_POLARITY_FALLING = 1 //!< pwm polarity falling
- }PwmPolarity_t;
- /**************************************************************************************
- * @fn HalPwmInit
- *
- * @brief This function process for pwm initial
- *
- * input parameters
- *
- * @param PwmCh_t pwmN : pwm channel
- * PwmClkDiv_t pwmDiv : clock prescaler of PWM channel
- * PwmMode_t pwmMode : count mode of PWM channel
- * PwmPolarity_t pwmPolarity : output polarity setting of PWM channel
- * unsigned short cmpVal : the compare value of PWM channel
- * unsigned short cntTopVal : the counter top value of PWM channel
- *
- * output parameters
- *
- * @param None.
- *
- * @return None.
- **************************************************************************************/
- void HalPwmInit(PwmCh_t pwmN, PwmClkDiv_t pwmDiv,
- PwmMode_t pwmMode, PwmPolarity_t pwmPolarity);
- /**************************************************************************************
- * @fn HalPwmOpenChannel
- *
- * @brief This function process for pwm start working
- *
- * input parameters
- *
- * @param PwmCh_t pwmN : pwm channel
- * GpioPin_t pwmPin : pwm pin number
- *
- * output parameters
- *
- * @param None.
- *
- * @return None.
- **************************************************************************************/
- void HalPwmOpenChannel(PwmCh_t pwmN,GpioPin_t pwmPin);
- /**************************************************************************************
- * @fn HalPwmCloseChannel
- *
- * @brief This function process for pwm stop working
- *
- * input parameters
- *
- * @param PwmCh_t pwmN : pwm channel
- *
- * output parameters
- *
- * @param None.
- *
- * @return None.
- **************************************************************************************/
- void HalPwmCloseChannel(PwmCh_t pwmN);
- /**************************************************************************************
- * @fn HalPwmDestroy
- *
- * @brief This function process for pwm clear and disable
- *
- * input parameters
- *
- * @param PwmCh_t pwmN : pwm channel
- *
- * output parameters
- *
- * @param None.
- *
- * @return None.
- **************************************************************************************/
- void HalPwmDestroy(PwmCh_t pwmN);
- /**************************************************************************************
- * @fn HalPwmSetCountVal
- *
- * @brief This function process for change pwm count value
- *
- * input parameters
- *
- * @param PwmCh_t pwmN : pwm channel
- * uint16_t cmpVal : the compare value of PWM channel
- * uint16_t cntTopVal : the counter top value of PWM channel
- *
- * output parameters
- *
- * @param None.
- *
- * @return None.
- **************************************************************************************/
- void HalPwmSetCountVal(PwmCh_t pwmN, uint16_t cmpVal, uint16_t cntTopVal);
- /**************************************************************************************
- * @fn HalPwmStart
- *
- * @brief pwm start
- *
- * input parameters
- *
- * @param None.
- *
- * output parameters
- *
- * @param None.
- *
- * @return None.
- **************************************************************************************/
- void HalPwmStart(void);
- /**************************************************************************************
- * @fn HalPwmStop
- *
- * @brief pwm stop
- *
- * input parameters
- *
- * @param None.
- *
- * output parameters
- *
- * @param None.
- *
- * @return None.
- **************************************************************************************/
- void HalPwmStop(void);
- //new api,make use easily
- typedef struct
- {
- PwmCh_t pwmN;
- GpioPin_t pwmPin;
- PwmClkDiv_t pwmDiv;
- PwmMode_t pwmMode;
- PwmPolarity_t pwmPolarity;
- uint16_t cmpVal;
- uint16_t cntTopVal;
-
- } PwmCfg_t;
- /**************************************************************************************
- * @fn HalPwmModuleInit
- *
- * @brief init pwm global variables
- *
- * input parameters
- *
- * @param None.
- *
- * output parameters
- *
- * @param None.
- *
- * @return None.
- **************************************************************************************/
- void HalPwmModuleInit(void);
- /**************************************************************************************
- * @fn HalPwmModuleDeinit
- *
- * @brief deinit pwm global variables
- *
- * input parameters
- *
- * @param None.
- *
- * output parameters
- *
- * @param None.
- *
- * @return None.
- **************************************************************************************/
- void HalPwmModuleDeinit(void);
- /**************************************************************************************
- * @fn HalPwmChannelStart
- *
- * @brief config and make a pwm start to work
- *
- * input parameters
- *
- * @param PwmCfg_t ch: pwm channel
- *
- * output parameters
- *
- * @param None.
- *
- * @return None.
- **************************************************************************************/
- void HalPwmChannelStart(PwmCfg_t ch);
- /**************************************************************************************
- * @fn HalPwmChannelStop
- *
- * @brief make a pwm stop form working
- *
- * input parameters
- *
- * @param PwmCfg_t ch: pwm channel
- *
- * output parameters
- *
- * @param None.
- *
- * @return None.
- **************************************************************************************/
- void HalPwmChannelStop(PwmCh_t pwmN);
- #ifdef __cplusplus
- }
- #endif
- #endif
|