Exemplo n.º 1
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);
    }
}
Exemplo n.º 2
0
/** \brief Get the current status of the LEDs in the last three bits
 * \return 2nd bit: Red; 1st bit: Blue; 0th bit: Green; Other bits: Zero
 * 		   1 if LED is on, 0 if off
 */
uint8_t
leds_arch_get(void) {
    uint8_t temp = 	(nrf_gpio_pin_read(LEDS_CONF_RED)*LEDS_RED)|
                    (nrf_gpio_pin_read(LEDS_CONF_BLUE)*LEDS_BLUE)|
                    (nrf_gpio_pin_read(LEDS_CONF_GREEN)*LEDS_GREEN);
    return ((~temp)&(LEDS_RED|LEDS_BLUE|LEDS_GREEN));
}
Exemplo n.º 3
0
static void ser_phy_init_gpiote(void)
{
    if (!nrf_drv_gpiote_is_init())
    {
        (void)nrf_drv_gpiote_init();
    }
    NVIC_SetPriority(GPIOTE_IRQn, APP_IRQ_PRIORITY_HIGH);

    nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(true);
    /* Enable pullup to ensure high state while connectivity device is reset */
    config.pull = NRF_GPIO_PIN_PULLUP;
    (void)nrf_drv_gpiote_in_init(SER_PHY_SPI_MASTER_PIN_SLAVE_REQUEST, &config,
        ser_phy_spi_master_request);
    nrf_drv_gpiote_in_event_enable(SER_PHY_SPI_MASTER_PIN_SLAVE_REQUEST,true);
    m_slave_request_flag = !(nrf_gpio_pin_read(SER_PHY_SPI_MASTER_PIN_SLAVE_REQUEST));

#ifdef _SPI_5W_
    m_slave_ready_flag = true;
#else
    (void)nrf_drv_gpiote_in_init(SER_PHY_SPI_MASTER_PIN_SLAVE_READY, &config,
        ser_phy_spi_master_ready);
    nrf_drv_gpiote_in_event_enable(SER_PHY_SPI_MASTER_PIN_SLAVE_READY,true);
    m_slave_ready_flag = !(nrf_gpio_pin_read(SER_PHY_SPI_MASTER_PIN_SLAVE_READY));
#endif

    NVIC_ClearPendingIRQ(SW_IRQn);
}
Exemplo n.º 4
0
/*************************************************************************
* charger status
* 2 -> NoCharge; 1 -> ChargingComplete£» 0 -> inCharging
**************************************************************************/
Charging_State charger_status(void)
{
    uint8_t charger = 0;
#ifdef CHARGER_CHARGING_PIN

    uint8_t charging = 0;
#endif
#ifdef CHARGER_CONNECTED_PIN

    charger = !(nrf_gpio_pin_read(CHARGER_CONNECTED_PIN) & 0xFF);
#else

    charger = (nrf_gpio_pin_read(CHARGER_CHARGING_PIN) & 0xFF);
#endif
#ifdef CHARGER_CHARGING_PIN

    charging = !(nrf_gpio_pin_read(CHARGER_CHARGING_PIN) & 0xFF);
#endif

    if(charger) {
#ifdef CHARGER_CHARGING_PIN
        if(charging)
            return InCharging;
        return ChargingComplete;
#else

        return InCharging;
#endif

    } else {
        return NoCharge;
    }
}
Exemplo n.º 5
0
uint8_t CHRG_getstate(void) {
    uint8_t result = 0;
    result = nrf_gpio_pin_read(CHRG_IN_PIN);
    nrf_gpio_cfg_output(CHRG_OUT_PIN);
    result |= (nrf_gpio_pin_read(CHRG_IN_PIN)<<1);
    nrf_gpio_cfg_input(CHRG_OUT_PIN, NRF_GPIO_PIN_NOPULL);
    return result;
}
Exemplo n.º 6
0
/**
 * @brief check_ready : check valid packet is received (DR = 1 indicate valid packet has been received)
 * @param none
 */
