/** * main() function * @return 0. int return type required by ANSI/ISO standard. */ int main(void) { /* Start 16 MHz crystal oscillator */ NRF_CLOCK->EVENTS_HFCLKSTARTED = 0; NRF_CLOCK->TASKS_HFCLKSTART = 1; /* Wait for the external oscillator to start up */ while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) { } // Set Port 1 as output nrf_gpio_range_cfg_output(8, 15); // Set radio configuration parameters radio_configure(); nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT1, 0x55); while(true) { // Set payload pointer NRF_RADIO->PACKETPTR = (uint32_t)packet; NRF_RADIO->EVENTS_READY = 0U; // Enable radio and wait for ready NRF_RADIO->TASKS_RXEN = 1U; while(NRF_RADIO->EVENTS_READY == 0U) { } NRF_RADIO->EVENTS_END = 0U; // Start listening and wait for address received event NRF_RADIO->TASKS_START = 1U; // Wait for end of packet while(NRF_RADIO->EVENTS_END == 0U) { } // Write received data to port 1 on CRC match if (NRF_RADIO->CRCSTATUS == 1U) { nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT1, packet[0]); } NRF_RADIO->EVENTS_DISABLED = 0U; // Disable radio NRF_RADIO->TASKS_DISABLE = 1U; while(NRF_RADIO->EVENTS_DISABLED == 0U) { } } }
void microbit_display_obj_t::advanceRow() { // First, clear the old row. // Clear the old bit pattern for this row. // Clear port 0 4-7 and retain lower 4 bits. nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT0, 0xF0 | (nrf_gpio_port_read(NRF_GPIO_PORT_SELECT_PORT0) & 0x0F)); // Clear port 1 8-12 for the current row. nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT1, strobe_mask | 0x1F); // Move on to the next row. strobe_mask <<= 1; strobe_row++; // Reset the row counts and bit mask when we have hit the max. if (strobe_row == MICROBIT_DISPLAY_ROW_COUNT) { strobe_row = 0; strobe_mask = 0x20; } // Prepare row for rendering. for (int i = 0; i < MICROBIT_DISPLAY_COLUMN_COUNT; i++) { int x = display_map[i][strobe_row].x; int y = display_map[i][strobe_row].y; row_brightness[i] = microbit_display_obj.image_buffer[x][y]; } // Turn on any pixels that are at max. setPinsForRow(MAX_BRIGHTNESS); }
int main(void){ nrf_gpio_range_cfg_output(18, 19); while(1){ nrf_gpio_port_write(2, 8); nrf_delay_ms(500); nrf_gpio_port_write(2, 4); nrf_delay_ms(500); } }
/** @brief Function for handling the GPIOTE interrupt. * Triggered on input Low-to-high transition. */ void GPIOTE_IRQHandler(void) { static uint32_t prev_cc1 = 0; uint32_t curr_cc1; uint32_t cycle_duration; uint32_t duty_cycle; uint32_t active_time; curr_cc1 = NRF_TIMER1->CC[1]; // Cycle duration is the difference between two low-to-high transitions. cycle_duration = (curr_cc1 - prev_cc1); active_time = cycle_duration - ((curr_cc1 - NRF_TIMER1->CC[0])); if (cycle_duration != 0) { duty_cycle = (DUTY_CYCLE_SCALE_VALUE * active_time) / cycle_duration; nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT1, (uint8_t)duty_cycle); } else { // Do nothing. } // Clear the event causing the interrupt. NRF_GPIOTE->EVENTS_IN[1] = 0; prev_cc1 = curr_cc1; }
/** * @brief Function for application main entry. * @return 0. int return type required by ANSI/ISO standard. */ int main(void) { const uint8_t *key_packet; uint8_t key_packet_size; #ifdef USE_UART simple_uart_config(0, SIMPLE_UART_TXD_PIN_NUMBER, 0, SIMPLE_UART_RXD_PIN_NUMBER, false); // Hardware flow control not used in this example. #else // Configure pins 24-30 for LEDs. Note that pin 31 is not connected. nrf_gpio_range_cfg_output(24, 30); #endif // Enable pulldowns on row port, see matrix_row_port. nrf_gpio_range_cfg_input(16, 23, NRF_GPIO_PIN_PULLDOWN); // Column pin configuration, see matrix_column_port. nrf_gpio_range_cfg_output(0, 15); if (cherry8x16_init(matrix_row_port, matrix_column_port, CHERRY8x16_DEFAULT_KEY_LOOKUP_MATRIX) != CHERRY8x16_OK) { #ifdef USE_UART simple_uart_putstring((const uint8_t *)"Init failed."); #else nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT3, 0x55); #endif while (true) { // Do nothing. } } while(true) { if (cherry8x16_new_packet(&key_packet, &key_packet_size)) { #ifdef USE_UART // Send the whole key packet over UART. uart_puthidstring((char*)&key_packet[KEY_PACKET_KEY_INDEX]); #else // Show modifier key state using the LEDs. Note LED's use GPIO pins from 8 to 15. nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT3, key_packet[KEY_PACKET_MODIFIER_KEY_INDEX]); #endif } nrf_delay_ms(25); } }
/** * @brief Function for application main entry. */ int main(void) { // Start 16 MHz crystal oscillator. NRF_CLOCK->EVENTS_HFCLKSTARTED = 0; NRF_CLOCK->TASKS_HFCLKSTART = 1; // Wait for the external oscillator to start up. while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) { // Do nothing. } // Set Port 0 as input. nrf_gpio_range_cfg_input(BUTTON_START, BUTTON_STOP, BUTTON_PULL); // Set Port 1 as output. nrf_gpio_range_cfg_output(LED_START, LED_STOP); // Set radio configuration parameters. radio_configure(); // Set payload pointer. NRF_RADIO->PACKETPTR = (uint32_t)packet; while (true) { // Read Data to send, button signals are default high, and low when pressed. packet[0] = ~(nrf_gpio_port_read(NRF_GPIO_PORT_SELECT_PORT0)); // Write GPIO to payload byte 0. NRF_RADIO->EVENTS_READY = 0U; NRF_RADIO->TASKS_TXEN = 1; // Enable radio and wait for ready. while (NRF_RADIO->EVENTS_READY == 0U) { // Do nothing. } // Start transmission. NRF_RADIO->TASKS_START = 1U; NRF_RADIO->EVENTS_END = 0U; while (NRF_RADIO->EVENTS_END == 0U) // Wait for end of the transmission packet. { // Do nothing. } // Write sent data to port 1. nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT1, packet[0]); NRF_RADIO->EVENTS_DISABLED = 0U; NRF_RADIO->TASKS_DISABLE = 1U; // Disable the radio. while (NRF_RADIO->EVENTS_DISABLED == 0U) { // Do nothing. } } }
int main() { uint8_t debug_led_output; // Setup port directions nrf_gpio_port_dir_set(BUTTONS, NRF_GPIO_PORT_DIR_INPUT); nrf_gpio_port_dir_set(LEDS, NRF_GPIO_PORT_DIR_OUTPUT); // Initialize Gazell init_ok = nrf_gzll_init(NRF_GZLL_MODE_DEVICE); // Attempt sending every packet up to 100 times init_ok &= nrf_gzll_set_max_tx_attempts(100); // Load data into TX queue data_payload[0] = nrf_gpio_port_read(BUTTONS); push_ok = nrf_gzll_add_packet_to_tx_fifo(PIPE_NUMBER, data_payload, TX_PAYLOAD_LENGTH); // Enable Gazell to start sending over the air enable_ok = nrf_gzll_enable(); while(1) { // Error handling debug_led_output = ((uint8_t)tx_success << 4) | ((uint8_t)pop_ok << 3) | ((uint8_t)push_ok << 2) | ((uint8_t)enable_ok << 1) | (uint8_t)init_ok; // If an error has occured if(debug_led_output != 0x1F) { nrf_gpio_port_write(LEDS,0xFF); nrf_delay_us(1000000); // 1 second delay nrf_gpio_port_write(LEDS, debug_led_output); nrf_delay_us(1000000); // 1 second delay nrf_gpio_port_write(LEDS,0xF0 + (uint32_t)nrf_gzll_get_error_code()); nrf_delay_us(1000000); // 1 second delay } // Optionally send the CPU to sleep while waiting for a callback. // __WFI(); } }
inline void microbit_display_obj_t::setPinsForRow(uint8_t brightness) { int column_strobe = 0; // Calculate the bitpattern to write. for (int i = 0; i < MICROBIT_DISPLAY_COLUMN_COUNT; i++) { if (row_brightness[i] >= brightness) { column_strobe |= (1 << i); } } // Wwrite the new bit pattern. // Set port 0 4-7 and retain lower 4 bits. nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT0, (~column_strobe<<4 & 0xF0) | (nrf_gpio_port_read(NRF_GPIO_PORT_SELECT_PORT0) & 0x0F)); // Set port 1 8-12 for the current row. nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT1, strobe_mask | (~column_strobe>>4 & 0x1F)); }
// If a data packet was received, we write the first byte to LEDS. void nrf_gzll_host_rx_data_ready(uint32_t pipe, nrf_gzll_host_rx_info_t rx_info) { uint32_t data_payload_length = NRF_GZLL_CONST_MAX_PAYLOAD_LENGTH; packet_received = true; // Pop packet and write first byte of the payload to the GPIO port. pop_ok = nrf_gzll_fetch_packet_from_rx_fifo(pipe, data_payload, &data_payload_length); if (data_payload_length > 0) { nrf_gpio_port_write(LEDS,~data_payload[0]); } // Read buttons and load ACK payload into TX queue ack_payload[0] = nrf_gpio_port_read(BUTTONS); // Button logic is inverted. push_ok = nrf_gzll_add_packet_to_tx_fifo(pipe, ack_payload, TX_PAYLOAD_LENGTH); }
// If an ACK was received, we send another packet. void nrf_gzll_device_tx_success(uint32_t pipe, nrf_gzll_device_tx_info_t tx_info) { uint32_t ack_payload_length = NRF_GZLL_CONST_MAX_PAYLOAD_LENGTH; tx_success = true; if (tx_info.payload_received_in_ack) { // Pop packet and write first byte of the payload to the GPIO port. pop_ok = nrf_gzll_fetch_packet_from_rx_fifo(pipe, ack_payload, &ack_payload_length); if (ack_payload_length > 0) { nrf_gpio_port_write(LEDS, ~ack_payload[0]); // Button press is active low. } } // Read buttons and load data payload into TX queue data_payload[0] = nrf_gpio_port_read(BUTTONS); push_ok = nrf_gzll_add_packet_to_tx_fifo(pipe, data_payload, TX_PAYLOAD_LENGTH); }
/** * @brief Function for application main entry. */ int main(void) { uint32_t *addr; uint8_t patwr; uint8_t patrd; uint8_t patold; uint32_t i; uint32_t pg_size; uint32_t pg_num; init(); patold = 0; pg_size = NRF_FICR->CODEPAGESIZE; pg_num = NRF_FICR->CODESIZE - 1; // Use last page in flash while (true) { // Start address: addr = (uint32_t *)(pg_size * pg_num); // Erase page: flash_page_erase(addr); i = 0; do { // Read pattern from port 0 (pins0-7), and write it to flash: patwr = nrf_gpio_port_read(NRF_GPIO_PORT_SELECT_PORT0); if (patold != patwr) { patold = patwr; flash_word_write(++addr, (uint32_t)patwr); i += 4; } // Read pattern from flash and write it to port 1 (pins8-15): patrd = (uint8_t)*addr; nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT1, patrd); } while (i < pg_size); } }
/** * main function * @return 0. int return type required by ANSI/ISO standard. */ int main(void) { // Configure pins 8-15 (port1) for LEDs as outputs nrf_gpio_range_cfg_output(8, 15); nrf_gpio_port_set(NRF_GPIO_PORT_SELECT_PORT1, 0XFF); NRF_RNG->TASKS_START = 1; // start RNG while (true) { // Clear the VALRDY EVENT and clear gpio8 - 15 (port1) NRF_RNG->EVENTS_VALRDY = 0; // Wait until the value ready event is generated while ( NRF_RNG->EVENTS_VALRDY == 0){} // Set output according to the random value nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT1, (uint8_t)NRF_RNG->VALUE); nrf_delay_ms(100); } }
/** @brief Function for main application entry. */ int main(void) { // Configure pins 8-15 (port 1) for LEDs as outputs. nrf_gpio_range_cfg_output(LED_START, LED_STOP); nrf_gpio_port_set(NRF_GPIO_PORT_SELECT_PORT1, 0XFF); NRF_RNG->TASKS_START = 1; // start the RNG peripheral. while (true) { // Clear the VALRDY EVENT. NRF_RNG->EVENTS_VALRDY = 0; // Wait until the value ready event is generated. while (NRF_RNG->EVENTS_VALRDY == 0) { // Do nothing. } // Set output according to the random value. nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT1, (uint8_t)NRF_RNG->VALUE); nrf_delay_ms(100); } }
/** * @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); } } } }