diff options
Diffstat (limited to 'drivers/avr/i2c_slave.c')
-rw-r--r-- | drivers/avr/i2c_slave.c | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/drivers/avr/i2c_slave.c b/drivers/avr/i2c_slave.c index 62a378165..2907f164c 100644 --- a/drivers/avr/i2c_slave.c +++ b/drivers/avr/i2c_slave.c | |||
@@ -17,6 +17,7 @@ | |||
17 | * GitHub repository: https://github.com/g4lvanix/I2C-slave-lib | 17 | * GitHub repository: https://github.com/g4lvanix/I2C-slave-lib |
18 | */ | 18 | */ |
19 | 19 | ||
20 | #include <stddef.h> | ||
20 | #include <avr/io.h> | 21 | #include <avr/io.h> |
21 | #include <util/twi.h> | 22 | #include <util/twi.h> |
22 | #include <avr/interrupt.h> | 23 | #include <avr/interrupt.h> |
@@ -24,6 +25,12 @@ | |||
24 | 25 | ||
25 | #include "i2c_slave.h" | 26 | #include "i2c_slave.h" |
26 | 27 | ||
28 | #if defined(USE_I2C) && defined(SPLIT_COMMON_TRANSACTIONS) | ||
29 | # include "transactions.h" | ||
30 | |||
31 | static volatile bool is_callback_executor = false; | ||
32 | #endif // defined(USE_I2C) && defined(SPLIT_COMMON_TRANSACTIONS) | ||
33 | |||
27 | volatile uint8_t i2c_slave_reg[I2C_SLAVE_REG_COUNT]; | 34 | volatile uint8_t i2c_slave_reg[I2C_SLAVE_REG_COUNT]; |
28 | 35 | ||
29 | static volatile uint8_t buffer_address; | 36 | static volatile uint8_t buffer_address; |
@@ -48,11 +55,14 @@ ISR(TWI_vect) { | |||
48 | case TW_SR_SLA_ACK: | 55 | case TW_SR_SLA_ACK: |
49 | // The device is now a slave receiver | 56 | // The device is now a slave receiver |
50 | slave_has_register_set = false; | 57 | slave_has_register_set = false; |
58 | #if defined(USE_I2C) && defined(SPLIT_COMMON_TRANSACTIONS) | ||
59 | is_callback_executor = false; | ||
60 | #endif // defined(USE_I2C) && defined(SPLIT_COMMON_TRANSACTIONS) | ||
51 | break; | 61 | break; |
52 | 62 | ||
53 | case TW_SR_DATA_ACK: | 63 | case TW_SR_DATA_ACK: |
54 | // This device is a slave receiver and has received data | 64 | // This device is a slave receiver and has received data |
55 | // First byte is the location then the bytes will be writen in buffer with auto-incriment | 65 | // First byte is the location then the bytes will be writen in buffer with auto-increment |
56 | if (!slave_has_register_set) { | 66 | if (!slave_has_register_set) { |
57 | buffer_address = TWDR; | 67 | buffer_address = TWDR; |
58 | 68 | ||
@@ -60,10 +70,25 @@ ISR(TWI_vect) { | |||
60 | ack = 0; | 70 | ack = 0; |
61 | buffer_address = 0; | 71 | buffer_address = 0; |
62 | } | 72 | } |
63 | slave_has_register_set = true; // address has been receaved now fill in buffer | 73 | slave_has_register_set = true; // address has been received now fill in buffer |
74 | |||
75 | #if defined(USE_I2C) && defined(SPLIT_COMMON_TRANSACTIONS) | ||
76 | // Work out if we're attempting to execute a callback | ||
77 | is_callback_executor = buffer_address == split_transaction_table[I2C_EXECUTE_CALLBACK].initiator2target_offset; | ||
78 | #endif // defined(USE_I2C) && defined(SPLIT_COMMON_TRANSACTIONS) | ||
64 | } else { | 79 | } else { |
65 | i2c_slave_reg[buffer_address] = TWDR; | 80 | i2c_slave_reg[buffer_address] = TWDR; |
66 | buffer_address++; | 81 | buffer_address++; |
82 | |||
83 | #if defined(USE_I2C) && defined(SPLIT_COMMON_TRANSACTIONS) | ||
84 | // If we're intending to execute a transaction callback, do so, as we've just received the transaction ID | ||
85 | if (is_callback_executor) { | ||
86 | split_transaction_desc_t *trans = &split_transaction_table[split_shmem->transaction_id]; | ||
87 | if (trans->slave_callback) { | ||
88 | trans->slave_callback(trans->initiator2target_buffer_size, split_trans_initiator2target_buffer(trans), trans->target2initiator_buffer_size, split_trans_target2initiator_buffer(trans)); | ||
89 | } | ||
90 | } | ||
91 | #endif // defined(USE_I2C) && defined(SPLIT_COMMON_TRANSACTIONS) | ||
67 | } | 92 | } |
68 | break; | 93 | break; |
69 | 94 | ||