BV I2C: add initial I2C support

This commit is contained in:
King Kévin 2018-03-11 17:51:53 +01:00
parent 141fa4f2c9
commit 07b4cb10cc
4 changed files with 532 additions and 0 deletions

213
lib/busvoodoo_i2c.c Normal file
View File

@ -0,0 +1,213 @@
/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/** BusVoodoo I²C mode (code)
* @file busvoodoo_i2c.c
* @author King Kévin <kingkevin@cuvoodoo.info>
* @date 2018
* @note peripherals used: I2C @ref busvoodoo_i2c
*/
/* standard libraries */
#include <stdint.h> // standard integer types
#include <stdlib.h> // standard utilities
#include <string.h> // string utilities
/* STM32 (including CM3) libraries */
#include <libopencm3/stm32/gpio.h> // general purpose input output library
#include <libopencm3/stm32/rcc.h> // real-time control clock library
/* own libraries */
#include "global.h" // board definitions
#include "print.h" // printing utilities
#include "menu.h" // menu definitions
#include "i2c_general.h" // I2C utilities
#include "busvoodoo_global.h" // BusVoodoo definitions
#include "busvoodoo_oled.h" // OLED utilities
#include "busvoodoo_i2c.h" // own definitions
/** mode setup stage */
static enum busvoodoo_i2c_setting_t {
BUSVOODOO_I2C_SETTING_NONE,
BUSVOODOO_I2C_SETTING_SPEED,
BUSVOODOO_I2C_SETTING_ADDRESSBITS,
BUSVOODOO_I2C_SETTING_PULLUP,
BUSVOODOO_I2C_SETTING_DONE,
} busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_NONE;
/** I2C speed (in kHz) */
uint16_t busvoodoo_i2c_speed = 100;
/** I2C address bits (7 or 10) */
uint8_t busvoodoo_i2c_addressbits = 7;
/** if embedded or external pull-up resistors are use */
bool busvoodoo_i2c_embedded_pullup = true;
/** setup I2C mode
* @param[out] prefix terminal prompt prefix
* @param[in] line terminal prompt line to configure mode
* @return if setup is complete
*/
static bool busvoodoo_i2c_setup(char** prefix, const char* line)
{
bool complete = false; // is the setup complete
static char config[32] = {0}; // configuration string used as prefix
if (NULL==line) { // first call
busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_NONE; // re-start configuration
}
switch (busvoodoo_i2c_setting) {
case BUSVOODOO_I2C_SETTING_NONE:
snprintf(config, LENGTH(config), "speed in kHz (1-400) [%u]", busvoodoo_i2c_speed);
*prefix = config; // ask for speed
busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_SPEED;
break;
case BUSVOODOO_I2C_SETTING_SPEED:
if (NULL==line || 0==strlen(line)) { // use default setting
busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_ADDRESSBITS; // go to next setting
} else { // setting provided
uint32_t speed = atoi(line); // parse setting
if (speed>0 && speed<=400) { // check setting
busvoodoo_i2c_speed = speed; // remember setting
busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_ADDRESSBITS; // go to next setting
}
}
if (BUSVOODOO_I2C_SETTING_ADDRESSBITS==busvoodoo_i2c_setting) { // if next setting
snprintf(config, LENGTH(config), "address size in bits (7,10) [%u]", busvoodoo_i2c_addressbits); // prepare next setting
*prefix = config; // display next setting
}
break;
case BUSVOODOO_I2C_SETTING_ADDRESSBITS:
if (NULL==line || 0==strlen(line)) { // use default setting
busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_PULLUP; // go to next setting
} else { // setting provided
uint8_t addressbits = atoi(line); // parse setting
if (7==addressbits || 10==addressbits) { // check setting
busvoodoo_i2c_addressbits = addressbits; // remember setting
busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_PULLUP; // go to next setting
}
}
if (BUSVOODOO_I2C_SETTING_PULLUP==busvoodoo_i2c_setting) { // if next setting
printf("i) embedded\n");
printf("e) external\n");
snprintf(config, LENGTH(config), "pull-up resistors (i,e) [%c]", busvoodoo_i2c_embedded_pullup ? 'i' : 'e'); // prepare next setting
*prefix = config; // display next setting
}
break;
case BUSVOODOO_I2C_SETTING_PULLUP:
if (NULL==line || 0==strlen(line)) { // use default setting
busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_DONE; // go to next setting
} else { // setting provided
if (0==strcmp("i", line)) { // check setting
busvoodoo_i2c_embedded_pullup = true; // remember setting
busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_DONE; // go to next setting
} else if (0==strcmp("e", line)) {
busvoodoo_i2c_embedded_pullup = false; // remember setting
busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_DONE; // go to next setting
}
}
if (BUSVOODOO_I2C_SETTING_DONE==busvoodoo_i2c_setting) { // we have all settings, configure I2C
i2c_general_setup_master(false); // setup I2C
busvoodoo_embedded_pullup(busvoodoo_i2c_embedded_pullup); // set pull-up
if (busvoodoo_i2c_embedded_pullup) {
printf("set pull-up voltage with LV\n");
}
led_off(); // disable LED because there is no activity
busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_NONE; // restart settings next time
*prefix = "I2C"; // display mode
busvoodoo_oled_text_left(*prefix); // set mode title on OLED display
const char* pinout_io[10] = {"GND", "3V3", "5V", "LV", "SDA", "SCL", NULL, NULL}; // HiZ mode pinout
for (uint8_t i=0; i<LENGTH(pinout_io) && i<LENGTH(busvoodoo_global_pinout_io); i++) {
busvoodoo_global_pinout_io[i] = pinout_io[i]; // set pin names
}
busvoodoo_oled_text_pinout(pinout_io, true); // set pinout on display
busvoodoo_oled_update(); // update display to show text and pinout
complete = true; // configuration is complete
}
break;
case BUSVOODOO_I2C_SETTING_DONE: // this case is already handled before
default: // unknown case
busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_NONE; // restart settings next time
complete = false; // restart setting
break;
}
return complete;
}
/** exit I2C mode
*/
static void busvoodoo_i2c_exit(void)
{
// TODO release I2C periph
busvoodoo_embedded_pullup(false); // disable embedded pull-ups
}
// command handlers
/** command to scan for slave devices
* @param[in] argument no argument required
*/
static void busvoodoo_i2c_command_scan(void* argument)
{
(void)argument; // we won't use the argument
if (!i2c_general_check()) { // ensure SCL and SDA are high
printf("SCL or SDA is low. The signals need to be pulled up\n");
if (busvoodoo_i2c_embedded_pullup) {
printf("set pull-up voltage with LV\n");
}
led_blink(0.5, 0.5); // show error on LEDs
return; // stop here
} else {
led_off(); // switch LEDs off
}
printf("scanning for I2C slaves\n");
uint16_t i2c_slaves = 0; // number of slaves found
// check for I2C slaves by going through the address space
for (uint16_t address=0; address < (1<<busvoodoo_i2c_addressbits); address++) {
if (!i2c_general_start()) { // send start condition
i2c_general_stop(); // send stop condition
printf("start condition failed\n"); // show error to user
led_blink(0.5, 0.5); // show error on LEDs
break; // stop scanning
} else {
busvoodoo_led_blue(100); // pulse blue LED to show transmission
}
if (i2c_general_select_slave(address, false)) { // try to select slave
busvoodoo_led_red(100); // pulse red LED to show we found a slave
printf((busvoodoo_i2c_addressbits>7) ? "0x%03x " : "0x%02x ", address); // display address
i2c_slaves++; // increase slave count
}
i2c_general_stop(); // send stop condition
}
if (i2c_slaves>0) {
printf("\n");
}
printf("%u slave(s) found\n", i2c_slaves); // show summary
}
static const struct menu_command_t busvoodoo_i2c_commands[] = {
{
's',
"scan",
"scan for slave devices",
MENU_ARGUMENT_NONE,
NULL,
&busvoodoo_i2c_command_scan,
},
};
struct busvoodoo_mode_t busvoodoo_i2c_mode = {
"i2c",
"Inter-Integrated Circuit",
&busvoodoo_i2c_setup,
busvoodoo_i2c_commands,
LENGTH(busvoodoo_i2c_commands),
&busvoodoo_i2c_exit,
};

