From 0bf84d974e94d4f612a1287b0a8901af4f729503 Mon Sep 17 00:00:00 2001
From: Jared Boone <jboone@earfeast.com>
Date: Tue, 4 Nov 2014 09:58:31 -0800
Subject: [PATCH 01/68] Si5351C: Extract low-level driver code.

---
 firmware/common/hackrf_core.c          |  1 +
 firmware/common/si5351c.c              | 50 +----------------------
 firmware/common/si5351c.h              |  5 ---
 firmware/common/si5351c_drv.c          | 74 ++++++++++++++++++++++++++++++++++
 firmware/common/si5351c_drv.h          | 41 +++++++++++++++++++
 firmware/hackrf-common.cmake           |  1 +
 firmware/hackrf_usb/usb_api_register.c |  2 +-
 7 files changed, 120 insertions(+), 54 deletions(-)
 create mode 100644 firmware/common/si5351c_drv.c
 create mode 100644 firmware/common/si5351c_drv.h

diff --git a/firmware/common/hackrf_core.c b/firmware/common/hackrf_core.c
index b862ba4..cd1c800 100644
--- a/firmware/common/hackrf_core.c
+++ b/firmware/common/hackrf_core.c
@@ -23,6 +23,7 @@
 
 #include "hackrf_core.h"
 #include "si5351c.h"
+#include "si5351c_drv.h"
 #include "max2837.h"
 #include "rffc5071.h"
 #include "sgpio.h"
diff --git a/firmware/common/si5351c.c b/firmware/common/si5351c.c
index 58b614d..3be5041 100644
--- a/firmware/common/si5351c.c
+++ b/firmware/common/si5351c.c
@@ -21,56 +21,10 @@
  */
 
 #include "si5351c.h"
-#include <libopencm3/lpc43xx/i2c.h>
 
-enum pll_sources active_clock_source;
-
-/* FIXME return i2c0 status from each function */
-
-/* write to single register */
-void si5351c_write_single(uint8_t reg, uint8_t val)
-{
-	i2c0_tx_start();
-	i2c0_tx_byte(SI5351C_I2C_ADDR | I2C_WRITE);
-	i2c0_tx_byte(reg);
-	i2c0_tx_byte(val);
-	i2c0_stop();
-}
-
-/* read single register */
-uint8_t si5351c_read_single(uint8_t reg)
-{
-	uint8_t val;
-
-	/* set register address with write */
-	i2c0_tx_start();
-	i2c0_tx_byte(SI5351C_I2C_ADDR | I2C_WRITE);
-	i2c0_tx_byte(reg);
-
-	/* read the value */
-	i2c0_tx_start();
-	i2c0_tx_byte(SI5351C_I2C_ADDR | I2C_READ);
-	val = i2c0_rx_byte();
-	i2c0_stop();
+#include "si5351c_drv.h"
 
-	return val;
-}
-
-/*
- * Write to one or more contiguous registers. data[0] should be the first
- * register number, one or more values follow.
- */
-void si5351c_write(uint8_t* const data, const uint_fast8_t data_count)
-{
-	uint_fast8_t i;
-
-	i2c0_tx_start();
-	i2c0_tx_byte(SI5351C_I2C_ADDR | I2C_WRITE);
-	
-	for (i = 0; i < data_count; i++)
-		i2c0_tx_byte(data[i]);
-	i2c0_stop();
-}
+enum pll_sources active_clock_source;
 
 /* Disable all CLKx outputs. */
 void si5351c_disable_all_outputs()
diff --git a/firmware/common/si5351c.h b/firmware/common/si5351c.h
index b62279d..fcc2d2b 100644
--- a/firmware/common/si5351c.h
+++ b/firmware/common/si5351c.h
@@ -31,7 +31,6 @@ extern "C"
 #include <stdint.h>
 
 #define SI_INTDIV(x)  (x*128-512)
-#define SI5351C_I2C_ADDR (0x60 << 1)
 
 #define SI5351C_CLK_POWERDOWN	(1<<7)
 #define SI5351C_CLK_INT_MODE	(1<<6)
