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 
28 #include <stdlib.h>
29 #include "i2c.h"
30 
36 void I2C_Initialize(unsigned char address)
37 {
38  if (!bcm2835_init()) //Configure I2C pins
39  {
40  printf("BCM libray error.\n");
41  }
42  bcm2835_i2c_end(); //Close I2C peripheral to reconfigure it
43 
44  bcm2835_i2c_begin(); //Set pins as I2C
45  bcm2835_i2c_set_baudrate(baudrate); //Set I2C baudrate
46  bcm2835_i2c_setClockDivider(BCM2835_I2C_CLOCK_DIVIDER_2500); //100 Khz
47  bcm2835_i2c_setSlaveAddress(address); //Set device address
48 }
49 
55 void I2C_WriteByte(char bdata)
56 { char data = bdata;
57  bcm2835_i2c_write(&data, 1);
58 }
59 
66 void I2C_WriteByteRegister(unsigned char reg,unsigned char data)
67 {
68  unsigned char wr_buf[2];
69 
70  wr_buf[0] = reg;
71  wr_buf[1] = data;
72 
73  bcm2835_i2c_write((const char *)wr_buf, 2);
74 }
75 
82 void I2C_WriteWordRegister(unsigned char reg, unsigned char* data)
83 {
84  unsigned char wr_buf[3];
85 
86  wr_buf[0] = reg;
87  wr_buf[1] = data[0];
88  wr_buf[2] = data[1];
89 
90  bcm2835_i2c_write((const char *)wr_buf, 3);
91 }
92 
100 void I2C_WriteByteArray(char reg, char* data, unsigned int length)
101 {
102  char* wr_buf = (char*) malloc(sizeof(char) * length);
103  if (wr_buf==NULL)
104  {
105  printf("Error allocating memory!\n"); //print an error message
106  }
107 
108  wr_buf[0] = reg;
109  for(unsigned int i = 1;i<length;i++)
110  {
111  wr_buf[i] = data[i];
112  }
113 
114  bcm2835_i2c_write((const char *)wr_buf, length);
115 }
116 
122 unsigned char I2C_ReadByteRegister(char reg)
123 {
124  char val = 0;
125 
126  bcm2835_i2c_read_register_rs(&reg,&val,1);
127 
128  return val;
129  }
130 
138 void I2C_ReadByteArray(char reg,char *buffer,unsigned int length)
139 {
140  bcm2835_i2c_read_register_rs(&reg,buffer,length);
141 }
142 
148 unsigned int I2C_ReadWordRegisterRS(char reg)
149 {
150  char cmd[1] = {reg};
151  char receive[2] = {0};
152  bcm2835_i2c_write_read_rs(cmd,1,receive,2);
153 
154  return (receive[0]<<8)|receive[1];
155 }
156 
161 unsigned int I2C_ReadWordPresetPointer(void)
162 {
163  char val[2] = {0};
164  bcm2835_i2c_read(val,2);
165  unsigned int data = (val[0] << 8)|val[1];
166 
167  return data;
168  }
169 
174  void I2C_Close(void)
175  {
176  bcm2835_i2c_end();
177 }
178 
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
I2C driver header.
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