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

how to use hardware spi and i2c on S130 softdevice(nRF51822)??

Hello,

I am developing a new product using S130 on nRF51822(QFAC). My product use LCD as spi interface and ADXL345 sensor as i2c interface, also test it, works well without softdevice, but when I upload S130 softdevice it goes to h/w fault.

I searched similar problem in this forum, but I cannot find clear solution. Is it possible use hardware spi and i2c on s130 softdevice concurrently?? Please let me know how to handle this problem.

ble_app_uart_on_SDK_v10_0_0.zip

Thanks,

  • It should be possible. Could you include some code that shows how you are using SPI and I2C? Are you using interrupts? What interrupt priority?

  • I am not using any interrupt, just polling style is used. Okay my codes are below.

    twi init code:

    void twi_init (void)
    {
        ret_code_t err_code;
            const nrf_drv_twi_config_t twi_adxl_345_config = {
           .scl                = POLARIS_SENSOR_SCL,//ARDUINO_SCL_PIN,
           .sda                = POLARIS_SENSOR_SDA,//ARDUINO_SDA_PIN,
           .frequency          = NRF_TWI_FREQ_100K,
           .interrupt_priority = APP_IRQ_PRIORITY_HIGH  // interrupt priority
      };
        err_code = nrf_drv_twi_init(&m_twi_adxl_345, &twi_adxl_345_config, NULL, NULL);
        APP_ERROR_CHECK(err_code);
        nrf_drv_twi_enable(&m_twi_adxl_345);
    }
    

    spi code:

    static void spi_master_init(nrf_drv_spi_t const * p_instance, bool lsb)
    {
        uint32_t err_code = NRF_SUCCESS;
    
        nrf_drv_spi_config_t config =
        {
            .ss_pin       = LCD_CS,//NRF_DRV_SPI_PIN_NOT_USED,
            .irq_priority = APP_IRQ_PRIORITY_LOW,
            .orc          = 0xCC,
            .frequency    = NRF_DRV_SPI_FREQ_125K,
            .mode         = NRF_DRV_SPI_MODE_3,//NRF_DRV_SPI_MODE_0,
            .bit_order    = (lsb ?
                NRF_DRV_SPI_BIT_ORDER_LSB_FIRST : NRF_DRV_SPI_BIT_ORDER_MSB_FIRST),
        };
    
            config.sck_pin  = LCD_SCK_PIN;//SPIM1_SCK_PIN;
            config.mosi_pin = LCD_MOSI_PIN;//SPIM1_MOSI_PIN;
            config.miso_pin = NRF_DRV_SPI_PIN_NOT_USED;//LCD_MISO_PIN;//SPIM1_MISO_PIN;
            err_code = nrf_drv_spi_init(p_instance, &config,
                NULL);//spi_master_1_event_handler);  
        APP_ERROR_CHECK(err_code);
    }
    

    the above codes are just init codes.

    in spi case, i used it for LCD display, just send data so no need to get data. in twi case, for sensor IC, just read data.

    Thank you for your comment.

  • Initialization looks ok. When does the hardfault happen? How and when are you using the SPI and TWI? What are you doing with the SoftDevice? Does the project work if you don't use TWI or SPI? Please consider editing the question and include your complete project with SoftDevice and SDK version.

  • I tested again, init code is ok but it can not work when execute twi_transfer();

    ret_code_t nrf_drv_twi_tx(nrf_drv_twi_t const * const p_instance, uint8_t address, uint8_t const * p_data, uint32_t length, bool xfer_pending) { return twi_transfer(p_instance, address, p_data, length, xfer_pending, true); }

    I am using SPI and TWI after BLE stack initiated. I want to trasfer some data to smart phone through BLE.

    S130 v1.0.1

    nRF51_SDK_10.0.0

    The above is my project environment. please consider it.

    I uploaded my complete project codes at main question, please find it.

    project code

    Thanks,

  • I believe it is going to hard fault because you are accessing an invalid memory location when you try to set the TWI0 address register. You should call something like ADXL375_I2C_init() so that the driver knows the memory address of the TWI peripheral. If you are not calling ADXL375_I2C_init() without SoftDevice I would guess the memory address is still incorrect, but it may not cause a hard fault. That is what I experienced at least.

Related