Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision

Target

Select target project
  • avn/swd/miosix-kernel
  • emilio.corigliano/miosix-kernel
2 results
Select Git revision
Show changes
Commits on Source (404)
Showing
with 1148 additions and 371 deletions
......@@ -13,3 +13,6 @@ main.map
# Exclude Mac OS X temporary
._*
.DS_Store
# CMake build directory
build
# Copyright (C) 2023 by Skyward
#
# This program is free software; you can redistribute it and/or
# it under the terms of the GNU General Public License as published
# 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.
#
# As a special exception, if other files instantiate templates or use
# macros or inline functions from this file, or you compile this file
# and link it with other works to produce a work based on this file,
# this file does not by itself cause the resulting work to be covered
# by the GNU General Public License. However the source code for this
# file must still be made available in accordance with the GNU
# Public License. This exception does not invalidate any other
# why a work based on this file might be covered by the GNU General
# Public License.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>
variables:
GIT_SUBMODULE_STRATEGY: recursive
stages:
- build
build:
stage: build
tags:
- miosix
script:
- ./mbs
# Copyright (C) 2023 by Skyward
#
# This program is free software; you can redistribute it and/or
# it under the terms of the GNU General Public License as published
# 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.
#
# As a special exception, if other files instantiate templates or use
# macros or inline functions from this file, or you compile this file
# and link it with other works to produce a work based on this file,
# this file does not by itself cause the resulting work to be covered
# by the GNU General Public License. However the source code for this
# file must still be made available in accordance with the GNU
# Public License. This exception does not invalidate any other
# why a work based on this file might be covered by the GNU General
# Public License.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>
cmake_minimum_required(VERSION 3.25)
include(miosix/cmake/mbs.cmake)
project(MiosixExamples)
#-----------------------------------------------------------------------------#
# Examples #
#-----------------------------------------------------------------------------#
add_executable(asm miosix/_examples/asm/main.s)
mbs_target(asm stm32f407vg_stm32f4discovery)
add_executable(atsam4l_lcd miosix/_examples/atsam4l_lcd/main.cpp)
mbs_target(atsam4l_lcd atsam4lc2aa_generic)
add_executable(blinking_led miosix/_examples/blinking_led/simple.cpp)
mbs_target(blinking_led stm32f429zi_stm32f4discovery)
add_executable(datalogger
${KPATH}/_examples/datalogger/main.cpp
${KPATH}/_examples/datalogger/Logger.cpp
${KPATH}/_examples/datalogger/tscpp/buffer.cpp
)
target_include_directories(datalogger PUBLIC miosix/_examples/datalogger/)
mbs_target(datalogger stm32f407vg_stm32f4discovery)
add_executable(hd44780 miosix/_examples/hd44780/hd44780.cpp)
mbs_target(hd44780 stm32f407vg_stm32f4discovery)
add_executable(ir_decoder miosix/_examples/ir_decoder/ir_decoder.cpp)
mbs_target(ir_decoder stm32f407vg_stm32f4discovery)
add_executable(led_display miosix/_examples/led_display/main.cpp)
mbs_target(led_display stm32f100rb_stm32vldiscovery)
add_executable(sad_trombone
miosix/_examples/sad_trombone/main.cpp
miosix/_examples/sad_trombone/player.cpp
miosix/_examples/sad_trombone/adpcm.c
)
mbs_target(sad_trombone stm32f407vg_stm32f4discovery)
add_executable(servo-prompt miosix/_examples/servo/prompt.cpp)
mbs_target(servo-prompt stm32f407vg_stm32f4discovery)
add_executable(servo-sweep miosix/_examples/servo/sweep.cpp)
mbs_target(servo-sweep stm32f407vg_stm32f4discovery)
add_executable(streamwriter miosix/_examples/streamwriter/streamwriter.cpp)
mbs_target(streamwriter stm32f407vg_stm32f4discovery)
add_executable(termios miosix/_examples/termios/main.cpp)
mbs_target(termios stm32f407vg_stm32f4discovery)
add_executable(thread_native miosix/_examples/thread_native/native_thread_example.cpp)
mbs_target(thread_native stm32f407vg_stm32f4discovery)
add_executable(thread_pthread miosix/_examples/thread_pthread/pthread_example.cpp)
mbs_target(thread_pthread stm32f407vg_stm32f4discovery)
#-----------------------------------------------------------------------------#
# Others #
#-----------------------------------------------------------------------------#
add_executable(delay_test miosix/_tools/delay_test/delay_test.cpp)
mbs_target(delay_test stm32f407vg_stm32f4discovery)
add_executable(feedforward_profiling miosix/_tools/feedforward_profiling/test.cpp)
mbs_target(feedforward_profiling stm32f407vg_stm32f4discovery)
add_executable(fs_backend miosix/_tools/fs_backend/backend_benchmark.cpp)
mbs_target(fs_backend stm32f407vg_stm32f4discovery)
add_executable(fs_misc_testcode miosix/_tools/fs_misc_testcode/dirlist.cpp)
mbs_target(fs_misc_testcode stm32f407vg_stm32f4discovery)
# To build this entrypoint remember to define WITH_PROCESSES in miosix_settings.h
# add_executable(processes miosix/_tools/processes/main_processes.cpp)
# target_include_directories(processes PUBLIC miosix/_tools/processes/)
# add_custom_command(
# TARGET processes
# COMMAND make
# WORKING_DIRECTORY ${KPATH}/_tools/processes/process_template
# )
# mbs_target(processes stm32f407vg_stm32f4discovery)
add_executable(ram_test
miosix/_tools/ram_test/main.cpp
miosix/_tools/ram_test/sha1.cpp
)
mbs_target(ram_test stm32f407vg_stm32f4discovery)
# If you want to test the MPU remember to run build.sh in miosix/_tools/testsuite
# add_executable(testsuite miosix/_tools/testsuite/testsuite.cpp)
# mbs_target(testsuite stm32f407vg_stm32f4discovery)
# Build the testsuite for all supported boards to test compilation
set(TESTSUITE_BOARDS
lpc2138_miosix_board
# stm32f072rb_stm32f0discovery
efm32gg332f1024_wandstem
# stm32f100c8_microboard
# stm32f100c8_vaisala_rs41
# stm32f100cb_tempsensor
# stm32f100cx_generic
# stm32f100rb_stm32vldiscovery
stm32f100rc_solertegiard
# stm32f103c8_breakout
# stm32f103cb_als_mainboard_rev2
# stm32f103cx_generic
stm32f103ve_mp3v2
stm32f103ve_strive_mini
stm32f103ze_redbull_v2
stm32f103ze_stm3210e-eval
stm32f205_generic
stm32f205rc_skyward_stormtrooper
stm32f205rg_sony-newman
stm32f207ig_stm3220g-eval
stm32f207ze_als_camboard
stm32f207zg_EthBoardV2
stm32f207zg_nucleo
# stm32l151c8_als_mainboard
# atsam4lcc
stm32f303vc_stm32f3discovery
stm32f401re_nucleo
stm32f401vc_stm32f4discovery
stm32f407vg_bitsboard
stm32f407vg_stm32f4discovery
stm32f407vg_thermal_test_chip
stm32f411ce_blackpill
stm32f411re_nucleo
stm32f429zi_oledboard2
stm32f429zi_skyward_anakin
stm32f429zi_skyward_homeone
stm32f429zi_stm32f4discovery
stm32f469ni_stm32f469i-disco
stm32l4r9zi_sensortile
stm32l476rg_nucleo
stm32f746zg_nucleo
stm32f767zi_nucleo
# stm32f769ni_discovery
stm32h753xi_eval
)
foreach(OPT_BOARD ${TESTSUITE_BOARDS})
add_executable(testsuite-${OPT_BOARD} miosix/_tools/testsuite/testsuite.cpp)
mbs_target(testsuite-${OPT_BOARD} ${OPT_BOARD})
endforeach()
##
## Makefile for Miosix embedded OS
##
MAKEFILE_VERSION := 1.09
GCCMAJOR := $(shell arm-miosix-eabi-gcc --version | \
perl -e '$$_=<>;/\(GCC\) (\d+)/;print "$$1"')
MAKEFILE_VERSION := 1.10
## Path to kernel directory (edited by init_project_out_of_git_repo.pl)
KPATH := miosix
## Path to config directory (edited by init_project_out_of_git_repo.pl)
......@@ -59,12 +57,7 @@ LFLAGS := $(LFLAGS_BASE)
DFLAGS := -MMD -MP
## libmiosix.a is among stdlibs because needs to be within start/end group
STDLIBS := -lmiosix -lstdc++ -lc -lm -lgcc
ifneq ($(GCCMAJOR),4)
STDLIBS += -latomic
endif
STDLIBS := -lmiosix -lstdc++ -lc -lm -lgcc -latomic
LINK_LIBS := $(LIBS) -L$(KPATH) -Wl,--start-group $(STDLIBS) -Wl,--end-group
all: all-recursive main
......
#!/bin/bash
# Copyright (C) 2023 by Skyward
#
# This program is free software; you can redistribute it and/or
# it under the terms of the GNU General Public License as published
# 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.
#
# As a special exception, if other files instantiate templates or use
# macros or inline functions from this file, or you compile this file
# and link it with other works to produce a work based on this file,
# this file does not by itself cause the resulting work to be covered
# by the GNU General Public License. However the source code for this
# file must still be made available in accordance with the GNU
# Public License. This exception does not invalidate any other
# why a work based on this file might be covered by the GNU General
# Public License.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>
print_banner() {
cat <<EOF
+---------------------------------------------------------------+
| __ __ _ _ |
| | \\/ (_) ___ ___(_)_ __ |
| | |\\/| | |/ _ \\/ __| \\ \\/ / |
| | | | | | (_) \\__ \\ |> < |
| |_| |_|_|\\___/|___/_/_/\\_\\ |
| ____ _ _ _ ____ _ |
| | __ ) _ _(_) | __| | / ___| _ _ ___| |_ ___ _ __ ___ |
| | _ \\| | | | | |/ _\` | \\___ \\| | | / __| __/ _ \\ '_ \` _ \\ |
| | |_) | |_| | | | (_| | ___) | |_| \\__ \\ || __/ | | | | | |
| |____/ \\__,_|_|_|\\__,_| |____/ \\__, |___/\\__\\___|_| |_| |_| |
+----------------------------------|___/-------------------v1.0-+
EOF
}
usage() {
echo
cat <<EOF
Usage: $(basename "$0") [OPTIONS]
OPTIONS:
General Options:
-h, --help Show this help message and exit
-j JOBS, --jobs JOBS Build or lint in parallel using a specific number of jobs
-l, --list_targets List all targets available
-r, --list_boards List all boards available
Build Options:
-b TARGET, --build TARGET
Build a specific target
-f TARGET, --flash TARGET
Build and flash a specific target
-c, --clean Clean the working tree
-u, --configure Force configure and do not build
-d, --debug Enable debug
-v, --verbose Print a verbose output
EOF
}
ohai() {
printf "\n${TTY_BLUE}==>${TTY_RESET}${TTY_BOLD} %s${TTY_RESET}\n" "$@"
}
init_dirs() {
msb_base=$(cd -- $(dirname "$0") > /dev/null 2>&1 && pwd -P)
source_dir="$PWD"
build_dir="$source_dir/$BUILD_DEFAULT_DIRNAME"
toolchain_file="$msb_base/miosix/cmake/toolchain.cmake"
}
find_deps() {
ohai "Find dependencies"
command -v cmake > /dev/null 2>&1 && found_cmake=true
command -v arm-miosix-eabi-g++ > /dev/null 2>&1 && found_miosixgpp=true
command -v ccache > /dev/null 2>&1 && found_ccache=true
command -v ninja > /dev/null 2>&1 && found_ninja=true
command -v st-flash > /dev/null 2>&1 && found_stflash=true
printf "Found CMake: "; [ "$found_cmake" = true ] && echo "yes" || echo "no"
printf "Found arm-miosix-eabi-g++: "; [ "$found_miosixgpp" = true ] && echo "yes" || echo "no"
printf "Found Ccache: "; [ "$found_ccache" = true ] && echo "yes" || echo "no"
printf "Found Ninja: "; [ "$found_ninja" = true ] && echo "yes" || echo "no"
printf "Found st-flash: "; [ "$found_stflash" = true ] && echo "yes" || echo "no"
[ "$found_cmake" = true ] || { echo "Error: CMake must be installed"; return 1; }
[ "$found_miosixgpp" = true ] || { echo "Error: arm-miosix-eabi-g++ must be installed"; return 1; }
# TODO: Maybe prompt the user to download and intall miosix?
}
configure() {
declare build_dir="$1"
ohai "Configure"
[ -f "$toolchain_file" ] || { echo "Error: CMake Toolchain File for Miosix was not found"; return 1; }
# Always use colors when possible
defs+=(-DCMAKE_C_FLAGS=-fdiagnostics-color=always -DCMAKE_CXX_FLAGS=-fdiagnostics-color=always)
# Toolchain files
defs+=(-DCMAKE_TOOLCHAIN_FILE="$toolchain_file")
# Use ccache when available
[ "$found_ccache" = true ] && defs+=(-DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache)
# Debug or release build types
[ "$config_debug" = true ] && defs+=(-DCMAKE_BUILD_TYPE=Debug) || defs+=(-DCMAKE_BUILD_TYPE=Release)
# Verbose output
[ "$config_verbose" = true ] && defs+=(-DCMAKE_VERBOSE_MAKEFILE=ON)
# CMake generator
declare gen
[ "$found_ninja" = true ] && gen=-GNinja || gen=-G"Unix Makefiles"
# Configure build
cmake -B"$build_dir" "${defs[@]}" "$gen" "$source_dir" || return
# Save configuration to avoid reconfiguring each time
{ [ "$config_debug" = true ] && touch "$build_dir/$DEBUG_FILENAME"; } || rm -f "$build_dir/$DEBUG_FILENAME"
{ [ "$config_verbose" = true ] && touch "$build_dir/$VERBOSE_FILENAME"; } || rm -f "$build_dir/$VERBOSE_FILENAME"
}
check_configured() {
declare build_dir="$1"
declare to_reconfigure=false
if [ ! -d "$build_dir" ]; then
to_reconfigure=true
elif [ ! -f "$build_dir/$CMAKE_FILENAME" ]; then
rm -rf "$build_dir"
to_reconfigure=true
else
[ -f "$build_dir/$DEBUG_FILENAME" ] && found_debug=true || found_debug=false
[ -f "$build_dir/$VERBOSE_FILENAME" ] && found_verbose=true || found_verbose=false
if [ "$config_debug" != "$found_debug" ] \
|| [ "$config_verbose" != "$found_verbose" ]; then
to_reconfigure=true
fi
fi
if [ "$to_reconfigure" = true ]; then
configure "$build_dir"
fi
}
build() {
declare build_dir="$1"
declare target="$2"
check_configured "$build_dir" || return
ohai "Build"
get_build_opts
cmake --build "$build_dir" "${opts[@]}" --target "$target"
}
build_all() {
build "$build_dir" all
}
clean() {
ohai "Cleaning build files"
if [ -f "$build_dir/$CMAKE_FILENAME" ]; then
get_build_opts
cmake --build "$build_dir" "${opts[@]}" --target clean
fi
echo "Removing build folder..."
rm -rf "$build_dir"
}
flash() {
declare target="$1"
build "$build_dir" "$target" || return
# This variable will be evaluated in the command
declare binary=$build_dir/$target.bin
declare hex=$build_dir/$target.hex
# Check if the target has a custom build command
declare custom_command_file=$build_dir/flash-$target.txt
if [[ -f "$custom_command_file" ]]; then
ohai "Flash via custom command"
eval $(cat $custom_command_file)
elif [ "$found_stflash" = true ]; then
ohai "Flash via st-flash"
st-flash --reset write "$build_dir/$target.bin" 0x8000000
else
echo "Error: No flashing software found!"
return 1
fi
}
list_targets() {
check_configured "$build_dir" || return
ohai "List targets"
echo "[1/1] All MBS targets available:"
cmake --build "$build_dir" --target help \
| grep -o '^[^/]*\.bin' | cut -f 1 -d '.'
}
list_boards() {
check_configured "$build_dir" || return
ohai "List boards"
cmake --build "$build_dir" --target help-boards
}
set_debug() {
config_debug=true
}
set_verbose() {
config_verbose=true
}
set_jobs() {
jobs="$1"
}
get_build_opts() {
declare -a opts
[ -n "$jobs" ] && opts=("-j $jobs")
}
CMAKE_FILENAME="CMakeCache.txt"
DEBUG_FILENAME=".sbs_debug"
VERBOSE_FILENAME=".sbs_verbose"
BUILD_DEFAULT_DIRNAME="build"
TTY_BLUE="\033[34m"
TTY_BOLD="\033[1m"
TTY_RESET="\033[0m"
msb_base=
source_dir=
build_dir=
toolchain_file=
found_cmake=false
found_miosixgpp=false
found_ccache=false
found_ninja=false
found_stflash=false
config_debug=false
config_verbose=false
jobs=
print_banner
init_dirs
for arg in "$@"; do
shift
case "$arg" in
--help) set -- "$@" "-h";;
--jobs) set -- "$@" "-j";;
--list_targets) set -- "$@" "-l";;
--list_boards) set -- "$@" "-r";;
--build) set -- "$@" "-b";;
--flash) set -- "$@" "-f";;
--clean) set -- "$@" "-c";;
--configure) set -- "$@" "-u";;
--debug) set -- "$@" "-d";;
--verbose) set -- "$@" "-v";;
*) set -- "$@" "$arg"
esac
done
while getopts b:cdef:hj:lnrt:uv opt; do
case "$opt" in
h) usage; exit 0;;
j) set_jobs "$OPTARG";;
l) find_deps && list_targets; exit;;
r) find_deps && list_boards; exit;;
b) find_deps && build "$build_dir" "$OPTARG"; exit;;
f) find_deps && flash "$OPTARG"; exit;;
c) find_deps && clean; exit;;
u) find_deps && configure "$build_dir"; exit;;
d) set_debug;;
v) set_verbose;;
?) usage; exit 2;;
esac
done
shift $((OPTIND - 1))
find_deps && build_all
......@@ -2,9 +2,7 @@
## Makefile for Miosix embedded OS
## This makefile builds the whole kernel
##
MAKEFILE_VERSION := 1.09
GCCMAJOR := $(shell arm-miosix-eabi-gcc --version | \
perl -e '$$_=<>;/\(GCC\) (\d+)/;print "$$1"')
MAKEFILE_VERSION := 1.10
## KPATH and CONFPATH are forwarded by the parent Makefile
include $(CONFPATH)/config/Makefile.inc
......@@ -21,7 +19,9 @@ kernel/elf_program.cpp \
kernel/process.cpp \
kernel/process_pool.cpp \
kernel/timeconversion.cpp \
kernel/intrusive.cpp \
kernel/SystemMap.cpp \
kernel/cpu_time_counter.cpp \
kernel/scheduler/priority/priority_scheduler.cpp \
kernel/scheduler/control/control_scheduler.cpp \
kernel/scheduler/edf/edf_scheduler.cpp \
......
......@@ -2024,6 +2024,7 @@ INCLUDE_FILE_PATTERNS =
# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
PREDEFINED = WITH_FILESYSTEM \
WITH_CPU_TIME_COUNTER \
_MIOSIX \
MIOSIX_LITTLE_ENDIAN \
__DOXYGEN__
......
......@@ -76,7 +76,7 @@ interrupts @ 0x6800000, or Miosix will fail at the first interrupt.
Then run openocd in a shell:
sudo openocd -f miosix/arch/cortexM3_stm32/stm32f103ze_stm3210e-eval/stm32f10x_eval.cfg
sudo openocd -f miosix/arch/cortexM3_stm32f1/stm32f103ze_stm3210e-eval/stm32f10x_eval.cfg
and in another shell type:
......
##
## Makefile for Miosix embedded OS
##
MAKEFILE_VERSION := 1.09
GCCMAJOR := $(shell arm-miosix-eabi-gcc --version | \
perl -e '$$_=<>;/\(GCC\) (\d+)/;print "$$1"')
MAKEFILE_VERSION := 1.10
## Path to kernel directory (edited by init_project_out_of_git_repo.pl)
KPATH := miosix
## Path to config directory (edited by init_project_out_of_git_repo.pl)
......@@ -59,12 +57,7 @@ LFLAGS := $(LFLAGS_BASE)
DFLAGS := -MMD -MP
## libmiosix.a is among stdlibs because needs to be within start/end group
STDLIBS := -lmiosix -lstdc++ -lc -lm -lgcc
ifneq ($(GCCMAJOR),4)
STDLIBS += -latomic
endif
STDLIBS := -lmiosix -lstdc++ -lc -lm -lgcc -latomic
LINK_LIBS := $(LIBS) -L$(KPATH) -Wl,--start-group $(STDLIBS) -Wl,--end-group
all: all-recursive main
......
/***************************************************************************
* Copyright (C) 2019 by Marsella Daniele *
* *
* 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. *
* *
* As a special exception, if other files instantiate templates or use *
* macros or inline functions from this file, or you compile this file *
* and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with the GNU General *
* Public License. This exception does not invalidate any other reasons *
* why a work based on this file might be covered by the GNU General *
* Public License. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, see <http://www.gnu.org/licenses/> *
***************************************************************************/
#include <cstdio>
#include "miosix.h"
using namespace std;
using namespace miosix;
int main()
{
long long int timeBefore, timeAfter;
using sleepLed = Gpio<GPIOD_BASE, 13>; // orange
using deepSleepLed = Gpio<GPIOD_BASE, 15>; // blue
sleepLed::mode(Mode::OUTPUT);
deepSleepLed::mode(Mode::OUTPUT);
int i=1;
for(;;)
{
printf("Going into deep sleep\n");
fflush(stdout);
delayMs(500);
deepSleepLed::high();
timeBefore = getTime();
Thread::sleep(i * 1000);
timeAfter = getTime();
deepSleepLed::low();
printf("Elapsed time: %lld \n", timeAfter - timeBefore);
{
DeepSleepLock dl;
printf("Going into normal sleep\n");
fflush(stdout);
delayMs(500);
sleepLed::high();
timeBefore = getTime();
Thread::sleep(i * 1000);
timeAfter = getTime();
sleepLed::low();
printf("Elapsed time: %lld \n", timeAfter - timeBefore);
}
i++;
}
}
##
## Makefile for Miosix embedded OS
##
MAKEFILE_VERSION := 1.09
GCCMAJOR := $(shell arm-miosix-eabi-gcc --version | \
perl -e '$$_=<>;/\(GCC\) (\d+)/;print "$$1"')
MAKEFILE_VERSION := 1.10
## Path to kernel directory (edited by init_project_out_of_git_repo.pl)
KPATH := miosix
## Path to config directory (edited by init_project_out_of_git_repo.pl)
......@@ -59,12 +57,7 @@ LFLAGS := $(LFLAGS_BASE)
DFLAGS := -MMD -MP
## libmiosix.a is among stdlibs because needs to be within start/end group
STDLIBS := -lmiosix -lstdc++ -lc -lm -lgcc
ifneq ($(GCCMAJOR),4)
STDLIBS += -latomic
endif
STDLIBS := -lmiosix -lstdc++ -lc -lm -lgcc -latomic
LINK_LIBS := $(LIBS) -L$(KPATH) -Wl,--start-group $(STDLIBS) -Wl,--end-group
all: all-recursive main
......
......@@ -28,7 +28,7 @@
#include <algorithm>
#include <stdexcept>
#include <cstring>
#include "miosix/kernel/scheduler/scheduler.h"
#include <kernel/scheduler/scheduler.h>
#include "util/software_i2c.h"
#include "adpcm.h"
#include "player.h"
......
......@@ -33,11 +33,11 @@ int main()
thread=Thread::create(threadfunc,2048,1,(void*)strlen(str),Thread::JOINABLE);
{
Lock<Mutex> lock(mutex);
for(int i=0;i<strlen(str);i++)
for(int i=0;i<(int)strlen(str);i++)
{
c=str[i];
cond.signal();
if(i<strlen(str)-1) ack.wait(lock);
if(i<(int)strlen(str)-1) ack.wait(lock);
}
}
thread->join();
......
......@@ -24,6 +24,7 @@ void *threadfunc(void *argv)
c=0;
}
pthread_mutex_unlock(&mutex);
return nullptr;
}
int main()
......@@ -33,11 +34,11 @@ int main()
pthread_t thread;
pthread_create(&thread,NULL,threadfunc,(void*)strlen(str));
pthread_mutex_lock(&mutex);
for(int i=0;i<strlen(str);i++)
for(int i=0;i<(int)strlen(str);i++)
{
c=str[i];
pthread_cond_signal(&cond);
if(i<strlen(str)-1) pthread_cond_wait(&ack,&mutex);
if(i<(int)strlen(str)-1) pthread_cond_wait(&ack,&mutex);
}
pthread_mutex_unlock(&mutex);
pthread_join(thread,NULL);
......
build
......@@ -16,6 +16,7 @@ endif()
## Link libraries
set(BOOST_LIBS system)
find_package(Boost COMPONENTS ${BOOST_LIBS} REQUIRED)
target_include_directories(pc_loader PRIVATE ${Boost_INCLUDE_DIRS})
target_link_libraries(pc_loader ${Boost_LIBRARIES})
find_package(Threads REQUIRED)
target_link_libraries(pc_loader ${CMAKE_THREAD_LIBS_INIT})
Update sys/lock.h make pthread_mutex_t compatible with future decisions to
replace the custom list with IntrusiveList.
Update _pthreadtypes.h removing the forward declaration of WaitingList and
change pthread_cond_t to be two opaque pointers, comment that it should have
a memory layout compatible with IntrusiveList.
In libstdc++ header condition_variable the __wait_until_impl checks for timeout
in this way:
__gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(), &__ts);
return (__clock_t::now() < __atime ? cv_status::no_timeout : cv_status::timeout);
use the return value of pthread_cond_timedwait instead to optimize code
In libstdc++ header condition_variable condition_variable::wait_for calls both clock_gettime(CLOCK_REALTIME... and clock_gettime(CLOCK_MONOTONIC...
This is not needed as pthread_cond_timedwait in Miosix refuses to follow the
(broken) standard on purpose and accepts the timeout directly in terms of
CLOCK_MONOTONIC.
defiitions are in time.h, CLOCK_REALTIME is 1, wile CLOCK_MONOTONIC is 4
Some test code:
#include <cstdio>
#include <cstring>
#include <chrono>
#include <thread>
#include <mutex>
#include <condition_variable>
#include "miosix.h"
using namespace std;
using namespace std::chrono;
using namespace miosix;
mutex t25_m1;
condition_variable t25_c1;
void fail(const char *s) { iprintf("Fail %s\n",s); for(;;) ; }
int main()
{
{
unique_lock<mutex> l(t25_m1);
auto a=chrono::steady_clock::now().time_since_epoch().count();
if(t25_c1.wait_for(l,10ms)!=cv_status::timeout) fail("timedwait (1)");
auto b=chrono::steady_clock::now().time_since_epoch().count();
//iprintf("delta=%lld\n",b-a-10000000);
if(llabs(b-a-10000000)>200000) fail("timedwait (2)");
}
{
unique_lock<mutex> l(t25_m1);
auto start=chrono::steady_clock::now();
auto a=start.time_since_epoch().count();
if(t25_c1.wait_until(l,start+10ms)!=cv_status::timeout) fail("timedwait (3)");
auto b=chrono::steady_clock::now().time_since_epoch().count();
//iprintf("delta=%lld\n",b-a-10000000);
if(llabs(b-a-10000000)>200000) fail("timedwait (4)");
}
{
thread t([]{
this_thread::sleep_for(30ms);
t25_c1.notify_one();
});
auto a=chrono::steady_clock::now().time_since_epoch().count();
unique_lock<mutex> l(t25_m1);
if(t25_c1.wait_for(l,100ms)!=cv_status::no_timeout) fail("timedwait (5)");
auto b=chrono::steady_clock::now().time_since_epoch().count();
//iprintf("delta=%lld\n",b-a-30000000);
if(llabs(b-a-30000000)>500000) fail("timedwait (6)");
t.join();
}
for(;;) ;
}
......@@ -22,7 +22,7 @@ void printStat(struct dirent *de, struct stat *st)
close(fd);
}
} else st2.st_ino=st->st_ino; //Can't fstat() an fd to a directory
printf("%13s iD=%d iS=%d iF=%d dev=%d mode=%#o size=%d\n",
printf("%13s iD=%ld iS=%ld iF=%ld dev=%d mode=%#lo size=%lld\n",
de->d_name,de->d_ino,st->st_ino,st2.st_ino,st->st_dev,st->st_mode,st->st_size);
}
......@@ -40,7 +40,7 @@ int main()
if(d==NULL) perror("opendir");
else {
struct dirent *de;
while(de=readdir(d))
while((de=readdir(d)))
{
struct stat st;
if(stat(de->d_name,&st)<0) perror(de->d_name);
......
......@@ -31,8 +31,10 @@ bool shaCmp(unsigned int a[5], unsigned int b[5])
{
if(memcmp(a,b,20)==0) return false;
iprintf("Mismatch\n");
for(int i=0;i<5;i++) iprintf("%04x",a[i]); iprintf("\n");
for(int i=0;i<5;i++) iprintf("%04x",b[i]); iprintf("\n");
for(int i=0;i<5;i++) iprintf("%04x",a[i]);
iprintf("\n");
for(int i=0;i<5;i++) iprintf("%04x",b[i]);
iprintf("\n");
return true;
}
......
/***************************************************************************
* Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013 by Terraneo Federico *
* Copyright (C) 2008 - 2021 by Terraneo Federico *
* *
* 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 *
......@@ -48,19 +48,16 @@
#include <errno.h>
#include <dirent.h>
#include <ext/atomicity.h>
#ifdef _MIOSIX_GCC_PATCH_MAJOR
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
#include <chrono>
#include <atomic>
#endif //_MIOSIX_GCC_PATCH_MAJOR
#include "miosix.h"
#include "config/miosix_settings.h"
#include "interfaces/atomic_ops.h"
#include "board_settings.h"
#include "interfaces/endianness.h"
#include "e20/e20.h"
#include "kernel/intrusive.h"
......@@ -82,10 +79,8 @@
#include <core/cache_cortexMx.h>
#endif //_ARCH_CORTEXM7_STM32F7/H7
#if _MIOSIX_GCC_PATCH_MAJOR >= 2
#include <ctime>
static_assert(sizeof(time_t)==8,"time_t is not 64 bit");
#endif
using namespace std;
using namespace miosix;
......@@ -96,6 +91,13 @@ using namespace miosix;
// Note: can be reduced down to STACK_MIN if only testing with -O2
const unsigned int STACK_SMALL=768;
#ifndef _BOARD_WANDSTEM
const unsigned int MAX_TIME_IRQ_DISABLED=14000;//us
#else
//theoretical maximum for this board is 2^16/2/48e6=680us
const unsigned int MAX_TIME_IRQ_DISABLED=500;//us
#endif
//Functions common to all tests
static void test_name(const char *name);
static void pass();
......@@ -107,7 +109,6 @@ static void test_3();
static void test_4();
static void test_5();
static void test_6();
static void test_7();
static void test_8();
static void test_9();
#ifndef __NO_EXCEPTIONS
......@@ -127,10 +128,9 @@ static void test_21();
static void test_22();
static void test_23();
static void test_24();
#ifdef _MIOSIX_GCC_PATCH_MAJOR
static void test_25();
#endif //_MIOSIX_GCC_PATCH_MAJOR
static void test_26();
static void test_27();
#if defined(_ARCH_CORTEXM7_STM32F7) || defined(_ARCH_CORTEXM7_STM32H7)
void testCacheAndDMA();
#endif //_ARCH_CORTEXM7_STM32F7/H7
......@@ -216,7 +216,6 @@ int main()
test_4();
test_5();
test_6();
test_7();
test_8();
test_9();
#ifndef __NO_EXCEPTIONS
......@@ -236,10 +235,9 @@ int main()
test_22();
test_23();
test_24();
#ifdef _MIOSIX_GCC_PATCH_MAJOR
test_25();
#endif //_MIOSIX_GCC_PATCH_MAJOR
test_26();
test_27();
#if defined(_ARCH_CORTEXM7_STM32F7) || defined(_ARCH_CORTEXM7_STM32H7)
testCacheAndDMA();
#endif //_ARCH_CORTEXM7_STM32F7/H7
......@@ -525,7 +523,8 @@ void syscall_test_system()
pid_t p = Process::create(prog);
Process::waitpid(p, &ret, 0);
if(WEXITSTATUS(ret) != 42){
if(WEXITSTATUS(ret) != 42)
{
iprintf("Host process returned: %d\n", WEXITSTATUS(ret));
fail("The system inside a process has failed");
}
......@@ -538,26 +537,21 @@ void syscall_test_system()
pass();
}
void syscall_test_sleep(){
void syscall_test_sleep()
{
test_name("System Call: sleep");
ElfProgram prog(reinterpret_cast<const unsigned int*>(testsuite_sleep_elf),testsuite_sleep_elf_len);
// iprintf("diff was: %d\n", (unsigned int)getTick());
int ret = 0;
long long time = getTick();
long long time = getTime();
pid_t p = Process::create(prog);
Process::waitpid(p, &ret, 0);
long long diff = llabs((getTick() - time));
if (llabs(diff - 5*TICK_FREQ) > static_cast<long long>(TICK_FREQ * 0.02))
fail("The sleep should have only a little more than 5 seconds.");
long long err = llabs((getTime()-time-5000000000ll)/1000000);
if(err>10) fail("The sleep should have only a little more than 5 seconds.");
pass();
}
void syscall_test_files(){
void syscall_test_files()
{
test_name("System Call: open, read, write, seek, close, system");
ElfProgram prog(reinterpret_cast<const unsigned int*>(testsuite_syscall_elf),testsuite_syscall_elf_len);
......@@ -569,7 +563,8 @@ void syscall_test_files(){
Process::waitpid(child, &ret, 0);
iprintf("Returned value %d\n", WEXITSTATUS(ret));
switch(WEXITSTATUS(ret)){
switch(WEXITSTATUS(ret))
{
case 0:
pass();
break;
......@@ -653,7 +648,6 @@ static void t1_f1(Thread *p)
// In this case t1_v1 will remain false and the test will fail.
// The Thread::sleep(5) is added to make this possibility as unlikely as
//possible.
//If the thread exists, it should modify t1_v1, and exist() must return true
for(int i=0;i<10;i++) //testing 10 times
{
......@@ -667,6 +661,7 @@ static void t1_f1(Thread *p)
if(t1_v1==false) fail("thread not created");
if(Thread::exists(p)==false) fail("Thread::exists (1)");
}
p->terminate();
#ifndef SCHED_TYPE_EDF
Thread::yield();//Give time to the second thread to terminate
......@@ -694,6 +689,7 @@ static void test_1()
test_name("thread creation/deletion");
//Testing getStackBottom()
const unsigned int *y=Thread::getStackBottom();
if(*y!=STACK_FILL) fail("getStackBottom() (1)");
y--;//Now should point to last word of watermark
if(*y!=WATERMARK_FILL) fail("getStackBottom() (2)");
......@@ -703,6 +699,7 @@ static void test_1()
//Testing argv passing
p=Thread::create(t1_p3,STACK_SMALL,0,reinterpret_cast<void*>(0xdeadbeef));
Thread::sleep(5);
if(Thread::exists(p)) fail("thread not deleted (2)");
pass();
}
......@@ -739,7 +736,15 @@ static void t2_p1(void *argv)
static void t2_p2(void *argv)
{
#ifdef SCHED_TYPE_EDF
do {
Thread::getCurrentThread()->wait();
Thread::sleep(20);
t2_v1=true;
} while(!Thread::testTerminate());
#else
while(Thread::testTerminate()==false) t2_v1=true;
#endif
}
static void test_2()
......@@ -773,6 +778,9 @@ static void test_2()
for(int i=0;i<5;i++)
{
bool failed=false;
#ifdef SCHED_TYPE_EDF
t2_p_v1->wakeup();
#endif
{
PauseKernelLock pk;
t2_v1=false;
......@@ -786,6 +794,9 @@ static void test_2()
if(failed) fail("pauseKernel");
}
t2_p_v1->terminate();
#ifdef SCHED_TYPE_EDF
t2_p_v1->wakeup();
#endif
t2_p_v1->join();
pass();
}
......@@ -796,22 +807,23 @@ static void test_2()
/*
tests:
Thread::sleep()
Thread::sleepUntil()
getTick()
Thread::nanoSleepUntil()
getTime()
also tests creation of multiple instances of the same thread
*/
static void t3_p1(void *argv)
{
const int SLEEP_TIME=100;
const int SLEEP_TIME=100;//ms
for(;;)
{
if(Thread::testTerminate()) break;
//Test that Thread::sleep sleeps the desired number of ticks
long long x=getTick();
//Test that Thread::sleep sleeps the desired time
long long x1=getTime(); //getTime returns passed time in ns
Thread::sleep(SLEEP_TIME);
if(llabs(((SLEEP_TIME*TICK_FREQ)/1000)-(getTick()-x))>5)
fail("Thread::sleep() or getTick()");
if(Thread::testTerminate()) break;
long long x2=getTime();
if(llabs((x2-x1)/1000000-SLEEP_TIME)>0) //Max tolerated error is 1ms
fail("Thread::sleep() or getTime()");
}
}
......@@ -820,7 +832,7 @@ static volatile bool t3_deleted;//Set when an instance of t3_p2 is deleted
static void t3_p2(void *argv)
{
const int SLEEP_TIME=15;
const int SLEEP_TIME=15;//ms
for(;;)
{
t3_v2=true;
......@@ -835,18 +847,29 @@ static void t3_p2(void *argv)
static void test_3()
{
test_name("tick and sleep");
Thread *p=Thread::create(t3_p1,STACK_SMALL,0,NULL);
test_name("time and sleep");
long long delta;
{
FastInterruptDisableLock dLock;
auto start=IRQgetTime();
delayUs(MAX_TIME_IRQ_DISABLED);
delta=IRQgetTime()-start;
}
iprintf("%lld\n",delta);
//10% tolerance
auto m=MAX_TIME_IRQ_DISABLED*1000;
if(delta<(m-m/10) || delta>(m+m/10)) fail("getTime and delayMs don't agree");
Thread *p=Thread::create(t3_p1,STACK_SMALL,0,NULL,Thread::JOINABLE);
for(int i=0;i<4;i++)
{
//The other thread is running tick() test
Thread::sleep((101*1000)/TICK_FREQ);
//The other thread is running time test
Thread::sleep(101);
}
p->terminate();
Thread::sleep(200); //make sure the other thread does terminate*/
p->join();
//Testing Thread::sleep() again
t3_v2=false;
p=Thread::create(t3_p2,STACK_SMALL,0,NULL);
p=Thread::create(t3_p2,STACK_SMALL,0,NULL,Thread::JOINABLE);
//t3_p2 sleeps for 15ms, then sets t3_v2. We sleep for 20ms so t3_v2 should
//be true
Thread::sleep(20);
......@@ -862,7 +885,7 @@ static void test_3()
t3_v2=false;
//Creating another instance of t3_p2 to check what happens when 3 threads
//are sleeping
Thread *p2=Thread::create(t3_p2,STACK_SMALL,0,NULL);
Thread *p2=Thread::create(t3_p2,STACK_SMALL,0,NULL,Thread::JOINABLE);
//p will wake @ t=45, main will wake @ t=47 and p2 @ t=50ms
//this will test proper sorting of sleeping_list in the kernel
Thread::sleep(12);
......@@ -870,27 +893,30 @@ static void test_3()
//Deleting the two instances of t3_p2
t3_deleted=false;
p->terminate();
Thread::sleep(20);
p->join();
if(Thread::exists(p)) fail("multiple instances (1)");
if(t3_deleted==false) fail("multiple instances (2)");
t3_deleted=false;
p2->terminate();
Thread::sleep(20);
p2->join();
if(Thread::exists(p)) fail("multiple instances (3)");
if(t3_deleted==false) fail("multiple instances (4)");
//Testing Thread::sleepUntil()
long long tick;
const int period=static_cast<int>(TICK_FREQ*0.01); //10ms
//Testing Thread::nanoSleepUntil()
long long time;
const int period=10000000;//10ms
{
InterruptDisableLock lock; //Making these two operations atomic.
tick=getTick();
tick+=period;
time=IRQgetTime();
time+=period;
}
for(int i=0;i<4;i++)
{
Thread::sleepUntil(tick);
if(tick!=getTick()) fail("Thread::sleepUntil()");
tick+=period;
//time is in number of ns passed, wakeup time should not differ by > 1ms
Thread::nanoSleepUntil(time);
long long t2 = getTime();
if(llabs(t2-time)/1000000>0) fail("Thread::nanoSleepUntil()");
time+=period;
}
pass();
}
......@@ -928,16 +954,16 @@ static void t4_p1(void *argv)
//This takes .014/.03=47% of CPU time
static void t4_p2(void *argv)
{
const int period=static_cast<int>(TICK_FREQ*0.03);
long long tick=getTick();
const int period=30000000;//ms
long long time=getTime();
for(int i=0;i<10;i++)
{
long long prevTick=tick;
tick+=period;
Thread::setPriority(Priority(tick)); //Change deadline
Thread::sleepUntil(prevTick); //Make sure the task is run periodically
long long prevTime=time;
time+=period;
Thread::setPriority(Priority(time)); //Change deadline
Thread::nanoSleepUntil(prevTime); //Make sure the task is run periodically
delayMs(14);
if(getTick()>tick) fail("Deadline missed (A)\n");
if(getTime()>time) fail("Deadline missed (A)\n");
}
}
#endif //SCHED_TYPE_EDF
......@@ -955,18 +981,14 @@ static void test_4()
if(q!=Thread::IRQgetCurrentThread()) fail("IRQgetCurrentThread");
//Check IRQgetPriority
if(p->IRQgetPriority()!=0) fail("IRQgetPriority");
//Check that tick is not incremented and t4_v1 is not updated
long long tick=getTick();
//Check that t4_v1 is not updated
t4_v1=false;
for(int i=0;i<4;i++)
{
delayMs(100);
if((t4_v1==true)||(tick!=getTick()))
delayUs(MAX_TIME_IRQ_DISABLED);
if(t4_v1)
{
enableInterrupts();//
fail("disableInterrupts");
}
}
enableInterrupts();//
#ifndef SCHED_TYPE_EDF
Thread::yield();
......@@ -977,18 +999,14 @@ static void test_4()
if(t4_v1==false) fail("variable not updated");
fastDisableInterrupts();//
//Check that tick is not incremented and t4_v1 is not updated
tick=getTick();
//Check that t4_v1 is not updated
t4_v1=false;
for(int i=0;i<4;i++)
{
delayMs(100);
if((t4_v1==true)||(tick!=getTick()))
delayUs(MAX_TIME_IRQ_DISABLED);
if(t4_v1)
{
fastEnableInterrupts();//
fail("disableInterrupts");
}
}
fastEnableInterrupts();//
#ifndef SCHED_TYPE_EDF
Thread::yield();
......@@ -1009,12 +1027,9 @@ static void test_4()
//Since priority is higher, the other thread must not run
//Of course this is not true for the control based scheduler
t4_v1=false;
for(int i=0;i<4;i++)
{
Thread::yield();//must return immediately
delayMs(100);
if(t4_v1==true) fail("setPriority (1)");
}
#endif //SCHED_TYPE_CONTROL_BASED
//Restoring original priority
Thread::setPriority(0);
......@@ -1024,17 +1039,17 @@ static void test_4()
Thread::sleep(10);
#ifdef SCHED_TYPE_EDF
Thread::create(t4_p2,STACK_SMALL);
const int period=static_cast<int>(TICK_FREQ*0.05);
tick=getTick();
const int period=50000000;//ms
long long time=getTime();
//This takes .024/.05=48% of CPU time
for(int i=0;i<10;i++)
{
long long prevTick=tick;
tick+=period;
Thread::setPriority(Priority(tick)); //Change deadline
Thread::sleepUntil(prevTick); //Make sure the task is run periodically
long long prevTime=time;
time+=period;
Thread::setPriority(Priority(time)); //Change deadline
Thread::nanoSleepUntil(prevTime); //Make sure the task is run periodically
delayMs(24);
if(getTick()>tick) fail("Deadline missed (B)\n");
if(getTime()>time) fail("Deadline missed (B)\n");
}
#endif //SCHED_TYPE_EDF
pass();
......@@ -1171,7 +1186,7 @@ class Sequence
static Sequence seq;
static Mutex t6_m1;
static FastMutex t6_m1a;
#ifndef SCHED_TYPE_CONTROL_BASED
static void t6_p1(void *argv)
{
t6_m1.lock();
......@@ -1205,7 +1220,7 @@ static void t6_p3(void *argv)
fail("priority inheritance (3)");
t6_m1.unlock();
}
#endif
static void t6_p4(void *argv)
{
t6_m1.lock();
......@@ -1361,6 +1376,7 @@ static void test_6()
Thread::setPriority(priorityAdapter(0));
#endif //SCHED_TYPE_EDF
seq.clear();
#ifndef SCHED_TYPE_CONTROL_BASED
//Create first thread
Thread::create(t6_p1,STACK_SMALL,priorityAdapter(0),NULL);
Thread::sleep(20);
......@@ -1391,6 +1407,7 @@ static void test_6()
t6_m1.unlock();
Thread::sleep(350);//Ensure all threads are deleted
#endif
//
// Testing tryLock
//
......@@ -1492,7 +1509,6 @@ static void test_6()
t3->join();
t4->join();
Thread::sleep(10);
//
// Testing recursive mutexes
//
......@@ -1658,87 +1674,8 @@ static void test_6()
}
//
// Test 7
// Test 7 removed, Timer class deprecated
//
/*
tests:
Timer::start
Timer::stop
Timer::isRunning
Timer::interval
Timer::clear
*/
static void test_timer(Timer *t)
{
//Testing interval behaviour when timer never started
if(t->interval()!=-1) fail("interval (1)");
//Testing isRunning
if(t->isRunning()==true) fail("isRunning (1)");
t->start();
//Testing interval behaviour when timer not stopped
if(t->interval()!=-1) fail("interval (2)");
//Testing isRunning
if(t->isRunning()==false) fail("isRunning (2)");
Thread::sleep(100);
t->stop();
//Testing interval precision
if(t->interval()==-1) fail("interval (3)");
if(llabs(t->interval()-((100*TICK_FREQ)/1000))>4) fail("not precise");
//Testing isRunning
if(t->isRunning()==true) fail("isRunning (1)");
}
static void test_7()
{
test_name("Timer class");
Timer t;
//Testing a new timer
test_timer(&t);
t.clear();
//Testing after clear
test_timer(&t);
//Testing copy constructor and operator = with a stopped timer
t.clear();
t.start();
Thread::sleep(100);
t.stop();
Timer w(t);
if(w.interval()==-1) fail("interval (copy 1)");
if(llabs(w.interval()-((100*TICK_FREQ)/1000))>4) fail("not precise (copy 1)");
if(w.isRunning()==true) fail("isRunning (copy 1)");
Timer q;
q=t;
if(q.interval()==-1) fail("interval (= 1)");
if(llabs(q.interval()-((100*TICK_FREQ)/1000))>4) fail("not precise (= 1)");
if(q.isRunning()==true) fail("isRunning (= 1)");
//Testing copy constructor and operator = with a running timer
t.clear();
t.start();
Thread::sleep(100);
Timer x(t);//copy constructor called when running
x.stop();
if(x.interval()==-1) fail("interval (copy 2)");
if(llabs(x.interval()-((100*TICK_FREQ)/1000))>4) fail("not precise (copy 2)");
if(x.isRunning()==true) fail("isRunning (copy 2)");
Timer y;
y=t;//Operator = called when running
y.stop();
if(y.interval()==-1) fail("interval (= 2)");
if(llabs(y.interval()-((100*TICK_FREQ)/1000))>4) fail("not precise (= 2)");
if(y.isRunning()==true) fail("isRunning (= 2)");
//Testing concatenating time intervals
t.clear();//Calling clear without calling stop. done on purpose
t.start();
Thread::sleep(100);
t.stop();
t.start();
Thread::sleep(150);
t.stop();
if(t.interval()==-1) fail("interval (= 2)");
if(llabs(t.interval()-((250*TICK_FREQ)/1000))>4) fail("not precise (= 2)");
pass();
}
//
// Test 8
......@@ -1751,9 +1688,9 @@ Queue::put()
Queue::get()
Queue::reset()
Queue::IRQput()
Queue::waitUntilNotFull()
Queue::IRQputBlocking()
Queue::IRQget()
Queue::waitUntilNotEmpty()
Queue::IRQgetBlocking()
FIXME: The overloaded versions of IRQput and IRQget are not tested
*/
......@@ -1835,52 +1772,41 @@ static void test_8()
read++;
}
}
//Test waitUntilNotFull and IRQput
//Test IRQputBlocking
t8_q1.reset();
t8_q2.reset();
write='A';
read='A';
disableInterrupts();//
for(j=0;j<8;j++)
{
if(t8_q1.isFull())
FastInterruptDisableLock dLock;
for(j=0;j<8;j++)
{
enableInterrupts();//
t8_q1.waitUntilNotFull();
disableInterrupts();//
}
if(t8_q1.isFull()) fail("waitUntilNotFull()");
if(t8_q1.IRQput(write)==false) fail("IRQput (1)");
t8_q1.IRQputBlocking(write,dLock);
write++;//Advance to next char, to check order
}
enableInterrupts();//
}
for(j=0;j<8;j++)
{
char c;
t8_q2.get(c);
if(c!=read) fail("IRQput (2)");
if(c!=read) fail("IRQputBlocking");
read++;
}
//Test waitUntilNotEmpty and IRQget
//Test IRQgetBlocking
t8_q1.reset();
t8_q2.reset();
write='A';
read='A';
for(j=0;j<8;j++)
{
disableInterrupts();//
FastInterruptDisableLock dLock;
t8_q1.IRQput(write);
write++;
if(t8_q2.isEmpty()==false) fail("Unexpected");
enableInterrupts();//
t8_q2.waitUntilNotEmpty();
disableInterrupts();//
if(t8_q2.isEmpty()==true) fail("waitUntilNotEmpty()");
char c='\0';
if(t8_q2.IRQget(c)==false) fail("IRQget (1)");
if(c!=read) fail("IRQget (2)");
t8_q2.IRQgetBlocking(c,dLock);
if(c!=read) fail("IRQgetBlocking");
read++;
enableInterrupts();//
}
p->terminate();
t8_q1.put('0');
......@@ -1900,10 +1826,25 @@ isKernelRunning()
areInterruptsEnabled()
*/
static volatile bool t9_v1;
void t9_p1(void*)
{
for(;;)
{
if(Thread::testTerminate()) break;
t9_v1=true;
#ifdef SCHED_TYPE_EDF
Thread::sleep(1);
#endif //SCHED_TYPE_EDF
}
}
static void test_9()
{
test_name("isKernelRunning and save/restore interrupts");
//Testing kernel_running with nested pause_kernel()
Thread *p=Thread::create(t9_p1,STACK_SMALL,0,NULL,Thread::JOINABLE);
if(isKernelRunning()==false) fail("isKernelRunning() (1)");
pauseKernel();//1
if(isKernelRunning()==true)
......@@ -1927,12 +1868,11 @@ static void test_9()
restartKernel();//1
if(isKernelRunning()==false) fail("isKernelRunning() (5)");
//Testing nesting of disableInterrupts()
long long i;
if(areInterruptsEnabled()==false) fail("areInterruptsEnabled() (1)");
disableInterrupts();//Now interrupts should be disabled
i=getTick();
delayMs(100);
if(i!=getTick())
t9_v1=false;
delayUs(MAX_TIME_IRQ_DISABLED/3);
if(t9_v1)
{
enableInterrupts();
fail("disableInterrups() nesting (1)");
......@@ -1943,22 +1883,22 @@ static void test_9()
fail("areInterruptsEnabled() (2)");
}
disableInterrupts();//Interrupts already disabled
i=getTick();
delayMs(100);
if(i!=getTick())
delayUs(MAX_TIME_IRQ_DISABLED/3);
if(t9_v1)
{
enableInterrupts();
enableInterrupts();
fail("disableInterrups() nesting (2)");
}
if(areInterruptsEnabled()==true)
{
enableInterrupts();
enableInterrupts();
fail("areInterruptsEnabled() (3)");
}
enableInterrupts();//Now interrupts should remain disabled
i=getTick();
delayMs(100);
if(i!=getTick())
delayUs(MAX_TIME_IRQ_DISABLED/3);
if(t9_v1)
{
enableInterrupts();
fail("enableInterrupts() nesting (1)");
......@@ -1969,14 +1909,14 @@ static void test_9()
fail("areInterruptsEnabled() (4)");
}
enableInterrupts();//Now interrupts should be enabled
i=getTick();
delayMs(100);
if(i==getTick())
delayMs(10);
if(t9_v1==false)
{
enableInterrupts();
fail("enableInterrupts() nesting (2)");
}
if(areInterruptsEnabled()==false) fail("areInterruptsEnabled() (5)");
p->terminate();
p->join();
pass();
}
......@@ -2013,9 +1953,11 @@ void t10_p1(void *argv)
void test_10()
{
test_name("Exception handling");
Thread::sleep(10);
Thread *t=Thread::create(t10_p1,1024+512,0,NULL);
Thread::sleep(80);
//This sleep needs to be long enough to let the other thread print the
//"An exception propagated ..." string on the serial port, otherwise the
//test fails because the other thread still exists because it is printing.
Thread::sleep(100);
if(Thread::exists(t)) fail("Thread not deleted");
pass();
}
......@@ -2306,6 +2248,12 @@ void t15_p2(void *argv)
}
}
void t15_p3(void *argv)
{
Thread::sleep(30);
t15_c1.signal();
}
static void test_15()
{
test_name("Condition variables");
......@@ -2355,6 +2303,26 @@ static void test_15()
Thread::sleep(10);
if(Thread::exists(p1) || Thread::exists(p2))
fail("Threads not deleted (2)");
//testing timedWait
long long b,a=getTime()+10000000; //10ms
{
Lock<Mutex> l(t15_m1);
if(t15_c1.timedWait(l,a)!=TimedWaitResult::Timeout) fail("timedwait (1)");
b=getTime();
}
//iprintf("delta=%lld\n",b-a);
if(llabs(b-a)>200000) fail("timedwait (2)");
Thread::create(t15_p3,STACK_SMALL,0);
a=getTime()+100000000; //100ms
{
Lock<Mutex> l(t15_m1);
if(t15_c1.timedWait(l,a)!=TimedWaitResult::NoTimeout) fail("timedwait (1)");
b=getTime();
}
//iprintf("delta=%lld\n",b-a);
if(llabs(b-a+70000000)>500000) fail("timedwait (4)");
Thread::sleep(10);
pass();
}
......@@ -2393,6 +2361,25 @@ inline Thread *toThread(pthread_t t)
return reinterpret_cast<Thread*>(t);
}
static constexpr int nsPerSec = 1000000000;
inline long long timespec2ll(const struct timespec *tp)
{
return static_cast<long long>(tp->tv_sec) * nsPerSec + tp->tv_nsec;
}
void timespecAdd(struct timespec *t, long long ns)
{
long long x=timespec2ll(t)+ns;
t->tv_sec = x / nsPerSec;
t->tv_nsec = static_cast<long>(x % nsPerSec);
}
long long timespecDelta(struct timespec *a, struct timespec *b)
{
return timespec2ll(a)-timespec2ll(b);
}
void *t16_p1(void *argv)
{
toThread(pthread_self())->wait();
......@@ -2441,17 +2428,17 @@ void *t16_p4(void *argv)
return NULL;
}
int a=0;
int t16_v2=0;
void t16_f1()
{
a++;
t16_v2++;
}
void t16_f2()
{
Thread::sleep(200);
a++;
t16_v2++;
}
pthread_once_t t16_o1=PTHREAD_ONCE_INIT;
......@@ -2625,6 +2612,29 @@ static void test_16()
pthread_join(thread,NULL);
Thread::sleep(10);
if(pthread_cond_destroy(&t16_c2)!=0) fail("cond destroy");
//testing pthread_cond_timedwait
timespec a,b;
clock_gettime(CLOCK_MONOTONIC,&a);
timespecAdd(&a,10000000); //10ms
if(pthread_mutex_lock(&t16_m1)!=0) fail("mutex lock (7)"); //<---
if(pthread_cond_timedwait(&t16_c1,&t16_m1,&a)!=ETIMEDOUT) fail("timedwait (1)");
clock_gettime(CLOCK_MONOTONIC,&b);
if(pthread_mutex_unlock(&t16_m1)!=0) fail("mutex unlock (7)"); //<---
//iprintf("delta=%lld\n",timespecDelta(&b,&a));
if(llabs(timespecDelta(&b,&a))>200000) fail("timedwait (2)");
if(pthread_create(&thread,NULL,t16_p3,NULL)!=0) fail("pthread_create (10)");
clock_gettime(CLOCK_MONOTONIC,&a);
timespecAdd(&a,100000000); //100ms
if(pthread_mutex_lock(&t16_m1)!=0) fail("mutex lock (8)"); //<---
t16_v1=false;
if(pthread_cond_timedwait(&t16_c1,&t16_m1,&a)!=0) fail("timedwait (3)");
clock_gettime(CLOCK_MONOTONIC,&b);
if(t16_v1==false) fail("did not really wait");
if(pthread_mutex_unlock(&t16_m1)!=0) fail("mutex unlock (8)"); //<---
//iprintf("delta=%lld\n",timespecDelta(&b,&a));
if(llabs(timespecDelta(&b,&a)+70000000)>500000) fail("timedwait (4)");
pthread_join(thread,NULL);
Thread::sleep(10);
//
// Testing pthread_once
//
......@@ -2632,17 +2642,17 @@ static void test_16()
//pthread_once, it wouldn't be possible to run the test more than once ;)
if(t16_o1.init_executed) t16_o1.init_executed=0;
if(t16_o2.init_executed) t16_o2.init_executed=0;
a=0;
t16_v2=0;
if(pthread_once(&t16_o1,t16_f1)!=0) fail("pthread_once 1");
if(a!=1) fail("pthread_once 2");
if(t16_v2!=1) fail("pthread_once 2");
if(pthread_once(&t16_o1,t16_f1)!=0) fail("pthread_once 2");
if(a!=1) fail("pthread_once 3");
if(t16_v2!=1) fail("pthread_once 3");
if(sizeof(pthread_once_t)!=2) fail("pthread_once 4");
a=0;
t16_v2=0;
Thread::create(t16_p5,STACK_MIN);
Thread::sleep(50);
if(pthread_once(&t16_o2,t16_f2)!=0) fail("pthread_once 5");
if(a!=1) fail("pthread_once does not wait");
if(t16_v2!=1) fail("pthread_once does not wait");
pass();
}
......@@ -3044,11 +3054,11 @@ void t20_t2(void* arg)
t20_v1=0;
eq->post(t20_f1);
eq->post(t20_f1);
unsigned long long t1=getTick();
unsigned long long t1=getTime();
eq->post(bind(t20_f2,10,4)); //This should block
unsigned long long t2=getTick();
unsigned long long t2=getTime();
//The other thread sleep for 50ms before calling run()
if((t2-t1)<static_cast<unsigned long long>(TICK_FREQ*0.04))
if((t2-t1)/1000000 < 40)
fail("Not blocked");
Thread::sleep(10);
if(t20_v1!=14) fail("Not called");
......@@ -3257,13 +3267,12 @@ These are not actually in the kernel but in the patches to gcc
also tests atomic operations provided by miosix (interfaces/atomic_ops.h)
*/
const int t22_iterations=100000;
static int t22_v1;
static int t22_v2;
static int t22_v3;
static int t22_v4;
#ifdef _MIOSIX_GCC_PATCH_MAJOR
atomic<int> t22_v6;
#endif //_MIOSIX_GCC_PATCH_MAJOR
static bool t22_v5;
......@@ -3278,26 +3287,30 @@ static void t22_t2(void *argv)
while(Thread::testTerminate()==false)
{
t22_v5=true;
#ifndef SCHED_TYPE_EDF
Thread::yield();
#else //SCHED_TYPE_EDF
Thread::sleep(1);
#endif //SCHED_TYPE_EDF
}
}
static void *t22_t1(void*)
{
for(int i=0;i<100000;i++)
for(int i=0;i<t22_iterations;i++)
{
__gnu_cxx::__atomic_add(&t22_v1,1);
__gnu_cxx::__exchange_and_add(&t22_v2,-1);
atomicAdd(&t22_v3,1);
atomicAddExchange(&t22_v4,1);
#ifdef _MIOSIX_GCC_PATCH_MAJOR
t22_v6++;
#endif //_MIOSIX_GCC_PATCH_MAJOR
#ifdef SCHED_TYPE_EDF
if((i % (t22_iterations/10))==0) Thread::sleep(1);
#endif //SCHED_TYPE_EDF
}
return 0;
}
#ifdef _MIOSIX_GCC_PATCH_MAJOR
int t22_v8;
class t22_c1
......@@ -3316,11 +3329,16 @@ shared_ptr<t22_c1> t22_v7;
void t22_t3(void*)
{
auto inst1=make_shared<t22_c1>();
for(int i=0;i<100000;i++) atomic_store(&t22_v7,inst1);
for(int i=0;i<t22_iterations;i++)
{
atomic_store(&t22_v7,inst1);
#ifdef SCHED_TYPE_EDF
if((i % (t22_iterations/10))==0) Thread::sleep(1);
#endif //SCHED_TYPE_EDF
}
atomic_store(&t22_v7,shared_ptr<t22_c1>(nullptr));
inst1->canDelete=true;
}
#endif //_MIOSIX_GCC_PATCH_MAJOR
static void test_22()
{
......@@ -3330,22 +3348,21 @@ static void test_22()
t22_v1=t22_v2=t22_v3=t22_v4=0;
pthread_t t;
pthread_create(&t,0,t22_t1,0);
for(int i=0;i<100000;i++)
for(int i=0;i<t22_iterations;i++)
{
__gnu_cxx::__atomic_add(&t22_v1,-1);
__gnu_cxx::__exchange_and_add(&t22_v2,1);
atomicAdd(&t22_v3,-1);
atomicAddExchange(&t22_v4,-1);
#ifdef _MIOSIX_GCC_PATCH_MAJOR
t22_v6--;
#endif //_MIOSIX_GCC_PATCH_MAJOR
#ifdef SCHED_TYPE_EDF
if((i % (t22_iterations/10))==0) Thread::sleep(1);
#endif //SCHED_TYPE_EDF
}
pthread_join(t,0);
if(t22_v1!=0 || t22_v2!=0 || t22_v3!=0 || t22_v4!=0)
fail("not thread safe");
#ifdef _MIOSIX_GCC_PATCH_MAJOR
if(t22_v6!=0) fail("C++11 atomics not thread safe");
#endif //_MIOSIX_GCC_PATCH_MAJOR
//Functional test for miosix atomic ops
int x=10;
......@@ -3384,35 +3401,32 @@ static void test_22()
bool error=false;
Thread *t2=Thread::create(t22_t2,STACK_MIN,0,0,Thread::JOINABLE);
{
#if __CORTEX_M != 0x00
FastInterruptDisableLock dLock;
#else
//Cortex M0 does not have atomic ops, they are emulated by disabling IRQ
InterruptDisableLock dLock;
#endif
t22_v5=false;
int x=10;
if(atomicSwap(&x,20)!=10) error=true;
if(x!=20) error=true;
delayMs(5); //Wait to check that interrupts are disabled
x=10;
atomicAdd(&x,-5);
if(x!=5) error=true;
delayMs(5); //Wait to check that interrupts are disabled
x=10;
if(atomicAddExchange(&x,5)!=10) error=true;
if(x!=15) error=true;
delayMs(5); //Wait to check that interrupts are disabled
x=10;
if(atomicCompareAndSwap(&x,11,12)!=10) error=true;
if(x!=10) error=true;
if(atomicCompareAndSwap(&x,10,13)!=10) error=true;
if(x!=13) error=true;
delayMs(5); //Wait to check that interrupts are disabled
t22_s1 data;
t22_s1 *dataPtr=&data;
void * const volatile *ptr=
......@@ -3424,7 +3438,7 @@ static void test_22()
if(atomicFetchAndIncrement(ptr,1,-2)!=dataPtr) error=true;
if(data.b!=8) error=true;
delayMs(5); //Wait to check that interrupts are disabled
delayUs(MAX_TIME_IRQ_DISABLED); //Wait to check interrupts are disabled
if(t22_v5) error=true;
}
if(error) fail("Interrupt test not passed");
......@@ -3432,12 +3446,17 @@ static void test_22()
t2->terminate();
t2->join();
#ifdef _MIOSIX_GCC_PATCH_MAJOR
t22_v8=0;
{
Thread *t3=Thread::create(t22_t3,STACK_SMALL,0,0,Thread::JOINABLE);
auto inst2=make_shared<t22_c1>();
for(int i=0;i<100000;i++) atomic_store(&t22_v7,inst2);
for(int i=0;i<t22_iterations;i++)
{
atomic_store(&t22_v7,inst2);
#ifdef SCHED_TYPE_EDF
if((i % (t22_iterations/10))==0) Thread::sleep(1);
#endif //SCHED_TYPE_EDF
}
atomic_store(&t22_v7,shared_ptr<t22_c1>(nullptr));
// NOTE: we can't check use_count to be 1 because the C++ specs say it
// can provide inaccurate results, and it does. Apparently atomic_store
......@@ -3455,7 +3474,6 @@ static void test_22()
iprintf("deleted %d\n",t22_v8);
fail("Not deleted");
}
#endif //_MIOSIX_GCC_PATCH_MAJOR
pass();
}
......@@ -3673,7 +3691,7 @@ static void test_24()
dtorCalled=false;
{
intrusive_ref_ptr<Base0> ptr1;
assert(ptr1==0);
assert(!ptr1);
}
assert(dtorCalled==false);
......@@ -3792,7 +3810,7 @@ static void test_24()
{
intrusive_ref_ptr<Derived1> ptr2=
dynamic_pointer_cast<Derived1>(ptr1);
assert(ptr2==0);
assert(!ptr2);
}
assert(dtorCalled==false);
}
......@@ -3822,7 +3840,7 @@ static void test_24()
intrusive_ref_ptr<Base0> ptr1(new Base0);
ptr1.reset();
assert(dtorCalled);
assert(ptr1==0);
assert(!ptr1);
}
// Reset, on a shared pointer
......@@ -3832,8 +3850,8 @@ static void test_24()
{
intrusive_ref_ptr<Base0> ptr2(ptr1);
ptr1.reset();
assert(ptr1==0);
assert(ptr2!=0);
assert(!ptr1);
assert(ptr2);
ptr2->check();
}
assert(dtorCalled);
......@@ -3867,7 +3885,6 @@ static void test_24()
pass();
}
#ifdef _MIOSIX_GCC_PATCH_MAJOR
//
// Test 25
//
......@@ -3931,7 +3948,7 @@ static void test_25()
* C++11 threads run with MAIN_PRIORITY, so to avoid deadlocks or other
* artifacts, we restore main't priority to the default, and set it back
* to 0 at the end of this test, as the rest of the testsuite runs with
lowest priority
* lowest priority
*/
Thread::setPriority(MAIN_PRIORITY);
test_name("C++11 threads");
......@@ -4063,6 +4080,7 @@ static void test_25()
//
// Testing yield
//
#ifndef SCHED_TYPE_EDF
{
volatile bool enable=false;
volatile bool flag=false;
......@@ -4073,6 +4091,7 @@ static void test_25()
if(flag==false) fail("this_thread::yield");
thr.join();
}
#endif //SCHED_TYPE_EDF
//
// Testing system_clock/this_thread::sleep_for
//
......@@ -4091,10 +4110,39 @@ static void test_25()
auto b=chrono::steady_clock::now().time_since_epoch().count();
if(llabs(b-a-100000000)>5000000) fail("sleep_until");
}
//
// Testing condition_variable timed wait
//
{
unique_lock<mutex> l(t25_m1);
auto a=chrono::steady_clock::now().time_since_epoch().count();
if(t25_c1.wait_for(l,10ms)!=cv_status::timeout) fail("timedwait (1)");
auto b=chrono::steady_clock::now().time_since_epoch().count();
//iprintf("delta=%lld\n",b-a-10000000);
if(llabs(b-a-10000000)>250000) fail("timedwait (2)");
}
{
unique_lock<mutex> l(t25_m1);
auto start=chrono::steady_clock::now();
auto a=start.time_since_epoch().count();
if(t25_c1.wait_until(l,start+10ms)!=cv_status::timeout) fail("timedwait (3)");
auto b=chrono::steady_clock::now().time_since_epoch().count();
//iprintf("delta=%lld\n",b-a-10000000);
if(llabs(b-a-10000000)>250000) fail("timedwait (4)");
}
{
thread t([]{ this_thread::sleep_for(30ms); t25_c1.notify_one(); });
auto a=chrono::steady_clock::now().time_since_epoch().count();
unique_lock<mutex> l(t25_m1);
if(t25_c1.wait_for(l,100ms)!=cv_status::no_timeout) fail("timedwait (5)");
auto b=chrono::steady_clock::now().time_since_epoch().count();
//iprintf("delta=%lld\n",b-a-30000000);
if(llabs(b-a-30000000)>500000) fail("timedwait (6)");
t.join();
}
pass();
Thread::setPriority(0);
}
#endif //_MIOSIX_GCC_PATCH_MAJOR
//
// Test 26
......@@ -4129,6 +4177,119 @@ static void test_26()
pass();
}
//
// Test 27
//
/*
tests:
Semaphore class
*/
struct t27_data
{
t27_data(): consumer(5), producer(5) {}
Semaphore consumer;
Semaphore producer;
};
void *t27_t1(void *xdata)
{
t27_data *data=reinterpret_cast<t27_data *>(xdata);
for(int i=0; i<4; i++) data->consumer.wait();
if(data->consumer.getCount() != 1)
fail("wait to 1 with counter >= 0 not working");
bool res = data->consumer.tryWait();
if(data->consumer.getCount() != 0 || res != true)
fail("tryWait to 0 with counter = 1 not working");
res = data->consumer.tryWait();
if(data->consumer.getCount() != 0 || res != false)
fail("tryWait with counter = 0 not working");
for(int i=0; i<10; i++)
{
data->producer.signal();
data->consumer.wait();
}
return nullptr;
}
void *t27_t2(void *xdata)
{
t27_data *data=reinterpret_cast<t27_data *>(xdata);
for(int i=0; i<2; i++)
{
Thread::sleep(100);
data->producer.signal();
data->consumer.wait();
}
return nullptr;
}
void *t27_t3(void *xdata)
{
t27_data *data=reinterpret_cast<t27_data *>(xdata);
data->producer.signal();
data->consumer.wait();
return nullptr;
}
static void test_27()
{
test_name("Semaphores");
t27_data data;
Thread *thd=Thread::create(t27_t1,STACK_SMALL,MAIN_PRIORITY,reinterpret_cast<void*>(&data),Thread::JOINABLE);
for(int i=0; i<5; i++) data.producer.wait();
if(data.producer.getCount() != 0)
fail("wait to zero with counter >= 0 not working");
for(int i=0; i<10; i++)
{
data.producer.wait();
data.consumer.signal();
}
thd->join();
if(data.producer.getCount() != 0 && data.consumer.getCount() != 0)
fail("wait with counter == 0 not working");
Thread *thd2=Thread::create(t27_t2,STACK_SMALL,MAIN_PRIORITY,reinterpret_cast<void*>(&data),Thread::JOINABLE);
auto res = data.producer.timedWait(getTime()+200*1000000LL);
if(res == TimedWaitResult::Timeout)
fail("timedWait did not return the first time without timeout, timebase incorrect?");
data.consumer.signal();
res = data.producer.timedWait(getTime()+8*1000000LL);
int j = 0;
while(res == TimedWaitResult::Timeout)
{
j++;
res = data.producer.timedWait(getTime()+8*1000000LL);
}
if(j < 12 || j > 13)
{
iprintf("j=%d, should be 12 or 13\n", j);
fail("timedWait timed out too many/few times, timebase incorrect?");
}
data.consumer.signal();
thd2->join();
if(data.producer.getCount() != 0 && data.consumer.getCount() != 0)
fail("timedWait with counter == 0 not working");
//Testing multiple threads enqueued on the same semaphore
#ifndef SCHED_TYPE_EDF
Thread *thds[3];
for(int i=0; i<3; i++)
{
//Priority is > than MAIN_PRIORITY to ensure all the threads get stuck
//at the `data->consumer.wait();` line and not earlier.
//This test is disabled on EDF scheduler to avoid having to fudge with
//deadlines to make this happen.
thds[i] = Thread::create(t27_t3,STACK_SMALL,MAIN_PRIORITY+1,reinterpret_cast<void*>(&data),Thread::JOINABLE);
data.producer.wait();
}
for(int i=0; i<3; i++) data.consumer.signal();
for(int i=0; i<3; i++) thds[i]->join(); //This hangs if the test fails
#endif
pass();
}
#if defined(_ARCH_CORTEXM7_STM32F7) || defined(_ARCH_CORTEXM7_STM32H7)
static Thread *waiting=nullptr; /// Thread waiting on DMA completion IRQ
......@@ -4887,29 +5048,18 @@ serial write speed
static void benchmark_1()
{
Timer t;
t.start();
using namespace std::chrono;
auto t=system_clock::now();
extern unsigned long _data asm("_data");
char *data=reinterpret_cast<char*>(&_data);
#ifndef _ARCH_ARM7_LPC2000
memDump(data,2048);
t.stop();
auto d=system_clock::now()-t;
//every line dumps 16 bytes, and is 81 char long (considering \r\n)
//so (2048/16)*81=10368
iprintf("Time required to print 10368 char is %dms\n",(
t.interval()*1000)/TICK_FREQ);
unsigned int baudrate=10368*10000/((t.interval()*1000)/TICK_FREQ);
iprintf("Time required to print 10368 char is %lldms\n",
duration_cast<milliseconds>(d).count());
unsigned int baudrate=10368*10000/duration_cast<milliseconds>(d).count();
iprintf("Effective baud rate =%u\n",baudrate);
#else //_ARCH_ARM7_LPC2000
memDump(data,32768);
t.stop();
//every line dumps 16 bytes, and is 81 char long (considering \r\n)
//so (32768/16)*81=165888
iprintf("Time required to print 165888 char is %dms\n",(
t.interval()*1000)/TICK_FREQ);
unsigned int baudrate=165888*10000/((t.interval()*1000)/TICK_FREQ);
iprintf("Effective baud rate =%u\n",baudrate);
#endif //_ARCH_ARM7_LPC2000
}
//
......@@ -4920,52 +5070,43 @@ tests:
context switch speed
*/
static bool b2_v1;
static int b2_v2;
static void b2_p1(void *argv)
{
for(;;)
{
if(Thread::testTerminate()) break;
Thread::yield();
if(b2_v1) b2_v2++;
}
}
static int b2_f1()
static int b2_f1(int priority)
{
int i=0;
Timer t;
t.start();
for(;;)
{
//Since calling interval() on a running timer is not allowed,
//we need to make a copy of the timer and stop the copy.
Timer k(t);
k.stop();
if((unsigned int)k.interval()>=TICK_FREQ) break;
i+=2;
Thread::yield();
}
t.stop();
return i;
Thread::setPriority(priority);
b2_v1=false;
b2_v2=0;
Thread *t1=Thread::create(b2_p1,STACK_SMALL,priority,NULL,Thread::JOINABLE);
Thread *t2=Thread::create(b2_p1,STACK_SMALL,priority,NULL,Thread::JOINABLE);
b2_v1=true; //Start counting
Thread::sleep(1000);
b2_v1=false; //Stop counting
t1->terminate();
t2->terminate();
t1->join();
t2->join();
return b2_v2;
}
static void benchmark_2()
{
#ifndef SCHED_TYPE_EDF
//Test context switch time at maximum priority
Thread::setPriority(3);//Using max priority
Thread *p=Thread::create(b2_p1,STACK_SMALL,3,NULL);
int i=b2_f1();
p->terminate();
iprintf("%d context switch per second (max priority)\n",i);
Thread::sleep(10);
Thread::setPriority(0);//Restoring original priority
p=Thread::create(b2_p1,STACK_SMALL,0,NULL);
i=b2_f1();
p->terminate();
iprintf("%d context switch per second (min priority)\n",i);
Thread::sleep(10);
iprintf("%d context switch per second (max priority)\n",b2_f1(3));
iprintf("%d context switch per second (min priority)\n",b2_f1(0));
#else //SCHED_TYPE_EDF
iprintf("Context switch benchmark not possible with edf\n");
iprintf("Context switch benchmark not possible with EDF\n");
#endif //SCHED_TYPE_EDF
}
......@@ -4980,6 +5121,7 @@ makes a 1MB file and measures time required to read/write it.
static void benchmark_3()
{
using namespace std::chrono;
//Write benchmark
const char FILENAME[]="/sd/speed.txt";
const unsigned int BUFSIZE=1024;
......@@ -4993,31 +5135,28 @@ static void benchmark_3()
return;
}
setbuf(f,NULL);
Timer total,part;
int i,max=0;
total.start();
auto total=system_clock::now();
for(i=0;i<1024;i++)
{
part.start();
auto part=system_clock::now();
if(fwrite(buf,1,BUFSIZE,f)!=BUFSIZE)
{
iprintf("Write error\n");
break;
}
part.stop();
if(part.interval()>max) max=part.interval();
part.clear();
auto d=system_clock::now()-part;
max=std::max(max,static_cast<int>(duration_cast<milliseconds>(d).count()));
}
total.stop();
auto d=system_clock::now()-total;
if(fclose(f)!=0) iprintf("Error in fclose 1\n");
iprintf("Filesystem write benchmark\n");
unsigned int writeTime=(total.interval()*1000)/TICK_FREQ;
unsigned int writeTime=duration_cast<milliseconds>(d).count();
unsigned int writeSpeed=static_cast<unsigned int>(1024000.0/writeTime);
iprintf("Total write time = %dms (%dKB/s)\n",writeTime,writeSpeed);
iprintf("Max filesystem latency = %dms\n",(max*1000)/TICK_FREQ);
iprintf("Max filesystem latency = %dms\n",max);
//Read benchmark
max=0;
total.clear();
if((f=fopen(FILENAME,"r"))==NULL)
{
iprintf("Filesystem read benchmark not made. Can't open file\n");
......@@ -5025,19 +5164,18 @@ static void benchmark_3()
return;
}
setbuf(f,NULL);
total.start();
total=system_clock::now();
for(i=0;i<1024;i++)
{
memset(buf,0,BUFSIZE);
part.start();
auto part=system_clock::now();
if(fread(buf,1,BUFSIZE,f)!=BUFSIZE)
{
iprintf("Read error 1\n");
break;
}
part.stop();
if(part.interval()>max) max=part.interval();
part.clear();
auto d=system_clock::now()-part;
max=std::max(max,static_cast<int>(duration_cast<milliseconds>(d).count()));
for(unsigned j=0;j<BUFSIZE;j++) if(buf[j]!='0')
{
iprintf("Read error 2\n");
......@@ -5045,13 +5183,13 @@ static void benchmark_3()
}
}
quit:
total.stop();
d=system_clock::now()-total;
if(fclose(f)!=0) iprintf("Error in fclose 2\n");
iprintf("Filesystem read test\n");
unsigned int readTime=(total.interval()*1000)/TICK_FREQ;
unsigned int readTime=duration_cast<milliseconds>(d).count();
unsigned int readSpeed=static_cast<unsigned int>(1024000.0/readTime);
iprintf("Total read time = %dms (%dKB/s)\n",readTime,readSpeed);
iprintf("Max filesystem latency = %dms\n",(max*1000)/TICK_FREQ);
iprintf("Max filesystem latency = %dms\n",max);
delete[] buf;
}
......