summaryrefslogtreecommitdiff
path: root/test_general/ecos/src/timer_int.c
diff options
context:
space:
mode:
Diffstat (limited to 'test_general/ecos/src/timer_int.c')
-rw-r--r--test_general/ecos/src/timer_int.c137
1 files changed, 137 insertions, 0 deletions
diff --git a/test_general/ecos/src/timer_int.c b/test_general/ecos/src/timer_int.c
new file mode 100644
index 0000000000..5c729e1037
--- /dev/null
+++ b/test_general/ecos/src/timer_int.c
@@ -0,0 +1,137 @@
+/* Cesar project {{{
+ *
+ * Copyright (C) 2008 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file timer_int.c
+ * \brief « brief description »
+ * \ingroup « module »
+ *
+ * « long description »
+ */
+
+#include <cyg/kernel/kapi.h>
+#include <cyg/hal/drv_api.h>
+#include <cyg/infra/diag.h>
+
+//
+// Local defines
+//
+#define MY_THREAD_STACK_SIZE (4096 / sizeof(int))
+#define CYGNUM_HAL_PRI_HIGH 0
+#define TIMER2_INT CYGNUM_HAL_INTERRUPT_9
+#define TIMER2_CONF 0x80000058
+#define TIMER2_RELOAD 0x80000054
+
+//
+// Static variables
+//
+static int my_thread_stack[MY_THREAD_STACK_SIZE];
+static cyg_handle_t my_thread_handle;
+static cyg_thread my_thread_obj;
+
+static cyg_interrupt inttimer;
+static cyg_handle_t inttimer_handle;
+static cyg_sem_t data_ready;
+
+//
+// Thread.
+//
+void my_thread(cyg_addrword_t index)
+{
+ while(1)
+ {
+ diag_write_string("Waiting timer2 semaphore...\n");
+ // Wait semaphore
+ cyg_semaphore_wait(&data_ready);
+ diag_write_string("Thank you timer2 DSR, I will continue\n\n");
+ }
+}
+
+//
+// Interrupt service routine for interrupt timer2.
+//
+cyg_uint32 interrupt_timer_isr(
+ cyg_vector_t vector,
+ cyg_addrword_t data)
+{
+ // Block this interrupt from occurring until
+ // the DSR completes.
+ cyg_drv_interrupt_mask(vector);
+
+ // Tell the processor that we have received
+ // the interrupt.
+ cyg_drv_interrupt_acknowledge(vector);
+
+ diag_write_char('i');
+ // Tell the kernel that chained interrupt processing
+ // is done and the DSR needs to be executed next.
+ return CYG_ISR_CALL_DSR;
+}
+
+//
+// Deferred service routine for interrupt timer2.
+//
+void interrupt_timer_dsr(
+ cyg_vector_t vector,
+ cyg_ucount32 count,
+ cyg_addrword_t data)
+{
+ // Signal the thread to run for further processing.
+ cyg_semaphore_post(&data_ready);
+
+ // Allow this interrupt to occur again.
+ cyg_drv_interrupt_unmask(vector);
+}
+
+//
+// Main.
+//
+void cyg_user_start(void)
+{
+ cyg_vector_t inttimer_vector = TIMER2_INT;
+ cyg_priority_t inttimer_priority = CYGNUM_HAL_PRI_HIGH;
+ unsigned int *timer2_conf = (unsigned int*)TIMER2_CONF;
+ unsigned int *timer2_reload = (unsigned int*)TIMER2_RELOAD;
+
+
+ // Thread Creation
+ cyg_thread_create(12, my_thread, (cyg_addrword_t) 0,
+ "My Thread", &my_thread_stack, MY_THREAD_STACK_SIZE,
+ &my_thread_handle, &my_thread_obj);
+
+ // Configure the timer2
+ *timer2_reload = (5000000 - 1); //corresponding to 2seconds (1us * 5000000)
+ *timer2_conf = 0x3; //enable the timer2 with autoreload mode
+
+ // Initialize the semaphore used for interrupt timer2.
+ cyg_semaphore_init(&data_ready, 0);
+
+ // Create interrupt timer2
+ cyg_drv_interrupt_create(
+ inttimer_vector,
+ inttimer_priority,
+ 0,
+ &interrupt_timer_isr,
+ &interrupt_timer_dsr,
+ &inttimer_handle,
+ &inttimer);
+
+ // Attach the interrupt created to the vector.
+ cyg_drv_interrupt_attach(inttimer_handle);
+
+ // Unmask the interrupt we just configured.
+ diag_write_string("Unmasking Interrupts....\n");
+ cyg_drv_interrupt_unmask(inttimer_vector);
+
+ // Starting Thread
+ diag_write_string("Starting Thread....\n\n");
+ cyg_thread_resume(my_thread_handle);
+
+ // Starting Scheduler for Thread and DSR
+ cyg_scheduler_start();
+}
+