i broke everything whaaat

This commit is contained in:
hugo LAMY
2025-03-16 21:08:33 +01:00
parent 6fa2df3b44
commit 54d9bea111
5 changed files with 182 additions and 96 deletions

View File

@@ -20,88 +20,106 @@ void i2c_start_repeat(void) { // 22.7.1 : "Repeated START enables the
void i2c_start(void) {
i2c_status = START;
TWCR = SEND_START_CONDITION; // 22.9.2 : (Control Register) send Start condition (22.7.1) ! writting 1 to TWINT clears it (set it to 0)
TWCR = TWI_START_CONDITION; // 22.9.2 : (Control Register) send Start condition (22.7.1) ! writting 1 to TWINT clears it (set it to 0)
while (!(TEST(TWCR, TWINT))); // 22.7.1 : Wait for TWINT Flag set. This indicates that the SLA+R/W has been transmitted, and ACK/NACK has been received
// uint8_t status = TWSR & 0b11111000; // Table 22-2. Status codes for Master Transmitter Mode
// if (status != TW_START && status != TW_REP_START) {
// return i2c_stop();
// }
uint8_t status = TWSR & 0b11111000; // Table 22-2. Status codes for Master Transmitter Mode
if (status != TW_START && status != TW_REP_START) {
return i2c_stop();
}
}
void i2c_send_addr_w(uint8_t slave_address) {
if (i2c_status == STOP) {
return;
}
uint8_t addr = (slave_address << 1) | TW_WRITE; // 22.3.3 : address format SLA+R/W
TWDR = addr; // 22.9.4 : (Data Register) load data into TWDR register
TWCR = SEND_CONTINUE_TRANSMISSION; // 22.7.1 : Master Transmitter Mode
while (!(TEST(TWCR, TWINT))); // 22.7.1 : Wait for TWINT Flag set. This indicates that the SLA+R/W has been transmitted, and ACK/NACK has been received
uint8_t addr = (slave_address << 1) | TW_WRITE; // 22.3.3 : address format SLA+R/W
TWDR = addr; // 22.9.4 : (Data Register) load data into TWDR register
TWCR = TWI_NACKNOWLEDGE; // 22.7.1 : Master Transmitter Mode
while (!(TEST(TWCR, TWINT))); // 22.7.1 : Wait for TWINT Flag set. This indicates that the SLA+R/W has been transmitted, and ACK/NACK has been received
// uint8_t status = TWSR & 0b11111000; // Table 22-2 : check status for Transmitter mode, mask prescaler bits
// if (status == TW_MT_SLA_ACK) { // status 0x18 : slave address transmitted, slave acknoledged (continue transmission)
// return;
// } else if (status == TW_MT_SLA_NACK) { // status 0x20 : slave address transmitted, slave did not acknoledge (retry or send STOP)
// return i2c_stop();
// } else if (status == TW_MT_ARB_LOST) { // status 0x38 : Arbitration Lost, Another I2C master took control of the bus (wait for bus to be free and retry)
// return i2c_stop();
// } else {
// return;
// }
uint8_t status = TWSR & 0b11111000; // Table 22-2 : check status for Transmitter mode, mask prescaler bits
if (status == TW_MT_SLA_ACK) { // status 0x18 : slave address transmitted, slave acknoledged (continue transmission)
return;
} else if (status == TW_MT_SLA_NACK) { // status 0x20 : slave address transmitted, slave did not acknoledge (retry or send STOP)
return i2c_stop();
} else if (status == TW_MT_ARB_LOST) { // status 0x38 : Arbitration Lost, Another I2C master took control of the bus (wait for bus to be free and retry)
return i2c_stop();
} else {
return;
}
}
void i2c_send_addr_r(uint8_t slave_address) {
if (i2c_status == STOP) {
return;
}
uint8_t addr = (slave_address << 1) | TW_READ; // 22.3.3 : address format SLA+R/W
TWDR = addr; // 22.9.4 : (Data Register) load data into TWDR register
TWCR = SEND_CONTINUE_TRANSMISSION; // 22.7.1 : Master Transmitter Mode
while (!(TEST(TWCR, TWINT))); // 22.7.1 : Wait for TWINT Flag set. This indicates that the SLA+R/W has been transmitted, and ACK/NACK has been received
uint8_t addr = (slave_address << 1) | TW_READ; // 22.3.3 : address format SLA+R/W
TWDR = addr; // 22.9.4 : (Data Register) load data into TWDR register
TWCR = TWI_NACKNOWLEDGE; // 22.7.1 : Master Transmitter Mode
while (!(TEST(TWCR, TWINT))); // 22.7.1 : Wait for TWINT Flag set. This indicates that the SLA+R/W has been transmitted, and ACK/NACK has been received
// uint8_t status = TWSR & 0b11111000; // Table 22-3 : check status for Receiver mode, mask prescaler bits
// if (status == TW_MR_SLA_ACK) { // status 0x40 : slave address transmitted, slave acknoledged (continue transmission)
// return;
// } else if (status == TW_MR_SLA_NACK) { // status 0x48 : slave address transmitted, slave did not acknoledge (retry or send STOP)
// return i2c_stop();
// } else if (status == TW_MR_ARB_LOST) { // status 0x38 : Arbitration Lost, Another I2C master took control of the bus (wait for bus to be free and retry)
// return i2c_stop();
// } else {
// return;
// }
uint8_t status = TWSR & 0b11111000; // Table 22-3 : check status for Receiver mode, mask prescaler bits
if (status == TW_MR_SLA_ACK) { // status 0x40 : slave address transmitted, slave acknoledged (continue transmission)
return;
} else if (status == TW_MR_SLA_NACK) { // status 0x48 : slave address transmitted, slave did not acknoledge (retry or send STOP)
return i2c_stop();
} else if (status == TW_MR_ARB_LOST) { // status 0x38 : Arbitration Lost, Another I2C master took control of the bus (wait for bus to be free and retry)
return i2c_stop();
} else {
return;
}
}
void i2c_write(uint8_t data) {
if (i2c_status == STOP) {
return;
}
TWDR = data; // 22.9.4 : (Data Register) load data into TWDR register
TWCR = SEND_CONTINUE_TRANSMISSION; // 22.7.1 : Master Transmitter Mode
while (!(TEST(TWCR, TWINT))); // 22.7.1 : Wait for TWINT Flag set. This indicates that the SLA+R/W has been transmitted, and ACK/NACK has been received
TWDR = data; // 22.9.4 : (Data Register) load data into TWDR register
TWCR = TWI_NACKNOWLEDGE; // 22.7.1 : Master Transmitter Mode
while (!(TEST(TWCR, TWINT))); // 22.7.1 : Wait for TWINT Flag set. This indicates that the SLA+R/W has been transmitted, and ACK/NACK has been received
// uint8_t status = TWSR & 0b11111000; // Table 22-3 : check status for Receiver mode, mask prescaler bits
// if (status != TW_MT_DATA_ACK) {
// return i2c_stop();
// }
uint8_t status = TWSR & 0b11111000; // Table 22-2 : check status for Transmitter mode, mask prescaler bits
if (status != TW_MT_DATA_ACK) {
return i2c_stop();
}
}
uint8_t i2c_read(void) {
uint8_t i2c_read_ack(void) {
if (i2c_status == STOP) {
return;
return 0;
}
TWCR = SEND_CONTINUE_TRANSMISSION; // 22.7.1 : Master Transmitter Mode
while (!(TEST(TWCR, TWINT))); // 22.7.1 : Wait for TWINT Flag set. This indicates that the SLA+R/W has been transmitted, and ACK/NACK has been received
TWCR = TWI_ACKNOWLEDGE; // 22.7.1 : Master Transmitter Mode
while (!(TEST(TWCR, TWINT))); // 22.7.1 : Wait for TWINT Flag set. This indicates that the SLA+R/W has been transmitted, and ACK/NACK has been received
// uint8_t status = TWSR & 0b11111000; // p225 example code : Check status for Receiver mode. Mask prescaler bits. If status different from MT_DATA_ACK go to ERROR
// if (status != TW_MT_DATA_ACK) {
// return i2c_stop();
// }
uint8_t status = TWSR & 0b11111000; // p225 example code : Check status for Receiver mode. Mask prescaler bits. If status different from MT_DATA_ACK go to ERROR
if (status != TW_MT_DATA_ACK) {
i2c_stop();
return 0;
}
return TWDR; // 22.9.4 : (Data Register) load data into TWDR register
return TWDR; // 22.9.4 : (Data Register) load data into TWDR register
}
uint8_t i2c_read_nack(void) {
if (i2c_status == STOP) {
return 0;
}
TWCR = TWI_NACKNOWLEDGE; // 22.7.1 : Master Transmitter Mode
while (!(TEST(TWCR, TWINT))); // 22.7.1 : Wait for TWINT Flag set. This indicates that the SLA+R/W has been transmitted, and ACK/NACK has been received
uint8_t status = TWSR & 0b11111000; // p225 example code : Check status for Receiver mode. Mask prescaler bits. If status different from MT_DATA_ACK go to ERROR
if (status != TW_MT_DATA_NACK) {
i2c_stop();
return 0;
}
return TWDR; // 22.9.4 : (Data Register) load data into TWDR register
}
void i2c_stop(void) {
TWCR = SEND_STOP_CONDITION; // 22.9.2 : send stop condition
TWCR = TWI_STOP_CONDITION; // 22.9.2 : send stop condition
i2c_status = STOP;
}