From e5ccbfdd5211f378a5147f4d3123f34783c1ca7b Mon Sep 17 00:00:00 2001 From: zer0 Date: Wed, 20 Jan 2010 00:15:55 +0100 Subject: [PATCH] hostsim test --- config/Configure.help | 7 + config/config.in | 3 + config/generate_aversive_config | 6 + include/aversive/wait.h | 8 +- mk/aversive_project.mk | 2 +- modules/base/hostsim/Makefile | 7 + modules/base/hostsim/hostsim.c | 151 +++++++++++++++++++ modules/base/hostsim/hostsim.h | 27 ++++ modules/base/scheduler/scheduler_interrupt.c | 4 +- modules/base/scheduler/test/.config | 62 ++++---- modules/base/scheduler/test/main.c | 19 ++- modules/comm/uart/Makefile | 4 +- modules/comm/uart/uart_host.c | 10 -- modules/hardware/timer/Makefile | 4 + modules/hardware/timer/timer_host.c | 24 +++ 15 files changed, 284 insertions(+), 54 deletions(-) create mode 100644 modules/base/hostsim/Makefile create mode 100644 modules/base/hostsim/hostsim.c create mode 100644 modules/base/hostsim/hostsim.h create mode 100644 modules/hardware/timer/timer_host.c diff --git a/config/Configure.help b/config/Configure.help index f300a4f..a960040 100644 --- a/config/Configure.help +++ b/config/Configure.help @@ -50,6 +50,13 @@ CONFIG_MODULE_VECT2 This module provides functions for converting 2D vectors from polar to cartesian and vice versa. +CONFIG_MODULE_HOSTSIM + AVR simulator. This code is used on host (PC for instance) to + generate a signal that will call other aversive modules like + scheduler or uart. The goal is to simulate the behaviour of + hardware interrupts in a unix application. This has no effect + when compiling for AVR. + CONFIG_MODULE_SCHEDULER The 'scheduler' module is NOT a scheduler in the same way than in diff --git a/config/config.in b/config/config.in index 415f082..305cb79 100644 --- a/config/config.in +++ b/config/config.in @@ -131,6 +131,9 @@ dep_bool 'Vect2 lib' CONFIG_MODULE_VECT2 \ dep_bool 'Geometry lib' CONFIG_MODULE_GEOMETRY \ $CONFIG_MATH_LIB +#### Hostsim +bool 'Hostsim' CONFIG_MODULE_HOSTSIM + #### SCHEDULER bool 'Scheduler' CONFIG_MODULE_SCHEDULER diff --git a/config/generate_aversive_config b/config/generate_aversive_config index 534a131..c78c127 100755 --- a/config/generate_aversive_config +++ b/config/generate_aversive_config @@ -9,6 +9,7 @@ # MODULES_LIST="CONFIG_MODULE_BRUSHLESS_3PHASE_DIGITAL_HALL,/devices/brushless_motors/brushless_3phase_digital_hall CONFIG_MODULE_BRUSHLESS_3PHASE_DIGITAL_HALL_DOUBLE,/devices/brushless_motors/brushless_3phase_digital_hall_double + CONFIG_MODULE_HOSTSIM,base/hostsim CONFIG_MODULE_MENU,ihm/menu CONFIG_MODULE_PARSE,ihm/parse CONFIG_MODULE_RDLINE,ihm/rdline @@ -125,6 +126,11 @@ if grep "CONFIG_FDEVOPEN_COMPAT=y" $1 > /dev/null 2> /dev/null echo "CFLAGS += -D__STDIO_FDEVOPEN_COMPAT_12" >> $2 fi +if grep "CONFIG_MODULE_HOSTSIM=y" $1 > /dev/null 2> /dev/null + then + echo "PTHREAD_LDFLAGS = -lpthread" >> $2 +fi + #### diff --git a/include/aversive/wait.h b/include/aversive/wait.h index 176152d..a845721 100644 --- a/include/aversive/wait.h +++ b/include/aversive/wait.h @@ -33,9 +33,11 @@ #ifdef HOST_VERSION -#define wait_3cyc(n) do {} while(0) -#define wait_4cyc(n) do {} while(0) -#define wait_ms(n) do {} while(0) +#include + +#define wait_3cyc(n) do { volatile int a = 0; a++; } while (0) +#define wait_4cyc(n) do { volatile int a = 0; a++; } while (0) +#define wait_ms(n) host_wait_ms(n) #else /* HOST_VERSION */ diff --git a/mk/aversive_project.mk b/mk/aversive_project.mk index cf9c938..8e56130 100644 --- a/mk/aversive_project.mk +++ b/mk/aversive_project.mk @@ -101,7 +101,7 @@ ifeq ($(HOST),avr) LDFLAGS += -mmcu=$(MCU) $(PRINTF_LDFLAGS) LDFLAGS += -Wl,-Map=$(addprefix compiler_files/,$(TARGET).map),--cref else -LDFLAGS += +LDFLAGS += $(PTHREAD_LDFLAGS) endif LDFLAGS += $(MATH_LIB) diff --git a/modules/base/hostsim/Makefile b/modules/base/hostsim/Makefile new file mode 100644 index 0000000..ca08af0 --- /dev/null +++ b/modules/base/hostsim/Makefile @@ -0,0 +1,7 @@ +TARGET = hostsim + +SRC = hostsim.c + +########################################### + +include $(AVERSIVE_DIR)/mk/aversive_module.mk diff --git a/modules/base/hostsim/hostsim.c b/modules/base/hostsim/hostsim.c new file mode 100644 index 0000000..850d23a --- /dev/null +++ b/modules/base/hostsim/hostsim.c @@ -0,0 +1,151 @@ +/* + * Copyright Droids Corporation + * Olivier Matz + * + * 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: main.c,v 1.10 2009-11-08 17:24:33 zer0 Exp $ + * + */ + +#ifdef HOST_VERSION +/* + * AVR simulator. This code is used on host (PC for instance) to + * generate a signal that will call other aversive modules like + * scheduler or uart. The goal is to simulate the behaviour of + * hardware interrupts in a unix application. + */ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef CONFIG_MODULE_SCHEDULER +#include +#endif + +pthread_mutex_t mut; +static volatile int cpt = 0; + +#ifdef SA_SIGINFO +static void sigusr1(__attribute__((unused)) int sig, + __attribute__((unused)) siginfo_t *info, + __attribute__((unused)) void *uc) +#else +void sigusr1(__attribute__((unused)) int sig) +#endif +{ + pthread_mutex_unlock(&mut); + +#ifdef CONFIG_MODULE_SCHEDULER + scheduler_interrupt(); +#endif +} + +void host_wait_ms(int ms) +{ + struct timeval tv, tv2, diff; + + gettimeofday(&tv, NULL); + diff.tv_sec = (1000 * ms) / 1000000; + diff.tv_usec = (1000 * ms) % 1000000; + timeradd(&tv, &diff, &tv); + gettimeofday(&tv2, NULL); + + while (timercmp(&tv2, &tv, <)) { + usleep(1000); + gettimeofday(&tv2, NULL); + } +} + + +/* sends signal to child */ +void *parent(void *arg) +{ + pthread_t *thread = arg; + struct timeval cur_tv, prev_tv, tv_millisec; + int n; + + gettimeofday(&prev_tv, NULL); + + while (1) { + usleep(1000); + gettimeofday(&cur_tv, NULL); + + n = 0; + while (timercmp(&prev_tv, &cur_tv, <)) { + if (n > 5) { + /* give some time between subsequent + * signals */ + usleep(100); + n = 0; + } + pthread_mutex_lock(&mut); + pthread_kill(*thread, SIGUSR1); + + /* signal was acked */ + tv_millisec.tv_sec = 0; + tv_millisec.tv_usec = 1000; + timeradd(&prev_tv, &tv_millisec, &prev_tv); + n ++; + } + } + + pthread_exit(NULL); + return NULL; +} + +int hostsim_init(void) +{ + struct sigaction sigact; + 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); + if (ret) { + printf("pthread_create() returned %d\n", ret); + pthread_mutex_unlock(&mut); + return -1; + } + + /* register a signal handler, which is interruptible */ + memset(&sigact, 0, sizeof(sigact)); + sigemptyset(&sigact.sa_mask); + sigact.sa_flags |= SA_NODEFER; + sigact.sa_sigaction = sigusr1; + sigaction(SIGUSR1, &sigact, NULL); + + /* */ + if (siginterrupt (SIGUSR1, 0) != 0) + return -1; + + pthread_mutex_unlock(&mut); + + return 0; +} +#endif /* HOST_VERSION */ diff --git a/modules/base/hostsim/hostsim.h b/modules/base/hostsim/hostsim.h new file mode 100644 index 0000000..b61d9e3 --- /dev/null +++ b/modules/base/hostsim/hostsim.h @@ -0,0 +1,27 @@ +/* + * Copyright Droids Corporation + * Olivier Matz + * + * 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: main.c,v 1.10 2009-11-08 17:24:33 zer0 Exp $ + * + */ + +/* initialize hostsim framework */ +int hostsim_init(void); + +/* replacement for wait_ms() */ +void host_wait_ms(int ms); diff --git a/modules/base/scheduler/scheduler_interrupt.c b/modules/base/scheduler/scheduler_interrupt.c index 15b9409..7045776 100644 --- a/modules/base/scheduler/scheduler_interrupt.c +++ b/modules/base/scheduler/scheduler_interrupt.c @@ -45,13 +45,13 @@ void scheduler_enable_restore(uint8_t old_prio) priority_running = old_prio; } -/** +/** * this function is called from a timer interruption. If an event has * to be scheduled, it will execute the fonction (IRQ are allowed * during the execution of the function). This interruption can be * interrupted by itself too, in this case only events with a higher * priority can be scheduled. - * + * * We assume that this function is called from a SIGNAL(), with * global interrupt flag disabled --> that's why we can use cli() and * sei() instead of IRQ_LOCK(flags). diff --git a/modules/base/scheduler/test/.config b/modules/base/scheduler/test/.config index 73a8d18..47bbc9c 100644 --- a/modules/base/scheduler/test/.config +++ b/modules/base/scheduler/test/.config @@ -1,5 +1,5 @@ # -# Automatically generated make config: don't edit +# Automatically generated by make menuconfig: don't edit # # @@ -45,9 +45,12 @@ # CONFIG_MCU_ATMEGA645 is not set # CONFIG_MCU_ATMEGA6450 is not set CONFIG_MCU_ATMEGA128=y +# CONFIG_MCU_ATMEGA1281 is not set # CONFIG_MCU_AT90CAN128 is not set # CONFIG_MCU_AT94K is not set # CONFIG_MCU_AT90S1200 is not set +# CONFIG_MCU_ATMEGA2560 is not set +# CONFIG_MCU_ATMEGA256 is not set CONFIG_QUARTZ=16000000 # @@ -60,6 +63,7 @@ CONFIG_QUARTZ=16000000 CONFIG_OPTM_S=y CONFIG_MATH_LIB=y # CONFIG_FDEVOPEN_COMPAT is not set +# CONFIG_NO_PRINTF is not set # CONFIG_MINIMAL_PRINTF is not set CONFIG_STANDARD_PRINTF=y # CONFIG_ADVANCED_PRINTF is not set @@ -70,31 +74,31 @@ CONFIG_FORMAT_IHEX=y # # Base modules # - -# -# Enable math library in generation options to see all modules -# CONFIG_MODULE_CIRBUF=y # CONFIG_MODULE_CIRBUF_LARGE is not set # CONFIG_MODULE_FIXED_POINT is not set # CONFIG_MODULE_VECT2 is not set +# CONFIG_MODULE_GEOMETRY is not set +CONFIG_MODULE_HOSTSIM=y CONFIG_MODULE_SCHEDULER=y +# CONFIG_MODULE_SCHEDULER_STATS is not set CONFIG_MODULE_SCHEDULER_CREATE_CONFIG=y # CONFIG_MODULE_SCHEDULER_USE_TIMERS is not set -CONFIG_MODULE_SCHEDULER_TIMER0=y -# CONFIG_MODULE_SCHEDULER_MANUAL 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_EXT is not set +# CONFIG_MODULE_TIME_EXT_CREATE_CONFIG is not set # # Communication modules # - -# -# uart needs circular buffer, mf2 client may need scheduler -# CONFIG_MODULE_UART=y +# CONFIG_MODULE_UART_9BITS is not set # CONFIG_MODULE_UART_CREATE_CONFIG is not set +# CONFIG_MODULE_SPI is not set +# CONFIG_MODULE_SPI_CREATE_CONFIG is not set # CONFIG_MODULE_I2C is not set # CONFIG_MODULE_I2C_MASTER is not set # CONFIG_MODULE_I2C_MULTIMASTER is not set @@ -108,11 +112,12 @@ CONFIG_MODULE_UART=y # # Hardware modules # -# CONFIG_MODULE_TIMER is not set +CONFIG_MODULE_TIMER=y # CONFIG_MODULE_TIMER_CREATE_CONFIG is not set # CONFIG_MODULE_TIMER_DYNAMIC is not set # CONFIG_MODULE_PWM is not set # CONFIG_MODULE_PWM_CREATE_CONFIG is not set +# CONFIG_MODULE_PWM_NG is not set # CONFIG_MODULE_ADC is not set # CONFIG_MODULE_ADC_CREATE_CONFIG is not set @@ -126,6 +131,7 @@ CONFIG_MODULE_UART=y # CONFIG_MODULE_RDLINE_KILL_BUF is not set # CONFIG_MODULE_RDLINE_HISTORY is not set # CONFIG_MODULE_PARSE is not set +# CONFIG_MODULE_PARSE_NO_FLOAT is not set # # External devices modules @@ -134,6 +140,8 @@ CONFIG_MODULE_UART=y # CONFIG_MODULE_LCD_CREATE_CONFIG is not set # CONFIG_MODULE_MULTISERVO is not set # CONFIG_MODULE_MULTISERVO_CREATE_CONFIG is not set +# CONFIG_MODULE_AX12 is not set +# CONFIG_MODULE_AX12_CREATE_CONFIG is not set # # Brushless motor drivers (you should enable pwm modules to see all) @@ -144,31 +152,32 @@ CONFIG_MODULE_UART=y # CONFIG_MODULE_BRUSHLESS_3PHASE_DIGITAL_HALL_DOUBLE_CREATE_CONFIG is not set # -# Encoders +# Encoders (you need comm/spi for encoders_spi) # # CONFIG_MODULE_ENCODERS_MICROB is not set # CONFIG_MODULE_ENCODERS_MICROB_CREATE_CONFIG is not set # CONFIG_MODULE_ENCODERS_EIRBOT is not set # CONFIG_MODULE_ENCODERS_EIRBOT_CREATE_CONFIG is not set +# CONFIG_MODULE_ENCODERS_SPI is not set +# CONFIG_MODULE_ENCODERS_SPI_CREATE_CONFIG is not set # -# Robot specific modules +# Robot specific modules (fixed point lib may be needed) # # CONFIG_MODULE_ROBOT_SYSTEM is not set +# CONFIG_MODULE_ROBOT_SYSTEM_USE_F64 is not set # CONFIG_MODULE_ROBOT_SYSTEM_MOT_AND_EXT is not set # CONFIG_MODULE_POSITION_MANAGER is not set +# CONFIG_MODULE_COMPENSATE_CENTRIFUGAL_FORCE is not set # CONFIG_MODULE_TRAJECTORY_MANAGER is not set # CONFIG_MODULE_BLOCKING_DETECTION_MANAGER is not set # CONFIG_MODULE_OBSTACLE_AVOIDANCE is not set +# CONFIG_MODULE_OBSTACLE_AVOIDANCE_CREATE_CONFIG is not set # # Control system modules # # CONFIG_MODULE_CONTROL_SYSTEM_MANAGER is not set - -# -# Filters -# # CONFIG_MODULE_PID is not set # CONFIG_MODULE_PID_CREATE_CONFIG is not set # CONFIG_MODULE_RAMP is not set @@ -177,11 +186,13 @@ CONFIG_MODULE_UART=y # CONFIG_MODULE_BIQUAD is not set # -# Crypto modules +# Radio devices # +# CONFIG_MODULE_CC2420 is not set +# CONFIG_MODULE_CC2420_CREATE_CONFIG is not set # -# Crypto modules depend on utils module +# Crypto modules # # CONFIG_MODULE_AES is not set # CONFIG_MODULE_AES_CTR is not set @@ -192,20 +203,12 @@ CONFIG_MODULE_UART=y # # Encodings modules # - -# -# Encoding modules depend on utils module -# # CONFIG_MODULE_BASE64 is not set # CONFIG_MODULE_HAMMING is not set # # Debug modules # - -# -# Debug modules depend on utils module -# # CONFIG_MODULE_DIAGNOSTIC is not set # CONFIG_MODULE_DIAGNOSTIC_CREATE_CONFIG is not set CONFIG_MODULE_ERROR=y @@ -237,7 +240,9 @@ CONFIG_AVRDUDE_PROG_STK200=y # CONFIG_AVRDUDE_PROG_BSD is not set # CONFIG_AVRDUDE_PROG_DAPA is not set # CONFIG_AVRDUDE_PROG_JTAG1 is not set +# CONFIG_AVRDUDE_PROG_AVR109 is not set CONFIG_AVRDUDE_PORT="/dev/parport0" +CONFIG_AVRDUDE_BAUDRATE=19200 # # Avarice @@ -246,3 +251,4 @@ CONFIG_AVARICE_PORT="/dev/ttyS0" CONFIG_AVARICE_DEBUG_PORT=1234 CONFIG_AVARICE_PROG_MKI=y # CONFIG_AVARICE_PROG_MKII is not set +# CONFIG_AVRDUDE_CHECK_SIGNATURE is not set diff --git a/modules/base/scheduler/test/main.c b/modules/base/scheduler/test/main.c index 43aff0f..cc9242c 100644 --- a/modules/base/scheduler/test/main.c +++ b/modules/base/scheduler/test/main.c @@ -23,6 +23,8 @@ #include #include #include +//#include +#include uint8_t event_id; @@ -45,7 +47,7 @@ void f3(void * nothing) void supp(void * nothing) { - scheduler_del_event(event_id); + scheduler_del_event(event_id); } int main(void) @@ -67,17 +69,18 @@ int main(void) wait_ms(2000); printf("init3\n"); +#ifdef HOST_VERSION + hostsim_init(); +#endif + event_id = scheduler_add_periodical_event_priority(f1, NULL, 500000l/SCHEDULER_UNIT, 200); scheduler_add_periodical_event_priority(f2, NULL, 500000l/SCHEDULER_UNIT, 100); scheduler_add_periodical_event(f3, NULL, 1000000l/SCHEDULER_UNIT); - - // scheduler_add_single_event(supp,65); - -#ifdef HOST_VERSION - for (i=0 ; i<50000 ; i++) - scheduler_interrupt(); -#endif + scheduler_add_single_event(supp, NULL, 5000000l/SCHEDULER_UNIT); + + while (1); + return 0; } diff --git a/modules/comm/uart/Makefile b/modules/comm/uart/Makefile index 483f156..28382d0 100644 --- a/modules/comm/uart/Makefile +++ b/modules/comm/uart/Makefile @@ -14,8 +14,8 @@ SRC += uart_send9.c uart_send9_nowait.c SRC += uart_recv9.c uart_recv9_nowait.c endif -else # is it ok ?? -SRC = uart_host.c +else +SRC = uart_host.c uart_events.c endif include $(AVERSIVE_DIR)/mk/aversive_module.mk diff --git a/modules/comm/uart/uart_host.c b/modules/comm/uart/uart_host.c index 8c58c44..429c4db 100644 --- a/modules/comm/uart/uart_host.c +++ b/modules/comm/uart/uart_host.c @@ -24,7 +24,6 @@ #include #include - /* this file os a stub for host */ void uart_init(void) @@ -59,12 +58,3 @@ int uart_send(uint8_t num, char c) return put_char(c); } -void uart_register_tx_event(uint8_t num, void (*f)(char)) -{ - tx_event = f; -} - -void uart_register_rx_event(uint8_t num, void (*f)(char)) -{ - rx_event = f; -} diff --git a/modules/hardware/timer/Makefile b/modules/hardware/timer/Makefile index 1afcd22..d47d603 100644 --- a/modules/hardware/timer/Makefile +++ b/modules/hardware/timer/Makefile @@ -1,6 +1,7 @@ TARGET = timer # List C source files here. (C dependencies are automatically generated.) +ifeq ($(HOST),avr) SRC = timer_init.c \ timer_intr.c \ timer_conf_check.c \ @@ -40,5 +41,8 @@ timer2_prescaler.c \ timer3_prescaler.c \ timer4_prescaler.c \ timer5_prescaler.c +else +SRC = timer_host.c +endif include $(AVERSIVE_DIR)/mk/aversive_module.mk diff --git a/modules/hardware/timer/timer_host.c b/modules/hardware/timer/timer_host.c new file mode 100644 index 0000000..c9ee86f --- /dev/null +++ b/modules/hardware/timer/timer_host.c @@ -0,0 +1,24 @@ +/* + * Copyright Droids Corporation, Microb Technology, (2010) + * + * 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: timer_init.c,v 1.1.2.4 2009-01-30 20:18:36 zer0 Exp $ + * + */ + +void timer_init(void) +{ +} -- 2.39.5