From 6947560e75b012a703979b5141f82ec2fc73b932 Mon Sep 17 00:00:00 2001 From: Hasan Unlu Date: Tue, 28 Aug 2018 22:12:26 -0700 Subject: HiFive-1 board bit-bang i2c demo Bit-bang i2c which is running around 400KHz. MPU6050 is used as an example device. --- software/i2c_demo/.gitignore | 1 + software/i2c_demo/Makefile | 6 + software/i2c_demo/i2c_demo.c | 318 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 325 insertions(+) create mode 100644 software/i2c_demo/.gitignore create mode 100644 software/i2c_demo/Makefile create mode 100644 software/i2c_demo/i2c_demo.c (limited to 'software') diff --git a/software/i2c_demo/.gitignore b/software/i2c_demo/.gitignore new file mode 100644 index 0000000..7a8011b --- /dev/null +++ b/software/i2c_demo/.gitignore @@ -0,0 +1 @@ +i2c_demo diff --git a/software/i2c_demo/Makefile b/software/i2c_demo/Makefile new file mode 100644 index 0000000..ef29ef2 --- /dev/null +++ b/software/i2c_demo/Makefile @@ -0,0 +1,6 @@ +TARGET = i2c_demo +C_SRCS += i2c_demo.c +CFLAGS += -O2 -fno-builtin-printf + +BSP_BASE = ../../bsp +include $(BSP_BASE)/env/common.mk diff --git a/software/i2c_demo/i2c_demo.c b/software/i2c_demo/i2c_demo.c new file mode 100644 index 0000000..4011c09 --- /dev/null +++ b/software/i2c_demo/i2c_demo.c @@ -0,0 +1,318 @@ +#include +#include +#include "platform.h" +#include +#include +#include +#include "stdatomic.h" + +#define MPU6050_ADDR 0x68 + +#define MPU6050_RA_ACCEL_XOUT_H 0x3B +#define MPU6050_RA_ACCEL_XOUT_L 0x3C +#define MPU6050_RA_ACCEL_YOUT_H 0x3D +#define MPU6050_RA_ACCEL_YOUT_L 0x3E +#define MPU6050_RA_ACCEL_ZOUT_H 0x3F +#define MPU6050_RA_ACCEL_ZOUT_L 0x40 +#define MPU6050_RA_TEMP_OUT_H 0x41 +#define MPU6050_RA_TEMP_OUT_L 0x42 +#define MPU6050_RA_GYRO_XOUT_H 0x43 +#define MPU6050_RA_GYRO_XOUT_L 0x44 +#define MPU6050_RA_GYRO_YOUT_H 0x45 +#define MPU6050_RA_GYRO_YOUT_L 0x46 +#define MPU6050_RA_GYRO_ZOUT_H 0x47 +#define MPU6050_RA_GYRO_ZOUT_L 0x48 +#define MPU6050_RA_PWR_MGMT_1 0x6B +#define MPU6050_RA_WHO_AM_I 0x75 + +#define SDA PIN_8_OFFSET +#define SCL PIN_9_OFFSET + +#define LONG 25 // Equals ~400KHz +#define SHORT 5 + +//busy wait for the specified time +void wait_ms(uint64_t ms) { + static const uint64_t ms_tick = RTC_FREQ/1000; + volatile uint64_t * mtime = (uint64_t*) (CLINT_CTRL_ADDR + CLINT_MTIME); + uint64_t then = (ms_tick * ms) + *mtime; + while(*mtime