Example #1
0
File: main.c Project: tve/Espruino
/**@brief Function for initialization of LEDs.
 */
static void leds_init(void)
{
    nrf_gpio_cfg_output(UPDATE_IN_PROGRESS_LED);
    nrf_gpio_pin_write(UPDATE_IN_PROGRESS_LED, !UPDATE_IN_PROGRESS_LED_ONSTATE);
    nrf_gpio_cfg_output(BOOTLOADER_BUTTON_PRESS_LED);
    nrf_gpio_pin_write(BOOTLOADER_BUTTON_PRESS_LED, !BOOTLOADER_BUTTON_PRESS_LED_ONSTATE);
}
static gzp_id_req_res_t send_host_id_req(void)
{
    gzp_id_req_res_t id_resp;
    
    // Try sending "Host ID" request
    id_resp = gzp_id_req_send(); 

    switch(id_resp)
    {
        case GZP_ID_RESP_REJECTED:
        case GZP_ID_RESP_FAILED:
             //nrfr_keyboard_led_blink_on(false, 0);   // Turn LED off
            nrf_gpio_pin_write(PAIRING_FAILED_LED,1);
            nrf_gpio_pin_write(PAIRING_SUCCESS_LED,0);
            // Reset variables so that pairing restarts from the beginning. 
            host_id_received     = false;
            system_addr_received = false;
            break;
        case GZP_ID_RESP_GRANTED:
            // Blink LED if new Host ID received from dongle (pairing success)
            //nrfr_keyboard_led_blink_on(true, 2000); // Blink LED for 2 seconds
            nrf_gpio_pin_write(PAIRING_FAILED_LED ,0);
            nrf_gpio_pin_write(PAIRING_SUCCESS_LED,1);
            // Reset variables so that pairing restarts from the beginning. 
            host_id_received = true;
            system_addr_received = true;
            break;
        case GZP_ID_RESP_PENDING:
            default:
            break; 
    }
    
     return id_resp; 
}
Example #3
0
static void leds_init(void)
{
    nrf_gpio_cfg_output(LED1_PININDEX);
    nrf_gpio_cfg_output(LED3_PININDEX);
    nrf_gpio_pin_write(LED1_PININDEX, !LED1_ONSTATE);
    nrf_gpio_pin_write(LED3_PININDEX, !LED3_ONSTATE);
}
Example #4
0
/**@brief Writes to the ABC pins which indicate the row number (0-7)
 */
static void Row_Write(uint8_t row) {
	//Get the row bits
	uint8_t row1 = row & 1, row2 = row & (1 << 1), row3 = row & (1 << 2);
	nrf_gpio_pin_write(A, row1);
	nrf_gpio_pin_write(B, row2);
	nrf_gpio_pin_write(C, row3);
}
Example #5
0
/**
 * \brief 		Set the LEDs of the RGB LED unit with last three bits
 * \param leds	2nd bit: Red; 1st bit: Blue; 0th bit: Green; Other bits: No effect
 * 				LED will light up if the bit is 1, switch off if bit is 0.
 */
void
leds_arch_set(uint8_t leds) {
    /* Invert the input since the LEDs are active low */
    leds = ~leds;
    nrf_gpio_pin_write(LEDS_CONF_GREEN,(leds & LEDS_GREEN));
    nrf_gpio_pin_write(LEDS_CONF_BLUE,(leds & LEDS_BLUE));
    nrf_gpio_pin_write(LEDS_CONF_RED,(leds & LEDS_RED));
}
/** @brief Function for Configuring PIN8 and PIN9 as outputs.
 */