23
lib/busvoodoo_i2c.h Normal file
View File

@ -0,0 +1,23 @@
/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/** BusVoodoo I²C mode (API)
* @file busvoodoo_i2c.h
* @author King Kévin <kingkevin@cuvoodoo.info>
* @date 2018
* @note peripherals used: I2C @ref busvoodoo_i2c
*/
/** I2C mode interface definition */
extern struct busvoodoo_mode_t busvoodoo_i2c_mode;

240
lib/i2c_general.c Normal file
View File

@ -0,0 +1,240 @@
/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/** library to communicate using I2C as master or slave (code)
* @file i2c_general.c
* @author King Kévin <kingkevin@cuvoodoo.info>
* @date 2017-2018
* @note peripherals used: I2C @ref i2c_general_i2c, timer @ref i2c_general_timer
*/
/* standard libraries */
#include <stdint.h> // standard integer types
//#include <stdio.h> // standard I/O facilities
#include <stdlib.h> // general utilities
/* STM32 (including CM3) libraries */
#include <libopencm3/stm32/rcc.h> // real-time control clock library
#include <libopencm3/stm32/gpio.h> // general purpose input output library
#include <libopencm3/stm32/i2c.h> // I2C library
#include <libopencm3/stm32/timer.h> // timer utilities
/* own libraries */
#include "global.h" // global utilities
#include "i2c_general.h" // I2C header and definitions
/** @defgroup i2c_general_i2c I2C peripheral used to communicate
* @{
*/
#define I2C_GENERAL_I2C 2 /**< I2C peripheral */
/** @} */
/** @defgroup i2c_general_timer timer peripheral used for timeouts
* @{
*/
#define I2C_GENERAL_TIMER 3 /**< timer peripheral */
#define I2C_GENERAL_TIMEOUT 4 /**< timeout factor (compared to expected time) */
/** @} */
void i2c_general_setup_master(bool fast)
{
// configure I2C peripheral
rcc_periph_clock_enable(RCC_I2C_SCL_PORT(I2C_GENERAL_I2C)); // enable clock for I2C I/O peripheral
gpio_set(I2C_SCL_PORT(I2C_GENERAL_I2C), I2C_SCL_PIN(I2C_GENERAL_I2C)); // already put signal high to avoid small pulse
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
rcc_periph_clock_enable(RCC_I2C_SCL_PORT(I2C_GENERAL_I2C)); // enable clock for I2C I/O peripheral
gpio_set(I2C_SDA_PORT(I2C_GENERAL_I2C), I2C_SDA_PIN(I2C_GENERAL_I2C)); // already put signal high to avoid small pulse
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
rcc_periph_clock_enable(RCC_AFIO); // enable clock for alternate function
rcc_periph_clock_enable(RCC_I2C(I2C_GENERAL_I2C)); // enable clock for I2C peripheral
i2c_reset(I2C(I2C_GENERAL_I2C)); // reset configuration
i2c_peripheral_disable(I2C(I2C_GENERAL_I2C)); // I2C needs to be disable to be configured
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)
if (fast) {
i2c_set_fast_mode(I2C(I2C_GENERAL_I2C));
i2c_set_ccr(I2C(I2C_GENERAL_I2C), rcc_apb1_frequency/(400000*2)); // set Thigh/Tlow to generate frequency of 400 kHz
i2c_set_trise(I2C(I2C_GENERAL_I2C), (300/(1000/(rcc_apb1_frequency/1000000)))+1); // max rise time for 300 kHz is 300 ns
} else {
i2c_set_standard_mode(I2C(I2C_GENERAL_I2C)); // the DS1307 has a maximum I2C SCL freq if 100 kHz (corresponding to the standard mode)
i2c_set_ccr(I2C(I2C_GENERAL_I2C), rcc_apb1_frequency/(100000*2)); // set Thigh/Tlow to generate frequency of 100 kHz
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)
}
i2c_peripheral_enable(I2C(I2C_GENERAL_I2C)); // enable I2C after configuration completed
// configure time for timeouts
rcc_periph_clock_enable(RCC_TIM(I2C_GENERAL_TIMER)); // enable clock for timer block
timer_reset(TIM(I2C_GENERAL_TIMER)); // reset timer state
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
timer_one_shot_mode(TIM(I2C_GENERAL_TIMER)); // stop counter after update event (we only need to one timeout and reset before next operation)
if (fast) {
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)
} else {
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)
}
timer_set_period(TIM(I2C_GENERAL_TIMER), I2C_GENERAL_TIMEOUT*9); // use factor to wait for all 9 bits to be transmitted
timer_update_on_overflow(TIM(I2C_GENERAL_TIMER)); // only use counter overflow as UEV source (use overflow as timeout)
// wait one transaction for the signal to be stable (some slave have issues when an I2C transaction immediately follows)
timer_set_counter(TIM(I2C_GENERAL_TIMER),0); // restart timer
timer_clear_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF); // clear flag
timer_enable_counter(TIM(I2C_GENERAL_TIMER)); // enable timer for timeouts
while ( !timer_get_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF));
timer_disable_counter(TIM(I2C_GENERAL_TIMER)); // disable timer for timeouts
timer_clear_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF); // clear flag
}
bool i2c_general_check(void)
{
return (0!=gpio_get(I2C_SCL_PORT(I2C_GENERAL_I2C), I2C_SCL_PIN(I2C_GENERAL_I2C)) && 0!=gpio_get(I2C_SDA_PORT(I2C_GENERAL_I2C), I2C_SDA_PIN(I2C_GENERAL_I2C)));
}
bool i2c_general_start(void)
{
// send (re-)start condition
i2c_send_start(I2C(I2C_GENERAL_I2C)); // send start condition to start transaction
timer_set_counter(TIM(I2C_GENERAL_TIMER), 0); // restart timer
timer_clear_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF); // clear flag
timer_enable_counter(TIM(I2C_GENERAL_TIMER)); // enable timer for timeouts
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
timer_disable_counter(TIM(I2C_GENERAL_TIMER)); // disable timer for timeouts
if (timer_get_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF)) { // timeout occurred
timer_clear_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF); // clear flag
return false;
}
if (!(I2C_SR2(I2C(I2C_GENERAL_I2C)) & I2C_SR2_MSL)) { // verify if in master mode
return false;
}
return true;
}
bool i2c_general_select_slave(uint8_t slave, bool write)
{
if (!(I2C_SR2(I2C(I2C_GENERAL_I2C)) & I2C_SR2_BUSY)) { // I2C device is not busy (start condition has not been sent)
if (!i2c_general_start()) { // send start condition
return false; // could not send start condition
}
}
if (!(I2C_SR2(I2C(I2C_GENERAL_I2C)) & I2C_SR2_MSL)) { // I2C device is already not master mode
return false;
}
// select slave
i2c_send_7bit_address(I2C(I2C_GENERAL_I2C), slave, write ? I2C_WRITE : I2C_READ); // select slave, with read/write flag
timer_set_counter(TIM(I2C_GENERAL_TIMER), 0); // restart timer
timer_clear_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF); // clear flag
timer_enable_counter(TIM(I2C_GENERAL_TIMER)); // enable timer for timeouts
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
timer_disable_counter(TIM(I2C_GENERAL_TIMER)); // disable timer for timeouts
if (timer_get_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF)) { // timeout occurred (no ACK received)
timer_clear_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF); // clear flag
return false;
}
if (write) {
if (!((I2C_SR2(I2C(I2C_GENERAL_I2C)) & I2C_SR2_TRA))) { // verify we are in transmit mode (and read SR2 to clear ADDR)
return false;
}
} else {
if ((I2C_SR2(I2C(I2C_GENERAL_I2C)) & I2C_SR2_TRA)) { // verify we are in read mode (and read SR2 to clear ADDR)
return false;
}
}
return true;
}
bool i2c_general_read(uint8_t* data, size_t data_size)
{
// sanity check
if (data==NULL || data_size==0) { // no data to read
return true;
}
if (!(I2C_SR2(I2C(I2C_GENERAL_I2C)) & I2C_SR2_BUSY)) { // I2C device is not busy (start condition has not been sent)
return false; // address has probably also not been sent
}
if (!(I2C_SR2(I2C(I2C_GENERAL_I2C)) & I2C_SR2_MSL)) { // I2C device not master mode
return false;
}
// read data
for (size_t i=0; i<data_size; i++) { // read bytes
if (i==data_size-1) { // prepare to sent NACK for last byte
i2c_disable_ack(I2C(I2C_GENERAL_I2C)); // NACK received to stop slave transmission
i2c_send_stop(I2C(I2C_GENERAL_I2C)); // send STOP after receiving byte
} else {
i2c_enable_ack(I2C(I2C_GENERAL_I2C)); // ACK received byte to continue slave transmission
}
timer_set_counter(TIM(I2C_GENERAL_TIMER), 0); // restart timer
timer_clear_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF); // clear flag
timer_enable_counter(TIM(I2C_GENERAL_TIMER)); // enable timer for timeouts
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
timer_disable_counter(TIM(I2C_GENERAL_TIMER)); // disable timer for timeouts
if (timer_get_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF)) { // timeout occurred
timer_clear_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF); // clear flag
return false;
}
data[i] = i2c_get_data(I2C(I2C_GENERAL_I2C)); // read received byte
}
return true;
}
bool i2c_general_write(const uint8_t* data, size_t data_size)
{
// sanity check
if (data==NULL || data_size==0) { // no data to write
return true;
}
if (!(I2C_SR2(I2C(I2C_GENERAL_I2C)) & I2C_SR2_BUSY)) { // I2C device is not busy (start condition has not been sent)
return false; // address has probably also not been sent
}
if (!(I2C_SR2(I2C(I2C_GENERAL_I2C)) & I2C_SR2_MSL)) { // I2C device is not master mode
return false;
}
// write data
for (size_t i=0; i<data_size; i++) { // write bytes
i2c_send_data(I2C(I2C_GENERAL_I2C), data[i]); // send byte to be written in memory
timer_set_counter(TIM(I2C_GENERAL_TIMER),0); // restart timer
timer_clear_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF); // clear flag
timer_enable_counter(TIM(I2C_GENERAL_TIMER)); // enable timer for timeouts
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
timer_disable_counter(TIM(I2C_GENERAL_TIMER)); // disable timer for timeouts
if (timer_get_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF)) { // timeout occurred (no ACK received)
timer_clear_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF); // clear flag
return false;
}
}
return true;
}
void i2c_general_stop(void)
{
// sanity check
if (!(I2C_SR2(I2C(I2C_GENERAL_I2C)) & I2C_SR2_BUSY)) { // release is not busy
return; // bus has probably already been released
}
// send stop condition
i2c_send_stop(I2C(I2C_GENERAL_I2C)); // send stop to release bus
timer_set_counter(TIM(I2C_GENERAL_TIMER), 0); // restart timer
timer_clear_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF); // clear flag
timer_enable_counter(TIM(I2C_GENERAL_TIMER)); // enable timer for timeouts
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)
timer_disable_counter(TIM(I2C_GENERAL_TIMER)); // disable timer for timeouts
if (timer_get_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF)) { // timeout occurred
timer_clear_flag(TIM(I2C_GENERAL_TIMER), TIM_SR_UIF); // clear flag
}
}

