Example #1
0
static void st7735_rect_draw(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint32_t color)
{
    set_addr_window(x, y, x + width - 1, y + height - 1);

    color = RGB2BGR(color);

    const uint8_t data[2] = {color >> 8, color};

    nrf_gpio_pin_set(ST7735_DC_PIN);

    // Duff's device algorithm for optimizing loop.
    uint32_t i = (height * width + 7) / 8;

/*lint -save -e525 -e616 -e646 */
    switch ((height * width) % 8) {
        case 0:
            do {
                spi_write(data, sizeof(data));
        case 7:
                spi_write(data, sizeof(data));
        case 6:
                spi_write(data, sizeof(data));
        case 5:
                spi_write(data, sizeof(data));
        case 4:
                spi_write(data, sizeof(data));
        case 3:
                spi_write(data, sizeof(data));
        case 2:
                spi_write(data, sizeof(data));
        case 1:
                spi_write(data, sizeof(data));
            } while (--i > 0);
        default:
            break;
    }
/*lint -restore */
    nrf_gpio_pin_clear(ST7735_DC_PIN);
}
Example #2
0
/**
 * @brief Function for main application entry.
 */
int main(void)
{
    nrf_gpio_cfg_output(LED_1);
    nrf_gpio_pin_set(LED_1);
    uart_config();
    printf("\033[2J\033[;H\r\n* Example to find TWI devices connected to \r\n* the TWI bus and their addresses\r\n\r\n");
    twi_init();
    
    uint8_t dummy_data = 0x55;
    // Itterate through all possible 7-bit TWI addresses
    for(uint8_t i = 0; i <= 0x7F; i++)
    {
        device_address = i;
        // Send dummy data. If a device is present on this particular address a TWI EVT_DONE event is 
        // received in the twi event handler and a message is printed to UART
        nrf_drv_twi_tx(&twi_instance, i, &dummy_data, 1, false);
        // Delay 10 ms to allow TWI transfer to complete and UART to print messages before starting new transfer
        nrf_delay_ms(10);
    }
    if(device_found)
    {
        // Blinke LED_1 rapidly if device is found
        while(true)
        {
            nrf_gpio_pin_toggle(LED_1);
            nrf_delay_ms(100);
        }
    }
    else
    {
        // Disable LED_1 if device is NOT found
        nrf_gpio_cfg_default(LED_1);
        printf("*****************\n\rNO DEVICES FOUND!\r\n*****************\n\r");
        while(true)
        {
            ;
        }      
    }
}
Example #3
0
/**
 * @brief Write_Config : send write configuration (WC) command and data.
 * @param none
 */
void Write_Config(void)
{	
	uint32_t err_code;
	
	unsigned char config_data[11]={
	0x00,
  	0x6A,                   //CH_NO,433MHZ
	0x0c,                   //output power 10db, resend disable, Current Normal operation
	0x44,                   //4-byte address
    	0x03,0x03,              //receive or send data length 32 bytes
	0xcc,0xcc,0xcc,0xcc,    //receiving address
    	0x58,									  //CRC enable,8bit CRC,external clock disable,16MHZ Oscillator               
	};
		
	/* Spi enable for write a spi command */
	nrf_gpio_pin_clear(CSN);

	err_code = nrf_drv_spi_transfer(&spi, config_data, 11, NULL, 0);
	APP_ERROR_CHECK(err_code);
		
	nrf_gpio_pin_set(CSN);					/* Disable Spi */
}
void PacketData_Update()
{
    uint8_t         flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
    PACKET_INCR++; //Incrementing the value

    incr_cnt++;
    if(incr_cnt==5)
    {
        incr_cnt = 0;
        _ADC_measurement = _SOILHUMIDITY_MEASURE;
        nrf_gpio_pin_set(GPIO2);
        nrf_delay_ms(2);
        soilhumidity_measure_start();
    }
    else
    {
        _ADC_measurement = _SUPERCAP_MEASURE;
        supercap_measure_start();
    }
    m_beacon_info[6]  = __SOIL_HUMIDITY; //Putting data into 2.4 Ghz beacon packet and update advertising data
    m_beacon_info[12] = __SUPERCAP_VOLTAGE>>8;
    m_beacon_info[13] = __SUPERCAP_VOLTAGE&0xFF;

    manuf_specific_data.data.p_data        = (uint8_t *) m_beacon_info;

    // Build and set advertising data.
    memset(&advdata, 0, sizeof(advdata));

    advdata.flags.size              = sizeof(flags);
    advdata.flags.p_data            = &flags;
    advdata.p_manuf_specific_data   = &manuf_specific_data;

    ble_advdata_set(&advdata, NULL);

    CC110L_PACKETCONTENT[2+6]  = __SOIL_HUMIDITY;   //Putting data into 868 Mhz packet
    CC110L_PACKETCONTENT[2+12] = __SUPERCAP_VOLTAGE>>8;
    CC110L_PACKETCONTENT[2+13] = __SUPERCAP_VOLTAGE&0xFF;
}
Example #5
0
void acc_read(uint8_t cmd, uint8_t len, uint8_t *data)
{
	nrf_gpio_pin_clear(CONFIG_ACC_nCS);

	/* send command */
	SPI_ACC->TXD = 0xC0|cmd;
	while (!SPI_ACC->EVENTS_READY);
	SPI_ACC->EVENTS_READY = 0;
	/* dummy read */
	SPI_ACC->RXD;

	while(len--)
	{
		/* send dummy zero */
		SPI_ACC->TXD = 0;
		while (!SPI_ACC->EVENTS_READY);
		SPI_ACC->EVENTS_READY = 0;

		*data++ = SPI_ACC->RXD;
	}

	nrf_gpio_pin_set(CONFIG_ACC_nCS);
}
Example #6
0
/**@brief Function for error handling, which is called when an error has occurred.
 *
 * @warning This handler is an example only and does not fit a final product. You need to analyze
 *          how your product is supposed to react in case of error.
 *
 * @param[in] error_code  Error code supplied to the handler.
 * @param[in] line_num    Line number where the handler is called.
 * @param[in] p_file_name Pointer to the file name.
 */
