2323#include "ads1115_internal.h"
2424
2525#include "periph/i2c.h"
26+ #include "byteorder.h"
2627
2728#define ENABLE_DEBUG 0
2829#include "debug.h"
@@ -69,25 +70,23 @@ int ads1115_init(ads1115_t *dev, const ads1115_params_t *params)
6970 i2c_acquire (DEV );
7071
7172 // Test communication
72- uint8_t test_conf [ 2 ] = { 0x00 , ADS1115_CONF_TEST_VALUE } ;
73- if (i2c_write_regs (DEV , ADDR , ADS1115_REG_CONFIG , test_conf , 2 , 0 ) < 0 ) {
73+ uint16_t test_conf = htons ( ADS1115_CONF_TEST_VALUE ) ;
74+ if (i2c_write_regs (DEV , ADDR , ADS1115_REG_CONFIG , & test_conf , sizeof ( test_conf ) , 0 ) < 0 ) {
7475 DEBUG ("[ads1115] init - error: write test failed\n" );
7576 res = ADS1115_NODEV ;
7677 }
7778
78- uint8_t reg [ 2 ] ;
79- if (i2c_read_regs (DEV , ADDR , ADS1115_REG_CONFIG , reg , 2 , 0 ) < 0 ||
80- ((( uint16_t ) reg [ 0 ] << 8 ) | reg [ 1 ] ) != ADS1115_CONF_TEST_VALUE ) {
81- DEBUG ("[ads1115] init - error: read test failed (reg=%02x%02x )\n" , reg [ 0 ], reg [ 1 ] );
79+ uint16_t reg ;
80+ if (i2c_read_regs (DEV , ADDR , ADS1115_REG_CONFIG , & reg , sizeof ( reg ) , 0 ) < 0 ||
81+ ntohs ( reg ) != ADS1115_CONF_TEST_VALUE ) {
82+ DEBUG ("[ads1115] init - error: read test failed (reg=%04x )\n" , ntohs ( reg ) );
8283 res = ADS1115_NOI2C ;
8384 goto release ;
8485 }
8586
8687 // Apply actual configuration
87- uint16_t conf = _build_config_reg (& dev -> params );
88- uint8_t conf_bytes [2 ] = { conf >> 8 , conf & 0xFF };
89-
90- if (i2c_write_regs (DEV , ADDR , ADS1115_REG_CONFIG , conf_bytes , 2 , 0 ) < 0 ) {
88+ uint16_t conf = htons (_build_config_reg (& dev -> params ));
89+ if (i2c_write_regs (DEV , ADDR , ADS1115_REG_CONFIG , & conf , sizeof (conf ), 0 ) < 0 ) {
9190 DEBUG ("[ads1115] init - error: setting config failed\n" );
9291 res = ADS1115_NOI2C ;
9392 goto release ;
@@ -109,22 +108,21 @@ int ads1115_set_ain_ch_input(ads1115_t *dev, ads1115_mux_t mux)
109108 i2c_acquire (DEV );
110109
111110 // Read current configuration
112- uint8_t reg [ 2 ] ;
113- if (i2c_read_regs (DEV , ADDR , ADS1115_REG_CONFIG , reg , 2 , 0 ) < 0 ) {
111+ uint16_t reg ;
112+ if (i2c_read_regs (DEV , ADDR , ADS1115_REG_CONFIG , & reg , sizeof ( reg ) , 0 ) < 0 ) {
114113 i2c_release (DEV );
115114 goto release ;
116115 }
117116
118117 // Update MUX bits
119- uint16_t conf = (( uint16_t ) reg [ 0 ] << 8 ) | reg [ 1 ] ;
118+ uint16_t conf = ntohs ( reg ) ;
120119 conf &= ~(0x07 << ADS1115_CONF_MUX_BIT ); // Clear MUX bits
121120 conf |= (mux << ADS1115_CONF_MUX_BIT ); // Set new MUX
122121
123122 // Write back updated configuration
124- reg [0 ] = conf >> 8 ;
125- reg [1 ] = conf & 0xFF ;
123+ reg = htons (conf );
126124
127- if (i2c_write_regs (DEV , ADDR , ADS1115_REG_CONFIG , reg , 2 , 0 ) < 0 ) {
125+ if (i2c_write_regs (DEV , ADDR , ADS1115_REG_CONFIG , & reg , sizeof ( reg ) , 0 ) < 0 ) {
128126 goto release ;
129127 }
130128
@@ -142,17 +140,17 @@ int ads1115_read_conversion(ads1115_t *dev, uint16_t *value)
142140 assert (dev && value );
143141
144142 int res = ADS1115_NOI2C ;
145- uint8_t buf [ 2 ] ;
143+ uint16_t buf ;
146144
147145 i2c_acquire (DEV );
148146
149147 // Read conversion register
150- if (i2c_read_regs (DEV , ADDR , ADS1115_REG_CONVERSION , buf , 2 , 0 ) < 0 ) {
148+ if (i2c_read_regs (DEV , ADDR , ADS1115_REG_CONVERSION , & buf , sizeof ( buf ) , 0 ) < 0 ) {
151149 goto release ;
152150 }
153151
154152 // Combine bytes into a single value
155- * value = (( int16_t ) buf [ 0 ] << 8 ) | buf [ 1 ] ;
153+ * value = ntohs ( buf ) ;
156154 res = ADS1115_OK ;
157155
158156release :
@@ -163,5 +161,5 @@ int ads1115_read_conversion(ads1115_t *dev, uint16_t *value)
163161int ads1115_convert_to_mv (ads1115_t * dev , uint16_t value )
164162{
165163 assert (dev );
166- return 1000 * value * _ads1115_get_pga_voltage (dev -> params .pga ) / (1 << 15 ); // Msb is sign bit
164+ return value * _ads1115_get_pga_voltage (dev -> params .pga ) / (1 << 15 ); // Msb is sign bit
167165}
0 commit comments