56
lib/i2c_general.h Normal file
View File

@ -0,0 +1,56 @@
/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/** library to communicate using I2C as master or slave (API)
* @file i2c_general.h
* @author King Kévin <kingkevin@cuvoodoo.info>
* @date 2018
* @note peripherals used: I2C @ref i2c_general_i2c, timer @ref i2c_general_timer
* @warning only 7-byte I2C slave addresses are supported
*/
#pragma once
/** setup I2C peripheral
* @param[in] fast use standard (100 kHz) or fast (400 kHz) mode
*/
void i2c_general_setup_master(bool fast);
/** check if SDA and SCL signals are high
* @return SDA and SCL signals are high
*/
bool i2c_general_check(void);
/** send start condition
* @return if start condition was sent successfully (true) or error occurred (false)
*/
bool i2c_general_start(void);
/** select slave device
* @warning a start condition should be sent before this operation
* @param[in] slave 7-bit I2C address of slave device to select
* @param[in] write this transaction will be followed by a read (false) or write (true) operation
* @return if slave was selected successfully (true) or error occurred (false)
*/
bool i2c_general_select_slave(uint8_t slave, bool write);
/** read data
* @warning the slave device must be selected before this operation
* @param[out] data array to store bytes read
* @param[in] data_size number of bytes to read
*/
bool i2c_general_read(uint8_t* data, size_t data_size);
/** write data
* @warning the slave device must be selected before this operation
* @param[in] data array of byte to write to slave
* @param[in] data_size number of bytes to write
*/
bool i2c_general_write(const uint8_t* data, size_t data_size);
/** sent stop condition */
void i2c_general_stop(void);