GEX core repository.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gex-core/platform/lock_jumper.c

134 lines
3.2 KiB

7 years ago
//
// Created by MightyPork on 2017/12/15.
//
#include "sched_queue.h"
#include "task_sched.h"
7 years ago
#include "usbd_core.h"
#include "usb_device.h"
7 years ago
#include "framework/settings.h"
#include "framework/resources.h"
#include "framework/system_settings.h"
#include "pin_utils.h"
#include "lock_jumper.h"
#include "status_led.h"
7 years ago
static bool LockJumper_ReadPin(void);
7 years ago
static GPIO_TypeDef *lock_periph;
static uint32_t lock_llpin;
static bool old_state = false;
static uint32_t debo_ticks = 0;
7 years ago
// We use macros LOCK_JUMPER_PORT, LOCK_JUMPER_PIN from plat_compat.h
/** Init the jumper subsystem */
void LockJumper_Init(void)
{
bool suc = true;
// Resolve and claim resource
Resource rsc = pin2resource(LOCK_JUMPER_PORT, LOCK_JUMPER_PIN, &suc);
7 years ago
assert_param(suc);
suc &= rsc_claim(&UNIT_SYSTEM, rsc);
assert_param(suc);
// Resolve pin
lock_periph = port2periph(LOCK_JUMPER_PORT, &suc);
lock_llpin = pin2ll(LOCK_JUMPER_PIN, &suc);
7 years ago
assert_param(suc);
// Configure for input
LL_GPIO_SetPinMode(lock_periph, lock_llpin, LL_GPIO_MODE_INPUT);
// pull-up/down in the opposite direction of the active state
#if PLAT_LOCK_1CLOSED
LL_GPIO_SetPinPull(lock_periph, lock_llpin, LL_GPIO_PULL_DOWN);
#else
LL_GPIO_SetPinPull(lock_periph, lock_llpin, LL_GPIO_PULL_UP);
#endif
7 years ago
SystemSettings.editable = (bool) LL_GPIO_IsInputPinSet(lock_periph, lock_llpin);
dbg("Settings editable? %d", SystemSettings.editable);
}
/** Handle jumper state change */
static void jumper_changed(void)
{
if (SystemSettings.editable) {
// Unlock
dbg("LOCK removed, enabling MSC!");
Indicator_Effect(STATUS_DISK_ATTACHED);
7 years ago
} else {
// Lock
dbg("LOCK replaced, disabling MSC!");
Indicator_Effect(STATUS_DISK_REMOVED);
7 years ago
if (SystemSettings.modified) {
dbg("Saving settings to Flash...");
7 years ago
settings_save();
} else {
dbg("No changes to save.");
7 years ago
}
}
plat_usb_reconnect();
}
/** Periodic jumper check */
void LockJumper_Check(void)
{
bool state = LockJumper_ReadPin();
if (state != old_state) {
old_state = state;
debo_ticks = 3;
7 years ago
}
if (debo_ticks > 0) {
if (--debo_ticks == 0) {
// we've reached a change
#if PLAT_LOCK_BTN
if (state == true) {
SystemSettings.editable = !SystemSettings.editable;
jumper_changed();
}
#else
SystemSettings.editable = state;
jumper_changed();
#endif
}
7 years ago
}
}
/**
* Read the jumper / button state.
*
* @return true if the jumper is closed, or button pressed
*/
static bool LockJumper_ReadPin(void)
{
// lock jumper - start with MSC on if removed
#if PLAT_LOCK_1CLOSED
return (bool) LL_GPIO_IsInputPinSet(lock_periph, lock_llpin);
#else
return ! ((bool) LL_GPIO_IsInputPinSet(lock_periph, lock_llpin));
#endif
}
void LockJumper_CheckInitialState(void)
{
#if PLAT_LOCK_BTN
// no static read - starts with MSC off
SystemSettings.editable = false;
old_state = false;
#else
old_state = SystemSettings.editable = ! LockJumper_ReadPin();
#endif
}