Beispiel #1
0
int main(void)
{
    nrf_gpio_cfg_output(PIN_LED_0);
    nrf_gpio_cfg_output(PIN_LED_1);
    nrf_gpio_cfg_input(PIN_BUTTON_0, GPIO_PIN_CNF_PULL_Pullup);

    nrf_gpio_pin_clear(PIN_LED_0);
    nrf_gpio_pin_clear(PIN_LED_1);

    lfclk_request();

    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS,
                   APP_TIMER_OP_QUEUE_SIZE, app_timer_evt_schedule);

    APP_SCHED_INIT(SCHED_MAX_EVENT_DATA_SIZE, SCHED_QUEUE_SIZE); 

    create_timers();

    timer_start(0);
    timer_start(1);
    
    while (1) {
        app_sched_execute();
        __WFI();
    }
}
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);
}
uint32_t sdm_rx_init(void)
{       
#if defined(TRACE_UART)
    app_uart_comm_params_t comm_params =  
    {
        RX_PIN_NUMBER, 
        TX_PIN_NUMBER, 
        RTS_PIN_NUMBER, 
        CTS_PIN_NUMBER, 
        APP_UART_FLOW_CONTROL_DISABLED, 
        false, 
        UART_BAUDRATE_BAUDRATE_Baud38400
    }; 
        
    uint32_t err_code;
    APP_UART_FIFO_INIT(&comm_params, 
                       UART_RX_BUF_SIZE, 
                       UART_TX_BUF_SIZE, 
                       uart_error_handle, 
                       APP_IRQ_PRIORITY_LOW,
                       err_code);
    APP_ERROR_CHECK(err_code);
#endif // TRACE_UART
#if defined(TRACE_GPIO)
    //  Configure pins LED_0 and LED_1 as outputs. 
    nrf_gpio_range_cfg_output(SDM_GPIO_START, SDM_GPIO_STOP);
    
    //  Reset the pins if they already are in a high state. 
    nrf_gpio_pin_clear(SDM_GPIO_0);
    nrf_gpio_pin_clear(SDM_GPIO_1);
    nrf_gpio_pin_clear(SDM_GPIO_2);
#endif // TRACE_GPIO

    return NRF_SUCCESS;         
}
/**< simple uart deinit. 					*/
void simple_uart_deinit(uint8_t rts_pin_number,
                        uint8_t txd_pin_number,
                        uint8_t cts_pin_number,
                        uint8_t rxd_pin_number,
                        bool    hwfc)
{
	NRF_UART0->TASKS_STOPRX	=	1;
	NRF_UART0->TASKS_STOPTX =	1;

	NRF_UART0->ENABLE		=	(UART_ENABLE_ENABLE_Disabled << UART_ENABLE_ENABLE_Pos);
		
	nrf_gpio_cfg_output(txd_pin_number);					//将不用的io设置为输出拉低模式,电流可以降到理论水平,不用串口时则将串口失能
	nrf_gpio_cfg_output(rxd_pin_number);
	nrf_gpio_pin_clear(txd_pin_number);
	nrf_gpio_pin_clear(rxd_pin_number);

	if(hwfc)
	{
		nrf_gpio_cfg_output(rts_pin_number);
		nrf_gpio_cfg_output(cts_pin_number);

		nrf_gpio_pin_clear(rts_pin_number);
		nrf_gpio_pin_clear(cts_pin_number);		
	}
}
Beispiel #5
0
/**@brief Function for turning on all leds. 
 *
 */
void n5_io_set_leds(void)
{
    nrf_gpio_pin_clear(LED_A);
    nrf_gpio_pin_clear(LED_B);
    nrf_gpio_pin_clear(LED_C);
    nrf_gpio_pin_clear(LED_D);
}
Beispiel #6
0
/**@brief Function for handling the Application's BLE Stack events.
 *
 * @param[in]   p_ble_evt   Bluetooth stack event.
 */
