forked from grblHAL/RP2040
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c.c
194 lines (141 loc) · 4.85 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
/*
i2c.c - I2C support for EEPROM, keypad and Trinamic plugins
Part of grblHAL driver for RP2040
Copyright (c) 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 <stdio.h>
#include <string.h>
#include "hardware/gpio.h"
#include "hardware/i2c.h"
#include "i2c.h"
#include "grbl/hal.h"
#if KEYPAD_ENABLE == 1
#include "keypad/keypad.h"
#endif
//#define QI2C_PORT i2c0
#define MAX_PAGE_SIZE 64
#define I2CN_PORT(port) I2Cn(port)
#define I2Cn(port) i2c ## port
#if I2C_ENABLE
#define QI2C_PORT I2CN_PORT(I2C_PORT)
void I2C_Init (void)
{
gpio_set_function(I2C_SDA, GPIO_FUNC_I2C);
gpio_set_function(I2C_SCL, GPIO_FUNC_I2C);
i2c_init(QI2C_PORT, 100000UL);
static const periph_pin_t scl = {
.function = Output_SCK,
.group = PinGroup_I2C,
.pin = I2C_SCL,
.mode = { .mask = PINMODE_OD }
};
static const periph_pin_t sda = {
.function = Bidirectional_SDA,
.group = PinGroup_I2C,
.pin = I2C_SDA,
.mode = { .mask = PINMODE_OD }
};
hal.periph_port.register_pin(&scl);
hal.periph_port.register_pin(&sda);
}
void I2C_Send (uint32_t i2cAddr, uint8_t *buf, uint16_t bytes, bool block)
{
i2c_write_blocking(QI2C_PORT, i2cAddr, buf, bytes, false);
}
uint8_t *I2C_ReadRegister (uint32_t i2cAddr, uint8_t *buf, uint16_t bytes, bool block)
{
i2c_write_blocking(QI2C_PORT, i2cAddr, buf, 1, true);
i2c_read_blocking(QI2C_PORT, i2cAddr, buf, bytes, false);
return buf;
}
#endif
#if EEPROM_ENABLE
nvs_transfer_result_t i2c_nvs_transfer (nvs_transfer_t *i2c, bool read)
{
static uint8_t txbuf[MAX_PAGE_SIZE + 2];
int retval = 0;
if(i2c->word_addr_bytes == 2) {
txbuf[0] = i2c->word_addr >> 8;
txbuf[1] = i2c->word_addr & 0xFF;
} else
txbuf[0] = i2c->word_addr;
if(!read)
memcpy(&txbuf[i2c->word_addr_bytes], i2c->data, i2c->count);
if(read) {
i2c_write_blocking(QI2C_PORT, i2c->address, txbuf, i2c->word_addr_bytes, true);
retval = i2c_read_blocking(QI2C_PORT, i2c->address, i2c->data, i2c->count, false);
} else {
retval = i2c_write_blocking(QI2C_PORT, i2c->address, txbuf, i2c->count + i2c->word_addr_bytes, false);
#if !EEPROM_IS_FRAM
hal.delay_ms(5, NULL);
#endif
}
i2c->data += i2c->count;
return retval == PICO_ERROR_GENERIC ? NVS_TransferResult_Failed : NVS_TransferResult_OK;
}
#endif
#if KEYPAD_ENABLE == 1
static uint8_t keycode = 0;
static keycode_callback_ptr keypad_callback = NULL;
void I2C_GetKeycode (uint32_t i2cAddr, keycode_callback_ptr callback)
{
uint8_t c;
keycode = 0;
keypad_callback = callback;
if(i2c_read_blocking(QI2C_PORT, KEYPAD_I2CADDR, &c, 1, false) == 1)
keypad_callback(c);
}
/*
void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
{
if(keypad_callback && keycode != 0) {
keypad_callback(keycode);
keypad_callback = NULL;
}
}
*/
#endif
#if TRINAMIC_ENABLE && TRINAMIC_I2C
static const uint8_t tmc_addr = I2C_ADR_I2CBRIDGE << 1;
static TMC2130_status_t TMC_I2C_ReadRegister (TMC2130_t *driver, TMC2130_datagram_t *reg)
{
uint8_t tmc_reg, buffer[5] = {0};
TMC2130_status_t status = {0};
if((tmc_reg = TMCI2C_GetMapAddress((uint8_t)(driver ? (uint32_t)driver->cs_pin : 0), reg->addr).value) == 0xFF) {
return status; // unsupported register
}
HAL_I2C_Mem_Read(&i2c_port, tmc_addr, tmc_reg, I2C_MEMADD_SIZE_8BIT, buffer, 5, 100);
status.value = buffer[0];
reg->payload.value = buffer[4];
reg->payload.value |= buffer[3] << 8;
reg->payload.value |= buffer[2] << 16;
reg->payload.value |= buffer[1] << 24;
return status;
}
static TMC2130_status_t TMC_I2C_WriteRegister (TMC2130_t *driver, TMC2130_datagram_t *reg)
{
uint8_t tmc_reg, buffer[4];
TMC2130_status_t status = {0};
reg->addr.write = 1;
tmc_reg = TMCI2C_GetMapAddress((uint8_t)(driver ? (uint32_t)driver->cs_pin : 0), reg->addr).value;
reg->addr.write = 0;
if(tmc_reg != 0xFF) {
buffer[0] = (reg->payload.value >> 24) & 0xFF;
buffer[1] = (reg->payload.value >> 16) & 0xFF;
buffer[2] = (reg->payload.value >> 8) & 0xFF;
buffer[3] = reg->payload.value & 0xFF;
HAL_I2C_Mem_Write(&i2c_port, tmc_addr, tmc_reg, I2C_MEMADD_SIZE_8BIT, buffer, 4, 100);
}
return status;
}
#endif