Example #1
0
void AppInit(sleep_state last_sleep_state)
{
    /* set all PIOs to inputs and pull them down */
    PioSetModes(0xFFFFFFFFUL, pio_mode_user);
    PioSetDirs(0xFFFFFFFFUL, FALSE);
    PioSetPullModes(0xFFFFFFFFUL, pio_mode_strong_pull_down);
    
    /* Set LED0 and LED1 to be controlled directly via PioSet */
    PioSetModes((1UL << PIO_LED0), pio_mode_user);

    /* Configure LED0 and LED1 to be outputs */
    PioSetDir(PIO_LED0, PIO_DIR_OUTPUT);

    /* Set the LED0 and LED1 to have strong internal pull ups */
    PioSetPullModes((1UL << PIO_LED0),
                    pio_mode_strong_pull_up);

    /* Configure button to be controlled directly */
    PioSetMode(PIR_SIGNAL, pio_mode_user);

    /* Configure button to be input */
    PioSetDir(PIR_SIGNAL, PIO_DIR_INPUT);

    /* Set weak pull up on button PIO, in order not to draw too much current
     * while button is pressed
     */
    PioSetPullModes((1UL << PIR_SIGNAL), pio_mode_weak_pull_down);

    /* Set the button to generate sys_event_pio_changed when pressed as well
     * as released
     */
    PioSetEventMask((1UL << PIR_SIGNAL), pio_event_mode_rising);


    /* disable wake up on UART RX */
    SleepWakeOnUartRX(FALSE);
    
    /* pull down the I2C lines */
    PioSetI2CPullMode(pio_i2c_pull_mode_strong_pull_down);

    /* Reset LED sequence */
    restartLedSeq();

	SleepModeChange(sleep_mode_deep);


    TimerInit(MAX_APP_TIMERS, (void*)app_timers);

    BeaconInit();

    pirStatusServiceInit();
}
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 */

}
Example #3
0
/*----------------------------------------------------------------------------*
 *  NAME
 *      I2CcommsInit
 *
 *  DESCRIPTION
 *      This function initialises the I2C
 *
 *  RETURNS
 *      Nothing
 *
 *----------------------------------------------------------------------------*/
extern void I2CcommsInit(void)
{
    /* Initialise I2C if it is not already initialised */

    if(!i2c_initialised)
    {     
        i2c_initialised = TRUE;
        PioSetModes(    ((0x01L << I2C_SDA_PIO) | (0x01L << I2C_SCL_PIO)), 
                                                pio_mode_user);

        /* Configure the PIOs as Input */
        PioSetDirs(     ((0x01L << I2C_SDA_PIO) | (0x01L << I2C_SCL_PIO)), 
                                                FALSE);
        PioSetPullModes(((0x01L << I2C_SDA_PIO) | (0x01L << I2C_SCL_PIO)), 
                                                pio_mode_strong_pull_down); 
        PioSetEventMask(((0x01L << I2C_SDA_PIO) | (0x01L << I2C_SCL_PIO)), 
                                                pio_event_mode_disable);

        /* Configure the I2C controller */
        I2cInit(I2C_SDA_PIO,
                I2C_SCL_PIO,
                I2C_POWER_PIO_UNDEFINED,
                pio_i2c_pull_mode_strong_pull_up);

        /* Configure pull mode of the I2C pins */
        PioSetI2CPullMode(pio_i2c_pull_mode_strong_pull_up);

        /* Configure the I2C clock */
        I2cConfigClock(I2C_SCL_100KBPS_HIGH_PERIOD, I2C_SCL_100KBPS_LOW_PERIOD);
//        I2cConfigClock(I2C_SCL_400KBPS_HIGH_PERIOD, I2C_SCL_400KBPS_LOW_PERIOD);
        
        /* Enable the I2C controller */
        I2cEnable(TRUE);
    }
}
Example #4
0
/*----------------------------------------------------------------------------*
 *  NAME
 *      InitHardware
 *
 *  DESCRIPTION
 *      This function is called to initialise the application hardware.
 *
 *  PARAMETERS
 *      None
 *
 *  RETURNS
 *      Nothing
 *----------------------------------------------------------------------------*/