static void on_ble_evt(ble_evt_t * p_ble_evt)
{
    uint32_t err_code = NRF_SUCCESS;

    switch (p_ble_evt->header.evt_id)
    {
    case BLE_GAP_EVT_CONNECTED:
        nrf_gpio_pin_set(CONNECTED_LED_PIN_NO);
        nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO);
        m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
        break;

    case BLE_GAP_EVT_DISCONNECTED:
        nrf_gpio_pin_clear(CONNECTED_LED_PIN_NO);
        m_conn_handle = BLE_CONN_HANDLE_INVALID;

        advertising_start();
        break;

    case BLE_GAP_EVT_TIMEOUT:
        if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT)
        {
            nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO);

            // Go to system-off mode (this function will not return; wakeup will cause a reset).
            err_code = sd_power_system_off();
            APP_ERROR_CHECK(err_code);
        }
        break;

    default:
        // No implementation needed.
        break;
    }
}
Beispiel #7
0
static void twi_clear_bus(twi_info_t *twi_info)
{
    // Try to set SDA high, and check if no slave tries to drive it low.
    nrf_gpio_pin_set(twi_info->pselsda);
    configure_twi_pin(twi_info->pselsda, NRF_GPIO_PIN_DIR_OUTPUT);
    // In case SDA is low, make up to 9 cycles on SCL line to help the slave
    // that pulls SDA low release it.
    if (!nrf_gpio_pin_read(twi_info->pselsda)) {
        nrf_gpio_pin_set(twi_info->pselscl);
        configure_twi_pin(twi_info->pselscl, NRF_GPIO_PIN_DIR_OUTPUT);
        nrf_delay_us(4);

        for (int i = 0; i < 9; i++) {
            if (nrf_gpio_pin_read(twi_info->pselsda)) {
                break;
            }
            nrf_gpio_pin_clear(twi_info->pselscl);
            nrf_delay_us(4);
            nrf_gpio_pin_set(twi_info->pselscl);
            nrf_delay_us(4);
        }

        // Finally, generate STOP condition to put the bus into initial state.
        nrf_gpio_pin_clear(twi_info->pselsda);
        nrf_delay_us(4);
        nrf_gpio_pin_set(twi_info->pselsda);
    }
}
Beispiel #8
0
 */
