static void twi_clear_bus(nrf_drv_twi_t const * const p_instance, nrf_drv_twi_config_t const * p_config) { NRF_GPIO->PIN_CNF[p_config->scl] = SCL_PIN_CONF; NRF_GPIO->PIN_CNF[p_config->sda] = SDA_PIN_CONF; nrf_gpio_pin_set(p_config->scl); nrf_gpio_pin_set(p_config->sda); NRF_GPIO->PIN_CNF[p_config->scl] = SCL_PIN_CONF_CLR; NRF_GPIO->PIN_CNF[p_config->sda] = SDA_PIN_CONF_CLR; nrf_delay_us(4); for(int i = 0; i < 9; i++) { if (nrf_gpio_pin_read(p_config->sda)) { if(i == 0) { return; } else { break; } } nrf_gpio_pin_clear(p_config->scl); nrf_delay_us(4); nrf_gpio_pin_set(p_config->scl); nrf_delay_us(4); } nrf_gpio_pin_clear(p_config->sda); nrf_delay_us(4); nrf_gpio_pin_set(p_config->sda); }
void playNote(uint16_t note){ for(uint32_t i = 0; i < 50000; i += note * 2){ nrf_gpio_pin_set(PIEZO_BUZZER_PIN); nrf_delay_us(note); nrf_gpio_pin_clear(PIEZO_BUZZER_PIN); nrf_delay_us(note); } }
ble_error_t nRF51822n::reset(void) { nrf_delay_us(500000); /* Wait for the radio to come back up */ nrf_delay_us(1000000); return BLE_ERROR_NONE; }
/** * @brief Function for stopping the RTC1 timer. We don't expect to call this. */ void tmr1_stop(void) { NVIC_DisableIRQ(TIMER1_IRQn); tmr1_disableCompareInterrupt(); tmr1_disableOverflowInterrupt(); NRF_TIMER1->TASKS_STOP = 1; nrf_delay_us(MAX_TMR1_TASKS_DELAY); NRF_TIMER1->TASKS_CLEAR = 1; nrf_delay_us(MAX_TMR1_TASKS_DELAY); }
/** * @brief Function for stopping the RTC1 timer. We don't expect to call this. */ void rtc1_stop(void) { NVIC_DisableIRQ(RTC1_IRQn); rtc1_disableCompareInterrupt(); rtc1_disableOverflowInterrupt(); NRF_RTC1->TASKS_STOP = 1; nrf_delay_us(MAX_RTC_TASKS_DELAY); NRF_RTC1->TASKS_CLEAR = 1; nrf_delay_us(MAX_RTC_TASKS_DELAY); }
void CLumenCapsense::init() { uint8_t data[10]; twi_master_init(); lumen_capsense_ambient_setpoint = 200; mpr121_soft_reset(); nrf_delay_us(100); mpr121_config_prox_touch_0(); nrf_delay_us(100000); mpr121_read_register(MPR121_OOR_0_7, data); }
void AT45DB161E_init(void) { // AT45DB161E == 16Mbit --> http://www.adestotech.com/wp-content/uploads/doc8782.pdf A2035H_RESET_ON(); nrf_delay_us(250); A2035H_POWER_OFF(); // RESET ASSERTED and 3V3GPS Power Maintained Forced OFF nrf_delay_us(100000); AT45_Congigure_IO_Pins(); // ToDo Sort Out PIN Config at A2035H.c AT45_initialize(); nrf_delay_us(100000); // 100ms Delay required to return correct during initial read of WhoAmI Value!!! }
static uint8_t read_single_diode(uint16_t chip, uint16_t diode) { uint16_t value = 0; nrf_gpio_pin_write(PD_CS_PIN_A, _select[3][0]); nrf_gpio_pin_write(PD_CS_PIN_B, _select[3][1]); nrf_delay_us(READ_WAIT); nrf_gpio_pin_clear(PD_CLOCK_PIN); nrf_delay_us(READ_WAIT); nrf_gpio_pin_write(PD_CS_PIN_A, _select[chip][0]); nrf_gpio_pin_write(PD_CS_PIN_B, _select[chip][1]); nrf_delay_us(READ_WAIT); uint16_t commandout = diode; commandout |= 0x18; commandout <<= 3; for (uint16_t i=0; i<5; i++) { if (commandout & 0x80) { nrf_gpio_pin_set(PD_MOSI_PIN); nrf_delay_us(READ_WAIT); } else { nrf_gpio_pin_clear(PD_MOSI_PIN); nrf_delay_us(READ_WAIT); } commandout <<= 1; nrf_gpio_pin_set(PD_CLOCK_PIN); nrf_delay_us(READ_WAIT); nrf_gpio_pin_clear(PD_CLOCK_PIN); nrf_delay_us(READ_WAIT); } for (uint16_t i=0; i<12; i++) { nrf_gpio_pin_set(PD_CLOCK_PIN); nrf_delay_us(READ_WAIT); nrf_gpio_pin_clear(PD_CLOCK_PIN); nrf_delay_us(READ_WAIT); value <<= 1; if (nrf_gpio_pin_read(PD_MISO_PIN)) value |= 0x1; } value >>= 6; //printf("Chip %i Diode %i Value %i\n", chip, diode, value); return (uint8_t) value; }
/** * 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(); } } }
/** * Performs a complete write transaction. The bottom bit of the address is forced to 0 to indicate a write. * * @address 8-bit I2C slave address [ addr | 0 ] * @data Pointer to the byte-arraycontaining the data to write * @length Number of bytes to write * @repeated Repeated start, true - don't send stop at end. * * @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if an unresolved write failure is detected. */ int MicroBitI2C::write(int address, const char *data, int length, bool repeated) { int result = I2C::write(address,data,length,repeated); //0 indicates a success, presume failure while(result != 0 && retries < MICROBIT_I2C_MAX_RETRIES) { _i2c.i2c->EVENTS_ERROR = 0; _i2c.i2c->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; _i2c.i2c->POWER = 0; nrf_delay_us(5); _i2c.i2c->POWER = 1; _i2c.i2c->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos; twi_master_init_and_clear(); result = I2C::write(address,data,length,repeated); retries++; } if(result != 0) return MICROBIT_I2C_ERROR; retries = 0; return MICROBIT_OK; }
static bool twi_master_write(uint8_t *data, uint8_t data_length, bool issue_stop_condition) { uint32_t timeout = MAX_TIMEOUT_LOOPS; /* max loops to wait for EVENTS_TXDSENT event*/ if (data_length == 0) { /* Return false for requesting data of size 0 */ return false; } NRF_TWI1->TXD = *data++; NRF_TWI1->TASKS_STARTTX = 1; /** @snippet [TWI HW master write] */ while (true) { while(NRF_TWI1->EVENTS_TXDSENT == 0 && NRF_TWI1->EVENTS_ERROR == 0 && (--timeout)) { // Do nothing. } if (timeout == 0 || NRF_TWI1->EVENTS_ERROR != 0) { // Recover the peripheral as indicated by PAN 56: "TWI: TWI module lock-up." found at // Product Anomaly Notification document found at // https://www.nordicsemi.com/eng/Products/Bluetooth-R-low-energy/nRF51822/#Downloads NRF_TWI1->EVENTS_ERROR = 0; NRF_TWI1->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; NRF_TWI1->POWER = 0; nrf_delay_us(5); NRF_TWI1->POWER = 1; NRF_TWI1->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos; (void)twi_master_init(); return false; } NRF_TWI1->EVENTS_TXDSENT = 0; if (--data_length == 0) { break; } NRF_TWI1->TXD = *data++; } /** @snippet [TWI HW master write] */ if (issue_stop_condition) { NRF_TWI1->EVENTS_STOPPED = 0; NRF_TWI1->TASKS_STOP = 1; /* Wait until stop sequence is sent */ while(NRF_TWI1->EVENTS_STOPPED == 0) { // Do nothing. } NRF_TWI1->EVENTS_STOPPED = 0; } return true; }
void neopixel_show(neopixel_strip_t *strip) { const uint8_t PIN = strip->pin_num; NRF_GPIO->OUTCLR = (1UL << PIN); nrf_delay_us(50); for (int i = 0; i < strip->num_leds; i++) { for (int j = 0; j < 3; j++) { if ((strip->leds[i].grb[j] & 128) > 0) {NEOPIXEL_SEND_ONE} else {NEOPIXEL_SEND_ZERO} if ((strip->leds[i].grb[j] & 64) > 0) {NEOPIXEL_SEND_ONE} else {NEOPIXEL_SEND_ZERO} if ((strip->leds[i].grb[j] & 32) > 0) {NEOPIXEL_SEND_ONE} else {NEOPIXEL_SEND_ZERO} if ((strip->leds[i].grb[j] & 16) > 0) {NEOPIXEL_SEND_ONE} else {NEOPIXEL_SEND_ZERO} if ((strip->leds[i].grb[j] & 8) > 0) {NEOPIXEL_SEND_ONE} else {NEOPIXEL_SEND_ZERO} if ((strip->leds[i].grb[j] & 4) > 0) {NEOPIXEL_SEND_ONE} else {NEOPIXEL_SEND_ZERO} if ((strip->leds[i].grb[j] & 2) > 0) {NEOPIXEL_SEND_ONE} else {NEOPIXEL_SEND_ZERO} if ((strip->leds[i].grb[j] & 1) > 0) {NEOPIXEL_SEND_ONE} else {NEOPIXEL_SEND_ZERO} } } }
uint16_t usr_measure(void) { static bool led = true; volatile uint32_t result; //enable USR set_pin(USR_EN_PIN); if (led) { set_pin(BRD_LED_PIN); led = false; } else { clear_pin(BRD_LED_PIN); led = true; } //delay 20us nrf_delay_us(20); NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled; NRF_ADC->EVENTS_END = 0; NRF_ADC->TASKS_START = 1; while (NRF_ADC->EVENTS_END == 0); //unenable USR clear_pin(USR_EN_PIN); result = NRF_ADC->RESULT; NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Disabled; return result; }
void tripointDataUpdate () { //update the data value and notify on the data if (blobLen >= 5) { led_on(LED_0); nrf_delay_us(1000); led_off(LED_0); } if(simple_ble_app->conn_handle != BLE_CONN_HANDLE_INVALID) { ble_gatts_hvx_params_t notify_params; uint16_t len = blobLen; notify_params.handle = char_range_handle.char_handle.value_handle; notify_params.type = BLE_GATT_HVX_NOTIFICATION; notify_params.offset = 0; notify_params.p_len = &len; notify_params.p_data = app.app_raw_response_buffer; // volatile uint32_t err_code = 0; // err_code = sd_ble_gatts_hvx(simple_ble_app->conn_handle, ¬ify_params); sd_ble_gatts_hvx(simple_ble_app->conn_handle, ¬ify_params); // APP_ERROR_CHECK(err_code); } updated = 0; }
int main(void) { uint32_t counter = 0; nrf_pwm_config_t pwm_config = PWM_DEFAULT_CONFIG; pwm_config.mode = PWM_MODE_LED_255; pwm_config.num_channels = 4; pwm_config.gpio_num[0] = LED_1; pwm_config.gpio_num[1] = LED_2; pwm_config.gpio_num[2] = LED_4; pwm_config.gpio_num[3] = LED_3; // Initialize the PWM library nrf_pwm_init(&pwm_config); while (true) { // Update the 3 outputs with out of phase sine waves nrf_pwm_set_value(0, sin_table[counter]); nrf_pwm_set_value(1, sin_table[(counter + 25) % 100]); nrf_pwm_set_value(2, sin_table[(counter + 50) % 100]); nrf_pwm_set_value(3, sin_table[(counter + 75) % 100]); counter = (counter + 1) % 100; // Add a delay to control the speed of the sine wave nrf_delay_us(8000); } }
/**@brief Function for handling timer expiration events, used to update RGB LED Display. The display is bottom side up, LEDs are displayed from the top Runs every millisecond, to refresh the display at a rate of 1kHz. */ static void timer_handler(void * p_context) { nrf_gpio_pin_set(OE); Row_Write(curr_height); for(uint8_t i = 0; i < RBG_WIDTH; i++) { //assume start is zero if(i < WIDTH) { //Draw top half if(curr_height < 8) { if(curr_height == 0) Color_Write(p1_home[i]); //draw current cursor position else Color_Write(game[curr_height - 1][i]); //i and row are switched, otherwise it'd draw horizontially } else { //Draw bottom half Color2_Write(game[curr_height - 1][i]); } } else { //Blank the rest out Color_Write(0); Color2_Write(0); } nrf_gpio_pin_set(CLK); nrf_gpio_pin_clear(CLK); } nrf_gpio_pin_set(LAT); nrf_delay_us(100); //Give enough time for latch to save nrf_gpio_pin_clear(LAT); nrf_gpio_pin_clear(OE); curr_height = curr_height == HEIGHT ? 0 : curr_height + 1; }
/** * @brief Function for application main entry. * @return 0. int return type required by ANSI/ISO standard. */ int main(void) { init(); for (int i =0; true; i++) { // Set payload pointer NRF_RADIO->PACKETPTR = (uint32_t) (i%2==0? packet:scan_rsp); NRF_RADIO->EVENTS_READY = 0U; NRF_RADIO->TASKS_TXEN = 1; while (NRF_RADIO->EVENTS_READY == 0U) { } NRF_RADIO->TASKS_START = 1U; NRF_RADIO->EVENTS_END = 0U; while(NRF_RADIO->EVENTS_END == 0U) { } NRF_RADIO->EVENTS_DISABLED = 0U; // Disable radio NRF_RADIO->TASKS_DISABLE = 1U; while(NRF_RADIO->EVENTS_DISABLED == 0U) { } if (i%2 == 0) { nrf_delay_us(200); } else { nrf_delay_ms(3); } } }
void DelayMs(volatile uint32_t ms) { while(ms != 0) { ms--; nrf_delay_us(US_PER_MS); } }
SwitchPosition Switch() { SwitchPosition result; //nrf_gpio_cfg_output(SWITCH_PIN1); //nrf_gpio_pin_clear(SWITCH_PIN1); nrf_gpio_cfg_input(SWITCH_PIN2, NRF_GPIO_PIN_PULLUP); nrf_delay_us(10); if(nrf_gpio_pin_read(SWITCH_PIN2) == 0) { result = SwitchESB; } else { result = SwitchBLE; } //nrf_gpio_cfg_input(SWITCH_PIN1, NRF_GPIO_PIN_NOPULL); nrf_gpio_cfg_input(SWITCH_PIN2, NRF_GPIO_PIN_NOPULL); return result; }
/*====================================================================================================*/ void delay_ms( __IO uint32_t vCnt_ms ) { while(vCnt_ms != 0) { vCnt_ms--; nrf_delay_us(999); } }
bool simple_uart_get_with_timeout(int32_t timeout_ms, uint8_t * rx_data) { bool ret = true; while (NRF_UART0->EVENTS_RXDRDY != 1) { if (timeout_ms-- >= 0) { // wait in 1ms chunk before checking for status. nrf_delay_us(1000); } else { ret = false; break; } } // Wait for RXD data to be received. if (timeout_ms >= 0) { // clear the event and set rx_data with received byte. NRF_UART0->EVENTS_RXDRDY = 0; *rx_data = (uint8_t)NRF_UART0->RXD; } return ret; }
/**@brief Function for stopping the RTC1 timer. */ static void rtc1_stop(void) { NVIC_DisableIRQ(RTC1_IRQn); NRF_RTC1->EVTENCLR = RTC_EVTEN_COMPARE0_Msk; NRF_RTC1->INTENCLR = RTC_INTENSET_COMPARE0_Msk; NRF_RTC1->TASKS_STOP = 1; nrf_delay_us(MAX_RTC_TASKS_DELAY); NRF_RTC1->TASKS_CLEAR = 1; m_ticks_latest = 0; nrf_delay_us(MAX_RTC_TASKS_DELAY); m_rtc1_running = false; }
bool nrf6350_lcd_init(void) { if (!twi_master_init()) { return false; } // Sometimes the first command doesn't get through, so we'll try // sending non-important "wake up" command first and don't care if it fails. (void)nrf6350_lcd_wake_up(); if (!nrf6350_lcd_set_instruction(0x38)) // Function set. return false; if (!nrf6350_lcd_set_instruction(0x39)) // Choose two-line mode. return false; if (!nrf6350_lcd_set_instruction(0x14)) // Internal OSC frequency. return false; if (!nrf6350_lcd_set_contrast(LCD_CONTRAST_HIGH)) // Contrast set (low byte). return false; if (!nrf6350_lcd_set_instruction(0x5F)) // Power/ICON control/. return false; if (!nrf6350_lcd_set_instruction(0x6A)) // Follower control. return false; nrf_delay_us(200000); // Need to wait 200ms here according to datasheet. if (!nrf6350_lcd_on()) // Display ON. return false; if (!nrf6350_lcd_clear()) // Clear display. return false; return nrf6350_lcd_set_instruction(0x06); // Entry mode set. }
/**@brief Function for notifying a DFU Controller about error conditions in the DFU module. * This function also ensures that an error is translated from nrf_errors to DFU Response * Value. * * @param[in] p_dfu DFU Service Structure. * @param[in] err_code Nrf error code that should be translated and send to the DFU Controller. */ static void dfu_error_notify(uint32_t err_code, uint32_t err_point) { // Unexpected error occured, #if defined (DBG_DFU_UART_OUT_PIN) //Wait until all the uart successfully sent out. nrf_delay_us(50); DEBUG_UART_OUT(0xEE); DEBUG_UART_OUT(err_point); nrf_delay_us(50); #endif // TODO: we need to come up of something to handle this NVIC_SystemReset(); }
bool nrf6350_lcd_off(void) { nrf_delay_us(10000); data_buffer[0] = FUNC_SET; data_buffer[1] = 0x08; return twi_master_transfer(LCD_ADDR << 1, data_buffer, 2, TWI_ISSUE_STOP); }
bool nrf6350_lcd_set_contrast(uint8_t contrast) { nrf_delay_us(10000); data_buffer[0] = FUNC_SET; data_buffer[1] = 0x70 | contrast; return twi_master_transfer(LCD_ADDR << 1, data_buffer, 2, TWI_ISSUE_STOP); }
static bool nrf6350_lcd_set_instruction(uint8_t instr) { nrf_delay_us(10000); data_buffer[0] = FUNC_SET; data_buffer[1] = instr; return twi_master_transfer(LCD_ADDR << 1, data_buffer, 2, TWI_ISSUE_STOP); }
static void disp_clearAll( void ) { const uint8_t clearAllData[2] = { 0x04, 0 }; uint8_t rxData[14]; spiMaster_txRx( &spiMaster, &screenSPIslave, 2, clearAllData, rxData ); nrf_delay_us( 2 ); }
/*lint --e{438} "Variable not used" */ void nrf_delay_ms(uint32_t volatile number_of_ms) { while(number_of_ms != 0) { number_of_ms--; nrf_delay_us(999); } }
void lumen_radio_active_notification_handler(bool radio_is_active) { //if the radio is no longer active, send an update to the leds if(!radio_is_active){ nrf_delay_us(250); app_sched_event_put(&lumen_event_show_led, sizeof(lumen_event_show_led),lumen_task_led_show); } }