extern void HandlePIOChangedEvent(uint32 pio_changed) { if(pio_changed & PIO_BIT_MASK(BUTTON_PIO)) { /* PIO changed */ uint32 pios = PioGets(); if(!(pios & PIO_BIT_MASK(BUTTON_PIO))) { /* This is for the case when pio changed event has come for button * press. */ /* Delete any button press timer if already running */ TimerDelete(g_app_hw_data.button_press_tid); g_app_hw_data.button_press_tid = TIMER_INVALID; /* Create a button press timer */ g_app_hw_data.button_press_tid = TimerCreate(LONG_BUTTON_PRESS_TIMER, TRUE, handleShortOrLongButtonPressTimerExpiry); /* Initialize the short (or) long button pressed flag with FALSE.*/ g_app_hw_data.long_or_short_button_pressed = FALSE; } else { /* This is for the case when pio changed event comes for * the button release */ if(g_app_hw_data.button_press_tid != TIMER_INVALID) { /* Button press timer is running.This means that either * 1.timer EXTRA_LONG_BUTTON_PRESS_TIMER has not expired yet * OR * 2.Timer EXTRA_LONG_BUTTON_PRESS_TIMER has expired but a 2nd * timer of length (EXTRA_LONG_BUTTON_PRESS_TIMER - * LONG_BUTTON_PRESS_TIMER) which we start on 1st timer * expiry, is still running. */ /* case when timer LONG_BUTTON_PRESS_TIMER is still running */ if(g_app_hw_data.long_or_short_button_pressed == FALSE) { HandleShortButtonPress(); } /* Delete the already running button press timer */ TimerDelete(g_app_hw_data.button_press_tid); g_app_hw_data.button_press_tid = TIMER_INVALID; g_app_hw_data.long_or_short_button_pressed = FALSE; } } } }
/*----------------------------------------------------------------------------* * NAME * InitAlertTagHardware - initialize application hardware * * DESCRIPTION * This function is called upon a power reset to initialize the PIOs * and configure their initial states. * * RETURNS * Nothing. * *----------------------------------------------------------------------------*/ extern void InitAlertTagHardware(void) { #ifndef DEBUG_THRU_UART /* Don't wakeup on UART RX line */ SleepWakeOnUartRX(FALSE); #endif /* Setup PIOs * PIO3 - Buzzer - BUZZER_PIO * PIO4 - LED 1 - LED_PIO * PIO11 - Button - BUTTON_PIO */ PioSetModes(PIO_BIT_MASK(BUTTON_PIO), pio_mode_user); PioSetDir(BUTTON_PIO, PIO_DIRECTION_INPUT); /* input */ PioSetPullModes(PIO_BIT_MASK(BUTTON_PIO), pio_mode_strong_pull_up); /* Setup button on PIO11 */ PioSetEventMask(PIO_BIT_MASK(BUTTON_PIO), pio_event_mode_both); #ifdef ENABLE_LEDBLINK /* PWM is being used for LED glowing.*/ PioSetModes(PIO_BIT_MASK(LED_PIO), pio_mode_pwm1); /* Advertising parameters are being configured for PWM right now. When * application moves to connection state, we change PWM parameters to * the ones for connection */ PioConfigPWM(LED_PWM_INDEX_1, pio_pwm_mode_push_pull, DULL_LED_ON_TIME_ADV, DULL_LED_OFF_TIME_ADV, DULL_LED_HOLD_TIME_ADV, BRIGHT_LED_OFF_TIME_ADV, BRIGHT_LED_ON_TIME_ADV, BRIGHT_LED_HOLD_TIME_ADV, LED_RAMP_RATE); PioEnablePWM(LED_PWM_INDEX_1, FALSE); #endif /* ENABLE_LEDBLINK */ #ifdef ENABLE_BUZZER PioSetModes(PIO_BIT_MASK(BUZZER_PIO), pio_mode_pwm0); /* Configure the buzzer on PIO3 */ PioConfigPWM(BUZZER_PWM_INDEX_0, pio_pwm_mode_push_pull, DULL_BUZZ_ON_TIME, DULL_BUZZ_OFF_TIME, DULL_BUZZ_HOLD_TIME, BRIGHT_BUZZ_ON_TIME, BRIGHT_BUZZ_OFF_TIME, BRIGHT_BUZZ_HOLD_TIME, BUZZ_RAMP_RATE); PioEnablePWM(BUZZER_PWM_INDEX_0, FALSE); #endif /* ENABLE_BUZZER */ /* Set the I2C pins to pull down */ PioSetI2CPullMode(pio_i2c_pull_mode_strong_pull_down); }
extern void WeightInitHardware(void) { /* Set up PIOs * PIO11 - Button */ /* Set the button PIO to user mode */ PioSetModes(BUTTON_PIO_MASK, pio_mode_user); /* Set the PIO direction as input. */ PioSetDir(BUTTON_PIO, PIO_DIRECTION_INPUT); /* Pull up the PIO. */ PioSetPullModes(BUTTON_PIO_MASK, pio_mode_strong_pull_up); /* Initialise Buzzer Hardware */ BuzzerInitHardware(); /* Set up button on PIO11 */ PioSetEventMask(BUTTON_PIO_MASK, pio_event_mode_both); /* Save power by changing the I2C pull mode to pull down */ PioSetI2CPullMode(pio_i2c_pull_mode_strong_pull_down); #ifdef ENABLE_LEDBLINK /* PIO 4 is being used for LED glowing. */ PioSetModes(PIO_BIT_MASK(LED_PIO), pio_mode_user); /* user mode set */ PioSetDir(LED_PIO, PIO_DIRECTION_OUTPUT); /* output */ PioSet(LED_PIO, FALSE); /* set low */ #endif /* ENABLE_LEDBLINK */ }
/*----------------------------------------------------------------------------* * NAME * SetIndication * * DESCRIPTION * This function indicates the app state through LED blinking * * RETURNS/MODIFIES * Nothing. * *----------------------------------------------------------------------------*/ extern void SetIndication(app_indication state) { #ifdef ENABLE_LEDBLINK if(state == app_ind_stop) { /*Stop LED glowing */ PioEnablePWM(LED_PWM_INDEX_1, FALSE); /* Reconfigure LED to pio_mode_user. This reconfiguration has been done * because When PWM is disabled, LED pio value remains same as it was at * the exact time of disabling. So if LED was on, it may remain ON even * after PWM disabling. So it is better to reconfigure it to user mode. * It will reconfigured to PWM mode while enabling. */ PioSetModes(PIO_BIT_MASK(LED_PIO), pio_mode_user); PioSet(LED_PIO, FALSE); } else { if(state == app_ind_adv) { /* Fast Blinking for advertising */ PioConfigPWM(LED_PWM_INDEX_1, pio_pwm_mode_push_pull, DULL_LED_ON_TIME_ADV, DULL_LED_OFF_TIME_ADV, DULL_LED_HOLD_TIME_ADV, BRIGHT_LED_ON_TIME_ADV, BRIGHT_LED_OFF_TIME_ADV, BRIGHT_LED_HOLD_TIME_ADV, LED_RAMP_RATE); } else if(state == app_ind_conn) { /* slow blinking for connected state */ PioConfigPWM(LED_PWM_INDEX_1, pio_pwm_mode_push_pull, DULL_LED_ON_TIME_CONN, DULL_LED_OFF_TIME_CONN, DULL_LED_HOLD_TIME_CONN, BRIGHT_LED_ON_TIME_CONN, BRIGHT_LED_OFF_TIME_CONN, BRIGHT_LED_HOLD_TIME_CONN, LED_RAMP_RATE); } PioSetModes(PIO_BIT_MASK(LED_PIO), pio_mode_pwm1); /*Start LED glowing */ PioEnablePWM(LED_PWM_INDEX_1, TRUE); PioSet(LED_PIO, TRUE); } #endif /* ENABLE_LEDBLINK */ }
/*----------------------------------------------------------------------------* * NAME * InitTimeClientHardware-initialize application hardware * * DESCRIPTION * This function is called upon a power reset to initialize the PIOs * and configure their initial states. * * RETURNS * Nothing. * *----------------------------------------------------------------------------*/ extern void InitTimeClientHardware(void) { /* Setup PIOs * PIO3 - Buzzer - BUZZER_PIO * PIO4 - LED 1 - LED_PIO * PIO11 - Button - BUTTON_PIO */ PioSetModes(PIO_BIT_MASK(BUTTON_PIO), pio_mode_user); PioSetDir(BUTTON_PIO, PIO_DIRECTION_INPUT); /* input */ PioSetPullModes(PIO_BIT_MASK(BUTTON_PIO), pio_mode_strong_pull_up); /* Setup button on PIO11 */ PioSetEventMask(PIO_BIT_MASK(BUTTON_PIO), pio_event_mode_both); /* Initialize Buzzer Hardware */ BuzzerInitHardware(); #ifdef ENABLE_LEDBLINK /* PWM is being used for LED glowing.*/ PioSetModes(PIO_BIT_MASK(LED_PIO), pio_mode_pwm1); /* Advertising parameters are being configured for PWM right now. When * application moves to connection state, we change PWM parameters to * the ones for connection */ PioConfigPWM(LED_PWM_INDEX_1, pio_pwm_mode_push_pull, DULL_LED_ON_TIME_ADV, DULL_LED_OFF_TIME_ADV, DULL_LED_HOLD_TIME_ADV, BRIGHT_LED_OFF_TIME_ADV, BRIGHT_LED_ON_TIME_ADV, BRIGHT_LED_HOLD_TIME_ADV, LED_RAMP_RATE); PioEnablePWM(LED_PWM_INDEX_1, FALSE); #endif /* ENABLE_LEDBLINK */ /* Set the I2C pins to pull down */ PioSetI2CPullMode(pio_i2c_pull_mode_strong_pull_down); }
/*----------------------------------------------------------------------------* * NAME * AppInit * * DESCRIPTION * This user application function is called after a power-on reset * (including after a firmware panic), after a wakeup from Hibernate or * Dormant sleep states, or after an HCI Reset has been requested. * * NOTE: In the case of a power-on reset, this function is called * after app_power_on_reset(). * * PARAMETERS * last_sleep_state [in] Last sleep state * * RETURNS * Nothing *----------------------------------------------------------------------------*/ void AppInit(sleep_state last_sleep_state) { /* Length octet for first transaction */ uint16 len = 0U; /* Initialise communications */ DebugInit(1, NULL, NULL); /* Set up the queue containing data to be sent to the SPI Master. The queue * is read by the SPI Slave library and the data transferred over to the Tx * area of the shared RAM when an interrupt from the PIO controller is * received. The application can queue data at any time. */ OQSetFill(&(g_spi_data.tx_q), FALSE, 0U); OQCreate(g_spi_data.data_tx, MAX_TRANSACTION_SIZE, OQDataMode_packed, &(g_spi_data.tx_q)); /* Set up the queue containing data received from the SPI Master. Data is * transferred from the Rx area of the shared memory and added to the queue * by the SPI Slave library when it receives an interrupt from the PIO * controller. The application may read data from the queue at any time, * but in order to prevent the buffer from overflowing and corrupting the * incoming data the queue should be read as soon as possible after the data * status callback is received. */ OQSetFill(&(g_spi_data.rx_q), FALSE, 0U); OQCreate(g_spi_data.data_rx, MAX_TRANSACTION_SIZE, OQDataMode_packed, &(g_spi_data.rx_q)); /* Only enter shallow sleep mode, because the PIO controller needs to run at * 16MHz in order to operate the SPI bus at ~1MHz */ SleepModeChange(sleep_mode_shallow); /* Keep awake when WAKE pin is low */ SleepWakePinEnable(wakepin_mode_low_level); /* Queue the length octet for the first transfer */ OQQueueData(&(g_spi_data.tx_q), &len, 1U); /* Initialise the SPI Slave, and fill the Tx area of the shared RAM with the * general response octet, which will be sent in tha absence of any data in * the queue. */ SpiSlaveInit(PIO_CTRLR_CODE_ADDR, spiSlaveSSELStatusCallback, &(g_spi_data.tx_q), &(g_spi_data.rx_q), GENERAL_RESP); /* Configure the SPI Slave data callback to occur when the length octet for * the next transaction is received. */ SpiSlaveConfigDataStatusCallback(spiSlaveDataCallback, 1U); /* Set the pull-up for the SSEL pin */ PioSetPullModes(PIO_BIT_MASK(SPI_SLAVE_PIO_SSEL), pio_mode_weak_pull_up); /* Set the pull-up for MOSI pin */ PioSetPullModes(PIO_BIT_MASK(SPI_SLAVE_PIO_MOSI), pio_mode_weak_pull_up); /* Set the pull-up/pull-down for SCLK pin, depending on the SPI Mode */ PioSetPullModes(PIO_BIT_MASK(SPI_SLAVE_PIO_SCLK), PIO_CTRLR_SCLK_PULL_MODE); /* Start the SPI slave */ SpiSlaveStart(); /* Set state to indicate we're ready to receive the length octet for the * next transaction */ g_spi_data.state = state_waiting_for_len; DebugWriteString("\r\nSPI Slave test application, stores previous data sent" " from SPI Master and returns the data in the subsequent " "transactions\r\n"); DebugWriteString("Transaction format: \r\n"); DebugWriteString("<LEN> <delay (at least 60us)> <DATA>\r\n"); DebugWriteString("Ready to receive data\r\n"); } /* AppInit */