static void alert_signal(uint8_t alert_level)
{
    switch (alert_level)
    {
        case BLE_CHAR_ALERT_LEVEL_NO_ALERT:
            m_is_link_loss_alerting = false;
            nrf_gpio_pin_clear(ALERT_LEVEL_MILD_LED_PIN_NO);
            nrf_gpio_pin_clear(ALERT_LEVEL_HIGH_LED_PIN_NO);
            break;

        case BLE_CHAR_ALERT_LEVEL_MILD_ALERT:
            nrf_gpio_pin_set(ALERT_LEVEL_MILD_LED_PIN_NO);
            nrf_gpio_pin_clear(ALERT_LEVEL_HIGH_LED_PIN_NO);
            break;

        case BLE_CHAR_ALERT_LEVEL_HIGH_ALERT:
            nrf_gpio_pin_clear(ALERT_LEVEL_MILD_LED_PIN_NO);
            nrf_gpio_pin_set(ALERT_LEVEL_HIGH_LED_PIN_NO);
            break;

        default:
            // No implementation needed.
            break;
    }
/**@brief Function for clearing the LEDs.
 *
 * @details Clears all LEDs used by the application.
 */
static void leds_off(void)
{
    nrf_gpio_pin_clear(LED_0);
    nrf_gpio_pin_clear(LED_1);
    nrf_gpio_pin_clear(LED_2);
    nrf_gpio_pin_clear(LED_7);
}
Beispiel #10
0
/**@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;
}
Beispiel #11
0
void mlcd_init(void)
{
    nrf_gpio_cfg_output(LCD_ENABLE);
    nrf_gpio_cfg_output(LCD_BACKLIGHT);
    nrf_gpio_cfg_output(LCD_VOLTAGE_REG);
    nrf_gpio_pin_clear(LCD_ENABLE);
    nrf_gpio_pin_clear(LCD_BACKLIGHT);
    nrf_gpio_pin_clear(LCD_VOLTAGE_REG);
}
Beispiel #12
0
/**@brief Function for handling the Application's BLE Stack events.
 *
 * @param[in]   p_ble_evt   Bluetooth stack event.
 */
static void on_ble_evt(ble_evt_t * p_ble_evt)
{
    uint32_t        err_code;

    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            nrf_gpio_pin_set(IMM_NRF5_CONNECT_LED);

            g_LmxServ.conn_handle  = p_ble_evt->evt.gap_evt.conn_handle;
            break;

        case BLE_GAP_EVT_DISCONNECTED:
            nrf_gpio_pin_clear(IMM_NRF5_CONNECT_LED);
            g_LmxServ.conn_handle = BLE_CONN_HANDLE_INVALID;

            advertising_start();

            // Go to system-off mode, should not return from this function, wakeup will trigger
            // a reset.
            //system_off_mode_enter();
            break;

        case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
            //err_code = sd_ble_gap_sec_params_reply(g_LmxServ.conn_handle,
            //                                       BLE_GAP_SEC_STATUS_SUCCESS,
            //                                       &g_GAPSecParams);
            //APP_ERROR_CHECK(err_code);
            break;

        case BLE_GAP_EVT_TIMEOUT:
            if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISING)
            {
            	nrf_gpio_pin_clear(IMM_NRF5_CONNECT_LED);

                    // Go to system-off mode.
                    // (this function will not return; wakeup will cause a reset).
                    //err_code = sd_power_system_off();
                	advertising_start();
                    APP_ERROR_CHECK(err_code);
            }
            break;

        case BLE_GATTC_EVT_TIMEOUT:
        case BLE_GATTS_EVT_TIMEOUT:
            // Disconnect on GATT Server and Client timeout events.
            err_code = sd_ble_gap_disconnect(g_LmxServ.conn_handle,
                                             BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
            APP_ERROR_CHECK(err_code);
            advertising_start();
            break;

        default:
            // No implementation needed.
            break;
    }
}
/**@brief Function for the Application's S110 SoftDevice event handler.
 *
 * @param[in] p_ble_evt S110 SoftDevice event.
 */
static void on_ble_evt(ble_evt_t * p_ble_evt)
{
    uint32_t err_code;

    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            nrf_gpio_pin_set(CONNECTED_LED_PIN_NO);
            nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO);

            m_conn_handle    = p_ble_evt->evt.gap_evt.conn_handle;
            m_is_advertising = false;
            break;

        case BLE_GAP_EVT_DISCONNECTED:
            nrf_gpio_pin_clear(CONNECTED_LED_PIN_NO);

            if (!m_tear_down_in_progress)
            {
                // The Disconnected event is because of an external event. (Link loss or
                // disconnect triggered by the DFU Controller before the firmware update was
                // complete).
                // Restart advertising so that the DFU Controller can reconnect if possible.
                advertising_start();
            }

            m_conn_handle = BLE_CONN_HANDLE_INVALID;

            break;

        case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
            err_code = sd_ble_gap_sec_params_reply(m_conn_handle,
                                                   BLE_GAP_SEC_STATUS_SUCCESS,
                                                   &m_sec_params);
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GATTS_EVT_TIMEOUT:
            if (p_ble_evt->evt.gatts_evt.params.timeout.src == BLE_GATT_TIMEOUT_SRC_PROTOCOL)
            {
                err_code = sd_ble_gap_disconnect(m_conn_handle,
                                                 BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
                APP_ERROR_CHECK(err_code);
            }
            break;

        case BLE_GATTS_EVT_SYS_ATTR_MISSING:
            err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0);
            APP_ERROR_CHECK(err_code);
            break;

        default:
            // No implementation needed.
            break;
    }
}
/**@brief Application's Stack BLE event handler.
 *
 * @param[in]   p_ble_evt   Bluetooth stack event.
 */
