Shorter critical sections in examples.
authorFlorian Pose <fp@igh-essen.com>
Mon, 08 Jan 2007 08:32:10 +0000
changeset 512 24292123d174
parent 511 73fb3c35e53d
child 513 d91976aea8c8
Shorter critical sections in examples.
examples/mini/mini.c
examples/msr/msr_sample.c
examples/rtai/rtai_sample.c
--- a/examples/mini/mini.c	Mon Jan 08 08:23:18 2007 +0000
+++ b/examples/mini/mini.c	Mon Jan 08 08:32:10 2007 +0000
@@ -32,7 +32,6 @@
  *****************************************************************************/
 
 #include <linux/module.h>
-#include <linux/delay.h>
 #include <linux/timer.h>
 #include <linux/spinlock.h>
 #include <linux/interrupt.h>
@@ -73,35 +72,36 @@
 void run(unsigned long data)
 {
     static unsigned int counter = 0;
-    static unsigned int einaus = 0;
-
+    static unsigned int blink = 0;
+
+    // receive
     spin_lock(&master_lock);
-
-    // receive
     ecrt_master_receive(master);
     ecrt_domain_process(domain1);
+    spin_unlock(&master_lock);
 
     // process data
-    //k_pos = EC_READ_U32(r_ssi);
+    // k_pos = EC_READ_U32(r_ssi);
+
+    if (counter) {
+        counter--;
+    }
+    else {
+        counter = FREQUENCY;
+        blink = !blink;
+    }
+
 #ifdef KBUS
-    EC_WRITE_U8(r_outputs + 2, einaus ? 0xFF : 0x00);
-#endif
-
+    EC_WRITE_U8(r_outputs + 2, blink ? 0xFF : 0x00);
+#endif
+    
     // send
+    spin_lock(&master_lock);
     ecrt_domain_queue(domain1);
     ecrt_master_run(master);
     ecrt_master_send(master);
-
     spin_unlock(&master_lock);
 
-    if (counter) {
-        counter--;
-    }
-    else {
-        counter = FREQUENCY;
-        einaus = !einaus;
-    }
-
     // restart timer
     timer.expires += HZ / FREQUENCY;
     add_timer(&timer);
@@ -211,8 +211,8 @@
 /*****************************************************************************/
 
 MODULE_LICENSE("GPL");
-MODULE_AUTHOR ("Florian Pose <fp@igh-essen.com>");
-MODULE_DESCRIPTION ("EtherCAT minimal test environment");
+MODULE_AUTHOR("Florian Pose <fp@igh-essen.com>");
+MODULE_DESCRIPTION("EtherCAT minimal test environment");
 
 module_init(init_mini_module);
 module_exit(cleanup_mini_module);
--- a/examples/msr/msr_sample.c	Mon Jan 08 08:23:18 2007 +0000
+++ b/examples/msr/msr_sample.c	Mon Jan 08 08:32:10 2007 +0000
@@ -66,7 +66,7 @@
 // raw process data
 void *r_ana_out;
 
-// Channels
+// channels
 double k_ana_out;
 
 ec_pdo_reg_t domain1_pdos[] = {
@@ -78,20 +78,20 @@
 
 void msr_controller_run(void)
 {
+    // receive
     rt_sem_wait(&master_sem);
-
-    // receive
     ecrt_master_receive(master);
     ecrt_domain_process(domain1);
+    rt_sem_signal(&master_sem);
 
     // Process data
     EC_WRITE_S16(r_ana_out, k_ana_out / 10.0 * 0x7FFF);
 
     // Send
+    rt_sem_wait(&master_sem);
     ecrt_domain_queue(domain1);
     ecrt_master_run(master);
     ecrt_master_send(master);
-
     rt_sem_signal(&master_sem);
 
     msr_write_kanal_list();
@@ -221,8 +221,8 @@
 /*****************************************************************************/
 
 MODULE_LICENSE("GPL");
-MODULE_AUTHOR ("Florian Pose <fp@igh-essen.com>");
-MODULE_DESCRIPTION ("EtherCAT RTAI MSR sample module");
+MODULE_AUTHOR("Florian Pose <fp@igh-essen.com>");
+MODULE_DESCRIPTION("EtherCAT RTAI MSR sample module");
 
 module_init(init_mod);
 module_exit(cleanup_mod);
--- a/examples/rtai/rtai_sample.c	Mon Jan 08 08:23:18 2007 +0000
+++ b/examples/rtai/rtai_sample.c	Mon Jan 08 08:32:10 2007 +0000
@@ -46,12 +46,6 @@
 
 /*****************************************************************************/
 
-MODULE_LICENSE("GPL");
-MODULE_AUTHOR("Florian Pose <fp@igh-essen.com>");
-MODULE_DESCRIPTION("EtherCAT RTAI sample module");
-
-/*****************************************************************************/
-
 // RTAI task frequency in Hz
 #define FREQUENCY 10000
 #define INHIBIT_TIME 20
@@ -84,22 +78,23 @@
 
 void run(long data)
 {
-    while (1)
-    {
+    while (1) {
         t_last_cycle = get_cycles();
+
         rt_sem_wait(&master_sem);
-
         ecrt_master_receive(master);
         ecrt_domain_process(domain1);
+        rt_sem_signal(&master_sem);
 
         // process data
         //k_pos = EC_READ_U32(r_ssi_input);
 
+        rt_sem_wait(&master_sem);
         ecrt_domain_queue(domain1);
         ecrt_master_run(master);
         ecrt_master_send(master);
-
         rt_sem_signal(&master_sem);
+
         rt_task_wait_period();
     }
 }
@@ -108,7 +103,7 @@
 
 int request_lock(void *data)
 {
-    // too close to the next RT cycle: deny access...
+    // too close to the next real time cycle: deny access...
     if (get_cycles() - t_last_cycle > t_critical) return -1;
 
     // allow access
@@ -142,7 +137,7 @@
 
     ecrt_master_callbacks(master, request_lock, release_lock, NULL);
 
-    printk(KERN_INFO "Registering domain...\n");
+    printk(KERN_INFO "Creating domain...\n");
     if (!(domain1 = ecrt_master_create_domain(master))) {
         printk(KERN_ERR "Domain creation failed!\n");
         goto out_release_master;
@@ -207,8 +202,11 @@
 
 /*****************************************************************************/
 
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Florian Pose <fp@igh-essen.com>");
+MODULE_DESCRIPTION("EtherCAT RTAI sample module");
+
 module_init(init_mod);
 module_exit(cleanup_mod);
 
 /*****************************************************************************/
-