diff options
author | mamilla11 <n_lazareva@mail.ru> | 2018-08-22 08:00:30 +0300 |
---|---|---|
committer | mamilla11 <n_lazareva@mail.ru> | 2018-08-22 08:00:30 +0300 |
commit | 334f844f69d6bb855a5413d59a6d304e21d9c168 (patch) | |
tree | e91649495f485f3511eb430f858b0b875f8b581d | |
parent | 0059daa4a9e4d0cdd0d3914c2efbd6fda52168dc (diff) | |
parent | e41ac8b05ba6b8e293827e4d9593fe58fbd6edb4 (diff) |
Merge branch 'feature/some-refactoring' of https://github.com/thirdpin/libopencm3_cpp_extensions into feature/some-refactoring
-rw-r--r-- | cm3cpp_config_template.h | 22 | ||||
-rw-r--r-- | cm3cpp_i2c.cpp | 96 | ||||
-rw-r--r-- | cm3cpp_i2c.h | 8 | ||||
-rw-r--r-- | cm3cpp_systick.cpp | 62 | ||||
-rw-r--r-- | cm3cpp_systick.h | 2 | ||||
-rw-r--r-- | cm3cpp_usart.cpp | 56 | ||||
-rw-r--r-- | cm3cpp_usart.h | 39 |
7 files changed, 170 insertions, 115 deletions
diff --git a/cm3cpp_config_template.h b/cm3cpp_config_template.h index 978212c..9ad1a2a 100644 --- a/cm3cpp_config_template.h +++ b/cm3cpp_config_template.h @@ -1,24 +1,26 @@ #ifndef CM3CPP_CONFIG_H_ #define CM3CPP_CONFIG_H_ -#define CM3CPP_ENABLE_CUSTOM_SYSTICK_SOURCE 0 +//*** Must be changed according to your RCC configuration +#define CM3CPP_SYSTEM_CORE_CLOCK (uint32_t)120000000 + +#define CM3CPP_ENABLE_CUSTOM_SYSTICK_SOURCE 1 //*** Or you can use custom systick source (like timer) #if (CM3CPP_ENABLE_CUSTOM_SYSTICK_SOURCE == 1) -#define CM3CPP_INT_SOURCE TIM1 -#define CM3CPP_SYS_TICK_INT_FUNC tim1_isr +#define CM3CPP_TIMER_N 12 +#define CM3CPP_SYSTICK_CLOCK (30000000 * 2) +#define CM3CPP_SYSTICK_CLOCK_DIV 1000 // 1 kHz +#define CM3CPP_SYSTICK_PERIOD 0xffff +#else +#define CM3CPP_SYSTICK_CLOCK_DIV 1000 // 1 kHz +#define CM3CPP_SYSTICK_CLOCK CM3CPP_SYSTEM_CORE_CLOCK #endif //*** User assert function define -#define CM3CPP_ASSERT( x ) configASSERT( x ) - -//*** Must be changed according to your RCC configuration -#define CM3CPP_SYSTEM_CORE_CLOCK (uint32_t)120000000 -#define CM3CPP_SYSTEM_CORE_CLOCK_DIV (uint32_t)1000 +#define CM3_ASSERT( x ) if( ( x ) == 0 ) user_assert_func( __FILE__, __LINE__ ) //*** This option enable destructors for classes using a heap allocation // inside. If you can't destruct an object it prevents a memory leak. #define CM3CPP_ENABLE_IMPLISIT_DESTRUCTOR_CALLS 0 - - #endif /* CM3CPP_CONFIG_H_ */ diff --git a/cm3cpp_i2c.cpp b/cm3cpp_i2c.cpp index a8cbefd..845a8d4 100644 --- a/cm3cpp_i2c.cpp +++ b/cm3cpp_i2c.cpp @@ -29,7 +29,7 @@ namespace cm3cpp { namespace i2c { -I2c::I2c(Config i2c_conf) +I2c::I2c(Config i2c_conf) : _counter_ms(new systick::Counter(systick::Counter::Mode::ONE_SHOT, MAX_TRANSMIT_TIME_MS)) { _config = i2c_conf; @@ -49,12 +49,6 @@ I2c::I2c(Config i2c_conf) sda.mode_setup(Gpio::Mode::ALTERNATE_FUNCTION, Gpio::PullMode::NO_PULL); sda.set_output_options(Gpio::OutputType::OPEN_DRAIN, Gpio::Speed::LOW_2MHz); sda.set_af(Gpio::AltFuncNumber::AF4); - -#ifndef FREERTOS - _counter_ms->init(systick::Counter::Mode::ONE_SHOT, MAX_TRANSMIT_TIME_MS); -#else - //_counter_ms = new timers::OneShotTimer(MAX_TRANSMIT_TIME_MS); -#endif } void I2c::reset() { @@ -168,25 +162,25 @@ auto I2c::master_transfer(MasterTransferCfg cfg) -> Result uint32_t reg __attribute__((unused)); Result result = OK; - //_counter_ms->start(); + _counter_ms->start(); _send_start(); - while (_get_flag_status(MASTER_MODE_SELECTED) == Result::ERROR); -// { -// if(_counter_ms->timeout()) { -// result = TIMEOUT; -// break; -// } -// } + while (_get_flag_status(MASTER_MODE_SELECTED) == Result::ERROR) + { + if(_counter_ms->timeout()) { + result = TIMEOUT; + break; + } + } _send_7bit_address(cfg.device_address, WRITE); - while (_get_flag_status(MASTER_TRANSMITTER_MODE_SELECTED) == Result::ERROR); -// { -// if(_counter_ms->timeout()) { -// result = TIMEOUT; -// break; -// } -// } + while (_get_flag_status(MASTER_TRANSMITTER_MODE_SELECTED) == Result::ERROR) + { + if(_counter_ms->timeout()) { + result = TIMEOUT; + break; + } + } uint8_t index = 0; @@ -194,13 +188,13 @@ auto I2c::master_transfer(MasterTransferCfg cfg) -> Result { _send_data(cfg.write_buf[index]); - while (_get_flag_status(MASTER_BYTE_TRANSMITTED) == Result::ERROR); -// { -// if(_counter_ms->timeout()) { -// result = TIMEOUT; -// break; -// } -// } + while (_get_flag_status(MASTER_BYTE_TRANSMITTED) == Result::ERROR) + { + if(_counter_ms->timeout()) { + result = TIMEOUT; + break; + } + } cfg.write_len--; index++; @@ -208,24 +202,24 @@ auto I2c::master_transfer(MasterTransferCfg cfg) -> Result if(cfg.read_len != 0) { _send_start(); - while (_get_flag_status(MASTER_MODE_SELECTED) == Result::ERROR); -// { -// if(_counter_ms->timeout()) { -// result = TIMEOUT; -// break; -// } -// } + while (_get_flag_status(MASTER_MODE_SELECTED) == Result::ERROR) + { + if(_counter_ms->timeout()) { + result = TIMEOUT; + break; + } + } _enable_ack(); _send_7bit_address(cfg.device_address, READ); - while (_get_flag_status(MASTER_RECEIVER_MODE_SELECTED) == Result::ERROR); -// { -// if(_counter_ms->timeout()) { -// result = TIMEOUT; -// break; -// } -// } + while (_get_flag_status(MASTER_RECEIVER_MODE_SELECTED) == Result::ERROR) + { + if(_counter_ms->timeout()) { + result = TIMEOUT; + break; + } + } uint8_t size_to_read = cfg.read_len; index = 0; @@ -235,13 +229,13 @@ auto I2c::master_transfer(MasterTransferCfg cfg) -> Result size_to_read--; if (!size_to_read) _disable_ack(); - while (_get_flag_status(MASTER_BYTE_RECEIVED) == Result::ERROR); -// { -// if(_counter_ms->timeout()) { -// result = TIMEOUT; -// break; -// } -// } + while (_get_flag_status(MASTER_BYTE_RECEIVED) == Result::ERROR) + { + if(_counter_ms->timeout()) { + result = TIMEOUT; + break; + } + } uint8_t data = _get_data(); cfg.read_buf[index] = data; @@ -250,7 +244,7 @@ auto I2c::master_transfer(MasterTransferCfg cfg) -> Result } _send_stop(); - //_counter_ms->stop(); + _counter_ms->stop(); return result; } diff --git a/cm3cpp_i2c.h b/cm3cpp_i2c.h index 4daf405..49cb775 100644 --- a/cm3cpp_i2c.h +++ b/cm3cpp_i2c.h @@ -32,11 +32,7 @@ I2C C++ Wrapper of libopencm3 library for STM32F2, STM32F4 #include "cm3cpp_gpio.h" #include "private/assert.h" -#ifndef FREERTOS #include "cm3cpp_systick.h" -#else -#include <timers/OneShotTimer.hpp> -#endif namespace cm3cpp { @@ -196,11 +192,7 @@ private: uint32_t _i2c; Config _config; -#ifndef FREERTOS systick::Counter *_counter_ms; -#else - //timers::OneShotTimer *_counter_ms; -#endif void _send_start(); void _send_stop(); diff --git a/cm3cpp_systick.cpp b/cm3cpp_systick.cpp index de73941..2ae45f3 100644 --- a/cm3cpp_systick.cpp +++ b/cm3cpp_systick.cpp @@ -24,28 +24,37 @@ SYSTICK implementation, public interface */ #include "cm3cpp_systick.h" + +#define __EXPAND2(a,b) a ## b +#define __EXPAND3(a,b,c) a ## b ## c + +#define __INT_SOURCE(n) __EXPAND2(TIM, n) +#define __RCC(n) __EXPAND2(rcc_periph_clken::RCC_TIM, n) +#define __SYSTICK_INT_FUNC(n) __EXPAND3(tim, n, _isr) + +#define CM3CPP_INT_SOURCE __INT_SOURCE(CM3CPP_TIMER_N) +#define CM3CPP_INT_SOURCE_RCC __RCC(CM3CPP_TIMER_N) +#define CM3CPP_SYSTICK_INT_FUNC __SYSTICK_INT_FUNC(CM3CPP_TIMER_N) + #if CM3CPP_ENABLE_CUSTOM_SYSTICK_SOURCE == 1 extern "C" { - void CM3CPP_SYS_TICK_INT_FUNC(void); + void CM3CPP_SYSTICK_INT_FUNC(void); } -#include "libopencm3/stm32/timer.h" +#include <libopencm3/stm32/timer.h> +#include <libopencm3/stm32/rcc.h> +#include "irq/cm3cpp_irq.h" #else -#define CM3CPP_SYS_TICK_INT_FUNC sys_tick_handler +#define CM3CPP_SYSTICK_INT_FUNC sys_tick_handler #endif volatile uint32_t counter; -void CM3CPP_SYS_TICK_INT_FUNC(void) +#if CM3CPP_ENABLE_CUSTOM_SYSTICK_SOURCE != 1 +void CM3CPP_SYSTICK_INT_FUNC(void) { -#if CM3CPP_ENABLE_CUSTOM_SYSTICK_SOURCE == 1 - if (timer_get_flag(CM3CPP_INT_SOURCE, TIM_SR_UIF)) { - timer_clear_flag(CM3CPP_INT_SOURCE, TIM_SR_UIF); - counter++; - } -#else counter++; -#endif } +#endif void delay_nop(uint32_t count) { @@ -61,24 +70,37 @@ namespace systick { void delay_systick(uint32_t ms) { - uint32_t time = counter; - while((counter - time) < ms); + uint32_t time = get_counter(); + while((get_counter() - time) < ms); } -//by default sets up a timer to create 1ms ticks (div = 1000) -//at system clock 120mhz. -void init(uint32_t div) +void init(uint32_t clock_div) { +#if CM3CPP_ENABLE_CUSTOM_SYSTICK_SOURCE == 1 + rcc_periph_clock_enable(CM3CPP_INT_SOURCE_RCC); + timer_reset(CM3CPP_INT_SOURCE); + timer_set_mode(CM3CPP_INT_SOURCE, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); + timer_set_prescaler(CM3CPP_INT_SOURCE, (CM3CPP_SYSTICK_CLOCK / clock_div) - 1); + timer_disable_preload(CM3CPP_INT_SOURCE); + timer_continuous_mode(CM3CPP_INT_SOURCE); + timer_set_period(CM3CPP_INT_SOURCE, CM3CPP_SYSTICK_PERIOD); + timer_enable_counter(CM3CPP_INT_SOURCE); +#else counter = 0; - systick_set_reload(CM3CPP_SYSTEM_CORE_CLOCK / div); + systick_set_reload(CM3CPP_SYSTEM_CORE_CLOCK / clock_div); systick_set_clocksource(STK_CSR_CLKSOURCE_AHB); systick_counter_enable(); systick_interrupt_enable(); +#endif } uint32_t get_counter() { +#if CM3CPP_ENABLE_CUSTOM_SYSTICK_SOURCE == 1 + return timer_get_counter(CM3CPP_INT_SOURCE); +#else return counter; +#endif } Counter::Counter(Mode mode, uint32_t period) @@ -105,11 +127,11 @@ void Counter::init(Mode mode, uint32_t period) bool Counter::timeout() { - if (_is_active && ((counter - _saved) >= _period)) { + if (_is_active && ((get_counter() - _saved) >= _period)) { switch (_mode) { case CYCLE: - _saved = counter; + _saved = get_counter(); return true; case ONE_SHOT: return true; @@ -123,7 +145,7 @@ bool Counter::start() { if(_mode == CYCLE) return false; - _saved = counter; + _saved = get_counter(); _is_active = true; return true; } diff --git a/cm3cpp_systick.h b/cm3cpp_systick.h index 25df69d..9ada945 100644 --- a/cm3cpp_systick.h +++ b/cm3cpp_systick.h @@ -38,7 +38,7 @@ namespace cm3cpp { namespace systick { -void init(uint32_t div = CM3CPP_SYSTEM_CORE_CLOCK_DIV); +void init(uint32_t div = CM3CPP_SYSTICK_CLOCK_DIV); uint32_t get_counter(); void delay_systick(uint32_t ms); diff --git a/cm3cpp_usart.cpp b/cm3cpp_usart.cpp index 80f14c2..40b98bd 100644 --- a/cm3cpp_usart.cpp +++ b/cm3cpp_usart.cpp @@ -36,27 +36,7 @@ Usart::Usart(LowLevelConfig config, Settings 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);
- }
+ _mode = settings.mode;
switch (config.usart_number)
{
@@ -89,6 +69,32 @@ void Usart::init(LowLevelConfig config, Settings settings) set_settings(settings);
usart_enable(_usart);
+ if (_mode == Mode::RX or _mode == Mode::RX_TX)
+ {
+ _rx.init(config.rx);
+
+ if ((config.usart_number >= 1) && (config.usart_number <= 3))
+ _rx.set_af(Gpio::AltFuncNumber::AF7);
+ else
+ _rx.set_af(Gpio::AltFuncNumber::AF8);
+
+ _rx.mode_setup(Gpio::Mode::ALTERNATE_FUNCTION, Gpio::PullMode::NO_PULL);
+ _rx.set_output_options(Gpio::OutputType::PUSH_PULL, Gpio::Speed::MEDIUM_25MHz);
+ }
+
+ if (_mode == Mode::TX or _mode == Mode::RX_TX)
+ {
+ _tx.init(config.tx);
+
+ if ((config.usart_number >= 1) && (config.usart_number <= 3))
+ _tx.set_af(Gpio::AltFuncNumber::AF7);
+ else
+ _tx.set_af(Gpio::AltFuncNumber::AF8);
+
+ _tx.mode_setup(Gpio::Mode::ALTERNATE_FUNCTION, Gpio::PullMode::NO_PULL);
+ _tx.set_output_options(Gpio::OutputType::PUSH_PULL, Gpio::Speed::MEDIUM_25MHz);
+ }
+
nvic_set_priority(_usart_nvic, config.nvic_priority);
nvic_enable_irq(_usart_nvic);
}
@@ -97,11 +103,17 @@ void Usart::deinit() {
usart_disable(_usart);
nvic_disable_irq(_usart_nvic);
- _tx.mode_setup(Gpio::Mode::INPUT, Gpio::PullMode::NO_PULL);
+
+ if (_mode == Mode::RX or _mode == Mode::RX_TX)
+ _rx.mode_setup(Gpio::Mode::INPUT, Gpio::PullMode::NO_PULL);
+
+ if (_mode == Mode::TX or _mode == Mode::RX_TX)
+ _tx.mode_setup(Gpio::Mode::INPUT, Gpio::PullMode::NO_PULL);
}
void Usart::set_settings(Settings settings)
{
+ _mode = settings.mode;
usart_set_baudrate(_usart, settings.baud_rate);
usart_set_databits(_usart, settings.word_length);
usart_set_stopbits(_usart, settings.stop_bits);
diff --git a/cm3cpp_usart.h b/cm3cpp_usart.h index 77ec82e..d4b2995 100644 --- a/cm3cpp_usart.h +++ b/cm3cpp_usart.h @@ -47,6 +47,8 @@ USART C++ Wrapper of libopencm3 library for STM32F2, STM32F4 #include <cm3cpp_config.h>
#include "cm3cpp_gpio.h"
+#include "irq/cm3cpp_irq.h"
+
namespace cm3cpp {
namespace usart {
@@ -179,12 +181,37 @@ public: usart_disable_tx_interrupt(_usart);
}
- void disable_tc_interrupt()
- {
+ void disable_tc_interrupt() {
USART_CR1(_usart) &= ~USART_CR1_TCIE;
}
- void write_blocking(uint16_t data) {
+ bool is_framing_error() {
+ return (USART_SR(_usart) & USART_SR_FE) != 0;
+ }
+
+ bool is_overrun_error() {
+ return (USART_SR(_usart) & USART_SR_IDLE) != 0;
+ }
+
+ bool is_any_error_occurred()
+ {
+ return (USART_SR(_usart) &
+ (USART_SR_ORE | USART_SR_FE | USART_SR_PE | USART_SR_NE)) != 0;
+ }
+
+ uint32_t get_sr_reg() {
+ return USART_SR(_usart);
+ }
+
+ bool is_data_received() {
+ return (USART_SR(_usart) & USART_SR_RXNE) != 0;
+ }
+
+ bool is_data_sended() {
+ return (USART_SR(_usart) & USART_SR_TXE) != 0;
+ }
+
+ void write_blocking(uint16_t data) {
usart_send_blocking(_usart, data);
}
@@ -200,11 +227,17 @@ public: return usart_recv_blocking(_usart);
}
+ auto get_irq() {
+ return static_cast<Interrupt>(_usart_nvic);
+ }
+
protected:
Gpio _rx;
Gpio _tx;
uint32_t _usart;
uint32_t _usart_nvic;
+
+ Mode _mode;
};
} // namespace usart
|