1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
From eb0dea483fa120fc8c4252ba0c7331879fe3d189 Mon Sep 17 00:00:00 2001
From: Jared Boone <jboone@earfeast.com>
Date: Wed, 12 Nov 2014 13:56:58 -0800
Subject: [PATCH 39/68] I2C: Finish extracting from Si5351C code.
---
firmware/common/hackrf_core.c | 31 ++++++++++++++++++++---
firmware/common/i2c_bus.c | 35 ++++++++++++++++++++++++++
firmware/common/i2c_bus.h | 50 +++++++++++++++++++++++++++++++++++++
firmware/common/i2c_lpc.c | 57 +++++++++++++++++++++++++++++++++++++++++++
firmware/common/i2c_lpc.h | 41 +++++++++++++++++++++++++++++++
firmware/common/si5351c_drv.c | 34 ++------------------------
firmware/common/si5351c_drv.h | 3 +++
firmware/hackrf-common.cmake | 2 ++
firmware/libopencm3 | 2 +-
9 files changed, 219 insertions(+), 36 deletions(-)
create mode 100644 firmware/common/i2c_bus.c
create mode 100644 firmware/common/i2c_bus.h
create mode 100644 firmware/common/i2c_lpc.c
create mode 100644 firmware/common/i2c_lpc.h
--- a/firmware/common/hackrf_core.c
+++ b/firmware/common/hackrf_core.c
@@ -34,7 +34,8 @@
#include "w25q80bv_target.h"
#include "sgpio.h"
#include "rf_path.h"
-#include <libopencm3/lpc43xx/i2c.h>
+#include "i2c_bus.h"
+#include "i2c_lpc.h"
#include <libopencm3/lpc43xx/cgu.h>
#include <libopencm3/lpc43xx/gpio.h>
#include <libopencm3/lpc43xx/scu.h>
@@ -42,7 +43,28 @@
#define WAIT_CPU_CLOCK_INIT_DELAY (10000)
+i2c_bus_t i2c0 = {
+ .obj = (void*)I2C0_BASE,
+ .init = i2c_lpc_init,
+ .transfer = i2c_lpc_transfer,
+};
+
+i2c_bus_t i2c1 = {
+ .obj = (void*)I2C1_BASE,
+ .init = i2c_lpc_init,
+ .transfer = i2c_lpc_transfer,
+};
+
+const i2c_lpc_config_t i2c_config_si5351c_slow_clock = {
+ .duty_cycle_count = 15,
+};
+
+const i2c_lpc_config_t i2c_config_si5351c_fast_clock = {
+ .duty_cycle_count = 255,
+};
+
si5351c_driver_t clock_gen = {
+ .bus = &i2c0,
.i2c_address = 0x60,
};
@@ -356,7 +378,7 @@
/* use IRC as clock source for APB3 */
CGU_BASE_APB3_CLK = CGU_BASE_APB3_CLK_CLK_SEL(CGU_SRC_IRC);
- i2c0_init(15);
+ i2c_bus_init(clock_gen.bus, &i2c_config_si5351c_slow_clock);
si5351c_disable_all_outputs(&clock_gen);
si5351c_disable_oeb_pin_control(&clock_gen);
@@ -428,7 +450,7 @@
//FIXME disable I2C
/* Kick I2C0 down to 400kHz when we switch over to APB1 clock = 204MHz */
- i2c0_init(255);
+ i2c_bus_init(clock_gen.bus, &i2c_config_si5351c_fast_clock);
/*
* 12MHz clock is entering LPC XTAL1/OSC input now. On
@@ -656,6 +678,9 @@
spi_init(&spi_ssp1, &ssp_config_max2837);
spi_init(&rffc5071_spi, NULL);
+ /* enable input on SCL and SDA pins */
+ SCU_SFSI2C0 = SCU_I2C0_NOMINAL;
+
rf_path_pin_setup();
/* Configure external clock in */
--- /dev/null
+++ b/firmware/common/i2c_bus.c
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
+ *
+ * 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 "i2c_bus.h"
+
+void i2c_bus_init(i2c_bus_t* const bus, const void* const config) {
+ bus->init(bus, config);
+}
+
+void i2c_bus_transfer(
+ i2c_bus_t* const bus,
+ const uint_fast8_t slave_address,
+ const uint8_t* const tx, const size_t tx_count,
+ uint8_t* const rx, const size_t rx_count
+) {
+ bus->transfer(bus, slave_address, tx, tx_count, rx, rx_count);
+}
--- /dev/null
+++ b/firmware/common/i2c_bus.h
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
+ *
+ * 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 __I2C_BUS_H__
+#define __I2C_BUS_H__
+
+#include <stdint.h>
+#include <stddef.h>
+
+struct i2c_bus_t;
+typedef struct i2c_bus_t i2c_bus_t;
+
+struct i2c_bus_t {
+ void* const obj;
+ void (*init)(i2c_bus_t* const bus, const void* const config);
+ void (*transfer)(
+ i2c_bus_t* const bus,
+ const uint_fast8_t slave_address,
+ const uint8_t* const tx, const size_t tx_count,
+ uint8_t* const rx, const size_t rx_count
+ );
+};
+
+void i2c_bus_init(i2c_bus_t* const bus, const void* const config);
+void i2c_bus_transfer(
+ i2c_bus_t* const bus,
+ const uint_fast8_t slave_address,
+ const uint8_t* const tx, const size_t tx_count,
+ uint8_t* const rx, const size_t rx_count
+);
+
+#endif/*__I2C_BUS_H__*/
--- /dev/null
+++ b/firmware/common/i2c_lpc.c
@@ -0,0 +1,57 @@
+/*
+ * 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 "i2c_lpc.h"
+
+#include <libopencm3/lpc43xx/i2c.h>
+
+/* FIXME return i2c0 status from each function */
+
+void i2c_lpc_init(i2c_bus_t* const bus, const void* const _config) {
+ const i2c_lpc_config_t* const config = _config;
+
+ const uint32_t port = (uint32_t)bus->obj;
+ i2c_init(port, config->duty_cycle_count);
+}
+
+void i2c_lpc_transfer(i2c_bus_t* const bus,
+ const uint_fast8_t slave_address,
+ const uint8_t* const data_tx, const size_t count_tx,
+ uint8_t* const data_rx, const size_t count_rx
+) {
+ const uint32_t port = (uint32_t)bus->obj;
+ i2c_tx_start(port);
+ i2c_tx_byte(port, (slave_address << 1) | I2C_WRITE);
+ for(size_t i=0; i<count_tx; i++) {
+ i2c_tx_byte(port, data_tx[i]);
+ }
+
+ if( data_rx ) {
+ i2c_tx_start(port);
+ i2c_tx_byte(port, (slave_address << 1) | I2C_READ);
+ for(size_t i=0; i<count_rx; i++) {
+ data_rx[i] = i2c_rx_byte(port);
+ }
+ }
+
+ i2c_stop(port);
+}
--- /dev/null
+++ b/firmware/common/i2c_lpc.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
+ *
+ * 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 __I2C_LPC_H__
+#define __I2C_LPC_H__
+
+#include <stdint.h>
+#include <stddef.h>
+
+#include "i2c_bus.h"
+
+typedef struct i2c_lpc_config_t {
+ const uint16_t duty_cycle_count;
+} i2c_lpc_config_t;
+
+void i2c_lpc_init(i2c_bus_t* const bus, const void* const config);
+void i2c_lpc_transfer(i2c_bus_t* const bus,
+ const uint_fast8_t slave_address,
+ const uint8_t* const data_tx, const size_t count_tx,
+ uint8_t* const data_rx, const size_t count_rx
+);
+
+#endif/*__I2C_LPC_H__*/
--- a/firmware/common/si5351c_drv.c
+++ b/firmware/common/si5351c_drv.c
@@ -22,36 +22,6 @@
#include "si5351c_drv.h"
-#include <libopencm3/lpc43xx/i2c.h>
-
-#define SI5351C_I2C_ADDR (0x60 << 1)
-
-/* FIXME return i2c0 status from each function */
-
-static void i2c0_transfer(si5351c_driver_t* const drv,
- const uint_fast8_t address,
- const uint8_t* const data_tx, const size_t count_tx,
- uint8_t* const data_rx, const size_t count_rx
-) {
- (void)drv;
-
- i2c0_tx_start();
- i2c0_tx_byte((address << 1) | I2C_WRITE);
- for(size_t i=0; i<count_tx; i++) {
- i2c0_tx_byte(data_tx[i]);
- }
-
- if( data_rx ) {
- i2c0_tx_start();
- i2c0_tx_byte((address << 1) | I2C_READ);
- for(size_t i=0; i<count_rx; i++) {
- data_rx[i] = i2c0_rx_byte();
- }
- }
-
- i2c0_stop();
-}
-
/* write to single register */
void si5351c_write_single(si5351c_driver_t* const drv, uint8_t reg, uint8_t val)
{
@@ -64,7 +34,7 @@
{
const uint8_t data_tx[] = { reg };
uint8_t data_rx[] = { 0x00 };
- i2c0_transfer(drv, drv->i2c_address, data_tx, 1, data_rx, 1);
+ i2c_bus_transfer(drv->bus, drv->i2c_address, data_tx, 1, data_rx, 1);
return data_rx[0];
}
@@ -74,5 +44,5 @@
*/
void si5351c_write(si5351c_driver_t* const drv, const uint8_t* const data, const size_t data_count)
{
- i2c0_transfer(drv, drv->i2c_address, data, data_count, NULL, 0);
+ i2c_bus_transfer(drv->bus, drv->i2c_address, data, data_count, NULL, 0);
}
--- a/firmware/common/si5351c_drv.h
+++ b/firmware/common/si5351c_drv.h
@@ -31,7 +31,10 @@
#include <stdint.h>
#include <stddef.h>
+#include "i2c_bus.h"
+
typedef struct {
+ i2c_bus_t* const bus;
uint8_t i2c_address;
} si5351c_driver_t;
|