-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2c.c
288 lines (218 loc) · 8.08 KB
/
i2c.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
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
/*
i2c.c - I2C interface
Grbl driver code for Texas Instruments Tiva C (TM4C123GH6PM) ARM processor
Part of grblHAL
Copyright (c) 2018-2021 Terje Io
Grbl 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 3 of the License, or
(at your option) any later version.
Grbl 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 Grbl. If not, see <http://www.gnu.org/licenses/>.
*/
#include "i2c.h"
#if I2C_ENABLE
#if TRINAMIC_ENABLE && TRINAMIC_I2C
#define I2C_ADR_I2CBRIDGE 0x47
#endif
#define i2cIsBusy ((i2c.state != I2CState_Idle) || I2CMasterBusy(I2C1_BASE))
typedef enum {
I2CState_Idle = 0,
I2CState_SendNext,
I2CState_SendLast,
I2CState_SendRegisterAddress,
I2CState_AwaitCompletion,
I2CState_ReceiveNext,
I2CState_ReceiveNextToLast,
I2CState_ReceiveLast,
} i2c_state_t;
typedef struct {
volatile i2c_state_t state;
uint8_t addr;
uint8_t count;
uint8_t *data;
keycode_callback_ptr keycode_callback;
uint8_t buffer[8];
} i2c_trans_t;
static i2c_trans_t i2c;
static void I2C_interrupt_handler (void);
// get bytes (max 8), waits for result
static uint8_t* I2C_Receive (uint32_t i2cAddr, uint32_t bytes, bool block)
{
i2c.data = i2c.buffer;
i2c.count = bytes;
i2c.state = bytes == 1 ? I2CState_ReceiveLast : (bytes == 2 ? I2CState_ReceiveNextToLast : I2CState_ReceiveNext);
I2CMasterSlaveAddrSet(I2C1_BASE, i2cAddr, true);
I2CMasterControl(I2C1_BASE, bytes == 1 ? I2C_MASTER_CMD_SINGLE_RECEIVE : I2C_MASTER_CMD_BURST_RECEIVE_START);
if(block)
while(i2cIsBusy);
return i2c.buffer;
}
static void I2C_Send (uint32_t i2cAddr, uint8_t bytes, bool block)
{
i2c.count = bytes - 1;
i2c.data = i2c.buffer;
i2c.state = bytes == 1 ? I2CState_AwaitCompletion : (bytes == 2 ? I2CState_SendLast : I2CState_SendNext);
I2CMasterSlaveAddrSet(I2C1_BASE, i2cAddr, false);
I2CMasterDataPut(I2C1_BASE, *i2c.data++);
I2CMasterControl(I2C1_BASE, bytes == 1 ? I2C_MASTER_CMD_SINGLE_SEND : I2C_MASTER_CMD_BURST_SEND_START);
if(block)
while(i2cIsBusy);
}
static uint8_t *I2C_ReadRegister (uint32_t i2cAddr, uint8_t bytes, bool block)
{
while(i2cIsBusy);
i2c.count = bytes;
i2c.data = i2c.buffer;
i2c.state = I2CState_SendRegisterAddress;
i2c.addr = i2cAddr;
I2CMasterSlaveAddrSet(I2C1_BASE, i2cAddr, false);
I2CMasterDataPut(I2C1_BASE, *i2c.data);
I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_START);
if(block)
while(i2cIsBusy);
return i2c.buffer;
}
void i2c_get_keycode (uint_fast16_t i2cAddr, keycode_callback_ptr callback)
{
while(i2cIsBusy);
i2c.keycode_callback = callback;
I2C_Receive(i2cAddr, 1, false);
}
#if TRINAMIC_ENABLE && TRINAMIC_I2C
static uint8_t axis = 0xFF;
TMC_spi_status_t tmc_spi_read (trinamic_motor_t driver, TMC_spi_datagram_t *datagram)
{
uint8_t *res;
TMC_spi_status_t status = 0;
while(i2cIsBusy);
if(driver.axis != axis) {
i2c.buffer[0] = driver.axis | 0x80;
I2C_Send(I2C_ADR_I2CBRIDGE, 1, true);
axis = driver.axis;
}
i2c.buffer[0] = datagram->addr.idx;
i2c.buffer[1] = 0;
i2c.buffer[2] = 0;
i2c.buffer[3] = 0;
i2c.buffer[4] = 0;
res = I2C_ReadRegister(I2C_ADR_I2CBRIDGE, 5, true);
status = (uint8_t)*res++;
datagram->payload.value = ((uint8_t)*res++ << 24);
datagram->payload.value |= ((uint8_t)*res++ << 16);
datagram->payload.value |= ((uint8_t)*res++ << 8);
datagram->payload.value |= (uint8_t)*res++;
return status;
}
TMC_spi_status_t tmc_spi_write (trinamic_motor_t driver, TMC_spi_datagram_t *datagram)
{
TMC_spi_status_t status = 0;
while(i2cIsBusy);
if(driver.axis != axis) {
i2c.buffer[0] = driver.axis | 0x80;
I2C_Send(I2C_ADR_I2CBRIDGE, 1, true);
while(i2cIsBusy);
axis = driver.axis;
}
datagram->addr.write = On;
i2c.buffer[0] = datagram->addr.value;
i2c.buffer[1] = (datagram->payload.value >> 24) & 0xFF;
i2c.buffer[2] = (datagram->payload.value >> 16) & 0xFF;
i2c.buffer[3] = (datagram->payload.value >> 8) & 0xFF;
i2c.buffer[4] = datagram->payload.value & 0xFF;
datagram->addr.write = Off;
I2C_Send(I2C_ADR_I2CBRIDGE, 5, true);
return status;
}
#endif
void i2c_init (void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1);
SysCtlPeripheralReset(SYSCTL_PERIPH_I2C1);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinConfigure(GPIO_PA6_I2C1SCL);
GPIOPinConfigure(GPIO_PA7_I2C1SDA);
GPIOPadConfigSet(GPIO_PORTA_BASE, GPIO_PIN_7, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD);
GPIOPinTypeI2CSCL(GPIO_PORTA_BASE, GPIO_PIN_6);
GPIOPinTypeI2C(GPIO_PORTA_BASE, GPIO_PIN_7);
I2CMasterInitExpClk(I2C1_BASE, SysCtlClockGet(), false);
I2CIntRegister(I2C1_BASE, I2C_interrupt_handler);
I2CMasterIntClear(I2C1_BASE);
I2CMasterIntEnable(I2C1_BASE);
static const periph_pin_t scl = {
.function = Output_SCK,
.group = PinGroup_I2C,
.port = (void *)GPIO_PORTA_BASE,
.pin = 6,
.mode = { .mask = PINMODE_OD }
};
static const periph_pin_t sda = {
.function = Bidirectional_SDA,
.group = PinGroup_I2C,
.port = (void *)GPIO_PORTA_BASE,
.pin = 7,
.mode = { .mask = PINMODE_OD }
};
hal.periph_port.register_pin(&scl);
hal.periph_port.register_pin(&sda);
}
static void I2C_interrupt_handler (void)
{
// based on code from https://e2e.ti.com/support/microcontrollers/tiva_arm/f/908/t/169882
// https://es.technikum-wien.at/ti-connected-launchpad/i2c_wrapper_public/blob/master/i2c_wrapper.c
I2CMasterIntClear(I2C1_BASE);
// if(I2CMasterErr(I2C1_BASE) == I2C_MASTER_ERR_NONE)
switch(i2c.state) {
case I2CState_Idle:
break;
case I2CState_SendNext:
I2CMasterDataPut(I2C1_BASE, *i2c.data++);
if(--i2c.count == 1)
i2c.state = I2CState_SendLast;
I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
break;
case I2CState_SendLast:
I2CMasterDataPut(I2C1_BASE, *i2c.data);
I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
i2c.state = I2CState_AwaitCompletion;
break;
case I2CState_AwaitCompletion:
i2c.count = 0;
i2c.state = I2CState_Idle;
break;
case I2CState_SendRegisterAddress:
I2CMasterSlaveAddrSet(I2C1_BASE, i2c.addr, true);
I2CMasterControl(I2C1_BASE, i2c.count == 1 ? I2C_MASTER_CMD_SINGLE_RECEIVE : I2C_MASTER_CMD_BURST_RECEIVE_START);
i2c.state = i2c.count == 1 ? I2CState_ReceiveLast : (i2c.count == 2 ? I2CState_ReceiveNextToLast : I2CState_ReceiveNext);
break;
case I2CState_ReceiveNext:
*i2c.data++ = I2CMasterDataGet(I2C1_BASE);
I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
if(--i2c.count == 2)
i2c.state = I2CState_ReceiveNextToLast;
break;
case I2CState_ReceiveNextToLast:
*i2c.data++ = I2CMasterDataGet(I2C1_BASE);
I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
i2c.count--;
i2c.state = I2CState_ReceiveLast;
break;
case I2CState_ReceiveLast:
*i2c.data = I2CMasterDataGet(I2C1_BASE);
i2c.count = 0;
i2c.state = I2CState_Idle;
#if KEYPAD_ENABLE == 1
if(i2c.keycode_callback) {
// if(GPIOIntStatus(I2C_STROBE_PORT, I2C_STROBE_PIN) != 0) { // only add keycode when key is still pressed
i2c.keycode_callback(*i2c.data);
i2c.keycode_callback = NULL;
}
#endif
break;
}
}
#endif