work on hostsim
authorzer0 <zer0@platinum>
Thu, 21 Jan 2010 00:29:44 +0000 (01:29 +0100)
committerzer0 <zer0@platinum>
Thu, 21 Jan 2010 00:29:44 +0000 (01:29 +0100)
include/aversive/irq_lock.h
modules/base/hostsim/hostsim.c
modules/base/hostsim/hostsim.h
modules/base/time/clock_time.h [new file with mode: 0644]
modules/base/time/time.c
modules/base/time/time.h [deleted file]
projects/microb2010/tests/hostsim/.config
projects/microb2010/tests/hostsim/cs.c
projects/microb2010/tests/hostsim/main.c
projects/microb2010/tests/hostsim/robotsim.c

index c8afdc4..ec45b68 100644 (file)
 
 #ifdef HOST_VERSION
 
+#include <hostsim.h>
+
 /* we must use 'flags' to avoid a warning */
-#define IRQ_UNLOCK(flags) flags=0
-#define IRQ_LOCK(flags) flags=0
-#define GLOBAL_IRQ_ARE_MASKED() (1)
+#define IRQ_UNLOCK(flags) do { flags=0; hostsim_lock(); } while(0)
+#define IRQ_LOCK(flags) do { flags=0; hostsim_unlock(); } while(0)
+#define GLOBAL_IRQ_ARE_MASKED() hostsim_islocked()
 
 #else
 
index 850d23a..e8a02f3 100644 (file)
@@ -44,7 +44,7 @@
 #include <scheduler.h>
 #endif
 
-pthread_mutex_t mut;
+static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
 static volatile int cpt = 0;
 
 #ifdef SA_SIGINFO
@@ -62,6 +62,26 @@ void sigusr1(__attribute__((unused)) int sig)
 #endif
 }
 