static void on_ble_evt(ble_evt_t * p_ble_evt)
{
    uint32_t err_code = NRF_SUCCESS;

    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            nrf_gpio_pin_set(CONNECTED_LED_PIN_NO);
            nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO);
            m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
            break;
            
        case BLE_GAP_EVT_DISCONNECTED:
            nrf_gpio_pin_clear(CONNECTED_LED_PIN_NO);
            m_conn_handle = BLE_CONN_HANDLE_INVALID;

            // Need to close the ANT channel to make it safe to write bonding information to flash
            err_code = sd_ant_channel_close(ANT_HRMRX_ANT_CHANNEL);
            APP_ERROR_CHECK(err_code);
            
            // Note: Bonding information will be stored, advertising will be restarted and the
            //       ANT channel will be reopened when ANT event CHANNEL_CLOSED is received.
            break;

#ifndef BONDING_ENABLE
        case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
            err_code = sd_ble_gap_sec_params_reply(m_conn_handle, 
                                                   BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, 
                                                   NULL);
#endif // BONDING_ENABLE
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GAP_EVT_TIMEOUT:
            if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT)
            { 
                nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO);
                
                // Go to system-off mode (this function will not return; wakeup will cause a reset)
                err_code = sd_power_system_off();
                APP_ERROR_CHECK(err_code);
            }
            break;

#ifndef	BONDING_ENABLE
            case BLE_GATTS_EVT_SYS_ATTR_MISSING:
                err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0);
                APP_ERROR_CHECK(err_code);
                break;
