Hi I trying to connect Altimeter ( mpl 3115a2) to the nrf52 development kit. Can someone guide me how to do it (pin connection). I have followed the example given in the sdk but still I am a little unclear. Thank you.
Hi I trying to connect Altimeter ( mpl 3115a2) to the nrf52 development kit. Can someone guide me how to do it (pin connection). I have followed the example given in the sdk but still I am a little unclear. Thank you.
In the twi_master_with_twis_slave example the pins and twi/s instances are defined in the file config.h:
#define TWI_SCL_M 3 //!< Master SCL pin
#define TWI_SDA_M 4 //!< Master SDA pin
#define MASTER_TWI_INST 0 //!< TWI interface used as a master accessing EEPROM memory
The macros are used in twi_master_init():
const nrf_drv_twi_config_t config =
{
.scl = TWI_SCL_M,
.sda = TWI_SDA_M,
...
So in the example you can change TWI_SCL_M and TWI_SDA_M to be 27 and 26 respectively to match your circuit.
The simulated EEPROM (slave) address is also defined in config.h:
#define EEPROM_SIM_ADDR 0x50 //!< Simulated EEPROM TWI address
and the address is used like so:
static ret_code_t eeprom_read(size_t addr, uint8_t * pdata, size_t size)
{
...
ret = nrf_drv_twi_tx(&m_twi_master, EEPROM_SIM_ADDR, &addr8, 1, true);
...
ret = nrf_drv_twi_rx(&m_twi_master, EEPROM_SIM_ADDR, pdata, size, false);
...
}
So EEPROM_SIM_ADDR above would in your altimeter's case be changed to 0xC0 (datasheet p.6. ; 7bit address 0x60 << 1). Otherwise the reads and writes are done similarly to how they're done in the example.
As for the read/write (register) addresses, you need to follow e.g. the sequence in MPL3115A2 datasheet p.10. IIC_RegWrite/Read are analogous to eeprom_write/read in the example (size=1).
I hope I managed to answer your question and didn't just blabber something... :)
In the twi_master_with_twis_slave example the pins and twi/s instances are defined in the file config.h:
#define TWI_SCL_M 3 //!< Master SCL pin
#define TWI_SDA_M 4 //!< Master SDA pin
#define MASTER_TWI_INST 0 //!< TWI interface used as a master accessing EEPROM memory
The macros are used in twi_master_init():
const nrf_drv_twi_config_t config =
{
.scl = TWI_SCL_M,
.sda = TWI_SDA_M,
...
So in the example you can change TWI_SCL_M and TWI_SDA_M to be 27 and 26 respectively to match your circuit.
The simulated EEPROM (slave) address is also defined in config.h:
#define EEPROM_SIM_ADDR 0x50 //!< Simulated EEPROM TWI address
and the address is used like so:
static ret_code_t eeprom_read(size_t addr, uint8_t * pdata, size_t size)
{
...
ret = nrf_drv_twi_tx(&m_twi_master, EEPROM_SIM_ADDR, &addr8, 1, true);
...
ret = nrf_drv_twi_rx(&m_twi_master, EEPROM_SIM_ADDR, pdata, size, false);
...
}
So EEPROM_SIM_ADDR above would in your altimeter's case be changed to 0xC0 (datasheet p.6. ; 7bit address 0x60 << 1). Otherwise the reads and writes are done similarly to how they're done in the example.
As for the read/write (register) addresses, you need to follow e.g. the sequence in MPL3115A2 datasheet p.10. IIC_RegWrite/Read are analogous to eeprom_write/read in the example (size=1).
I hope I managed to answer your question and didn't just blabber something... :)
Thanks, this was really helpful. Just one more doubt, so i will have to use nrf_drv_twi_tx to write to the register right ?
Yes with nrf_drv_twi_tx you write. First the register address, then the data. Like in the eeprom_write example function, something like
uint8_t target_register = 0x26;
nrf_drv_twi_tx(&m_twi_master, ALTIMETER_ADDRESS, &target_register, 1, true);
uint8_t data_to_write = 0xB8;
size_t size = sizeof(data_to_write);
nrf_drv_twi_tx(&m_twi_master, ALTIMETER_ADDRESS, &data_to_write, size, false);
I wrote two functions to read and write register as follows:
//Read and write register
static ret_code_t write_reg( uint8_t slave_address, uint8_t target_register, uint8_t data_to_write) {
ret_code_t ret;
nrf_drv_twi_tx(&m_twi_master, slave_address, &target_register, 1, true);
size_t size = sizeof(data_to_write);
nrf_drv_twi_tx(&m_twi_master, slave_address, &data_to_write, size, false);
return ret;
}
static ret_code_t read_reg( uint8_t slave_address, uint8_t register_address) {
ret_code_t ret;
uint8_t buff[IN_LINE_PRINT_CNT];
nrf_drv_twi_tx(&m_twi_master, slave_address, ®ister_address, 1, true);
nrf_drv_twi_rx(&m_twi_master, slave_address, buff, IN_LINE_PRINT_CNT, false);
return ret;
}
Then according to the datasheet I did the following:
/*Set Altimeter with an OSR = 128 */
write_reg(0x60, 0x26, 0xB8);
/*Enable Data Flags in PT_DATA_CFG*/
write_reg(0X60, 0x13 , 0x07);
/*By Polling*/
/* Set Active*/
write_reg(0x60, 0x26, 0xB9);
/* Read Status register*/
int STA = read_reg(0x60, 0x00);
while ( (STA & 0x08) == 1) {
int OUT_P_MSB = read_reg(0x60, 0x01);
int OUT_P_CSB = read_reg(0x60, 0x02);
int OUT_P_LSB = read_reg(0x60, 0x03);
int OUT_T_MSB = read_reg(0x60, 0x04);
int OUT_T_LSB = read_reg(0x60, 0x05);
printf(" Raw data %d, %d, %d, %d, %d", OUT_P_MSB, OUT_P_CSB, OUT_P_LSB, OUT_T_MSB, OUT_T_LSB);
}
But still I am not getting any output. But if I use it with the do_print_data and put the EEPROM_SIM_ADDR as 0x60 I get some random address but with 0xC0 I do not get any output. Any idea why?
Your read_reg() returns the unitialized ret
, not the actual register value.
Poll the STATUS register until it is 1 and then do the reading. Currently you read the register once and it's too early for the data to be ready.
So you should have something like
// reads a single 8-bit register and returns its value
static uint8_t read_reg( uint8_t slave_address, uint8_t register_address) {
uint8_t buff;
nrf_drv_twi_tx(&m_twi_master, slave_address, ®ister_address, 1, true);
nrf_drv_twi_rx(&m_twi_master, slave_address, &buff, 1, false);
return buff;
}
and
// initialization, activation
// read the status register until PTDR is set
while( !(read_reg(0x60, 0x00) & 0x08) );
// data should be ready when we get out of the while loop
// read_reg() and print