void spi_master_close(const spi_master_hw_instance_t spi_master_hw_instance) { #if defined(SPI_MASTER_0_ENABLE) || defined(SPI_MASTER_1_ENABLE) volatile spi_master_instance_t * p_spi_instance = spi_master_get_instance( spi_master_hw_instance); APP_ERROR_CHECK_BOOL(p_spi_instance != NULL); /* Disable interrupt */ APP_ERROR_CHECK(sd_nvic_ClearPendingIRQ(p_spi_instance->irq_type)); APP_ERROR_CHECK(sd_nvic_DisableIRQ(p_spi_instance->irq_type)); p_spi_instance->p_nrf_spi->ENABLE = (SPI_ENABLE_ENABLE_Disabled << SPI_ENABLE_ENABLE_Pos); /* Set Slave Select pin as input with pull-up. */ nrf_gpio_pin_set(p_spi_instance->pin_slave_select); nrf_gpio_cfg_input(p_spi_instance->pin_slave_select, NRF_GPIO_PIN_PULLUP); p_spi_instance->pin_slave_select = (uint8_t)0xFF; /* Disconnect pins from SPI hardware */ p_spi_instance->p_nrf_spi->PSELSCK = (uint32_t)SPI_PIN_DISCONNECTED; p_spi_instance->p_nrf_spi->PSELMOSI = (uint32_t)SPI_PIN_DISCONNECTED; p_spi_instance->p_nrf_spi->PSELMISO = (uint32_t)SPI_PIN_DISCONNECTED; /* Reset to default values */ spi_master_init_hw_instance(NULL, (IRQn_Type)0, p_spi_instance, false); #else return; #endif }
void battery_start(void) { uint32_t err_code; // Configure ADC NRF_ADC->INTENSET = ADC_INTENSET_END_Msk; NRF_ADC->CONFIG = (ADC_CONFIG_RES_8bit << ADC_CONFIG_RES_Pos) | (ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) | (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) | (ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos) | (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos); NRF_ADC->EVENTS_END = 0; NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled; // Enable ADC interrupt err_code = sd_nvic_ClearPendingIRQ(ADC_IRQn); APP_ERROR_CHECK(err_code); err_code = sd_nvic_SetPriority(ADC_IRQn, NRF_APP_PRIORITY_LOW); APP_ERROR_CHECK(err_code); err_code = sd_nvic_EnableIRQ(ADC_IRQn); APP_ERROR_CHECK(err_code); NRF_ADC->EVENTS_END = 0; // Stop any running conversions. NRF_ADC->TASKS_START = 1; }
uint32_t ble_radio_notification_init(nrf_app_irq_priority_t irq_priority, nrf_radio_notification_distance_t distance, ble_radio_notification_evt_handler_t evt_handler) { uint32_t err_code; m_evt_handler = evt_handler; // Initialize Radio Notification software interrupt err_code = sd_nvic_ClearPendingIRQ(SWI1_IRQn); if (err_code != NRF_SUCCESS) { return err_code; } err_code = sd_nvic_SetPriority(SWI1_IRQn, irq_priority); if (err_code != NRF_SUCCESS) { return err_code; } err_code = sd_nvic_EnableIRQ(SWI1_IRQn); if (err_code != NRF_SUCCESS) { return err_code; } // Configure the event return sd_radio_notification_cfg_set(NRF_RADIO_NOTIFICATION_TYPE_INT_ON_BOTH, distance); }
void proprietary_rf_init(void) { uint32_t err_code; m_uesb_config.rf_channel = 64; m_uesb_config.crc = UESB_CRC_16BIT; m_uesb_config.payload_length = 10; m_uesb_config.protocol = UESB_PROTOCOL_SB; m_uesb_config.bitrate = UESB_BITRATE_250KBPS; m_uesb_config.mode = UESB_MODE_PTX; m_uesb_config.rf_addr_length = 5; m_uesb_config.tx_output_power = UESB_TX_POWER_4DBM; m_uesb_config.dynamic_ack_enabled = 0; m_uesb_config.dynamic_payload_length_enabled = 0; m_uesb_config.rx_pipes_enabled = 0x01; m_uesb_config.retransmit_delay = 3750; m_uesb_config.retransmit_count = 0; m_uesb_config.event_handler = uesb_event_handler; m_uesb_config.radio_irq_priority = 0; m_tx_payload.pipe = FIXED_PIPE_INDEX; // The radio timeslot API will use the QDEC interrupt because that is rarely used. err_code = sd_nvic_ClearPendingIRQ(QDEC_IRQn); APP_ERROR_CHECK(err_code); err_code = sd_nvic_SetPriority(QDEC_IRQn, NRF_APP_PRIORITY_HIGH); APP_ERROR_CHECK(err_code); err_code = sd_nvic_EnableIRQ(QDEC_IRQn); APP_ERROR_CHECK(err_code); syma_init(); }
void protocol_init(struct ir_protocol *protocol, uint8_t led_pin, struct rtc_ctx *c) { ctx = c; context.protocol = protocol; context.led_pin = led_pin; // low freq clock NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos); NRF_CLOCK->EVENTS_LFCLKSTARTED = 0; NRF_CLOCK->TASKS_LFCLKSTART = 1; while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0) /* NOTHING */; NRF_CLOCK->EVENTS_LFCLKSTARTED = 0; // rtc1 interrupt sd_nvic_ClearPendingIRQ(RTC1_IRQn); sd_nvic_SetPriority(RTC1_IRQn, NRF_APP_PRIORITY_LOW); sd_nvic_EnableIRQ(RTC1_IRQn); NRF_RTC1->EVTENSET = RTC_EVTENSET_COMPARE0_Msk; NRF_RTC1->INTENSET = RTC_INTENSET_COMPARE0_Msk; // high freq clock sd_clock_hfclk_request(); // timer1 NRF_TIMER1->TASKS_STOP = 1; NRF_TIMER1->TASKS_CLEAR = 1; NRF_TIMER1->PRESCALER = 4; NRF_TIMER1->MODE = TIMER_MODE_MODE_Timer; NRF_TIMER1->BITMODE = TIMER_BITMODE_BITMODE_16Bit; NRF_TIMER1->SHORTS = TIMER_SHORTS_COMPARE2_CLEAR_Msk; NRF_TIMER1->CC[0] = 1; NRF_TIMER1->CC[1] = ROUNDED_DIV(context.protocol->pulse_width, 3); NRF_TIMER1->CC[2] = context.protocol->pulse_width; // timer2 (counter) NRF_TIMER2->TASKS_STOP = 1; NRF_TIMER2->TASKS_CLEAR = 1; NRF_TIMER2->MODE = TIMER_MODE_MODE_Counter; NRF_TIMER2->BITMODE = TIMER_BITMODE_BITMODE_16Bit; NRF_TIMER2->TASKS_START = 1; NRF_TIMER2->SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Msk; // gpio (led) nrf_gpio_cfg_output(led_pin); // ppi's sd_ppi_channel_assign(0, &NRF_TIMER1->EVENTS_COMPARE[0], &NRF_GPIOTE->TASKS_OUT[0]); // toggle led sd_ppi_channel_assign(1, &NRF_TIMER1->EVENTS_COMPARE[1], &NRF_GPIOTE->TASKS_OUT[0]); // toggle led sd_ppi_channel_assign(2, &NRF_TIMER1->EVENTS_COMPARE[2], &NRF_TIMER2->TASKS_COUNT); // inc timer2 sd_ppi_channel_assign(3, &NRF_TIMER2->EVENTS_COMPARE[0], &NRF_TIMER1->TASKS_STOP); // stops timer1 after timer2 reaches N sd_ppi_channel_enable_set(PPI_CHEN_CH0_Msk | PPI_CHEN_CH1_Msk | PPI_CHEN_CH2_Msk | PPI_CHEN_CH3_Msk); }
uint32_t nrf_pwm_init(nrf_pwm_config_t *config) { static volatile uint32_t err_code; if(config->num_channels < 1 || config->num_channels > 2) return 0xFFFFFFFF; switch(config->mode) { case PWM_MODE_LED_255: // 8-bit resolution, 122 Hz PWM frequency, 65 kHz timer frequency (prescaler 8) PWM_TIMER->PRESCALER = 8; pwm_max_value = 255; pwm_period_us = (pwm_max_value * 2 * 1000000) / (16000000 / 256); break; case PWM_MODE_LED_4095: // 0-4095 resolution, 488Hz PWM frequency, 2MHz timer frequency (prescaler 2) PWM_TIMER->PRESCALER = 2; pwm_max_value = 4095; pwm_period_us = (pwm_max_value * 2 * 1000000) / (16000000 / 4); break; default: return 0xFFFFFFFF; } pwm_ppi_chg = config->ppi_group[0]; pwm_num_channels = config->num_channels; for(int i = 0; i < pwm_num_channels; i++) { pwm_io_ch[i] = (uint32_t)config->gpio_num[i]; nrf_gpio_cfg_output(pwm_io_ch[i]); pwm_gpiote_channel[i] = config->gpiote_channel[i]; } PWM_TIMER->TASKS_CLEAR = 1; PWM_TIMER->BITMODE = TIMER_BITMODE_BITMODE_16Bit; PWM_TIMER->CC[3] = pwm_max_value*2; PWM_TIMER->MODE = TIMER_MODE_MODE_Timer; PWM_TIMER->SHORTS = TIMER_SHORTS_COMPARE3_CLEAR_Msk; PWM_TIMER->EVENTS_COMPARE[0] = PWM_TIMER->EVENTS_COMPARE[1] = PWM_TIMER->EVENTS_COMPARE[2] = PWM_TIMER->EVENTS_COMPARE[3] = 0; for(int i = 0; i < pwm_num_channels; i++) { ppi_configure_channel(config->ppi_channel[i*2], &PWM_TIMER->EVENTS_COMPARE[i], &NRF_GPIOTE->TASKS_OUT[pwm_gpiote_channel[i]]); ppi_configure_channel(config->ppi_channel[i*2+1], &PWM_TIMER->EVENTS_COMPARE[3], &NRF_GPIOTE->TASKS_OUT[pwm_gpiote_channel[i]]); } #if(USE_WITH_SOFTDEVICE == 1) sd_nvic_SetPriority(PWM_IRQn, 1); sd_nvic_ClearPendingIRQ(PWM_IRQn); sd_nvic_EnableIRQ(PWM_IRQn); #else NVIC_SetPriority(PWM_IRQn, 3); NVIC_ClearPendingIRQ(PWM_IRQn); NVIC_EnableIRQ(PWM_IRQn); #endif PWM_TIMER->TASKS_START = 1; return 0; }
//Start supercapacitor voltage measurement void supercap_measure_start(void) { // Configure ADC /* Enable interrupt on ADC sample ready event*/ NRF_ADC->INTENSET = ADC_INTENSET_END_Msk; sd_nvic_SetPriority(ADC_IRQn, NRF_APP_PRIORITY_LOW); sd_nvic_EnableIRQ(ADC_IRQn); NRF_ADC->CONFIG = (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos) /* Bits 17..16 : ADC external reference pin selection. */ | (ADC_CONFIG_PSEL_AnalogInput7 << ADC_CONFIG_PSEL_Pos) /*!< Use analog input 2 as analog input. */ | (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) /*!< Use internal 1.2V bandgap voltage as reference for conversion. */ | (ADC_CONFIG_INPSEL_AnalogInputNoPrescaling << ADC_CONFIG_INPSEL_Pos) /*!< Analog input specified by PSEL with no prescaling used as input for the conversion. */ | (ADC_CONFIG_RES_8bit << ADC_CONFIG_RES_Pos); /*!< 8bit ADC resolution. */ sd_nvic_ClearPendingIRQ(ADC_IRQn); NRF_ADC->EVENTS_END = 0; /* Enable ADC*/ NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled; NRF_ADC->TASKS_START = 1; }
/**@brief Function for configuring ADC to do battery level conversion. */ static void adc_configure(void) { uint32_t err_code; nrf_adc_config_t adc_config = NRF_ADC_CONFIG_DEFAULT; // Configure ADC adc_config.reference = NRF_ADC_CONFIG_REF_VBG; adc_config.resolution = NRF_ADC_CONFIG_RES_8BIT; adc_config.scaling = NRF_ADC_CONFIG_SCALING_SUPPLY_ONE_THIRD; nrf_adc_configure(&adc_config); // Enable ADC interrupt nrf_adc_int_enable(ADC_INTENSET_END_Msk); err_code = sd_nvic_ClearPendingIRQ(ADC_IRQn); APP_ERROR_CHECK(err_code); err_code = sd_nvic_SetPriority(ADC_IRQn, NRF_APP_PRIORITY_LOW); APP_ERROR_CHECK(err_code); err_code = sd_nvic_EnableIRQ(ADC_IRQn); APP_ERROR_CHECK(err_code); }
uint32_t spi_master_open(const spi_master_hw_instance_t spi_master_hw_instance, spi_master_config_t const * const p_spi_master_config) { #if defined(SPI_MASTER_0_ENABLE) || defined(SPI_MASTER_1_ENABLE) /* Check against null */ if (p_spi_master_config == NULL) { return NRF_ERROR_NULL; } volatile spi_master_instance_t * p_spi_instance = spi_master_get_instance( spi_master_hw_instance); APP_ERROR_CHECK_BOOL(p_spi_instance != NULL); switch (spi_master_hw_instance) { #ifdef SPI_MASTER_0_ENABLE case SPI_MASTER_0: spi_master_init_hw_instance(NRF_SPI0, SPI0_TWI0_IRQn, p_spi_instance, p_spi_master_config->SPI_DisableAllIRQ); break; #endif /* SPI_MASTER_0_ENABLE */ #ifdef SPI_MASTER_1_ENABLE case SPI_MASTER_1: spi_master_init_hw_instance(NRF_SPI1, SPI1_TWI1_IRQn, p_spi_instance, p_spi_master_config->SPI_DisableAllIRQ); break; #endif /* SPI_MASTER_1_ENABLE */ default: break; } //A Slave select must be set as high before setting it as output, //because during connect it to the pin it causes glitches. nrf_gpio_pin_set(p_spi_master_config->SPI_Pin_SS); nrf_gpio_cfg_output(p_spi_master_config->SPI_Pin_SS); nrf_gpio_pin_set(p_spi_master_config->SPI_Pin_SS); //Configure GPIO nrf_gpio_cfg_output(p_spi_master_config->SPI_Pin_SCK); nrf_gpio_cfg_output(p_spi_master_config->SPI_Pin_MOSI); nrf_gpio_cfg_input(p_spi_master_config->SPI_Pin_MISO, NRF_GPIO_PIN_NOPULL); p_spi_instance->pin_slave_select = p_spi_master_config->SPI_Pin_SS; /* Configure SPI hardware */ p_spi_instance->p_nrf_spi->PSELSCK = p_spi_master_config->SPI_Pin_SCK; p_spi_instance->p_nrf_spi->PSELMOSI = p_spi_master_config->SPI_Pin_MOSI; p_spi_instance->p_nrf_spi->PSELMISO = p_spi_master_config->SPI_Pin_MISO; p_spi_instance->p_nrf_spi->FREQUENCY = p_spi_master_config->SPI_Freq; p_spi_instance->p_nrf_spi->CONFIG = (uint32_t)(p_spi_master_config->SPI_CONFIG_CPHA << SPI_CONFIG_CPHA_Pos) | (p_spi_master_config->SPI_CONFIG_CPOL << SPI_CONFIG_CPOL_Pos) | (p_spi_master_config->SPI_CONFIG_ORDER << SPI_CONFIG_ORDER_Pos); /* Clear waiting interrupts and events */ p_spi_instance->p_nrf_spi->EVENTS_READY = 0; APP_ERROR_CHECK(sd_nvic_ClearPendingIRQ(p_spi_instance->irq_type)); APP_ERROR_CHECK(sd_nvic_SetPriority(p_spi_instance->irq_type, p_spi_master_config->SPI_PriorityIRQ)); /* Clear event handler */ p_spi_instance->callback_event_handler = NULL; /* Enable interrupt */ p_spi_instance->p_nrf_spi->INTENSET = (SPI_INTENSET_READY_Set << SPI_INTENCLR_READY_Pos); APP_ERROR_CHECK(sd_nvic_EnableIRQ(p_spi_instance->irq_type)); /* Enable SPI hardware */ p_spi_instance->p_nrf_spi->ENABLE = (SPI_ENABLE_ENABLE_Enabled << SPI_ENABLE_ENABLE_Pos); /* Change state to IDLE */ p_spi_instance->state = SPI_MASTER_STATE_IDLE; return NRF_SUCCESS; #else return NRF_ERROR_NOT_SUPPORTED; #endif }
int main(void) { u32 err; //Initialize the UART Terminal Terminal::Init(); //Initialialize the SoftDevice and the BLE stack bleInit(); //Enable logging for some interesting log tags Logger::getInstance().enableTag("NODE"); Logger::getInstance().enableTag("STORAGE"); Logger::getInstance().enableTag("DATA"); Logger::getInstance().enableTag("SEC"); //Initialize the storage class Storage::getInstance(); //Init the magic node = new Node(Config->meshNetworkIdentifier); new Testing(); struct mallinfo used = mallinfo(); volatile u32 size = used.uordblks + used.hblkhd; //Start Timers initTimers(); while (true) { u32 err = NRF_ERROR_NOT_FOUND; //Check if there is input on uart Terminal::PollUART(); do { //Fetch the event sizeOfCurrentEvent = sizeOfEvent; err = sd_ble_evt_get(currentEventBuffer, &sizeOfCurrentEvent); //Handle ble event event if (err == NRF_SUCCESS) { //logt("EVENT", "--- EVENT_HANDLER %d -----", currentEvent->header.evt_id); bleDispatchEventHandler(currentEvent); } //No more events available else if (err == NRF_ERROR_NOT_FOUND) { //Handle Timer event that was waiting if (node && node->passsedTimeSinceLastTimerHandler > 0) { //Call the timer handler from the node node->TimerTickHandler(node->passsedTimeSinceLastTimerHandler); //Dispatch timer to all other modules timerEventDispatch(node->passsedTimeSinceLastTimerHandler, node->appTimerMs); node->passsedTimeSinceLastTimerHandler = 0; } err = sd_app_evt_wait(); APP_ERROR_CHECK(err); sd_nvic_ClearPendingIRQ(SD_EVT_IRQn); break; } else { APP_ERROR_CHECK(err); break; } } while (true); } }