CuVoodoo STM32F1 firmware template
i2c_general.c
Go to the documentation of this file.
1 /* This program is free software: you can redistribute it and/or modify
2  * it under the terms of the GNU General Public License as published by
3  * the Free Software Foundation, either version 3 of the License, or
4  * (at your option) any later version.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program. If not, see <http://www.gnu.org/licenses/>.
13  *
14  */
22 /* standard libraries */
23 #include <stdint.h> // standard integer types
24 //#include <stdio.h> // standard I/O facilities
25 #include <stdlib.h> // general utilities
26 
27 /* STM32 (including CM3) libraries */
28 #include <libopencm3/stm32/rcc.h> // real-time control clock library
29 #include <libopencm3/stm32/gpio.h> // general purpose input output library
30 #include <libopencm3/stm32/i2c.h> // I2C library
31 #include <libopencm3/stm32/timer.h> // timer utilities
32 
33 /* own libraries */
34 #include "global.h" // global utilities
35 #include "i2c_general.h" // I2C header and definitions
36 
40 #define I2C_GENERAL_I2C 2
46 #define I2C_GENERAL_TIMER 3
47 #define I2C_GENERAL_TIMEOUT 4
50 void i2c_general_setup_master(bool fast)
51 {
52  // configure I2C peripheral
53  rcc_periph_clock_enable(RCC_I2C_SCL_PORT(I2C_GENERAL_I2C)); // enable clock for I2C I/O peripheral
54  gpio_set(I2C_SCL_PORT(I2C_GENERAL_I2C), I2C_SCL_PIN(I2C_GENERAL_I2C)); // already put signal high to avoid small pulse
55  gpio_set_mode(I2C_SCL_PORT(I2C_GENERAL_I2C), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, I2C_SCL_PIN(I2C_GENERAL_I2C)); // setup I2C I/O pins
56  rcc_periph_clock_enable(RCC_I2C_SCL_PORT(I2C_GENERAL_I2C)); // enable clock for I2C I/O peripheral
57  gpio_set(I2C_SDA_PORT(I2C_GENERAL_I2C), I2C_SDA_PIN(I2C_GENERAL_I2C)); // already put signal high to avoid small pulse
58  gpio_set_mode(I2C_SDA_PORT(I2C_GENERAL_I2C), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, I2C_SDA_PIN(I2C_GENERAL_I2C)); // setup I2C I/O pins
59  rcc_periph_clock_enable(RCC_AFIO); // enable clock for alternate function
60  rcc_periph_clock_enable(RCC_I2C(I2C_GENERAL_I2C)); // enable clock for I2C peripheral
61  i2c_reset(I2C(I2C_GENERAL_I2C)); // reset configuration
62  i2c_peripheral_disable(I2C(I2C_GENERAL_I2C)); // I2C needs to be disable to be configured
63  i2c_set_clock_frequency(I2C(I2C_GENERAL_I2C), rcc_apb1_frequency/1000000); // configure the peripheral clock to the APB1 freq (where it is connected to)
64  if (fast) {
65  i2c_set_fast_mode(I2C(I2C_GENERAL_I2C));
66  i2c_set_ccr(I2C(I2C_GENERAL_I2C), rcc_apb1_frequency/(400000*2)); // set Thigh/Tlow to generate frequency of 400 kHz
67  i2c_set_trise(I2C(I2C_GENERAL_I2C), (300/(1000/(rcc_apb1_frequency/1000000)))+1); // max rise time for 300 kHz is 300 ns
68  } else {
69  i2c_set_standard_mode(I2C(I2C_GENERAL_I2C)); // the DS1307 has a maximum I2C SCL freq if 100 kHz (corresponding to the standard mode)
70  i2c_set_ccr(I2C(I2C_GENERAL_I2C), rcc_apb1_frequency/(100000*2)); // set Thigh/Tlow to generate frequency of 100 kHz
71  i2c_set_trise(I2C(I2C_GENERAL_I2C), (1000/(1000/(rcc_apb1_frequency/1000000)))+1); // max rise time for 100 kHz is 1000 ns (~1 MHz)
72  }
73  i2c_peripheral_enable(I2C(I2C_GENERAL_I2C)); // enable I2C after configuration completed
74 
75  // configure time for timeouts
76  rcc_periph_clock_enable(RCC_TIM(I2C_GENERAL_TIMER)); // enable clock for timer block
77  timer_reset(TIM(I2C_GENERAL_TIMER)); // reset timer state
78  timer_set_mode(TIM(I2C_GENERAL_TIMER), TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock, edge alignment (simple count), and count up
79  timer_one_shot_mode(TIM(I2C_GENERAL_TIMER)); // stop counter after update event (we only need to one timeout and reset before next operation)
80  if (fast) {
81  timer_set_prescaler(TIM(I2C_GENERAL_TIMER), rcc_ahb_frequency/400000-1); // set the prescaler so one tick is also one I2C bit (used I2C frequency)
82  } else {
83  timer_set_prescaler(TIM(I2C_GENERAL_TIMER), rcc_ahb_frequency/100000-1); // set the prescaler so one tick is also one I2C bit (used I2C frequency)
84  }
85  timer_set_period(TIM(I2C_GENERAL_TIMER), I2C_GENERAL_TIMEOUT*9); // use factor to wait for all 9 bits to be transmitted
86  timer_update_on_overflow(TIM(I2C_GENERAL_TIMER)); // only use counter overflow as UEV source (use overflow as timeout)
87 
88  // wait one transaction for the signal to be stable (some slave have issues when an I2C transaction immediately follows)
89  timer_set_counter(TIM(I2C_GENERAL_TIMER),0); // restart timer
90  timer_clear_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF); // clear flag
91  timer_enable_counter(TIM(I2C_GENERAL_TIMER)); // enable timer for timeouts
92  while ( !timer_get_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF));
93  timer_disable_counter(TIM(I2C_GENERAL_TIMER)); // disable timer for timeouts
94  timer_clear_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF); // clear flag
95 }
96 
98 {
100 }
101 
103 {
104  // send (re-)start condition
105  i2c_send_start(I2C(I2C_GENERAL_I2C)); // send start condition to start transaction
106  timer_set_counter(TIM(I2C_GENERAL_TIMER), 0); // restart timer
107  timer_clear_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF); // clear flag
108  timer_enable_counter(TIM(I2C_GENERAL_TIMER)); // enable timer for timeouts
109  while (!(I2C_SR1(I2C(I2C_GENERAL_I2C)) & I2C_SR1_SB) && !timer_get_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF)); // wait until start condition is transmitted
110  timer_disable_counter(TIM(I2C_GENERAL_TIMER)); // disable timer for timeouts
111  if (timer_get_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF)) { // timeout occurred
112  timer_clear_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF); // clear flag
113  return false;
114  }
115  if (!(I2C_SR2(I2C(I2C_GENERAL_I2C)) & I2C_SR2_MSL)) { // verify if in master mode
116  return false;
117  }
118 
119  return true;
120 }
121 
122 bool i2c_general_select_slave(uint8_t slave, bool write)
123 {
124  if (!(I2C_SR2(I2C(I2C_GENERAL_I2C)) & I2C_SR2_BUSY)) { // I2C device is not busy (start condition has not been sent)
125  if (!i2c_general_start()) { // send start condition
126  return false; // could not send start condition
127  }
128  }
129  if (!(I2C_SR2(I2C(I2C_GENERAL_I2C)) & I2C_SR2_MSL)) { // I2C device is already not master mode
130  return false;
131  }
132 
133  // select slave
134  i2c_send_7bit_address(I2C(I2C_GENERAL_I2C), slave, write ? I2C_WRITE : I2C_READ); // select slave, with read/write flag
135  timer_set_counter(TIM(I2C_GENERAL_TIMER), 0); // restart timer
136  timer_clear_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF); // clear flag
137  timer_enable_counter(TIM(I2C_GENERAL_TIMER)); // enable timer for timeouts
138  while (!(I2C_SR1(I2C(I2C_GENERAL_I2C)) & I2C_SR1_ADDR) && !timer_get_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF)); // wait until address is transmitted
139  timer_disable_counter(TIM(I2C_GENERAL_TIMER)); // disable timer for timeouts
140  if (timer_get_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF)) { // timeout occurred (no ACK received)
141  timer_clear_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF); // clear flag
142  return false;
143  }
144  if (write) {
145  if (!((I2C_SR2(I2C(I2C_GENERAL_I2C)) & I2C_SR2_TRA))) { // verify we are in transmit mode (and read SR2 to clear ADDR)
146  return false;
147  }
148  } else {
149  if ((I2C_SR2(I2C(I2C_GENERAL_I2C)) & I2C_SR2_TRA)) { // verify we are in read mode (and read SR2 to clear ADDR)
150  return false;
151  }
152  }
153 
154  return true;
155 }
156 
157 bool i2c_general_read(uint8_t* data, size_t data_size)
158 {
159  // sanity check
160  if (data==NULL || data_size==0) { // no data to read
161  return true;
162  }
163  if (!(I2C_SR2(I2C(I2C_GENERAL_I2C)) & I2C_SR2_BUSY)) { // I2C device is not busy (start condition has not been sent)
164  return false; // address has probably also not been sent
165  }
166  if (!(I2C_SR2(I2C(I2C_GENERAL_I2C)) & I2C_SR2_MSL)) { // I2C device not master mode
167  return false;
168  }
169 
170  // read data
171  for (size_t i=0; i<data_size; i++) { // read bytes
172  if (i==data_size-1) { // prepare to sent NACK for last byte
173  i2c_disable_ack(I2C(I2C_GENERAL_I2C)); // NACK received to stop slave transmission
174  i2c_send_stop(I2C(I2C_GENERAL_I2C)); // send STOP after receiving byte
175  } else {
176  i2c_enable_ack(I2C(I2C_GENERAL_I2C)); // ACK received byte to continue slave transmission
177  }
178  timer_set_counter(TIM(I2C_GENERAL_TIMER), 0); // restart timer
179  timer_clear_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF); // clear flag
180  timer_enable_counter(TIM(I2C_GENERAL_TIMER)); // enable timer for timeouts
181  while (!(I2C_SR1(I2C(I2C_GENERAL_I2C)) & I2C_SR1_RxNE) && !timer_get_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF)); // wait until byte has been received
182  timer_disable_counter(TIM(I2C_GENERAL_TIMER)); // disable timer for timeouts
183  if (timer_get_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF)) { // timeout occurred
184  timer_clear_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF); // clear flag
185  return false;
186  }
187  data[i] = i2c_get_data(I2C(I2C_GENERAL_I2C)); // read received byte
188  }
189 
190  return true;
191 }
192 
193 bool i2c_general_write(const uint8_t* data, size_t data_size)
194 {
195  // sanity check
196  if (data==NULL || data_size==0) { // no data to write
197  return true;
198  }
199  if (!(I2C_SR2(I2C(I2C_GENERAL_I2C)) & I2C_SR2_BUSY)) { // I2C device is not busy (start condition has not been sent)
200  return false; // address has probably also not been sent
201  }
202  if (!(I2C_SR2(I2C(I2C_GENERAL_I2C)) & I2C_SR2_MSL)) { // I2C device is not master mode
203  return false;
204  }
205 
206  // write data
207  for (size_t i=0; i<data_size; i++) { // write bytes
208  i2c_send_data(I2C(I2C_GENERAL_I2C), data[i]); // send byte to be written in memory
209  timer_set_counter(TIM(I2C_GENERAL_TIMER),0); // restart timer
210  timer_clear_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF); // clear flag
211  timer_enable_counter(TIM(I2C_GENERAL_TIMER)); // enable timer for timeouts
212  while (!(I2C_SR1(I2C(I2C_GENERAL_I2C)) & I2C_SR1_TxE) && !timer_get_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF)); // wait until byte has been transmitted
213  timer_disable_counter(TIM(I2C_GENERAL_TIMER)); // disable timer for timeouts
214  if (timer_get_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF)) { // timeout occurred (no ACK received)
215  timer_clear_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF); // clear flag
216  return false;
217  }
218  }
219 
220  return true;
221 }
222 
224 {
225  // sanity check
226  if (!(I2C_SR2(I2C(I2C_GENERAL_I2C)) & I2C_SR2_BUSY)) { // release is not busy
227  return; // bus has probably already been released
228  }
229 
230  // send stop condition
231  i2c_send_stop(I2C(I2C_GENERAL_I2C)); // send stop to release bus
232  timer_set_counter(TIM(I2C_GENERAL_TIMER), 0); // restart timer
233  timer_clear_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF); // clear flag
234  timer_enable_counter(TIM(I2C_GENERAL_TIMER)); // enable timer for timeouts
235  while ((I2C_SR2(I2C(I2C_GENERAL_I2C)) & I2C_SR2_MSL) && !timer_get_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF)); // wait until bus released (non master mode)
236  timer_disable_counter(TIM(I2C_GENERAL_TIMER)); // disable timer for timeouts
237  if (timer_get_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF)) { // timeout occurred
238  timer_clear_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF); // clear flag
239  }
240 }
#define I2C_SCL_PORT(x)
get I2C port for SCL pin based on I2C identifier
Definition: global.h:272
#define I2C(x)
get I2C based on I2C identifier
Definition: global.h:260
bool i2c_general_select_slave(uint8_t slave, bool write)
select slave device
Definition: i2c_general.c:122
#define I2C_SDA_PIN(x)
get I2C port for SDA pin based on I2C identifier
Definition: global.h:278
global definitions and methods (API)
#define I2C_SDA_PORT(x)
get I2C port for SDA pin based on I2C identifier
Definition: global.h:274
#define RCC_TIM(x)
get RCC for timer based on TIM identifier
Definition: global.h:109
bool i2c_general_read(uint8_t *data, size_t data_size)
read data
Definition: i2c_general.c:157
bool i2c_general_check(void)
check if SDA and SCL signals are high
Definition: i2c_general.c:97
#define I2C_GENERAL_TIMEOUT
timeout factor (compared to expected time)
Definition: i2c_general.c:47
#define I2C_GENERAL_I2C
I2C peripheral.
Definition: i2c_general.c:40
library to communicate using I2C as master or slave (API)
void i2c_general_stop(void)
sent stop condition
Definition: i2c_general.c:223
#define I2C_SCL_PIN(x)
get I2C pin for SCL pin based on I2C identifier
Definition: global.h:276
bool i2c_general_start(void)
send start condition
Definition: i2c_general.c:102
#define RCC_I2C_SCL_PORT(x)
get RCC for GPIO port for SCL based on I2C identifier
Definition: global.h:264
bool i2c_general_write(const uint8_t *data, size_t data_size)
write data
Definition: i2c_general.c:193
#define TIM(x)
get TIM based on TIM identifier
Definition: global.h:107
#define I2C_GENERAL_TIMER
timer peripheral
Definition: i2c_general.c:46
#define RCC_I2C(x)
get RCC for I2C based on I2C identifier
Definition: global.h:262