unsigned char check_ready(void)
{	
	if(nrf_gpio_pin_read(DR) && nrf_gpio_pin_read(TRX_CE) && !(nrf_gpio_pin_read(TXEN)))	{
		return 1;
	}  
	else {
		return 0;
	}
}
Exemplo n.º 7
0
//
// function for sending a strobe command to the CC1101
//
void SpiStrobe(uint8_t Strobe)
{
										nrf_gpio_pin_clear(SPIM0_SS_PIN);							//set SS low
                    while(nrf_gpio_pin_read(SPIM0_MISO_PIN));    	//wait until SO goes low(0)
                    m_tx_data[0] = Strobe;    										//send SRES command strobe
                    m_tx_data[1] = 0x00;    											//send empty byte
                    while(nrf_gpio_pin_read(SPIM0_MISO_PIN));    	//wait until SO goes low(0)
                    spi_send_recv(m_tx_data, m_rx_data, 2u);
										nrf_gpio_pin_set(SPIM0_SS_PIN);
}
Exemplo n.º 8
0
//
//function for a write single byte command
//
void CC1101_WriteSingle(uint8_t address, uint8_t data)
{
			m_tx_data[0] = address; //set address
      m_tx_data[1] = data;
      nrf_gpio_pin_clear(SPIM0_SS_PIN);  //set SS low
      while(nrf_gpio_pin_read(SPIM0_MISO_PIN));  //wait until SO goes low(0)
      spi_send_recv(m_tx_data, m_rx_data, 2u);
      while(nrf_gpio_pin_read(SPIM0_MISO_PIN));  //wait for SS to low
      nrf_gpio_pin_set(SPIM0_SS_PIN);          //set SS high
                
}
Exemplo n.º 9
0
/**
 * @brief Function for application main entry.
 * @return 0. int return type required by ANSI/ISO standard.
 */
int main(void)
{
  init();
  
  uint8_t btn0_nstate;                              // Store new (current) state of button 0
  uint8_t btn1_nstate;                              // Store new (current) state of button 1
  uint8_t btn0_ostate = nrf_gpio_pin_read(BUTTON_0); // Store old (previous) state of button 0
  uint8_t btn1_ostate = nrf_gpio_pin_read(BUTTON_1); // Store old (previous) state of button 1
  
  while(true)
  {
    uint8_t btns = 0;
    btn0_nstate = nrf_gpio_pin_read(BUTTON_0);
    btn1_nstate = nrf_gpio_pin_read(BUTTON_1);
    if ((btn0_ostate == 1) && (btn0_nstate == 0))
    {
      btns |= 1;
    }
     
    if ((btn1_ostate == 1) && (btn1_nstate == 0))
    {
      btns |= 2;
    }
    
    btn0_ostate = btn0_nstate;
    btn1_ostate = btn1_nstate;
    
    // Place the read buttons in the payload, enable the radio and
    // send the packet:
		
		packet[0]= btns;

    NRF_RADIO->EVENTS_READY = 0U;
    NRF_RADIO->TASKS_TXEN = 1;
    while (NRF_RADIO->EVENTS_READY == 0U)
    {
    }
    NRF_RADIO->TASKS_START = 1U;
    NRF_RADIO->EVENTS_END = 0U;  
    while(NRF_RADIO->EVENTS_END == 0U)
    {
    }
    NRF_RADIO->EVENTS_DISABLED = 0U;
    // Disable radio
    NRF_RADIO->TASKS_DISABLE = 1U;
    while(NRF_RADIO->EVENTS_DISABLED == 0U)
    {
    }
  }
}
/**
 * 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();
        }
    }
}
void btns_gpioTeHandler(BTNS_Data *context)
{
    const BTNS_PrivData *p = context->priv;
    int i = 0;
    for (; i <p->configSize; ++i) {
        const BTNS_Config *cfg = &(p->config[i]);
        if (BTNS_UnassignedChannel != cfg->btnChannelNo) {
            if (1 == NRF_GPIOTE->EVENTS_IN[cfg->btnChannelNo]){                            // Check if GPIO2 being pulled high triggered the interrupt
                NRF_GPIOTE->EVENTS_IN[cfg->btnChannelNo] = 0;                             // Reset interrupt

                uint32_t isReleased = nrf_gpio_pin_read(cfg->btnGpio);
                //for a button, value 0 means it's pressed, 1 - released
                if (isReleased) {
                    uint32_t upTime;
                    app_timer_cnt_get(&upTime);
                    if (upTime - downStartTime > BTNS_LONG_COUNT)
                        cfg->handler(cfg->hndlrData, BTNS_ButtonLongPress);
                    else
                        cfg->handler(cfg->hndlrData, BTNS_ButtonShortPress);
                }
                else {
                    app_timer_cnt_get(&downStartTime);
                    dbg_debugModule("Btn down\n");
                }
            }
        }
    }
}
Exemplo n.º 12
0
Arquivo: main.c Projeto: tkadom/TWBLE
/**@brief Function for handling button events.
 *
 * @param[in] pin_no The pin number of the button pressed.
 * @param[in] button_action Action of button that caused this event.
 */