@@ -76,10 +75,6 @@ void si5351c_configure_multisynth(const uint_fast8_t ms_number,
 void si5351c_configure_clock_control(const enum pll_sources source);
 void si5351c_enable_clock_outputs();
 void si5351c_set_int_mode(const uint_fast8_t ms_number, const uint_fast8_t on);
-
-void si5351c_write_single(uint8_t reg, uint8_t val);
-uint8_t si5351c_read_single(uint8_t reg);
-void si5351c_write(uint8_t* const data, const uint_fast8_t data_count);
 void si5351c_set_clock_source(const enum pll_sources source);
 void si5351c_activate_best_clock_source(void);
 
diff --git a/firmware/common/si5351c_drv.c b/firmware/common/si5351c_drv.c
new file mode 100644
index 0000000..e63912c
--- /dev/null
+++ b/firmware/common/si5351c_drv.c
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2012 Michael Ossmann <mike@ossmann.com>
+ * Copyright 2012 Jared Boone <jared@sharebrained.com>
+ *
+ * This file is part of HackRF.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include "si5351c_drv.h"
+
+#include <libopencm3/lpc43xx/i2c.h>
+
+#define SI5351C_I2C_ADDR (0x60 << 1)
+
+/* FIXME return i2c0 status from each function */
+
+/* write to single register */
+void si5351c_write_single(uint8_t reg, uint8_t val)
+{
+	i2c0_tx_start();
+	i2c0_tx_byte(SI5351C_I2C_ADDR | I2C_WRITE);
+	i2c0_tx_byte(reg);
+	i2c0_tx_byte(val);
+	i2c0_stop();
+}
+
+/* read single register */
+uint8_t si5351c_read_single(uint8_t reg)
+{
+	uint8_t val;
+
+	/* set register address with write */
+	i2c0_tx_start();
+	i2c0_tx_byte(SI5351C_I2C_ADDR | I2C_WRITE);
+	i2c0_tx_byte(reg);
+
+	/* read the value */
+	i2c0_tx_start();
+	i2c0_tx_byte(SI5351C_I2C_ADDR | I2C_READ);
+	val = i2c0_rx_byte();
+	i2c0_stop();
+
+	return val;
+}
+
+/*
+ * Write to one or more contiguous registers. data[0] should be the first
+ * register number, one or more values follow.
+ */
+void si5351c_write(uint8_t* const data, const uint_fast8_t data_count)
+{
+	uint_fast8_t i;
+
+	i2c0_tx_start();
+	i2c0_tx_byte(SI5351C_I2C_ADDR | I2C_WRITE);
+	
+	for (i = 0; i < data_count; i++)
+		i2c0_tx_byte(data[i]);
+	i2c0_stop();
+}
diff --git a/firmware/common/si5351c_drv.h b/firmware/common/si5351c_drv.h
new file mode 100644
index 0000000..501b221
--- /dev/null
+++ b/firmware/common/si5351c_drv.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2012 Michael Ossmann <mike@ossmann.com>
+ * Copyright 2012 Jared Boone <jared@sharebrained.com>
+ *
+ * This file is part of HackRF.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifndef __SI5351C_DRV_H
+#define __SI5351C_DRV_H
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#include <stdint.h>
+
+void si5351c_write_single(uint8_t reg, uint8_t val);
+uint8_t si5351c_read_single(uint8_t reg);
+void si5351c_write(uint8_t* const data, const uint_fast8_t data_count);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __SI5351C_DRV_H */
diff --git a/firmware/hackrf-common.cmake b/firmware/hackrf-common.cmake
index df5d5c9..9b4fb9a 100644
--- a/firmware/hackrf-common.cmake
+++ b/firmware/hackrf-common.cmake
@@ -134,6 +134,7 @@ macro(DeclareTargets)
 		${PATH_HACKRF_FIRMWARE_COMMON}/sgpio.c
 		${PATH_HACKRF_FIRMWARE_COMMON}/rf_path.c
 		${PATH_HACKRF_FIRMWARE_COMMON}/si5351c.c
+		${PATH_HACKRF_FIRMWARE_COMMON}/si5351c_drv.c
 		${PATH_HACKRF_FIRMWARE_COMMON}/max2837.c
 		${PATH_HACKRF_FIRMWARE_COMMON}/max5864.c
 		${PATH_HACKRF_FIRMWARE_COMMON}/rffc5071.c
diff --git a/firmware/hackrf_usb/usb_api_register.c b/firmware/hackrf_usb/usb_api_register.c
index d822c92..9478e7c 100644
--- a/firmware/hackrf_usb/usb_api_register.c
+++ b/firmware/hackrf_usb/usb_api_register.c
@@ -24,7 +24,7 @@
 
 #include <usb_queue.h>
 #include <max2837.h>
-#include <si5351c.h>
+#include <si5351c_drv.h>
 #include <rffc5071.h>
 
 #include <stddef.h>
-- 
2.1.4

