SELKIELogger  1.0.0
I2CConnection.c
1 /*
2  * Copyright (C) 2023 Swansea University
3  *
4  * This file is part of the SELKIELogger suite of tools.
5  *
6  * SELKIELogger is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation, either version 3 of the License, or (at your option)
9  * any later version.
10  *
11  * SELKIELogger is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this SELKIELogger product.
18  * If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "I2CConnection.h"
22 
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <string.h>
26 #include <unistd.h>
27 
34 int i2c_openConnection(const char *bus) {
35  errno = 0;
36  int handle = open(bus, O_RDWR);
37  if (handle < 0) { return -1; }
38  return handle;
39 }
40 
46 void i2c_closeConnection(int handle) {
47  close(handle);
48 }
49 
58 int16_t i2c_swapbytes(const int16_t in) {
59  uint16_t tmp = in;
60  return (int16_t)((tmp >> 8) + ((tmp & 0xFF) << 8));
61 }
int i2c_openConnection(const char *bus)
Set up a connection to the specified bus.
Definition: I2CConnection.c:34
void i2c_closeConnection(int handle)
Close existing connection.
Definition: I2CConnection.c:46
int16_t i2c_swapbytes(const int16_t in)
Swap word byte order.
Definition: I2CConnection.c:58