Commit dff7c0d1 authored by Federico Vaga's avatar Federico Vaga

freewatch: apply Linux style step 1

Signed-off-by: 's avatarFederico Vaga <federico.vaga@gmail.com>
parent ace9c21b
......@@ -46,40 +46,40 @@ static unsigned cur_percentage;
static void battery_monitor_task(void *params)
{
(void) params;
struct event evt;
(void) params;
struct event evt;
bool charging = max17047_is_charging();
unsigned percentage = max17047_get_charge();
bool charging = max17047_is_charging();
unsigned percentage = max17047_get_charge();
// Send an event if measurements are different
if(!params || charging != cur_charging || percentage != cur_percentage) {
evt.type = BATTERY_STATUS;
evt.data.battery.percentage = percentage;
evt.data.battery.charging = charging;
cur_percentage = percentage;
cur_charging = charging;
xQueueSendToBack(appQueue, &evt, 0);
}
// Send an event if measurements are different
if(!params || charging != cur_charging || percentage != cur_percentage) {
evt.type = BATTERY_STATUS;
evt.data.battery.percentage = percentage;
evt.data.battery.charging = charging;
cur_percentage = percentage;
cur_charging = charging;
xQueueSendToBack(appQueue, &evt, 0);
}
}
void battery_update(void)
{
battery_monitor_task(NULL);
battery_monitor_task(NULL);
}
void battery_monitor_init(void)
{
// Set invalid values to force an update
cur_charging = false;
cur_percentage = 255;
// Set invalid values to force an update
cur_charging = false;
cur_percentage = 255;
timer_handle = xTimerCreate((signed char*) "battery_monitor",
BATTERY_TASK_PERIOD, pdTRUE,
(void*) 0, battery_monitor_task);
timer_handle = xTimerCreate((signed char*) "battery_monitor",
BATTERY_TASK_PERIOD, pdTRUE,
(void*) 0, battery_monitor_task);
if(xTimerStart(timer_handle, 0) != pdPASS) {
// TODO kernel panic
}
if(xTimerStart(timer_handle, 0) != pdPASS) {
// TODO kernel panic
}
}
......@@ -40,15 +40,15 @@
///> Entry that stores an appropriate backlight level value
///> depending on the light sensor readings.
static const struct {
uint16_t lux;
uint16_t level;
uint16_t lux;
uint16_t level;
} blight_lut[] = {
{ 500, 100 },
{ 2000, 90 },
{ 4000, 80 },
{ 10000, 60 },
{ 20000, 30 },
{ 65535, 0 }
{ 500, 100 },
{ 2000, 90 },
{ 4000, 80 },
{ 10000, 60 },
{ 20000, 30 },
{ 65535, 0 }
};
///> Number of pairs
......@@ -65,49 +65,49 @@ static int8_t cur_idx = -1;
static void auto_backlight_task(void *params)
{
(void) params;
uint32_t lux;
if(!light_sensor_get_lux(&lux)) {
// Look for the right value
uint8_t index;
for(index = 0; index < blight_lut_size; ++index) {
if(lux < blight_lut[index].lux)
break;
}
// Change the backlight level if it is different than the previous one
if(index != cur_idx) {
backlight_set_level(blight_lut[index].level);
cur_idx = index;
}
}
(void) params;
uint32_t lux;
if(!light_sensor_get_lux(&lux)) {
// Look for the right value
uint8_t index;
for(index = 0; index < blight_lut_size; ++index) {
if(lux < blight_lut[index].lux)
break;
}
// Change the backlight level if it is different than the previous one
if(index != cur_idx) {
backlight_set_level(blight_lut[index].level);
cur_idx = index;
}
}
}
void auto_backlight_init(void)
{
timer_handle = xTimerCreate((signed char*) "auto_blight",
BLIGHT_TASK_PERIOD, pdTRUE,
(void*) 0, auto_backlight_task);
timer_handle = xTimerCreate((signed char*) "auto_blight",
BLIGHT_TASK_PERIOD, pdTRUE,
(void*) 0, auto_backlight_task);
}
void auto_backlight_enable(bool enable)
{
if(enable == auto_enabled)
return;
if(enable) {
if(xTimerStart(timer_handle, 0) != pdPASS) {
// TODO kernel panic
}
// Select appropriate backlight level
auto_backlight_task(NULL);
} else {
xTimerStop(timer_handle, 0);
cur_idx = -1;
}
auto_enabled = enable;
if(enable == auto_enabled)
return;
if(enable) {
if(xTimerStart(timer_handle, 0) != pdPASS) {
// TODO kernel panic
}
// Select appropriate backlight level
auto_backlight_task(NULL);
} else {
xTimerStop(timer_handle, 0);
cur_idx = -1;
}
auto_enabled = enable;
}
This diff is collapsed.
......@@ -37,34 +37,34 @@
static portBASE_TYPE gpio_irq_dispatcher(uint32_t flags)
{
// We have not woken a task at the start of the ISR
portBASE_TYPE task_woken1 = pdFALSE;
portBASE_TYPE task_woken2 = pdFALSE;
// Fill the event data
struct event evt;
switch(flags)
{
// Buttons
// We have not woken a task at the start of the ISR
portBASE_TYPE task_woken1 = pdFALSE;
portBASE_TYPE task_woken2 = pdFALSE;
// Fill the event data
struct event evt;
switch(flags)
{
// Buttons
case (1 << 0): // PA0
evt.type = BUTTON_PRESSED;
evt.data.button = BUT_TR;
break;
evt.type = BUTTON_PRESSED;
evt.data.button = BUT_TR;
break;
case (1 << 6): // PC6
evt.type = BUTTON_PRESSED;
evt.data.button = BUT_BL;
break;
evt.type = BUTTON_PRESSED;
evt.data.button = BUT_BL;
break;
case (1 << 7): // PC7
evt.type = BUTTON_PRESSED;
evt.data.button = BUT_TL;
break;
evt.type = BUTTON_PRESSED;
evt.data.button = BUT_TL;
break;
case (1 << 8): // PA8
evt.type = BUTTON_PRESSED;
evt.data.button = BUT_BR;
break;
evt.type = BUTTON_PRESSED;
evt.data.button = BUT_BR;
break;
// Sensors
// There is a conflict with the bottom-left button interrupt
......@@ -74,75 +74,76 @@ static portBASE_TYPE gpio_irq_dispatcher(uint32_t flags)
// break;
case (1 << 10): // PA10
evt.type = SENSOR_INT;
evt.data.sensor = MAGNETOMETER;
break;
evt.type = SENSOR_INT;
evt.data.sensor = MAGNETOMETER;
break;
case (1 << 5): // PD5
evt.type = SENSOR_INT;
evt.data.sensor = ACCELEROMETER;
break;
evt.type = SENSOR_INT;
evt.data.sensor = ACCELEROMETER;
break;
// Unexpected event, do not send it
default: return pdFALSE;
}
default:
return pdFALSE;
}
// Post the event to the back of the queue
xQueueSendToBackFromISR(appQueue, &evt, &task_woken1);
// Post the event to the back of the queue
xQueueSendToBackFromISR(appQueue, &evt, &task_woken1);
// Switch to active state if a button was pressed
if(evt.type == BUTTON_PRESSED)
reset_active_irq(&task_woken2);
// Switch to active state if a button was pressed
if(evt.type == BUTTON_PRESSED)
reset_active_irq(&task_woken2);
return task_woken1 || task_woken2;
return task_woken1 || task_woken2;
}
void GPIO_EVEN_IRQHandler(void)
{
uint32_t iflags;
uint32_t iflags;
// Get all even interrupts
iflags = GPIO_IntGetEnabled() & 0x00005555;
// Get all even interrupts
iflags = GPIO_IntGetEnabled() & 0x00005555;
// Clean only even interrupts
GPIO_IntClear(iflags);
// Clean only even interrupts
GPIO_IntClear(iflags);
portEND_SWITCHING_ISR(gpio_irq_dispatcher(iflags));
portEND_SWITCHING_ISR(gpio_irq_dispatcher(iflags));
}
void GPIO_ODD_IRQHandler(void)
{
uint32_t iflags;
uint32_t iflags;
// Get all odd interrupts
iflags = GPIO_IntGetEnabled() & 0x0000AAAA;
// Get all odd interrupts
iflags = GPIO_IntGetEnabled() & 0x0000AAAA;
// Clean only odd interrupts
GPIO_IntClear(iflags);
// Clean only odd interrupts
GPIO_IntClear(iflags);
portEND_SWITCHING_ISR(gpio_irq_dispatcher(iflags));
portEND_SWITCHING_ISR(gpio_irq_dispatcher(iflags));
}
void BURTC_IRQHandler(void)
{
// We have not woken a task at the start of the ISR
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
// We have not woken a task at the start of the ISR
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
rtc_tick();
rtc_tick();
// Fill the event data
struct event evt;
evt.type = RTC_TICK;
// Fill the event data
struct event evt;
evt.type = RTC_TICK;
// Post the byte to the back of the queue
xQueueSendToBackFromISR(appQueue, &evt, &xHigherPriorityTaskWoken);
// Post the byte to the back of the queue
xQueueSendToBackFromISR(appQueue, &evt, &xHigherPriorityTaskWoken);
BURTC_IntClear(BURTC_IFC_COMP0);
BURTC_IntClear(BURTC_IFC_COMP0);
portEND_SWITCHING_ISR(xHigherPriorityTaskWoken);
portEND_SWITCHING_ISR(xHigherPriorityTaskWoken);
}
void HardFault_Handler()
{
SCB->AIRCR = 0x05FA0004;
SCB->AIRCR = 0x05FA0004;
}
......@@ -59,53 +59,53 @@ xSemaphoreHandle mutexSdCardAccess;
int main(void)
{
// Chip errata
CHIP_Init();
// Chip errata
CHIP_Init();
// Enable clocks
CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFXO);
CMU_OscillatorEnable(cmuOsc_LFXO, true, false);
CMU_ClockEnable(cmuClock_HFPER, true);
CMU_ClockEnable(cmuClock_GPIO, true);
// Enable clocks
CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFXO);
CMU_OscillatorEnable(cmuOsc_LFXO, true, false);
CMU_ClockEnable(cmuClock_HFPER, true);
CMU_ClockEnable(cmuClock_GPIO, true);
I2C_Init_TypeDef i2cInit = I2C_INIT_DEFAULT;
I2CDRV_Init(&i2cInit);
I2C_Init_TypeDef i2cInit = I2C_INIT_DEFAULT;
I2CDRV_Init(&i2cInit);
mutexSdCardAccess = xSemaphoreCreateMutex();
mutexSdCardAccess = xSemaphoreCreateMutex();
MSC_Init();
setting_init();
MSC_Init();
setting_init();
#ifdef DEBUG
usbdbg_init();
usbdbg_init();
#endif
backlight_init();
buttons_init();
buzzer_init();
vibra_init();
rtc_init();
lcd_init();
gps_init(setting_get(&setting_gps_on));
ui_init();
auto_backlight_init();
battery_monitor_init();
state_init();
gpsbkgnd_init();
// Initialize SLEEP driver, no callbacks are used
SLEEP_Init(NULL, NULL);
backlight_init();
buttons_init();
buzzer_init();
vibra_init();
rtc_init();
lcd_init();
gps_init(setting_get(&setting_gps_on));
ui_init();
auto_backlight_init();
battery_monitor_init();
state_init();
gpsbkgnd_init();
// Initialize SLEEP driver, no callbacks are used
SLEEP_Init(NULL, NULL);
#if (configSLEEP_MODE < 3)
// do not let to sleep deeper than define
SLEEP_SleepBlockBegin((SLEEP_EnergyMode_t)(configSLEEP_MODE+1));
// do not let to sleep deeper than define
SLEEP_SleepBlockBegin((SLEEP_EnergyMode_t)(configSLEEP_MODE+1));
#endif
startMain(&menu);
startMain(&menu);
// Start FreeRTOS Scheduler
vTaskStartScheduler();
// Start FreeRTOS Scheduler
vTaskStartScheduler();
return 0;
return 0;
}
......@@ -42,53 +42,53 @@ static void stop_active(void *params);
void state_init(void)
{
timer_handle = xTimerCreate((signed char*) "active_state",
ACTIVE_STATE_TICKS, pdTRUE,
(void*) 0, stop_active);
timer_handle = xTimerCreate((signed char*) "active_state",
ACTIVE_STATE_TICKS, pdTRUE,
(void*) 0, stop_active);
reset_active();
reset_active();
}
enum watch_state get_state(void)
{
return current_state;
return current_state;
}
void reset_active(void)
{
xTimerReset(timer_handle, 0);
state_handler(ACTIVE);
xTimerReset(timer_handle, 0);
state_handler(ACTIVE);
}
void reset_active_irq(portBASE_TYPE *task_woken)
{
xTimerResetFromISR(timer_handle, task_woken);
state_handler(ACTIVE);
xTimerResetFromISR(timer_handle, task_woken);
state_handler(ACTIVE);
}
static void stop_active(void *params)
{
(void) params;
(void) params;
xTimerStop(timer_handle, 0);
state_handler(IDLE);
xTimerStop(timer_handle, 0);
state_handler(IDLE);
}
static void state_handler(enum watch_state state)
{
if(state == current_state)
return;
if(state == current_state)
return;
switch(state) {
switch(state) {
case ACTIVE:
auto_backlight_enable(true);
break;
auto_backlight_enable(true);
break;
case IDLE:
auto_backlight_enable(false);
backlight_set_level(0);
break;
auto_backlight_enable(false);
backlight_set_level(0);
break;
}
current_state = state;
current_state = state;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment