SERCOM I2C NACK on Address Match does not work

Discussions around product based on ARM Cortex M0+ core.

Moderator: nferre

mkwired
Posts: 4
Joined: Tue Feb 25, 2014 11:39 pm

SERCOM I2C NACK on Address Match does not work

Thu Jun 26, 2014 11:19 pm

I cannot get the SAM D20 to "send" a NACK on address match.  To demonstrate the problem, I took the "SAM SERCOM I2C Slave Quick Start Guide with Callbacks" example and modified it slightly to add the i2c_slave_enable_nack_on_address() and i2c_slave_disable_nack_on_address() function calls.  I posted that code below.  What I think should happen is that when a Master Write occurs on the SAM D20, future Master Writes and Reads will get a NACK on the first byte (the address) but that does not happen.  It ACKs.  Can someone see what I must be doing incorrectly and tell me how to fix it?  Thanks in advance.

Code: Select all

/**
 * \file
 *
 * \brief SAM SERCOM I2C Slave Quick Start Guide with Callbacks
 *
 * Copyright (C) 2013-2014 Atmel Corporation. All rights reserved.
 *
 * \asf_license_start
 *
 * \page License
 *
 * 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. The name of Atmel may not be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * 4. This software may only be redistributed and used in connection with an
 *    Atmel microcontroller product.
 *
 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL 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.
 *
 * \asf_license_stop
 *
 */

#include <asf.h>

void i2c_read_request_callback(struct i2c_slave_module *const module);
void i2c_write_request_callback(struct i2c_slave_module *const module);
void configure_i2c_slave(void);
void configure_i2c_slave_callbacks(void);

static struct i2c_slave_packet packet;

#define DATA_LENGTH 10
static uint8_t write_buffer[DATA_LENGTH] = {
		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
};
static uint8_t read_buffer [DATA_LENGTH];

/* Address of the slave */
#define SLAVE_ADDRESS 0x12

/* Init device instance. */
struct i2c_slave_module i2c_slave_instance;

bool custom_wait = false;

void i2c_read_request_callback(struct i2c_slave_module *const module)
{
   /* Init i2c packet. */
   packet.data_length = DATA_LENGTH;
   packet.data        = write_buffer;

   /* Write buffer to master */
   i2c_slave_write_packet_job(module, &packet);
}

void i2c_write_complete_callback(struct i2c_slave_module *const module)
{
   i2c_slave_enable_nack_on_address(module);
   custom_wait = true;
}

void i2c_write_request_callback(struct i2c_slave_module *const module)
{
   /* Init i2c packet. */
   packet.data_length = DATA_LENGTH;
   packet.data        = read_buffer;
   
   /* Read buffer from master */
   if (i2c_slave_read_packet_job(module, &packet) != STATUS_OK) {
   }
}

void configure_i2c_slave(void)
{
   /* Initialize config structure and module instance. */
   struct i2c_slave_config config_i2c_slave;
   i2c_slave_get_config_defaults(&config_i2c_slave);

   /* Change address and address_mode. */
   config_i2c_slave.address      = SLAVE_ADDRESS;
   config_i2c_slave.address_mode = I2C_SLAVE_ADDRESS_MODE_MASK;
   config_i2c_slave.pinmux_pad0  = PINMUX_PA22C_SERCOM3_PAD0;
   config_i2c_slave.pinmux_pad1  = PINMUX_PA23C_SERCOM3_PAD1;

   /* Initialize and enable device with config. */
   i2c_slave_init(&i2c_slave_instance, SERCOM3, &config_i2c_slave);

   i2c_slave_enable(&i2c_slave_instance);
}

void configure_i2c_slave_callbacks(void)
{
   /* Register and enable callback functions */
   i2c_slave_register_callback(&i2c_slave_instance, i2c_read_request_callback, I2C_SLAVE_CALLBACK_READ_REQUEST);
	i2c_slave_enable_callback(&i2c_slave_instance, I2C_SLAVE_CALLBACK_READ_REQUEST);

	i2c_slave_register_callback(&i2c_slave_instance, i2c_write_request_callback, I2C_SLAVE_CALLBACK_WRITE_REQUEST);
	i2c_slave_enable_callback(&i2c_slave_instance, I2C_SLAVE_CALLBACK_WRITE_REQUEST);
   
   i2c_slave_register_callback(&i2c_slave_instance, i2c_write_complete_callback, I2C_SLAVE_CALLBACK_WRITE_COMPLETE);
   i2c_slave_enable_callback(&i2c_slave_instance, I2C_SLAVE_CALLBACK_WRITE_COMPLETE);
}

int main(void)
{
   system_init();

   /* Configure device and enable. */
   configure_i2c_slave();
   configure_i2c_slave_callbacks();

   while (true) {
      /* Infinite loop while waiting for I2C master interaction */
      if (!custom_wait)
         continue;
      delay_ms(10);
      i2c_slave_disable_nack_on_address(&i2c_slave_instance);
      custom_wait = false;
   }
}
mkwired
Posts: 4
Joined: Tue Feb 25, 2014 11:39 pm

