From d7cd899653531ac5a8def9e9697b0d65b438b773 Mon Sep 17 00:00:00 2001
From: Clyne Sullivan <clyne@bitgloo.com>
Date: Tue, 10 Oct 2023 09:48:31 -0400
Subject: small fixes; add compile checks

---
 firmware/Makefile                 | 15 +++++++++++++--
 firmware/source/communication.cpp |  2 +-
 firmware/source/conversion.cpp    |  2 ++
 firmware/source/periph/usbcfg.c   |  1 -
 firmware/source/samplebuffer.cpp  | 12 ++++++------
 firmware/source/samplebuffer.hpp  |  2 +-
 6 files changed, 23 insertions(+), 11 deletions(-)

(limited to 'firmware')

diff --git a/firmware/Makefile b/firmware/Makefile
index 0db249b..542bcea 100644
--- a/firmware/Makefile
+++ b/firmware/Makefile
@@ -162,10 +162,10 @@ INCDIR = $(CONFDIR) $(ALLINC) $(TESTINC) \
          source source/periph
 
 # Define C warning options here.
-CWARN = -Wall -Wextra -Wundef -Wstrict-prototypes -pedantic
+CWARN = -Wall -Wextra -Wundef -Wstrict-prototypes -pedantic -Werror
 
 # Define C++ warning options here.
-CPPWARN = -Wall -Wextra -Wundef -pedantic -Wno-volatile
+CPPWARN = -Wall -Wextra -Wundef -pedantic -Wno-volatile -Werror -Wconversion
 
 #
 # Project, target, sources and paths
@@ -217,6 +217,17 @@ include $(RULESPATH)/rules.mk
 # Custom rules
 #
 
+check:
+	cppcheck --std=c++17 --enable=warning,style,performance,portability --force $(shell find source/ -name "*.cpp" -or -name "*.hpp" -or -name "*.c" -or -name "*.h" -type f)
+
+tidy:
+	clang-tidy \
+		-warnings-as-errors=* \
+		--extra-arg=-I --extra-arg=/usr/lib/gcc/arm-none-eabi/13/include/g++-v13 \
+		--extra-arg=-I --extra-arg=/usr/lib/gcc/arm-none-eabi/13/include/g++-v13/arm-none-eabi \
+		$(CPPSRC)
+
 #
 # Custom rules
 ##############################################################################
+
diff --git a/firmware/source/communication.cpp b/firmware/source/communication.cpp
index e85828b..8a9f86e 100644
--- a/firmware/source/communication.cpp
+++ b/firmware/source/communication.cpp
@@ -223,7 +223,7 @@ void sampleRate(unsigned char *cmd)
 {
     if (EM.assert(USBSerial::read(&cmd[1], 1) == 1, Error::BadParamSize)) {
         if (cmd[1] == 0xFF) {
-            unsigned char r = SClock::getRate();
+            auto r = static_cast<unsigned char>(SClock::getRate());
             USBSerial::write(&r, 1);
         } else {
             auto r = static_cast<SClock::Rate>(cmd[1]);
diff --git a/firmware/source/conversion.cpp b/firmware/source/conversion.cpp
index 56a689e..8b6a927 100644
--- a/firmware/source/conversion.cpp
+++ b/firmware/source/conversion.cpp
@@ -156,6 +156,7 @@ void ConversionManager::threadRunner(void *)
                     samples = entry(samples, size);
                     asm("mov sp, %0" :: "r" (sp));
                     volatile auto testRead = *samples;
+                    (void)testRead;
                 } else {
                     // Start execution timer:
                     asm("mov %0, sp; eor r0, r0; svc 2" : "=r" (sp));
@@ -163,6 +164,7 @@ void ConversionManager::threadRunner(void *)
                     // Stop execution timer:
                     asm("mov r0, #1; svc 2; mov sp, %0" :: "r" (sp));
                     volatile auto testRead = *samples;
+                    (void)testRead;
                 } 
             }
 
diff --git a/firmware/source/periph/usbcfg.c b/firmware/source/periph/usbcfg.c
index b726e23..537cca7 100644
--- a/firmware/source/periph/usbcfg.c
+++ b/firmware/source/periph/usbcfg.c
@@ -264,7 +264,6 @@ static const USBEndpointConfig ep2config = {
  * Handles the USB driver global events.
  */
 static void usb_event(USBDriver *usbp, usbevent_t event) {
-  extern SerialUSBDriver SDU1;
 
   switch (event) {
   case USB_EVENT_ADDRESS:
diff --git a/firmware/source/samplebuffer.cpp b/firmware/source/samplebuffer.cpp
index 74c6778..f04173a 100644
--- a/firmware/source/samplebuffer.cpp
+++ b/firmware/source/samplebuffer.cpp
@@ -19,12 +19,12 @@ void SampleBuffer::clear() {
 }
 __attribute__((section(".convcode")))
 void SampleBuffer::modify(Sample *data, unsigned int srcsize) {
-    auto size = srcsize < m_size ? srcsize : m_size;
-    size = (size + 15) & (~15);
+    auto dsize = srcsize < m_size ? srcsize : m_size;
+    dsize = (dsize + 15) & (~15);
 
     m_modified = m_buffer;
     const int *src = reinterpret_cast<const int *>(data);
-    const int * const srcend = src + (size / 2);
+    const int * const srcend = src + (dsize / 2);
     int *dst = reinterpret_cast<int *>(m_buffer);
     do {
         int a = src[0];
@@ -49,12 +49,12 @@ void SampleBuffer::modify(Sample *data, unsigned int srcsize) {
 }
 __attribute__((section(".convcode")))
 void SampleBuffer::midmodify(Sample *data, unsigned int srcsize) {
-    auto size = srcsize < m_size / 2 ? srcsize : m_size / 2;
-    size = (size + 15) & (~15);
+    auto dsize = srcsize < m_size / 2 ? srcsize : m_size / 2;
+    dsize = (dsize + 15) & (~15);
 
     m_modified = middata();
     const int *src = reinterpret_cast<const int *>(data);
-    const int * const srcend = src + (size / 2);
+    const int * const srcend = src + (dsize / 2);
     int *dst = reinterpret_cast<int *>(middata());
     do {
         int a = src[0];
diff --git a/firmware/source/samplebuffer.hpp b/firmware/source/samplebuffer.hpp
index 8328770..9b837bc 100644
--- a/firmware/source/samplebuffer.hpp
+++ b/firmware/source/samplebuffer.hpp
@@ -29,7 +29,7 @@ class SampleBuffer
 {
 public:
     // Manage the sample data memory at 'buffer'.
-    SampleBuffer(Sample *buffer);
+    explicit SampleBuffer(Sample *buffer);
 
     /**
      * Fill the current buffer with midpoint (2048/0V) values.
-- 
cgit v1.2.3