void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name)
{
    nrf_gpio_pin_set(ASSERT_LED_PIN_NO);

    // This call can be used for debug purposes during application development.
    // @note CAUTION: Activating this code will write the stack to flash on an error.
    //                This function should NOT be used in a final product.
    //                It is intended STRICTLY for development/debugging purposes.
    //                The flash write will happen EVEN if the radio is active, thus interrupting
    //                any communication.
    //                Use with care. Un-comment the line below to use.
    // ble_debug_assert_handler(error_code, line_num, p_file_name);

    // On assert, the system can only recover with a reset.
    simple_uart_putstring((const uint8_t*) "app error\r\n");
    simple_uart_putstring(p_file_name);
    simple_uart_putstring((const uint8_t*) "\r\n");
    uart_put_dec32bit(line_num);
    simple_uart_putstring((const uint8_t*) "\r\n");
    uart_put_dec32bit(error_code);
    simple_uart_putstring((const uint8_t*) "\r\n");
    NVIC_SystemReset();
}
Example #7
0
/**@brief Error handler function, which is called when an error has occurred. 
 *
 * @param[in] error_code  Error code supplied to the handler.
 * @param[in] line_num    Line number where the handler is called.
 * @param[in] p_file_name Pointer to the file name. 
 */
void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name)
{
    // Copying parameters to static variables because parameters are not accessible in debugger.
    static volatile uint8_t  s_file_name[128];
    static volatile uint16_t s_line_num;
    static volatile uint32_t s_error_code;    
        
    strcpy((char *)s_file_name, (const char *)p_file_name);
    s_line_num   = line_num;
    s_error_code = error_code;
    UNUSED_VARIABLE(s_file_name);
    UNUSED_VARIABLE(s_line_num);
    UNUSED_VARIABLE(s_error_code);    
    
    nrf_gpio_pin_set(ASSERT_LED_PIN_NO);

    (void) ble_error_log_write(DEAD_BEEF, p_file_name, line_num);

    for (;;)
    {
        // Loop forever. On assert, the system can only recover on reset.
    }
}
void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name)
{
	// disable INTs
    CRITICAL_REGION_ENTER();

    /* Light a LED on error or warning. */
    nrf_gpio_cfg_output(SER_CONN_ASSERT_LED_PIN);
    nrf_gpio_pin_set(SER_CONN_ASSERT_LED_PIN);

   	// m_p_error_file_name = p_file_name;
    // m_error_code = error_code;
    // m_error_line_num = line_num;

    /* Do not reset when warning. */
    if(SER_WARNING_CODE != error_code)
    {
        /* This call can be used for debug purposes during application development.
        * @note CAUTION: Activating code below will write the stack to flash on an error.
        * This function should NOT be used in a final product.
        * It is intended STRICTLY for development/debugging purposes.
        * The flash write will happen EVEN if the radio is active, thus interrupting any communication.
        * Use with care. Un-comment the line below to use. */

        /* ble_debug_assert_handler(error_code, line_num, p_file_name); */

#if 0
        /* Reset the chip. Should be used in the release version. */
        NVIC_SystemReset();
#else   /* Debug version. */
        /* To be able to see function parameters in a debugger. */
        uint32_t temp = 1;
        while(temp);
#endif
        CRITICAL_REGION_EXIT();

    }
}
static bool keymatrix_read(uint8_t *pressed_keys, uint8_t *number_of_pressed_keys)
{
    uint_fast8_t row_state[KEYBOARD_NUM_OF_COLUMNS];
    uint_fast8_t blocking_mask = 0;
    *number_of_pressed_keys = 0;

    for (uint_fast8_t column =  KEYBOARD_NUM_OF_COLUMNS; column--;){	//Loop through each column
		nrf_gpio_pin_set((uint32_t) column_pin_array[column]);
        if (((NRF_GPIO->IN)&input_scan_vector) != 0){					//If any input is high
			uint_fast8_t pin_state;
			row_state[column] = 0;
            uint_fast8_t detected_keypresses_on_column = 0;
            for (uint_fast8_t row = KEYBOARD_NUM_OF_ROWS; row--;){		//Loop through each row
				pin_state = nrf_gpio_pin_read((uint32_t) row_pin_array[row]);
				row_state[column] |= (pin_state << row);				//Record pin state
                if (pin_state){											//If pin is high
                    if (*number_of_pressed_keys < MAX_NUM_OF_PRESSED_KEYS){
                        *pressed_keys = matrix_lookup[row * KEYBOARD_NUM_OF_COLUMNS + column];
                        if(*pressed_keys != 0){							//Record the pressed key if it's not 0
							pressed_keys++;
							(*number_of_pressed_keys)++;
						}
                    }
                    detected_keypresses_on_column++;
                }
            }
            if (detected_keypresses_on_column > 1){
                if (blocking_mask & row_state[column]){					//If there is ghosting
                    return false;
                }
            }
            blocking_mask |= row_state[column];
        }
		nrf_gpio_pin_clear((uint32_t) column_pin_array[column]);
    }
    return true;
}
Example #10
0
uint32_t ble_error_log_read(ble_error_log_data_t * error_log)
{
    uint8_t  error_log_size = CEIL_DIV(sizeof(ble_error_log_data_t), sizeof(uint32_t));
    uint32_t err_code       = NRF_SUCCESS;
    
    err_code = ble_flash_page_read(FLASH_PAGE_ERROR_LOG, (uint32_t *) error_log, &error_log_size);
    
    // If nothing is in flash; then return NRF_SUCCESS.
    if (err_code == NRF_ERROR_NOT_FOUND)
    {
        return NRF_SUCCESS;
    }

    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }

    if (!error_log->failure)
    {
        return NRF_SUCCESS;
    }

    nrf_gpio_pin_set(LOG_LED_PIN_NO); // Notify that a message exists in the log.

    while (error_log->failure && !m_ble_log_clear_flag)
    {
        // Put breakpoint, and read data, then log->failure=false; to continue in debug mode.
        // In application, define how to clear the error log, 
        // e.g. read button 6, if pressed, then clear log and continue.
    }
    
    nrf_gpio_pin_clear(LOG_LED_PIN_NO);
    err_code = ble_flash_page_erase(FLASH_PAGE_ERROR_LOG);

    return err_code;
}
Example #11
0
/**
 * @brief Function for handling conversion values.
 *
 * @param[in] val                Value received from ADC or COMP.
 */
