1 #ifndef __SWM341_PWM_H__
2 #define __SWM341_PWM_H__
3 
4 
5 typedef struct {
6     uint8_t  Mode;          //PWM_EDGE_ALIGNED、PWM_CENTER_ALIGNED、PWM_ASYM_CENTER_ALIGNED
7 
8     uint16_t Clkdiv;        //1--1024
9 
10     uint16_t Period;        //周期
11 
12     uint16_t HdutyA;        //占空比,高电平时长;PWM_ASYM_CENTER_ALIGNED 模式下设定前半周期的高电平时长
13     uint16_t HdutyA2;       //只在 PWM_ASYM_CENTER_ALIGNED 模式下使用,用于设定后半周期的高电平时长
14     uint16_t DeadzoneA;     //死区时长,取值0--1023
15     uint8_t  IdleLevelA;    //停止状态下PWMxA输出电平
16     uint8_t  IdleLevelAN;   //停止状态下PWMxAN输出电平
17     uint8_t  OutputInvA;    //PWMxA输出取反
18     uint8_t  OutputInvAN;   //PWMxAN输出取反
19 
20     uint16_t HdutyB;
21     uint16_t HdutyB2;
22     uint16_t DeadzoneB;
23     uint8_t  IdleLevelB;
24     uint8_t  IdleLevelBN;
25     uint8_t  OutputInvB;
26     uint8_t  OutputInvBN;
27 
28     uint8_t  UpOvfIE;       //上升沿计数器溢出中断使能,PWM_EDGE_ALIGNED 模式下向上计数,因此只有上升沿
29     uint8_t  DownOvfIE;     //下降沿计数器溢出中断使能,另外两种模式下先向上计数、后向下计数,上升沿是前半周期、下降沿是后半周期
30     uint8_t  UpCmpAIE;      //上升沿比较器A匹配中断使能
31     uint8_t  DownCmpAIE;    //下降沿比较器A匹配中断使能
32     uint8_t  UpCmpBIE;      //上升沿比较器B匹配中断使能
33     uint8_t  DownCmpBIE;    //下降沿比较器B匹配中断使能
34 } PWM_InitStructure;
35 
36 
37 #define PWM_EDGE_ALIGNED          0
38 #define PWM_CENTER_ALIGNED        1
39 #define PWM_ASYM_CENTER_ALIGNED   2     // 非对称中心对齐模式,上升沿和下降沿具有不同的翻转点比较值
40 
41 
42 #define PWM_CH_A    0
43 #define PWM_CH_B    1
44 
45 #define PWM0_MSK    (1 << 0)
46 #define PWM1_MSK    (1 << 1)
47 #define PWM2_MSK    (1 << 2)
48 #define PWM3_MSK    (1 << 3)
49 #define PWM4_MSK    (1 << 4)
50 
51 #define PWM_BRK0    (1 << 0)
52 #define PWM_BRK1    (1 << 1)
53 #define PWM_BRK2    (1 << 2)
54 
55 #define PWM_DIR_UP      0
56 #define PWM_DIR_DOWN    1
57 
58 #define PWM_TRG_0       0       //计数器溢出或与指定值相等时 PWM 输出触发信号
59 #define PWM_TRG_1       1
60 #define PWM_TRG_2       2
61 #define PWM_TRG_3       3
62 #define PWM_TRG_4       4
63 #define PWM_TRG_5       5
64 #define PWM_TRG_6       6
65 #define PWM_TRG_7       7
66 
67 #define PWM_EVT_DIS     0       //外部事件信号为高时 PWM 启动计数、停止计数、暂停计数、屏蔽输出
68 #define PWM_EVT_0       1
69 #define PWM_EVT_1       2
70 #define PWM_EVT_2       3
71 #define PWM_EVT_3       4
72 #define PWM_EVT_4       5
73 #define PWM_EVT_TIMR0   6
74 #define PWM_EVT_TIMR1   7
75 
76 /* Interrupt Type */
77 #define PWM_IT_OVF_UP      (1 << 0)     //向上计数时计数器溢出,PWM_EDGE_ALIGNED 模式下向上计数,因此只有上升沿
78 #define PWM_IT_OVF_DOWN    (1 << 1)     //向下计数时计数器溢出,另外两种模式下先向上计数、后向下计数,上升沿是前半周期、下降沿是后半周期
79 #define PWM_IT_CMPA_UP     (1 << 2)     //向上计数时计数器值与CMPA相等
80 #define PWM_IT_CMPB_UP     (1 << 3)     //向上计数时计数器值与CMPB相等
81 #define PWM_IT_CMPA_DOWN   (1 << 4)     //向下计数时计数器值与CMPA相等
82 #define PWM_IT_CMPB_DOWN   (1 << 5)     //向下计数时计数器值与CMPB相等
83 
84 #define PWM_BRKIT_BRK0     (1 << 0)     //PWM_BRK0引脚上出现刹车信号
85 #define PWM_BRKIT_BRK1     (1 << 1)
86 #define PWM_BRKIT_BRK2     (1 << 2)
87 
88 
89 
90 void PWM_Init(PWM_TypeDef * PWMx, PWM_InitStructure * initStruct);          //PWM初始化
91 void PWM_Start(uint32_t pwm);                                               //启动PWM,开始PWM输出
92 void PWM_Stop(uint32_t pwm);                                                //关闭PWM,停止PWM输出
93 void PWM_Restart(uint32_t pwm);
94 
95 void PWM_ReloadEn(uint32_t pwm);
96 void PWM_ReloadDis(uint32_t pwm);
97 
98 void PWM_BrkInPolarity(uint32_t brk, uint32_t level);
99 void PWM_BrkConfig(PWM_TypeDef * PWMx, uint32_t chn, uint32_t brk, uint32_t out, uint32_t outN, uint32_t outHold, uint32_t stpCount);
100 
101 void PWM_OvfTrigger(PWM_TypeDef * PWMx, uint32_t en_up, uint32_t en_down, uint32_t trig_chn);
102 void PWM_CmpTrigger(PWM_TypeDef * PWMx, uint16_t match, uint32_t dir, uint32_t width, uint32_t trig_chn, uint32_t adc_trig_pos);
103 
104 void PWM_OutMask(PWM_TypeDef * PWMx, uint32_t chn, uint32_t evt, uint32_t out, uint32_t evt_n, uint32_t out_n);
105 
106 void PWM_SetPeriod(PWM_TypeDef * PWMx, uint16_t period);                    //设置周期
107 uint16_t PWM_GetPeriod(PWM_TypeDef * PWMx);                                 //获取周期
108 void PWM_SetHDuty(PWM_TypeDef * PWMx, uint32_t chn, uint16_t hduty);        //设置高电平时长
109 uint16_t PWM_GetHDuty(PWM_TypeDef * PWMx, uint32_t chn);                    //获取高电平时长
110 void PWM_SetHDuty2(PWM_TypeDef * PWMx, uint32_t chn, uint16_t hduty, uint16_t hduty2);      //设置高电平时长,用于非对称中心对齐模式
111 void PWM_GetHDuty2(PWM_TypeDef * PWMx, uint32_t chn, uint16_t *hduty, uint16_t *hduty2);    //获取高电平时长,用于非对称中心对齐模式
112 void PWM_SetDeadzone(PWM_TypeDef * PWMx, uint32_t chn, uint16_t deadzone);  //设置死区时长
113 uint16_t PWM_GetDeadzone(PWM_TypeDef * PWMx, uint32_t chn);                 //获取死区时长
114 
115 void PWM_IntEn(PWM_TypeDef * PWMx, uint32_t it);                            //中断使能
116 void PWM_IntDis(PWM_TypeDef * PWMx, uint32_t it);                           //中断禁能
117 void PWM_IntClr(PWM_TypeDef * PWMx, uint32_t it);                           //中断标志清除
118 uint32_t PWM_IntStat(PWM_TypeDef * PWMx, uint32_t it);                      //中断标志查询
119 void PWM_BrkIntEn(uint32_t brkit);                                          //刹车中断使能
120 void PWM_BrkIntDis(uint32_t brkit);                                         //刹车中断禁能
121 void PWM_BrkIntClr(uint32_t brkit);                                         //刹车中断标志清除
122 uint32_t PWM_BrkIntStat(uint32_t brkit);                                    //刹车中断标志查询
123 
124 
125 #endif //__SWM341_PWM_H__
126