/* 
 * Copyright (c) 2018 Nordic Semiconductor ASA 
 * 
 * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic 
 */ 
 fffff
#include <zephyr/kernel.h> 
//#include <zephyr/drivers/i2c_slave.h> // Use i2c_slave.h for the slave API 

#include <zephyr/device.h> 
#include <zephyr/drivers/i2c.h>

#include <stdio.h> 
 
 // ...
#include <zephyr/kernel.h> 
#include <zephyr/drivers/i2c_slave.h> // <-- UNCOMMENT THIS LINE

#include <zephyr/device.h> 
#include <zephyr/drivers/i2c.h>
#include <zephyr/sys/util.h> // <-- ADD this for MIN()
#include <string.h>          // <-- ADD this for memcpy()
#include <stdio.h> 
// ...
 
 
#define I2C_SLAVE_DEVICE_NAME "I2C_1" // Matches the label in your overlay 
 
// Buffer to simulate the slave's internal "register" space 
static uint8_t slave_data[16];  
static const struct device *i2c_slave_dev; 
 
// --- I2C Slave Protocol Handler --- 
 
static int i2c_slave_write_received(const struct device *dev, const uint8_t *buf, uint32_t len) 
{ 
    // The master is writing data to us. 
    // In a "register model," the first byte is usually the register address. 
    // We'll just print all incoming data for simplicity. 
    printk("SLAVE: Received %d bytes: ", len); 
    for (uint32_t i = 0; i < len; i++) { 
        printk("0x%02X ", buf[i]); 
    } 
    printk("\n"); 
 
    // Copy data (excluding the register address if applicable) to slave_data 
    if (len > 0) { 
        memcpy(slave_data, buf, MIN(len, sizeof(slave_data))); 
    } 
     
    return 0; // Return 0 to allow the write 
} 
 
static int i2c_slave_read_requested(const struct device *dev, uint8_t *buf, uint32_t *len) 
{ 
    // The master is requesting data from us. 
    // Load the output buffer with simulated register content. 
    uint32_t tx_len = MIN(*len, sizeof(slave_data)); 
    memcpy(buf, slave_data, tx_len);  
    *len = tx_len; 
     
    printk("SLAVE: Sending %d bytes to Master.\n", tx_len); 
    return 0; // Return 0 to allow the read 
} 
 
static void i2c_slave_stop(const struct device *dev) 
{ 
    // Transfer complete (STOP condition received) 
    // No action needed for this simple example. 
} 
 
static const struct i2c_slave_driver_api slave_api = { 
    .write_received = i2c_slave_write_received, 
    .read_request = i2c_slave_read_requested, 
    .stop = i2c_slave_stop, 
}; 
 
// --- Main Application --- 
 
void main(void) 
{ 
    printk("*** Booting I2C Slave Application ***\n"); 
 
    i2c_slave_dev = device_get_binding(I2C_SLAVE_DEVICE_NAME); 
    if (!i2c_slave_dev) { 
        printk("Error: Device binding failed for %s.\n", I2C_SLAVE_DEVICE_NAME); 
        return; 
    } 
     
    // Initialize slave_data with a pattern for the master to read 
    for (int i = 0; i < sizeof(slave_data); i++) { 
        slave_data[i] = 0x80 + i; 
    } 
 
    int ret = i2c_slave_register(i2c_slave_dev, &slave_api); 
    if (ret != 0) { 
        printk("Error registering I2C slave driver: %d\n", ret); 
        return; 
    } 
 
    printk("I2C Slave Registered at 0x%02X. Waiting for Master...\n", 0x42); 
 
    // Slave thread enters an endless sleep loop.  
    // All communication is handled by the interrupt-driven driver callbacks. 
    while (1) { 
        k_sleep(K_SECONDS(5)); 
    } 
} 