From b5d802c1ee29f3056ed20a6440936daa7c817265 Mon Sep 17 00:00:00 2001 From: "John C. Peterson" Date: Fri, 19 May 2017 21:08:07 -0700 Subject: [PATCH 1/3] Improved error checking code and fixes to code for system freezes. --- PiCW.cpp | 25 +++++++++++++++---------- mailbox.c | 47 ++++++++++++++++++++++++++++++++++++----------- makefile | 15 ++++++++++----- 3 files changed, 61 insertions(+), 26 deletions(-) diff --git a/PiCW.cpp b/PiCW.cpp index 4010722..f419b1c 100644 --- a/PiCW.cpp +++ b/PiCW.cpp @@ -110,10 +110,14 @@ // the PPM correction reported by NTP and the actual frequency offset of // the crystal. This 2.5 PPM offset is not present in the RPi2 and RPi3. // This 2.5 PPM offset is compensated for here, but only for the RPi1. -#ifdef RPI2 +#ifdef RPI23 #define F_PLLD_CLK (500000000.0) #else +#ifdef RPI1 #define F_PLLD_CLK (500000000.0*(1-2.500e-6)) +#else +#error "RPI version macro is not defined" +#endif #endif // Empirical value for F_PWM_CLK that produces WSPR symbols that are 'close' to // 0.682s long. For some reason, despite the use of DMA, the load on the PI @@ -121,15 +125,19 @@ // compensated for in the main loop. #define F_PWM_CLK_INIT (31156186.6125761) -// Choose proper base address depending on RPI1/RPI2 setting from makefile. +// Choose proper base address depending on RPI1/RPI23 macro from makefile. // PERI_BASE_PHYS is the base address of the peripherals, in physical // address space. -#ifdef RPI2 +#ifdef RPI23 #define PERI_BASE_PHYS 0x3f000000 #define MEM_FLAG 0x04 #else +#ifdef RPI1 #define PERI_BASE_PHYS 0x20000000 #define MEM_FLAG 0x0c +#else +#error "RPI version macro is not defined" +#endif #endif #define PAGE_SIZE (4*1024) @@ -1125,12 +1133,6 @@ void morse_table_init( // Create the mbox special files and open mbox. void open_mbox() { - unlink(DEVICE_FILE_NAME); - unlink(LOCAL_DEVICE_FILE_NAME); - if (mknod(DEVICE_FILE_NAME, S_IFCHR|0600, makedev(100, 0)) < 0) { - std::cerr << "Failed to create mailbox device." << std::endl; - ABORT(-1); - } mbox.handle = mbox_open(); if (mbox.handle < 0) { std::cerr << "Failed to open mailbox." << std::endl; @@ -1143,7 +1145,6 @@ void cleanup() { disable_clock(); unSetupDMA(); deallocMemPool(); - unlink(DEVICE_FILE_NAME); unlink(LOCAL_DEVICE_FILE_NAME); } @@ -1207,7 +1208,11 @@ int main(const int argc, char * const argv[]) { #ifdef RPI1 std::cout << "Detected Raspberry Pi version 1" << std::endl; #else +#ifdef RPI23 std::cout << "Detected Raspberry Pi version 2/3" << std::endl; +#else +#error "RPI version macro is not defined" +#endif #endif // Parse arguments diff --git a/mailbox.c b/mailbox.c index 8336eba..3f0ece0 100644 --- a/mailbox.c +++ b/mailbox.c @@ -86,7 +86,8 @@ static int mbox_property(int file_desc, void *buf) int ret_val = ioctl(file_desc, IOCTL_MBOX_PROPERTY, buf); if (ret_val < 0) { - printf("ioctl_set_msg failed:%d\n", ret_val); + // something wrong somewhere, send some details to stderr + perror("ioctl_set_msg failed"); } #ifdef DEBUG @@ -114,7 +115,10 @@ unsigned mem_alloc(int file_desc, unsigned size, unsigned align, unsigned flags) p[i++] = 0x00000000; // end tag p[0] = i*sizeof *p; // actual size - if(mbox_property(file_desc, p) < 0) return 0; + if(mbox_property(file_desc, p) < 0) { + printf("mem_alloc: mbox_property() error, abort!\n"); + exit (-1); + } return p[5]; } @@ -133,7 +137,10 @@ unsigned mem_free(int file_desc, unsigned handle) p[i++] = 0x00000000; // end tag p[0] = i*sizeof *p; // actual size - if(mbox_property(file_desc, p) < 0) return 0; + if(mbox_property(file_desc, p) < 0) { + printf("mem_free: mbox_property() error, ignoring\n"); + return 0; + } return p[5]; } @@ -152,7 +159,10 @@ unsigned mem_lock(int file_desc, unsigned handle) p[i++] = 0x00000000; // end tag p[0] = i*sizeof *p; // actual size - if(mbox_property(file_desc, p) < 0) return 0; + if(mbox_property(file_desc, p) < 0) { + printf("mem_lock: mbox_property() error, abort!\n"); + exit (-1); + } return p[5]; } @@ -171,7 +181,10 @@ unsigned mem_unlock(int file_desc, unsigned handle) p[i++] = 0x00000000; // end tag p[0] = i*sizeof *p; // actual size - if(mbox_property(file_desc, p) < 0) return 0; + if(mbox_property(file_desc, p) < 0) { + printf("mem_unlock: mbox_property() error, ignoring\n"); + return 0; + } return p[5]; } @@ -196,7 +209,10 @@ unsigned execute_code(int file_desc, unsigned code, unsigned r0, unsigned r1, un p[i++] = 0x00000000; // end tag p[0] = i*sizeof *p; // actual size - if(mbox_property(file_desc, p) < 0) return 0; + if(mbox_property(file_desc, p) < 0) { + printf("execute_code: mbox_property() error, ignoring\n"); + return 0; + } return p[5]; } @@ -216,7 +232,10 @@ unsigned qpu_enable(int file_desc, unsigned enable) p[i++] = 0x00000000; // end tag p[0] = i*sizeof *p; // actual size - if(mbox_property(file_desc, p) < 0) return 0; + if(mbox_property(file_desc, p) < 0) { + printf("qpu_enable: mbox_property() error, ignoring\n"); + return 0; + } return p[5]; } @@ -237,7 +256,10 @@ unsigned execute_qpu(int file_desc, unsigned num_qpus, unsigned control, unsigne p[i++] = 0x00000000; // end tag p[0] = i*sizeof *p; // actual size - if(mbox_property(file_desc, p) < 0) return 0; + if(mbox_property(file_desc, p) < 0) { + printf("execute_qpu: mbox_property() error, ignoring\n"); + return 0; + } return p[5]; } @@ -245,6 +267,8 @@ int mbox_open() { int file_desc; // Open a char device file used for communicating with kernel mbox driver. + + // try to use the device node in /dev first (created by kernels 4.1+) file_desc = open(DEVICE_FILE_NAME, 0); if(file_desc >= 0) { //printf("Using mbox device " DEVICE_FILE_NAME ".\n"); @@ -255,18 +279,19 @@ int mbox_open() { unlink(LOCAL_DEVICE_FILE_NAME); if(mknod(LOCAL_DEVICE_FILE_NAME, S_IFCHR|0600, makedev(MAJOR_NUM_A, 0)) >= 0 && (file_desc = open(LOCAL_DEVICE_FILE_NAME, 0)) >= 0) { - //printf("Using local mbox device file with major %d.\n", MAJOR_NUM_A); + printf("Using local mbox device file with major %d.\n", MAJOR_NUM_A); return file_desc; } unlink(LOCAL_DEVICE_FILE_NAME); if(mknod(LOCAL_DEVICE_FILE_NAME, S_IFCHR|0600, makedev(MAJOR_NUM_B, 0)) >= 0 && (file_desc = open(LOCAL_DEVICE_FILE_NAME, 0)) >= 0) { - //printf("Using local mbox device file with major %d.\n", MAJOR_NUM_B); + printf("Using local mbox device file with major %d.\n", MAJOR_NUM_B); return file_desc; } - return -1; + printf("Unable to open / create kernel mbox device file, abort!\n"); + exit (-1); } void mbox_close(int file_desc) { diff --git a/makefile b/makefile index 380cdb8..cbc3f3e 100644 --- a/makefile +++ b/makefile @@ -1,7 +1,13 @@ prefix=/usr/local -archis = $(if $(findstring $(1),$(shell uname -m)),$(2)) -pi_version_flag = $(if $(call archis,armv7,dummy-text),-DRPI2,-DRPI1) +ifeq ($(findstring armv6,$(shell uname -m)),armv6) +# Broadcom BCM2835 SoC with 700 MHz 32-bit ARM 1176JZF-S (ARMv6 arch) +PI_VERSION = -DRPI1 +else +# Broadcom BCM2836 SoC with 900 MHz 32-bit quad-core ARM Cortex-A7 (ARMv7 arch) +# Broadcom BCM2837 SoC with 1.2 GHz 64-bit quad-core ARM Cortex-A53 (ARMv8 arch) +PI_VERSION = -DRPI23 +endif all: PiCW @@ -9,11 +15,10 @@ mailbox.o: mailbox.c mailbox.h g++ -c -Wall -lm mailbox.c PiCW: PiCW.cpp mailbox.o mailbox.h - g++ -D_GLIBCXX_DEBUG -std=c++11 -Wall -Werror -fmax-errors=5 -lm $(pi_version_flag) mailbox.o PiCW.cpp -pthread -oPiCW + g++ -D_GLIBCXX_DEBUG -std=c++11 -Wall -Werror -fmax-errors=5 -lm $(PI_VERSION) mailbox.o PiCW.cpp -pthread -oPiCW clean: - -rm PiCW - -rm mailbox.o + -rm -f PiCW PiCW.o mailbox.o .PHONY: install install: PiCW From 89f81dd3c277b724e6d2d66a843af9ad43914cc0 Mon Sep 17 00:00:00 2001 From: Mathias Gibbens Date: Sat, 24 Aug 2019 21:54:59 +0100 Subject: [PATCH 2/3] Compile fixes for Debian "buster" --- mailbox.c | 1 + makefile | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/mailbox.c b/mailbox.c index 3f0ece0..f3252eb 100644 --- a/mailbox.c +++ b/mailbox.c @@ -33,6 +33,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include "mailbox.h" diff --git a/makefile b/makefile index cbc3f3e..575252f 100644 --- a/makefile +++ b/makefile @@ -15,7 +15,7 @@ mailbox.o: mailbox.c mailbox.h g++ -c -Wall -lm mailbox.c PiCW: PiCW.cpp mailbox.o mailbox.h - g++ -D_GLIBCXX_DEBUG -std=c++11 -Wall -Werror -fmax-errors=5 -lm $(PI_VERSION) mailbox.o PiCW.cpp -pthread -oPiCW + g++ -D_GLIBCXX_DEBUG -std=c++11 -Wall -Werror -Wno-psabi -fmax-errors=5 -latomic -lm $(PI_VERSION) mailbox.o PiCW.cpp -pthread -oPiCW clean: -rm -f PiCW PiCW.o mailbox.o From 7d4299e0d51c9d8768003aefd7ab66575fbd9b32 Mon Sep 17 00:00:00 2001 From: Mathias Gibbens Date: Sat, 24 Aug 2019 21:56:38 +0100 Subject: [PATCH 3/3] Minor cleanup --- LICENSE | 0 makefile => Makefile | 10 +++++++--- PiCW.cpp | 2 ++ 3 files changed, 9 insertions(+), 3 deletions(-) mode change 100755 => 100644 LICENSE rename makefile => Makefile (70%) diff --git a/LICENSE b/LICENSE old mode 100755 new mode 100644 diff --git a/makefile b/Makefile similarity index 70% rename from makefile rename to Makefile index 575252f..f3d5e54 100644 --- a/makefile +++ b/Makefile @@ -1,5 +1,9 @@ prefix=/usr/local +CFLAGS += -Wall +CXXFLAGS += -D_GLIBCXX_DEBUG -std=c++11 -Wall -Werror -Wno-psabi +LDLIBS += -lm -latomic + ifeq ($(findstring armv6,$(shell uname -m)),armv6) # Broadcom BCM2835 SoC with 700 MHz 32-bit ARM 1176JZF-S (ARMv6 arch) PI_VERSION = -DRPI1 @@ -12,13 +16,13 @@ endif all: PiCW mailbox.o: mailbox.c mailbox.h - g++ -c -Wall -lm mailbox.c + $(CC) $(CFLAGS) -c mailbox.c PiCW: PiCW.cpp mailbox.o mailbox.h - g++ -D_GLIBCXX_DEBUG -std=c++11 -Wall -Werror -Wno-psabi -fmax-errors=5 -latomic -lm $(PI_VERSION) mailbox.o PiCW.cpp -pthread -oPiCW + $(CXX) $(CXXFLAGS) $(LDLIBS) $(PI_VERSION) -pthread mailbox.o PiCW.cpp -o PiCW clean: - -rm -f PiCW PiCW.o mailbox.o + -rm -f PiCW *.o .PHONY: install install: PiCW diff --git a/PiCW.cpp b/PiCW.cpp index f419b1c..aa5c1c8 100644 --- a/PiCW.cpp +++ b/PiCW.cpp @@ -50,7 +50,9 @@ #include #include +extern "C" { #include "mailbox.h" +} // Note on accessing memory in RPi: //