2021-07-21 02:32:49 +08:00
|
|
|
/*
|
2021-08-18 01:16:38 +08:00
|
|
|
* MIT License
|
2021-07-21 02:32:49 +08:00
|
|
|
*
|
2021-08-18 01:16:38 +08:00
|
|
|
* Copyright (c) 2021 Joey Castillo
|
2021-07-21 02:32:49 +08:00
|
|
|
*
|
2021-08-18 01:16:38 +08:00
|
|
|
* 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:
|
2021-07-21 02:32:49 +08:00
|
|
|
*
|
2021-08-18 01:16:38 +08:00
|
|
|
* 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.
|
2021-07-21 02:32:49 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
2021-07-31 02:37:32 +08:00
|
|
|
#include <stdio.h>
|
2021-07-21 02:32:49 +08:00
|
|
|
#include "saml22.h"
|
2021-07-31 01:49:09 +08:00
|
|
|
#include "hal_init.h"
|
2021-07-31 02:37:32 +08:00
|
|
|
#include "watch.h"
|
2021-08-29 01:35:52 +08:00
|
|
|
#include "tusb.h"
|
2021-07-21 02:32:49 +08:00
|
|
|
|
2021-08-03 04:14:47 +08:00
|
|
|
int main(void) {
|
|
|
|
// ASF code. Initialize the MCU with configuration options from Atmel Studio.
|
2021-08-03 01:36:53 +08:00
|
|
|
init_mcu();
|
2021-08-03 04:14:47 +08:00
|
|
|
|
2021-08-30 03:42:37 +08:00
|
|
|
// 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);
|
|
|
|
|
2021-09-19 00:57:21 +08:00
|
|
|
// initialize the delay driver before any user code is called.
|
|
|
|
delay_driver_init();
|
|
|
|
|
2021-08-03 04:14:47 +08:00
|
|
|
// User code. Give the app a chance to initialize its data structures and state.
|
2021-08-02 06:40:03 +08:00
|
|
|
app_init();
|
2021-07-21 02:32:49 +08:00
|
|
|
|
2021-08-04 06:00:07 +08:00
|
|
|
// If the RTC is already enabled, we're either waking from BACKUP mode or a reset.
|
|
|
|
// Ideally we should check if the TAMPER or CMP0 (alarm) flags are set.
|
2021-08-09 03:01:44 +08:00
|
|
|
if (_watch_rtc_is_enabled()) {
|
2021-08-03 04:14:47 +08:00
|
|
|
// User code. Give the application a chance to restore state from backup registers.
|
2021-10-21 01:45:22 +08:00
|
|
|
app_wake_from_backup();
|
2021-08-04 06:00:07 +08:00
|
|
|
|
|
|
|
// disable the tamper interrupt and clear the tamper bit
|
|
|
|
hri_rtcmode0_clear_INTEN_TAMPER_bit(RTC);
|
|
|
|
hri_rtcmode0_clear_interrupt_TAMPER_bit(RTC);
|
2021-08-03 04:14:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Watch library code. Set initial parameters for the device and enable the RTC.
|
2021-08-06 03:49:20 +08:00
|
|
|
_watch_init();
|
2021-08-03 04:14:47 +08:00
|
|
|
|
2023-08-28 05:53:02 +08:00
|
|
|
// if date/time register is 0 (power on reset state), default year to 2023.
|
2022-04-22 02:12:16 +08:00
|
|
|
watch_date_time date_time = watch_rtc_get_date_time();
|
|
|
|
if (date_time.reg == 0) {
|
2023-08-28 05:53:02 +08:00
|
|
|
date_time.unit.year = 3;
|
2022-04-22 02:12:16 +08:00
|
|
|
watch_rtc_set_date_time(date_time);
|
|
|
|
}
|
|
|
|
|
2021-08-03 04:14:47 +08:00
|
|
|
// User code. Give the app a chance to enable and set up peripherals.
|
|
|
|
app_setup();
|
2021-07-31 00:07:41 +08:00
|
|
|
|
|
|
|
while (1) {
|
2021-08-29 07:49:49 +08:00
|
|
|
bool usb_enabled = hri_usbdevice_get_CTRLA_ENABLE_bit(USB);
|
2021-08-03 04:14:47 +08:00
|
|
|
bool can_sleep = app_loop();
|
2021-08-29 07:49:49 +08:00
|
|
|
|
|
|
|
if (can_sleep && !usb_enabled) {
|
2021-10-21 01:45:22 +08:00
|
|
|
app_prepare_for_standby();
|
2021-08-03 04:14:47 +08:00
|
|
|
sleep(4);
|
2021-10-21 01:45:22 +08:00
|
|
|
app_wake_from_standby();
|
2021-08-03 04:14:47 +08:00
|
|
|
}
|
2021-07-21 02:32:49 +08:00
|
|
|
}
|
|
|
|
|
2021-07-31 00:07:41 +08:00
|
|
|
return 0;
|
2021-07-21 02:32:49 +08:00
|
|
|
}
|