static void gpio_config(void)
{
    nrf_gpio_cfg_output(GPIO_TOGGLE_TICK_EVENT);
    nrf_gpio_cfg_output(GPIO_TOGGLE_COMPARE_EVENT);

    nrf_gpio_pin_write(GPIO_TOGGLE_TICK_EVENT, 0);
    nrf_gpio_pin_write(GPIO_TOGGLE_COMPARE_EVENT, 0);
}
Example #7
0
/*************************************************************************************************
 *  Function    £ºLOS_EvbLedControl
 *  Discription : Led control function
 *  Input       : (1) index Led's index                                                                  *
 *                (2) cmd   Led on or off  
 *  Output      : None
 *  Return      : None                                                                                *
 *                                                                                 *
**************************************************************************************************/
void LOS_EvbLedControl(int index, int cmd)
{
    switch (index)
    {
        case LOS_LED1:
        {
            if (cmd == LED_ON)
            {
                
                //add you code here.
                nrf_gpio_pin_write(17,0);
                        /*led1 on */
            }
            else
            {
                //add you code here.
                nrf_gpio_pin_write(17,1);
                        /*led1 off */
            }
            break;
        }
        case LOS_LED2:
        {
            if (cmd == LED_ON)
            {
                //add you code here.
                        /*led2 on */
            }
            else
            { 
							//add you code here.
                        /*led2 off */
            }
            break;
        }
        case LOS_LED3:
        {
            if (cmd == LED_ON)
            {
                //add you code here.
                        /*led3 on */
            }
            else
            {  
							//add you code here.
                        /*led3 off */
            }
            break;
        }
        default:
        {
            break;
        }
    }
    return;
}
Example #8
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;
}
Example #9
0
/** Set @ref ERROR_PIN to one and enters an infinite loop. This function is called if any of the
 *  nRF6350 functions fail.
 */
static void show_error(void)
{
  nrf_gpio_pin_write(ERROR_PIN, 1);
  while(true)
  {
  }
}
Example #10
0
void makeTheCall(void) {
	uint32_t err_code;
	nrf_gpio_pin_write(LED_FALL_DETECTED,1);
	// set timer to execute callLow() after 3 seconds.
	err_code = app_timer_start(m_app_call_id, TIMER_3S_SINGLE, NULL);
  APP_ERROR_CHECK(err_code);
	
}
Example #11
0
void testLedSet(LED_t led, bool on)
{
  switch (led) {
  case Green:
    nrf_gpio_pin_write(18, on);
    break;

  case Red:
    nrf_gpio_pin_write(19, on);
    break;

  case Yellow:
    nrf_gpio_pin_write(20, on);
    break;

  }
}
Example #12
0
static void qdec_nrfx_gpio_ctrl(bool enable)
{
#if defined(CONFIG_QDEC_ENABLE_PIN)
	uint32_t val = (enable)?(0):(1);

	nrf_gpio_pin_write(CONFIG_QDEC_ENABLE_PIN, val);
	nrf_gpio_cfg_output(CONFIG_QDEC_ENABLE_PIN);
#endif
}
Example #13
0
static void qdec_nrfx_gpio_ctrl(bool enable)
{
#if defined(DT_NORDIC_NRF_QDEC_QDEC_0_ENABLE_PIN)
	uint32_t val = (enable)?(0):(1);

	nrf_gpio_pin_write(DT_NORDIC_NRF_QDEC_QDEC_0_ENABLE_PIN, val);
	nrf_gpio_cfg_output(DT_NORDIC_NRF_QDEC_QDEC_0_ENABLE_PIN);
#endif
}
Example #14
0
static void service_switch_onoff(uint8_t* data, uint16_t length, const service_characteristic_t* characteristic)
{
  if (length > 0 && data[0] != switch_state)
  {
    switch_state = !!data[0];
    nrf_gpio_pin_write(LED, !switch_state);
#if !defined(NRF52)
    service_notify(characteristic);
#endif
  }
}
/**
 * @brief Function for configuring: pin 0 for input, pin 8 for output, 
 * and configures GPIOTE to give an interrupt on pin change.
 */
