diff --git a/include/zh_avr_i2c.h b/include/zh_avr_i2c.h index bcec575..df55eb2 100644 --- a/include/zh_avr_i2c.h +++ b/include/zh_avr_i2c.h @@ -5,7 +5,6 @@ #include "avr/io.h" #include "avr/interrupt.h" #include "stdbool.h" -#include "stdio.h" #ifdef __cplusplus extern "C" diff --git a/zh_avr_i2c.c b/zh_avr_i2c.c index a305be7..d9c4786 100644 --- a/zh_avr_i2c.c +++ b/zh_avr_i2c.c @@ -20,8 +20,7 @@ typedef enum MASTER_WRITE, MASTER_READ, MASTER_WRITE_REG, - MASTER_READ_REG, - MASTER_PROBE + MASTER_READ_REG } _work_mode_t; avr_err_t _zh_avr_i2c_master_start(TickType_t xTicksToWait); @@ -52,12 +51,8 @@ avr_err_t zh_avr_i2c_master_init(const bool pullup) avr_err_t zh_avr_i2c_master_probe(const uint8_t addr, TickType_t xTicksToWait) { - // ZH_ERROR_CHECK(_master_is_initialized == true, AVR_ERR_INVALID_STATE); - // _work_mode = MASTER_PROBE; - // _target_i2c_address = addr; - // return _zh_avr_i2c_master_start(xTicksToWait); uint8_t temp = 0; - return zh_avr_i2c_master_transmit(addr, &temp, 1, xTicksToWait); + return zh_avr_i2c_master_transmit(addr, &temp, sizeof(temp), xTicksToWait); } avr_err_t zh_avr_i2c_master_transmit(const uint8_t addr, uint8_t *data, uint8_t size, TickType_t xTicksToWait) @@ -117,12 +112,10 @@ ISR(TWI_vect) switch (TWSR & 0xF8) { case 0x00: // Bus error. - // printf("00\n"); TWCR = I2C_START | (1 << TWSTO); xEventGroupSetBitsFromISR(_event_group_handle, I2C_BUS_FAIL, &xHigherPriorityTaskWoken); break; case 0x08: // A START condition has been transmitted. - // printf("08\n"); switch (_work_mode) { case MASTER_WRITE: @@ -131,7 +124,6 @@ ISR(TWI_vect) TWDR = (_target_i2c_address << 1) | I2C_MASTER_WRITE; break; case MASTER_READ: - case MASTER_PROBE: TWDR = (_target_i2c_address << 1) | I2C_MASTER_READ; break; default: @@ -140,22 +132,18 @@ ISR(TWI_vect) TWCR = I2C_START; break; case 0x10: // A repeated START condition has been transmitted. - // printf("10\n"); - // TWCR = I2C_START | (1 << TWSTO); + // To Do. break; case 0x18: // SLA+W has been transmitted. ACK has been received. - // printf("18\n"); TWDR = *(_master_data++); --_master_data_size; TWCR = I2C_START; break; case 0x20: // SLA+W has been transmitted. NACK has been received. - // printf("20\n"); TWCR = I2C_START | (1 << TWSTO); xEventGroupSetBitsFromISR(_event_group_handle, I2C_NACK, &xHigherPriorityTaskWoken); break; case 0x28: // Data byte has been transmitted. ACK has been received. - // printf("28\n"); if (_master_data_size-- == 0) { TWCR = I2C_START | (1 << TWSTO); @@ -168,17 +156,14 @@ ISR(TWI_vect) } break; case 0x30: // Data byte has been transmitted. NACK has been received. - // printf("30\n"); TWCR = I2C_START | (1 << TWSTO); xEventGroupSetBitsFromISR(_event_group_handle, I2C_NACK, &xHigherPriorityTaskWoken); break; case 0x38: // Arbitration lost in SLA+W or data bytes. Arbitration lost in SLA+R or NACK bit. - // printf("38\n"); TWCR = I2C_START | (1 << TWSTO); xEventGroupSetBitsFromISR(_event_group_handle, I2C_COLLISION, &xHigherPriorityTaskWoken); break; case 0x40: // SLA+R has been transmitted. ACK has been received. - // printf("40\n"); switch (_work_mode) { case MASTER_WRITE: @@ -197,22 +182,15 @@ ISR(TWI_vect) break; case MASTER_READ_REG: break; - case MASTER_PROBE: - // TWCR = I2C_START | (1 << TWSTO); - TWCR = I2C_START; - xEventGroupSetBitsFromISR(_event_group_handle, I2C_OK, &xHigherPriorityTaskWoken); - break; default: break; } break; case 0x48: // SLA+R has been transmitted. NACK has been received. - // printf("48\n"); TWCR = I2C_START | (1 << TWSTO); xEventGroupSetBitsFromISR(_event_group_handle, I2C_NACK, &xHigherPriorityTaskWoken); break; case 0x50: // Data byte has been received. ACK has been returned. - // printf("50\n"); *(_master_data++) = TWDR; if (--_master_data_size == 1) { @@ -224,7 +202,6 @@ ISR(TWI_vect) } break; case 0x58: // Data byte has been received. NACK has been returned. - // printf("58\n"); *(_master_data) = TWDR; TWCR = I2C_START | (1 << TWSTO); xEventGroupSetBitsFromISR(_event_group_handle, I2C_OK, &xHigherPriorityTaskWoken);