-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
174 lines (151 loc) · 4.4 KB
/
main.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
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
/*
* ORFA -- Open Robotics Firmware Architecture
*
* Copyright (c) 2009 Vladimir Ermakov, Andrey Demenev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*****************************************************************************/
/** ORFA with serial gate
* @file main.c
*
* @author Vladimir Ermakov <[email protected]>
*/
#include <stdint.h>
#include <stdio.h>
#include "eterm/eterm_main.h"
#include "core/i2cadapter.h"
#include "core/scheduler.h"
#include "hal/i2c.h"
// -- virtual slave --
#define BUF_LEN 65
#define GET_REGISTER true
#define GET_DATA false
static bool state_i2c = GET_REGISTER;
static uint8_t register_addr = 0x00;
static uint8_t buf[BUF_LEN];
static uint8_t data_len = 0;
static uint8_t* read_ptr;
static bool is_restart = false;
static bool is_read = false;
static bool prev_is_read = false;
static bool read_always = false;
static GATE_RESULT result = GR_OK;
/** Handle I2C Start event
* @param[in] address device address
* @param[in] flag Write/Read flag
* @return true if success (always)
*/
bool i2c_start_handler(uint8_t flag)
{
debug("%% > i2c_start_handler(0x%02x, %i)\n", 0, flag);
if (is_restart && !is_read && data_len > 0) {
debug("%% `-> gate_register_write(0x%02X, buf, %d)\n", register_addr, data_len);
result = gate_register_write(register_addr, buf+1, data_len);
data_len = 0;
}
state_i2c = GET_REGISTER;
is_read = (flag == 0) ? false : true;
is_restart = true;
if ((is_read && !prev_is_read) ||
(is_read && (!data_len || read_always)))
{
data_len = BUF_LEN - 1;
read_ptr = buf;
result = gate_register_read(register_addr, buf, &data_len);
debug("%% `-> gate_register_read(0x%02X, buf, %d)\n", register_addr, data_len);
}
prev_is_read = is_read;
return true;
}
/** Handle I2C Stop event
*/
void i2c_stop_handler(void)
{
debug("%% > i2c_stop_handler()\n");
is_restart = false;
if (!is_read) {
debug("%% `-> gate_register_write(0x%02X, buf, %d)\n", register_addr, data_len);
result = gate_register_write(register_addr, buf+1, data_len);
data_len = 0;
}
}
/** Handle I2C master write (slave receiver)
* @param[in] c writed byte
* @return true if success
*/
bool i2c_txc_handler(uint8_t c)
{
if (state_i2c) {
// Get register
read_always = c & 0x80;
register_addr = c & ~0x80;
state_i2c = GET_DATA;
} else {
// Get data
if ((++data_len) < BUF_LEN)
buf[data_len] = c;
else
data_len = BUF_LEN - 1;
}
debug("%% > i2c_txc_handler(0x%02x)\n", c);
return true;
}
/** Handle I2C master read (slave transmitter)
* @param[out] *c byte for read
* @param[in] ack ack/nack
* @return true if success
*/
bool i2c_rxc_handler(uint8_t *c, bool *ack)
{
if (!data_len) {
data_len = BUF_LEN - 1;
read_ptr = buf;
result = gate_register_read(register_addr, buf, &data_len);
debug("%% ,-> gate_register_read(0x%02X, buf, %d)\n", register_addr, data_len);
}
if (data_len > 0) {
*c = *read_ptr++;
--data_len;
} else {
*c = 0;
}
debug("%% > i2c_rxc_handler(0x%02x, %i)\n", *c, *ack);
return true;
}
// -- Main --
SYSTEM_INIT()
{
// Set I2C
i2c_set_evt_handlers(i2c_start_handler, i2c_stop_handler);
i2c_set_slave_handlers(i2c_txc_handler, i2c_rxc_handler);
// register supertask
gate_supertask_register(eterm_supertask);
// register introspection driver
gate_init_introspection();
// init gate deivces
eterm_init();
}
/** Main
*/
int main(void)
{
asm volatile ("sei");
gate_scheduler_loop();
return 0;
}