/** @brief Function for initializing the GPIO Tasks/Events peripheral. */ static void gpiote_init(void) { // Connect GPIO input buffers and configure PWM_OUTPUT_PIN_NUMBER as an output. nrf_gpio_range_cfg_input(BUTTON_START, BUTTON_STOP, BUTTON_PULL); nrf_gpio_cfg_output(PWM_OUTPUT_PIN_NUMBER); nrf_gpio_port_clear(NRF_GPIO_PORT_SELECT_PORT0, 0xFF); // Configure GPIOTE channel 0 to toggle the PWM pin state // @note Only one GPIOTE task can be connected to an output pin. nrf_gpiote_task_config(0, PWM_OUTPUT_PIN_NUMBER, \ NRF_GPIOTE_POLARITY_TOGGLE, NRF_GPIOTE_INITIAL_VALUE_LOW); }
/** * main function * \return 0. int return type required by ANSI/ISO standard. */ int main(void) { int i; gpio_init(); gpiote_init(); wdt_init(); //Write the value of RESETREAS to pins 9-15 (LEDs 1-7) nrf_gpio_pin_write(LED_1, NRF_POWER->RESETREAS & POWER_RESETREAS_RESETPIN_Msk); //Bit A in RESETREAS nrf_gpio_pin_write(LED_2, NRF_POWER->RESETREAS & POWER_RESETREAS_DOG_Msk); //Bit B in RESETREAS nrf_gpio_pin_write(LED_3, NRF_POWER->RESETREAS & POWER_RESETREAS_SREQ_Msk); //Bit C in RESETREAS nrf_gpio_pin_write(LED_4, NRF_POWER->RESETREAS & POWER_RESETREAS_LOCKUP_Msk); //Bit D in RESETREAS nrf_gpio_pin_write(LED_5, NRF_POWER->RESETREAS & POWER_RESETREAS_OFF_Msk); //Bit E in RESETREAS nrf_gpio_pin_write(LED_6, NRF_POWER->RESETREAS & POWER_RESETREAS_LPCOMP_Msk); //Bit F in RESETREAS nrf_gpio_pin_write(LED_7, NRF_POWER->RESETREAS & POWER_RESETREAS_DIF_Msk); //Bit G in RESETREAS NRF_POWER->RESETREAS = 0xFFFFFFFF; //Clear the RESETREAS register for(i=0;i<STARTUP_TOGGLE_ITERATIONS;i++) { nrf_gpio_pin_toggle(LED_0); nrf_delay_us(DELAY); } while (true) { //Blink LED 0 fast until watchdog triggers reset nrf_gpio_pin_toggle(LED_0); nrf_delay_us(DELAY/3); // If SYSTEM_OFF_BUTTON is pressed.. enter System Off mode if(nrf_gpio_pin_read(SYSTEM_OFF_BUTTON) == BTN_PRESSED) { // Clear PORT 1 (pins 8-15) nrf_gpio_port_clear(NRF_GPIO_PORT_SELECT_PORT1, 0xFF); // Enter system OFF. After wakeup the chip will be reset, and the program will run from the top NRF_POWER->SYSTEMOFF = 1; } // If SOFTWARE_RESET_BUTTON is pressed.. soft-reset if(nrf_gpio_pin_read(SOFTWARE_RESET_BUTTON) == BTN_PRESSED) { NVIC_SystemReset(); } } }
/** * @brief Function for application main entry. */ int main(void) { // Configure pins 8-15 (Port1) as outputs nrf_gpio_range_cfg_output(8, 15); nrf_gpio_port_clear(NRF_GPIO_PORT_SELECT_PORT1, 0XFF); // Configure motion interrupt pin nrf_gpio_cfg_input(MOTION_INTERRUPT_PIN_NUMBER, NRF_GPIO_PIN_PULLDOWN); gpiote_init(); if (adns2080_init() != ADNS2080_OK) { // ADNS2080 init failed, set rightmost LED on. nrf_gpio_pin_write(15, 1); while (true) { // Do nothing. } } // By default, ADNS2080 motion interrupt output is active low, edge sensitive; make it active high. if (adns2080_motion_interrupt_set(ADNS2080_MOTION_OUTPUT_POLARITY_HIGH, ADNS2080_MOTION_OUTPUT_SENSITIVITY_LEVEL) != ADNS2080_OK) { nrf_gpio_pin_write(14, 1); while (true) { // Do nothing. } } // Read out movement to clear ADNS2080 interrupt flags. if (adns2080_is_motion_detected()) { int16_t dummy; adns2080_movement_read(&dummy, &dummy); } // Enable GPIOTE interrupt in Nested Vector Interrupt Controller. NVIC_EnableIRQ(GPIOTE_IRQn); // Enable global interrupts. __enable_irq(); while(true) { if (motion_interrupt_detected) { // Toggle pin 12 to indicate that we've detected motion interrupt. nrf_gpio_pin_toggle(12); motion_interrupt_detected = false; // On our Nordic reference design PCB, the chip orientation is not the same as in the ADNS2080 // datasheet diagram, so X and Y axis are reversed. This is corrected by passing the pointer // parameters in reversed order. */ adns2080_movement_read(&m_delta_y, &m_delta_x); // If movement delta_x is above the threshold, the LEDs light up accordingly. When the mouse is moved // to the left, LEDs 1 through 4 are lit and when the mouse is moved right, LEDs 5 through 8 are lit. if (m_delta_x > MOUSE_MOVEMENT_THRESHOLD) { nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT1, 0xF0); } else if (m_delta_x < (-MOUSE_MOVEMENT_THRESHOLD)) { nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT1, 0x0F); } else { nrf_gpio_port_clear(NRF_GPIO_PORT_SELECT_PORT1, 0xFF); } } } }