Sensorian  1.0
C API Reference Guide Library
i2c.c
Go to the documentation of this file.
1 /****************************************************************************
2  * Copyright (C) 2015 Sensorian
3  * *
4  * This file is part of Sensorian. *
5  * *
6  * Sensorian is free software: you can redistribute it and/or modify it *
7  * under the terms of the GNU Lesser General Public License as published *
8  * by the Free Software Foundation, either version 3 of the License, or *
9  * (at your option) any later version. *
10  * *
11  * Sensorian is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with Sensorian. *
18  * If not, see <http://www.gnu.org/licenses/>. *
19  ****************************************************************************/
20 
29 #include <stdlib.h>
30 #include "i2c.h"
31 
37 void I2C_Initialize(unsigned char address)
38 {
39  if (!bcm2835_init()) //Configure I2C pins
40  {
41  printf("BCM libray error.\n");
42  }
43  bcm2835_i2c_end(); //Close I2C peripheral to reconfigure it
44 
45  bcm2835_i2c_begin(); //Set pins as I2C
46  bcm2835_i2c_set_baudrate(baudrate); //Set I2C baudrate
47  bcm2835_i2c_setClockDivider(BCM2835_I2C_CLOCK_DIVIDER_2500); //100 Khz
48  bcm2835_i2c_setSlaveAddress(address); //Set device address
49 }
50 
56 void I2C_WriteByte(char bdata)
57 { char data = bdata;
58  bcm2835_i2c_write(&data, 1);
59 }
60 
67 void I2C_WriteByteRegister(unsigned char reg,unsigned char data)
68 {
69  unsigned char wr_buf[2];
70 
71  wr_buf[0] = reg;
72  wr_buf[1] = data;
73 
74  bcm2835_i2c_write((const char *)wr_buf, 2);
75 }
76 
83 void I2C_WriteWordRegister(unsigned char reg, unsigned char* data)
84 {
85  unsigned char wr_buf[3];
86 
87  wr_buf[0] = reg;
88  wr_buf[1] = data[0];
89  wr_buf[2] = data[1];
90 
91  bcm2835_i2c_write((const char *)wr_buf, 3);
92 }
93 
101 void I2C_WriteByteArray(char reg, char* data, unsigned int length)
102 {
103  char* wr_buf = (char*) malloc(sizeof(char) * length);
104  if (wr_buf==NULL)
105  {
106  printf("Error allocating memory!\n"); //print an error message
107  }
108 
109  wr_buf[0] = reg;
110  for(unsigned int i = 1;i<length;i++)
111  {
112  wr_buf[i] = data[i];
113  }
114 
115  bcm2835_i2c_write((const char *)wr_buf, length);
116 }
117 
123 unsigned char I2C_ReadByteRegister(char reg)
124 {
125  char val = 0;
126 
127  bcm2835_i2c_read_register_rs(&reg,&val,1);
128 
129  return val;
130  }
131 
139 void I2C_ReadByteArray(char reg,char *buffer,unsigned int length)
140 {
141  bcm2835_i2c_read_register_rs(&reg,buffer,length);
142 }
143 
149 unsigned int I2C_ReadWordRegisterRS(char reg)
150 {
151  char cmd[1] = {reg};
152  char receive[2] = {0};
153  bcm2835_i2c_write_read_rs(cmd,1,receive,2);
154 
155  return (receive[0]<<8)|receive[1];
156 }
157 
162 unsigned int I2C_ReadWordPresetPointer(void)
163 {
164  char val[2] = {0};
165  bcm2835_i2c_read(val,2);
166  unsigned int data = (val[0] << 8)|val[1];
167 
168  return data;
169  }
170 
175  void I2C_Close(void)
176  {
177  bcm2835_i2c_end();
178 }
179 
I2C driver.
unsigned char buffer[256]
Definition: main.c:74
void I2C_WriteByte(char bdata)
Writes a byte value to the I2C bus. This assumes the register pointer is preset.
Definition: i2c.c:55
void I2C_Close(void)
Closes the I2C peripheral.
Definition: i2c.c:174
void I2C_WriteByteArray(char reg, char *data, unsigned int length)
Writes a buffer array to the registers.
Definition: i2c.c:100
void I2C_WriteByteRegister(unsigned char reg, unsigned char data)
Writes a byte value to a register address.
Definition: i2c.c:66
#define baudrate
Definition: i2c.h:34
void I2C_Initialize(unsigned char address)
Initializes the I2C peripheral.
Definition: i2c.c:36
unsigned int I2C_ReadWordPresetPointer(void)
Read the value of a register that has already been select via the address pointer.
Definition: i2c.c:161
void I2C_WriteWordRegister(unsigned char reg, unsigned char *data)
Writes a word value (16 bit) to a register address.
Definition: i2c.c:82
unsigned char I2C_ReadByteRegister(char reg)
Reads a byte from a register.
Definition: i2c.c:122
unsigned long address
Definition: main.c:76
unsigned int I2C_ReadWordRegisterRS(char reg)
Readm result from a word length register.
Definition: i2c.c:148
void I2C_ReadByteArray(char reg, char *buffer, unsigned int length)
Initializes the I2C peripheral.
Definition: i2c.c:138