#endif // BONDING_ENABLE
            
        default:
            // No implementation needed.
            break;
    }
}
Beispiel #15
0
/*---------------------------   -------------------------*/
void system_led_flicker_init()
{
    // gpio and timer init
    // timer init
    nrf_gpio_cfg_output(RED_LED);
    nrf_gpio_cfg_output(GREEN_LED);
    nrf_gpio_pin_clear(RED_LED);
    nrf_gpio_pin_clear(GREEN_LED);

}
Beispiel #16
0
// Clearing an LED actually lights it for the NRF52 platform and setting the LED turns it off.
void debug_all_leds_on(void) {
  nrf_gpio_cfg_output(LED_1);
  nrf_gpio_pin_clear(LED_1);
  nrf_gpio_cfg_output(LED_2);
  nrf_gpio_pin_clear(LED_2);
  nrf_gpio_cfg_output(LED_3);
  nrf_gpio_pin_clear(LED_3);
  nrf_gpio_cfg_output(LED_4);
  nrf_gpio_pin_clear(LED_4);
}
//function to init the PWM -> results in a PWM signal and an inverted PWM signal on pins CALLIOPE_SM_PIN_IN2 and CALLIOPE_SM_PIN_IN1
void CalliopeSoundMotor::PWM_init()
{
    //init pins
    nrf_gpio_cfg_output(CALLIOPE_SM_PIN_IN1);
    nrf_gpio_cfg_output(CALLIOPE_SM_PIN_IN2);
    nrf_gpio_pin_clear(CALLIOPE_SM_PIN_IN1);
    nrf_gpio_pin_clear(CALLIOPE_SM_PIN_IN2);

    //create tasks to perform on timer compare match
    NRF_GPIOTE->POWER = 1;
    //task 0
    nrf_gpiote_task_configure(0, CALLIOPE_SM_PIN_IN1, NRF_GPIOTE_POLARITY_TOGGLE, NRF_GPIOTE_INITIAL_VALUE_LOW);
    nrf_gpiote_task_enable(0);
    //task 1
    nrf_gpiote_task_configure(1, CALLIOPE_SM_PIN_IN2, NRF_GPIOTE_POLARITY_TOGGLE, NRF_GPIOTE_INITIAL_VALUE_HIGH);
    nrf_gpiote_task_enable(1);

    //Three NOPs are required to make sure configuration is written before setting tasks or getting events
    __NOP();
    __NOP();
    __NOP();

    //connect the tasks to the corresponding compare match events, toggle twice per period (PWM)
    //connect task 0
    NRF_PPI->CH[0].EEP = (uint32_t)&NRF_TIMER2->EVENTS_COMPARE[0];
    NRF_PPI->CH[0].TEP = (uint32_t)&NRF_GPIOTE->TASKS_OUT[0];
    //connect task 1
    NRF_PPI->CH[1].EEP = (uint32_t)&NRF_TIMER2->EVENTS_COMPARE[1];
    NRF_PPI->CH[1].TEP = (uint32_t)&NRF_GPIOTE->TASKS_OUT[1];

    //connect task 0
    NRF_PPI->CH[2].EEP = (uint32_t)&NRF_TIMER2->EVENTS_COMPARE[2];
    NRF_PPI->CH[2].TEP = (uint32_t)&NRF_GPIOTE->TASKS_OUT[0];
    //connect task 1
    NRF_PPI->CH[3].EEP = (uint32_t)&NRF_TIMER2->EVENTS_COMPARE[3];
    NRF_PPI->CH[3].TEP = (uint32_t)&NRF_GPIOTE->TASKS_OUT[1];

    NRF_PPI->CHENSET = 15; // bits 0 - 3 for channels 0 - 3

     //init TIMER2 for PWM use
    NRF_TIMER2->POWER = 1;
    NRF_TIMER2->MODE = TIMER_MODE_MODE_Timer << TIMER_MODE_MODE_Pos;
    NRF_TIMER2->BITMODE = TIMER_BITMODE_BITMODE_16Bit << TIMER_BITMODE_BITMODE_Pos;
    NRF_TIMER2->PRESCALER = CALLIOPE_SM_PRESCALER_M;
    NRF_TIMER2->TASKS_CLEAR = 1;

    //initialize compare registers
    //set compare registers 0 and 1 (duty cycle for PWM on pins CALLIOPE_SM_PIN_IN1 and CALLIOPE_SM_PIN_IN2)
    NRF_TIMER2->CC[0] = CALLIOPE_SM_PERIOD_M - CALLIOPE_SM_DEFAULT_DUTY_M;
    NRF_TIMER2->CC[1] = CALLIOPE_SM_DEFAULT_DUTY_M-1;
    //set compare register 2 and 3 (period for PWM on pins CALLIOPE_SM_PIN_IN1 and CALLIOPE_SM_PIN_IN2)
    NRF_TIMER2->CC[2] = CALLIOPE_SM_PERIOD_M-1;
    NRF_TIMER2->CC[3] = CALLIOPE_SM_PERIOD_M;
    NRF_TIMER2->SHORTS = TIMER_SHORTS_COMPARE3_CLEAR_Msk;
}
Beispiel #18
0
ret_code_t nrf_drv_csense_sample(void)
{
    ASSERT(m_csense.module_state == NRF_DRV_STATE_POWERED_ON);

    if(m_csense.adc_channels_input_mask != 0)
    {
        if(m_csense.channels_to_read == 0)
        {
#if USE_COMP == 0 && defined(SAADC_PRESENT)
            nrf_saadc_enable();
#endif
            if(nrf_drv_csense_is_busy() == true)
            {
                return NRF_ERROR_BUSY;
            }
            m_csense.busy = true;
            m_csense.channels_to_read = m_csense.adc_channels_input_mask;
            calculate_next_channel();
        }

#if USE_COMP
        if (!m_csense.timers_powered_on)
        {
            nrf_drv_timer_enable(&m_timer0);
            nrf_drv_timer_enable(&m_timer1);
            m_csense.timers_powered_on = true;
        }
        else
        {
            nrf_drv_timer_resume(&m_timer0);
            nrf_drv_timer_resume(&m_timer1);
        }
        nrf_drv_comp_pin_select((nrf_comp_input_t)m_csense.cur_chann_idx);
        nrf_drv_comp_start(0, 0);
#else
        ret_code_t err_code;
#ifdef ADC_PRESENT
        adc_channel.config.config.ain = (nrf_adc_config_input_t)(1<<m_csense.cur_chann_idx);
        nrf_gpio_pin_clear(m_csense.output_pin);
        err_code = nrf_drv_adc_sample_convert(&adc_channel, NULL);
#elif defined(SAADC_PRESENT)
        saadc_channel.pin_p = (nrf_saadc_input_t)(m_csense.cur_chann_idx + 1);
        nrf_saadc_channel_input_set(0, saadc_channel.pin_p, NRF_SAADC_INPUT_DISABLED);
        nrf_gpio_pin_clear(m_csense.output_pin);
        err_code = nrf_drv_saadc_sample();
#endif //ADC_PRESENT
        if(err_code != NRF_SUCCESS)
        {
            return err_code;
        }
#endif //USE_COMP
    }

    return NRF_SUCCESS;
}
/**@brief Function for handling the Application's BLE Stack events.
 *
 * @param[in]   p_ble_evt   Bluetooth stack event.
 */
