2013-09-11 12:28:52 -06:00
|
|
|
/*******************************************************************************
|
|
|
|
* arch/arm/src/sama5/sam_twi.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
2013-09-11 16:48:56 -06:00
|
|
|
* References:
|
|
|
|
* SAMA5D3 Series Data Sheet
|
|
|
|
* Atmel NoOS sample code.
|
|
|
|
*
|
2013-12-21 09:45:27 -06:00
|
|
|
* The Atmel sample code has a BSD compatible license that requires this
|
2013-09-11 16:48:56 -06:00
|
|
|
* copyright notice:
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011, Atmel Corporation
|
|
|
|
*
|
2013-09-11 12:28:52 -06: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.
|
2013-09-11 16:48:56 -06:00
|
|
|
* 3. Neither the name NuttX, Atmel, nor the names of its contributors may
|
|
|
|
* be used to endorse or promote products derived from this software
|
2013-09-11 12:28:52 -06:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Included Files
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <wdog.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/i2c.h>
|
|
|
|
|
|
|
|
#include <arch/irq.h>
|
|
|
|
#include <arch/board/board.h>
|
|
|
|
|
|
|
|
#include "up_arch.h"
|
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
#include "chip/sam_pmc.h"
|
|
|
|
#include "chip/sam_pinmap.h"
|
|
|
|
|
2013-09-11 12:28:52 -06:00
|
|
|
#include "sam_periphclks.h"
|
2013-09-11 16:48:56 -06:00
|
|
|
#include "sam_pio.h"
|
2013-09-11 12:28:52 -06:00
|
|
|
#include "sam_twi.h"
|
|
|
|
|
|
|
|
#if defined(CONFIG_SAMA5_TWI0) || defined(CONFIG_SAMA5_TWI1) || defined(CONFIG_SAMA5_TWI2)
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
*******************************************************************************/
|
|
|
|
/* Configuration ***************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_SAMA5_TWI0_FREQUENCY
|
|
|
|
# define CONFIG_SAMA5_TWI0_FREQUENCY 100000
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_SAMA5_TWI1_FREQUENCY
|
|
|
|
#define CONFIG_SAMA5_TWI1_FREQUENCY 100000
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_SAMA5_TWI2_FREQUENCY
|
|
|
|
#define CONFIG_SAMA5_TWI2_FREQUENCY 100000
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Driver internal definitions *************************************************/
|
|
|
|
|
|
|
|
#define TWI_TIMEOUT ((20 * CLK_TCK) / 1000) /* 20 mS */
|
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
/* Clocking to the TWO module(s) is provided by the main clocked, divided down
|
|
|
|
* as necessary.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define TWI_MAX_FREQUENCY 66000000 /* Maximum TWI frequency */
|
|
|
|
|
2013-09-11 12:28:52 -06:00
|
|
|
/* Debug ***********************************************************************/
|
|
|
|
/* CONFIG_DEBUG_I2C + CONFIG_DEBUG enables general I2C debug output. */
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_I2C
|
2013-09-11 16:48:56 -06:00
|
|
|
# define i2cdbg dbg
|
|
|
|
# define i2cvdbg vdbg
|
|
|
|
# define i2clldbg lldbg
|
|
|
|
# define i2cllvdbg llvdbg
|
2013-09-11 12:28:52 -06:00
|
|
|
#else
|
|
|
|
# define i2cdbg(x...)
|
|
|
|
# define i2cvdbg(x...)
|
2013-09-11 16:48:56 -06:00
|
|
|
# define i2clldbg(x...)
|
|
|
|
# define i2cllvdbg(x...)
|
2013-09-11 12:28:52 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Private Types
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
struct twi_dev_s
|
|
|
|
{
|
|
|
|
struct i2c_dev_s dev; /* Generic I2C device */
|
2013-09-11 17:52:23 -06:00
|
|
|
struct i2c_msg_s *msg; /* Message list */
|
2013-09-11 16:48:56 -06:00
|
|
|
uintptr_t base; /* Base address of registers */
|
2014-04-16 12:09:55 -06:00
|
|
|
uint32_t frequency; /* TWO clock frequency */
|
2013-09-11 12:28:52 -06:00
|
|
|
uint16_t irq; /* IRQ number for this device */
|
2013-09-11 17:52:23 -06:00
|
|
|
uint16_t address; /* Slave address */
|
|
|
|
uint16_t flags; /* Transfer flags */
|
|
|
|
uint8_t msgc; /* Number of message in the message list */
|
2013-09-11 16:48:56 -06:00
|
|
|
uint8_t twi; /* TWI peripheral number (for debug output) */
|
2013-09-11 12:28:52 -06:00
|
|
|
|
|
|
|
sem_t exclsem; /* Only one thread can access at a time */
|
|
|
|
sem_t waitsem; /* Wait for TWI transfer completion */
|
|
|
|
WDOG_ID timeout; /* Watchdog to recover from bus hangs */
|
2013-09-11 16:48:56 -06:00
|
|
|
volatile int result; /* The result of the transfer */
|
|
|
|
volatile int xfrd; /* Number of bytes transfers */
|
2013-09-11 12:28:52 -06:00
|
|
|
|
|
|
|
/* Debug stuff */
|
|
|
|
|
|
|
|
#ifdef CONFIG_SAMA5_TWI_REGDEBUG
|
|
|
|
bool wrlast; /* Last was a write */
|
|
|
|
uint32_t addrlast; /* Last address */
|
|
|
|
uint32_t vallast; /* Last value */
|
|
|
|
int ntimes; /* Number of times */
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
/* Low-level helper functions */
|
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
static void twi_takesem(sem_t *sem);
|
|
|
|
#define twi_givesem(sem) (sem_post(sem))
|
2013-09-11 12:28:52 -06:00
|
|
|
|
|
|
|
#ifdef CONFIG_SAMA5_TWI_REGDEBUG
|
2013-09-11 16:48:56 -06:00
|
|
|
static bool twi_checkreg(struct twi_dev_s *priv, bool wr,
|
2013-09-11 12:28:52 -06:00
|
|
|
uint32_t value, uintptr_t address);
|
2013-09-12 12:25:31 -06:00
|
|
|
static uint32_t twi_getabs(struct twi_dev_s *priv, uintptr_t address);
|
|
|
|
static void twi_putabs(struct twi_dev_s *priv, uintptr_t address,
|
|
|
|
uint32_t value);
|
2013-09-11 12:28:52 -06:00
|
|
|
#else
|
2013-09-11 16:48:56 -06:00
|
|
|
# define twi_checkreg(priv,wr,value,address) (false)
|
2013-09-12 12:25:31 -06:00
|
|
|
# define twi_putabs(p,a,v) putreg32(v,a)
|
|
|
|
# define twi_getabs(p,a) getreg32(a)
|
2013-09-11 12:28:52 -06:00
|
|
|
#endif
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
static inline uint32_t twi_getrel(struct twi_dev_s *priv,
|
2013-09-11 12:28:52 -06:00
|
|
|
unsigned int offset);
|
2013-09-12 12:25:31 -06:00
|
|
|
static inline void twi_putrel(struct twi_dev_s *priv, unsigned int offset,
|
2013-09-11 16:48:56 -06:00
|
|
|
uint32_t value);
|
2013-09-11 12:28:52 -06:00
|
|
|
|
|
|
|
/* I2C transfer helper functions */
|
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
static int twi_wait(struct twi_dev_s *priv);
|
2013-12-10 09:10:39 -06:00
|
|
|
static void twi_wakeup(struct twi_dev_s *priv, int result);
|
2013-09-11 12:28:52 -06:00
|
|
|
static int twi_interrupt(struct twi_dev_s *priv);
|
|
|
|
#ifdef CONFIG_SAMA5_TWI0
|
|
|
|
static int twi0_interrupt(int irq, FAR void *context);
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SAMA5_TWI1
|
|
|
|
static int twi1_interrupt(int irq, FAR void *context);
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SAMA5_TWI2
|
|
|
|
static int twi2_interrupt(int irq, FAR void *context);
|
|
|
|
#endif
|
|
|
|
static void twi_timeout(int argc, uint32_t arg, ...);
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
static void twi_startread(struct twi_dev_s *priv, struct i2c_msg_s *msg);
|
|
|
|
static void twi_startwrite(struct twi_dev_s *priv, struct i2c_msg_s *msg);
|
|
|
|
static void twi_startmessage(struct twi_dev_s *priv, struct i2c_msg_s *msg);
|
2013-09-11 17:52:23 -06:00
|
|
|
|
2013-09-11 12:28:52 -06:00
|
|
|
/* I2C device operations */
|
|
|
|
|
|
|
|
static uint32_t twi_setfrequency(FAR struct i2c_dev_s *dev,
|
|
|
|
uint32_t frequency);
|
|
|
|
static int twi_setaddress(FAR struct i2c_dev_s *dev, int addr, int nbits);
|
|
|
|
static int twi_write(FAR struct i2c_dev_s *dev, const uint8_t *buffer,
|
|
|
|
int buflen);
|
|
|
|
static int twi_read(FAR struct i2c_dev_s *dev, uint8_t *buffer, int buflen);
|
|
|
|
#ifdef CONFIG_I2C_WRITEREAD
|
2013-09-11 17:52:23 -06:00
|
|
|
static int twi_writeread(FAR struct i2c_dev_s *inst, const uint8_t *wbuffer,
|
|
|
|
int wbuflen, uint8_t *rbuffer, int rbuflen);
|
2013-09-11 12:28:52 -06:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_I2C_TRANSFER
|
|
|
|
static int twi_transfer(FAR struct i2c_dev_s *dev,
|
|
|
|
FAR struct i2c_msg_s *msgs, int count);
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_I2C_SLAVE
|
|
|
|
static int twi_setownaddress(FAR struct i2c_dev_s *dev, int addr, int nbits);
|
|
|
|
static int twi_registercallback(FAR struct i2c_dev_s *dev,
|
|
|
|
int (*callback)(void));
|
|
|
|
#endif
|
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
/* Initialization */
|
|
|
|
|
|
|
|
static uint32_t twi_hw_setfrequency(struct twi_dev_s *priv,
|
|
|
|
uint32_t frequency);
|
|
|
|
static void twi_hw_initialize(struct twi_dev_s *priv, unsigned int pid,
|
|
|
|
uint32_t frequency);
|
|
|
|
|
2013-09-11 12:28:52 -06:00
|
|
|
/*******************************************************************************
|
|
|
|
* Private Data
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SAMA5_TWI0
|
|
|
|
static struct twi_dev_s g_twi0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_SAMA5_TWI1
|
|
|
|
static struct twi_dev_s g_twi1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_SAMA5_TWI2
|
|
|
|
static struct twi_dev_s g_twi2;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct i2c_ops_s g_twiops =
|
|
|
|
{
|
2013-09-12 09:46:20 -06:00
|
|
|
.setfrequency = twi_setfrequency,
|
|
|
|
.setaddress = twi_setaddress,
|
|
|
|
.write = twi_write,
|
|
|
|
.read = twi_read,
|
2013-09-11 12:28:52 -06:00
|
|
|
#ifdef CONFIG_I2C_WRITEREAD
|
2013-09-12 09:46:20 -06:00
|
|
|
.writeread = twi_writeread,
|
2013-09-11 12:28:52 -06:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_I2C_TRANSFER
|
2013-09-12 09:46:20 -06:00
|
|
|
.transfer = twi_transfer
|
2013-09-11 12:28:52 -06:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_I2C_SLAVE
|
2013-09-12 09:46:20 -06:00
|
|
|
.setownaddress = twi_setownaddress
|
|
|
|
.registercallback = twi_registercallback
|
2013-09-11 12:28:52 -06:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Low-level Helpers
|
|
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
2013-09-11 16:48:56 -06:00
|
|
|
* Name: twi_takesem
|
2013-09-11 12:28:52 -06:00
|
|
|
*
|
|
|
|
* Description:
|
2014-04-16 12:09:55 -06:00
|
|
|
* Take the wait semaphore (handling false alarm wake-ups due to the receipt
|
2013-09-11 12:28:52 -06:00
|
|
|
* of signals).
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - Instance of the SDIO device driver state structure.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
static void twi_takesem(sem_t *sem)
|
2013-09-11 12:28:52 -06:00
|
|
|
{
|
|
|
|
/* Take the semaphore (perhaps waiting) */
|
|
|
|
|
|
|
|
while (sem_wait(sem) != 0)
|
|
|
|
{
|
|
|
|
/* The only case that an error should occr here is if the wait was
|
|
|
|
* awakened by a signal.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ASSERT(errno == EINTR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2013-09-11 16:48:56 -06:00
|
|
|
* Name: twi_checkreg
|
2013-09-11 12:28:52 -06:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if the current register access is a duplicate of the preceding.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* value - The value to be written
|
|
|
|
* address - The address of the register to write to
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* true: This is the first register access of this type.
|
2014-04-16 12:09:55 -06:00
|
|
|
* false: This is the same as the preceding register access.
|
2013-09-11 12:28:52 -06:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SAMA5_TWI_REGDEBUG
|
2013-09-11 16:48:56 -06:00
|
|
|
static bool twi_checkreg(struct twi_dev_s *priv, bool wr, uint32_t value,
|
2013-09-11 12:28:52 -06:00
|
|
|
uint32_t address)
|
|
|
|
{
|
2014-04-16 12:09:55 -06:00
|
|
|
if (wr == priv->wrlast && /* Same kind of access? */
|
2013-09-11 12:28:52 -06:00
|
|
|
value == priv->vallast && /* Same value? */
|
2014-04-16 12:09:55 -06:00
|
|
|
address == priv->addrlast) /* Same address? */
|
2013-09-11 12:28:52 -06:00
|
|
|
{
|
|
|
|
/* Yes, then just keep a count of the number of times we did this. */
|
|
|
|
|
|
|
|
priv->ntimes++;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Did we do the previous operation more than once? */
|
|
|
|
|
|
|
|
if (priv->ntimes > 0)
|
|
|
|
{
|
|
|
|
/* Yes... show how many times we did it */
|
|
|
|
|
|
|
|
lldbg("...[Repeats %d times]...\n", priv->ntimes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save information about the new access */
|
|
|
|
|
|
|
|
priv->wrlast = wr;
|
|
|
|
priv->vallast = value;
|
|
|
|
priv->addrlast = address;
|
|
|
|
priv->ntimes = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return true if this is the first time that we have done this operation */
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2013-09-12 12:25:31 -06:00
|
|
|
* Name: twi_getabs
|
2013-09-11 12:28:52 -06:00
|
|
|
*
|
|
|
|
* Description:
|
2013-09-12 12:25:31 -06:00
|
|
|
* Read any 32-bit register using an absolute
|
2013-09-11 12:28:52 -06:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
#ifdef CONFIG_SAMA5_TWI_REGDEBUG
|
|
|
|
static uint32_t twi_getabs(struct twi_dev_s *priv, uintptr_t address)
|
2013-09-11 12:28:52 -06:00
|
|
|
{
|
|
|
|
uint32_t value = getreg32(address);
|
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
if (twi_checkreg(priv, false, value, address))
|
2013-09-11 12:28:52 -06:00
|
|
|
{
|
|
|
|
lldbg("%08x->%08x\n", address, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
2013-09-12 12:25:31 -06:00
|
|
|
#endif
|
2013-09-11 12:28:52 -06:00
|
|
|
|
|
|
|
/****************************************************************************
|
2013-09-12 12:25:31 -06:00
|
|
|
* Name: twi_putabs
|
2013-09-11 12:28:52 -06:00
|
|
|
*
|
|
|
|
* Description:
|
2013-09-12 12:25:31 -06:00
|
|
|
* Write to any 32-bit register using an absolute address
|
2013-09-11 12:28:52 -06:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SAMA5_TWI_REGDEBUG
|
2013-09-12 12:25:31 -06:00
|
|
|
static void twi_putabs(struct twi_dev_s *priv, uintptr_t address,
|
|
|
|
uint32_t value)
|
|
|
|
{
|
2013-09-11 16:48:56 -06:00
|
|
|
if (twi_checkreg(priv, true, value, address))
|
2013-09-11 12:28:52 -06:00
|
|
|
{
|
|
|
|
lldbg("%08x<-%08x\n", address, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
putreg32(value, address);
|
|
|
|
}
|
2013-09-12 12:25:31 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: twi_getrel
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read a TWI register using an offset relative to the TWI base address
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline uint32_t twi_getrel(struct twi_dev_s *priv, unsigned int offset)
|
|
|
|
{
|
|
|
|
return twi_getabs(priv, priv->base + offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: twi_putrel
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write a value to a TWI register using an offset relative to the TWI base
|
|
|
|
* address.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline void twi_putrel(struct twi_dev_s *priv, unsigned int offset,
|
|
|
|
uint32_t value)
|
|
|
|
{
|
|
|
|
twi_putabs(priv, priv->base + offset, value);
|
|
|
|
}
|
2013-09-11 12:28:52 -06:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* I2C transfer helper functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*******************************************************************************
|
2013-09-11 16:48:56 -06:00
|
|
|
* Name: twi_wait
|
2013-09-11 12:28:52 -06:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Perform a I2C transfer start
|
|
|
|
*
|
2013-09-12 12:25:31 -06:00
|
|
|
* Assumptions:
|
|
|
|
* Interrupts are disabled
|
|
|
|
*
|
2013-09-11 12:28:52 -06:00
|
|
|
*******************************************************************************/
|
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
static int twi_wait(struct twi_dev_s *priv)
|
2013-09-11 12:28:52 -06:00
|
|
|
{
|
|
|
|
/* Start a timeout to avoid hangs */
|
|
|
|
|
|
|
|
wd_start(priv->timeout, TWI_TIMEOUT, twi_timeout, 1, (uint32_t)priv);
|
|
|
|
|
|
|
|
/* Wait for either the TWI transfer or the timeout to complete */
|
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
do
|
|
|
|
{
|
|
|
|
i2cvdbg("TWI%d Waiting...\n", priv->twi);
|
|
|
|
twi_takesem(&priv->waitsem);
|
|
|
|
i2cvdbg("TWI%d Awakened with result: %d\n", priv->twi, priv->result);
|
|
|
|
}
|
|
|
|
while (priv->result == -EBUSY);
|
2013-09-11 12:28:52 -06:00
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
/* We get here via twi_wakeup. The watchdog timer has been disabled and
|
|
|
|
* all further interrupts for the TWI have been disabled.
|
|
|
|
*/
|
2013-09-11 12:28:52 -06:00
|
|
|
|
|
|
|
return priv->result;
|
|
|
|
}
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
/*******************************************************************************
|
|
|
|
* Name: twi_wakeup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* A terminal event has occurred. Wake-up the waiting thread
|
|
|
|
*
|
|
|
|
*******************************************************************************/
|
|
|
|
|
2013-12-10 09:10:39 -06:00
|
|
|
static void twi_wakeup(struct twi_dev_s *priv, int result)
|
2013-09-12 12:25:31 -06:00
|
|
|
{
|
|
|
|
/* Cancel any pending timeout */
|
|
|
|
|
|
|
|
wd_cancel(priv->timeout);
|
|
|
|
|
|
|
|
/* Disable any further TWI interrupts */
|
|
|
|
|
|
|
|
twi_putrel(priv, SAM_TWI_IDR_OFFSET, TWI_INT_ALL);
|
|
|
|
|
|
|
|
/* Wake up the waiting thread with the result of the transfer */
|
|
|
|
|
|
|
|
priv->result = result;
|
|
|
|
twi_givesem(&priv->waitsem);
|
|
|
|
}
|
|
|
|
|
2013-09-11 12:28:52 -06:00
|
|
|
/*******************************************************************************
|
|
|
|
* Name: twi_interrupt
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The TWI Interrupt Handler
|
|
|
|
*
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
static int twi_interrupt(struct twi_dev_s *priv)
|
|
|
|
{
|
2013-09-11 17:52:23 -06:00
|
|
|
struct i2c_msg_s *msg;
|
2013-09-11 16:48:56 -06:00
|
|
|
uint32_t sr;
|
|
|
|
uint32_t imr;
|
|
|
|
uint32_t pending;
|
|
|
|
uint32_t regval;
|
|
|
|
|
|
|
|
/* Retrieve masked interrupt status */
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
sr = twi_getrel(priv, SAM_TWI_SR_OFFSET);
|
|
|
|
imr = twi_getrel(priv, SAM_TWI_IMR_OFFSET);
|
2013-09-11 16:48:56 -06:00
|
|
|
pending = sr & imr;
|
|
|
|
|
|
|
|
i2cllvdbg("TWI%d pending: %08x\n", priv->twi, pending);
|
2013-09-11 12:28:52 -06:00
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
/* Byte received */
|
|
|
|
|
2013-09-11 17:52:23 -06:00
|
|
|
msg = priv->msg;
|
2013-09-12 12:25:31 -06:00
|
|
|
if ((pending & TWI_INT_RXRDY) != 0)
|
2013-09-11 16:48:56 -06:00
|
|
|
{
|
2013-09-12 12:25:31 -06:00
|
|
|
msg->buffer[priv->xfrd] = twi_getrel(priv, SAM_TWI_RHR_OFFSET);
|
2013-09-11 16:48:56 -06:00
|
|
|
priv->xfrd++;
|
|
|
|
|
|
|
|
/* Check for transfer complete */
|
|
|
|
|
2013-09-11 17:52:23 -06:00
|
|
|
if (priv->xfrd >= msg->length)
|
2013-09-11 16:48:56 -06:00
|
|
|
{
|
|
|
|
/* The transfer is complete. Disable the RXRDY interrupt and
|
|
|
|
* enable the TXCOMP interrupt
|
|
|
|
*/
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
twi_putrel(priv, SAM_TWI_IDR_OFFSET, TWI_INT_RXRDY);
|
|
|
|
twi_putrel(priv, SAM_TWI_IER_OFFSET, TWI_INT_TXCOMP);
|
2013-09-11 16:48:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Not yet complete, but will the next be the last byte? */
|
|
|
|
|
2013-09-11 17:52:23 -06:00
|
|
|
else if (priv->xfrd == (msg->length - 1))
|
2013-09-11 16:48:56 -06:00
|
|
|
{
|
|
|
|
/* Yes, set the stop signal */
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
twi_putrel(priv, SAM_TWI_CR_OFFSET, TWI_CR_STOP);
|
2013-09-11 16:48:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Byte sent*/
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
else if ((pending & TWI_INT_TXRDY) != 0)
|
2013-09-11 16:48:56 -06:00
|
|
|
{
|
|
|
|
/* Transfer finished? */
|
|
|
|
|
2013-09-11 17:52:23 -06:00
|
|
|
if (priv->xfrd >= msg->length)
|
2013-09-11 16:48:56 -06:00
|
|
|
{
|
|
|
|
/* The transfer is complete. Disable the TXRDY interrupt and
|
|
|
|
* enable the TXCOMP interrupt
|
|
|
|
*/
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
twi_putrel(priv, SAM_TWI_IDR_OFFSET, TWI_INT_TXRDY);
|
|
|
|
twi_putrel(priv, SAM_TWI_IER_OFFSET, TWI_INT_TXCOMP);
|
2013-09-11 16:48:56 -06:00
|
|
|
|
|
|
|
/* Send the STOP condition */
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
regval = twi_getrel(priv, SAM_TWI_CR_OFFSET);
|
2013-09-11 16:48:56 -06:00
|
|
|
regval |= TWI_CR_STOP;
|
2013-09-12 12:25:31 -06:00
|
|
|
twi_putrel(priv, SAM_TWI_CR_OFFSET, regval);
|
2013-09-11 16:48:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* No, there are more bytes remaining to be sent */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2013-09-12 12:25:31 -06:00
|
|
|
twi_putrel(priv, SAM_TWI_THR_OFFSET, msg->buffer[priv->xfrd]);
|
2013-09-11 16:48:56 -06:00
|
|
|
priv->xfrd++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Transfer complete */
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
else if ((pending & TWI_INT_TXCOMP) != 0)
|
2013-09-11 16:48:56 -06:00
|
|
|
{
|
2013-09-12 12:25:31 -06:00
|
|
|
twi_putrel(priv, SAM_TWI_IDR_OFFSET, TWI_INT_TXCOMP);
|
2013-09-11 17:52:23 -06:00
|
|
|
|
2014-04-16 12:09:55 -06:00
|
|
|
/* Is there another message to send? */
|
2013-09-11 16:48:56 -06:00
|
|
|
|
2013-09-11 17:52:23 -06:00
|
|
|
if (priv->msgc > 1)
|
|
|
|
{
|
|
|
|
/* Yes... start the next message */
|
|
|
|
|
|
|
|
priv->msg++;
|
|
|
|
priv->msgc--;
|
2013-09-12 12:25:31 -06:00
|
|
|
twi_startmessage(priv, priv->msg);
|
2013-09-11 17:52:23 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No.. we made it to the end of the message list with no errors.
|
2013-09-12 12:25:31 -06:00
|
|
|
* Cancel any timeout and wake up the waiting thread with a
|
|
|
|
* success indication.
|
2013-09-11 17:52:23 -06:00
|
|
|
*/
|
2013-09-11 16:48:56 -06:00
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
twi_wakeup(priv, OK);
|
2013-09-11 17:52:23 -06:00
|
|
|
}
|
2013-09-11 16:48:56 -06:00
|
|
|
}
|
2013-09-11 12:28:52 -06:00
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
/* Check for errors */
|
|
|
|
|
|
|
|
else if ((pending & TWI_INT_ERRORS) != 0)
|
|
|
|
{
|
|
|
|
/* Wake up the thread with an I/O error indication */
|
|
|
|
|
|
|
|
i2clldbg("ERROR: TWI%d pending: %08x\n", priv->twi, pending);
|
|
|
|
twi_wakeup(priv, -EIO);
|
|
|
|
}
|
|
|
|
|
2013-09-11 12:28:52 -06:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_SAMA5_TWI0
|
|
|
|
static int twi0_interrupt(int irq, FAR void *context)
|
|
|
|
{
|
|
|
|
return twi_interrupt(&g_twi0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_SAMA5_TWI1
|
|
|
|
static int twi1_interrupt(int irq, FAR void *context)
|
|
|
|
{
|
|
|
|
return twi_interrupt(&g_twi1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_SAMA5_TWI2
|
|
|
|
static int twi2_interrupt(int irq, FAR void *context)
|
|
|
|
{
|
|
|
|
return twi_interrupt(&g_twi2);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Name: twi_timeout
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Watchdog timer for timeout of TWI operation
|
|
|
|
*
|
2013-09-11 16:48:56 -06:00
|
|
|
* Assumptions:
|
|
|
|
* Called from the timer interrupt handler with interrupts disabled.
|
|
|
|
*
|
2013-09-11 12:28:52 -06:00
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
static void twi_timeout(int argc, uint32_t arg, ...)
|
|
|
|
{
|
2013-09-11 16:48:56 -06:00
|
|
|
struct twi_dev_s *priv = (struct twi_dev_s *)arg;
|
2013-09-11 12:28:52 -06:00
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
i2clldbg("TWI%d Timeout!\n", priv->twi);
|
2013-09-12 12:25:31 -06:00
|
|
|
twi_wakeup(priv, -ETIMEDOUT);
|
2013-09-11 12:28:52 -06:00
|
|
|
}
|
|
|
|
|
2013-09-11 17:52:23 -06:00
|
|
|
/*******************************************************************************
|
|
|
|
* Name: twi_startread
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Start the next read message
|
|
|
|
*
|
|
|
|
*******************************************************************************/
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
static void twi_startread(struct twi_dev_s *priv, struct i2c_msg_s *msg)
|
2013-09-11 17:52:23 -06:00
|
|
|
{
|
|
|
|
/* Setup for the transfer */
|
|
|
|
|
|
|
|
priv->result = -EBUSY;
|
|
|
|
priv->xfrd = 0;
|
|
|
|
|
|
|
|
/* Set STOP signal if only one byte is sent*/
|
|
|
|
|
|
|
|
if (msg->length == 1)
|
|
|
|
{
|
2013-09-12 12:25:31 -06:00
|
|
|
twi_putrel(priv, SAM_TWI_CR_OFFSET, TWI_CR_STOP);
|
2013-09-11 17:52:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set slave address and number of internal address bytes. */
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
twi_putrel(priv, SAM_TWI_MMR_OFFSET, 0);
|
|
|
|
twi_putrel(priv, SAM_TWI_MMR_OFFSET, TWI_MMR_IADRSZ_NONE | TWI_MMR_MREAD | TWI_MMR_DADR(msg->addr));
|
2013-09-11 17:52:23 -06:00
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
/* Set internal address bytes (not used) */
|
2013-09-11 17:52:23 -06:00
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
twi_putrel(priv, SAM_TWI_IADR_OFFSET, 0);
|
2013-09-11 17:52:23 -06:00
|
|
|
|
|
|
|
/* Enable read interrupt and send the START condition */
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
twi_putrel(priv, SAM_TWI_IER_OFFSET, TWI_INT_RXRDY | TWI_INT_ERRORS);
|
|
|
|
twi_putrel(priv, SAM_TWI_CR_OFFSET, TWI_CR_START);
|
2013-09-11 17:52:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Name: twi_startwrite
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Start the next write message
|
|
|
|
*
|
|
|
|
*******************************************************************************/
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
static void twi_startwrite(struct twi_dev_s *priv, struct i2c_msg_s *msg)
|
2013-09-11 17:52:23 -06:00
|
|
|
{
|
|
|
|
/* Setup for the transfer */
|
|
|
|
|
|
|
|
priv->result = -EBUSY;
|
|
|
|
priv->xfrd = 0;
|
|
|
|
|
|
|
|
/* Set slave address and number of internal address bytes. */
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
twi_putrel(priv, SAM_TWI_MMR_OFFSET, 0);
|
|
|
|
twi_putrel(priv, SAM_TWI_MMR_OFFSET, TWI_MMR_IADRSZ_NONE | TWI_MMR_DADR(msg->addr));
|
2013-09-11 17:52:23 -06:00
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
/* Set internal address bytes (not used) */
|
2013-09-11 17:52:23 -06:00
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
twi_putrel(priv, SAM_TWI_IADR_OFFSET, 0);
|
2013-09-11 17:52:23 -06:00
|
|
|
|
|
|
|
/* Write first byte to send.*/
|
|
|
|
|
2014-04-10 18:19:03 -06:00
|
|
|
twi_putrel(priv, SAM_TWI_THR_OFFSET, msg->buffer[priv->xfrd++]);
|
2013-09-11 17:52:23 -06:00
|
|
|
|
|
|
|
/* Enable write interrupt */
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
twi_putrel(priv, SAM_TWI_IER_OFFSET, TWI_INT_TXRDY | TWI_INT_ERRORS);
|
2013-09-11 17:52:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Name: twi_startmessage
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Start the next write message
|
|
|
|
*
|
|
|
|
*******************************************************************************/
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
static void twi_startmessage(struct twi_dev_s *priv, struct i2c_msg_s *msg)
|
2013-09-11 17:52:23 -06:00
|
|
|
{
|
|
|
|
if ((msg->flags & I2C_M_READ) == 0)
|
|
|
|
{
|
2013-09-12 12:25:31 -06:00
|
|
|
twi_startread(priv, msg);
|
2013-09-11 17:52:23 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-12 12:25:31 -06:00
|
|
|
twi_startwrite(priv, msg);
|
2013-09-11 17:52:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-11 12:28:52 -06:00
|
|
|
/*******************************************************************************
|
|
|
|
* I2C device operations
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Name: twi_setfrequency
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set the frequence for the next transfer
|
|
|
|
*
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
static uint32_t twi_setfrequency(FAR struct i2c_dev_s *dev, uint32_t frequency)
|
|
|
|
{
|
2013-09-11 16:48:56 -06:00
|
|
|
struct twi_dev_s *priv = (struct twi_dev_s *)dev;
|
|
|
|
uint32_t actual;
|
2013-09-11 12:28:52 -06:00
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
DEBUGASSERT(dev);
|
2013-09-11 12:28:52 -06:00
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
/* Get exclusive access to the device */
|
2013-09-11 12:28:52 -06:00
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
twi_takesem(&priv->exclsem);
|
|
|
|
|
|
|
|
/* And setup the clock frequency */
|
|
|
|
|
|
|
|
actual = twi_hw_setfrequency(priv, frequency);
|
|
|
|
twi_givesem(&priv->exclsem);
|
|
|
|
return actual;
|
2013-09-11 12:28:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Name: twi_setaddress
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set the I2C slave address for a subsequent read/write
|
|
|
|
*
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
static int twi_setaddress(FAR struct i2c_dev_s *dev, int addr, int nbits)
|
|
|
|
{
|
|
|
|
struct twi_dev_s *priv = (struct twi_dev_s *) dev;
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
i2cvdbg("TWI%d address: %02x nbits: %d\n", priv->twi, addr, nbits);
|
2013-09-11 16:48:56 -06:00
|
|
|
DEBUGASSERT(dev != NULL && nbits == 7);
|
|
|
|
|
|
|
|
/* Get exclusive access to the device */
|
|
|
|
|
|
|
|
twi_takesem(&priv->exclsem);
|
|
|
|
|
2013-09-12 09:46:20 -06:00
|
|
|
/* Remember 7- or 10-bit address */
|
2013-09-11 12:28:52 -06:00
|
|
|
|
2013-09-11 17:52:23 -06:00
|
|
|
priv->address = addr;
|
|
|
|
priv->flags = (nbits == 10) ? I2C_M_TEN : 0;
|
2013-09-11 12:28:52 -06:00
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
twi_givesem(&priv->exclsem);
|
2013-09-11 12:28:52 -06:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Name: twi_write
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Send a block of data on I2C using the previously selected I2C
|
|
|
|
* frequency and slave address.
|
|
|
|
*
|
|
|
|
*******************************************************************************/
|
|
|
|
|
2013-09-12 09:46:20 -06:00
|
|
|
static int twi_write(FAR struct i2c_dev_s *dev, const uint8_t *wbuffer, int wbuflen)
|
2013-09-11 12:28:52 -06:00
|
|
|
{
|
|
|
|
struct twi_dev_s *priv = (struct twi_dev_s *) dev;
|
2013-09-12 12:25:31 -06:00
|
|
|
irqstate_t flags;
|
2013-09-11 12:28:52 -06:00
|
|
|
int ret;
|
|
|
|
|
2013-09-11 17:52:23 -06:00
|
|
|
struct i2c_msg_s msg =
|
|
|
|
{
|
|
|
|
.addr = priv->address,
|
|
|
|
.flags = priv->flags,
|
2013-09-12 09:46:20 -06:00
|
|
|
.buffer = (uint8_t *)wbuffer, /* Override const */
|
|
|
|
.length = wbuflen
|
2013-09-11 17:52:23 -06:00
|
|
|
};
|
|
|
|
|
2013-09-12 09:46:20 -06:00
|
|
|
i2cvdbg("TWI%d buflen: %d\n", priv->twi, wbuflen);
|
2013-09-11 12:28:52 -06:00
|
|
|
DEBUGASSERT(dev != NULL);
|
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
/* Get exclusive access to the device */
|
|
|
|
|
|
|
|
twi_takesem(&priv->exclsem);
|
|
|
|
|
2013-09-11 17:52:23 -06:00
|
|
|
/* Initiate the wrte */
|
2013-09-11 16:48:56 -06:00
|
|
|
|
2013-09-11 17:52:23 -06:00
|
|
|
priv->msg = &msg;
|
|
|
|
priv->msgc = 1;
|
2013-09-11 16:48:56 -06:00
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
/* Initiate the write operation. The rest will be handled from interrupt
|
|
|
|
* logic. Interrupts must be disabled to prevent re-entrance from the
|
|
|
|
* interrupt level.
|
|
|
|
*/
|
2013-09-11 16:48:56 -06:00
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
flags = irqsave();
|
|
|
|
twi_startwrite(priv, &msg);
|
|
|
|
|
|
|
|
/* And wait for the write to complete. Interrupts will be re-enabled while
|
|
|
|
* we are waiting.
|
|
|
|
*/
|
2013-09-11 12:28:52 -06:00
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
ret = twi_wait(priv);
|
2013-09-12 12:25:31 -06:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
i2cdbg("ERROR: Transfer failed: %d\n", ret);
|
|
|
|
}
|
2013-09-11 16:48:56 -06:00
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
irqrestore(flags);
|
2013-09-11 16:48:56 -06:00
|
|
|
twi_givesem(&priv->exclsem);
|
|
|
|
return ret;
|
2013-09-11 12:28:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Name: twi_read
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Receive a block of data on I2C using the previously selected I2C
|
|
|
|
* frequency and slave address.
|
|
|
|
*
|
|
|
|
*******************************************************************************/
|
|
|
|
|
2013-09-12 09:46:20 -06:00
|
|
|
static int twi_read(FAR struct i2c_dev_s *dev, uint8_t *rbuffer, int rbuflen)
|
2013-09-11 12:28:52 -06:00
|
|
|
{
|
2013-09-11 17:52:23 -06:00
|
|
|
struct twi_dev_s *priv = (struct twi_dev_s *)dev;
|
2013-09-12 12:25:31 -06:00
|
|
|
irqstate_t flags;
|
2013-09-11 12:28:52 -06:00
|
|
|
int ret;
|
|
|
|
|
2013-09-11 17:52:23 -06:00
|
|
|
struct i2c_msg_s msg =
|
|
|
|
{
|
|
|
|
.addr = priv->address,
|
|
|
|
.flags = priv->flags | I2C_M_READ,
|
2013-09-12 09:46:20 -06:00
|
|
|
.buffer = rbuffer,
|
|
|
|
.length = rbuflen
|
2013-09-11 17:52:23 -06:00
|
|
|
};
|
|
|
|
|
2013-09-11 12:28:52 -06:00
|
|
|
DEBUGASSERT(dev != NULL);
|
2013-09-12 09:46:20 -06:00
|
|
|
i2cvdbg("TWI%d rbuflen: %d\n", priv->twi, rbuflen);
|
2013-09-11 12:28:52 -06:00
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
/* Get exclusive access to the device */
|
|
|
|
|
|
|
|
twi_takesem(&priv->exclsem);
|
|
|
|
|
2013-09-11 17:52:23 -06:00
|
|
|
/* Initiate the read */
|
2013-09-11 16:48:56 -06:00
|
|
|
|
2013-09-11 17:52:23 -06:00
|
|
|
priv->msg = &msg;
|
|
|
|
priv->msgc = 1;
|
2013-09-11 16:48:56 -06:00
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
/* Initiate the read operation. The rest will be handled from interrupt
|
|
|
|
* logic. Interrupts must be disabled to prevent re-entrance from the
|
|
|
|
* interrupt level.
|
|
|
|
*/
|
2013-09-11 16:48:56 -06:00
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
flags = irqsave();
|
|
|
|
twi_startread(priv, &msg);
|
|
|
|
|
|
|
|
/* And wait for the read to complete. Interrupts will be re-enabled while
|
|
|
|
* we are waiting.
|
|
|
|
*/
|
2013-09-11 16:48:56 -06:00
|
|
|
|
|
|
|
ret = twi_wait(priv);
|
2013-09-12 12:25:31 -06:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
i2cdbg("ERROR: Transfer failed: %d\n", ret);
|
|
|
|
}
|
2013-09-11 16:48:56 -06:00
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
irqrestore(flags);
|
2013-09-11 16:48:56 -06:00
|
|
|
twi_givesem(&priv->exclsem);
|
|
|
|
return ret;
|
2013-09-11 12:28:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Name: twi_writeread
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_I2C_WRITEREAD
|
2013-09-11 17:52:23 -06:00
|
|
|
static int twi_writeread(FAR struct i2c_dev_s *dev, const uint8_t *wbuffer,
|
|
|
|
int wbuflen, uint8_t *rbuffer, int rbuflen)
|
2013-09-11 12:28:52 -06:00
|
|
|
{
|
2013-09-11 17:52:23 -06:00
|
|
|
struct twi_dev_s *priv = (struct twi_dev_s *)dev;
|
2013-09-12 12:25:31 -06:00
|
|
|
irqstate_t flags;
|
2013-09-11 17:52:23 -06:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
struct i2c_msg_s msgv[2] =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
.addr = priv->address,
|
|
|
|
.flags = priv->flags,
|
|
|
|
.buffer = (uint8_t *)wbuffer, /* Override const */
|
|
|
|
.length = wbuflen
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.addr = priv->address,
|
|
|
|
.flags = priv->flags | ((rbuflen > 0) ? I2C_M_READ : I2C_M_NORESTART),
|
|
|
|
.buffer = rbuffer,
|
|
|
|
.length = (rbuflen < 0) ? -rbuflen : rbuflen
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DEBUGASSERT(dev != NULL);
|
2013-09-12 09:46:20 -06:00
|
|
|
i2cvdbg("TWI%d wbuflen: %d rbuflen: %d\n", priv->twi, wbuflen, rbuflen);
|
2013-09-11 17:52:23 -06:00
|
|
|
|
|
|
|
/* Get exclusive access to the device */
|
|
|
|
|
|
|
|
twi_takesem(&priv->exclsem);
|
|
|
|
|
|
|
|
/* Initiate the read */
|
|
|
|
|
|
|
|
priv->msg = msgv;
|
|
|
|
priv->msgc = 2;
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
/* Initiate the write operation. The rest will be handled from interrupt
|
|
|
|
* logic. Interrupts must be disabled to prevent re-entrance from the
|
|
|
|
* interrupt level.
|
|
|
|
*/
|
2013-09-11 17:52:23 -06:00
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
flags = irqsave();
|
|
|
|
twi_startwrite(priv, msgv);
|
|
|
|
|
|
|
|
/* And wait for the write/read to complete. Interrupts will be re-enabled
|
|
|
|
* while we are waiting.
|
|
|
|
*/
|
2013-09-11 17:52:23 -06:00
|
|
|
|
|
|
|
ret = twi_wait(priv);
|
2013-09-12 12:25:31 -06:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
i2cdbg("ERROR: Transfer failed: %d\n", ret);
|
|
|
|
}
|
2013-09-11 17:52:23 -06:00
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
irqrestore(flags);
|
2013-09-11 17:52:23 -06:00
|
|
|
twi_givesem(&priv->exclsem);
|
|
|
|
return ret;
|
2013-09-11 12:28:52 -06:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Name: twi_setownaddress
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_I2C_SLAVE
|
|
|
|
static int twi_setownaddress(FAR struct i2c_dev_s *dev, int addr, int nbits)
|
|
|
|
{
|
|
|
|
#error Not implemented
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Name: twi_registercallback
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_I2C_SLAVE
|
|
|
|
static int twi_registercallback(FAR struct i2c_dev_s *dev,
|
|
|
|
int (*callback)(void))
|
|
|
|
{
|
|
|
|
#error Not implemented
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Name: twi_transfer
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Receive a block of data on I2C using the previously selected I2C
|
|
|
|
* frequency and slave address.
|
|
|
|
*
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_I2C_TRANSFER
|
|
|
|
static int twi_transfer(FAR struct i2c_dev_s *dev,
|
|
|
|
FAR struct i2c_msg_s *msgs, int count)
|
|
|
|
{
|
2013-09-11 17:52:23 -06:00
|
|
|
struct twi_dev_s *priv = (struct twi_dev_s *)dev;
|
2013-09-12 12:25:31 -06:00
|
|
|
irqstate_t flags;
|
2013-09-11 17:52:23 -06:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(dev != NULL);
|
2013-09-12 09:46:20 -06:00
|
|
|
i2cvdbg("TWI%d count: %d\n", priv->twi, count);
|
2013-09-11 17:52:23 -06:00
|
|
|
|
|
|
|
/* Get exclusive access to the device */
|
|
|
|
|
|
|
|
twi_takesem(&priv->exclsem);
|
|
|
|
|
|
|
|
/* Initiate the message transfer */
|
|
|
|
|
|
|
|
priv->msg = msgs;
|
|
|
|
priv->msgc = count;
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
/* Initiate the transfer. The rest will be handled from interrupt
|
|
|
|
* logic. Interrupts must be disabled to prevent re-entrance from the
|
|
|
|
* interrupt level.
|
|
|
|
*/
|
2013-09-11 17:52:23 -06:00
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
flags = irqsave();
|
|
|
|
twi_startmessage(priv, msgs);
|
|
|
|
|
|
|
|
/* And wait for the transfers to complete. Interrupts will be re-enabled
|
|
|
|
* while we are waiting.
|
|
|
|
*/
|
2013-09-11 17:52:23 -06:00
|
|
|
|
|
|
|
ret = twi_wait(priv);
|
2013-09-12 12:25:31 -06:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
i2cdbg("ERROR: Transfer failed: %d\n", ret);
|
|
|
|
}
|
2013-09-11 17:52:23 -06:00
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
irqrestore(flags);
|
2013-09-11 17:52:23 -06:00
|
|
|
twi_givesem(&priv->exclsem);
|
|
|
|
return ret;
|
2013-09-11 12:28:52 -06:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
/*******************************************************************************
|
|
|
|
* Initialization
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Name: twi_hw_setfrequency
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set the frequence for the next transfer
|
|
|
|
*
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
static uint32_t twi_hw_setfrequency(struct twi_dev_s *priv, uint32_t frequency)
|
|
|
|
{
|
|
|
|
unsigned int ckdiv;
|
|
|
|
unsigned int cldiv;
|
|
|
|
uint32_t actual;
|
|
|
|
uint32_t regval;
|
|
|
|
|
|
|
|
/* Configure TWI output clocking, trying each value of CKDIV {0..7} */
|
|
|
|
|
|
|
|
for (ckdiv = 0; ckdiv < 8; ckdiv++)
|
|
|
|
{
|
|
|
|
/* Calulate the CLDIV value using the current CKDIV guess */
|
|
|
|
|
2014-04-16 12:09:55 -06:00
|
|
|
cldiv = ((priv->frequency / (frequency << 1)) - 4) / (1 << ckdiv);
|
2013-09-11 16:48:56 -06:00
|
|
|
|
|
|
|
/* Is CLDIV in range? */
|
|
|
|
|
|
|
|
if (cldiv <= 255)
|
|
|
|
{
|
|
|
|
/* Yes, break out and use it */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then setup the TWI Clock Waveform Generator Register, using the same
|
|
|
|
* value for CLDIV and CHDIV (for 1:1 duty).
|
|
|
|
*/
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
twi_putrel(priv, SAM_TWI_CWGR_OFFSET, 0);
|
2013-09-11 16:48:56 -06:00
|
|
|
|
|
|
|
regval = ((uint32_t)ckdiv << TWI_CWGR_CKDIV_SHIFT) |
|
|
|
|
((uint32_t)cldiv << TWI_CWGR_CHDIV_SHIFT) |
|
|
|
|
((uint32_t)cldiv << TWI_CWGR_CLDIV_SHIFT);
|
2013-09-12 12:25:31 -06:00
|
|
|
twi_putrel(priv, SAM_TWI_CWGR_OFFSET, regval);
|
2013-09-11 16:48:56 -06:00
|
|
|
|
|
|
|
/* Return the actual frequency */
|
|
|
|
|
2014-04-16 12:09:55 -06:00
|
|
|
actual = (priv->frequency / 2) / (((1 << ckdiv) * cldiv) + 2);
|
2013-09-11 16:48:56 -06:00
|
|
|
i2cvdbg("TWI%d frequency: %d ckdiv: %d cldiv: %d actual: %d\n",
|
|
|
|
priv->twi, frequency, ckdiv, cldiv, actual);
|
|
|
|
|
|
|
|
return actual;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Name: twi_hw_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize one TWI peripheral for I2C operation
|
|
|
|
*
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
static void twi_hw_initialize(struct twi_dev_s *priv, unsigned int pid,
|
|
|
|
uint32_t frequency)
|
|
|
|
{
|
|
|
|
uint32_t regval;
|
2014-04-16 12:09:55 -06:00
|
|
|
uint32_t mck;
|
2013-09-11 16:48:56 -06:00
|
|
|
|
2013-09-12 09:46:20 -06:00
|
|
|
i2cvdbg("TWI%d Initializing\n", priv->twi);
|
2013-09-11 16:48:56 -06:00
|
|
|
|
|
|
|
/* SVEN: TWI Slave Mode Enabled */
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
twi_putrel(priv, SAM_TWI_CR_OFFSET, TWI_CR_SVEN);
|
2013-09-11 16:48:56 -06:00
|
|
|
|
|
|
|
/* Reset the TWI */
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
twi_putrel(priv, SAM_TWI_CR_OFFSET, TWI_CR_SWRST);
|
|
|
|
(void)twi_getrel(priv, SAM_TWI_RHR_OFFSET);
|
2013-09-11 16:48:56 -06:00
|
|
|
|
|
|
|
/* TWI Slave Mode Disabled, TWI Master Mode Disabled. */
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
twi_putrel(priv, SAM_TWI_CR_OFFSET, TWI_CR_SVDIS);
|
|
|
|
twi_putrel(priv, SAM_TWI_CR_OFFSET, TWI_CR_MSDIS);
|
2013-09-11 16:48:56 -06:00
|
|
|
|
|
|
|
/* Set master mode */
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
twi_putrel(priv, SAM_TWI_CR_OFFSET, TWI_CR_MSEN);
|
2013-09-11 16:48:56 -06:00
|
|
|
|
2014-04-16 12:09:55 -06:00
|
|
|
/* Determine the maximum valid frequency setting */
|
|
|
|
|
|
|
|
mck = BOARD_MCK_FREQUENCY;
|
|
|
|
DEBUGASSERT((mck >> 3) <= TWI_MAX_FREQUENCY);
|
|
|
|
|
|
|
|
if (mck <= TWI_MAX_FREQUENCY)
|
|
|
|
{
|
|
|
|
priv->frequency = mck;
|
|
|
|
regval = PMC_PCR_DIV1;
|
|
|
|
}
|
|
|
|
else if ((mck >> 1) <= TWI_MAX_FREQUENCY)
|
|
|
|
{
|
|
|
|
priv->frequency = (mck >> 1);
|
|
|
|
regval = PMC_PCR_DIV2;
|
|
|
|
}
|
|
|
|
else if ((mck >> 2) <= TWI_MAX_FREQUENCY)
|
|
|
|
{
|
|
|
|
priv->frequency = (mck >> 2);
|
|
|
|
regval = PMC_PCR_DIV4;
|
|
|
|
}
|
|
|
|
else /* if ((mck >> 3) <= TWI_MAX_FREQUENCY) */
|
|
|
|
{
|
|
|
|
priv->frequency = (mck >> 3);
|
|
|
|
regval = PMC_PCR_DIV8;
|
|
|
|
}
|
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
/* Set the TWI peripheral input clock to the maximum, valid frequency */
|
|
|
|
|
2014-04-16 12:09:55 -06:00
|
|
|
regval |= PMC_PCR_PID(pid) | PMC_PCR_CMD | PMC_PCR_EN;
|
2013-09-12 12:25:31 -06:00
|
|
|
twi_putabs(priv, SAM_PMC_PCR, regval);
|
2013-09-11 16:48:56 -06:00
|
|
|
|
|
|
|
/* Set the initial TWI data transfer frequency */
|
|
|
|
|
|
|
|
(void)twi_hw_setfrequency(priv, frequency);
|
|
|
|
}
|
|
|
|
|
2013-09-11 12:28:52 -06:00
|
|
|
/*******************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Name: up_i2cinitialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize a TWI device for I2C operation
|
|
|
|
*
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
struct i2c_dev_s *up_i2cinitialize(int bus)
|
|
|
|
{
|
|
|
|
struct twi_dev_s *priv;
|
|
|
|
xcpt_t handler;
|
|
|
|
irqstate_t flags;
|
2013-09-11 16:48:56 -06:00
|
|
|
uint32_t frequency;
|
|
|
|
unsigned int pid;
|
|
|
|
|
2013-09-12 12:25:31 -06:00
|
|
|
i2cvdbg("Initializing TWI%d\n", bus);
|
2013-09-11 12:28:52 -06:00
|
|
|
|
|
|
|
flags = irqsave();
|
|
|
|
|
|
|
|
#ifdef CONFIG_SAMA5_TWI0
|
|
|
|
if (bus == 0)
|
|
|
|
{
|
2013-09-11 16:48:56 -06:00
|
|
|
/* Set up TWI2 register base address and IRQ number */
|
|
|
|
|
2013-09-11 12:28:52 -06:00
|
|
|
priv = &g_twi0;
|
|
|
|
priv->base = SAM_TWI0_VBASE;
|
|
|
|
priv->irq = SAM_IRQ_TWI0;
|
2013-09-11 16:48:56 -06:00
|
|
|
priv->twi = 0;
|
2013-09-11 12:28:52 -06:00
|
|
|
|
|
|
|
/* Enable peripheral clocking */
|
|
|
|
|
|
|
|
sam_twi0_enableclk();
|
|
|
|
|
|
|
|
/* Configure PIO pins */
|
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
sam_configpio(PIO_TWI0_CK);
|
|
|
|
sam_configpio(PIO_TWI0_D);
|
2013-09-11 12:28:52 -06:00
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
/* Select the interrupt handler, TWI frequency, and peripheral ID */
|
2013-09-11 12:28:52 -06:00
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
handler = twi0_interrupt;
|
|
|
|
frequency = CONFIG_SAMA5_TWI0_FREQUENCY;
|
|
|
|
pid = SAM_PID_TWI0;
|
2013-09-11 12:28:52 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SAMA5_TWI1
|
|
|
|
if (bus == 1)
|
|
|
|
{
|
2013-09-11 16:48:56 -06:00
|
|
|
/* Set up TWI1 register base address and IRQ number */
|
|
|
|
|
2013-09-11 12:28:52 -06:00
|
|
|
priv = &g_twi1;
|
|
|
|
priv->base = SAM_TWI1_VBASE;
|
|
|
|
priv->irq = SAM_IRQ_TWI1;
|
2013-09-11 16:48:56 -06:00
|
|
|
priv->twi = 1;
|
2013-09-11 12:28:52 -06:00
|
|
|
|
|
|
|
/* Enable peripheral clocking */
|
|
|
|
|
|
|
|
sam_twi1_enableclk();
|
|
|
|
|
|
|
|
/* Configure PIO pins */
|
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
sam_configpio(PIO_TWI1_CK);
|
|
|
|
sam_configpio(PIO_TWI1_D);
|
2013-09-11 12:28:52 -06:00
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
/* Select the interrupt handler, TWI frequency, and peripheral ID */
|
2013-09-11 12:28:52 -06:00
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
handler = twi1_interrupt;
|
|
|
|
frequency = CONFIG_SAMA5_TWI1_FREQUENCY;
|
|
|
|
pid = SAM_PID_TWI1;
|
2013-09-11 12:28:52 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SAMA5_TWI2
|
|
|
|
if (bus == 2)
|
|
|
|
{
|
2013-09-11 16:48:56 -06:00
|
|
|
/* Set up TWI2 register base address and IRQ number */
|
|
|
|
|
2013-09-11 12:28:52 -06:00
|
|
|
priv = &g_twi2;
|
|
|
|
priv->base = SAM_TWI2_VBASE;
|
|
|
|
priv->irq = SAM_IRQ_TWI2;
|
2013-09-11 16:48:56 -06:00
|
|
|
priv->twi = 2;
|
2013-09-11 12:28:52 -06:00
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
/* Configure PIO pins */
|
2013-09-11 12:28:52 -06:00
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
sam_configpio(PIO_TWI2_CK);
|
|
|
|
sam_configpio(PIO_TWI2_D);
|
2013-09-11 12:28:52 -06:00
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
/* Enable peripheral clocking */
|
2013-09-11 12:28:52 -06:00
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
sam_twi2_enableclk();
|
2013-09-11 12:28:52 -06:00
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
/* Select the interrupt handler, TWI frequency, and peripheral ID */
|
2013-09-11 12:28:52 -06:00
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
handler = twi2_interrupt;
|
|
|
|
frequency = CONFIG_SAMA5_TWI2_FREQUENCY;
|
|
|
|
pid = SAM_PID_TWI2;
|
2013-09-11 12:28:52 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
irqrestore(flags);
|
2013-09-12 12:25:31 -06:00
|
|
|
i2cdbg("ERROR: Unsupported bus: TWI%d\n", bus);
|
2013-09-11 12:28:52 -06:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the device structure */
|
|
|
|
|
|
|
|
priv->dev.ops = &g_twiops;
|
2013-09-11 17:52:23 -06:00
|
|
|
priv->address = 0;
|
|
|
|
priv->flags = 0;
|
|
|
|
|
2013-09-11 12:28:52 -06:00
|
|
|
sem_init(&priv->exclsem, 0, 1);
|
|
|
|
sem_init(&priv->waitsem, 0, 0);
|
|
|
|
|
|
|
|
/* Allocate a watchdog timer */
|
|
|
|
|
|
|
|
priv->timeout = wd_create();
|
|
|
|
DEBUGASSERT(priv->timeout != 0);
|
|
|
|
|
2013-09-11 16:48:56 -06:00
|
|
|
/* Configure and enable the TWI hardware */
|
|
|
|
|
|
|
|
twi_hw_initialize(priv, pid, frequency);
|
|
|
|
|
2013-09-11 12:28:52 -06:00
|
|
|
/* Attach Interrupt Handler */
|
|
|
|
|
|
|
|
irq_attach(priv->irq, handler);
|
|
|
|
|
|
|
|
/* Enable Interrupts */
|
|
|
|
|
|
|
|
up_enable_irq(priv->irq);
|
|
|
|
irqrestore(flags);
|
|
|
|
return &priv->dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Name: up_i2cuninitalize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Uninitialise an I2C device
|
|
|
|
*
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
int up_i2cuninitialize(FAR struct i2c_dev_s * dev)
|
|
|
|
{
|
|
|
|
struct twi_dev_s *priv = (struct twi_dev_s *) dev;
|
|
|
|
|
2013-09-12 09:46:20 -06:00
|
|
|
i2cvdbg("TWI%d Un-initializing\n", priv->twi);
|
2013-09-11 16:48:56 -06:00
|
|
|
|
|
|
|
/* Disable interrupts */
|
|
|
|
|
|
|
|
up_disable_irq(priv->irq);
|
2013-09-11 12:28:52 -06:00
|
|
|
|
|
|
|
/* Reset data structures */
|
|
|
|
|
|
|
|
sem_destroy(&priv->exclsem);
|
|
|
|
sem_destroy(&priv->waitsem);
|
|
|
|
|
|
|
|
/* Free the watchdog timer */
|
|
|
|
|
|
|
|
wd_delete(priv->timeout);
|
|
|
|
priv->timeout = NULL;
|
|
|
|
|
|
|
|
/* Detach Interrupt Handler */
|
|
|
|
|
|
|
|
irq_detach(priv->irq);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|