-
Notifications
You must be signed in to change notification settings - Fork 11
/
RPiPICO_UBLOX_I2C_FORWARDER.ino
220 lines (177 loc) · 5.96 KB
/
RPiPICO_UBLOX_I2C_FORWARDER.ino
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
/*
RPi Pico uBlox I2C and UART adapter
24-Jan-2024
Copyright (C) 2024 C Turvey (aka sourcer32, clive1)
All Rights Reserved
If this saves you several man-hours/days consider https://paypal.me/cliveone
24-Jan-2024 [email protected]
PICO
GP25 LED
1 D0 GP00 TX 40 VBUS (5V)
2 D1 GP01 RX 39 VSYS
3 GND 38 GND
4 GP02 37 3V3_EN
5 GP03 36 3V3_OUT
6 GP04 SDA 35 ADC_VREF
7 GP05 SCL 34 GP28 ADC2
8 GND 33 GND
9 GP06 32 GP27 ADC1
10 GP07 31 GP26 ADC0
11 GP08 30 RUN
12 GP09 29 GP22
13 GND 28 GND
14 GP10 27 GP21
15 GP11 26 GP20
16 GP12 25 GP19
17 GP13 24 GP18
18 GND 23 GND
19 GP14 22 GP17
20 GP15 21 GP16
Serial1 GP00/GP01
Wire GP04/GP05
C:\Users\xxx\AppData\Local\Arduino15\packages\arduino\hardware\mbed_rp2040\4.0.8\variants\RASPBERRY_PI_PICO\pins_arduino.h
Would power uBlox / SparkFun boards via VBUS (5V) as appropriate.
Common ground
Pin 6 (GP04) SDA
Pin 7 (GP05) SCL
*/
#include <Wire.h> // Arduino standard I2C/Two-Wire Library
//***************************************************************************
// Quick I2C
//***************************************************************************
#define UBX_I2C_ADDR 0x42
// Assumptions, you'll send at least 3-bytes, ideally a whole packet
// min packet size with nothing would be 6 bytes
#define MAX_I2C_UBX_SEND 32 // This is some mythical limitation of Arduino Library
int ubx_send_i2c(size_t size, const uint8_t *buffer) // [email protected]
{
while(size)
{
size_t length = size;
// A method to get at least 5 bytes left to send in final packet
if (length > MAX_I2C_UBX_SEND) length = MAX_I2C_UBX_SEND - 5;
// Begin transmission to the I2C slave device
Wire.beginTransmission(UBX_I2C_ADDR);
// Queue data array for transmission to the I2C device
if (Wire.write(buffer, length) != length) // Was it accepted?
{
Wire.endTransmission(true); // Send and Close the I2C interface.
return(0); // Failed
}
// Transmit the bytes and a stop message to release the I2C bus.
if (Wire.endTransmission(true))
return(0); // Failed
size -= length;
buffer += length;
}
return(1); // Succeed
}
//***************************************************************************
int ubx_read_i2c(uint8_t reg, size_t size, uint8_t *buffer)
{
// Begin transmission to the I2C slave device
Wire.beginTransmission(UBX_I2C_ADDR);
Wire.write(®, sizeof(reg));
if (Wire.endTransmission(false))
return(0); // Failed
Wire.requestFrom(UBX_I2C_ADDR, size); // Request bytes
while(Wire.available() < size) {}; // Wait for delivery
Wire.readBytes(buffer, size);
return(1); // Succeed
}
//***************************************************************************
int ubx_write_i2c_byte(uint8_t reg, uint8_t byte)
{
// Begin transmission to the I2C slave device
Wire.beginTransmission(UBX_I2C_ADDR);
Wire.write(®, sizeof(reg));
Wire.write(&byte, sizeof(byte));
if (Wire.endTransmission(true))
return(0); // Failed
return(1); // Succeed
}
//***************************************************************************
int ubx_write(uint8_t *buffer, size_t size)
{
if (size >= 3)
return(ubx_send_i2c(size, buffer));
else
{
while(size--)
{
if (!ubx_write_i2c_byte(0xFF, *buffer++)) return(0);
}
return(1);
}
}
//***************************************************************************
void ubx_read_flush(void)
{
uint8_t buffer[64]; // Buffer of some reasonable size to pull down content
if (ubx_read_i2c(0xFD, 2, buffer)) // Read a byte count from registers 0xFD/0xFE, 2-bytes
{
uint16_t count = ((uint16_t)buffer[0] << 8) + (uint16_t)buffer[1]; // MSB first
while(count) // Consume available data
{
size_t length = count;
if (length > sizeof(buffer)) length = sizeof(buffer);
if (!ubx_read_i2c(0xFF, length, buffer)) break; // Read block from register 0xFF
Serial.write(buffer, length); // Forward to PC
count -= length;
}
}
}
//****************************************************************************
#define BEAT 200
void loop(void)
{
static uint32_t start = millis();
int len;
len = Serial.available();
#if 0
while(len > 0) // Do all pending data
#else
if (len > 0) // Do blocks of bytes for efficiency
#endif
{
uint8_t buffer[128];
int xferlen = len;
if (len > sizeof(buffer)) xferlen = sizeof(buffer);
Serial.readBytes(buffer, xferlen);
ubx_write(buffer, xferlen); // Forward to GPS
len -=xferlen;
}
if ((millis() - start) >= BEAT) // HeatBeat - GP25
{
static uint32_t led = 0;
ubx_read_flush(); // Periodic so as not to overwhelm with I2C traffic/queries
digitalWrite(LED_BUILTIN, (led & 1) ? HIGH : LOW);
led++;
start += BEAT;
}
}
//****************************************************************************
void setup(void)
{
pinMode(LED_BUILTIN, OUTPUT);
Wire.begin(); // Bring up the I2C
#define USB_BAUD 38400
Serial.begin(USB_BAUD); // USB to PC
while(!Serial) // Wait for attachment
{
static uint32_t start = millis();
if ((millis() - start) >= 100) // Fast HeatBeat - GREEN LED GP25
{
static uint32_t led = 0;
digitalWrite(LED_BUILTIN, (led & 1) ? HIGH : LOW);
led++;
start += 100;
}
}
Serial.println();
Serial.println("RPiPICO_UBLOX_I2C_FORWARDER Starting");
// Send something to precipitate a response, unstall interface
uint8_t ubx_nav_posllh_req[] = { 0xB5,0x62,0x01,0x02,0x00,0x00,0x03,0x0A }; // Zero payload request form
ubx_send_i2c(sizeof(ubx_nav_posllh_req), ubx_nav_posllh_req);
}
//****************************************************************************