static void gpio_init(void)
{
	//INIT LED0: Initial state is off. 
	nrf_gpio_cfg_output(LED_0);
	nrf_gpio_cfg_output(LED_1);
	nrf_gpio_pin_write(LED_0, 1);
	
	//INIT BUTTON
	nrf_gpio_cfg_input(BUTTON_0, NRF_GPIO_PIN_NOPULL);

}
Example #16
0
int main(void)
{
    ret_code_t err_code;

    gpio_init();

    err_code = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(err_code);

    clocks_start();

    err_code = esb_init();
    APP_ERROR_CHECK(err_code);

    LEDS_CONFIGURE(LEDS_MASK);

    NRF_LOG_DEBUG("Enhanced ShockBurst Transmitter Example running.\r\n");

    while (true)
    {
        NRF_LOG_DEBUG("Transmitting packet %02x\r\n", tx_payload.data[1]);

        tx_payload.noack = false;
        if (nrf_esb_write_payload(&tx_payload) == NRF_SUCCESS)
        {
            // Toggle one of the LEDs.
            nrf_gpio_pin_write(LED_1, !(tx_payload.data[1]%8>0 && tx_payload.data[1]%8<=4));
            nrf_gpio_pin_write(LED_2, !(tx_payload.data[1]%8>1 && tx_payload.data[1]%8<=5));
            nrf_gpio_pin_write(LED_3, !(tx_payload.data[1]%8>2 && tx_payload.data[1]%8<=6));
            nrf_gpio_pin_write(LED_4, !(tx_payload.data[1]%8>3));
            tx_payload.data[1]++;
        }
        else
        {
            NRF_LOG_WARNING("Sending packet failed\r\n");
        }

        nrf_delay_us(50000);
    }
}
Example #17
0
/** RTC0 interrupt handler.
 * Triggered on TICK and COMPARE0 match.
 */
void RTC0_IRQHandler()
{
  if ((NRF_RTC0->EVENTS_TICK != 0) && ((NRF_RTC0->INTENSET & RTC_INTENSET_TICK_Msk) != 0))
  {
    NRF_RTC0->EVENTS_TICK = 0;
    nrf_gpio_pin_toggle(GPIO_TOGGLE_TICK_EVENT);
  }
  if ((NRF_RTC0->EVENTS_COMPARE[0] != 0) && ((NRF_RTC0->INTENSET & RTC_INTENSET_COMPARE0_Msk) != 0))
  {
    NRF_RTC0->EVENTS_COMPARE[0] = 0;
    nrf_gpio_pin_write(GPIO_TOGGLE_COMPARE_EVENT, 1);
  }
}
Example #18
0
/**
 * @brief Function for configuring: pin 0 for input, pin 8 for output, 
 * and configures GPIOTE to give an interrupt on pin change.
 */
static void gpio_init(void)
{
    nrf_gpio_cfg_input(BUTTON_0, BUTTON_PULL);
    nrf_gpio_cfg_output(LED_0);

    nrf_gpio_pin_write(LED_0, BUTTON_0);

    // Enable interrupt:
    NVIC_EnableIRQ(GPIOTE_IRQn);
    NRF_GPIOTE->CONFIG[0] =  (GPIOTE_CONFIG_POLARITY_Toggle << GPIOTE_CONFIG_POLARITY_Pos)
                           | (0 << GPIOTE_CONFIG_PSEL_Pos)  
                           | (GPIOTE_CONFIG_MODE_Event << GPIOTE_CONFIG_MODE_Pos);
    NRF_GPIOTE->INTENSET  = GPIOTE_INTENSET_IN0_Set << GPIOTE_INTENSET_IN0_Pos;
}
/**
 * main function
 * \return 0. int return type required by ANSI/ISO standard.
 */
