2014-10-15 11:29:41 +02:00
|
|
|
/*
|
2017-06-01 16:00:31 +02:00
|
|
|
* Copyright (C) 2017 Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
* 2014 FU Berlin
|
2014-10-15 11:29:41 +02:00
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2017-06-22 15:43:17 +02:00
|
|
|
* @ingroup cpu_stm32f4
|
|
|
|
* @ingroup drivers_periph_i2c
|
2014-10-15 11:29:41 +02:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Low-level I2C driver implementation
|
|
|
|
*
|
|
|
|
* @note This implementation only implements the 7-bit addressing mode.
|
|
|
|
*
|
|
|
|
* @author Peter Kietzmann <peter.kietzmann@haw-hamburg.de>
|
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
2017-06-01 16:00:31 +02:00
|
|
|
* @author Thomas Eichinger <thomas.eichinger@fu-berlin.de>
|
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
2014-10-15 11:29:41 +02:00
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "irq.h"
|
2015-01-19 12:15:45 +01:00
|
|
|
#include "mutex.h"
|
2014-10-15 11:29:41 +02:00
|
|
|
#include "periph_conf.h"
|
|
|
|
#include "periph/i2c.h"
|
|
|
|
|
|
|
|
#define ENABLE_DEBUG (0)
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
/* static function definitions */
|
|
|
|
static void _i2c_init(I2C_TypeDef *i2c, int ccr);
|
|
|
|
static void _toggle_pins(GPIO_TypeDef *port_scl, GPIO_TypeDef *port_sda, int pin_scl, int pin_sda);
|
|
|
|
static void _pin_config(GPIO_TypeDef *port_scl, GPIO_TypeDef *port_sda, int pin_scl, int pin_sda);
|
|
|
|
static void _start(I2C_TypeDef *dev, uint8_t address, uint8_t rw_flag);
|
|
|
|
static inline void _clear_addr(I2C_TypeDef *dev);
|
2016-09-30 23:01:46 +02:00
|
|
|
static inline void _write(I2C_TypeDef *dev, const uint8_t *data, int length);
|
2014-10-15 11:29:41 +02:00
|
|
|
static inline void _stop(I2C_TypeDef *dev);
|
|
|
|
|
2015-01-19 12:15:45 +01:00
|
|
|
/**
|
|
|
|
* @brief Array holding one pre-initialized mutex for each I2C device
|
|
|
|
*/
|
|
|
|
static mutex_t locks[] = {
|
|
|
|
#if I2C_0_EN
|
|
|
|
[I2C_0] = MUTEX_INIT,
|
|
|
|
#endif
|
|
|
|
#if I2C_1_EN
|
|
|
|
[I2C_1] = MUTEX_INIT,
|
|
|
|
#endif
|
|
|
|
#if I2C_2_EN
|
|
|
|
[I2C_2] = MUTEX_INIT
|
|
|
|
#endif
|
|
|
|
#if I2C_3_EN
|
|
|
|
[I2C_3] = MUTEX_INIT
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2014-10-15 11:29:41 +02:00
|
|
|
int i2c_init_master(i2c_t dev, i2c_speed_t speed)
|
|
|
|
{
|
|
|
|
I2C_TypeDef *i2c;
|
|
|
|
GPIO_TypeDef *port_scl;
|
|
|
|
GPIO_TypeDef *port_sda;
|
|
|
|
int pin_scl = 0, pin_sda = 0;
|
|
|
|
int ccr;
|
|
|
|
|
|
|
|
/* read speed configuration */
|
|
|
|
switch (speed) {
|
2016-12-08 12:38:12 +01:00
|
|
|
case I2C_SPEED_LOW:
|
|
|
|
/* 10Kbit/s */
|
|
|
|
ccr = I2C_APBCLK / 20000;
|
|
|
|
break;
|
|
|
|
|
2014-10-15 11:29:41 +02:00
|
|
|
case I2C_SPEED_NORMAL:
|
2016-12-08 12:38:12 +01:00
|
|
|
/* 100Kbit/s */
|
2014-10-15 11:29:41 +02:00
|
|
|
ccr = I2C_APBCLK / 200000;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case I2C_SPEED_FAST:
|
|
|
|
ccr = I2C_APBCLK / 800000;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read static device configuration */
|
|
|
|
switch (dev) {
|
|
|
|
#if I2C_0_EN
|
|
|
|
case I2C_0:
|
|
|
|
i2c = I2C_0_DEV;
|
|
|
|
port_scl = I2C_0_SCL_PORT;
|
|
|
|
pin_scl = I2C_0_SCL_PIN;
|
|
|
|
port_sda = I2C_0_SDA_PORT;
|
|
|
|
pin_sda = I2C_0_SDA_PIN;
|
|
|
|
I2C_0_CLKEN();
|
|
|
|
I2C_0_SCL_CLKEN();
|
|
|
|
I2C_0_SDA_CLKEN();
|
|
|
|
NVIC_SetPriority(I2C_0_ERR_IRQ, I2C_IRQ_PRIO);
|
|
|
|
NVIC_EnableIRQ(I2C_0_ERR_IRQ);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* configure pins */
|
|
|
|
_pin_config(port_scl, port_sda, pin_scl, pin_sda);
|
|
|
|
|
|
|
|
/* configure device */
|
|
|
|
_i2c_init(i2c, ccr);
|
|
|
|
|
|
|
|
/* make sure the analog filters don't hang -> see errata sheet 2.14.7 */
|
|
|
|
if (i2c->SR2 & I2C_SR2_BUSY) {
|
|
|
|
DEBUG("LINE BUSY AFTER RESET -> toggle pins now\n");
|
|
|
|
/* disable peripheral */
|
|
|
|
i2c->CR1 &= ~I2C_CR1_PE;
|
|
|
|
/* toggle both pins to reset analog filter */
|
|
|
|
_toggle_pins(port_scl, port_sda, pin_scl, pin_sda);
|
|
|
|
/* reset pins for alternate function */
|
|
|
|
_pin_config(port_scl, port_sda, pin_scl, pin_sda);
|
|
|
|
/* make peripheral soft reset */
|
|
|
|
i2c->CR1 |= I2C_CR1_SWRST;
|
|
|
|
i2c->CR1 &= ~I2C_CR1_SWRST;
|
|
|
|
/* enable device */
|
|
|
|
_i2c_init(i2c, ccr);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _i2c_init(I2C_TypeDef *i2c, int ccr)
|
|
|
|
{
|
|
|
|
/* disable device and set ACK bit */
|
|
|
|
i2c->CR1 = I2C_CR1_ACK;
|
|
|
|
/* configure I2C clock */
|
2017-06-01 16:00:31 +02:00
|
|
|
i2c->CR2 = (I2C_APBCLK / 1000000);
|
2014-10-15 11:29:41 +02:00
|
|
|
i2c->CCR = ccr;
|
|
|
|
i2c->TRISE = (I2C_APBCLK / 1000000) + 1;
|
|
|
|
/* configure device */
|
|
|
|
i2c->OAR1 = 0; /* makes sure we are in 7-bit address mode */
|
|
|
|
/* enable device */
|
|
|
|
i2c->CR1 |= I2C_CR1_PE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _pin_config(GPIO_TypeDef *port_scl, GPIO_TypeDef *port_sda, int pin_scl, int pin_sda)
|
|
|
|
{
|
|
|
|
/* Set GPIOs to AF mode */
|
|
|
|
port_scl->MODER &= ~(3 << (2 * pin_scl));
|
|
|
|
port_scl->MODER |= (2 << (2 * pin_scl));
|
|
|
|
port_sda->MODER &= ~(3 << (2 * pin_sda));
|
|
|
|
port_sda->MODER |= (2 << (2 * pin_sda));
|
|
|
|
|
|
|
|
/* Set speed high*/
|
|
|
|
port_scl->OSPEEDR |= (3 << (2 * pin_scl));
|
|
|
|
port_sda->OSPEEDR |= (3 << (2 * pin_sda));
|
|
|
|
|
|
|
|
/* Set to push-pull configuration open drain*/
|
|
|
|
port_scl->OTYPER |= (1 << pin_scl);
|
|
|
|
port_sda->OTYPER |= (1 << pin_sda);
|
|
|
|
|
|
|
|
/* Enable pull-up resistors */
|
|
|
|
port_scl->PUPDR &= ~(3 << (2 * pin_scl));
|
|
|
|
port_scl->PUPDR |= (1 << (2 * pin_scl));
|
|
|
|
port_sda->PUPDR &= ~(3 << (2 * pin_sda));
|
|
|
|
port_sda->PUPDR |= (1 << (2 * pin_sda));
|
|
|
|
|
|
|
|
/* Configure GPIOs to for the I2C alternate function */
|
|
|
|
if (pin_scl < 8) {
|
|
|
|
port_scl->AFR[0] &= ~(0xf << (4 * pin_scl));
|
|
|
|
port_scl->AFR[0] |= (I2C_0_SCL_AF << (4 * pin_scl));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
port_scl->AFR[1] &= ~(0xf << (4 * (pin_scl - 8)));
|
|
|
|
port_scl->AFR[1] |= (I2C_0_SCL_AF << (4 * (pin_scl - 8)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pin_sda < 8) {
|
|
|
|
port_sda->AFR[0] &= ~(0xf << (4 * pin_sda));
|
|
|
|
port_sda->AFR[0] |= (I2C_0_SDA_AF << (4 * pin_sda));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
port_sda->AFR[1] &= ~(0xf << (4 * (pin_sda - 8)));
|
|
|
|
port_sda->AFR[1] |= (I2C_0_SDA_AF << (4 * (pin_sda - 8)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _toggle_pins(GPIO_TypeDef *port_scl, GPIO_TypeDef *port_sda, int pin_scl, int pin_sda)
|
|
|
|
{
|
|
|
|
/* Set GPIOs to General purpose output mode mode */
|
|
|
|
port_scl->MODER &= ~(3 << (2 * pin_scl));
|
|
|
|
port_scl->MODER |= (1 << (2 * pin_scl));
|
|
|
|
port_sda->MODER &= ~(3 << (2 * pin_sda));
|
|
|
|
port_sda->MODER |= (1 << (2 * pin_sda));
|
|
|
|
|
|
|
|
/* Set speed high*/
|
|
|
|
port_scl->OSPEEDR |= (3 << (2 * pin_scl));
|
|
|
|
port_sda->OSPEEDR |= (3 << (2 * pin_sda));
|
|
|
|
|
|
|
|
/* Set to push-pull configuration open drain*/
|
|
|
|
port_scl->OTYPER |= (1 << pin_scl);
|
|
|
|
port_sda->OTYPER |= (1 << pin_sda);
|
|
|
|
|
|
|
|
/* set both to high */
|
|
|
|
port_scl->ODR |= (1 << pin_scl);
|
|
|
|
port_sda->ODR |= (1 << pin_sda);
|
|
|
|
/* set SDA to low */
|
|
|
|
port_sda->ODR &= ~(1 << pin_sda);
|
|
|
|
/* set SCL to low */
|
|
|
|
port_scl->ODR &= ~(1 << pin_scl);
|
|
|
|
/* set SCL to high */
|
|
|
|
port_scl->ODR |= (1 << pin_scl);
|
|
|
|
/* set SDA to high */
|
|
|
|
port_sda->ODR |= (1 << pin_sda);
|
|
|
|
}
|
|
|
|
|
2015-01-19 12:15:45 +01:00
|
|
|
int i2c_acquire(i2c_t dev)
|
|
|
|
{
|
|
|
|
if (dev >= I2C_NUMOF) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
mutex_lock(&locks[dev]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int i2c_release(i2c_t dev)
|
|
|
|
{
|
|
|
|
if (dev >= I2C_NUMOF) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
mutex_unlock(&locks[dev]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-30 23:01:46 +02:00
|
|
|
int i2c_read_byte(i2c_t dev, uint8_t address, void *data)
|
2014-10-15 11:29:41 +02:00
|
|
|
{
|
|
|
|
return i2c_read_bytes(dev, address, data, 1);
|
|
|
|
}
|
|
|
|
|
2016-09-30 23:01:46 +02:00
|
|
|
int i2c_read_bytes(i2c_t dev, uint8_t address, void *data, int length)
|
2014-10-15 11:29:41 +02:00
|
|
|
{
|
2017-06-01 16:00:31 +02:00
|
|
|
int n = length;
|
2014-10-15 11:29:41 +02:00
|
|
|
I2C_TypeDef *i2c;
|
2017-11-09 16:25:09 +01:00
|
|
|
char *in = (char *)data;
|
2014-10-15 11:29:41 +02:00
|
|
|
|
|
|
|
switch (dev) {
|
|
|
|
#if I2C_0_EN
|
|
|
|
case I2C_0:
|
|
|
|
i2c = I2C_0_DEV;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-06-01 16:00:31 +02:00
|
|
|
DEBUG("Send Slave address and wait for ADDR == 1\n");
|
|
|
|
_start(i2c, address, I2C_FLAG_READ);
|
2014-10-15 11:29:41 +02:00
|
|
|
|
2017-06-01 16:00:31 +02:00
|
|
|
if (length == 1) {
|
|
|
|
DEBUG("Set ACK = 0\n");
|
|
|
|
i2c->CR1 &= ~(I2C_CR1_ACK);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i2c->CR1 |= I2C_CR1_ACK;
|
|
|
|
}
|
2014-10-15 11:29:41 +02:00
|
|
|
|
2017-06-01 16:00:31 +02:00
|
|
|
_clear_addr(i2c);
|
2014-10-15 11:29:41 +02:00
|
|
|
|
2017-06-01 16:00:31 +02:00
|
|
|
while(n--) {
|
|
|
|
/* wait for reception to complete */
|
|
|
|
while (!(i2c->SR1 & I2C_SR1_RXNE)) {}
|
2014-10-15 11:29:41 +02:00
|
|
|
|
2017-06-01 16:00:31 +02:00
|
|
|
if (n == 1) {
|
|
|
|
/* disable ACK */
|
2014-10-15 11:29:41 +02:00
|
|
|
i2c->CR1 &= ~(I2C_CR1_ACK);
|
2017-06-01 16:00:31 +02:00
|
|
|
}
|
2014-10-15 11:29:41 +02:00
|
|
|
|
2017-06-01 16:00:31 +02:00
|
|
|
/* read byte */
|
make: fix various compile errors with Wextra
pkg, nordic_softdevice_ble: disable CFLAGS to omit compiler error
sys, pm_layered: fix casting nonscalar to the same type
cpu, stm32_common: fix type-limits, remove always true assert
cpu, stm32f4: fix pointer arithmetic in periph/i2c
drivers, at86rf2xx: fix type-limits where condition always true
saul, gpio: fix if no gpio configured for saul
cpu, saml21: add frequency check to periph/timer
driver, cc110x: fix unused param and type-limts errors
boards, wsn430-common: fix old-style-declaration
make: fix old style definition
drivers, sdcard_spi: fix old style typedef
driver, at30tse: remove unnecessary check
driver, nrf24: fix type-limit
driver, pn532: change buffer from char to uint8_t
tests/driver_sdcard: fix type limits
boards, feather-m0: add missing field inits
driver, tcs37727: fix type limits
pkg, emb6: disable some compiler warnings
tests/emb6: disable some compiler warings
pkg, openthread: fix sign compare and unused params
tests/trickle: fix struct init
tests/pthread_cooperation: fix type limits
board, mips-malta: remove feature periph_uart
shell: fix var size for netif command
gnrc, netif: fix sign-compare
gnrc, nib: fix sign-compare
shell: fix output in netif command
posix: fix type-limits in pthread_cond
2017-10-31 11:52:18 +01:00
|
|
|
*(in++) = i2c->DR;
|
2017-06-01 16:00:31 +02:00
|
|
|
}
|
2014-10-15 11:29:41 +02:00
|
|
|
|
2017-06-01 16:00:31 +02:00
|
|
|
/* set STOP */
|
|
|
|
i2c->CR1 |= (I2C_CR1_STOP);
|
2014-10-15 11:29:41 +02:00
|
|
|
|
2017-06-01 16:00:31 +02:00
|
|
|
while (i2c->CR1 & I2C_CR1_STOP) {}
|
2014-10-15 11:29:41 +02:00
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2016-09-30 23:01:46 +02:00
|
|
|
int i2c_read_reg(i2c_t dev, uint8_t address, uint8_t reg, void *data)
|
2014-10-15 11:29:41 +02:00
|
|
|
{
|
|
|
|
return i2c_read_regs(dev, address, reg, data, 1);
|
|
|
|
}
|
|
|
|
|
2016-09-30 23:01:46 +02:00
|
|
|
int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, void *data, int length)
|
2014-10-15 11:29:41 +02:00
|
|
|
{
|
|
|
|
I2C_TypeDef *i2c;
|
|
|
|
|
|
|
|
switch (dev) {
|
|
|
|
#if I2C_0_EN
|
|
|
|
case I2C_0:
|
|
|
|
i2c = I2C_0_DEV;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send start condition and slave address */
|
|
|
|
DEBUG("Send slave address and clear ADDR flag\n");
|
|
|
|
_start(i2c, address, I2C_FLAG_WRITE);
|
|
|
|
DEBUG("Write reg into DR\n");
|
2017-06-01 16:00:31 +02:00
|
|
|
_clear_addr(i2c);
|
|
|
|
while (!(i2c->SR1 & I2C_SR1_TXE)) {}
|
2014-10-15 11:29:41 +02:00
|
|
|
i2c->DR = reg;
|
2017-06-01 16:00:31 +02:00
|
|
|
while (!(i2c->SR1 & I2C_SR1_TXE)) {}
|
2014-10-15 11:29:41 +02:00
|
|
|
DEBUG("Now start a read transaction\n");
|
|
|
|
return i2c_read_bytes(dev, address, data, length);
|
|
|
|
}
|
|
|
|
|
2016-09-30 23:01:46 +02:00
|
|
|
int i2c_write_byte(i2c_t dev, uint8_t address, uint8_t data)
|
2014-10-15 11:29:41 +02:00
|
|
|
{
|
|
|
|
return i2c_write_bytes(dev, address, &data, 1);
|
|
|
|
}
|
|
|
|
|
2016-09-30 23:01:46 +02:00
|
|
|
int i2c_write_bytes(i2c_t dev, uint8_t address, const void *data, int length)
|
2014-10-15 11:29:41 +02:00
|
|
|
{
|
|
|
|
I2C_TypeDef *i2c;
|
|
|
|
|
|
|
|
switch (dev) {
|
|
|
|
#if I2C_0_EN
|
|
|
|
case I2C_0:
|
|
|
|
i2c = I2C_0_DEV;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* start transmission and send slave address */
|
|
|
|
DEBUG("sending start sequence\n");
|
|
|
|
_start(i2c, address, I2C_FLAG_WRITE);
|
|
|
|
_clear_addr(i2c);
|
|
|
|
/* send out data bytes */
|
|
|
|
_write(i2c, data, length);
|
|
|
|
/* end transmission */
|
|
|
|
DEBUG("Ending transmission\n");
|
|
|
|
_stop(i2c);
|
|
|
|
DEBUG("STOP condition was send out\n");
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2016-09-30 23:01:46 +02:00
|
|
|
int i2c_write_reg(i2c_t dev, uint8_t address, uint8_t reg, uint8_t data)
|
2014-10-15 11:29:41 +02:00
|
|
|
{
|
|
|
|
return i2c_write_regs(dev, address, reg, &data, 1);
|
|
|
|
}
|
|
|
|
|
2016-09-30 23:01:46 +02:00
|
|
|
int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, const void *data, int length)
|
2014-10-15 11:29:41 +02:00
|
|
|
{
|
|
|
|
I2C_TypeDef *i2c;
|
|
|
|
|
|
|
|
switch (dev) {
|
|
|
|
#if I2C_0_EN
|
|
|
|
case I2C_0:
|
|
|
|
i2c = I2C_0_DEV;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* start transmission and send slave address */
|
|
|
|
_start(i2c, address, I2C_FLAG_WRITE);
|
|
|
|
_clear_addr(i2c);
|
|
|
|
/* send register address and wait for complete transfer to be finished*/
|
2016-09-30 23:01:46 +02:00
|
|
|
_write(i2c, ®, 1);
|
2014-10-15 11:29:41 +02:00
|
|
|
/* write data to register */
|
|
|
|
_write(i2c, data, length);
|
|
|
|
/* finish transfer */
|
|
|
|
_stop(i2c);
|
|
|
|
/* return number of bytes send */
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
void i2c_poweron(i2c_t dev)
|
|
|
|
{
|
|
|
|
switch (dev) {
|
|
|
|
#if I2C_0_EN
|
|
|
|
case I2C_0:
|
|
|
|
I2C_0_CLKEN();
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void i2c_poweroff(i2c_t dev)
|
|
|
|
{
|
|
|
|
switch (dev) {
|
|
|
|
#if I2C_0_EN
|
|
|
|
case I2C_0:
|
2016-01-17 12:20:17 +01:00
|
|
|
while (I2C_0_DEV->SR2 & I2C_SR2_BUSY) {}
|
2014-10-15 11:29:41 +02:00
|
|
|
|
|
|
|
I2C_0_CLKDIS();
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _start(I2C_TypeDef *dev, uint8_t address, uint8_t rw_flag)
|
|
|
|
{
|
2017-06-01 16:00:31 +02:00
|
|
|
start:
|
2014-10-15 11:29:41 +02:00
|
|
|
|
|
|
|
/* generate start condition */
|
|
|
|
dev->CR1 |= I2C_CR1_START;
|
|
|
|
|
2017-06-01 16:00:31 +02:00
|
|
|
/* Wait for SB flag to be set */
|
2016-01-17 12:20:17 +01:00
|
|
|
while (!(dev->SR1 & I2C_SR1_SB)) {}
|
2014-10-15 11:29:41 +02:00
|
|
|
|
|
|
|
/* send address and read/write flag */
|
|
|
|
dev->DR = (address << 1) | rw_flag;
|
|
|
|
|
2017-06-01 16:00:31 +02:00
|
|
|
/* Wait for ADDR flag to be set */
|
|
|
|
while (!(dev->SR1 & I2C_SR1_ADDR)) {
|
|
|
|
if (dev->SR1 & I2C_SR1_AF) {
|
|
|
|
/* if the device answers NACK on sending the address, retry */
|
|
|
|
dev->SR1 &= ~(I2C_SR1_AF);
|
|
|
|
goto start;
|
|
|
|
}
|
|
|
|
}
|
2014-10-15 11:29:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void _clear_addr(I2C_TypeDef *dev)
|
|
|
|
{
|
|
|
|
dev->SR1;
|
|
|
|
dev->SR2;
|
|
|
|
}
|
|
|
|
|
2016-09-30 23:01:46 +02:00
|
|
|
static inline void _write(I2C_TypeDef *dev, const uint8_t *data, int length)
|
2014-10-15 11:29:41 +02:00
|
|
|
{
|
|
|
|
DEBUG("Looping through bytes\n");
|
|
|
|
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
/* write data to data register */
|
2016-09-30 23:01:46 +02:00
|
|
|
dev->DR = data[i];
|
2014-10-15 11:29:41 +02:00
|
|
|
DEBUG("Written %i byte to data reg, now waiting for DR to be empty again\n", i);
|
|
|
|
|
|
|
|
/* wait for transfer to finish */
|
2016-01-17 12:20:17 +01:00
|
|
|
while (!(dev->SR1 & I2C_SR1_TXE)) {}
|
2014-10-15 11:29:41 +02:00
|
|
|
|
|
|
|
DEBUG("DR is now empty again\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void _stop(I2C_TypeDef *dev)
|
|
|
|
{
|
|
|
|
/* make sure last byte was send */
|
|
|
|
DEBUG("Wait if last byte hasn't been sent\n");
|
|
|
|
|
2016-01-17 12:20:17 +01:00
|
|
|
while (!(dev->SR1 & I2C_SR1_BTF)) {}
|
2014-10-15 11:29:41 +02:00
|
|
|
|
|
|
|
/* send STOP condition */
|
|
|
|
dev->CR1 |= I2C_CR1_STOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if I2C_0_EN
|
|
|
|
void I2C_0_ERR_ISR(void)
|
|
|
|
{
|
|
|
|
unsigned state = I2C_0_DEV->SR1;
|
|
|
|
DEBUG("\n\n### I2C ERROR OCCURED ###\n");
|
|
|
|
DEBUG("status: %08x\n", state);
|
|
|
|
if (state & I2C_SR1_OVR) {
|
|
|
|
DEBUG("OVR\n");
|
|
|
|
}
|
|
|
|
if (state & I2C_SR1_AF) {
|
|
|
|
DEBUG("AF\n");
|
|
|
|
}
|
|
|
|
if (state & I2C_SR1_ARLO) {
|
|
|
|
DEBUG("ARLO\n");
|
|
|
|
}
|
|
|
|
if (state & I2C_SR1_BERR) {
|
|
|
|
DEBUG("BERR\n");
|
|
|
|
}
|
|
|
|
if (state & I2C_SR1_PECERR) {
|
|
|
|
DEBUG("PECERR\n");
|
|
|
|
}
|
|
|
|
if (state & I2C_SR1_TIMEOUT) {
|
|
|
|
DEBUG("TIMEOUT\n");
|
|
|
|
}
|
|
|
|
if (state & I2C_SR1_SMBALERT) {
|
|
|
|
DEBUG("SMBALERT\n");
|
|
|
|
}
|
2016-01-17 12:20:17 +01:00
|
|
|
while (1) {}
|
2014-10-15 11:29:41 +02:00
|
|
|
}
|
|
|
|
#endif /* I2C_0_EN */
|