Build failed Completed nRF52840 with ADXL345

Hi,

This post is the continuation of this project:  Undefined reference to `SystemInit' and undefined reference to "bsp_board_init"

It turns out that I probably didn't include the c. and h. files well into the project so I decided to create a new one based on it yet. The program worked until I deleted the first version. It turned out that I copy paste some part of it including the path of the include files. So when I deleted the first version, the include path didn't exist anymore. So I wrote again the good one and now I can't even build anymore.

Here is the code of ADXL345.c :

#include "ADXL345.h"




//flag generated dans when interrupt of SPI com. Become True after a frame is sent.
void spi_event_handler(nrf_drv_spi_evt_t const * p_event, void * p_context)
{
    spi_xfer_done = true;
    //nrf_gpio_pin_set(ADXL345_CS_PIN);
}

void SPI_Init(void){
    nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
    spi_config.ss_pin = ADXL345_CS_PIN;
    spi_config.miso_pin = ADXL345_MISO_PIN;
    spi_config.mosi_pin = ADXL345_MOSI_PIN;
    spi_config.sck_pin = ADXL345_SCK_PIN;
    spi_config.frequency = NRF_DRV_SPI_FREQ_1M;
    spi_config.mode = NRF_DRV_SPI_MODE_3;
    APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));

    nrf_gpio_cfg_output(ADXL345_CS_PIN);
    nrf_gpio_pin_set(ADXL345_CS_PIN);
    }

void ADXL345_SPI_readRegister(uint8_t address, uint8_t * rx_data, uint8_t bytes)
{
    uint8_t tx_data; 
    if(bytes > 1)
    {
        tx_data = (0x80|address)|0x40;
    }
    else
    {
        tx_data = 0x80|address;
    }
    
    nrf_gpio_pin_clear(ADXL345_CS_PIN);
    spi_xfer_done=false;
    nrf_drv_spi_transfer(&spi, &tx_data, bytes+1, rx_data, bytes+1);
    while(!spi_xfer_done){}
    nrf_gpio_pin_set(ADXL345_CS_PIN);
}

void ADXL345_SPI_writeRegister(uint8_t reg_addr, uint8_t * p_data, uint8_t bytes)
{
    uint8_t tx_data[2];
    uint8_t rx_data[2];
    if(bytes > 1)
    {
        tx_data[0] = (reg_addr|0x40);
    }
    else
    {
        tx_data[0] = reg_addr;
    }
    
    tx_data[1]=*p_data;
    
    nrf_gpio_pin_clear(ADXL345_CS_PIN);
    spi_xfer_done=false;
    nrf_drv_spi_transfer(&spi, tx_data, sizeof(tx_data), rx_data, sizeof(rx_data));
    while(!spi_xfer_done){}
    nrf_gpio_pin_set(ADXL345_CS_PIN);
}

void adxl345_init(void)
{

    //Read measurement mode
    uint8_t rx_data[2];
    ADXL345_SPI_readRegister(ADXL345_REG_POWER_CTL, &rx_data[0], 1);
    NRF_LOG_INFO( "Read measurement mode avec fonction: 0x%02X \n",rx_data[1]);
    
    //Read test
    ADXL345_SPI_readRegister(0x00, &rx_data[0], 1);
    NRF_LOG_INFO( "TEST: 0x%02X \r",rx_data[1]);
    
    //Set measurement mode
    uint8_t tx_data[2] = {ADXL345_REG_POWER_CTL, 0x08u}; 
    ADXL345_SPI_writeRegister(ADXL345_REG_POWER_CTL, &tx_data[1], 1);
    NRF_LOG_INFO( "Set measurement mode with function: done \r");
    
    //Read measurement mode
    ADXL345_SPI_readRegister(ADXL345_REG_POWER_CTL, rx_data, 1);
    NRF_LOG_INFO( "Read measurement mode avec fonction: 0x%02X \r",rx_data[1]);
   
    //Compare value sent and received 
    if (rx_data[1]!=tx_data[1]) { NRF_LOG_INFO( "Measurement mode sent: 0x%02X \n Measure mode received: 0x%02X \n",tx_data[1], rx_data[1]);}
    else {NRF_LOG_INFO( "Same measure mode sent and received");}

    //Set data rate
    uint8_t tx_data_2[2] = {ADXL345_REG_BW_RATE, 0x0A}; 
    ADXL345_SPI_writeRegister(ADXL345_REG_BW_RATE, &tx_data_2[1], 1);
    NRF_LOG_INFO( "Set data rate with function: done \r");

    // Read data rate
    ADXL345_SPI_readRegister(ADXL345_REG_BW_RATE, rx_data, 1);
    NRF_LOG_INFO( "Read data rate with fonction: 0x%02X \r",rx_data[1]);

    //Compare value sent and received 
    if (rx_data[1]!=tx_data_2[1]) { NRF_LOG_INFO( "Data rate sent: 0x%02X \n Data rate received: 0x%02X \n",tx_data_2[1], rx_data[1]);}
    else {NRF_LOG_INFO( "Same data rate sent and received");}

    //Set resolution and range
    uint8_t tx_data_3[2] = {ADXL345_REG_DATA_FORMAT, 0x01}; 
    ADXL345_SPI_writeRegister(ADXL345_REG_DATA_FORMAT, &tx_data_3[1], 1);
    NRF_LOG_INFO( "Set resolution and range: done \r");
    
    //Read resolution range
    ADXL345_SPI_readRegister(ADXL345_REG_DATA_FORMAT, rx_data, 1);
    NRF_LOG_INFO( "Read resolution and range with fonction: 0x%02X \r",rx_data[1]);

    //Compare value sent and received 
    if (rx_data[1]!=tx_data_3[1]) { NRF_LOG_INFO( "Resolution and range sent: 0x%02X \n Resolution and range received: 0x%02X \n",tx_data_3[1], rx_data[1]);}
    else {NRF_LOG_INFO( "Same resolution and range sent and received");}

    // Set FIFO CTL
    uint8_t tx_data_4[2] = {ADXL345_REG_FIFO_CTRL, 0x80}; 
    ADXL345_SPI_writeRegister(ADXL345_REG_FIFO_CTRL, &tx_data_4[1], 1);
    NRF_LOG_INFO( "Set FIFO CTL: done \r");

    //Read FIFO CTL
    ADXL345_SPI_readRegister(ADXL345_REG_FIFO_CTRL, rx_data, 1);
    NRF_LOG_INFO( "Read FIFO CTL with fonction: 0x%02X \n",rx_data[1]);
    
    if (rx_data[1]!=tx_data_4[1]) { NRF_LOG_INFO( "FIFO_CTL sent: 0x%02X \n FIFO_CTL received: 0x%02X \n",tx_data_4[1], rx_data[1]);}
    else {NRF_LOG_INFO( "FIFO CTL same value sent and received");}
}

bool  adxl345_read_fifo_status(void)
{
  
  // Read FIFO status and return true if there are values to read
  uint8_t tx_data[2] = {ADXL345_REG_FIFO_STS|0x80, 0};
  uint8_t rx_data[2];
  ADXL345_SPI_readRegister(ADXL345_REG_FIFO_STS, rx_data, 1);
  return (bool)((rx_data[1] & 0x3Fu) != 0u);
  
}



void adxl345_read_acceleration(int16_t *x, int16_t *y, int16_t *z)
{
    uint8_t tx_data = ADXL345_REG_DATAX0 | 0x80 |0x40;
    uint8_t rx_data[7] = {0, 0, 0, 0, 0, 0, 0};
    ADXL345_SPI_readRegister(ADXL345_REG_DATAX0, rx_data, 6);

    *x = (((int16_t)rx_data[2] << 8 | rx_data[1])) * SCALE_FACTOR_4G;
    *y = (((int16_t)rx_data[4] << 8 | rx_data[3])) * SCALE_FACTOR_4G;
    *z = (((int16_t)rx_data[6] << 8 | rx_data[5])) * SCALE_FACTOR_4G;
}

Here is the code of the main.c :

#include "ADXL345.h"


int main(void)
{


    //Initialize the LEDs on board to use them
    bsp_board_init(BSP_INIT_LEDS); 

    // Initialize the Logger module and check if any error occured during initialization
    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
	
    // Initialize the default backends for nrf logger
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    // print the log msg over uart port
    NRF_LOG_INFO("This is log data from nordic device..");
    NRF_LOG_INFO("Initialisation \n");

    int16_t x, y, z;
    float xg, yg, zg;
 
    SPI_Init();
    adxl345_init();

    while (true)
    {
        if (adxl345_read_fifo_status() != false)
        {
          //NRF_LOG_INFO( "Range acceleromter: %X \n", adxl345_read_range())
          adxl345_read_acceleration(&x, &y, &z);
          xg= (float)x *SCALE_FACTOR_4G /(pow(10,3));
          yg= (float)y *SCALE_FACTOR_4G/(pow(10,3));
          zg= (float)z *SCALE_FACTOR_4G/(pow(10,3));
          NRF_LOG_INFO("x avec facteur: "NRF_LOG_FLOAT_MARKER" g, x: %d \r", NRF_LOG_FLOAT(xg), x);
          NRF_LOG_INFO("y avec facteur: "NRF_LOG_FLOAT_MARKER" g, y: %d \r", NRF_LOG_FLOAT(yg), y);
          NRF_LOG_INFO("z avec facteur: "NRF_LOG_FLOAT_MARKER" g, z: %d \r\n", NRF_LOG_FLOAT(zg), z);
        }
        NRF_LOG_FLUSH();
        nrf_delay_ms(100);
    }
}

I also created an header for the ADXL345 with all the declarations and definitions.

The following picture is the output I get:

I really need your help because it doesn't make any sense for me that it used to work and now it doesn't anymore.

Looking forward for your answer,

Kind regards

Parents Reply Children
No Data
Related