static void on_ble_evt(ble_evt_t * p_ble_evt)
{
    uint32_t err_code;

    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            nrf_gpio_pin_set(CONNECTED_LED_PIN_NO);
            nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO);

            m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
            break;

        case BLE_GAP_EVT_DISCONNECTED:
            nrf_gpio_pin_clear(CONNECTED_LED_PIN_NO);


            advertising_start();
            break;

        case BLE_GAP_EVT_TIMEOUT:
            if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT)
            {
                nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO);

                // Go to system-off mode (this function will not return; wakeup will cause a reset).
                err_code = sd_power_system_off();
                APP_ERROR_CHECK(err_code);
            }
            break;
				case BLE_EVT_USER_MEM_REQUEST:
            
          
            mem_block.len = QUEUED_WRITE_BUFFER_SIZE;
            mem_block.p_mem = &queued_write_buffer[0];
            err_code = sd_ble_user_mem_reply(m_conn_handle, &mem_block);
						//simple_uart_putstring("User mem request \r\n");
            break;
        
        case BLE_EVT_USER_MEM_RELEASE:
						
            if ((p_ble_evt->evt.common_evt.params.user_mem_release.mem_block.p_mem == mem_block.p_mem)&&(p_ble_evt->evt.common_evt.params.user_mem_release.mem_block.len == mem_block.len))
            {
                //memory released do nothing. 
								//simple_uart_putstring("User mem released \r\n");
            }
						break;
				case BLE_GATTS_EVT_WRITE:
								//simple_uart_putstring("Evt Write \r\n");
            break;
        default:
            // No implementation needed.
            break;
    }
}
Beispiel #20
0
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;
}
Beispiel #21
0
/*---------------------------------------------------------------------------*/
static void buzzer_gpiote_unconfig(void)
{
    nrf_gpiote_unconfig(GPIOTE_CHANNEL_NUMBER_0);
    nrf_gpiote_unconfig(GPIOTE_CHANNEL_NUMBER_1);

    nrf_gpio_cfg_output(BUZZ1);
    nrf_gpio_cfg_output(BUZZ2);

    nrf_gpio_pin_clear(BUZZ1);
    nrf_gpio_pin_clear(BUZZ2);
}
Beispiel #22
0
void test_led(void)
{
  NRF_LOG_INFO("Turning LEDs on for a second\r\n");
  NRF_LOG_FLUSH();
  nrf_delay_ms(10);
  nrf_gpio_pin_clear(LED_RED);
  nrf_gpio_pin_clear(LED_GREEN);
  nrf_delay_ms(1000);
  nrf_gpio_pin_set(LED_RED);
  nrf_gpio_pin_set(LED_GREEN);
  nrf_delay_ms(10);
}
Beispiel #23
0
/**@brief Function for application main entry.
 */