static void conversion_handler(uint16_t val)
{
    nrf_drv_csense_evt_t event_struct;

#if USE_COMP == 0
    nrf_gpio_pin_set(m_csense.output_pin);
#endif //USE_COMP

    m_csense.analog_values[m_csense.cur_chann_idx] = val;

    event_struct.read_value = val;
    event_struct.analog_channel = m_csense.cur_chann_idx;

    m_csense.channels_to_read &= ~(1UL<<m_csense.cur_chann_idx);

    // decide if there will be more conversions
    if(m_csense.channels_to_read == 0)
    {
        m_csense.busy = false;
#if USE_COMP == 0 && defined(SAADC_PRESENT)
        nrf_saadc_disable();
#endif
    }

    m_csense.event_handler(&event_struct);

    if(m_csense.channels_to_read > 0)     // Start new conversion.
    {
        ret_code_t err_code;
        calculate_next_channel();
        err_code = nrf_drv_csense_sample();
        if(err_code != NRF_SUCCESS)
        {
            return;
        }
    }
}
static void init(void){
	NRF_GPIO->PIN_CNF[HT_VCC_PIN] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
                                            | (GPIO_PIN_CNF_DRIVE_S0H1 << GPIO_PIN_CNF_DRIVE_Pos)
                                            | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
                                            | (GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos)
                                            | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
	nrf_gpio_pin_set(HT_VCC_PIN);
	
	my_twi_config.twi_ppi_ch = 1;
	my_twi_config.twi = NRF_TWI1;
	
	twi_init_config_t init_config;
	init_config.frequency = TWI_FREQ_400KHZ;
	init_config.twi_pinselect_scl =HT_TWI_SCL;
	init_config.twi_pinselect_sda = HT_TWI_SDA;
	init_config.twi_interrupt_no = SPI1_TWI1_IRQn;
	
	if(!twi_master_init(&init_config,&my_twi_config))
	{
			APP_ERROR_CHECK(666);
	}
	
	iss_struct_h.p_update_samp_freq = update_measurement_samp_freq_h;
	iss_struct_h.coord = HT_COORDINATE;
	iss_struct_h.type = BLE_UUID_ITU_SENSOR_TYPE_HUMIDITY;
	iss_struct_h.make = BLE_UUID_ITU_SENSOR_MAKE_SI7021;
	iss_struct_h.IEEE_exponent = -1;
	iss_update_config(&iss_struct_h);
	
	iss_struct_t.p_update_samp_freq = update_measurement_samp_freq_h;
	iss_struct_t.coord = HT_COORDINATE;
	iss_struct_t.type = BLE_UUID_ITU_SENSOR_TYPE_TEMPERATURE;
	iss_struct_t.make = BLE_UUID_ITU_SENSOR_MAKE_SI7021;
	iss_struct_t.IEEE_exponent = -1;
	iss_update_config(&iss_struct_t);
}
Example #13
0
/**@brief Start advertising.
 */
static void advertising_start(void)
{
    uint32_t err_code;

    if (m_advertising_mode == BLE_NO_ADVERTISING)
    {
        m_advertising_mode = BLE_FAST_ADVERTISING;
    }
    else
    {
        m_advertising_mode = BLE_SLOW_ADVERTISING;
    }

    // Initialize advertising parameters (used when starting advertising)
    memset(&m_adv_params, 0, sizeof(m_adv_params));
    
    m_adv_params.type        = BLE_GAP_ADV_TYPE_ADV_IND;
    m_adv_params.p_peer_addr = NULL;                           // Undirected advertisement
    m_adv_params.fp          = BLE_GAP_ADV_FP_ANY;

    if (m_advertising_mode == BLE_FAST_ADVERTISING)
    {
        m_adv_params.interval = APP_ADV_INTERVAL;
        m_adv_params.timeout  = ADV_INTERVAL_FAST_PERIOD;
    }
    else
    {
        m_adv_params.interval = APP_ADV_INTERVAL_SLOW;
        m_adv_params.timeout  = APP_ADV_TIMEOUT_IN_SECONDS;
    }

    err_code = sd_ble_gap_adv_start(&m_adv_params);
    APP_ERROR_CHECK(err_code);
    
    nrf_gpio_pin_set(ADVERTISING_LED_PIN_NO);
}
Example #14
0
/**
 * @brief Function for application main entry.
 */
int main(void)
{
    nrf_gpio_range_cfg_output(LED_0, LED_1);
    nrf_gpio_pin_set(LED_0);
    
    NRF_CLOCK->TASKS_HFCLKSTART = 1;
    while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0)
    {
    }
    NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
    
    // Workaround for PAN item 11, nRF51822-PAN v2.0.pdf. Not needed on second revision chips. 
    NRF_POWER->TASKS_CONSTLAT = 1;
    
    NRF_GPIOTE->CONFIG[0] = GPIOTE_CONFIG_MODE_Task << GPIOTE_CONFIG_MODE_Pos |
                            GPIOTE_CONFIG_POLARITY_Toggle << GPIOTE_CONFIG_POLARITY_Pos |
                            LED_1 << GPIOTE_CONFIG_PSEL_Pos | 
                            GPIOTE_CONFIG_OUTINIT_Low << GPIOTE_CONFIG_OUTINIT_Pos;
                            
    NRF_TIMER1->PRESCALER = 0;
    // Adjust the output frequency by adjusting the CC. 
    // Due to PAN item 33, you can't have this at 1 for first revision chips, and will hence be limited to 4 MHz. 
    NRF_TIMER1->CC[0] = 1;
    NRF_TIMER1->SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Enabled << TIMER_SHORTS_COMPARE0_CLEAR_Pos;
    NRF_TIMER1->TASKS_START = 1;
    
    NRF_PPI->CH[0].EEP = (uint32_t) &NRF_TIMER1->EVENTS_COMPARE[0];
    NRF_PPI->CH[0].TEP = (uint32_t) &NRF_GPIOTE->TASKS_OUT[0];
    
    NRF_PPI->CHENSET = PPI_CHENSET_CH0_Enabled << PPI_CHENSET_CH0_Pos;
    
    while (true)
    {
        
    }
}
Example #15
0
/**@brief IAS Client event handler.
 *
 * @details This function will be called for all IAS Client events which are passed to the
 *          application.
 *
 * @param[in]   p_ias_c  IAS Client stucture.
 * @param[in]   p_evt    Event received.
 */
