This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

GPS with nRF52 on i2c

Hi,

I am struggling with the Ublox EVA7M with nRF52 on i2c and wonder if anyone can share a sample code of connecting a GPS to the nRF52. I am unable to make the twi work. I am using the twi_sensor example with the GPS address of 0x42U. My goal is to read the GPS messages and print them on USB UART. Can someone check the code below as well, please ?

Thanks !

/*Common addresses definition for GPS. */

#define GPS_ADDR        (0x42U)
#define NUMBER_OF_SAMPLES 256

static volatile bool m_xfer_done = true;
static volatile bool m_set_mode_done = false;
static const nrf_drv_twi_t m_twi_GPS = NRF_DRV_TWI_INSTANCE(0);

static void uart_events_handler(app_uart_evt_t * p_event)
{
    switch (p_event->evt_type)
    {
        case APP_UART_COMMUNICATION_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_communication);
            break;

        case APP_UART_FIFO_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_code);
            break;

        default:
            break;
    }
}

void read_data(int * p_new_sample)
{
    printf("%c", *p_new_sample);
}

static void uart_config(void)
{

    uint32_t                     err_code;
    const app_uart_comm_params_t comm_params =
    {

        RX_PIN_NUMBER,
        TX_PIN_NUMBER,
        RTS_PIN_NUMBER,
        CTS_PIN_NUMBER,
        APP_UART_FLOW_CONTROL_DISABLED,
        false,
        UART_BAUDRATE_BAUDRATE_Baud115200
    };

    APP_UART_FIFO_INIT(&comm_params,
                       UART_RX_BUF_SIZE,
                       UART_TX_BUF_SIZE,
                       uart_events_handler,
                       APP_IRQ_PRIORITY_LOW,
                       err_code);

    APP_ERROR_CHECK(err_code);
}

void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
{
   
    ret_code_t err_code;
    static int m_sample;
    printf("2\n");
    switch(p_event->type)
    {
        case NRF_DRV_TWI_EVT_DONE:
            if ((p_event->type == NRF_DRV_TWI_EVT_DONE) &&
                (p_event->xfer_desc.type == NRF_DRV_TWI_XFER_TX))
            {
                 printf("3\n");
                if(m_set_mode_done != true)
                {
                    m_set_mode_done  = true;
                    return;
                }
                m_xfer_done = false;
                /* Read 4 bytes from the specified address. */
                err_code = nrf_drv_twi_rx(&m_twi_GPS, GPS_ADDR, (uint8_t*)&m_sample, sizeof(m_sample));
                APP_ERROR_CHECK(err_code);
                
            }
            else
            {
                 printf("4\n");
                read_data(&m_sample);
                m_xfer_done = true;
                
            }
            break;
        default:
            break;        
    }   
}


void twi_init (void)
{

    ret_code_t err_code;
    printf("1\n");
    const nrf_drv_twi_config_t twi_GPS_config = {
       .scl                = 26,
       .sda                = 27,
       .frequency          = NRF_TWI_FREQ_100K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH
    };
    
    err_code = nrf_drv_twi_init(&m_twi_GPS, &twi_GPS_config, twi_handler, NULL);
    APP_ERROR_CHECK(err_code);
    
    nrf_drv_twi_enable(&m_twi_GPS);
}

int main(void)
{
    uart_config();
    printf("\n\rTWI sensor example\r\n");
    twi_init();
    uint8_t reg = 0;
    ret_code_t err_code;
    
    while(true)
    {
        nrf_delay_ms(100);
        /* Start transaction with a slave with the specified address. */
        do
        {
            __WFE();
        }while(m_xfer_done == false);
        err_code = nrf_drv_twi_tx(&m_twi_GPS, GPS_ADDR, &reg, sizeof(reg), true);
        //err_code = nrf_drv_twi_rx(&m_twi_GPS, GPS_ADDR, (uint8_t*)&m_sample, sizeof(m_sample));
        printf("%c",reg);
        APP_ERROR_CHECK(err_code);
        m_xfer_done = false;
    }
}

/** @} */
Related