int main(void)
{
    int i;

    gpio_init();
    gpiote_init();
    wdt_init();

    //Write the value of RESETREAS to pins 9-15 (LEDs 1-7)
    nrf_gpio_pin_write(LED_1, NRF_POWER->RESETREAS & POWER_RESETREAS_RESETPIN_Msk); //Bit A in RESETREAS
    nrf_gpio_pin_write(LED_2, NRF_POWER->RESETREAS & POWER_RESETREAS_DOG_Msk);      //Bit B in RESETREAS
    nrf_gpio_pin_write(LED_3, NRF_POWER->RESETREAS & POWER_RESETREAS_SREQ_Msk);     //Bit C in RESETREAS
    nrf_gpio_pin_write(LED_4, NRF_POWER->RESETREAS & POWER_RESETREAS_LOCKUP_Msk);   //Bit D in RESETREAS
    nrf_gpio_pin_write(LED_5, NRF_POWER->RESETREAS & POWER_RESETREAS_OFF_Msk);      //Bit E in RESETREAS
    nrf_gpio_pin_write(LED_6, NRF_POWER->RESETREAS & POWER_RESETREAS_LPCOMP_Msk);   //Bit F in RESETREAS
    nrf_gpio_pin_write(LED_7, NRF_POWER->RESETREAS & POWER_RESETREAS_DIF_Msk);      //Bit G in RESETREAS    

    NRF_POWER->RESETREAS = 0xFFFFFFFF;   //Clear the RESETREAS register

    for(i=0;i<STARTUP_TOGGLE_ITERATIONS;i++)
    {
        nrf_gpio_pin_toggle(LED_0);
        nrf_delay_us(DELAY);
    }

    while (true)
    {
        //Blink LED 0 fast until watchdog triggers reset
        nrf_gpio_pin_toggle(LED_0);
        nrf_delay_us(DELAY/3);

        // If SYSTEM_OFF_BUTTON is pressed.. enter System Off mode
        if(nrf_gpio_pin_read(SYSTEM_OFF_BUTTON) == BTN_PRESSED)
        {
            // Clear PORT 1 (pins 8-15)
            nrf_gpio_port_clear(NRF_GPIO_PORT_SELECT_PORT1, 0xFF);

            // Enter system OFF. After wakeup the chip will be reset, and the program will run from the top 
            NRF_POWER->SYSTEMOFF = 1;
        }

        // If SOFTWARE_RESET_BUTTON is pressed.. soft-reset
        if(nrf_gpio_pin_read(SOFTWARE_RESET_BUTTON) == BTN_PRESSED)
        {
            NVIC_SystemReset();
        }
    }
}
/* CENTRAL: Callback handling NUS Client events.
   Handling events from the ble_nus_c module.
   This function is called to notify the application of NUS client events.
   Parameters: p_ble_nus_c   NUS Client Handle. This identifies the NUS client
               p_ble_nus_evt Pointer to the NUS Client event.
*/
static void ble_nus_c_evt_handler(ble_nus_c_t * p_ble_nus_c, const ble_nus_c_evt_t * p_ble_nus_evt)
{
    uint32_t err_code;
    switch (p_ble_nus_evt->evt_type)
    {
        case BLE_NUS_C_EVT_FOUND_NUS_TX_CHARACTERISTIC:
		{
            /* TX characteristic found */
            break;
        }
        case BLE_NUS_C_EVT_FOUND_NUS_RX_CHARACTERISTIC:
		{
	    	/* RX characteristic found: enable notification on that */
            err_code = ble_nus_c_rx_notif_enable(p_ble_nus_c);
            APP_ERROR_CHECK(err_code);
            break;
        }
        case BLE_NUS_C_EVT_NUS_RX_EVT:
		{
			/* send received data from NUS to uart interface */
            for (uint32_t i = 0; i < p_ble_nus_evt->data_len; i++)
            {
                while(app_uart_put( p_ble_nus_evt->p_data[i]) != NRF_SUCCESS);
            }
			app_uart_put('.');
            break;
        }
        case BLE_NUS_C_EVT_DISCONNECTED:
		{
			/* clear related connection handle */
			active_conn_handles[pending_nus_conn_index] = BLE_CONN_HANDLE_INVALID;

			/* reset pending NUS connection index */
			pending_nus_conn_index = 0xFF;
			/* reset uart */
			uart_reset();
			/* set "connection" pin as disconnected */
			nrf_gpio_pin_write(CONN_PIN_NUMBER, DISCONNECTED_PIN_STATE);
			/* send confirmation string */
			uart_send_string((uint8_t *)"OK.", 3);
		
            break;
		}
    }
}
Example #21
0
void PingModule::ConnectionPacketReceivedEventHandler(connectionPacket* inPacket, Connection* connection, connPacketHeader* packetHeader, u16 dataLength)
{
	//Must call superclass for handling
	Module::ConnectionPacketReceivedEventHandler(inPacket, connection, packetHeader, dataLength);

    connPacketModule* packet = (connPacketModule*) packetHeader;

    switch(packetHeader->messageType)
    {
        case MESSAGE_TYPE_MODULE_TRIGGER_ACTION:
            //Check if our module is meant and we should trigger an action
            if(packet->moduleId == moduleId)
            {
                switch(packet->actionType)
                {
                    case PingModuleTriggerActionMessages::TRIGGER_PING:
                        // logt("PINGMOD", "Ping request received from %u with data: %d", packetHeader->sender, packet->data[0]);

                        //Send PING_RESPONSE
                        connPacketModule outPacket;
                        outPacket.header.messageType = MESSAGE_TYPE_MODULE_ACTION_RESPONSE;
                        outPacket.header.sender = node->persistentConfig.nodeId;
                        outPacket.header.receiver = packetHeader->sender;

                        outPacket.moduleId = moduleId;
                        outPacket.actionType = PingModuleActionResponseMessages::PING_RESPONSE;
                        outPacket.data[0] = packet->data[0];
                        outPacket.data[1] = packet->data[0];

                        cm->SendMessageToReceiver(NULL, (u8*)&outPacket, SIZEOF_CONN_PACKET_MODULE + 2, true);

                        {
	                        int a = cm->connections[0]->GetAverageRSSI();
	                        int b = cm->connections[1]->GetAverageRSSI();
	                        int c = cm->connections[2]->GetAverageRSSI();
	                        int d = cm->connections[3]->GetAverageRSSI();
            				int sum = -(a+b+c+d);

                            if(sum == 0)
                            {
                                nrf_gpio_pin_write(kPinNumberRed, 0);
                                nrf_gpio_pin_write(kPinNumberGreen, 0);
                                nrf_gpio_pin_write(kPinNumberBlue, 0);
                            } else
                            if(sum >= 80) {
                                nrf_gpio_pin_write(kPinNumberRed, 255);
                                nrf_gpio_pin_write(kPinNumberGreen, 0);
                                nrf_gpio_pin_write(kPinNumberBlue, 0);
                            } else
                            if (sum >= 70) {
                                nrf_gpio_pin_write(kPinNumberRed, 153);
                                nrf_gpio_pin_write(kPinNumberGreen, 76);
            	                nrf_gpio_pin_write(kPinNumberBlue, 0);
            				} else {					
            					nrf_gpio_pin_write(kPinNumberRed, 0);
                                nrf_gpio_pin_write(kPinNumberGreen, 255);
                                nrf_gpio_pin_write(kPinNumberBlue, 0);
            				}
			
                        	//logt("PINGMOD", "RSSI: [%d] [%d] [%d] [%d] Sum: %d", a, b, c, d, sum);
                        }
                        break;

                     default:
                         logt("PINGMOD", "PingModuleTriggerActionMessages::TRIGGER_PING: Unknown action: %d", packet->actionType);
                         break;
                }
            }
            break;

        case MESSAGE_TYPE_MODULE_ACTION_RESPONSE:
            //Check if our module is meant and we should trigger an action
            if(packet->moduleId == moduleId)
            {
                switch(packet->actionType)
                {
                    case PingModuleActionResponseMessages::PING_RESPONSE:
                        break;

                     default:
                         logt("PINGMOD", "MESSAGE_TYPE_MODULE_ACTION_RESPONSE: Unknown action: %d", packet->actionType);
                         break;
                }
            }
            break;
    }
}
Example #22
0
void callLow(void * p_context) {
	
	nrf_gpio_pin_write(LED_FALL_DETECTED,0);
	//SaveSnapshot = 1;
	do_post_processing = true;
}
Example #23
0
/**
 * @brief Timer routine for fall & step detection.
 */
