├── tests ├── mbedtls │ ├── digicert.der │ ├── Makefile │ └── mbedtls_nucleo_config.h ├── udp_client │ ├── host │ │ └── Makefile │ ├── Makefile │ └── udp_client.c ├── socket_client │ ├── host │ │ ├── Makefile │ │ └── server.c │ ├── Makefile │ └── client.c ├── socket_server │ ├── host │ │ └── Makefile │ ├── Makefile │ └── server.c ├── socket_errors │ ├── host │ │ └── Makefile │ ├── Makefile │ └── socket_errors.c ├── test.mk ├── udp_server │ ├── host │ │ └── Makefile │ ├── Makefile │ └── udp_server.c ├── socket_nonblock │ ├── host │ │ └── Makefile │ ├── Makefile │ └── socket_nonblock.c ├── dns │ ├── host │ │ └── Makefile │ ├── Makefile │ └── dns.c ├── gethostbyname │ ├── host │ │ └── Makefile │ ├── Makefile │ └── gethostbyname_test.c ├── w5100_dhcp │ ├── w5100_dhcp_test.c │ └── Makefile ├── blink │ ├── Makefile │ └── blink.c ├── spi │ ├── Makefile │ └── spi.c ├── usart │ ├── Makefile │ └── usart.c ├── stdio │ ├── stdio_test.c │ └── Makefile ├── sd_spi │ ├── Makefile │ └── sd_spi_test.c ├── signal │ ├── Makefile │ └── signal_test.c ├── diskio │ ├── Makefile │ └── diskio_test.c ├── fatfs_ro │ ├── Makefile │ └── fatfs_ro_test.c ├── fatfs_rw │ ├── Makefile │ └── fatfs_rw_test.c ├── time │ ├── Makefile │ └── time_test.c ├── fatfs_dirent │ └── Makefile ├── fatfs_stdio │ ├── Makefile │ └── fatfs_stdio.c ├── fatfs_unistd │ ├── Makefile │ └── fatfs_unistd.c ├── sntp │ ├── Makefile │ └── sntp_test.c ├── timers │ ├── Makefile │ └── timers_test.c ├── rfc868_time │ ├── Makefile │ └── rfc868_time_test.c ├── socket_poll │ ├── Makefile │ └── socket_poll.c ├── socket_select │ └── Makefile ├── dhcp_client │ ├── Makefile │ └── dhcp_allocate.c ├── timesync │ ├── Makefile │ └── timesync_test.c └── wolfssl │ ├── Makefile │ └── wolfssl.c ├── .gitmodules ├── README.md ├── scripts ├── attach.gdb ├── gdb-pipe.cfg ├── posix.mk ├── stm32f103rb.ld ├── stm32f411re.ld ├── libopencm3.stm32f103rb.mk ├── libopencm3.stm32f411re.mk └── libopencm3.target.mk ├── .gitignore ├── src ├── getpid.c ├── nanosleep.c ├── raise.c ├── sleep.c ├── gettimeofday.c ├── kill.c ├── clock_nanosleep_poll.c ├── faults.c ├── file.c ├── fcntl.c ├── gethostbyname.c ├── w5100_dhcp.c ├── timespec.c ├── timesync.c ├── clock_gettime_dummy.c ├── poll.c └── stdio_usart.c └── include ├── sigqueue_info.h ├── sys ├── uio.h ├── select.h └── socket.h ├── sd_spi.h ├── poll.h ├── file.h ├── arpa └── inet.h ├── limits.h ├── timespec.h ├── w5100_dhcp.h ├── dirent.h ├── fatfs.h ├── timesync.h ├── netinet └── in.h ├── dhcp_client.h ├── netdb.h └── signal.h /tests/mbedtls/digicert.der: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/balau/nucleo_tests/HEAD/tests/mbedtls/digicert.der -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "libopencm3"] 2 | path = libopencm3 3 | url = https://github.com/libopencm3/libopencm3 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nucleo_tests 2 | Tests to program STM32 Nucleo in C with GCC ARM embedded toolchain and libopencm3 3 | -------------------------------------------------------------------------------- /scripts/attach.gdb: -------------------------------------------------------------------------------- 1 | target remote | openocd -f interface/stlink-v2-1.cfg -f target/stm32f1x_stlink.cfg -f gdb-pipe.cfg 2 | monitor halt 3 | monitor gdb_sync 4 | stepi 5 | 6 | -------------------------------------------------------------------------------- /scripts/gdb-pipe.cfg: -------------------------------------------------------------------------------- 1 | gdb_port pipe 2 | log_output openocd.log 3 | 4 | $_TARGETNAME configure -event gdb-detach { 5 | echo "Debugger detaching: resuming execution." 6 | resume 7 | } 8 | 9 | -------------------------------------------------------------------------------- /tests/udp_client/host/Makefile: -------------------------------------------------------------------------------- 1 | 2 | .PHONY: all run clean 3 | all: server 4 | @echo \"make run\" to start server. 5 | 6 | run: server 7 | ./$< 8 | 9 | server.c: ../../udp_server/udp_server.c 10 | cp $< $@ 11 | 12 | clean: 13 | rm server server.c 14 | 15 | -------------------------------------------------------------------------------- /tests/socket_client/host/Makefile: -------------------------------------------------------------------------------- 1 | 2 | .PHONY: all run clean 3 | all: server 4 | @echo \"make run\" to start server. 5 | 6 | run: server 7 | ./$< 8 | 9 | server.c: ../../socket_server/server.c 10 | cp $< $@ 11 | 12 | clean: 13 | rm server server.c 14 | 15 | -------------------------------------------------------------------------------- /scripts/posix.mk: -------------------------------------------------------------------------------- 1 | 2 | CPPFLAGS += -D_POSIX_SOURCE=200809L 3 | CPPFLAGS += -D_POSIX_C_SOURCE=200809L 4 | CPPFLAGS += -D_POSIX_TIMERS=200809L 5 | CPPFLAGS += -D_POSIX_MONOTONIC_CLOCK=200809L 6 | CPPFLAGS += -D_POSIX_CLOCK_SELECTION=200809L 7 | CPPFLAGS += -D_POSIX_REALTIME_SIGNALS=200809L 8 | 9 | -------------------------------------------------------------------------------- /tests/socket_server/host/Makefile: -------------------------------------------------------------------------------- 1 | 2 | CPPFLAGS += -DSERVER_IP_ADDR="\"192.168.1.99\"" 3 | 4 | .PHONY: all run clean 5 | all: client 6 | @echo \"make run\" to start client. 7 | 8 | run: client 9 | ./$< 10 | 11 | client.c: ../../socket_client/client.c 12 | cp $< $@ 13 | 14 | clean: 15 | rm client client.c 16 | 17 | -------------------------------------------------------------------------------- /tests/socket_errors/host/Makefile: -------------------------------------------------------------------------------- 1 | 2 | #CPPFLAGS += -DSERVER_IP_ADDR="\"192.168.1.99\"" 3 | 4 | .PHONY: all run clean 5 | all: socket_errors 6 | @echo \"make run\" to start client. 7 | 8 | run: socket_errors 9 | ./$< 10 | 11 | socket_errors.c: ../socket_errors.c 12 | cp $< $@ 13 | 14 | clean: 15 | rm socket_errors socket_errors.c 16 | 17 | -------------------------------------------------------------------------------- /tests/test.mk: -------------------------------------------------------------------------------- 1 | 2 | ROOT_DIR = ../../ 3 | OPENCM3_DIR = $(ROOT_DIR)/libopencm3 4 | 5 | CPPFLAGS += -I$(ROOT_DIR)/include 6 | include $(ROOT_DIR)/scripts/posix.mk 7 | 8 | #include $(ROOT_DIR)/scripts/libopencm3.target.mk 9 | include $(ROOT_DIR)/scripts/libopencm3.stm32f103rb.mk 10 | #include $(ROOT_DIR)/scripts/libopencm3.stm32f411re.mk 11 | 12 | 13 | -------------------------------------------------------------------------------- /tests/udp_server/host/Makefile: -------------------------------------------------------------------------------- 1 | 2 | CPPFLAGS += -DSERVER_IP_ADDR="\"192.168.1.99\"" 3 | #CPPFLAGS += -DSERVER_IP_ADDR="\"192.168.1.173\"" 4 | 5 | .PHONY: all run clean 6 | all: client 7 | @echo \"make run\" to start client. 8 | 9 | run: client 10 | ./$< 11 | 12 | client.c: ../../udp_client/udp_client.c 13 | cp $< $@ 14 | 15 | clean: 16 | rm client client.c 17 | 18 | -------------------------------------------------------------------------------- /tests/socket_nonblock/host/Makefile: -------------------------------------------------------------------------------- 1 | 2 | all: 3 | 4 | EXE = ./socket_nonblock 5 | 6 | $(EXE): ../socket_nonblock.c 7 | 8 | CFLAGS += -g -Wall -Wextra 9 | 10 | .PHONY: all run clean 11 | all: $(EXE) 12 | @echo \"make run\" to start. 13 | 14 | run: $(EXE) 15 | $(EXE) 16 | 17 | $(EXE): 18 | $(LINK.c) $^ $(LOADLIBES) $(LDLIBS) -o $@ 19 | 20 | clean: 21 | rm -f $(EXE) 22 | 23 | -------------------------------------------------------------------------------- /tests/dns/host/Makefile: -------------------------------------------------------------------------------- 1 | 2 | all: 3 | 4 | EXE = ./dns 5 | 6 | $(EXE): ../dns.c 7 | $(EXE): ../../../src/getaddrinfo.c 8 | 9 | CFLAGS += -g -Wall -Wextra 10 | 11 | .PHONY: all run clean 12 | all: $(EXE) 13 | @echo \"make run\" to start. 14 | 15 | run: $(EXE) 16 | $(EXE) 17 | 18 | $(EXE): 19 | $(LINK.c) $^ $(LOADLIBES) $(LDLIBS) -o $@ 20 | 21 | clean: 22 | rm -f $(EXE) 23 | 24 | -------------------------------------------------------------------------------- /tests/gethostbyname/host/Makefile: -------------------------------------------------------------------------------- 1 | 2 | all: 3 | 4 | EXE = ./gethostbyname_test 5 | 6 | $(EXE): ../gethostbyname_test.c 7 | $(EXE): ../../../src/getaddrinfo.c 8 | $(EXE): ../../../src/gethostbyname.c 9 | 10 | CFLAGS += -g -Wall -Wextra 11 | 12 | .PHONY: all run clean 13 | all: $(EXE) 14 | @echo \"make run\" to start. 15 | 16 | run: $(EXE) 17 | $(EXE) 18 | 19 | $(EXE): 20 | $(LINK.c) $^ $(LOADLIBES) $(LDLIBS) -o $@ 21 | 22 | clean: 23 | rm -f $(EXE) 24 | 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Libraries 12 | *.lib 13 | *.a 14 | *.la 15 | *.lo 16 | 17 | # Shared objects (inc. Windows DLLs) 18 | *.dll 19 | *.so 20 | *.so.* 21 | *.dylib 22 | 23 | # Executables 24 | *.exe 25 | *.out 26 | *.app 27 | *.i*86 28 | *.x86_64 29 | *.hex 30 | 31 | # Debug files 32 | *.dSYM/ 33 | 34 | # Build artifacts 35 | *.d 36 | *.map 37 | *.lst 38 | *.dis 39 | 40 | # Log files 41 | *.log 42 | 43 | -------------------------------------------------------------------------------- /tests/w5100_dhcp/w5100_dhcp_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "w5100.h" 6 | #include "w5100_dhcp.h" 7 | 8 | static 9 | void print_ipaddr(const char *name, in_addr_t addr) 10 | { 11 | struct in_addr in; 12 | 13 | in.s_addr = addr; 14 | printf("%s: %s\n", name, inet_ntoa(in)); 15 | } 16 | 17 | static 18 | time_t next; 19 | 20 | static 21 | int loop(void) 22 | { 23 | in_addr_t ip; 24 | 25 | next = w5100_dhcp_bind(); 26 | w5100_read_regx(W5100_SIPR, &ip); 27 | print_ipaddr("IP", ip); 28 | print_ipaddr("DNS", w5100_getdns()); 29 | printf("next: %d\n", (int)next); 30 | sleep(5); 31 | 32 | return 0; 33 | } 34 | 35 | int main(void) 36 | { 37 | while(1) 38 | { 39 | loop(); 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /tests/blink/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = blink 21 | 22 | include ../test.mk 23 | 24 | -------------------------------------------------------------------------------- /tests/spi/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = spi 21 | 22 | include ../test.mk 23 | 24 | -------------------------------------------------------------------------------- /tests/usart/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = usart 21 | 22 | include ../test.mk 23 | 24 | -------------------------------------------------------------------------------- /src/getpid.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | 21 | pid_t getpid(void) 22 | { 23 | return 1; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /tests/stdio/stdio_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | 21 | int main(void) 22 | { 23 | puts("Hello World!"); 24 | while(1); 25 | } 26 | 27 | 28 | -------------------------------------------------------------------------------- /tests/stdio/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = stdio_test 21 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 22 | OBJS += $(ROOT_DIR)/src/syscalls.o 23 | OBJS += $(ROOT_DIR)/src/file.o 24 | 25 | include ../test.mk 26 | 27 | -------------------------------------------------------------------------------- /src/nanosleep.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | 21 | int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) 22 | { 23 | return clock_nanosleep(CLOCK_MONOTONIC, 0, rqtp, rmtp); 24 | } 25 | 26 | -------------------------------------------------------------------------------- /include/sigqueue_info.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #ifndef SIGQUEUE_INFO_H 20 | #define SIGQUEUE_INFO_H 21 | 22 | #include 23 | 24 | extern 25 | int sigqueue_info (const siginfo_t *info); 26 | 27 | #endif /* SIGQUEUE_INFO_H */ 28 | 29 | -------------------------------------------------------------------------------- /tests/sd_spi/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = sd_spi_test 21 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 22 | OBJS += $(ROOT_DIR)/src/syscalls.o 23 | OBJS += $(ROOT_DIR)/src/file.o 24 | OBJS += $(ROOT_DIR)/src/sd_spi.o 25 | 26 | include ../test.mk 27 | 28 | -------------------------------------------------------------------------------- /src/raise.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include 21 | 22 | int raise (int sig) 23 | { 24 | int ret; 25 | pid_t pid; 26 | 27 | pid = getpid(); 28 | 29 | ret = kill(pid, sig); 30 | 31 | return ret; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /tests/signal/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = signal_test 21 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 22 | OBJS += $(ROOT_DIR)/src/syscalls.o 23 | OBJS += $(ROOT_DIR)/src/file.o 24 | OBJS += $(ROOT_DIR)/src/signal.o 25 | OBJS += $(ROOT_DIR)/src/raise.o 26 | OBJS += $(ROOT_DIR)/src/kill.o 27 | 28 | include ../test.mk 29 | 30 | -------------------------------------------------------------------------------- /tests/diskio/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = diskio_test 21 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 22 | OBJS += $(ROOT_DIR)/src/syscalls.o 23 | OBJS += $(ROOT_DIR)/src/file.o 24 | OBJS += $(ROOT_DIR)/src/sd_spi_diskio.o 25 | OBJS += $(ROOT_DIR)/src/sd_spi.o 26 | 27 | CPPFLAGS += -I$(ROOT_DIR)/ff11a/src 28 | 29 | include ../test.mk 30 | 31 | -------------------------------------------------------------------------------- /include/sys/uio.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #ifndef SYS_UIO_H 20 | #define SYS_UIO_H 21 | 22 | #include 23 | 24 | struct iovec { 25 | void *iov_base; 26 | size_t iov_len; 27 | }; 28 | 29 | ssize_t readv(int, const struct iovec *, int); 30 | 31 | ssize_t writev(int, const struct iovec *, int); 32 | 33 | #endif /* SYS_UIO_H */ 34 | 35 | -------------------------------------------------------------------------------- /scripts/stm32f103rb.ld: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the libopencm3 project. 3 | * 4 | * Copyright (C) 2009 Uwe Hermann 5 | * 6 | * This library is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with this library. If not, see . 18 | */ 19 | 20 | /* Linker script for STM32F103RB, 128K flash, 8K RAM. */ 21 | 22 | /* Define memory regions. */ 23 | MEMORY 24 | { 25 | rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K 26 | ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K 27 | } 28 | 29 | /* Include the common ld script. */ 30 | INCLUDE libopencm3_stm32f1.ld 31 | 32 | -------------------------------------------------------------------------------- /scripts/stm32f411re.ld: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the libopencm3 project. 3 | * 4 | * Copyright (C) 2009 Uwe Hermann 5 | * 6 | * This library is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with this library. If not, see . 18 | */ 19 | 20 | /* Linker script for STM32F411RE, 512K flash, 128K RAM. */ 21 | 22 | /* Define memory regions. */ 23 | MEMORY 24 | { 25 | rom (rx) : ORIGIN = 0x08000000, LENGTH = 512K 26 | ram (rwx) : ORIGIN = 0x20000000, LENGTH = 128K 27 | } 28 | 29 | /* Include the common ld script. */ 30 | INCLUDE libopencm3_stm32f4.ld 31 | 32 | -------------------------------------------------------------------------------- /tests/fatfs_ro/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = fatfs_ro_test 21 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 22 | OBJS += $(ROOT_DIR)/src/syscalls.o 23 | OBJS += $(ROOT_DIR)/src/file.o 24 | OBJS += $(ROOT_DIR)/src/sd_spi_diskio.o 25 | OBJS += $(ROOT_DIR)/src/sd_spi.o 26 | OBJS += $(ROOT_DIR)/ff11a/src/ff.o 27 | 28 | CPPFLAGS += -I$(ROOT_DIR)/ff11a/src 29 | 30 | include ../test.mk 31 | 32 | -------------------------------------------------------------------------------- /tests/fatfs_rw/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = fatfs_rw_test 21 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 22 | OBJS += $(ROOT_DIR)/src/syscalls.o 23 | OBJS += $(ROOT_DIR)/src/file.o 24 | OBJS += $(ROOT_DIR)/src/sd_spi_diskio.o 25 | OBJS += $(ROOT_DIR)/src/sd_spi.o 26 | OBJS += $(ROOT_DIR)/ff11a/src/ff.o 27 | 28 | CPPFLAGS += -I$(ROOT_DIR)/ff11a/src 29 | 30 | include ../test.mk 31 | 32 | -------------------------------------------------------------------------------- /tests/time/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = time_test 21 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 22 | OBJS += $(ROOT_DIR)/src/syscalls.o 23 | OBJS += $(ROOT_DIR)/src/file.o 24 | OBJS += $(ROOT_DIR)/src/nanosleep.o 25 | OBJS += $(ROOT_DIR)/src/clock_nanosleep_poll.o 26 | OBJS += $(ROOT_DIR)/src/clock_gettime_systick.o 27 | OBJS += $(ROOT_DIR)/src/timespec.o 28 | 29 | include ../test.mk 30 | 31 | -------------------------------------------------------------------------------- /tests/fatfs_dirent/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = fatfs_dirent 21 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 22 | OBJS += $(ROOT_DIR)/src/syscalls.o 23 | OBJS += $(ROOT_DIR)/src/file.o 24 | OBJS += $(ROOT_DIR)/src/sd_spi_diskio.o 25 | OBJS += $(ROOT_DIR)/src/sd_spi.o 26 | OBJS += $(ROOT_DIR)/src/fatfs.o 27 | OBJS += $(ROOT_DIR)/ff11a/src/ff.o 28 | 29 | CPPFLAGS += -I$(ROOT_DIR)/ff11a/src 30 | 31 | include ../test.mk 32 | 33 | -------------------------------------------------------------------------------- /tests/fatfs_stdio/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = fatfs_stdio 21 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 22 | OBJS += $(ROOT_DIR)/src/syscalls.o 23 | OBJS += $(ROOT_DIR)/src/file.o 24 | OBJS += $(ROOT_DIR)/src/sd_spi_diskio.o 25 | OBJS += $(ROOT_DIR)/src/sd_spi.o 26 | OBJS += $(ROOT_DIR)/src/fatfs.o 27 | OBJS += $(ROOT_DIR)/ff11a/src/ff.o 28 | 29 | CPPFLAGS += -I$(ROOT_DIR)/ff11a/src 30 | 31 | include ../test.mk 32 | 33 | -------------------------------------------------------------------------------- /tests/fatfs_unistd/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = fatfs_unistd 21 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 22 | OBJS += $(ROOT_DIR)/src/syscalls.o 23 | OBJS += $(ROOT_DIR)/src/file.o 24 | OBJS += $(ROOT_DIR)/src/sd_spi_diskio.o 25 | OBJS += $(ROOT_DIR)/src/sd_spi.o 26 | OBJS += $(ROOT_DIR)/src/fatfs.o 27 | OBJS += $(ROOT_DIR)/ff11a/src/ff.o 28 | 29 | CPPFLAGS += -I$(ROOT_DIR)/ff11a/src 30 | 31 | include ../test.mk 32 | 33 | -------------------------------------------------------------------------------- /tests/time/time_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include 21 | 22 | int main(void) 23 | { 24 | struct timespec t; 25 | 26 | while(1) 27 | { 28 | clock_gettime(CLOCK_MONOTONIC, &t); 29 | printf("%lds %ldns\n", (long)t.tv_sec, (long)t.tv_nsec); 30 | 31 | t.tv_sec = 1; 32 | t.tv_nsec = 0; 33 | nanosleep(&t, NULL); 34 | } 35 | } 36 | 37 | 38 | -------------------------------------------------------------------------------- /tests/socket_client/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = client 21 | OBJS += $(ROOT_DIR)/src/w5100_socket.o 22 | OBJS += $(ROOT_DIR)/src/w5100_spi.o 23 | OBJS += $(ROOT_DIR)/src/inet.o 24 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 25 | OBJS += $(ROOT_DIR)/src/syscalls.o 26 | OBJS += $(ROOT_DIR)/src/file.o 27 | OBJS += $(ROOT_DIR)/src/timespec.o 28 | OBJS += $(ROOT_DIR)/src/clock_gettime_systick.o 29 | LDLIBS_SYS = 30 | 31 | include ../test.mk 32 | 33 | -------------------------------------------------------------------------------- /tests/socket_server/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = server 21 | OBJS += $(ROOT_DIR)/src/w5100_socket.o 22 | OBJS += $(ROOT_DIR)/src/w5100_spi.o 23 | OBJS += $(ROOT_DIR)/src/inet.o 24 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 25 | OBJS += $(ROOT_DIR)/src/syscalls.o 26 | OBJS += $(ROOT_DIR)/src/file.o 27 | OBJS += $(ROOT_DIR)/src/timespec.o 28 | OBJS += $(ROOT_DIR)/src/clock_gettime_systick.o 29 | LDLIBS_SYS = 30 | 31 | include ../test.mk 32 | 33 | -------------------------------------------------------------------------------- /tests/udp_client/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = udp_client 21 | OBJS += $(ROOT_DIR)/src/w5100_socket.o 22 | OBJS += $(ROOT_DIR)/src/w5100_spi.o 23 | OBJS += $(ROOT_DIR)/src/inet.o 24 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 25 | OBJS += $(ROOT_DIR)/src/syscalls.o 26 | OBJS += $(ROOT_DIR)/src/file.o 27 | OBJS += $(ROOT_DIR)/src/timespec.o 28 | OBJS += $(ROOT_DIR)/src/clock_gettime_systick.o 29 | LDLIBS_SYS = 30 | 31 | include ../test.mk 32 | 33 | -------------------------------------------------------------------------------- /tests/udp_server/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = udp_server 21 | OBJS += $(ROOT_DIR)/src/w5100_socket.o 22 | OBJS += $(ROOT_DIR)/src/w5100_spi.o 23 | OBJS += $(ROOT_DIR)/src/inet.o 24 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 25 | OBJS += $(ROOT_DIR)/src/syscalls.o 26 | OBJS += $(ROOT_DIR)/src/file.o 27 | OBJS += $(ROOT_DIR)/src/timespec.o 28 | OBJS += $(ROOT_DIR)/src/clock_gettime_systick.o 29 | LDLIBS_SYS = 30 | 31 | include ../test.mk 32 | 33 | -------------------------------------------------------------------------------- /tests/socket_errors/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = socket_errors 21 | OBJS += $(ROOT_DIR)/src/w5100_socket.o 22 | OBJS += $(ROOT_DIR)/src/w5100_spi.o 23 | OBJS += $(ROOT_DIR)/src/inet.o 24 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 25 | OBJS += $(ROOT_DIR)/src/syscalls.o 26 | OBJS += $(ROOT_DIR)/src/file.o 27 | OBJS += $(ROOT_DIR)/src/timespec.o 28 | OBJS += $(ROOT_DIR)/src/clock_gettime_systick.o 29 | LDLIBS_SYS = 30 | 31 | include ../test.mk 32 | 33 | -------------------------------------------------------------------------------- /tests/sntp/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2016 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = sntp_test 21 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 22 | OBJS += $(ROOT_DIR)/src/syscalls.o 23 | OBJS += $(ROOT_DIR)/src/file.o 24 | OBJS += $(ROOT_DIR)/src/clock_gettime_systick.o 25 | OBJS += $(ROOT_DIR)/src/timespec.o 26 | OBJS += $(ROOT_DIR)/src/sntp.o 27 | OBJS += $(ROOT_DIR)/src/w5100_socket.o 28 | OBJS += $(ROOT_DIR)/src/w5100_spi.o 29 | OBJS += $(ROOT_DIR)/src/inet.o 30 | 31 | include ../test.mk 32 | 33 | -------------------------------------------------------------------------------- /src/sleep.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include 21 | #include 22 | 23 | unsigned sleep(unsigned seconds) 24 | { 25 | struct timespec rqt; 26 | struct timespec rmt; 27 | 28 | if (seconds > INT_MAX) 29 | { 30 | seconds = INT_MAX; 31 | } 32 | rqt.tv_sec = seconds; 33 | rqt.tv_nsec = 0; 34 | 35 | (void)nanosleep(&rqt, &rmt); 36 | 37 | return (unsigned)rmt.tv_sec; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /tests/timers/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = timers_test 21 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 22 | OBJS += $(ROOT_DIR)/src/syscalls.o 23 | OBJS += $(ROOT_DIR)/src/file.o 24 | OBJS += $(ROOT_DIR)/src/nanosleep.o 25 | OBJS += $(ROOT_DIR)/src/clock_nanosleep_poll.o 26 | OBJS += $(ROOT_DIR)/src/clock_gettime_systick.o 27 | OBJS += $(ROOT_DIR)/src/timespec.o 28 | OBJS += $(ROOT_DIR)/src/timers.o 29 | OBJS += $(ROOT_DIR)/src/signal.o 30 | 31 | include ../test.mk 32 | 33 | -------------------------------------------------------------------------------- /tests/rfc868_time/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = rfc868_time_test 21 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 22 | OBJS += $(ROOT_DIR)/src/syscalls.o 23 | OBJS += $(ROOT_DIR)/src/file.o 24 | OBJS += $(ROOT_DIR)/src/clock_gettime_systick.o 25 | OBJS += $(ROOT_DIR)/src/timespec.o 26 | OBJS += $(ROOT_DIR)/src/rfc868_time.o 27 | OBJS += $(ROOT_DIR)/src/w5100_socket.o 28 | OBJS += $(ROOT_DIR)/src/w5100_spi.o 29 | OBJS += $(ROOT_DIR)/src/inet.o 30 | 31 | include ../test.mk 32 | 33 | -------------------------------------------------------------------------------- /scripts/libopencm3.stm32f103rb.mk: -------------------------------------------------------------------------------- 1 | ## 2 | ## This file is part of the libopencm3 project. 3 | ## 4 | ## Copyright (C) 2009 Uwe Hermann 5 | ## Copyright (C) 2010 Piotr Esden-Tempski 6 | ## 7 | ## This library is free software: you can redistribute it and/or modify 8 | ## it under the terms of the GNU Lesser General Public License as published by 9 | ## the Free Software Foundation, either version 3 of the License, or 10 | ## (at your option) any later version. 11 | ## 12 | ## This library is distributed in the hope that it will be useful, 13 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | ## GNU Lesser General Public License for more details. 16 | ## 17 | ## You should have received a copy of the GNU Lesser General Public License 18 | ## along with this library. If not, see . 19 | ## 20 | 21 | LIBNAME = opencm3_stm32f1 22 | DEFS = -DSTM32F1 23 | FP_FLAGS = -msoft-float 24 | ARCH_FLAGS = -mthumb -mcpu=cortex-m3 $(FP_FLAGS) -mfix-cortex-m3-ldrd 25 | LDSCRIPT = $(ROOT_DIR)/scripts/stm32f103rb.ld 26 | OOCD_BOARD = st_nucleo_f103rb 27 | OOCD = openocd 28 | OOCD_INTERFACE = stlink-v2-1 29 | 30 | include $(ROOT_DIR)/scripts/libopencm3.rules.mk 31 | 32 | -------------------------------------------------------------------------------- /scripts/libopencm3.stm32f411re.mk: -------------------------------------------------------------------------------- 1 | ## 2 | ## This file is part of the libopencm3 project. 3 | ## 4 | ## Copyright (C) 2009 Uwe Hermann 5 | ## Copyright (C) 2010 Piotr Esden-Tempski 6 | ## 7 | ## This library is free software: you can redistribute it and/or modify 8 | ## it under the terms of the GNU Lesser General Public License as published by 9 | ## the Free Software Foundation, either version 3 of the License, or 10 | ## (at your option) any later version. 11 | ## 12 | ## This library is distributed in the hope that it will be useful, 13 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | ## GNU Lesser General Public License for more details. 16 | ## 17 | ## You should have received a copy of the GNU Lesser General Public License 18 | ## along with this library. If not, see . 19 | ## 20 | 21 | LIBNAME = opencm3_stm32f4 22 | DEFS = -DSTM32F4 23 | FP_FLAGS = -mfloat-abi=hard -mfpu=fpv4-sp-d16 24 | ARCH_FLAGS = -mthumb -mcpu=cortex-m4 $(FP_FLAGS) 25 | LDSCRIPT = $(ROOT_DIR)/scripts/stm32f411re.ld 26 | OOCD_BOARD = st_nucleo_f4 27 | OOCD = openocd 28 | OOCD_INTERFACE = stlink-v2-1 29 | 30 | include $(ROOT_DIR)/scripts/libopencm3.rules.mk 31 | 32 | -------------------------------------------------------------------------------- /tests/dns/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = dns 21 | OBJS += $(ROOT_DIR)/src/w5100_socket.o 22 | OBJS += $(ROOT_DIR)/src/w5100_spi.o 23 | OBJS += $(ROOT_DIR)/src/inet.o 24 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 25 | OBJS += $(ROOT_DIR)/src/syscalls.o 26 | OBJS += $(ROOT_DIR)/src/file.o 27 | OBJS += $(ROOT_DIR)/src/timespec.o 28 | OBJS += $(ROOT_DIR)/src/clock_gettime_systick.o 29 | OBJS += $(ROOT_DIR)/src/getaddrinfo.o 30 | 31 | LDLIBS_SYS = 32 | 33 | include ../test.mk 34 | 35 | -------------------------------------------------------------------------------- /tests/socket_poll/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = socket_poll 21 | OBJS += $(ROOT_DIR)/src/w5100_socket.o 22 | OBJS += $(ROOT_DIR)/src/w5100_spi.o 23 | OBJS += $(ROOT_DIR)/src/inet.o 24 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 25 | OBJS += $(ROOT_DIR)/src/syscalls.o 26 | OBJS += $(ROOT_DIR)/src/file.o 27 | OBJS += $(ROOT_DIR)/src/timespec.o 28 | OBJS += $(ROOT_DIR)/src/clock_gettime_systick.o 29 | OBJS += $(ROOT_DIR)/src/poll.o 30 | LDLIBS_SYS = 31 | 32 | include ../test.mk 33 | 34 | -------------------------------------------------------------------------------- /tests/socket_nonblock/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = socket_nonblock 21 | OBJS += $(ROOT_DIR)/src/w5100_socket.o 22 | OBJS += $(ROOT_DIR)/src/w5100_spi.o 23 | OBJS += $(ROOT_DIR)/src/inet.o 24 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 25 | OBJS += $(ROOT_DIR)/src/syscalls.o 26 | OBJS += $(ROOT_DIR)/src/file.o 27 | OBJS += $(ROOT_DIR)/src/timespec.o 28 | OBJS += $(ROOT_DIR)/src/clock_gettime_systick.o 29 | OBJS += $(ROOT_DIR)/src/fcntl.o 30 | LDLIBS_SYS = 31 | 32 | include ../test.mk 33 | 34 | -------------------------------------------------------------------------------- /tests/socket_select/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = socket_select 21 | OBJS += $(ROOT_DIR)/src/w5100_socket.o 22 | OBJS += $(ROOT_DIR)/src/w5100_spi.o 23 | OBJS += $(ROOT_DIR)/src/inet.o 24 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 25 | OBJS += $(ROOT_DIR)/src/syscalls.o 26 | OBJS += $(ROOT_DIR)/src/file.o 27 | OBJS += $(ROOT_DIR)/src/timespec.o 28 | OBJS += $(ROOT_DIR)/src/clock_gettime_systick.o 29 | OBJS += $(ROOT_DIR)/src/poll.o 30 | OBJS += $(ROOT_DIR)/src/select.o 31 | LDLIBS_SYS = 32 | 33 | include ../test.mk 34 | 35 | -------------------------------------------------------------------------------- /tests/dhcp_client/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = dhcp_allocate 21 | OBJS += $(ROOT_DIR)/src/dhcp_client.o 22 | OBJS += $(ROOT_DIR)/src/w5100_socket.o 23 | OBJS += $(ROOT_DIR)/src/w5100_spi.o 24 | OBJS += $(ROOT_DIR)/src/inet.o 25 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 26 | OBJS += $(ROOT_DIR)/src/syscalls.o 27 | OBJS += $(ROOT_DIR)/src/file.o 28 | OBJS += $(ROOT_DIR)/src/timespec.o 29 | OBJS += $(ROOT_DIR)/src/clock_gettime_systick.o 30 | LDLIBS_SYS = 31 | 32 | CPPFLAGS += -DW5100_NO_STATIC_IP 33 | 34 | include ../test.mk 35 | 36 | -------------------------------------------------------------------------------- /tests/dhcp_client/dhcp_allocate.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "w5100.h" 9 | #include "dhcp_client.h" 10 | 11 | static 12 | void print_ipaddr(const char *name, in_addr_t addr) 13 | { 14 | struct in_addr in; 15 | 16 | in.s_addr = addr; 17 | printf("%s: %s\n", name, inet_ntoa(in)); 18 | } 19 | 20 | static 21 | int loop(void) 22 | { 23 | struct dhcp_binding binding; 24 | uint8_t mac_addr[6]; 25 | time_t next; 26 | 27 | printf("Press any key to continue..."); 28 | getchar(); 29 | printf("\n"); 30 | 31 | w5100_read_regx(W5100_SHAR, mac_addr); 32 | dhcp_init(mac_addr, &binding); 33 | 34 | next = dhcp_bind(&binding); 35 | print_ipaddr("new client address", binding.client); 36 | print_ipaddr("gateway", binding.gateway); 37 | print_ipaddr("subnet", binding.subnet); 38 | print_ipaddr("DNS", binding.dns_server); 39 | printf("lease T1: %d seconds\n", (int)binding.lease_t1.tv_sec); 40 | printf("lease T2: %d seconds\n", (int)binding.lease_t2.tv_sec); 41 | printf("next call should be in %d seconds\n", (int)next); 42 | 43 | return 0; 44 | } 45 | 46 | int main(void) 47 | { 48 | while(1) 49 | { 50 | loop(); 51 | } 52 | } 53 | 54 | -------------------------------------------------------------------------------- /tests/gethostbyname/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = gethostbyname_test 21 | OBJS += $(ROOT_DIR)/src/w5100_socket.o 22 | OBJS += $(ROOT_DIR)/src/w5100_spi.o 23 | OBJS += $(ROOT_DIR)/src/inet.o 24 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 25 | OBJS += $(ROOT_DIR)/src/syscalls.o 26 | OBJS += $(ROOT_DIR)/src/file.o 27 | OBJS += $(ROOT_DIR)/src/timespec.o 28 | OBJS += $(ROOT_DIR)/src/clock_gettime_systick.o 29 | OBJS += $(ROOT_DIR)/src/getaddrinfo.o 30 | OBJS += $(ROOT_DIR)/src/gethostbyname.o 31 | 32 | LDLIBS_SYS = 33 | 34 | include ../test.mk 35 | 36 | -------------------------------------------------------------------------------- /src/gettimeofday.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include 21 | #include "timespec.h" 22 | 23 | /* Used by newlib as system call */ 24 | extern 25 | int _gettimeofday(struct timeval *tp, void *tzp); 26 | 27 | int _gettimeofday(struct timeval *tp, void *tzp) 28 | { 29 | int ret; 30 | 31 | struct timespec ts; 32 | 33 | (void)tzp; /* ignore */ 34 | 35 | ret = clock_gettime(CLOCK_REALTIME, &ts); 36 | 37 | timespec_to_timeval(&ts, tp); 38 | 39 | (void)ret; /* ignore and return 0 anyway */ 40 | return 0; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /tests/gethostbyname/gethostbyname_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | C ECHO client example using sockets 3 | http://www.binarytides.com/server-client-example-c-sockets-linux/ 4 | */ 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | static 13 | int loop(void) 14 | { 15 | struct hostent *h; 16 | char *name; 17 | 18 | printf("Press any key to continue..."); 19 | getchar(); 20 | printf("\n"); 21 | 22 | name = "www.facebook.com"; 23 | 24 | h = gethostbyname(name); 25 | if (h == NULL) 26 | { 27 | fprintf(stderr, "error: gethostbyname\n"); 28 | } 29 | else 30 | { 31 | char **aliases; 32 | char **addresses; 33 | 34 | aliases = h->h_aliases; 35 | while(*aliases != NULL) 36 | { 37 | printf("alias: %s\n", *aliases); 38 | aliases++; 39 | } 40 | 41 | addresses = h->h_addr_list; 42 | while(*addresses != NULL) 43 | { 44 | struct in_addr addr; 45 | 46 | memcpy(&addr.s_addr, *addresses, sizeof(in_addr_t)); 47 | printf("addr: %s\n", inet_ntoa(addr)); 48 | addresses++; 49 | } 50 | 51 | } 52 | return 0; 53 | } 54 | 55 | int main(void) 56 | { 57 | while(1) 58 | { 59 | loop(); 60 | } 61 | } 62 | 63 | -------------------------------------------------------------------------------- /tests/blink/blink.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include 21 | #include 22 | 23 | static void delay_loop(int32_t loops) 24 | { 25 | while(loops > 0) 26 | { 27 | asm("nop"); 28 | loops--; 29 | } 30 | } 31 | 32 | int main(void) 33 | { 34 | rcc_periph_clock_enable(RCC_GPIOA); 35 | gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO5); 36 | while(1) 37 | { 38 | gpio_toggle(GPIOA, GPIO5); 39 | delay_loop(1000000); 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /include/sd_spi.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #ifndef SD_SPI_H 20 | #define SD_SPI_H 21 | 22 | #include 23 | #include 24 | 25 | extern 26 | uint8_t sd_send_command_r1(uint8_t cmd, uint32_t arg); 27 | 28 | extern 29 | void sd_send_command(uint8_t cmd, uint32_t arg, void *resp, size_t len); 30 | 31 | extern 32 | int sd_read_single_block(uint32_t address, void *dst); 33 | 34 | extern 35 | int sd_write_single_block(uint32_t address, const void *src); 36 | 37 | extern 38 | void sd_full_speed(void); 39 | 40 | extern 41 | void sd_init(void); 42 | 43 | #endif /* SD_SPI_H */ 44 | 45 | -------------------------------------------------------------------------------- /include/poll.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #ifndef POLL_H 20 | #define POLL_H 21 | 22 | struct pollfd 23 | { 24 | int fd; 25 | short events; 26 | short revents; 27 | }; 28 | 29 | typedef unsigned int nfds_t; 30 | 31 | #define POLLIN 0x0001 32 | #define POLLRDNORM 0x0002 33 | #define POLLRDBAND 0x0004 34 | #define POLLPRI 0x0008 35 | #define POLLOUT 0x0010 36 | #define POLLWRNORM POLLOUT 37 | #define POLLWRBAND 0x0040 38 | #define POLLERR 0x0080 39 | #define POLLHUP 0x0100 40 | #define POLLNVAL 0x0200 41 | 42 | int poll(struct pollfd [], nfds_t, int); 43 | 44 | #endif /* POLL_H */ 45 | 46 | -------------------------------------------------------------------------------- /include/file.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #ifndef SYSCALLS_H 20 | #define SYSCALLS_H 21 | 22 | #include 23 | #include 24 | 25 | struct fd { 26 | int fd; 27 | struct stat stat; 28 | int isatty; 29 | int isopen; 30 | int (*write)(int, char*, int); 31 | int (*read)(int, char*, int); 32 | int (*close)(int); 33 | short (*poll)(int); 34 | int isallocated; 35 | int descriptor_flags; 36 | int status_flags; 37 | void *opaque; 38 | }; 39 | 40 | extern 41 | struct fd *file_struct_get(int fd); 42 | 43 | extern 44 | int file_alloc(void); 45 | 46 | extern 47 | void file_free(int fd); 48 | 49 | #endif 50 | 51 | -------------------------------------------------------------------------------- /include/arpa/inet.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #ifndef ARPA_INET_H 20 | #define ARPA_INET_H 21 | 22 | /* http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/arpa_inet.h.html */ 23 | 24 | #include 25 | #include 26 | 27 | uint32_t htonl(uint32_t); 28 | uint16_t htons(uint16_t); 29 | uint32_t ntohl(uint32_t); 30 | uint16_t ntohs(uint16_t); 31 | 32 | in_addr_t inet_addr(const char *); 33 | char *inet_ntoa(struct in_addr); 34 | const char *inet_ntop(int, const void *__restrict__, char *__restrict__, socklen_t); 35 | int inet_pton(int, const char *__restrict__, void *__restrict__); 36 | 37 | #endif /* ARPA_INET_H */ 38 | 39 | -------------------------------------------------------------------------------- /tests/w5100_dhcp/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = w5100_dhcp_test 21 | OBJS += $(ROOT_DIR)/src/dhcp_client.o 22 | OBJS += $(ROOT_DIR)/src/w5100_dhcp.o 23 | OBJS += $(ROOT_DIR)/src/w5100_socket.o 24 | OBJS += $(ROOT_DIR)/src/w5100_spi.o 25 | OBJS += $(ROOT_DIR)/src/inet.o 26 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 27 | OBJS += $(ROOT_DIR)/src/syscalls.o 28 | OBJS += $(ROOT_DIR)/src/file.o 29 | OBJS += $(ROOT_DIR)/src/timespec.o 30 | OBJS += $(ROOT_DIR)/src/clock_gettime_systick.o 31 | OBJS += $(ROOT_DIR)/src/sleep.o 32 | OBJS += $(ROOT_DIR)/src/nanosleep.o 33 | OBJS += $(ROOT_DIR)/src/clock_nanosleep_poll.o 34 | LDLIBS_SYS = 35 | 36 | CPPFLAGS += -DW5100_NO_STATIC_IP 37 | 38 | include ../test.mk 39 | 40 | -------------------------------------------------------------------------------- /src/kill.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include "sigqueue_info.h" 24 | 25 | int kill (pid_t pid, int sig) 26 | { 27 | int ret; 28 | pid_t this; 29 | 30 | this = getpid(); 31 | 32 | if ( (pid != this) && (pid > 0) ) 33 | { 34 | errno = ESRCH; 35 | ret = -1; 36 | } 37 | else if (sig != 0) 38 | { 39 | siginfo_t info; 40 | 41 | memset(&info, 0, sizeof(info)); 42 | info.si_signo = sig; 43 | info.si_code = SI_USER; 44 | info.si_pid = this; 45 | 46 | ret = sigqueue_info(&info); 47 | } 48 | else 49 | { 50 | ret = 0; 51 | } 52 | 53 | return ret; 54 | } 55 | 56 | -------------------------------------------------------------------------------- /tests/dns/dns.c: -------------------------------------------------------------------------------- 1 | /* 2 | C ECHO client example using sockets 3 | http://www.binarytides.com/server-client-example-c-sockets-linux/ 4 | */ 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | extern 12 | in_addr_t getaddrinfo_dns_server; 13 | 14 | static 15 | int loop(void) 16 | { 17 | struct addrinfo *ai; 18 | struct addrinfo *ai_iter; 19 | struct addrinfo hints; 20 | char *name; 21 | int res; 22 | 23 | printf("Press any key to continue..."); 24 | getchar(); 25 | printf("\n"); 26 | 27 | hints.ai_flags = 0; 28 | hints.ai_family = AF_INET; 29 | hints.ai_socktype = SOCK_STREAM; 30 | hints.ai_protocol = 0; 31 | name = "www.facebook.com"; 32 | 33 | res = getaddrinfo(name, NULL, &hints, &ai); 34 | if (res != 0) 35 | { 36 | if (res == EAI_SYSTEM) 37 | { 38 | perror("getaddrinfo"); 39 | } 40 | else 41 | { 42 | fprintf(stderr, "error: getaddrinfo: %d\n", res); 43 | } 44 | return 1; 45 | } 46 | for (ai_iter = ai; ai_iter != NULL; ai_iter = ai_iter->ai_next) 47 | { 48 | struct sockaddr_in *addr; 49 | 50 | addr = (struct sockaddr_in *)ai_iter->ai_addr; 51 | 52 | printf("%s: %s\n", 53 | ai_iter->ai_canonname, 54 | inet_ntoa(addr->sin_addr)); 55 | } 56 | 57 | freeaddrinfo(ai); 58 | return 0; 59 | } 60 | 61 | int main(void) 62 | { 63 | while(1) 64 | { 65 | loop(); 66 | } 67 | } 68 | 69 | -------------------------------------------------------------------------------- /tests/timesync/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = timesync_test 21 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 22 | OBJS += $(ROOT_DIR)/src/syscalls.o 23 | OBJS += $(ROOT_DIR)/src/file.o 24 | OBJS += $(ROOT_DIR)/src/clock_gettime_systick.o 25 | OBJS += $(ROOT_DIR)/src/timespec.o 26 | OBJS += $(ROOT_DIR)/src/timesync.o 27 | OBJS += $(ROOT_DIR)/src/w5100_socket.o 28 | OBJS += $(ROOT_DIR)/src/w5100_spi.o 29 | OBJS += $(ROOT_DIR)/src/inet.o 30 | 31 | TIMESYNC_METHOD ?= SNTP 32 | 33 | ifeq ($(TIMESYNC_METHOD),SNTP) 34 | #CPPFLAGS += -DTIMESYNC_METHOD=TIMESYNC_METHOD_SNTP 35 | OBJS += $(ROOT_DIR)/src/sntp.o 36 | endif 37 | ifeq ($(TIMESYNC_METHOD),RFC868) 38 | CPPFLAGS += -DTIMESYNC_METHOD=TIMESYNC_METHOD_RFC868 39 | OBJS += $(ROOT_DIR)/src/rfc868_time.o 40 | endif 41 | 42 | include ../test.mk 43 | 44 | -------------------------------------------------------------------------------- /include/limits.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #ifndef LIMITS_H 20 | #define LIMITS_H 21 | 22 | #include_next 23 | 24 | #ifndef OPEN_MAX 25 | # ifdef _POSIX_OPEN_MAX 26 | # define OPEN_MAX _POSIX_OPEN_MAX 27 | # else 28 | # define OPEN_MAX 20 29 | # endif 30 | #endif 31 | 32 | #ifndef NAME_MAX 33 | # ifdef _POSIX_NAME_MAX 34 | # define NAME_MAX _POSIX_NAME_MAX 35 | # else 36 | # define NAME_MAX 12 /* 8+1+3 like FILENAME.EXT */ 37 | # endif 38 | #endif 39 | 40 | #ifndef TIMER_MAX 41 | # ifdef _POSIX_TIMER_MAX 42 | # define TIMER_MAX _POSIX_TIMER_MAX 43 | # else 44 | # define TIMER_MAX 32 45 | # endif 46 | #endif 47 | 48 | #ifndef SIGQUEUE_MAX 49 | # ifdef _POSIX_SIGQUEUE_MAX 50 | # define SIGQUEUE_MAX _POSIX_SIGQUEUE_MAX 51 | # else 52 | # define SIGQUEUE_MAX 32 53 | # endif 54 | #endif 55 | 56 | #endif 57 | 58 | -------------------------------------------------------------------------------- /include/timespec.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #ifndef TIMESPEC_H 20 | #define TIMESPEC_H 21 | 22 | #include 23 | #include 24 | 25 | #define NSECS_IN_SEC 1000000000 26 | 27 | #define USECS_IN_SEC 1000000 28 | 29 | #define MSECS_IN_SEC 1000 30 | 31 | extern 32 | void timespec_to_timeval(const struct timespec *src, struct timeval *dst); 33 | 34 | extern 35 | void timeval_to_timespec(const struct timeval *src, struct timespec *dst); 36 | 37 | extern 38 | void timespec_add(const struct timespec *t1, const struct timespec *t2, struct timespec *dst); 39 | 40 | extern 41 | void timespec_incr(struct timespec *x, const struct timespec *step); 42 | 43 | extern 44 | int timespec_diff(const struct timespec *to, const struct timespec *from, struct timespec *diff); 45 | 46 | extern 47 | const struct timespec TIMESPEC_ZERO; 48 | 49 | extern 50 | const struct timespec TIMESPEC_INFINITY; 51 | 52 | #endif /* TIMESPEC_H */ 53 | 54 | -------------------------------------------------------------------------------- /include/w5100_dhcp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #ifndef W5100_DHCP 20 | #define W5100_DHCP 21 | 22 | #include 23 | #include 24 | 25 | /** 26 | * Manage DHCP. 27 | * 28 | * \retval < 0 an error occurred. 29 | * \retval == 0 should be re-called as soon as possible. 30 | * \retval > 0 the number of seconds after which it should be called again. 31 | */ 32 | extern 33 | time_t w5100_dhcp(void); 34 | 35 | /** 36 | * Manage DHCP and ensure that interface is bound to an address on exit. 37 | * 38 | * \return the number of seconds after which it should be called again. 39 | */ 40 | extern 41 | time_t w5100_dhcp_bind(void); 42 | 43 | /** 44 | * Get DNS server retrieved by DHCP 45 | */ 46 | extern 47 | in_addr_t w5100_getdns(void); 48 | 49 | /* 50 | * Get whether the interface has an IP address bound with a DHCP server. 51 | */ 52 | extern 53 | int w5100_dhcp_isbound(void); 54 | 55 | #endif /* W5100_DHCP */ 56 | 57 | -------------------------------------------------------------------------------- /include/dirent.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #ifndef DIRENT_H 20 | #define DIRENT_H 21 | 22 | typedef struct dirstream DIR; 23 | 24 | #include 25 | 26 | struct dirent { 27 | ino_t d_ino; /* File serial number. */ 28 | char d_name[]; /* Filename string of entry. */ 29 | }; 30 | 31 | int alphasort( 32 | const struct dirent **, 33 | const struct dirent **); 34 | 35 | int closedir(DIR *); 36 | 37 | int dirfd(DIR *); 38 | 39 | DIR *fdopendir(int); 40 | 41 | DIR *opendir(const char *); 42 | 43 | struct dirent *readdir(DIR *); 44 | 45 | int readdir_r( 46 | DIR *__restrict, 47 | struct dirent *__restrict, 48 | struct dirent **__restrict); 49 | 50 | void rewinddir(DIR *); 51 | 52 | int scandir( 53 | const char *, 54 | struct dirent ***, 55 | int (*)(const struct dirent *), 56 | int (*)(const struct dirent **, const struct dirent **)); 57 | 58 | void seekdir(DIR *, long); 59 | 60 | long telldir(DIR *); 61 | 62 | #endif /* DIRENT_H */ 63 | 64 | -------------------------------------------------------------------------------- /src/clock_nanosleep_poll.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include "timespec.h" 21 | 22 | /* polling implementation */ 23 | int clock_nanosleep( 24 | clockid_t clock_id, 25 | int flags, 26 | const struct timespec *rqtp, 27 | struct timespec *rmtp) 28 | { 29 | int ret; 30 | struct timespec tcurrent; 31 | struct timespec tend; 32 | 33 | ret = clock_gettime(clock_id, &tcurrent); 34 | 35 | if (!(flags & TIMER_ABSTIME)) 36 | { 37 | ret = clock_gettime(clock_id, &tcurrent); 38 | timespec_add(&tcurrent, rqtp, &tend); 39 | rqtp = &tend; 40 | } 41 | 42 | while (ret == 0) 43 | { 44 | if (timespec_diff(rqtp, &tcurrent, NULL) <= 0) 45 | { 46 | if (rmtp != NULL) 47 | { 48 | rmtp->tv_sec = 0; 49 | rmtp->tv_nsec = 0; 50 | } 51 | break; 52 | } 53 | ret = clock_gettime(clock_id, &tcurrent); 54 | } 55 | 56 | return ret; 57 | } 58 | 59 | -------------------------------------------------------------------------------- /include/sys/select.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #ifndef SYS_SELECT_H 20 | #define SYS_SELECT_H 21 | 22 | /* TODO: don't include this, instead define struct timeval here and 23 | * include sys/select.h in sys/time.h 24 | */ 25 | #include 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #define FD_SETSIZE OPEN_MAX 33 | 34 | struct fd_set_s { 35 | unsigned long mask[(FD_SETSIZE + (8*sizeof(unsigned long)) - 1) / (8*sizeof(unsigned long))]; 36 | }; 37 | 38 | typedef struct fd_set_s fd_set; 39 | 40 | void FD_CLR(int, fd_set *); 41 | 42 | int FD_ISSET(int, fd_set *); 43 | 44 | void FD_SET(int, fd_set *); 45 | 46 | void FD_ZERO(fd_set *); 47 | 48 | int pselect(int, fd_set *__restrict, fd_set *__restrict, fd_set *__restrict, 49 | const struct timespec *__restrict, const sigset_t *__restrict); 50 | 51 | int select(int, fd_set *__restrict, fd_set *__restrict, fd_set *__restrict, 52 | struct timeval *__restrict); 53 | 54 | #endif /* SYS_SELECT_H */ 55 | 56 | -------------------------------------------------------------------------------- /src/faults.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include 21 | #include "sigqueue_info.h" 22 | 23 | void mem_manage_handler(void) 24 | { 25 | siginfo_t info; 26 | 27 | info.si_signo = SIGSEGV; 28 | info.si_code = SEGV_ACCERR; 29 | /* TODO: addr */ 30 | 31 | sigqueue_info(&info); 32 | } 33 | 34 | void bus_fault_handler(void) 35 | { 36 | siginfo_t info; 37 | 38 | info.si_signo = SIGBUS; 39 | info.si_code = BUS_ADRERR; 40 | /* TODO: addr */ 41 | 42 | sigqueue_info(&info); 43 | } 44 | 45 | void usage_fault_handler(void) 46 | { 47 | siginfo_t info; 48 | 49 | info.si_signo = SIGILL; 50 | info.si_code = ILL_ILLOPC; 51 | /* TODO: addr */ 52 | 53 | sigqueue_info(&info); 54 | } 55 | 56 | void hard_fault_handler(void) 57 | { 58 | siginfo_t info; 59 | 60 | info.si_signo = SIGABRT; 61 | 62 | sigqueue_info(&info); 63 | } 64 | 65 | void nmi_handler(void) 66 | { 67 | siginfo_t info; 68 | 69 | info.si_signo = SIGABRT; 70 | 71 | sigqueue_info(&info); 72 | } 73 | 74 | -------------------------------------------------------------------------------- /tests/sntp/sntp_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include 21 | #include 22 | #include "timesync.h" 23 | 24 | static 25 | void print_timespec(const struct timespec *t) 26 | { 27 | struct tm datetime; 28 | char datetime_str[80]; 29 | 30 | printf("%lds %ldns\n", (long)t->tv_sec, (long)t->tv_nsec); 31 | datetime = *gmtime(&t->tv_sec); 32 | strftime(datetime_str, sizeof(datetime_str), "%A %d %B %Y, %H:%M:%S", &datetime); 33 | printf("%s\n", datetime_str); 34 | } 35 | 36 | int main(void) 37 | { 38 | struct timespec t; 39 | struct in_addr server; 40 | int res; 41 | 42 | printf( 43 | "sntp_test\n" 44 | "Press any key to continue...\n"); 45 | getchar(); 46 | 47 | clock_gettime(CLOCK_REALTIME, &t); 48 | print_timespec(&t); 49 | 50 | server.s_addr = sntp_timeserver_get(); 51 | printf("SNTP server: %s\n", inet_ntoa(server)); 52 | res = sntp_gettime(&t); 53 | if (res == 0) 54 | { 55 | print_timespec(&t); 56 | } 57 | else 58 | { 59 | perror("sntp_gettime"); 60 | } 61 | 62 | return 0; 63 | } 64 | 65 | -------------------------------------------------------------------------------- /tests/rfc868_time/rfc868_time_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include 21 | #include 22 | #include "timesync.h" 23 | 24 | static 25 | void print_timespec(const struct timespec *t) 26 | { 27 | struct tm datetime; 28 | char datetime_str[80]; 29 | 30 | printf("%lds %ldns\n", (long)t->tv_sec, (long)t->tv_nsec); 31 | datetime = *gmtime(&t->tv_sec); 32 | strftime(datetime_str, sizeof(datetime_str), "%A %d %B %Y, %H:%M:%S", &datetime); 33 | printf("%s\n", datetime_str); 34 | } 35 | 36 | int main(void) 37 | { 38 | struct timespec t; 39 | struct in_addr server; 40 | int res; 41 | 42 | printf( 43 | "rfc868_time_test\n" 44 | "Press any key to continue...\n"); 45 | getchar(); 46 | 47 | clock_gettime(CLOCK_REALTIME, &t); 48 | print_timespec(&t); 49 | 50 | server.s_addr = rfc868_timeserver_get(); 51 | printf("RFC868 server: %s\n", inet_ntoa(server)); 52 | res = rfc868_gettime(&t); 53 | if (res == 0) 54 | { 55 | print_timespec(&t); 56 | } 57 | else 58 | { 59 | perror("rfc868_gettime"); 60 | } 61 | 62 | return 0; 63 | } 64 | 65 | -------------------------------------------------------------------------------- /tests/timesync/timesync_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include 21 | #include "timesync.h" 22 | #include "timespec.h" 23 | 24 | static 25 | void print_timespec(const struct timespec *t) 26 | { 27 | struct tm datetime; 28 | char datetime_str[80]; 29 | 30 | printf("%lds %ldns\n", (long)t->tv_sec, (long)t->tv_nsec); 31 | datetime = *gmtime(&t->tv_sec); 32 | strftime(datetime_str, sizeof(datetime_str), "%A %d %B %Y, %H:%M:%S", &datetime); 33 | printf("%s\n", datetime_str); 34 | } 35 | 36 | int main(void) 37 | { 38 | int res; 39 | struct timespec t; 40 | 41 | printf( 42 | "timesync_test\n" 43 | "Press any key to continue...\n"); 44 | getchar(); 45 | 46 | t = TIMESPEC_ZERO; 47 | res = timesync_timespec(&t); 48 | printf("timesync_timespec(TIMESPEC_ZERO) returned %d\n", res); 49 | clock_gettime(CLOCK_REALTIME, &t); 50 | print_timespec(&t); 51 | res = timesync(); 52 | printf("timesync returned %d\n", res); 53 | res = timesync_now_timespec(&t); 54 | printf("timesync_now_timespec returned %d\n", res); 55 | print_timespec(&t); 56 | 57 | return 0; 58 | } 59 | 60 | 61 | -------------------------------------------------------------------------------- /scripts/libopencm3.target.mk: -------------------------------------------------------------------------------- 1 | ## 2 | ## This file is part of the libopencm3 project. 3 | ## 4 | ## Copyright (C) 2009 Uwe Hermann 5 | ## Copyright (C) 2010 Piotr Esden-Tempski 6 | ## 7 | ## This library is free software: you can redistribute it and/or modify 8 | ## it under the terms of the GNU Lesser General Public License as published by 9 | ## the Free Software Foundation, either version 3 of the License, or 10 | ## (at your option) any later version. 11 | ## 12 | ## This library is distributed in the hope that it will be useful, 13 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | ## GNU Lesser General Public License for more details. 16 | ## 17 | ## You should have received a copy of the GNU Lesser General Public License 18 | ## along with this library. If not, see . 19 | ## 20 | 21 | LIBNAME = opencm3_stm32f1 22 | DEFS = -DSTM32F1 23 | 24 | FP_FLAGS ?= -msoft-float 25 | ARCH_FLAGS = -mthumb -mcpu=cortex-m3 $(FP_FLAGS) -mfix-cortex-m3-ldrd 26 | 27 | ################################################################################ 28 | # OpenOCD specific variables 29 | 30 | OOCD ?= openocd 31 | OOCD_INTERFACE ?= flossjtag 32 | OOCD_BOARD ?= olimex_stm32_h103 33 | 34 | ################################################################################ 35 | # Black Magic Probe specific variables 36 | # Set the BMP_PORT to a serial port and then BMP is used for flashing 37 | BMP_PORT ?= 38 | 39 | ################################################################################ 40 | # texane/stlink specific variables 41 | #STLINK_PORT ?= :4242 42 | 43 | CPPFLAGS += -D_POSIX_SOURCE=200809L 44 | CPPFLAGS += -D_POSIX_C_SOURCE=200809L 45 | CPPFLAGS += -D_POSIX_TIMERS=200809L 46 | CPPFLAGS += -D_POSIX_MONOTONIC_CLOCK=200809L 47 | CPPFLAGS += -D_POSIX_CLOCK_SELECTION=200809L 48 | CPPFLAGS += -D_POSIX_REALTIME_SIGNALS=200809L 49 | 50 | include $(ROOT_DIR)/scripts/libopencm3.rules.mk 51 | 52 | -------------------------------------------------------------------------------- /src/file.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | static 29 | struct fd files[OPEN_MAX]; 30 | 31 | struct fd *file_struct_get(int fd) 32 | { 33 | struct fd *f; 34 | 35 | if (fd >= OPEN_MAX) 36 | { 37 | f = NULL; 38 | } 39 | else 40 | { 41 | f = &files[fd]; 42 | } 43 | return f; 44 | } 45 | 46 | int file_alloc(void) 47 | { 48 | int fd; 49 | int ret = -1; 50 | 51 | /* starting from 3 because of STDIN, STDOUT, STDERR */ 52 | for (fd = 3; fd < OPEN_MAX; fd++) 53 | { 54 | if (!files[fd].isallocated) 55 | { 56 | memset(&files[fd], 0, sizeof(files[fd])); 57 | 58 | files[fd].isallocated = 1; 59 | files[fd].fd = fd; 60 | ret = fd; 61 | break; 62 | } 63 | } 64 | return ret; 65 | } 66 | 67 | void file_free(int fd) 68 | { 69 | if ((fd < OPEN_MAX) && (fd >= 0) && (files[fd].isallocated)) 70 | { 71 | files[fd].isallocated = 0; 72 | } 73 | } 74 | 75 | -------------------------------------------------------------------------------- /src/fcntl.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include 21 | #include 22 | #include "file.h" 23 | 24 | int fcntl(int fildes, int cmd, ...) 25 | { 26 | va_list ap; 27 | struct fd *f; 28 | int ret; 29 | 30 | va_start(ap, cmd); 31 | 32 | f = file_struct_get(fildes); 33 | if (f == NULL) 34 | { 35 | errno = EBADF; 36 | ret = -1; 37 | } 38 | else 39 | { 40 | switch(cmd) 41 | { 42 | case F_GETFD: 43 | ret = f->descriptor_flags; 44 | break; 45 | case F_SETFD: 46 | f->descriptor_flags = va_arg(ap, int); 47 | ret = 0; 48 | break; 49 | case F_SETFL: 50 | f->status_flags = va_arg(ap, int); 51 | ret = 0; 52 | break; 53 | case F_GETFL: 54 | ret = f->status_flags; 55 | break; 56 | default: 57 | errno = ENOSYS; /* function not implemented */ 58 | ret = -1; 59 | break; 60 | } 61 | } 62 | 63 | va_end(ap); 64 | 65 | return ret; 66 | } 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /tests/diskio/diskio_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include 21 | #include "diskio.h" 22 | 23 | static 24 | void wait_enter(void) 25 | { 26 | int c; 27 | 28 | do { 29 | c = getchar(); 30 | } while ((c != '\n') && (c != '\r')); 31 | } 32 | 33 | static 34 | void print_data(const void *data, size_t n) 35 | { 36 | const uint8_t *bytes; 37 | size_t i; 38 | 39 | bytes = data; 40 | for (i = 0; i < n; i++) 41 | { 42 | printf("%02X", bytes[i]); 43 | if (((i + 1) % 16) == 0) 44 | { 45 | printf("\n"); 46 | } 47 | } 48 | } 49 | 50 | int main(void) 51 | { 52 | DSTATUS status; 53 | DRESULT result; 54 | BYTE pdrv; 55 | uint8_t data[512*2]; 56 | 57 | printf( 58 | "diskio_test\n" 59 | "Press Enter to continue...\n"); 60 | wait_enter(); 61 | 62 | pdrv = 0; 63 | 64 | status = disk_status(pdrv); 65 | printf("status: 0x%02X\n", status); 66 | status = disk_initialize(pdrv); 67 | printf("status: 0x%02X\n", status); 68 | status = disk_status(pdrv); 69 | printf("status: 0x%02X\n", status); 70 | 71 | result = disk_read (pdrv, data, 0, 2); 72 | printf("result: 0x%02X\n", result); 73 | if (result == RES_OK) 74 | { 75 | print_data(data, sizeof(data)); 76 | } 77 | return 0; 78 | } 79 | 80 | -------------------------------------------------------------------------------- /tests/fatfs_ro/fatfs_ro_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include 21 | #include "ff.h" 22 | 23 | DWORD get_fattime (void) 24 | { 25 | return ( 26 | ((2015-1980)<<25) 27 | | 28 | (1<<21) 29 | | 30 | (1<<16) 31 | ); 32 | } 33 | 34 | static 35 | void wait_enter(void) 36 | { 37 | int c; 38 | 39 | do { 40 | c = getchar(); 41 | } while ((c != '\n') && (c != '\r')); 42 | } 43 | 44 | int main(void) 45 | { 46 | FRESULT result; 47 | FATFS fs; 48 | FIL fp; 49 | 50 | printf( 51 | "fatfs_ro_test\n" 52 | "Press Enter to continue...\n"); 53 | wait_enter(); 54 | 55 | result = f_mount(&fs, "/", 1); 56 | printf("result: %d\n", result); 57 | if (result == FR_OK) 58 | { 59 | result = f_open(&fp, "/test.txt", FA_READ); 60 | printf("result: %d\n", result); 61 | } 62 | if (result == FR_OK) 63 | { 64 | uint8_t data[64]; 65 | UINT nread; 66 | 67 | do { 68 | 69 | result = f_read(&fp, data, sizeof(data), &nread); 70 | fwrite(data, nread, 1, stdout); 71 | } while((result == FR_OK) && (nread == sizeof(data))); 72 | if (result != FR_OK) 73 | { 74 | printf("\nresult: %d\n", result); 75 | } 76 | } 77 | 78 | return 0; 79 | } 80 | 81 | -------------------------------------------------------------------------------- /tests/socket_client/client.c: -------------------------------------------------------------------------------- 1 | /* 2 | C ECHO client example using sockets 3 | http://www.binarytides.com/server-client-example-c-sockets-linux/ 4 | */ 5 | #include //printf 6 | #include //strlen 7 | #include //close 8 | #include //socket 9 | #include //inet_addr 10 | 11 | #ifndef SERVER_IP_ADDR 12 | # define SERVER_IP_ADDR "192.168.1.173" 13 | #endif 14 | 15 | #ifndef SERVER_PORT 16 | # define SERVER_PORT 8888 17 | #endif 18 | 19 | static 20 | int loop(void) 21 | { 22 | int sock; 23 | struct sockaddr_in server; 24 | char message[1000] , server_reply[2000]; 25 | 26 | printf("Press any key to continue..."); 27 | getchar(); 28 | printf("\n"); 29 | 30 | //Create socket 31 | sock = socket(AF_INET , SOCK_STREAM , 0); 32 | if (sock == -1) 33 | { 34 | perror("socket creation failed"); 35 | return 1; 36 | } 37 | puts("Socket created\n"); 38 | 39 | server.sin_addr.s_addr = inet_addr(SERVER_IP_ADDR); 40 | server.sin_family = AF_INET; 41 | server.sin_port = htons( SERVER_PORT ); 42 | 43 | //Connect to remote server 44 | if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0) 45 | { 46 | perror("connect failed"); 47 | close(sock); 48 | return 1; 49 | } 50 | 51 | puts("Connected\n"); 52 | 53 | //keep communicating with server 54 | while(1) 55 | { 56 | printf("Enter message : "); 57 | scanf("%s" , message); 58 | 59 | //Send some data 60 | if( send(sock , message , strlen(message)+1 , 0) < 0) 61 | { 62 | perror("Send failed"); 63 | close(sock); 64 | return 1; 65 | } 66 | 67 | //Receive a reply from the server 68 | if( recv(sock , server_reply , 2000 , 0) < 0) 69 | { 70 | perror("recv failed"); 71 | break; 72 | } 73 | 74 | puts("Server reply :"); 75 | puts(server_reply); 76 | } 77 | 78 | close(sock); 79 | return 0; 80 | } 81 | 82 | int main(void) 83 | { 84 | while(1) 85 | { 86 | loop(); 87 | } 88 | } 89 | 90 | -------------------------------------------------------------------------------- /tests/udp_server/udp_server.c: -------------------------------------------------------------------------------- 1 | /* 2 | C socket server example 3 | http://www.binarytides.com/server-client-example-c-sockets-linux/ 4 | */ 5 | 6 | #include 7 | #include //strlen 8 | #include 9 | #include //inet_addr 10 | #include //write 11 | 12 | static 13 | int loop(void) 14 | { 15 | int socket_desc , client_sock , c , read_size; 16 | struct sockaddr_in server , client; 17 | char client_message[2000]; 18 | 19 | printf("Press any key to continue..."); 20 | getchar(); 21 | printf("\n"); 22 | 23 | //Create socket 24 | socket_desc = socket(AF_INET , SOCK_DGRAM , 0); 25 | if (socket_desc == -1) 26 | { 27 | perror("Could not create socket"); 28 | } 29 | puts("Socket created"); 30 | 31 | //Prepare the sockaddr_in structure 32 | server.sin_family = AF_INET; 33 | server.sin_addr.s_addr = INADDR_ANY; 34 | server.sin_port = htons( 8888 ); 35 | 36 | //Bind 37 | if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) != 0) 38 | { 39 | //print the error message 40 | perror("bind failed."); 41 | close(socket_desc); 42 | return 1; 43 | } 44 | puts("bind done"); 45 | 46 | while(1) 47 | { 48 | struct sockaddr_in peer; 49 | socklen_t address_len = sizeof(peer); 50 | 51 | //Receive a message from client 52 | while( (read_size = recvfrom(socket_desc , client_message , 2000 , 0, &peer, &address_len)) > 0 ) 53 | { 54 | uint8_t *ipaddr = &peer.sin_addr.s_addr; 55 | printf("recv(%d.%d.%d.%d:%d): %s\n", 56 | ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3], 57 | ntohs(peer.sin_port), 58 | client_message); 59 | //Send the message back to client 60 | sendto(socket_desc , client_message , strlen(client_message)+1, 0, &peer, address_len); 61 | } 62 | 63 | if(read_size == -1) 64 | { 65 | perror("recv failed"); 66 | close(socket_desc); 67 | } 68 | } 69 | 70 | return 0; 71 | } 72 | 73 | int main(void) 74 | { 75 | while(1) 76 | { 77 | loop(); 78 | } 79 | } 80 | 81 | -------------------------------------------------------------------------------- /include/fatfs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #ifndef FATFS_H 20 | #define FATFS_H 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | extern 27 | int fatfs_open(const char *pathname, int flags); 28 | 29 | extern 30 | off_t fatfs_lseek(int fd, off_t offset, int whence ); 31 | 32 | extern 33 | int fatfs_unlink(const char *path); 34 | 35 | extern 36 | int fatfs_link(const char *path1, const char *path2); 37 | 38 | extern 39 | int fatfs_rename(const char *old, const char *new); 40 | 41 | extern 42 | int fatfs_fsync(int fd); 43 | 44 | extern 45 | int fatfs_stat(const char *path, struct stat *buf); 46 | 47 | extern 48 | int fatfs_mkdir(const char *path, mode_t mode); 49 | 50 | extern 51 | int fatfs_rmdir(const char *path); 52 | 53 | extern 54 | int fatfs_chdir(const char *path); 55 | 56 | extern 57 | char *fatfs_getcwd(char *buf, size_t size); 58 | 59 | extern 60 | DIR *fatfs_opendir(const char *path); 61 | 62 | extern 63 | int fatfs_closedir(DIR *dirp); 64 | 65 | extern 66 | struct dirent *fatfs_readdir(DIR *); 67 | 68 | extern 69 | int fatfs_readdir_r( 70 | DIR *__restrict, 71 | struct dirent *__restrict, 72 | struct dirent **__restrict); 73 | 74 | extern 75 | void fatfs_rewinddir(DIR *); 76 | 77 | extern 78 | long fatfs_telldir(DIR *); 79 | 80 | extern 81 | int fatfs_dirfd(DIR *); 82 | 83 | extern 84 | DIR *fatfs_fdopendir(int fd); 85 | 86 | extern 87 | void fatfs_seekdir(DIR *, long); 88 | 89 | #endif /* FATFS_H */ 90 | 91 | -------------------------------------------------------------------------------- /tests/udp_client/udp_client.c: -------------------------------------------------------------------------------- 1 | /* 2 | C ECHO client example using sockets 3 | http://www.binarytides.com/server-client-example-c-sockets-linux/ 4 | */ 5 | #include //printf 6 | #include //strlen 7 | #include //close 8 | #include //socket 9 | #include //inet_addr 10 | 11 | #ifndef SERVER_IP_ADDR 12 | # define SERVER_IP_ADDR "192.168.1.173" 13 | #endif 14 | 15 | #ifndef SERVER_PORT 16 | # define SERVER_PORT 8888 17 | #endif 18 | 19 | static 20 | int loop(void) 21 | { 22 | int sock; 23 | struct sockaddr_in server; 24 | 25 | char message[1000] , server_reply[2000]; 26 | 27 | printf("Press any key to continue..."); 28 | getchar(); 29 | printf("\n"); 30 | 31 | //Create socket 32 | sock = socket(AF_INET , SOCK_DGRAM , 0); 33 | if (sock == -1) 34 | { 35 | perror("socket creation failed"); 36 | return 1; 37 | } 38 | puts("Socket created\n"); 39 | 40 | server.sin_addr.s_addr = inet_addr(SERVER_IP_ADDR); 41 | server.sin_family = AF_INET; 42 | server.sin_port = htons( SERVER_PORT ); 43 | 44 | //keep communicating with server 45 | while(1) 46 | { 47 | struct sockaddr_in peer; 48 | socklen_t address_len = sizeof(server); 49 | 50 | printf("Enter message : "); 51 | scanf("%s" , message); 52 | 53 | //Send some data 54 | if( sendto(sock , message , strlen(message)+1 , 0, &server, address_len) < 0) 55 | { 56 | perror("Send failed"); 57 | close(sock); 58 | return 1; 59 | } 60 | 61 | //Receive a reply from the server 62 | if( recvfrom(sock , server_reply , 2000 , 0, &peer, &address_len) < 0) 63 | { 64 | perror("recv failed"); 65 | break; 66 | } 67 | 68 | puts("Server reply :"); 69 | uint8_t *ipaddr = &peer.sin_addr.s_addr; 70 | printf("Server (%d.%d.%d.%d:%d) reply: %s\n", 71 | ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3], 72 | ntohs(peer.sin_port), 73 | server_reply); 74 | } 75 | 76 | close(sock); 77 | return 0; 78 | } 79 | 80 | int main(void) 81 | { 82 | while(1) 83 | { 84 | loop(); 85 | } 86 | } 87 | 88 | -------------------------------------------------------------------------------- /src/gethostbyname.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include 21 | #include 22 | 23 | #define NAME_MAX_LEN 256 24 | #define ADDRESSES_MAX 4 25 | 26 | static 27 | struct hostent host; 28 | 29 | static 30 | in_addr_t addresses[ADDRESSES_MAX]; 31 | 32 | static 33 | char *paddresses[ADDRESSES_MAX + 1]; /* NULL-terminated */ 34 | 35 | static 36 | char hostname[NAME_MAX_LEN]; 37 | 38 | static 39 | char *alias_null[1] = { NULL }; /* NULL-terminated immediately */ 40 | 41 | struct hostent *gethostbyname(const char *name) 42 | { 43 | struct addrinfo *res; 44 | int ret; 45 | 46 | if (strlen(name) >= NAME_MAX_LEN) 47 | { 48 | return NULL; 49 | } 50 | ret = getaddrinfo(name, NULL, NULL, &res); 51 | if ((ret != 0) || (res == NULL)) 52 | { 53 | return NULL; 54 | } 55 | else 56 | { 57 | int i_addr; 58 | 59 | strncpy(hostname, name, NAME_MAX_LEN); 60 | i_addr = 0; 61 | while((res != NULL) && (i_addr < ADDRESSES_MAX)) 62 | { 63 | struct sockaddr_in *addr; 64 | 65 | addr = (struct sockaddr_in *)res->ai_addr; 66 | addresses[i_addr] = addr->sin_addr.s_addr; 67 | paddresses[i_addr] = (char *)&addresses[i_addr]; 68 | res = res->ai_next; 69 | i_addr++; 70 | } 71 | paddresses[i_addr] = NULL; 72 | host.h_name = hostname; 73 | host.h_aliases = &alias_null[0]; 74 | host.h_addrtype = AF_INET; 75 | host.h_length = 4; 76 | host.h_addr_list = paddresses; 77 | 78 | freeaddrinfo(res); 79 | 80 | return &host; 81 | } 82 | } 83 | 84 | -------------------------------------------------------------------------------- /tests/wolfssl/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = wolfssl 21 | OBJS += $(ROOT_DIR)/src/w5100_socket.o 22 | OBJS += $(ROOT_DIR)/src/w5100_spi.o 23 | OBJS += $(ROOT_DIR)/src/inet.o 24 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 25 | OBJS += $(ROOT_DIR)/src/syscalls.o 26 | OBJS += $(ROOT_DIR)/src/file.o 27 | OBJS += $(ROOT_DIR)/src/timespec.o 28 | OBJS += $(ROOT_DIR)/src/clock_gettime_systick.o 29 | OBJS += $(ROOT_DIR)/src/gettimeofday.o 30 | OBJS += $(ROOT_DIR)/src/getaddrinfo.o 31 | LDLIBS_SYS = 32 | 33 | WOLFSSL_DIR = $(ROOT_DIR)/wolfssl-3.7.0 34 | CPPFLAGS += -I$(WOLFSSL_DIR) 35 | LDFLAGS += -L$(WOLFSSL_DIR)/src/.libs 36 | LDLIBS += -lwolfssl -lm 37 | 38 | include ../test.mk 39 | 40 | clean: libwolfssl_clean 41 | 42 | libwolfssl_clean: 43 | cd $(WOLFSSL_DIR); \ 44 | make -s distclean || true; 45 | 46 | wolfssl.o: $(WOLFSSL_DIR)/src/.libs/libwolfssl.a 47 | 48 | $(WOLFSSL_DIR)/src/.libs/libwolfssl.a: 49 | cd $(WOLFSSL_DIR); \ 50 | export CPPFLAGS="-I/home/francesco/Projects/Nucleo/nucleo_tests/include -D_POSIX_SOURCE=200809L -D_POSIX_C_SOURCE=200809L -D_POSIX_TIMERS=200809L -D_POSIX_MONOTONIC_CLOCK=200809L -D_POSIX_CLOCK_SELECTION=200809L -D_XOPEN_SOURCE=700 -DCUSTOM_RAND_GENERATE=rand -DWOLFSSL_STATIC_RSA -DNO_WOLFSSL_SERVER -DNO_SESSION_CACHE"; \ 51 | export CFLAGS="-mthumb -march=armv7-m -g"; \ 52 | export LDFLAGS="-mthumb -march=armv7-m -g --specs=nosys.specs -L/home/francesco/Projects/Nucleo/nucleo_tests/src/ -L/home/francesco/Projects/Nucleo/nucleo_tests/libopencm3/lib"; \ 53 | export LIBS="-lposix -lopencm3_stm32f1"; \ 54 | ./configure -q --host=arm-none-eabi --disable-filesystem --disable-des3 --disable-chacha --disable-poly1305 --disable-memory --disable-oldtls; \ 55 | make -s 56 | 57 | -------------------------------------------------------------------------------- /tests/signal/signal_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include 21 | 22 | static 23 | void alarm_handler(int signo, siginfo_t *info, void *context) 24 | { 25 | (void)context; 26 | printf("Alarm! %d %d %d %d\n", signo, info->si_signo, info->si_value.sival_int, info->si_code); 27 | } 28 | 29 | int main(void) 30 | { 31 | struct sigaction act; 32 | 33 | act.sa_sigaction = alarm_handler; 34 | sigemptyset(&act.sa_mask); 35 | act.sa_flags = SA_SIGINFO; 36 | if (sigaction( 37 | SIGALRM, 38 | &act, 39 | NULL) != 0) 40 | { 41 | perror("sigaction"); 42 | return 1; 43 | } 44 | 45 | if (sigaction( 46 | SIGUSR1, 47 | &act, 48 | NULL) != 0) 49 | { 50 | perror("sigaction"); 51 | return 1; 52 | } 53 | 54 | if (sighold(SIGALRM) != 0) 55 | { 56 | perror("sighold"); 57 | return 1; 58 | } 59 | if (sighold(SIGUSR1) != 0) 60 | { 61 | perror("sighold"); 62 | return 1; 63 | } 64 | printf("holding...\n"); 65 | 66 | if (raise(SIGALRM) != 0) 67 | { 68 | perror("raise"); 69 | return 1; 70 | } 71 | printf("raised...\n"); 72 | 73 | if (raise(SIGUSR1) != 0) 74 | { 75 | perror("raise"); 76 | return 1; 77 | } 78 | printf("raised...\n"); 79 | 80 | printf("unblocking...\n"); 81 | sigset_t set; 82 | sigemptyset(&set); 83 | sigaddset(&set, SIGALRM); 84 | sigaddset(&set, SIGUSR1); 85 | 86 | if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) 87 | { 88 | perror("sigprocmask"); 89 | return 1; 90 | } 91 | printf("unblocked!\n"); 92 | } 93 | 94 | 95 | -------------------------------------------------------------------------------- /tests/mbedtls/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Francesco Balducci 3 | # 4 | # This file is part of nucleo_tests. 5 | # 6 | # nucleo_tests is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # nucleo_tests is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with nucleo_tests. If not, see . 18 | # 19 | 20 | BINARY = mbedtls_test 21 | OBJS += $(ROOT_DIR)/src/w5100_socket.o 22 | OBJS += $(ROOT_DIR)/src/w5100_spi.o 23 | OBJS += $(ROOT_DIR)/src/inet.o 24 | OBJS += $(ROOT_DIR)/src/stdio_usart.o 25 | OBJS += $(ROOT_DIR)/src/syscalls.o 26 | OBJS += $(ROOT_DIR)/src/file.o 27 | OBJS += $(ROOT_DIR)/src/timespec.o 28 | OBJS += $(ROOT_DIR)/src/clock_gettime_systick.o 29 | OBJS += $(ROOT_DIR)/src/gettimeofday.o 30 | OBJS += $(ROOT_DIR)/src/getaddrinfo.o 31 | OBJS += $(ROOT_DIR)/src/select.o 32 | OBJS += $(ROOT_DIR)/src/poll.o 33 | OBJS += digicert_der.o 34 | LDLIBS_SYS = 35 | 36 | MBEDTLS_DIR = $(ROOT_DIR)/mbedtls-2.2.0 37 | CPPFLAGS += -I$(MBEDTLS_DIR)/include 38 | LDFLAGS += -L$(MBEDTLS_DIR)/library 39 | LDLIBS += -lmbedtls -lmbedx509 -lmbedcrypto 40 | CPPFLAGSG += -I$(PWD) -DMBEDTLS_CONFIG_FILE="" 41 | 42 | include ../test.mk 43 | 44 | %_der.c: %.der 45 | xxd -i $< |sed 's/\/const unsigned/g' >$@ 46 | 47 | clean: libmbedtls_clean 48 | 49 | libmbedtls_clean: 50 | cd $(MBEDTLS_DIR); \ 51 | make -s clean; 52 | 53 | mbedtls_test.o: $(MBEDTLS_DIR)/library/libmbedtls.a 54 | 55 | $(MBEDTLS_DIR)/library/libmbedtls.a: 56 | root_dir=`readlink -f $(ROOT_DIR)`; \ 57 | cd $(MBEDTLS_DIR); \ 58 | export CPPFLAGS="-I$$root_dir/include -D_POSIX_SOURCE=200809L -D_POSIX_C_SOURCE=200809L -D_POSIX_TIMERS=200809L -D_POSIX_MONOTONIC_CLOCK=200809L -D_POSIX_CLOCK_SELECTION=200809L -D_XOPEN_SOURCE=700 -I$(PWD) -DMBEDTLS_CONFIG_FILE=\"\""; \ 59 | export CFLAGS="$(ARCH_FLAGS) -g -Os -ffunction-sections $$CPPFLAGS"; \ 60 | export LDFLAGS="$(ARCH_FLAGS) -g --specs=nosys.specs -L$$root_dir/src/ -L$$root_dir/libopencm3/lib"; \ 61 | export CC=arm-none-eabi-gcc; \ 62 | export AR=arm-none-eabi-ar; \ 63 | export LIBS="-lposix -lopencm3_stm32f1"; \ 64 | make lib 65 | 66 | -------------------------------------------------------------------------------- /src/w5100_dhcp.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include "w5100_dhcp.h" 20 | #include "dhcp_client.h" 21 | #include "w5100.h" 22 | #include "timespec.h" 23 | 24 | static 25 | struct dhcp_binding binding; 26 | 27 | static struct config { 28 | in_addr_t client; 29 | in_addr_t gateway; 30 | in_addr_t subnet; 31 | } config; 32 | 33 | static 34 | int initialized = 0; 35 | 36 | static 37 | void configure(void) 38 | { 39 | if (binding.client != config.client) 40 | { 41 | w5100_write_regx(W5100_SIPR, &binding.client); 42 | config.client = binding.client; 43 | } 44 | if (binding.gateway != config.gateway) 45 | { 46 | w5100_write_regx(W5100_GAR, &binding.gateway); 47 | config.gateway = binding.gateway; 48 | } 49 | if (binding.subnet != config.subnet) 50 | { 51 | w5100_write_regx(W5100_SUBR, &binding.subnet); 52 | config.subnet = binding.subnet; 53 | } 54 | } 55 | 56 | in_addr_t w5100_getdns(void) 57 | { 58 | return binding.dns_server; 59 | } 60 | 61 | int w5100_dhcp_isbound(void) 62 | { 63 | return dhcp_isbound(&binding); 64 | } 65 | 66 | time_t w5100_dhcp(void) 67 | { 68 | time_t next; 69 | 70 | if (!initialized) 71 | { 72 | uint8_t mac_addr[6]; 73 | 74 | w5100_read_regx(W5100_SHAR, mac_addr); 75 | 76 | /* TODO: check if we already have a preferred IP address */ 77 | dhcp_init(mac_addr, &binding); 78 | initialized = 1; 79 | } 80 | next = dhcp_step(&binding); 81 | 82 | if (w5100_dhcp_isbound()) 83 | { 84 | configure(); 85 | } 86 | return next; 87 | } 88 | 89 | time_t w5100_dhcp_bind(void) 90 | { 91 | time_t ret; 92 | 93 | do 94 | { 95 | ret = w5100_dhcp(); 96 | } while(!w5100_dhcp_isbound()); 97 | 98 | return ret; 99 | } 100 | 101 | -------------------------------------------------------------------------------- /src/timespec.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include "timespec.h" 20 | #include 21 | 22 | const struct timespec TIMESPEC_ZERO = {0, 0}; 23 | const struct timespec TIMESPEC_INFINITY = {INT_MAX, LONG_MAX}; 24 | 25 | void timespec_add(const struct timespec *t1, const struct timespec *t2, struct timespec *dst) 26 | { 27 | dst->tv_sec = t1->tv_sec + t2->tv_sec; 28 | dst->tv_nsec = t1->tv_nsec + t2->tv_nsec; 29 | if (dst->tv_nsec >= NSECS_IN_SEC) 30 | { 31 | dst->tv_nsec -= NSECS_IN_SEC; 32 | dst->tv_sec++; 33 | } 34 | } 35 | 36 | void timespec_incr(struct timespec *x, const struct timespec *step) 37 | { 38 | struct timespec tmp; 39 | 40 | tmp = *x; 41 | timespec_add(&tmp, step, x); 42 | } 43 | 44 | int timespec_diff(const struct timespec *to, const struct timespec *from, struct timespec *diff) 45 | { 46 | long diff_nsec; 47 | long diff_sec; 48 | int ret; 49 | 50 | /* assuming to and from have tv_nsec 0 up to 1000000000 excluded */ 51 | 52 | diff_nsec = to->tv_nsec - from->tv_nsec; 53 | diff_sec = to->tv_sec - from->tv_sec; 54 | 55 | if (diff_nsec < 0) 56 | { 57 | diff_nsec += NSECS_IN_SEC; 58 | diff_sec--; 59 | } 60 | if (diff != NULL) 61 | { 62 | diff->tv_nsec = diff_nsec; 63 | diff->tv_sec = diff_sec; 64 | } 65 | 66 | if (diff_sec == 0) 67 | { 68 | ret = diff_nsec; 69 | } 70 | else 71 | { 72 | ret = diff_sec; 73 | } 74 | return ret; 75 | } 76 | 77 | void timespec_to_timeval(const struct timespec *src, struct timeval *dst) 78 | { 79 | dst->tv_sec = src->tv_sec; 80 | dst->tv_usec = src->tv_nsec / (NSECS_IN_SEC / USECS_IN_SEC); 81 | } 82 | 83 | extern 84 | void timeval_to_timespec(const struct timeval *src, struct timespec *dst) 85 | { 86 | dst->tv_sec = src->tv_sec; 87 | dst->tv_nsec = src->tv_usec * (NSECS_IN_SEC / USECS_IN_SEC); 88 | } 89 | 90 | -------------------------------------------------------------------------------- /tests/mbedtls/mbedtls_nucleo_config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Minimal configuration for TLS 1.1 (RFC 4346) 3 | * 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 5 | * SPDX-License-Identifier: GPL-2.0 6 | * 7 | * This program is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License along 18 | * with this program; if not, write to the Free Software Foundation, Inc., 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | /* 24 | * Minimal configuration for TLS 1.1 (RFC 4346), implementing only the 25 | * required ciphersuite: MBEDTLS_TLS_RSA_WITH_3DES_EDE_CBC_SHA 26 | * 27 | * See README.txt for usage instructions. 28 | */ 29 | 30 | #ifndef MBEDTLS_CONFIG_H 31 | #define MBEDTLS_CONFIG_H 32 | 33 | /* System support */ 34 | #define MBEDTLS_HAVE_ASM 35 | #define MBEDTLS_HAVE_TIME 36 | 37 | /* mbed TLS feature support */ 38 | #define MBEDTLS_CIPHER_MODE_CBC 39 | #define MBEDTLS_PKCS1_V15 40 | #define MBEDTLS_KEY_EXCHANGE_RSA_ENABLED 41 | #define MBEDTLS_SSL_PROTO_TLS1_2 42 | 43 | /* mbed TLS modules */ 44 | #define MBEDTLS_AES_C 45 | #define MBEDTLS_ASN1_PARSE_C 46 | #define MBEDTLS_ASN1_WRITE_C 47 | #define MBEDTLS_BIGNUM_C 48 | #define MBEDTLS_CIPHER_C 49 | #define MBEDTLS_CTR_DRBG_C 50 | //#define MBEDTLS_DES_C 51 | #define MBEDTLS_ENTROPY_C 52 | #define MBEDTLS_MD_C 53 | #define MBEDTLS_MD5_C 54 | #define MBEDTLS_NET_C 55 | #define MBEDTLS_OID_C 56 | #define MBEDTLS_PK_C 57 | #define MBEDTLS_PK_PARSE_C 58 | #define MBEDTLS_RSA_C 59 | #define MBEDTLS_SHA1_C 60 | #define MBEDTLS_SHA256_C 61 | #define MBEDTLS_SSL_CLI_C 62 | //#define MBEDTLS_SSL_SRV_C 63 | #define MBEDTLS_SSL_TLS_C 64 | #define MBEDTLS_X509_CRT_PARSE_C 65 | #define MBEDTLS_X509_USE_C 66 | #define MBEDTLS_ERROR_C 67 | 68 | /* For test certificates */ 69 | #define MBEDTLS_BASE64_C 70 | #define MBEDTLS_CERTS_C 71 | #define MBEDTLS_PEM_PARSE_C 72 | 73 | /* For testing with compat.sh */ 74 | #define MBEDTLS_FS_IO 75 | 76 | //#define MBEDTLS_MPI_MAX_SIZE 512 77 | //#define MBEDTLS_SSL_MAX_CONTENT_LEN 512 78 | //#define MBEDTLS_SSL_MAX_FRAGMENT_LENGTH 79 | #define MBEDTLS_AES_ROM_TABLES 80 | 81 | #include "mbedtls/check_config.h" 82 | 83 | #endif /* MBEDTLS_CONFIG_H */ 84 | -------------------------------------------------------------------------------- /tests/socket_server/server.c: -------------------------------------------------------------------------------- 1 | /* 2 | C socket server example 3 | http://www.binarytides.com/server-client-example-c-sockets-linux/ 4 | */ 5 | 6 | #include 7 | #include //strlen 8 | #include 9 | #include //inet_addr 10 | #include //write 11 | 12 | static 13 | int loop(void) 14 | { 15 | int socket_desc , client_sock , c , read_size; 16 | struct sockaddr_in server , client; 17 | char client_message[2000]; 18 | 19 | printf("Press any key to continue..."); 20 | getchar(); 21 | printf("\n"); 22 | 23 | //Create socket 24 | socket_desc = socket(AF_INET , SOCK_STREAM , 0); 25 | if (socket_desc == -1) 26 | { 27 | perror("Could not create socket"); 28 | } 29 | puts("Socket created"); 30 | 31 | //Prepare the sockaddr_in structure 32 | server.sin_family = AF_INET; 33 | server.sin_addr.s_addr = INADDR_ANY; 34 | server.sin_port = htons( 8888 ); 35 | 36 | //Bind 37 | if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) != 0) 38 | { 39 | //print the error message 40 | perror("bind failed."); 41 | close(socket_desc); 42 | return 1; 43 | } 44 | puts("bind done"); 45 | 46 | //Listen 47 | if (listen(socket_desc , SOMAXCONN) != 0) 48 | { 49 | //print the error message 50 | perror("listen failed."); 51 | close(socket_desc); 52 | return 1; 53 | } 54 | 55 | while(1) 56 | { 57 | //Accept and incoming connection 58 | puts("Waiting for incoming connections..."); 59 | c = sizeof(struct sockaddr_in); 60 | 61 | //accept connection from an incoming client 62 | client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c); 63 | if (client_sock < 0) 64 | { 65 | perror("accept failed."); 66 | close(socket_desc); 67 | return 1; 68 | } 69 | puts("Connection accepted"); 70 | 71 | //Receive a message from client 72 | while( (read_size = recv(client_sock , client_message , 2000 , 0)) > 0 ) 73 | { 74 | printf("recv: %s\n", client_message); 75 | //Send the message back to client 76 | write(client_sock , client_message , strlen(client_message)+1); 77 | } 78 | 79 | if(read_size == 0) 80 | { 81 | puts("Client disconnected"); 82 | fflush(stdout); 83 | close(client_sock); 84 | } 85 | else if(read_size == -1) 86 | { 87 | perror("recv failed"); 88 | close(client_sock); 89 | } 90 | } 91 | 92 | return 0; 93 | } 94 | 95 | int main(void) 96 | { 97 | while(1) 98 | { 99 | loop(); 100 | } 101 | } 102 | 103 | -------------------------------------------------------------------------------- /tests/socket_client/host/server.c: -------------------------------------------------------------------------------- 1 | /* 2 | C socket server example 3 | http://www.binarytides.com/server-client-example-c-sockets-linux/ 4 | */ 5 | 6 | #include 7 | #include //strlen 8 | #include 9 | #include //inet_addr 10 | #include //write 11 | 12 | static 13 | int loop(void) 14 | { 15 | int socket_desc , client_sock , c , read_size; 16 | struct sockaddr_in server , client; 17 | char client_message[2000]; 18 | 19 | printf("Press any key to continue..."); 20 | getchar(); 21 | printf("\n"); 22 | 23 | //Create socket 24 | socket_desc = socket(AF_INET , SOCK_STREAM , 0); 25 | if (socket_desc == -1) 26 | { 27 | perror("Could not create socket"); 28 | } 29 | puts("Socket created"); 30 | 31 | //Prepare the sockaddr_in structure 32 | server.sin_family = AF_INET; 33 | server.sin_addr.s_addr = INADDR_ANY; 34 | server.sin_port = htons( 8888 ); 35 | 36 | //Bind 37 | if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) != 0) 38 | { 39 | //print the error message 40 | perror("bind failed."); 41 | close(socket_desc); 42 | return 1; 43 | } 44 | puts("bind done"); 45 | 46 | //Listen 47 | if (listen(socket_desc , SOMAXCONN) != 0) 48 | { 49 | //print the error message 50 | perror("listen failed."); 51 | close(socket_desc); 52 | return 1; 53 | } 54 | 55 | while(1) 56 | { 57 | //Accept and incoming connection 58 | puts("Waiting for incoming connections..."); 59 | c = sizeof(struct sockaddr_in); 60 | 61 | //accept connection from an incoming client 62 | client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c); 63 | if (client_sock < 0) 64 | { 65 | perror("accept failed."); 66 | close(socket_desc); 67 | return 1; 68 | } 69 | puts("Connection accepted"); 70 | 71 | //Receive a message from client 72 | while( (read_size = recv(client_sock , client_message , 2000 , 0)) > 0 ) 73 | { 74 | printf("recv: %s\n", client_message); 75 | //Send the message back to client 76 | write(client_sock , client_message , strlen(client_message)+1); 77 | } 78 | 79 | if(read_size == 0) 80 | { 81 | puts("Client disconnected"); 82 | fflush(stdout); 83 | close(client_sock); 84 | } 85 | else if(read_size == -1) 86 | { 87 | perror("recv failed"); 88 | close(client_sock); 89 | } 90 | } 91 | 92 | return 0; 93 | } 94 | 95 | int main(void) 96 | { 97 | while(1) 98 | { 99 | loop(); 100 | } 101 | } 102 | 103 | -------------------------------------------------------------------------------- /include/timesync.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #ifndef TIMESYNC_H 20 | #define TIMESYNC_H 21 | 22 | #include 23 | #include 24 | 25 | /* 26 | * timesync synchronizes CLOCK_REALTIME to a time synchronization source. 27 | * It uses clock_gettime to take current time, then decides if too much 28 | * time has passed since last synchronization and in case it executes it, 29 | * then uses clock_settime to apply the new time setting. 30 | * 31 | * With timesync_timespec the current time must be 32 | * passed as parameter. The current time after synchronization is written 33 | * back in the same parameter. 34 | * 35 | * timesync_now forces synchronization, and timesync_now_timespec also 36 | * returns the current time into the parameter. 37 | * 38 | * All functions return 1 when synchronization has been successful, 39 | * and -1 when an error occurred during synchronization. timesync and 40 | * timesync_timespec return 0 when it is not time to synchronize yet. 41 | */ 42 | 43 | extern 44 | int timesync(void); 45 | 46 | extern 47 | int timesync_timespec(struct timespec *now); 48 | 49 | extern 50 | int timesync_now(void); 51 | 52 | extern 53 | int timesync_now_timespec(struct timespec *out); 54 | 55 | /* Get time from a RFC868 server. 56 | * The time is written in the timespec structure pointed by ts parameter. 57 | * Returns 0 if successful, -1 if some error happened. 58 | */ 59 | extern 60 | int rfc868_gettime(struct timespec *ts); 61 | 62 | /* Set the time server to retrieve time with RFC868 */ 63 | extern 64 | void rfc868_timeserver_set(in_addr_t server); 65 | 66 | /* Get the time server used to retrieve time with RFC868 */ 67 | extern 68 | in_addr_t rfc868_timeserver_get(void); 69 | 70 | /* Get time from a RFC4330 SNTP server. 71 | * The time is written in the timespec structure pointed by ts parameter. 72 | * Returns 0 if successful, -1 if some error happened. 73 | */ 74 | extern 75 | int sntp_gettime(struct timespec *ts); 76 | 77 | /* Set the time server to retrieve time with RFC4330 */ 78 | extern 79 | void sntp_timeserver_set(in_addr_t server); 80 | 81 | /* Get the time server used to retrieve time with RFC4330 */ 82 | extern 83 | in_addr_t sntp_timeserver_get(void); 84 | 85 | 86 | #endif /* TIMESYNC_H */ 87 | 88 | -------------------------------------------------------------------------------- /tests/usart/usart.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | static void delay_loop(int32_t loops) 27 | { 28 | while(loops > 0) 29 | { 30 | asm("nop"); 31 | loops--; 32 | } 33 | } 34 | 35 | static void usart_puts(uint32_t usart, const char *s) 36 | { 37 | while(*s != '\0') 38 | { 39 | usart_send_blocking(usart, *s); 40 | s++; 41 | } 42 | } 43 | 44 | int main(void) 45 | { 46 | int32_t i; 47 | 48 | rcc_periph_clock_enable(RCC_GPIOA); 49 | gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO5); 50 | 51 | flash_set_ws(FLASH_ACR_LATENCY_2WS); 52 | rcc_set_adcpre(RCC_CFGR_ADCPRE_PCLK2_DIV4); /* 14MHz */ 53 | rcc_set_hpre(RCC_CFGR_HPRE_SYSCLK_NODIV); /* 56MHz */ 54 | rcc_set_ppre1(RCC_CFGR_PPRE1_HCLK_DIV2); /* 28MHz */ 55 | rcc_set_ppre2(RCC_CFGR_PPRE2_HCLK_NODIV); /* 56MHz */ 56 | rcc_set_pll_multiplication_factor(RCC_CFGR_PLLMUL_PLL_CLK_MUL14); /* 8MHz/2 x14 = 56MHz */ 57 | rcc_set_pll_source(RCC_CFGR_PLLSRC_HSI_CLK_DIV2); 58 | rcc_osc_on(PLL); 59 | rcc_wait_for_osc_ready(PLL); 60 | rcc_set_sysclk_source(RCC_CFGR_SW_SYSCLKSEL_PLLCLK); 61 | 62 | rcc_periph_clock_enable(RCC_USART2); 63 | gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX); 64 | gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_USART2_RX); 65 | 66 | uint32_t baud = 57600; 67 | uint32_t clock = 28000000; 68 | USART2_BRR = ((2 * clock) + baud) / (2 * baud); 69 | 70 | usart_set_databits(USART2, 8); 71 | usart_set_stopbits(USART2, USART_STOPBITS_1); 72 | usart_set_mode(USART2, USART_MODE_TX_RX); 73 | usart_set_parity(USART2, USART_PARITY_NONE); 74 | usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); 75 | usart_enable(USART2); 76 | 77 | usart_puts(USART2, "ready to receive.\r\n"); 78 | gpio_set(GPIOA, GPIO5); 79 | while(1) 80 | { 81 | uint16_t c; 82 | c = usart_recv_blocking(USART2); 83 | usart_send_blocking(USART2, toupper(c)); 84 | gpio_toggle(GPIOA, GPIO5); 85 | } 86 | } 87 | 88 | -------------------------------------------------------------------------------- /include/netinet/in.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #ifndef NETINET_IN_H 20 | #define NETINET_IN_H 21 | 22 | /* http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/netinet_in.h.html */ 23 | 24 | #include 25 | 26 | typedef uint16_t in_port_t; 27 | 28 | typedef uint32_t in_addr_t; 29 | 30 | struct in_addr { 31 | in_addr_t s_addr; 32 | }; 33 | 34 | #include 35 | #include 36 | 37 | struct sockaddr_in { 38 | sa_family_t sin_family; /* AF_INET. */ 39 | in_port_t sin_port; /* Port number. */ 40 | struct in_addr sin_addr; /* IP address. */ 41 | }; 42 | 43 | struct in6_addr { 44 | uint8_t s6_addr[16]; 45 | }; 46 | 47 | struct sockaddr_in6 { 48 | sa_family_t sin6_family; /* AF_INET6. */ 49 | in_port_t sin6_port; /* Port number. */ 50 | uint32_t sin6_flowinfo; /* IPv6 traffic class and flow information. */ 51 | struct in6_addr sin6_addr; /* IPv6 address. */ 52 | uint32_t sin6_scope_id; /* Set of interfaces for a scope. */ 53 | }; 54 | 55 | #define IPPROTO_IP 1 /* Internet protocol. */ 56 | #define IPPROTO_IPV6 2 /* [IP6] Internet Protocol Version 6. */ 57 | #define IPPROTO_ICMP 3 /* Control message protocol. */ 58 | #define IPPROTO_RAW 4 /* [RS] Raw IP Packets Protocol. */ 59 | #define IPPROTO_TCP 5 /* Transmission control protocol. */ 60 | #define IPPROTO_UDP 6 /* User datagram protocol. */ 61 | 62 | #define INADDR_ANY ((in_addr_t) 0x00000000) /* IPv4 local host address. */ 63 | #define INADDR_BROADCAST ((in_addr_t) 0xffffffff) /* IPv4 broadcast address */ 64 | 65 | #define INET_ADDRSTRLEN 16 /* Length of the string form for IP. */ 66 | #define INET6_ADDRSTRLEN 46 /* Length of the string form for IPv6. */ 67 | 68 | #define IPV6_JOIN_GROUP 1 /* Join a multicast group. */ 69 | #define IPV6_LEAVE_GROUP 2 /* Quit a multicast group. */ 70 | #define IPV6_MULTICAST_HOPS 3 /* Multicast hop limit. */ 71 | #define IPV6_MULTICAST_IF 4 /* Interface to use for outgoing multicast packets. */ 72 | #define IPV6_MULTICAST_LOOP 5 /* Multicast packets are delivered back to the local application. */ 73 | #define IPV6_UNICAST_HOPS 6 /* Unicast hop limit. */ 74 | #define IPV6_V6ONLY 7 /* Restrict AF_INET6 socket to IPv6 communications only. */ 75 | 76 | /* Missing IPv6 stuff */ 77 | 78 | #endif /* NETINET_IN_H */ 79 | 80 | -------------------------------------------------------------------------------- /tests/timers/timers_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include 21 | #include 22 | 23 | volatile int alarm_cnt; 24 | 25 | static 26 | void alarm_handler(int signo) 27 | { 28 | printf("Alarm! %d\n", signo); 29 | alarm_cnt++; 30 | } 31 | 32 | int main(void) 33 | { 34 | struct sigevent ev; 35 | timer_t timerid; 36 | struct itimerspec timspec; 37 | struct sigaction act; 38 | 39 | ev.sigev_notify = SIGEV_SIGNAL; 40 | ev.sigev_signo = SIGALRM; 41 | ev.sigev_value.sival_int = 0; 42 | 43 | if (timer_create( 44 | CLOCK_REALTIME, 45 | &ev, 46 | &timerid) != 0) 47 | { 48 | perror("timer_create"); 49 | return 1; 50 | } 51 | 52 | act.sa_handler = alarm_handler; 53 | sigemptyset(&act.sa_mask); 54 | act.sa_flags = 0; 55 | if (sigaction( 56 | SIGALRM, 57 | &act, 58 | NULL)!= 0) 59 | { 60 | perror("timer_create"); 61 | return 1; 62 | } 63 | 64 | timspec.it_interval.tv_sec = 1; 65 | timspec.it_interval.tv_nsec = 0; 66 | timspec.it_value.tv_sec = 2; 67 | timspec.it_value.tv_nsec = 0; 68 | 69 | if (timer_settime( 70 | timerid, 71 | 0, 72 | &timspec, 73 | NULL) != 0) 74 | { 75 | perror("timer_settime"); 76 | return 1; 77 | } 78 | 79 | while(1) 80 | { 81 | struct timespec t; 82 | 83 | clock_gettime(CLOCK_MONOTONIC, &t); 84 | printf("%lds %ldns\n", (long)t.tv_sec, (long)t.tv_nsec); 85 | 86 | t.tv_sec = 0; 87 | t.tv_nsec = 500*1000*1000; 88 | nanosleep(&t, NULL); 89 | 90 | if (timer_gettime( 91 | timerid, 92 | &timspec) != 0) 93 | { 94 | perror("timer_gettime"); 95 | return 1; 96 | } 97 | printf("%lds %ldns\n", (long)timspec.it_value.tv_sec, (long)timspec.it_value.tv_nsec); 98 | if (alarm_cnt > 3) 99 | { 100 | break; 101 | } 102 | } 103 | 104 | if (timer_delete(timerid) != 0) 105 | { 106 | perror("timer_delete"); 107 | } 108 | } 109 | 110 | 111 | -------------------------------------------------------------------------------- /tests/fatfs_rw/fatfs_rw_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include 21 | #include 22 | #include "ff.h" 23 | 24 | DWORD get_fattime (void) 25 | { 26 | return ( 27 | ((2015-1980)<<25) 28 | | 29 | (1<<21) 30 | | 31 | (1<<16) 32 | ); 33 | } 34 | 35 | static 36 | void wait_enter(void) 37 | { 38 | int c; 39 | 40 | do { 41 | c = getchar(); 42 | } while ((c != '\n') && (c != '\r')); 43 | } 44 | 45 | int main(void) 46 | { 47 | FRESULT result; 48 | FATFS fs; 49 | FIL fp; 50 | const char *filepath = "/rwtest.txt"; 51 | 52 | printf( 53 | "fatfs_ro_test\n" 54 | "Press Enter to continue...\n"); 55 | wait_enter(); 56 | 57 | result = f_mount(&fs, "/", 1); 58 | printf("f_mount: %d\n", result); 59 | if (result == FR_OK) 60 | { 61 | result = f_open(&fp, filepath, FA_WRITE|FA_CREATE_ALWAYS); 62 | printf("f_open: %d\n", result); 63 | } 64 | if (result == FR_OK) 65 | { 66 | char message[80]; 67 | UINT towrite; 68 | UINT bw; 69 | 70 | printf("Write a message:\n"); 71 | scanf("%s", message); 72 | towrite = strlen(message) + 1; 73 | result = f_write(&fp, message, towrite, &bw); 74 | printf("f_write: %d\n", result); 75 | if (bw != towrite) 76 | { 77 | printf("f_write did write only %u/%u bytes.\n", bw, towrite); 78 | } 79 | } 80 | if (result == FR_OK) 81 | { 82 | result = f_close(&fp); 83 | printf("f_close: %d\n", result); 84 | } 85 | 86 | if (result == FR_OK) 87 | { 88 | result = f_open(&fp, filepath, FA_READ); 89 | printf("result: %d\n", result); 90 | } 91 | if (result == FR_OK) 92 | { 93 | uint8_t data[64]; 94 | UINT nread; 95 | 96 | do { 97 | 98 | result = f_read(&fp, data, sizeof(data), &nread); 99 | fwrite(data, nread, 1, stdout); 100 | } while((result == FR_OK) && (nread == sizeof(data))); 101 | printf("\n"); 102 | if (result != FR_OK) 103 | { 104 | printf("result: %d\n", result); 105 | } 106 | } 107 | if (result == FR_OK) 108 | { 109 | result = f_close(&fp); 110 | printf("f_close: %d\n", result); 111 | } 112 | 113 | return 0; 114 | } 115 | 116 | -------------------------------------------------------------------------------- /src/timesync.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include "timesync.h" 20 | #include 21 | #include "timespec.h" 22 | 23 | #define TIMESYNC_METHOD_RFC868 1 24 | #define TIMESYNC_METHOD_SNTP 2 25 | 26 | #ifndef TIMESYNC_METHOD 27 | # define TIMESYNC_METHOD TIMESYNC_METHOD_SNTP 28 | #endif 29 | 30 | #if (TIMESYNC_METHOD != TIMESYNC_METHOD_RFC868) && (TIMESYNC_METHOD != TIMESYNC_METHOD_SNTP) 31 | /* TODO: NTP */ 32 | # error Supported TIMESYNC_METHOD values: TIMESYNC_METHOD_RFC868 TIMESYNC_METHOD_SNTP 33 | #endif 34 | 35 | #ifndef TIMESYNC_INTERVAL 36 | # define TIMESYNC_INTERVAL (60*60*24) /* 1 day */ 37 | #endif 38 | 39 | static 40 | struct timespec next_sync = {0, 0}; 41 | 42 | static 43 | struct timespec sync_interval = { 44 | .tv_sec = TIMESYNC_INTERVAL, 45 | .tv_nsec = 0}; 46 | 47 | static 48 | int time_to_sync(const struct timespec *now) 49 | { 50 | int res; 51 | 52 | if (now == NULL) 53 | { 54 | res = 1; /* assuming TIMESPEC_ZERO */ 55 | } 56 | else 57 | { 58 | res = (timespec_diff(now, &next_sync, NULL) >= 0)?1:0; 59 | } 60 | 61 | return res; 62 | } 63 | 64 | int timesync_now_timespec(struct timespec *out) 65 | { 66 | int res; 67 | int gettime_ret; 68 | struct timespec now; 69 | 70 | #if (TIMESYNC_METHOD == TIMESYNC_METHOD_RFC868) 71 | gettime_ret = rfc868_gettime(&now); 72 | #elif (TIMESYNC_METHOD == TIMESYNC_METHOD_SNTP) 73 | gettime_ret = sntp_gettime(&now); 74 | #endif 75 | 76 | if (gettime_ret == 0) 77 | { 78 | int clk_ret; 79 | 80 | clk_ret = clock_settime(CLOCK_REALTIME, &now); 81 | if (clk_ret == 0) 82 | { 83 | if (out != NULL) 84 | { 85 | *out = now; 86 | } 87 | timespec_add(&now, &sync_interval, &next_sync); 88 | res = 1; 89 | } 90 | else 91 | { 92 | res = -1; 93 | } 94 | } 95 | else 96 | { 97 | res = -1; 98 | } 99 | return res; 100 | } 101 | 102 | int timesync_timespec(struct timespec *now) 103 | { 104 | int res; 105 | 106 | if (time_to_sync(now)) 107 | { 108 | res = timesync_now_timespec(now); 109 | } 110 | else 111 | { 112 | res = 0; 113 | } 114 | 115 | return res; 116 | } 117 | 118 | int timesync(void) 119 | { 120 | struct timespec now; 121 | 122 | clock_gettime(CLOCK_REALTIME, &now); 123 | return timesync_timespec(&now); 124 | } 125 | 126 | int timesync_now(void) 127 | { 128 | return timesync_now_timespec(NULL); 129 | } 130 | 131 | -------------------------------------------------------------------------------- /tests/socket_errors/socket_errors.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Trying different socket errors. 3 | */ 4 | #include //printf 5 | #include //strlen 6 | #include //close 7 | #include //socket 8 | #include //inet_addr 9 | #include 10 | #include 11 | 12 | #ifndef SERVER_IP_ADDR 13 | # define SERVER_IP_ADDR "192.168.1.173" 14 | #endif 15 | 16 | #ifndef SERVER_PORT 17 | # define SERVER_PORT 8888 18 | #endif 19 | 20 | int assertions_failed = 0; 21 | 22 | #define assert_equal(x, y) do { \ 23 | if (x != y) { \ 24 | fprintf(stderr, "%s:%d: %s: " #x " != " #y ": %d != %d\n", __FILE__, __LINE__, __func__, x, y); \ 25 | assertions_failed++; \ 26 | }\ 27 | } while(0); 28 | 29 | int main(void) 30 | { 31 | int sock; 32 | int ret; 33 | struct sockaddr_in server; 34 | 35 | sock = socket(AF_INET , SOCK_STREAM , 1); 36 | assert_equal(sock, -1); 37 | assert_equal(errno, EPROTONOSUPPORT); 38 | 39 | errno = 0; 40 | sock = socket(0xFFFF , SOCK_STREAM , 0); 41 | assert_equal(sock, -1); 42 | assert_equal(errno, EAFNOSUPPORT); 43 | 44 | errno = 0; 45 | sock = socket(AF_INET , 0xFFFF , 0); 46 | assert_equal(sock, -1); 47 | assert_equal(errno, EPROTOTYPE); 48 | 49 | errno = 0; 50 | sock = socket(AF_INET , SOCK_STREAM , 0); 51 | assert(sock != -1); 52 | assert_equal(errno, 0); 53 | 54 | server.sin_addr.s_addr = inet_addr(SERVER_IP_ADDR); 55 | server.sin_family = AF_INET; 56 | server.sin_port = htons( SERVER_PORT ); 57 | 58 | errno = 0; 59 | ret = connect(0xFFFF, (struct sockaddr *)&server, sizeof(server)); 60 | assert_equal(ret, -1); 61 | assert_equal(errno, EBADF); 62 | 63 | errno = 0; 64 | ret = connect(STDIN_FILENO, (struct sockaddr *)&server, sizeof(server)); 65 | assert_equal(ret, -1); 66 | assert_equal(errno, ENOTSOCK); 67 | 68 | server.sin_family = 0xFFFF; 69 | errno = 0; 70 | ret = connect(sock, (struct sockaddr *)&server, sizeof(server)); 71 | assert_equal(ret, -1); 72 | assert_equal(errno, EAFNOSUPPORT); 73 | 74 | server.sin_addr.s_addr = inet_addr("0.0.0.0"); 75 | server.sin_family = AF_INET; 76 | server.sin_port = htons( SERVER_PORT ); 77 | errno = 0; 78 | ret = connect(sock, (struct sockaddr *)&server, sizeof(server)); 79 | assert_equal(ret, -1); 80 | assert_equal(errno, ECONNREFUSED); 81 | 82 | errno = 0; 83 | ret = close(0xFFFF); 84 | assert_equal(ret, -1); 85 | assert_equal(errno, EBADF); 86 | 87 | errno = 0; 88 | ret = close(sock); 89 | assert_equal(ret, 0); 90 | assert_equal(errno, 0); 91 | 92 | errno = 0; 93 | ret = close(sock); 94 | assert_equal(ret, -1); 95 | assert_equal(errno, EBADF); 96 | 97 | server.sin_addr.s_addr = inet_addr(SERVER_IP_ADDR); 98 | server.sin_family = AF_INET; 99 | server.sin_port = htons( SERVER_PORT ); 100 | errno = 0; 101 | ret = connect(sock, (struct sockaddr *)&server, sizeof(server)); 102 | assert_equal(ret, -1); 103 | assert_equal(errno, EBADF); 104 | 105 | if (assertions_failed) 106 | { 107 | printf("%d assertions failed.\n", assertions_failed); 108 | return 1; 109 | } 110 | else 111 | { 112 | puts("All tests OK"); 113 | return 0; 114 | } 115 | } 116 | 117 | -------------------------------------------------------------------------------- /include/dhcp_client.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | 20 | #ifndef DHCP_CLIENT_H 21 | #define DHCP_CLIENT_H 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | #define DHCP_MAC_ADDR_LEN 6 28 | 29 | enum dhcp_state { 30 | DHCP_INIT, 31 | DHCP_SELECTING, 32 | DHCP_REQUESTING, 33 | DHCP_BOUND, 34 | DHCP_INIT_REBOOT, 35 | DHCP_REBOOTING, 36 | DHCP_RENEWING, 37 | DHCP_REBINDING, 38 | }; 39 | 40 | struct dhcp_binding { 41 | enum dhcp_state state; /**< DHCP state. */ 42 | uint8_t mac_addr[DHCP_MAC_ADDR_LEN]; /* Client MAC address. */ 43 | in_addr_t client; /**< New client IP address. */ 44 | in_addr_t dhcp_server; /**< Server that issued the client IP address. */ 45 | in_addr_t gateway; /**< Gateway IP address. */ 46 | in_addr_t subnet; /**< Subnet IP mask. */ 47 | in_addr_t dns_server; /**< DNS server address. */ 48 | uint32_t xid; /**< Transaction ID */ 49 | struct timespec lease_t1; /**< Client IP address lease expiring. */ 50 | struct timespec lease_t2; /**< Client IP address lease end. */ 51 | }; 52 | 53 | /** 54 | * Initialize DHCP client library. 55 | * 56 | * Other functions shall not be called before this. 57 | * 58 | * \param[in] mac_addr 59 | * \param[out] binding 60 | */ 61 | extern 62 | void dhcp_init(const uint8_t *mac_addr, struct dhcp_binding *binding); 63 | 64 | /** 65 | * Perform a step of DHCP procedure. 66 | * 67 | * \param[inout] binding 68 | * 69 | * \retval < 0 an error occurred. 70 | * \retval == 0 should be re-called as soon as possible. 71 | * \retval > 0 the number of seconds after which it should be called again. 72 | */ 73 | extern 74 | time_t dhcp_step(struct dhcp_binding *binding); 75 | 76 | /** 77 | * Get whether client is bound to an address with a DHCP server. 78 | * 79 | * \param[inout] binding 80 | * 81 | * \retval nonzero the interface is bound to an address. 82 | * \retval zero the interface is not bound to an address. 83 | */ 84 | extern 85 | int dhcp_isbound(struct dhcp_binding *binding); 86 | 87 | /** 88 | * Perform DHCP procedure blocking until client is bound to an address. 89 | * 90 | * \param[inout] binding 91 | * 92 | * \return the number of seconds after which it should be called again. 93 | */ 94 | extern 95 | time_t dhcp_bind(struct dhcp_binding *binding); 96 | 97 | #define DHCP_EINTERNAL (-1) 98 | #define DHCP_ESYSCALL (-2) 99 | #define DHCP_EYIADDR (-3) 100 | #define DHCP_EOFFEREXPECTED (-4) 101 | #define DHCP_ENOSERVERID (-5) 102 | #define DHCP_ENOGATEWAY (-6) 103 | #define DHCP_ENOSUBNET (-7) 104 | #define DHCP_EAGAIN (-8) 105 | 106 | #endif /* DHCP_CLIENT_H */ 107 | 108 | -------------------------------------------------------------------------------- /src/clock_gettime_dummy.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include 21 | #include "timespec.h" 22 | 23 | #ifndef CLOCK_GETTIME_DUMMY_STEP_SECS 24 | # define CLOCK_GETTIME_DUMMY_STEP_SECS 0 25 | #endif 26 | 27 | #ifndef CLOCK_GETTIME_DUMMY_STEP_NSECS 28 | # define CLOCK_GETTIME_DUMMY_STEP_NSECS 1000 29 | #endif 30 | 31 | static struct timespec realtime; 32 | 33 | static struct timespec monotonic; 34 | 35 | static const struct timespec dummy_step = { 36 | .tv_sec = CLOCK_GETTIME_DUMMY_STEP_SECS, 37 | .tv_nsec = CLOCK_GETTIME_DUMMY_STEP_NSECS, 38 | }; 39 | 40 | static 41 | struct timespec *clock_get(clockid_t clock_id) 42 | { 43 | struct timespec *clk; 44 | 45 | switch(clock_id) 46 | { 47 | case CLOCK_MONOTONIC: 48 | clk = &monotonic; 49 | break; 50 | case CLOCK_REALTIME: 51 | clk = &realtime; 52 | break; 53 | default: 54 | clk = NULL; 55 | break; 56 | } 57 | return clk; 58 | } 59 | 60 | static 61 | void clock_dummy_advance(clockid_t clock_id) 62 | { 63 | timespec_incr(clock_get(clock_id), &dummy_step); 64 | } 65 | 66 | static 67 | void clock_dummy_advance_all(void) 68 | { 69 | clock_dummy_advance(CLOCK_MONOTONIC); 70 | clock_dummy_advance(CLOCK_REALTIME); 71 | } 72 | 73 | int clock_gettime(clockid_t clock_id, struct timespec *tp) 74 | { 75 | int ret; 76 | struct timespec *clk; 77 | 78 | clock_dummy_advance_all(); 79 | 80 | clk = clock_get(clock_id); 81 | if (clk == NULL) 82 | { 83 | ret = -1; 84 | errno = EINVAL; 85 | } 86 | else 87 | { 88 | *tp = *clk; 89 | ret = 0; 90 | } 91 | return ret; 92 | } 93 | 94 | int clock_settime(clockid_t clock_id, const struct timespec *tp) 95 | { 96 | int ret; 97 | struct timespec *clk; 98 | 99 | clk = clock_get(clock_id); 100 | if (clk == NULL) 101 | { 102 | ret = EINVAL; 103 | } 104 | else if (clock_id == CLOCK_MONOTONIC) 105 | { 106 | ret = EINVAL; 107 | } 108 | else if (tp == NULL) 109 | { 110 | ret = EINVAL; 111 | } 112 | else if (tp->tv_nsec < 0) 113 | { 114 | ret = EINVAL; 115 | } 116 | else if (tp->tv_nsec >= NSECS_IN_SEC) 117 | { 118 | ret = EINVAL; 119 | } 120 | else 121 | { 122 | *clk = *tp; 123 | ret = 0; 124 | } 125 | 126 | return ret; 127 | } 128 | 129 | int clock_getres(clockid_t clock_id, struct timespec *res) 130 | { 131 | int ret; 132 | 133 | if (clock_get(clock_id) == NULL) 134 | { 135 | ret = EINVAL; 136 | } 137 | else 138 | { 139 | ret = 0; 140 | if (res != NULL) 141 | { 142 | *res = dummy_step; 143 | } 144 | } 145 | return ret; 146 | } 147 | 148 | -------------------------------------------------------------------------------- /tests/sd_spi/sd_spi_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include 21 | #include "sd_spi.h" 22 | 23 | static 24 | void print_resp(uint8_t cmd, void *resp, size_t len) 25 | { 26 | size_t i; 27 | uint8_t *resp_bytes; 28 | 29 | printf("CMD%d: ", cmd); 30 | resp_bytes = resp; 31 | for (i = 0; i < len; i++) 32 | { 33 | printf("%02X", (unsigned)resp_bytes[i]); 34 | } 35 | printf("\n"); 36 | } 37 | 38 | int main(void) 39 | { 40 | uint8_t r1; 41 | uint8_t r7[5]; 42 | uint8_t r3[5]; 43 | uint8_t r2[2]; 44 | uint32_t arg_hcs; 45 | uint8_t block[512]; 46 | int tries; 47 | int read_res; 48 | 49 | 50 | printf("SD card SPI initialization..."); 51 | getchar(); 52 | printf("\n"); 53 | 54 | sd_init(); 55 | 56 | tries = 4; 57 | do { 58 | r1 = sd_send_command_r1(0, 0); 59 | print_resp(0, &r1, 1); 60 | if (r1 != 0x01) 61 | { 62 | fprintf(stderr, "state not idle\n"); 63 | } 64 | tries--; 65 | if (tries <= 0) 66 | { 67 | fprintf(stderr, "giving up\n"); 68 | return 1; 69 | } 70 | } while (r1 != 0x01); 71 | 72 | sd_send_command(8, 0x1AA, r7, sizeof(r7)); 73 | print_resp(8, r7, sizeof(r7)); 74 | 75 | if (r7[0] != 0x01) 76 | { 77 | fprintf(stderr, "state not idle\n"); 78 | return 1; 79 | } 80 | else if ((r7[3]&0x0F) != 0x01) 81 | { 82 | fprintf(stderr, "non supported voltage range\n"); 83 | return 1; 84 | } 85 | else if (r7[4] != 0xAA) 86 | { 87 | fprintf(stderr, "check pattern error\n"); 88 | return 1; 89 | } 90 | 91 | sd_send_command(58, 0, r3, sizeof(r3)); 92 | print_resp(58, r3, sizeof(r3)); 93 | 94 | arg_hcs = 0x40000000; 95 | do 96 | { 97 | r1 = sd_send_command_r1(55, 0); 98 | r1 = sd_send_command_r1(41, arg_hcs); 99 | } while(r1 & 0x01); 100 | print_resp(41, &r1, 1); 101 | 102 | sd_send_command(58, 0, r3, sizeof(r3)); 103 | print_resp(58, r3, sizeof(r3)); 104 | if (r3[1] & 0x40) 105 | { 106 | printf("High capacity\n"); 107 | } 108 | else 109 | { 110 | printf("Standard capacity\n"); 111 | } 112 | 113 | //sd_send_command(13, 0, r2, sizeof(r2)); 114 | //print_resp(13, r2, sizeof(r2)); 115 | 116 | read_res = sd_read_single_block(0, block); 117 | if (read_res == 0) 118 | { 119 | print_resp(17, block, sizeof(block)); 120 | } 121 | else 122 | { 123 | print_resp(17, &read_res, sizeof(read_res)); 124 | } 125 | 126 | sd_send_command(13, 0, r2, sizeof(r2)); 127 | print_resp(13, r2, sizeof(r2)); 128 | 129 | sd_send_command(13, 0, r2, sizeof(r2)); 130 | print_resp(13, r2, sizeof(r2)); 131 | 132 | return 0; 133 | } 134 | 135 | -------------------------------------------------------------------------------- /tests/fatfs_stdio/fatfs_stdio.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include 21 | #include 22 | 23 | static 24 | void wait_enter(void) 25 | { 26 | int c; 27 | 28 | do { 29 | c = getchar(); 30 | } while ((c != '\n') && (c != '\r')); 31 | } 32 | 33 | int main(void) 34 | { 35 | const char *filepath = "rwtest.txt"; 36 | const char *filepath2 = "rwtest2.txt"; 37 | FILE *fout; 38 | FILE *fin; 39 | char message[80]; 40 | int result; 41 | 42 | printf( 43 | "fatfs_stdio\n" 44 | "Press Enter to continue...\n"); 45 | wait_enter(); 46 | 47 | printf("Write a message:\n"); 48 | scanf("%s", message); 49 | printf("Writing message to %s...\n", filepath); 50 | 51 | fout = fopen(filepath, "w"); 52 | if (fout == NULL) 53 | { 54 | perror(filepath); 55 | return 1; 56 | } 57 | else 58 | { 59 | result = fputs(message, fout); 60 | if (result < 0) 61 | { 62 | perror(filepath); 63 | return 1; 64 | } 65 | } 66 | result = fflush(fout); 67 | if (result != 0) 68 | { 69 | perror(filepath); 70 | return 1; 71 | } 72 | printf("\nftell = %ld.\n", ftell(fout)); 73 | result = fclose(fout); 74 | if (result != 0) 75 | { 76 | perror(filepath); 77 | return 1; 78 | } 79 | printf("Done.\n" 80 | "Reading message from %s...\n", filepath); 81 | 82 | fin = fopen(filepath, "r"); 83 | if (fout == NULL) 84 | { 85 | perror(filepath); 86 | return 1; 87 | } 88 | else 89 | { 90 | while (!feof(fin)) 91 | { 92 | size_t nbytes; 93 | char *p; 94 | 95 | nbytes = fread(message, 1, sizeof(message), fin); 96 | if(ferror(fin)) 97 | { 98 | perror(filepath); 99 | fclose(fin); 100 | return 1; 101 | } 102 | p = message; 103 | while (nbytes > 0) 104 | { 105 | size_t nwritten; 106 | nwritten = fwrite(p, 1, nbytes, stdout); 107 | if (nwritten > 0) 108 | { 109 | nbytes -= nwritten; 110 | p += nwritten; 111 | } 112 | } 113 | } 114 | } 115 | printf("\nftell = %ld.\n", ftell(fin)); 116 | result = fclose(fin); 117 | if (result != 0) 118 | { 119 | perror(filepath); 120 | return 1; 121 | } 122 | printf( 123 | "Done.\n" 124 | "Renaming file...\n"); 125 | result = rename(filepath, filepath2); 126 | if (result != 0) 127 | { 128 | perror(filepath2); 129 | return 1; 130 | } 131 | printf( 132 | "Done.\n" 133 | "Removing file...\n"); 134 | 135 | result = remove(filepath2); 136 | if (result != 0) 137 | { 138 | perror(filepath2); 139 | return 1; 140 | } 141 | printf("Done.\n"); 142 | 143 | return 0; 144 | } 145 | 146 | -------------------------------------------------------------------------------- /src/poll.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include 21 | #include "file.h" 22 | #include "time.h" 23 | #include "timespec.h" 24 | 25 | static 26 | short poll_one(struct pollfd *p) 27 | { 28 | short revents; 29 | 30 | if (p->fd < 0) 31 | { 32 | revents = 0; 33 | } 34 | else 35 | { 36 | struct fd *f; 37 | 38 | f = file_struct_get(p->fd); 39 | if (f == NULL) 40 | { 41 | revents = POLLNVAL; 42 | } 43 | else if (f->poll == NULL) 44 | { 45 | revents = POLLNVAL; 46 | } 47 | else 48 | { 49 | revents = f->poll(p->fd); 50 | if (revents == -1) 51 | { 52 | revents = POLLNVAL; 53 | } 54 | } 55 | } 56 | if (revents >= 0) 57 | { 58 | short events; 59 | 60 | events = p->events; 61 | events |= POLLHUP|POLLERR|POLLNVAL; 62 | revents &= events; 63 | p->revents = revents; 64 | } 65 | 66 | return revents; 67 | } 68 | 69 | static 70 | int poll_tentative(struct pollfd fds[], nfds_t nfds) 71 | { 72 | int ret; 73 | nfds_t i; 74 | int nfiles_ready; 75 | 76 | nfiles_ready = 0; 77 | 78 | for (i = 0; i < nfds; i++) 79 | { 80 | short revents; 81 | 82 | revents = poll_one(&fds[i]); 83 | if (revents == -1) 84 | { 85 | break; 86 | } 87 | else if (revents > 0) 88 | { 89 | nfiles_ready++; 90 | } 91 | } 92 | if (i == nfds) 93 | { 94 | ret = nfiles_ready; 95 | } 96 | else 97 | { 98 | ret = -1; 99 | } 100 | 101 | return ret; 102 | } 103 | 104 | int poll(struct pollfd fds[], nfds_t nfds, int timeout) 105 | { 106 | int ret; 107 | 108 | if (timeout == 0) 109 | { 110 | ret = poll_tentative(fds, nfds); 111 | } 112 | else 113 | { 114 | struct timespec tend; 115 | int timeout_expired; 116 | 117 | if (timeout == -1) 118 | { 119 | tend = TIMESPEC_INFINITY; 120 | } 121 | else 122 | { 123 | struct timespec tcurrent; 124 | struct timespec timeout_ts; 125 | 126 | timeout_ts.tv_sec = timeout / MSECS_IN_SEC; 127 | timeout -= timeout_ts.tv_sec * MSECS_IN_SEC; 128 | timeout_ts.tv_nsec = timeout * (NSECS_IN_SEC / MSECS_IN_SEC); 129 | 130 | ret = clock_gettime(CLOCK_MONOTONIC, &tcurrent); 131 | 132 | timespec_add(&tcurrent, &timeout_ts, &tend); 133 | } 134 | do 135 | { 136 | struct timespec tcurrent; 137 | 138 | ret = clock_gettime(CLOCK_MONOTONIC, &tcurrent); 139 | if (ret != 0) 140 | { 141 | break; 142 | } 143 | 144 | ret = poll_tentative(fds, nfds); 145 | if (ret != 0) 146 | { 147 | break; 148 | } 149 | 150 | timeout_expired = (timespec_diff(&tcurrent, &tend, NULL) >= 0); 151 | } while(!timeout_expired); 152 | 153 | } 154 | 155 | return ret; 156 | } 157 | 158 | -------------------------------------------------------------------------------- /tests/wolfssl/wolfssl.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | static 12 | void ssl_print_error(WOLFSSL * ssl, int ret) 13 | { 14 | int err; 15 | char err_str[80]; 16 | 17 | err = wolfSSL_get_error(ssl, ret); 18 | wolfSSL_ERR_error_string(err, err_str); 19 | fprintf(stderr, "err = %d, %s\n", err, err_str); 20 | } 21 | 22 | int main(void) 23 | { 24 | struct addrinfo hints; 25 | struct addrinfo *ai; 26 | struct sockaddr_in server; 27 | int sock; 28 | int res; 29 | WOLFSSL_CTX* ctx; 30 | WOLFSSL* ssl; 31 | char http_get[200] = 32 | "GET /index.html HTTP/1.1\r\n" 33 | "Host: www.eff.org\r\n" 34 | "\r\n"; 35 | char http_get_resp[200]; 36 | 37 | printf("Press any key to continue..."); 38 | getchar(); 39 | printf("\n"); 40 | 41 | hints.ai_flags = 0; 42 | hints.ai_family = AF_INET; 43 | hints.ai_socktype = SOCK_STREAM; 44 | hints.ai_protocol = 0; 45 | 46 | res = getaddrinfo("www.eff.org", NULL, &hints, &ai); 47 | if (res != 0) 48 | { 49 | if (res == EAI_SYSTEM) 50 | { 51 | perror("getaddrinfo"); 52 | } 53 | else 54 | { 55 | fprintf(stderr, "error: getaddrinfo: %d\n", res); 56 | } 57 | return 1; 58 | } 59 | if (ai == NULL) 60 | { 61 | fprintf(stderr, "error: getaddrinfo : output is NULL\n"); 62 | return 1; 63 | } 64 | 65 | sock = socket(AF_INET , SOCK_STREAM , 0); 66 | if (sock == -1) 67 | { 68 | perror("socket creation failed"); 69 | return 1; 70 | } 71 | #if 0 72 | server = *((const struct sockaddr_in *)ai->ai_addr); 73 | server.sin_port = htons( 443 ); /* HTTPS */ 74 | #else 75 | /* 76 | * nslookup www.eff.org -> 69.50.225.155 77 | * socat TCP-LISTEN:44333 TCP:69.50.225.155:443 78 | */ 79 | server.sin_family = AF_INET; 80 | server.sin_port = htons(44333); 81 | server.sin_addr.s_addr = inet_addr("192.168.1.173"); /* my PC */ 82 | #endif 83 | freeaddrinfo(ai); 84 | if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0) 85 | { 86 | perror("connect failed"); 87 | close(sock); 88 | return 1; 89 | } 90 | wolfSSL_Init(); 91 | if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) { 92 | fprintf(stderr, "SSL_CTX_new error.\n"); 93 | close(sock); 94 | return 1; 95 | } 96 | wolfSSL_SetIOSend(ctx, EmbedSend); 97 | wolfSSL_SetIORecv(ctx, EmbedReceive); 98 | ssl = wolfSSL_new(ctx); 99 | if (ssl == NULL) 100 | { 101 | fprintf(stderr, "wolfSSL_new error.\n"); 102 | close(sock); 103 | return 1; 104 | } 105 | wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, NULL); 106 | wolfSSL_set_fd(ssl, sock); 107 | 108 | res = wolfSSL_connect(ssl); 109 | if (res != SSL_SUCCESS) 110 | { 111 | fprintf(stderr, "wolfSSL_connect error.\n"); 112 | ssl_print_error(ssl, res); 113 | close(sock); 114 | return 1; 115 | } 116 | 117 | res = wolfSSL_write(ssl, http_get, strlen(http_get)); 118 | if (res <= 0) 119 | { 120 | fprintf(stderr, "wolfSSL_write error.\n"); 121 | ssl_print_error(ssl, res); 122 | close(sock); 123 | return 1; 124 | } 125 | do 126 | { 127 | res = wolfSSL_read(ssl, http_get_resp, sizeof(http_get_resp)); 128 | if (res <= 0) 129 | { 130 | fprintf(stderr, "wolfSSL_read error.\n"); 131 | ssl_print_error(ssl, res); 132 | close(sock); 133 | return 1; 134 | } 135 | fwrite(http_get_resp, res, 1, stdout); 136 | } while(res == sizeof(http_get_resp)); //TODO: cleaner 137 | 138 | wolfSSL_free(ssl); 139 | wolfSSL_CTX_free(ctx); 140 | wolfSSL_Cleanup(); 141 | 142 | close(sock); 143 | 144 | return 0; 145 | } 146 | 147 | 148 | -------------------------------------------------------------------------------- /include/netdb.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #ifndef NETDB_H 20 | #define NETDB_H 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | struct hostent { 27 | char *h_name; 28 | char **h_aliases; 29 | int h_addrtype; 30 | int h_length; 31 | char **h_addr_list; 32 | }; 33 | 34 | struct netent { 35 | char *n_name; 36 | char **n_aliases; 37 | int n_addrtype; 38 | uint32_t n_net; 39 | }; 40 | 41 | struct protoent { 42 | char *p_name; 43 | char **p_aliases; 44 | int p_proto; 45 | }; 46 | 47 | struct servent { 48 | char *s_name; 49 | char **s_aliases; 50 | int s_port; 51 | char *s_proto; 52 | }; 53 | 54 | #define IPPORT_RESERVED 1024 55 | 56 | struct addrinfo { 57 | int ai_flags; 58 | int ai_family; 59 | int ai_socktype; 60 | int ai_protocol; 61 | socklen_t ai_addrlen; 62 | struct sockaddr *ai_addr; 63 | char *ai_canonname; 64 | struct addrinfo *ai_next; 65 | }; 66 | 67 | #define AI_PASSIVE 0x01 68 | #define AI_CANONNAME 0x02 69 | #define AI_NUMERICHOST 0x04 70 | #define AI_NUMERICSERV 0x08 71 | #define AI_V4MAPPED 0x10 72 | #define AI_ALL 0x20 73 | #define AI_ADDRCONFIG 0x40 74 | 75 | #define NI_NOFQDN 0x01 76 | #define NI_NUMERICHOST 0x02 77 | #define NI_NAMEREQD 0x04 78 | #define NI_NUMERICSERV 0x08 79 | #define NI_NUMERICSCOPE 0x10 80 | #define NI_DGRAM 0x20 81 | 82 | #define EAI_AGAIN 1 83 | #define EAI_BADFLAGS 2 84 | #define EAI_FAIL 3 85 | #define EAI_FAMILY 4 86 | #define EAI_MEMORY 5 87 | #define EAI_NONAME 6 88 | #define EAI_SERVICE 7 89 | #define EAI_SOCKTYPE 8 90 | #define EAI_SYSTEM 9 91 | 92 | void endhostent(void); 93 | 94 | void endnetent(void); 95 | 96 | void endprotoent(void); 97 | 98 | void endservent(void); 99 | 100 | void freeaddrinfo(struct addrinfo *); 101 | 102 | const char *gai_strerror(int); 103 | 104 | int getaddrinfo( 105 | const char *__restrict, 106 | const char *__restrict, 107 | const struct addrinfo *__restrict, 108 | struct addrinfo **__restrict); 109 | 110 | struct hostent *gethostent(void); 111 | 112 | int getnameinfo( 113 | const struct sockaddr *__restrict, 114 | socklen_t, 115 | char *__restrict, 116 | socklen_t, 117 | char *__restrict, 118 | socklen_t, 119 | int); 120 | 121 | struct netent *getnetbyaddr(uint32_t, int); 122 | 123 | struct netent *getnetbyname(const char *); 124 | 125 | struct netent *getnetent(void); 126 | 127 | struct protoent *getprotobyname(const char *); 128 | 129 | struct protoent *getprotobynumber(int); 130 | 131 | struct protoent *getprotoent(void); 132 | 133 | struct servent *getservbyname(const char *, const char *); 134 | 135 | struct servent *getservbyport(int, const char *); 136 | 137 | struct servent *getservent(void); 138 | 139 | void sethostent(int); 140 | 141 | void setnetent(int); 142 | 143 | void setprotoent(int); 144 | 145 | void setservent(int); 146 | 147 | /* Obsoleted by POSIX.1-2004, removed from POSIX.1-2008 148 | * but still needed by libcurl and others. 149 | */ 150 | 151 | struct hostent *gethostbyname(const char *); 152 | 153 | #endif /* NETDB_H */ 154 | 155 | -------------------------------------------------------------------------------- /tests/fatfs_unistd/fatfs_unistd.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | static 28 | void wait_enter(void) 29 | { 30 | int c; 31 | 32 | do { 33 | c = getchar(); 34 | } while ((c != '\n') && (c != '\r')); 35 | } 36 | 37 | int main(void) 38 | { 39 | const char *filepath = "rwtest.txt"; 40 | int fdout; 41 | int fdin; 42 | char message[80]; 43 | int result; 44 | struct stat s; 45 | 46 | printf( 47 | "fatfs_unistd\n" 48 | "Press Enter to continue...\n"); 49 | wait_enter(); 50 | 51 | printf("Write a message:\n"); 52 | scanf("%s", message); 53 | printf("Writing message to %s...\n", filepath); 54 | 55 | fdout = open(filepath, O_WRONLY|O_TRUNC|O_CREAT); 56 | if (fdout == -1) 57 | { 58 | perror(filepath); 59 | return 1; 60 | } 61 | else 62 | { 63 | result = write(fdout, message, strlen(message)); 64 | if (result < 0) 65 | { 66 | perror(filepath); 67 | return 1; 68 | } 69 | } 70 | result = fsync(fdout); 71 | if (result != 0) 72 | { 73 | perror(filepath); 74 | return 1; 75 | } 76 | printf("pos = %ld.\n", lseek(fdout, 0, SEEK_CUR)); 77 | result = close(fdout); 78 | if (result != 0) 79 | { 80 | perror(filepath); 81 | return 1; 82 | } 83 | printf("Done.\n" 84 | "Reading message from %s...\n", filepath); 85 | 86 | fdin = open(filepath, O_RDONLY); 87 | if (fdout == -1) 88 | { 89 | perror(filepath); 90 | return 1; 91 | } 92 | else 93 | { 94 | ssize_t nbytes; 95 | 96 | do { 97 | char *p; 98 | 99 | nbytes = read(fdin, message, sizeof(message)); 100 | if(nbytes == -1) 101 | { 102 | perror(filepath); 103 | close(fdin); 104 | return 1; 105 | } 106 | if (nbytes == 0) 107 | { 108 | /* EOF */ 109 | break; 110 | } 111 | p = message; 112 | while (nbytes > 0) 113 | { 114 | size_t nwritten; 115 | 116 | nwritten = fwrite(p, 1, nbytes, stdout); 117 | if (nwritten > 0) 118 | { 119 | nbytes -= nwritten; 120 | p += nwritten; 121 | } 122 | } 123 | } while(1); 124 | } 125 | printf("\npos = %ld.\n", lseek(fdin, 0, SEEK_CUR)); 126 | result = close(fdin); 127 | if (result != 0) 128 | { 129 | perror(filepath); 130 | return 1; 131 | } 132 | printf( 133 | "Done.\n" 134 | "Removing file...\n"); 135 | 136 | result = stat(filepath, &s); 137 | if (result != 0) 138 | { 139 | perror(filepath); 140 | return 1; 141 | } 142 | result = unlink(filepath); 143 | if (result != 0) 144 | { 145 | perror(filepath); 146 | return 1; 147 | } 148 | result = stat(filepath, &s); 149 | if (result != -1 || errno != ENOENT) 150 | { 151 | perror(filepath); 152 | return 1; 153 | } 154 | printf("Done.\n"); 155 | 156 | return 0; 157 | } 158 | 159 | -------------------------------------------------------------------------------- /tests/spi/spi.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | static 27 | void usart_init(void) 28 | { 29 | uint32_t baud = 57600; 30 | uint32_t clock = 8000000; 31 | 32 | rcc_periph_clock_enable(RCC_GPIOA); 33 | rcc_periph_clock_enable(RCC_USART2); 34 | 35 | gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX); 36 | gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_USART2_RX); 37 | USART2_BRR = ((2 * clock) + baud) / (2 * baud); 38 | 39 | usart_set_databits(USART2, 8); 40 | usart_set_stopbits(USART2, USART_STOPBITS_1); 41 | usart_set_mode(USART2, USART_MODE_TX_RX); 42 | usart_set_parity(USART2, USART_PARITY_NONE); 43 | usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); 44 | usart_enable(USART2); 45 | } 46 | 47 | static 48 | void usart_puts(const char *s) 49 | { 50 | while(*s != '\0') 51 | { 52 | usart_send_blocking(USART2, *s); 53 | s++; 54 | } 55 | } 56 | 57 | static 58 | void usart_printhex(uint8_t v) 59 | { 60 | char hex[3]; 61 | 62 | snprintf(hex, 3, "%02x", v); 63 | usart_puts(hex); 64 | } 65 | 66 | static 67 | void spi1_init(void) 68 | { 69 | rcc_periph_clock_enable(RCC_SPI1); 70 | rcc_periph_clock_enable(RCC_GPIOA); 71 | rcc_periph_clock_enable(RCC_GPIOB); 72 | 73 | /* CN5_6 D13 PA5 SPI1_SCK */ 74 | gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_SPI1_SCK); 75 | /* CN5_4 D11 PA7 SPI1_MOSI */ 76 | gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_SPI1_MOSI); 77 | /* CN5_5 D12 PA6 SPI1_MISO */ 78 | gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_SPI1_MISO); 79 | /* CN5_3 D10 PB6 SPI1_CS */ 80 | gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_10_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO6); 81 | gpio_set(GPIOB, GPIO6); 82 | 83 | /* Clock: 84 | * HSI 8MHz is the default 85 | * RCC_CFGR_SW = 0b00 -> HSI chosen as SYSCLK 86 | * RCC_CFGR_HPRE = 0b0000 -> no AHB prescaler 87 | * SPI1 is on APB2 88 | * RCC_CFGR_PRE2 = 0b0000 -> no APB2 prescaler 89 | * -> FPCLK = 8MHz 90 | * -> BR FPCLK/2 -> SCLK @ 4MHz 91 | */ 92 | spi_init_master( 93 | SPI1, 94 | SPI_CR1_BAUDRATE_FPCLK_DIV_2, 95 | SPI_CR1_CPOL_CLK_TO_0_WHEN_IDLE, 96 | SPI_CR1_CPHA_CLK_TRANSITION_1, 97 | SPI_CR1_DFF_8BIT, 98 | SPI_CR1_MSBFIRST); 99 | 100 | spi_enable_software_slave_management(SPI1); 101 | spi_set_nss_high(SPI1); /* Avoid Master mode fault MODF */ 102 | spi_enable(SPI1); 103 | } 104 | 105 | static 106 | uint8_t w5100_read_reg1(uint16_t reg) 107 | { 108 | uint8_t rx; 109 | 110 | gpio_clear(GPIOB, GPIO6); /* lower chip select */ 111 | (void)spi_xfer(SPI1, 0x0F); 112 | (void)spi_xfer(SPI1, reg >> 8); 113 | (void)spi_xfer(SPI1, reg & 0xFF); 114 | rx = spi_xfer(SPI1, 0x00); 115 | gpio_set(GPIOB, GPIO6); /* raise chip select */ 116 | return rx; 117 | } 118 | 119 | int main(void) 120 | { 121 | uint8_t rmsr; 122 | 123 | usart_init(); 124 | spi1_init(); 125 | 126 | usart_puts("Running.\r\n"); 127 | rmsr = w5100_read_reg1(0x001a); 128 | usart_puts("RMSR = 0x"); 129 | usart_printhex(rmsr); 130 | usart_puts("\r\n"); 131 | while(1); 132 | } 133 | 134 | -------------------------------------------------------------------------------- /src/stdio_usart.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | extern 29 | void stdio_init(void); 30 | 31 | extern 32 | void stdio_delete(void); 33 | 34 | static 35 | int stdio_usart_write(int fd, char *ptr, int len) 36 | { 37 | int i; 38 | struct fd *f; 39 | 40 | f = file_struct_get(fd); 41 | /* TODO: non-blocking */ 42 | for(i = 0; i < len; i++) 43 | { 44 | if (f->isatty && (ptr[i] == '\n')) 45 | { 46 | usart_send_blocking(USART2, '\r'); 47 | } 48 | usart_send_blocking(USART2, ptr[i]); 49 | } 50 | return len; 51 | } 52 | 53 | static 54 | int stdio_usart_read(int fd, char *ptr, int len) 55 | { 56 | int nread = 0; 57 | struct fd *f; 58 | 59 | f = file_struct_get(fd); 60 | (void)f; 61 | if (len > 0) 62 | { 63 | usart_wait_recv_ready(USART2); 64 | do { 65 | ptr[nread] = usart_recv(USART2); 66 | nread++; 67 | } while ((nread < len) && (USART_SR(USART2) & USART_SR_RXNE)); 68 | if (nread == 0) 69 | { 70 | errno = EAGAIN; 71 | nread = -1; 72 | } 73 | } 74 | return nread; 75 | } 76 | 77 | static 78 | void stdio_usart_init(void) 79 | { 80 | uint32_t baud = 57600; 81 | rcc_periph_clock_enable(RCC_GPIOA); 82 | rcc_periph_clock_enable(RCC_USART2); 83 | 84 | #ifdef STM32F1 85 | gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX); 86 | gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_USART2_RX); 87 | #elif defined(STM32F4) 88 | gpio_set_af(GPIOA, GPIO_AF7, GPIO2 | GPIO3); 89 | gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2|GPIO3); 90 | #endif 91 | USART2_BRR = ((2 * rcc_ahb_frequency) + baud) / (2 * baud); 92 | 93 | usart_set_databits(USART2, 8); 94 | usart_set_stopbits(USART2, USART_STOPBITS_1); 95 | usart_set_mode(USART2, USART_MODE_TX_RX); 96 | usart_set_parity(USART2, USART_PARITY_NONE); 97 | usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); 98 | usart_enable(USART2); 99 | } 100 | 101 | static 102 | void fileno_out_init(int fd) 103 | { 104 | struct fd *f; 105 | 106 | f = file_struct_get(fd); 107 | 108 | f->fd = fd; 109 | f->stat.st_mode = S_IFCHR|S_IWUSR|S_IWGRP|S_IWOTH; 110 | f->status_flags = O_WRONLY; 111 | f->write = stdio_usart_write; 112 | f->isatty = 1; 113 | f->isopen = 1; 114 | } 115 | 116 | static 117 | void fileno_in_init(int fd) 118 | { 119 | struct fd *f; 120 | f = file_struct_get(fd); 121 | 122 | f->fd = fd; 123 | f->stat.st_mode = S_IFCHR|S_IRUSR|S_IRGRP|S_IROTH; 124 | f->status_flags = O_RDONLY; 125 | f->read = stdio_usart_read; 126 | f->isatty = 1; 127 | f->isopen = 1; 128 | } 129 | 130 | __attribute__((__constructor__)) 131 | void stdio_init(void) 132 | { 133 | stdio_usart_init(); 134 | fileno_in_init(STDIN_FILENO); 135 | fileno_out_init(STDOUT_FILENO); 136 | fileno_out_init(STDERR_FILENO); 137 | } 138 | 139 | static 140 | void fileno_delete(int fd) 141 | { 142 | struct fd *f; 143 | f = file_struct_get(fd); 144 | f->isopen = 0; 145 | } 146 | 147 | __attribute__((__destructor__)) 148 | void stdio_delete(void) 149 | { 150 | fileno_delete(STDIN_FILENO); 151 | fileno_delete(STDOUT_FILENO); 152 | fileno_delete(STDERR_FILENO); 153 | } 154 | 155 | -------------------------------------------------------------------------------- /tests/socket_poll/socket_poll.c: -------------------------------------------------------------------------------- 1 | /* 2 | C socket server example 3 | http://www.binarytides.com/server-client-example-c-sockets-linux/ 4 | */ 5 | 6 | #include 7 | #include //strlen 8 | #include 9 | #include //inet_addr 10 | #include //write 11 | #include 12 | #include 13 | 14 | static 15 | void spin(void) 16 | { 17 | static int i_spin = 0; 18 | const char spin[4] = "-/|\\"; 19 | 20 | printf("%c", spin[i_spin % sizeof(spin)]); 21 | i_spin++; 22 | if ((i_spin % 41) == 0) 23 | { 24 | printf("\r"); 25 | } 26 | fflush(stdout); 27 | } 28 | 29 | static 30 | int loop(void) 31 | { 32 | int socket_desc; 33 | struct sockaddr_in server; 34 | 35 | printf("Press any key to continue..."); 36 | getchar(); 37 | printf("\n"); 38 | 39 | //Create socket 40 | socket_desc = socket(AF_INET , SOCK_STREAM , 0); 41 | if (socket_desc == -1) 42 | { 43 | perror("Could not create socket"); 44 | return 1; 45 | } 46 | puts("Socket created"); 47 | 48 | //Prepare the sockaddr_in structure 49 | server.sin_family = AF_INET; 50 | server.sin_addr.s_addr = INADDR_ANY; 51 | server.sin_port = htons( 8888 ); 52 | 53 | //Bind 54 | if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) != 0) 55 | { 56 | //print the error message 57 | perror("bind failed."); 58 | close(socket_desc); 59 | return 1; 60 | } 61 | puts("bind done"); 62 | 63 | //Listen 64 | if (listen(socket_desc , SOMAXCONN) != 0) 65 | { 66 | //print the error message 67 | perror("listen failed."); 68 | close(socket_desc); 69 | return 1; 70 | } 71 | 72 | while(1) 73 | { 74 | int client_sock, c; 75 | struct sockaddr_in client; 76 | 77 | //Accept and incoming connection 78 | puts("Waiting for incoming connections..."); 79 | c = sizeof(struct sockaddr_in); 80 | do 81 | { 82 | struct pollfd pfd; 83 | int ret; 84 | 85 | pfd.fd = socket_desc; 86 | pfd.events = POLLIN; 87 | 88 | ret = poll(&pfd, 1, 100); 89 | if (ret < 0) 90 | { 91 | perror("poll"); 92 | close(socket_desc); 93 | return 1; 94 | } 95 | else if (ret > 0) 96 | { 97 | if (pfd.revents & POLLNVAL) 98 | { 99 | perror("POLLNVAL"); 100 | close(socket_desc); 101 | return 1; 102 | } 103 | if (pfd.revents & POLLIN) 104 | { 105 | break; 106 | } 107 | } 108 | else 109 | { 110 | spin(); 111 | } 112 | } while(1); 113 | //accept connection from an incoming client 114 | client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c); 115 | if (client_sock < 0 && (errno != EAGAIN)) 116 | { 117 | perror("accept failed."); 118 | close(socket_desc); 119 | return 1; 120 | } 121 | puts("\rConnection accepted"); 122 | 123 | while(1) 124 | { 125 | struct pollfd pfd; 126 | int ret; 127 | 128 | pfd.fd = client_sock; 129 | pfd.events = POLLIN; 130 | 131 | ret = poll(&pfd, 1, 100); 132 | if ((pfd.revents & POLLNVAL) || (ret == -1)) 133 | { 134 | perror("recv failed"); 135 | close(socket_desc); 136 | close(client_sock); 137 | return 1; 138 | } 139 | else if (pfd.revents & (POLLHUP|POLLERR)) 140 | { 141 | puts("Client disconnected"); 142 | fflush(stdout); 143 | close(client_sock); 144 | break; 145 | } 146 | else if (pfd.revents & POLLIN) 147 | { 148 | char client_message[256]; 149 | ssize_t read_size; 150 | 151 | read_size = recv(client_sock , client_message , sizeof(client_message), 0); 152 | if (read_size > 0) 153 | { 154 | client_message[read_size] = '\0'; /* NULL-terminate before printing */ 155 | printf("\rrecv: %s\n", client_message); 156 | //Send the message back to client 157 | write(client_sock , client_message , read_size); 158 | } 159 | } 160 | else 161 | { 162 | spin(); 163 | } 164 | } 165 | } 166 | close(socket_desc); 167 | 168 | return 0; 169 | } 170 | 171 | int main(void) 172 | { 173 | while(1) 174 | { 175 | loop(); 176 | } 177 | } 178 | 179 | -------------------------------------------------------------------------------- /tests/socket_nonblock/socket_nonblock.c: -------------------------------------------------------------------------------- 1 | /* 2 | C socket server example 3 | http://www.binarytides.com/server-client-example-c-sockets-linux/ 4 | */ 5 | 6 | #include 7 | #include //strlen 8 | #include 9 | #include //inet_addr 10 | #include //write 11 | #include 12 | #include 13 | 14 | static 15 | void spin(void) 16 | { 17 | static int i_spin = 0; 18 | const char spin[4] = "-/|\\"; 19 | 20 | printf("%c", spin[i_spin % sizeof(spin)]); 21 | i_spin++; 22 | if ((i_spin % 41) == 0) 23 | { 24 | printf("\r"); 25 | } 26 | } 27 | 28 | static 29 | int loop(void) 30 | { 31 | int socket_desc; 32 | struct sockaddr_in server; 33 | int ret; 34 | int flags; 35 | 36 | printf("Press any key to continue..."); 37 | getchar(); 38 | printf("\n"); 39 | 40 | //Create socket 41 | socket_desc = socket(AF_INET , SOCK_STREAM , 0); 42 | if (socket_desc == -1) 43 | { 44 | perror("Could not create socket"); 45 | return 1; 46 | } 47 | puts("Socket created"); 48 | 49 | flags = fcntl(socket_desc, F_GETFL); 50 | if (flags == -1) 51 | { 52 | perror("fcntl(*, F_GETFL)"); 53 | close(socket_desc); 54 | return 1; 55 | } 56 | flags |= O_NONBLOCK; 57 | ret = fcntl(socket_desc, F_SETFL, flags); 58 | if (ret == -1) 59 | { 60 | perror("fcntl(*, F_SETFL, O_NONBLOCK)"); 61 | close(socket_desc); 62 | return 1; 63 | } 64 | printf("status: %08x\n", flags); 65 | 66 | //Prepare the sockaddr_in structure 67 | server.sin_family = AF_INET; 68 | server.sin_addr.s_addr = INADDR_ANY; 69 | server.sin_port = htons( 8888 ); 70 | 71 | //Bind 72 | if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) != 0) 73 | { 74 | //print the error message 75 | perror("bind failed."); 76 | close(socket_desc); 77 | return 1; 78 | } 79 | puts("bind done"); 80 | 81 | //Listen 82 | if (listen(socket_desc , SOMAXCONN) != 0) 83 | { 84 | //print the error message 85 | perror("listen failed."); 86 | close(socket_desc); 87 | return 1; 88 | } 89 | 90 | while(1) 91 | { 92 | int client_sock, c; 93 | struct sockaddr_in client; 94 | 95 | //Accept and incoming connection 96 | puts("Waiting for incoming connections..."); 97 | c = sizeof(struct sockaddr_in); 98 | do 99 | { 100 | //accept connection from an incoming client 101 | client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c); 102 | if (client_sock < 0 && (errno != EAGAIN)) 103 | { 104 | perror("accept failed."); 105 | close(socket_desc); 106 | return 1; 107 | } 108 | else 109 | { 110 | spin(); 111 | } 112 | } while(client_sock <= 0); 113 | puts("\rConnection accepted"); 114 | 115 | flags = fcntl(client_sock, F_GETFL); 116 | if (flags == -1) 117 | { 118 | perror("fcntl(*, F_GETFL)"); 119 | close(socket_desc); 120 | close(client_sock); 121 | return 1; 122 | } 123 | flags |= O_NONBLOCK; 124 | ret = fcntl(client_sock, F_SETFL, O_NONBLOCK); 125 | if (ret == -1) 126 | { 127 | perror("fcntl(*, F_SETFL, O_NONBLOCK)"); 128 | close(socket_desc); 129 | close(client_sock); 130 | return 1; 131 | } 132 | 133 | while(1) 134 | { 135 | char client_message[256]; 136 | ssize_t read_size; 137 | 138 | read_size = recv(client_sock , client_message , sizeof(client_message), 0); 139 | if (read_size > 0) 140 | { 141 | client_message[read_size] = '\0'; /* NULL-terminate before printing */ 142 | printf("\rrecv: %s\n", client_message); 143 | //Send the message back to client 144 | write(client_sock , client_message , read_size); 145 | } 146 | else if(read_size == 0) 147 | { 148 | puts("Client disconnected"); 149 | fflush(stdout); 150 | close(client_sock); 151 | break; 152 | } 153 | else if((read_size == -1) && (errno != EAGAIN)) 154 | { 155 | perror("recv failed"); 156 | close(socket_desc); 157 | close(client_sock); 158 | return 1; 159 | } 160 | else 161 | { 162 | spin(); 163 | } 164 | } 165 | } 166 | close(socket_desc); 167 | 168 | return 0; 169 | } 170 | 171 | int main(void) 172 | { 173 | while(1) 174 | { 175 | loop(); 176 | } 177 | } 178 | 179 | -------------------------------------------------------------------------------- /include/sys/socket.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Francesco Balducci 3 | * 4 | * This file is part of nucleo_tests. 5 | * 6 | * nucleo_tests is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * nucleo_tests is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with nucleo_tests. If not, see . 18 | */ 19 | #ifndef SYS_SOCKET_H 20 | #define SYS_SOCKET_H 21 | 22 | /* http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_socket.h.html */ 23 | 24 | #include 25 | #include 26 | 27 | typedef int32_t socklen_t; 28 | 29 | typedef unsigned int sa_family_t; 30 | 31 | struct sockaddr 32 | { 33 | sa_family_t sa_family; 34 | char sa_data[6]; /* TODO: enough for IPV6 */ 35 | }; 36 | 37 | struct sockaddr_storage 38 | { 39 | sa_family_t ss_family; 40 | char sa_data[6]; /* TODO: enough for IPV6 */ 41 | }; 42 | 43 | #define AF_INET 0x1 /* Internet domain sockets for use with IPv4 addresses. */ 44 | #define AF_INET6 0x2 /* Internet domain sockets for use with IPv6 addresses. */ 45 | #define AF_UNIX 0x3 /* UNIX domain sockets. */ 46 | #define AF_UNSPEC 0x4 /* Unspecified. */ 47 | 48 | #define SOCK_STREAM 0x1 /* Byte-stream socket. */ 49 | #define SOCK_DGRAM 0x2 /* Datagram socket. */ 50 | #define SOCK_RAW 0x3 /* Raw Protocol Interface. */ 51 | #define SOCK_SEQPACKET 0x4 /* Sequenced-packet socket. */ 52 | 53 | #define MSG_CTRUNC 0x01 /* Control data truncated. */ 54 | #define MSG_DONTROUTE 0x02 /* Send without using routing tables. */ 55 | #define MSG_EOR 0x04 /* Terminates a record (if supported by the protocol). */ 56 | #define MSG_OOB 0x08 /* Out-of-band data. */ 57 | #define MSG_PEEK 0x10 /* Leave received data in queue. */ 58 | #define MSG_TRUNC 0x20 /* Normal data truncated. */ 59 | #define MSG_WAITALL 0x40 /* Attempt to fill the read buffer. */ 60 | 61 | #define SO_ACCEPTCONN 0x01 /* Socket is accepting connections. */ 62 | #define SO_BROADCAST 0x02 /* Transmission of broadcast messages is supported. */ 63 | #define SO_DEBUG 0x03 /* Debugging information is being recorded. */ 64 | #define SO_DONTROUTE 0x04 /* Bypass normal routing. */ 65 | #define SO_ERROR 0x05 /* Socket error status. */ 66 | #define SO_KEEPALIVE 0x06 /* Connections are kept alive with periodic messages. */ 67 | #define SO_LINGER 0x07 /* Socket lingers on close. */ 68 | #define SO_OOBINLINE 0x08 /* Out-of-band data is transmitted in line. */ 69 | #define SO_RCVBUF 0x09 /* Receive buffer size. */ 70 | #define SO_RCVLOWAT 0x0A /* Receive ``low water mark''. */ 71 | #define SO_RCVTIMEO 0x0B /* Receive timeout. */ 72 | #define SO_REUSEADDR 0x0C /* Reuse of local addresses is supported. */ 73 | #define SO_SNDBUF 0x0D /* Send buffer size. */ 74 | #define SO_SNDLOWAT 0x0E /* Send ``low water mark''. */ 75 | #define SO_SNDTIMEO 0x0F /* Send timeout. */ 76 | #define SO_TYPE 0x10 /* Socket type. */ 77 | 78 | #define SOL_SOCKET 0xFF /* Options to be accessed at socket level, not protocol level. */ 79 | 80 | #define SOMAXCONN 1 /* The maximum backlog queue length. */ 81 | 82 | #define SHUT_RD 0x1 /* Disables further receive operations. */ 83 | #define SHUT_WR 0x2 /* Disables further send and receive operations. */ 84 | #define SHUT_RDWR 0x3 /* Disables further send operations. */ 85 | 86 | extern 87 | int accept(int, struct sockaddr *__restrict, socklen_t *__restrict); 88 | 89 | extern 90 | int bind(int, const struct sockaddr *, socklen_t); 91 | 92 | extern 93 | int connect(int, const struct sockaddr *, socklen_t); 94 | 95 | extern 96 | int getpeername(int, struct sockaddr *__restrict, socklen_t *__restrict); 97 | 98 | extern 99 | int getsockname(int, struct sockaddr *__restrict, socklen_t *__restrict); 100 | 101 | extern 102 | int getsockopt(int, int, int, void *__restrict, socklen_t *__restrict); 103 | 104 | extern 105 | int listen(int, int); 106 | 107 | extern 108 | ssize_t recv(int, void *, size_t, int); 109 | 110 | extern 111 | ssize_t recvfrom(int, void *__restrict, size_t, int, 112 | struct sockaddr *__restrict, socklen_t *__restrict); 113 | 114 | #if 0 115 | extern 116 | ssize_t recvmsg(int, struct msghdr *, int); 117 | #endif 118 | 119 | extern 120 | ssize_t send(int, const void *, size_t, int); 121 | 122 | #if 0 123 | extern 124 | ssize_t sendmsg(int, const struct msghdr *, int); 125 | #endif 126 | 127 | extern 128 | ssize_t sendto(int, const void *, size_t, int, const struct sockaddr *, 129 | socklen_t); 130 | 131 | extern 132 | int setsockopt(int, int, int, const void *, socklen_t); 133 | 134 | 135 | extern 136 | int shutdown(int, int); 137 | 138 | extern 139 | int socket(int, int, int); 140 | 141 | #if 0 142 | extern 143 | int sockatmark(int); 144 | 145 | extern 146 | int socketpair(int, int, int, int[2]); 147 | #endif 148 | 149 | #endif /* SYS_SOCKET_H */ 150 | 151 | -------------------------------------------------------------------------------- /include/signal.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef SIGNAL_H 3 | #define SIGNAL_H 4 | 5 | #include 6 | 7 | union sigval 8 | { 9 | int sival_int; /* Integer signal value. */ 10 | void *sival_ptr; /* Pointer signal value. */ 11 | }; 12 | 13 | typedef struct 14 | { 15 | int si_signo ; /* Signal number. */ 16 | int si_code ; /* Signal code. */ 17 | int si_errno ; /* If non-zero, an errno value associated with this signal, as described in . */ 18 | pid_t si_pid ; /* Sending process ID. */ 19 | uid_t si_uid ; /* Real user ID of sending process. */ 20 | void *si_addr ; /* Address of faulting instruction. */ 21 | int si_status ; /* Exit value or signal. */ 22 | long si_band ; /* Band event for SIGPOLL. */ 23 | union sigval si_value ; /* Signal value. */ 24 | } siginfo_t; 25 | 26 | #include_next 27 | 28 | #define sa_sigaction sa_handler 29 | 30 | typedef int pthread_attr_t; /* TODO: something about it in sys/types.h */ 31 | 32 | struct sigevent 33 | { 34 | int sigev_notify; /* Notification type. */ 35 | int sigev_signo; /* Signal number. */ 36 | union sigval sigev_value; /* Signal value. */ 37 | void (*sigev_notify_function)(union sigval); /* Notification function. */ 38 | pthread_attr_t *sigev_notify_attributes; /* Notification attributes. */ 39 | }; 40 | 41 | #define SIGEV_NONE 0 /* No asynchronous notification is delivered when the event of interest occurs. */ 42 | #define SIGEV_SIGNAL 1 /* A queued signal, with an application-defined value, is generated when the event of interest occurs. */ 43 | #define SIGEV_THREAD 2 /* A notification function is called to perform notification. */ 44 | 45 | #define SA_NOCLDSTOP 1 /* Do not generate SIGCHLD when children stop */ 46 | #define SA_ONSTACK 2 /* Causes signal delivery to occur on an alternate stack. */ 47 | #define SA_RESETHAND 4 /* Causes signal dispositions to be set to SIG_DFL on entry to signal handlers. */ 48 | #define SA_RESTART 8 /* Causes certain functions to become restartable. */ 49 | #define SA_SIGINFO 16 /* Causes extra information to be passed to signal handlers at the time of receipt of a signal. */ 50 | #define SA_NOCLDWAIT 32 /* Causes implementations not to create zombie processes or status information on child termination. See sigaction. */ 51 | #define SA_NODEFER 64 /* Causes signal not to be automatically blocked on entry to signal handler. */ 52 | 53 | #define ILL_ILLOPC 10 /* Illegal opcode. */ 54 | #define ILL_ILLOPN 10 /* Illegal operand. */ 55 | #define ILL_ILLADR 10 /* Illegal addressing mode. */ 56 | #define ILL_ILLTRP 10 /* Illegal trap. */ 57 | #define ILL_PRVOPC 10 /* Privileged opcode. */ 58 | #define ILL_PRVREG 10 /* Privileged register. */ 59 | #define ILL_COPROC 10 /* Coprocessor error. */ 60 | #define ILL_BADSTK 20 /* Internal stack error. */ 61 | #define FPE_INTDIV 21 /* Integer divide by zero. */ 62 | #define FPE_INTOVF 22 /* Integer overflow. */ 63 | #define FPE_FLTDIV 23 /* Floating-point divide by zero. */ 64 | #define FPE_FLTOVF 24 /* Floating-point overflow. */ 65 | #define FPE_FLTUND 25 /* Floating-point underflow. */ 66 | #define FPE_FLTRES 26 /* Floating-point inexact result. */ 67 | #define FPE_FLTINV 27 /* Invalid floating-point operation. */ 68 | #define FPE_FLTSUB 28 /* Subscript out of range. */ 69 | #define SEGV_MAPERR 30 /* Address not mapped to object. */ 70 | #define SEGV_ACCERR 31 /* Invalid permissions for mapped object. */ 71 | #define BUS_ADRALN 40 /* Invalid address alignment.  */ 72 | #define BUS_ADRERR 41 /* Nonexistent physical address. */ 73 | #define BUS_OBJERR 42 /* Object-specific hardware error. */ 74 | #define TRAP_BRKPT 50 /* Process breakpoint. */ 75 | #define TRAP_TRACE 51 /* Process trace trap.  */ 76 | #define CLD_EXITED 60 /* Child has exited. */ 77 | #define CLD_KILLED 61 /* Child has terminated abnormally and did not create a core file. */ 78 | #define CLD_DUMPED 62 /* Child has terminated abnormally and created a core file. */ 79 | #define CLD_TRAPPED 63 /* Traced child has trapped. */ 80 | #define CLD_STOPPED 64 /* Child has stopped. */ 81 | #define CLD_CONTINUED 65 /* Stopped child has continued.  */ 82 | #define POLL_IN 70 /* Data input available. */ 83 | #define POLL_OUT 71 /* Output buffers available. */ 84 | #define POLL_MSG 72 /* Input message available. */ 85 | #define POLL_ERR 73 /* I/O error. */ 86 | #define POLL_PRI 74 /* High priority input available. */ 87 | #define POLL_HUP 75 /* Device disconnected.  */ 88 | #define SI_USER 1 /* Signal sent by kill(). */ 89 | #define SI_QUEUE 2 /* Signal sent by sigqueue(). */ 90 | #define SI_TIMER 3 /* Signal generated by expiration of a timer set by timer_settime(). */ 91 | #define SI_ASYNCIO 4 /* Signal generated by completion of an asynchronous I/O request. */ 92 | #define SI_MESGQ 5 /* Signal generated by arrival of a message on an empty message queue. */ 93 | 94 | int killpg (pid_t, int); 95 | int sigaction (int, const struct sigaction *, struct sigaction *); 96 | int sigaddset (sigset_t *, const int); 97 | int sigdelset (sigset_t *, const int); 98 | int sigismember (const sigset_t *, int); 99 | int sigfillset (sigset_t *); 100 | int sigemptyset (sigset_t *); 101 | int sigpending (sigset_t *); 102 | int sigsuspend (const sigset_t *); 103 | int sigpause (int); 104 | int siginterrupt(int, int); 105 | int sighold(int); 106 | int sigrelse(int); 107 | int sigignore(int); 108 | 109 | #endif /* SIGNAL_H */ 110 | 111 | --------------------------------------------------------------------------------