Qiao li
/
MAX86150_evkit_board_OK
none
Diff: i2c_functions.h
- Revision:
- 2:7d005ac4146f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/i2c_functions.h Fri Apr 12 06:56:27 2019 +0000 @@ -0,0 +1,58 @@ +#ifndef __I2C_FUNCTIONS_H__ +#define __I2C_FUNCTIONS_H__ + +#include "mbed.h" + +/* + *************************************************************************************** + * + * Functions for I2C Communication + * + *************************************************************************************** + */ + +static I2C i2c(P2_6,P2_7); + +static int i2c_freq (int freqHz ) +{ + i2c.frequency(freqHz); +} + +static int i2c_write (uint8_t i2c_addr, uint8_t register_addr, char* buffer, uint8_t Nbyte ) +{ + int ret; + char *tmp_buffer; + + tmp_buffer = (char*)malloc(sizeof(char)*(Nbyte+1)); + + /* First, send device address. Then, send data and STOP condition */ + tmp_buffer[0] = register_addr; + memcpy(tmp_buffer+1, buffer, Nbyte); + + ret = i2c.write(i2c_addr, tmp_buffer, Nbyte+1, false); + + return ret; +} + +static int i2c_read (uint8_t i2c_addr, uint8_t register_addr, char* buffer, uint8_t Nbyte ) +{ + int ret; + + /* Send device address, with no STOP condition */ + ret = i2c.write(i2c_addr, (const char*)®ister_addr, 1, true); + if(!ret) { + /* Read data, with STOP condition */ + ret = i2c.read((i2c_addr|0x01), buffer, Nbyte, false); + } + + return ret; +} + +/* + *************************************************************************************** + * + * Functions for .... + * + *************************************************************************************** + */ + #endif \ No newline at end of file