-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy path0024-RP2040-add-I2C-peripheral-support.patch
133 lines (128 loc) · 4.3 KB
/
0024-RP2040-add-I2C-peripheral-support.patch
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
From d6cbdb2efee07a2ae881023b42522a71d9b54eb9 Mon Sep 17 00:00:00 2001
From: Alexander Entinger <cto@lxrobotics.com>
Date: Tue, 16 Feb 2021 06:29:59 +0100
Subject: [PATCH 024/157] RP2040: add I2C peripheral support
---
.../TARGET_RP2040/i2c_api.c | 88 +++++++++++++++++++
.../TARGET_RP2040/objects.h | 1 +
targets/targets.json | 1 +
3 files changed, 90 insertions(+)
create mode 100644 targets/TARGET_RASPBERRYPI/TARGET_RP2040/i2c_api.c
diff --git a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/i2c_api.c b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/i2c_api.c
new file mode 100644
index 0000000000..210279b3b0
--- /dev/null
+++ b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/i2c_api.c
@@ -0,0 +1,88 @@
+/******************************************************************************
+ * INCLUDE
+ ******************************************************************************/
+
+#include "mbed_assert.h"
+#include "i2c_api.h"
+#include "pinmap.h"
+#include "PeripheralPins.h"
+
+/******************************************************************************
+ * CONST
+ ******************************************************************************/
+
+static unsigned int const DEFAULT_I2C_BAUDRATE = 100 * 1000; /* 100 kHz */
+
+/******************************************************************************
+ * FUNCTION DEFINITION
+ ******************************************************************************/
+
+void i2c_init(i2c_t *obj, PinName sda, PinName scl)
+{
+ /* Verify if both pins belong to the same I2C peripheral. */
+ I2CName const i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
+ I2CName const i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
+ MBED_ASSERT(i2c_sda == i2c_scl);
+
+ /* Obtain the pointer to the I2C hardware instance. */
+ obj->dev = (i2c_inst_t *)pinmap_function(sda, PinMap_I2C_SDA);
+ obj->baudrate = DEFAULT_I2C_BAUDRATE;
+
+ /* Initialize the I2C module. */
+ _i2c_init(obj->dev, obj->baudrate);
+
+ /* Configure GPIO for I2C as alternate function. */
+ gpio_set_function(sda, GPIO_FUNC_I2C);
+ gpio_set_function(scl, GPIO_FUNC_I2C);
+
+ /* Enable pull-ups for I2C pins. */
+ gpio_pull_up(sda);
+ gpio_pull_up(scl);
+}
+
+void i2c_frequency(i2c_t *obj, int hz)
+{
+ obj->baudrate = i2c_set_baudrate(obj->dev, hz);
+}
+
+int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
+{
+ int const bytes_read = i2c_read_blocking(obj->dev,
+ (uint8_t)address,
+ (uint8_t *)data,
+ (size_t)length,
+ /* nostop = */(stop == 0));
+ if (bytes_read < 0)
+ return I2C_ERROR_NO_SLAVE;
+ else
+ return bytes_read;
+}
+
+int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
+{
+ int const bytes_written = i2c_write_blocking(obj->dev,
+ address,
+ (const uint8_t *)data,
+ (size_t)length,
+ /* nostop = */(stop == 0));
+ if (bytes_written < 0)
+ return I2C_ERROR_NO_SLAVE;
+ else
+ return bytes_written;
+}
+
+void i2c_reset(i2c_t *obj)
+{
+ i2c_deinit(obj->dev);
+ _i2c_init(obj->dev, obj->baudrate);
+}
+
+const PinMap *i2c_master_sda_pinmap()
+{
+ return PinMap_I2C_SDA;
+}
+
+const PinMap *i2c_master_scl_pinmap()
+{
+ return PinMap_I2C_SCL;
+}
diff --git a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/objects.h b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/objects.h
index 16dab0daaf..061cee5908 100644
--- a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/objects.h
+++ b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/objects.h
@@ -94,6 +94,7 @@ struct serial_s {
struct i2c_s {
i2c_inst_t * dev;
+ unsigned int baudrate;
};
struct spi_s {
diff --git a/targets/targets.json b/targets/targets.json
index 98997cd193..3f8b505fd3 100644
--- a/targets/targets.json
+++ b/targets/targets.json
@@ -8973,6 +8973,7 @@
},
"device_has": [
"ANALOGIN",
+ "I2C",
"PORT_IN",
"PORT_OUT",
"SERIAL",
--
2.36.0