extern void InitHardware(void)
{
    /* Setup 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();
    
    /* Initialise LED hardware */
    LedInitHardware();

    /* Request an event when the button PIO changes state */
    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);
}
void LcdDisplayInitRst(void)
{
    /* Set up the PIOs for the RST pin on the LCD display. */
    PioSetPullModes((1UL << PIO_RST), pio_mode_no_pulls);
    PioSetMode(PIO_RST, pio_mode_user);
    PioSetDir(PIO_RST, TRUE); /* Output */
    PioSet(PIO_RST, TRUE);
} /* LcdDisplayInitRst */
Example #6
0
/*----------------------------------------------------------------------------*
 *  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);
}
Example #7
0
void AppInit(sleep_state last_sleep_state)
{
    /* set all PIOs to inputs and pull them down */
    PioSetModes(0xFFFFFFFFUL, pio_mode_user);
    PioSetDirs(0xFFFFFFFFUL, FALSE);
    PioSetPullModes(0xFFFFFFFFUL, pio_mode_strong_pull_down);
    
    /* disable wake up on UART RX */
    SleepWakeOnUartRX(FALSE);
    
    /* pull down the I2C lines */
    PioSetI2CPullMode(pio_i2c_pull_mode_strong_pull_down);
       
    /* Start advertising */
    startAdvertising();
}
/*----------------------------------------------------------------------------*
 *  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);
}
Example #9
0
/*----------------------------------------------------------------------------*
 *  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 */
Example #10
0
/*----------------------------------------------------------------------------*
 *  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 AppPowerOnReset().
 *
 * PARAMETERS
 *      last_sleep_state [in]   Last sleep state
 *
 * RETURNS
 *      Nothing
 *----------------------------------------------------------------------------*/
void AppInit(sleep_state last_sleep_state)
{
    /* Initialise UART communications */
    DebugInit(1, NULL, NULL);

    DebugWriteString("Configuring PWM Modes\r\n");

    /* Configure the output PIO on which the slow flashing LED signal is
     * generated */
    PioSetDir(PIO_LED, PIO_DIR_OUTPUT);

    /* SLOW FLASHING WITH PWM:
     * Configure PWM 0 to have the following characteristics
     * DULL LED Light is generated by having pulses of 0ms ON and
     *      6ms OFF (~0% duty cycle)
     * BRIGHT LED Light is generated by having pulses of 6ms ON and
     *      0ms OFF (~100% duty cycle)
     * Dullest and Brightest level are held for ~1s
     *
     * The brightness level ramps for ~1s when going from dullest to
     * brightest and vice-versa */
    if (PioConfigPWM(0, pio_pwm_mode_push_pull,
                     /* Pulse timings for the dullest part of the sequence:
                      * dullest part of the sequence has the pulse off for the whole
                      * period, in effect the line stays low for the duration for which
                      * the dullest part of the sequence lasts. */
                     0,          /* ON time for the pulse is 0us */
                     255,        /* OFF time for the pulse is (255 * 30)us */
                     62,         /* Dullest part of the sequence lasts for
                            ( 62 * 16 )ms before ramping up to the brightest
                            part of the sequence */

                     /* Pulse timings for the brightest part of the sequence:
                      * brightest part of the sequence has the pulse ON for the whole
                      * period, in effect the line stays high for the duration for which
                      * the brightest part of the sequence lasts. */
                     255,        /* ON time for the pulse is (255 * 30)us */
                     0,          /* OFF time for the pulse is 0us */
                     62,         /* Brightest part of the sequence lasts for
                            ( 62 * 16 )ms before ramping down to the dullest
                            part of the sequence */

                     /* Ramping between dullest and brightest parts of the sequence
                      * This parameter determines the duration for which the ramping
                      * lasts when going from dullest to the brightest (and vice-versa).
                      *
                      * The total duration for which the ramping lasts is determined by
                      * multiplying this value with one less than the difference between
                      * the on_time or off_time of the two states, whichever is bigger;
                      * in the units of 30us
                      * */
                     132         /* Ramping lasts for ((255-1) * 132 * 30)us */
                    ))
    {
        DebugWriteString("PWM0 was set to ramp between brightest and dullest "
                         "levels\r\n");

        /* Connect PWM0 output to LED */
        PioSetMode(PIO_LED, pio_mode_pwm0);

        /* Enable the PWM0 */
        PioEnablePWM(0, TRUE);
    }
    else
    {
        DebugWriteString("PWM0 couldn't be configured\r\n");
    }

    /* Set both outputs to have strong internal pull ups */
    PioSetPullModes((1UL << PIO_MOTOR) | (1UL << PIO_LED),
                    pio_mode_strong_pull_up);

    /* Configure motor PIO to be output */
    PioSetDir(PIO_MOTOR, PIO_DIR_OUTPUT);

    /* Connect PWM1 output to motor PIO */
    PioSetMode(PIO_MOTOR, pio_mode_pwm1);

    /* Initialise timers */
    TimerInit(MAX_APP_TIMERS, app_timers);

    /* Call the function (with a dummy timer ID to start with) to configure
     * the PWM1 with initial duty cycle and start a timer. When the timer
     * expires the same function gets called again and the duty cycle for
     * PWM1 gets updated. It takes care of restarting the timer and ensuring
     * that duty cycle alternates between 0% and 100% back and forth. */
    dutyCycleTask(TIMER_INVALID);

    /* Enable the PWM1 */
    PioEnablePWM(1, TRUE);
}