134 lines
4.3 KiB
Diff
134 lines
4.3 KiB
Diff
From e3f2b9e35f7c557ecea0cd92b763aac2f63bed59 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/204] 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 ec39112970..a383c71b27 100644
|
|
--- a/targets/targets.json
|
|
+++ b/targets/targets.json
|
|
@@ -9420,6 +9420,7 @@
|
|
},
|
|
"device_has": [
|
|
"ANALOGIN",
|
|
+ "I2C",
|
|
"PORT_IN",
|
|
"PORT_OUT",
|
|
"SERIAL",
|
|
--
|
|
2.39.1
|
|
|