Contiki 2.5
i2c.h
1 /*
2  * Copyright (c) 2011, Hedde Bosman <heddebosman@incas3.eu>
3  *
4  * I2C communication device drivers for mc1322x
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
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  * 3. Neither the name of the Institute nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $Id$
31  */
32 
33 #ifndef I2C_H
34 #define I2C_H
35 
36 #include <stdint.h>
37 #include "isr.h"
38 #include "gpio.h"
39 
40 #define I2C_NON_BLOCKING 1
41 
42 /* The I2C Interrupt Service Routine */
43 void i2c_isr (void);
44 
45 /* Enable the I2C module */
46 void i2c_enable(void);
47 
48 /* Disable the I2C module */
49 void i2c_disable(void);
50 
51 
52 
53 /* Returns 1 if the I2C bus is active (we or some other device are transferring data) */
54 uint8_t i2c_busy(void);
55 
56 /* Returns 1 if our data is sent or received */
57 uint8_t i2c_transferred(void); // indicates success of data last transfer (send or receive)
58 
59 
60 
61 /* start receiving data from 'slave_address' of length 'byte_ctr' and store it in 'rx_buf'
62  * @parameter slave_addr 7bits (and will be padded with the 'receive bit')
63  * @parameter byte_ctr the number of bytes expected to be received
64  * @parameter rx_buf the buffer in which the received bytes will be stored.
65  *
66  * IFDEF I2C_NON_BLOCKING THEN THIS FUNCTION RETURNS IMMEDIATLY BUT THE TRANSFER WILL ONLY
67  * BE COMPLETE WHEN i2c_transferred IS 1
68  */
69 void i2c_receiveinit( uint8_t slave_address, uint8_t byte_ctr, uint8_t *rx_buf);
70 
71 /* start sending data to 'slave_address' of length 'byte_ctr' found in the buffer 'rx_buf'
72  * @parameter slave_addr 7bits (and will be padded with the 'send bit')
73  * @parameter byte_ctr the number of bytes to be sent
74  * @parameter tx_buf the buffer in which the bytes to be sent are stored
75  *
76  * IFDEF I2C_NON_BLOCKING THEN THIS FUNCTION RETURNS IMMEDIATLY BUT THE TRANSFER WILL ONLY
77  * BE COMPLETE WHEN i2c_transferred IS 1
78  */
79 void i2c_transmitinit(uint8_t slave_address, uint8_t byte_ctr, uint8_t *tx_buf);
80 
81 
82 #ifndef I2C_NON_BLOCKING
83 #define I2C_NON_BLOCKING 1
84 #endif
85 
86 #if I2C_NON_BLOCKING
87 uint8_t i2c_receive(void);
88 void i2c_transmit(void);
89 #endif
90 
91 // TODO: see if this is better fit in platform definition
92 #define I2C_SDA 13 //SDA == P5.1 // GPIO 13
93 #define I2C_SCL 12 //SCL == P5.2 // GPIO 12
94 
95 // TODO: this could use a nice struct with bit members...
96 
97 #define I2C_BASE (0x80006000)
98 
99 #define I2CADR ((volatile uint8_t *) ( I2C_BASE + 0x00 ))
100 #define I2CFDR ((volatile uint8_t *) ( I2C_BASE + 0x04 ))
101 #define I2CCR ((volatile uint8_t *) ( I2C_BASE + 0x08 ))
102 #define I2CSR ((volatile uint8_t *) ( I2C_BASE + 0x0C ))
103 #define I2CDR ((volatile uint8_t *) ( I2C_BASE + 0x10 ))
104 #define I2CDFSRR ((volatile uint8_t *) ( I2C_BASE + 0x14 ))
105 #define I2CCKER ((volatile uint8_t *) ( I2C_BASE + 0x18 ))
106 
107 // TODO: fix nice structs
108 
109 // i2c CR
110 #define I2C_MEN 0x80
111 #define I2C_MIEN 0x40
112 #define I2C_MSTA 0x20
113 #define I2C_MTX 0x10
114 #define I2C_TXAK 0x08
115 #define I2C_RSTA 0x04
116 #define I2C_BCST 0x01
117 
118 // i2c SR
119 #define I2C_MCF 0x80
120 #define I2C_MAAS 0x40
121 #define I2C_MBB 0x20
122 #define I2C_MAL 0x10
123 #define I2C_BCSTM 0x08
124 #define I2C_SRW 0x04
125 #define I2C_MIF 0x02
126 #define I2C_RXAK 0x01
127 
128 // i2c digital filter sample rate register
129 #define I2C_DFSR 0x3f // default = 0x10
130 
131 // i2c CKER
132 #define I2C_CKEN 0x01
133 
134 
135 #endif