diff --git a/module06/ex01/i2c.c b/module06/ex01/i2c.c index d110e40..f3ca365 100644 --- a/module06/ex01/i2c.c +++ b/module06/ex01/i2c.c @@ -14,10 +14,6 @@ void i2c_init(void) { TWBR = ((F_CPU / TWI_FREQ) - 16) / (2 * TWI_PRESCALE_VALUE); // 22.9.1 : (Bit Rate Register) set SCL frequency (formula from datasheet, 22.5.2) } -void i2c_start_repeat(void) { // 22.7.1 : "Repeated START enables the Master to switch between Slaves, Master Transmitter mode and Master Receiver mode without losing control of the bus" - i2c_start(); -} - void i2c_start(void) { i2c_status = START; 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) @@ -28,6 +24,9 @@ void i2c_start(void) { return i2c_stop(); } } +void i2c_start_repeat(void) { // 22.7.1 : "Repeated START enables the Master to switch between Slaves, Master Transmitter mode and Master Receiver mode without losing control of the bus" + i2c_start(); +} void i2c_send_addr_w(uint8_t slave_address) { if (i2c_status == STOP) { @@ -93,8 +92,8 @@ uint8_t i2c_read_ack(void) { 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) { + uint8_t status = TWSR & 0b11111000; // Table 22-3 : check status for Receiver mode, mask prescaler bits + if (status != TW_MR_DATA_ACK) { i2c_stop(); return 0; } @@ -110,8 +109,8 @@ uint8_t i2c_read_nack(void) { 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) { + uint8_t status = TWSR & 0b11111000; // Table 22-3 : check status for Receiver mode, mask prescaler bits + if (status != TW_MR_DATA_NACK) { i2c_stop(); return 0; } diff --git a/module06/ex01/main.c b/module06/ex01/main.c index 96f4ba6..3e3886d 100644 --- a/module06/ex01/main.c +++ b/module06/ex01/main.c @@ -1,384 +1,5 @@ #include "header.h" -/* -before init : 11111000 -after init : 11111000 -after start : 1000 -after SLA+W : 11000 -1 : 11000 -2 : 11000 -3 : 11000 -4 : 11000 -5 : 11000 -6 : 11000 -7 : 11000 -8 : 11000 -9 : 11000 -10 : 11000 -20 : 11000 -30 : 11000 -40 : 11000 -50 : 11000 -60 : 11000 -70 : 11000 -80 : 11000 -90 : 11000 -100 : 11000 -110 : 11000 -120 : 11000 -130 : 11000 -140 : 11000 -150 : 11000 -160 : 11000 -170 : 11000 -180 : 11000 -190 : 11000 -200 : 11000 -1200 : 11000 -after stop : 11111000 -*/ - -/* -before init : 11111000 - after init : 11111000 -before start : 11111000 - after start : 1000 -waiting 200ms... : 1000 - done waiting : 1000 -send SLA+W : 1000 - after SLA+W : 11000 -waiting 10ms... : 11000 - done waiting : 11000 -send 0xAC (trigger measurement) : 11000 - after 0xAC : 101000 -send parameter 1st byte 0x33 : 101000 - after 0x33 : 101000 -send parameter 2nd byte 0x00 : 101000 - after 0x00 : 101000 -status ? 0 : 101000 -status ? 10 : 101000 -status ? 20 : 101000 -status ? 30 : 101000 -status ? 40 : 101000 -status ? 50 : 101000 -status ? 60 : 101000 -status ? 70 : 101000 -status ? 80 : 101000 -status ? 90 : 101000 -status ? 100 : 101000 -status ? 110 : 101000 -status ? 120 : 101000 -status ? 130 : 101000 -after stop : 11111000 -*/ - -/* -before init : 11111000 no state information available - after init : 11111000 -before start : 11111000 - after start : 1000 start condition transmitted -waiting 200ms... : 1000 - done waiting : 1000 -send SLA+W : 1000 - after SLA+W : 11000 SLA+W transmitted, ACK received -waiting 10ms... : 11000 - done waiting : 11000 -send 0xAC (trigger measurement) : 11000 - after 0xAC : 101000 data transmitted, ACK received -send parameter 1st byte 0x33 : 101000 - after 0x33 : 101000 -send parameter 2nd byte 0x00 : 101000 - after 0x00 : 101000 -send repeat start : 101000 - after repeat start : 10000 repeated start condition transmitted -send SLA+R : 10000 - after SLA+R : 1000000 SLA+R transmitted, ACK received -status ? 0 : 1000000 -status ? 10 : 1000000 -status ? 20 : 1000000 -status ? 30 : 1000000 -status ? 40 : 1000000 -status ? 50 : 1000000 -status ? 60 : 1000000 -status ? 70 : 1000000 -status ? 80 : 1000000 -status ? 90 : 1000000 -status ? 100 : 1000000 -status ? 110 : 1000000 -status ? 120 : 1000000 -status ? 130 : 1000000 -after stop : 11111000 -*/ - -/* -before init : 11111000 no state information available - after init : 11111000 -before start : 11111000 - after start : 1000 start condition transmitted -send 0x71 to get a byte of status word : 1000 - after 0x71 : 1000000 SLA+R transmitted, ACK received -waiting 200ms... : 1000000 - done waiting : 1000000 -send 0x71 to get a byte of status word : 1000000 - after 0x71 : 1011000 data received, NACK returned -send SLA+W : 1011000 - after SLA+W : 1011000 -waiting 10ms... : 1011000 - done waiting : 1011000 -send 0xAC (trigger measurement) : 1011000 - after 0xAC : 11111000 no state information available -send parameter 1st byte 0x33 : 11111000 - after 0x33 : 11111000 -send parameter 2nd byte 0x00 : 11111000 - after 0x00 : 11111000 -send repeat start : 11111000 - after repeat start : 1000 start condition transmitted -send SLA+R : 1000 - after SLA+R : 1000000 SLA+R transmitted, ACK received -status ? 0 : 1000000 -status ? 10 : 1000000 -status ? 20 : 1000000 -status ? 30 : 1000000 -status ? 40 : 1000000 -status ? 50 : 1000000 -status ? 60 : 1000000 -status ? 70 : 1000000 -status ? 80 : 1000000 -status ? 90 : 1000000 -status ? 100 : 1000000 -status ? 110 : 1000000 -status ? 120 : 1000000 -status ? 130 : 1000000 -after stop : 11111000 -*/ - -/* -init i2c : 11111000 no state information available - after init : 11111000 - -start transmission : 11111000 - after start : 1000 start condition transmitted - -send SLA+W : 1000 - after SLA+W : 11000 SLA+W transmitted, ACK received - -waiting 10ms... : 11000 - done waiting : 11000 - -send 0xAC (trigger measurement) : 11000 - after 0xAC : 101000 data transmitted, ACK received -send parameter 1st byte 0x33 : 101000 - after 0x33 : 101000 -send parameter 2nd byte 0x00 : 101000 - after 0x00 : 101000 - -send repeat start : 101000 - after repeat start : 10000 repeated start condition transmitted - -send SLA+R : 10000 - after SLA+R : 1000000 SLA+R transmitted, ACK received - -status ? 0 : 1000000 -status ? 10 : 1000000 -status ? 20 : 1000000 -status ? 30 : 1000000 -status ? 40 : 1000000 -status ? 50 : 1000000 -status ? 60 : 1000000 -status ? 70 : 1000000 -status ? 80 : 1000000 -status ? 90 : 1000000 -status ? 100 : 1000000 -status ? 110 : 1000000 -status ? 120 : 1000000 -status ? 130 : 1000000 - -send stop : 1000000 - after stop : 11111000 no state information available -*/ - -/* -init i2c : 11111000 no state information available - after init : 11111000 - -start transmission : 11111000 - after start : 1000 start condition transmitted - -send SLA+W : 1000 - after SLA+W : 11000 SLA+W transmitted, ACK received - -send 0xAC (trigger measurement) : 11000 - after 0xAC : 101000 data transmitted, ACK received -send parameter 1st byte 0x33 : 101000 - after 0x33 : 101000 -send parameter 2nd byte 0x00 : 101000 - after 0x00 : 101000 - -send repeat start : 101000 - after repeat start : 10000 repeated start condition transmitted - -send SLA+R : 10000 - after SLA+R : 1000000 SLA+R transmitted, ACK received - -status ? 0 : 1000000 -status ? 10 : 1000000 -status ? 20 : 1000000 -status ? 30 : 1000000 -status ? 40 : 1000000 -status ? 50 : 1000000 -status ? 60 : 1000000 -status ? 70 : 1000000 -status ? 80 : 1000000 -status ? 90 : 1000000 -status ? 100 : 1000000 -status ? 110 : 1000000 -status ? 120 : 1000000 -status ? 130 : 1000000 - -send stop : 1000000 - after stop : 11111000 no state information available -*/ - -/* -init i2c : 11111000 no state information available - after init : 11111000 - -start transmission : 11111000 - after start : 1000 start condition transmitted - -send SLA+W : 1000 - after SLA+W : 11000 SLA+W transmitted, ACK received - -send 0xAC (trigger measurement) : 11000 - after 0xAC : 101000 data transmitted, ACK received -send parameter 1st byte 0x33 : 101000 - after 0x33 : 101000 -send parameter 2nd byte 0x00 : 101000 - after 0x00 : 101000 - -send SLA+R : 101000 - after SLA+R : 110000 data transmitted, NACK received - -status ? 0 : 110000 -status ? 10 : 110000 -status ? 20 : 110000 -status ? 30 : 110000 -status ? 40 : 110000 -status ? 50 : 110000 -status ? 60 : 110000 -status ? 70 : 110000 -status ? 80 : 110000 -status ? 90 : 110000 -status ? 100 : 110000 -status ? 110 : 110000 -status ? 120 : 110000 -status ? 130 : 110000 - -send stop : 110000 - after stop : 11111000 -*/ - -/* -init i2c : 11111000 no state information available - after init : 11111000 - -start transmission : 11111000 - after start : 1000 start condition transmitted - -send SLA+W : 1000 - after SLA+W : 11000 SLA+W transmitted, ACK received - -send 0xAC (trigger measurement) : 11000 - after 0xAC : 101000 -send parameter 1st byte 0x33 : 101000 - after 0x33 : 101000 -send parameter 2nd byte 0x00 : 101000 - after 0x00 : 101000 - -send repeat start : 101000 - after repeat start : 10000 - -send SLA+R : 10000 - after SLA+R : 1000000 - -read 1 data : 1000000 - after read : 1011000 -````````````````````````read 2 data : 1011000 - after read : 1011000 -read 3 data : 1011000 - after read : 1011000 -```````````````````````````````````````` : read 4 data : 1011000 - after read : 1011000 -`read 5 data : 1011000 - after read : 1011000 -read 6 data : 1011000 - after read : 1011000 -read 7 data : 1011000 - after read : 1011000 -```````````````````````````` : read 8 data : 1011000 - after read : 1011000 -` : read 9 data : 1011000 - after read : 1011000 -` : -send stop : 1011000 - after stop : 11111000 -*/ - -/* -init i2c : 11111000 TW_NO_INFO -> No state information available - after init : 11111000 TW_NO_INFO -> No state information available - -start transmission : 11111000 TW_NO_INFO -> No state information available - after start : 1000 TW_START -> Start condition transmitted - -send SLA+W : 1000 TW_START -> Start condition transmitted - after SLA+W : 11000 TW_MT_SLA_ACK -> SLA+W transmitted, ACK received - -send 0xAC (trigger measurement) : 11000 TW_MT_SLA_ACK -> SLA+W transmitted, ACK received - after 0xAC : 101000 TW_MT_DATA_ACK -> Data transmitted, ACK received -send parameter 1st byte 0x33 : 101000 TW_MT_DATA_ACK -> Data transmitted, ACK received - after 0x33 : 101000 TW_MT_DATA_ACK -> Data transmitted, ACK received -send parameter 2nd byte 0x00 : 101000 TW_MT_DATA_ACK -> Data transmitted, ACK received - after 0x00 : 101000 TW_MT_DATA_ACK -> Data transmitted, ACK received - -send repeat start : 101000 TW_MT_DATA_ACK -> Data transmitted, ACK received - after repeat start : 10000 TW_REP_START -> Repeated start condition transmitted - -send SLA+R : 10000 TW_REP_START -> Repeated start condition transmitted - after SLA+R : 1000000 TW_MR_SLA_ACK -> SLA+R transmitted, ACK received - -read 1 data : 1000000 TW_MR_SLA_ACK -> SLA+R transmitted, ACK received - after read : 1011000 TW_MR_DATA_NACK -> Data received, NACK returned -152 -read 2 data : 1011000 TW_MR_DATA_NACK -> Data received, NACK returned - after read : 1011000 TW_MR_DATA_NACK -> Data received, NACK returned -196 -read 3 data : 1011000 TW_MR_DATA_NACK -> Data received, NACK returned - after read : 1011000 TW_MR_DATA_NACK -> Data received, NACK returned -182 -read 4 data : 1011000 TW_MR_DATA_NACK -> Data received, NACK returned - after read : 1011000 TW_MR_DATA_NACK -> Data received, NACK returned -213 -read 5 data : 1011000 TW_MR_DATA_NACK -> Data received, NACK returned - after read : 1011000 TW_MR_DATA_NACK -> Data received, NACK returned -255 -read 6 data : 1011000 TW_MR_DATA_NACK -> Data received, NACK returned - after read : 1011000 TW_MR_DATA_NACK -> Data received, NACK returned -212 -read 7 data : 1011000 TW_MR_DATA_NACK -> Data received, NACK returned - after read : 1011000 TW_MR_DATA_NACK -> Data received, NACK returned -164 -read 8 data : 1011000 TW_MR_DATA_NACK -> Data received, NACK returned - after read : 1011000 TW_MR_DATA_NACK -> Data received, NACK returned -255 -read 9 data : 1011000 TW_MR_DATA_NACK -> Data received, NACK returned - after read : 1011000 TW_MR_DATA_NACK -> Data received, NACK returned -255 - -send stop : 1011000 TW_MR_DATA_NACK -> Data received, NACK returned - after stop : 11111000 TW_NO_INFO -> No state information available -*/ - // status codes : // -- Master -- // TW_BUS_ERROR 0x00 = 0b0 = 0 -> illegal start or stop condition @@ -402,55 +23,15 @@ send stop : 1011000 TW_MR_DATA_NACK -> Data recei #define AHT20_ADDRESS 0x38 // doc AHT20, 7.3 : address of thermistor : 0b111000, 56 -// volatile uint8_t i2c_data = 0; - void print_hex_value(char c) { char buffer[3] = {0}; int_to_hex_string(c, buffer, 2); uart_printstr(buffer); } -void print_status_meaning(uint16_t status) { - switch (status) { - case 0x00: uart_printstr("TW_BUS_ERROR -> Illegal start or stop condition"); break; - case 0x08: uart_printstr("TW_START -> Start condition transmitted"); break; - case 0x10: uart_printstr("TW_REP_START -> Repeated start condition transmitted"); break; - case 0x18: uart_printstr("TW_MT_SLA_ACK -> SLA+W transmitted, ACK received"); break; - case 0x20: uart_printstr("TW_MT_SLA_NACK -> SLA+W transmitted, NACK received"); break; - case 0x28: uart_printstr("TW_MT_DATA_ACK -> Data transmitted, ACK received"); break; - case 0x30: uart_printstr("TW_MT_DATA_NACK -> Data transmitted, NACK received"); break; - case 0x38: uart_printstr("TW_MT_ARB_LOST/TW_MR_ARB_LOST -> Arbitration lost in SLA+W/R or Nack"); break; - case 0x40: uart_printstr("TW_MR_SLA_ACK -> SLA+R transmitted, ACK received"); break; - case 0x48: uart_printstr("TW_MR_SLA_NACK -> SLA+R transmitted, NACK received"); break; - case 0x50: uart_printstr("TW_MR_DATA_ACK -> Data received, ACK returned"); break; - case 0x58: uart_printstr("TW_MR_DATA_NACK -> Data received, NACK returned"); break; - case 0x98: uart_printstr("TW_SR_GCALL_DATA_NACK -> General call data received, NACK returned"); break; - case 0xA8: uart_printstr("TW_ST_SLA_ACK -> SLA+R received, ACK returned"); break; - case 0xF8: uart_printstr("TW_NO_INFO -> No state information available"); break; - default: uart_printstr("Unknown status code"); - } -} - -void print_status(char *str) { - return; - uint8_t status; - status = TWSR & 0b11111000; // Table 22-2. Status codes for Master Transmitter Mode - uint16_t size = 53; - size -= uart_printstr(str); - size -= uart_printstr(" : "); - size -= uart_printstr_itoa_base(status, 2); - - // print status meaning - while (size--) { - uart_tx(' '); - } - print_status_meaning(status); - uart_printstr_endl(""); -} - void print_data() { uint8_t data; - for (uint8_t i = 0; i < 10; i++) { + for (uint8_t i = 0; i < 7; i++) { data = i2c_read_nack(); print_hex_value(data); if (i == 0) { @@ -468,22 +49,23 @@ void print_data() { Boolean aht20_is_status_ready(void) { uint8_t status; - i2c_start(); - i2c_send_addr_w(AHT20_ADDRESS); + // i2c_start(); + // i2c_send_addr_w(AHT20_ADDRESS); // i2c_write(0xAC); // i2c_write(0x33); // i2c_write(0x00); i2c_start_repeat(); - i2c_send_addr_r(AHT20_ADDRESS); + i2c_send_addr_r(AHT20_ADDRESS); // AHT20 doc 7.4 : "get a byte of status word by sending 0x71" -> SLA+R == 0x71 status = i2c_read_nack(); i2c_stop(); uart_printstr("- status : "); uart_printstr_itoa_base_endl(status, 2); if (status & 0b10000000) { - return TRUE; + // if ((status & 0x18) != 0x18) { // AHT20 doc 7.4 : "If the status word and 0x18 are not equal to 0x18 ..., if they are equal proceed to the next step" + return FALSE; } else { - return FALSE; + return TRUE; } } @@ -493,7 +75,7 @@ void aht20_wait_for_status() { if (aht20_is_status_ready()) { break; } - _delay_ms(10); + _delay_ms(50); } } @@ -522,63 +104,31 @@ int main() { // 9. read 6 bytes // 10. read CRC - print_status("init i2c"); i2c_init(); - print_status(" after init"); - - print_status("\r\nstart transmission"); i2c_start(); - print_status(" after start"); + _delay_ms(200); // aht20_wait_for_status(); - print_status("\r\nwaiting 200ms..."); - _delay_ms(200); - print_status(" done waiting"); - print_status("\r\nsend SLA+W"); - i2c_send_addr_w(AHT20_ADDRESS); - print_status(" after SLA+W"); - - print_status("\r\nwaiting 20ms..."); - _delay_ms(20); - print_status(" done waiting"); - - print_status("\r\nsend 0xAC (trigger measurement)"); - i2c_write(0xAC); - print_status(" after 0xAC"); - print_status("send parameter 1st byte 0x33"); - i2c_write(0x33); - print_status(" after 0x33"); - print_status("send parameter 2nd byte 0x00"); - i2c_write(0x00); - print_status(" after 0x00"); - - for (uint8_t i = 0; i < 3; i++) { - // print_status("\r\nwaiting 100ms..."); - // _delay_ms(100); - // print_status(" done waiting"); + // while(1) { + for (uint8_t i = 0; i < 6; i++) { + i2c_start_repeat(); + i2c_send_addr_w(AHT20_ADDRESS); + _delay_ms(20); // aht20_wait_for_status(); - print_status("\r\nsend repeat start"); + i2c_write(0xAC); + i2c_write(0x33); + i2c_write(0x00); + + aht20_wait_for_status(); + i2c_start_repeat(); - print_status(" after repeat start"); - - print_status("\r\nsend SLA+R"); i2c_send_addr_r(AHT20_ADDRESS); - print_status(" after SLA+R"); - print_status("\r\nbefore read data"); print_data(); - print_status(" after read data"); - - _delay_ms(300); } - print_status("\r\nsend stop"); - i2c_stop(); - print_status(" after stop"); - - while(1); }