+static int lock_count = 0;
+
+void hostsim_lock(void)
+{
+       if (lock_count++)
+               return;
+       pthread_mutex_lock(&mut);
+}
+
+void hostsim_unlock(void)
+{
+       if (lock_count-- == 1)
+               pthread_mutex_unlock(&mut);
+}
+
+int hostsim_islocked(void)
+{
+       return lock_count;
+}
+
 void host_wait_ms(int ms)
 {
        struct timeval tv, tv2, diff;
@@ -121,12 +141,10 @@ int hostsim_init(void)
        pthread_t parent_id, child_id;
        int ret;
 
-       pthread_mutex_init(&mut, NULL);
-
        parent_id = pthread_self();
 
        pthread_mutex_lock(&mut);
-       ret = pthread_create(&child_id, NULL, parent, (void *)&parent_id);
+       ret = pthread_create(&child_id, NULL, parent, (void *)parent_id);
        if (ret) {
                printf("pthread_create() returned %d\n", ret);
                pthread_mutex_unlock(&mut);
@@ -144,6 +162,7 @@ int hostsim_init(void)
        if (siginterrupt (SIGUSR1, 0) != 0)
                return -1;
 
+       printf("hostsim_init()\n", ret);
        pthread_mutex_unlock(&mut);
 
        return 0;
index b61d9e3..18a9175 100644 (file)
@@ -25,3 +25,7 @@ int hostsim_init(void);
 
 /* replacement for wait_ms() */
 void host_wait_ms(int ms);
+
+void hostsim_lock(void);
+void hostsim_unlock(void);
+int hostsim_islocked(void);
diff --git a/modules/base/time/clock_time.h b/modules/base/time/clock_time.h
new file mode 100644 (file)
index 0000000..4e1fcec
--- /dev/null
@@ -0,0 +1,96 @@
+/*  
+ *  Copyright Droids Corporation, Microb Technology, Eirbot (2005)
+ * 
+ *  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
+ *
+ *  Revision : $Id: time.h,v 1.3.4.2 2007-05-23 17:18:11 zer0 Exp $
+ *
+ */
+
+/* Droids-corp, Eirbot, Microb Technology 2005 - Zer0
+ * Interface of the time module
+ */
+
+/**
+ *  This module can be used to get a human readable time. It uses the
+ *  scheduler module. Its goal is not to be very precise, but just
+ *  simple to use.  provides two timers: one in s and us, and one in
+ *  us which doesn't overflow on seconds (better to substract two
+ *  times)
+ */
+
+#ifndef _TIME_H_
+#define _TIME_H_
+
+#include <aversive.h>
+
+/* a 16 bit variable cannot cover one day */
+typedef int32_t seconds; 
+typedef int32_t microseconds;
+
+
+/** the time structure */
+typedef struct 
+{
+  microseconds us;
+  seconds s;
+} time_h;
+
+
+
+/**********************************************************/
+
+/** init time module : schedule the event with the givent priority */
+void time_init(uint8_t priority);
+
+/**********************************************************/
+
+/** get time in second since last init/reset */
+seconds time_get_s(void);
+
+/**********************************************************/
+
+/** get time in microsecond since last init/reset */
+microseconds time_get_us(void);
+
+/**********************************************************/
+
+/** get the complete time struct since last init/reset */
+time_h time_get_time(void); 
+
+/**********************************************************/
+
+/** reset time counter */
+void time_reset(void);
+
+/**********************************************************/
+
+/** set time */
+void time_set(seconds s, microseconds us);
+
+/**********************************************************/
+
+/** This is an equivalent of 'wait_ms(x)', but uses time value, so it
+ *  is independant of CPU load. Warning, you should not use this
+ *  function in a irq locked context, or in a scheduled function with
+ *  higher priority than time module */
+void time_wait_ms(uint16_t ms);
+
+/**********************************************************/
+
+/** get a microsecond timer that overflows naturally */
+microseconds time_get_us2(void);
+
+#endif
index c44bcd4..af38b83 100644 (file)
@@ -38,7 +38,7 @@
 #include <stdlib.h>
 #include <scheduler.h>
 
-#include <time.h>
+#include <clock_time.h>
 #include <time_config.h>
 
 /**********************************************************/
diff --git a/modules/base/time/time.h b/modules/base/time/time.h
deleted file mode 100644 (file)
index 4e1fcec..0000000
+++ /dev/null
@@ -1,96 +0,0 @@
-/*  
- *  Copyright Droids Corporation, Microb Technology, Eirbot (2005)
- * 
- *  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
- *
- *  Revision : $Id: time.h,v 1.3.4.2 2007-05-23 17:18:11 zer0 Exp $
- *
- */
-
-/* Droids-corp, Eirbot, Microb Technology 2005 - Zer0
- * Interface of the time module
- */
-
-/**
- *  This module can be used to get a human readable time. It uses the
- *  scheduler module. Its goal is not to be very precise, but just
- *  simple to use.  provides two timers: one in s and us, and one in
- *  us which doesn't overflow on seconds (better to substract two
- *  times)
- */
-
-#ifndef _TIME_H_
-#define _TIME_H_
-
-#include <aversive.h>
-
-/* a 16 bit variable cannot cover one day */
-typedef int32_t seconds; 
-typedef int32_t microseconds;
-
-
-/** the time structure */
-typedef struct 
-{
-  microseconds us;
-  seconds s;
-} time_h;
-
-
-
-/**********************************************************/
-
-/** init time module : schedule the event with the givent priority */
-void time_init(uint8_t priority);
-
-/**********************************************************/
-
-/** get time in second since last init/reset */
-seconds time_get_s(void);
-
-/**********************************************************/
-
-/** get time in microsecond since last init/reset */
-microseconds time_get_us(void);
-
-/**********************************************************/
-
-/** get the complete time struct since last init/reset */
-time_h time_get_time(void); 
-
-/**********************************************************/
-
-/** reset time counter */
-void time_reset(void);
-
-/**********************************************************/
-
-/** set time */
-void time_set(seconds s, microseconds us);
-
-/**********************************************************/
-
-/** This is an equivalent of 'wait_ms(x)', but uses time value, so it
- *  is independant of CPU load. Warning, you should not use this
- *  function in a irq locked context, or in a scheduled function with
- *  higher priority than time module */
-void time_wait_ms(uint16_t ms);
-
-/**********************************************************/
-
-/** get a microsecond timer that overflows naturally */
-microseconds time_get_us2(void);
-
-#endif
index 149a604..a5d281a 100644 (file)
@@ -86,8 +86,8 @@ CONFIG_MODULE_SCHEDULER_CREATE_CONFIG=y
 # CONFIG_MODULE_SCHEDULER_USE_TIMERS is not set
 # CONFIG_MODULE_SCHEDULER_TIMER0 is not set
 CONFIG_MODULE_SCHEDULER_MANUAL=y
-# CONFIG_MODULE_TIME is not set
-# CONFIG_MODULE_TIME_CREATE_CONFIG is not set
+CONFIG_MODULE_TIME=y
+CONFIG_MODULE_TIME_CREATE_CONFIG=y
 # CONFIG_MODULE_TIME_EXT is not set
 # CONFIG_MODULE_TIME_EXT_CREATE_CONFIG is not set
 
index cbb65d6..8632a23 100644 (file)
@@ -29,7 +29,7 @@
 
 #include <timer.h>
 #include <scheduler.h>
-#include <time.h>
+#include <clock_time.h>
 
 #include <pid.h>
 #include <quadramp.h>
index fe884e7..0182943 100644 (file)
@@ -28,7 +28,6 @@
 \r
 #include <timer.h>\r
 #include <scheduler.h>\r
-#include <time.h>\r
 \r
 #include <pid.h>\r
 #include <quadramp.h>\r
@@ -42,6 +41,7 @@
 #include <rdline.h>\r
 \r
 #include <uart.h>\r
+#include <clock_time.h>\r
 //#include <timer.h>\r
 #include <hostsim.h>\r
 \r
@@ -53,25 +53,13 @@ struct mainboard mainboard;
 \r
 int main(void)\r
 {\r
-#ifndef HOST_VERSION\r
-       uart_init();\r
-       fdevopen(uart0_dev_send, uart0_dev_recv);\r
-       sei();\r
-#else\r
-       int i;\r
-#endif\r
-\r
-#ifdef CONFIG_MODULE_TIMER\r
-       timer_init();\r
-#endif\r
-       scheduler_init();\r
        printf("init\n");\r
 \r
-#ifdef HOST_VERSION\r
+       scheduler_init();\r
+       time_init(TIME_PRIO);\r
+\r
        hostsim_init();\r
        robotsim_init();\r
-#endif\r
-\r
 \r
        microb_cs_init();\r
 \r
@@ -81,10 +69,13 @@ int main(void)
        mainboard.flags = DO_ENCODERS | DO_RS |\r
                DO_POS | DO_POWER | DO_BD | DO_CS;\r
 \r
+       time_wait_ms(1000);\r
+       printf("init\n");\r
        trajectory_d_rel(&mainboard.traj, 1000);\r
-       wait_ms(2000);\r
+       time_wait_ms(2000);\r
+       printf("init\n");\r
        trajectory_goto_xy_abs(&mainboard.traj, 1500, 2000);\r
-       wait_ms(2000);\r
+       time_wait_ms(2000);\r
        return 0;\r
 }\r
 \r
index 713b7f7..9303a85 100644 (file)
@@ -22,6 +22,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdint.h>
+#include <unistd.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <fcntl.h>
@@ -72,11 +73,13 @@ void robotsim_dump(void)
        char buf[BUFSIZ];
        int len;
 
-       len =snprintf(buf, sizeof(buf), "%d %d %d\n",
+       len = snprintf(buf, sizeof(buf), "%d %d %d\n",
                      position_get_x_s16(&mainboard.pos),
                      position_get_y_s16(&mainboard.pos),
                      position_get_a_deg_s16(&mainboard.pos));
+       hostsim_lock();
        write(fd, buf, len);
+       hostsim_unlock();
 }
 
 void robotsim_pwm(void *arg, int32_t val)