aboutsummaryrefslogtreecommitdiff
path: root/main/i2c.c
diff options
context:
space:
mode:
authorThomasintAnker <thomasintanker1@gmail.com>2024-05-18 20:48:50 +0200
committerThomasintAnker <thomasintanker1@gmail.com>2024-05-18 20:48:50 +0200
commit56440df6b9810dbbc4b33171030970fa2fbe1ca1 (patch)
tree9a227b9397ca4fa464165fc4836ba8dd22275202 /main/i2c.c
parent1152ec32b6086c153dc41da59c9c451aa4465995 (diff)
Base i2c master functions
Diffstat (limited to 'main/i2c.c')
-rw-r--r--main/i2c.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/main/i2c.c b/main/i2c.c
new file mode 100644
index 0000000..7ab1d14
--- /dev/null
+++ b/main/i2c.c
@@ -0,0 +1,29 @@
+#include "i2c.h"
+
+#include <stdio.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <pico/stdlib.h>
+#include <hardware/i2c.h>
+
+void init_i2c() {
+ stdio_init_all();
+ i2c_init(i2c_default, 100 * 1000);
+
+ // Initialize I2C pins - sda(16), scl(17)
+ gpio_set_function(SDA_PIN, GPIO_FUNC_I2C);
+ gpio_set_function(SCL_PIN, GPIO_FUNC_I2C);
+
+ gpio_pull_up(SDA_PIN);
+ gpio_pull_up(SCL_PIN);
+}
+
+int read_i2c(uint8_t addr, uint8_t *output, size_t len) {
+ // false - finished with bus
+ return i2c_read_blocking (i2c_default, addr, output, len, false);
+}
+
+int write_i2c(uint8_t addr, uint8_t *input, size_t len) {
+ // true to keep master control of bus
+ return i2c_write_blocking (i2c_default, addr, input, len, true);
+}