27fed7c69e06985e5a51a29661332baa07b4a186
[bertos.git] / bertos / cpu / cortex-m3 / drv / i2c_lm3s.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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 General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Driver for the LM3S I2C (implementation)
34  *
35  */
36
37 #include "cfg/cfg_i2c.h"
38
39 #define LOG_LEVEL  I2C_LOG_LEVEL
40 #define LOG_FORMAT I2C_LOG_FORMAT
41
42 #include <cfg/log.h>
43
44 #include <cfg/debug.h>
45 #include <cfg/macros.h> // BV()
46 #include <cfg/module.h>
47
48 #include <cpu/detect.h>
49 #include <cpu/irq.h>
50 #include <drv/timer.h>
51 #include <drv/i2c.h>
52
53
54 /**
55  * Send START condition on the bus.
56  *
57  * \return true on success, false otherwise.
58  */
59 static bool i2c_builtin_start(void)
60 {
61
62         return false;
63 }
64
65
66 /**
67  * Send START condition and select slave for write.
68  * \c id is the device id comprehensive of address left shifted by 1.
69  * The LSB of \c id is ignored and reset to 0 for write operation.
70  *
71  * \return true on success, false otherwise.
72  */
73 bool i2c_builtin_start_w(uint8_t id)
74 {
75         /*
76          * Loop on the select write sequence: when the eeprom is busy
77          * writing previously sent data it will reply to the SLA_W
78          * control byte with a NACK.  In this case, we must
79          * keep trying until the eeprom responds with an ACK.
80          */
81         ticks_t start = timer_clock();
82         while (i2c_builtin_start())
83         {
84                 else if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
85                 {
86                         LOG_ERR("Timeout on TWI_MT_START\n");
87                         break;
88                 }
89         }
90
91         return false;
92 }
93
94
95 /**
96  * Send START condition and select slave for read.
97  * \c id is the device id comprehensive of address left shifted by 1.
98  * The LSB of \c id is ignored and set to 1 for read operation.
99  *
100  * \return true on success, false otherwise.
101  */
102 bool i2c_builtin_start_r(uint8_t id)
103 {
104         if (i2c_builtin_start())
105         {
106
107         }
108
109         return false;
110 }
111
112
113 /**
114  * Send STOP condition.
115  */
116 void i2c_builtin_stop(void)
117 {
118
119 }
120
121
122 /**
123  * Put a single byte in master transmitter mode
124  * to the selected slave device through the TWI bus.
125  *
126  * \return true on success, false on error.
127  */
128 bool i2c_builtin_put(const uint8_t data)
129 {
130         return true;
131 }
132
133 /**
134  * Get 1 byte from slave in master transmitter mode
135  * to the selected slave device through the I2C bus.
136  * If \a ack is true issue a ACK after getting the byte,
137  * otherwise a NACK is issued.
138  *
139  * \return the byte read if ok, EOF on errors.
140  */
141 int i2c_builtin_get(bool ack)
142 {
143
144         if (ack)
145         {
146
147         }
148         else
149         {
150
151         }
152
153         /* avoid sign extension */
154         return 0;
155 }
156
157 MOD_DEFINE(i2c);
158
159 /**
160  * Initialize I2C module.
161  */
162 void i2c_builtin_init(void)
163 {
164         MOD_INIT(i2c);
165 }