This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Nrf52832 connect BMX055 with i2c,auto restart problem

Hi,I used Nrf52832 connect BMX055 with i2c, I use sdk nRF52_SDK_11.0.0 example examples\ble_peripheral\ble_app_cscs and add my code to read the BMX055,but when it work a short miniutes the board will restart,when i comment the MAG init and read part ,only read accel and prg.. it can work well ,can anyone help me ?

The following is my code

==============================================

//main.c
/**@brief Function for application main entry.
*/
int main(void)
{
uint32_t err_code;
bool erase_bonds;

// Initialize.
app_trace_init();
timers_init();
// buttons_leds_init(&erase_bonds);
ble_stack_init();
device_manager_init(erase_bonds);
gap_params_init();
advertising_init();
services_init();
sensor_simulator_init();
conn_params_init();

// Start execution.
application_timers_start();
err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
// APP_ERROR_CHECK(err_code);

init_base();
beep();
success_blink();
Twi_init();
send_str("Start\n");
int8_t rslt = 0;
rslt =init_mag();
if(rslt!=0){
send_str("init_mag error\n");
}
uint8_t reg = 0;
// ret_code_t err_code;
rslt =init_accel();
if(rslt!=0){
send_str("init_accel error\n");
}

rslt =init_gyr();
if(rslt!=0){
send_str("init_gyr error\n");
}

nrf_delay_ms(200);
send_str("\nbegin send data\n");
// Enter main loop.
for (;; )
{
power_manage();
// beep();
success_blink();
/* Start transaction with a slave with the specified address. */

send_str("step1\n");
// rslt = read_mag_data();
if(rslt!=0){
send_str("read mag error\n");
}
send_str("step2\n");

rslt = read_accel();
if(rslt!=0){
send_str("read accel error\n");
}
send_str("step3\n");
rslt = read_gyr();
if(rslt!=0){
send_str("read_gyr error\n");
}
send_str("step4\n");
}
}


/**
* @}
*/

============================

//other xxx.c

/*
****************************************************************************
* Copyright (C) 2015 - 2016 Bosch Sensortec GmbH
*
* bmm050_support.c
* Date: 2016/03/17
* Revision: 1.0.6 $
*
* Usage: Sensor Driver support file for BMM050 and BMM150 sensor
*
****************************************************************************
* License:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of the copyright holder nor the names of the
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
* OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
*
* The information provided is believed to be accurate and reliable.
* The copyright holder assumes no responsibility
* for the consequences of use
* of such information nor for any infringement of patents or
* other rights of third parties which may result from its use.
* No license is granted by implication or otherwise under any patent or
* patent rights of the copyright holder.
**************************************************************************/

/*---------------------------------------------------------------------------*/
/* Includes*/
/*---------------------------------------------------------------------------*/
#include "app_util_platform.h"
#include "nrf_delay.h"
#include "nrf_drv_twi.h"
#include "bmm050_support.h"
#include "up_uart.h"

/*----------------------------------------------------------------------------*/
/* The following functions are used for reading and writing of
* sensor data using I2C or SPI communication
*----------------------------------------------------------------------------*/
#define BMM050_API
/* TWI instance ID. */
#define TWI_INSTANCE_ID 0
#define IIC_SCL_PIN 4
#define IIC_SDA_PIN 3
/* TWI instance. */
//static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);
static const nrf_drv_twi_t m_twi_mma_7660 = NRF_DRV_TWI_INSTANCE(0);
AGG_DATA_ST agg_data;
u8 config[2];
u8 temp_data[6]={0};
u8 data[6]={0};
static volatile bool m_xfer_done = true;
ret_code_t r_code;
/*----------------------------------------------------------------------------*
* struct bmm050_t parameters can be accessed by using bmm050
* bmm050_t having the following parameters
* Bus write function pointer: BMM050_WR_FUNC_PTR
* Bus read function pointer: BMM050_RD_FUNC_PTR
* Burst read function pointer: BMM050_BRD_FUNC_PTR
* Delay function pointer: delay_msec
* I2C address: dev_addr
* Chip id of the sensor: chip_id
*---------------------------------------------------------------------------*/
void BEGIN_TX(){
}
void END_TX();
void log_num(uint8_t num,uint8_t *str){
send_num(num,str);
}
void log(uint8_t *str){
send_str(str);
}
void Twi_init(void)
{
ret_code_t err_code;
const nrf_drv_twi_config_t twi_mma_7660_config = {
.scl = IIC_SCL_PIN,
.sda = IIC_SDA_PIN,
// .frequency = TWI_FREQUENCY_FREQUENCY_K100,
.frequency = TWI_FREQUENCY_FREQUENCY_K400,
//.interrupt_priority = APP_IRQ_PRIORITY_HIGH
.interrupt_priority = APP_IRQ_PRIORITY_LOW
};

err_code = nrf_drv_twi_init(&m_twi_mma_7660, &twi_mma_7660_config, NULL, NULL);
log_num(err_code,"twi_init_err=");
//APP_ERROR_CHECK(err_code);
nrf_drv_twi_enable(&m_twi_mma_7660);
send_str("nrf_drv_twi_enabled\n");
}