int main(void)
{
    uint32_t err_code;
    bool     bootloader_is_pushed = false;
    
    leds_init();

    // This check ensures that the defined fields in the bootloader corresponds with actual
    // setting in the nRF51 chip.
    APP_ERROR_CHECK_BOOL(NRF_UICR->CLENR0 == CODE_REGION_1_START);

    APP_ERROR_CHECK_BOOL(*((uint32_t *)NRF_UICR_BOOT_START_ADDRESS) == BOOTLOADER_REGION_START);
    APP_ERROR_CHECK_BOOL(NRF_FICR->CODEPAGESIZE == CODE_PAGE_SIZE);

    // Initialize.
    timers_init();
    gpiote_init();
    buttons_init();
    ble_stack_init();
    scheduler_init();

    bootloader_is_pushed = ((nrf_gpio_pin_read(BOOTLOADER_BUTTON_PIN) == 0)? true: false);
    
    if (bootloader_is_pushed || (!bootloader_app_is_valid(DFU_BANK_0_REGION_START)))
    {
        nrf_gpio_pin_set(LED_2);

        // Initiate an update of the firmware.
        err_code = bootloader_dfu_start();
        APP_ERROR_CHECK(err_code);

        nrf_gpio_pin_clear(LED_2);
    }

    if (bootloader_app_is_valid(DFU_BANK_0_REGION_START))
    {
        leds_off();
        
        // Select a bank region to use as application region.
        // @note: Only applications running from DFU_BANK_0_REGION_START is supported.
        bootloader_app_start(DFU_BANK_0_REGION_START);
        
    }

    nrf_gpio_pin_clear(LED_0);
    nrf_gpio_pin_clear(LED_1);
    nrf_gpio_pin_clear(LED_2);
    nrf_gpio_pin_clear(LED_7);
    
    NVIC_SystemReset();
}
Beispiel #24
0
static void pmNrfPower(bool enable)
{
  if (!enable) {
    //stop NRF
    LED_OFF();
    // Turn off PA
    if (platformHasRfx2411n()) {
      nrf_gpio_pin_clear(RADIO_PA_RX_EN);
      nrf_gpio_pin_clear(RADIO_PA_MODE);
      nrf_gpio_pin_clear(RADIO_PA_ANT_SW);
    } else {
      nrf_gpio_pin_clear(RADIO_PAEN_PIN);
    }
    // Disable 1-wire pull-up
    nrf_gpio_pin_clear(OW_PULLUP_PIN);
    // CE, EN1 and EN2 externally pulled low. Put low to not draw any current.
    nrf_gpio_pin_clear(PM_EN1);
    nrf_gpio_pin_clear(PM_EN2);
    nrf_gpio_pin_clear(PM_CHG_EN);

    nrf_gpio_cfg_input(PM_VBAT_SINK_PIN, NRF_GPIO_PIN_NOPULL);
    NRF_POWER->GPREGRET |= 0x01; // Workaround for not being able to determine reset reason...
#ifdef BLE
    sd_power_system_off();
#else
    NRF_POWER->SYSTEMOFF = 1UL;
#endif
    while(1);
  }
}
Beispiel #25
0
static void pmPowerSystem(bool enable)
{
  if (enable) {
    NRF_GPIO->PIN_CNF[STM_NRST_PIN] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
                                      | (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos)
                                      | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
                                      | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
                                      | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
    nrf_gpio_pin_clear(STM_NRST_PIN); //Hold STM reset
    nrf_gpio_pin_set(PM_VCCEN_PIN);
  } else {
    nrf_gpio_cfg_input(STM_NRST_PIN, NRF_GPIO_PIN_PULLDOWN);
    nrf_gpio_pin_clear(PM_VCCEN_PIN);
  }
}
/**
* App error check callback. Something went wrong, and we go into a blocking state, as continued 
* executing may lead to undefined behavoiur.
*/
void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name)
{
  char buf[256];

  sprintf(&buf[0], "ERROR: 0x%X, line: %d, file: %s\r\n", error_code, line_num, p_file_name);
  uart_putstring((uint8_t* ) buf);
#if defined(BOARD_PCA10028)
  nrf_gpio_pin_clear(LED_1);
  nrf_gpio_pin_clear(LED_2);
#elif defined(BOARD_PCA10031)
  nrf_gpio_pin_clear(LED_RGB_BLUE);
#endif

  while(1);
}
/**@brief       Function for the Application's S110 SoftDevice event handler.
 *
 * @param[in]   p_ble_evt   S110 SoftDevice event.
 */
