From 2535aace51f29c050d84ac7fcd923649d72f2ae4 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Fri, 7 Jun 2024 20:02:42 -0400 Subject: [PATCH 1/6] wip: prep code for brief execution we do *not* have much power available --- board.h | 8 +++----- cfg/halconf.h | 2 +- cfg/mcuconf.h | 2 +- main.cpp | 34 +++++++++++++++++++--------------- 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/board.h b/board.h index 55ef0aa..9576dba 100644 --- a/board.h +++ b/board.h @@ -53,11 +53,9 @@ /* * IO lines assignments. */ -//#define LINE_LED_GREEN PAL_LINE(GPIOA, 12U) - -#define LINE_I2S_SD PAL_LINE(GPIOA, 12U) // or PB5 -#define LINE_I2S_WS PAL_LINE(GPIOB, 0U) -#define LINE_I2S_CK PAL_LINE(GPIOA, 1U) +#define LINE_I2S_SD PAL_LINE(GPIOA, 7U) +#define LINE_I2S_WS PAL_LINE(GPIOA, 4U) +#define LINE_I2S_CK PAL_LINE(GPIOA, 5U) #define LINE_USART2_TX PAL_LINE(GPIOA, 14U) diff --git a/cfg/halconf.h b/cfg/halconf.h index 3a05139..164616c 100644 --- a/cfg/halconf.h +++ b/cfg/halconf.h @@ -142,7 +142,7 @@ * @brief Enables the SERIAL subsystem. */ #if !defined(HAL_USE_SERIAL) || defined(__DOXYGEN__) -#define HAL_USE_SERIAL TRUE +#define HAL_USE_SERIAL FALSE #endif /** diff --git a/cfg/mcuconf.h b/cfg/mcuconf.h index 3905c30..8c768d7 100644 --- a/cfg/mcuconf.h +++ b/cfg/mcuconf.h @@ -172,7 +172,7 @@ * SERIAL driver system settings. */ #define STM32_SERIAL_USE_USART1 FALSE -#define STM32_SERIAL_USE_USART2 TRUE +#define STM32_SERIAL_USE_USART2 FALSE #define STM32_SERIAL_USE_LPUART1 FALSE /* diff --git a/main.cpp b/main.cpp index 298c3f8..f7c8027 100644 --- a/main.cpp +++ b/main.cpp @@ -34,7 +34,7 @@ static constexpr auto MIC_BITS = 18u; static constexpr auto SAMPLE_RATE = 48000u; static constexpr unsigned I2S_BUFSIZ = 1024; -static constexpr unsigned I2S_STRIDE = 16; +static constexpr unsigned I2S_STRIDE = 32: // Calculate reference amplitude value at compile time static const auto MIC_REF_AMPL = sos_t((1 << (MIC_BITS - 1)) - 1) * @@ -64,18 +64,19 @@ int main(void) { halInit(); osalSysEnable(); - osalThreadSleepMilliseconds(2000); + //osalThreadSleepMilliseconds(2000); palSetPadMode(GPIOB, 7, PAL_MODE_OUTPUT_PUSHPULL); - palSetPadMode(GPIOF, 2, PAL_MODE_UNCONNECTED); + palSetPad(GPIOB, 7); + //palSetPadMode(GPIOF, 2, PAL_MODE_UNCONNECTED); palSetLineMode(LINE_I2S_SD, PAL_MODE_ALTERNATE(0)); palSetLineMode(LINE_I2S_WS, PAL_MODE_ALTERNATE(0)); palSetLineMode(LINE_I2S_CK, PAL_MODE_ALTERNATE(0)); - palSetLineMode(LINE_USART2_TX, PAL_MODE_ALTERNATE(1)); + //palSetLineMode(LINE_USART2_TX, PAL_MODE_ALTERNATE(1)); - sdStart(&SD2, NULL); - sdWrite(&SD2, (uint8_t *)"Noisemeter\n", 11); - osalThreadSleepMilliseconds(2); + //sdStart(&SD2, NULL); + //sdWrite(&SD2, (uint8_t *)"Noisemeter\n", 11); + //osalThreadSleepMilliseconds(2); i2sStart(&I2SD1, &i2sConfig); i2sStartExchange(&I2SD1); @@ -86,7 +87,7 @@ int main(void) SCB->SCR |= SCB_SCR_SLEEPONEXIT_Msk; __WFI(); - palSetPad(GPIOB, 7); + //palSetPad(GPIOB, 7); const sos_t Leq_RMS = qfp_fsqrt(Leq_sum_sqr / qfp_uint2float(Leq_samples)); const sos_t Leq_dB = MIC_OFFSET_DB + MIC_REF_DB + sos_t(20.f) * qfp_flog10(Leq_RMS / MIC_REF_AMPL); @@ -94,12 +95,15 @@ int main(void) Leq_samples = 0; auto n = std::clamp(qfp_float2int(Leq_dB), 0, 999); - strbuf[2] = n % 10 + '0'; n /= 10; - strbuf[1] = n % 10 + '0'; n /= 10; - strbuf[0] = n ? n + '0' : ' '; - sdWrite(&SD2, strbuf, sizeof(strbuf)); - osalThreadSleepMilliseconds(2); palClearPad(GPIOB, 7); + osalThreadSleepMicroseconds(200); + palSetPad(GPIOB, 7); + //strbuf[2] = n % 10 + '0'; n /= 10; + //strbuf[1] = n % 10 + '0'; n /= 10; + //strbuf[0] = n ? n + '0' : ' '; + //sdWrite(&SD2, strbuf, sizeof(strbuf)); + //osalThreadSleepMilliseconds(2); + ////palClearPad(GPIOB, 7); } } @@ -114,7 +118,7 @@ void i2sCallback(I2SDriver *i2s) if (i2sReady.load()) return; - palSetPad(GPIOB, 7); + //palSetPad(GPIOB, 7); const auto halfsize = i2sBuffer.size() / 2; const auto source = i2sBuffer.data() + (i2sIsBufferComplete(i2s) ? halfsize : 0); auto samples = reinterpret_cast(source); @@ -135,6 +139,6 @@ void i2sCallback(I2SDriver *i2s) i2sReady.store(true); SCB->SCR &= ~SCB_SCR_SLEEPONEXIT_Msk; } - palClearPad(GPIOB, 7); + //palClearPad(GPIOB, 7); } From b4360fbf445df5ff326af25bac30395f871fb2a8 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Mon, 10 Jun 2024 08:28:36 -0400 Subject: [PATCH 2/6] schematic cleanup --- .gitignore | 2 ++ hardware/noisecard.kicad_pcb | 2 +- hardware/noisecard.kicad_sch | 62 ++++++++++++++++++------------------ 3 files changed, 34 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index 5c702ba..09cb13e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ *.pretty *.step build +hardware/fp-info-cache +hardware/noisecard-backups hardware/production diff --git a/hardware/noisecard.kicad_pcb b/hardware/noisecard.kicad_pcb index 1bb0b9e..323da7c 100755 --- a/hardware/noisecard.kicad_pcb +++ b/hardware/noisecard.kicad_pcb @@ -7416,7 +7416,7 @@ (gr_text "FABRICATION DRAWING" (at 150 25) (layer "Dwgs.User") (tstamp d68f8496-1f7a-4583-b409-3c765734f735) (effects (font (size 2 2) (thickness 0.25) bold)) ) - (gr_text "NOTES\n\n 1. PRINTED CIRCUIT BOARD MADE FROM NEMA GRADE FR-4 EPOXY LAMINATE.\n 2. ALL DIMENSIONS ARE GIVEN IN MILLIMETERS.\n 3. CIRCUIT PATHS ARE FOR REFERENCE ONLY.\n 4. HOLE SIZES SHOWN ARE FINISHED DIAMETERS AFTER PLATING.\n 5. APPLY GREEN SOLDER MASK ON PLATED SURFACES.\n 6. SILKSCREEN TOP SIDE USING WHITE INK.\n 7. ALL VIAS ARE TENTED ON BOTH SIDES.\n 8. 2 COPPER LAYERS.\n 9. 1.6MM BOARD THICKNESS.\n10. COPPER THICKNESS 1 OZ." (at 38.75 139) (layer "Dwgs.User") (tstamp d939a69e-194e-46a0-9c0c-335c0a91742c) + (gr_text "NOTES\n\n 1. PRINTED CIRCUIT BOARD MADE FROM NEMA GRADE FR-4 EPOXY LAMINATE.\n 2. ALL DIMENSIONS ARE GIVEN IN MILLIMETERS.\n 3. CIRCUIT PATHS ARE FOR REFERENCE ONLY.\n 4. HOLE SIZES SHOWN ARE FINISHED DIAMETERS AFTER PLATING.\n 5. APPLY GREEN SOLDER MASK ON PLATED SURFACES.\n 6. SILKSCREEN TOP SIDE USING WHITE INK.\n 7. ALL VIAS ARE TENTED ON BOTH SIDES.\n 8. 2 COPPER LAYERS.\n 9. 0.8MM BOARD THICKNESS.\n10. COPPER THICKNESS 1 OZ." (at 38.75 139) (layer "Dwgs.User") (tstamp d939a69e-194e-46a0-9c0c-335c0a91742c) (effects (font (size 1.5 1.5) (thickness 0.1875)) (justify left top)) ) (gr_text "{\\H2.0000;www.hammondmfg.com}" (at 602.59387 247.297935) (layer "User.3") (tstamp 05fdb0fe-aa41-4d1c-9218-ed7330023c5e) diff --git a/hardware/noisecard.kicad_sch b/hardware/noisecard.kicad_sch index 6b94dfc..3ce1d91 100755 --- a/hardware/noisecard.kicad_sch +++ b/hardware/noisecard.kicad_sch @@ -1876,7 +1876,7 @@ (property "Reference" "C3" (at 73.66 32.766 0) (effects (font (size 1.27 1.27)) (justify left)) ) - (property "Value" "330u" (at 73.66 35.306 0) + (property "Value" "330uF" (at 73.66 35.306 0) (effects (font (size 1.27 1.27)) (justify left)) ) (property "Footprint" "Capacitor_Tantalum_SMD:CP_EIA-7343-15_Kemet-W" (at 71.4502 38.735 0) @@ -1971,8 +1971,8 @@ (property "Part Number" "APTD1608LSURCK" (at 214.63 102.87 0) (effects (font (size 1.27 1.27)) hide) ) - (pin "2" (uuid 455ac6a2-863b-43b8-9272-a864429f15f4)) - (pin "1" (uuid d1a0fde7-e9af-40b6-a11b-543a47a2f89d)) + (pin "2" (uuid 455ac6a2-863b-43b8-9272-a864429f15f5)) + (pin "1" (uuid d1a0fde7-e9af-40b6-a11b-543a47a2f89e)) (instances (project "noisecard" (path "/2019f223-1307-408a-bf87-dad398f72c59" @@ -2000,8 +2000,8 @@ (property "Part Number" "APTD1608LSURCK" (at 214.63 115.57 0) (effects (font (size 1.27 1.27)) hide) ) - (pin "2" (uuid 455ac6a2-863b-43b8-9272-a864429f15f4)) - (pin "1" (uuid d1a0fde7-e9af-40b6-a11b-543a47a2f89d)) + (pin "2" (uuid 455ac6a2-863b-43b8-9272-a864429f15f6)) + (pin "1" (uuid d1a0fde7-e9af-40b6-a11b-543a47a2f89f)) (instances (project "noisecard" (path "/2019f223-1307-408a-bf87-dad398f72c59" @@ -2029,8 +2029,8 @@ (property "Part Number" "APTD1608LSURCK" (at 214.63 96.52 0) (effects (font (size 1.27 1.27)) hide) ) - (pin "2" (uuid 455ac6a2-863b-43b8-9272-a864429f15f4)) - (pin "1" (uuid d1a0fde7-e9af-40b6-a11b-543a47a2f89d)) + (pin "2" (uuid 455ac6a2-863b-43b8-9272-a864429f15f7)) + (pin "1" (uuid d1a0fde7-e9af-40b6-a11b-543a47a2f8a0)) (instances (project "noisecard" (path "/2019f223-1307-408a-bf87-dad398f72c59" @@ -2104,7 +2104,7 @@ (property "Reference" "C8" (at 50.165 114.9413 0) (effects (font (size 1.27 1.27)) (justify right)) ) - (property "Value" "2U2" (at 50.165 117.4813 0) + (property "Value" "2.2uF" (at 50.165 117.4813 0) (effects (font (size 1.27 1.27)) (justify right)) ) (property "Footprint" "Capacitor_SMD:C_0805_2012Metric" (at 53.34 115.57 0) @@ -2183,7 +2183,7 @@ (property "Reference" "C9" (at 61.595 114.9413 0) (effects (font (size 1.27 1.27)) (justify right)) ) - (property "Value" "0.1U" (at 61.595 117.4813 0) + (property "Value" "0.1uF" (at 61.595 117.4813 0) (effects (font (size 1.27 1.27)) (justify right)) ) (property "Footprint" "Capacitor_SMD:C_0603_1608Metric" (at 58.42 115.57 0) @@ -2212,7 +2212,7 @@ (property "Reference" "C1" (at 102.87 33.655 0) (effects (font (size 1.27 1.27)) (justify left)) ) - (property "Value" "4u7" (at 102.87 36.195 0) + (property "Value" "4.7uF" (at 102.87 36.195 0) (effects (font (size 1.27 1.27)) (justify left)) ) (property "Footprint" "Capacitor_SMD:C_0805_2012Metric" (at 100.0252 38.735 0) @@ -2253,8 +2253,8 @@ (property "Part Number" "APTD1608LSURCK" (at 214.63 121.92 0) (effects (font (size 1.27 1.27)) hide) ) - (pin "2" (uuid 455ac6a2-863b-43b8-9272-a864429f15f4)) - (pin "1" (uuid d1a0fde7-e9af-40b6-a11b-543a47a2f89d)) + (pin "2" (uuid 455ac6a2-863b-43b8-9272-a864429f15f8)) + (pin "1" (uuid d1a0fde7-e9af-40b6-a11b-543a47a2f8a1)) (instances (project "noisecard" (path "/2019f223-1307-408a-bf87-dad398f72c59" @@ -2346,7 +2346,7 @@ (property "Reference" "C2" (at 148.59 33.655 0) (effects (font (size 1.27 1.27)) (justify left)) ) - (property "Value" "10u" (at 148.59 36.195 0) + (property "Value" "10uF" (at 148.59 36.195 0) (effects (font (size 1.27 1.27)) (justify left)) ) (property "Footprint" "Capacitor_SMD:C_0805_2012Metric" (at 146.3802 38.735 0) @@ -2387,8 +2387,8 @@ (property "Part Number" "APTD1608LSURCK" (at 214.63 128.27 0) (effects (font (size 1.27 1.27)) hide) ) - (pin "2" (uuid 455ac6a2-863b-43b8-9272-a864429f15f4)) - (pin "1" (uuid d1a0fde7-e9af-40b6-a11b-543a47a2f89d)) + (pin "2" (uuid 455ac6a2-863b-43b8-9272-a864429f15f9)) + (pin "1" (uuid d1a0fde7-e9af-40b6-a11b-543a47a2f8a2)) (instances (project "noisecard" (path "/2019f223-1307-408a-bf87-dad398f72c59" @@ -2442,10 +2442,10 @@ (effects (font (size 1.27 1.27)) hide) ) (pin "1" (uuid bd037501-f2bd-4716-a242-b9d25cf98220)) - (pin "6" (uuid 3278223c-3fa9-40df-bb18-ef628deff05e)) + (pin "6" (uuid 3278223c-3fa9-40df-bb18-ef628deff05f)) (pin "3" (uuid 11f70334-c848-47e5-a24d-625d53901c27)) - (pin "4" (uuid d24118b6-8fda-400d-b722-8d6333a5a013)) - (pin "5" (uuid 175b6856-006f-44d2-9833-9acdf94689ee)) + (pin "4" (uuid d24118b6-8fda-400d-b722-8d6333a5a014)) + (pin "5" (uuid 175b6856-006f-44d2-9833-9acdf94689ef)) (pin "2" (uuid ce0b9538-62de-4592-920a-f74542d66135)) (instances (project "noisecard" @@ -2819,8 +2819,8 @@ (property "Part Number" "APTD1608LSURCK" (at 214.63 147.32 0) (effects (font (size 1.27 1.27)) hide) ) - (pin "2" (uuid 455ac6a2-863b-43b8-9272-a864429f15f4)) - (pin "1" (uuid d1a0fde7-e9af-40b6-a11b-543a47a2f89d)) + (pin "2" (uuid 455ac6a2-863b-43b8-9272-a864429f15fa)) + (pin "1" (uuid d1a0fde7-e9af-40b6-a11b-543a47a2f8a3)) (instances (project "noisecard" (path "/2019f223-1307-408a-bf87-dad398f72c59" @@ -2836,7 +2836,7 @@ (property "Reference" "C4" (at 88.9 32.766 0) (effects (font (size 1.27 1.27)) (justify left)) ) - (property "Value" "330u" (at 88.9 35.306 0) + (property "Value" "330uF" (at 88.9 35.306 0) (effects (font (size 1.27 1.27)) (justify left)) ) (property "Footprint" "Capacitor_Tantalum_SMD:CP_EIA-7343-15_Kemet-W" (at 86.0552 38.735 0) @@ -2915,7 +2915,7 @@ (property "Reference" "L1" (at 135.255 24.765 90) (effects (font (size 1.27 1.27))) ) - (property "Value" "10u" (at 135.255 27.305 90) + (property "Value" "10uH" (at 135.255 27.305 90) (effects (font (size 1.27 1.27))) ) (property "Footprint" "Inductor_SMD:L_0805_2012Metric" (at 135.255 29.845 0) @@ -2956,8 +2956,8 @@ (property "Part Number" "APTD1608LSURCK" (at 214.63 153.67 0) (effects (font (size 1.27 1.27)) hide) ) - (pin "2" (uuid 455ac6a2-863b-43b8-9272-a864429f15f4)) - (pin "1" (uuid d1a0fde7-e9af-40b6-a11b-543a47a2f89d)) + (pin "2" (uuid 455ac6a2-863b-43b8-9272-a864429f15fb)) + (pin "1" (uuid d1a0fde7-e9af-40b6-a11b-543a47a2f8a4)) (instances (project "noisecard" (path "/2019f223-1307-408a-bf87-dad398f72c59" @@ -2973,7 +2973,7 @@ (property "Reference" "C6" (at 245.11 38.7413 0) (effects (font (size 1.27 1.27)) (justify right)) ) - (property "Value" "0.1U" (at 245.11 41.2813 0) + (property "Value" "0.1uF" (at 245.11 41.2813 0) (effects (font (size 1.27 1.27)) (justify right)) ) (property "Footprint" "Capacitor_SMD:C_0603_1608Metric" (at 241.935 39.37 0) @@ -3027,7 +3027,7 @@ (property "Reference" "C5" (at 66.675 90.1763 0) (effects (font (size 1.27 1.27)) (justify right)) ) - (property "Value" "0.1U" (at 66.675 92.7163 0) + (property "Value" "0.1uF" (at 66.675 92.7163 0) (effects (font (size 1.27 1.27)) (justify right)) ) (property "Footprint" "Capacitor_SMD:C_0603_1608Metric" (at 63.5 90.805 0) @@ -3209,8 +3209,8 @@ (property "Part Number" "APTD1608LSURCK" (at 214.63 140.97 0) (effects (font (size 1.27 1.27)) hide) ) - (pin "2" (uuid 455ac6a2-863b-43b8-9272-a864429f15f4)) - (pin "1" (uuid d1a0fde7-e9af-40b6-a11b-543a47a2f89d)) + (pin "2" (uuid 455ac6a2-863b-43b8-9272-a864429f15fc)) + (pin "1" (uuid d1a0fde7-e9af-40b6-a11b-543a47a2f8a5)) (instances (project "noisecard" (path "/2019f223-1307-408a-bf87-dad398f72c59" @@ -3226,7 +3226,7 @@ (property "Reference" "C7" (at 254 38.7413 0) (effects (font (size 1.27 1.27)) (justify right)) ) - (property "Value" "200P" (at 254 41.2813 0) + (property "Value" "200pF" (at 254 41.2813 0) (effects (font (size 1.27 1.27)) (justify right)) ) (property "Footprint" "Capacitor_SMD:C_0603_1608Metric" (at 250.825 39.37 0) @@ -3341,8 +3341,8 @@ (property "Part Number" "APTD1608LSURCK" (at 214.63 109.22 0) (effects (font (size 1.27 1.27)) hide) ) - (pin "2" (uuid 455ac6a2-863b-43b8-9272-a864429f15f4)) - (pin "1" (uuid d1a0fde7-e9af-40b6-a11b-543a47a2f89d)) + (pin "2" (uuid 455ac6a2-863b-43b8-9272-a864429f15fd)) + (pin "1" (uuid d1a0fde7-e9af-40b6-a11b-543a47a2f8a6)) (instances (project "noisecard" (path "/2019f223-1307-408a-bf87-dad398f72c59" From 686e00c9217f47ead88662ab6fd3eb00c6ee24de Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Mon, 10 Jun 2024 08:31:40 -0400 Subject: [PATCH 3/6] pcb firmware working --- board.h | 11 +++++++ main.cpp | 88 +++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 69 insertions(+), 30 deletions(-) diff --git a/board.h b/board.h index 9576dba..98fa567 100644 --- a/board.h +++ b/board.h @@ -59,6 +59,17 @@ #define LINE_USART2_TX PAL_LINE(GPIOA, 14U) +#define LINE_LED0 PAL_LINE(GPIOB, 7U) +#define LINE_LED1 PAL_LINE(GPIOC, 14U) +#define LINE_LED2 PAL_LINE(GPIOC, 15U) +#define LINE_LED3 PAL_LINE(GPIOA, 0U) +#define LINE_LED4 PAL_LINE(GPIOA, 1U) +#define LINE_LED5 PAL_LINE(GPIOA, 2U) +#define LINE_LED6 PAL_LINE(GPIOA, 3U) +#define LINE_LED7 PAL_LINE(GPIOA, 6U) +#define LINE_LED8 PAL_LINE(GPIOB, 0U) +#define LINE_LED9 PAL_LINE(GPIOB, 3U) + /* * I/O ports initial setup, this configuration is established soon after reset * in the initialization code. diff --git a/main.cpp b/main.cpp index f7c8027..c3abd77 100644 --- a/main.cpp +++ b/main.cpp @@ -34,7 +34,7 @@ static constexpr auto MIC_BITS = 18u; static constexpr auto SAMPLE_RATE = 48000u; static constexpr unsigned I2S_BUFSIZ = 1024; -static constexpr unsigned I2S_STRIDE = 32: +static constexpr unsigned I2S_STRIDE = 32; // Calculate reference amplitude value at compile time static const auto MIC_REF_AMPL = sos_t((1 << (MIC_BITS - 1)) - 1) * @@ -45,6 +45,7 @@ static std::array i2sBuffer; static sos_t Leq_sum_sqr (0.f); static unsigned Leq_samples = 0; +static void blinkDb(int db); static void i2sCallback(I2SDriver *i2s); static constexpr unsigned I2SPRval = 16'000'000 / SAMPLE_RATE / 32 / 2; @@ -64,49 +65,78 @@ int main(void) { halInit(); osalSysEnable(); - //osalThreadSleepMilliseconds(2000); - palSetPadMode(GPIOB, 7, PAL_MODE_OUTPUT_PUSHPULL); - palSetPad(GPIOB, 7); - //palSetPadMode(GPIOF, 2, PAL_MODE_UNCONNECTED); + palSetLineMode(LINE_LED0, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED0); + palSetLineMode(LINE_LED1, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED1); + palSetLineMode(LINE_LED2, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED2); + palSetLineMode(LINE_LED3, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED3); + palSetLineMode(LINE_LED4, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED4); + palSetLineMode(LINE_LED5, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED5); + palSetLineMode(LINE_LED6, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED6); + palSetLineMode(LINE_LED7, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED7); + palSetLineMode(LINE_LED8, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED8); + palSetLineMode(LINE_LED9, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED9); palSetLineMode(LINE_I2S_SD, PAL_MODE_ALTERNATE(0)); palSetLineMode(LINE_I2S_WS, PAL_MODE_ALTERNATE(0)); palSetLineMode(LINE_I2S_CK, PAL_MODE_ALTERNATE(0)); - //palSetLineMode(LINE_USART2_TX, PAL_MODE_ALTERNATE(1)); - - //sdStart(&SD2, NULL); - //sdWrite(&SD2, (uint8_t *)"Noisemeter\n", 11); - //osalThreadSleepMilliseconds(2); + i2sReady.store(true); i2sStart(&I2SD1, &i2sConfig); i2sStartExchange(&I2SD1); + // Microphone warmup time + osalThreadSleepMilliseconds(140); + // Reach filter delay steady state + i2sReady.store(false); + osalThreadSleepMilliseconds(120); + // Discard initial readings + Leq_sum_sqr = 0.f; + Leq_samples = 0; - uint8_t strbuf[7] = { 0, 0, 0, 'd', 'B', '\n', '\0' }; for (;;) { i2sReady.store(false); SCB->SCR |= SCB_SCR_SLEEPONEXIT_Msk; __WFI(); - //palSetPad(GPIOB, 7); - const sos_t Leq_RMS = qfp_fsqrt(Leq_sum_sqr / qfp_uint2float(Leq_samples)); + const auto sum_sqr = std::exchange(Leq_sum_sqr, sos_t(0.f)); + const auto count = std::exchange(Leq_samples, 0); + const sos_t Leq_RMS = qfp_fsqrt(sum_sqr / qfp_uint2float(count)); const sos_t Leq_dB = MIC_OFFSET_DB + MIC_REF_DB + sos_t(20.f) * qfp_flog10(Leq_RMS / MIC_REF_AMPL); - Leq_sum_sqr = sos_t(0.f); - Leq_samples = 0; - - auto n = std::clamp(qfp_float2int(Leq_dB), 0, 999); - palClearPad(GPIOB, 7); - osalThreadSleepMicroseconds(200); - palSetPad(GPIOB, 7); - //strbuf[2] = n % 10 + '0'; n /= 10; - //strbuf[1] = n % 10 + '0'; n /= 10; - //strbuf[0] = n ? n + '0' : ' '; - //sdWrite(&SD2, strbuf, sizeof(strbuf)); - //osalThreadSleepMilliseconds(2); - ////palClearPad(GPIOB, 7); + const auto n = std::clamp(qfp_float2int(Leq_dB), 0, 999); + blinkDb(n); } } +void blinkDb(int db) +{ + auto line = LINE_LED0; + + if (db < 45) + line = LINE_LED0; + else if (db < 55) + line = LINE_LED1; + else if (db < 65) + line = LINE_LED2; + else if (db < 75) + line = LINE_LED3; + else if (db < 82) + line = LINE_LED4; + else if (db < 87) + line = LINE_LED5; + else if (db < 92) + line = LINE_LED6; + else if (db < 97) + line = LINE_LED7; + else if (db < 102) + line = LINE_LED8; + else + line = LINE_LED9; + + palClearLine(line); + osalThreadSleepMilliseconds(50); + palSetLine(line); +} + __attribute__((section(".data"))) int32_t fixsample(uint32_t s) { return (int32_t)(((s & 0xFFFF) << 16) | (s >> 16)) >> (32 - MIC_BITS); @@ -118,7 +148,6 @@ void i2sCallback(I2SDriver *i2s) if (i2sReady.load()) return; - //palSetPad(GPIOB, 7); const auto halfsize = i2sBuffer.size() / 2; const auto source = i2sBuffer.data() + (i2sIsBufferComplete(i2s) ? halfsize : 0); auto samples = reinterpret_cast(source); @@ -134,11 +163,10 @@ void i2sCallback(I2SDriver *i2s) Leq_sum_sqr += WEIGHTING.filter_sum_sqr(samps); Leq_samples += samps.size(); - // Wakeup main thread for dB calculation every second - if (Leq_samples >= SAMPLE_RATE / I2S_STRIDE) { + // Wakeup main thread for dB calculation every half second + if (Leq_samples >= SAMPLE_RATE / I2S_STRIDE / 2) { i2sReady.store(true); SCB->SCR &= ~SCB_SCR_SLEEPONEXIT_Msk; } - //palClearPad(GPIOB, 7); } From e941048b066dc167ee870545d99532a8d1815e89 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Mon, 10 Jun 2024 13:25:48 -0400 Subject: [PATCH 4/6] stop using stride term --- main.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/main.cpp b/main.cpp index c3abd77..6cb6eaa 100644 --- a/main.cpp +++ b/main.cpp @@ -34,7 +34,7 @@ static constexpr auto MIC_BITS = 18u; static constexpr auto SAMPLE_RATE = 48000u; static constexpr unsigned I2S_BUFSIZ = 1024; -static constexpr unsigned I2S_STRIDE = 32; +static constexpr unsigned I2S_USESIZ = 16; // Calculate reference amplitude value at compile time static const auto MIC_REF_AMPL = sos_t((1 << (MIC_BITS - 1)) - 1) * @@ -152,19 +152,19 @@ void i2sCallback(I2SDriver *i2s) const auto source = i2sBuffer.data() + (i2sIsBufferComplete(i2s) ? halfsize : 0); auto samples = reinterpret_cast(source); std::ranges::copy( - std::views::counted(source, halfsize / I2S_STRIDE) + std::views::counted(source, I2S_USESIZ * 2) | std::ranges::views::stride(2) | std::views::transform([](uint32_t s) { return sos_t(qfp_int2float_asm(fixsample(s))); }), samples); - auto samps = std::views::counted(samples, halfsize / (2 * I2S_STRIDE)); + auto samps = std::views::counted(samples, I2S_USESIZ); // Accumulate Leq sum MIC_EQUALIZER.filter(samps); Leq_sum_sqr += WEIGHTING.filter_sum_sqr(samps); - Leq_samples += samps.size(); + Leq_samples += halfsize / 2; // Wakeup main thread for dB calculation every half second - if (Leq_samples >= SAMPLE_RATE / I2S_STRIDE / 2) { + if (Leq_samples >= SAMPLE_RATE / 2) { i2sReady.store(true); SCB->SCR &= ~SCB_SCR_SLEEPONEXIT_Msk; } From 8745f7c29470ec2267eb5b90e4644499200bafd3 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Mon, 10 Jun 2024 15:21:47 -0400 Subject: [PATCH 5/6] move io config to board.c --- board.c | 17 ++++++++++++++++- board.h | 2 +- main.cpp | 20 ++++++-------------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/board.c b/board.c index 3e81227..3ec4581 100644 --- a/board.c +++ b/board.c @@ -40,6 +40,21 @@ void __early_init(void) { * @note Add your board-specific code, if any. */ void boardInit(void) { - //palSetLineMode(LINE_LED_GREEN, PAL_MODE_OUTPUT_PUSHPULL); + palSetLineMode(LINE_LED0, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED0); + palSetLineMode(LINE_LED1, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED1); + palSetLineMode(LINE_LED2, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED2); + palSetLineMode(LINE_LED3, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED3); + palSetLineMode(LINE_LED4, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED4); + palSetLineMode(LINE_LED5, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED5); + palSetLineMode(LINE_LED6, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED6); + palSetLineMode(LINE_LED7, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED7); + palSetLineMode(LINE_LED8, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED8); + palSetLineMode(LINE_LED9, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED9); + + palSetLineMode(LINE_TP1, PAL_MODE_OUTPUT_PUSHPULL); palClearLine(LINE_TP1); + + palSetLineMode(LINE_I2S_SD, PAL_MODE_ALTERNATE(0)); + palSetLineMode(LINE_I2S_WS, PAL_MODE_ALTERNATE(0)); + palSetLineMode(LINE_I2S_CK, PAL_MODE_ALTERNATE(0)); } diff --git a/board.h b/board.h index 98fa567..801cf6b 100644 --- a/board.h +++ b/board.h @@ -57,7 +57,7 @@ #define LINE_I2S_WS PAL_LINE(GPIOA, 4U) #define LINE_I2S_CK PAL_LINE(GPIOA, 5U) -#define LINE_USART2_TX PAL_LINE(GPIOA, 14U) +#define LINE_TP1 PAL_LINE(GPIOA, 12U) #define LINE_LED0 PAL_LINE(GPIOB, 7U) #define LINE_LED1 PAL_LINE(GPIOC, 14U) diff --git a/main.cpp b/main.cpp index 6cb6eaa..0473d6e 100644 --- a/main.cpp +++ b/main.cpp @@ -66,20 +66,6 @@ int main(void) halInit(); osalSysEnable(); - palSetLineMode(LINE_LED0, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED0); - palSetLineMode(LINE_LED1, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED1); - palSetLineMode(LINE_LED2, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED2); - palSetLineMode(LINE_LED3, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED3); - palSetLineMode(LINE_LED4, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED4); - palSetLineMode(LINE_LED5, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED5); - palSetLineMode(LINE_LED6, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED6); - palSetLineMode(LINE_LED7, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED7); - palSetLineMode(LINE_LED8, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED8); - palSetLineMode(LINE_LED9, PAL_MODE_OUTPUT_PUSHPULL); palSetLine(LINE_LED9); - palSetLineMode(LINE_I2S_SD, PAL_MODE_ALTERNATE(0)); - palSetLineMode(LINE_I2S_WS, PAL_MODE_ALTERNATE(0)); - palSetLineMode(LINE_I2S_CK, PAL_MODE_ALTERNATE(0)); - i2sReady.store(true); i2sStart(&I2SD1, &i2sConfig); i2sStartExchange(&I2SD1); @@ -95,7 +81,9 @@ int main(void) for (;;) { i2sReady.store(false); SCB->SCR |= SCB_SCR_SLEEPONEXIT_Msk; + //palClearLine(LINE_TP1); __WFI(); + //palSetLine(LINE_TP1); const auto sum_sqr = std::exchange(Leq_sum_sqr, sos_t(0.f)); const auto count = std::exchange(Leq_samples, 0); @@ -148,6 +136,8 @@ void i2sCallback(I2SDriver *i2s) if (i2sReady.load()) return; + //palSetLine(LINE_TP1); + const auto halfsize = i2sBuffer.size() / 2; const auto source = i2sBuffer.data() + (i2sIsBufferComplete(i2s) ? halfsize : 0); auto samples = reinterpret_cast(source); @@ -168,5 +158,7 @@ void i2sCallback(I2SDriver *i2s) i2sReady.store(true); SCB->SCR &= ~SCB_SCR_SLEEPONEXIT_Msk; } + + //palClearLine(LINE_TP1); } From 67bdf76f1942eb210a52e5f0a96d92ed3238f456 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Tue, 11 Jun 2024 17:01:40 -0400 Subject: [PATCH 6/6] make led show a little longer --- main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 0473d6e..de9e093 100644 --- a/main.cpp +++ b/main.cpp @@ -121,7 +121,7 @@ void blinkDb(int db) line = LINE_LED9; palClearLine(line); - osalThreadSleepMilliseconds(50); + osalThreadSleepMilliseconds(100); palSetLine(line); }