bool Pwm_chibios::init(void) { pwmStop(driver_); pwmStart(driver_, &config_); pwmEnablePeriodicNotification(driver_); pwmEnableChannelNotification(driver_, channel_); return true; }
/* * Application entry point. */ int main(void) { /* * System initializations. * - HAL initialization, this also initializes the configured device drivers * and performs the board-specific initializations. * - Kernel initialization, the main() function becomes a thread and the * RTOS is active. */ halInit(); chSysInit(); /* * Initializes the PWM driver 1. * GPIOC1 is the PWM output. */ palSetPadMode(IOPORT3, 1, PAL_MODE_ALTERNATIVE_4); pwmStart(&PWMD1, &pwmcfg); pwmEnablePeriodicNotification(&PWMD1); /* * Starts the PWM channel 0 using 25% duty cycle. */ pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 2500)); pwmEnableChannel(&PWMD1, 1, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 2500)); pwmEnableChannelNotification(&PWMD1, 1); // MUST be before EnableChannel... chThdSleepMilliseconds(2500); /* * Changes PWM period to 500µs the duty cycle becomes 50% * implicitly. */ pwmChangePeriod(&PWMD1, 18000); chThdSleepMilliseconds(2500); //~ pwmEnablePeriodicNotification(&PWMD1); /* * Disables channel 0 and stops the drivers. */ //~ pwmDisableChannel(&PWMD1, 0); //~ pwmStop(&PWMD1); /* * Normal main() thread activity, in this demo it does nothing. */ while (TRUE) { palTogglePad(TEENSY_PIN13_IOPORT, TEENSY_PIN13); chThdSleepMilliseconds(5); } return 0; }
/* * Application entry point. */ int main(void) { /* * System initializations. * - HAL initialization, this also initializes the configured device drivers * and performs the board-specific initializations. * - Kernel initialization, the main() function becomes a thread and the * RTOS is active. */ halInit(); chSysInit(); /* * Starting PWM driver 1 and enabling the notifications. * GPIOA8 is programmed as PWM output (channel 1 of TIM1). */ pwmStart(&PWMD1, &pwmcfg); pwmEnablePeriodicNotification(&PWMD1); palSetPadMode(GPIOA, GPIOA_ARD_D5, PAL_MODE_ALTERNATE(1)); /* * Starting ICU driver 2. * GPIOA15 is programmed as ICU input (channel 1 of TIM2). */ icuStart(&ICUD2, &icucfg); palSetPadMode(GPIOA, GPIOA_ARD_D9, PAL_MODE_ALTERNATE(1)); /* * GPIOI1 is programmed as output (board LED). */ palClearPad(GPIOI, GPIOI_ARD_D13); palSetPadMode(GPIOI, GPIOI_ARD_D13, PAL_MODE_OUTPUT_PUSHPULL); chThdSleepMilliseconds(1000); /* * Starting ICU capture and enabling the notifications. */ icuStartCapture(&ICUD2); icuEnableNotifications(&ICUD2); /* * Normal main() thread activity, various PWM patterns are generated * cyclically, if the ICU input is connected to the PWM output the * board LED mirrors the PWM output. */ while (true) { /* * Starts the PWM channel 0 using 75% duty cycle. */ pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 7500)); pwmEnableChannelNotification(&PWMD1, 0); chThdSleepMilliseconds(5000); /* * Changes the PWM channel 0 to 50% duty cycle. */ pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000)); chThdSleepMilliseconds(5000); /* * Changes the PWM channel 0 to 25% duty cycle. */ pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 2500)); chThdSleepMilliseconds(5000); /* * Changes PWM period to half second the duty cycle becomes 50% * implicitly. */ pwmChangePeriod(&PWMD1, 5000); chThdSleepMilliseconds(5000); /* * Disables channel 0. */ pwmDisableChannel(&PWMD1, 0); } }
/* * Application entry point. */ int main(void) { /* * System initializations. * - HAL initialization, this also initializes the configured device drivers * and performs the board-specific initializations. * - Kernel initialization, the main() function becomes a thread and the * RTOS is active. */ halInit(); chSysInit(); /* * Initializes the PWM driver 4 and ICU driver 3. * GPIOD12 is the PWM output channel 0. * GPIOC6 is the ICU input ICU_CHANNEL_1. * The two pins have to be externally connected together. */ pwmStart(&PWMD4, &pwmcfg); pwmEnablePeriodicNotification(&PWMD4); palSetPadMode(GPIOD, 12, PAL_MODE_ALTERNATE(2)); icuStart(&ICUD3, &icucfg); palSetPadMode(GPIOC, 6, PAL_MODE_ALTERNATE(2)); icuStartCapture(&ICUD3); icuEnableNotifications(&ICUD3); chThdSleepMilliseconds(2000); /* * Starts the PWM channel 0 using 75% duty cycle. */ pwmEnableChannel(&PWMD4, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD4, 7500)); pwmEnableChannelNotification(&PWMD4, 0); chThdSleepMilliseconds(5000); /* * Changes the PWM channel 0 to 50% duty cycle. */ pwmEnableChannel(&PWMD4, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD4, 5000)); chThdSleepMilliseconds(5000); /* * Changes the PWM channel 0 to 25% duty cycle. */ pwmEnableChannel(&PWMD4, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD4, 2500)); chThdSleepMilliseconds(5000); /* * Changes PWM period to half second the duty cycle becomes 50% * implicitly. */ pwmChangePeriod(&PWMD4, 5000); chThdSleepMilliseconds(5000); /* * Disables channel 0 and stops the drivers. */ pwmDisableChannel(&PWMD4, 0); pwmStop(&PWMD4); icuStopCapture(&ICUD3); icuStop(&ICUD3); palClearPad(GPIOE, GPIOE_LED4_BLUE); palClearPad(GPIOE, GPIOE_LED9_BLUE); /* * Normal main() thread activity, in this demo it does nothing. */ while (true) { chThdSleepMilliseconds(500); } return 0; }
/* * Application entry point. */ int main(void) { /* * System initializations. * - HAL initialization, this also initializes the configured device drivers * and performs the board-specific initializations. * - Kernel initialization, the main() function becomes a thread and the * RTOS is active. */ halInit(); chSysInit(); palSetPadMode(GPIOF, GPIOF_LED_RED, PAL_MODE_OUTPUT_PUSHPULL); palSetPadMode(GPIOF, GPIOF_LED_GREEN, PAL_MODE_OUTPUT_PUSHPULL); palSetPadMode(GPIOF, GPIOF_LED_BLUE, PAL_MODE_OUTPUT_PUSHPULL); /* * Start PWM driver */ pwmStart(&PWMD1, &pwmcfg); pwmEnableChannel(&PWMD1, 0, 0); pwmEnableChannel(&PWMD1, 1, 0); pwmEnableChannel(&PWMD1, 2, 0); pwmEnableChannelNotification(&PWMD1, 0); pwmEnableChannelNotification(&PWMD1, 1); pwmEnableChannelNotification(&PWMD1, 2); pwmEnablePeriodicNotification(&PWMD1); /* * Normal main() thread activity */ while (TRUE) { uint16_t rgbColour[3]; uint8_t decColour; uint16_t i; // Start off with red. rgbColour[0] = pwmcfg.frequency - 2; rgbColour[1] = 0; rgbColour[2] = 0; // Choose the colours to increment and decrement. for (decColour = 0; decColour < 3; decColour++) { int incColour = decColour == 2 ? 0 : decColour + 1; // cross-fade the two colours. for(i = 0; i < pwmcfg.frequency - 2; i++) { rgbColour[decColour] -= 1; rgbColour[incColour] += 1; pwmEnableChannel(&PWMD1, 0, rgbColour[0]); pwmEnableChannel(&PWMD1, 1, rgbColour[1]); pwmEnableChannel(&PWMD1, 2, rgbColour[2]); chThdSleepMilliseconds(1); } } } return 0; }