s8 init_mag(){
u8 dev_addr = 0x10;
// Select Mag register(0x4B)
// Soft reset(0x83)
config[0] = 0x4B;
config[1] = 0x83;
bool flag = false;
r_code = nrf_drv_twi_tx(&m_twi_mma_7660, dev_addr, config,2,flag);

send_num(r_code,"rslt0m=");
r_code += nrf_drv_twi_rx(&m_twi_mma_7660, dev_addr, temp_data, 2);
// Select Mag register(0x4C)
// Normal Mode, ODR = 10 Hz(0x00)

config[0] = 0x4C;
config[1] = 0x00;
send_num(r_code,"rslt1m=");
r_code += nrf_drv_twi_tx(&m_twi_mma_7660, dev_addr, config,2, flag);
send_num(r_code,"rslt2=");
// Select Mag register(0x4E)
// X, Y, Z-Axis enabled(0x84)
config[0] = 0x4E;
config[1] = 0x84;
r_code += nrf_drv_twi_tx(&m_twi_mma_7660, dev_addr, config,2, flag);
send_num(r_code,"rslt3=");
// Select Mag register(0x51)
// No. of Repetitions for X-Y Axis = 9(0x04)
config[0] = 0x51;
config[1] = 0x04;
r_code += nrf_drv_twi_tx(&m_twi_mma_7660, dev_addr, config,2, flag);
send_num(r_code,"rslt4=");
// Select Mag register(0x52)
// No. of Repetitions for Z-Axis = 15(0x0F)
config[0] = 0x52;
config[1] = 0x0F;
r_code += nrf_drv_twi_tx(&m_twi_mma_7660, dev_addr, config,2, flag);
send_num(r_code,"rslt5=");
nrf_delay_ms(2);

return r_code;
}
s8 read_mag_data(){
u8 dev_addr = 0x10;
bool flag = false;
// Read 6 bytes of data from register(0x42)
// xMag lsb, xMag msb, yMag lsb, yMag msb, zMag lsb, zMag msb
config[0] = 0x42;
r_code += nrf_drv_twi_tx(&m_twi_mma_7660, dev_addr, config,1, flag);
//send_num(rslt,"rslt6=");
r_code += nrf_drv_twi_rx(&m_twi_mma_7660, dev_addr, data, 6);
//send_num(rslt,"rslt7=");
s16 xMag = (data[1] * 256 + (data[0] & 0xF8)) / 8;
if(xMag > 4095)
{
xMag -= 8192;
}
// r_data->mx = xMag;
s16 yMag = (data[3] * 256 + (data[2] & 0xF8)) / 8;
if(yMag > 4095)
{
yMag -= 8192;
}
//r_data->my = yMag;
s16 zMag = (data[5] * 256 + (data[4] & 0xFE)) / 2;
if(zMag > 16383)
{
zMag -= 32768;
}
agg_data.mx = xMag;
agg_data.my = yMag;
agg_data.mz = zMag;
log_num(xMag,"xMag=");
log_num(yMag,"yMag=");
log_num(zMag,"zMag=");
return r_code;
}
u8 init_accel(){
bool flag = false;
u8 dev_addr = 0x18;
// Select PMU_Range register(0x0F)
// Range = +/- 2g(0x03)
config[0] = 0x0F;
config[1] = 0x03;
r_code += nrf_drv_twi_tx(&m_twi_mma_7660, dev_addr, config,2,flag);
log_num(r_code,"arslt0=");
r_code += nrf_drv_twi_rx(&m_twi_mma_7660, dev_addr, temp_data, 2);
log_num(r_code,"arslt1=");
// Select PMU_BW register(0x10)
// Bandwidth = 7.81 Hz(0x08)
config[0] = 0x10;
config[1] = 0x08;
r_code += nrf_drv_twi_tx(&m_twi_mma_7660, dev_addr, config,2, flag);
log_num(r_code,"arslt2=");
// Select PMU_LPW register(0x11)
// Normal mode, Sleep duration = 0.5ms(0x00)
config[0] = 0x11;
config[1] = 0x00;
r_code += nrf_drv_twi_tx(&m_twi_mma_7660, dev_addr, config,2, flag);
log_num(r_code,"arslt3=");
nrf_delay_ms(1);
return r_code;
}
u8 read_accel(){
bool flag = false;
u8 dev_addr = 0x18;
config[0] = 0x02;
r_code = nrf_drv_twi_tx(&m_twi_mma_7660, dev_addr, config,1, flag);
//send_num(rslt,"rslt6=");
r_code += nrf_drv_twi_rx(&m_twi_mma_7660, dev_addr, data, 6);
// Convert the data
int xAccl = (data[1] * 256 + (data[0] & 0xF0)) / 16;
if(xAccl > 2047)
{
xAccl -= 4096;
}

int yAccl = (data[3] * 256 + (data[2] & 0xF0)) / 16;
if(yAccl > 2047)
{
yAccl -= 4096;
}

int zAccl = (data[5] * 256 + (data[4] & 0xF0)) / 16;
if(zAccl > 2047)
{
zAccl -= 4096;
}
log_num(xAccl,"xAccl=");
log_num(yAccl,"yAccl=");
log_num(zAccl,"zAccl=");
agg_data.ax = xAccl;
agg_data.ay = yAccl;
agg_data.az = zAccl;
return r_code;
}
u8 init_gyr(){
bool flag = false;
u8 dev_addr = 0x68;
// Select PMU_Range register(0x0F)
// Range = +/- 2g(0x03)
config[0] = 0x0F;
config[1] = 0x04;
r_code = nrf_drv_twi_tx(&m_twi_mma_7660, dev_addr, config,2,flag);
log_num(r_code,"arslt0=");
r_code += nrf_drv_twi_rx(&m_twi_mma_7660, dev_addr, temp_data, 2);
//log_num(rslt,"arslt1=");
// Select PMU_BW register(0x10)
// Bandwidth = 7.81 Hz(0x08)
config[0] = 0x10;
config[1] = 0x07;

r_code += nrf_drv_twi_tx(&m_twi_mma_7660, dev_addr, config,2, flag);
log_num(r_code,"arslt2=");
// Select PMU_LPW register(0x11)
// Normal mode, Sleep duration = 0.5ms(0x00)
config[0] = 0x11;
config[1] = 0x00;
r_code += nrf_drv_twi_tx(&m_twi_mma_7660, dev_addr, config,2, flag);
log_num(r_code,"arslt3=");
nrf_delay_ms(1);
return r_code;
}
u8 read_gyr(){
bool flag = false;
u8 dev_addr = 0x68;
config[0] = 0x02;
r_code = nrf_drv_twi_tx(&m_twi_mma_7660, dev_addr, config,1, flag);
//send_num(rslt,"rslt6=");
r_code += nrf_drv_twi_rx(&m_twi_mma_7660, dev_addr, data, 6);
// Convert the data
int xGyro = (data[1] * 256 + data[0]);
if (xGyro > 32767)
{
xGyro -= 65536;
}

int yGyro = (data[3] * 256 + data[2]);
if (yGyro > 32767)
{
yGyro -= 65536;
}

int zGyro = (data[5] * 256 + data[4]);
if (zGyro > 32767)
{
zGyro -= 65536;
}
log_num(xGyro,"xGyro=");
log_num(yGyro,"yGyro=");
log_num(zGyro,"zGyro=");
agg_data.gx = xGyro;
agg_data.gy = yGyro;
agg_data.gz = zGyro;
return r_code;
}

Parents Reply Children
No Data
Related