add general I2C library

This commit is contained in:
King Kévin 2017-02-08 17:22:47 +01:00
parent 7fa8d251cd
commit 0bb18e7622
2 changed files with 212 additions and 0 deletions

167
lib/i2c.c Normal file
View File

@ -0,0 +1,167 @@
/* 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 (code)
* @file i2c.h
* @author King Kévin <kingkevin@cuvoodoo.info>
* @date 2017
* @note peripherals used: I2C @ref i2c_i2c, timer @i2c_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 "global.h" // global utilities
#include "i2c.h" // I2C header and definitions
/** @defgroup i2c_i2c I2C peripheral used to communicate
* @{
*/
#define I2C_I2C 2 /**< I2C peripheral */
/** @} */
void i2c_master(bool fast)
{
// configure I2C peripheral
rcc_periph_clock_enable(RCC_I2C_SCL_PORT(I2C_I2C)); // enable clock for I2C I/O peripheral
gpio_set_mode(I2C_SCL_PORT(I2C_I2C), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, I2C_SCL_PIN(I2C_I2C)); // setup I2C I/O pins
rcc_periph_clock_enable(RCC_I2C_SCL_PORT(I2C_I2C)); // enable clock for I2C I/O peripheral
gpio_set_mode(I2C_SDA_PORT(I2C_I2C), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, I2C_SDA_PIN(I2C_I2C)); // setup I2C I/O pins
rcc_periph_clock_enable(RCC_AFIO); // enable clock for alternate function
rcc_periph_clock_enable(RCC_I2C(I2C_I2C)); // enable clock for I2C peripheral
i2c_reset(I2C(I2C_I2C)); // reset configuration
i2c_peripheral_disable(I2C(I2C_I2C)); // I2C needs to be disable to be configured
i2c_set_clock_frequency(I2C(I2C_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_I2C));
i2c_set_ccr(I2C(I2C_I2C), rcc_apb1_frequency/(400000*2)); // set Thigh/Tlow to generate frequency of 400 kHz
i2c_set_trise(I2C(I2C_I2C), (300/(1000/(rcc_apb1_frequency/1000000)))+1); // max rise time for 300 kHz is 300 ns
} else {
i2c_set_standard_mode(I2C(I2C_I2C)); // the DS1307 has a maximum I2C SCL freq if 100 kHz (corresponding to the standard mode)
i2c_set_ccr(I2C(I2C_I2C), rcc_apb1_frequency/(100000*2)); // set Thigh/Tlow to generate frequency of 100 kHz
i2c_set_trise(I2C(I2C_I2C), (1000/(1000/(rcc_apb1_frequency/1000000)))+1); // max rise time for 100 kHz is 1000 ns (~1 MHz)
}
i2c_peripheral_enable(I2C(I2C_I2C)); // enable I2C after configuration completed
}
bool i2c_read(uint8_t slave, const uint8_t* address, size_t address_size, uint8_t* data, size_t data_size)
{
// sanity check
if (address==NULL || address_size==0 || data==NULL || data_size==0) {
return false;
}
bool to_return = false; // return if read succeeded
// send start condition
i2c_send_start(I2C(I2C_I2C)); // send start condition to start transaction
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_SB)); // wait until start condition is transmitted
if (!(I2C_SR2(I2C(I2C_I2C)) & I2C_SR2_MSL)) { // verify if in master mode
goto error;
}
// select slave
i2c_send_7bit_address(I2C(I2C_I2C), slave, I2C_WRITE); // select slave
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_ADDR)); // wait until address is transmitted
if (!((I2C_SR2(I2C(I2C_I2C)) & I2C_SR2_TRA))) { // verify we are in transmit mode (and read SR2 to clear ADDR)
goto error;
}
// send address
for (size_t i=0; i<address_size; i++) {
i2c_send_data(I2C(I2C_I2C), address[i]); // send memory address we want to read
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_TxE)); // wait until byte has been transmitted
}
// switch to read mode
i2c_send_start(I2C(I2C_I2C)); // send restart condition to switch from write to read mode
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_SB)); // wait until start condition is transmitted
i2c_send_7bit_address(I2C(I2C_I2C), slave, I2C_READ); // select slave
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_ADDR)); // wait until address is transmitted
if ((I2C_SR2(I2C(I2C_I2C)) & I2C_SR2_TRA)) { // verify we are in read mode (and read SR2 to clear ADDR)
goto error;
}
// 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_I2C)); // NACK received to stop slave transmission
i2c_send_stop(I2C(I2C_I2C)); // send STOP after receiving byte
} else {
i2c_enable_ack(I2C(I2C_I2C)); // ACK received byte to continue slave transmission
}
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_RxNE)); // wait until byte has been received
data[i] = i2c_get_data(I2C(I2C_I2C)); // read received byte
}
to_return = true;
error:
if (I2C_SR2(I2C(I2C_I2C)) & I2C_SR2_BUSY) { // release bus if busy
i2c_send_stop(I2C(I2C_I2C)); // send stop to release bus
}
while (I2C_SR2(I2C(I2C_I2C)) & I2C_SR2_MSL); // wait until bus released (non master mode)
return to_return;
}
bool i2c_write(uint8_t slave, const uint8_t* address, size_t address_size, const uint8_t* data, size_t data_size)
{
// sanity check
if (address==NULL || address_size==0 || data==NULL || data_size==0) {
return false;
}
bool to_return = false; // return if read succeeded
// send start condition
i2c_send_start(I2C(I2C_I2C)); // send start condition to start transaction
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_SB)); // wait until start condition is transmitted
if (!(I2C_SR2(I2C(I2C_I2C)) & I2C_SR2_MSL)) { // verify if in master mode
goto error;
}
// select slave
i2c_send_7bit_address(I2C(I2C_I2C), slave, I2C_WRITE); // select slave
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_ADDR)); // wait until address is transmitted
if (!((I2C_SR2(I2C(I2C_I2C)) & I2C_SR2_TRA))) { // verify we are in transmit mode (and read SR2 to clear ADDR)
goto error;
}
// send address
for (size_t i=0; i<address_size; i++) {
i2c_send_data(I2C(I2C_I2C), address[i]); // send memory address we want to read
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_TxE)); // wait until byte has been transmitted
}
// write data
for (size_t i=0; i<data_size; i++) { // write bytes
i2c_send_data(I2C(I2C_I2C), data[i]); // send byte to be written in memory
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_TxE)); // wait until byte has been transmitted
}
to_return = true;
error:
if (I2C_SR2(I2C(I2C_I2C)) & I2C_SR2_BUSY) { // release bus if busy
i2c_send_stop(I2C(I2C_I2C)); // send stop to release bus
}
while (I2C_SR2(I2C(I2C_I2C)) & I2C_SR2_MSL); // wait until bus released (non master mode)
return to_return;
}

45
lib/i2c.h Normal file
View File

@ -0,0 +1,45 @@
/* 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 (API)
* @file i2c.h
* @author King Kévin <kingkevin@cuvoodoo.info>
* @date 2017
* @note peripherals used: I2C @ref i2c_i2c, timer @i2c_timer
*/
#pragma once
/** setup I2C peripheral
* @param[in] fast use standard (100 kHz) or fast (400 kHz) mode
*/
void i2c_master(bool fast);
/** read from I2C slave
* @param[in] slave 7-bit I2C salve device address to read from
* @param[in] address memory address of slave to read from
* @param[in] address_size address size in bytes
* @param[out] data array to store bytes read
* @pramr[in] data_size number of bytes to read
* @return if read succeeded
*/
bool i2c_read(uint8_t slave, const uint8_t* address, size_t address_size, uint8_t* data, size_t data_size);
/** write to I2C slave
* @param[in] slave 7-bit I2C salve device address to write to
* @param[in] address memory address of slave to write to
* @param[in] address_size address size in bytes
* @param[in] data array of byte to write to slave
* @pramr[in] data_size number of bytes to write
* @return if write succeeded
*/
bool i2c_write(uint8_t slave, const uint8_t* address, size_t address_size, const uint8_t* data, size_t data_size);