int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id) { if (pin == NC) { return -1; } MBED_ASSERT((uint32_t)pin < GPIO_PIN_COUNT); (void) nrf_drv_gpiote_init(); gpiote_pin_uninit(pin); // try to uninitialize gpio before a change. m_gpio_cfg[pin].used_as_irq = true; m_channel_ids[pin] = id; obj->ch = pin; m_irq_handler = handler; m_channel_ids[pin] = id; gpio_apply_config(pin); return 1; }
void setup_example(void) { uint32_t event; nrf_ppi_channel_t ppi_channel; uint32_t err_code; nrf_gpio_cfg_output(BSP_LED_0); err_code = nrf_drv_rtc_init(&rtc, NULL, rtc_evt_handler); APP_ERROR_CHECK(err_code); nrf_drv_rtc_tick_enable(&rtc, false); event = nrf_drv_rtc_event_address_get(&rtc, NRF_RTC_EVENT_TICK); if (!nrf_drv_gpiote_is_init()) { err_code = nrf_drv_gpiote_init(); APP_ERROR_CHECK(err_code); } nrf_drv_gpiote_out_config_t pin_out_config = GPIOTE_CONFIG_OUT_TASK_TOGGLE(false); err_code = nrf_drv_gpiote_out_init(BSP_LED_0,&pin_out_config); APP_ERROR_CHECK(err_code); nrf_drv_gpiote_out_task_enable(BSP_LED_0); uint32_t gpiote_task_addr = nrf_drv_gpiote_out_task_addr_get(BSP_LED_0); err_code = nrf_drv_ppi_init(); APP_ERROR_CHECK(err_code); err_code = nrf_drv_ppi_channel_alloc(&ppi_channel); APP_ERROR_CHECK(err_code); err_code = nrf_drv_ppi_channel_assign(ppi_channel,event,gpiote_task_addr); APP_ERROR_CHECK(err_code); err_code = nrf_drv_ppi_channel_enable(ppi_channel); APP_ERROR_CHECK(err_code); nrf_drv_rtc_enable(&rtc); }
static void gpio_init(void) { ret_code_t err_code; err_code = nrf_drv_gpiote_init(); APP_ERROR_CHECK(err_code); nrf_drv_gpiote_out_config_t out_config = GPIOTE_CONFIG_OUT_SIMPLE(false); err_code = nrf_drv_gpiote_out_init(PIN_OUT, &out_config); APP_ERROR_CHECK(err_code); nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(true); in_config.pull = NRF_GPIO_PIN_PULLUP; err_code = nrf_drv_gpiote_in_init(PIN_IN, &in_config, in_pin_handler); APP_ERROR_CHECK(err_code); nrf_drv_gpiote_in_event_enable(PIN_IN, true); }
uint32_t app_button_init(app_button_cfg_t * p_buttons, uint8_t button_count, uint32_t detection_delay) { uint32_t err_code; if (detection_delay < APP_TIMER_MIN_TIMEOUT_TICKS) { return NRF_ERROR_INVALID_PARAM; } if (!nrf_drv_gpiote_is_init()) { err_code = nrf_drv_gpiote_init(); VERIFY_SUCCESS(err_code); } // Save configuration. mp_buttons = p_buttons; m_button_count = button_count; m_detection_delay = detection_delay; m_pin_state = 0; m_pin_transition = 0; while (button_count--) { app_button_cfg_t * p_btn = &p_buttons[button_count]; nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(false); config.pull = p_btn->pull_cfg; err_code = nrf_drv_gpiote_in_init(p_btn->pin_no, &config, gpiote_event_handler); VERIFY_SUCCESS(err_code); } // Create polling timer. return app_timer_create(&m_detection_delay_timer_id, APP_TIMER_MODE_SINGLE_SHOT, detection_delay_timeout_handler); }
/** * @brief GPIO初期化 */ static void gpio_init(void) { ret_code_t err_code; nrf_drv_gpiote_out_config_t out_config = GPIOTE_CONFIG_OUT_SIMPLE(true); /* 初期値 : 1 */ nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_HITOLO(true); /* 立ち下がり,EVENT使用 */ err_code = nrf_drv_gpiote_init(); APP_ERROR_CHECK(err_code); /* output */ err_code = nrf_drv_gpiote_out_init(LED_PIN_NO_ADVERTISING, &out_config); APP_ERROR_CHECK(err_code); err_code = nrf_drv_gpiote_out_init(LED_PIN_NO_CONNECTED, &out_config); APP_ERROR_CHECK(err_code); err_code = nrf_drv_gpiote_out_init(LED_PIN_NO_ASSERT, &out_config); APP_ERROR_CHECK(err_code); /* input */ in_config.pull = NRF_GPIO_PIN_NOPULL; err_code = nrf_drv_gpiote_in_init(RCS730_IRQ, &in_config, gpiote_irq_handler); APP_ERROR_CHECK(err_code); nrf_drv_gpiote_in_event_enable(RCS730_IRQ, true); }
uint32_t app_uart_init(const app_uart_comm_params_t * p_comm_params, app_uart_buffers_t * p_buffers, app_uart_event_handler_t event_handler, app_irq_priority_t irq_priority, uint16_t * p_app_uart_uid) { uint32_t err_code; m_current_state = UART_OFF; m_event_handler = event_handler; m_rx_byte = BYTE_INVALID; // Configure RX and TX pins. nrf_gpio_pin_set(p_comm_params->tx_pin_no); nrf_gpio_cfg_output(p_comm_params->tx_pin_no); nrf_gpio_cfg_input(p_comm_params->rx_pin_no, NRF_GPIO_PIN_PULLUP); NRF_UART0->PSELTXD = p_comm_params->tx_pin_no; NRF_UART0->PSELRXD = p_comm_params->rx_pin_no; // Configure baud rate and parity. NRF_UART0->BAUDRATE = (p_comm_params->baud_rate << UART_BAUDRATE_BAUDRATE_Pos); if (p_comm_params->use_parity) { NRF_UART0->CONFIG = (UART_CONFIG_PARITY_Included << UART_CONFIG_PARITY_Pos); } else { NRF_UART0->CONFIG = (UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos); } if (p_comm_params->flow_control == APP_UART_FLOW_CONTROL_LOW_POWER) { if (!nrf_drv_gpiote_is_init()) { err_code = nrf_drv_gpiote_init(); if (err_code != NRF_SUCCESS) { return err_code; } } // Configure hardware flow control. nrf_drv_gpiote_out_config_t rts_config = GPIOTE_CONFIG_OUT_SIMPLE(true); err_code = nrf_drv_gpiote_out_init(p_comm_params->rts_pin_no, &rts_config); if (err_code != NRF_SUCCESS) { return err_code; } NRF_UART0->PSELCTS = UART_PIN_DISCONNECTED; NRF_UART0->PSELRTS = p_comm_params->rts_pin_no; NRF_UART0->CONFIG |= (UART_CONFIG_HWFC_Enabled << UART_CONFIG_HWFC_Pos); // Setup the gpiote to handle pin events on cts-pin. // For the UART we want to detect both low->high and high->low transitions in order to // know when to activate/de-activate the TX/RX in the UART. // Configure pin. nrf_drv_gpiote_in_config_t cts_config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(false); err_code = nrf_drv_gpiote_in_init(p_comm_params->cts_pin_no, &cts_config, gpiote_uart_event_handler); if (err_code != NRF_SUCCESS) { return err_code; } nrf_drv_gpiote_in_event_enable(p_comm_params->cts_pin_no, true); // UART CTS pin is active when low. if (nrf_drv_gpiote_in_is_set(p_comm_params->cts_pin_no)) { on_uart_event(ON_CTS_HIGH); } else { on_uart_event(ON_CTS_LOW); } } else if (p_comm_params->flow_control == APP_UART_FLOW_CONTROL_ENABLED) { uart_standard_flow_control_init(p_comm_params); m_current_state = UART_READY; } else { uart_no_flow_control_init(); m_current_state = UART_READY; } if (*p_app_uart_uid == UART_INSTANCE_ID_INVALID) { *p_app_uart_uid = m_instance_counter++; } // Enable UART interrupt NRF_UART0->INTENCLR = 0xffffffffUL; NRF_UART0->INTENSET = (UART_INTENSET_RXDRDY_Set << UART_INTENSET_RXDRDY_Pos) | (UART_INTENSET_TXDRDY_Set << UART_INTENSET_TXDRDY_Pos) | (UART_INTENSET_ERROR_Set << UART_INTENSET_ERROR_Pos); NVIC_ClearPendingIRQ(UART_IRQ); NVIC_SetPriority(UART_IRQ, irq_priority); NVIC_EnableIRQ(UART_IRQ); return NRF_SUCCESS; }
// Initialize and configure ADC nrf_adc_configure( (nrf_adc_config_t *)&nrf_adc_config); nrf_adc_int_enable(ADC_INTENSET_END_Enabled << ADC_INTENSET_END_Pos); NVIC_SetPriority(ADC_IRQn, APP_IRQ_PRIORITY_LOW); NVIC_EnableIRQ(ADC_IRQn); } static void btn_initialize(void) { nrf_drv_gpiote_in_config_t cfg = { .is_watcher = false, .hi_accuracy = false, .pull = NRF_GPIO_PIN_PULLUP, .sense = NRF_GPIOTE_POLARITY_HITOLO, }; ret_code_t err_code = nrf_drv_gpiote_init(); APP_ERROR_CHECK(err_code); err_code = nrf_drv_gpiote_in_init(BUTTON_USER, &cfg, btn_pressed_cb); APP_ERROR_CHECK(err_code); nrf_drv_gpiote_in_event_enable(BUTTON_USER, true); } /** * @brief Function for application main entry. */ int main(void) { unsigned role = role_get(); g_role = role & ROLE_MASK; BUG_ON(g_role >= MAX_GR_ROLES); nrf_gpio_cfg_output(LED_PWR);
// Application main function. int main(void) { uint32_t err_code; // set up timers APP_TIMER_INIT(0, 4, 4, false); // initlialize BLE ble_stack_init(); gap_params_init(); services_init(); advertising_init(); conn_params_init(); err_code = ble_advertising_start(BLE_ADV_MODE_FAST); APP_ERROR_CHECK(err_code); // init GPIOTE err_code = nrf_drv_gpiote_init(); APP_ERROR_CHECK(err_code); // init PPI err_code = nrf_drv_ppi_init(); APP_ERROR_CHECK(err_code); // intialize UART uart_init(); // prints to serial port printf("starting...\n"); // Create the instance "PWM1" using TIMER1. APP_PWM_INSTANCE(PWM1,1); // RGB LED pins // (Common cathode) uint32_t pinR = 1; uint32_t pinG = 2; uint32_t pinB = 3; // 2-channel PWM, 200Hz app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_2CH(5000L, pinR, pinG); /* Initialize and enable PWM. */ err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback); APP_ERROR_CHECK(err_code); app_pwm_enable(&PWM1); // Create the instance "PWM2" using TIMER2. APP_PWM_INSTANCE(PWM2,2); // 1-channel PWM, 200Hz app_pwm_config_t pwm2_cfg = APP_PWM_DEFAULT_CONFIG_1CH(5000L, pinB); /* Initialize and enable PWM. */ err_code = app_pwm_init(&PWM2,&pwm2_cfg,pwm_ready_callback); APP_ERROR_CHECK(err_code); app_pwm_enable(&PWM2); // Enter main loop. int dir = 1; int val = 0; // main loop: bool pwmEnabled = true; while(1) { // only if not paused if (!pausePWM) { // enable disable as needed if(!enablePWM) { if(pwmEnabled) { app_pwm_disable(&PWM1); app_pwm_disable(&PWM2); // This is required becauase app_pwm_disable() // has a bug. // See: // https://devzone.nordicsemi.com/question/41179/how-to-stop-pwm-and-set-pin-to-clear/ nrf_drv_gpiote_out_task_disable(pinR); nrf_gpio_cfg_output(pinR); nrf_gpio_pin_clear(pinR); nrf_drv_gpiote_out_task_disable(pinG); nrf_gpio_cfg_output(pinG); nrf_gpio_pin_clear(pinG); nrf_drv_gpiote_out_task_disable(pinB); nrf_gpio_cfg_output(pinB); nrf_gpio_pin_clear(pinB); pwmEnabled = false; } } else { if(!pwmEnabled) { // enable PWM nrf_drv_gpiote_out_task_enable(pinR); nrf_drv_gpiote_out_task_enable(pinG); nrf_drv_gpiote_out_task_enable(pinB); app_pwm_enable(&PWM1); app_pwm_enable(&PWM2); pwmEnabled = true; } } if(pwmEnabled) { // Set the duty cycle - keep trying until PWM is ready while (app_pwm_channel_duty_set(&PWM1, 0, val) == NRF_ERROR_BUSY); while (app_pwm_channel_duty_set(&PWM1, 1, val) == NRF_ERROR_BUSY); while (app_pwm_channel_duty_set(&PWM2, 0, val) == NRF_ERROR_BUSY); } // change direction at edges if(val > 99) { dir = -1; } else if (val < 1){ dir = 1; } // increment/decrement val += dir*5; } // delay nrf_delay_ms(delay); } }
/**@brief Application main function. */ int main(void) { /* beginning of Initializing services for the Nordic Board */ uint32_t err_code; bool erase_bonds; //print start string to terminal uint8_t start_string[] = START_STRING; printf("%s",start_string); // Initialize timer. APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false); nrf_drv_gpiote_init(); uart_init(); //buttons_leds_init(&erase_bonds); ble_stack_init(); gap_params_init(); services_init(); advertising_init(); conn_params_init(); err_code = ble_advertising_start(BLE_ADV_MODE_FAST); APP_ERROR_CHECK(err_code); //More SPI Additions nrf_drv_spi_config_t const config = { #if (SPI0_ENABLED == 1) .sck_pin = SPIM0_SCK_PIN, .mosi_pin = SPIM0_MOSI_PIN, .miso_pin = SPIM0_MISO_PIN, .ss_pin = SPIM0_SS_PIN, #elif (SPI1_ENABLED == 1) .sck_pin = SPIM1_SCK_PIN, .mosi_pin = SPIM1_MOSI_PIN, .miso_pin = SPIM1_MISO_PIN, .ss_pin = SPIM1_SS_PIN, #elif (SPI2_ENABLED == 1) .sck_pin = SPIM2_SCK_PIN, .mosi_pin = SPIM2_MOSI_PIN, .miso_pin = SPIM2_MISO_PIN, .ss_pin = SPIM2_SS_PIN, #endif .irq_priority = APP_IRQ_PRIORITY_LOW, .orc = 0xCC, .frequency = NRF_DRV_SPI_FREQ_1M, .mode = NRF_DRV_SPI_MODE_0, .bit_order = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST, }; ret_code_t err_code1 = nrf_drv_spi_init(&m_spi_master, &config, spi_master_event_handler); APP_ERROR_CHECK(err_code1); /* End of Initializing services for the Nordic Board Beginning of CC1101 initializing. */ CC1101_Init(); // Enter main loop. for (;;) { // //for setting in receive mode // RecvDataPacket(); SEGGER_RTT_WriteString(0,"RX data:"); for(uint32_t i = 0; i<8;i++){ SEGGER_RTT_printf(0,"%x",m_rx_data[i]); } SEGGER_RTT_WriteString(0,"\n"); // //for sending data that was recieved from the BTLE event // if(m_transfer_completed & newData) { m_transfer_completed = false; newData = false; SendDataPacket(txt_data, strlen((char *) txt_data) + 1);//Additional byte (5+1) is header byte nrf_delay_ms(1); } } } void CC1101_Init(void){ //sequence of SS pin on/off to indicate we are going to reset the system nrf_gpio_pin_clear(SPIM0_SS_PIN); nrf_delay_ms(1); nrf_gpio_pin_set(SPIM0_SS_PIN); nrf_delay_ms(1); nrf_gpio_pin_clear(SPIM0_SS_PIN); //strobe CC1101 reset uint8_t SRES = 0x30; SpiStrobe(SRES); nrf_delay_ms(5); //calibrate CC1101 CC1101_Calibrate(); nrf_delay_ms(1); }