From 14990c5bfe9d53afae066c6ab3f704eebac3bbbc Mon Sep 17 00:00:00 2001 From: Thomas Petig Date: Mon, 24 Jun 2024 02:07:44 +0200 Subject: [PATCH] refactored code --- include/adc.hpp | 17 ++++++++++++++ include/led.hpp | 32 ++++++++++++++++++++++++++ include/scd30.hpp | 9 +------- include/uart.hpp | 11 +++++++++ src/main.cpp | 57 +++++++++-------------------------------------- 5 files changed, 72 insertions(+), 54 deletions(-) create mode 100644 include/adc.hpp create mode 100644 include/led.hpp create mode 100644 include/uart.hpp diff --git a/include/adc.hpp b/include/adc.hpp new file mode 100644 index 0000000..84c1363 --- /dev/null +++ b/include/adc.hpp @@ -0,0 +1,17 @@ +#pragma once + +#define ADC_CELL 0 +#define ADC_SYS 3 + +/** +** Return ADC value in Volt +** +** \return a float containing the ADC value. +*/ +float adc_read_V(int ch) +{ + const float conversion_factor = 3.3f / (1 << 12) * 2.0; + adc_select_input(ch); + uint16_t result = adc_read(); + return result * conversion_factor; +} diff --git a/include/led.hpp b/include/led.hpp new file mode 100644 index 0000000..6a063de --- /dev/null +++ b/include/led.hpp @@ -0,0 +1,32 @@ +#pragma once + +#define LED0_PIN 19 +#define LED1_PIN 20 +#define LED2_PIN 21 +#define LED3_PIN 22 + + +void led_set(uint led) +{ + gpio_put(led, 1); +} + +void led_reset(uint led) +{ + gpio_put(led, 0); +} + +void led_init(uint led) +{ + gpio_init(led); + gpio_set_dir(led, GPIO_OUT); +} + + +uint led_toggle(uint led) +{ + uint val = gpio_get(led); + val = (~val & 0x1); + gpio_put(led, val); + return val; +} diff --git a/include/scd30.hpp b/include/scd30.hpp index cd93e56..f7b6cec 100644 --- a/include/scd30.hpp +++ b/include/scd30.hpp @@ -3,6 +3,7 @@ #include "hardware/uart.h" #include "crc.h" #include "measurement.hpp" +#include "uart.hpp" /** Inter-character timeout in us */ #define MODBUS_INTERCHAR_TIMEOUT 750 @@ -36,14 +37,6 @@ void on_timeout(unsigned int alarm_num) { } constexpr int alarm_scd30 = 0; -template< int ID > uart_inst_t* uart_id(); -template<> uart_inst_t* uart_id<0> () {return uart0;} -template<> uart_inst_t* uart_id<1> () {return uart1;} - -template< int ID > unsigned int uart_irq(); -template<> unsigned int uart_irq<0> () {return UART0_IRQ;} -template<> unsigned int uart_irq<1> () {return UART1_IRQ;} - template< int UART, int TX, int RX > class SCD30 { public: diff --git a/include/uart.hpp b/include/uart.hpp new file mode 100644 index 0000000..1e5ca65 --- /dev/null +++ b/include/uart.hpp @@ -0,0 +1,11 @@ +#pragma once +#include + + +template< int ID > uart_inst_t* uart_id(); +template<> uart_inst_t* uart_id<0> () {return uart0;} +template<> uart_inst_t* uart_id<1> () {return uart1;} + +template< int ID > unsigned int uart_irq(); +template<> unsigned int uart_irq<0> () {return UART0_IRQ;} +template<> unsigned int uart_irq<1> () {return UART1_IRQ;} diff --git a/src/main.cpp b/src/main.cpp index 456d30e..2cf123a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,12 +4,15 @@ #include #include "pico/stdlib.h" -#include "hardware/uart.h" #include "hardware/adc.h" +#include "hardware/uart.h" +#include "hardware/watchdog.h" #include "scd30.hpp" #include "pico/util/queue.h" #include "pico/multicore.h" +#include "led.hpp" +#include "adc.hpp" // We are using pins 0 and 1, but see the GPIO function select table in the // datasheet for information on which other pins can be used. @@ -28,39 +31,12 @@ #define UART1_TX_PIN 4 #define UART1_RX_PIN 5 -#define LED0_PIN 19 -#define LED1_PIN 20 -#define LED2_PIN 21 -#define LED3_PIN 22 - -#define ADC_CELL 0 -#define ADC_SYS 3 queue_t res_queue; -/** -** Return ADC value in Volt -** -** \return a float containing the ADC value. -*/ -float adc_read_V(int ch) -{ - const float conversion_factor = 3.3f / (1 << 12) * 2.0; - adc_select_input(ch); - uint16_t result = adc_read(); - return result * conversion_factor; -} -uint led_toggle(uint led) -{ - uint val = gpio_get(led); - val = (~val & 0x1); - gpio_put(led, val); - return val; -} - void core1_entry() { - gpio_init(LED0_PIN); - gpio_set_dir(LED0_PIN, GPIO_OUT); + watchdog_enable(5000,1); + led_init(LED0_PIN); printf("booting core 1\r\n"); gpio_put(LED0_PIN, 1); sleep_ms(1000); @@ -73,6 +49,7 @@ void core1_entry() { print_meas(meas); printf("\n"); sleep_ms(10); + watchdog_update(); //modbus_rx_enable(); } } @@ -80,11 +57,6 @@ void core1_entry() { int main() { -// stdio_init_all(); -// uart_init(UART0_ID, UART0_BAUD); -// gpio_set_function(UART0_TX_PIN, GPIO_FUNC_UART); -// gpio_set_function(UART0_RX_PIN, GPIO_FUNC_UART); - adc_init(); adc_gpio_init(26); adc_gpio_init(29); @@ -92,12 +64,9 @@ int main() { modbus_init(); printf("REBOOT\r\n"); - gpio_init(LED1_PIN); - gpio_init(LED2_PIN); - gpio_init(LED3_PIN); - gpio_set_dir(LED1_PIN, GPIO_OUT); - gpio_set_dir(LED2_PIN, GPIO_OUT); - gpio_set_dir(LED3_PIN, GPIO_OUT); + led_init(LED1_PIN); + led_init(LED2_PIN); + led_init(LED3_PIN); @@ -126,15 +95,12 @@ int main() { multicore_launch_core1(&core1_entry); while (true) { + led_toggle(LED2_PIN); gpio_put(LED1_PIN, 1); - gpio_put(LED2_PIN, 0); scd.txMeas(); gpio_put(LED1_PIN, 0); - gpio_put(LED2_PIN, 1); led_toggle(LED3_PIN); sleep_ms(2000); -// printf("DATA,%f,", result1 * conversion_factor); -// printf("%f,", result2 * conversion_factor); // scd.printMeas(false);//true); //scd.printStatus(); if (scd.check_resp()) { @@ -146,7 +112,6 @@ int main() { printf("ERROR\r\n"); scd.printStatus(); } -// scd.check_resp(); } }