PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
i2c.h
Go to the documentation of this file.
1 
7 /*
8  * Copyright (c) 2012 David Michael Betz
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
11  * and associated documentation files (the "Software"), to deal in the Software without restriction,
12  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
13  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in all copies or
17  * substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
20  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  *
25  */
26 
27 #ifndef __I2C_H__
28 #define __I2C_H__
29 
30 #if defined(__cplusplus)
31 extern "C" {
32 #endif
33 
34 #include <stdint.h>
35 #include "i2c_driver.h"
36 
38 typedef struct I2C I2C;
39 
40 /* i2c operations */
41 typedef struct {
42  int (*close)(I2C *dev);
43  int (*read)(I2C *dev, int address, uint8_t *buffer, int count, int stop);
44  int (*readMore)(I2C *dev, uint8_t *buffer, int count, int stop);
45  int (*write)(I2C *dev, int address, uint8_t *buffer, int count, int stop);
46  int (*writeMore)(I2C *dev, uint8_t *buffer, int count, int stop);
47 } I2C_OPS;
48 
49 /* i2c state information */
50 struct I2C {
51  I2C_OPS *ops;
52 };
53 
54 typedef struct {
55  I2C i2c;
56  int cog;
57  volatile I2C_MAILBOX mailbox;
59 
60 typedef struct {
61  I2C i2c;
62  uint32_t scl_mask;
63  uint32_t sda_mask;
64 } I2C_SIMPLE;
65 
79 I2C *i2cOpen(I2C_COGDRIVER *dev, int scl, int sda, int freq);
80 
92 void *i2cGetCogBuffer(void);
93 
107 I2C *simple_i2cOpen(I2C_SIMPLE *dev, int scl, int sda);
108 
117 I2C *i2cBootOpen(void);
118 
132 void *i2cBootBuffer(void);
133 
145 static inline int i2cClose(I2C *dev)
146 {
147  return (*dev->ops->close)(dev);
148 }
149 
169 static inline int i2cWrite(I2C *dev, int address, uint8_t *buffer, int count, int stop)
170 {
171  return (*dev->ops->write)(dev, address, buffer, count, stop);
172 }
173 
191 static inline int i2cWriteMore(I2C *dev, uint8_t *buffer, int count, int stop)
192 {
193  return (*dev->ops->writeMore)(dev, buffer, count, stop);
194 }
195 
214 static inline int i2cRead(I2C *dev, int address, uint8_t *buffer, int count, int stop)
215 {
216  return (*dev->ops->read)(dev, address, buffer, count, stop);
217 }
218 
237 static inline int i2cReadMore(I2C *dev, uint8_t *buffer, int count, int stop)
238 {
239  return (*dev->ops->readMore)(dev, buffer, count, stop);
240 }
241 
242 /* internal functions */
243 int cog_i2cRead(I2C *dev, int address, uint8_t *buffer, int count, int stop);
244 int cog_i2cReadMore(I2C *dev, uint8_t *buffer, int count, int stop);
245 int cog_i2cWrite(I2C *dev, int address, uint8_t *buffer, int count, int stop);
246 int cog_i2cWriteMore(I2C *dev, uint8_t *buffer, int count, int stop);
247 
248 #if defined(__cplusplus)
249 }
250 #endif
251 
252 #endif
I2C
Definition: i2c.h:50
i2cClose
static int i2cClose(I2C *dev)
Close an I2C device.
Definition: i2c.h:145
i2cRead
static int i2cRead(I2C *dev, int address, uint8_t *buffer, int count, int stop)
Read from an I2C device.
Definition: i2c.h:214
i2cWrite
static int i2cWrite(I2C *dev, int address, uint8_t *buffer, int count, int stop)
Write to an I2C device.
Definition: i2c.h:169
count
long count(int pin, long duration)
Count number of low to high transitions an external input applies to an I/O pin over a certain period...
Definition: count.c:19
i2c_st
Definition: simplei2c.h:25
i2cBootOpen
I2C * i2cBootOpen(void)
Open the boot i2c bus on Propeller pins 28/29.
i2cBootBuffer
void * i2cBootBuffer(void)
Get the address of the boot i2c bus COG driver.
I2C_COGDRIVER
Definition: i2c.h:54
i2cReadMore
static int i2cReadMore(I2C *dev, uint8_t *buffer, int count, int stop)
Read more from an I2C device.
Definition: i2c.h:237
i2cWriteMore
static int i2cWriteMore(I2C *dev, uint8_t *buffer, int count, int stop)
Write more to an I2C device.
Definition: i2c.h:191
simple_i2cOpen
I2C * simple_i2cOpen(I2C_SIMPLE *dev, int scl, int sda)
Open an I2C device.
i2c_driver.h
Declarations here are not meant as public API.
i2cOpen
I2C * i2cOpen(I2C_COGDRIVER *dev, int scl, int sda, int freq)
Open an I2C device.
I2C_MAILBOX
Definition: i2c_driver.h:49
I2C_SIMPLE
Definition: i2c.h:60
I2C_OPS
Definition: i2c.h:41
i2cGetCogBuffer
void * i2cGetCogBuffer(void)
Return a pointer to the I2C COG driver image.