void process_step_count(void * p_context)
{
  //timer_stop(m_app_timer_id);
	
	reset_wdt();
	
	nrf_gpio_pin_toggle(LED_RESETTING); 
	
	if (NRF_NVMC->READY == NVMC_READY_READY_Busy)
  {
        // Do not interfere if flash write is ongoing.
			return;
  }

  
	
	if (SaveSnapshot == 1)
	{
		nrf_gpio_pin_set(LED_RESETTING);
		return;
	}
	
	if (ReadBigSnapshot == 1)
		return;

    
    // store snapshot
		readSensorsAndStore(); 
	
	//process activity levels
		updateAverages();
		curr_sv = latestResultant();

	
	if (BLESessionActive == 1)
		return;
	
		if (curr_sv < getPerso()->freefall_threshold) {
			zero_g_detected = true;	
		}
	
//		if ( checkOrientation() == ORIENTATION_STANDING){
//			if (curr_sv < getPerso()->freefall_threshold){
//					zero_g_detected = true;
//				}
//			}
//		else if (checkOrientation() == ORIENTATION_NOT_STANDING)
//		{
//			if (curr_sv < NOT_STANDING_FREEFALL_THRESHOLD_DEFAULT){
//				zero_g_detected = true;
//			}
//		}
		else {
			zero_g_detected = false;
			
		}
    
    //zero_g_detected = fd_check_zero_g();    
    if (zero_g_detected == true) 
    {        
      nrf_gpio_pin_write(LED_OTHER,1);
			
//			if (count == 0) 
				accel_get_angles(&xAngleBeforeImpact, &yAngleBeforeImpact);			
			
			count = getPerso()->fall_detection_window;
		}
			
			if (count > 0)
			{
				count--;
					
				fall_detected = fd_check_for_impact(curr_sv);
					if (fall_detected == 0xFF) 
					{
							accel_get_angles(&xAngleAfterImpact, &yAngleAfterImpact);
						
	//						if (abs(xAngleAfterImpact - xAngleBeforeImpact) >= getPerso()->x_angle_threshold && 
	//								abs(yAngleAfterImpact - yAngleBeforeImpact) >= getPerso()->y_angle_threshold)
							{                   
									ClearDataBuffer = ClearSnapshots = SetTimestamp = 0;
								
							//		backupRawData();
								
									do_post_processing = true;
									
									emergencyCall = 1;

									zero_g_detected = false;
									fall_detected = 0;
									count = 0;
									
							}
							
					}
					
			}

			if (count == 0)
				nrf_gpio_pin_write(LED_OTHER,0);
 /*   
			else 
			{
				// detect if step occurred
				if (detect_step(xRaw,yRaw,zRaw)==1) 
						{
								nSteps++;
						}
			}
*/
 
}
Example #24
0
void lights(){
        nrf_gpio_pin_write(21,0);
        nrf_delay_ms(500);
        nrf_gpio_pin_write(25,0);
        nrf_delay_ms(500);
        nrf_gpio_pin_write(23,0);
        nrf_delay_ms(500);
        nrf_gpio_pin_write(19,0);
        nrf_delay_ms(500);
        nrf_gpio_pin_write(18,0);
        nrf_delay_ms(500);
        nrf_gpio_pin_write(12,0);
        nrf_delay_ms(500);
        nrf_gpio_pin_write(10,0);
        nrf_delay_ms(500);
        nrf_gpio_pin_write(9,0);
        nrf_delay_ms(500);
        nrf_gpio_pin_write(30,0);
        nrf_delay_ms(500);
        nrf_gpio_pin_write(28,0);
        nrf_delay_ms(500);
        nrf_gpio_pin_write(29,0);
        nrf_delay_ms(500);
        nrf_gpio_pin_write(22,0);
        nrf_delay_ms(1000);
}
Example #25
0
// called on a timer to scan rows out
void jswrap_microbit_display_callback() {
  microbitRow++;
  if (microbitRow>2) microbitRow=0;

  int n = microbitRow*9;
  uint32_t s = ~microbitLEDState;
  nrf_gpio_pin_clear(MB_LED_ROW1);
  nrf_gpio_pin_clear(MB_LED_ROW2);
  nrf_gpio_pin_clear(MB_LED_ROW3);
  nrf_gpio_pin_write(MB_LED_COL1, s & (1 << MB_LED_MAPPING[n++]));
  nrf_gpio_pin_write(MB_LED_COL2, s & (1 << MB_LED_MAPPING[n++]));
  nrf_gpio_pin_write(MB_LED_COL3, s & (1 << MB_LED_MAPPING[n++]));
  nrf_gpio_pin_write(MB_LED_COL4, s & (1 << MB_LED_MAPPING[n++]));
  nrf_gpio_pin_write(MB_LED_COL5, s & (1 << MB_LED_MAPPING[n++]));
  nrf_gpio_pin_write(MB_LED_COL6, s & (1 << MB_LED_MAPPING[n++]));
  nrf_gpio_pin_write(MB_LED_COL7, s & (1 << MB_LED_MAPPING[n++]));
  nrf_gpio_pin_write(MB_LED_COL8, s & (1 << MB_LED_MAPPING[n++]));
  nrf_gpio_pin_write(MB_LED_COL9, s & (1 << MB_LED_MAPPING[n++]));
  nrf_gpio_pin_write(MB_LED_ROW1, microbitRow==0);
  nrf_gpio_pin_write(MB_LED_ROW2, microbitRow==1);
  nrf_gpio_pin_write(MB_LED_ROW3, microbitRow==2);
}
Example #26
0
File: main.c Project: tve/Espruino
/**@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);

    if (app_reset)
    {
        NRF_POWER->GPREGRET = 0;
    }

    leds_init();

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

    (void)bootloader_init();

    if (bootloader_dfu_sd_in_progress())
    {
        nrf_gpio_pin_write(UPDATE_IN_PROGRESS_LED, UPDATE_IN_PROGRESS_LED_ONSTATE);

        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);

        nrf_gpio_pin_write(UPDATE_IN_PROGRESS_LED, !UPDATE_IN_PROGRESS_LED_ONSTATE);
    }
    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) == BOOTLOADER_BUTTON_ONSTATE) ? true: false);

    // If button is held down for 3 seconds, don't start bootloader.
    // This means that we go straight to Espruino, where the button is still
    // pressed and can be used to stop execution of the sent code.
    if (dfu_start) {
      nrf_gpio_pin_write(BOOTLOADER_BUTTON_PRESS_LED, BOOTLOADER_BUTTON_PRESS_LED_ONSTATE);
      int count = 3000;
      while (nrf_gpio_pin_read(BOOTLOADER_BUTTON) == BOOTLOADER_BUTTON_ONSTATE && count) {
        nrf_delay_us(999);
        count--;
      }
      if (!count)
        dfu_start = false;
      nrf_gpio_pin_write(BOOTLOADER_BUTTON_PRESS_LED, !BOOTLOADER_BUTTON_PRESS_LED_ONSTATE);
    }

    if (dfu_start || (!bootloader_app_is_valid(DFU_BANK_0_REGION_START))) {
        nrf_gpio_pin_write(UPDATE_IN_PROGRESS_LED, UPDATE_IN_PROGRESS_LED_ONSTATE);

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

        nrf_gpio_pin_write(UPDATE_IN_PROGRESS_LED, !UPDATE_IN_PROGRESS_LED_ONSTATE);
    }

    if (bootloader_app_is_valid(DFU_BANK_0_REGION_START) && !bootloader_dfu_sd_in_progress())
    {
        // 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);
    }

    NVIC_SystemReset();
}
Example #27
0
void jshPinSetValue(Pin pin, bool value) 
{
  nrf_gpio_pin_write((uint32_t)pinInfo[pin].pin, value);
}
Example #28
0
/**
 * @brief Function for application main entry.
 */