void button_event_handler(uint8_t pin_no, uint8_t button_action)
{
    if (button_action == APP_BUTTON_PUSH)
    {
        switch (pin_no)
        {
            case BSP_BUTTON_0:
                // Toggle the ste of the LED
                m_led_change_counter++;

                if(nrf_gpio_pin_read(BSP_LED_0) == 0)
                    nrf_gpio_pin_set(BSP_LED_0);
                else
                    nrf_gpio_pin_clear(BSP_LED_0);
                break;
            case BSP_BUTTON_1:
            {
                // Open slave channel to 
                uint8_t channel_status;
                uint32_t err_code = sd_ant_channel_status_get (ANT_RELAY_SLAVE_CHANNEL, &channel_status);
                APP_ERROR_CHECK(err_code);

                if((channel_status & STATUS_CHANNEL_STATE_MASK) == STATUS_ASSIGNED_CHANNEL)
                {
                    err_code = sd_ant_channel_open(ANT_RELAY_SLAVE_CHANNEL);
                    APP_ERROR_CHECK(err_code);
                }
                break;
            }
            default:

                break;
        }
    }
}
Exemplo n.º 13
0
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);
}
Exemplo n.º 14
0
//================USB charing detect==================================
uint8_t USB_detect(void)
{ 
   //nrf_gpio_cfg_input(USB_DETECT_PIN, NRF_GPIO_PIN_PULLUP);
  //nrf_gpio_cfg_input(USB_DETECT_PIN, NRF_GPIO_PIN_NOPULL);
  //nrf_delay_100us(1);
   if(nrf_gpio_pin_read(USB_DETECT_PIN)==1)
   {
    //nrf_gpio_cfg_input(USB_DETECT_PIN, NRF_GPIO_PIN_PULLDOWN);
    if(chargeing_init==DISABLE_CHARGING_PIN_INIT)
    {
      //nrf_gpio_cfg_input(CHARGE_PIN, NRF_GPIO_PIN_NOPULL);
      nrf_gpio_cfg_input(CHARGE_PIN, NRF_GPIO_PIN_PULLUP);
      chargeing_init=ENABLE_CHARGING_PIN_INIT;
    }
   	return 1;
   }
   else
   {
    if(chargeing_init==ENABLE_CHARGING_PIN_INIT)
    {
      nrf_gpio_cfg_input(CHARGE_PIN, NRF_GPIO_PIN_PULLDOWN);
      chargeing_init=DISABLE_CHARGING_PIN_INIT;
    }  
   //nrf_gpio_cfg_input(USB_DETECT_PIN, NRF_GPIO_PIN_PULLDOWN);
   return 0;
   }
}
Exemplo n.º 15
0
/**@brief Function for the Device Manager initialization.
 */
