-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmcu.c
115 lines (99 loc) · 2.53 KB
/
mcu.c
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
#include <stdbool.h>
#include <stdlib.h>
#include <avr/io.h>
#include <util/delay.h>
#include "protocol.h"
#include "mcu.h"
#include "zif.h"
#define NO_CONFIG -1
struct mcu_port_config mcu_port[MAX_CONFIGS][MCU_PORT_CNT];
struct mcu_port_config *mcu_config;
// -----------------------------------------------------------------------
void mcu_init()
{
mcu_config_clear();
mcu_config = NULL;
}
// -----------------------------------------------------------------------
void mcu_config_select(uint8_t cfgnum)
{
mcu_config = mcu_port[cfgnum];
}
// -----------------------------------------------------------------------
void mcu_deconfigure()
{
ZIF_MCU_DDR_0 = 0;
ZIF_MCU_PORT_0 = 0;
ZIF_MCU_DDR_1 = 0;
ZIF_MCU_PORT_1 = 0;
ZIF_MCU_DDR_2 = 0;
ZIF_MCU_PORT_2 = 0;
mcu_config = NULL;
}
// -----------------------------------------------------------------------
void mcu_drain_pins()
{
ZIF_MCU_DDR_0 = 0xff;
ZIF_MCU_PORT_0 = 0;
ZIF_MCU_DDR_1 = 0xff;
ZIF_MCU_PORT_1 = 0;
ZIF_MCU_DDR_2 = 0xff;
ZIF_MCU_PORT_2 = 0;
_delay_ms(3);
}
// -----------------------------------------------------------------------
bool mcu_connect()
{
if (!mcu_config) {
error(ERR_NO_PINCFG);
return false;
}
ZIF_MCU_DDR_0 = mcu_config[ZIF_PORT_0].output;
ZIF_MCU_DDR_1 = mcu_config[ZIF_PORT_1].output;
ZIF_MCU_DDR_2 = mcu_config[ZIF_PORT_2].output;
ZIF_MCU_PORT_0 = mcu_config[ZIF_PORT_0].pullup;
ZIF_MCU_PORT_1 = mcu_config[ZIF_PORT_1].pullup;
ZIF_MCU_PORT_2 = mcu_config[ZIF_PORT_2].pullup;
return true;
}
// -----------------------------------------------------------------------
void mcu_config_clear()
{
for (uint8_t cfgnum=0 ; cfgnum<MAX_CONFIGS ; cfgnum++) {
for (uint8_t i=0 ; i<MCU_PORT_CNT ; i++) {
mcu_port[cfgnum][i].input = 0;
mcu_port[cfgnum][i].output = 0;
mcu_port[cfgnum][i].pullup = 0;
}
}
}
// -----------------------------------------------------------------------
bool mcu_func(uint8_t func, uint8_t port_pos, uint8_t port_bit)
{
if (!mcu_config) {
error(ERR_NO_PINCFG);
return false;
}
switch (func) {
case ZIF_OUT:
mcu_config[port_pos].output |= _BV(port_bit);
break;
case ZIF_IN_PU_WEAK:
mcu_config[port_pos].pullup |= _BV(port_bit);
[[ fallthrough ]];
case ZIF_IN_HIZ:
case ZIF_IN_PU_STRONG:
mcu_config[port_pos].input |= _BV(port_bit);
break;
default:
error(ERR_PIN_FUNC);
return false;
}
return true;
}
// -----------------------------------------------------------------------
struct mcu_port_config * mcu_get_port_config()
{
return mcu_config;
}
// vim: tabstop=4 shiftwidth=4 autoindent