aboutsummaryrefslogtreecommitdiff
path: root/FanController/fan_controller/hw/io.c
blob: 3246f781fc12ca2cb4efb9fa1311db4e87b17c38 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/* Autor: Ján Suèan <jan@jansucan.sk>
 * 
 * Zdrojove kody, ich casti a subory z nich vzniknute priamo alebo nepriamo
 * (objektove subory, Intel Hex, ...) prosim nepouzivajte komercne, ani ako
 * sucast komercnych diel. Vsetky ostatne pripady pouzitia su dovolene.
 *
 * Please don't use the source codes, their parts and files created from them
 * directly or indirectly, (object files, Intel Hex files, ...) for commercial
 * purposes, not even as a part of commercial products. All other use cases
 * are allowed.
 */

#include <fan_controller/hw/io.h>
#include <fan_controller/utils.h>

#include <avr/io.h>

static void io_init_button(void);
static void io_init_fan_pwm(void);
static void io_init_fan_tachometer(void);
static void io_init_red_led(void);
static void io_init_green_led_0(void);
static void io_init_green_led_1(void);
static void io_init_green_led_2(void);
static void io_init_green_led_3(void);
static void io_init_green_led_4(void);
static void io_init_green_led_5(void);
static void io_init_green_led_6(void);
static void io_init_green_led_7(void);
static void io_init_green_led_8(void);

/**
 * @brief Inicializacia vstupov a vystupov mikrokontroleru.
 */
void
io_init(void)
{
	io_init_button();
	io_init_fan_pwm();
	io_init_fan_tachometer();
	io_init_red_led();
	io_init_green_led_0();
	io_init_green_led_1();
	io_init_green_led_2();
	io_init_green_led_3();
	io_init_green_led_4();
	io_init_green_led_5();
	io_init_green_led_6();
	io_init_green_led_7();
	io_init_green_led_8();
}

/**
 * @brief Nastavenie vstupu pre tlacitko.
 */
void
io_init_button(void)
{
	// Aktivacia pull-up rezistoru, to je treba, aby vstup "neplaval"
	UTILS_SET_BIT(PORTA, PORTA2);
}

/**
 * @brief Zistenie, ci je tlacitko stlacene, alebo nie.
 *
 * @retval true Tlacitko je stlacene.
 * @retval false Tlacitko nie je stlacene.
 */
bool
io_is_button_pressed(void)
{
	return UTILS_IS_BIT_SET(PINA, PINA2);		
}

/**
 * @brief Nastavenie vystupu pre PWM signal pre ovladanie otacok ventilatora.
 */
void
io_init_fan_pwm(void)
{
	// Nastavenie vystupu pre PWM (Timer0 a OutputCompare0)
	UTILS_SET_BIT(DDRB, PORTB3); // OC0 ako vystup
	UTILS_CLR_BIT(PORTB, PORTB3);
}

/**
 * @brief Odpojenie vystupu pre PWM signal pre ovladanie otacok ventilatora.
 */
void
io_disable_fan_pwm(void)
{
	// Deaktivacia vystupu pre PWM (Timer0 a OutputCompare0)
	UTILS_CLR_BIT(PORTB, PORTB3); // Vypnutie pull-up rezistoru
	UTILS_CLR_BIT(DDRB, PORTB3); // OC0 ako vstup v tretom stave
	
}

/**
 * @brief Nastavenie vstupu pre signal od tachometra ventilatora.
 */
void
io_init_fan_tachometer(void)
{
	// Aktivacia pull-up rezistoru, to je treba, inak je na vstupe "rusenie"
	UTILS_SET_BIT(PORTB, PORTB2);
}

/**
 * @brief Zistenie, ci je signal z tachometra v log. 1, alebo nie.
 *
 * @retval true Signal je v log. 1.
 * @retval false Signal nie je v log. 1.
 */
bool
io_is_fan_tachometer_set(void) 
{
	return UTILS_IS_BIT_SET(PINB, PINB2);	
}

/**
 * @brief Inicializacia cervenej LED.
 */
void
io_init_red_led(void)
{
	UTILS_SET_BIT(DDRA, PORTA3);
	io_off_red_led();
}

/**
 * @brief Zasvietenie cervenej LED.
 */
void
io_on_red_led(void)
{
	 UTILS_CLR_BIT(PORTA, PORTA3);
}

/**
 * @brief Zhasnutie cervenej LED.
 */
void
io_off_red_led(void)
{
	UTILS_SET_BIT(PORTA, PORTA3);
}

/**
 * @brief Inicializacia zelenej LED najviac vlavo.
 *
 * Ta bude ovladana PWM od Casovaca 2, aby sa dal regulovat jej jas.
 * Casovac 2 je zarovej pouziti implementaciu cakania v delay.c.
 */
void
io_init_green_led_0(void)
{
	UTILS_SET_BIT(DDRD, PORTD7);
	
	// LED 0 bude ovladana PWM casovaca 2
	// 62,5 Hz perioda
	TCNT2 = 0;
	// Fast PWM mode
	TCCR2 |= (1 << WGM21) | (1 << WGM20);
	// Inverted PWM
	TCCR2 |= ((1 << COM21) | (1 << COM20));
	// Prescale by 1024 = Zapnutie casovaca
	TCCR2 |= ((1 << CS22) | (1 << CS21) | (1 << CS20));
	
	io_off_green_led_0();	
}

