Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #390 from sifive/add-i2c-example
Browse files Browse the repository at this point in the history
Add i2c example
  • Loading branch information
nategraff-sifive authored Oct 17, 2019
2 parents dedbe99 + 8320263 commit 2cdbeb4
Show file tree
Hide file tree
Showing 26 changed files with 339 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,6 @@
[submodule "software/atomics"]
path = software/atomics
url = https://github.com/sifive/example-atomics.git
[submodule "software/example-i2c"]
path = software/example-i2c
url = https://github.com/sifive/example-i2c.git
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ operating systems to RISC-V.
- atomics
- Demonstrates how to use the Metal Atomic API to leverage the RISC-V
atomic instruction set.
- example-i2c
- Demonstrates usage of the I2C API to communicate with I2C slaves.

### Setting up the SDK ###

Expand Down
3 changes: 3 additions & 0 deletions bsp/freedom-e310-arty/metal-inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ extern __inline__ int __metal_driver_sifive_gpio0_interrupt_lines(struct metal_g
/* --------------------- sifive_gpio_switch ------------ */


/* --------------------- sifive_i2c0 ------------ */


/* --------------------- sifive_rtc0 ------------ */


Expand Down
10 changes: 10 additions & 0 deletions bsp/freedom-e310-arty/metal.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@

#define METAL_MAX_GPIO_INTERRUPTS 16

#define METAL_MAX_I2C0_INTERRUPTS 0

#define __METAL_SERIAL_10013000_INTERRUPTS 1

#define METAL_MAX_UART_INTERRUPTS 1
Expand Down Expand Up @@ -500,6 +502,9 @@ static __inline__ int __metal_driver_sifive_gpio0_interrupt_lines(struct metal_g
/* --------------------- sifive_gpio_switch ------------ */


/* --------------------- sifive_i2c0 ------------ */


/* --------------------- sifive_rtc0 ------------ */

static __inline__ unsigned long __metal_driver_sifive_spi0_control_base(struct metal_spi *spi)
Expand Down Expand Up @@ -742,6 +747,11 @@ struct __metal_driver_sifive_gpio_led *__metal_led_table[] = {
__asm__ (".weak __metal_switch_table");
struct __metal_driver_sifive_gpio_switch *__metal_switch_table[] = {
NULL };
#define __METAL_DT_MAX_I2CS 0

__asm__ (".weak __metal_i2c_table");
struct __metal_driver_sifive_i2c0 *__metal_i2c_table[] = {
NULL };
#define __METAL_DT_MAX_RTCS 0

__asm__ (".weak __metal_rtc_table");
Expand Down
3 changes: 3 additions & 0 deletions bsp/qemu-sifive-e31/metal-inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ extern __inline__ char * __metal_driver_sifive_gpio_led_label(struct metal_led *
/* --------------------- sifive_gpio_switch ------------ */


/* --------------------- sifive_i2c0 ------------ */


/* --------------------- sifive_rtc0 ------------ */
extern __inline__ unsigned long __metal_driver_sifive_rtc0_control_base(const struct metal_rtc *const rtc);
extern __inline__ unsigned long __metal_driver_sifive_rtc0_control_size(const struct metal_rtc *const rtc);
Expand Down
10 changes: 10 additions & 0 deletions bsp/qemu-sifive-e31/metal.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@

#define METAL_MAX_GPIO_INTERRUPTS 16

#define METAL_MAX_I2C0_INTERRUPTS 0

#define __METAL_SERIAL_10013000_INTERRUPTS 1

#define METAL_MAX_UART_INTERRUPTS 1
Expand Down Expand Up @@ -600,6 +602,9 @@ static __inline__ char * __metal_driver_sifive_gpio_led_label(struct metal_led *
/* --------------------- sifive_gpio_switch ------------ */


/* --------------------- sifive_i2c0 ------------ */


/* --------------------- sifive_rtc0 ------------ */
static __inline__ unsigned long __metal_driver_sifive_rtc0_control_base(const struct metal_rtc *const rtc)
{
Expand Down Expand Up @@ -1089,6 +1094,11 @@ struct __metal_driver_sifive_gpio_led *__metal_led_table[] = {
__asm__ (".weak __metal_switch_table");
struct __metal_driver_sifive_gpio_switch *__metal_switch_table[] = {
NULL };
#define __METAL_DT_MAX_I2CS 0

__asm__ (".weak __metal_i2c_table");
struct __metal_driver_sifive_i2c0 *__metal_i2c_table[] = {
NULL };
#define __METAL_DT_MAX_RTCS 1

__asm__ (".weak __metal_rtc_table");
Expand Down
3 changes: 3 additions & 0 deletions bsp/qemu-sifive-s51/metal-inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ extern __inline__ char * __metal_driver_sifive_gpio_led_label(struct metal_led *
/* --------------------- sifive_gpio_switch ------------ */


/* --------------------- sifive_i2c0 ------------ */


/* --------------------- sifive_rtc0 ------------ */
extern __inline__ unsigned long __metal_driver_sifive_rtc0_control_base(const struct metal_rtc *const rtc);
extern __inline__ unsigned long __metal_driver_sifive_rtc0_control_size(const struct metal_rtc *const rtc);
Expand Down
10 changes: 10 additions & 0 deletions bsp/qemu-sifive-s51/metal.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@

#define METAL_MAX_GPIO_INTERRUPTS 16

#define METAL_MAX_I2C0_INTERRUPTS 0

#define __METAL_SERIAL_10013000_INTERRUPTS 1

#define METAL_MAX_UART_INTERRUPTS 1
Expand Down Expand Up @@ -600,6 +602,9 @@ static __inline__ char * __metal_driver_sifive_gpio_led_label(struct metal_led *
/* --------------------- sifive_gpio_switch ------------ */


/* --------------------- sifive_i2c0 ------------ */


/* --------------------- sifive_rtc0 ------------ */
static __inline__ unsigned long __metal_driver_sifive_rtc0_control_base(const struct metal_rtc *const rtc)
{
Expand Down Expand Up @@ -1089,6 +1094,11 @@ struct __metal_driver_sifive_gpio_led *__metal_led_table[] = {
__asm__ (".weak __metal_switch_table");
struct __metal_driver_sifive_gpio_switch *__metal_switch_table[] = {
NULL };
#define __METAL_DT_MAX_I2CS 0

__asm__ (".weak __metal_i2c_table");
struct __metal_driver_sifive_i2c0 *__metal_i2c_table[] = {
NULL };
#define __METAL_DT_MAX_RTCS 1

__asm__ (".weak __metal_rtc_table");
Expand Down
3 changes: 3 additions & 0 deletions bsp/qemu-sifive-u54/metal-inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ extern __inline__ int __metal_driver_sifive_plic0_context_ids(int hartid);
/* --------------------- sifive_gpio_switch ------------ */


/* --------------------- sifive_i2c0 ------------ */


/* --------------------- sifive_rtc0 ------------ */


Expand Down
10 changes: 10 additions & 0 deletions bsp/qemu-sifive-u54/metal.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@

#define METAL_MAX_GPIO_INTERRUPTS 0

#define METAL_MAX_I2C0_INTERRUPTS 0

#define __METAL_UART_10013000_INTERRUPTS 1

#define METAL_MAX_UART_INTERRUPTS 1
Expand Down Expand Up @@ -311,6 +313,9 @@ static __inline__ int __metal_driver_sifive_plic0_context_ids(int hartid)
/* --------------------- sifive_gpio_switch ------------ */


/* --------------------- sifive_i2c0 ------------ */


/* --------------------- sifive_rtc0 ------------ */


Expand Down Expand Up @@ -506,6 +511,11 @@ struct __metal_driver_sifive_gpio_led *__metal_led_table[] = {
__asm__ (".weak __metal_switch_table");
struct __metal_driver_sifive_gpio_switch *__metal_switch_table[] = {
NULL };
#define __METAL_DT_MAX_I2CS 0

__asm__ (".weak __metal_i2c_table");
struct __metal_driver_sifive_i2c0 *__metal_i2c_table[] = {
NULL };
#define __METAL_DT_MAX_RTCS 0

__asm__ (".weak __metal_rtc_table");
Expand Down
3 changes: 3 additions & 0 deletions bsp/qemu-sifive-u54mc/metal-inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ extern __inline__ int __metal_driver_sifive_plic0_context_ids(int hartid);
/* --------------------- sifive_gpio_switch ------------ */


/* --------------------- sifive_i2c0 ------------ */


/* --------------------- sifive_rtc0 ------------ */


Expand Down
10 changes: 10 additions & 0 deletions bsp/qemu-sifive-u54mc/metal.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@

#define METAL_MAX_GPIO_INTERRUPTS 0

#define METAL_MAX_I2C0_INTERRUPTS 0

#define __METAL_UART_10013000_INTERRUPTS 1

#define METAL_MAX_UART_INTERRUPTS 1
Expand Down Expand Up @@ -456,6 +458,9 @@ static __inline__ int __metal_driver_sifive_plic0_context_ids(int hartid)
/* --------------------- sifive_gpio_switch ------------ */


/* --------------------- sifive_i2c0 ------------ */


/* --------------------- sifive_rtc0 ------------ */


Expand Down Expand Up @@ -654,6 +659,11 @@ struct __metal_driver_sifive_gpio_led *__metal_led_table[] = {
__asm__ (".weak __metal_switch_table");
struct __metal_driver_sifive_gpio_switch *__metal_switch_table[] = {
NULL };
#define __METAL_DT_MAX_I2CS 0

__asm__ (".weak __metal_i2c_table");
struct __metal_driver_sifive_i2c0 *__metal_i2c_table[] = {
NULL };
#define __METAL_DT_MAX_RTCS 0

__asm__ (".weak __metal_rtc_table");
Expand Down
17 changes: 17 additions & 0 deletions bsp/sifive-hifive-unleashed/metal-inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ extern __inline__ int __metal_driver_sifive_gpio0_interrupt_lines(struct metal_g
/* --------------------- sifive_gpio_switch ------------ */


/* --------------------- sifive_i2c0 ------------ */
extern __inline__ unsigned long __metal_driver_sifive_i2c0_control_base(struct metal_i2c *i2c);
extern __inline__ unsigned long __metal_driver_sifive_i2c0_control_size(struct metal_i2c *i2c);
extern __inline__ int __metal_driver_sifive_i2c0_num_interrupts(struct metal_i2c *i2c);
extern __inline__ struct metal_interrupt * __metal_driver_sifive_i2c0_interrupt_parent(struct metal_i2c *i2c);
extern __inline__ int __metal_driver_sifive_i2c0_interrupt_line(struct metal_i2c *i2c);
extern __inline__ struct metal_clock * __metal_driver_sifive_i2c0_clock(struct metal_i2c *i2c);
extern __inline__ struct __metal_driver_sifive_gpio0 * __metal_driver_sifive_i2c0_pinmux(struct metal_i2c *i2c);
extern __inline__ unsigned long __metal_driver_sifive_i2c0_pinmux_output_selector(struct metal_i2c *i2c);
extern __inline__ unsigned long __metal_driver_sifive_i2c0_pinmux_source_selector(struct metal_i2c *i2c);


/* --------------------- sifive_rtc0 ------------ */


Expand Down Expand Up @@ -317,6 +329,11 @@ struct __metal_driver_sifive_gpio0 __metal_dt_gpio_10060000 = {
.gpio.vtable = &__metal_driver_vtable_sifive_gpio0.gpio,
};

/* From i2c@10030000 */
struct __metal_driver_sifive_i2c0 __metal_dt_i2c_10030000 = {
.i2c.vtable = &__metal_driver_vtable_sifive_i2c0.i2c,
};

/* From spi@10040000 */
struct __metal_driver_sifive_spi0 __metal_dt_spi_10040000 = {
.spi.vtable = &__metal_driver_vtable_sifive_spi0.spi,
Expand Down
97 changes: 97 additions & 0 deletions bsp/sifive-hifive-unleashed/metal.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@

#define METAL_MAX_GPIO_INTERRUPTS 16

#define __METAL_I2C_10030000_INTERRUPTS 0

#define METAL_MAX_I2C0_INTERRUPTS 0

#define __METAL_SERIAL_10010000_INTERRUPTS 1

#define __METAL_SERIAL_10011000_INTERRUPTS 1
Expand All @@ -78,6 +82,7 @@
#include <metal/drivers/riscv_plic0.h>
#include <metal/pmp.h>
#include <metal/drivers/sifive_gpio0.h>
#include <metal/drivers/sifive_i2c0.h>
#include <metal/drivers/sifive_spi0.h>
#include <metal/drivers/sifive_test0.h>
#include <metal/drivers/sifive_uart0.h>
Expand Down Expand Up @@ -145,6 +150,9 @@ struct metal_pmp __metal_dt_pmp;
/* From gpio@10060000 */
struct __metal_driver_sifive_gpio0 __metal_dt_gpio_10060000;

/* From i2c@10030000 */
struct __metal_driver_sifive_i2c0 __metal_dt_i2c_10030000;

/* From spi@10040000 */
struct __metal_driver_sifive_spi0 __metal_dt_spi_10040000;

Expand Down Expand Up @@ -664,6 +672,89 @@ static __inline__ int __metal_driver_sifive_gpio0_interrupt_lines(struct metal_g
/* --------------------- sifive_gpio_switch ------------ */


/* --------------------- sifive_i2c0 ------------ */
static __inline__ unsigned long __metal_driver_sifive_i2c0_control_base(struct metal_i2c *i2c)
{
if ((uintptr_t)i2c == (uintptr_t)&__metal_dt_i2c_10030000) {
return METAL_SIFIVE_I2C0_10030000_BASE_ADDRESS;
}
else {
return 0;
}
}

static __inline__ unsigned long __metal_driver_sifive_i2c0_control_size(struct metal_i2c *i2c)
{
if ((uintptr_t)i2c == (uintptr_t)&__metal_dt_i2c_10030000) {
return METAL_SIFIVE_I2C0_10030000_SIZE;
}
else {
return 0;
}
}

static __inline__ struct metal_clock * __metal_driver_sifive_i2c0_clock(struct metal_i2c *i2c)
{
if ((uintptr_t)i2c == (uintptr_t)&__metal_dt_i2c_10030000) {
return (struct metal_clock *)&__metal_dt_tlclk.clock;
}
else {
return NULL;
}
}

static __inline__ struct __metal_driver_sifive_gpio0 * __metal_driver_sifive_i2c0_pinmux(struct metal_i2c *i2c)
{
if ((uintptr_t)i2c == (uintptr_t)&__metal_dt_i2c_10030000) {
return NULL;
}
else {
return NULL;
}
}

static __inline__ unsigned long __metal_driver_sifive_i2c0_pinmux_output_selector(struct metal_i2c *i2c)
{
if ((uintptr_t)i2c == (uintptr_t)&__metal_dt_i2c_10030000) {
return 0;
}
else {
return 0;
}
}

static __inline__ unsigned long __metal_driver_sifive_i2c0_pinmux_source_selector(struct metal_i2c *i2c)
{
if ((uintptr_t)i2c == (uintptr_t)&__metal_dt_i2c_10030000) {
return 0;
}
else {
return 0;
}
}

static __inline__ int __metal_driver_sifive_i2c0_num_interrupts(struct metal_i2c *i2c)
{
return METAL_MAX_I2C0_INTERRUPTS;
}

static __inline__ struct metal_interrupt * __metal_driver_sifive_i2c0_interrupt_parent(struct metal_i2c *i2c)
{
return NULL;
}

static __inline__ int __metal_driver_sifive_i2c0_interrupt_line(struct metal_i2c *i2c)
{
if ((uintptr_t)i2c == (uintptr_t)&__metal_dt_i2c_10030000) {
return 0;
}
else {
return 0;
}
}



/* --------------------- sifive_rtc0 ------------ */

static __inline__ unsigned long __metal_driver_sifive_spi0_control_base(struct metal_spi *spi)
Expand Down Expand Up @@ -1022,6 +1113,12 @@ struct __metal_driver_sifive_gpio_led *__metal_led_table[] = {
__asm__ (".weak __metal_switch_table");
struct __metal_driver_sifive_gpio_switch *__metal_switch_table[] = {
NULL };
#define __METAL_DT_MAX_I2CS 1

__asm__ (".weak __metal_i2c_table");
struct __metal_driver_sifive_i2c0 *__metal_i2c_table[] = {
&__metal_dt_i2c_10030000};

#define __METAL_DT_MAX_RTCS 0

__asm__ (".weak __metal_rtc_table");
Expand Down
1 change: 1 addition & 0 deletions bsp/sifive-hifive1-revb/design.dts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
reg = <0x10016000 0x1000>;
reg-names = "control";
clocks = <&hfclk>;
pinmux = <&gpio0 0x3000 0x3000>;
};
uart1: serial@10023000 {
compatible = "sifive,uart0";
Expand Down
Loading

0 comments on commit 2cdbeb4

Please sign in to comment.