##// END OF EJS Templates
PWM library added
kaveh -
r90:dc25ea8a495c dev_alexis
parent child
Show More
@@ -0,0 +1,340
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the libuc, microcontroler library
3 -- Copyright (C) 2014, Kaveh Mohamadabadi
4 --
5 -- This program is free software; you can redistribute it and/or modify
6 -- it under the terms of the GNU General Public License as published by
7 -- the Free Software Foundation; either version 3 of the License, or
8 -- (at your option) any later version.
9 --
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
14 --
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------
19 -- Author : Kaveh Mohamadabadi
20 -- Mail : kaveh.mohamadabadi@gmail.com
21 -------------------------------------------------------------------------------*/
22
23 /*! \file pwm.h
24 \brief PWM api.
25
26 The pwm api gives you a standard way to drive any pwm pin on any processor.
27 With this api you will be able to open and configure your PWM module, define the
28 associated pins when applicable and set the duty cycle and frequency. Note that depending
29 on the target architecture you can have soft or hard PWM.
30
31 A simple example to configure and use PWM4_CH4 on STM32 discovery for example.
32 \code
33 // lets open PWM4_CH4
34 //config :
35 // -TIMER = TIMER4 (PWM4=TIMER4)
36 // -PWM channel = 4
37 // -PWM output = PD15
38 // -PWM frequency = 1kHz
39 // -PWM duty cycle = 40.5%
40 pwmopen(PWM4_CH4,PD15);
41 int error = pwmsetconfig(PWM4_CH4,1000,40.5);
42 //check that everything is ok
43 if(error!=-1) //=> no error
44 {
45 //to change duty cycle to 15% for example:
46 pwmsetdutycycle(PWM4_CH4,15.0);
47 //you can also change the frequency:
48 pwmsetfrequency(PWM4_CH4,455000);
49 }
50
51 \endcode
52
53 */
54 #ifndef PWM_H
55 #define PWM_H
56
57 #include <stdint.h>
58 #ifdef __cplusplus
59 extern "C" {
60 #endif
61 extern int pwmopen(int PWM,uint32_t pin);
62 extern int pwmsetconfig(int PWM,uint32_t freq,float dutyCycle);
63 extern int pwmsetdutycycle(int PWM,float dutyCycle);
64 extern int pwmsetfrequency(int PWM,uint32_t freq);
65
66 #ifdef __cplusplus
67 }
68 #endif
69
70 #ifndef DOXYGEN_SHOULD_SKIP_THIS
71 #define UARTPARITYMASK 0x3
72 #define UARTBITSMASK 0xC
73 #define UARTSTOPBITSMASK 0x70
74
75 typedef enum
76 {
77 channel1 = 0,
78 channel2 = 1,
79 channel3 = 2,
80 channel4 = 3,
81 }channelx;
82
83 #define PWM1_CH1 ((0 << 8) + 0 )
84 #define PWM1_CH2 ((0 << 8) + 1 )
85 #define PWM1_CH3 ((0 << 8) + 2 )
86 #define PWM1_CH4 ((0 << 8) + 3 )
87 #define PWM1_CH5 ((0 << 8) + 4 )
88 #define PWM1_CH6 ((0 << 8) + 5 )
89 #define PWM1_CH7 ((0 << 8) + 6 )
90 #define PWM1_CH8 ((0 << 8) + 7 )
91 #define PWM1_CH9 ((0 << 8) + 8 )
92 #define PWM1_CH10 ((0 << 8) + 9 )
93 #define PWM1_CH11 ((0 << 8) + 10 )
94 #define PWM1_CH12 ((0 << 8) + 11 )
95 #define PWM1_CH13 ((0 << 8) + 12 )
96 #define PWM1_CH14 ((0 << 8) + 13 )
97 #define PWM1_CH15 ((0 << 8) + 14 )
98 #define PWM1_CH16 ((0 << 8) + 15 )
99 #define PWM2_CH1 ((1 << 8) + 0 )
100 #define PWM2_CH2 ((1 << 8) + 1 )
101 #define PWM2_CH3 ((1 << 8) + 2 )
102 #define PWM2_CH4 ((1 << 8) + 3 )
103 #define PWM2_CH5 ((1 << 8) + 4 )
104 #define PWM2_CH6 ((1 << 8) + 5 )
105 #define PWM2_CH7 ((1 << 8) + 6 )
106 #define PWM2_CH8 ((1 << 8) + 7 )
107 #define PWM2_CH9 ((1 << 8) + 8 )
108 #define PWM2_CH10 ((1 << 8) + 9 )
109 #define PWM2_CH11 ((1 << 8) + 10 )
110 #define PWM2_CH12 ((1 << 8) + 11 )
111 #define PWM2_CH13 ((1 << 8) + 12 )
112 #define PWM2_CH14 ((1 << 8) + 13 )
113 #define PWM2_CH15 ((1 << 8) + 14 )
114 #define PWM2_CH16 ((1 << 8) + 15 )
115 #define PWM3_CH1 ((2 << 8) + 0 )
116 #define PWM3_CH2 ((2 << 8) + 1 )
117 #define PWM3_CH3 ((2 << 8) + 2 )
118 #define PWM3_CH4 ((2 << 8) + 3 )
119 #define PWM3_CH5 ((2 << 8) + 4 )
120 #define PWM3_CH6 ((2 << 8) + 5 )
121 #define PWM3_CH7 ((2 << 8) + 6 )
122 #define PWM3_CH8 ((2 << 8) + 7 )
123 #define PWM3_CH9 ((2 << 8) + 8 )
124 #define PWM3_CH10 ((2 << 8) + 9 )
125 #define PWM3_CH11 ((2 << 8) + 10 )
126 #define PWM3_CH12 ((2 << 8) + 11 )
127 #define PWM3_CH13 ((2 << 8) + 12 )
128 #define PWM3_CH14 ((2 << 8) + 13 )
129 #define PWM3_CH15 ((2 << 8) + 14 )
130 #define PWM3_CH16 ((2 << 8) + 15 )
131 #define PWM4_CH1 ((3 << 8) + 0 )
132 #define PWM4_CH2 ((3 << 8) + 1 )
133 #define PWM4_CH3 ((3 << 8) + 2 )
134 #define PWM4_CH4 ((3 << 8) + 3 )
135 #define PWM4_CH5 ((3 << 8) + 4 )
136 #define PWM4_CH6 ((3 << 8) + 5 )
137 #define PWM4_CH7 ((3 << 8) + 6 )
138 #define PWM4_CH8 ((3 << 8) + 7 )
139 #define PWM4_CH9 ((3 << 8) + 8 )
140 #define PWM4_CH10 ((3 << 8) + 9 )
141 #define PWM4_CH11 ((3 << 8) + 10 )
142 #define PWM4_CH12 ((3 << 8) + 11 )
143 #define PWM4_CH13 ((3 << 8) + 12 )
144 #define PWM4_CH14 ((3 << 8) + 13 )
145 #define PWM4_CH15 ((3 << 8) + 14 )
146 #define PWM4_CH16 ((3 << 8) + 15 )
147 #define PWM5_CH1 ((4 << 8) + 0 )
148 #define PWM5_CH2 ((4 << 8) + 1 )
149 #define PWM5_CH3 ((4 << 8) + 2 )
150 #define PWM5_CH4 ((4 << 8) + 3 )
151 #define PWM5_CH5 ((4 << 8) + 4 )
152 #define PWM5_CH6 ((4 << 8) + 5 )
153 #define PWM5_CH7 ((4 << 8) + 6 )
154 #define PWM5_CH8 ((4 << 8) + 7 )
155 #define PWM5_CH9 ((4 << 8) + 8 )
156 #define PWM5_CH10 ((4 << 8) + 9 )
157 #define PWM5_CH11 ((4 << 8) + 10 )
158 #define PWM5_CH12 ((4 << 8) + 11 )
159 #define PWM5_CH13 ((4 << 8) + 12 )
160 #define PWM5_CH14 ((4 << 8) + 13 )
161 #define PWM5_CH15 ((4 << 8) + 14 )
162 #define PWM5_CH16 ((4 << 8) + 15 )
163 #define PWM6_CH1 ((5 << 8) + 0 )
164 #define PWM6_CH2 ((5 << 8) + 1 )
165 #define PWM6_CH3 ((5 << 8) + 2 )
166 #define PWM6_CH4 ((5 << 8) + 3 )
167 #define PWM6_CH5 ((5 << 8) + 4 )
168 #define PWM6_CH6 ((5 << 8) + 5 )
169 #define PWM6_CH7 ((5 << 8) + 6 )
170 #define PWM6_CH8 ((5 << 8) + 7 )
171 #define PWM6_CH9 ((5 << 8) + 8 )
172 #define PWM6_CH10 ((5 << 8) + 9 )
173 #define PWM6_CH11 ((5 << 8) + 10 )
174 #define PWM6_CH12 ((5 << 8) + 11 )
175 #define PWM6_CH13 ((5 << 8) + 12 )
176 #define PWM6_CH14 ((5 << 8) + 13 )
177 #define PWM6_CH15 ((5 << 8) + 14 )
178 #define PWM6_CH16 ((5 << 8) + 15 )
179 #define PWM7_CH1 ((6 << 8) + 0 )
180 #define PWM7_CH2 ((6 << 8) + 1 )
181 #define PWM7_CH3 ((6 << 8) + 2 )
182 #define PWM7_CH4 ((6 << 8) + 3 )
183 #define PWM7_CH5 ((6 << 8) + 4 )
184 #define PWM7_CH6 ((6 << 8) + 5 )
185 #define PWM7_CH7 ((6 << 8) + 6 )
186 #define PWM7_CH8 ((6 << 8) + 7 )
187 #define PWM7_CH9 ((6 << 8) + 8 )
188 #define PWM7_CH10 ((6 << 8) + 9 )
189 #define PWM7_CH11 ((6 << 8) + 10 )
190 #define PWM7_CH12 ((6 << 8) + 11 )
191 #define PWM7_CH13 ((6 << 8) + 12 )
192 #define PWM7_CH14 ((6 << 8) + 13 )
193 #define PWM7_CH15 ((6 << 8) + 14 )
194 #define PWM7_CH16 ((6 << 8) + 15 )
195 #define PWM8_CH1 ((7 << 8) + 0 )
196 #define PWM8_CH2 ((7 << 8) + 1 )
197 #define PWM8_CH3 ((7 << 8) + 2 )
198 #define PWM8_CH4 ((7 << 8) + 3 )
199 #define PWM8_CH5 ((7 << 8) + 4 )
200 #define PWM8_CH6 ((7 << 8) + 5 )
201 #define PWM8_CH7 ((7 << 8) + 6 )
202 #define PWM8_CH8 ((7 << 8) + 7 )
203 #define PWM8_CH9 ((7 << 8) + 8 )
204 #define PWM8_CH10 ((7 << 8) + 9 )
205 #define PWM8_CH11 ((7 << 8) + 10 )
206 #define PWM8_CH12 ((7 << 8) + 11 )
207 #define PWM8_CH13 ((7 << 8) + 12 )
208 #define PWM8_CH14 ((7 << 8) + 13 )
209 #define PWM8_CH15 ((7 << 8) + 14 )
210 #define PWM8_CH16 ((7 << 8) + 15 )
211 #define PWM9_CH1 ((8 << 8) + 0 )
212 #define PWM9_CH2 ((8 << 8) + 1 )
213 #define PWM9_CH3 ((8 << 8) + 2 )
214 #define PWM9_CH4 ((8 << 8) + 3 )
215 #define PWM9_CH5 ((8 << 8) + 4 )
216 #define PWM9_CH6 ((8 << 8) + 5 )
217 #define PWM9_CH7 ((8 << 8) + 6 )
218 #define PWM9_CH8 ((8 << 8) + 7 )
219 #define PWM9_CH9 ((8 << 8) + 8 )
220 #define PWM9_CH10 ((8 << 8) + 9 )
221 #define PWM9_CH11 ((8 << 8) + 10 )
222 #define PWM9_CH12 ((8 << 8) + 11 )
223 #define PWM9_CH13 ((8 << 8) + 12 )
224 #define PWM9_CH14 ((8 << 8) + 13 )
225 #define PWM9_CH15 ((8 << 8) + 14 )
226 #define PWM9_CH16 ((8 << 8) + 15 )
227 #define PWM10_CH1 ((9 << 8) + 0 )
228 #define PWM10_CH2 ((9 << 8) + 1 )
229 #define PWM10_CH3 ((9 << 8) + 2 )
230 #define PWM10_CH4 ((9 << 8) + 3 )
231 #define PWM10_CH5 ((9 << 8) + 4 )
232 #define PWM10_CH6 ((9 << 8) + 5 )
233 #define PWM10_CH7 ((9 << 8) + 6 )
234 #define PWM10_CH8 ((9 << 8) + 7 )
235 #define PWM10_CH9 ((9 << 8) + 8 )
236 #define PWM10_CH10 ((9 << 8) + 9 )
237 #define PWM10_CH11 ((9 << 8) + 10 )
238 #define PWM10_CH12 ((9 << 8) + 11 )
239 #define PWM10_CH13 ((9 << 8) + 12 )
240 #define PWM10_CH14 ((9 << 8) + 13 )
241 #define PWM10_CH15 ((9 << 8) + 14 )
242 #define PWM10_CH16 ((9 << 8) + 15 )
243 #define PWM11_CH1 ((10 << 8) + 0 )
244 #define PWM11_CH2 ((10 << 8) + 1 )
245 #define PWM11_CH3 ((10 << 8) + 2 )
246 #define PWM11_CH4 ((10 << 8) + 3 )
247 #define PWM11_CH5 ((10 << 8) + 4 )
248 #define PWM11_CH6 ((10 << 8) + 5 )
249 #define PWM11_CH7 ((10 << 8) + 6 )
250 #define PWM11_CH8 ((10 << 8) + 7 )
251 #define PWM11_CH9 ((10 << 8) + 8 )
252 #define PWM11_CH10 ((10 << 8) + 9 )
253 #define PWM11_CH11 ((10 << 8) + 10 )
254 #define PWM11_CH12 ((10 << 8) + 11 )
255 #define PWM11_CH13 ((10 << 8) + 12 )
256 #define PWM11_CH14 ((10 << 8) + 13 )
257 #define PWM11_CH15 ((10 << 8) + 14 )
258 #define PWM11_CH16 ((10 << 8) + 15 )
259 #define PWM12_CH1 ((11 << 8) + 0 )
260 #define PWM12_CH2 ((11 << 8) + 1 )
261 #define PWM12_CH3 ((11 << 8) + 2 )
262 #define PWM12_CH4 ((11 << 8) + 3 )
263 #define PWM12_CH5 ((11 << 8) + 4 )
264 #define PWM12_CH6 ((11 << 8) + 5 )
265 #define PWM12_CH7 ((11 << 8) + 6 )
266 #define PWM12_CH8 ((11 << 8) + 7 )
267 #define PWM12_CH9 ((11 << 8) + 8 )
268 #define PWM12_CH10 ((11 << 8) + 9 )
269 #define PWM12_CH11 ((11 << 8) + 10 )
270 #define PWM12_CH12 ((11 << 8) + 11 )
271 #define PWM12_CH13 ((11 << 8) + 12 )
272 #define PWM12_CH14 ((11 << 8) + 13 )
273 #define PWM12_CH15 ((11 << 8) + 14 )
274 #define PWM12_CH16 ((11 << 8) + 15 )
275 #define PWM13_CH1 ((12 << 8) + 0 )
276 #define PWM13_CH2 ((12 << 8) + 1 )
277 #define PWM13_CH3 ((12 << 8) + 2 )
278 #define PWM13_CH4 ((12 << 8) + 3 )
279 #define PWM13_CH5 ((12 << 8) + 4 )
280 #define PWM13_CH6 ((12 << 8) + 5 )
281 #define PWM13_CH7 ((12 << 8) + 6 )
282 #define PWM13_CH8 ((12 << 8) + 7 )
283 #define PWM13_CH9 ((12 << 8) + 8 )
284 #define PWM13_CH10 ((12 << 8) + 9 )
285 #define PWM13_CH11 ((12 << 8) + 10 )
286 #define PWM13_CH12 ((12 << 8) + 11 )
287 #define PWM13_CH13 ((12 << 8) + 12 )
288 #define PWM13_CH14 ((12 << 8) + 13 )
289 #define PWM13_CH15 ((12 << 8) + 14 )
290 #define PWM13_CH16 ((12 << 8) + 15 )
291 #define PWM14_CH1 ((13 << 8) + 0 )
292 #define PWM14_CH2 ((13 << 8) + 1 )
293 #define PWM14_CH3 ((13 << 8) + 2 )
294 #define PWM14_CH4 ((13 << 8) + 3 )
295 #define PWM14_CH5 ((13 << 8) + 4 )
296 #define PWM14_CH6 ((13 << 8) + 5 )
297 #define PWM14_CH7 ((13 << 8) + 6 )
298 #define PWM14_CH8 ((13 << 8) + 7 )
299 #define PWM14_CH9 ((13 << 8) + 8 )
300 #define PWM14_CH10 ((13 << 8) + 9 )
301 #define PWM14_CH11 ((13 << 8) + 10 )
302 #define PWM14_CH12 ((13 << 8) + 11 )
303 #define PWM14_CH13 ((13 << 8) + 12 )
304 #define PWM14_CH14 ((13 << 8) + 13 )
305 #define PWM14_CH15 ((13 << 8) + 14 )
306 #define PWM14_CH16 ((13 << 8) + 15 )
307 #define PWM15_CH1 ((14 << 8) + 0 )
308 #define PWM15_CH2 ((14 << 8) + 1 )
309 #define PWM15_CH3 ((14 << 8) + 2 )
310 #define PWM15_CH4 ((14 << 8) + 3 )
311 #define PWM15_CH5 ((14 << 8) + 4 )
312 #define PWM15_CH6 ((14 << 8) + 5 )
313 #define PWM15_CH7 ((14 << 8) + 6 )
314 #define PWM15_CH8 ((14 << 8) + 7 )
315 #define PWM15_CH9 ((14 << 8) + 8 )
316 #define PWM15_CH10 ((14 << 8) + 9 )
317 #define PWM15_CH11 ((14 << 8) + 10 )
318 #define PWM15_CH12 ((14 << 8) + 11 )
319 #define PWM15_CH13 ((14 << 8) + 12 )
320 #define PWM15_CH14 ((14 << 8) + 13 )
321 #define PWM15_CH15 ((14 << 8) + 14 )
322 #define PWM15_CH16 ((14 << 8) + 15 )
323 #define PWM16_CH1 ((15 << 8) + 0 )
324 #define PWM16_CH2 ((15 << 8) + 1 )
325 #define PWM16_CH3 ((15 << 8) + 2 )
326 #define PWM16_CH4 ((15 << 8) + 3 )
327 #define PWM16_CH5 ((15 << 8) + 4 )
328 #define PWM16_CH6 ((15 << 8) + 5 )
329 #define PWM16_CH7 ((15 << 8) + 6 )
330 #define PWM16_CH8 ((15 << 8) + 7 )
331 #define PWM16_CH9 ((15 << 8) + 8 )
332 #define PWM16_CH10 ((15 << 8) + 9 )
333 #define PWM16_CH11 ((15 << 8) + 10 )
334 #define PWM16_CH12 ((15 << 8) + 11 )
335 #define PWM16_CH13 ((15 << 8) + 12 )
336 #define PWM16_CH14 ((15 << 8) + 13 )
337 #define PWM16_CH15 ((15 << 8) + 14 )
338 #define PWM16_CH16 ((15 << 8) + 15 )
339 #endif
340 #endif // PWM_H
@@ -0,0 +1,222
1 #include <pwm.h>
2 #include <stm32f4xx_tim.h>
3 #include <stm32f4xx_gpio.h>
4 #include <stm32f4xx_rcc.h>
5 #include <stm32f4xx_tim.h>
6 #include <core.h>
7 #include <gpio.h>
8 const TIM_TypeDef* _timer_dev_table[14]={TIM1,TIM2,TIM3,TIM4,TIM5,TIM6,TIM7,
9 TIM8,TIM9,TIM10,TIM11,TIM12,TIM13,TIM14};
10
11 #define timer1 0
12 #define timer2 1
13 #define timer3 2
14 #define timer4 3
15 #define timer5 4
16 #define timer6 5
17 #define timer7 6
18 #define timer8 7
19 #define timer9 8
20 #define timer10 9
21 #define timer11 10
22 #define timer12 11
23 #define timer13 12
24 #define timer14 13
25 #define timer15 14
26 #define timer16 15
27
28 #define PWMGETTIMNUMBER(PWM) (((uint32_t)(PWM) & (uint32_t)0x0000FF00)>>(uint32_t)8)
29
30 int pwmopen(int PWM,uint32_t pin)
31 {
32 #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA))
33 #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8)
34 int timer = PWMGETTIMNUMBER(PWM);
35 gpio_t PWMOUT;
36 PWMOUT = gpioopen(pin);
37 PWMOUT |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
38 gpiosetconfig(&PWMOUT);
39 uint8_t GPIO_AF = -1;
40 if(timer==timer1 || timer==timer2)GPIO_AF=1;
41 if(timer==timer3 || timer==timer4 || timer==timer5)GPIO_AF=2;
42 if(timer==timer8 || timer==timer9 || timer==timer10 || timer==timer11)GPIO_AF=3;
43 if(timer==timer12 || timer==timer13 || timer==timer14)GPIO_AF=9;
44
45 if(timer>=timer2 && timer <= timer7)RCC_APB1PeriphClockCmd((1<<(timer-1)), ENABLE);
46 if(timer>=timer12 && timer <= timer14)RCC_APB1PeriphClockCmd((1<<(timer-5)), ENABLE);
47 if(timer==timer1)RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
48 if(timer==timer8)RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
49 if(timer==timer9)RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9, ENABLE);
50 if(timer==timer10)RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM10, ENABLE);
51 if(timer==timer11)RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM11, ENABLE);
52
53 if(GPIO_AF!=-1)GPIO_PinAFConfig(GPIOGETPORT(PWMOUT), (uint8_t)(PWMOUT & 0xF), GPIO_AF);
54
55 return 0;
56 }
57
58 int pwmsetconfig(int PWM,uint32_t freq,float dutyCycle)
59 {
60 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
61 TIM_OCInitTypeDef TIM_OCInitStructure;
62 if(PWM==-1) return -1;
63 int timer = PWMGETTIMNUMBER(PWM);
64 TIM_TypeDef* tim = _timer_dev_table[timer];
65 uint32_t timfreq = getCpuFreq()/2;
66 uint32_t period = timfreq/freq - 1;
67 uint16_t PrescalerValue=0;
68
69 while (period>=0x0FFFF)
70 {
71 PrescalerValue++;
72 timfreq = getCpuFreq()/(2*(PrescalerValue+1));
73 period = (timfreq/freq) - 1;
74 }
75
76 TIM_TimeBaseStructure.TIM_Period = period;
77 TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
78 TIM_TimeBaseStructure.TIM_ClockDivision = 0;
79 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
80 TIM_TimeBaseInit(tim, &TIM_TimeBaseStructure);
81
82 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
83 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
84 TIM_OCInitStructure.TIM_Pulse = (uint32_t)((period * dutyCycle)/100);
85 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
86
87 switch (PWM&0x0FF) {
88 case 0:
89 TIM_OC1Init(tim, &TIM_OCInitStructure);
90 TIM_OC1PreloadConfig(tim, TIM_OCPreload_Enable);
91 TIM_ARRPreloadConfig(tim, ENABLE);
92 TIM_Cmd(tim, ENABLE);
93 break;
94 case 1:
95 TIM_OC2Init(tim, &TIM_OCInitStructure);
96 TIM_OC2PreloadConfig(tim, TIM_OCPreload_Enable);
97 TIM_ARRPreloadConfig(tim, ENABLE);
98 TIM_Cmd(tim, ENABLE);
99 break;
100 case 2:
101 TIM_OC3Init(tim, &TIM_OCInitStructure);
102 TIM_OC3PreloadConfig(tim, TIM_OCPreload_Enable);
103 TIM_ARRPreloadConfig(tim, ENABLE);
104 TIM_Cmd(tim, ENABLE);
105 break;
106 case 3:
107 TIM_OC4Init(tim, &TIM_OCInitStructure);
108 TIM_OC4PreloadConfig(tim, TIM_OCPreload_Enable);
109 TIM_ARRPreloadConfig(tim, ENABLE);
110 TIM_Cmd(tim, ENABLE);
111 break;
112 default:
113 return -1;
114 break;
115 }
116 return 0;
117 }
118
119
120
121
122
123 int pwmsetdutycycle(int PWM,float dutyCycle)
124 {
125 if(PWM==-1) return -1;
126 int timer = PWMGETTIMNUMBER(PWM);
127 TIM_TypeDef* tim = _timer_dev_table[timer];
128 switch (PWM&0x0FF) {
129 case 0:
130 tim->CCR1 = (uint32_t)((tim->ARR * dutyCycle)/100);
131 break;
132 case 1:
133 tim->CCR2 = (uint32_t)((tim->ARR * dutyCycle)/100);
134 break;
135 case 2:
136 tim->CCR3 = (uint32_t)((tim->ARR * dutyCycle)/100);
137 break;
138 case 3:
139 tim->CCR4 = (uint32_t)((tim->ARR * dutyCycle)/100);
140 break;
141 default:
142 return -1;
143 break;
144 }
145 return 0;
146 }
147
148 float pwmgetdutycycle(int PWM)
149 {
150 if(PWM==-1) return -1;
151 int timer = PWMGETTIMNUMBER(PWM);
152 TIM_TypeDef* tim = _timer_dev_table[timer];
153 switch (PWM&0x0FF) {
154 case 0:
155 return (float)((tim->CCR1*100)/tim->ARR);
156 break;
157 case 1:
158 return (float)((tim->CCR2*100)/tim->ARR);
159 break;
160 case 2:
161 return (float)((tim->CCR3*100)/tim->ARR);
162 break;
163 case 3:
164 return (float)((tim->CCR4*100)/tim->ARR);
165 break;
166 default:
167 return -1;
168 break;
169 }
170 return 0;
171 }
172
173
174 int pwmsetfrequency(int PWM,uint32_t freq)
175 {
176 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
177 if(PWM==-1) return -1;
178 int timer = PWMGETTIMNUMBER(PWM);
179 TIM_TypeDef* tim = _timer_dev_table[timer];
180 uint32_t timfreq = getCpuFreq()/2;
181 uint32_t period = timfreq/freq - 1;
182 uint16_t PrescalerValue=0;
183 float dutyCycle = pwmgetdutycycle(PWM);
184 while (period>=0x0FFFF)
185 {
186 PrescalerValue++;
187 timfreq = getCpuFreq()/(2*(PrescalerValue+1));
188 period = (timfreq/freq) - 1;
189 }
190
191 TIM_TimeBaseStructure.TIM_Period = period;
192 TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
193 TIM_TimeBaseStructure.TIM_ClockDivision = 0;
194 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
195 TIM_TimeBaseInit(tim, &TIM_TimeBaseStructure);
196
197 switch (PWM&0x0FF) {
198 case 0:
199 tim->CCR1 = (uint32_t)((tim->ARR * dutyCycle)/100);
200 TIM_ARRPreloadConfig(tim, ENABLE);
201 break;
202 case 1:
203 tim->CCR2 = (uint32_t)((tim->ARR * dutyCycle)/100);
204 TIM_ARRPreloadConfig(tim, ENABLE);
205 break;
206 case 2:
207 tim->CCR3 = (uint32_t)((tim->ARR * dutyCycle)/100);
208 TIM_ARRPreloadConfig(tim, ENABLE);
209 break;
210 case 3:
211 tim->CCR4 = (uint32_t)((tim->ARR * dutyCycle)/100);
212 TIM_ARRPreloadConfig(tim, ENABLE);
213 break;
214 default:
215 return -1;
216 break;
217 }
218 return 0;
219 }
220
221
222
@@ -0,0 +1,13
1 TEMPLATE = lib
2 CONFIG += libuc2lib
3
4 SOURCES += pwm.c
5
6
7 UCMODEL=stm32f4
8
9 HEADERS += \
10 ../../../include/PERIPHERALS/pwm.h
11
12
13
@@ -1,19 +1,19
1 1 TEMPLATE = lib
2 2
3 3 TARGET=bsp
4 4 CONFIG += bsp
5 5
6 6 BSP=STM32F4Discovery
7 7
8 8 UCMODEL=stm32f4
9 9
10 10 SOURCES += bsp.c
11 11
12 12 HEADERS += bsp.h
13 13
14 LIBS+= -lgpio -luart -li2c -lspi
14 LIBS+= -lgpio -luart -li2c -lspi -lpwm
15 15
16 16 BSPFILE = bsp.pri
17 17
18 18
19 19
@@ -1,282 +1,281
1 1 #include <stdlib.h>
2 2 #include <errno.h>
3 3 #include <string.h>
4 4 #include <sys/stat.h>
5 5 #include <sys/types.h>
6 6 #include <sys/times.h>
7 7 #include <streamdevices.h>
8 8 #include <string.h>
9 9 #include <stdio.h>
10 10 #include <gpio.h>
11 11 #include <uart.h>
12 12 #include <stdint.h>
13 13 #include <bsp.h>
14 #include <core.h>
15 14
16 15 #undef errno
17 16
18 17
19 18 #ifdef __cplusplus
20 19 extern "C" {
21 20 #endif
22 21 extern int errno;
23 22 extern int32_t __max_opened_files__;
24 23 extern streamdevice* __opnfiles__[];
25 24 extern int32_t* __fs_root__;
26 25 extern int32_t __fs_root_size__;
27 26
28 27 char *__env[1] = { 0 };
29 28 char **environ = __env;
30 29
31 30 int _exit()
32 31 {
33 32 while(1)
34 33 {
35 34 delay_100us(10000);
36 35 gpioset(LED2);
37 36 delay_100us(10000);
38 37 gpioclr(LED2);
39 38 }
40 39 }
41 40
42 41 int _close(int file)
43 42 {
44 43 if(file<__max_opened_files__ && __opnfiles__[file]!=NULL)
45 44 {
46 45 return __opnfiles__[file]->ops->close( __opnfiles__[file]);
47 46 }
48 47 return 0;
49 48 }
50 49
51 50
52 51 int _write(int file, char *ptr, int len)
53 52 {
54 53 if(file<__max_opened_files__ && __opnfiles__[file]!=NULL)
55 54 {
56 55 if(__opnfiles__[file]->ops->write(__opnfiles__[file],ptr,1,len)) return len;
57 56 }
58 57 return len;
59 58 }
60 59
61 60 int _execve(char *name, char **argv, char **env) {
62 61 errno = ENOMEM;
63 62 return -1;
64 63 }
65 64
66 65
67 66 int _fork(void) {
68 67 errno = EAGAIN;
69 68 return -1;
70 69 }
71 70
72 71 int _fstat(int file, struct stat *st) {
73 72 st->st_mode = S_IFCHR;
74 73 return 0;
75 74 }
76 75
77 76
78 77 int _getpid(void) {
79 78 return 1;
80 79 }
81 80
82 81
83 82 int _isatty(int file) {
84 83 return 1;
85 84 }
86 85
87 86
88 87 int _kill(int pid, int sig) {
89 88 errno = EINVAL;
90 89 return -1;
91 90 }
92 91
93 92
94 93 int _link(char *old, char *_new) {
95 94 errno = EMLINK;
96 95 return -1;
97 96 }
98 97
99 98
100 99 int _lseek(int file, int ptr, int dir) {
101 100 return 0;
102 101 }
103 102
104 103 #include <gpio.h>
105 104
106 105 int _open(const char *name, int flags, int mode)
107 106 {
108 107 if(!strncmp("UART", name, 4) && ((name[4] & 0x30)==0x30))
109 108 {
110 109 //uart_t* uart1 = malloc(sizeof(uart_t));
111 110 streamdevice* fd1 = malloc(sizeof(streamdevice));
112 111 uart_t uart=uartopen((name[4] & 0xF)-1);
113 112 if(uart!=-1);
114 113 {
115 114 uartmkstreamdev(uart,fd1);
116 115 int i=2;
117 116 while((i<__max_opened_files__) && (__opnfiles__[i]!=NULL))i++;
118 117 if(i!=__max_opened_files__)
119 118 {
120 119 __opnfiles__[i] = fd1;
121 120 return i;
122 121 }
123 122 else printf("Too much files opened\n\r");
124 123 }
125 124 }
126 125 return -1;
127 126 }
128 127
129 128 int _read(int file, char *ptr, int len) {
130 129 if(file<__max_opened_files__ && __opnfiles__[file]!=NULL)
131 130 {
132 131 if(__opnfiles__[file]->ops->read(__opnfiles__[file],ptr,1,len)) return len;
133 132 }
134 133 return 0;
135 134 }
136 135
137 136
138 137
139 138 caddr_t _sbrk(int incr) {
140 139 register char * stack_ptr __asm__ ("sp");
141 140 extern char end; /* Defined by the linker */
142 141 static char *heap_end;
143 142 char *prev_heap_end;
144 143 if (heap_end == 0) {
145 144 heap_end = &end;
146 145 }
147 146 prev_heap_end = heap_end;
148 147 if (heap_end + incr > stack_ptr) {
149 148 printf("Heap and stack collision\n");
150 149 //abort ();
151 150 while(1)
152 151 {
153 152 delay_100us(10000);
154 153 gpioset(LED2);
155 154 delay_100us(10000);
156 155 gpioclr(LED2);
157 156 }
158 157 }
159 158 heap_end += incr;
160 159 return (caddr_t) prev_heap_end;
161 160 }
162 161
163 162
164 163
165 164 int _stat(char *file, struct stat *st) {
166 165 st->st_mode = S_IFCHR;
167 166 return 0;
168 167 }
169 168
170 169
171 170 int _times(struct tms *buf) {
172 171 return -1;
173 172 }
174 173
175 174
176 175 int _unlink(char *name) {
177 176 errno = ENOENT;
178 177 return -1;
179 178 }
180 179
181 180 int _wait(int *status) {
182 181 errno = ECHILD;
183 182 return -1;
184 183 }
185 184
186 185
187 186 int _read_r (struct _reent *r, int file, char * ptr, int len)
188 187 {
189 188 r = r;
190 189 file = file;
191 190 ptr = ptr;
192 191 len = len;
193 192 _read(file,ptr,len);
194 193 errno = EINVAL;
195 194 return -1;
196 195 }
197 196
198 197 /***************************************************************************/
199 198
200 199 int _lseek_r (struct _reent *r, int file, int ptr, int dir)
201 200 {
202 201 r = r;
203 202 file = file;
204 203 ptr = ptr;
205 204 dir = dir;
206 205
207 206 return 0;
208 207 }
209 208
210 209 /***************************************************************************/
211 210
212 211 int _write_r (struct _reent *r, int file, char * ptr, int len)
213 212 {
214 213 return _write(file, ptr, len);
215 214 }
216 215
217 216 /***************************************************************************/
218 217
219 218 int _close_r (struct _reent *r, int file)
220 219 {
221 220 return 0;
222 221 }
223 222
224 223 /***************************************************************************/
225 224
226 225 caddr_t _sbrk_r (struct _reent *r, int incr)
227 226 {
228 227 register char * stack_ptr __asm__ ("sp");
229 228 extern char end; /* Defined by the linker */
230 229 static char *heap_end;
231 230 char *prev_heap_end;
232 231 if (heap_end == 0) {
233 232 heap_end = &end;
234 233 }
235 234 prev_heap_end = heap_end;
236 235 if (heap_end + incr > stack_ptr) {
237 236 printf( "Heap and stack collision\n");
238 237 //abort ();
239 238 while(1)
240 239 {
241 240 delay_100us(10000);
242 241 gpioset(LED2);
243 242 delay_100us(10000);
244 243 gpioclr(LED2);
245 244 }
246 245 }
247 246 heap_end += incr;
248 247 return (caddr_t) prev_heap_end;
249 248 }
250 249
251 250 /***************************************************************************/
252 251
253 252 int _fstat_r (struct _reent *r, int file, struct stat * st)
254 253 {
255 254 r = r;
256 255 file = file;
257 256
258 257 memset (st, 0, sizeof (* st));
259 258 st->st_mode = S_IFCHR;
260 259 return 0;
261 260 }
262 261
263 262 /***************************************************************************/
264 263 int _open_r(struct _reent *r,const char *name, int flags, int mode)
265 264 {
266 265 return _open(name, flags, mode);
267 266 }
268 267
269 268 int _isatty_r(struct _reent *r, int fd)
270 269 {
271 270 r = r;
272 271 fd = fd;
273 272
274 273 return 1;
275 274 }
276 275
277 276
278 277 #ifdef __cplusplus
279 278 }
280 279 #endif
281 280
282 281
@@ -1,13 +1,15
1 1 TEMPLATE = subdirs
2 2 CONFIG += ordered
3 3
4 4 SUBDIRS = CORE/core.pro \
5 5 CPU/cpu.pro \
6 6 GPIO/gpio.pro \
7 7 UART/uart.pro \
8 8 SPI/spi.pro \
9 9 I2C/i2c.pro \
10 PWM/pwm.pro \
10 11 SDCARD-SDIO/sdcard-sdio.pro
11 12
12 13
14
13 15
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now