mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-01-18 12:52:44 +01:00
Merge pull request #1785 from haukepetersen/add_stm32f1_i2c
cpu/stm32f1: added I2C interface&implementation
This commit is contained in:
commit
5d08a0e30c
@ -1 +1 @@
|
||||
FEATURES_PROVIDED = transceiver periph_gpio periph_spi
|
||||
FEATURES_PROVIDED = transceiver periph_gpio periph_uart periph_spi periph_i2c
|
||||
|
@ -281,5 +281,29 @@
|
||||
#define RTT_PRESCALER (0x7fff) /* run with 1 Hz */
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @name I2C configuration
|
||||
* @{
|
||||
*/
|
||||
#define I2C_NUMOF (1U)
|
||||
#define I2C_0_EN 1
|
||||
#define I2C_IRQ_PRIO 1
|
||||
#define I2C_APBCLK (36000000U)
|
||||
|
||||
/* I2C 0 device configuration */
|
||||
#define I2C_0_DEV I2C1
|
||||
#define I2C_0_CLKEN() (RCC->APB1ENR |= RCC_APB1ENR_I2C1EN)
|
||||
#define I2C_0_CLKDIS() (RCC->APB1ENR &= ~(RCC_APB1ENR_I2C1EN))
|
||||
#define I2C_0_IRQ I2C1_EV_IRQn
|
||||
#define I2C_0_ISR isr_i2c1
|
||||
/* I2C 0 pin configuration */
|
||||
#define I2C_0_SCL_PORT GPIOB
|
||||
#define I2C_0_SCL_PIN 6
|
||||
#define I2C_0_SCL_CLKEN() (RCC->APB2ENR |= RCC_APB2ENR_IOPBEN)
|
||||
#define I2C_0_SDA_PORT GPIOB
|
||||
#define I2C_0_SDA_PIN 7
|
||||
#define I2C_0_SDA_CLKEN() (RCC->APB2ENR |= RCC_APB2ENR_IOPBEN)
|
||||
/** @} */
|
||||
|
||||
#endif /* __PERIPH_CONF_H */
|
||||
/** @} */
|
||||
|
390
cpu/stm32f1/periph/i2c.c
Normal file
390
cpu/stm32f1/periph/i2c.c
Normal file
@ -0,0 +1,390 @@
|
||||
/*
|
||||
* Copyright (C) 2014 FU Berlin
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup driver_periph
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Low-level I2C driver implementation
|
||||
*
|
||||
* @note This implementation only implements the 7-bit addressing mode.
|
||||
*
|
||||
* For implementation details please refer to STM application note AN2824.
|
||||
*
|
||||
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "cpu.h"
|
||||
#include "irq.h"
|
||||
#include "periph_conf.h"
|
||||
#include "periph/i2c.h"
|
||||
|
||||
#define ENABLE_DEBUG (0)
|
||||
#include "debug.h"
|
||||
|
||||
/* guard file in case no I2C device is defined */
|
||||
#if I2C_NUMOF
|
||||
|
||||
/* static function definitions */
|
||||
static void _start(I2C_TypeDef *dev, uint8_t address, uint8_t rw_flag);
|
||||
static inline void _clear_addr(I2C_TypeDef *dev);
|
||||
static inline void _write(I2C_TypeDef *dev, char *data, int length);
|
||||
static inline void _stop(I2C_TypeDef *dev);
|
||||
|
||||
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, pin_sda;
|
||||
int ccr;
|
||||
|
||||
/* read speed configuration */
|
||||
switch (speed) {
|
||||
case I2C_SPEED_NORMAL:
|
||||
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();
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* configure pins, alternate output, open-drain, output mode with 50MHz */
|
||||
if (pin_scl < 8) {
|
||||
port_scl->CRL |= (0xf << (pin_scl * 4));
|
||||
}
|
||||
else {
|
||||
port_scl->CRH |= (0xf << ((pin_scl - 8) * 4));
|
||||
}
|
||||
if (pin_sda < 8) {
|
||||
port_sda->CRL |= (0xf << (pin_sda * 4));
|
||||
}
|
||||
else {
|
||||
port_sda->CRH |= (0xf << ((pin_sda - 8) * 4));
|
||||
}
|
||||
|
||||
/* disable device and set ACK bit */
|
||||
i2c->CR1 = I2C_CR1_ACK;
|
||||
/* configure I2C clock */
|
||||
i2c->CR2 = (I2C_APBCLK / 1000000);
|
||||
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;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int i2c_init_slave(i2c_t dev, uint8_t address)
|
||||
{
|
||||
/* TODO: implement slave mode */
|
||||
return -1;
|
||||
}
|
||||
|
||||
int i2c_read_byte(i2c_t dev, uint8_t address, char *data)
|
||||
{
|
||||
return i2c_read_bytes(dev, address, data, 1);
|
||||
}
|
||||
|
||||
int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
|
||||
{
|
||||
unsigned int state;
|
||||
int i = 0;
|
||||
I2C_TypeDef *i2c;
|
||||
|
||||
switch (dev) {
|
||||
#if I2C_0_EN
|
||||
case I2C_0:
|
||||
i2c = I2C_0_DEV;
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
switch (length) {
|
||||
case 1:
|
||||
DEBUG("Send Slave address and wait for ADDR == 1\n");
|
||||
_start(i2c, address, I2C_FLAG_READ);
|
||||
|
||||
DEBUG("Set ACK = 0\n");
|
||||
i2c->CR1 &= ~(I2C_CR1_ACK);
|
||||
|
||||
DEBUG("Clear ADDR and set STOP = 1\n");
|
||||
state = disableIRQ();
|
||||
_clear_addr(i2c);
|
||||
i2c->CR1 |= (I2C_CR1_STOP);
|
||||
restoreIRQ(state);
|
||||
|
||||
DEBUG("Wait for RXNE == 1\n");
|
||||
while (!(i2c->SR1 & I2C_SR1_RXNE));
|
||||
|
||||
DEBUG("Read received data\n");
|
||||
*data = (char)i2c->DR;
|
||||
/* wait until STOP is cleared by hardware */
|
||||
while (i2c->CR1 & I2C_CR1_STOP);
|
||||
/* reset ACK to be able to receive new data */
|
||||
i2c->CR1 |= (I2C_CR1_ACK);
|
||||
break;
|
||||
case 2:
|
||||
DEBUG("Send Slave address and wait for ADDR == 1\n");
|
||||
_start(i2c, address, I2C_FLAG_READ);
|
||||
DEBUG("Set POS bit\n");
|
||||
i2c->CR1 |= I2C_CR1_POS;
|
||||
DEBUG("Crit block: Clear ADDR bit and clear ACK flag\n");
|
||||
state = disableIRQ();
|
||||
_clear_addr(i2c);
|
||||
i2c->CR1 &= ~(I2C_CR1_ACK);
|
||||
restoreIRQ(state);
|
||||
|
||||
DEBUG("Wait for transfer to be completed\n");
|
||||
while (!(i2c->SR1 & I2C_SR1_BTF));
|
||||
|
||||
DEBUG("Crit block: set STOP and read first byte\n");
|
||||
state = disableIRQ();
|
||||
i2c->CR1 |= (I2C_CR1_STOP);
|
||||
data[0] = (char)i2c->DR;
|
||||
restoreIRQ(state);
|
||||
|
||||
DEBUG("read second byte\n");
|
||||
data[1] = (char)i2c->DR;
|
||||
|
||||
DEBUG("wait for STOP bit to be cleared again\n");
|
||||
while (i2c->CR1 & I2C_CR1_STOP);
|
||||
|
||||
DEBUG("reset POS = 0 and ACK = 1\n");
|
||||
i2c->CR1 &= ~(I2C_CR1_POS);
|
||||
i2c->CR1 |= (I2C_CR1_ACK);
|
||||
break;
|
||||
default:
|
||||
DEBUG("Send Slave address and wait for ADDR == 1\n");
|
||||
_start(i2c, address, I2C_FLAG_READ);
|
||||
_clear_addr(i2c);
|
||||
|
||||
while (i < (length - 2)) {
|
||||
DEBUG("Wait until byte was received\n");
|
||||
while (!(i2c->SR1 & I2C_SR1_BTF));
|
||||
DEBUG("Copy byte from DR\n");
|
||||
data[i++] = (char)i2c->DR;
|
||||
}
|
||||
|
||||
DEBUG("Reading the last 3 bytes, waiting for BTF flag\n");
|
||||
while (!(i2c->SR1 & I2C_SR1_BTF));
|
||||
|
||||
DEBUG("Disable ACK\n");
|
||||
i2c->CR1 &= ~(I2C_CR1_ACK);
|
||||
|
||||
DEBUG("Crit block: set STOP and read second last byte\n");
|
||||
state = disableIRQ();
|
||||
i2c->CR1 |= (I2C_CR1_STOP);
|
||||
data[i++] = (char)i2c->DR;
|
||||
restoreIRQ(state);
|
||||
while (!(i2c->SR1 & I2C_SR1_RXNE));
|
||||
data[i++] = (char)i2c->DR;
|
||||
|
||||
DEBUG("wait for STOP bit to be cleared again\n");
|
||||
while (i2c->CR1 & I2C_CR1_STOP);
|
||||
|
||||
DEBUG("reset POS = 0 and ACK = 1\n");
|
||||
i2c->CR1 &= ~(I2C_CR1_POS);
|
||||
i2c->CR1 |= (I2C_CR1_ACK);
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
int i2c_read_reg(i2c_t dev, uint8_t address, uint8_t reg, char *data)
|
||||
{
|
||||
return i2c_read_regs(dev, address, reg, data, 1);
|
||||
|
||||
}
|
||||
|
||||
int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int length)
|
||||
{
|
||||
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);
|
||||
_clear_addr(i2c);
|
||||
DEBUG("Write reg into DR\n");
|
||||
i2c->DR = reg;
|
||||
_stop(i2c);
|
||||
DEBUG("Now start a read transaction\n");
|
||||
return i2c_read_bytes(dev, address, data, length);
|
||||
}
|
||||
|
||||
int i2c_write_byte(i2c_t dev, uint8_t address, char data)
|
||||
{
|
||||
return i2c_write_bytes(dev, address, &data, 1);
|
||||
}
|
||||
|
||||
int i2c_write_bytes(i2c_t dev, uint8_t address, char *data, int length)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
int i2c_write_reg(i2c_t dev, uint8_t address, uint8_t reg, char data)
|
||||
{
|
||||
return i2c_write_regs(dev, address, reg, &data, 1);
|
||||
}
|
||||
|
||||
int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int length)
|
||||
{
|
||||
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*/
|
||||
_write(i2c, (char *)(®), 1);
|
||||
/* 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:
|
||||
while (I2C_0_DEV->SR2 & I2C_SR2_BUSY);
|
||||
I2C_0_CLKDIS();
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
static void _start(I2C_TypeDef *dev, uint8_t address, uint8_t rw_flag)
|
||||
{
|
||||
/* wait for device to be ready */
|
||||
DEBUG("Wait for device to be ready\n");
|
||||
while (dev->SR2 & I2C_SR2_BUSY);
|
||||
/* generate start condition */
|
||||
DEBUG("Generate start condition\n");
|
||||
dev->CR1 |= I2C_CR1_START;
|
||||
DEBUG("Wait for SB flag to be set\n");
|
||||
while (!(dev->SR1 & I2C_SR1_SB));
|
||||
/* send address and read/write flag */
|
||||
DEBUG("Send address\n");
|
||||
dev->DR = (address << 1) | rw_flag;
|
||||
/* clear ADDR flag by reading first SR1 and then SR2 */
|
||||
DEBUG("Wait for ADDR flag to be set\n");
|
||||
while (!(dev->SR1 & I2C_SR1_ADDR));
|
||||
}
|
||||
|
||||
static inline void _clear_addr(I2C_TypeDef *dev)
|
||||
{
|
||||
dev->SR1;
|
||||
dev->SR2;
|
||||
}
|
||||
|
||||
static inline void _write(I2C_TypeDef *dev, char *data, int length)
|
||||
{
|
||||
DEBUG("Looping through bytes\n");
|
||||
for (int i = 0; i < length; i++) {
|
||||
/* write data to data register */
|
||||
dev->DR = (uint8_t)data[i];
|
||||
DEBUG("Written %i byte to data reg, now waiting for DR to be empty again\n", i);
|
||||
/* wait for transfer to finish */
|
||||
while (!(dev->SR1 & I2C_SR1_TXE));
|
||||
DEBUG("DR is now empty again\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static inline void _stop(I2C_TypeDef *dev)
|
||||
{
|
||||
/* make sure last byte was send */
|
||||
while (!(dev->SR1 & I2C_SR1_BTF));
|
||||
/* send STOP condition */
|
||||
dev->CR1 |= I2C_CR1_STOP;
|
||||
}
|
||||
|
||||
#endif /* I2C_NUMOF */
|
219
drivers/include/periph/i2c.h
Normal file
219
drivers/include/periph/i2c.h
Normal file
@ -0,0 +1,219 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Freie Universität Berlin
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup driver_periph
|
||||
* @brief Low-level I2C peripheral driver
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Low-level I2C peripheral driver interface definition
|
||||
*
|
||||
* @note The current version of this interface only supports the 7-bit addressing mode.
|
||||
*
|
||||
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
||||
*/
|
||||
|
||||
#ifndef __I2C_H
|
||||
#define __I2C_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "periph_conf.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* guard this file in case no I2C device is defined */
|
||||
#if I2C_NUMOF
|
||||
|
||||
/** @brief Flag to use for reading from the I2C bus */
|
||||
#define I2C_FLAG_WRITE 0
|
||||
/** @brief Flag to use for writing to the I2C bus */
|
||||
#define I2C_FLAG_READ 1
|
||||
|
||||
/**
|
||||
* @brief define I2C device identifiers
|
||||
*/
|
||||
typedef enum {
|
||||
#if I2C_0_EN
|
||||
I2C_0 = 0, /**< I2C device 0 */
|
||||
#endif
|
||||
#if I2C_1_EN
|
||||
I2C_1, /**< I2C device 1 */
|
||||
#endif
|
||||
#if I2C_2_EN
|
||||
I2C_2, /**< I2C device 2 */
|
||||
#endif
|
||||
#if I2C_3_EN
|
||||
I2C_3, /**< I2C device 3 */
|
||||
#endif
|
||||
} i2c_t;
|
||||
|
||||
/**
|
||||
* @brief define I2C bus speed values
|
||||
*/
|
||||
typedef enum {
|
||||
I2C_SPEED_LOW = 0, /**< low speed mode: ~10kbit/s */
|
||||
I2C_SPEED_NORMAL, /**< normal mode: ~100kbit/s */
|
||||
I2C_SPEED_FAST, /**< fast mode: ~400kbit/sj */
|
||||
I2C_SPEED_FAST_PLUS, /**< fast plus mode: ~1Mbit/s */
|
||||
I2C_SPEED_HIGH, /**< high speed mode: ~3.4Mbit/s */
|
||||
} i2c_speed_t;
|
||||
|
||||
/**
|
||||
* @brief Initialize an I2C device to run as bus master
|
||||
*
|
||||
* @param[in] dev the device to initialize
|
||||
* @param[in] speed the selected bus speed
|
||||
*
|
||||
* @return 0 on successful initialization
|
||||
* @return -1 on undefined device given
|
||||
* @return -2 on unsupported speed value
|
||||
*/
|
||||
int i2c_init_master(i2c_t dev, i2c_speed_t speed);
|
||||
|
||||
/**
|
||||
* @brief Initialize an I2C device to run in slave mode
|
||||
*
|
||||
* @param[in] dev the device to initialize
|
||||
* @param[in] address the devices I2C address
|
||||
*
|
||||
* @return 0 on success
|
||||
* @return -1 on undefined device given
|
||||
*/
|
||||
int i2c_init_slave(i2c_t dev, uint8_t address);
|
||||
|
||||
/**
|
||||
* @brief Read one byte from an I2C device with the given address
|
||||
*
|
||||
* @param[in] dev I2C peripheral device
|
||||
* @param[in] address bus address of the target device
|
||||
* @param[out] data the result that was read
|
||||
*
|
||||
* @return the number of bytes that were read
|
||||
* @return -1 on undefined device given
|
||||
* @return -2 on invalid address
|
||||
*/
|
||||
int i2c_read_byte(i2c_t dev, uint8_t address, char *data);
|
||||
|
||||
/**
|
||||
* @brief Read multiple bytes from an I2C device with the given address
|
||||
*
|
||||
* @param[in] dev I2C peripheral device
|
||||
* @param[in] address bus address of the target device
|
||||
* @param[out] data array holding the received bytes
|
||||
* @param[in] length the number of bytes to read into `data`
|
||||
*
|
||||
* @return the number of bytes that were read
|
||||
* @return -1 on undefined device given
|
||||
*/
|
||||
int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length);
|
||||
|
||||
/**
|
||||
* @brief Read one byte from a register at the I2C slave with the given address
|
||||
*
|
||||
* @param[in] dev I2C peripheral device
|
||||
* @param[in] address bus address of the target device
|
||||
* @param[in] reg the register address on the targeted I2C device
|
||||
* @param[out] data the result that was read
|
||||
*
|
||||
* @return the number of bytes that were read
|
||||
* @return -1 on undefined device given
|
||||
*/
|
||||
int i2c_read_reg(i2c_t dev, uint8_t address, uint8_t reg, char *data);
|
||||
|
||||
/**
|
||||
* @brief Read multiple bytes from a register at the I2C slave with the given address
|
||||
*
|
||||
* @param[in] dev I2C peripheral device
|
||||
* @param[in] address bus address of the target device
|
||||
* @param[in] reg the register address on the targeted I2C device
|
||||
* @param[out] data array holding the received bytes
|
||||
* @param[in] length the number of bytes to read into `data`
|
||||
*
|
||||
* @return the number of bytes that were read
|
||||
* @return -1 on undefined device given
|
||||
*/
|
||||
int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int length);
|
||||
|
||||
/**
|
||||
* @brief Write one byte to an I2C device with the given address
|
||||
*
|
||||
* @param[in] dev I2C peripheral device
|
||||
* @param[in] address bus address of the target device
|
||||
* @param[in] data byte to write to the device
|
||||
*
|
||||
* @return the number of bytes that were written
|
||||
* @return -1 on undefined device given
|
||||
*/
|
||||
int i2c_write_byte(i2c_t dev, uint8_t address, char data);
|
||||
|
||||
/**
|
||||
* @brief Write multiple bytes to an I2C device with the given address
|
||||
*
|
||||
* @param[in] dev I2C peripheral device
|
||||
* @param[in] address bus address of the target device
|
||||
* @param[in] data array with bytes to write to the target device
|
||||
* @param[in] length number of bytes to write to the target device
|
||||
*
|
||||
* @return the number of bytes that were written
|
||||
* @return -1 on undefined device given
|
||||
*/
|
||||
int i2c_write_bytes(i2c_t dev, uint8_t address, char *data, int length);
|
||||
|
||||
/**
|
||||
* @brief Write one byte to a register at the I2C slave with the given address
|
||||
*
|
||||
* @param[in] dev I2C peripheral device
|
||||
* @param[in] address bus address of the target device
|
||||
* @param[in] reg the register address on the targeted I2C device
|
||||
* @param[in] data byte to write to the device
|
||||
*
|
||||
* @return the number of bytes that were written
|
||||
* @return -1 on undefined device given
|
||||
*/
|
||||
int i2c_write_reg(i2c_t dev, uint8_t address, uint8_t reg, char data);
|
||||
|
||||
/**
|
||||
* @brief Write multiple bytes to a register at the I2C slave with the given address
|
||||
*
|
||||
* @param[in] dev I2C peripheral device
|
||||
* @param[in] address bus address of the target device
|
||||
* @param[in] reg the register address on the targeted I2C device
|
||||
* @param[in] data array with bytes to write to the target device
|
||||
* @param[in] length number of bytes to write to the target device
|
||||
*
|
||||
* @return the number of bytes that were written
|
||||
* @return -1 on undefined device given
|
||||
*/
|
||||
int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int length);
|
||||
|
||||
/**
|
||||
* @brief Power on the given I2C peripheral
|
||||
*
|
||||
* @param[in] dev the I2C device to power on
|
||||
*/
|
||||
void i2c_poweron(i2c_t dev);
|
||||
|
||||
/**
|
||||
* @brief Power off the given I2C peripheral
|
||||
*
|
||||
* @param[in] dev the I2C device to power off
|
||||
*/
|
||||
void i2c_poweroff(i2c_t dev);
|
||||
|
||||
#endif /* I2C_NUMOF */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __I2C_H */
|
||||
/** @} */
|
9
tests/periph_i2c/Makefile
Normal file
9
tests/periph_i2c/Makefile
Normal file
@ -0,0 +1,9 @@
|
||||
APPLICATION = periph_i2c
|
||||
include ../Makefile.tests_common
|
||||
|
||||
FEATURES_REQUIRED = periph_i2c
|
||||
|
||||
USEMODULE += shell
|
||||
USEMODULE += uart0
|
||||
|
||||
include $(RIOTBASE)/Makefile.include
|
8
tests/periph_i2c/README.md
Normal file
8
tests/periph_i2c/README.md
Normal file
@ -0,0 +1,8 @@
|
||||
Expected result
|
||||
===============
|
||||
This test enables you to test all available low-level I2C functions. Consult the 'help'
|
||||
shell command for available actions.
|
||||
|
||||
Background
|
||||
==========
|
||||
Test for the low-level I2C driver.
|
322
tests/periph_i2c/main.c
Normal file
322
tests/periph_i2c/main.c
Normal file
@ -0,0 +1,322 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Freie Universität Berlin
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup tests
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Test application for the low-level I2C peripheral driver
|
||||
*
|
||||
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "periph_conf.h"
|
||||
#include "periph/i2c.h"
|
||||
#include "shell.h"
|
||||
#include "posix_io.h"
|
||||
#include "board_uart0.h"
|
||||
|
||||
#define BUFSIZE (128U)
|
||||
|
||||
static int i2c_dev = -1;
|
||||
|
||||
void cmd_init_master(int argc, char **argv)
|
||||
{
|
||||
int dev, speed, res;
|
||||
|
||||
if (argc != 3) {
|
||||
puts("Error: Init: Invalid number of arguments!");
|
||||
printf("Usage:\n%s: [DEVICE] [SPEED]\n", argv[0]);
|
||||
puts(" with DEVICE:");
|
||||
for (int i = 0; i < I2C_NUMOF; i++) {
|
||||
printf(" %i -> I2C_%i\n", i, i);
|
||||
}
|
||||
puts(" SPEED:");
|
||||
puts(" 0 -> SPEED_LOW (10kbit/s)");
|
||||
puts(" 1 -> SPEED_NORMAL (100kbit/s)");
|
||||
puts(" 2 -> SPEED_FAST (400kbit/s)");
|
||||
puts(" 3 -> SPEED_FAST_PLUS (1Mbit/s)");
|
||||
puts(" 4 -> SPEED_HIGH (3.4Mbit/s)\n");
|
||||
return;
|
||||
}
|
||||
|
||||
dev = atoi(argv[1]);
|
||||
speed = atoi(argv[2]);
|
||||
|
||||
res = i2c_init_master(dev, speed);
|
||||
if (res == -1) {
|
||||
puts("Error: Init: Given device not available");
|
||||
return;
|
||||
}
|
||||
else if (res == -2) {
|
||||
puts("Error: Init: Unsupported speed value");
|
||||
return;
|
||||
}
|
||||
else {
|
||||
printf("I2C_%i successfully initialized as master!\n", dev);
|
||||
i2c_dev = dev;
|
||||
}
|
||||
}
|
||||
|
||||
void cmd_init_slave(int argc, char **argv)
|
||||
{
|
||||
int dev, addr, res;
|
||||
|
||||
if (argc != 3) {
|
||||
puts("Error: Invalid number of arguments!");
|
||||
printf("Usage:\n%s: [DEVICE] [ADDRESS]\n", argv[0]);
|
||||
puts(" with DEVICE:");
|
||||
for (int i = 0; i < I2C_NUMOF; i++) {
|
||||
printf(" %i -> I2C_%i\n", i, i);
|
||||
}
|
||||
puts(" ADDRESS: value between 0 and 127");
|
||||
return;
|
||||
}
|
||||
|
||||
dev = atoi(argv[1]);
|
||||
addr = atoi(argv[1]);
|
||||
|
||||
res = i2c_init_slave(dev, addr);
|
||||
if (res == -1) {
|
||||
puts("Error: Init: Given device not available");
|
||||
return;
|
||||
}
|
||||
else if (res == -2) {
|
||||
puts("Error: Init: Invalid address given");
|
||||
return;
|
||||
}
|
||||
else {
|
||||
printf("I2C_%i successfully initialized as slave with address %i!\n", dev, addr);
|
||||
i2c_dev = dev;
|
||||
}
|
||||
}
|
||||
|
||||
void cmd_write(int argc, char **argv)
|
||||
{
|
||||
int res;
|
||||
uint8_t addr;
|
||||
int length = argc - 2;
|
||||
char data[BUFSIZE];
|
||||
|
||||
if (i2c_dev < 0) {
|
||||
puts("Error: no I2C device was initialized");
|
||||
return;
|
||||
}
|
||||
if (argc < 3) {
|
||||
puts("Error: not enough arguments given");
|
||||
printf("Usage:\n%s: ADDR BYTE0 [BYTE1 [BYTE_n [...]]]\n", argv[0]);
|
||||
return;
|
||||
}
|
||||
|
||||
addr = (uint8_t)atoi(argv[1]);
|
||||
for (int i = 0; i < length; i++) {
|
||||
data[i] = (char)atoi(argv[i + 2]);
|
||||
}
|
||||
|
||||
if (length == 1) {
|
||||
printf("i2c_write_byte(I2C_%i, 0x%02x, 0x%02x)\n", i2c_dev, addr, (unsigned int)data[0]);
|
||||
res = i2c_write_byte(i2c_dev, addr, data[0]);
|
||||
}
|
||||
else {
|
||||
printf("i2c_write_bytes(I2C_%i, 0x%02x, [", i2c_dev, addr);
|
||||
for (int i = 0; i < length; i++) {
|
||||
printf(", 0x%02x", (unsigned int)data[i]);
|
||||
}
|
||||
puts("])");
|
||||
res = i2c_write_bytes(i2c_dev, addr, data, length);
|
||||
}
|
||||
|
||||
if (res < 0) {
|
||||
puts("Error: no bytes were written");
|
||||
}
|
||||
else {
|
||||
printf("I2C_%i: successfully wrote %i bytes to the bus\n", i2c_dev, res);
|
||||
}
|
||||
}
|
||||
|
||||
void cmd_write_reg(int argc, char **argv)
|
||||
{
|
||||
int res;
|
||||
uint8_t addr, reg;
|
||||
int length = argc - 3;
|
||||
char data[BUFSIZE];
|
||||
|
||||
if (i2c_dev < 0) {
|
||||
puts("Error: no I2C device initialized");
|
||||
return;
|
||||
}
|
||||
if (length < 1) {
|
||||
puts("Error: not enough arguments given");
|
||||
printf("Usage:\n%s ADDR REG BYTE0 [BYTE1 ...]\n", argv[0]);
|
||||
return;
|
||||
}
|
||||
|
||||
addr = (uint8_t)atoi(argv[1]);
|
||||
reg = (uint8_t)atoi(argv[2]);
|
||||
for (int i = 0; i < length; i++) {
|
||||
data[i] = (char)atoi(argv[i + 3]);
|
||||
}
|
||||
|
||||
if (length == 1) {
|
||||
printf("i2c_write_reg(I2C_%i, 0x%02x, 0x%02x, 0x%02x)\n",
|
||||
i2c_dev, addr, reg, (unsigned int)data[0]);
|
||||
res = i2c_write_reg(i2c_dev, addr, reg, data[0]);
|
||||
}
|
||||
else {
|
||||
printf("i2c_write_regs(I2C_%i, 0x%02x, 0x%02x, [", i2c_dev, addr, reg);
|
||||
for (int i = 0; i < length; i++) {
|
||||
printf("0x%02x, ", (unsigned int)data[i]);
|
||||
}
|
||||
puts("])");
|
||||
res = i2c_write_regs(i2c_dev, addr, reg, data, length);
|
||||
}
|
||||
|
||||
if (res < 1) {
|
||||
puts("Error: no bytes were written");
|
||||
}
|
||||
else {
|
||||
printf("I2C_%i: successfully wrote %i bytes to register 0x%02x\n", i2c_dev, res, reg);
|
||||
}
|
||||
}
|
||||
|
||||
void cmd_read(int argc, char **argv)
|
||||
{
|
||||
int res;
|
||||
uint8_t addr;
|
||||
int length;
|
||||
char data[BUFSIZE];
|
||||
|
||||
if (i2c_dev < 0) {
|
||||
puts("Error: no I2C device initialized");
|
||||
return;
|
||||
}
|
||||
if (argc < 3) {
|
||||
puts("Error: not enough arguments given");
|
||||
printf("Usage:\n%s ADDR LENGTH]\n", argv[0]);
|
||||
return;
|
||||
}
|
||||
|
||||
addr = (uint8_t)atoi(argv[1]);
|
||||
length = atoi(argv[2]);
|
||||
|
||||
if (length < 1 || length > BUFSIZE) {
|
||||
puts("Error: invalid LENGTH parameter given\n");
|
||||
return;
|
||||
}
|
||||
else if (length == 1) {
|
||||
printf("i2c_read_byte(I2C_%i, 0x%02x, char *res)\n", i2c_dev, addr);
|
||||
res = i2c_read_byte(i2c_dev, addr, data);
|
||||
}
|
||||
else {
|
||||
printf("i2c_read_bytes(I2C_%i, 0x%02x, char *res, %i)\n", i2c_dev, addr, length);
|
||||
res = i2c_read_bytes(i2c_dev, addr, data, length);
|
||||
}
|
||||
|
||||
if (res < 1) {
|
||||
puts("Error: no bytes were read");
|
||||
}
|
||||
else {
|
||||
printf("I2C_%i: successfully read %i bytes:\n [", i2c_dev, res);
|
||||
for (int i = 0; i < res; i++) {
|
||||
printf("0x%02x, ", (unsigned int)data[i]);
|
||||
}
|
||||
puts("])");
|
||||
}
|
||||
}
|
||||
|
||||
void cmd_read_reg(int argc, char **argv)
|
||||
{
|
||||
int res;
|
||||
uint8_t addr, reg;
|
||||
int length;
|
||||
char data[BUFSIZE];
|
||||
|
||||
if (i2c_dev < 0) {
|
||||
puts("Error: no I2C device initialized");
|
||||
return;
|
||||
}
|
||||
if (argc < 4) {
|
||||
puts("Error: not enough arguments given");
|
||||
printf("Usage:\n%s ADDR REG LENGTH]\n", argv[0]);
|
||||
return;
|
||||
}
|
||||
|
||||
addr = (uint8_t)atoi(argv[1]);
|
||||
reg = (uint8_t)atoi(argv[2]);
|
||||
length = atoi(argv[3]);
|
||||
|
||||
if (length < 1 || length > BUFSIZE) {
|
||||
puts("Error: invalid LENGTH parameter given");
|
||||
return;
|
||||
}
|
||||
else if (length == 1) {
|
||||
printf("i2c_read_reg(I2C_%i, 0x%02x, 0x%02x, char *res)\n", i2c_dev, addr, reg);
|
||||
res = i2c_read_reg(i2c_dev, addr, reg, data);
|
||||
}
|
||||
else {
|
||||
printf("i2c_read_regs(I2C_%i, 0x%02x, 0x%02x, char *res, %i)\n", i2c_dev, addr, reg, length);
|
||||
res = i2c_read_regs(i2c_dev, addr, reg, data, length);
|
||||
}
|
||||
|
||||
if (res < 1) {
|
||||
puts("Error: no bytes were read");
|
||||
}
|
||||
else {
|
||||
printf("I2C_%i: successfully read %i bytes from reg 0x%02x:\n [", i2c_dev, res, reg);
|
||||
for (int i = 0; i < res; i++) {
|
||||
printf("0x%02x, ", (unsigned int)data[i]);
|
||||
}
|
||||
puts("])");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static const shell_command_t shell_commands[] = {
|
||||
{ "init_master", "Initialize I2C as master", cmd_init_master },
|
||||
{ "init_slave", "Initialize I2C as slave", cmd_init_slave },
|
||||
{ "w", "write bytes to given address", cmd_write },
|
||||
{ "wr", "write to register ", cmd_write_reg },
|
||||
{ "r", "read bytes from given address", cmd_read },
|
||||
{ "rr", "read bytes from register", cmd_read_reg },
|
||||
{ NULL, NULL, NULL }
|
||||
};
|
||||
|
||||
static int shell_readc(void)
|
||||
{
|
||||
char c = 0;
|
||||
(void) posix_read(uart0_handler_pid, &c, 1);
|
||||
return c;
|
||||
}
|
||||
|
||||
static void shell_putchar(int c)
|
||||
{
|
||||
(void) putchar(c);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
shell_t shell;
|
||||
|
||||
puts("Test for the low-level I2C driver");
|
||||
|
||||
/* prepare I/O for shell */
|
||||
board_uart0_init();
|
||||
posix_open(uart0_handler_pid, 0);
|
||||
|
||||
/* define own shell commands */
|
||||
shell_init(&shell, shell_commands, UART0_BUFSIZE, shell_readc, shell_putchar);
|
||||
shell_run(&shell);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user