static void on_ias_c_evt(ble_ias_c_t * p_ias_c, ble_ias_c_evt_t * p_evt)
{
    uint32_t err_code = NRF_SUCCESS;

    switch (p_evt->evt_type)
    {
        case BLE_IAS_C_EVT_SRV_DISCOVERED:
            nrf_gpio_pin_set(PEER_SRV_DISC_LED_PIN_NO);
            break;

        case BLE_IAS_C_EVT_SRV_NOT_FOUND:
            // IAS is not found on peer.
            break;

        case BLE_IAS_C_EVT_DISCONN_COMPLETE:
            nrf_gpio_pin_clear(PEER_SRV_DISC_LED_PIN_NO);
            break;

        default:
            break;
    }

    APP_ERROR_CHECK(err_code);
}
Example #16
0
File: main.c Project: tkadom/TWBLE
/**
* @brief Initialize GPIO pins, for LEDs and debugging
*/
void gpio_init(void)
{
#ifdef BOARD_PCA10028
    nrf_gpio_cfg_output(LED_1);
    nrf_gpio_cfg_output(LED_2);

  #ifdef BUTTONS
    nrf_gpio_cfg_input(BUTTON_1, NRF_GPIO_PIN_PULLUP);
    nrf_gpio_cfg_input(BUTTON_2, NRF_GPIO_PIN_PULLUP);
    nrf_gpio_cfg_input(BUTTON_3, NRF_GPIO_PIN_PULLUP);
    nrf_gpio_cfg_input(BUTTON_4, NRF_GPIO_PIN_PULLUP);
  #endif
#endif
#ifdef BOARD_PCA10031
    nrf_gpio_cfg_output(LED_RGB_RED);
    nrf_gpio_cfg_output(LED_RGB_GREEN);
    nrf_gpio_cfg_output(LED_RGB_BLUE);

    nrf_gpio_pin_set(LED_RGB_RED);
    nrf_gpio_pin_set(LED_RGB_GREEN);
    nrf_gpio_pin_set(LED_RGB_BLUE);
#endif

#ifdef BOARD_PCA10000
    nrf_gpio_cfg_output(LED_RGB_RED);
    nrf_gpio_cfg_output(LED_RGB_GREEN);
    nrf_gpio_cfg_output(LED_RGB_BLUE);

    nrf_gpio_pin_set(LED_RGB_RED);
    nrf_gpio_pin_set(LED_RGB_GREEN);
    nrf_gpio_pin_set(LED_RGB_BLUE);
#endif

#ifdef BOARD_PCA10001 
    nrf_gpio_range_cfg_output(0, 32);
#endif

    led_config(1, 0);
    led_config(2, 0);
}
Example #17
0
static void updateDisplay()
{
//	int i;
	// should first byte be 3 or 1? I had 1, Arduino code has 3.
	uint8_t txData1[15] = { 0x3, 0x1, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0x00 };
  const uint8_t txData2[2] = { 0, 0 };
	uint8_t rxData[14];

	// wait 30 us, why?
	nrf_delay_us( 30 );

// inhibit VCOM toggle?
	VCOM_inhibit = 1;
	
	for( int i = 1; i <96; i++ )
  {
		txData1[1] = i;
		
		// copy data from screen dump to tx buffer
		
		memcpy( &(txData1[2]), &(image[ (i-1) * 12 ]), 12 );
		for( int j = 2; j < 14; j++ )
			txData1[ j ] = txData1[ j ] ^ 0xff; // invert with xor
		
		spiMaster_txRx( &spiMaster, &screenSPIslave, 15, txData1, rxData );
		nrf_delay_us( 2 );
  }
	spiMaster_txRx( &spiMaster, &screenSPIslave, 2, txData2, rxData );
	nrf_delay_us( 2 );

#if 0
	// turn on chip select
	nrf_gpio_pin_set( PIN_DISP_CS );

	// wait 6 us (tsSCS)
	nrf_delay_us( 6 );
	
	// power up SPI peripheral
	NRF_SPI0->ENABLE = 1;
	
	// inhibit VCOM toggle?
	VCOM_inhibit = 1;
	
	// send data
	NRF_SPI0->TXD = 0x1; // enter dynamic mode
	NRF_SPI0->TXD = 0x1; // double buffering, can send next byte immediately. 1 = line 1

	for( i = 0; i < 15; i++ ) // 96 bits data + 16 bits don't care
  {	
		while( NRF_SPI0->EVENTS_READY == 0 )
			;
		NRF_SPI0->EVENTS_READY = 0;
		NRF_SPI0->TXD = 0xAA;
  }	
	// wait 2 us (thSCS)
	nrf_delay_us( 2 );

	// turn off chip select
	nrf_gpio_pin_clear( PIN_DISP_CS );

	// wait twSCSL = 2 us
	nrf_delay_us( 2 );
	
	// turn on chip select
	nrf_gpio_pin_set( PIN_DISP_CS );

	// wait 6 us (tsSCS)
	nrf_delay_us( 6 );

	// send data
	NRF_SPI0->TXD = 0x0; // enter static mode
	NRF_SPI0->TXD = 0x0; // double buffering, can send next byte immediately. 1 = line 1

	while( NRF_SPI0->EVENTS_READY == 0 )
		;
	NRF_SPI0->EVENTS_READY = 0;
	
	// wait twSCSL = 2 us
	nrf_delay_us( 2 );

	// turn off chip select
	nrf_gpio_pin_clear( PIN_DISP_CS );
#endif	
	// enable VCOM inversion
	VCOM_inhibit = 0;

	// power down SPI peripheral
//	NRF_SPI0->ENABLE = 0;
}
Example #18
0
/**@brief Function for bootloader main entry.
 */
int main(void)
{
    uint32_t err_code;
    bool     dfu_start = false;
    bool     app_reset = (NRF_POWER->GPREGRET == BOOTLOADER_DFU_START);

    leds_init();

    // This check ensures that the defined fields in the bootloader corresponds with actual
    // setting in the nRF51 chip.
    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();
    (void)bootloader_init();

    if (bootloader_dfu_sd_in_progress())
    {
        err_code = bootloader_dfu_sd_update_continue();
        APP_ERROR_CHECK(err_code);

        ble_stack_init(!app_reset);
        scheduler_init();

        err_code = bootloader_dfu_sd_update_finalize();
        APP_ERROR_CHECK(err_code);
    }
    else
    {
        // If stack is present then continue initialization of bootloader.
        ble_stack_init(!app_reset);
        scheduler_init();
    }

    dfu_start  = app_reset;
   // dfu_start |= ((nrf_gpio_pin_read(BOOTLOADER_BUTTON_PIN) == 0) ? true: false);
    
    if (dfu_start || (!bootloader_app_is_valid(DFU_BANK_0_REGION_START)))
    {
        err_code = sd_power_gpregret_clr(POWER_GPREGRET_GPREGRET_Msk);
        APP_ERROR_CHECK(err_code);

        nrf_gpio_pin_set(LED_0);

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

        nrf_gpio_pin_clear(LED_0);
    }

    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);
    
    NVIC_SystemReset();
}
/**@brief Function for handling the Application's BLE Stack events.
 *
 * @details This function is responsible for managing possible quick connect->write->disconnect sequence.
 *          It to write characteristic and switch the device in scan mode and getting advertising packets.
 *
 * @param[in]   p_ble_evt   Bluetooth stack event.
 */
