application: minor, restructure

This commit is contained in:
King Kévin 2019-12-22 11:24:04 +01:00
parent 439472730d
commit e85cdb52d0
1 changed files with 69 additions and 41 deletions

View File

@ -74,22 +74,11 @@ volatile bool rtc_internal_tick_flag = false; /**< flag set when internal RTC ti
bool disable_print = false;
/** activity timeout before switching off (in seconds) */
#define SHUTDOWN_TIMEOUT 60
#define SHUTDOWN_TIMEOUT 10
/** the current cable state */
struct cable_t {
uint16_t connections_nb; // number of connections the cable has
uint8_t (*connections)[2]; // the cable connections (pin pairs)
uint8_t connectors_nb; // number of connectors the cable has
bool connectors[LENGTH(usb_connectors)]; // which connectors the cable connects
uint8_t cables_nb; // number of cable definitions the connectors set match to
bool cables[LENGTH(usb_cables)]; // cable definitions the connectors set match to
uint16_t unconnected_nb[LENGTH(usb_cables)]; // number of unconnected pairs which should be connected according to cable specification
uint16_t unspecified_nb[LENGTH(usb_cables)]; // number of connected pairs which are not specified by cable
uint8_t cable_best; // best matching cable index (e.g. with lowest score)
uint8_t (*unconnected)[2]; // unconnected pairs which should be connected according to best cable specification
uint8_t (*unspecified)[2]; // connected pairs which are not specified by best cable
};
// ====================
// = common functions =
// ====================
size_t putc(char c)
{
@ -110,12 +99,55 @@ size_t putc(char c)
return length; // return number of characters printed
}
/** switch on power to display */
inline static void display_on(void)
{
gpio_clear(GPIO_PORT(DISPLAY_POWER_PIN), GPIO_PIN(DISPLAY_POWER_PIN));
}
/** switch off power to display */
inline static void display_off(void)
{
gpio_set(GPIO_PORT(DISPLAY_POWER_PIN), GPIO_PIN(DISPLAY_POWER_PIN));
}
/** go into standby mode */
static void standby(void)
{
while (true) { // try until success
SCB_SCR |= SCB_SCR_SLEEPDEEP; // Cortex-M3 standby setting
pwr_set_standby_mode(); // power setting
pwr_clear_wakeup_flag(); // clear wake-up flag to be able to sleep
__WFI(); // go to standby (e.g. shut down)
}
}
/** put all pins of all connectors to float */
static void usb_pins_float(void)
{
usb_cables_connectors_float(usb_connectors, LENGTH(usb_connectors)); // put every pin of every connector in floating mode
}
// ===================
// = cable utilities =
// ===================
/** the current cable state */
struct cable_t {
uint16_t connections_nb; // number of connections the cable has
uint8_t (*connections)[2]; // the cable connections (pin pairs)
uint8_t connectors_nb; // number of connectors the cable has
bool connectors[LENGTH(usb_connectors)]; // which connectors the cable connects
uint8_t cables_nb; // number of cable definitions the connectors set match to
bool cables[LENGTH(usb_cables)]; // cable definitions the connectors set match to
uint16_t unconnected_nb[LENGTH(usb_cables)]; // number of unconnected pairs which should be connected according to cable specification
uint16_t unspecified_nb[LENGTH(usb_cables)]; // number of connected pairs which are not specified by cable
uint8_t cable_best; // best matching cable index (e.g. with lowest score)
uint8_t (*unconnected)[2]; // unconnected pairs which should be connected according to best cable specification
uint8_t (*unspecified)[2]; // connected pairs which are not specified by best cable
};
/** clear the cable information
* @param[out] cable structure to be cleared
*/
@ -444,9 +476,9 @@ static void cable_issues(struct cable_t* cable)
}
}
// ===================
// = action commands =
// ===================
// ================================
// = generic commands definitions =
// ================================
/** display available commands
* @param[in] argument no argument required
@ -480,6 +512,10 @@ static void command_reset(void* argument);
*/
static void command_bootloader(void* argument);
// ===================
// = custom commands =
// ===================
/** test USB cables
* @param[in] argument no argument required
*/
@ -967,6 +1003,10 @@ static void command_connections(void* argument)
}
}
// ====================
// = list of commands =
// ====================
/** list of all supported commands */
static const struct menu_command_t menu_commands[] = {
{
@ -1061,6 +1101,10 @@ static const struct menu_command_t menu_commands[] = {
},
};
// ====================
// = generic commands =
// ====================
static void command_help(void* argument)
{
(void)argument; // we won't use the argument
@ -1194,17 +1238,9 @@ static void process_command(char* str)
}
}
/** switch on power to display */
inline static void display_on(void)
{
gpio_clear(GPIO_PORT(DISPLAY_POWER_PIN), GPIO_PIN(DISPLAY_POWER_PIN));
}
/** switch off power to display */
inline static void display_off(void)
{
gpio_set(GPIO_PORT(DISPLAY_POWER_PIN), GPIO_PIN(DISPLAY_POWER_PIN));
}
// ========
// = main =
// ========
/** program entry point
* this is the firmware function started by the micro-controller
@ -1226,12 +1262,7 @@ void main(void)
if (RCC_CSR & RCC_CSR_IWDGRSTF && 0x22 == BKP_DR1) { // we have been woken up by independent watchdog but actually want to stay in standby mode
RCC_CSR |= RCC_CSR_RMVF; // clear reset flags
// the reset will have clearer the software set watchdog
while (true) { // go to standby
SCB_SCR |= SCB_SCR_SLEEPDEEP; // Cortex-M3 standby setting
pwr_set_standby_mode(); // power setting
pwr_clear_wakeup_flag(); // clear wake-up flag to be able to sleep
__WFI(); // go to standby (e.g. shut down)
}
standby(); // go to standby (e.g. shut down)
}
// setup watchdog to reset in case we get stuck (i.e. when an error occurred)
iwdg_set_period_ms(WATCHDOG_PERIOD); // set independent watchdog period
@ -1273,7 +1304,7 @@ void main(void)
RCC_CSR |= RCC_CSR_RMVF; // clear reset flags
}
#endif
#if !(DEBUG)
#if !(DEBUG) && false
// show watchdog information
printf("setup watchdog: %.2fs", WATCHDOG_PERIOD / 1000.0);
if (FLASH_OBR & FLASH_OBR_OPTERR) {
@ -1349,13 +1380,10 @@ void main(void)
action = true; // action has been performed
while (!interactive && rtc_get_counter_val() >= last_connect_time + SHUTDOWN_TIMEOUT) { // time to shut down
#if !DEBUG
BKP_DR1 = 0x22; // indicate we want to stay in standby mode
BKP_DR1 = 0x22; // indicate we want to stay in standby mode (it's not possible to disable the independent watchdog and it will reset the system even in standby mode
#endif
display_off(); // cut power to displays (at stop D+ pull-up to indicate disconnect)
SCB_SCR |= SCB_SCR_SLEEPDEEP; // Cortex-M3 standby setting
pwr_set_standby_mode(); // power setting
pwr_clear_wakeup_flag(); // clear wake-up flag to be able to sleep
__WFI(); // go to standby (e.g. shut down)
standby(); // go into standby mode (shut down)
}
}
if (action) { // go to sleep if nothing had to be done, else recheck for activity