summaryrefslogtreecommitdiffhomepage
path: root/digital/io-hub/src/common/pwm.avr.c
blob: 4b6ad8960b356dc0374486d71b6e366beffbb9cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* pwm.avr.c */
/* io-hub - Modular Input/Output. {{{
 *
 * Copyright (C) 2011 Nicolas Schodet
 *
 * APBTeam:
 *        Web: http://apbteam.org/
 *      Email: team AT apbteam DOT org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */
#include "common.h"
#include "pwm.h"

#include "modules/utils/utils.h"
#include "preproc.h"

/** PWM context. */
struct pwm_t
{
    /** Output compare register. */
    volatile uint16_t *ocr;
    /** Direction port. */
    volatile uint8_t *dir_port;
    /** Direction bit value. */
    uint8_t dir_bv;
    /** Remaining time before timer elapse, 0 for disabled. */
    uint16_t time;
    /** Value to be used after timer elapse. */
    int16_t value_rest;
};

/** Array of PWM contexts. */
#define PWM(timer, pwm, pwm_io_port, pwm_io_n, dir_io_port, dir_io_n) \
{ &PREPROC_PASTE (OCR, timer, pwm), \
    &IO_PORT_ (dir_io_port, dir_io_n), IO_BV_ (dir_io_port, dir_io_n), \
    0, 0 },
struct pwm_t pwm[] = {
    AC_IOHUB_PWM
};
#undef PWM

void
pwm_init (void)
{
    /* Fast PWM, TOP = 0x3ff, with positive logic.
       f_IO without prescaler.
       Fpwm = f_IO / (prescaler * (1 + TOP)). */
    uint8_t timer_com_1 = 0, timer_com_3 = 0;
#define PWM(timer, pwm, pwm_io_port, pwm_io_n, dir_io_port, dir_io_n) \
    do { \
	PREPROC_PASTE (timer_com_, timer) |= \
	    _BV (PREPROC_PASTE (COM, timer, pwm, 1)); \
	IO_DDR_ (pwm_io_port, pwm_io_n) |= IO_BV_ (pwm_io_port, pwm_io_n); \
	IO_DDR_ (dir_io_port, dir_io_n) |= IO_BV_ (dir_io_port, dir_io_n); \
    } while (0);
    AC_IOHUB_PWM
#undef PWM
    if (timer_com_1)
      {
	TCCR1A = timer_com_1 |
	    regv (COM1A1, COM1A0, COM1B1, COM1B0, COM1C1, COM1C0, WGM11, WGM10,
		       0,      0,      0,      0,      0,      0,     1,     1);
	TCCR1B = regv (ICNC1, ICES1, 5, WGM13, WGM12, CS12, CS11, CS10,
			   0,     0, 0,     0,     1,    0,    0,    1);
      }
    if (timer_com_3)
      {
	TCCR3A = timer_com_3 |
	    regv (COM3A1, COM3A0, COM3B1, COM3B0, COM3C1, COM3C0, WGM31, WGM30,
		       0,      0,      0,      0,      0,      0,     1,     1);
	TCCR3B = regv (ICNC3, ICES3, 5, WGM33, WGM32, CS32, CS31, CS30,
			   0,     0, 0,     0,     1,    0,    0,    1);
      }
}

void
pwm_update (void)
{
    uint8_t i;
    for (i = 0; i < UTILS_COUNT (pwm); i++)
      {
	if (pwm[i].time)
	  {
	    pwm[i].time--;
	    if (pwm[i].time == 0)
		pwm_set (i, pwm[i].value_rest);
	  }
      }
}

void
pwm_set (uint8_t index, int16_t value)
{
    assert (index < UTILS_COUNT (pwm));
    assert (value <= PWM_MAX && value >= -PWM_MAX);
    pwm[index].time = 0;
    if (value >= 0)
      {
	*pwm[index].dir_port &= ~pwm[index].dir_bv;
	*pwm[index].ocr = value;
      }
    else
      {
	*pwm[index].dir_port |= pwm[index].dir_bv;
	*pwm[index].ocr = -value;
      }
}

void
pwm_set_timed (uint8_t index, int16_t value, uint16_t time, int16_t value_rest)
{
    assert (index < UTILS_COUNT (pwm));
    assert (value <= PWM_MAX && value >= -PWM_MAX);
    assert (value_rest <= PWM_MAX && value_rest >= -PWM_MAX);
    pwm_set (index, value);
    pwm[index].time = time;
    pwm[index].value_rest = value_rest;
}