EPPL example libraries  0.1 alpha
 All Files Functions Variables Groups Pages
spwm.h
Go to the documentation of this file.
1 #ifndef SPWM_H_INCLUDED
2 #define SPWM_H_INCLUDED
3 /*
4  Copyright (c) 2014, Radoslaw Koppel
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without
8  modification, are permitted provided that the following conditions are met:
9 
10  - Redistributions of source code must retain the above copyright
11  notice, this list of conditions and the following disclaimer.
12 
13  - Redistributions in binary form must reproduce the above copyright
14  notice, this list of conditions and the following disclaimer in the
15  documentation and/or other materials provided with the distribution.
16 
17  - Neither the name of the copyright holders nor the
18  names of its contributors may be used to endorse or promote products
19  derived from this software without specific prior written permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER BE LIABLE FOR ANY
25  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
42 #include <stdint.h>
43 #include <util/atomic.h>
44 #include "spwm_config.h"
45 #include "eppl.h"
46 
78  #ifndef SPWM_OUTPUTS_ALL
79  #define SPWM_OUTPUTS_ALL (EPPL_NP, 0, 0)
80  #warning No SPWM_OUTPUTS_ALL definition found. PWM would not be generated.
81  #endif
82 
89  #ifndef SPWM_TOPCNT
90  #define SPWM_TOPCNT 255
91  #warning No SPWM_TOPCNT definition found. Using default value.
92  #endif
93 
107  #define SPWM_PIN_SC(pin, off, on) EPPL_SC(pin, off)
108 
116  #define SPWM_PIN_SC_GENERATE(n, arg) SPWM_PIN_SC arg
117 
124  #define SPWM_PIN_HOTPIN(pin, off, on) EPPL_SR(pin, off, on)
125 
133  #define SPWM_PIN_HOTPINS_GENERATE(n, arg) SPWM_PIN_HOTPIN arg
134 
140  #define SPWM_PIN_ONRESET(pin, off, on) \
141  if(0 != *pval++) { \
142  eppl_reinitFromISR(EPPL_SR(pin, off, on)); \
143  }
144 
151  #define SPWM_PIN_ONRESET_GENERATE(n, arg) SPWM_PIN_ONRESET arg
152 
159  #define SPWM_PIN_ONTICK(pin, off, on) \
160  if(spwm_cnt >= *pval++) { \
161  eppl_reinitFromISR(EPPL_SR(pin, on, off)); \
162  }
163 
170  #define SPWM_PIN_ONTICK_GENERATE(n, arg) SPWM_PIN_ONTICK arg
171 
187 #define SPWM_PINCONFIG EPPL_PROCESSEVERYARG(SPWM_PIN_SC_GENERATE, com, SPWM_OUTPUTS_ALL)
188 
195 #define SPWM_HOTPINS EPPL_PROCESSEVERYARG(SPWM_PIN_HOTPINS_GENERATE, com, SPWM_OUTPUTS_ALL)
196 
204 #if SPWM_TOPCNT > UINT32_MAX
205  #error SPWM_TOPCNT out of supported range
206 #elif SPWM_TOPCNT > UINT16_MAX
207  typedef uint32_t spwm_val_T;
208 #elif SPWM_TOPCNT > UINT8_MAX
209  typedef uint16_t spwm_val_T;
210 #else
211  typedef uint8_t spwm_val_T;
212 #endif /* SPWM_TOPCNT */
213 
227  extern spwm_val_T spwm_cnt;
228 
234  extern spwm_val_T spwm_val[EPPL_COUNTARGS(SPWM_OUTPUTS_ALL)];
244 static inline void spwm_set(uint8_t ch, spwm_val_T val)
245 {
246  if(sizeof(val) > 1)
247  {
248  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
249  {
250  spwm_val[ch] = val;
251  }
252  }
253  else
254  {
255  spwm_val[ch] = val;
256  }
257 }
258 
267 static inline void spwm_set_FromISR(uint8_t ch, spwm_val_T val)
268 {
269  spwm_val[ch] = val;
270 }
271 
277 static inline void spwm_onTick(void)
278 {
279  if( (SPWM_TOPCNT != UINT32_MAX) &&
280  (SPWM_TOPCNT != UINT16_MAX) &&
281  (SPWM_TOPCNT != UINT8_MAX) )
282  {
283  if(spwm_cnt == SPWM_TOPCNT)
284  spwm_cnt = 0;
285  else
286  ++spwm_cnt;
287  }
288  else
289  ++spwm_cnt;
290 
291  if(spwm_cnt == 0)
292  {
293  spwm_val_T *pval = spwm_val;
295  }
296  else
297  {
298  spwm_val_T *pval = spwm_val;
300  }
301 }
302 
304 #endif /* SPWM_H_INCLUDED */