int main(void)
{
    // Configure pins 8-15 (Port1) as outputs
    nrf_gpio_range_cfg_output(8, 15);
    nrf_gpio_port_clear(NRF_GPIO_PORT_SELECT_PORT1, 0XFF);

    // Configure motion interrupt pin
    nrf_gpio_cfg_input(MOTION_INTERRUPT_PIN_NUMBER, NRF_GPIO_PIN_PULLDOWN);

    gpiote_init();

    if (adns2080_init() != ADNS2080_OK)
    {
        // ADNS2080 init failed, set rightmost LED on.
        nrf_gpio_pin_write(15, 1);
        while (true)
        {
            //  Do nothing.
        }
    }

    // By default, ADNS2080 motion interrupt output is active low, edge sensitive; make it active high.
    if (adns2080_motion_interrupt_set(ADNS2080_MOTION_OUTPUT_POLARITY_HIGH, ADNS2080_MOTION_OUTPUT_SENSITIVITY_LEVEL) != ADNS2080_OK)
    {
        nrf_gpio_pin_write(14, 1);
        while (true)
        {
            // Do nothing.
        }
    }

    // Read out movement to clear ADNS2080 interrupt flags.
    if (adns2080_is_motion_detected())
    {
        int16_t dummy;
        adns2080_movement_read(&dummy, &dummy);
    }

    // Enable GPIOTE interrupt in Nested Vector Interrupt Controller.
    NVIC_EnableIRQ(GPIOTE_IRQn);

    // Enable global interrupts.
    __enable_irq();

    while(true)
    {
        if (motion_interrupt_detected)
        {
            // Toggle pin 12 to indicate that we've detected motion interrupt.
            nrf_gpio_pin_toggle(12);
            
            motion_interrupt_detected = false;

            // On our Nordic reference design PCB, the chip orientation is not the same as in the ADNS2080
            // datasheet diagram, so X and Y axis are reversed. This is corrected by passing the pointer
            // parameters in reversed order. */
            adns2080_movement_read(&m_delta_y, &m_delta_x);

            // If movement delta_x is above the threshold, the LEDs light up accordingly. When the mouse is moved
            // to the left, LEDs 1 through 4 are lit and when the mouse is moved right, LEDs 5 through 8 are lit.
            if (m_delta_x > MOUSE_MOVEMENT_THRESHOLD)
            {
                nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT1, 0xF0);
            }
            else if (m_delta_x < (-MOUSE_MOVEMENT_THRESHOLD))
            {
                nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT1, 0x0F);
            }
            else
            {
                nrf_gpio_port_clear(NRF_GPIO_PORT_SELECT_PORT1,  0xFF);
            }
        }
    }
}
Example #29
0
static int uarte_instance_init(struct device *dev,
			       const struct uarte_init_config *config,
			       u8_t interrupts_active)
{
	int err;
	NRF_UARTE_Type *uarte = get_uarte_instance(dev);
	struct uarte_nrfx_data *data = get_dev_data(dev);

	nrf_gpio_pin_write(config->pseltxd, 1);
	nrf_gpio_cfg_output(config->pseltxd);

	nrf_gpio_cfg_input(config->pselrxd, NRF_GPIO_PIN_NOPULL);

	nrf_uarte_txrx_pins_set(uarte,
				config->pseltxd,
				config->pselrxd);

	if (config->hwfc == NRF_UARTE_HWFC_ENABLED) {
		nrf_gpio_pin_write(config->pselrts, 1);
		nrf_gpio_cfg_output(config->pselrts);

		nrf_gpio_cfg_input(config->pselcts, NRF_GPIO_PIN_NOPULL);

		nrf_uarte_hwfc_pins_set(uarte,
					config->pselrts,
					config->pselcts);
	}

	/* Configure flow control and parity checking */
	nrf_uarte_configure(uarte,
			    config->parity,
			    config->hwfc);

	err = baudrate_set(dev, config->baudrate);
	if (err) {
		return err;
	}

	/* Enable receiver and transmitter */
	nrf_uarte_enable(uarte);

	nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_ENDRX);

	nrf_uarte_rx_buffer_set(uarte, &data->rx_data, 1);
	nrf_uarte_task_trigger(uarte, NRF_UARTE_TASK_STARTRX);

#if UARTE_INTERRUPT_DRIVEN
	if (interrupts_active) {
		/* Set ENDTX event by requesting fake (zero-length) transfer.
		 * Pointer to RAM variable (data->tx_buffer) is set because
		 * otherwise such operation may result in HardFault or RAM
		 * corruption.
		 */
		nrf_uarte_tx_buffer_set(uarte, data->tx_buffer, 0);
		nrf_uarte_task_trigger(uarte, NRF_UARTE_TASK_STARTTX);

		/* switch off transmitter to save an energy */
		nrf_uarte_task_trigger(uarte, NRF_UARTE_TASK_STOPTX);
	}
#endif /* UARTE_INTERRUPT_DRIVEN */

	return 0;
}
Example #30
0
/**@brief Writes to the Color 2 pins which indicate the LED color for the last 8 rows
 */
static void Color2_Write(uint8_t rgb) {
	uint8_t blue = rgb & 1, green = rgb & (1 << 1), red = rgb & (1 << 2);
	nrf_gpio_pin_write(R2, red);
	nrf_gpio_pin_write(G2, green);
	nrf_gpio_pin_write(B2, blue);
}