static void device_manager_init(void)
{
    uint32_t                err_code;
    dm_init_param_t         init_data;
    dm_application_param_t  register_param;

    // Initialize persistent storage module.
    err_code = pstorage_init();
    APP_ERROR_CHECK(err_code);

    // Clear all bonded centrals if the Bonds Delete button is pushed.
    init_data.clear_persistent_data = (nrf_gpio_pin_read(BOND_DELETE_ALL_BUTTON_ID) == 0);

    err_code = dm_init(&init_data);
    APP_ERROR_CHECK(err_code);

    memset(&register_param.sec_param, 0, sizeof(ble_gap_sec_params_t));

    register_param.sec_param.timeout      = SEC_PARAM_TIMEOUT;
    register_param.sec_param.bond         = SEC_PARAM_BOND;
    register_param.sec_param.mitm         = SEC_PARAM_MITM;
    register_param.sec_param.io_caps      = SEC_PARAM_IO_CAPABILITIES;
    register_param.sec_param.oob          = SEC_PARAM_OOB;
    register_param.sec_param.min_key_size = SEC_PARAM_MIN_KEY_SIZE;
    register_param.sec_param.max_key_size = SEC_PARAM_MAX_KEY_SIZE;
    register_param.evt_handler            = device_manager_evt_handler;
    register_param.service_type           = DM_PROTOCOL_CNTXT_GATT_SRVR_ID;

    err_code = dm_register(&m_app_handle, &register_param);
    APP_ERROR_CHECK(err_code);
}
Exemplo n.º 16
0
SwitchPosition Switch()
{
    SwitchPosition result;

	//nrf_gpio_cfg_output(SWITCH_PIN1);
	//nrf_gpio_pin_clear(SWITCH_PIN1);

    nrf_gpio_cfg_input(SWITCH_PIN2, NRF_GPIO_PIN_PULLUP);

    nrf_delay_us(10);

    if(nrf_gpio_pin_read(SWITCH_PIN2) == 0)
    {
        result = SwitchESB;
    }
    else
    {
        result = SwitchBLE;
    }

    //nrf_gpio_cfg_input(SWITCH_PIN1, NRF_GPIO_PIN_NOPULL);
    nrf_gpio_cfg_input(SWITCH_PIN2, NRF_GPIO_PIN_NOPULL);

    return result;
}
Exemplo n.º 17
0
uint8_t Charge_full_detect(void)
{ 
   if(nrf_gpio_pin_read(CHARGE_PIN)==0)
   {
   	return 1;
   }
   return 0;
}
Exemplo n.º 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);

    // 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();
    bootloader_init();

    // Check if we reset in the middle of a firmware update
    if (bootloader_dfu_sd_in_progress()) {
        err_code = bootloader_dfu_sd_update_continue();
        APP_ERROR_CHECK(err_code);

        softdevice_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.
        softdevice_init(!app_reset);
        scheduler_init();
    }

    // Check if the Bootloader Control pin is low. If so, enter the bootloader
    // mode.
    dfu_start = (nrf_gpio_pin_read(BOOTLOADER_CTRL_PIN) == 0);

    // If the Bootloader Control pin is low or the application in the flash
    // is not valid, enter the bootloader mode.
    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);

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

    // If the application was or now is valid, run it
    if (bootloader_app_is_valid(DFU_BANK_0_REGION_START)) {
        // 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();
}
Exemplo n.º 19
0
int gpio_read(gpio_t *obj)
{
    MBED_ASSERT(obj->pin != (PinName)NC);
    if (m_gpio_cfg[obj->pin].direction == PIN_OUTPUT) {
        return ((NRF_GPIO->OUTSET & (1UL << obj->pin)) ? 1 : 0);
    } else {
        return nrf_gpio_pin_read(obj->pin);
    }
}
Exemplo n.º 20
0
//
//function for a write burst command to the CC1101
//
void CC1101_WriteBurst(uint8_t address, uint8_t * data, uint16_t len)
{
  uint8_t burstAddress;
  int i;

  burstAddress = (address | 0x40);//set R/W=0 and B=1
  m_tx_data[0] = burstAddress; //set address
	
	//for loop to fill our TX BUFFER with the data passed in to the write burst function
   for(i=1;i<=len+1;i++){
   m_tx_data[i] = data[i-1];
    }                                                                          
   nrf_gpio_pin_clear(SPIM0_SS_PIN);  				//set SS low
   while(nrf_gpio_pin_read(SPIM0_MISO_PIN));  //wait until SO goes low(0)
   spi_send_recv(m_tx_data, m_rx_data, len);
   while(nrf_gpio_pin_read(SPIM0_MISO_PIN));  //wait for SS to low
   nrf_gpio_pin_set(SPIM0_SS_PIN);          //set SS high      
}
Exemplo n.º 21
0
int gpio_read(gpio_t *obj)
{
    MBED_ASSERT(obj->pin != (PinName)NC);
    if (m_gpio_cfg[obj->pin].direction == PIN_OUTPUT) {
        return (nrf_gpio_pin_out_read(obj->pin) ? 1 : 0);
    } else {
        return nrf_gpio_pin_read(obj->pin);
    }
}
Exemplo n.º 22
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;
}
Exemplo n.º 23
0
void TIMER0_IRQHandler()
{
    if((p_timer->EVENTS_COMPARE[0] ==1) && (p_timer->INTENSET & TIMER_INTENSET_COMPARE0_Msk))     // 仅用来去抖延时
    {
        p_timer->EVENTS_COMPARE[0] = 0;
        p_timer->INTENSET = TIMER_INTENSET_COMPARE0_Disabled << TIMER_INTENSET_COMPARE0_Pos; // 关定时器延时中断0
        //p_timer->TASKS_CLEAR = 1;
        
        NRF_GPIOTE->INTENSET  = GPIOTE_INTENSET_IN0_Enabled << GPIOTE_INTENSET_IN0_Pos;     // 开GPIO引脚中断
        
        if((nrf_gpio_pin_read(8) & 1) && (button_tmp == 0))     // 一次有效高电平
        {
            button_tmp = 1;
           // xprintf("one ok.\r\n");
        }
        else 
        {
            if( (nrf_gpio_pin_read(8) & 1) && (button_tmp == 1))     // 检测到两次有效高电平 结束
            {
                p_timer->TASKS_STOP = 1;
                p_timer->TASKS_CLEAR = 1;

                p_timer->INTENSET = TIMER_INTENSET_COMPARE3_Enabled << TIMER_INTENSET_COMPARE3_Disabled; // 关延时中断2
                BUTTON_STATE = 2;
                button_tmp = 0;
                xprintf("Double click.\r\n");
            }
        }
        //xprintf("10ms.\r\n");
    }
    if( (p_timer->EVENTS_COMPARE[3] ==1) && (p_timer->INTENSET & TIMER_INTENSET_COMPARE3_Msk) && (nrf_gpio_pin_read(8) == 0 )&& (button_tmp == 1))  // 1s延时中断到达,只检测到一次
     {
         p_timer->EVENTS_COMPARE[3] = 0;
         p_timer->TASKS_STOP  = 1;
         p_timer->TASKS_CLEAR = 1;
         p_timer->INTENSET = TIMER_INTENSET_COMPARE0_Disabled << TIMER_INTENSET_COMPARE0_Pos; // 关定时器延时中断0
         p_timer->INTENSET = TIMER_INTENSET_COMPARE3_Disabled << TIMER_INTENSET_COMPARE3_Pos; // 关延时中断
         BUTTON_STATE = 1;
         button_tmp = 0;
         xprintf("Single click.\r\n");
     }
}
Exemplo n.º 24
0
/*====================================================================================================*/
int main( void )
{
  GPIO_Config();

  nrf_gpio_pin_clear(LED_1);
  nrf_gpio_pin_set(LED_2);
  nrf_gpio_pin_set(LED_3);
  nrf_gpio_pin_set(LED_4);
  Delay_100ms(10);

  while(1) {
    while(nrf_gpio_pin_read(KEY_1) == 0) {
      nrf_gpio_pin_toggle(LED_1);
      Delay_10ms(8);
    }
    while(nrf_gpio_pin_read(KEY_2) == 0) {
      nrf_gpio_pin_toggle(LED_2);
      Delay_10ms(8);
    }
    while(nrf_gpio_pin_read(KEY_3) == 0) {
      nrf_gpio_pin_toggle(LED_3);
      Delay_10ms(8);
    }
    while(nrf_gpio_pin_read(KEY_4) == 0) {
      nrf_gpio_pin_toggle(LED_4);
      Delay_10ms(8);
    }
      
    nrf_gpio_pin_toggle(LED_1);
    nrf_gpio_pin_toggle(LED_2);
    Delay_10ms(8);
    nrf_gpio_pin_toggle(LED_2);
    nrf_gpio_pin_toggle(LED_4);
    Delay_10ms(8);
    nrf_gpio_pin_toggle(LED_4);
    nrf_gpio_pin_toggle(LED_3);
    Delay_10ms(8);
    nrf_gpio_pin_toggle(LED_3);
    nrf_gpio_pin_toggle(LED_1);
    Delay_10ms(8);
  }
}
Exemplo n.º 25
0
/**@brief Function for doing power management.
 */
