From 9e58c8bcb7aa4c7c8a84368c0f7be5448c122fbb Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Thu, 16 Dec 2010 18:21:24 +0100 Subject: [PATCH] *watch: chronos watch application initial checkin --- cpu/cc430/cc430-rtc.c | 35 ++++++++++---- drivers/include/rtc.h | 4 ++ projects/watch/Jamfile | 5 ++ projects/watch/alarm_app.c | 62 ++++++++++++++++++++++++ projects/watch/alarm_app.h | 6 +++ projects/watch/clock_app.c | 58 ++++++++++++++++++++++ projects/watch/clock_app.h | 7 +++ projects/watch/main.c | 83 ++++++++++++++++++++++++++++++++ projects/watch/tests/hello-world | 13 +++++ projects/watch/watch.h | 9 ++++ 10 files changed, 272 insertions(+), 10 deletions(-) create mode 100644 projects/watch/Jamfile create mode 100644 projects/watch/alarm_app.c create mode 100644 projects/watch/alarm_app.h create mode 100644 projects/watch/clock_app.c create mode 100644 projects/watch/clock_app.h create mode 100644 projects/watch/main.c create mode 100755 projects/watch/tests/hello-world create mode 100644 projects/watch/watch.h diff --git a/cpu/cc430/cc430-rtc.c b/cpu/cc430/cc430-rtc.c index 3ef3d7c27..8895e0599 100644 --- a/cpu/cc430/cc430-rtc.c +++ b/cpu/cc430/cc430-rtc.c @@ -33,11 +33,16 @@ and the mailinglist (subscription via web site) //static volatile time_t epoch; static struct tm time_to_set; +static int set_time = 0; +int rtc_second_pid = 0; /*---------------------------------------------------------------------------*/ void rtc_init(void) { /* Set to calendar mode */ RTCCTL1 |= RTCMODE_H; + + /* enable ready interrupt (every second) */ + RTCCTL0 |= RTCRDYIE; } /*---------------------------------------------------------------------------*/ @@ -59,7 +64,8 @@ void rtc_set_localtime(struct tm* localt) { /* copy time to be set */ memcpy(&time_to_set, localt, sizeof(struct tm)); /* set interrupt to set this time after the next transition */ - RTCCTL0 |= RTCRDYIE; +// RTCCTL0 |= RTCRDYIE; + set_time = 1; } /*--------------------------------------------------------------------------- @@ -166,16 +172,25 @@ interrupt(RTC_VECTOR) __attribute__ ((naked)) rtc_isr(void) { /* RTC is save to write for up to one second now */ if (RTCIV == RTC_RTCRDYIFG) { /* disable interrupt */ - RTCCTL0 &= ~RTCRDYIE; + //RTCCTL0 &= ~RTCRDYIE; + + if (set_time) { + set_time = 0; /* set previous set time and reset it */ - RTCSEC = time_to_set.tm_sec; - RTCMIN = time_to_set.tm_min; - RTCHOUR = time_to_set.tm_hour; - RTCDAY = time_to_set.tm_mday; - RTCDOW = time_to_set.tm_wday; - RTCMON = time_to_set.tm_mon + 1; - RTCYEARL = (time_to_set.tm_year + 1900) & 0xFF; - RTCYEARH = (time_to_set.tm_year + 1900) >> 0x08; + RTCSEC = time_to_set.tm_sec; + RTCMIN = time_to_set.tm_min; + RTCHOUR = time_to_set.tm_hour; + RTCDAY = time_to_set.tm_mday; + RTCDOW = time_to_set.tm_wday; + RTCMON = time_to_set.tm_mon + 1; + RTCYEARL = (time_to_set.tm_year + 1900) & 0xFF; + RTCYEARH = (time_to_set.tm_year + 1900) >> 0x08; + } + if (rtc_second_pid) { + msg m; + m.type = RTC_SECOND; + msg_send_int(&m, rtc_second_pid); + } } /* RTC alarm */ else if (RTCIV == RTC_RTCAIFG) { diff --git a/drivers/include/rtc.h b/drivers/include/rtc.h index d36382cfd..b40b72b50 100644 --- a/drivers/include/rtc.h +++ b/drivers/include/rtc.h @@ -27,6 +27,8 @@ and the mailinglist (subscription via web site) #ifndef RTC_H #define RTC_H +#define RTC_SECOND 10001U + #include /** @@ -56,4 +58,6 @@ void rtc_set_localtime(struct tm* localt); */ void rtc_get_localtime(struct tm* localt); +extern int rtc_second_pid; + #endif diff --git a/projects/watch/Jamfile b/projects/watch/Jamfile new file mode 100644 index 000000000..d3484567e --- /dev/null +++ b/projects/watch/Jamfile @@ -0,0 +1,5 @@ +SubDir TOP projects watch ; + +Module watch : main.c clock_app.c alarm_app.c : uart0 posix_io rtc display_putchar gpioint hwtimer board_buzzer auto_init ; + +UseModule watch ; diff --git a/projects/watch/alarm_app.c b/projects/watch/alarm_app.c new file mode 100644 index 000000000..7a9f206bb --- /dev/null +++ b/projects/watch/alarm_app.c @@ -0,0 +1,62 @@ +#include +#include + +#include +#include +#include +#include + +#include "alarm_app.h" +#include "clock_app.h" +#include "watch.h" + +static char alarm_stack[KERNEL_CONF_STACKSIZE_DEFAULT]; + +static void alarm_thread(void) { + msg m; + + struct tm time; + + time.tm_sec = 1; + time.tm_min = 2; + time.tm_hour = 3; + + int active = 0; + + while(1) { + msg_receive(&m); + switch (m.type) { + case MSG_ACTIVATE: + { + time_print(&time); + if (active) { + } else { + } + break; + } + case MSG_DEACTIVATE: + { + break; + } + case MSG_BUTTON_HASH: + { + if (active) { + active = 0; + display_symbol(LCD_ICON_ALARM, SEG_OFF); + } else { + active = 1; + display_symbol(LCD_ICON_ALARM, SEG_ON); + } + break; + } + default: + { + printf("def alarm\n"); + } + } + } +} + +int alarm_app_init() { + return thread_create(alarm_stack, sizeof(alarm_stack), PRIORITY_MAIN-1, CREATE_STACKTEST, alarm_thread, "alarm"); +} diff --git a/projects/watch/alarm_app.h b/projects/watch/alarm_app.h new file mode 100644 index 000000000..569be69b0 --- /dev/null +++ b/projects/watch/alarm_app.h @@ -0,0 +1,6 @@ +#ifndef __ALARM_APP_H +#define __ALARM_APP_H + +int alarm_app_init(); + +#endif /* __ALARM_APP_H */ diff --git a/projects/watch/clock_app.c b/projects/watch/clock_app.c new file mode 100644 index 000000000..761ebe17f --- /dev/null +++ b/projects/watch/clock_app.c @@ -0,0 +1,58 @@ +#include +#include + +#include +#include +#include + +#include "clock_app.h" +#include "watch.h" + +void time_print(struct tm *time) { + printf("%02i %02i%02i\n", time->tm_sec, time->tm_hour, time->tm_min); +} + +static char clock_stack[KERNEL_CONF_STACKSIZE_DEFAULT]; + +static void clock_thread(void) { + msg m; + + int active = 0; + rtc_second_pid = thread_getpid(); + + while(1) { + msg_receive(&m); + switch (m.type) { + case RTC_SECOND: + { + if (! active) break; + } + case MSG_ACTIVATE: + { + active = 1; + struct tm now; + rtc_get_localtime(&now); + time_print(&now); + break; + } + case MSG_DEACTIVATE: + { + active = 0; + break; + } + case MSG_BUTTON_HASH: + { + printf("hashclock\n"); + break; + } + default: + { + printf("def clock\n"); + } + } + } +} + +int clock_app_init() { + return thread_create(clock_stack, sizeof(clock_stack), PRIORITY_MAIN-1, CREATE_STACKTEST, clock_thread, "clock"); +} diff --git a/projects/watch/clock_app.h b/projects/watch/clock_app.h new file mode 100644 index 000000000..084f58866 --- /dev/null +++ b/projects/watch/clock_app.h @@ -0,0 +1,7 @@ +#ifndef __CLOCK_APP_H +#define __CLOCK_APP_H + +int clock_app_init(); +void time_print(struct tm *time); + +#endif /* __CLOCK_APP_H */ diff --git a/projects/watch/main.c b/projects/watch/main.c new file mode 100644 index 000000000..e67dbe601 --- /dev/null +++ b/projects/watch/main.c @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include + +#include "watch.h" +#include "alarm_app.h" +#include "clock_app.h" + +#define NUM_APPS 2 +int napps = NUM_APPS; +int apps[NUM_APPS]; + +int button_thread = 0; +void button_star(void) { + msg m; + + if (button_thread) { + m.type = MSG_BUTTON_STAR; + msg_send(&m, button_thread, false); + } +} + +int main(void) +{ + memset(apps, '\0', sizeof(apps)); + apps[0] = clock_app_init(); + apps[1] = alarm_app_init(); + + gpioint_set(2, BUTTON_STAR_PIN, (GPIOINT_RISING_EDGE | GPIOINT_DEBOUNCE), button_star); + button_thread = thread_getpid(); + + int active_app = 0; + + msg m; + + //buzzer_beep(15, 5000); + + printf("ukleos\n"); + + m.type = MSG_ACTIVATE; + msg_send(&m, apps[active_app], true); + + while(1) { + msg_receive(&m); + + switch (m.type) { + case MSG_BUTTON_STAR: { + m.type = MSG_DEACTIVATE; + msg_send(&m, apps[active_app], true); + + active_app++; + + if (active_app == (NUM_APPS)) active_app = 0; + + m.type = MSG_ACTIVATE; + msg_send(&m, apps[active_app], true); + + // buzzer_beep(15, 5000); + + break; + } + case MSG_BUTTON_HASH: + { + m.type = MSG_BUTTON_HASH; + msg_send(&m, apps[active_app], true); + break; + } + default: + { + printf("msg\n"); + } + } + } +} diff --git a/projects/watch/tests/hello-world b/projects/watch/tests/hello-world new file mode 100755 index 000000000..acde8265f --- /dev/null +++ b/projects/watch/tests/hello-world @@ -0,0 +1,13 @@ +#!/usr/bin/expect + +set timeout 5 + +spawn pseudoterm $env(PORT) + +expect { + "Hello World!" {} + timeout { exit 1 } +} + +puts "\nTest successful!\n" + diff --git a/projects/watch/watch.h b/projects/watch/watch.h new file mode 100644 index 000000000..c2cd7ddbe --- /dev/null +++ b/projects/watch/watch.h @@ -0,0 +1,9 @@ +#ifndef __WATCH_H +#define __WATCH_H + +#define MSG_ACTIVATE 0 +#define MSG_DEACTIVATE 1 +#define MSG_BUTTON_STAR 2 +#define MSG_BUTTON_HASH 3 + +#endif /* __WATCH_H */