diff options
author | Victor Pushkarev <corvinus.v@gmail.com> | 2018-06-21 18:45:22 +0300 |
---|---|---|
committer | Victor Pushkarev <corvinus.v@gmail.com> | 2018-06-21 18:45:22 +0300 |
commit | 330a274c588fbefcfc7e3dd3b5fbef86394e5b80 (patch) | |
tree | 22ee1d96f5624140014e556fc0b1ec2357992172 | |
parent | 08cfb7f7e568e9d1102144982d0f8b656bcce50b (diff) |
IMPR: add custom timer (systick)
-rw-r--r-- | cm3cpp_i2c.cpp | 96 | ||||
-rw-r--r-- | cm3cpp_i2c.h | 8 | ||||
-rw-r--r-- | cm3cpp_systick.cpp | 43 |
3 files changed, 77 insertions, 70 deletions
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..84528b6 100644 --- a/cm3cpp_systick.cpp +++ b/cm3cpp_systick.cpp @@ -29,6 +29,8 @@ extern "C" { void CM3CPP_SYS_TICK_INT_FUNC(void); } #include "libopencm3/stm32/timer.h" +#include <libopencm3/stm32/rcc.h> +#include <libopencm3_cpp_extensions/irq/cm3cpp_irq.h> #else #define CM3CPP_SYS_TICK_INT_FUNC sys_tick_handler #endif @@ -38,10 +40,10 @@ volatile uint32_t counter; void CM3CPP_SYS_TICK_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++; - } +// if (timer_get_flag(CM3CPP_INT_SOURCE, TIM_SR_UIF)) { +// timer_clear_flag(CM3CPP_INT_SOURCE, TIM_SR_UIF); +// counter++; +// } #else counter++; #endif @@ -61,24 +63,43 @@ 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) { +#if CM3CPP_ENABLE_CUSTOM_SYSTICK_SOURCE == 1 + + 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, ((rcc_apb1_frequency * 2) / 1000) - 1); // 1 kHz + timer_disable_preload(CM3CPP_INT_SOURCE); + timer_continuous_mode(CM3CPP_INT_SOURCE); + timer_set_period(CM3CPP_INT_SOURCE, 0xffff); // 65535 or (UINT32_MAX - 1) + timer_enable_counter(CM3CPP_INT_SOURCE); +// nvic_enable_irq(NVIC_TIM1_CC_IRQ); +// timer_enable_irq(TIM1, TIM_DIER_UIE); + +#else + //by default sets up a timer to create 1ms ticks (div = 1000) + //at system clock 120mhz. counter = 0; systick_set_reload(CM3CPP_SYSTEM_CORE_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 +126,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 +144,7 @@ bool Counter::start() { if(_mode == CYCLE) return false; - _saved = counter; + _saved = get_counter(); _is_active = true; return true; } |