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

NRF51822qfaa,s110 v8.0,TWI question,please help me.

I use NRF51822qfaa,s110 v8.0, now I use master TWI to drive the TWI bus chip. the TWI bus chip Slave Address is 0x48.please see the below program .

question:

1.The slave_add is 0X24,is OK?if is not ,please tell me the right value.

2.The nrf_drv_twi_rx function is return 3,is not 0,why?please tell me which is wrong?

3.Who can offert an example use twi for init/read/write chip?

  #define slave_add  0x24

  static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(0);

  uint8_t reg[10] = {0,0};

  void twi_init(void)
  {
    ret_code_t err_code;
    nrf_drv_twi_config_t    p_twi_config;

    // = NRF_DRV_TWI_DEFAULT_CONFIG(1);
    // Set up TWI configuration default values
    p_twi_config.frequency=NRF_TWI_FREQ_100K;
    p_twi_config.scl=TWI0_CONFIG_SCL;
    p_twi_config.sda=TWI0_CONFIG_SDA;
    p_twi_config.interrupt_priority=APP_IRQ_PRIORITY_LOW;

    err_code = nrf_drv_twi_init(&m_twi, &p_twi_config, 0);
    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&m_twi);

    reg[0]=0x00;

    nrf_delay_ms(5); 

    err_code = nrf_drv_twi_rx(&m_twi, slave_add, reg, 1, false);//true);
    nrf_delay_ms(50); 
  }
Parents
  • Hi

    1. The slave_add is 0X24,is OK: This depends. My guess is that it should stay at 0x48. Please have a look at my answer in this thread.
    2. If you look at the declaration of nrf_drv_twi_rx() in nrf_drv_twi.h and the error codes defined in nrf_error.h you will see that error code 3 signify // NRF_ERROR_INTERNAL If an error was detected by hardware.. This error might be caused by many things. It might for example very well be that you are using the wrong address so your slave doesn't acknowledge your transmissions.
    3. You can try this example. It is a very basic example that will show you how to use the drivers with an event handler and what address to use.

    Regarding your code snippet: It looks like you are trying to do a TWI read operation immediately after you have enabled the TWI. I don't know what kind of TWI slave you are using, but it is common that you first have to do a TWI write operation to tell your slave what address to read from. Here are some more advanced TWI examples showing how to interface an accelerometer. In the driver file, mpu.c, you can see how one can read registers in the function called mpu_read_registers().

Reply
  • Hi

    1. The slave_add is 0X24,is OK: This depends. My guess is that it should stay at 0x48. Please have a look at my answer in this thread.
    2. If you look at the declaration of nrf_drv_twi_rx() in nrf_drv_twi.h and the error codes defined in nrf_error.h you will see that error code 3 signify // NRF_ERROR_INTERNAL If an error was detected by hardware.. This error might be caused by many things. It might for example very well be that you are using the wrong address so your slave doesn't acknowledge your transmissions.
    3. You can try this example. It is a very basic example that will show you how to use the drivers with an event handler and what address to use.

    Regarding your code snippet: It looks like you are trying to do a TWI read operation immediately after you have enabled the TWI. I don't know what kind of TWI slave you are using, but it is common that you first have to do a TWI write operation to tell your slave what address to read from. Here are some more advanced TWI examples showing how to interface an accelerometer. In the driver file, mpu.c, you can see how one can read registers in the function called mpu_read_registers().

Children
No Data
Related