Pico GPS Teseo I2C
Loading...
Searching...
No Matches
teseo_communicate.cpp
Go to the documentation of this file.
1module;
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
12export module port_pico_communicate;
13
14import 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
38
39uint8_t buf[BUFFSIZE]; // read buffer, intentionally not initialised
40
41export 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
52}
53
54export 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
59export 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}
#define I2C_SDA
#define I2C_SCL
void initialize()
#define I2C_ADDR
#define BUFFSIZE
void read(::std::string &s)
#define I2C_FAIL_AFTER_EMPTY_READS
#define I2C_BAUD
uint8_t buf[BUFFSIZE]
#define MAX_SATELLITE_REPLIES
void write(const ::std::string &s)
#define I2C_PORT
void reset_initialize()
Definition reset.cpp:25
const size_t NMEA_MAX_REPLIES