static void on_ble_evt(ble_evt_t * p_ble_evt)
{
    uint32_t              err_code;
    const ble_gap_evt_t * p_gap_evt = &p_ble_evt->evt.gap_evt;

    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GATTC_EVT_WRITE_RSP:
            mb_send_write_rsp();
            err_code = bt_disconnect(p_ble_evt->evt.gap_evt.conn_handle);

            if (err_code != NRF_SUCCESS)
            {
                bt_scan_start();
                mb_set_ready_for_rq();
            }
            break;

        case BLE_GAP_EVT_CONNECTED:
            nrf_gpio_pin_set(LED_CONNECTED);
            err_code = bt_write_to_device(p_ble_evt->evt.gap_evt.conn_handle);

            if (err_code != NRF_SUCCESS)
            {
                err_code = bt_disconnect(p_ble_evt->evt.gap_evt.conn_handle);

                if (err_code != NRF_SUCCESS)
                {
                    bt_scan_start();
                    mb_set_ready_for_rq();
                }
            }
            break;

        case BLE_GAP_EVT_DISCONNECTED:
            nrf_gpio_pin_clear(LED_CONNECTED);
            bt_scan_start();
            break;

        case BLE_GAP_EVT_ADV_REPORT:
        {
            uint8_array_t  adv_data;
            ble_gap_addr_t address;

            // Initialize advertisement report for parsing.
            adv_data.p_data = (uint8_t *)(p_gap_evt->params.adv_report.data);
            adv_data.size   = p_gap_evt->params.adv_report.dlen;
            address         = p_gap_evt->params.adv_report.peer_addr;

            bt_handle_temp_sensor(&address, &adv_data);
            bt_handle_led_driver(&address, &adv_data);
            break;
        }

        case BLE_GAP_EVT_TIMEOUT:
            nrf_gpio_pin_clear(LED_CONNECTED);
            bt_scan_start();
            mb_set_ready_for_rq();
            break;

        case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST:
            // Accepting parameters requested by peer.
            err_code = sd_ble_gap_conn_param_update(p_gap_evt->conn_handle,
                                                    &p_gap_evt->params.conn_param_update_request.conn_params);
            APP_ERROR_CHECK(err_code);
            break;

        default:
            break;
    }
}
Example #20
0
int main()
{
    static char address[5];

    sd_mbr_command(&startSdCmd);
    sd_softdevice_vector_table_base_set(BOOTLOADER_ADDRESS);

    // If the master boot switch has detected short or no click: boot the firmware
    if (((NRF_POWER->GPREGRET&0x86U) != 0x82U) &&
            ((NRF_POWER->GPREGRET&0x40U) != 0x40U) &&
            (*(uint32_t *)FW_ADDRESS != 0xFFFFFFFFU) ) {
        start_firmware();
    }

    if (NRF_POWER->GPREGRET&0x40U) {
        address[4] = 0xb1;
        memcpy(&address[0], (char*)&NRF_FICR->DEVICEADDR[0], 4);
        esbSetAddress(address);
    }

    NRF_POWER->GPREGRET &= ~(0x60U);

    // Enable the radio LNA
    nrf_gpio_cfg_output(RADIO_PAEN_PIN);
    nrf_gpio_pin_set(RADIO_PAEN_PIN);

    // Initialize timer module.
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, false);

    ble_init();
    /*
      NRF_CLOCK->LFCLKSRC = CLOCK_LFCLKSTAT_SRC_Synth;

      NRF_CLOCK->TASKS_LFCLKSTART = 1UL;
      while(!NRF_CLOCK->EVENTS_LFCLKSTARTED);
    */
    systickInit();
    buttonInit(buttonIdle);

#ifndef DEBUG_TIMESLOT
    //sd_ppi_channel_assign(0, &(NRF_TIMER1->EVENTS_COMPARE[0]), &(NRF_GPIOTE->TASKS_OUT[0]));
    //sd_ppi_channel_enable_set(PPI_CHEN_CH0_Msk);

    //NRF_PPI->CH[0].EEP = &(NRF_TIMER1->EVENTS_COMPARE[0]);
    //NRF_PPI->CH[0].TEP = &(NRF_GPIOTE->TASKS_OUT[0]);
    //NRF_PPI->CHENSET = 1;
#endif

    // Start (or continue) to blink  the LED at 0.5Hz
    //NRF_TIMER1->TASKS_STOP = 1;

    //NRF_TIMER1->MODE      = TIMER_MODE_MODE_Timer;
    //NRF_TIMER1->PRESCALER = 7;
    //NRF_TIMER1->BITMODE = TIMER_BITMODE_BITMODE_16Bit << TIMER_BITMODE_BITMODE_Pos;
    //NRF_TIMER1->SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Msk; // | TIMER_SHORTS_COMPARE1_CLEAR_Msk;

    //NRF_TIMER1->TASKS_CLEAR = 1;

    NRF_TIMER1->CC[0] = 1*SEC; //0x1E84 ;
    NRF_TIMER1->CC[1] = 2*SEC;


    nrf_gpio_cfg_output(LED_PIN);

    nrf_gpiote_task_config(0,
                           LED_PIN,
                           NRF_GPIOTE_POLARITY_TOGGLE,
                           NRF_GPIOTE_INITIAL_VALUE_LOW);
    NRF_TIMER1->TASKS_START = 1;


    // Enable 500mA USB input and enable battery charging
    nrf_gpio_cfg_output(PM_EN1);
    nrf_gpio_pin_set(PM_EN1);
    nrf_gpio_cfg_output(PM_EN2);
    nrf_gpio_pin_clear(PM_EN2);
    nrf_gpio_cfg_output(PM_CHG_EN);
    nrf_gpio_pin_clear(PM_CHG_EN);


    // Power STM32, hold reset
    nrf_gpio_cfg_output(PM_VCCEN_PIN);
    nrf_gpio_pin_set(PM_VCCEN_PIN);
    nrf_gpio_cfg_output(STM_NRST_PIN);
    nrf_gpio_pin_clear(STM_NRST_PIN);

    // Set flow control and activate pull-down on RX data pin
    nrf_gpio_cfg_output(UART_TX_PIN);
    nrf_gpio_pin_set(UART_TX_PIN);
    nrf_gpio_cfg_output(UART_RTS_PIN);
    nrf_gpio_pin_set(UART_RTS_PIN);
    nrf_gpio_cfg_input(UART_RX_PIN, NRF_GPIO_PIN_PULLDOWN);


    nrf_gpio_pin_set(STM_NRST_PIN);

    //systickInit();
    //syslinkInit();
    //buttonInit();

//  nrf_gpio_cfg_input(BUTTON_PIN, NRF_GPIO_PIN_PULLUP);

    mainLoop();

    while(1);
}
Example #21
0
/**@brief Function for initialization of LEDs.
 */
