1 /*
2  * Copyright 2012 Michael Ossmann <mike@ossmann.com>
3  * Copyright 2012 Jared Boone <jared@sharebrained.com>
4  *
5  * This file is part of HackRF.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #include "i2c_lpc.h"
24 
25 #include <libopencm3/lpc43xx/i2c.h>
26 
27 /* FIXME return i2c0 status from each function */
28 
i2c_lpc_start(i2c_bus_t * const bus,const void * const _config)29 void i2c_lpc_start(i2c_bus_t* const bus, const void* const _config) {
30 	const i2c_lpc_config_t* const config = _config;
31 
32 	const uint32_t port = (uint32_t)bus->obj;
33 	i2c_init(port, config->duty_cycle_count);
34 }
35 
i2c_lpc_stop(i2c_bus_t * const bus)36 void i2c_lpc_stop(i2c_bus_t* const bus) {
37 	const uint32_t port = (uint32_t)bus->obj;
38 	i2c_disable(port);
39 }
40 
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)41 void i2c_lpc_transfer(i2c_bus_t* const bus,
42 	const uint_fast8_t slave_address,
43 	const uint8_t* const data_tx, const size_t count_tx,
44 	uint8_t* const data_rx, const size_t count_rx
45 ) {
46 	const uint32_t port = (uint32_t)bus->obj;
47 	i2c_tx_start(port);
48 	i2c_tx_byte(port, (slave_address << 1) | I2C_WRITE);
49 	for(size_t i=0; i<count_tx; i++) {
50 		i2c_tx_byte(port, data_tx[i]);
51 	}
52 
53 	if( data_rx ) {
54 		i2c_tx_start(port);
55 		i2c_tx_byte(port, (slave_address << 1) | I2C_READ);
56 		for(size_t i=0; i<count_rx; i++) {
57 			data_rx[i] = i2c_rx_byte(port);
58 		}
59 	}
60 
61 	i2c_stop(port);
62 }
63