static void power_manage(void)
{
  // Clear both leds before sleep if not indicating button press
  if (nrf_gpio_pin_read(BUTTON_1))
  {
  GREEN_LED_OFF;
  RED_LED_OFF;
  }
  uint32_t err_code = sd_app_evt_wait();
  APP_ERROR_CHECK(err_code);
}
Exemplo n.º 26
0
int main() {
	
	LED_INIT();
	BUT_INIT();
	LEDS_OFF(); //the default is on
	while(1){
		if (nrf_gpio_pin_read(BUTTON_0) == 0){
			lights();
			LEDS_OFF();
		}
	}
}
Exemplo n.º 27
0
static __INLINE void ser_phy_init_gpiote()
{
    m_slave_request_flag = !(nrf_gpio_pin_read(SER_PHY_SPI_MASTER_PIN_SLAVE_REQUEST));
    m_slave_ready_flag   = !(nrf_gpio_pin_read(SER_PHY_SPI_MASTER_PIN_SLAVE_READY));

    (void)app_gpiote_input_event_handler_register(1,
                                                  SER_PHY_SPI_MASTER_PIN_SLAVE_REQUEST,
                                                  GPIOTE_CONFIG_POLARITY_Toggle,
                                                  ser_phy_spi_master_request);

    (void)app_gpiote_input_event_handler_register(0,
                                                  SER_PHY_SPI_MASTER_PIN_SLAVE_READY,
                                                  GPIOTE_CONFIG_POLARITY_Toggle,
                                                  ser_phy_spi_master_ready);

    (void)app_gpiote_end_irq_event_handler_register(ser_phy_spi_master_irq_end);

    (void)app_gpiote_enable_interrupts();

    NVIC_ClearPendingIRQ(PendSV_IRQn);
}
Exemplo n.º 28
0
void TIMER2_IRQHandler(void)
{
  // Clear interrupt
  if ((NRF_TIMER2->EVENTS_COMPARE[2] == 1) && (NRF_TIMER2->INTENSET & TIMER_INTENSET_COMPARE2_Msk))
  {
    NRF_TIMER2->EVENTS_COMPARE[2] = 0;
  }
  // Process buttons
  if (nrf_gpio_pin_read(BUTTON1) == 0)
  {
    pwm_set(LED_INTENSITY_HIGH);
  }
  else if (nrf_gpio_pin_read(BUTTON0) == 0)
  {
    pwm_set(LED_INTENSITY_LOW);
  }
  else
  {
    pwm_set(LED_OFF);
  }
}
Exemplo n.º 29
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();
}
static uint8_t input_get(void)
{
    uint32_t buttons[] = BUTTONS_LIST;
    uint32_t i;
    uint8_t  val = 0;
    // Saturate if more than 8 buttons
    uint32_t button_len = (BUTTONS_NUMBER > 8) ? 8 : BUTTONS_NUMBER;

    for (i = 0; i < button_len; i++)
    {
        val |= (nrf_gpio_pin_read(buttons[i]) << i);
    }
    return val;
}