123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- #include "ap_cp.h"
- #include "clock.h"
- #include "pwm.h"
- #include "gpio.h"
- #include "pwrmgr.h"
- #include "common.h"
- void hal_pwm_init(
- PWMN_e pwmN,
- PWM_CLK_DIV_e pwmDiv,
- PWM_CNT_MODE_e pwmMode,
- PWM_POLARITY_e pwmPolarity)
- {
- clk_gate_enable(MOD_PWM);
- PWM_DISABLE_CH(pwmN);
-
- PWM_SET_DIV(pwmN, pwmDiv);
- PWM_SET_MODE(pwmN, pwmMode);
- PWM_SET_POL(pwmN, pwmPolarity);
-
-
- hal_pwrmgr_register(MOD_PWM, NULL, NULL);
- }
- void hal_pwm_open_channel(PWMN_e pwmN,GPIO_Pin_e pwmPin){
- hal_gpio_fmux_set(pwmPin, (Fmux_Type_e)(PWM0 + pwmN));
- PWM_ENABLE_CH(pwmN);
- }
- void hal_pwm_close_channel(PWMN_e pwmN){
-
- PWM_DISABLE_CH(pwmN);
-
- }
- void hal_pwm_destroy(PWMN_e pwmN){
-
- PWM_DISABLE_CH(pwmN);
- PWM_NO_LOAD_CH(pwmN);
- PWM_NO_INSTANT_LOAD_CH(pwmN);
-
- PWM_SET_DIV(pwmN, 0);
- PWM_SET_MODE(pwmN, 0);
- PWM_SET_POL(pwmN, 0);
-
- PWM_SET_TOP_VAL(pwmN, 0);
- PWM_SET_CMP_VAL(pwmN, 0);
- }
- void hal_pwm_set_count_val(PWMN_e pwmN, uint16_t cmpVal, uint16_t cntTopVal){
- PWM_NO_LOAD_CH(pwmN);
- PWM_SET_CMP_VAL(pwmN, cmpVal);
- PWM_SET_TOP_VAL(pwmN, cntTopVal);
- PWM_LOAD_CH(pwmN);
- }
- static unsigned int pwm_en = 0;
- void hal_pwm_start(void)
- {
- if(pwm_en == 0)
- {
- hal_pwrmgr_lock(MOD_PWM);
- PWM_ENABLE_ALL;
- pwm_en = 1;
- }
- }
- void hal_pwm_stop(void)
- {
- if(pwm_en == 1)
- {
- hal_pwrmgr_unlock(MOD_PWM);
- PWM_DISABLE_ALL;
- pwm_en = 0;
- }
- }
- typedef struct {
- bool enable;
- bool ch_en[6];
- pwm_ch_t ch[6];
- }pwm_Ctx_t;
- static pwm_Ctx_t pwmCtx =
- {
- .enable = FALSE,
- .ch_en = {FALSE,FALSE,FALSE,FALSE,FALSE,FALSE},
- };
- void hal_pwm_module_init(void)
- {
- int i = 0;
-
- if(pwmCtx.enable == TRUE)
- return;
- pwmCtx.enable = TRUE;
-
- for(i = 0;i < 6;i++)
- {
- pwmCtx.ch_en[i] = FALSE;
-
- pwmCtx.ch[i].pwmN = (PWMN_e)i;
- pwmCtx.ch[i].pwmPin = GPIO_DUMMY;
- pwmCtx.ch[i].pwmDiv = PWM_CLK_NO_DIV;
- pwmCtx.ch[i].pwmMode = PWM_CNT_UP;
- pwmCtx.ch[i].pwmPolarity = PWM_POLARITY_RISING;
- pwmCtx.ch[i].cmpVal = 0;
- pwmCtx.ch[i].cntTopVal = 0;
- }
-
- hal_pwm_stop();
- }
- void hal_pwm_module_deinit(void)
- {
- int i = 0;
-
- if(pwmCtx.enable == FALSE)
- return;
- pwmCtx.enable = FALSE;
-
- for(i = 0;i < 6;i++)
- {
- pwmCtx.ch_en[i] = FALSE;
-
- pwmCtx.ch[i].pwmN = (PWMN_e)i;
- pwmCtx.ch[i].pwmPin = GPIO_DUMMY;
- pwmCtx.ch[i].pwmDiv = PWM_CLK_NO_DIV;
- pwmCtx.ch[i].pwmMode = PWM_CNT_UP;
- pwmCtx.ch[i].pwmPolarity = PWM_POLARITY_RISING;
- pwmCtx.ch[i].cmpVal = 0;
- pwmCtx.ch[i].cntTopVal = 0;
-
- hal_pwm_close_channel((PWMN_e)i);
- hal_pwm_destroy((PWMN_e)i);
- }
-
- hal_pwm_stop();
- }
- void hal_pwm_ch_start(pwm_ch_t ch)
- {
- if(pwmCtx.enable == FALSE)
- return;
-
- if(pwmCtx.ch_en[ch.pwmN] == TRUE)
- {
- hal_pwm_set_count_val(ch.pwmN,ch.cmpVal,ch.cntTopVal);
- }
- else
- {
- hal_pwm_init(ch.pwmN,ch.pwmDiv,ch.pwmMode,ch.pwmPolarity);
- hal_pwm_set_count_val(ch.pwmN,ch.cmpVal,ch.cntTopVal);
- hal_pwm_open_channel(ch.pwmN,ch.pwmPin);
- pwmCtx.ch_en[ch.pwmN] = TRUE;
- hal_pwm_start();
- }
- }
- void hal_pwm_ch_stop(pwm_ch_t ch)
- {
- if(pwmCtx.ch_en[ch.pwmN] == FALSE)
- return;
- else
- {
- hal_pwm_stop();
- pwmCtx.ch_en[ch.pwmN] = FALSE;
- hal_pwm_destroy(ch.pwmN);
- hal_pwm_close_channel(ch.pwmN);
- }
- }
- bool hal_pwm_ch_enable(PWMN_e pwmN)
- {
- return pwmCtx.ch_en[pwmN];
- }
- pwm_ch_t hal_pwm_ch_reg(PWMN_e pwmN)
- {
- return pwmCtx.ch[pwmN];
- }
|