#include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <sys/ioctl.h> #include <linux/i2c-dev.h> #include <stdint.h> int main() { printf("make file\n\r"); int file; printf("Make filename\n\r"); char filename[] = "/dev/i2c-1"; printf("Pick address\n\r"); int addr = 0x08; // Replace with your I2C device address uint8_t buffer[1]; buffer[0]=0xFF; printf("Open the I2C device file\n\r"); // Open the I2C device file if ((file = open(filename, O_RDWR)) < 0) { perror("Failed to open the I2C bus"); return 1; } printf("Set the I2C device address\n\r"); // Set the I2C device address if (ioctl(file, I2C_SLAVE, addr) < 0) { perror("Failed to acquire bus access and/or talk to slave"); return 1; } printf("Write data\n\r"); // Write data // Example: Writing a single byte if (write(file, buffer, 1) != 1) { perror("Failed to write to the I2C bus"); return 1; } return 0; } #include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <sys/ioctl.h> #include <linux/i2c-dev.h> #include <stdint.h> int main() { printf("make file\n\r"); int file; printf("Make filename\n\r"); char filename[] = "/dev/i2c-1"; printf("Pick address\n\r"); int addr = 0x08; // Replace with your I2C device address uint8_t buffer[1]; buffer[0]=0xFF; printf("Open the I2C device file\n\r"); // Open the I2C device file if ((file = open(filename, O_RDWR)) < 0) { perror("Failed to open the I2C bus"); return 1; } printf("Set the I2C device address\n\r"); // Set the I2C device address if (ioctl(file, I2C_SLAVE, addr) < 0) { perror("Failed to acquire bus access and/or talk to slave"); return 1; } printf("Write data\n\r"); // Write data // Example: Writing a single byte if (write(file, buffer, 1) != 1) { perror("Failed to write to the I2C bus"); return 1; } return 0; } BIT_SET(SREG, BIT(7));//Interrupt bit in SREG set //slave TWAR = 0B00010001;//Device’s Own Slave Address is 0x08 (0001000X), responds to general call(XXXXXX1) BIT_CLEAR(TWCR, BIT(TWIE));//I2C Interrupt disable BIT_CLEAR(TWCR, BIT(TWSTA));//Clear TWSTA to become slave BIT_CLEAR(TWCR, BIT(TWSTO));//Stop condition disabled BIT_SET(TWCR, BIT(TWEA));//acknowledge own address BIT_SET(TWCR, BIT(TWEN));//TWI Enable BIT_SET(SREG, BIT(7));//Interrupt bit in SREG set //slave TWAR = 0B00010001;//Device’s Own Slave Address is 0x08 (0001000X), responds to general call(XXXXXX1) BIT_CLEAR(TWCR, BIT(TWIE));//I2C Interrupt disable BIT_CLEAR(TWCR, BIT(TWSTA));//Clear TWSTA to become slave BIT_CLEAR(TWCR, BIT(TWSTO));//Stop condition disabled BIT_SET(TWCR, BIT(TWEA));//acknowledge own address BIT_SET(TWCR, BIT(TWEN));//TWI Enable /*After its own slave address and the write bit have been received, the TWINT Flag is set and a valid status code can be read from TWSR*/ if(BIT_GET(TWCR, BIT(TWINT)))//if TWI interrupt flag is set { static uint8_t twStatus,receivedData; twStatus = TWSR;//read the status BIT_CLEAR(twStatus, BIT(0));//clear prescaler values BIT_CLEAR(twStatus, BIT(1)); sw("TWSR status code is ",1); swn(twStatus,16,0,1); //serialWrite the current status sw(":->",1); //serialWrite //Slave receiver if (twStatus == 0x60) { sw("Own SLA+W received, ACK returned\n\r",1); BIT_CLEAR(TWCR, BIT(TWSTA)); BIT_CLEAR(TWCR, BIT(TWSTO)); //Data byte will be received and ACK will be returned. BIT_SET(TWCR, BIT(TWEA)); } else if (twStatus == 0x80) { sw("Prev addressed with SLA+W, data received, ACK returned\n\r",1); BIT_CLEAR(TWCR, BIT(TWSTA)); BIT_CLEAR(TWCR, BIT(TWSTO)); //Data byte will be received and ACK will be returned. BIT_SET(TWCR, BIT(TWEA)); sw("Data: ",1); receivedData = TWDR; swn(receivedData,16,1,1); sw("\n\r",1); } else if (twStatus == 0x88) { sw("Prev addressed with own SLA+W, data received, ACK returned\n\r",1); //Switch to non addressed slave, Own SLA recognized. BIT_CLEAR(TWCR, BIT(TWSTA)); BIT_CLEAR(TWCR, BIT(TWSTO)); BIT_SET(TWCR, BIT(TWEA)); //sw("Data: ",1); receivedData = TWDR; swn(receivedData,16,1,1); sw("\n\r",1); } else if (twStatus == 0xA0) { sw("Stop or repeated start received while being slave.\n\r",1); //Switch to non addressed slave, Own SLA recognized. BIT_CLEAR(TWCR, BIT(TWSTA)); BIT_CLEAR(TWCR, BIT(TWSTO)); BIT_SET(TWCR, BIT(TWEA)); sw("Data: ",1); receivedData = TWDR; swn(receivedData,16,1,1); sw("\n\r",1); } /*clearing this flag starts the operation of the TWI, so all accesses to the TWI Address Register (TWAR), TWI Status Register (TWSR), and TWI Data Register (TWDR) must be complete before clearing this flag.*/ BIT_SET(TWCR, BIT(TWINT));//WRITE 1 to TWINT to clear it /*After its own slave address and the write bit have been received, the TWINT Flag is set and a valid status code can be read from TWSR*/ if (BIT_GET(TWCR, BIT(TWINT)))//if TWI interrupt flag is set { static uint8_t twStatus,receivedData; twStatus = TWSR;//read the status BIT_CLEAR(twStatus, BIT(0));//clear prescaler values BIT_CLEAR(twStatus, BIT(1)); sw("TWSR status code is ",1); swn(twStatus,16,0,1); //serialWrite the current status sw(":->",1); //serialWrite //Slave receiver if (twStatus == 0x60) { sw("Own SLA+W received, ACK returned\n\r",1); BIT_CLEAR(TWCR, BIT(TWSTA)); BIT_CLEAR(TWCR, BIT(TWSTO)); //Data byte will be received and ACK will be returned. BIT_SET(TWCR, BIT(TWEA)); } else if (twStatus == 0x80) { sw("Prev addressed with SLA+W, data received, ACK returned\n\r",1); BIT_CLEAR(TWCR, BIT(TWSTA)); BIT_CLEAR(TWCR, BIT(TWSTO)); //Data byte will be received and ACK will be returned. BIT_SET(TWCR, BIT(TWEA)); sw("Data: ",1); receivedData = TWDR; swn(receivedData,16,1,1); sw("\n\r",1); } else if (twStatus == 0x88) { sw("Prev addressed with own SLA+W, data received, ACK returned\n\r",1); //Switch to non addressed slave, Own SLA recognized. BIT_CLEAR(TWCR, BIT(TWSTA)); BIT_CLEAR(TWCR, BIT(TWSTO)); BIT_SET(TWCR, BIT(TWEA)); //sw("Data: ",1); receivedData = TWDR; swn(receivedData,16,1,1); sw("\n\r",1); } else if (twStatus == 0xA0) { sw("Stop or repeated start received while being slave.\n\r",1); //Switch to non addressed slave, Own SLA recognized. BIT_CLEAR(TWCR, BIT(TWSTA)); BIT_CLEAR(TWCR, BIT(TWSTO)); BIT_SET(TWCR, BIT(TWEA)); sw("Data: ",1); receivedData = TWDR; swn(receivedData,16,1,1); sw("\n\r",1); } /*clearing this flag starts the operation of the TWI, so all accesses to the TWI Address Register (TWAR), TWI Status Register (TWSR), and TWI Data Register (TWDR) must be complete before clearing this flag.*/ BIT_SET(TWCR, BIT(TWINT));//WRITE 1 to TWINT to clear it TWSR status code is 60:->Own SLA+W received, ACK returned TWSR status code is 80:->Prev addressed with SLA+W, data received, ACK returned Data: fe TWSR status code is 60:->Own SLA+W received, ACK returned
TWSR status code is 80:->Prev addressed with SLA+W, data received, ACK returned
Data: fe


