From 7d6a4a1f99a11437d608cbe61f07fac0e3d70ad1 Mon Sep 17 00:00:00 2001 From: zer0 Date: Thu, 21 Jan 2010 01:29:44 +0100 Subject: [PATCH] work on hostsim --- include/aversive/irq_lock.h | 8 +++--- modules/base/hostsim/hostsim.c | 27 +++++++++++++++++--- modules/base/hostsim/hostsim.h | 4 +++ modules/base/time/{time.h => clock_time.h} | 0 modules/base/time/time.c | 2 +- projects/microb2010/tests/hostsim/.config | 4 +-- projects/microb2010/tests/hostsim/cs.c | 2 +- projects/microb2010/tests/hostsim/main.c | 27 +++++++------------- projects/microb2010/tests/hostsim/robotsim.c | 5 +++- 9 files changed, 49 insertions(+), 30 deletions(-) rename modules/base/time/{time.h => clock_time.h} (100%) diff --git a/include/aversive/irq_lock.h b/include/aversive/irq_lock.h index c8afdc4..ec45b68 100644 --- a/include/aversive/irq_lock.h +++ b/include/aversive/irq_lock.h @@ -42,10 +42,12 @@ #ifdef HOST_VERSION +#include + /* 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 diff --git a/modules/base/hostsim/hostsim.c b/modules/base/hostsim/hostsim.c index 850d23a..e8a02f3 100644 --- a/modules/base/hostsim/hostsim.c +++ b/modules/base/hostsim/hostsim.c @@ -44,7 +44,7 @@ #include #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; diff --git a/modules/base/hostsim/hostsim.h b/modules/base/hostsim/hostsim.h index b61d9e3..18a9175 100644 --- a/modules/base/hostsim/hostsim.h +++ b/modules/base/hostsim/hostsim.h @@ -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/time.h b/modules/base/time/clock_time.h similarity index 100% rename from modules/base/time/time.h rename to modules/base/time/clock_time.h diff --git a/modules/base/time/time.c b/modules/base/time/time.c index c44bcd4..af38b83 100644 --- a/modules/base/time/time.c +++ b/modules/base/time/time.c @@ -38,7 +38,7 @@ #include #include -#include +#include #include /**********************************************************/ diff --git a/projects/microb2010/tests/hostsim/.config b/projects/microb2010/tests/hostsim/.config index 149a604..a5d281a 100644 --- a/projects/microb2010/tests/hostsim/.config +++ b/projects/microb2010/tests/hostsim/.config @@ -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 diff --git a/projects/microb2010/tests/hostsim/cs.c b/projects/microb2010/tests/hostsim/cs.c index cbb65d6..8632a23 100644 --- a/projects/microb2010/tests/hostsim/cs.c +++ b/projects/microb2010/tests/hostsim/cs.c @@ -29,7 +29,7 @@ #include #include -#include +#include #include #include diff --git a/projects/microb2010/tests/hostsim/main.c b/projects/microb2010/tests/hostsim/main.c index fe884e7..0182943 100644 --- a/projects/microb2010/tests/hostsim/main.c +++ b/projects/microb2010/tests/hostsim/main.c @@ -28,7 +28,6 @@ #include #include -#include #include #include @@ -42,6 +41,7 @@ #include #include +#include //#include #include @@ -53,25 +53,13 @@ struct mainboard mainboard; int main(void) { -#ifndef HOST_VERSION - uart_init(); - fdevopen(uart0_dev_send, uart0_dev_recv); - sei(); -#else - int i; -#endif - -#ifdef CONFIG_MODULE_TIMER - timer_init(); -#endif - scheduler_init(); printf("init\n"); -#ifdef HOST_VERSION + scheduler_init(); + time_init(TIME_PRIO); + hostsim_init(); robotsim_init(); -#endif - microb_cs_init(); @@ -81,10 +69,13 @@ int main(void) mainboard.flags = DO_ENCODERS | DO_RS | DO_POS | DO_POWER | DO_BD | DO_CS; + time_wait_ms(1000); + printf("init\n"); trajectory_d_rel(&mainboard.traj, 1000); - wait_ms(2000); + time_wait_ms(2000); + printf("init\n"); trajectory_goto_xy_abs(&mainboard.traj, 1500, 2000); - wait_ms(2000); + time_wait_ms(2000); return 0; } diff --git a/projects/microb2010/tests/hostsim/robotsim.c b/projects/microb2010/tests/hostsim/robotsim.c index 713b7f7..9303a85 100644 --- a/projects/microb2010/tests/hostsim/robotsim.c +++ b/projects/microb2010/tests/hostsim/robotsim.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -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) -- 2.20.1