static void leds_init(void)
{
    nrf_gpio_cfg_output(UPDATE_IN_PROGRESS_LED);
    nrf_gpio_pin_set(UPDATE_IN_PROGRESS_LED);
}
Example #22
0
uint32_t app_uart_init(const app_uart_comm_params_t * p_comm_params,
                       app_uart_buffers_t           * p_buffers,
                       app_uart_event_handler_t       event_handler,
                       app_irq_priority_t             irq_priority,
                       uint16_t                     * p_app_uart_uid)
{
    uint32_t err_code;

    m_current_state = UART_OFF;
    m_event_handler = event_handler;
    m_rx_byte       = BYTE_INVALID;


    // Configure RX and TX pins.
    nrf_gpio_pin_set(p_comm_params->tx_pin_no);
    nrf_gpio_cfg_output(p_comm_params->tx_pin_no);
    nrf_gpio_cfg_input(p_comm_params->rx_pin_no, NRF_GPIO_PIN_PULLUP);


    NRF_UART0->PSELTXD = p_comm_params->tx_pin_no;
    NRF_UART0->PSELRXD = p_comm_params->rx_pin_no;

    // Configure baud rate and parity.
    NRF_UART0->BAUDRATE = (p_comm_params->baud_rate << UART_BAUDRATE_BAUDRATE_Pos);

    if (p_comm_params->use_parity)
    {
        NRF_UART0->CONFIG = (UART_CONFIG_PARITY_Included << UART_CONFIG_PARITY_Pos);
    }
    else
    {
        NRF_UART0->CONFIG = (UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos);
    }

    if (p_comm_params->flow_control == APP_UART_FLOW_CONTROL_LOW_POWER)
    {
        if (!nrf_drv_gpiote_is_init())
        {
            err_code = nrf_drv_gpiote_init();
            if (err_code != NRF_SUCCESS)
            {
                return err_code;
            }
        }

        // Configure hardware flow control.
        nrf_drv_gpiote_out_config_t rts_config = GPIOTE_CONFIG_OUT_SIMPLE(true);
        err_code = nrf_drv_gpiote_out_init(p_comm_params->rts_pin_no, &rts_config);
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }

        NRF_UART0->PSELCTS = UART_PIN_DISCONNECTED;
        NRF_UART0->PSELRTS = p_comm_params->rts_pin_no;
        NRF_UART0->CONFIG |= (UART_CONFIG_HWFC_Enabled << UART_CONFIG_HWFC_Pos);

        // Setup the gpiote to handle pin events on cts-pin.
        // For the UART we want to detect both low->high and high->low transitions in order to
        // know when to activate/de-activate the TX/RX in the UART.
        // Configure pin.
        nrf_drv_gpiote_in_config_t cts_config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(false);
        err_code = nrf_drv_gpiote_in_init(p_comm_params->cts_pin_no, &cts_config, gpiote_uart_event_handler);
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }

        nrf_drv_gpiote_in_event_enable(p_comm_params->cts_pin_no, true);

        // UART CTS pin is active when low.
        if (nrf_drv_gpiote_in_is_set(p_comm_params->cts_pin_no))
        {
            on_uart_event(ON_CTS_HIGH);
        }
        else
        {
            on_uart_event(ON_CTS_LOW);
        }
    }
    else if (p_comm_params->flow_control == APP_UART_FLOW_CONTROL_ENABLED)
    {
        uart_standard_flow_control_init(p_comm_params);
        m_current_state = UART_READY;
    }
    else
    {
        uart_no_flow_control_init();
        m_current_state = UART_READY;
    }
    if (*p_app_uart_uid == UART_INSTANCE_ID_INVALID)
    {
        *p_app_uart_uid = m_instance_counter++;
    }

    // Enable UART interrupt
    NRF_UART0->INTENCLR = 0xffffffffUL;
    NRF_UART0->INTENSET = (UART_INTENSET_RXDRDY_Set << UART_INTENSET_RXDRDY_Pos) |
                          (UART_INTENSET_TXDRDY_Set << UART_INTENSET_TXDRDY_Pos) |
                          (UART_INTENSET_ERROR_Set << UART_INTENSET_ERROR_Pos);

    NVIC_ClearPendingIRQ(UART_IRQ);
    NVIC_SetPriority(UART_IRQ, irq_priority);
    NVIC_EnableIRQ(UART_IRQ);

    return NRF_SUCCESS;
}
Example #23
0
 */
static void on_ble_evt(ble_evt_t * p_ble_evt)
{
    uint32_t        err_code;
    static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID;

    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);
            nrf_gpio_pin_clear(ADV_INTERVAL_SLOW_LED_PIN_NO);
            nrf_gpio_pin_clear(ADV_WHITELIST_LED_PIN_NO);
            
            // Start handling button presses
            err_code = app_button_enable();
            APP_ERROR_CHECK(err_code);

            m_advertising_mode = BLE_NO_ADV;
            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);

            if (!m_is_link_loss_alerting)
            {
                nrf_gpio_pin_clear(ALERT_LEVEL_MILD_LED_PIN_NO);
                nrf_gpio_pin_clear(ALERT_LEVEL_HIGH_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);

            // Stop detecting button presses when not connected.
            err_code = app_button_disable();
            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)
            {
                if (m_advertising_mode == BLE_SLEEP)
                {
                    m_advertising_mode = BLE_NO_ADV;

                    nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO);
                    nrf_gpio_pin_clear(ADV_WHITELIST_LED_PIN_NO);
                    nrf_gpio_pin_clear(ADV_INTERVAL_SLOW_LED_PIN_NO);

                    // Configure buttons with sense level low as wakeup source.
                    nrf_gpio_cfg_sense_input(SIGNAL_ALERT_BUTTON,
                                             BUTTON_PULL,
                                             NRF_GPIO_PIN_SENSE_LOW);
                    
                    nrf_gpio_cfg_sense_input(BONDMNGR_DELETE_BUTTON_PIN_NO,
                                             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);
                }
                else
                {
                    advertising_start();
                }
            }
            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(m_conn_handle, 
                                             BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
            APP_ERROR_CHECK(err_code);
            break;

        default:
            // No implementation needed.
            break;
    }
Example #24
0
/**@brief Function for starting advertising.
 */
