summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Schodet2010-06-17 00:44:17 +0200
committerNicolas Schodet2010-06-20 19:09:38 +0200
commit9c9b1e87e7bca6ba0c5c101b0b76a514a820ea7c (patch)
tree89b791b65ca2d4f9283abe899957e792cab7c65f
parent35370c5301abeede6017e971a44c7b3200d40aa7 (diff)
bwbootloader: add start delay
-rw-r--r--src/bwbootloader/bwbootloader.c25
-rw-r--r--src/bwbootloader/timer.h54
2 files changed, 73 insertions, 6 deletions
diff --git a/src/bwbootloader/bwbootloader.c b/src/bwbootloader/bwbootloader.c
index b7cc283..82644ca 100644
--- a/src/bwbootloader/bwbootloader.c
+++ b/src/bwbootloader/bwbootloader.c
@@ -28,21 +28,31 @@
#include "bwbootloader.h"
#include "prog.h"
+#include "timer.h"
#include <string.h>
-/** Start application when 1. */
-volatile uint8_t start;
+/** Start application when it reach zero. */
+volatile uint8_t start_delay;
+/** Stop start delay counter when 1. */
+volatile uint8_t start_delay_pause;
int
main (void)
{
twi_init (0xb8);
- while (!start)
+ timer_init ();
+ /* Handle bootloader messages. */
+ start_delay = 40;
+ while (start_delay)
+ {
twi_update ();
- /* Small delay for acknowledge. */
- utils_delay_ms (500);
+ if (!start_delay_pause && timer_overflowed ())
+ start_delay--;
+ }
+ /* Stop bootloader, start application. */
twi_uninit ();
+ timer_uninit ();
prog_start ();
return 0;
}
@@ -50,6 +60,8 @@ main (void)
void
bwbootloader_recv (const uint8_t *buffer, uint8_t size)
{
+ /* Stop start countdown. */
+ start_delay_pause = 1;
/* Command is the first byte, it will be repeated as acknowledgement.
* Words are little endian. */
static uint8_t rbuffer[AC_TWI_SLAVE_SEND_BUFFER_SIZE];
@@ -76,7 +88,8 @@ bwbootloader_recv (const uint8_t *buffer, uint8_t size)
/* Start application. */
if (size != 1)
break;
- start = 1;
+ start_delay_pause = 0;
+ start_delay = 4;
twi_slave_update (buffer, 1);
return;
case BWBOOTLOADER_READ:
diff --git a/src/bwbootloader/timer.h b/src/bwbootloader/timer.h
new file mode 100644
index 0000000..8f03a86
--- /dev/null
+++ b/src/bwbootloader/timer.h
@@ -0,0 +1,54 @@
+#ifndef timer_h
+#define timer_h
+/* timer.h */
+/* binwatch - Tiny binary wristwatch. {{{
+ *
+ * Copyright (C) 2010 Nicolas Schodet
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Contact :
+ * Web: http://ni.fr.eu.org/
+ * Email: <nico at ni.fr.eu.org>
+ * }}} */
+
+static inline void
+timer_init (void)
+{
+ TCCR0B = 0b101;
+}
+
+static inline void
+timer_uninit (void)
+{
+ TCCR0B = 0;
+ TCNT0 = 0;
+ GTCCR = _BV (PSR0);
+ TIFR = _BV (TOV0);
+}
+
+static inline uint8_t
+timer_overflowed (void)
+{
+ if (TIFR & _BV (TOV0))
+ {
+ TIFR = _BV (TOV0);
+ return 1;
+ }
+ else
+ return 0;
+}
+
+#endif /* timer_h */