/*----------------------------------------------------------------------------* * 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); }
/*----------------------------------------------------------------------------* * 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 */ }
void LcdDisplayBacklight(uint8 dull, uint8 bright) { PioConfigPWM ( PWM_ID_LED, pio_pwm_mode_push_pull, /* All time units are ~30us except for hold_time (which is ~16ms). */ dull, 100 - (dull % 101), 20, /* dull_on/off/hold_time */ bright, 100 - (bright % 101), 20, /* bright_on/off/hold_time */ 255 /* ramp_rate */ ); } /* LcdDisplayBacklight */
/*----------------------------------------------------------------------------* * NAME * setPwmDutyCycle * * DESCRIPTION * Configures PWM1 to generate a waveform of a specified duty cycle. * * PARAMETERS * value A value in the range (0 to NUMBER_OF_STEPS). Setting * it to 0 would set the duty cycle to 0%, while setting * it to NUMBER_OF_STEPS would result in 100% duty cycle. * * RETURNS/MODIFIES * None *----------------------------------------------------------------------------*/ static void setPwmDutyCycle( uint8 value ) { const uint8 total_period = NUMBER_OF_STEPS; /* Total period including on and off times, in number of ticks */ PioConfigPWM( 1, /* Use the PWM1 */ pio_pwm_mode_push_pull, /* PWM1 is configured with push-pull output current driver circuitry */ /* Pulse timings for the first part of the sequence */ /* ON time for the pulses during the first part of the * sequence is (value * 30)us */ value, /* OFF time for the pulses during the first part of the * sequence is (total_period - value) * 30us */ (total_period - value), /* Duration for which the first part of the sequence lasts for * in units of 16ms. Since a fixed frequency waveform is * produced, both parts of the sequence contain identical * pulses. As ramping will not enabled for this PWM, the * output switches from the first part of the sequence to * the second part instantaneously. Since they are identical, * the switching doesn't have any effect and hence the duration * for which the first or second part of the sequence last * doesn't really matter. We simply choose a non-zero value. */ 1U, /* Pulse timings for the second part of the sequence. * This is chosen to be identical to the first part of the * sequence as the output is supposed to be fixed frequency. */ /* ON time for the pulses during the second part of the * sequence is also (value * 30)us */ value, /* OFF time for the pulses during the second part of the * sequence is also (total_period - value) * 30us */ (total_period - value), /* Duration for which the second part of the sequence lasts for * chosen to be 1 * 16ms */ 1U, /* Ramping is disabled as the output is supposed to be fixed * frequency */ 0U ); }
/*----------------------------------------------------------------------------* * NAME * BuzzerInitHardware * * DESCRIPTION * This function initialises the buzzer hardware. * * PARAMETERS * None * * RETURNS * Nothing *----------------------------------------------------------------------------*/ extern void BuzzerInitHardware(void) { /* Configure the buzzer PIO to use PWM. */ PioSetModes(BUZZER_PIO_MASK, pio_mode_pwm0); /* Configure the PWM for buzzer ON OFF values */ 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); /* Disable buzzer for the time being. */ PioEnablePWM(BUZZER_PWM_INDEX_0, FALSE); }
/*----------------------------------------------------------------------------* * 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 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); }