port buzzer and LED functions to gossamer framework
This commit is contained in:
@@ -133,81 +133,6 @@ void watch_disable_TRNG(void) {
|
||||
hri_trng_write_CTRLA_reg(TRNG, 0);
|
||||
}
|
||||
|
||||
|
||||
void _watch_enable_tcc(void) {
|
||||
// clock TCC0 with the main clock (8 MHz) and enable the peripheral clock.
|
||||
hri_gclk_write_PCHCTRL_reg(GCLK, TCC0_GCLK_ID, GCLK_PCHCTRL_GEN_GCLK0_Val | GCLK_PCHCTRL_CHEN);
|
||||
hri_mclk_set_APBCMASK_TCC0_bit(MCLK);
|
||||
// disable and reset TCC0.
|
||||
hri_tcc_clear_CTRLA_ENABLE_bit(TCC0);
|
||||
hri_tcc_wait_for_sync(TCC0, TCC_SYNCBUSY_ENABLE);
|
||||
hri_tcc_write_CTRLA_reg(TCC0, TCC_CTRLA_SWRST);
|
||||
hri_tcc_wait_for_sync(TCC0, TCC_SYNCBUSY_SWRST);
|
||||
// divide the clock down to 1 MHz
|
||||
if (hri_usbdevice_get_CTRLA_ENABLE_bit(USB)) {
|
||||
// if USB is enabled, we are running an 8 MHz clock.
|
||||
hri_tcc_write_CTRLA_reg(TCC0, TCC_CTRLA_PRESCALER_DIV8);
|
||||
} else {
|
||||
// otherwise it's 4 Mhz.
|
||||
hri_tcc_write_CTRLA_reg(TCC0, TCC_CTRLA_PRESCALER_DIV4);
|
||||
}
|
||||
// We're going to use normal PWM mode, which means period is controlled by PER, and duty cycle is controlled by
|
||||
// each compare channel's value:
|
||||
// * Buzzer tones are set by setting PER to the desired period for a given frequency, and CC[1] to half of that
|
||||
// period (i.e. a square wave with a 50% duty cycle).
|
||||
// * LEDs on CC[2] and CC[3] can be set to any value from 0 (off) to PER (fully on).
|
||||
hri_tcc_write_WAVE_reg(TCC0, TCC_WAVE_WAVEGEN_NPWM);
|
||||
#ifdef WATCH_INVERT_LED_POLARITY
|
||||
// This is here for the dev board and Pro, which use a common anode LED (instead of common cathode like the actual watch).
|
||||
hri_tcc_set_WAVE_reg(TCC0, (1 << (TCC_WAVE_POL0_Pos + WATCH_RED_TCC_CHANNEL)) |
|
||||
#ifdef WATCH_BLUE_TCC_CHANNEL
|
||||
(1 << (TCC_WAVE_POL0_Pos + WATCH_BLUE_TCC_CHANNEL)) |
|
||||
#endif // WATCH_BLUE_TCC_CHANNEL
|
||||
(1 << (TCC_WAVE_POL0_Pos + WATCH_GREEN_TCC_CHANNEL)));
|
||||
#endif // WATCH_INVERT_LED_POLARITY
|
||||
// The buzzer will set the period depending on the tone it wants to play, but we have to set some period here to
|
||||
// get the LED working. Almost any period will do, tho it should be below 20000 (i.e. 50 Hz) to avoid flickering.
|
||||
hri_tcc_write_PER_reg(TCC0, 1024);
|
||||
// Set the duty cycle of all pins to 0: LED's off, buzzer not buzzing.
|
||||
hri_tcc_write_CC_reg(TCC0, WATCH_BUZZER_TCC_CHANNEL, 0);
|
||||
hri_tcc_write_CC_reg(TCC0, WATCH_RED_TCC_CHANNEL, 0);
|
||||
hri_tcc_write_CC_reg(TCC0, WATCH_GREEN_TCC_CHANNEL, 0);
|
||||
#ifdef WATCH_BLUE_TCC_CHANNEL
|
||||
hri_tcc_write_CC_reg(TCC0, WATCH_BLUE_TCC_CHANNEL, 0);
|
||||
#endif
|
||||
// Enable the TCC
|
||||
hri_tcc_set_CTRLA_ENABLE_bit(TCC0);
|
||||
hri_tcc_wait_for_sync(TCC0, TCC_SYNCBUSY_ENABLE);
|
||||
|
||||
// enable LED PWM pins (the LED driver assumes if the TCC is on, the pins are enabled)
|
||||
gpio_set_pin_direction(RED, GPIO_DIRECTION_OUT);
|
||||
gpio_set_pin_function(RED, WATCH_RED_TCC_PINMUX);
|
||||
gpio_set_pin_direction(GREEN, GPIO_DIRECTION_OUT);
|
||||
gpio_set_pin_function(GREEN, WATCH_GREEN_TCC_PINMUX);
|
||||
#ifdef WATCH_BLUE_TCC_CHANNEL
|
||||
gpio_set_pin_direction(BLUE, GPIO_DIRECTION_OUT);
|
||||
gpio_set_pin_function(BLUE, WATCH_BLUE_TCC_PINMUX);
|
||||
#endif
|
||||
}
|
||||
|
||||
void _watch_disable_tcc(void) {
|
||||
// disable all PWM pins
|
||||
gpio_set_pin_direction(BUZZER, GPIO_DIRECTION_OFF);
|
||||
gpio_set_pin_function(BUZZER, GPIO_PIN_FUNCTION_OFF);
|
||||
gpio_set_pin_direction(RED, GPIO_DIRECTION_OFF);
|
||||
gpio_set_pin_function(RED, GPIO_PIN_FUNCTION_OFF);
|
||||
gpio_set_pin_direction(GREEN, GPIO_DIRECTION_OFF);
|
||||
gpio_set_pin_function(GREEN, GPIO_PIN_FUNCTION_OFF);
|
||||
#ifdef WATCH_BLUE_TCC_CHANNEL
|
||||
gpio_set_pin_direction(BLUE, GPIO_DIRECTION_OFF);
|
||||
gpio_set_pin_function(BLUE, GPIO_PIN_FUNCTION_OFF);
|
||||
#endif
|
||||
|
||||
// disable the TCC
|
||||
hri_tcc_clear_CTRLA_ENABLE_bit(TCC0);
|
||||
hri_mclk_clear_APBCMASK_TCC0_bit(MCLK);
|
||||
}
|
||||
|
||||
void _watch_enable_tc0(void) {
|
||||
// before we init TinyUSB, we are going to need a periodic callback to handle TinyUSB tasks.
|
||||
// TC2 and TC3 are reserved for devices on the 9-pin connector, so let's use TC0.
|
||||
|
||||
Reference in New Issue
Block a user