diff --git a/firmware/inc/init.h b/firmware/inc/init.h new file mode 100644 index 0000000..097d384 --- /dev/null +++ b/firmware/inc/init.h @@ -0,0 +1,49 @@ +/* + * Board level init functions + * Copyright (C) 2015 Richard Meadows + * + * 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 INIT_H +#define INIT_H + +#include "samd20.h" +#include "system/port.h" +#include "timer.h" + +/** + * Turns the status LED on + */ +static inline void led_on(void) +{ + port_pin_set_output_level(LED0_PIN, 0); /* LED is active low */ +} +/** + * Turns the status LED off + */ +static inline void led_off(void) +{ + port_pin_set_output_level(LED0_PIN, 1); /* LED is active low */ +} + +void init(timepulse_callback_t callback); + +#endif /* INIT_H */ diff --git a/firmware/src/init.c b/firmware/src/init.c new file mode 100644 index 0000000..7ac258d --- /dev/null +++ b/firmware/src/init.c @@ -0,0 +1,126 @@ +/* + * Board init functions + * Copyright (C) 2015 Richard Meadows + * + * 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 "samd20.h" +#include "hw_config.h" +#include "system/system.h" +#include "system/port.h" +#include "system/events.h" +#include "system/extint.h" +#include "gps.h" +#include "si_trx.h" +#include "watchdog.h" +#include "xosc.h" +#include "timer.h" + +/** + * Initialises the status LED. SHOULD TURN ON THE LED + */ +static inline void led_reset(void) +{ + port_pin_set_config(LED0_PIN, + PORT_PIN_DIR_OUTPUT, /* Direction */ + PORT_PIN_PULL_NONE, /* Pull */ + false); /* Powersave */ + port_pin_set_output_level(LED0_PIN, 0); /* LED is active low */ +} + +/** + * Power Management + */ +void powermananger_init(void) +{ + system_apb_clock_clear_mask(SYSTEM_CLOCK_APB_APBA, +// PM_APBAMASK_EIC | /* EIC is used now */ +// PM_APBAMASK_RTC | /* RTC is used now */ + 0); +} + +/** + * Internal initialisation + * ============================================================================= + */ +void init(timepulse_callback_t callback) +{ + /** + * Reset to get the system in a safe state + * -------------------------------------------------------------------------- + */ + led_reset(); + si_trx_shutdown(); + + /* If the reset was caused by the internal watchdog... */ + if (PM->RCAUSE.reg & PM_RCAUSE_WDT) { + /* External hardware is in an undefined state. Wait here for the + external watchdog to trigger an external reset */ + + while (1); + } + + /** + * Internal initialisation + * --------------------------------------------------------------------------- + */ + + /* Clock up to 14MHz with 0 wait states */ + system_flash_set_waitstates(SYSTEM_WAIT_STATE_1_8V_14MHZ); + + /* Up the clock rate to 4MHz */ + system_clock_source_osc8m_set_config(SYSTEM_OSC8M_DIV_2, /* Prescaler */ + false, /* Run in Standby */ + false); /* Run on Demand */ + + /* Restart the GCLK Module */ + system_gclk_init(); + system_events_init(); + system_extint_init(); + + /* Remember the HW watchdog has been running since reset */ + //watchdog_init(); + + /* Configure Sleep Mode */ + //system_set_sleepmode(SYSTEM_SLEEPMODE_STANDBY); + system_set_sleepmode(SYSTEM_SLEEPMODE_IDLE_2); /* Disable CPU, AHB and APB */ + + /* Configure the Power Manager */ + //powermananger_init(); + + /** + * System initialisation + * --------------------------------------------------------------------------- + */ + + /* Enable the xosc on gclk1 */ + xosc_init(); + + /* GPS init */ +// gps_init(); + + /* Enable timer interrupt and event channel */ + timepulse_extint_init(); + timepulse_set_callback(callback); + + /* Initialise Si4060 interface */ + si_trx_init(); +} diff --git a/firmware/src/main.c b/firmware/src/main.c index e74c7af..2619989 100644 --- a/firmware/src/main.c +++ b/firmware/src/main.c @@ -32,9 +32,9 @@ #include "system/system.h" #include "sercom/usart.h" #include "system/port.h" -#include "system/events.h" #include "system/extint.h" #include "tc/tc_driver.h" +#include "init.h" #include "gps.h" #include "mfsk.h" #include "ubx_messages.h" @@ -59,44 +59,6 @@ void timepulse_callback(uint32_t sequence); int32_t _xosc_error = 0; -/** - * Initialises the status LED. SHOULD TURN ON THE LED - */ -static inline void led_reset(void) -{ - port_pin_set_config(LED0_PIN, - PORT_PIN_DIR_OUTPUT, /* Direction */ - PORT_PIN_PULL_NONE, /* Pull */ - false); /* Powersave */ - port_pin_set_output_level(LED0_PIN, 0); /* LED is active low */ -} -/** - * Turns the status LED on - */ -static inline void led_on(void) -{ - port_pin_set_output_level(LED0_PIN, 0); /* LED is active low */ -} -/** - * Turns the status LED off - */ -static inline void led_off(void) -{ - port_pin_set_output_level(LED0_PIN, 1); /* LED is active low */ -} - -/** - * Power Management - */ -void powermananger_init(void) -{ - system_apb_clock_clear_mask(SYSTEM_CLOCK_APB_APBA, -// PM_APBAMASK_EIC | /* EIC is used now */ -// PM_APBAMASK_RTC | /* RTC is used now */ - 0); -} - - /** * Telemetry String * ============================================================================= @@ -274,74 +236,6 @@ void aprs_test(void) } } -/** - * Internal initialisation - * ============================================================================= - */ -void init(void) -{ - /** - * Reset to get the system in a safe state - * -------------------------------------------------------------------------- - */ - led_reset(); - si_trx_shutdown(); - - /* If the reset was caused by the internal watchdog... */ - if (PM->RCAUSE.reg & PM_RCAUSE_WDT) { - /* External hardware is in an undefined state. Wait here for the - external watchdog to trigger an external reset */ - - while (1); - } - - /** - * Internal initialisation - * --------------------------------------------------------------------------- - */ - - /* Clock up to 14MHz with 0 wait states */ - system_flash_set_waitstates(SYSTEM_WAIT_STATE_1_8V_14MHZ); - - /* Up the clock rate to 4MHz */ - system_clock_source_osc8m_set_config(SYSTEM_OSC8M_DIV_2, /* Prescaler */ - false, /* Run in Standby */ - false); /* Run on Demand */ - - /* Restart the GCLK Module */ - system_gclk_init(); - system_events_init(); - system_extint_init(); - - /* Remember the HW watchdog has been running since reset */ - //watchdog_init(); - - /* Configure Sleep Mode */ - //system_set_sleepmode(SYSTEM_SLEEPMODE_STANDBY); - system_set_sleepmode(SYSTEM_SLEEPMODE_IDLE_2); /* Disable CPU, AHB and APB */ - - /* Configure the Power Manager */ - //powermananger_init(); - - /** - * System initialisation - * --------------------------------------------------------------------------- - */ - - /* Enable the xosc on gclk1 */ - xosc_init(); - - /* GPS init */ -// gps_init(); - - /* Enable timer interrupt and event channel */ - timepulse_extint_init(); - timepulse_set_callback(timepulse_callback); - - /* Initialise Si4060 interface */ - si_trx_init(); -} - void xosc_measure_callback(uint32_t result) { @@ -382,7 +276,7 @@ int main(void) { uint32_t telemetry_alternate = 0; - init(); + init(timepulse_callback); while (1) { /* Send a packet */