/**
 * @brief Nastavenie maximalneho jasu zelenej LED najviac vlavo.
 *
 * Zasvietenie LED.
 */
void
io_on_green_led_0(void)
{
	// LED 0 je ovladana PWM casovaca 2
	io_set_green_led_0(UINT8_MAX);
}

/**
 * @brief Nastavenie jasu zelenej LED najviac vlavo.
 *
 * @param light Jas zelenej LED z rozsahu 0 az 255.
 */
void
io_set_green_led_0(uint8_t light)
{
	OCR2 = light;
}

/**
 * @brief Nastavenie minimalneho jasu zelenej LED najviac vlavo.
 *
 * Zhasnutie LED.
 */
void
io_off_green_led_0(void)
{
	io_set_green_led_0(0);
}

/**
 * @brief Inizializacia zelej LED 1.
 */
void
io_init_green_led_1(void)
{
	UTILS_SET_BIT(DDRC, PORTC0);
	io_off_green_led_1();
}

/**
 * @brief Zasvietenie zelej LED 1.
 */
void
io_on_green_led_1(void)
{
	UTILS_CLR_BIT(PORTC, PORTC0);
}

/**
 * @brief Zhasnutie zelej LED 1.
 */
void
io_off_green_led_1(void)
{
	UTILS_SET_BIT(PORTC, PORTC0);
}

/**
 * @brief Inizializacia zelej LED 2.
 */
void
io_init_green_led_2(void)
{
	UTILS_SET_BIT(DDRC, PORTC1);
	io_off_green_led_2();
}

/**
 * @brief Zasvietenie zelej LED 2.
 */
void
io_on_green_led_2(void)
{
	UTILS_CLR_BIT(PORTC, PORTC1);
}

/**
 * @brief Zhasnutie zelej LED 2.
 */
void
io_off_green_led_2(void)
{
	UTILS_SET_BIT(PORTC, PORTC1);
}

/**
 * @brief Inizializacia zelej LED 3.
 */
void
io_init_green_led_3(void)
{
	UTILS_SET_BIT(DDRC, PORTC3);
	io_off_green_led_3();
}

/**
 * @brief Zasvietenie zelej LED 3.
 */
void
io_on_green_led_3(void)
{
	UTILS_CLR_BIT(PORTC, PORTC3);
}

/**
 * @brief Zhasnutie zelej LED 3.
 */
void
io_off_green_led_3(void)
{
	UTILS_SET_BIT(PORTC, PORTC3);
}

/**
 * @brief Inizializacia zelej LED 4.
 */
void
io_init_green_led_4(void)
{
	UTILS_SET_BIT(DDRC, PORTC5);
	io_off_green_led_4();
}

/**
 * @brief Zasvietenie zelej LED 4.
 */
void
io_on_green_led_4(void)
{
	UTILS_CLR_BIT(PORTC, PORTC5);
}

/**
 * @brief Zhasnutie zelej LED 4.
 */
void
io_off_green_led_4(void)
{
	UTILS_SET_BIT(PORTC, PORTC5);
}

/**
 * @brief Inizializacia zelej LED 5.
 */
void
io_init_green_led_5(void)
{
	UTILS_SET_BIT(DDRC, PORTC6);
	io_off_green_led_5();
}

/**
 * @brief Zasvietenie zelej LED 5.
 */
void
io_on_green_led_5(void)
{
	UTILS_CLR_BIT(PORTC, PORTC6);
}

/**
 * @brief Zhasnutie zelej LED 5.
 */
void
io_off_green_led_5(void)
{
	UTILS_SET_BIT(PORTC, PORTC6);
}

/**
 * @brief Inizializacia zelej LED 6.
 */
void
io_init_green_led_6(void)
{
	UTILS_SET_BIT(DDRC, PORTC7);
	io_off_green_led_6();
}

/**
 * @brief Zasvietenie zelej LED 6.
 */
void
io_on_green_led_6(void)
{
	UTILS_CLR_BIT(PORTC, PORTC7);
}

/**
 * @brief Zhasnutie zelej LED 6.
 */
void
io_off_green_led_6(void)
{
	UTILS_SET_BIT(PORTC, PORTC7);
}

/**
 * @brief Inizializacia zelej LED 7.
 */
void
io_init_green_led_7(void)
{
	UTILS_SET_BIT(DDRA, PORTA7);
	io_off_green_led_7();
}

/**
 * @brief Zasvietenie zelej LED 7.
 */
void
io_on_green_led_7(void)
{
	UTILS_CLR_BIT(PORTA, PORTA7);
}

/**
 * @brief Zhasnutie zelej LED 8.
 */
void
io_off_green_led_7(void)
{
	UTILS_SET_BIT(PORTA, PORTA7);
}

/**
 * @brief Inizializacia zelej LED 8.
 */
void
io_init_green_led_8(void)
{
	UTILS_SET_BIT(DDRA, PORTA5);
	io_off_green_led_8();
}

/**
 * @brief Zasvietenie zelej LED 8.
 */
void
io_on_green_led_8(void)
{
	UTILS_CLR_BIT(PORTA, PORTA5);
}

/**
 * @brief Zhasnutie zelej LED 8.
 */
void
io_off_green_led_8(void)
{
	UTILS_SET_BIT(PORTA, PORTA5);
}