hsk-libs-dev  270
High Speed Karlsruhe XC878 library collection
hsk_pwm.h
Go to the documentation of this file.
1 /** \file
2  * HSK Pulse Width Modulation headers
3  *
4  * This file provides function prototypes to perform Timer T12 and T13
5  * based PWM with CCU6.
6  *
7  * The CCU6 offers the following PWM channels:
8  * - PWM_60
9  * - PWM_61
10  * - PWM_62
11  * - PWM_63
12  *
13  * Each PWM channel is connected to two IO channels for output:
14  * - PWM_CCx
15  * - PWM_COUTx
16  *
17  * The distinction between PWM and IO channels is important to understand
18  * the side effects of some operations.
19  *
20  * Refer to the PWM_OUT_x_* defines to know which channel can be connected to
21  * which output pins.
22  *
23  * The functions are implemented under the assumption, that the use of
24  * the timers T12 and T13 as well of the CCU6 is exclusive to this library.
25  *
26  * The safe boot order for pwm output is the following:
27  * - hsk_pwm_init()
28  * - hsk_pwm_enable()
29  * - hsk_pwm_port_open()
30  *
31  * @author kami
32  */
33 
34 #ifndef _HSK_PWM_H_
35 #define _HSK_PWM_H_
36 
37 /**
38  * Type definition for PWM channels.
39  */
40 typedef ubyte hsk_pwm_channel;
41 
42 /**
43  * PWM channel 60, Timer T12 driven.
44  */
45 #define PWM_60 0
46 
47 /**
48  * PWM channel 61, Timer T12 driven.
49  */
50 #define PWM_61 1
51 
52 /**
53  * PWM channel 62, Timer T12 driven.
54  */
55 #define PWM_62 2
56 
57 /**
58  * PWM channel 63, Timer T13 driven.
59  */
60 #define PWM_63 3
61 
62 /**
63  * Type definition for output channels.
64  */
65 typedef ubyte hsk_pwm_outChannel;
66 
67 /**
68  * IO channel configuration for PWM_60.
69  */
70 #define PWM_CC60 0
71 
72 /**
73  * Output channel configuration for PWM_60.
74  */
75 #define PWM_COUT60 1
76 
77 /**
78  * IO channel configuration for PWM_61.
79  */
80 #define PWM_CC61 2
81 
82 /**
83  * Output channel configuration for PWM_61.
84  */
85 #define PWM_COUT61 3
86 
87 /**
88  * IO channel configuration for PWM_62.
89  */
90 #define PWM_CC62 4
91 
92 /**
93  * Output channel configuration for PWM_62,
94  */
95 #define PWM_COUT62 5
96 
97 /**
98  * Output channel configuration for PWM_63.
99  */
100 #define PWM_COUT63 6
101 
102 /**
103  * Type definition for ports.
104  */
105 typedef ubyte hsk_pwm_port;
106 
107 /**
108  * PWM_60 output configuration for P3.0 through PWM_CC60.
109  */
110 #define PWM_OUT_60_P30 0
111 
112 /**
113  * PWM_60 output configuration for P3.1 through PWM_COUT60.
114  */
115 #define PWM_OUT_60_P31 1
116 
117 /**
118  * PWM_60 output configuration for P4.0 through PWM_CC60.
119  */
120 #define PWM_OUT_60_P40 2
121 
122 /**
123  * PWM_60 output configuration for P4.1 through PWM_COUT60.
124  */
125 #define PWM_OUT_60_P41 3
126 
127 /**
128  * PWM_61 output configuration for P0.0 through PWM_CC61.
129  */
130 #define PWM_OUT_61_P00 4
131 
132 /**
133  * PWM_61 output configuration for P0.1 through PWM_COUT61.
134  */
135 #define PWM_OUT_61_P01 5
136 
137 /**
138  * PWM_61 output configuration for P3.1 through PWM_CC61.
139  */
140 #define PWM_OUT_61_P31 6
141 
142 /**
143  * PWM_61 output configuration for P3.2 through PWM_CC61.
144  */
145 #define PWM_OUT_61_P32 7
146 
147 /**
148  * PWM_61 output configuration for P3.3 through PWM_COUT61.
149  */
150 #define PWM_OUT_61_P33 8
151 
152 /**
153  * PWM_61 output configuration for P4.4 through PWM_CC61.
154  */
155 #define PWM_OUT_61_P44 9
156 
157 /**
158  * PWM_61 output configuration for P4.5 through PWM_COUT61.
159  */
160 #define PWM_OUT_61_P45 10
161 
162 /**
163  * PWM_62 output configuration for P0.4 through PWM_CC62.
164  */
165 #define PWM_OUT_62_P04 11
166 
167 /**
168  * PWM_62 output configuration for P0.5 through PWM_COUT62.
169  */
170 #define PWM_OUT_62_P05 12
171 
172 /**
173  * PWM_62 output configuration for P3.4 through PWM_CC62.
174  */
175 #define PWM_OUT_62_P34 13
176 
177 /**
178  * PWM_62 output configuration for P3.5 through PWM_COUT62.
179  */
180 #define PWM_OUT_62_P35 14
181 
182 /**
183  * PWM_62 output configuration for P4.6 through PWM_CC62.
184  */
185 #define PWM_OUT_62_P46 15
186 
187 /**
188  * PWM_62 output configuration for P4.7 through PWM_COUT62.
189  */
190 #define PWM_OUT_62_P47 16
191 
192 /**
193  * PWM_63 output configuration for P0.3 through PWM_COUT63.
194  */
195 #define PWM_OUT_63_P03 17
196 
197 /**
198  * PWM_63 output configuration for P3.7 through PWM_COUT63.
199  */
200 #define PWM_OUT_63_P37 18
201 
202 /**
203  * PWM_63 output configuration for P4.3 through PWM_COUT63.
204  */
205 #define PWM_OUT_63_P43 19
206 
207 /**
208  * Sets up the the CCU6 timer frequencies that control the PWM
209  * cycle.
210  *
211  * The channels PWM_60, PWM_61 and PWM_62 share the timer T12, thus
212  * initializing one of them, initializes them all.
213  * The channel PWM_63 has exclusive use of the timer T13 and can thus be used
214  * with its own operating frequency.
215  *
216  * Frequencies up to ~732.4Hz are always between 15 and 16 bits precision.
217  *
218  * Frequencies above 48kHz offer less than 1/1000 precision.
219  * From there it is a linear function, i.e. 480kHz still offer 1/100
220  * precision.
221  *
222  * The freq value 0 will result in ~0.02Hz (\f$48000000 / 2^{31}\f$).
223  *
224  * The following formula results in the freq value that yields exactly the
225  * desired precision, this is useful to avoid precision loss by rounding:
226  * \f[freq(precision) = 480000000 * precision\f]
227  *
228  * E.g. 10 bit precision: \f$freq(1/2^{10}) = 468750\f$
229  *
230  * @param channel
231  * The channel to change the frequency for
232  * @param freq
233  * The desired PWM cycle frequency in units of 0.1Hz
234  */
235 void hsk_pwm_init(const hsk_pwm_channel channel, const ulong freq);
236 
237 /**
238  * Set up a PWM output port.
239  *
240  * This configures the necessary port direction bits and activates the
241  * corresponding output channels.
242  *
243  * The port can be any one of the PWM_OUT_x_* defines.
244  *
245  * @pre
246  * This function should only be called after hsk_pwm_enable(), otherwise
247  * the output port will be driven (1) until PWM is enabled
248  * @param port
249  * The output port to activate
250  */
251 void hsk_pwm_port_open(const hsk_pwm_port port);
252 
253 /**
254  * Close a PWM output port.
255  *
256  * This configures the necessary port direction bits.
257  *
258  * The port can be any one of the PWM_OUT_x_* defines.
259  *
260  * @param port
261  * The output port to deactivate
262  */
263 void hsk_pwm_port_close(const hsk_pwm_port port);
264 
265 /**
266  * Set the duty cycle for the given channel.
267  *
268  * I.e. the active time frame slice of period can be set with max and value.
269  *
270  * To set the duty cycle in percent specify a max of 100 and values from 0 to
271  * 100.
272  *
273  * @param channel
274  * The PWM channel to set the duty cycle for, check the PWM_6x defines
275  * @param max
276  * Defines the scope value can move in
277  * @param value
278  * The current duty cycle value
279  */
280 void hsk_pwm_channel_set(const hsk_pwm_channel channel,
281  const uword max, const uword value);
282 
283 /**
284  * Set the direction of an output channel.
285  *
286  * The channel value can be taken from any of the PWM_CCx/PWM_COUTx defines.
287  *
288  * @param channel
289  * The IO channel to set the direction bit for
290  * @param up
291  * Set 1 to output a 1 during the cycle set with hsk_pwm_channel_set(),
292  * set 0 to output a 0 during the cycle set with hsk_pwm_channel_set()
293  */
295  const bool up);
296 
297 /**
298  * Turns on the CCU6.
299  *
300  * Deactivates the power disable mode and sets the T12 and T13 Timer Run bits.
301  *
302  * @pre
303  * All hsk_pwm_init() calls have to be completed to call this
304  */
305 void hsk_pwm_enable(void);
306 
307 /**
308  * Deactivates the CCU6 to reduce power consumption.
309  */
310 void hsk_pwm_disable(void);
311 
312 #endif /* _HSK_PWM_H_ */
void hsk_pwm_channel_set(const hsk_pwm_channel channel, const uword max, const uword value)
Set the duty cycle for the given channel.
Definition: hsk_pwm.c:354
void hsk_pwm_enable(void)
Turns on the CCU6.
Definition: hsk_pwm.c:424
void hsk_pwm_disable(void)
Deactivates the CCU6 to reduce power consumption.
Definition: hsk_pwm.c:436
void hsk_pwm_port_open(const hsk_pwm_port port)
Set up a PWM output port.
Definition: hsk_pwm.c:241
ubyte hsk_pwm_port
Type definition for ports.
Definition: hsk_pwm.h:105
void hsk_pwm_outChannel_dir(hsk_pwm_outChannel channel, const bool up)
Set the direction of an output channel.
Definition: hsk_pwm.c:396
void hsk_pwm_port_close(const hsk_pwm_port port)
Close a PWM output port.
Definition: hsk_pwm.c:294
ubyte hsk_pwm_channel
Type definition for PWM channels.
Definition: hsk_pwm.h:40
void hsk_pwm_init(const hsk_pwm_channel channel, const ulong freq)
Sets up the the CCU6 timer frequencies that control the PWM cycle.
Definition: hsk_pwm.c:80
ubyte hsk_pwm_outChannel
Type definition for output channels.
Definition: hsk_pwm.h:65