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

Lets just stick to GPIO?

Lets just stick to GPIO

  1. I am trying to initialize multiple input buttons and multiple output LEDS.

  2. I am trying to glow the LEDS with corresponding buttons. ex:- button 0 glows led0 and so on.

    // Here i can only initialize single input and output But not multiple one. #ifdef BSP_LED_0 #define GPIO_OUTPUT_PIN_NUMBER BSP_LED_0 /**< Pin number for output. */ #endif

    #ifndef GPIO_OUTPUT_PIN_NUMBER #error "Please indicate output pin" #endif

    #ifdef BSP_BUTTON_0 #define GPIO_INPUT_PIN_NUMBER BSP_BUTTON_0 /**< Pin number for output. */ #endif

    #ifndef GPIO_INPUT_PIN_NUMBER #error "Please indicate output pin" #endif

    #define BSP_LED_0 #define BSP_LED_1 LED_2 #define BSP_BUTTON_0 #define BSP_BUTTON_1 BUTTON_2

    int main(void) {

     while(1)
     {
         
         if(BSP_BUTTON_0 == 1) // Here I am facing error every time
         {
             nrf_gpio_port_pin_toggle(BSP_LED_0);
             nrf_delay_ms(500);
             
    
         }
     }
    

    } Explain my mistakes

Parents
  • This should fix your example:

    int main(void)
    {
        //BSP_LED0 and BSP_BUTTON_0 is defined in boards.h file
        
        nrf_gpio_cfg_output(BSP_LED_0);
        nrf_gpio_cfg_input(BSP_BUTTON_0, NRF_GPIO_PIN_PULLUP);
        
        while (true)
        {
            if(nrf_gpio_pin_read(BSP_BUTTON_0) == 0)
            {
                nrf_gpio_pin_toggle(BSP_LED_0);
                nrf_delay_ms(500);
            }
        }
    }
    

    A better solution is to use PPI, this code will set that up:

    void button_led_ppi_init()
    {
        uint32_t gpiote_event_addr;
        uint32_t gpiote_task_addr;
        nrf_ppi_channel_t ppi_channel;
        ret_code_t err_code;
        
        //init GPIOTE driver
        if(!nrf_drv_gpiote_is_init())
        {
            err_code = nrf_drv_gpiote_init();
            APP_ERROR_CHECK(err_code);
        }
        
        //LED
        nrf_drv_gpiote_out_config_t config = GPIOTE_CONFIG_OUT_TASK_TOGGLE(true);
    
        err_code = nrf_drv_gpiote_out_init(BSP_LED_0, &config);
        APP_ERROR_CHECK(err_code);
        
        nrf_drv_gpiote_out_task_enable(BSP_LED_0);
        
        //BUTTON
        nrf_drv_gpiote_in_config_t event_config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(true);
        event_config.pull = NRF_GPIO_PIN_PULLUP;
        
        err_code = nrf_drv_gpiote_in_init(BSP_BUTTON_0, &event_config, NULL);
        APP_ERROR_CHECK(err_code);
        
        nrf_drv_gpiote_in_event_enable(BSP_BUTTON_0, true);
        
        //PPI
        err_code = nrf_drv_ppi_channel_alloc(&ppi_channel);
        APP_ERROR_CHECK(err_code);
        
        gpiote_task_addr = nrf_drv_gpiote_out_task_addr_get(BSP_LED_0);
        gpiote_event_addr = nrf_drv_gpiote_in_event_addr_get(BSP_BUTTON_0);
        
        err_code = nrf_drv_ppi_channel_assign(ppi_channel, gpiote_event_addr, gpiote_task_addr);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_drv_ppi_channel_enable(ppi_channel);
        APP_ERROR_CHECK(err_code);
    }
    
Reply
  • This should fix your example:

    int main(void)
    {
        //BSP_LED0 and BSP_BUTTON_0 is defined in boards.h file
        
        nrf_gpio_cfg_output(BSP_LED_0);
        nrf_gpio_cfg_input(BSP_BUTTON_0, NRF_GPIO_PIN_PULLUP);
        
        while (true)
        {
            if(nrf_gpio_pin_read(BSP_BUTTON_0) == 0)
            {
                nrf_gpio_pin_toggle(BSP_LED_0);
                nrf_delay_ms(500);
            }
        }
    }
    

    A better solution is to use PPI, this code will set that up:

    void button_led_ppi_init()
    {
        uint32_t gpiote_event_addr;
        uint32_t gpiote_task_addr;
        nrf_ppi_channel_t ppi_channel;
        ret_code_t err_code;
        
        //init GPIOTE driver
        if(!nrf_drv_gpiote_is_init())
        {
            err_code = nrf_drv_gpiote_init();
            APP_ERROR_CHECK(err_code);
        }
        
        //LED
        nrf_drv_gpiote_out_config_t config = GPIOTE_CONFIG_OUT_TASK_TOGGLE(true);
    
        err_code = nrf_drv_gpiote_out_init(BSP_LED_0, &config);
        APP_ERROR_CHECK(err_code);
        
        nrf_drv_gpiote_out_task_enable(BSP_LED_0);
        
        //BUTTON
        nrf_drv_gpiote_in_config_t event_config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(true);
        event_config.pull = NRF_GPIO_PIN_PULLUP;
        
        err_code = nrf_drv_gpiote_in_init(BSP_BUTTON_0, &event_config, NULL);
        APP_ERROR_CHECK(err_code);
        
        nrf_drv_gpiote_in_event_enable(BSP_BUTTON_0, true);
        
        //PPI
        err_code = nrf_drv_ppi_channel_alloc(&ppi_channel);
        APP_ERROR_CHECK(err_code);
        
        gpiote_task_addr = nrf_drv_gpiote_out_task_addr_get(BSP_LED_0);
        gpiote_event_addr = nrf_drv_gpiote_in_event_addr_get(BSP_BUTTON_0);
        
        err_code = nrf_drv_ppi_channel_assign(ppi_channel, gpiote_event_addr, gpiote_task_addr);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_drv_ppi_channel_enable(ppi_channel);
        APP_ERROR_CHECK(err_code);
    }
    
Children
No Data
Related