Re: SERCOM I2C NACK on Address Match does not work

Mon Jun 30, 2014 8:11 pm

I believe I found the problem.  The ASF file i2c_slave_interrupt.c has a line where it states it "ACK next incoming packet".  I don't know why that is there.  If I comment it out, the problem goes away.

Snippet from ..\src\ASF\sam0\drivers\sercom\i2c\i2c_samd20\i2c_slave_interrupt.c starting at line 352

Code: Select all

/* ACK next incoming packet */
i2c_hw->CTRLB.reg &= ~SERCOM_I2CS_CTRLB_ACKACT;
Example code

Code: Select all

/**
 * \file
 *
 * \brief SAM SERCOM I2C Slave Quick Start Guide with Callbacks
 *
 * Copyright (C) 2013-2014 Atmel Corporation. All rights reserved.
 *
 * \asf_license_start
 *
 * \page License
 *
 * 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. The name of Atmel may not be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * 4. This software may only be redistributed and used in connection with an
 *    Atmel microcontroller product.
 *
 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL 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.
 *
 * \asf_license_stop
 *
 */

#include <asf.h>

void i2c_read_request_callback(struct i2c_slave_module *const module);
void i2c_write_request_callback(struct i2c_slave_module *const module);
void configure_i2c_slave(void);
void configure_i2c_slave_callbacks(void);

static struct i2c_slave_packet packet;

#define DATA_LENGTH 10
static uint8_t write_buffer[DATA_LENGTH] = {
   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
};
static uint8_t read_buffer [DATA_LENGTH];

/* Address of the slave */
#define SLAVE_ADDRESS 0x12

/* Init device instance. */
struct i2c_slave_module i2c_slave_instance;

volatile bool custom_wait = false;

void i2c_read_request_callback(struct i2c_slave_module *const module)
{
   /* Init i2c packet. */
   packet.data_length = DATA_LENGTH;
   packet.data        = write_buffer;

   /* Write buffer to master */
   i2c_slave_write_packet_job(module, &packet);
}

void i2c_write_request_callback(struct i2c_slave_module *const module)
{
   /* Init i2c packet. */
   packet.data_length = DATA_LENGTH;
   packet.data        = read_buffer;

   /* Read buffer from master */
   if (i2c_slave_read_packet_job(module, &packet) != STATUS_OK) {
   }
}

void i2c_read_complete_callback(struct i2c_slave_module *const module)
{
   i2c_slave_enable_nack_on_address(module);
   custom_wait = true;
}

void configure_i2c_slave(void)
{
   /* Initialize config structure and module instance. */
   struct i2c_slave_config config_i2c_slave;
   i2c_slave_get_config_defaults(&config_i2c_slave);

   /* Change address and address_mode. */
   config_i2c_slave.address      = SLAVE_ADDRESS;
   config_i2c_slave.address_mode = I2C_SLAVE_ADDRESS_MODE_MASK;
   config_i2c_slave.pinmux_pad0  = PINMUX_PA22C_SERCOM3_PAD0;
   config_i2c_slave.pinmux_pad1  = PINMUX_PA23C_SERCOM3_PAD1;   

   /* Initialize and enable device with config. */
   i2c_slave_init(&i2c_slave_instance, SERCOM3, &config_i2c_slave);
   i2c_slave_enable(&i2c_slave_instance);
}

void configure_i2c_slave_callbacks(void)
{
   /* Register and enable callback functions */
   i2c_slave_register_callback(&i2c_slave_instance, i2c_read_request_callback, I2C_SLAVE_CALLBACK_READ_REQUEST);
   i2c_slave_enable_callback(&i2c_slave_instance, I2C_SLAVE_CALLBACK_READ_REQUEST);

   i2c_slave_register_callback(&i2c_slave_instance, i2c_write_request_callback, I2C_SLAVE_CALLBACK_WRITE_REQUEST);
   i2c_slave_enable_callback(&i2c_slave_instance, I2C_SLAVE_CALLBACK_WRITE_REQUEST);
   
   i2c_slave_register_callback(&i2c_slave_instance, i2c_read_complete_callback, I2C_SLAVE_CALLBACK_READ_COMPLETE);
   i2c_slave_enable_callback(&i2c_slave_instance, I2C_SLAVE_CALLBACK_READ_COMPLETE);   
}

int main(void)
{
   system_init();

   /* Configure device and enable. */
   configure_i2c_slave();
   configure_i2c_slave_callbacks();

   while (true) {
   /* Infinite loop while waiting for I2C master interaction */
      if (!custom_wait)
         continue;
      delay_ms(100);
      i2c_slave_disable_nack_on_address(&i2c_slave_instance);
      custom_wait = false;      
   }
}

Return to “SAM D20 Cortex-M0+ MCU”

Who is online

Users browsing this forum: No registered users and 1 guest