Contiki-Inga 3.x
Main Page
Related Pages
Modules
Data Structures
Files
Examples
File List
Globals
cpu
mc1322x
lib
include
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
*/
31
32
#ifndef I2C_H
33
#define I2C_H
34
35
#include <stdint.h>
36
#include "isr.h"
37
#include "
gpio.h
"
38
39
#define I2C_NON_BLOCKING 1
40
41
/* The I2C Interrupt Service Routine */
42
void
i2c_isr (
void
);
43
44
/* Enable the I2C module */
45
void
i2c_enable(
void
);
46
47
/* Disable the I2C module */
48
void
i2c_disable(
void
);
49
50
51
52
/* Returns 1 if the I2C bus is active (we or some other device are transferring data) */
53
uint8_t i2c_busy(
void
);
54
55
/* Returns 1 if our data is sent or received */
56
uint8_t i2c_transferred(
void
);
// indicates success of data last transfer (send or receive)
57
58
59
60
/* start receiving data from 'slave_address' of length 'byte_ctr' and store it in 'rx_buf'
61
* @parameter slave_addr 7bits (and will be padded with the 'receive bit')
62
* @parameter byte_ctr the number of bytes expected to be received
63
* @parameter rx_buf the buffer in which the received bytes will be stored.
64
*
65
* IFDEF I2C_NON_BLOCKING THEN THIS FUNCTION RETURNS IMMEDIATLY BUT THE TRANSFER WILL ONLY
66
* BE COMPLETE WHEN i2c_transferred IS 1
67
*/
68
void
i2c_receiveinit( uint8_t slave_address, uint8_t byte_ctr, uint8_t *rx_buf);
69
70
/* start sending data to 'slave_address' of length 'byte_ctr' found in the buffer 'rx_buf'
71
* @parameter slave_addr 7bits (and will be padded with the 'send bit')
72
* @parameter byte_ctr the number of bytes to be sent
73
* @parameter tx_buf the buffer in which the bytes to be sent are stored
74
*
75
* IFDEF I2C_NON_BLOCKING THEN THIS FUNCTION RETURNS IMMEDIATLY BUT THE TRANSFER WILL ONLY
76
* BE COMPLETE WHEN i2c_transferred IS 1
77
*/
78
void
i2c_transmitinit(uint8_t slave_address, uint8_t byte_ctr, uint8_t *tx_buf);
79
80
81
#ifndef I2C_NON_BLOCKING
82
#define I2C_NON_BLOCKING 1
83
#endif
84
85
#if I2C_NON_BLOCKING
86
uint8_t i2c_receive(
void
);
87
void
i2c_transmit(
void
);
88
#endif
89
90
// TODO: see if this is better fit in platform definition
91
#define I2C_SDA 13 //SDA == P5.1 // GPIO 13
92
#define I2C_SCL 12 //SCL == P5.2 // GPIO 12
93
94
// TODO: this could use a nice struct with bit members...
95
96
#define I2C_BASE (0x80006000)
97
98
#define I2CADR ((volatile uint8_t *) ( I2C_BASE + 0x00 ))
99
#define I2CFDR ((volatile uint8_t *) ( I2C_BASE + 0x04 ))
100
#define I2CCR ((volatile uint8_t *) ( I2C_BASE + 0x08 ))
101
#define I2CSR ((volatile uint8_t *) ( I2C_BASE + 0x0C ))
102
#define I2CDR ((volatile uint8_t *) ( I2C_BASE + 0x10 ))
103
#define I2CDFSRR ((volatile uint8_t *) ( I2C_BASE + 0x14 ))
104
#define I2CCKER ((volatile uint8_t *) ( I2C_BASE + 0x18 ))
105
106
// TODO: fix nice structs
107
108
// i2c CR
109
#define I2C_MEN 0x80
110
#define I2C_MIEN 0x40
111
#define I2C_MSTA 0x20
112
#define I2C_MTX 0x10
113
#define I2C_TXAK 0x08
114
#define I2C_RSTA 0x04
115
#define I2C_BCST 0x01
116
117
// i2c SR
118
#define I2C_MCF 0x80
119
#define I2C_MAAS 0x40
120
#define I2C_MBB 0x20
121
#define I2C_MAL 0x10
122
#define I2C_BCSTM 0x08
123
#define I2C_SRW 0x04
124
#define I2C_MIF 0x02
125
#define I2C_RXAK 0x01
126
127
// i2c digital filter sample rate register
128
#define I2C_DFSR 0x3f // default = 0x10
129
130
// i2c CKER
131
#define I2C_CKEN 0x01
132
133
134
#endif
Generated on Thu Apr 24 2014 16:26:14 for Contiki-Inga 3.x by
1.8.3.1