SAMC21 Sercom I2C read from slave register address

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am new to Sercom I2C interface. I was unable to read the data from the I2C slave register . I using SAMC21 Xplained-Pro kit and Configured below pin as SDA and SCL.

 

PA22 --> SERCOM3 Pad[0] --> SDA

PA23 --> SERCOM3 Pad[1] --> SCL

 

Using Default example from Atmel studio i.e I2C_Quick_start_master_basic project

we have gone through the file and API but unable to find the slave register address for reading or writing.

 

I want to interface ADXL345 Accelerometer. 

Thanks 

Hiren

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For writing you can just include the slave internal address in the packet of data sent with i2c_master_write_packet_wait. For reading you can call i2c_master_write_packet_wait_no_stop first (write a single byte, the slave internal address) and then perform the read with i2c_master_read_packet_wait.

/Lars

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

         I have tried as per your suggestion. I make some modifications in default examples(change SLAVE_ADDRESS, configure pins, and change SECOM2 to SERCOM3). But still, I can not read data. Please check the below lines of code and comments if any suggestions. I am trying to read Device_ID which is available at the "0x00" address of slave address "0x53".

/**
 * \file
 *
 * \brief SAM SERCOM I2C Master Quick Start Guide
 *
 * Copyright (c) 2012-2018 Microchip Technology Inc. and its subsidiaries.
 *
 * \asf_license_start
 *
 * \page License
 *
 * Subject to your compliance with these terms, you may use Microchip
 * software and any derivatives exclusively with Microchip products.
 * It is your responsibility to comply with third party license terms applicable
 * to your use of third party software (including open source software) that
 * may accompany Microchip software.
 *
 * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
 * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
 * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
 * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
 * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
 * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE.  TO THE FULLEST EXTENT
 * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
 * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
 * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
 *
 * \asf_license_stop
 *
 */
/*
 * Support and FAQ: visit <a href="https://www.microchip.com/support/">Microchip Support</a>
 */

#include <asf.h>

//! [packet_data]
#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]={0x00};
//! [packet_data]

//! [address]
#define SLAVE_ADDRESS 0x53		//0x12
//! [address]

/* Number of times to try to send packet if failed. */
//! [timeout]
#define TIMEOUT 1000
//! [timeout]

/* Init software module. */
//! [dev_inst]
struct i2c_master_module i2c_master_instance;
//! [dev_inst]

void configure_i2c_master(void);

//! [initialize_i2c]
void configure_i2c_master(void)
{
	/* Initialize config structure and software module. */
	//! [init_conf]
	struct i2c_master_config config_i2c_master;
	i2c_master_get_config_defaults(&config_i2c_master);
	//! [init_conf]

	/* Change buffer timeout to something longer. */
	//! [conf_change]
	config_i2c_master.buffer_timeout = 10000;
	config_i2c_master.pinmux_pad0 = PINMUX_PA22C_SERCOM3_PAD0;
	config_i2c_master.pinmux_pad1 = PINMUX_PA23C_SERCOM3_PAD1;
	
	config_i2c_master.baud_rate = I2C_MASTER_BAUD_RATE_100KHZ;
	
#if SAMR30
	config_i2c_master.pinmux_pad0    = CONF_MASTER_SDA_PINMUX;
	config_i2c_master.pinmux_pad1    = CONF_MASTER_SCK_PINMUX;
#endif
	//! [conf_change]
	/* Initialize and enable device with config. */
	//! [init_module]
	i2c_master_init(&i2c_master_instance, CONF_I2C_MASTER_MODULE, &config_i2c_master);
	//! [init_module]

	//! [enable_module]
	i2c_master_enable(&i2c_master_instance);
	//! [enable_module]
}
//! [initialize_i2c]

int main(void)
{
	uint8_t device_id = 0;
	
	system_init();

	//! [init]
	/* Configure device and enable. */
	//! [config]
	configure_i2c_master();
	//! [config]

	/* Timeout counter. */
	//! [timeout_counter]
	uint16_t timeout = 0;
	//! [timeout_counter]

	/* Init i2c packet. */
	//! [packet]
	struct i2c_master_packet packet = {
		.address     = SLAVE_ADDRESS,
		.data_length = 1,				//DATA_LENGTH,
		.data        = 0x00,			//write_buffer,
		.ten_bit_address = false,
		.high_speed      = false,
		.hs_master_code  = 0x0,
	};
	//! [packet]
	//! [init]

	//! [main]
	/* Write buffer to slave until success. */
	//! [write_packet]
	//while (i2c_master_write_packet_wait(&i2c_master_instance, &packet) !=
	while (i2c_master_write_packet_wait_no_stop(&i2c_master_instance, &packet) !=
			STATUS_OK) {
		/* Increment timeout counter and check if timed out. */
		if (timeout++ == TIMEOUT) {
			break;
		}
	}
	//! [write_packet]

	/* Read from slave until success. */
	//! [read_packet]
	//--packet.data_length = 2;
	packet.data = &device_id;	//read_buffer;
	while (i2c_master_read_packet_wait(&i2c_master_instance, &packet) !=
			STATUS_OK) {
		/* Increment timeout counter and check if timed out. */
		if (timeout++ == TIMEOUT) {
			break;
		}
	}
	//! [read_packet]

	//! [main]

	while (true) {
		/* Infinite loop */
		;
	}

}
 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

        Now, it's working fine. I have made some modification as below:

 

-Use {uint8_t register_addres=0x00;}

-Then {pass .data        = &register_addres,    //0x00,}

 

I just did it and working properly.

 

Thanks,

Hiren Kumbhare