66#define I2C_POLL_TIMEOUT 2000
77#endif
88
9- void i2c_init (uint8_t clkdiv ) {
9+ w_status_t i2c_init (uint8_t clkdiv ) {
1010 // CSTR Enable clocking; S Cleared by hardware after Start; MODE 7-bit address; EN enabled; RSEN
1111 // disabled;
1212 I2C1CON0 = 0x84 ;
@@ -23,9 +23,11 @@ void i2c_init(uint8_t clkdiv) {
2323 I2C1CLK = 0x04 ;
2424 I2C1PIR = 0 ; // ;Clear all the error flags
2525 I2C1ERR = 0 ;
26+
27+ return W_SUCCESS ;
2628}
2729
28- static bool i2c_write (uint8_t address , const uint8_t * data , uint8_t len ) {
30+ static w_status_t i2c_write (uint8_t address , const uint8_t * data , uint8_t len ) {
2931 I2C1ADB1 = (uint8_t )(address << 1 );
3032 I2C1CNT = len ;
3133 I2C1PIRbits .PCIF = 0 ;
@@ -38,30 +40,34 @@ static bool i2c_write(uint8_t address, const uint8_t *data, uint8_t len) {
3840 timeout = 0 ;
3941 while (!I2C1STAT1bits .TXBE && !I2C1ERRbits .NACKIF ) {
4042 if (timeout >= I2C_POLL_TIMEOUT ) {
41- break ;
43+ return W_IO_TIMEOUT ;
4244 }
4345 timeout ++ ;
4446 }
4547 if (I2C1ERRbits .NACKIF ) {
46- break ;
48+ return W_IO_ERROR ;
4749 }
4850 }
4951
5052 timeout = 0 ;
5153 while (!I2C1PIRbits .PCIF ) {
5254 if (timeout >= I2C_POLL_TIMEOUT ) {
53- break ;
55+ return W_IO_TIMEOUT ;
5456 }
5557 timeout ++ ;
5658 }
5759
5860 I2C1PIRbits .PCIF = 0 ;
5961 I2C1STAT1bits .CLRBF = 1 ;
6062
61- return (I2C1ERR & 0x70 ) != 0 ;
63+ if ((I2C1ERR & 0x70 ) != 0 ) {
64+ return W_IO_ERROR ;
65+ }
66+
67+ return W_SUCCESS ;
6268}
6369
64- static bool i2c_read (uint8_t address , uint8_t * data , uint8_t len ) {
70+ static w_status_t i2c_read (uint8_t address , uint8_t * data , uint8_t len ) {
6571 I2C1ADB1 = (uint8_t )((address << 1 ) | 1 );
6672 I2C1CNT = len ;
6773 I2C1PIRbits .PCIF = 0 ;
@@ -73,72 +79,76 @@ static bool i2c_read(uint8_t address, uint8_t *data, uint8_t len) {
7379 timeout = 0 ;
7480 while (!I2C1STAT1bits .RXBF && !I2C1ERRbits .NACKIF ) {
7581 if (timeout >= I2C_POLL_TIMEOUT ) {
76- break ;
82+ return W_IO_TIMEOUT ;
7783 }
7884 timeout ++ ;
7985 }
8086 if (I2C1ERRbits .NACKIF ) {
81- break ;
87+ return W_IO_ERROR ;
8288 }
8389 * data ++ = I2C1RXB ;
8490 }
8591
8692 timeout = 0 ;
8793 while (!I2C1PIRbits .PCIF ) {
8894 if (timeout >= I2C_POLL_TIMEOUT ) {
89- break ;
95+ return W_IO_TIMEOUT ;
9096 }
9197 timeout ++ ;
9298 }
9399 I2C1PIRbits .PCIF = 0 ;
94100 I2C1STAT1bits .CLRBF = 1 ;
95101
96- return (I2C1ERR & 0x70 ) != 0 ;
102+ if ((I2C1ERR & 0x70 ) != 0 ) {
103+ return W_IO_ERROR ;
104+ }
105+
106+ return W_SUCCESS ;
97107}
98108
99- bool i2c_write_data (uint8_t address , const uint8_t * data , uint8_t len ) {
109+ w_status_t i2c_write_data (uint8_t address , const uint8_t * data , uint8_t len ) {
100110 return i2c_write (address , data , len );
101111}
102112
103- bool i2c_read_data (uint8_t address , uint8_t * data , uint8_t len ) {
113+ w_status_t i2c_read_data (uint8_t address , uint8_t * data , uint8_t len ) {
104114 return i2c_read (address , data , len );
105115}
106116
107- bool i2c_write_reg8 (uint8_t address , uint8_t reg , uint8_t val ) {
117+ w_status_t i2c_write_reg8 (uint8_t address , uint8_t reg , uint8_t val ) {
108118 uint8_t data [2 ] = {reg , val };
109119 return i2c_write (address , data , 2 );
110120}
111121
112- bool i2c_write_reg16 (uint8_t address , uint8_t reg , uint16_t val ) {
122+ w_status_t i2c_write_reg16 (uint8_t address , uint8_t reg , uint16_t val ) {
113123 uint8_t data [3 ] = {reg , (uint8_t )(val >> 8 ), (uint8_t )val };
114124 return i2c_write (address , data , 3 );
115125}
116126
117- bool i2c_read_reg8 (uint8_t address , uint8_t reg , uint8_t * value ) {
118- bool success = i2c_write (address , & reg , 1 );
119- if (! success ) {
120- return false ;
127+ w_status_t i2c_read_reg8 (uint8_t address , uint8_t reg , uint8_t * value ) {
128+ w_status_t status = i2c_write (address , & reg , 1 );
129+ if (status != W_SUCCESS ) {
130+ return W_IO_ERROR ;
121131 }
122132
123133 uint8_t data ;
124- success = i2c_read (address , & data , 1 );
125- if (! success ) {
126- return false ;
134+ status = i2c_read (address , & data , 1 );
135+ if (status != W_SUCCESS ) {
136+ return W_IO_ERROR ;
127137 }
128138 * value = data ;
129- return true ;
139+ return W_SUCCESS ;
130140}
131141
132- bool i2c_read_reg16 (uint8_t address , uint8_t reg , uint16_t * value ) {
133- bool success = i2c_write (address , & reg , 1 );
134- if (! success ) {
135- return false ;
142+ w_status_t i2c_read_reg16 (uint8_t address , uint8_t reg , uint16_t * value ) {
143+ w_status_t status = i2c_write (address , & reg , 1 );
144+ if (status != W_SUCCESS ) {
145+ return W_IO_ERROR ;
136146 }
137147 uint8_t data [2 ];
138- success = i2c_read (address , data , 2 );
139- if (! success ) {
140- return false ;
148+ status = i2c_read (address , data , 2 );
149+ if (status != W_SUCCESS ) {
150+ return W_IO_ERROR ;
141151 }
142152 * value = (uint16_t )(data [0 ]) << 8 | data [1 ];
143- return true ;
153+ return W_SUCCESS ;
144154}
0 commit comments