SAMD51, I2C ASYNC SLAVE not working with the example

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

hello, i am beginer of atmel mcu. 

 

i made simple  i2c async slave code in the samd51 based custom board, 

first, i additionally registered a transfer callback function to the example of the  i2c async slave sample code which was gotten from the ASF version is 4.0 

and i2c master was made in the raspberry pi model b,  

 

when the master wrote a byte data,  slave received without any problem,

but when the slave tried to write a byte, the samd51 become dead

here are the code 

 

 

master code [raspberry]

 

int main(void) {
    int fd;
    int ret;
    unsigned char c = 0xa;

 

    fd = wiringPiI2CSetupInterface ("/dev/i2c-1", 0x12);
    if (fd < 0) {
        printf("wiringPiI2CSetupInterface error(%d)\n", fd);
        return 1;
    }
    printf("fd: %d\n", fd);
    while (1) {

        ret = wiringPiI2CWrite(fd, (int)c);
        if (ret <0) {
            printf("wiringPiI2CWrite error[%d]\n", ret);
        }

        c = wiringPiI2CRead(fd);
        if (c < 0) {
            printf("wiringPiI2CRead error[%d]\n", c);
        }

        if (c == 0xa) {
            printf("----->>>>>  got!!![0x%x]\n", c);
        } else {
            printf("[0x%x]\n", c);
        }
        sleep(1);
    }

    close(fd);
    return 0;
}

 

 

slave [samd51]

#include <atmel_start.h>

static struct io_descriptor *ui_io;
uint8_t buf;

 

static void ui_i2c_rx(const struct i2c_s_async_descriptor *const descr)
{
    io_read(ui_io, &buf, 1);
    printf("\r\nrecv[0x%x]\r\n", buf);
}

static void ui_i2c_tx(const struct i2c_s_async_descriptor *const descr)
{
    io_write(ui_io, &buf, 1);
    printf("\r\nsend[0x%x]\r\n", buf);

}

void i2c_init(void)
{
    i2c_s_async_get_io_descriptor(&I2C_0, &ui_io);
    i2c_s_async_register_callback(&I2C_0, I2C_S_RX_COMPLETE, ui_i2c_rx);
    i2c_s_async_register_callback(&I2C_0, I2C_S_TX_PENDING, ui_i2c_tx);
    i2c_s_async_set_addr(&I2C_0, 0x12);
    i2c_s_async_enable(&I2C_0);
}

 

int main(void)
{
    int count = 0;
    /* Initializes MCU, drivers and middleware */
    atmel_start_init();
    i2c_init();
    stdio_redirect_init();

    printf("\r\nhello world\r\n");
    /* Replace with your application code */
    while (1) {
        printf("\r\n%d\r\n", count++);
        delay_ms(1000);
    }
}

 

i want to know why my samd51 die when it try to write a recevied data ??

 

previously thanks :-( 

----
Hello ~ (^^ 3 i am beginner of atmel mcu. but i will study hard.

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

i resolved this issue.

 

there was no problem in the i2c async driver of the ASF4.0 

 

here is last code 

 

[i2c async master, raspberry pi model b]

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <wiringPiI2C.h>

int main(void) {
    int fd;
    int ret;
    unsigned char rbuf = 0;
    unsigned char tbuf = 0;

    fd = wiringPiI2CSetupInterface ("/dev/i2c-1", 0x12);
    if (fd < 0) {
        printf("wiringPiI2CSetupInterface error(%d)\n", fd);
        return 1;
    }
    printf("fd: %d\n", fd);
    while (1) {

        ret = wiringPiI2CWrite(fd, (int)tbuf++);
        if (ret <0) {
            printf("wiringPiI2CWrite error[%d]\n", ret);
        } else {
            printf("wrote[%d]\n", tbuf);
        }

        rbuf = wiringPiI2CRead(fd);
        if (rbuf < 0) {
            printf("wiringPiI2CRead error[%d]\n", rbuf);
        }

        printf("tbuf[%d], rbuf[%d]\n", tbuf, rbuf);
    }

    close(fd);
    return 0;
}

 

 

i2c async slave [samd51, custom board]

#include <atmel_start.h>

static struct io_descriptor *ui_io;
uint8_t rbuf = 0;
uint8_t tbuf = 128;

static void ui_i2c_rx(const struct i2c_s_async_descriptor *const descr)
{
    io_read(ui_io, &rbuf, 1);
    //printf("\r\nrecv[0x%x]\r\n", buf);
}

static void ui_i2c_tx(const struct i2c_s_async_descriptor *const descr)
{
    io_write(ui_io, &tbuf, 1);
    --tbuf;
    //printf("\r\nsend[0x%x]\r\n", buf);

}

void ui_i2c_init(void)
{
    i2c_s_async_get_io_descriptor(&I2C_0, &ui_io);
    i2c_s_async_register_callback(&I2C_0, I2C_S_RX_COMPLETE, ui_i2c_rx);
    i2c_s_async_register_callback(&I2C_0, I2C_S_TX_PENDING, ui_i2c_tx);
    i2c_s_async_set_addr(&I2C_0, 0x12);
    i2c_s_async_enable(&I2C_0);

}

int main(void)
{
    /* Initializes MCU, drivers and middleware */
    atmel_start_init();
    ui_i2c_init();
    stdio_redirect_init();
    
    printf("\r\nhello world\r\n");
    /* Replace with your application code */
    while (1) {
        printf("\r\nrbuf[%d], tbuf[%d]\r\n", rbuf, tbuf);
        delay_ms(100);
    }
}

 

----
Hello ~ (^^ 3 i am beginner of atmel mcu. but i will study hard.