2011-03-27 17:03:49 +02:00
|
|
|
/************************************************************************************
|
2011-03-27 21:53:36 +02:00
|
|
|
* arch/arm/src/stm32/stm32_i2c.c
|
2011-03-27 17:03:49 +02:00
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Uros Platise. All rights reserved.
|
|
|
|
* Author: Uros Platise <uros.platise@isotel.eu>
|
|
|
|
*
|
2011-09-09 21:30:03 +02:00
|
|
|
* With extensions, modifications by:
|
|
|
|
*
|
2011-10-02 00:09:00 +02:00
|
|
|
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
|
2011-09-09 21:30:03 +02:00
|
|
|
* Author: Gregroy Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
2011-03-27 17:03:49 +02:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
/* \file
|
2011-03-27 17:03:49 +02:00
|
|
|
* \author Uros Platise
|
|
|
|
* \brief STM32 I2C Hardware Layer - Device Driver
|
|
|
|
*
|
|
|
|
* Supports:
|
|
|
|
* - Master operation, 100 kHz (standard) and 400 kHz (full speed)
|
|
|
|
* - Multiple instances (shared bus)
|
|
|
|
* - Interrupt based operation
|
|
|
|
*
|
|
|
|
* Structure naming:
|
2011-04-03 16:26:05 +02:00
|
|
|
* - Device: structure as defined by the nuttx/i2c/i2c.h
|
2011-03-27 17:03:49 +02:00
|
|
|
* - Instance: represents each individual access to the I2C driver, obtained by
|
2011-04-03 16:26:05 +02:00
|
|
|
* the i2c_init(); it extends the Device structure from the nuttx/i2c/i2c.h;
|
2011-03-27 17:03:49 +02:00
|
|
|
* Instance points to OPS, to common I2C Hardware private data and contains
|
|
|
|
* its own private data, as frequency, address, mode of operation (in the future)
|
|
|
|
* - Private: Private data of an I2C Hardware
|
|
|
|
*
|
|
|
|
* \todo
|
2011-04-03 16:26:05 +02:00
|
|
|
* - Check for all possible deadlocks (as BUSY='1' I2C needs to be reset in HW using the I2C_CR1_SWRST)
|
2011-03-27 17:03:49 +02:00
|
|
|
* - SMBus support (hardware layer timings are already supported) and add SMBA gpio pin
|
|
|
|
* - Slave support with multiple addresses (on multiple instances):
|
|
|
|
* - 2 x 7-bit address or
|
|
|
|
* - 1 x 10 bit adresses + 1 x 7 bit address (?)
|
|
|
|
* - plus the broadcast address (general call)
|
|
|
|
* - Multi-master support
|
|
|
|
* - DMA (to get rid of too many CPU wake-ups and interventions)
|
|
|
|
* - Be ready for IPMI
|
|
|
|
**/
|
|
|
|
|
2011-07-30 17:31:23 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Included Files
|
|
|
|
************************************************************************************/
|
|
|
|
|
2011-03-27 17:03:49 +02:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2011-04-03 16:26:05 +02:00
|
|
|
#include <stdio.h>
|
2011-03-27 17:03:49 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/irq.h>
|
|
|
|
#include <nuttx/i2c.h>
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/clock.h>
|
|
|
|
|
|
|
|
#include <arch/board/board.h>
|
|
|
|
|
2011-03-27 17:03:49 +02:00
|
|
|
#include "up_arch.h"
|
|
|
|
|
|
|
|
#include "stm32_rcc.h"
|
|
|
|
#include "stm32_i2c.h"
|
2011-04-15 18:20:25 +02:00
|
|
|
#include "stm32_waste.h"
|
2011-03-27 17:03:49 +02:00
|
|
|
|
2011-03-28 17:01:43 +02:00
|
|
|
#if defined(CONFIG_STM32_I2C1) || defined(CONFIG_STM32_I2C2)
|
2011-03-27 17:03:49 +02:00
|
|
|
|
2011-08-30 21:48:47 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
************************************************************************************/
|
|
|
|
/* Configuration ********************************************************************/
|
2011-09-17 17:04:24 +02:00
|
|
|
/* CONFIG_I2C_POLLED may be set so that I2C interrrupts will not be used. Instead,
|
|
|
|
* CPU-intensive polling will be used.
|
|
|
|
*/
|
|
|
|
|
2011-09-08 19:56:08 +02:00
|
|
|
/* Interrupt wait timeout in seconds and milliseconds */
|
2011-08-30 21:48:47 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
#if !defined(CONFIG_STM32_I2CTIMEOSEC) && !defined(CONFIG_STM32_I2CTIMEOMS)
|
|
|
|
# define CONFIG_STM32_I2CTIMEOSEC 0
|
|
|
|
# define CONFIG_STM32_I2CTIMEOMS 500 /* Default is 500 milliseconds */
|
|
|
|
#elif !defined(CONFIG_STM32_I2CTIMEOSEC)
|
|
|
|
# define CONFIG_STM32_I2CTIMEOSEC 0 /* User provided milliseconds */
|
|
|
|
#elif !defined(CONFIG_STM32_I2CTIMEOMS)
|
|
|
|
# define CONFIG_STM32_I2CTIMEOMS 0 /* User provided seconds */
|
2011-08-30 21:48:47 +02:00
|
|
|
#endif
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/* Interrupt wait time timeout in system timer ticks */
|
|
|
|
|
|
|
|
#define CONFIG_STM32_I2CTIMEOTICKS \
|
|
|
|
(SEC2TICK(CONFIG_STM32_I2CTIMEOSEC) + MSEC2TICK(CONFIG_STM32_I2CTIMEOMS))
|
|
|
|
|
2011-09-09 21:50:18 +02:00
|
|
|
/* Debug ****************************************************************************/
|
2011-09-18 19:52:00 +02:00
|
|
|
/* CONFIG_DEBUG_I2C + CONFIG_DEBUG enables general I2C debug output. */
|
2011-09-09 21:50:18 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_I2C
|
|
|
|
# define i2cdbg dbg
|
2011-09-18 19:52:00 +02:00
|
|
|
# define i2cvdbg vdbg
|
2011-09-09 21:50:18 +02:00
|
|
|
#else
|
|
|
|
# define i2cdbg(x...)
|
2011-09-18 19:52:00 +02:00
|
|
|
# define i2cvdbg(x...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* I2C event trace logic. NOTE: trace uses the internal, non-standard, low-level
|
|
|
|
* debug interface lib_rawprintf() but does not require that any other debug
|
|
|
|
* is enabled.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_I2C_TRACE
|
|
|
|
# define stm32_i2c_tracereset(p)
|
|
|
|
# define stm32_i2c_tracenew(p,s)
|
|
|
|
# define stm32_i3c_traceevent(p,e,a)
|
|
|
|
# define stm32_i2c_tracedump(p)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_I2C_NTRACE
|
|
|
|
# define CONFIG_I2C_NTRACE 20
|
2011-09-09 21:50:18 +02:00
|
|
|
#endif
|
|
|
|
|
2011-03-27 17:03:49 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Private Types
|
|
|
|
************************************************************************************/
|
2011-09-10 01:13:17 +02:00
|
|
|
/* Interrupt state */
|
|
|
|
|
|
|
|
enum stm32_intstate_e
|
|
|
|
{
|
|
|
|
INTSTATE_IDLE = 0, /* No I2C activity */
|
|
|
|
INTSTATE_WAITING, /* Waiting for completion of interrupt activity */
|
|
|
|
INTSTATE_DONE, /* Interrupt activity complete */
|
|
|
|
};
|
2011-03-27 17:03:49 +02:00
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
/* Trace events */
|
|
|
|
|
|
|
|
enum stm32_trace_e
|
|
|
|
{
|
|
|
|
I2CEVENT_NONE = 0, /* No events have occurred with this status */
|
|
|
|
I2CEVENT_SB, /* Start/Master, param = msgc */
|
|
|
|
I2CEVENT_SENDBYTE, /* Send byte, param = byte sent */
|
|
|
|
I2CEVENT_READ, /* Read data, param = dcnt */
|
|
|
|
I2CEVENT_ITBUFEN, /* Enable buffer interrupts, param = 0 */
|
|
|
|
I2CEVENT_RXNE, /* Read more dta, param = dcnt */
|
|
|
|
I2CEVENT_REITBUFEN, /* Re-enable buffer interrupts, param = 0 */
|
|
|
|
I2CEVENT_DISITBUFEN, /* Disable buffer interrupts, param = 0 */
|
|
|
|
I2CEVENT_BTFSTART, /* Last byte sent, re-starting, param = msgc */
|
|
|
|
I2CEVENT_BTFSTOP, /* Last byte sten, send stop, param = 0 */
|
|
|
|
I2CEVENT_ERROR /* Error occurred, param = 0 */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Trace data */
|
|
|
|
|
|
|
|
struct stm32_trace_s
|
|
|
|
{
|
|
|
|
uint32_t status; /* I2C 32-bit SR2|SR1 status */
|
|
|
|
uint32_t count; /* Interrupt count when status change */
|
|
|
|
enum stm32_intstate_e event; /* Last event that occurred with this status */
|
|
|
|
uint32_t parm; /* Parameter associated with the event */
|
|
|
|
};
|
|
|
|
|
2011-09-08 19:56:08 +02:00
|
|
|
/* I2C Device Private Data */
|
|
|
|
|
|
|
|
struct stm32_i2c_priv_s
|
|
|
|
{
|
2011-09-09 21:30:03 +02:00
|
|
|
uint32_t base; /* I2C base address */
|
|
|
|
int refs; /* Referernce count */
|
|
|
|
sem_t sem_excl; /* Mutual exclusion semaphore */
|
2011-09-17 17:04:24 +02:00
|
|
|
#ifndef CONFIG_I2C_POLLED
|
2011-09-09 21:30:03 +02:00
|
|
|
sem_t sem_isr; /* Interrupt wait semaphore */
|
2011-09-17 17:04:24 +02:00
|
|
|
#endif
|
2011-09-10 01:13:17 +02:00
|
|
|
volatile uint8_t intstate; /* Interrupt handshake (see enum stm32_intstate_e) */
|
2011-09-08 19:56:08 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
uint8_t msgc; /* Message count */
|
|
|
|
struct i2c_msg_s *msgv; /* Message list */
|
|
|
|
uint8_t *ptr; /* Current message buffer */
|
|
|
|
int dcnt; /* Current message length */
|
|
|
|
uint16_t flags; /* Current message flags */
|
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
/* I2C trace support */
|
|
|
|
|
|
|
|
#ifdef CONFIG_I2C_TRACE
|
|
|
|
int tndx; /* Trace array index */
|
|
|
|
uint32_t isr_count; /* Count of ISRs processed */
|
|
|
|
uint32_t old_status; /* Last 32-bit status value */
|
|
|
|
|
|
|
|
/* The actual trace data */
|
|
|
|
|
|
|
|
struct stm32_trace_s trace[CONFIG_I2C_NTRACE];
|
|
|
|
#endif
|
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
uint32_t status; /* End of transfer SR2|SR1 status */
|
2011-03-27 17:03:49 +02:00
|
|
|
};
|
|
|
|
|
2011-09-08 19:56:08 +02:00
|
|
|
/* I2C Device, Instance */
|
|
|
|
|
|
|
|
struct stm32_i2c_inst_s
|
|
|
|
{
|
2011-09-09 21:30:03 +02:00
|
|
|
struct i2c_ops_s *ops; /* Standard I2C operations */
|
|
|
|
struct stm32_i2c_priv_s *priv; /* Common driver private data structure */
|
2011-03-27 17:03:49 +02:00
|
|
|
|
2011-09-10 01:13:17 +02:00
|
|
|
uint32_t frequency; /* Frequency used in this instantiation */
|
|
|
|
int address; /* Address used in this instantiation */
|
|
|
|
uint16_t flags; /* Flags used in this instantiation */
|
2011-03-28 17:01:43 +02:00
|
|
|
};
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
************************************************************************************/
|
|
|
|
|
|
|
|
static inline uint16_t stm32_i2c_getreg(FAR struct stm32_i2c_priv_s *priv,
|
|
|
|
uint8_t offset);
|
|
|
|
static inline void stm32_i2c_putreg(FAR struct stm32_i2c_priv_s *priv, uint8_t offset,
|
|
|
|
uint16_t value);
|
|
|
|
static inline void stm32_i2c_modifyreg(FAR struct stm32_i2c_priv_s *priv,
|
|
|
|
uint8_t offset, uint16_t clearbits,
|
|
|
|
uint16_t setbits);
|
2011-09-18 19:52:00 +02:00
|
|
|
static inline void stm32_i2c_sem_wait(FAR struct i2c_dev_s *dev);
|
|
|
|
static inline int stm32_i2c_sem_waitdone(FAR struct stm32_i2c_priv_s *priv);
|
|
|
|
static inline void stm32_i2c_sem_waitstop(FAR struct stm32_i2c_priv_s *priv);
|
|
|
|
static inline void stm32_i2c_sem_post(FAR struct i2c_dev_s *dev);
|
|
|
|
static inline void stm32_i2c_sem_init(FAR struct i2c_dev_s *dev);
|
|
|
|
static inline void stm32_i2c_sem_destroy(FAR struct i2c_dev_s *dev);
|
|
|
|
#ifdef CONFIG_I2C_TRACE
|
|
|
|
static void stm32_i2c_tracereset(FAR struct stm32_i2c_priv_s *priv);
|
|
|
|
static void stm32_i2c_tracenew(FAR struct stm32_i2c_priv_s *priv, uint32_t status);
|
|
|
|
static void stm32_i3c_traceevent(FAR struct stm32_i2c_priv_s *priv,
|
|
|
|
enum stm32_trace_e event, uint32_t parm);
|
|
|
|
static void stm32_i2c_tracedump(FAR struct stm32_i2c_priv_s *priv);
|
|
|
|
#endif
|
2011-09-17 17:04:24 +02:00
|
|
|
static void stm32_i2c_setclock(FAR struct stm32_i2c_priv_s *priv,
|
|
|
|
uint32_t frequency);
|
|
|
|
static inline void stm32_i2c_sendstart(FAR struct stm32_i2c_priv_s *priv);
|
|
|
|
static inline void stm32_i2c_clrstart(FAR struct stm32_i2c_priv_s *priv);
|
|
|
|
static inline void stm32_i2c_sendstop(FAR struct stm32_i2c_priv_s *priv);
|
|
|
|
static inline uint32_t stm32_i2c_getstatus(FAR struct stm32_i2c_priv_s *priv);
|
|
|
|
#if defined(CONFIG_STM32_FSMC) && defined (CONFIG_STM32_I2C1)
|
|
|
|
static inline uint32_t stm32_i2c_disablefsmc(FAR struct stm32_i2c_priv_s *priv);
|
|
|
|
static inline void stm32_i2c_enablefsmc(uint32_t ahbenr);
|
|
|
|
#endif
|
|
|
|
static int stm32_i2c_isr(struct stm32_i2c_priv_s * priv);
|
|
|
|
#ifndef CONFIG_I2C_POLLED
|
|
|
|
#ifdef CONFIG_STM32_I2C1
|
|
|
|
static int stm32_i2c1_isr(int irq, void *context);
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_STM32_I2C2
|
|
|
|
static int stm32_i2c2_isr(int irq, void *context);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
static int stm32_i2c_init(FAR struct stm32_i2c_priv_s *priv);
|
|
|
|
static int stm32_i2c_deinit(FAR struct stm32_i2c_priv_s *priv);
|
|
|
|
static uint32_t stm32_i2c_setfrequency(FAR struct i2c_dev_s *dev,
|
|
|
|
uint32_t frequency);
|
|
|
|
static int stm32_i2c_setaddress(FAR struct i2c_dev_s *dev, int addr, int nbits);
|
|
|
|
static int stm32_i2c_process(FAR struct i2c_dev_s *dev, FAR struct i2c_msg_s *msgs,
|
|
|
|
int count);
|
|
|
|
static int stm32_i2c_write(FAR struct i2c_dev_s *dev, const uint8_t *buffer,
|
|
|
|
int buflen);
|
|
|
|
static int stm32_i2c_read(FAR struct i2c_dev_s *dev, uint8_t *buffer, int buflen);
|
|
|
|
#ifdef CONFIG_I2C_WRITEREAD
|
|
|
|
static int stm32_i2c_writeread(FAR struct i2c_dev_s *dev,
|
|
|
|
const uint8_t *wbuffer, int wbuflen,
|
|
|
|
uint8_t *buffer, int buflen);
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_I2C_TRANSFER
|
|
|
|
static int stm32_i2c_transfer(FAR struct i2c_dev_s *dev, FAR struct i2c_msg_s *msgs,
|
|
|
|
int count);
|
|
|
|
#endif
|
|
|
|
|
2011-03-28 17:01:43 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Private Data
|
|
|
|
************************************************************************************/
|
|
|
|
|
2011-09-08 19:56:08 +02:00
|
|
|
#ifdef CONFIG_STM32_I2C1
|
|
|
|
struct stm32_i2c_priv_s stm32_i2c1_priv =
|
|
|
|
{
|
|
|
|
.base = STM32_I2C1_BASE,
|
|
|
|
.refs = 0,
|
2011-09-10 01:13:17 +02:00
|
|
|
.intstate = INTSTATE_IDLE,
|
2011-09-08 19:56:08 +02:00
|
|
|
.msgc = 0,
|
|
|
|
.msgv = NULL,
|
|
|
|
.ptr = NULL,
|
|
|
|
.dcnt = 0,
|
|
|
|
.flags = 0,
|
|
|
|
.status = 0
|
2011-03-27 17:03:49 +02:00
|
|
|
};
|
2011-03-28 17:01:43 +02:00
|
|
|
#endif
|
2011-03-27 17:03:49 +02:00
|
|
|
|
2011-09-08 19:56:08 +02:00
|
|
|
#ifdef CONFIG_STM32_I2C2
|
|
|
|
struct stm32_i2c_priv_s stm32_i2c2_priv =
|
|
|
|
{
|
|
|
|
.base = STM32_I2C2_BASE,
|
|
|
|
.refs = 0,
|
2011-09-10 01:13:17 +02:00
|
|
|
.intstate = INTSTATE_IDLE,
|
2011-09-08 19:56:08 +02:00
|
|
|
.msgc = 0,
|
|
|
|
.msgv = NULL,
|
|
|
|
.ptr = NULL,
|
|
|
|
.dcnt = 0,
|
|
|
|
.flags = 0,
|
|
|
|
.status = 0
|
2011-03-28 17:01:43 +02:00
|
|
|
};
|
|
|
|
#endif
|
2011-03-27 17:03:49 +02:00
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/* Device Structures, Instantiation */
|
|
|
|
|
|
|
|
struct i2c_ops_s stm32_i2c_ops =
|
|
|
|
{
|
|
|
|
.setfrequency = stm32_i2c_setfrequency,
|
|
|
|
.setaddress = stm32_i2c_setaddress,
|
|
|
|
.write = stm32_i2c_write,
|
|
|
|
.read = stm32_i2c_read
|
|
|
|
#ifdef CONFIG_I2C_WRITEREAD
|
|
|
|
, .writeread = stm32_i2c_writeread
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_I2C_TRANSFER
|
|
|
|
, .transfer = stm32_i2c_transfer
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_I2C_SLAVE
|
|
|
|
, .setownaddress = stm32_i2c_setownaddress,
|
|
|
|
.registercallback = stm32_i2c_registercallback
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2011-03-27 17:03:49 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
************************************************************************************/
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: stm32_i2c_getreg
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get register value by offset
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
2011-09-09 21:30:03 +02:00
|
|
|
|
|
|
|
static inline uint16_t stm32_i2c_getreg(FAR struct stm32_i2c_priv_s *priv,
|
|
|
|
uint8_t offset)
|
2011-03-27 17:03:49 +02:00
|
|
|
{
|
2011-09-09 21:30:03 +02:00
|
|
|
return getreg16(priv->base + offset);
|
2011-03-27 17:03:49 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: stm32_i2c_putreg
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Put register value by offset
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
2011-09-09 21:30:03 +02:00
|
|
|
|
|
|
|
static inline void stm32_i2c_putreg(FAR struct stm32_i2c_priv_s *priv, uint8_t offset,
|
|
|
|
uint16_t value)
|
2011-03-27 17:03:49 +02:00
|
|
|
{
|
2011-09-09 21:30:03 +02:00
|
|
|
putreg16(value, priv->base + offset);
|
2011-03-27 17:03:49 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: stm32_i2c_modifyreg
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Modify register value by offset
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
2011-09-09 21:30:03 +02:00
|
|
|
|
|
|
|
static inline void stm32_i2c_modifyreg(FAR struct stm32_i2c_priv_s *priv,
|
|
|
|
uint8_t offset, uint16_t clearbits,
|
|
|
|
uint16_t setbits)
|
2011-03-27 17:03:49 +02:00
|
|
|
{
|
2011-09-09 21:30:03 +02:00
|
|
|
modifyreg16(priv->base + offset, clearbits, setbits);
|
2011-03-27 17:03:49 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name:
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
static inline void stm32_i2c_sem_wait(FAR struct i2c_dev_s *dev)
|
2011-03-27 17:03:49 +02:00
|
|
|
{
|
2011-09-09 21:30:03 +02:00
|
|
|
while (sem_wait(&((struct stm32_i2c_inst_s *)dev)->priv->sem_excl) != 0)
|
|
|
|
{
|
|
|
|
ASSERT(errno == EINTR);
|
2011-03-27 17:03:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
2011-09-18 19:52:00 +02:00
|
|
|
* Name: stm32_i2c_sem_waitdone
|
2011-09-17 17:04:24 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Wait for a transfer to complete
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_I2C_POLLED
|
2011-09-18 19:52:00 +02:00
|
|
|
static inline int stm32_i2c_sem_waitdone(FAR struct stm32_i2c_priv_s *priv)
|
2011-04-03 16:26:05 +02:00
|
|
|
{
|
2011-08-30 21:48:47 +02:00
|
|
|
struct timespec abstime;
|
|
|
|
irqstate_t flags;
|
2011-09-09 21:50:18 +02:00
|
|
|
uint32_t regval;
|
2011-08-30 21:48:47 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
flags = irqsave();
|
2011-09-09 21:50:18 +02:00
|
|
|
|
|
|
|
/* Enable I2C interrupts */
|
|
|
|
|
|
|
|
regval = stm32_i2c_getreg(priv, STM32_I2C_CR2_OFFSET);
|
|
|
|
regval |= (I2C_CR2_ITERREN | I2C_CR2_ITEVFEN);
|
|
|
|
stm32_i2c_putreg(priv, STM32_I2C_CR2_OFFSET, regval);
|
|
|
|
|
2011-09-10 01:13:17 +02:00
|
|
|
/* Signal the interrupt handler that we are waiting. NOTE: Interrupts
|
|
|
|
* are currently disabled but will be temporarily re-enabled below when
|
|
|
|
* sem_timedwait() sleeps.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->intstate = INTSTATE_WAITING;
|
|
|
|
do
|
2011-08-30 21:48:47 +02:00
|
|
|
{
|
2011-09-08 19:56:08 +02:00
|
|
|
/* Get the current time */
|
|
|
|
|
2011-09-06 23:00:48 +02:00
|
|
|
(void)clock_gettime(CLOCK_REALTIME, &abstime);
|
2011-09-08 19:56:08 +02:00
|
|
|
|
|
|
|
/* Calculate a time in the future */
|
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
#if CONFIG_STM32_I2CTIMEOSEC > 0
|
2011-09-08 19:56:08 +02:00
|
|
|
abstime.tv_sec += CONFIG_STM32_I2CTIMEOSEC;
|
|
|
|
#endif
|
2011-09-09 21:30:03 +02:00
|
|
|
#if CONFIG_STM32_I2CTIMEOMS > 0
|
2011-08-30 21:48:47 +02:00
|
|
|
abstime.tv_nsec += CONFIG_STM32_I2CTIMEOMS * 1000 * 1000;
|
|
|
|
if (abstime.tv_nsec > 1000 * 1000 * 1000)
|
|
|
|
{
|
|
|
|
abstime.tv_sec++;
|
|
|
|
abstime.tv_nsec -= 1000 * 1000 * 1000;
|
|
|
|
}
|
2011-09-08 19:56:08 +02:00
|
|
|
#endif
|
|
|
|
/* Wait until either the transfer is complete or the timeout expires */
|
2011-08-30 21:48:47 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
ret = sem_timedwait(&priv->sem_isr, &abstime);
|
2011-09-10 01:13:17 +02:00
|
|
|
if (ret != OK && errno != EINTR)
|
|
|
|
{
|
|
|
|
/* Break out of the loop on irrecoverable errors. This would
|
|
|
|
* include timeouts and mystery errors reported by sem_timedwait.
|
|
|
|
* NOTE that we try again if we are awakened by a signal (EINTR).
|
|
|
|
*/
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
2011-08-30 21:48:47 +02:00
|
|
|
|
2011-09-10 01:13:17 +02:00
|
|
|
/* Loop until the interrupt level transfer is complete. */
|
|
|
|
|
|
|
|
while (priv->intstate != INTSTATE_DONE);
|
|
|
|
|
|
|
|
/* Set the interrupt state back to IDLE */
|
|
|
|
|
|
|
|
priv->intstate = INTSTATE_IDLE;
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/* Disable I2C interrupts */
|
2011-09-09 21:50:18 +02:00
|
|
|
|
|
|
|
regval = stm32_i2c_getreg(priv, STM32_I2C_CR2_OFFSET);
|
|
|
|
regval &= ~I2C_CR2_ALLINTS;
|
|
|
|
stm32_i2c_putreg(priv, STM32_I2C_CR2_OFFSET, regval);
|
2011-09-10 01:13:17 +02:00
|
|
|
|
2011-08-30 21:48:47 +02:00
|
|
|
irqrestore(flags);
|
|
|
|
return ret;
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
2011-09-17 17:04:24 +02:00
|
|
|
#else
|
2011-09-18 19:52:00 +02:00
|
|
|
static inline int stm32_i2c_sem_waitdone(FAR struct stm32_i2c_priv_s *priv )
|
2011-09-17 17:04:24 +02:00
|
|
|
{
|
|
|
|
uint32_t start;
|
|
|
|
uint32_t elapsed;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Signal the interrupt handler that we are waiting. NOTE: Interrupts
|
|
|
|
* are currently disabled but will be temporarily re-enabled below when
|
|
|
|
* sem_timedwait() sleeps.
|
|
|
|
*/
|
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
priv->intstate = INTSTATE_WAITING;
|
|
|
|
start = clock_systimer();
|
|
|
|
do
|
2011-09-17 17:04:24 +02:00
|
|
|
{
|
|
|
|
/* Poll by simply calling the timer interrupt handler until it
|
|
|
|
* reports that it is done.
|
|
|
|
*/
|
|
|
|
|
|
|
|
stm32_i2c_isr(priv);
|
|
|
|
|
|
|
|
/* Calculate the elapsed time */
|
|
|
|
|
|
|
|
elapsed = clock_systimer() - start;
|
|
|
|
}
|
2011-04-03 16:26:05 +02:00
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
/* Loop until the transfer is complete. */
|
2011-09-17 17:04:24 +02:00
|
|
|
|
|
|
|
while (priv->intstate != INTSTATE_DONE && elapsed < CONFIG_STM32_I2CTIMEOTICKS);
|
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
i2cvdbg("intstate: %d elapsed: %d threshold: %d status: %08x\n",
|
|
|
|
priv->intstate, elapsed, CONFIG_STM32_I2CTIMEOTICKS, priv->status);
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/* Set the interrupt state back to IDLE */
|
|
|
|
|
|
|
|
ret = priv->intstate == INTSTATE_DONE ? OK : -ETIMEDOUT;
|
|
|
|
priv->intstate = INTSTATE_IDLE;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: stm32_i2c_sem_waitstop
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Wait for a STOP to complete
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
|
|
|
static inline void stm32_i2c_sem_waitstop(FAR struct stm32_i2c_priv_s *priv)
|
|
|
|
{
|
|
|
|
uint32_t start;
|
|
|
|
uint32_t elapsed;
|
|
|
|
uint32_t cr1;
|
|
|
|
uint32_t sr1;
|
|
|
|
|
|
|
|
/* Wait as stop might still be in progress; but stop might also
|
|
|
|
* be set because of a timeout error: "The [STOP] bit is set and
|
|
|
|
* cleared by software, cleared by hardware when a Stop condition is
|
|
|
|
* detected, set by hardware when a timeout error is detected."
|
|
|
|
*/
|
|
|
|
|
|
|
|
start = clock_systimer();
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Check for STOP condition */
|
|
|
|
|
|
|
|
cr1 = stm32_i2c_getreg(priv, STM32_I2C_CR1_OFFSET);
|
|
|
|
if ((cr1 & I2C_CR1_STOP) == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for timeout error */
|
|
|
|
|
|
|
|
sr1 = stm32_i2c_getreg(priv, STM32_I2C_SR1_OFFSET);
|
|
|
|
if ((sr1 & I2C_SR1_TIMEOUT) != 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate the elapsed time */
|
|
|
|
|
|
|
|
elapsed = clock_systimer() - start;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop until the stop is complete or a timeout occurs. */
|
|
|
|
|
|
|
|
while (elapsed < CONFIG_STM32_I2CTIMEOTICKS);
|
|
|
|
|
|
|
|
/* If we get here then a timeout occurred with the STOP condition
|
|
|
|
* still pending.
|
|
|
|
*/
|
|
|
|
|
|
|
|
i2cvdbg("Timeout with CR1: %04x SR1: %04x\n", cr1, sr1);
|
|
|
|
}
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: stm32_i2c_sem_post
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Release the mutual exclusion semaphore
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
static inline void stm32_i2c_sem_post(FAR struct i2c_dev_s *dev)
|
2011-03-27 17:03:49 +02:00
|
|
|
{
|
2011-09-09 21:30:03 +02:00
|
|
|
sem_post( &((struct stm32_i2c_inst_s *)dev)->priv->sem_excl );
|
2011-03-27 17:03:49 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: stm32_i2c_sem_init
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize semaphores
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
static inline void stm32_i2c_sem_init(FAR struct i2c_dev_s *dev)
|
2011-03-27 17:03:49 +02:00
|
|
|
{
|
2011-09-17 17:04:24 +02:00
|
|
|
sem_init(&((struct stm32_i2c_inst_s *)dev)->priv->sem_excl, 0, 1);
|
|
|
|
#ifndef CONFIG_I2C_POLLED
|
|
|
|
sem_init(&((struct stm32_i2c_inst_s *)dev)->priv->sem_isr, 0, 0);
|
|
|
|
#endif
|
2011-03-27 17:03:49 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: stm32_i2c_sem_destroy
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Destroy semaphores.
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
static inline void stm32_i2c_sem_destroy(FAR struct i2c_dev_s *dev)
|
2011-03-27 17:03:49 +02:00
|
|
|
{
|
2011-09-17 17:04:24 +02:00
|
|
|
sem_destroy(&((struct stm32_i2c_inst_s *)dev)->priv->sem_excl);
|
|
|
|
#ifndef CONFIG_I2C_POLLED
|
|
|
|
sem_destroy(&((struct stm32_i2c_inst_s *)dev)->priv->sem_isr);
|
|
|
|
#endif
|
2011-03-27 17:03:49 +02:00
|
|
|
}
|
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: stm32_i2c_trace*
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* I2C trace instrumentation
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_I2C_TRACE
|
|
|
|
static void stm32_i2c_tracereset(FAR struct stm32_i2c_priv_s *priv)
|
|
|
|
{
|
|
|
|
/* Reset the trace info for a new data collection */
|
|
|
|
|
|
|
|
priv->isr_count = 0;
|
|
|
|
priv->old_status = 0xffffffff;
|
|
|
|
priv->tndx = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stm32_i2c_tracenew(FAR struct stm32_i2c_priv_s *priv, uint32_t status)
|
|
|
|
{
|
|
|
|
/* Increment the cout of interrupts received */
|
|
|
|
|
|
|
|
priv->isr_count++;
|
|
|
|
|
|
|
|
/* Has the status changed from the last interrupt */
|
|
|
|
|
|
|
|
if (status != priv->old_status)
|
|
|
|
{
|
|
|
|
/* Yes.. bump up the trace index (unless we are out of trace entries) */
|
|
|
|
|
|
|
|
if (priv->tndx < CONFIG_I2C_NTRACE)
|
|
|
|
{
|
|
|
|
priv->tndx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the new trace entry */
|
|
|
|
|
|
|
|
priv->trace[priv->tndx].status = status;
|
|
|
|
priv->trace[priv->tndx].count = priv->isr_count;
|
|
|
|
priv->trace[priv->tndx].event = I2CEVENT_NONE;
|
|
|
|
priv->trace[priv->tndx].parm = 0;
|
|
|
|
priv->old_status = status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stm32_i3c_traceevent(FAR struct stm32_i2c_priv_s *priv,
|
|
|
|
enum stm32_trace_e event, uint32_t parm)
|
|
|
|
{
|
|
|
|
/* Add the event to the trace entry (possibly overwriting a previous trace
|
|
|
|
* event.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->trace[priv->tndx].event = event;
|
|
|
|
priv->trace[priv->tndx].parm = parm;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stm32_i2c_tracedump(FAR struct stm32_i2c_priv_s *priv)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Dump all of the buffered trace entries */
|
|
|
|
|
|
|
|
for (i = 0; i < priv->tndx; i++)
|
|
|
|
{
|
|
|
|
lib_rawprintf("%2d. STATUS: %08x COUNT: %3d EVENT: %2d PARM: %08x\n", i,
|
|
|
|
priv->trace[i].status, priv->trace[i].count,
|
|
|
|
priv->trace[i].event, priv->trace[i].parm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_I2C_TRACE */
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: stm32_i2c_setclock
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set the I2C clock
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
2011-04-03 16:26:05 +02:00
|
|
|
static void stm32_i2c_setclock(FAR struct stm32_i2c_priv_s *priv, uint32_t frequency)
|
2011-03-27 17:03:49 +02:00
|
|
|
{
|
2011-09-08 19:56:08 +02:00
|
|
|
uint16_t cr1;
|
|
|
|
uint16_t ccr;
|
|
|
|
uint16_t trise;
|
|
|
|
uint16_t freqmhz;
|
|
|
|
uint16_t speed;
|
2011-03-27 17:03:49 +02:00
|
|
|
|
2011-09-08 19:56:08 +02:00
|
|
|
/* Disable the selected I2C peripheral to configure TRISE */
|
|
|
|
|
|
|
|
cr1 = stm32_i2c_getreg(priv, STM32_I2C_CR1_OFFSET);
|
|
|
|
stm32_i2c_putreg(priv, STM32_I2C_CR1_OFFSET, cr1 & ~I2C_CR1_PE);
|
|
|
|
|
|
|
|
/* Update timing and control registers */
|
|
|
|
|
|
|
|
freqmhz = (uint16_t)(STM32_PCLK1_FREQUENCY / 1000000);
|
|
|
|
ccr = 0;
|
|
|
|
|
|
|
|
/* Configure speed in standard mode */
|
|
|
|
|
|
|
|
if (frequency <= 100000)
|
|
|
|
{
|
|
|
|
/* Standard mode speed calculation */
|
|
|
|
|
|
|
|
speed = (uint16_t)(STM32_PCLK1_FREQUENCY / (frequency << 1));
|
|
|
|
|
|
|
|
/* The CCR fault must be >= 4 */
|
|
|
|
|
|
|
|
if (speed < 4)
|
|
|
|
{
|
|
|
|
/* Set the minimum allowed value */
|
|
|
|
|
|
|
|
speed = 4;
|
|
|
|
}
|
|
|
|
ccr |= speed;
|
|
|
|
|
|
|
|
/* Set Maximum Rise Time for standard mode */
|
|
|
|
|
|
|
|
trise = freqmhz + 1;
|
2011-03-27 17:03:49 +02:00
|
|
|
}
|
2011-09-08 19:56:08 +02:00
|
|
|
|
|
|
|
/* Configure speed in fast mode */
|
|
|
|
|
|
|
|
else /* (frequency <= 400000) */
|
|
|
|
{
|
|
|
|
/* Fast mode speed calculation with Tlow/Thigh = 16/9 */
|
|
|
|
|
|
|
|
#ifdef CONFIG_I2C_DUTY16_9
|
|
|
|
speed = (uint16_t)(STM32_PCLK1_FREQUENCY / (frequency * 25));
|
|
|
|
|
|
|
|
/* Set DUTY and fast speed bits */
|
|
|
|
|
|
|
|
ccr |= (I2C_CCR_DUTY|I2C_CCR_FS);
|
|
|
|
#else
|
|
|
|
/* Fast mode speed calculation with Tlow/Thigh = 2 */
|
|
|
|
|
|
|
|
speed = (uint16_t)(STM32_PCLK1_FREQUENCY / (frequency * 3));
|
|
|
|
|
|
|
|
/* Set fast speed bit */
|
|
|
|
|
|
|
|
ccr |= I2C_CCR_FS;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Verify that the CCR speed value is nonzero */
|
|
|
|
|
|
|
|
if (speed < 1)
|
|
|
|
{
|
|
|
|
/* Set the minimum allowed value */
|
|
|
|
|
|
|
|
speed = 1;
|
|
|
|
}
|
|
|
|
ccr |= speed;
|
|
|
|
|
|
|
|
/* Set Maximum Rise Time for fast mode */
|
|
|
|
|
|
|
|
trise = (uint16_t)(((freqmhz * 300) / 1000) + 1);
|
2011-03-27 17:03:49 +02:00
|
|
|
}
|
2011-09-08 19:56:08 +02:00
|
|
|
|
|
|
|
/* Write the new values of the CCR and TRISE registers */
|
|
|
|
|
|
|
|
stm32_i2c_putreg(priv, STM32_I2C_CCR_OFFSET, ccr);
|
|
|
|
stm32_i2c_putreg(priv, STM32_I2C_TRISE_OFFSET, trise);
|
|
|
|
|
|
|
|
/* Bit 14 of OAR1 must be configured and kept at 1 */
|
|
|
|
|
|
|
|
stm32_i2c_putreg(priv, STM32_I2C_OAR1_OFFSET, I2C_OAR1_ONE);
|
|
|
|
|
|
|
|
/* Re-enable the peripheral (or not) */
|
|
|
|
|
|
|
|
stm32_i2c_putreg(priv, STM32_I2C_CR1_OFFSET, cr1);
|
2011-03-27 17:03:49 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: stm32_i2c_sendstart
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Send the START conditions/force Master mode
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
2011-04-03 16:26:05 +02:00
|
|
|
static inline void stm32_i2c_sendstart(FAR struct stm32_i2c_priv_s *priv)
|
2011-03-28 17:01:43 +02:00
|
|
|
{
|
2011-09-08 19:56:08 +02:00
|
|
|
/* Disable ACK on receive by default and generate START */
|
|
|
|
|
|
|
|
stm32_i2c_modifyreg(priv, STM32_I2C_CR1_OFFSET, I2C_CR1_ACK, I2C_CR1_START);
|
|
|
|
}
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: stm32_i2c_clrstart
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Clear the STOP, START or PEC condition on certain error recovery steps.
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
2011-09-08 19:56:08 +02:00
|
|
|
static inline void stm32_i2c_clrstart(FAR struct stm32_i2c_priv_s *priv)
|
|
|
|
{
|
2011-09-11 16:55:31 +02:00
|
|
|
/* "Note: When the STOP, START or PEC bit is set, the software must
|
|
|
|
* not perform any write access to I2C_CR1 before this bit is
|
|
|
|
* cleared by hardware. Otherwise there is a risk of setting a
|
|
|
|
* second STOP, START or PEC request."
|
|
|
|
*
|
|
|
|
* "The [STOP] bit is set and cleared by software, cleared by hardware
|
|
|
|
* when a Stop condition is detected, set by hardware when a timeout
|
|
|
|
* error is detected.
|
|
|
|
*
|
|
|
|
* "This [START] bit is set and cleared by software and cleared by hardware
|
2011-09-08 19:56:08 +02:00
|
|
|
* when start is sent or PE=0." The bit must be cleared by software if the
|
|
|
|
* START is never sent.
|
2011-09-11 16:55:31 +02:00
|
|
|
*
|
|
|
|
* "This [PEC] bit is set and cleared by software, and cleared by hardware
|
|
|
|
* when PEC is transferred or by a START or Stop condition or when PE=0."
|
2011-09-08 19:56:08 +02:00
|
|
|
*/
|
|
|
|
|
2011-09-11 16:55:31 +02:00
|
|
|
stm32_i2c_modifyreg(priv, STM32_I2C_CR1_OFFSET,
|
|
|
|
I2C_CR1_START|I2C_CR1_STOP|I2C_CR1_PEC, 0);
|
2011-03-28 17:01:43 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: stm32_i2c_sendstop
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Send the STOP conditions
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
2011-04-03 16:26:05 +02:00
|
|
|
static inline void stm32_i2c_sendstop(FAR struct stm32_i2c_priv_s *priv)
|
2011-03-28 17:01:43 +02:00
|
|
|
{
|
2011-09-08 19:56:08 +02:00
|
|
|
stm32_i2c_modifyreg(priv, STM32_I2C_CR1_OFFSET, I2C_CR1_ACK, I2C_CR1_STOP);
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: stm32_i2c_getstatus
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get 32-bit status (SR1 and SR2 combined)
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
2011-04-03 16:26:05 +02:00
|
|
|
static inline uint32_t stm32_i2c_getstatus(FAR struct stm32_i2c_priv_s *priv)
|
|
|
|
{
|
2011-09-08 19:56:08 +02:00
|
|
|
uint32_t status = stm32_i2c_getreg(priv, STM32_I2C_SR1_OFFSET);
|
|
|
|
status |= (stm32_i2c_getreg(priv, STM32_I2C_SR2_OFFSET) << 16);
|
|
|
|
return status;
|
2011-03-28 17:01:43 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: stm32_i2c_disablefsmc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* FSMC must be disable while accessing I2C1 because it uses a common resource
|
|
|
|
* (LBAR)
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
2011-09-08 19:56:08 +02:00
|
|
|
|
|
|
|
#if defined(CONFIG_STM32_FSMC) && defined (CONFIG_STM32_I2C1)
|
|
|
|
static inline uint32_t stm32_i2c_disablefsmc(FAR struct stm32_i2c_priv_s *priv)
|
|
|
|
{
|
|
|
|
uint32_t ret = 0;
|
|
|
|
uint32_t regval;
|
|
|
|
|
|
|
|
/* Is this I2C1 */
|
|
|
|
|
|
|
|
#ifdef CONFIG_STM32_I2C2
|
|
|
|
if (priv->base == STM32_I2C1_BASE)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Disable FSMC unconditionally */
|
|
|
|
|
|
|
|
ret = getreg32( STM32_RCC_AHBENR);
|
|
|
|
regval = ret & ~RCC_AHBENR_FSMCEN;
|
|
|
|
putreg32(regval, STM32_RCC_AHBENR);
|
|
|
|
}
|
2011-09-09 02:52:24 +02:00
|
|
|
return ret;
|
2011-09-08 19:56:08 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: stm32_i2c_enablefsmc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Re-enabled the FSMC
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
2011-09-08 19:56:08 +02:00
|
|
|
static inline void stm32_i2c_enablefsmc(uint32_t ahbenr)
|
|
|
|
{
|
|
|
|
uint32_t regval;
|
|
|
|
|
|
|
|
/* Enable AHB clocking to the FSMC only if it was previously enabled. */
|
|
|
|
|
|
|
|
if ((ahbenr & RCC_AHBENR_FSMCEN) != 0)
|
|
|
|
{
|
|
|
|
regval = getreg32( STM32_RCC_AHBENR);
|
|
|
|
regval |= RCC_AHBENR_FSMCEN;
|
|
|
|
putreg32(regval, STM32_RCC_AHBENR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2011-10-02 00:09:00 +02:00
|
|
|
# define stm32_i2c_disablefsmc(priv) (0)
|
2011-09-08 19:56:08 +02:00
|
|
|
# define stm32_i2c_enablefsmc(ahbenr)
|
|
|
|
#endif
|
|
|
|
|
2011-03-28 17:01:43 +02:00
|
|
|
/************************************************************************************
|
2011-09-17 17:04:24 +02:00
|
|
|
* Name: stm32_i2c_isr
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Common Interrupt Service Routine
|
|
|
|
*
|
2011-03-28 17:01:43 +02:00
|
|
|
************************************************************************************/
|
2011-04-05 18:25:04 +02:00
|
|
|
|
2011-04-03 16:26:05 +02:00
|
|
|
static int stm32_i2c_isr(struct stm32_i2c_priv_s * priv)
|
2011-03-28 17:01:43 +02:00
|
|
|
{
|
2011-09-09 21:30:03 +02:00
|
|
|
uint32_t status = stm32_i2c_getstatus(priv);
|
2011-08-30 21:48:47 +02:00
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
/* Check for new trace setup */
|
2011-04-05 18:25:04 +02:00
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
stm32_i2c_tracenew(priv, status);
|
2011-04-03 16:26:05 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
/* Was start bit sent */
|
2011-04-03 16:26:05 +02:00
|
|
|
|
2011-09-10 01:13:17 +02:00
|
|
|
if ((status & I2C_SR1_SB) != 0)
|
2011-09-09 21:30:03 +02:00
|
|
|
{
|
2011-09-18 19:52:00 +02:00
|
|
|
stm32_i3c_traceevent(priv, I2CEVENT_SB, priv->msgc);
|
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
/* Get run-time data */
|
2011-08-30 21:48:47 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
priv->ptr = priv->msgv->buffer;
|
|
|
|
priv->dcnt = priv->msgv->length;
|
|
|
|
priv->flags = priv->msgv->flags;
|
2011-04-03 16:26:05 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
/* Send address byte and define addressing mode */
|
|
|
|
|
|
|
|
stm32_i2c_putreg(priv, STM32_I2C_DR_OFFSET,
|
|
|
|
(priv->flags & I2C_M_TEN) ?
|
2011-09-10 01:13:17 +02:00
|
|
|
0 : ((priv->msgv->addr << 1) | (priv->flags & I2C_M_READ)));
|
2011-09-09 21:30:03 +02:00
|
|
|
|
|
|
|
/* Set ACK for receive mode */
|
|
|
|
|
2011-09-10 01:13:17 +02:00
|
|
|
if (priv->dcnt > 1 && (priv->flags & I2C_M_READ) != 0)
|
2011-09-09 21:30:03 +02:00
|
|
|
{
|
|
|
|
stm32_i2c_modifyreg(priv, STM32_I2C_CR1_OFFSET, 0, I2C_CR1_ACK);
|
2011-04-05 18:25:04 +02:00
|
|
|
}
|
2011-08-30 21:48:47 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
/* Increment to next pointer and decrement message count */
|
|
|
|
|
|
|
|
priv->msgv++;
|
|
|
|
priv->msgc--;
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
2011-09-09 21:30:03 +02:00
|
|
|
|
|
|
|
/* In 10-bit addressing mode, was first byte sent */
|
2011-04-03 16:26:05 +02:00
|
|
|
|
2011-09-10 01:13:17 +02:00
|
|
|
else if ((status & I2C_SR1_ADD10) != 0)
|
2011-09-09 21:30:03 +02:00
|
|
|
{
|
|
|
|
/* \todo Finish 10-bit mode addressing */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Was address sent, continue with ether sending or reading data */
|
2011-04-03 16:26:05 +02:00
|
|
|
|
2011-09-10 01:13:17 +02:00
|
|
|
else if ((priv->flags & I2C_M_READ) == 0 && (status & (I2C_SR1_ADDR | I2C_SR1_TXE)) != 0)
|
2011-09-09 21:30:03 +02:00
|
|
|
{
|
2011-09-18 19:52:00 +02:00
|
|
|
stm32_i3c_traceevent(priv, I2CEVENT_READ, priv->dcnt);
|
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
if (--priv->dcnt >= 0)
|
|
|
|
{
|
|
|
|
/* Send a byte */
|
2011-09-09 21:50:18 +02:00
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
stm32_i3c_traceevent(priv, I2CEVENT_SENDBYTE, *priv->ptr);
|
2011-09-09 21:30:03 +02:00
|
|
|
stm32_i2c_putreg(priv, STM32_I2C_DR_OFFSET, *priv->ptr++);
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-10 01:13:17 +02:00
|
|
|
else if ((priv->flags & I2C_M_READ) != 0 && (status & I2C_SR1_ADDR) != 0)
|
2011-09-09 21:30:03 +02:00
|
|
|
{
|
|
|
|
/* Enable RxNE and TxE buffers in order to receive one or multiple bytes */
|
2011-04-03 16:26:05 +02:00
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
#ifndef CONFIG_I2C_POLLED
|
2011-09-18 19:52:00 +02:00
|
|
|
stm32_i3c_traceevent(priv, I2CEVENT_ITBUFEN, 0);
|
2011-09-09 21:30:03 +02:00
|
|
|
stm32_i2c_modifyreg(priv, STM32_I2C_CR2_OFFSET, 0, I2C_CR2_ITBUFEN);
|
2011-09-17 17:04:24 +02:00
|
|
|
#endif
|
2011-09-09 21:30:03 +02:00
|
|
|
}
|
2011-04-03 16:26:05 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
/* More bytes to read */
|
2011-08-30 21:48:47 +02:00
|
|
|
|
2011-09-10 01:13:17 +02:00
|
|
|
else if ((status & I2C_SR1_RXNE) != 0)
|
2011-09-09 21:30:03 +02:00
|
|
|
{
|
|
|
|
/* Read a byte, if dcnt goes < 0, then read dummy bytes to ack ISRs */
|
2011-04-03 16:26:05 +02:00
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
stm32_i3c_traceevent(priv, I2CEVENT_RXNE, priv->dcnt);
|
2011-09-09 21:30:03 +02:00
|
|
|
if (--priv->dcnt >= 0)
|
|
|
|
{
|
|
|
|
*priv->ptr++ = stm32_i2c_getreg(priv, STM32_I2C_DR_OFFSET);
|
2011-09-09 21:50:18 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
/* Disable acknowledge when last byte is to be received */
|
|
|
|
|
|
|
|
if (priv->dcnt == 1)
|
|
|
|
{
|
|
|
|
stm32_i2c_modifyreg(priv, STM32_I2C_CR1_OFFSET, I2C_CR1_ACK, 0);
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
/* Do we have more bytes to send, enable/disable buffer interrupts
|
|
|
|
* (these ISRs could be replaced by DMAs)
|
|
|
|
*/
|
2011-09-17 17:04:24 +02:00
|
|
|
|
|
|
|
#ifndef CONFIG_I2C_POLLED
|
2011-09-10 01:13:17 +02:00
|
|
|
if (priv->dcnt > 0)
|
2011-09-09 21:30:03 +02:00
|
|
|
{
|
2011-09-18 19:52:00 +02:00
|
|
|
stm32_i3c_traceevent(priv, I2CEVENT_REITBUFEN, 0);
|
2011-09-09 21:30:03 +02:00
|
|
|
stm32_i2c_modifyreg(priv, STM32_I2C_CR2_OFFSET, 0, I2C_CR2_ITBUFEN);
|
2011-04-05 18:25:04 +02:00
|
|
|
}
|
2011-09-10 01:13:17 +02:00
|
|
|
else if (priv->dcnt == 0)
|
2011-09-09 21:30:03 +02:00
|
|
|
{
|
2011-09-18 19:52:00 +02:00
|
|
|
stm32_i3c_traceevent(priv, I2CEVENT_DISITBUFEN, 0);
|
2011-09-09 21:30:03 +02:00
|
|
|
stm32_i2c_modifyreg(priv, STM32_I2C_CR2_OFFSET, I2C_CR2_ITBUFEN, 0);
|
2011-04-05 18:25:04 +02:00
|
|
|
}
|
2011-09-17 17:04:24 +02:00
|
|
|
#endif
|
2011-04-03 16:26:05 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
/* Was last byte received or sent? */
|
2011-08-30 21:48:47 +02:00
|
|
|
|
2011-09-10 01:13:17 +02:00
|
|
|
if (priv->dcnt <= 0 && (status & I2C_SR1_BTF) != 0)
|
2011-09-09 21:30:03 +02:00
|
|
|
{
|
|
|
|
stm32_i2c_getreg(priv, STM32_I2C_DR_OFFSET); /* ACK ISR */
|
2011-04-03 16:26:05 +02:00
|
|
|
|
2011-09-10 01:13:17 +02:00
|
|
|
/* Do we need to terminate or restart after this byte?
|
|
|
|
* If there are more messages to send, then we may:
|
|
|
|
*
|
2011-09-09 21:30:03 +02:00
|
|
|
* - continue with repeated start
|
|
|
|
* - or just continue sending writeable part
|
|
|
|
* - or we close down by sending the stop bit
|
|
|
|
*/
|
|
|
|
|
2011-09-10 01:13:17 +02:00
|
|
|
if (priv->msgc > 0)
|
2011-09-09 21:30:03 +02:00
|
|
|
{
|
2011-09-18 19:52:00 +02:00
|
|
|
stm32_i3c_traceevent(priv, I2CEVENT_BTFSTART, priv->msgc);
|
2011-09-09 21:30:03 +02:00
|
|
|
if (priv->msgv->flags & I2C_M_NORESTART)
|
|
|
|
{
|
|
|
|
priv->ptr = priv->msgv->buffer;
|
|
|
|
priv->dcnt = priv->msgv->length;
|
|
|
|
priv->flags = priv->msgv->flags;
|
|
|
|
priv->msgv++;
|
|
|
|
priv->msgc--;
|
|
|
|
|
|
|
|
/* Restart this ISR! */
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
#ifndef CONFIG_I2C_POLLED
|
2011-09-09 21:30:03 +02:00
|
|
|
stm32_i2c_modifyreg(priv, STM32_I2C_CR2_OFFSET, 0, I2C_CR2_ITBUFEN);
|
2011-09-17 17:04:24 +02:00
|
|
|
#endif
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
2011-09-09 21:30:03 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
stm32_i2c_sendstart(priv);
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
|
|
|
}
|
2011-09-09 21:30:03 +02:00
|
|
|
else if (priv->msgv)
|
|
|
|
{
|
2011-09-18 19:52:00 +02:00
|
|
|
stm32_i3c_traceevent(priv, I2CEVENT_BTFSTOP, 0);
|
2011-09-09 21:30:03 +02:00
|
|
|
stm32_i2c_sendstop(priv);
|
2011-09-10 01:13:17 +02:00
|
|
|
|
|
|
|
/* Is there a thread waiting for this event (there should be) */
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
#ifndef CONFIG_I2C_POLLED
|
2011-09-10 01:13:17 +02:00
|
|
|
if (priv->intstate == INTSTATE_WAITING)
|
2011-09-09 21:30:03 +02:00
|
|
|
{
|
2011-09-10 01:13:17 +02:00
|
|
|
/* Yes.. inform the thread that the transfer is complete
|
|
|
|
* and wake it up.
|
|
|
|
*/
|
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
sem_post( &priv->sem_isr );
|
2011-09-10 01:13:17 +02:00
|
|
|
priv->intstate = INTSTATE_DONE;
|
2011-09-09 21:30:03 +02:00
|
|
|
}
|
2011-09-17 17:04:24 +02:00
|
|
|
#else
|
|
|
|
priv->intstate = INTSTATE_DONE;
|
|
|
|
#endif
|
2011-09-09 21:30:03 +02:00
|
|
|
|
|
|
|
/* Mark that we have stopped with this transaction */
|
|
|
|
|
|
|
|
priv->msgv = NULL;
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for errors, in which case, stop the transfer and return
|
|
|
|
* Note that in master reception mode AF becomes set on last byte
|
|
|
|
* since ACK is not returned. We should ignore this error.
|
|
|
|
*/
|
|
|
|
|
2011-09-10 01:13:17 +02:00
|
|
|
if ((status & I2C_SR1_ERRORMASK) != 0)
|
|
|
|
{
|
2011-09-18 19:52:00 +02:00
|
|
|
stm32_i3c_traceevent(priv, I2CEVENT_ERROR, 0);
|
|
|
|
|
2011-09-10 01:13:17 +02:00
|
|
|
/* Clear interrupt flags */
|
|
|
|
|
|
|
|
stm32_i2c_putreg(priv, STM32_I2C_SR1_OFFSET, 0);
|
|
|
|
|
|
|
|
/* Is there a thread waiting for this event (there should be) */
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
#ifndef CONFIG_I2C_POLLED
|
2011-09-10 01:13:17 +02:00
|
|
|
if (priv->intstate == INTSTATE_WAITING)
|
2011-09-09 21:30:03 +02:00
|
|
|
{
|
2011-09-10 01:13:17 +02:00
|
|
|
/* Yes.. inform the thread that the transfer is complete
|
|
|
|
* and wake it up.
|
|
|
|
*/
|
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
sem_post( &priv->sem_isr );
|
2011-09-10 01:13:17 +02:00
|
|
|
priv->intstate = INTSTATE_DONE;
|
2011-09-09 21:30:03 +02:00
|
|
|
}
|
2011-09-17 17:04:24 +02:00
|
|
|
#else
|
|
|
|
priv->intstate = INTSTATE_DONE;
|
|
|
|
#endif
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
priv->status = status;
|
2011-03-28 17:01:43 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: stm32_i2c1_isr
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* I2C1 interrupt service routine
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
2011-03-28 17:01:43 +02:00
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
#ifndef CONFIG_I2C_POLLED
|
2011-09-08 19:56:08 +02:00
|
|
|
#ifdef CONFIG_STM32_I2C1
|
2011-04-03 16:26:05 +02:00
|
|
|
static int stm32_i2c1_isr(int irq, void *context)
|
2011-03-28 17:01:43 +02:00
|
|
|
{
|
2011-09-09 21:30:03 +02:00
|
|
|
return stm32_i2c_isr(&stm32_i2c1_priv);
|
2011-03-28 17:01:43 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: stm32_i2c2_isr
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* I2C2 interrupt service routine
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
2011-09-08 19:56:08 +02:00
|
|
|
#ifdef CONFIG_STM32_I2C2
|
2011-04-03 16:26:05 +02:00
|
|
|
static int stm32_i2c2_isr(int irq, void *context)
|
2011-03-28 17:01:43 +02:00
|
|
|
{
|
2011-09-09 21:30:03 +02:00
|
|
|
return stm32_i2c_isr(&stm32_i2c2_priv);
|
2011-03-28 17:01:43 +02:00
|
|
|
}
|
|
|
|
#endif
|
2011-09-17 17:04:24 +02:00
|
|
|
#endif
|
2011-03-28 17:01:43 +02:00
|
|
|
|
|
|
|
/************************************************************************************
|
|
|
|
* Private Initialization and Deinitialization
|
|
|
|
************************************************************************************/
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: stm32_i2c_init
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Setup the I2C hardware, ready for operation with defaults
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
2011-04-03 16:26:05 +02:00
|
|
|
static int stm32_i2c_init(FAR struct stm32_i2c_priv_s *priv)
|
2011-03-27 17:03:49 +02:00
|
|
|
{
|
2011-09-09 21:30:03 +02:00
|
|
|
/* Power-up and configure GPIOs */
|
2011-03-27 17:03:49 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
switch (priv->base)
|
|
|
|
{
|
2011-09-08 19:56:08 +02:00
|
|
|
#ifdef CONFIG_STM32_I2C1
|
2011-09-09 21:30:03 +02:00
|
|
|
case STM32_I2C1_BASE:
|
|
|
|
|
|
|
|
/* Enable power and reset the peripheral */
|
2011-09-08 19:56:08 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
modifyreg32(STM32_RCC_APB1ENR, 0, RCC_APB1ENR_I2C1EN);
|
2011-08-27 16:58:42 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
modifyreg32(STM32_RCC_APB1RSTR, 0, RCC_APB1RSTR_I2C1RST);
|
|
|
|
modifyreg32(STM32_RCC_APB1RSTR, RCC_APB1RSTR_I2C1RST, 0);
|
2011-09-08 19:56:08 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
/* Configure pins */
|
2011-03-27 17:03:49 +02:00
|
|
|
|
2011-10-05 03:22:49 +02:00
|
|
|
if (stm32_configgpio(GPIO_I2C1_SCL) < 0)
|
2011-09-09 21:30:03 +02:00
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2011-10-05 03:22:49 +02:00
|
|
|
if (stm32_configgpio(GPIO_I2C1_SDA) < 0)
|
2011-09-09 21:30:03 +02:00
|
|
|
{
|
|
|
|
stm32_unconfiggpio(GPIO_I2C1_SCL);
|
|
|
|
return ERROR;
|
|
|
|
}
|
2011-09-08 19:56:08 +02:00
|
|
|
|
2011-09-09 21:50:18 +02:00
|
|
|
/* Attach ISRs */
|
2011-08-27 16:58:42 +02:00
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
#ifndef CONFIG_I2C_POLLED
|
2011-09-09 21:30:03 +02:00
|
|
|
irq_attach(STM32_IRQ_I2C1EV, stm32_i2c1_isr);
|
|
|
|
irq_attach(STM32_IRQ_I2C1ER, stm32_i2c1_isr);
|
|
|
|
up_enable_irq(STM32_IRQ_I2C1EV);
|
|
|
|
up_enable_irq(STM32_IRQ_I2C1ER);
|
2011-09-17 17:04:24 +02:00
|
|
|
#endif
|
2011-09-09 21:30:03 +02:00
|
|
|
break;
|
2011-03-28 17:01:43 +02:00
|
|
|
#endif
|
|
|
|
|
2011-09-08 19:56:08 +02:00
|
|
|
#ifdef CONFIG_STM32_I2C2
|
2011-09-09 21:30:03 +02:00
|
|
|
case STM32_I2C2_BASE:
|
2011-08-27 16:58:42 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
/* Enable power and reset the peripheral */
|
2011-08-27 16:58:42 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
modifyreg32(STM32_RCC_APB1ENR, 0, RCC_APB1ENR_I2C2EN);
|
2011-08-27 16:58:42 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
modifyreg32(STM32_RCC_APB1RSTR, 0, RCC_APB1RSTR_I2C2RST);
|
|
|
|
modifyreg32(STM32_RCC_APB1RSTR, RCC_APB1RSTR_I2C2RST, 0);
|
2011-08-27 16:58:42 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
/* Configure pins */
|
|
|
|
|
2011-10-05 03:22:49 +02:00
|
|
|
if (stm32_configgpio(GPIO_I2C2_SCL) < 0)
|
2011-09-09 21:30:03 +02:00
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2011-10-05 03:22:49 +02:00
|
|
|
if (stm32_configgpio(GPIO_I2C2_SDA) < 0)
|
2011-09-09 21:30:03 +02:00
|
|
|
{
|
|
|
|
stm32_unconfiggpio(GPIO_I2C2_SCL);
|
|
|
|
return ERROR;
|
|
|
|
}
|
2011-08-27 16:58:42 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
/* Attach ISRs */
|
2011-08-27 16:58:42 +02:00
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
#ifndef CONFIG_I2C_POLLED
|
2011-09-09 21:30:03 +02:00
|
|
|
irq_attach(STM32_IRQ_I2C2EV, stm32_i2c2_isr);
|
|
|
|
irq_attach(STM32_IRQ_I2C2ER, stm32_i2c2_isr);
|
|
|
|
up_enable_irq(STM32_IRQ_I2C2EV);
|
|
|
|
up_enable_irq(STM32_IRQ_I2C2ER);
|
2011-09-17 17:04:24 +02:00
|
|
|
#endif
|
2011-09-09 21:30:03 +02:00
|
|
|
break;
|
2011-03-28 17:01:43 +02:00
|
|
|
#endif
|
2011-09-09 21:30:03 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
return ERROR;
|
2011-03-27 17:03:49 +02:00
|
|
|
}
|
|
|
|
|
2011-09-09 21:50:18 +02:00
|
|
|
/* Set peripheral frequency, where it must be at least 2 MHz for 100 kHz
|
|
|
|
* or 4 MHz for 400 kHz. This also disables all I2C interrupts.
|
2011-09-09 21:30:03 +02:00
|
|
|
*/
|
2011-03-28 17:01:43 +02:00
|
|
|
|
2011-09-09 21:50:18 +02:00
|
|
|
stm32_i2c_putreg(priv, STM32_I2C_CR2_OFFSET, (STM32_PCLK1_FREQUENCY / 1000000));
|
2011-09-09 21:30:03 +02:00
|
|
|
stm32_i2c_setclock(priv, 100000);
|
2011-03-28 17:01:43 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
/* Enable I2C */
|
2011-03-27 17:03:49 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
stm32_i2c_putreg(priv, STM32_I2C_CR1_OFFSET, I2C_CR1_PE);
|
|
|
|
return OK;
|
2011-03-27 17:03:49 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: stm32_i2c_deinit
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Shutdown the I2C hardware
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
2011-04-03 16:26:05 +02:00
|
|
|
static int stm32_i2c_deinit(FAR struct stm32_i2c_priv_s *priv)
|
2011-03-27 17:03:49 +02:00
|
|
|
{
|
2011-09-09 21:30:03 +02:00
|
|
|
/* Disable I2C */
|
2011-03-27 17:03:49 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
stm32_i2c_putreg(priv, STM32_I2C_CR1_OFFSET, 0);
|
2011-03-27 17:03:49 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
switch (priv->base)
|
|
|
|
{
|
2011-09-08 19:56:08 +02:00
|
|
|
#ifdef CONFIG_STM32_I2C1
|
2011-09-09 21:30:03 +02:00
|
|
|
case STM32_I2C1_BASE:
|
|
|
|
stm32_unconfiggpio(GPIO_I2C1_SCL);
|
|
|
|
stm32_unconfiggpio(GPIO_I2C1_SDA);
|
2011-09-17 17:04:24 +02:00
|
|
|
|
|
|
|
#ifndef CONFIG_I2C_POLLED
|
2011-09-09 21:30:03 +02:00
|
|
|
up_disable_irq(STM32_IRQ_I2C1EV);
|
|
|
|
up_disable_irq(STM32_IRQ_I2C1ER);
|
|
|
|
irq_detach(STM32_IRQ_I2C1EV);
|
|
|
|
irq_detach(STM32_IRQ_I2C1ER);
|
2011-09-17 17:04:24 +02:00
|
|
|
#endif
|
2011-09-09 21:30:03 +02:00
|
|
|
modifyreg32(STM32_RCC_APB1ENR, RCC_APB1ENR_I2C1EN, 0);
|
|
|
|
break;
|
2011-03-28 17:01:43 +02:00
|
|
|
#endif
|
|
|
|
|
2011-09-08 19:56:08 +02:00
|
|
|
#ifdef CONFIG_STM32_I2C2
|
2011-09-09 21:30:03 +02:00
|
|
|
case STM32_I2C2_BASE:
|
|
|
|
stm32_unconfiggpio(GPIO_I2C2_SCL);
|
|
|
|
stm32_unconfiggpio(GPIO_I2C2_SDA);
|
2011-03-28 17:01:43 +02:00
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
#ifndef CONFIG_I2C_POLLED
|
|
|
|
up_disable_irq(STM32_IRQ_I2C2EV);
|
|
|
|
up_disable_irq(STM32_IRQ_I2C2ER);
|
|
|
|
irq_detach(STM32_IRQ_I2C2EV);
|
|
|
|
irq_detach(STM32_IRQ_I2C2ER);
|
|
|
|
#endif
|
2011-09-09 21:30:03 +02:00
|
|
|
modifyreg32(STM32_RCC_APB1ENR, RCC_APB1ENR_I2C2EN, 0);
|
|
|
|
break;
|
2011-03-28 17:01:43 +02:00
|
|
|
#endif
|
2011-03-27 17:03:49 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
default:
|
|
|
|
return ERROR;
|
2011-03-27 17:03:49 +02:00
|
|
|
}
|
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
return OK;
|
2011-03-27 17:03:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************************
|
2011-09-17 17:04:24 +02:00
|
|
|
* Device Driver Operations
|
2011-03-27 17:03:49 +02:00
|
|
|
************************************************************************************/
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: stm32_i2c_setfrequency
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set the I2C frequency
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
|
|
|
static uint32_t stm32_i2c_setfrequency(FAR struct i2c_dev_s *dev, uint32_t frequency)
|
2011-03-27 17:03:49 +02:00
|
|
|
{
|
2011-09-09 21:30:03 +02:00
|
|
|
stm32_i2c_sem_wait(dev);
|
|
|
|
|
2011-09-08 19:56:08 +02:00
|
|
|
#if STM32_PCLK1_FREQUENCY < 4000000
|
2011-09-09 21:30:03 +02:00
|
|
|
((struct stm32_i2c_inst_s *)dev)->frequency = 100000;
|
2011-03-28 17:01:43 +02:00
|
|
|
#else
|
2011-09-09 21:30:03 +02:00
|
|
|
((struct stm32_i2c_inst_s *)dev)->frequency = frequency;
|
2011-03-28 17:01:43 +02:00
|
|
|
#endif
|
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
stm32_i2c_sem_post(dev);
|
|
|
|
return ((struct stm32_i2c_inst_s *)dev)->frequency;
|
2011-03-27 17:03:49 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: stm32_i2c_setaddress
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set the I2C slave address
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
|
|
|
static int stm32_i2c_setaddress(FAR struct i2c_dev_s *dev, int addr, int nbits)
|
2011-03-27 17:03:49 +02:00
|
|
|
{
|
2011-09-09 21:30:03 +02:00
|
|
|
stm32_i2c_sem_wait(dev);
|
|
|
|
|
|
|
|
((struct stm32_i2c_inst_s *)dev)->address = addr;
|
|
|
|
((struct stm32_i2c_inst_s *)dev)->flags = (nbits == 10) ? I2C_M_TEN : 0;
|
|
|
|
|
2011-04-03 16:26:05 +02:00
|
|
|
stm32_i2c_sem_post(dev);
|
2011-03-27 17:03:49 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: stm32_i2c_process
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Common I2C transfer logic
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
|
|
|
static int stm32_i2c_process(FAR struct i2c_dev_s *dev, FAR struct i2c_msg_s *msgs, int count)
|
2011-03-28 17:01:43 +02:00
|
|
|
{
|
2011-09-18 19:52:00 +02:00
|
|
|
struct stm32_i2c_inst_s *inst = (struct stm32_i2c_inst_s *)dev;
|
|
|
|
FAR struct stm32_i2c_priv_s *priv = inst->priv;
|
2011-09-08 19:56:08 +02:00
|
|
|
uint32_t status = 0;
|
|
|
|
uint32_t ahbenr;
|
2011-09-18 19:52:00 +02:00
|
|
|
int errval = 0;
|
2011-09-08 19:56:08 +02:00
|
|
|
|
|
|
|
ASSERT(count);
|
2011-04-07 23:22:06 +02:00
|
|
|
|
2011-09-08 19:56:08 +02:00
|
|
|
/* Disable FSMC that shares a pin with I2C1 (LBAR) */
|
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
ahbenr = stm32_i2c_disablefsmc(priv);
|
2011-09-08 19:56:08 +02:00
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
/* Wait for any STOP in progress. NOTE: If we have to disable the FSMC
|
|
|
|
* then we cannot do this at the top of the loop, unfortunately. The STOP
|
|
|
|
* will not complete normally if the FSMC is enabled.
|
2011-09-08 19:56:08 +02:00
|
|
|
*/
|
2011-09-11 16:55:31 +02:00
|
|
|
|
2011-09-18 20:11:11 +02:00
|
|
|
#if !defined(CONFIG_STM32_FSMC) || !defined (CONFIG_STM32_I2C1)
|
2011-09-18 19:52:00 +02:00
|
|
|
stm32_i2c_sem_waitstop(priv);
|
|
|
|
#endif
|
2011-09-11 16:55:31 +02:00
|
|
|
|
|
|
|
/* Clear any pending error interrupts */
|
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
stm32_i2c_putreg(priv, STM32_I2C_SR1_OFFSET, 0);
|
2011-09-11 16:55:31 +02:00
|
|
|
|
|
|
|
/* "Note: When the STOP, START or PEC bit is set, the software must
|
|
|
|
* not perform any write access to I2C_CR1 before this bit is
|
|
|
|
* cleared by hardware. Otherwise there is a risk of setting a
|
|
|
|
* second STOP, START or PEC request." However, if the bits are
|
|
|
|
* not cleared by hardware, then we will have to do that from hardware.
|
|
|
|
*/
|
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
stm32_i2c_clrstart(priv);
|
2011-04-07 23:22:06 +02:00
|
|
|
|
2011-09-08 19:56:08 +02:00
|
|
|
/* Old transfers are done */
|
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
priv->msgv = msgs;
|
|
|
|
priv->msgc = count;
|
|
|
|
|
|
|
|
/* Reset I2C trace logic */
|
|
|
|
|
|
|
|
stm32_i2c_tracereset(priv);
|
2011-04-03 16:26:05 +02:00
|
|
|
|
2011-09-09 21:50:18 +02:00
|
|
|
/* Set I2C clock frequency (on change it toggles I2C_CR1_PE !) */
|
2011-09-08 19:56:08 +02:00
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
stm32_i2c_setclock(priv, inst->frequency);
|
2011-09-08 19:56:08 +02:00
|
|
|
|
2011-09-09 21:50:18 +02:00
|
|
|
/* Trigger start condition, then the process moves into the ISR. I2C
|
2011-09-18 19:52:00 +02:00
|
|
|
* interrupts will be enabled within stm32_i2c_waitdone().
|
2011-09-09 21:50:18 +02:00
|
|
|
*/
|
2011-09-08 19:56:08 +02:00
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
priv->status = 0;
|
|
|
|
stm32_i2c_sendstart(priv);
|
2011-09-08 19:56:08 +02:00
|
|
|
|
|
|
|
/* Wait for an ISR, if there was a timeout, fetch latest status to get
|
|
|
|
* the BUSY flag.
|
|
|
|
*/
|
|
|
|
|
2011-10-05 03:22:49 +02:00
|
|
|
if (stm32_i2c_sem_waitdone(priv) < 0)
|
2011-09-08 19:56:08 +02:00
|
|
|
{
|
2011-09-18 19:52:00 +02:00
|
|
|
status = stm32_i2c_getstatus(priv);
|
|
|
|
errval = ETIMEDOUT;
|
|
|
|
|
|
|
|
i2cdbg("Timed out: CR1: %04x status: %08x\n",
|
|
|
|
stm32_i2c_getreg(priv, STM32_I2C_CR1_OFFSET), status);
|
2011-09-08 19:56:08 +02:00
|
|
|
|
2011-09-11 16:55:31 +02:00
|
|
|
/* "Note: When the STOP, START or PEC bit is set, the software must
|
|
|
|
* not perform any write access to I2C_CR1 before this bit is
|
|
|
|
* cleared by hardware. Otherwise there is a risk of setting a
|
|
|
|
* second STOP, START or PEC request."
|
2011-09-08 19:56:08 +02:00
|
|
|
*/
|
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
stm32_i2c_clrstart(priv);
|
2011-09-08 19:56:08 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* clear SR2 (BUSY flag) as we've done successfully */
|
2011-04-03 16:26:05 +02:00
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
status = priv->status & 0xffff;
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
2011-09-09 21:50:18 +02:00
|
|
|
|
|
|
|
/* Check for error status conditions */
|
2011-04-03 16:26:05 +02:00
|
|
|
|
2011-09-10 18:20:09 +02:00
|
|
|
if ((status & I2C_SR1_ERRORMASK) != 0)
|
2011-09-08 19:56:08 +02:00
|
|
|
{
|
2011-09-11 16:55:31 +02:00
|
|
|
/* I2C_SR1_ERRORMASK is the 'OR' of the following individual bits: */
|
|
|
|
|
2011-09-10 18:20:09 +02:00
|
|
|
if (status & I2C_SR1_BERR)
|
|
|
|
{
|
|
|
|
/* Bus Error */
|
2011-09-08 19:56:08 +02:00
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
errval = EIO;
|
2011-09-10 18:20:09 +02:00
|
|
|
}
|
|
|
|
else if (status & I2C_SR1_ARLO)
|
|
|
|
{
|
|
|
|
/* Arbitration Lost (master mode) */
|
2011-09-08 19:56:08 +02:00
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
errval = EAGAIN;
|
2011-09-10 18:20:09 +02:00
|
|
|
}
|
|
|
|
else if (status & I2C_SR1_AF)
|
|
|
|
{
|
|
|
|
/* Acknowledge Failure */
|
2011-09-08 19:56:08 +02:00
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
errval = ENXIO;
|
2011-09-10 18:20:09 +02:00
|
|
|
}
|
|
|
|
else if (status & I2C_SR1_OVR)
|
|
|
|
{
|
|
|
|
/* Overrun/Underrun */
|
2011-09-08 19:56:08 +02:00
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
errval = EIO;
|
2011-09-10 18:20:09 +02:00
|
|
|
}
|
|
|
|
else if (status & I2C_SR1_PECERR)
|
|
|
|
{
|
|
|
|
/* PEC Error in reception */
|
2011-09-08 19:56:08 +02:00
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
errval = EPROTO;
|
2011-09-10 18:20:09 +02:00
|
|
|
}
|
|
|
|
else if (status & I2C_SR1_TIMEOUT)
|
|
|
|
{
|
|
|
|
/* Timeout or Tlow Error */
|
2011-09-08 19:56:08 +02:00
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
errval = ETIME;
|
2011-09-10 18:20:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This is not an error and should never happen since SMBus is not enabled */
|
|
|
|
|
2011-09-11 16:55:31 +02:00
|
|
|
else /* if (status & I2C_SR1_SMBALERT) */
|
2011-09-10 18:20:09 +02:00
|
|
|
{
|
|
|
|
/* SMBus alert is an optional signal with an interrupt line for devices
|
|
|
|
* that want to trade their ability to master for a pin.
|
|
|
|
*/
|
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
errval = EINTR;
|
2011-09-10 18:20:09 +02:00
|
|
|
}
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
2011-09-10 01:13:17 +02:00
|
|
|
|
|
|
|
/* This is not an error, but should not happen. The BUSY signal can hang,
|
|
|
|
* however, if there are unhealthy devices on the bus that need to be reset.
|
2011-09-18 19:52:00 +02:00
|
|
|
* NOTE: We will only see this buy indication if stm32_i2c_sem_waitdone()
|
2011-09-11 16:55:31 +02:00
|
|
|
* fails above; Otherwise it is cleared.
|
2011-09-10 01:13:17 +02:00
|
|
|
*/
|
|
|
|
|
2011-09-11 16:55:31 +02:00
|
|
|
else if ((status & (I2C_SR2_BUSY << 16)) != 0)
|
2011-09-08 19:56:08 +02:00
|
|
|
{
|
|
|
|
/* I2C Bus is for some reason busy */
|
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
errval = EBUSY;
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
2011-09-08 19:56:08 +02:00
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
/* Dump the trace result */
|
|
|
|
|
|
|
|
stm32_i2c_tracedump(priv);
|
|
|
|
|
|
|
|
/* Wait for any STOP in progress. NOTE: If we have to disable the FSMC
|
|
|
|
* then we cannot do this at the top of the loop, unfortunately. The STOP
|
|
|
|
* will not complete normally if the FSMC is enabled.
|
|
|
|
*/
|
|
|
|
|
2011-09-18 20:11:11 +02:00
|
|
|
#if defined(CONFIG_STM32_FSMC) && defined (CONFIG_STM32_I2C1)
|
2011-09-18 19:52:00 +02:00
|
|
|
stm32_i2c_sem_waitstop(priv);
|
|
|
|
#endif
|
|
|
|
|
2011-09-08 19:56:08 +02:00
|
|
|
/* Re-enable the FSMC */
|
|
|
|
|
|
|
|
stm32_i2c_enablefsmc(ahbenr);
|
|
|
|
stm32_i2c_sem_post(dev);
|
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
return -errval;
|
2011-03-28 17:01:43 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: stm32_i2c_write
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write I2C data
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
|
|
|
static int stm32_i2c_write(FAR struct i2c_dev_s *dev, const uint8_t *buffer, int buflen)
|
2011-03-27 17:03:49 +02:00
|
|
|
{
|
2011-09-09 21:30:03 +02:00
|
|
|
stm32_i2c_sem_wait(dev); /* ensure that address or flags don't change meanwhile */
|
|
|
|
|
|
|
|
struct i2c_msg_s msgv =
|
|
|
|
{
|
|
|
|
.addr = ((struct stm32_i2c_inst_s *)dev)->address,
|
|
|
|
.flags = ((struct stm32_i2c_inst_s *)dev)->flags,
|
|
|
|
.buffer = (uint8_t *)buffer,
|
|
|
|
.length = buflen
|
|
|
|
};
|
2011-09-11 16:55:31 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
return stm32_i2c_process(dev, &msgv, 1);
|
2011-03-27 17:03:49 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: stm32_i2c_read
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read I2C data
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
2011-04-03 16:26:05 +02:00
|
|
|
int stm32_i2c_read(FAR struct i2c_dev_s *dev, uint8_t *buffer, int buflen)
|
2011-03-27 17:03:49 +02:00
|
|
|
{
|
2011-09-09 21:30:03 +02:00
|
|
|
stm32_i2c_sem_wait(dev); /* ensure that address or flags don't change meanwhile */
|
|
|
|
|
|
|
|
struct i2c_msg_s msgv =
|
|
|
|
{
|
|
|
|
.addr = ((struct stm32_i2c_inst_s *)dev)->address,
|
|
|
|
.flags = ((struct stm32_i2c_inst_s *)dev)->flags | I2C_M_READ,
|
|
|
|
.buffer = buffer,
|
|
|
|
.length = buflen
|
|
|
|
};
|
2011-03-27 17:03:49 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
return stm32_i2c_process(dev, &msgv, 1);
|
2011-03-27 17:03:49 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: stm32_i2c_writeread
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read then write I2C data
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
2011-03-28 17:01:43 +02:00
|
|
|
#ifdef CONFIG_I2C_WRITEREAD
|
2011-09-17 17:04:24 +02:00
|
|
|
static int stm32_i2c_writeread(FAR struct i2c_dev_s *dev,
|
|
|
|
const uint8_t *wbuffer, int wbuflen,
|
|
|
|
uint8_t *buffer, int buflen)
|
2011-03-28 17:01:43 +02:00
|
|
|
{
|
2011-09-09 21:30:03 +02:00
|
|
|
stm32_i2c_sem_wait(dev); /* ensure that address or flags don't change meanwhile */
|
2011-03-28 17:01:43 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
struct i2c_msg_s msgv[2] =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
.addr = ((struct stm32_i2c_inst_s *)dev)->address,
|
|
|
|
.flags = ((struct stm32_i2c_inst_s *)dev)->flags,
|
|
|
|
.buffer = (uint8_t *)wbuffer, /* this is really ugly, sorry const ... */
|
|
|
|
.length = wbuflen
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.addr = ((struct stm32_i2c_inst_s *)dev)->address,
|
|
|
|
.flags = ((struct stm32_i2c_inst_s *)dev)->flags | ((buflen>0) ? I2C_M_READ : I2C_M_NORESTART),
|
|
|
|
.buffer = buffer,
|
|
|
|
.length = (buflen>0) ? buflen : -buflen
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return stm32_i2c_process(dev, msgv, 2);
|
2011-03-28 17:01:43 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: stm32_i2c_transfer
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Generic I2C transfer function
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
2011-03-27 17:03:49 +02:00
|
|
|
#ifdef CONFIG_I2C_TRANSFER
|
2011-09-17 17:04:24 +02:00
|
|
|
static int stm32_i2c_transfer(FAR struct i2c_dev_s *dev, FAR struct i2c_msg_s *msgs,
|
|
|
|
int count)
|
2011-03-27 17:03:49 +02:00
|
|
|
{
|
2011-09-09 21:30:03 +02:00
|
|
|
stm32_i2c_sem_wait(dev); /* ensure that address or flags don't change meanwhile */
|
|
|
|
return stm32_i2c_process(dev, msgs, count);
|
2011-03-27 17:03:49 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/************************************************************************************
|
2011-09-17 17:04:24 +02:00
|
|
|
* Public Functions
|
2011-03-27 17:03:49 +02:00
|
|
|
************************************************************************************/
|
|
|
|
|
|
|
|
/************************************************************************************
|
2011-09-17 17:04:24 +02:00
|
|
|
* Name: up_i2cinitialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize one I2C bus
|
|
|
|
*
|
2011-03-27 17:03:49 +02:00
|
|
|
************************************************************************************/
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
FAR struct i2c_dev_s *up_i2cinitialize(int port)
|
2011-03-27 17:03:49 +02:00
|
|
|
{
|
2011-09-09 21:30:03 +02:00
|
|
|
struct stm32_i2c_priv_s * priv = NULL; /* private data of device with multiple instances */
|
|
|
|
struct stm32_i2c_inst_s * inst = NULL; /* device, single instance */
|
|
|
|
int irqs;
|
|
|
|
|
2011-09-08 19:56:08 +02:00
|
|
|
#if STM32_PCLK1_FREQUENCY < 4000000
|
2011-03-28 17:01:43 +02:00
|
|
|
# warning STM32_I2C_INIT: Peripheral clock must be at least 4 MHz to support 400 kHz operation.
|
|
|
|
#endif
|
|
|
|
|
2011-09-08 19:56:08 +02:00
|
|
|
#if STM32_PCLK1_FREQUENCY < 2000000
|
2011-03-28 17:01:43 +02:00
|
|
|
# warning STM32_I2C_INIT: Peripheral clock must be at least 2 MHz to support 100 kHz operation.
|
|
|
|
return NULL;
|
|
|
|
#endif
|
2011-03-27 17:03:49 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
/* Get I2C private structure */
|
|
|
|
|
|
|
|
switch (port)
|
|
|
|
{
|
2011-09-08 19:56:08 +02:00
|
|
|
#ifdef CONFIG_STM32_I2C1
|
2011-09-09 21:30:03 +02:00
|
|
|
case 1:
|
|
|
|
priv = (struct stm32_i2c_priv_s *)&stm32_i2c1_priv;
|
|
|
|
break;
|
2011-03-28 17:01:43 +02:00
|
|
|
#endif
|
2011-09-08 19:56:08 +02:00
|
|
|
#ifdef CONFIG_STM32_I2C2
|
2011-09-09 21:30:03 +02:00
|
|
|
case 2:
|
|
|
|
priv = (struct stm32_i2c_priv_s *)&stm32_i2c2_priv;
|
|
|
|
break;
|
2011-03-28 17:01:43 +02:00
|
|
|
#endif
|
2011-09-09 21:30:03 +02:00
|
|
|
default:
|
|
|
|
return NULL;
|
2011-03-27 17:03:49 +02:00
|
|
|
}
|
2011-09-09 21:30:03 +02:00
|
|
|
|
|
|
|
/* Allocate instance */
|
2011-03-27 17:03:49 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
if (!(inst = kmalloc( sizeof(struct stm32_i2c_inst_s))))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize instance */
|
|
|
|
|
|
|
|
inst->ops = &stm32_i2c_ops;
|
|
|
|
inst->priv = priv;
|
|
|
|
inst->frequency = 100000;
|
|
|
|
inst->address = 0;
|
|
|
|
inst->flags = 0;
|
|
|
|
|
|
|
|
/* Init private data for the first time, increment refs count,
|
|
|
|
* power-up hardware and configure GPIOs.
|
|
|
|
*/
|
2011-03-27 17:03:49 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
irqs = irqsave();
|
2011-03-27 17:03:49 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
if ((volatile int)priv->refs++ == 0)
|
|
|
|
{
|
|
|
|
stm32_i2c_sem_init( (struct i2c_dev_s *)inst );
|
|
|
|
stm32_i2c_init( priv );
|
2011-03-27 17:03:49 +02:00
|
|
|
}
|
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
irqrestore(irqs);
|
|
|
|
return (struct i2c_dev_s *)inst;
|
2011-03-27 17:03:49 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 17:04:24 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: up_i2cuninitialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Uninitialize an I2C bus
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
2011-04-03 16:26:05 +02:00
|
|
|
int up_i2cuninitialize(FAR struct i2c_dev_s * dev)
|
2011-03-27 17:03:49 +02:00
|
|
|
{
|
2011-09-09 21:30:03 +02:00
|
|
|
int irqs;
|
2011-03-27 17:03:49 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
ASSERT(dev);
|
2011-03-27 17:03:49 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
/* Decrement refs and check for underflow */
|
2011-03-27 17:03:49 +02:00
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
if (((struct stm32_i2c_inst_s *)dev)->priv->refs == 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
2011-03-27 17:03:49 +02:00
|
|
|
}
|
|
|
|
|
2011-09-09 21:30:03 +02:00
|
|
|
irqs = irqsave();
|
|
|
|
|
|
|
|
if (--((struct stm32_i2c_inst_s *)dev)->priv->refs)
|
|
|
|
{
|
|
|
|
irqrestore(irqs);
|
|
|
|
kfree(dev);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
irqrestore(irqs);
|
|
|
|
|
|
|
|
/* Disable power and other HW resource (GPIO's) */
|
|
|
|
|
|
|
|
stm32_i2c_deinit( ((struct stm32_i2c_inst_s *)dev)->priv );
|
|
|
|
|
|
|
|
/* Release unused resources */
|
|
|
|
|
|
|
|
stm32_i2c_sem_destroy( (struct i2c_dev_s *)dev );
|
|
|
|
|
|
|
|
kfree(dev);
|
|
|
|
return OK;
|
2011-03-27 17:03:49 +02:00
|
|
|
}
|
|
|
|
|
2011-03-28 17:01:43 +02:00
|
|
|
#endif /* defined(CONFIG_STM32_I2C1) && defined(CONFIG_STM32_I2C2) */
|