Pico GPS Teseo I2C
Loading...
Searching...
No Matches
port
pico
i2c
teseo_communicate.cpp
Go to the documentation of this file.
1
module
;
2
3
// for debug messages
4
#include <string>
5
// for memset
6
#include <cstring>
7
#include "hardware/gpio.h"
8
#include <stdio.h>
9
#include "pico/stdlib.h"
10
#include <algorithm>
11
12
export
module
port_pico_communicate
;
13
14
import
port_pico_reset
;
15
16
17
// for the moment, the library restricts how many sattelites it entertains.
18
// it influences the size of the read buffer (not a drama, this is a static buffer)
19
// it also influences the size of the vector that will accept replies that are "per sattelite"
20
// Currently, the code does not allow that the vector that holds these, grows (focus on embedded)
21
// later, this can be changed to allow flex, if you accept the dynamic
22
// memory growth impact (acceptable for larger systems like PC, processors, ...)
23
#define MAX_SATELLITE_REPLIES 7
24
25
#include "hardware/i2c.h"
26
#define I2C_PORT (i2c0)
27
#define I2C_BAUD (100 * 1000)
28
#define I2C_SDA (16)
29
#define I2C_SCL (17)
30
#define I2C_ADDR (0x3A)
31
#define BUFFSIZE (1024)
32
#define I2C_FAIL_AFTER_EMPTY_READS (1024U * 4)
33
34
35
// calculate 70 characters per satellite, + 60 for the status line
36
// many libraries limit the number of satelites to say 6
37
export
const
size_t
NMEA_MAX_REPLIES
=
MAX_SATELLITE_REPLIES
;
38
39
uint8_t
buf
[
BUFFSIZE
];
// read buffer, intentionally not initialised
40
41
export
void
initialize
() {
42
stdio_init_all();
43
// I2C is "open drain", pull ups to keep signal high when no data is being sent
44
// (not needed. board has pullups)
45
i2c_init(
I2C_PORT
,
I2C_BAUD
);
46
gpio_set_function(
I2C_SDA
, GPIO_FUNC_I2C);
47
gpio_set_function(
I2C_SCL
, GPIO_FUNC_I2C);
48
// gpio_pull_up(I2C_SDA);
49
// gpio_pull_up(I2C_SCL);
50
51
port_pico::reset_initialize
();
52
}
53
54
export
void
write
(const ::std::string& s) {
55
i2c_write_blocking(
I2C_PORT
,
I2C_ADDR
,
reinterpret_cast<
const
uint8_t*
>
(s.c_str()), s.length() +1,
false
);
56
return
;
57
}
58
59
export
void
read
(::std::string& s) {
60
memset (
buf
, 0,
BUFFSIZE
);
// initialise buffer before reading
61
bool
gotData =
false
;
62
unsigned
int
failures = 0U;
63
uint8_t *bufptr =
buf
;
64
do
{
65
i2c_read_blocking(
I2C_PORT
,
I2C_ADDR
, bufptr, 1,
false
);
66
if
(*bufptr != 0xff) {
67
gotData =
true
;
68
bufptr++;
69
}
else
if
(gotData) {
// we are done
70
*bufptr = 0;
71
bufptr =
buf
+
BUFFSIZE
;
72
}
else
{
73
*bufptr = 0;
74
failures++;
75
}
76
}
77
while
((bufptr -
buf
<
BUFFSIZE
) && (failures <
I2C_FAIL_AFTER_EMPTY_READS
));
78
s =
reinterpret_cast<
const
char
*
>
(
buf
);
79
return
;
80
}
I2C_SDA
#define I2C_SDA
Definition
teseo_communicate.cpp:28
I2C_SCL
#define I2C_SCL
Definition
teseo_communicate.cpp:29
initialize
void initialize()
Definition
teseo_communicate.cpp:41
I2C_ADDR
#define I2C_ADDR
Definition
teseo_communicate.cpp:30
BUFFSIZE
#define BUFFSIZE
Definition
teseo_communicate.cpp:31
read
void read(::std::string &s)
Definition
teseo_communicate.cpp:59
I2C_FAIL_AFTER_EMPTY_READS
#define I2C_FAIL_AFTER_EMPTY_READS
Definition
teseo_communicate.cpp:32
I2C_BAUD
#define I2C_BAUD
Definition
teseo_communicate.cpp:27
buf
uint8_t buf[BUFFSIZE]
Definition
teseo_communicate.cpp:39
MAX_SATELLITE_REPLIES
#define MAX_SATELLITE_REPLIES
Definition
teseo_communicate.cpp:23
write
void write(const ::std::string &s)
Definition
teseo_communicate.cpp:54
I2C_PORT
#define I2C_PORT
Definition
teseo_communicate.cpp:26
port_pico_communicate
port_pico_reset
port_pico::reset_initialize
void reset_initialize()
Definition
reset.cpp:25
NMEA_MAX_REPLIES
const size_t NMEA_MAX_REPLIES
Definition
test_gps_teseo_lib.cpp:5
Generated by
1.13.2