Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/thirdpin/libopencm3_cpp_extensions.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorana_lazareva <n_lazareva@mail.ru>2018-03-27 14:39:23 +0300
committerana_lazareva <n_lazareva@mail.ru>2018-03-27 14:39:23 +0300
commit94a0a5d153a31734e46bd11edf2ee6dd8befc392 (patch)
tree6e95e1b6aae16c20765f224e802f37b9bbb9f461
parentba5cce6d9e2448fb59a67c28e259051ffc0695e2 (diff)
MISC: [cm3cpp_usart] init() and deinit() function added
-rw-r--r--cm3cpp_usart.cpp220
-rw-r--r--cm3cpp_usart.h394
2 files changed, 315 insertions, 299 deletions
diff --git a/cm3cpp_usart.cpp b/cm3cpp_usart.cpp
index 16ded74..80f14c2 100644
--- a/cm3cpp_usart.cpp
+++ b/cm3cpp_usart.cpp
@@ -1,105 +1,115 @@
-/*
- * This file is part of the libopencm3_cpp_extensions project.
- * hosted at http://github.com/thirdpin/libopencm3_cpp_extensions
- *
- * Copyright (C) 2016 Third Pin LLC
- * Written by Anastasiia Lazareva <a.lazareva@thirdpin.ru>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/*
-USART C++ Wrapper of libopencm3 library for STM32F2, STM32F4
-*/
-
-#include "cm3cpp_usart.h"
-
-namespace cm3cpp {
-
-namespace usart {
-
-Usart::Usart(LowLevelConfig config, Settings settings)
-{
- if( config.rx.port)
- {
- Gpio rx(config.rx);
- rx.mode_setup(Gpio::Mode::ALTERNATE_FUNCTION, Gpio::PullMode::NO_PULL);
- rx.set_output_options(Gpio::OutputType::PUSH_PULL, Gpio::Speed::MEDIUM_25MHz);
- if ((config.usart_number >= 1) && (config.usart_number <= 3))
- rx.set_af(Gpio::AltFuncNumber::AF7);
- else //if ((config.usart_number >= 4) && (config.usart_number <= 6))
- rx.set_af(Gpio::AltFuncNumber::AF8);
- }
-
- if( config.tx.port)
- {
- Gpio tx(config.tx);
- tx.mode_setup(Gpio::Mode::ALTERNATE_FUNCTION, Gpio::PullMode::NO_PULL);
- tx.set_output_options(Gpio::OutputType::PUSH_PULL, Gpio::Speed::MEDIUM_25MHz);
- if ((config.usart_number >= 1) && (config.usart_number <= 3))
- tx.set_af(Gpio::AltFuncNumber::AF7);
- else //if ((config.usart_number >= 4) && (config.usart_number <= 6))
- tx.set_af(Gpio::AltFuncNumber::AF8);
- }
-
-
-
- switch (config.usart_number)
- {
- case 1:
- _usart = USART1;
- _usart_nvic = NVIC_USART1_IRQ;
- break;
- case 2:
- _usart = USART2;
- _usart_nvic = NVIC_USART2_IRQ;
- break;
- case 3:
- _usart = USART3;
- _usart_nvic = NVIC_USART3_IRQ;
- break;
- case 4:
- _usart = UART4;
- _usart_nvic = NVIC_UART4_IRQ;
- break;
- case 5:
- _usart = UART5;
- _usart_nvic = NVIC_UART5_IRQ;
- break;
- case 6:
- _usart = USART6;
- _usart_nvic = NVIC_USART6_IRQ;
- break;
- }
-
- set_settings(settings);
- usart_enable(_usart);
-
- nvic_set_priority(_usart_nvic, config.nvic_priority);
- nvic_enable_irq(_usart_nvic);
-}
-
-void Usart::set_settings(Settings settings)
-{
- usart_set_baudrate(_usart, settings.baud_rate);
- usart_set_databits(_usart, settings.word_length);
- usart_set_stopbits(_usart, settings.stop_bits);
- usart_set_mode(_usart, settings.mode);
- usart_set_parity(_usart, settings.parity);
- usart_set_flow_control(_usart, settings.flow_control);
-}
-
-} // namespace usart
-
-} // namespace cm3cpp
+/*
+ * This file is part of the libopencm3_cpp_extensions project.
+ * hosted at http://github.com/thirdpin/libopencm3_cpp_extensions
+ *
+ * Copyright (C) 2016 Third Pin LLC
+ * Written by Anastasiia Lazareva <a.lazareva@thirdpin.ru>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/*
+USART C++ Wrapper of libopencm3 library for STM32F2, STM32F4
+*/
+
+#include "cm3cpp_usart.h"
+
+namespace cm3cpp {
+
+namespace usart {
+
+Usart::Usart(LowLevelConfig config, Settings settings)
+{
+ init(config, settings);
+}
+
+void Usart::init(LowLevelConfig config, Settings settings)
+{
+ if (config.rx.port)
+ {
+ _rx.init(config.rx);
+ _rx.mode_setup(Gpio::Mode::ALTERNATE_FUNCTION, Gpio::PullMode::NO_PULL);
+ _rx.set_output_options(Gpio::OutputType::PUSH_PULL, Gpio::Speed::MEDIUM_25MHz);
+ if ((config.usart_number >= 1) && (config.usart_number <= 3))
+ _rx.set_af(Gpio::AltFuncNumber::AF7);
+ else //if ((config.usart_number >= 4) && (config.usart_number <= 6))
+ _rx.set_af(Gpio::AltFuncNumber::AF8);
+ }
+
+ if (config.tx.port)
+ {
+ _tx.init(config.tx);
+ _tx.mode_setup(Gpio::Mode::ALTERNATE_FUNCTION, Gpio::PullMode::NO_PULL);
+ _tx.set_output_options(Gpio::OutputType::PUSH_PULL, Gpio::Speed::MEDIUM_25MHz);
+ if ((config.usart_number >= 1) && (config.usart_number <= 3))
+ _tx.set_af(Gpio::AltFuncNumber::AF7);
+ else //if ((config.usart_number >= 4) && (config.usart_number <= 6))
+ _tx.set_af(Gpio::AltFuncNumber::AF8);
+ }
+
+ switch (config.usart_number)
+ {
+ case 1:
+ _usart = USART1;
+ _usart_nvic = NVIC_USART1_IRQ;
+ break;
+ case 2:
+ _usart = USART2;
+ _usart_nvic = NVIC_USART2_IRQ;
+ break;
+ case 3:
+ _usart = USART3;
+ _usart_nvic = NVIC_USART3_IRQ;
+ break;
+ case 4:
+ _usart = UART4;
+ _usart_nvic = NVIC_UART4_IRQ;
+ break;
+ case 5:
+ _usart = UART5;
+ _usart_nvic = NVIC_UART5_IRQ;
+ break;
+ case 6:
+ _usart = USART6;
+ _usart_nvic = NVIC_USART6_IRQ;
+ break;
+ }
+
+ set_settings(settings);
+ usart_enable(_usart);
+
+ nvic_set_priority(_usart_nvic, config.nvic_priority);
+ nvic_enable_irq(_usart_nvic);
+}
+
+void Usart::deinit()
+{
+ usart_disable(_usart);
+ nvic_disable_irq(_usart_nvic);
+ _tx.mode_setup(Gpio::Mode::INPUT, Gpio::PullMode::NO_PULL);
+}
+
+void Usart::set_settings(Settings settings)
+{
+ usart_set_baudrate(_usart, settings.baud_rate);
+ usart_set_databits(_usart, settings.word_length);
+ usart_set_stopbits(_usart, settings.stop_bits);
+ usart_set_mode(_usart, settings.mode);
+ usart_set_parity(_usart, settings.parity);
+ usart_set_flow_control(_usart, settings.flow_control);
+}
+
+} // namespace usart
+
+} // namespace cm3cpp
diff --git a/cm3cpp_usart.h b/cm3cpp_usart.h
index 3cc27c4..a954dda 100644
--- a/cm3cpp_usart.h
+++ b/cm3cpp_usart.h
@@ -1,194 +1,200 @@
-/*
- * This file is part of the libopencm3_cpp_extensions project.
- * hosted at http://github.com/thirdpin/libopencm3_cpp_extensions
- *
- * Copyright (C) 2016 Third Pin LLC
- * Written by Anastasiia Lazareva <a.lazareva@thirdpin.ru>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/*
-USART C++ Wrapper of libopencm3 library for STM32F2, STM32F4
-*/
-
-#ifndef CM3CPP_USART_H_
-#define CM3CPP_USART_H_
-
-/**************************************************************************************************
- * GENERAL INCLUDES
- *************************************************************************************************/
-#include <cstdint>
-/**************************************************************************************************
- * LIBOPENCM3 INCLUDES
- *************************************************************************************************/
-#include <libopencm3/stm32/usart.h>
-#ifdef STM32F2
-#include <libopencm3/stm32/f2/nvic.h>
-#endif
-#ifdef STM32F4
-#include <libopencm3/stm32/f4/nvic.h>
-#endif
-/**************************************************************************************************
- * CM3CPP INCLUDES
- *************************************************************************************************/
-#include "private/assert.h"
-#include <cm3cpp_config.h>
-#include "cm3cpp_gpio.h"
-
-namespace cm3cpp {
-
-namespace usart {
-
-enum DataBits : uint8_t
-{
- _8 = 8,
- _9 = 9
-};
-
-enum Mode : uint16_t
-{
- RX = USART_MODE_RX,
- TX = USART_MODE_TX,
- RX_TX = USART_MODE_TX_RX
-};
-
-enum StopBits : uint16_t
-{
- _0_5 = USART_STOPBITS_0_5,
- _1 = USART_STOPBITS_1,
- _1_5 = USART_STOPBITS_1_5,
- _2 = USART_STOPBITS_2
-};
-
-enum Parity : uint16_t
-{
- PAR_NONE = USART_PARITY_NONE,
- PAR_EVEN = USART_PARITY_EVEN,
- PAR_ODD = USART_PARITY_ODD
-};
-
-enum FlowControl : uint16_t
-{
- NONE = USART_FLOWCONTROL_NONE,
- RTS = USART_FLOWCONTROL_RTS,
- CTS = USART_FLOWCONTROL_CTS,
- RTS_CTS = USART_FLOWCONTROL_RTS_CTS
-};
-
-class Usart
-{
-public:
- using Gpio = gpio::Gpio;
-
- struct Settings
- {
- uint32_t baud_rate;
- DataBits word_length;
- StopBits stop_bits;
- Parity parity;
- Mode mode;
- FlowControl flow_control;
- };
-
- struct LowLevelConfig
- {
- uint8_t usart_number;
- gpio::Gpio::Pinout tx;
- gpio::Gpio::Pinout rx;
- uint8_t nvic_priority;
- };
-
- Usart(LowLevelConfig config, Settings settings);
-
- void set_settings(Settings settings);
-
- bool interrupt_source_rx() {
- return (((USART_CR1(_usart) & USART_CR1_RXNEIE) != 0) &&
- usart_get_flag(_usart, USART_SR_RXNE));
- }
-
- bool interrupt_source_tx() {
- return (((USART_CR1(_usart) & USART_CR1_TXEIE) != 0) &&
- usart_get_flag(_usart, USART_SR_TXE));
- }
-
- bool interrupt_source_TC()
- {
- return (((USART_CR1(_usart) & USART_CR1_TCIE) != 0) &&
- usart_get_flag(_usart, USART_SR_TC));
- }
-
- void enable_irq() {
- nvic_enable_irq(_usart_nvic);
- }
-
- void disable_irq() {
- nvic_disable_irq(_usart_nvic);
- }
-
- void enable_rx_interrupt()
- {
- usart_enable_rx_interrupt(_usart);
- }
-
- void enable_tx_interrupt() {
- usart_enable_tx_interrupt(_usart);
- }
-
- void enable_tc_interrupt()
- {
- USART_CR1(_usart) |= USART_CR1_TCIE;
- }
-
- void disable_rx_interrupt()
- {
- usart_disable_rx_interrupt(_usart);
- }
-
- void disable_tx_interrupt() {
- usart_disable_tx_interrupt(_usart);
- }
-
- void disable_tc_interrupt()
- {
- USART_CR1(_usart) &= ~USART_CR1_TCIE;
- }
-
- void write_blocking(uint16_t data) {
- usart_send_blocking(_usart, data);
- }
-
- void write(uint16_t data) {
- usart_send(_usart, data);
- }
-
- uint16_t read() {
- return usart_recv(_usart);
- }
-
- uint16_t read_blocking() {
- return usart_recv_blocking(_usart);
- }
-
-protected:
- uint32_t _usart;
- uint32_t _usart_nvic;
-};
-
-} // namespace usart
-
-} // namespace cm3cpp
-
-#endif /* CM3CPP_USART_H_ */
+/*
+ * This file is part of the libopencm3_cpp_extensions project.
+ * hosted at http://github.com/thirdpin/libopencm3_cpp_extensions
+ *
+ * Copyright (C) 2016 Third Pin LLC
+ * Written by Anastasiia Lazareva <a.lazareva@thirdpin.ru>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/*
+USART C++ Wrapper of libopencm3 library for STM32F2, STM32F4
+*/
+
+#ifndef CM3CPP_USART_H_
+#define CM3CPP_USART_H_
+
+/**************************************************************************************************
+ * GENERAL INCLUDES
+ *************************************************************************************************/
+#include <cstdint>
+/**************************************************************************************************
+ * LIBOPENCM3 INCLUDES
+ *************************************************************************************************/
+#include <libopencm3/stm32/usart.h>
+#ifdef STM32F2
+#include <libopencm3/stm32/f2/nvic.h>
+#endif
+#ifdef STM32F4
+#include <libopencm3/stm32/f4/nvic.h>
+#endif
+/**************************************************************************************************
+ * CM3CPP INCLUDES
+ *************************************************************************************************/
+#include "private/assert.h"
+#include <cm3cpp_config.h>
+#include "cm3cpp_gpio.h"
+
+namespace cm3cpp {
+
+namespace usart {
+
+enum DataBits : uint8_t
+{
+ _8 = 8,
+ _9 = 9
+};
+
+enum Mode : uint16_t
+{
+ RX = USART_MODE_RX,
+ TX = USART_MODE_TX,
+ RX_TX = USART_MODE_TX_RX
+};
+
+enum StopBits : uint16_t
+{
+ _0_5 = USART_STOPBITS_0_5,
+ _1 = USART_STOPBITS_1,
+ _1_5 = USART_STOPBITS_1_5,
+ _2 = USART_STOPBITS_2
+};
+
+enum Parity : uint16_t
+{
+ PAR_NONE = USART_PARITY_NONE,
+ PAR_EVEN = USART_PARITY_EVEN,
+ PAR_ODD = USART_PARITY_ODD
+};
+
+enum FlowControl : uint16_t
+{
+ NONE = USART_FLOWCONTROL_NONE,
+ RTS = USART_FLOWCONTROL_RTS,
+ CTS = USART_FLOWCONTROL_CTS,
+ RTS_CTS = USART_FLOWCONTROL_RTS_CTS
+};
+
+class Usart
+{
+public:
+ using Gpio = gpio::Gpio;
+
+ struct Settings
+ {
+ uint32_t baud_rate;
+ DataBits word_length;
+ StopBits stop_bits;
+ Parity parity;
+ Mode mode;
+ FlowControl flow_control;
+ };
+
+ struct LowLevelConfig
+ {
+ uint8_t usart_number;
+ gpio::Gpio::Pinout tx;
+ gpio::Gpio::Pinout rx;
+ uint8_t nvic_priority;
+ };
+
+ Usart(LowLevelConfig config, Settings settings);
+
+ void init(LowLevelConfig config, Settings settings);
+
+ void deinit();
+
+ void set_settings(Settings settings);
+
+ bool interrupt_source_rx() {
+ return (((USART_CR1(_usart) & USART_CR1_RXNEIE) != 0) &&
+ usart_get_flag(_usart, USART_SR_RXNE));
+ }
+
+ bool interrupt_source_tx() {
+ return (((USART_CR1(_usart) & USART_CR1_TXEIE) != 0) &&
+ usart_get_flag(_usart, USART_SR_TXE));
+ }
+
+ bool interrupt_source_TC()
+ {
+ return (((USART_CR1(_usart) & USART_CR1_TCIE) != 0) &&
+ usart_get_flag(_usart, USART_SR_TC));
+ }
+
+ void enable_irq() {
+ nvic_enable_irq(_usart_nvic);
+ }
+
+ void disable_irq() {
+ nvic_disable_irq(_usart_nvic);
+ }
+
+ void enable_rx_interrupt()
+ {
+ usart_enable_rx_interrupt(_usart);
+ }
+
+ void enable_tx_interrupt() {
+ usart_enable_tx_interrupt(_usart);
+ }
+
+ void enable_tc_interrupt()
+ {
+ USART_CR1(_usart) |= USART_CR1_TCIE;
+ }
+
+ void disable_rx_interrupt()
+ {
+ usart_disable_rx_interrupt(_usart);
+ }
+
+ void disable_tx_interrupt() {
+ usart_disable_tx_interrupt(_usart);
+ }
+
+ void disable_tc_interrupt()
+ {
+ USART_CR1(_usart) &= ~USART_CR1_TCIE;
+ }
+
+ void write_blocking(uint16_t data) {
+ usart_send_blocking(_usart, data);
+ }
+
+ void write(uint16_t data) {
+ usart_send(_usart, data);
+ }
+
+ uint16_t read() {
+ return usart_recv(_usart);
+ }
+
+ uint16_t read_blocking() {
+ return usart_recv_blocking(_usart);
+ }
+
+protected:
+ Gpio _rx;
+ Gpio _tx;
+ uint32_t _usart;
+ uint32_t _usart_nvic;
+};
+
+} // namespace usart
+
+} // namespace cm3cpp
+
+#endif /* CM3CPP_USART_H_ */