libpynq (release 5EWC0-2023 version 0.2.0 of 2023-08-28 20:33)
Loading...
Searching...
No Matches
i2cps.c
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2016, Xilinx, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION). HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 *****************************************************************************/
32/******************************************************************************
33 *
34 *
35 * @file i2cps.c
36 *
37 * Functions to interact with linux I2C. No safe checks here, so users must
38 * know what they are doing.
39 *
40 * <pre>
41 * MODIFICATION HISTORY:
42 *
43 * Ver Who Date Changes
44 * ----- --- ------- -----------------------------------------------
45 * 1.00a gn 02/03/16 release
46 * 1.00b yrq 08/31/16 add license header
47 *
48 * </pre>
49 *
50 *****************************************************************************/
51
52#include "i2cps.h"
53#include <fcntl.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57#include <sys/ioctl.h>
58#include <unistd.h>
59
60int setI2C(unsigned int index, long slave_addr) {
61 int i2c_fd;
62 char buf[50];
63 sprintf(buf, "/dev/i2c-%d", index);
64 // printf("buf = %s \n",buf);
65 if ((i2c_fd = open(buf, O_RDWR)) < 0) {
66 return -1;
67 }
68 if (ioctl(i2c_fd, I2C_SLAVE, slave_addr) < 0) {
69 return -1;
70 }
71 return i2c_fd;
72}
73
74int unsetI2C(int i2c_fd) {
75 close(i2c_fd);
76 return 0;
77}
78
79int writeI2C_asFile(int i2c_fd, unsigned char writebuffer[],
80 unsigned char bytes) {
81 unsigned char bytesWritten = write(i2c_fd, writebuffer, bytes);
82 if (bytes != bytesWritten) {
83 return -1;
84 }
85 return 0;
86}
87
88int readI2C_asFile(int i2c_fd, unsigned char readbuffer[],
89 unsigned char bytes) {
90 unsigned char bytesRead = read(i2c_fd, readbuffer, bytes);
91 if (bytes != bytesRead)
92 return -1;
93 return 0;
94}
int setI2C(unsigned int index, long slave_addr)
Definition i2cps.c:2
int writeI2C_asFile(int i2c_fd, unsigned char writebuffer[], unsigned char bytes)
Definition i2cps.c:4
int unsetI2C(int i2c_fd)
Definition i2cps.c:3
int readI2C_asFile(int i2c_fd, unsigned char readbuffer[], unsigned char bytes)
Definition i2cps.c:6