From 65301e5a0b382585887ea7b13e4eb9b0cb26c60f Mon Sep 17 00:00:00 2001
From: Karl Palsson <karlp@tweak.net.au>
Date: Tue, 21 Feb 2017 23:11:43 +0000
Subject: i2c-master: start progressing to extracting common i2c code

---
 tests/i2c-master/Makefile.stm32f4-disco |  2 +-
 tests/i2c-master/hw.h                   | 38 +++++++++++++++++++++++++++++++++
 tests/i2c-master/i2c-master.c           | 23 ++++++++++++++++++++
 tests/i2c-master/i2c-master.h           | 26 ++++++++++++++++++++++
 tests/i2c-master/main-stm32f4-disco.c   | 31 ++++++++++++---------------
 5 files changed, 102 insertions(+), 18 deletions(-)
 create mode 100644 tests/i2c-master/hw.h
 create mode 100644 tests/i2c-master/i2c-master.c
 create mode 100644 tests/i2c-master/i2c-master.h

(limited to 'tests')

diff --git a/tests/i2c-master/Makefile.stm32f4-disco b/tests/i2c-master/Makefile.stm32f4-disco
index 8621f2f..2456820 100644
--- a/tests/i2c-master/Makefile.stm32f4-disco
+++ b/tests/i2c-master/Makefile.stm32f4-disco
@@ -22,7 +22,7 @@ BUILD_DIR = bin-$(BOARD)
 SHARED_DIR = ../../shared
 
 CFILES = main-$(BOARD).c
-#CFILES += adc-power.c
+CFILES += i2c-master.c
 CFILES += trace.c trace_stdio.c
 
 VPATH += $(SHARED_DIR)
diff --git a/tests/i2c-master/hw.h b/tests/i2c-master/hw.h
new file mode 100644
index 0000000..7ce9c50
--- /dev/null
+++ b/tests/i2c-master/hw.h
@@ -0,0 +1,38 @@
+/*
+ * Feb 2017 Karl Palsson <karlp@tweak.net.au>
+ */
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#pragma once
+
+struct hw_detail
+{
+       uint32_t periph; /* eg: I2C1 */
+       uint32_t periph_rcc; /* eg: RCC_I2C1 */
+       uint32_t periph_rst; /* eg: RST_I2C1 */
+       uint32_t pins; /* eg: GPIO8 | GPIO9 */ /* ASSUMES SAME PORT*/
+       uint32_t port; /* eg GPIOB */
+       uint32_t port_rcc; /* eg RCC_GPIOB */
+};
+
+extern struct hw_detail hw_details;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+        * Expected to setup clocks, turn on all peripherals, and configure
+        * any gpios necessary.
+        * @param hw pointer to hw details necessary
+        */
+       void hw_setup(struct hw_detail* hw);
+
+       /* let devices have a status led */
+       void hw_set_led(bool val);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/tests/i2c-master/i2c-master.c b/tests/i2c-master/i2c-master.c
new file mode 100644
index 0000000..64c9cfe
--- /dev/null
+++ b/tests/i2c-master/i2c-master.c
@@ -0,0 +1,23 @@
+/*
+ * Feb 2017, Karl Palsson <karlp@tweak.net.au>
+ */
+
+#include <libopencm3/stm32/i2c.h>
+#include <libopencm3/stm32/rcc.h>
+#include "hw.h"
+#include "i2c-master.h"
+
+
+void i2cm_init(void) {
+	rcc_periph_clock_enable(hw_details.periph_rcc);
+	rcc_periph_reset_pulse(hw_details.periph_rst);
+	i2c_set_standard_mode(hw_details.periph);
+	i2c_enable_ack(hw_details.periph);
+	//i2c_set_dutycycle(hw_details.periph, I2C_CCR_DUTY_DIV2); /* default, no need to do this really */
+	i2c_set_clock_frequency(hw_details.periph, I2C_CR2_FREQ_42MHZ);
+	/* 42MHz / (100kHz * 2) */
+	i2c_set_ccr(hw_details.periph, 210);
+	/* standard mode, freqMhz+1*/
+	i2c_set_trise(hw_details.periph, 43);
+	i2c_peripheral_enable(hw_details.periph);
+}
\ No newline at end of file
diff --git a/tests/i2c-master/i2c-master.h b/tests/i2c-master/i2c-master.h
new file mode 100644
index 0000000..5e41a9a
--- /dev/null
+++ b/tests/i2c-master/i2c-master.h
@@ -0,0 +1,26 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+/* 
+ * File:   i2c-master.h
+ * Author: karlp
+ *
+ * Created on February 21, 2017, 10:59 PM
+ */
+
+#pragma once
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+	void i2cm_init(void);
+
+#ifdef __cplusplus
+}
+#endif
+
diff --git a/tests/i2c-master/main-stm32f4-disco.c b/tests/i2c-master/main-stm32f4-disco.c
index c74b5eb..f5594c0 100644
--- a/tests/i2c-master/main-stm32f4-disco.c
+++ b/tests/i2c-master/main-stm32f4-disco.c
@@ -12,12 +12,25 @@
 
 #include "trace.h"
 
+#include "hw.h"
+#include "i2c-master.h"
+
 #define LED_DISCO_GREEN_PORT GPIOD
 #define LED_DISCO_GREEN_PIN GPIO12
 
 #define CODEC_ADDRESS 0x4a
 
 
+struct hw_detail hw_details = {
+	.periph = I2C1,
+	.periph_rcc = RCC_I2C1,
+	.periph_rst = RST_I2C1,
+	.pins = GPIO6 | GPIO9, /* FIXME - only for onboard! */
+	.port = GPIOB,
+	.port_rcc = RCC_GPIOB,
+};
+
+
 static void codec_gpio_init(void)
 {
 	/* reset pin */
@@ -31,22 +44,6 @@ static void codec_gpio_init(void)
 	gpio_set_af(GPIOB, GPIO_AF4, GPIO6 | GPIO9);
 }
 
-static void codec_i2c_init(void)
-{
-	rcc_periph_clock_enable(RCC_I2C1);
-	i2c_peripheral_disable(I2C1);
-	i2c_reset(I2C1);
-	i2c_set_standard_mode(I2C1);
-	i2c_enable_ack(I2C1);
-	i2c_set_dutycycle(I2C1, I2C_CCR_DUTY_DIV2); /* default, no need to do this really */
-	i2c_set_clock_frequency(I2C1, I2C_CR2_FREQ_42MHZ);
-	/* 42MHz / (100kHz * 2) */
-	i2c_set_ccr(I2C1, 210);
-	/* standard mode, freqMhz+1*/
-	i2c_set_trise(I2C1, 43);
-	i2c_peripheral_enable(I2C1);
-}
-
 static void codec_init(void)
 {
 	int i;
@@ -60,7 +57,7 @@ static void codec_init(void)
 	}
 	gpio_set(GPIOD, GPIO4);
 
-	codec_i2c_init();
+	i2cm_init();
 }
 
 static int codec_write_reg(uint8_t reg, uint8_t val)
-- 
cgit