From cae1a47905b0a5145a6aebb7875b3a46b5bdd407 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Fri, 24 Jan 2014 20:29:34 +0100 Subject: move debugging and panic implementation to tshield --- Makefile | 6 +- kernel/bug/debug.c | 6 -- kernel/bug/include/bug/debug.h | 2 - kernel/bug/panic.c | 23 ------ kernel/sched/idle.c | 1 - kernel/sched/include/sched/sched.h | 12 +-- kernel/sched/mcu/atmega2560/context.c | 1 - kernel/sched/sched.c | 4 +- kernel/tshield/include/tshield/tshield.h | 3 +- kernel/tshield/mcu/atmega2560/tshield.c | 138 +++++++++++++++++++++++++++++++ kernel/tshield/tshield.c | 115 -------------------------- main.c | 9 +- 12 files changed, 156 insertions(+), 164 deletions(-) delete mode 100644 kernel/bug/debug.c delete mode 100644 kernel/bug/panic.c create mode 100644 kernel/tshield/mcu/atmega2560/tshield.c delete mode 100644 kernel/tshield/tshield.c diff --git a/Makefile b/Makefile index 200e467..b0fd7c0 100644 --- a/Makefile +++ b/Makefile @@ -83,11 +83,11 @@ kernel/%.o: kernel/%.c # Utility rules -upload: target - $(AVRDUDE) $(AVRDUDEFLAGS) -U flash:w:$(TARGET).hex:i - size: target $(AVRSIZE) --format=avr --mcu=$(MCU) $(TARGET).elf + +upload: target + $(AVRDUDE) $(AVRDUDEFLAGS) -U flash:w:$(TARGET).hex:i monitor: cu -l $(SERIAL) -s $(BAUD) --parity=none -h diff --git a/kernel/bug/debug.c b/kernel/bug/debug.c deleted file mode 100644 index 10a67d8..0000000 --- a/kernel/bug/debug.c +++ /dev/null @@ -1,6 +0,0 @@ -#include "bug/debug.h" -#include "tshield/tshield.h" - -void debug_led(int led, int value) { - tshield_led(led, value); -} \ No newline at end of file diff --git a/kernel/bug/include/bug/debug.h b/kernel/bug/include/bug/debug.h index 937ff2c..854f4c6 100644 --- a/kernel/bug/include/bug/debug.h +++ b/kernel/bug/include/bug/debug.h @@ -1,8 +1,6 @@ #ifndef DEBUG_H #define DEBUG_H -#define DEBUG_LEDS 4 - void debug_led(int led, int value); #endif \ No newline at end of file diff --git a/kernel/bug/panic.c b/kernel/bug/panic.c deleted file mode 100644 index c82d0f0..0000000 --- a/kernel/bug/panic.c +++ /dev/null @@ -1,23 +0,0 @@ -#include "bug/panic.h" -#include "bug/debug.h" -#include - -static inline void wait() { - for (volatile long i = 0; i < 20000; ++i) {}; -} - -void panic() { - cli(); - while(1) { - for(int i = 0; i < DEBUG_LEDS; ++i) { - debug_led((i - 1) % DEBUG_LEDS, 0); - debug_led(i, 1); - wait(); - } - for(int i = DEBUG_LEDS - 1; i >= 0; --i) { - debug_led((i + 1) % DEBUG_LEDS, 0); - debug_led(i, 1); - wait(); - } - } -} \ No newline at end of file diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c index 18ccdd5..d55d694 100644 --- a/kernel/sched/idle.c +++ b/kernel/sched/idle.c @@ -6,7 +6,6 @@ void idle_entry(char args) { while(1) { - tshield_led(TSHIELD_LED_IDLE,1); set_sleep_mode(SLEEP_MODE_IDLE); cli(); sleep_enable(); diff --git a/kernel/sched/include/sched/sched.h b/kernel/sched/include/sched/sched.h index d63b78c..a7f45ee 100644 --- a/kernel/sched/include/sched/sched.h +++ b/kernel/sched/include/sched/sched.h @@ -36,7 +36,7 @@ struct tcb_t { * Note: for a task to be scheduled, it must first be spawned (see spawn()). */ #define DECLARE_TASK(name, stack_size, entry_function) \ - static char volatile declared_stack_##name[stack_size]; \ + static char declared_stack_##name[stack_size]; \ static struct tcb_t name = { \ .sp = 0, \ .mem_low = declared_stack_##name, \ @@ -86,6 +86,11 @@ static inline void wake_all(struct list_head* queue) { */ void sched_init(); +/** + * Enters the scheduler, setting current to the next runnable task. + */ +void schedule(); + /** * Ticks the scheduler. */ @@ -93,11 +98,6 @@ inline void sched_tick() { schedule(); //in a round-robin scheduler, scheduling is called after every tick } -/** - * Enters the scheduler, setting current to the next runnable task. - */ -void schedule(); - /** * Initializes a given task and adds it to the ready queue. */ diff --git a/kernel/sched/mcu/atmega2560/context.c b/kernel/sched/mcu/atmega2560/context.c index 7a88b33..ef7e172 100644 --- a/kernel/sched/mcu/atmega2560/context.c +++ b/kernel/sched/mcu/atmega2560/context.c @@ -4,7 +4,6 @@ char* init_stack(const char* const mem_low, const char* const mem_high, void (*e char* sp = (char*) mem_high; unsigned long address = (unsigned long) entry; - *sp = (char) 0x1; sp--; *sp = (char) 0x2; diff --git a/kernel/sched/sched.c b/kernel/sched/sched.c index 8406b8d..bc3b30d 100644 --- a/kernel/sched/sched.c +++ b/kernel/sched/sched.c @@ -37,11 +37,11 @@ void spawn(struct tcb_t* const tcb, char args) { list_add_tail(&tcb->q, &ready); } -void yield(void) { +void yield() { SAVE_CONTEXT(); schedule(); RESTORE_CONTEXT(); - asm volatile ( "ret" ); + RETURN(); } void freeze() { diff --git a/kernel/tshield/include/tshield/tshield.h b/kernel/tshield/include/tshield/tshield.h index 2624131..b485ad2 100644 --- a/kernel/tshield/include/tshield/tshield.h +++ b/kernel/tshield/include/tshield/tshield.h @@ -32,7 +32,8 @@ * */ - #define TSHIELD_LED_IDLE 0 +#define DEBUG_LEDS 4 +#define DEBUG_LED_IDLE 0 void tshield_init(); diff --git a/kernel/tshield/mcu/atmega2560/tshield.c b/kernel/tshield/mcu/atmega2560/tshield.c new file mode 100644 index 0000000..4c1e150 --- /dev/null +++ b/kernel/tshield/mcu/atmega2560/tshield.c @@ -0,0 +1,138 @@ +#include +#include "tshield/tshield.h" + +static void tshield_init_servo(); + +void tshield_init() { + + DDRH |= (1 << 3) | (1 << 4); //leds + PORTH &= ~( (1 << 3) | (1 << 4)); + DDRE |= (1 << 3); + PORTE &= ~(1 << 3); + DDRG |= (1 << 5); + PORTG &= ~(1 << 5); + + DDRE &= ~(1 << 4); // buttons + PORTE |= (1 << 4); + DDRH &= ~(1 << 5); + PORTH |= (1 << 5); + DDRB &= ~(1 << 4); + PORTB |= (1 << 4); + + DDRB |= (1 << 6); + PORTB &= ~(1 << 6); + + tshield_init_servo(); +} + +#define WAIT_VALUE 20000 +#define WAIT() \ + for (volatile long x = 0; x < WAIT_VALUE; ++x){} + +void tshield_test() { + while(1) { + PORTG |= (1 << 5); + WAIT(); + PORTG &= ~(1 << 5); + PORTE |= (1 << 3); + WAIT(); + PORTE &= ~(1 << 3); + PORTH |= (1 << 3); + WAIT(); + PORTH &= ~(1 << 3); + PORTH |= (1 << 4); + WAIT(); + PORTH &= ~(1 << 4); + } + +} + +unsigned char tshield_read() { + return ((PINE & (1 << 4) ? 0 : 1) << 2 ) | + ((PINH & (1 << 5) ? 0 : 1) << 1 ) | + (PINB & (1 << 4) ? 0 : 1); +} + +void tshield_led(unsigned char led, unsigned char value) { + volatile unsigned char* port; + unsigned char bit; + switch (led) { + case 0: + port = &PORTG; + bit = 5; + break; + case 1: + port = &PORTE; + bit = 3; + break; + case 2: + port = &PORTH; + bit = 3; + break; + case 3: + port = &PORTH; + bit = 4; + break; + default: + return; + } + + if (value) { + *port |= (1 << bit); + } else { + *port &= ~(1 << bit); + } + +} + +void tshield_pp(unsigned char value) { + if (value) { + PORTB |= (1 << 6); + } else { + PORTB &= ~(1 << 6); + } +} + +void tshield_init_servo() { + + TCCR1A |= (1<= 0; --i) { + debug_led((i + 1) % DEBUG_LEDS, 0); + debug_led(i, 1); + _wait(); + } + } +} \ No newline at end of file diff --git a/kernel/tshield/tshield.c b/kernel/tshield/tshield.c deleted file mode 100644 index ccf234d..0000000 --- a/kernel/tshield/tshield.c +++ /dev/null @@ -1,115 +0,0 @@ -#include -#include "tshield/tshield.h" - -static void tshield_init_servo(); - -void tshield_init() { - - DDRH |= (1 << 3) | (1 << 4); //leds - PORTH &= ~( (1 << 3) | (1 << 4)); - DDRE |= (1 << 3); - PORTE &= ~(1 << 3); - DDRG |= (1 << 5); - PORTG &= ~(1 << 5); - - DDRE &= ~(1 << 4); // buttons - PORTE |= (1 << 4); - DDRH &= ~(1 << 5); - PORTH |= (1 << 5); - DDRB &= ~(1 << 4); - PORTB |= (1 << 4); - - DDRB |= (1 << 6); - PORTB &= ~(1 << 6); - - tshield_init_servo(); - -} - -#define WAIT_VALUE 20000 -#define WAIT() \ - for (volatile long x = 0; x < WAIT_VALUE; ++x){} - -void tshield_test() { - while(1) { - PORTG |= (1 << 5); - WAIT(); - PORTG &= ~(1 << 5); - PORTE |= (1 << 3); - WAIT(); - PORTE &= ~(1 << 3); - PORTH |= (1 << 3); - WAIT(); - PORTH &= ~(1 << 3); - PORTH |= (1 << 4); - WAIT(); - PORTH &= ~(1 << 4); - } - -} - -unsigned char tshield_read() { - return ((PINE & (1 << 4) ? 0 : 1) << 2 ) | - ((PINH & (1 << 5) ? 0 : 1) << 1 ) | - (PINB & (1 << 4) ? 0 : 1); -} - -void tshield_led(unsigned char led, unsigned char value) { - volatile unsigned char* port; - unsigned char bit; - switch (led) { - case 0: - port = &PORTG; - bit = 5; - break; - case 1: - port = &PORTE; - bit = 3; - break; - case 2: - port = &PORTH; - bit = 3; - break; - case 3: - port = &PORTH; - bit = 4; - break; - default: - return; - } - - if (value) { - *port |= (1 << bit); - } else { - *port &= ~(1 << bit); - } - -} - -void tshield_pp(unsigned char value) { - if (value) { - PORTB |= (1 << 6); - } else { - PORTB &= ~(1 << 6); - } -} - -void tshield_init_servo() { - - TCCR1A |= (1< #include #include +#include #define WAIT_CYCLES(cycles) for (volatile unsigned long i = 0; i < cycles; ++i) {} void blink( char id) { while(1) { debug_led(id,1); - //yield(); - WAIT_CYCLES(20000); + yield(); + WAIT_CYCLES(5000); debug_led(id,0); - //yield(); - WAIT_CYCLES(20000); + yield(); + WAIT_CYCLES(5000); } } -- cgit v1.2.3