2828static volatile uint8_t i2c_tx_complete ;
2929static volatile uint8_t i2c_rx_complete ;
3030static volatile uint8_t i2c_err_detect ;
31+ static volatile uint32_t i2c_err_source ;
3132
3233static volatile uint8_t i2c_slave = 0 ;
3334
@@ -44,13 +45,25 @@ static void ss_i2c_tx(uint32_t dev_id)
4445static void ss_i2c_err (uint32_t dev_id )
4546{
4647 i2c_err_detect = 1 ;
48+ i2c_err_source = dev_id ;
4749}
4850
4951static int wait_rx_or_err (bool no_stop ){
5052 uint64_t timeout = TIMEOUT_MS ;
5153 while (timeout -- ) {
5254 if (i2c_err_detect ) {
53- return I2C_ERROR ;
55+ if (i2c_err_source & I2C_ABRT_7B_ADDR_NOACK )
56+ {
57+ return I2C_ERROR_ADDRESS_NOACK ; // NACK on transmit of address
58+ }
59+ else if (i2c_err_source & I2C_ABRT_TXDATA_NOACK )
60+ {
61+ return I2C_ERROR_DATA_NOACK ; // NACK on transmit of data
62+ }
63+ else
64+ {
65+ return I2C_ERROR_OTHER ; // other error
66+ }
5467 }
5568 if (!no_stop ) {
5669 if (i2c_rx_complete ) {
@@ -69,7 +82,18 @@ static int wait_tx_or_err(bool no_stop){
6982 uint64_t timeout = TIMEOUT_MS ;
7083 while (timeout -- ) {
7184 if (i2c_err_detect ) {
72- return I2C_ERROR ;
85+ if (i2c_err_source & I2C_ABRT_7B_ADDR_NOACK )
86+ {
87+ return I2C_ERROR_ADDRESS_NOACK ; // NACK on transmit of address
88+ }
89+ else if (i2c_err_source & I2C_ABRT_TXDATA_NOACK )
90+ {
91+ return I2C_ERROR_DATA_NOACK ; // NACK on transmit of data
92+ }
93+ else
94+ {
95+ return I2C_ERROR_OTHER ; // other error
96+ }
7397 }
7498 if (!no_stop ) {
7599 if (i2c_tx_complete ) {
@@ -86,16 +110,17 @@ static int wait_tx_or_err(bool no_stop){
86110
87111static int wait_dev_ready (I2C_CONTROLLER controller_id , bool no_stop ){
88112 uint64_t timeout = TIMEOUT_MS ;
113+ int ret = 0 ;
89114 while (timeout -- ) {
90- int ret = ss_i2c_status (controller_id , no_stop );
115+ ret = ss_i2c_status (controller_id , no_stop );
91116 if (ret == I2C_OK ) {
92117 return I2C_OK ;
93118 }
94119 if (ret == I2C_BUSY ) {
95120 delay (1 );
96121 }
97122 }
98- return I2C_TIMEOUT ;
123+ return I2C_TIMEOUT - ret ;
99124}
100125
101126
@@ -122,6 +147,7 @@ int i2c_openadapter(void)
122147 i2c_tx_complete = 0 ;
123148 i2c_rx_complete = 0 ;
124149 i2c_err_detect = 0 ;
150+ i2c_err_source = 0 ;
125151
126152 ss_i2c_set_config (I2C_SENSING_0 , & i2c_cfg );
127153 ss_i2c_clock_enable (I2C_SENSING_0 );
@@ -142,6 +168,7 @@ int i2c_writebytes(uint8_t *bytes, uint8_t length, bool no_stop)
142168
143169 i2c_tx_complete = 0 ;
144170 i2c_err_detect = 0 ;
171+ i2c_err_source = 0 ;
145172 ss_i2c_transfer (I2C_SENSING_0 , bytes , length , 0 , 0 , i2c_slave , no_stop );
146173 ret = wait_tx_or_err (no_stop );
147174 if (ret )
@@ -158,6 +185,7 @@ int i2c_readbytes(uint8_t *buf, int length, bool no_stop)
158185
159186 i2c_rx_complete = 0 ;
160187 i2c_err_detect = 0 ;
188+ i2c_err_source = 0 ;
161189 ss_i2c_transfer (I2C_SENSING_0 , 0 , 0 , buf , length , i2c_slave , no_stop );
162190 ret = wait_rx_or_err (no_stop );
163191 if (ret )
0 commit comments