The goal for resolving this issue is to setup the RAK4260 as a master with i2c. We want to read 100 bytes and then write 100 byte of data. Our slave has been confirmed to work with another device as master.
Currently we are not getting any data and the i2c bus times out waiting for data. No data is being read.
The issue is setting up the I2C with the RAK4260-LoRaNode-demo which is located here on Github.
The closest thing I found to code examples using this chip was located on microchip’s documentation for SERCOM with I2C on their website here.
A question I don’t know the answer to is which SERCOM am I suppose to be using? I see references that indicate SERCOM1,SERCOM4, or SERCOM5 being for I2C and cannot find definite answer.
A second question I have is whether there is any additional stuff that needs to be turned on for I2C? Something could not be initiated correctly or at all.
The code follows the logic of the Microchip Basic example on Page 35 but with different parameters. For now, the read function from the slave is the one we are focused on getting completed and is the only one shown.
Thank you in advanced.
Code Below:
Pre main function code:
#define DATA_LENGTH 100
static uint8_t write_buffer[DATA_LENGTH] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
};
static uint8_t read_buffer[DATA_LENGTH];
#define SLAVE_ADDRESS 0x50
#define TIMEOUT 1000
struct i2c_master_module i2c_master_instance;
void configure_i2c_master(void)
{
/* Initialize config structure and software module. */
struct i2c_master_config config_i2c_master;
i2c_master_get_config_defaults(&config_i2c_master);
/* Change buffer timeout to something longer. */
config_i2c_master.buffer_timeout = 10000;
/* Initialize and enable device with config. */
i2c_master_init(&i2c_master_instance, SERCOM4,
&config_i2c_master);
i2c_master_enable(&i2c_master_instance);
}
Main Function Code After Initial Setup.
/* Configure device and enable. */
configure_i2c_master();
/* Timeout counter. */
uint16_t timeout = 0;
/* Init i2c packet. */
struct i2c_master_packet packet = {
.address = SLAVE_ADDRESS,
.data_length = DATA_LENGTH,
.data = write_buffer,
.ten_bit_address = false,
.high_speed = false,
.hs_master_code = 0x0,
};
struct i2c_master_config config_i2c_master;
i2c_master_get_config_defaults(&config_i2c_master);
config_i2c_master.buffer_timeout = 10000;
i2c_master_init(&i2c_master_instance, SERCOM4,&config_i2c_master);
printf("I2C INITIALIZED %u \n" , 100);
/* Read from slave until success. */
packet.data = 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;
}
}
printf("I2C Done");
for(int i =0 ; i < DATA_LENGTH ; i++){
printf("Index");
printf("%u" , i );
printf(":");
printf("%u" , packet.data[i]);
printf("\n");
}