summaryrefslogtreecommitdiffstats
path: root/watch-library
diff options
context:
space:
mode:
authorjoeycastillo <joeycastillo@utexas.edu>2021-08-30 14:42:11 -0400
committerGitHub <noreply@github.com>2021-08-30 14:42:11 -0400
commiteb3d9b26cbda2d2612f11eb39843b221224f1fa7 (patch)
tree7a514b4d21dd0d2a324a5e1313a144f26bf20799 /watch-library
parentee9cc322d301631c9ff0751d9bed717c6492b6a5 (diff)
parentb0845cc3f1a8234a30c980eccf10e44765e4e105 (diff)
downloadSensor-Watch-eb3d9b26cbda2d2612f11eb39843b221224f1fa7.tar.gz
Sensor-Watch-eb3d9b26cbda2d2612f11eb39843b221224f1fa7.tar.bz2
Sensor-Watch-eb3d9b26cbda2d2612f11eb39843b221224f1fa7.zip
Merge pull request #9 from joeycastillo/usb-refactor
USB refactor / Makefile simplification
Diffstat (limited to 'watch-library')
-rw-r--r--watch-library/config/hpl_oscctrl_config.h2
-rw-r--r--watch-library/config/peripheral_clk_config.h2
-rw-r--r--watch-library/hpl/tc/hpl_tc.c374
-rw-r--r--watch-library/hpl/tc/hpl_tc_base.h160
-rw-r--r--watch-library/hpl/tcc/hpl_tcc.c364
-rw-r--r--watch-library/hpl/tcc/hpl_tcc.h160
-rw-r--r--watch-library/hw/atmel_start_pins.h2
-rw-r--r--watch-library/hw/driver_init.c31
-rwxr-xr-xwatch-library/main.c14
-rw-r--r--watch-library/watch/tusb_config.h91
-rw-r--r--watch-library/watch/watch_buzzer.c24
-rw-r--r--watch-library/watch/watch_buzzer.h33
-rw-r--r--watch-library/watch/watch_extint.h2
-rw-r--r--watch-library/watch/watch_gpio.c6
-rw-r--r--watch-library/watch/watch_gpio.h5
-rw-r--r--watch-library/watch/watch_led.c46
-rw-r--r--watch-library/watch/watch_led.h9
-rw-r--r--watch-library/watch/watch_private.c313
-rw-r--r--watch-library/watch/watch_private.h9
-rw-r--r--watch-library/watch/watch_uart.c2
20 files changed, 493 insertions, 1156 deletions
diff --git a/watch-library/config/hpl_oscctrl_config.h b/watch-library/config/hpl_oscctrl_config.h
index ba2d42e6..405ff207 100644
--- a/watch-library/config/hpl_oscctrl_config.h
+++ b/watch-library/config/hpl_oscctrl_config.h
@@ -147,7 +147,7 @@
// <i> This defines the oscillator frequency (Mhz)
// <id> osc16m_freq
#ifndef CONF_OSC16M_FSEL
-#define CONF_OSC16M_FSEL OSCCTRL_OSC16MCTRL_FSEL_4_Val
+#define CONF_OSC16M_FSEL OSCCTRL_OSC16MCTRL_FSEL_8_Val
#endif
// <q> Oscillator Calibration Control
diff --git a/watch-library/config/peripheral_clk_config.h b/watch-library/config/peripheral_clk_config.h
index 61619b6a..5cd1bb68 100644
--- a/watch-library/config/peripheral_clk_config.h
+++ b/watch-library/config/peripheral_clk_config.h
@@ -61,7 +61,7 @@
* \brief CPU's Clock frequency
*/
#ifndef CONF_CPU_FREQUENCY
-#define CONF_CPU_FREQUENCY 4000000
+#define CONF_CPU_FREQUENCY 8000000
#endif
// <y> RTC Clock Source
diff --git a/watch-library/hpl/tc/hpl_tc.c b/watch-library/hpl/tc/hpl_tc.c
index 38fa8938..e69de29b 100644
--- a/watch-library/hpl/tc/hpl_tc.c
+++ b/watch-library/hpl/tc/hpl_tc.c
@@ -1,374 +0,0 @@
-
-/**
- * \file
- *
- * \brief SAM TC
- *
- * Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.
- *
- * \asf_license_start
- *
- * \page License
- *
- * Subject to your compliance with these terms, you may use Microchip
- * software and any derivatives exclusively with Microchip products.
- * It is your responsibility to comply with third party license terms applicable
- * to your use of third party software (including open source software) that
- * may accompany Microchip software.
- *
- * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
- * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
- * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
- * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
- * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
- * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
- * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
- * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
- * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
- * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
- * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
- *
- * \asf_license_stop
- *
- */
-
-#include <hpl_pwm.h>
-#include <hpl_tc_config.h>
-#include <hpl_timer.h>
-#include <utils.h>
-#include <utils_assert.h>
-#include <hpl_tc_base.h>
-
-#ifndef CONF_TC0_ENABLE
-#define CONF_TC0_ENABLE 0
-#endif
-#ifndef CONF_TC1_ENABLE
-#define CONF_TC1_ENABLE 0
-#endif
-#ifndef CONF_TC2_ENABLE
-#define CONF_TC2_ENABLE 0
-#endif
-#ifndef CONF_TC3_ENABLE
-#define CONF_TC3_ENABLE 0
-#endif
-#ifndef CONF_TC4_ENABLE
-#define CONF_TC4_ENABLE 0
-#endif
-#ifndef CONF_TC5_ENABLE
-#define CONF_TC5_ENABLE 0
-#endif
-#ifndef CONF_TC6_ENABLE
-#define CONF_TC6_ENABLE 0
-#endif
-#ifndef CONF_TC7_ENABLE
-#define CONF_TC7_ENABLE 0
-#endif
-
-/**
- * \brief Macro is used to fill usart configuration structure based on its
- * number
- *
- * \param[in] n The number of structures
- */
-#define TC_CONFIGURATION(n) \
- { \
- n, TC##n##_IRQn, \
- TC_CTRLA_MODE(CONF_TC##n##_MODE) | TC_CTRLA_PRESCSYNC(CONF_TC##n##_PRESCSYNC) \
- | (CONF_TC##n##_RUNSTDBY << TC_CTRLA_RUNSTDBY_Pos) | (CONF_TC##n##_ONDEMAND << TC_CTRLA_ONDEMAND_Pos) \
- | TC_CTRLA_PRESCALER(CONF_TC##n##_PRESCALER) | (CONF_TC##n##_ALOCK << TC_CTRLA_ALOCK_Pos), \
- (CONF_TC##n##_OVFEO << TC_EVCTRL_OVFEO_Pos) | (CONF_TC##n##_TCEI << TC_EVCTRL_TCEI_Pos) \
- | (CONF_TC##n##_TCINV << TC_EVCTRL_TCINV_Pos) | (CONF_TC##n##_EVACT << TC_EVCTRL_EVACT_Pos) \
- | (CONF_TC##n##_MCEO0 << TC_EVCTRL_MCEO0_Pos) | (CONF_TC##n##_MCEO1 << TC_EVCTRL_MCEO1_Pos), \
- (CONF_TC##n##_DBGRUN << TC_DBGCTRL_DBGRUN_Pos), CONF_TC##n##_PER, CONF_TC##n##_CC0, CONF_TC##n##_CC1, \
- }
-/**
- * \brief TC configuration type
- */
-struct tc_configuration {
- uint8_t number;
- IRQn_Type irq;
- hri_tc_ctrla_reg_t ctrl_a;
- hri_tc_evctrl_reg_t event_ctrl;
- hri_tc_dbgctrl_reg_t dbg_ctrl;
- hri_tccount8_per_reg_t per;
- hri_tccount32_cc_reg_t cc0;
- hri_tccount32_cc_reg_t cc1;
-};
-
-/**
- * \brief Array of TC configurations
- */
-static struct tc_configuration _tcs[] = {
-#if CONF_TC0_ENABLE == 1
- TC_CONFIGURATION(0),
-#endif
-#if CONF_TC1_ENABLE == 1
- TC_CONFIGURATION(1),
-#endif
-#if CONF_TC2_ENABLE == 1
- TC_CONFIGURATION(2),
-#endif
-#if CONF_TC3_ENABLE == 1
- TC_CONFIGURATION(3),
-#endif
-#if CONF_TC4_ENABLE == 1
- TC_CONFIGURATION(4),
-#endif
-#if CONF_TC5_ENABLE == 1
- TC_CONFIGURATION(5),
-#endif
-#if CONF_TC6_ENABLE == 1
- TC_CONFIGURATION(6),
-#endif
-#if CONF_TC7_ENABLE == 1
- TC_CONFIGURATION(7),
-#endif
-};
-/**
- * \brief Set of pointer to hal_pwm helper functions
- */
-static struct _pwm_hpl_interface _tc_pwm_functions = {
- _tc_pwm_init,
- _tc_pwm_deinit,
- _tc_start_pwm,
- _tc_stop_pwm,
- _tc_set_pwm_param,
- _tc_is_pwm_enabled,
- _tc_pwm_get_period,
- _tc_pwm_get_duty,
- _tc_pwm_set_irq_state,
-};
-
-static struct _pwm_device *_tc3_dev = NULL;
-
-static int8_t get_tc_index(const void *const hw);
-static void _tc_init_irq_param(const void *const hw, void *dev);
-static inline uint8_t _get_hardware_offset(const void *const hw);
-/**
- * \brief Initialize TC for PWM mode
- */
-int32_t _tc_pwm_init(struct _pwm_device *const device, void *const hw)
-{
- int8_t i = get_tc_index(hw);
- device->hw = hw;
-
- if (!hri_tc_is_syncing(hw, TC_SYNCBUSY_SWRST)) {
- if (hri_tc_get_CTRLA_reg(hw, TC_CTRLA_ENABLE)) {
- hri_tc_clear_CTRLA_ENABLE_bit(hw);
- hri_tc_wait_for_sync(hw, TC_SYNCBUSY_ENABLE);
- }
- hri_tc_write_CTRLA_reg(hw, TC_CTRLA_SWRST);
- }
- hri_tc_wait_for_sync(hw, TC_SYNCBUSY_SWRST);
-
- hri_tc_write_CTRLA_reg(hw, _tcs[i].ctrl_a);
- hri_tc_write_DBGCTRL_reg(hw, _tcs[i].dbg_ctrl);
- hri_tc_write_EVCTRL_reg(hw, _tcs[i].event_ctrl);
- hri_tc_write_WAVE_reg(hw, TC_WAVE_WAVEGEN_MPWM_Val);
-
- if ((_tcs[i].ctrl_a & TC_CTRLA_MODE_Msk) == TC_CTRLA_MODE_COUNT32) {
- hri_tccount32_write_CC_reg(hw, 0, _tcs[i].cc0);
- hri_tccount32_write_CC_reg(hw, 1, _tcs[i].cc1);
- } else if ((_tcs[i].ctrl_a & TC_CTRLA_MODE_Msk) == TC_CTRLA_MODE_COUNT16) {
- hri_tccount16_write_CC_reg(hw, 0, (uint16_t)_tcs[i].cc0);
- hri_tccount16_write_CC_reg(hw, 1, (uint16_t)_tcs[i].cc1);
- } else {
- /* 8-bit resolution is not accepted by duty cycle control */
- return -1;
- }
-
- _tc_init_irq_param(hw, (void *)device);
- NVIC_DisableIRQ(_tcs[i].irq);
- NVIC_ClearPendingIRQ(_tcs[i].irq);
- NVIC_EnableIRQ(_tcs[i].irq);
-
- return 0;
-}
-/**
- * \brief De-initialize TC for PWM mode
- */
-void _tc_pwm_deinit(struct _pwm_device *const device)
-{
- void *const hw = device->hw;
- int8_t i = get_tc_index(hw);
- ASSERT(ARRAY_SIZE(_tcs));
-
- NVIC_DisableIRQ(_tcs[i].irq);
-
- hri_tc_clear_CTRLA_ENABLE_bit(hw);
- hri_tc_set_CTRLA_SWRST_bit(hw);
-}
-/**
- * \brief Start PWM
- */
-void _tc_start_pwm(struct _pwm_device *const device)
-{
- hri_tc_set_CTRLA_ENABLE_bit(device->hw);
-}
-/**
- * \brief Stop PWM
- */
-void _tc_stop_pwm(struct _pwm_device *const device)
-{
- hri_tc_clear_CTRLA_ENABLE_bit(device->hw);
-}
-/**
- * \brief Set PWM parameter
- */
-void _tc_set_pwm_param(struct _pwm_device *const device, const pwm_period_t period, const pwm_period_t duty_cycle)
-{
- void *const hw = device->hw;
- int8_t i = get_tc_index(hw);
-
- _tcs[i].cc0 = period;
- _tcs[i].cc1 = duty_cycle;
- if ((_tcs[i].ctrl_a & TC_CTRLA_MODE_Msk) == TC_CTRLA_MODE_COUNT32) {
- hri_tccount32_write_CC_reg(hw, 0, _tcs[i].cc0);
- hri_tccount32_write_CC_reg(hw, 1, _tcs[i].cc1);
- } else {
- hri_tccount16_write_CC_reg(hw, 0, _tcs[i].cc0);
- hri_tccount16_write_CC_reg(hw, 1, _tcs[i].cc1);
- }
-}
-/**
- * \brief Get pwm waveform period value
- */
-pwm_period_t _tc_pwm_get_period(const struct _pwm_device *const device)
-{
- void *const hw = device->hw;
- int8_t i = get_tc_index(hw);
-
- if ((_tcs[i].ctrl_a & TC_CTRLA_MODE_Msk) == TC_CTRLA_MODE_COUNT32) {
- return (pwm_period_t)(hri_tccount32_read_CC_reg(hw, 0));
- } else {
- return (pwm_period_t)(hri_tccount16_read_CC_reg(hw, 0));
- }
-}
-/**
- * \brief Get pwm waveform duty cycle
- */
-uint32_t _tc_pwm_get_duty(const struct _pwm_device *const device)
-{
- void *const hw = device->hw;
- int8_t i = get_tc_index(hw);
- uint32_t per;
- uint32_t duty_cycle;
-
- if ((_tcs[i].ctrl_a & TC_CTRLA_MODE_Msk) == TC_CTRLA_MODE_COUNT32) {
- per = hri_tccount32_read_CC_reg(hw, 0);
- duty_cycle = hri_tccount32_read_CC_reg(hw, 1);
- } else {
- per = hri_tccount16_read_CC_reg(hw, 0);
- duty_cycle = hri_tccount16_read_CC_reg(hw, 1);
- }
-
- return ((duty_cycle * 1000) / per);
-}
-/**
- * \brief Check if PWM is running
- */
-bool _tc_is_pwm_enabled(const struct _pwm_device *const device)
-{
- return hri_tc_get_CTRLA_ENABLE_bit(device->hw);
-}
-/**
- * \brief Enable/disable PWM interrupt
- */
-void _tc_pwm_set_irq_state(struct _pwm_device *const device, const enum _pwm_callback_type type, const bool disable)
-{
- ASSERT(device);
-
- if (PWM_DEVICE_PERIOD_CB == type) {
- hri_tc_write_INTEN_OVF_bit(device->hw, disable);
- } else if (PWM_DEVICE_ERROR_CB == type) {
- hri_tc_write_INTEN_ERR_bit(device->hw, disable);
- }
-}
-
-/**
- * \brief Retrieve timer helper functions
- */
-struct _timer_hpl_interface *_tc_get_timer(void)
-{
- return NULL;
-}
-
-/**
- * \brief Retrieve pwm helper functions
- */
-struct _pwm_hpl_interface *_tc_get_pwm(void)
-{
- return &_tc_pwm_functions;
-}
-/**
- * \internal TC interrupt handler for PWM
- *
- * \param[in] instance TC instance number
- */
-static void tc_pwm_interrupt_handler(struct _pwm_device *device)
-{
- void *const hw = device->hw;
-
- if (hri_tc_get_interrupt_OVF_bit(hw)) {
- hri_tc_clear_interrupt_OVF_bit(hw);
- if (NULL != device->callback.pwm_period_cb) {
- device->callback.pwm_period_cb(device);
- }
- }
- if (hri_tc_get_INTEN_ERR_bit(hw)) {
- hri_tc_clear_interrupt_ERR_bit(hw);
- if (NULL != device->callback.pwm_error_cb) {
- device->callback.pwm_error_cb(device);
- }
- }
-}
-
-/**
- * \brief TC interrupt handler
- */
-void TC3_Handler(void)
-{
- tc_pwm_interrupt_handler(_tc3_dev);
-}
-
-/**
- * \internal Retrieve TC index
- *
- * \param[in] hw The pointer to hardware instance
- *
- * \return The index of TC configuration
- */
-static int8_t get_tc_index(const void *const hw)
-{
- uint8_t index = _get_hardware_offset(hw);
- uint8_t i;
-
- for (i = 0; i < ARRAY_SIZE(_tcs); i++) {
- if (_tcs[i].number == index) {
- return i;
- }
- }
-
- ASSERT(false);
- return -1;
-}
-
-/**
- * \brief Init irq param with the given tc hardware instance
- */
-static void _tc_init_irq_param(const void *const hw, void *dev)
-{
- if (hw == TC3) {
- _tc3_dev = (struct _pwm_device *)dev;
- }
-}
-
-/**
- * \internal Retrieve TC hardware index
- *
- * \param[in] hw The pointer to hardware instance
- */
-static inline uint8_t _get_hardware_offset(const void *const hw)
-{
- return (((uint32_t)hw - (uint32_t)TC0) >> 10);
-}
diff --git a/watch-library/hpl/tc/hpl_tc_base.h b/watch-library/hpl/tc/hpl_tc_base.h
index d602ae66..e69de29b 100644
--- a/watch-library/hpl/tc/hpl_tc_base.h
+++ b/watch-library/hpl/tc/hpl_tc_base.h
@@ -1,160 +0,0 @@
-/**
- * \file
- *
- * \brief SAM Timer/Counter
- *
- * Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.
- *
- * \asf_license_start
- *
- * \page License
- *
- * Subject to your compliance with these terms, you may use Microchip
- * software and any derivatives exclusively with Microchip products.
- * It is your responsibility to comply with third party license terms applicable
- * to your use of third party software (including open source software) that
- * may accompany Microchip software.
- *
- * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
- * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
- * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
- * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
- * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
- * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
- * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
- * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
- * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
- * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
- * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
- *
- * \asf_license_stop
- */
-
-#ifndef _HPL_TC_BASE_H_INCLUDED
-#define _HPL_TC_BASE_H_INCLUDED
-
-#include <hpl_timer.h>
-#include <hpl_pwm.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/**
- * \addtogroup tc_group TC Hardware Proxy Layer
- *
- * \section tc_hpl_rev Revision History
- * - v0.0.0.1 Initial Commit
- *
- *@{
- */
-
-/**
- * \name HPL functions
- */
-//@{
-
-/**
- * \brief Retrieve timer helper functions
- *
- * \return A pointer to set of timer helper functions
- */
-struct _timer_hpl_interface *_tc_get_timer(void);
-/**
- * \brief Initialize TC for PWM
- *
- * This function does low level TC configuration.
- *
- * \param[in] device The pointer to TC device instance
- * \param[in] hw The pointer to hardware instance
- *
- * \return Initialization status.
- */
-int32_t _tc_pwm_init(struct _pwm_device *const device, void *const hw);
-
-/**
- * \brief De-initialize TC for PWM
- *
- * \param[in] device The pointer to TC device instance
- */
-void _tc_pwm_deinit(struct _pwm_device *const device);
-
-/**
- * \brief Retrieve offset of the given tc hardware instance
- *
- * \param[in] device The pointer to TC device instance
- *
- * \return The offset of the given tc hardware instance
- */
-uint8_t _tc_pwm_get_hardware_offset(const struct _pwm_device *const device);
-
-/**
- * \brief Start PWM
- *
- * \param[in] device The pointer to TC device instance
- */
-void _tc_start_pwm(struct _pwm_device *const device);
-
-/**
- * \brief Stop PWM
- *
- * \param[in] device The pointer to TC device instance
- */
-void _tc_stop_pwm(struct _pwm_device *const device);
-
-/**
- * \brief Check if PWM is running
- *
- * \param[in] device The pointer to TC device instance
- *
- * \return Check status.
- * \retval true The given timer is running
- * \retval false The given timer is not running
- */
-bool _tc_is_pwm_enabled(const struct _pwm_device *const device);
-
-/**
- * \brief Set PWM parameter
- * \param[in] device The pointer to TC device instance
- * \param[in] period Total period of one PWM cycle.
- * \param[in] duty_cycle Period of PWM first half during one cycle.
- */
-void _tc_set_pwm_param(struct _pwm_device *const device, const pwm_period_t period, const pwm_period_t duty_cycle);
-
-/**
- * \brief Get pwm waveform period value
- * \param[in] device The pointer to TC device instance
- * \return Period value.
- */
-pwm_period_t _tc_pwm_get_period(const struct _pwm_device *const device);
-
-/**
- * \brief Get pwm waveform duty cycle value
- * \param[in] device The pointer to TC device instance
- * \return Duty cycle value
- */
-uint32_t _tc_pwm_get_duty(const struct _pwm_device *const device);
-
-/**
- * \brief Enable/disable PWM interrupt
- *
- * param[in] device The pointer to PWM device instance
- * param[in] type The type of interrupt to disable/enable if applicable
- * param[in] disable Enable or disable
- */
-void _tc_pwm_set_irq_state(struct _pwm_device *const device, const enum _pwm_callback_type type, const bool disable);
-
-/**
- * \brief Retrieve pwm helper functions
- *
- * \return A pointer to set of pwm helper functions
- */
-struct _pwm_hpl_interface *_tc_get_pwm(void);
-
-//@}
-/**@}*/
-
-#ifdef __cplusplus
-}
-#endif
-#endif /* _HPL_TC_BASE_H_INCLUDED */
diff --git a/watch-library/hpl/tcc/hpl_tcc.c b/watch-library/hpl/tcc/hpl_tcc.c
index 52fc6f3c..e69de29b 100644
--- a/watch-library/hpl/tcc/hpl_tcc.c
+++ b/watch-library/hpl/tcc/hpl_tcc.c
@@ -1,364 +0,0 @@
-/**
- * \file
- *
- * \brief SAM TCC
- *
- * Copyright (c) 2014-2019 Microchip Technology Inc. and its subsidiaries.
- *
- * \asf_license_start
- *
- * \page License
- *
- * Subject to your compliance with these terms, you may use Microchip
- * software and any derivatives exclusively with Microchip products.
- * It is your responsibility to comply with third party license terms applicable
- * to your use of third party software (including open source software) that
- * may accompany Microchip software.
- *
- * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
- * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
- * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
- * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
- * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
- * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
- * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
- * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
- * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
- * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
- * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
- *
- * \asf_license_stop
- *
- */
-
-#include <compiler.h>
-#include <hpl_pwm.h>
-#include <hpl_tcc.h>
-#include <hpl_tcc_config.h>
-#include <hpl_timer.h>
-#include <utils.h>
-#include <utils_assert.h>
-
-/**
- * \brief TCC configuration type
- */
-struct tcc_cfg {
- void * hw; /*!< instance of TCC */
- IRQn_Type irq;
- hri_tcc_ctrla_reg_t ctrl_a;
- hri_tcc_ctrlbset_reg_t ctrl_b;
- hri_tcc_dbgctrl_reg_t dbg_ctrl;
- hri_tcc_evctrl_reg_t event_ctrl;
- hri_tcc_cc_reg_t cc0;
- hri_tcc_cc_reg_t cc1;
- hri_tcc_cc_reg_t cc2;
- hri_tcc_cc_reg_t cc3;
- hri_tcc_per_reg_t per;
-};
-/**
- * \brief pwm configuration type
- */
-struct tcc_pwm_cfg {
- void * hw; /*!< instance of TCC */
- IRQn_Type irq;
- uint8_t sel_ch;
- uint32_t period;
- uint32_t duty_cycle;
- uint32_t wave;
-};
-
-/**
- * \internal Retrieve configuration
- *
- * \param[in] hw The pointer of TCC base address
- *
- * \return The configuration
- */
-static struct tcc_cfg *_get_tcc_cfg(void *hw);
-
-/**
- * \brief Array of TCC configurations
- */
-static struct tcc_cfg _cfgs[1] = {
- {(void *)TCC0,
- TCC0_IRQn,
- CONF_TCC0_CTRLA,
- CONF_TCC0_CTRLB,
- CONF_TCC0_DBGCTRL,
- CONF_TCC0_EVCTRL,
- CONF_TCC0_CC0,
- CONF_TCC0_CC1,
- CONF_TCC0_CC2,
- CONF_TCC0_CC3,
- CONF_TCC0_PER},
-};
-
-/**
- * \internal Retrieve configuration
- *
- * \param[in] hw The pointer of TCC base address
- *
- * \return The configuration
- */
-static struct tcc_pwm_cfg *_get_tcc_pwm_cfg(void *hw);
-
-/**
- * \brief Array of PWM configurations
- */
-static struct tcc_pwm_cfg _cfgs_pwm[1] = {
- {(void *)TCC0,
- TCC0_IRQn,
- CONF_TCC0_SEL_CH,
- CONF_TCC0_PER_REG,
- CONF_TCC0_CCX_REG,
- (CONF_TCC0_WAVEGEN << TCC_WAVE_WAVEGEN_Pos)},
-};
-/* Renamed access REG name PERB -> PERBUF */
-#define hri_tcc_write_PERB_reg hri_tcc_write_PERBUF_reg
-#define hri_tcc_read_PERB_reg hri_tcc_read_PERBUF_reg
-
-/** Renamed access REG name CCB -> CCBUF */
-#define hri_tcc_write_CCB_reg hri_tcc_write_CCBUF_reg
-#define hri_tcc_read_CCB_reg hri_tcc_read_CCBUF_reg
-
-static struct _pwm_device *_tcc0_dev = NULL;
-
-/**
- * \brief Set of pointer to hal_pwm helper functions
- */
-static struct _pwm_hpl_interface _tcc_pwm_functions = {
- _tcc_pwm_init,
- _tcc_pwm_deinit,
- _tcc_start_pwm,
- _tcc_stop_pwm,
- _tcc_set_pwm_param,
- _tcc_is_pwm_enabled,
- _tcc_pwm_get_period,
- _tcc_pwm_get_duty,
- _tcc_pwm_set_irq_state,
-};
-
-/**
- * \brief Init irq param with the given tcc hardware instance
- */
-static void _tcc_init_irq_param(const void *const hw, void *dev)
-{
- if (hw == TCC0) {
- _tcc0_dev = (struct _pwm_device *)dev;
- }
-}
-/**
- * \brief Initialize TCC for PWM mode
- */
-int32_t _tcc_pwm_init(struct _pwm_device *const device, void *const hw)
-{
- struct tcc_cfg *cfg = _get_tcc_cfg(hw);
- if (cfg == NULL) {
- return ERR_NOT_FOUND;
- }
- struct tcc_pwm_cfg *cfg_pwm = _get_tcc_pwm_cfg(hw);
- if (cfg_pwm == NULL) {
- return ERR_NOT_FOUND;
- }
-
- device->hw = hw;
-
- if (!hri_tcc_is_syncing(hw, TCC_SYNCBUSY_SWRST)) {
- if (hri_tcc_get_CTRLA_reg(hw, TCC_CTRLA_ENABLE)) {
- hri_tcc_clear_CTRLA_ENABLE_bit(hw);
- hri_tcc_wait_for_sync(hw, TCC_SYNCBUSY_ENABLE);
- }
- hri_tcc_write_CTRLA_reg(hw, TCC_CTRLA_SWRST);
- }
- hri_tcc_wait_for_sync(hw, TCC_SYNCBUSY_SWRST);
-
- hri_tcc_write_CTRLA_reg(hw, cfg->ctrl_a);
- hri_tcc_set_CTRLB_reg(hw, cfg->ctrl_b);
- hri_tcc_write_DBGCTRL_reg(hw, cfg->dbg_ctrl);
- hri_tcc_write_EVCTRL_reg(hw, cfg->event_ctrl);
-
- hri_tcc_write_WAVE_reg(hw, cfg_pwm->wave);
- hri_tcc_write_PER_reg(hw, cfg_pwm->period);
- cfg->per = cfg_pwm->period;
- switch (cfg_pwm->sel_ch) {
- case 0:
- cfg->cc0 = cfg_pwm->duty_cycle;
- hri_tcc_write_CC_reg(hw, 0, cfg->cc0);
- break;
- case 1:
- cfg->cc1 = cfg_pwm->duty_cycle;
- hri_tcc_write_CC_reg(hw, 1, cfg->cc1);
- break;
- case 2:
- cfg->cc2 = cfg_pwm->duty_cycle;
- hri_tcc_write_CC_reg(hw, 2, cfg->cc2);
- break;
- case 3:
- cfg->cc3 = cfg_pwm->duty_cycle;
- hri_tcc_write_CC_reg(hw, 3, cfg->cc3);
- break;
- default:
- return ERR_NO_RESOURCE;
- break;
- }
- hri_tcc_clear_CTRLB_LUPD_bit(hw);
-
- _tcc_init_irq_param(hw, (void *)device);
- NVIC_DisableIRQ((IRQn_Type)cfg_pwm->irq);
- NVIC_ClearPendingIRQ((IRQn_Type)cfg_pwm->irq);
- NVIC_EnableIRQ((IRQn_Type)cfg_pwm->irq);
-
- return ERR_NONE;
-}
-/**
- * \brief De-initialize TCC for PWM mode
- */
-void _tcc_pwm_deinit(struct _pwm_device *const device)
-{
- void *const hw = device->hw;
- struct tcc_pwm_cfg *cfg_pwm = _get_tcc_pwm_cfg(hw);
- if (cfg_pwm != NULL) {
- NVIC_DisableIRQ((IRQn_Type)cfg_pwm->irq);
- hri_tcc_clear_CTRLA_ENABLE_bit(hw);
- hri_tcc_set_CTRLA_SWRST_bit(hw);
- }
-}
-/**
- * \brief Start PWM
- */
-void _tcc_start_pwm(struct _pwm_device *const device)
-{
- hri_tcc_set_CTRLA_ENABLE_bit(device->hw);
-}
-/**
- * \brief Stop PWM
- */
-void _tcc_stop_pwm(struct _pwm_device *const device)
-{
- hri_tcc_clear_CTRLA_ENABLE_bit(device->hw);
-}
-/**
- * \brief Set PWM parameter
- */
-void _tcc_set_pwm_param(struct _pwm_device *const device, const pwm_period_t period, const pwm_period_t duty_cycle)
-{
- void *const hw = device->hw;
- struct tcc_pwm_cfg *cfg_pwm = _get_tcc_pwm_cfg(hw);
- if (cfg_pwm != NULL) {
- hri_tcc_write_PERB_reg(hw, period);
- hri_tcc_write_CCB_reg(hw, cfg_pwm->sel_ch, duty_cycle);
- ;
- }
-}
-/**
- * \brief Get pwm waveform period value
- */
-pwm_period_t _tcc_pwm_get_period(const struct _pwm_device *const device)
-{
- return (pwm_period_t)(hri_tcc_read_PERB_reg(device->hw));
-}
-/**
- * \brief Get pwm waveform duty cycle
- */
-uint32_t _tcc_pwm_get_duty(const struct _pwm_device *const device)
-{
- void *const hw = device->hw;
- struct tcc_pwm_cfg *cfg_pwm = _get_tcc_pwm_cfg(hw);
- if (cfg_pwm == NULL) {
- return ERR_NOT_FOUND;
- }
- uint32_t per = hri_tcc_read_PERB_reg(hw);
- uint32_t duty_cycle = hri_tcc_read_CCB_reg(hw, cfg_pwm->sel_ch);
-
- return ((duty_cycle * 1000) / per);
-}
-/**
- * \brief Check if PWM is running
- */
-bool _tcc_is_pwm_enabled(const struct _pwm_device *const device)
-{
- return hri_tcc_get_CTRLA_ENABLE_bit(device->hw);
-}
-/**
- * \brief Enable/disable PWM interrupt
- */
-void _tcc_pwm_set_irq_state(struct _pwm_device *const device, const enum _pwm_callback_type type, const bool disable)
-{
- ASSERT(device);
-
- if (PWM_DEVICE_PERIOD_CB == type) {
- hri_tcc_write_INTEN_OVF_bit(device->hw, disable);
- } else if (PWM_DEVICE_ERROR_CB == type) {
- hri_tcc_write_INTEN_ERR_bit(device->hw, disable);
- }
-}
-
-/**
- * \brief Retrieve timer helper functions
- */
-struct _timer_hpl_interface *_tcc_get_timer(void)
-{
- return NULL;
-}
-
-/**
- * \brief Retrieve pwm helper functions
- */
-struct _pwm_hpl_interface *_tcc_get_pwm(void)
-{
- return &_tcc_pwm_functions;
-}
-/**
- * \internal TC interrupt handler for PWM
- *
- * \param[in] instance TC instance number
- */
-static void tcc_pwm_interrupt_handler(struct _pwm_device *device)
-{
- void *const hw = device->hw;
-
- if (hri_tcc_get_interrupt_OVF_bit(hw)) {
- hri_tcc_clear_interrupt_OVF_bit(hw);
- if (NULL != device->callback.pwm_period_cb) {
- device->callback.pwm_period_cb(device);
- }
- }
- if (hri_tcc_get_INTEN_ERR_bit(hw)) {
- hri_tcc_clear_interrupt_ERR_bit(hw);
- if (NULL != device->callback.pwm_error_cb) {
- device->callback.pwm_error_cb(device);
- }
- }
-}
-
-/**
- * \brief TCC interrupt handler
- */
-void TCC0_Handler(void)
-{
- tcc_pwm_interrupt_handler(_tcc0_dev);
-}
-
-static struct tcc_cfg *_get_tcc_cfg(void *hw)
-{
- uint8_t i;
-
- for (i = 0; i < ARRAY_SIZE(_cfgs); i++) {
- if (_cfgs[i].hw == hw) {
- return &(_cfgs[i]);
- }
- }
- return NULL;
-}
-
-static struct tcc_pwm_cfg *_get_tcc_pwm_cfg(void *hw)
-{
- uint8_t i;
-
- for (i = 0; i < ARRAY_SIZE(_cfgs_pwm); i++) {
- if (_cfgs_pwm[i].hw == hw) {
- return &(_cfgs_pwm[i]);
- }
- }
- return NULL;
-}
diff --git a/watch-library/hpl/tcc/hpl_tcc.h b/watch-library/hpl/tcc/hpl_tcc.h
index 6ccf04f3..e69de29b 100644
--- a/watch-library/hpl/tcc/hpl_tcc.h
+++ b/watch-library/hpl/tcc/hpl_tcc.h
@@ -1,160 +0,0 @@
-/**
- * \file
- *
- * \brief SAM Timer/Counter for Control Applications
- *
- * Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.
- *
- * \asf_license_start
- *
- * \page License
- *
- * Subject to your compliance with these terms, you may use Microchip
- * software and any derivatives exclusively with Microchip products.
- * It is your responsibility to comply with third party license terms applicable
- * to your use of third party software (including open source software) that
- * may accompany Microchip software.
- *
- * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
- * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
- * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
- * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
- * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
- * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
- * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
- * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
- * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
- * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
- * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
- *
- * \asf_license_stop
- */
-
-#ifndef _HPL_TCC_V101_BASE_H_INCLUDED
-#define _HPL_TCC_V101_BASE_H_INCLUDED
-
-#include <hpl_timer.h>
-#include <hpl_pwm.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/**
- * \addtogroup tcc_group TCC Low Level Driver Helpers
- *
- * \section tcc_helpers_rev Revision History
- * - v0.0.0.1 Initial Commit
- *
- *@{
- */
-
-/**
- * \name HPL functions
- */
-//@{
-
-/**
- * \brief Retrieve timer helper functions
- *
- * \return A pointer to set of timer helper functions
- */
-struct _timer_hpl_interface *_tcc_get_timer(void);
-/**
- * \brief Initialize TCC for PWM
- *
- * This function does low level TCC configuration.
- *
- * \param[in] device The pointer to PWM device instance
- * \param[in] hw The pointer to hardware instance
- *
- * \return Initialization status.
- */
-int32_t _tcc_pwm_init(struct _pwm_device *const device, void *const hw);
-
-/**
- * \brief De-initialize TCC for PWM
- *
- * \param[in] device The pointer to TCC device instance
- */
-void _tcc_pwm_deinit(struct _pwm_device *const device);
-
-/**
- * \brief Retrieve offset of the given tcc hardware instance
- *
- * \param[in] device The pointer to TCC device instance
- *
- * \return The offset of the given tcc hardware instance
- */
-uint8_t _tcc_pwm_get_hardware_offset(const struct _pwm_device *const device);
-
-/**
- * \brief Start PWM
- *
- * \param[in] device The pointer to TCC device instance
- */
-void _tcc_start_pwm(struct _pwm_device *const device);
-
-/**
- * \brief Stop PWM
- *
- * \param[in] device The pointer to TCC device instance
- */
-void _tcc_stop_pwm(struct _pwm_device *const device);
-
-/**
- * \brief Check if PWM is running
- *
- * \param[in] device The pointer to TCC device instance
- *
- * \return Check status.
- * \retval true The given pwm is running
- * \retval false The given pwm is not running
- */
-bool _tcc_is_pwm_enabled(const struct _pwm_device *const device);
-
-/**
- * \brief Set PWM parameter
- * \param[in] device The pointer to TCC device instance
- * \param[in] period Total period of one PWM cycle.
- * \param[in] duty_cycle Period of PWM first half during one cycle.
- */
-void _tcc_set_pwm_param(struct _pwm_device *const device, const pwm_period_t period, const pwm_period_t duty_cycle);
-
-/**
- * \brief Get pwm waveform period value
- * \param[in] device The pointer to TCC device instance
- * \return Period value.
- */
-pwm_period_t _tcc_pwm_get_period(const struct _pwm_device *const device);
-
-/**
- * \brief Get pwm waveform duty cycle value
- * \param[in] device The pointer to TCC device instance
- * \return Duty cycle value
- */
-uint32_t _tcc_pwm_get_duty(const struct _pwm_device *const device);
-
-/**
- * \brief Enable/disable PWM interrupt
- *
- * param[in] device The pointer to PWM device instance
- * param[in] type The type of interrupt to disable/enable if applicable
- * param[in] disable Enable or disable
- */
-void _tcc_pwm_set_irq_state(struct _pwm_device *const device, const enum _pwm_callback_type type, const bool disable);
-
-/**
- * \brief Retrieve pwm helper functions
- *
- * \return A pointer to set of pwm helper functions
- */
-struct _pwm_hpl_interface *_tcc_get_pwm(void);
-
-//@}
-/**@}*/
-
-#ifdef __cplusplus
-}
-#endif
-#endif /* _HPL_TCC_V101_BASE_H_INCLUDED */
diff --git a/watch-library/hw/atmel_start_pins.h b/watch-library/hw/atmel_start_pins.h
index 7a3a7d78..14df95e1 100644
--- a/watch-library/hw/atmel_start_pins.h
+++ b/watch-library/hw/atmel_start_pins.h
@@ -22,7 +22,7 @@
#define GPIO_PIN_FUNCTION_H 7
#define GPIO_PIN_FUNCTION_I 8
-#define VBUS_DET GPIO(GPIO_PORTA, 2)
+#define VBUS_DET GPIO(GPIO_PORTA, 3)
#define SEG1 GPIO(GPIO_PORTA, 4)
#define SEG2 GPIO(GPIO_PORTA, 5)
#define SEG3 GPIO(GPIO_PORTA, 6)
diff --git a/watch-library/hw/driver_init.c b/watch-library/hw/driver_init.c
index 6d910d22..daf3901d 100644
--- a/watch-library/hw/driver_init.c
+++ b/watch-library/hw/driver_init.c
@@ -83,37 +83,6 @@ void delay_driver_init(void) {
delay_init(SysTick);
}
-void PWM_0_PORT_init(void) {
- gpio_set_pin_function(RED, PINMUX_PA20E_TC3_WO0);
- gpio_set_pin_function(GREEN, PINMUX_PA21E_TC3_WO1);
-}
-
-void PWM_0_CLOCK_init(void) {
- hri_mclk_set_APBCMASK_TC3_bit(MCLK);
- hri_gclk_write_PCHCTRL_reg(GCLK, TC3_GCLK_ID, CONF_GCLK_TC3_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
-}
-
-void PWM_0_init(void) {
- PWM_0_CLOCK_init();
- PWM_0_PORT_init();
- pwm_init(&PWM_0, TC3, _tc_get_pwm());
-}
-
-void PWM_1_PORT_init(void) {
- gpio_set_pin_function(BUZZER, PINMUX_PA27F_TCC0_WO5);
-}
-
-void PWM_1_CLOCK_init(void) {
- hri_mclk_set_APBCMASK_TCC0_bit(MCLK);
- hri_gclk_write_PCHCTRL_reg(GCLK, TCC0_GCLK_ID, CONF_GCLK_TCC0_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
-}
-
-void PWM_1_init(void) {
- PWM_1_CLOCK_init();
- PWM_1_PORT_init();
- pwm_init(&PWM_1, TCC0, _tcc_get_pwm());
-}
-
void SEGMENT_LCD_0_PORT_init(void) {
gpio_set_pin_function(COM0, PINMUX_PB06B_SLCD_LP0);
gpio_set_pin_function(COM1, PINMUX_PB07B_SLCD_LP1);
diff --git a/watch-library/main.c b/watch-library/main.c
index c23ceac6..af34d850 100755
--- a/watch-library/main.c
+++ b/watch-library/main.c
@@ -32,11 +32,21 @@
#include "hal_init.h"
#include "atmel_start_pins.h"
#include "watch.h"
+#include "tusb.h"
int main(void) {
// ASF code. Initialize the MCU with configuration options from Atmel Studio.
init_mcu();
+ // check if we are plugged into USB power.
+ watch_enable_digital_input(VBUS_DET);
+ watch_enable_pull_down(VBUS_DET);
+ if (watch_get_pin_level(VBUS_DET)) {
+ // if so, enable USB functionality.
+ _watch_enable_usb();
+ }
+ watch_disable_digital_input(VBUS_DET);
+
// User code. Give the app a chance to initialize its data structures and state.
app_init();
@@ -58,8 +68,10 @@ int main(void) {
app_setup();
while (1) {
+ bool usb_enabled = hri_usbdevice_get_CTRLA_ENABLE_bit(USB);
bool can_sleep = app_loop();
- if (can_sleep) {
+
+ if (can_sleep && !usb_enabled) {
app_prepare_for_sleep();
sleep(4);
app_wake_from_sleep();
diff --git a/watch-library/watch/tusb_config.h b/watch-library/watch/tusb_config.h
new file mode 100644
index 00000000..a22b2b99
--- /dev/null
+++ b/watch-library/watch/tusb_config.h
@@ -0,0 +1,91 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Ha Thach (tinyusb.org)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#ifndef _TUSB_CONFIG_H_
+#define _TUSB_CONFIG_H_
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+//--------------------------------------------------------------------
+// COMMON CONFIGURATION
+//--------------------------------------------------------------------
+
+// defined by board.mk
+#define CFG_TUSB_MCU OPT_MCU_SAML22
+
+#define BOARD_DEVICE_RHPORT_NUM 0
+#define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_FULL_SPEED
+#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_FULL_SPEED)
+
+#define CFG_TUSB_OS OPT_OS_NONE
+
+// disable TinyUSB debug. our printf method prints stuff to the USB console, so you just get infinite noise.
+// if you need to debug tinyUSB issues, use the alternate _write function in watch_private.c to echo to the UART.
+#define CFG_TUSB_DEBUG 0
+
+/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
+ * Tinyusb use follows macros to declare transferring memory so that they can be put
+ * into those specific section.
+ * e.g
+ * - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") ))
+ * - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4)))
+ */
+#ifndef CFG_TUSB_MEM_SECTION
+#define CFG_TUSB_MEM_SECTION
+#endif
+
+#ifndef CFG_TUSB_MEM_ALIGN
+#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4)))
+#endif
+
+//--------------------------------------------------------------------
+// DEVICE CONFIGURATION
+//--------------------------------------------------------------------
+
+#ifndef CFG_TUD_ENDPOINT0_SIZE
+#define CFG_TUD_ENDPOINT0_SIZE 64
+#endif
+
+//------------- CLASS -------------//
+#define CFG_TUD_CDC 1
+#define CFG_TUD_MSC 0
+#define CFG_TUD_HID 0
+#define CFG_TUD_MIDI 0
+#define CFG_TUD_VENDOR 0
+
+// CDC FIFO size of TX and RX
+#define CFG_TUD_CDC_RX_BUFSIZE (64)
+#define CFG_TUD_CDC_TX_BUFSIZE (64)
+
+// CDC Endpoint transfer buffer size, more is faster
+#define CFG_TUD_CDC_EP_BUFSIZE (64)
+
+#ifdef __cplusplus
+ }
+#endif
+
+#endif /* _TUSB_CONFIG_H_ */
diff --git a/watch-library/watch/watch_buzzer.c b/watch-library/watch/watch_buzzer.c
index 04746416..c6c89a0e 100644
--- a/watch-library/watch/watch_buzzer.c
+++ b/watch-library/watch/watch_buzzer.c
@@ -23,28 +23,38 @@
*/
inline void watch_enable_buzzer() {
- PWM_1_init();
+ if (!hri_tcc_get_CTRLA_reg(TCC0, TCC_CTRLA_ENABLE)) {
+ _watch_enable_tcc();
+ }
}
-
inline void watch_set_buzzer_period(uint32_t period) {
- pwm_set_parameters(&PWM_1, period, period / 2);
+ hri_tcc_write_PERBUF_reg(TCC0, period);
+}
+
+void watch_disable_buzzer() {
+ _watch_disable_tcc();
}
inline void watch_set_buzzer_on() {
- pwm_enable(&PWM_1);
+ gpio_set_pin_direction(BUZZER, GPIO_DIRECTION_OUT);
+ gpio_set_pin_function(BUZZER, PINMUX_PA27F_TCC0_WO5);
}
inline void watch_set_buzzer_off() {
- pwm_disable(&PWM_1);
+ gpio_set_pin_direction(BUZZER, GPIO_DIRECTION_OFF);
+ gpio_set_pin_function(BUZZER, GPIO_PIN_FUNCTION_OFF);
}
-const uint16_t NotePeriods[108] = {31047, 29301, 27649, 26079, 24617, 23224, 21923, 20683, 19515, 18418, 17377, 16399, 15477, 14603, 13780, 13004, 12272, 11580, 10926, 10311, 9730, 9181, 8664, 8175, 7714, 7280, 6869, 6483, 6117, 5772, 5447, 5140, 4850, 4577, 4319, 4076, 3846, 3629, 3425, 3232, 3050, 2878, 2715, 2562, 2418, 2282, 2153, 2032, 1917, 1809, 1707, 1611, 1520, 1435, 1354, 1277, 1205, 1137, 1073, 1013, 956, 902, 851, 803, 758, 715, 675, 637, 601, 567, 535, 505, 476, 450, 424, 400, 378, 357, 336, 317, 300, 283, 267, 252, 238, 224, 212, 200, 188, 178, 168, 158, 149, 141, 133, 125, 118, 112, 105, 99, 94, 89, 84, 79, 74, 70, 66, 63};
+// note: the buzzer uses a 1 MHz clock. these values were determined by dividing 1,000,000 by the target frequency.
+// i.e. for a 440 Hz tone (A4 on the piano), 1MHz/440Hz = 2273
+const uint16_t NotePeriods[108] = {18182,17161,16197,15288,14430,13620,12857,12134,11453,10811,10204,9631,9091,8581,8099,7645,7216,6811,6428,6068,5727,5405,5102,4816,4545,4290,4050,3822,3608,3405,3214,3034,2863,2703,2551,2408,2273,2145,2025,1911,1804,1703,1607,1517,1432,1351,1276,1204,1136,1073,1012,956,902,851,804,758,716,676,638,602,568,536,506,478,451,426,402,379,358,338,319,301,284,268,253,239,225,213,201,190,179,169,159,150,142,134,127};
void watch_buzzer_play_note(BuzzerNote note, uint16_t duration_ms) {
if (note == BUZZER_NOTE_REST) {
watch_set_buzzer_off();
} else {
- pwm_set_parameters(&PWM_1, NotePeriods[note], NotePeriods[note] / 2);
+ hri_tcc_write_PERBUF_reg(TCC0, NotePeriods[note]);
+ hri_tcc_write_CCBUF_reg(TCC0, 1, NotePeriods[note] / 2);
watch_set_buzzer_on();
}
delay_ms(duration_ms);
diff --git a/watch-library/watch/watch_buzzer.h b/watch-library/watch/watch_buzzer.h
index 75b30a79..995e059a 100644
--- a/watch-library/watch/watch_buzzer.h
+++ b/watch-library/watch/watch_buzzer.h
@@ -32,11 +32,17 @@
void watch_enable_buzzer();
/** @brief Sets the period of the buzzer.
- * @param period The period of a single cycle for the PWM peripheral. You can use the following formula to
- * convert a desired frequency to a period for this function: period = 513751 * (freq^−1.0043)
+ * @param period The period of a single cycle for the TCC peripheral. You can determine the period for
+ * a desired frequency with the following formula: period = 1000000 / freq
*/
void watch_set_buzzer_period(uint32_t period);
+/** @brief Disables the TCC peripheral that drives the buzzer.
+ * @note If you are using PWM to set custom LED colors, this method will also disable the LED PWM driver,
+ * since the buzzer and LED both make use of the same peripheral to drive their PWM behavior.
+ */
+void watch_disable_buzzer();
+
/** @brief Turns the buzzer output on. It will emit a continuous sound at the given frequency.
* @note The TCC peripheral that drives the buzzer does not run in standby mode; if you wish for buzzer
* output to continue, you should prevent your app from going to sleep.
@@ -47,29 +53,8 @@ void watch_set_buzzer_on();
*/
void watch_set_buzzer_off();
-/// @brief 108 notes for use with watch_buzzer_play_note
+/// @brief 87 notes for use with watch_buzzer_play_note
typedef enum BuzzerNote {
- BUZZER_NOTE_C0 = 0, ///< 16.35 Hz
- BUZZER_NOTE_C0SHARP_D0FLAT, ///< 17.32 Hz
- BUZZER_NOTE_D0, ///< 18.35 Hz
- BUZZER_NOTE_D0SHARP_E0FLAT, ///< 19.45 Hz
- BUZZER_NOTE_E0, ///< 20.60 Hz
- BUZZER_NOTE_F0, ///< 21.83 Hz
- BUZZER_NOTE_F0SHARP_G0FLAT, ///< 23.12 Hz
- BUZZER_NOTE_G0, ///< 24.50 Hz
- BUZZER_NOTE_G0SHARP_A0FLAT, ///< 25.96 Hz
- BUZZER_NOTE_A0, ///< 27.50 Hz
- BUZZER_NOTE_A0SHARP_B0FLAT, ///< 29.14 Hz
- BUZZER_NOTE_B0, ///< 30.87 Hz
- BUZZER_NOTE_C1, ///< 32.70 Hz
- BUZZER_NOTE_C1SHARP_D1FLAT, ///< 34.65 Hz
- BUZZER_NOTE_D1, ///< 36.71 Hz
- BUZZER_NOTE_D1SHARP_E1FLAT, ///< 38.89 Hz
- BUZZER_NOTE_E1, ///< 41.20 Hz
- BUZZER_NOTE_F1, ///< 43.65 Hz
- BUZZER_NOTE_F1SHARP_G1FLAT, ///< 46.25 Hz
- BUZZER_NOTE_G1, ///< 49.00 Hz
- BUZZER_NOTE_G1SHARP_A1FLAT, ///< 51.91 Hz
BUZZER_NOTE_A1, ///< 55.00 Hz
BUZZER_NOTE_A1SHARP_B1FLAT, ///< 58.27 Hz
BUZZER_NOTE_B1, ///< 61.74 Hz
diff --git a/watch-library/watch/watch_extint.h b/watch-library/watch/watch_extint.h
index d1265119..e3084371 100644
--- a/watch-library/watch/watch_extint.h
+++ b/watch-library/watch/watch_extint.h
@@ -73,7 +73,7 @@ void watch_disable_external_interrupts();
*/
void watch_register_interrupt_callback(const uint8_t pin, ext_irq_cb_t callback, watch_interrupt_trigger trigger);
-__attribute__((deprecated("Use watch_register_interrupt_callback instead")))
+__attribute__((deprecated("Use watch_register_interrupt_callback or watch_register_extwake_callback instead")))
void watch_register_button_callback(const uint8_t pin, ext_irq_cb_t callback);
__attribute__((deprecated("Use watch_enable_external_interrupts instead")))
diff --git a/watch-library/watch/watch_gpio.c b/watch-library/watch/watch_gpio.c
index 460a38df..a9bc139d 100644
--- a/watch-library/watch/watch_gpio.c
+++ b/watch-library/watch/watch_gpio.c
@@ -27,6 +27,12 @@
gpio_set_pin_function(pin, GPIO_PIN_FUNCTION_OFF);
}
+void watch_disable_digital_input(const uint8_t pin) {
+ gpio_set_pin_direction(pin, GPIO_DIRECTION_OFF);
+ gpio_set_pin_pull_mode(pin, GPIO_PULL_OFF);
+ gpio_set_pin_function(pin, GPIO_PIN_FUNCTION_OFF);
+}
+
void watch_enable_pull_up(const uint8_t pin) {
gpio_set_pin_pull_mode(pin, GPIO_PULL_UP);
}
diff --git a/watch-library/watch/watch_gpio.h b/watch-library/watch/watch_gpio.h
index f7678f6f..640686e0 100644
--- a/watch-library/watch/watch_gpio.h
+++ b/watch-library/watch/watch_gpio.h
@@ -32,6 +32,11 @@
*/
void watch_enable_digital_input(const uint8_t pin);
+/** @brief Disables any digital input, along with any pull-up or pull-down configuration.
+ * @param pin The pin that you wish to disable.
+ */
+void watch_disable_digital_input(const uint8_t pin);
+
/** @brief Enables a pull-up resistor on the selected pin.
* @param pin The pin that you wish to configure.
*/
diff --git a/watch-library/watch/watch_led.c b/watch-library/watch/watch_led.c
index 01f59fc4..4f9898d8 100644
--- a/watch-library/watch/watch_led.c
+++ b/watch-library/watch/watch_led.c
@@ -22,45 +22,39 @@
* SOFTWARE.
*/
- bool PWM_0_enabled = false;
void watch_enable_led(bool pwm) {
if (pwm) {
- if (PWM_0_enabled) return;
-
- PWM_0_init();
- pwm_set_parameters(&PWM_0, 10000, 0);
- pwm_enable(&PWM_0);
-
- PWM_0_enabled = true;
+ if (!hri_tcc_get_CTRLA_reg(TCC0, TCC_CTRLA_ENABLE)) {
+ _watch_enable_tcc();
+ }
} else {
watch_enable_digital_output(RED);
watch_enable_digital_output(GREEN);
+ watch_set_led_off();
}
- watch_set_led_off();
}
void watch_disable_led(bool pwm) {
if (pwm) {
- if (!PWM_0_enabled) return;
- pwm_disable(&PWM_0);
- PWM_0_enabled = false;
+ _watch_disable_tcc();
+ } else {
+ watch_disable_digital_output(RED);
+ watch_disable_digital_output(GREEN);
}
-
- watch_disable_digital_output(RED);
- watch_disable_digital_output(GREEN);
}
-void watch_set_led_color(uint16_t red, uint16_t green) {
- if (PWM_0_enabled) {
- TC3->COUNT16.CC[0].reg = red;
- TC3->COUNT16.CC[1].reg = green;
+void watch_set_led_color(uint8_t red, uint8_t green) {
+ if (hri_tcc_get_CTRLA_reg(TCC0, TCC_CTRLA_ENABLE)) {
+ uint32_t period = hri_tcc_get_PER_reg(TCC0, TCC_PER_MASK);
+ hri_tcc_write_CCBUF_reg(TCC0, 2, ((period * red * 1000ull) / 255000ull));
+ hri_tcc_write_CCBUF_reg(TCC0, 3, ((period * green * 1000ull) / 255000ull));
}
}
void watch_set_led_red() {
- if (PWM_0_enabled) {
- watch_set_led_color(65535, 0);
+ if (hri_tcc_get_CTRLA_reg(TCC0, TCC_CTRLA_ENABLE)) {
+ watch_set_led_color(255, 0);
} else {
watch_set_pin_level(RED, true);
watch_set_pin_level(GREEN, false);
@@ -68,8 +62,8 @@ void watch_set_led_red() {
}
void watch_set_led_green() {
- if (PWM_0_enabled) {
- watch_set_led_color(65535, 0);
+ if (hri_tcc_get_CTRLA_reg(TCC0, TCC_CTRLA_ENABLE)) {
+ watch_set_led_color(0, 255);
} else {
watch_set_pin_level(RED, false);
watch_set_pin_level(GREEN, true);
@@ -77,8 +71,8 @@ void watch_set_led_green() {
}
void watch_set_led_yellow() {
- if (PWM_0_enabled) {
- watch_set_led_color(65535, 65535);
+ if (hri_tcc_get_CTRLA_reg(TCC0, TCC_CTRLA_ENABLE)) {
+ watch_set_led_color(255, 255);
} else {
watch_set_pin_level(RED, true);
watch_set_pin_level(GREEN, true);
@@ -86,7 +80,7 @@ void watch_set_led_yellow() {
}
void watch_set_led_off() {
- if (PWM_0_enabled) {
+ if (hri_tcc_get_CTRLA_reg(TCC0, TCC_CTRLA_ENABLE)) {
watch_set_led_color(0, 0);
} else {
watch_set_pin_level(RED, false);
diff --git a/watch-library/watch/watch_led.h b/watch-library/watch/watch_led.h
index 04b6e5fa..5dedadf3 100644
--- a/watch-library/watch/watch_led.h
+++ b/watch-library/watch/watch_led.h
@@ -43,15 +43,16 @@ void watch_enable_led(bool pwm);
/** @brief Disables the LEDs.
* @param pwm if true, disables the PWM output. If false, disables the digital outputs.
+ * @note If pwm is true, this method will also disable the buzzer, since the buzzer and LED both make use of
+ * the same peripheral to drive their PWM behavior.
*/
void watch_disable_led(bool pwm);
/** @brief Sets the LED to a custom color by modulating each output's duty cycle.
- * @param red The red value.
- * @param green The green value.
- * @note still working on this, 0-65535 works now but these values may change.
+ * @param red The red value from 0-255.
+ * @param green The green value from 0-255.
*/
-void watch_set_led_color(uint16_t red, uint16_t green);
+void watch_set_led_color(uint8_t red, uint8_t green);
/** @brief Sets the red LED to full brightness, and turns the green LED off.
* @note Of the two LED's in the RG bi-color LED, the red LED is the less power-efficient one (~4.5 mA).
diff --git a/watch-library/watch/watch_private.c b/watch-library/watch/watch_private.c
index cd9c2baa..a4e2f72c 100644
--- a/watch-library/watch/watch_private.c
+++ b/watch-library/watch/watch_private.c
@@ -22,6 +22,8 @@
* SOFTWARE.
*/
+#include "tusb.h"
+
void _watch_init() {
// disable the LED pin (it may have been enabled by the bootloader)
watch_disable_digital_output(RED);
@@ -42,3 +44,314 @@ void _watch_init() {
a2_callback = NULL;
a4_callback = NULL;
}
+
+void _watch_enable_tcc() {
+ // 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);
+ // have prescaler divide our 8 MHz clock down to 1 MHz.
+ hri_tcc_write_CTRLA_reg(TCC0, TCC_CTRLA_PRESCALER_DIV8);
+ // 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);
+ // 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, 4096);
+ // Set the duty cycle of all pins to 0: LED's off, buzzer not buzzing.
+ hri_tcc_write_CC_reg(TCC0, 1, 0);
+ hri_tcc_write_CC_reg(TCC0, 2, 0);
+ hri_tcc_write_CC_reg(TCC0, 3, 0);
+ // 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, PINMUX_PA20F_TCC0_WO6);
+ gpio_set_pin_direction(GREEN, GPIO_DIRECTION_OUT);
+ gpio_set_pin_function(GREEN, PINMUX_PA21F_TCC0_WO7);
+}
+
+void _watch_disable_tcc() {
+ // 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);
+
+ // disable the TCC
+ hri_tcc_clear_CTRLA_ENABLE_bit(TCC0);
+ hri_mclk_clear_APBCMASK_TCC0_bit(MCLK);
+}
+
+void _watch_enable_usb() {
+ // disable USB, just in case.
+ hri_usb_clear_CTRLA_ENABLE_bit(USB);
+
+ // reset flags and disable DFLL
+ OSCCTRL->INTFLAG.reg = OSCCTRL_INTFLAG_DFLLRDY;
+ OSCCTRL->DFLLCTRL.reg = 0;
+ while (!(OSCCTRL->STATUS.reg & OSCCTRL_STATUS_DFLLRDY));
+
+ // set the coarse and fine values to speed up frequency lock.
+ uint32_t coarse =(*((uint32_t *)NVMCTRL_OTP5)) >> 26;
+ OSCCTRL->DFLLVAL.reg = OSCCTRL_DFLLVAL_COARSE(coarse) |
+ OSCCTRL_DFLLVAL_FINE(0x200);
+ // set coarse and fine steps, and multiplier (48 MHz = 32768 Hz * 1465)
+ OSCCTRL->DFLLMUL.reg = OSCCTRL_DFLLMUL_CSTEP( 1 ) |
+ OSCCTRL_DFLLMUL_FSTEP( 1 ) |
+ OSCCTRL_DFLLMUL_MUL( 1465 );
+ // set closed loop mode, chill cycle disable and USB clock recovery mode, and enable the DFLL.
+ OSCCTRL->DFLLCTRL.reg = OSCCTRL_DFLLCTRL_MODE | OSCCTRL_DFLLCTRL_CCDIS | OSCCTRL_DFLLCTRL_ONDEMAND | OSCCTRL_DFLLCTRL_RUNSTDBY | OSCCTRL_DFLLCTRL_USBCRM | OSCCTRL_DFLLCTRL_ENABLE;
+ while (!(OSCCTRL->STATUS.reg & OSCCTRL_STATUS_DFLLRDY));
+
+ // assign DFLL to GCLK1
+ GCLK->GENCTRL[1].reg = GCLK_GENCTRL_SRC(GCLK_GENCTRL_SRC_DFLL48M) | GCLK_GENCTRL_DIV(1) | GCLK_GENCTRL_GENEN;// | GCLK_GENCTRL_OE;
+ while (GCLK->SYNCBUSY.bit.GENCTRL1);
+
+ // assign GCLK1 to USB
+ hri_gclk_write_PCHCTRL_reg(GCLK, USB_GCLK_ID, GCLK_PCHCTRL_GEN_GCLK1_Val | GCLK_PCHCTRL_CHEN);
+ hri_mclk_set_AHBMASK_USB_bit(MCLK);
+ hri_mclk_set_APBBMASK_USB_bit(MCLK);
+
+ // USB Pin Init
+ gpio_set_pin_direction(PIN_PA24, GPIO_DIRECTION_OUT);
+ gpio_set_pin_level(PIN_PA24, false);
+ gpio_set_pin_pull_mode(PIN_PA24, GPIO_PULL_OFF);
+ gpio_set_pin_direction(PIN_PA25, GPIO_DIRECTION_OUT);
+ gpio_set_pin_level(PIN_PA25, false);
+ gpio_set_pin_pull_mode(PIN_PA25, GPIO_PULL_OFF);
+
+ gpio_set_pin_function(PIN_PA24, PINMUX_PA24G_USB_DM);
+ gpio_set_pin_function(PIN_PA25, PINMUX_PA25G_USB_DP);
+
+ // 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.
+ // clock TC0 with the 8 MHz clock on GCLK0.
+ hri_gclk_write_PCHCTRL_reg(GCLK, TC0_GCLK_ID, GCLK_PCHCTRL_GEN_GCLK0_Val | GCLK_PCHCTRL_CHEN);
+ // and enable the peripheral clock.
+ hri_mclk_set_APBCMASK_TC0_bit(MCLK);
+ // disable and reset TC0.
+ hri_tc_clear_CTRLA_ENABLE_bit(TC0);
+ hri_tc_wait_for_sync(TC0, TC_SYNCBUSY_ENABLE);
+ hri_tc_write_CTRLA_reg(TC0, TC_CTRLA_SWRST);
+ hri_tc_wait_for_sync(TC0, TC_SYNCBUSY_SWRST);
+ // configure the TC to overflow 1,000 times per second
+ hri_tc_write_CTRLA_reg(TC0, TC_CTRLA_PRESCALER_DIV64 | // divide the 8 MHz clock by 64 to count at 125 KHz
+ TC_CTRLA_MODE_COUNT8 | // count in 8-bit mode
+ TC_CTRLA_RUNSTDBY); // run in standby, just in case we figure that out
+ hri_tccount8_write_PER_reg(TC0, 125); // 125000 Hz / 125 = 1,000 Hz
+ // set an interrupt on overflow; this will call TC0_Handler below.
+ hri_tc_set_INTEN_OVF_bit(TC0);
+ NVIC_ClearPendingIRQ(TC0_IRQn);
+ NVIC_EnableIRQ (TC0_IRQn);
+
+ // now we can init TinyUSB
+ tusb_init();
+ // and start the timer that handles USB device tasks.
+ hri_tc_set_CTRLA_ENABLE_bit(TC0);
+}
+
+// this function ends up getting called by printf to log stuff to the USB console.
+int _write(int file, char *ptr, int len) {
+ (void)file;
+ if (hri_usbdevice_get_CTRLA_ENABLE_bit(USB)) {
+ tud_cdc_n_write(0, (void const*)ptr, len);
+ tud_cdc_n_write_flush(0);
+ return len;
+ }
+
+ return 0;
+}
+
+// this method could be overridden to read stuff from the USB console? but no need rn.
+int _read() {
+ return 0;
+}
+
+// Alternate function that outputs to the debug UART. useful for debugging USB issues.
+// int _write(int file, char *ptr, int len) {
+// (void)file;
+// int pos = 0;
+// while(pos < len) watch_debug_putc(ptr[pos++]);
+
+// return 0;
+// }
+
+void USB_Handler(void) {
+ tud_int_handler(0);
+}
+
+void TC0_Handler(void) {
+ tud_task();
+ TC0->COUNT8.INTFLAG.reg |= TC_INTFLAG_OVF;
+}
+
+
+// USB Descriptors and tinyUSB callbacks follow.
+
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Ha Thach (tinyusb.org)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#include "tusb.h"
+
+/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
+ * Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC.
+ *
+ * Auto ProductID layout's Bitmap:
+ * [MSB] HID | MSC | CDC [LSB]
+ */
+#define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) )
+#define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \
+ _PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) )
+
+//--------------------------------------------------------------------+
+// Device Descriptors
+//--------------------------------------------------------------------+
+tusb_desc_device_t const desc_device =
+{
+ .bLength = sizeof(tusb_desc_device_t),
+ .bDescriptorType = TUSB_DESC_DEVICE,
+ .bcdUSB = 0x0200,
+
+ // Use Interface Association Descriptor (IAD) for CDC
+ // As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1)
+ .bDeviceClass = TUSB_CLASS_MISC,
+ .bDeviceSubClass = MISC_SUBCLASS_COMMON,
+ .bDeviceProtocol = MISC_PROTOCOL_IAD,
+
+ .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
+
+ .idVendor = 0xCafe,
+ .idProduct = USB_PID,
+ .bcdDevice = 0x0100,
+
+ .iManufacturer = 0x01,
+ .iProduct = 0x02,
+ .iSerialNumber = 0x03,
+
+ .bNumConfigurations = 0x01
+};
+
+// Invoked when received GET DEVICE DESCRIPTOR
+// Application return pointer to descriptor
+uint8_t const * tud_descriptor_device_cb(void) {
+ return (uint8_t const *) &desc_device;
+}
+
+//--------------------------------------------------------------------+
+// Configuration Descriptor
+//--------------------------------------------------------------------+
+
+enum {
+ ITF_NUM_CDC = 0,
+ ITF_NUM_CDC_DATA,
+ ITF_NUM_TOTAL
+};
+
+#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN)
+
+#define EPNUM_CDC_NOTIF 0x81
+#define EPNUM_CDC_OUT 0x02
+#define EPNUM_CDC_IN 0x82
+
+
+uint8_t const desc_fs_configuration[] = {
+ // Config number, interface count, string index, total length, attribute, power in mA
+ TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
+
+ // Interface number, string index, EP notification address and size, EP data address (out, in) and size.
+ TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 64),
+};
+
+// Invoked when received GET CONFIGURATION DESCRIPTOR
+// Application return pointer to descriptor
+// Descriptor contents must exist long enough for transfer to complete
+uint8_t const * tud_descriptor_configuration_cb(uint8_t index) {
+ (void) index; // for multiple configurations
+ return desc_fs_configuration;
+}
+
+//--------------------------------------------------------------------+
+// String Descriptors
+//--------------------------------------------------------------------+
+
+// array of pointer to string descriptors
+char const* string_desc_arr [] =
+{
+ (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
+ "TinyUSB", // 1: Manufacturer
+ "TinyUSB Device", // 2: Product
+ "123456", // 3: Serials, should use chip ID
+ "TinyUSB CDC", // 4: CDC Interface
+};
+
+static uint16_t _desc_str[32];
+
+// Invoked when received GET STRING DESCRIPTOR request
+// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
+uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
+{
+ (void) langid;
+
+ uint8_t chr_count;
+
+ if ( index == 0) {
+ memcpy(&_desc_str[1], string_desc_arr[0], 2);
+ chr_count = 1;
+ } else {
+ // Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
+ // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
+
+ if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
+
+ const char* str = string_desc_arr[index];
+
+ // Cap at max char
+ chr_count = strlen(str);
+ if ( chr_count > 31 ) chr_count = 31;
+
+ // Convert ASCII string into UTF-16
+ for(uint8_t i=0; i<chr_count; i++)
+ {
+ _desc_str[1+i] = str[i];
+ }
+ }
+
+ // first byte is length (including header), second byte is string type
+ _desc_str[0] = (TUSB_DESC_STRING << 8 ) | (2*chr_count + 2);
+
+ return _desc_str;
+}
diff --git a/watch-library/watch/watch_private.h b/watch-library/watch/watch_private.h
index 99171126..abee085a 100644
--- a/watch-library/watch/watch_private.h
+++ b/watch-library/watch/watch_private.h
@@ -24,3 +24,12 @@
/// Called by main.c while setting up the app. You should not call this from your app.
void _watch_init();
+
+/// Called by buzzer and LED setup functions. You should not call this from your app.
+void _watch_enable_tcc();
+
+/// Called by buzzer and LED teardown functions. You should not call this from your app.
+void _watch_disable_tcc();
+
+/// Called by main.c if plugged in to USB. You should not call this from your app.
+void _watch_enable_usb();
diff --git a/watch-library/watch/watch_uart.c b/watch-library/watch/watch_uart.c
index 4cbac89b..a747e71b 100644
--- a/watch-library/watch/watch_uart.c
+++ b/watch-library/watch/watch_uart.c
@@ -53,7 +53,7 @@
#include "peripheral_clk_config.h"
void watch_enable_debug_uart(uint32_t baud) {
- uint64_t br = (uint64_t)65536 * (CONF_CPU_FREQUENCY - 16 * baud) / CONF_CPU_FREQUENCY;
+ uint64_t br = (uint64_t)65536 * ((CONF_CPU_FREQUENCY * 4) - 16 * baud) / (CONF_CPU_FREQUENCY * 4);
gpio_set_pin_direction(D1, GPIO_DIRECTION_IN);
gpio_set_pin_function(D1, PINMUX_PB00C_SERCOM3_PAD2);