You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					88 lines
				
				1.8 KiB
			
		
		
			
		
	
	
					88 lines
				
				1.8 KiB
			| 
											3 years ago
										 | #include "common/i2c.h"
 | ||
| 
											5 years ago
										 | 
 | ||
|  | #include <fcntl.h>
 | ||
|  | #include <sys/ioctl.h>
 | ||
| 
											5 years ago
										 | #include <unistd.h>
 | ||
|  | 
 | ||
|  | #include <cassert>
 | ||
| 
											4 years ago
										 | #include <cstdio>
 | ||
| 
											5 years ago
										 | #include <stdexcept>
 | ||
|  | 
 | ||
| 
											3 years ago
										 | #include "common/util.h"
 | ||
|  | #include "common/swaglog.h"
 | ||
|  | #include "common/util.h"
 | ||
| 
											5 years ago
										 | 
 | ||
|  | #define UNUSED(x) (void)(x)
 | ||
|  | 
 | ||
|  | #ifdef QCOM2
 | ||
|  | // TODO: decide if we want to isntall libi2c-dev everywhere
 | ||
| 
											5 years ago
										 | extern "C" {
 | ||
|  |   #include <linux/i2c-dev.h>
 | ||
|  |   #include <i2c/smbus.h>
 | ||
|  | }
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											4 years ago
										 | I2CBus::I2CBus(uint8_t bus_id) {
 | ||
| 
											5 years ago
										 |   char bus_name[20];
 | ||
|  |   snprintf(bus_name, 20, "/dev/i2c-%d", bus_id);
 | ||
|  | 
 | ||
| 
											4 years ago
										 |   i2c_fd = HANDLE_EINTR(open(bus_name, O_RDWR));
 | ||
| 
											4 years ago
										 |   if(i2c_fd < 0) {
 | ||
| 
											5 years ago
										 |     throw std::runtime_error("Failed to open I2C bus");
 | ||
|  |   }
 | ||
|  | }
 | ||
|  | 
 | ||
| 
											4 years ago
										 | I2CBus::~I2CBus() {
 | ||
|  |   if(i2c_fd >= 0) { close(i2c_fd); }
 | ||
| 
											5 years ago
										 | }
 | ||
|  | 
 | ||
| 
											4 years ago
										 | int I2CBus::read_register(uint8_t device_address, uint register_address, uint8_t *buffer, uint8_t len) {
 | ||
| 
											5 years ago
										 |   int ret = 0;
 | ||
|  | 
 | ||
| 
											4 years ago
										 |   ret = HANDLE_EINTR(ioctl(i2c_fd, I2C_SLAVE, device_address));
 | ||
| 
											5 years ago
										 |   if(ret < 0) { goto fail; }
 | ||
|  | 
 | ||
|  |   ret = i2c_smbus_read_i2c_block_data(i2c_fd, register_address, len, buffer);
 | ||
|  |   if((ret < 0) || (ret != len)) { goto fail; }
 | ||
|  | 
 | ||
|  | fail:
 | ||
|  |   return ret;
 | ||
|  | }
 | ||
|  | 
 | ||
| 
											4 years ago
										 | int I2CBus::set_register(uint8_t device_address, uint register_address, uint8_t data) {
 | ||
| 
											5 years ago
										 |   int ret = 0;
 | ||
|  | 
 | ||
| 
											4 years ago
										 |   ret = HANDLE_EINTR(ioctl(i2c_fd, I2C_SLAVE, device_address));
 | ||
| 
											5 years ago
										 |   if(ret < 0) { goto fail; }
 | ||
|  | 
 | ||
|  |   ret = i2c_smbus_write_byte_data(i2c_fd, register_address, data);
 | ||
|  |   if(ret < 0) { goto fail; }
 | ||
|  | 
 | ||
|  | fail:
 | ||
|  |   return ret;
 | ||
|  | }
 | ||
|  | 
 | ||
|  | #else
 | ||
|  | 
 | ||
| 
											4 years ago
										 | I2CBus::I2CBus(uint8_t bus_id) {
 | ||
| 
											5 years ago
										 |   UNUSED(bus_id);
 | ||
|  |   i2c_fd = -1;
 | ||
|  | }
 | ||
|  | 
 | ||
| 
											4 years ago
										 | I2CBus::~I2CBus() {}
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											4 years ago
										 | int I2CBus::read_register(uint8_t device_address, uint register_address, uint8_t *buffer, uint8_t len) {
 | ||
| 
											5 years ago
										 |   UNUSED(device_address);
 | ||
|  |   UNUSED(register_address);
 | ||
|  |   UNUSED(buffer);
 | ||
|  |   UNUSED(len);
 | ||
|  |   return -1;
 | ||
|  | }
 | ||
|  | 
 | ||
| 
											4 years ago
										 | int I2CBus::set_register(uint8_t device_address, uint register_address, uint8_t data) {
 | ||
| 
											5 years ago
										 |   UNUSED(device_address);
 | ||
|  |   UNUSED(register_address);
 | ||
|  |   UNUSED(data);
 | ||
|  |   return -1;
 | ||
|  | }
 | ||
|  | #endif
 |