This commit is contained in:
2025-08-10 11:41:36 +03:00
parent 74408d8f32
commit 700848f9b2
2 changed files with 3 additions and 27 deletions

View File

@@ -5,7 +5,6 @@
#include "avr/io.h" #include "avr/io.h"
#include "avr/interrupt.h" #include "avr/interrupt.h"
#include "stdbool.h" #include "stdbool.h"
#include "stdio.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"

View File

@@ -20,8 +20,7 @@ typedef enum
MASTER_WRITE, MASTER_WRITE,
MASTER_READ, MASTER_READ,
MASTER_WRITE_REG, MASTER_WRITE_REG,
MASTER_READ_REG, MASTER_READ_REG
MASTER_PROBE
} _work_mode_t; } _work_mode_t;
avr_err_t _zh_avr_i2c_master_start(TickType_t xTicksToWait); 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) 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; 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) 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) switch (TWSR & 0xF8)
{ {
case 0x00: // Bus error. case 0x00: // Bus error.
// printf("00\n");
TWCR = I2C_START | (1 << TWSTO); TWCR = I2C_START | (1 << TWSTO);
xEventGroupSetBitsFromISR(_event_group_handle, I2C_BUS_FAIL, &xHigherPriorityTaskWoken); xEventGroupSetBitsFromISR(_event_group_handle, I2C_BUS_FAIL, &xHigherPriorityTaskWoken);
break; break;
case 0x08: // A START condition has been transmitted. case 0x08: // A START condition has been transmitted.
// printf("08\n");
switch (_work_mode) switch (_work_mode)
{ {
case MASTER_WRITE: case MASTER_WRITE:
@@ -131,7 +124,6 @@ ISR(TWI_vect)
TWDR = (_target_i2c_address << 1) | I2C_MASTER_WRITE; TWDR = (_target_i2c_address << 1) | I2C_MASTER_WRITE;
break; break;
case MASTER_READ: case MASTER_READ:
case MASTER_PROBE:
TWDR = (_target_i2c_address << 1) | I2C_MASTER_READ; TWDR = (_target_i2c_address << 1) | I2C_MASTER_READ;
break; break;
default: default:
@@ -140,22 +132,18 @@ ISR(TWI_vect)
TWCR = I2C_START; TWCR = I2C_START;
break; break;
case 0x10: // A repeated START condition has been transmitted. case 0x10: // A repeated START condition has been transmitted.
// printf("10\n"); // To Do.
// TWCR = I2C_START | (1 << TWSTO);
break; break;
case 0x18: // SLA+W has been transmitted. ACK has been received. case 0x18: // SLA+W has been transmitted. ACK has been received.
// printf("18\n");
TWDR = *(_master_data++); TWDR = *(_master_data++);
--_master_data_size; --_master_data_size;
TWCR = I2C_START; TWCR = I2C_START;
break; break;
case 0x20: // SLA+W has been transmitted. NACK has been received. case 0x20: // SLA+W has been transmitted. NACK has been received.
// printf("20\n");
TWCR = I2C_START | (1 << TWSTO); TWCR = I2C_START | (1 << TWSTO);
xEventGroupSetBitsFromISR(_event_group_handle, I2C_NACK, &xHigherPriorityTaskWoken); xEventGroupSetBitsFromISR(_event_group_handle, I2C_NACK, &xHigherPriorityTaskWoken);
break; break;
case 0x28: // Data byte has been transmitted. ACK has been received. case 0x28: // Data byte has been transmitted. ACK has been received.
// printf("28\n");
if (_master_data_size-- == 0) if (_master_data_size-- == 0)
{ {
TWCR = I2C_START | (1 << TWSTO); TWCR = I2C_START | (1 << TWSTO);
@@ -168,17 +156,14 @@ ISR(TWI_vect)
} }
break; break;
case 0x30: // Data byte has been transmitted. NACK has been received. case 0x30: // Data byte has been transmitted. NACK has been received.
// printf("30\n");
TWCR = I2C_START | (1 << TWSTO); TWCR = I2C_START | (1 << TWSTO);
xEventGroupSetBitsFromISR(_event_group_handle, I2C_NACK, &xHigherPriorityTaskWoken); xEventGroupSetBitsFromISR(_event_group_handle, I2C_NACK, &xHigherPriorityTaskWoken);
break; break;
case 0x38: // Arbitration lost in SLA+W or data bytes. Arbitration lost in SLA+R or NACK bit. 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); TWCR = I2C_START | (1 << TWSTO);
xEventGroupSetBitsFromISR(_event_group_handle, I2C_COLLISION, &xHigherPriorityTaskWoken); xEventGroupSetBitsFromISR(_event_group_handle, I2C_COLLISION, &xHigherPriorityTaskWoken);
break; break;
case 0x40: // SLA+R has been transmitted. ACK has been received. case 0x40: // SLA+R has been transmitted. ACK has been received.
// printf("40\n");
switch (_work_mode) switch (_work_mode)
{ {
case MASTER_WRITE: case MASTER_WRITE:
@@ -197,22 +182,15 @@ ISR(TWI_vect)
break; break;
case MASTER_READ_REG: case MASTER_READ_REG:
break; break;
case MASTER_PROBE:
// TWCR = I2C_START | (1 << TWSTO);
TWCR = I2C_START;
xEventGroupSetBitsFromISR(_event_group_handle, I2C_OK, &xHigherPriorityTaskWoken);
break;
default: default:
break; break;
} }
break; break;
case 0x48: // SLA+R has been transmitted. NACK has been received. case 0x48: // SLA+R has been transmitted. NACK has been received.
// printf("48\n");
TWCR = I2C_START | (1 << TWSTO); TWCR = I2C_START | (1 << TWSTO);
xEventGroupSetBitsFromISR(_event_group_handle, I2C_NACK, &xHigherPriorityTaskWoken); xEventGroupSetBitsFromISR(_event_group_handle, I2C_NACK, &xHigherPriorityTaskWoken);
break; break;
case 0x50: // Data byte has been received. ACK has been returned. case 0x50: // Data byte has been received. ACK has been returned.
// printf("50\n");
*(_master_data++) = TWDR; *(_master_data++) = TWDR;
if (--_master_data_size == 1) if (--_master_data_size == 1)
{ {
@@ -224,7 +202,6 @@ ISR(TWI_vect)
} }
break; break;
case 0x58: // Data byte has been received. NACK has been returned. case 0x58: // Data byte has been received. NACK has been returned.
// printf("58\n");
*(_master_data) = TWDR; *(_master_data) = TWDR;
TWCR = I2C_START | (1 << TWSTO); TWCR = I2C_START | (1 << TWSTO);
xEventGroupSetBitsFromISR(_event_group_handle, I2C_OK, &xHigherPriorityTaskWoken); xEventGroupSetBitsFromISR(_event_group_handle, I2C_OK, &xHigherPriorityTaskWoken);