4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
33 * \brief Driver for the LM3S I2C (implementation)
37 #include "cfg/cfg_i2c.h"
39 #define LOG_LEVEL I2C_LOG_LEVEL
40 #define LOG_FORMAT I2C_LOG_FORMAT
44 #include <cfg/debug.h>
45 #include <cfg/macros.h> // BV()
46 #include <cfg/module.h>
48 #include <cpu/detect.h>
51 #include <io/cm3_types.h>
54 #include <drv/timer.h>
56 #include <drv/gpio_lm3s.h>
57 #include <drv/clock_lm3s.h>
64 #define I2C I2C0_MASTER_BASE
65 #define SYSCTL_RCGC1_I2C SYSCTL_RCGC1_I2C0
66 #define SYSCTL_RCGC2_GPIO SYSCTL_RCGC2_GPIOB
67 #define GPIO_I2C_SCL_PIN GPIO_I2C0_SCL_PIN
68 #define GPIO_I2C_SDA_PIN GPIO_I2C0_SDA_PIN
69 #define GPIO_PORT_BASE GPIO_PORTB_BASE
72 #define I2C I2C1_MASTER_BASE
73 #define SYSCTL_RCGC1_I2C SYSCTL_RCGC1_I2C1
74 #define SYSCTL_RCGC2_GPIO SYSCTL_RCGC2_GPIOA
75 #define GPIO_I2C_SCL_PIN GPIO_I2C1_SCL_PIN
76 #define GPIO_I2C_SDA_PIN GPIO_I2C1_SDA_PIN
77 #define GPIO_PORT_BASE GPIO_PORTA_BASE
82 * Send START condition and select slave for write.
83 * \c id is the device id comprehensive of address left shifted by 1.
84 * The LSB of \c id is ignored and reset to 0 for write operation.
86 * \return true on success, false otherwise.
88 bool i2c_builtin_start_w(uint8_t id)
90 HWREG(I2C + I2C_O_MSA) = id & ~BV(0);
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.
100 * \return true on success, false otherwise.
102 bool i2c_builtin_start_r(uint8_t id)
104 HWREG(I2C + I2C_O_MSA) = id | BV(0);
109 void i2c_builtin_stop(void)
114 bool i2c_builtin_put(const uint8_t data)
121 int i2c_builtin_get(bool ack)
127 INLINE bool check_ack(uint32_t mode_mask)
129 ticks_t start = timer_clock();
130 while ( HWREG(I2C + I2C_O_MCS) & I2C_MCS_ADRACK )
132 if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
134 LOG_ERR("Timeout on I2C_START\n");
138 HWREG(I2C + I2C_O_MCS) = I2C_MASTER_CMD_BURST_SEND_ERROR_STOP;
140 HWREG(I2C + I2C_O_MCS) = mode_mask;
141 while( HWREG(I2C + I2C_O_MCS) & I2C_MCS_BUSY );
148 * With this function is allowed only the atomic write.
150 bool i2c_send(const void *_buf, size_t count)
152 const uint8_t *buf = (const uint8_t *)_buf;
154 HWREGB(I2C + I2C_O_MDR) = *buf++;
160 HWREG(I2C + I2C_O_MCS) = I2C_MASTER_CMD_SINGLE_SEND;
162 while( HWREG(I2C + I2C_O_MCS) & I2C_MCS_BUSY );
164 if ( !check_ack(I2C_MASTER_CMD_SINGLE_SEND) )
166 LOG_ERR("No single send start");
173 HWREG(I2C + I2C_O_MCS) = I2C_MASTER_CMD_BURST_SEND_START;
175 while( HWREG(I2C + I2C_O_MCS) & I2C_MCS_BUSY );
177 if ( !check_ack(I2C_MASTER_CMD_BURST_SEND_START) )
179 LOG_ERR("No burst send start");
185 HWREGB(I2C + I2C_O_MDR) = *buf++;
187 HWREG(I2C + I2C_O_MCS) = I2C_MASTER_CMD_BURST_SEND_CONT;
188 while( HWREG(I2C + I2C_O_MCS) & I2C_MCS_BUSY );
191 HWREGB(I2C + I2C_O_MDR) = *buf++;
194 HWREG(I2C + I2C_O_MCS) = I2C_MASTER_CMD_BURST_SEND_FINISH;
195 while( HWREG(I2C + I2C_O_MCS) & I2C_MCS_BUSY );
202 * In order to read bytes from the i2c we should make some tricks.
204 bool i2c_recv(void *_buf, size_t count)
206 uint8_t *buf = (uint8_t *)_buf;
210 HWREG(I2C + I2C_O_MCS) = I2C_MASTER_CMD_SINGLE_RECEIVE;
211 while( HWREG(I2C + I2C_O_MCS) & I2C_MCS_BUSY );
213 if ( !check_ack(I2C_MASTER_CMD_SINGLE_RECEIVE) )
216 *buf++ = HWREG(I2C + I2C_O_MDR);
222 HWREG(I2C + I2C_O_MCS) = I2C_MASTER_CMD_BURST_RECEIVE_START;
223 while( HWREG(I2C + I2C_O_MCS) & I2C_MCS_BUSY );
225 if ( !check_ack(I2C_MASTER_CMD_BURST_RECEIVE_START) )
228 *buf++ = (uint8_t)HWREG(I2C + I2C_O_MDR);
234 HWREG(I2C + I2C_O_MCS) = I2C_MASTER_CMD_BURST_RECEIVE_CONT;
235 while( HWREG(I2C + I2C_O_MCS) & I2C_MCS_BUSY );
237 *buf++ = (uint8_t)HWREG(I2C + I2C_O_MDR);
241 HWREG(I2C + I2C_O_MCS) = I2C_MASTER_CMD_BURST_RECEIVE_FINISH;
242 while( HWREG(I2C + I2C_O_MCS) & I2C_MCS_BUSY );
244 *buf++ = (uint8_t)HWREG(I2C + I2C_O_MDR);
254 * Initialize I2C module.
256 void i2c_builtin_init(void)
259 /* Enable the peripheral clock */
260 SYSCTL_RCGC1_R |= SYSCTL_RCGC1_I2C;
261 SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIO;
263 /* Configure GPIO pins to work as I2C pins */
264 lm3s_gpioPinConfig(GPIO_PORT_BASE, GPIO_I2C_SCL_PIN,
265 GPIO_DIR_MODE_HW, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD_WPU);
267 lm3s_gpioPinConfig(GPIO_PORT_BASE, GPIO_I2C_SDA_PIN,
268 GPIO_DIR_MODE_HW, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD_WPU);
272 * Note: to set correctly the i2c speed we shold before enable the i2c
273 * device and then set in master time period the correct value
276 /* Enable i2c device */
277 HWREG(I2C + I2C_O_MCR) |= I2C_MCR_MFE;
280 * Compute the clock divider that achieves the fastest speed less than or
281 * equal to the desired speed. The numerator is biased to favor a larger
282 * clock divider so that the resulting clock is always less than or equal
283 * to the desired clock, never greater.
285 HWREG(I2C + I2C_O_MTPR) = ((CPU_FREQ + (2 * 10 * CONFIG_I2C_FREQ) - 1) / (2 * 10 * CONFIG_I2C_FREQ)) - 1;