static void advertising_start(void)
{
    uint32_t             err_code;
    ble_gap_adv_params_t adv_params;
    ble_gap_whitelist_t  whitelist;

    // Clear all advertising LEDs
    nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO);
    nrf_gpio_pin_clear(ADV_WHITELIST_LED_PIN_NO);
    nrf_gpio_pin_clear(ADV_INTERVAL_SLOW_LED_PIN_NO);

    // Initialize advertising parameters with defaults values
    memset(&adv_params, 0, sizeof(adv_params));

    adv_params.type        = BLE_GAP_ADV_TYPE_ADV_IND;
    adv_params.p_peer_addr = NULL;
    adv_params.fp          = BLE_GAP_ADV_FP_ANY;
    adv_params.p_whitelist = NULL;

    // Configure advertisement according to current advertising state
    switch (m_advertising_mode)
    {
        case BLE_NO_ADV:
            m_advertising_mode = BLE_FAST_ADV_WHITELIST;
            // fall through.

        case BLE_FAST_ADV_WHITELIST:
            err_code = ble_bondmngr_whitelist_get(&whitelist);
            APP_ERROR_CHECK(err_code);

            if ((whitelist.addr_count != 0) || (whitelist.irk_count != 0))
            {
                adv_params.fp          = BLE_GAP_ADV_FP_FILTER_CONNREQ;
                adv_params.p_whitelist = &whitelist;

                advertising_init(BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED);
                m_advertising_mode = BLE_FAST_ADV;
                nrf_gpio_pin_set(ADV_WHITELIST_LED_PIN_NO);
            }
            else
            {
                m_advertising_mode = BLE_SLOW_ADV;
            }

            adv_params.interval = APP_ADV_INTERVAL_FAST;
            adv_params.timeout  = APP_FAST_ADV_WHITELIST_TIMEOUT;
            break;

        case BLE_FAST_ADV:
            advertising_init(BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE);

            adv_params.interval = APP_ADV_INTERVAL_FAST;
            adv_params.timeout  = APP_FAST_ADV_TIMEOUT;
            m_advertising_mode  = BLE_SLOW_ADV;
            break;

        case BLE_SLOW_ADV:
            advertising_init(BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE);

            adv_params.interval = APP_ADV_INTERVAL_SLOW;
            adv_params.timeout  = APP_SLOW_ADV_TIMEOUT;
            m_advertising_mode  = BLE_SLEEP;

            nrf_gpio_pin_set(ADV_INTERVAL_SLOW_LED_PIN_NO);
            break;

        default:
            // No implementation needed.
            break;
    }

    // Start advertising.
    err_code = sd_ble_gap_adv_start(&adv_params);
    APP_ERROR_CHECK(err_code);

    nrf_gpio_pin_set(ADVERTISING_LED_PIN_NO);
}
Example #25
0
// When timer time happens, toggle GPIO to raise interrupt
static void intermcu_timeout_handler(void * p_context)
{
	if (intermcu_init_state < IO_CONNECTED) {
		if (intermcu_init_state == IO_INVALID) {
			if (nrf_gpio_pin_read(RT5350_TO_NRF51_MAIL_IO) == 0) {
				intermcu_init_state = IO_INIT;
			}
		} else if (intermcu_init_state == IO_INIT) {
			if (nrf_gpio_pin_read(RT5350_TO_NRF51_MAIL_IO) == 1) {
				intermcu_init_state = IO_UP;
				init_state_cnt = 0;
			}
		} else if (intermcu_init_state == IO_UP) {
			if (nrf_gpio_pin_read(RT5350_TO_NRF51_MAIL_IO) == 1) {
				init_state_cnt++;
			} else {
				if ((init_state_cnt >= 20) && (init_state_cnt <= 40)) {
					intermcu_init_state = IO_CONNECTED;
					init_state_cnt = 0;
					rino_init();
				} else {
					intermcu_init_state = IO_INIT;
				}
				init_state_cnt = 0;
			}
		} else {
			intermcu_init_state = IO_INVALID;
		}
	} else {
		// Handle commands from RT5350
		if (intermcu_init_state == IO_CONNECTED) {
			if (nrf_gpio_pin_read(RT5350_TO_NRF51_MAIL_IO) == 1) {
				// Prepare SPI transaction
				intermcu_spi_transaction(m_tx_buf, 2);

				// Setup flag to tell RT5350 we are ready!
				nrf_gpio_pin_set(NRF51_TO_RT5350_MAIL_IO);

				intermcu_init_state = IO_HEADER_IN_PROGRESS;
			}
		} else if (intermcu_init_state == IO_HEADER_RECEIVED) {
			nrf_gpio_pin_clear(NRF51_TO_RT5350_MAIL_IO);
			magic_header.type = m_rx_buf[0];
			magic_header.len = m_rx_buf[1];
			intermcu_init_state = IO_WAIT_FOR_PAYLOAD;
		} else if (intermcu_init_state == IO_WAIT_FOR_PAYLOAD) {
			if (nrf_gpio_pin_read(RT5350_TO_NRF51_MAIL_IO) == 1) {
				// Prepare SPI transaction
				intermcu_spi_transaction(m_tx_buf, magic_header.len);

				// Setup flag to tell RT5350 we are ready!
				nrf_gpio_pin_set(NRF51_TO_RT5350_MAIL_IO);

				intermcu_init_state = IO_PAYLOAD_IN_PROGRESS;
			}
		} else if (intermcu_init_state == IO_PAYLOAD_RECEIVED) {
	        if (intermcu_recv_cb != NULL) {
	            (*intermcu_recv_cb)(magic_header.type, magic_header.len, m_rx_buf);
	        }
			nrf_gpio_pin_clear(NRF51_TO_RT5350_MAIL_IO);
			intermcu_init_state = IO_WAIT_PEER_IDLE;
		} else if (intermcu_init_state == IO_WAIT_PEER_IDLE) {
			if (nrf_gpio_pin_read(RT5350_TO_NRF51_MAIL_IO) == 0) {
				intermcu_init_state = IO_CONNECTED;
			}
		}
	}
}
Example #26
0
/**@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;
    static ble_gap_evt_auth_status_t m_auth_status;
    ble_gap_enc_info_t *             p_enc_info;
    
    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_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_SYS_ATTR_MISSING:
            err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0);
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GAP_EVT_AUTH_STATUS:
            m_auth_status = p_ble_evt->evt.gap_evt.params.auth_status;
            break;
            
        case BLE_GAP_EVT_SEC_INFO_REQUEST:
            p_enc_info = &m_auth_status.periph_keys.enc_info;
            if (p_enc_info->div == p_ble_evt->evt.gap_evt.params.sec_info_request.div)
            {
                err_code = sd_ble_gap_sec_info_reply(m_conn_handle, p_enc_info, NULL);
                APP_ERROR_CHECK(err_code);
            }
            else
            {
                // No keys found for this device
                err_code = sd_ble_gap_sec_info_reply(m_conn_handle, NULL, NULL);
                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;
        case BLE_EVT_TX_COMPLETE:
            if(!ble_buffer_available) tx_complete = true;
            break;

        default:
            // No implementation needed.
            break;
    }
}
Example #27
0
uint32_t* spi_master_init(SPIModuleNumber module_number, SPIMode mode, bool lsb_first)
{
    uint32_t config_mode;

    NRF_SPI_Type *spi_base_address = (SPI0 == module_number)? NRF_SPI0 : (NRF_SPI_Type *)NRF_SPI1;

    if(SPI0 == module_number)
    {
        /* Configure GPIO pins used for pselsck, pselmosi, pselmiso and pselss for SPI0 */
        nrf_gpio_cfg_output(SPI_PSELSCK0);
        nrf_gpio_cfg_output(SPI_PSELMOSI0);
        nrf_gpio_cfg_input(SPI_PSELMISO0, NRF_GPIO_PIN_NOPULL);
        nrf_gpio_cfg_output(SPI_PSELSS0);

        /* Configure pins, frequency and mode */
        spi_base_address->PSELSCK  = SPI_PSELSCK0;
        spi_base_address->PSELMOSI = SPI_PSELMOSI0;
        spi_base_address->PSELMISO = SPI_PSELMISO0;
        nrf_gpio_pin_set(SPI_PSELSS0); /* disable Set slave select (inactive high) */
    }
    else
    {
        /* Configure GPIO pins used for pselsck, pselmosi, pselmiso and pselss for SPI1*/
        nrf_gpio_cfg_output(SPI_PSELSCK1);
        nrf_gpio_cfg_output(SPI_PSELMOSI1);
        nrf_gpio_cfg_input(SPI_PSELMISO1, NRF_GPIO_PIN_NOPULL);
        nrf_gpio_cfg_output(SPI_PSELSS1);

        /* Configure pins, frequency and mode */
        spi_base_address->PSELSCK  = SPI_PSELSCK1;
        spi_base_address->PSELMOSI = SPI_PSELMOSI1;
        spi_base_address->PSELMISO = SPI_PSELMISO1;
        nrf_gpio_pin_set(SPI_PSELSS1);         /* disable Set slave select (inactive high) */
    }

    spi_base_address->FREQUENCY = (uint32_t) SPI_OPERATING_FREQUENCY;

    /*lint -e845 -save // A zero has been given as right argument to operator '!'" */
    /** @snippet [SPI Select mode] */
    switch (mode )
    {
       
        case SPI_MODE0:
            config_mode = (SPI_CONFIG_CPHA_Leading << SPI_CONFIG_CPHA_Pos) | (SPI_CONFIG_CPOL_ActiveHigh << SPI_CONFIG_CPOL_Pos);
            break;
        case SPI_MODE1:
            config_mode = (SPI_CONFIG_CPHA_Trailing << SPI_CONFIG_CPHA_Pos) | (SPI_CONFIG_CPOL_ActiveHigh << SPI_CONFIG_CPOL_Pos);
            break;
        case SPI_MODE2:
            config_mode = (SPI_CONFIG_CPHA_Leading << SPI_CONFIG_CPHA_Pos) | (SPI_CONFIG_CPOL_ActiveLow << SPI_CONFIG_CPOL_Pos);
            break;
        case SPI_MODE3:
            config_mode = (SPI_CONFIG_CPHA_Trailing << SPI_CONFIG_CPHA_Pos) | (SPI_CONFIG_CPOL_ActiveLow << SPI_CONFIG_CPOL_Pos);
            break;
        default:
            config_mode = 0;
            break;
    
    }
    /** @snippet [SPI Select mode] */
    /*lint -restore */

    /*lint -e845 -save // A zero has been given as right argument to operator '!'" */
    /** @snippet [SPI Select endianess] */
    if (lsb_first)
    {
        spi_base_address->CONFIG = (config_mode | (SPI_CONFIG_ORDER_LsbFirst << SPI_CONFIG_ORDER_Pos));
    }
    else
    {
        spi_base_address->CONFIG = (config_mode | (SPI_CONFIG_ORDER_MsbFirst << SPI_CONFIG_ORDER_Pos));
    }
    /** @snippet [SPI Select endianess] */
    /*lint -restore */

    spi_base_address->EVENTS_READY = 0U;

    /* Enable */
    spi_base_address->ENABLE = (SPI_ENABLE_ENABLE_Enabled << SPI_ENABLE_ENABLE_Pos);

    return (uint32_t *)spi_base_address;
}
Example #28
0
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
}
Example #29
0
/**@brief Application's BLE Stack 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);
            // Start detecting button presses
            err_code = app_button_enable();
            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;
            m_bps_meas_ind_conf_pending = false;

            // Stop detecting button presses when not connected
            err_code = app_button_disable();
            APP_ERROR_CHECK(err_code);

            // Since we are not in a connection and have not started advertising, store bonds
            err_code = ble_bondmngr_bonded_masters_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);
            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)
                GPIO_WAKEUP_BUTTON_CONFIG(SEND_MEAS_BUTTON_PIN_NO);
                err_code = sd_power_system_off();    
            }
            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);
            }
            break;

        default:
            break;
    }

    APP_ERROR_CHECK(err_code);
}
Example #30
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;
    static ble_gap_evt_auth_status_t m_auth_status;
    ble_gap_enc_info_t *             p_enc_info;

    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;

            /* YOUR_JOB: Uncomment this part if you are using the app_button module to handle button
                         events (assuming that the button events are only needed in connected
                         state). If this is uncommented out here,
                            1. Make sure that app_button_disable() is called when handling
                               BLE_GAP_EVT_DISCONNECTED below.
                            2. Make sure the app_button module is initialized.
            err_code = app_button_enable();
            APP_ERROR_CHECK(err_code);
            */
            break;

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

            /* YOUR_JOB: Uncomment this part if you are using the app_button module to handle button
                         events. This should be done to save power when not connected
                         to a peer.
            err_code = app_button_disable();
            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_GATTS_EVT_SYS_ATTR_MISSING:
            err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0);
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GAP_EVT_AUTH_STATUS:
            m_auth_status = p_ble_evt->evt.gap_evt.params.auth_status;
            break;

        case BLE_GAP_EVT_SEC_INFO_REQUEST:
            p_enc_info = &m_auth_status.periph_keys.enc_info;
            if (p_enc_info->div == p_ble_evt->evt.gap_evt.params.sec_info_request.div)
            {
                err_code = sd_ble_gap_sec_info_reply(m_conn_handle, p_enc_info, NULL);
                APP_ERROR_CHECK(err_code);
            }
            else
            {
                // No keys found for this device
                err_code = sd_ble_gap_sec_info_reply(m_conn_handle, NULL, NULL);
                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;
    }
}