summaryrefslogtreecommitdiff
path: root/openpicc/application/led.c
blob: df13d2165dcf3280152feef320581d54f777e670 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/***************************************************************
 *
 * OpenBeacon.org - LED support
 *
 * Copyright 2007 Milosch Meriac <meriac@openbeacon.de>
 * Copyright 2008 Henryk Plötz <henryk@ploetzli.ch>
 *
 ***************************************************************

    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; version 2.

    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.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

*/
#include <FreeRTOS.h>
#include <AT91SAM7.h>
#include <lib_AT91SAM7.h>
#include <string.h>
#include <board.h>
#include <task.h>
#include "led.h"

#include "gammatable.inc"

/**********************************************************************/

#define BLINK_TIME 5
#define PWM_PERIOD GAMMA_MAX
const struct channel_definition {
	int channel_no;
	enum { PERIPH_NONE=0, PERIPH_A, PERIPH_B} periph;
	AT91PS_PWMC_CH channel;
} 
pwm_channels[] = {
		[0] =  {0, PERIPH_A,  AT91C_BASE_PWMC_CH0},
		[1] =  {1, PERIPH_A,  AT91C_BASE_PWMC_CH1},
		[2] =  {2, PERIPH_A,  AT91C_BASE_PWMC_CH2},
		[7] =  {3, PERIPH_B,  AT91C_BASE_PWMC_CH3},
		[11] = {0, PERIPH_B,  AT91C_BASE_PWMC_CH0},
		[12] = {1, PERIPH_B,  AT91C_BASE_PWMC_CH1},
		[13] = {2, PERIPH_B,  AT91C_BASE_PWMC_CH2},
		[14] = {3, PERIPH_B,  AT91C_BASE_PWMC_CH3},
		[23] = {0, PERIPH_B,  AT91C_BASE_PWMC_CH0},
		[24] = {1, PERIPH_B,  AT91C_BASE_PWMC_CH1},
		[25] = {2, PERIPH_B,  AT91C_BASE_PWMC_CH2},
};

static const struct channel_definition* find_channel(unsigned int led)
{
	unsigned int pos = ffs(led);
	if(pos==0) return 0;
	
	if(pos-1 >= sizeof(pwm_channels) / sizeof(pwm_channels[0]) ) return 0;
	if(pwm_channels[pos-1].periph == PERIPH_NONE) return 0;
	return &pwm_channels[pos-1];
}

// Brightness is in per thousand
void vLedSetBrightness(unsigned int led, int brightness)
{
	const struct channel_definition *c = find_channel(led);
	if(c==0) return;
	
	if(brightness < 0) brightness = 0;
	if(brightness > GAMMA_LEN-1) brightness = GAMMA_LEN-1;
	
	int duty = gammatable[brightness];
	if(duty > 1) {
		c->channel->PWMC_CUPDR = duty;
		if(c->periph == PERIPH_A) {
			AT91F_PIO_CfgPeriph(AT91C_BASE_PIOA, led, 0);
		} else if(c->periph == PERIPH_B) {
			AT91F_PIO_CfgPeriph(AT91C_BASE_PIOA, 0, led);
		}
	} else {
	    vLedSet(led, 0);
	}
}

void vLedSet(int led, bool_t on)
{
    if(on)
    	AT91F_PIO_ClearOutput( AT91C_BASE_PIOA, led );
    else
        AT91F_PIO_SetOutput( AT91C_BASE_PIOA, led );
    AT91F_PIO_CfgOutput( AT91C_BASE_PIOA, led );
}

void vLedBlink(int led)
{
	volatile int i=0;
	vLedSet(led, 1);
	for(i=0; i<BLINK_TIME; i++) {vLedSet(led,0);vLedSet(led,1);}
	vLedSet(led, 0);	
}

/**********************************************************************/
void vLedSetRed(bool_t on)
{
	vLedSet(LED_RED, on);
}

void vLedBlinkRed(void)
{
	vLedBlink(LED_RED);
}
/**********************************************************************/

void vLedSetGreen(bool_t on)
{
	vLedSet(LED_GREEN, on);
}
/**********************************************************************/

void vLedBlinkGreen(void)
{
	vLedBlink(LED_GREEN);
}
/**********************************************************************/

void vLedHaltBlinking(int reason)
{
    volatile u_int32_t i=0;
    while(1)
    {
        AT91F_PIO_ClearOutput( AT91C_BASE_PIOA, LED_RED | LED_GREEN );
        for(i=0; i<MCK/40; i++) ;
	
	switch(reason) {
		case 1:
			vLedSetGreen(1);
			vLedSetRed(0);
			break;
		case 2:
			vLedSetGreen(0);
			vLedSetRed(1);
			break;
		case 3:
			vLedSetGreen(1);
			vLedSetRed(1);
			break;
		case 0:
		default:
			vLedSetGreen(0);
			vLedSetRed(0);
			break;
	}
        for(i=0; i<MCK/20; i++) ;
	
        AT91F_PIO_SetOutput( AT91C_BASE_PIOA, LED_RED | LED_GREEN );
        for(i=0; i<MCK/40; i++) ;
    }
}
/**********************************************************************/

static void init_led_pwm(unsigned int led)
{
	const struct channel_definition *c = find_channel(led);
	
	if(c==0) return;
	
	// Set PIO mapping
	if(c->periph == PERIPH_A) {
		AT91F_PIO_CfgPeriph(AT91C_BASE_PIOA, led, 0);
	} else if(c->periph == PERIPH_B) {
		AT91F_PIO_CfgPeriph(AT91C_BASE_PIOA, 0, led);
	} else
		return;
	
	// Configure channel: clock = MCK, period = PWM_PERIOD, CPOL=1, duty cycle variable
	c->channel->PWMC_CMR = 0x0 | AT91C_PWMC_CPOL;
	c->channel->PWMC_CDTYR = 1;
	c->channel->PWMC_CPRDR = PWM_PERIOD;
	
	AT91C_BASE_PWMC->PWMC_ENA = (1<<(c->channel_no));
	vLedSetBrightness(led, 0);
}

void vLedInit(void)
{
    // turn off LED's 
    AT91F_PIO_CfgOutput( AT91C_BASE_PIOA, LED_RED | LED_GREEN );
    AT91F_PIO_SetOutput( AT91C_BASE_PIOA, LED_RED | LED_GREEN );
	
    AT91F_PMC_EnablePeriphClock(AT91C_BASE_PMC,
				    ((unsigned int) 1 << AT91C_ID_PWMC));
    
    init_led_pwm(LED_GREEN);
    init_led_pwm(LED_RED);
}
personal git repositories of Harald Welte. Your mileage may vary