static void on_ble_evt(ble_evt_t * p_ble_evt)
{
    uint32_t                         err_code;
    
    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            nrf_gpio_pin_set(CONNECTED_LED_PIN_NO);
            nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO);
            m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;

            break;
            
        case BLE_GAP_EVT_DISCONNECTED:
            nrf_gpio_pin_clear(CONNECTED_LED_PIN_NO);
            m_conn_handle = BLE_CONN_HANDLE_INVALID;

            advertising_start();

            break;
                    
        case BLE_GAP_EVT_PASSKEY_DISPLAY:
            // Don't send delayed Security Request if security procedure is already in progress.
            err_code = app_timer_stop(m_sec_req_timer_id);
            APP_ERROR_CHECK(err_code);
            break;        

        case BLE_GAP_EVT_TIMEOUT:
            if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT)
            { 
                nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO);

                // Configure buttons with sense level low as wakeup source.
                nrf_gpio_cfg_sense_input(WAKEUP_BUTTON_PIN,
                                         BUTTON_PULL,
                                         NRF_GPIO_PIN_SENSE_LOW);
                
                // Go to system-off mode (this function will not return; wakeup will cause a reset)
                err_code = sd_power_system_off();    
                APP_ERROR_CHECK(err_code);
            }
            break;

        default:
            // No implementation needed.
            break;
    }
}
Beispiel #28
0
/**@brief Function for handling the Application's BLE Stack events.
 *
 * @param[in]   p_ble_evt   Bluetooth stack event.
 */
static void on_ble_evt(ble_evt_t * p_ble_evt)
{
    uint32_t err_code;

    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            nrf_gpio_pin_set(CONNECTED_LED_PIN_NO);
            nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO);

            m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
            break;

        case BLE_GAP_EVT_DISCONNECTED:
            nrf_gpio_pin_clear(CONNECTED_LED_PIN_NO);

            m_conn_handle = BLE_CONN_HANDLE_INVALID;

            // Since we are not in a connection and have not started advertising, store bonds.
            err_code = ble_bondmngr_bonded_centrals_store();
            APP_ERROR_CHECK(err_code);

            advertising_start();
            break;

        case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
            err_code = sd_ble_gap_sec_params_reply(m_conn_handle,
                                                   BLE_GAP_SEC_STATUS_SUCCESS,
                                                   &m_sec_params);
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GAP_EVT_TIMEOUT:
            if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT)
            {
                nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO);

                // Go to system-off mode (this function will not return; wakeup will cause a reset).
                err_code = sd_power_system_off();
                APP_ERROR_CHECK(err_code);
            }
            break;

        default:
            // No implementation needed.
            break;
    }
}
Beispiel #29
0
/*====================================================================================================*/
int main( void )
{
  u8 i = 0;

  GPIO_Config();
  TIMER_Config();

  nrf_gpio_pin_set(LED_0);
  nrf_gpio_pin_clear(LED_1);

  while(1) {

//    nrf_gpio_pin_toggle(LED_0);
    nrf_gpio_pin_toggle(LED_1);
    for(i = 0; i<7; i++) {
      LED_PORT = ~(0x01<<i);
      Delay_1ms(DELAY_TIME);
    }
//    nrf_gpio_pin_toggle(LED_0);
    nrf_gpio_pin_toggle(LED_1);
    for(i = 7; i>0; i--) {
      LED_PORT = ~(0x01<<i);
      Delay_1ms(DELAY_TIME);
    }
  }
}
static void set_request_line(void)
{
    //active low logic - set is 0
    nrf_gpio_pin_clear(m_spi_slave_raw_config.pin_req);
    DEBUG_EVT_SPI_SLAVE_RAW_REQ_SET(0);
    return;
}