Skip to content

Commit

Permalink
[stm32] Use :platform:rcc module for clock enable
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Feb 28, 2019
1 parent e50b9f2 commit 4a82a94
Show file tree
Hide file tree
Showing 18 changed files with 57 additions and 132 deletions.
6 changes: 3 additions & 3 deletions src/modm/platform/clock/stm32/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# -----------------------------------------------------------------------------


def init(module):
module.name = "clock"
module.parent = "platform"
Expand All @@ -20,7 +21,7 @@ def prepare(module, options):
if not options[":target"].has_driver("rcc:stm32*"):
return False

module.depends(":cmsis:device", ":utils", ":platform:clock.cortex")
module.depends(":cmsis:device", ":utils", ":platform:clock.cortex", ":platform:rcc")
if "stm32f100" in options[":target"].partname:
module.depends(":architecture:clock")
return True
Expand All @@ -29,10 +30,9 @@ def build(env):
device = env[":target"]
driver = device.get_driver("rcc")

properties = device.properties
properties = {}
properties["target"] = target = device.identifier
properties["partname"] = device.partname
properties["driver"] = driver
properties["core"] = device.get_driver("core")["type"]

if target["family"] in ["f1", "f3", "f4"]:
Expand Down
17 changes: 4 additions & 13 deletions src/modm/platform/dma/stm32/dma_impl.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef MODM_STM32_DMA{{ id }}_HPP
# error "Don't include this file directly, use 'dma_{{ id }}.hpp' instead!"
#endif
#include <modm/platform/clock/rcc.hpp>

%% if target["family"] == "f4"
%% set streams = range(0,8)
Expand All @@ -27,24 +28,14 @@
void
modm::platform::Dma{{ id }}::enable()
{
%% if target["family"] == "f4"
RCC->AHB1ENR |= RCC_AHB1ENR_DMA{{ id }}EN;
RCC->AHB1RSTR |= RCC_AHB1RSTR_DMA{{ id }}RST;
RCC->AHB1RSTR &= ~RCC_AHB1RSTR_DMA{{ id }}RST;
%% elif target["family"] == "f3"
RCC->AHBENR |= RCC_AHBENR_DMA{{ id }}EN;
%% endif
Rcc::enable<Peripheral::Dma{{id}}>();
Rcc::reset<Peripheral::Dma{{id}}>();
}

void
modm::platform::Dma{{ id }}::disable()
{
%% if target["family"] == "f4"
RCC->AHB1ENR &= ~RCC_AHB1ENR_DMA{{ id }}EN;
%% elif target["family"] == "f3"
RCC->AHBENR &= ~RCC_AHBENR_DMA{{ id }}EN;
%% endif

Rcc::disable<Peripheral::Dma{{id}}>();
}


Expand Down
2 changes: 1 addition & 1 deletion src/modm/platform/dma/stm32/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def prepare(module, options):
if device.identifier["family"] not in ["f3", "f4"]:
return False

module.depends(":cmsis:device")
module.depends(":cmsis:device", ":platform:rcc")

for instance in device.get_driver("dma")["instance"]:
module.add_submodule(Instance(int(instance)))
Expand Down
14 changes: 4 additions & 10 deletions src/modm/platform/fsmc/stm32/fsmc.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <stdint.h>
#include "../device.hpp"
#include <modm/platform/clock/rcc.hpp>

namespace modm
{
Expand Down Expand Up @@ -298,21 +299,14 @@ namespace modm
static inline void
enable()
{
%% if target["family"] in ["f2", "f4", "f7"]
RCC->AHB3ENR |= RCC_AHB3ENR_{{ FMC }}EN;
%% else
RCC->AHBENR |= RCC_AHBENR_{{ FMC }}EN;
%% endif
Rcc::enable<Peripheral::{{ FMC | capitalize }}>();
Rcc::reset<Peripheral::{{ FMC | capitalize }}>();
}

static inline void
disable()
{
%% if target["family"] in ["f2", "f4", "f7"]
RCC->AHB3ENR &= ~RCC_AHB3ENR_{{ FMC }}EN;
%% else
RCC->AHBENR &= ~RCC_AHBENR_{{ FMC }}EN;
%% endif
Rcc::disable<Peripheral::{{ FMC | capitalize }}>();
}
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/modm/platform/fsmc/stm32/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ def prepare(module, options):
if not device.has_driver("fsmc:stm32*"):
return False

module.depends(":cmsis:device")
module.depends(":cmsis:device", ":platform:rcc")
return True

def build(env):
device = env[":target"]

properties = device.properties
properties = {}
properties["target"] = device.identifier

target = device.identifier
Expand Down
9 changes: 3 additions & 6 deletions src/modm/platform/i2c/stm32-extended/i2c_master.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include <modm/architecture/interface/atomic_lock.hpp>
#include <modm/architecture/interface/interrupt.hpp>
#include <modm/container.hpp>
#include <modm/platform/clock/rcc.hpp>

%% if not shared_interrupt
MODM_ISR_DECL(I2C{{ id }}_ER);
Expand Down Expand Up @@ -495,12 +496,8 @@ MODM_ISR(I2C{{ id }}_ER)
void
modm::platform::I2cMaster{{ id }}::initializeWithPrescaler(uint32_t timingRegisterValue)
{
// Enable clock
RCC->APB1ENR{{aid}} |= RCC_APB1ENR{{aid}}_I2C{{ id }}EN;

// reset module
RCC->APB1RSTR{{aid}} |= RCC_APB1RSTR{{aid}}_I2C{{ id }}RST;
RCC->APB1RSTR{{aid}} &= ~RCC_APB1RSTR{{aid}}_I2C{{ id }}RST;
Rcc::enable<Peripheral::I2c{{id}}>();
Rcc::reset<Peripheral::I2c{{id}}>();

// Disable the I2C peripheral which causes a software reset
I2C{{ id }}->CR1 &= ~I2C_CR1_PE;
Expand Down
11 changes: 1 addition & 10 deletions src/modm/platform/i2c/stm32-extended/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,7 @@ class Instance(Module):
properties = device.properties
properties["target"] = target = device.identifier
properties["id"] = self.instance

if target["family"] == "l4":
properties["aid"] = "2" if self.instance == 4 else "1"
else:
properties["aid"] = ""

if target["family"] == "f0" or target["family"] == "l0":
properties["shared_interrupt"] = True
else:
properties["shared_interrupt"] = False
properties["shared_interrupt"] = "0" in target.family

env.substitutions = properties
env.outbasepath = "modm/src/modm/platform/i2c"
Expand Down
5 changes: 3 additions & 2 deletions src/modm/platform/i2c/stm32/i2c_master.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
#include <modm/architecture/interface/atomic_lock.hpp>
#include <modm/architecture/interface/interrupt.hpp>
#include <modm/container.hpp>
#include <modm/platform/clock/rcc.hpp>

MODM_ISR_DECL(I2C{{ id }}_ER);

Expand Down Expand Up @@ -591,8 +592,8 @@ modm::platform::I2cMaster{{ id }}::initializeWithPrescaler(uint8_t peripheralFre
{
// no reset, since we want to keep the transaction attached!

// Enable clock
RCC->APB1ENR |= RCC_APB1ENR_I2C{{ id }}EN;
Rcc::enable<Peripheral::I2c{{id}}>();
Rcc::reset<Peripheral::I2c{{id}}>();

I2C{{ id }}->CR1 = I2C_CR1_SWRST; // reset module
I2C{{ id }}->CR1 = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/modm/platform/random/stm32/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def prepare(module, options):
if not options[":target"].has_driver("rng:stm32"):
return False

module.depends(":cmsis:device")
module.depends(":cmsis:device", ":platform:rcc")
return True

def build(env):
Expand Down
3 changes: 3 additions & 0 deletions src/modm/platform/random/stm32/random_number_generator.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include <stdint.h>
#include "../device.hpp"
#include <modm/platform/clock/rcc.hpp>


namespace modm
Expand All @@ -37,6 +38,8 @@ public:
static inline void
enable()
{
Rcc::enable<Peripheral::Rng>();
Rcc::reset<Peripheral::Rng>();
RNG->CR = RNG_CR_RNGEN;
}

Expand Down
8 changes: 3 additions & 5 deletions src/modm/platform/spi/stm32/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
def get_properties(env):
device = env[":target"]
driver = device.get_driver("spi")
properties = device.properties
properties = {}
properties["target"] = device.identifier
properties["driver"] = driver
properties["features"] = driver["feature"] if "feature" in driver else []
return properties

Expand All @@ -36,8 +35,6 @@ class Instance(Module):
def build(self, env):
properties = get_properties(env)
properties["id"] = self.instance
properties["apb"] = "2" if self.instance in [1, 4, 5, 6] else "1"
properties["apb_post"] = "1" if properties["target"]["family"] in ["l4"] and self.instance != 1 else ""

env.substitutions = properties
env.outbasepath = "modm/src/modm/platform/spi"
Expand All @@ -62,7 +59,8 @@ def prepare(module, options):
":architecture:register",
":architecture:spi",
":cmsis:device",
":platform:gpio")
":platform:gpio",
":platform:rcc")

for driver in device.get_all_drivers("spi:stm32"):
for instance in driver["instance"]:
Expand Down
12 changes: 5 additions & 7 deletions src/modm/platform/spi/stm32/spi_hal_impl.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,21 @@
#ifndef MODM_STM32_SPI_HAL{{ id }}_HPP
# error "Don't include this file directly, use 'spi_hal{{ id }}.hpp' instead!"
#endif
#include <modm/platform/clock/rcc.hpp>

void inline
modm::platform::SpiHal{{ id }}::enable()
{
// enable clock
RCC->APB{{ apb }}ENR{{ apb_post }} |= RCC_APB{{ apb }}ENR{{ apb_post }}_SPI{{ id }}EN;
// reset spi
RCC->APB{{ apb }}RSTR{{ apb_post }} |= RCC_APB{{ apb }}RSTR{{ apb_post }}_SPI{{ id }}RST;
RCC->APB{{ apb }}RSTR{{ apb_post }} &= ~RCC_APB{{ apb }}RSTR{{ apb_post }}_SPI{{ id }}RST;
Rcc::enable<Peripheral::Spi{{id}}>();
Rcc::reset<Peripheral::Spi{{id}}>();
SPI{{ id }}->CR1 |= SPI_CR1_SPE; // SPI Enable
}

void inline
modm::platform::SpiHal{{ id }}::disable()
{
// disable clock
RCC->APB{{ apb }}ENR{{ apb_post }} &= ~RCC_APB{{ apb }}ENR{{ apb_post }}_SPI{{ id }}EN;
SPI{{ id }}->CR1 = 0;
Rcc::disable<Peripheral::Spi{{id}}>();
}

void inline
Expand Down
14 changes: 5 additions & 9 deletions src/modm/platform/timer/stm32/advanced.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,24 @@
// ----------------------------------------------------------------------------

#include "timer_{{ id }}.hpp"
#include <modm/platform/clock/rcc.hpp>

// ----------------------------------------------------------------------------
void
modm::platform::Timer{{ id }}::enable()
{
// enable clock
RCC->APB2ENR |= RCC_APB2ENR_TIM{{ id }}EN;

// reset timer
RCC->APB2RSTR |= RCC_APB2RSTR_TIM{{ id }}RST;
RCC->APB2RSTR &= ~RCC_APB2RSTR_TIM{{ id }}RST;
Rcc::enable<Peripheral::Tim{{id}}>();
Rcc::reset<Peripheral::Tim{{id}}>();
}

void
modm::platform::Timer{{ id }}::disable()
{
// disable clock
RCC->APB2ENR &= ~RCC_APB2ENR_TIM{{ id }}EN;

TIM{{ id }}->CR1 = 0;
TIM{{ id }}->DIER = 0;
TIM{{ id }}->CCER = 0;

Rcc::disable<Peripheral::Tim{{id}}>();
}

// ----------------------------------------------------------------------------
Expand Down
13 changes: 5 additions & 8 deletions src/modm/platform/timer/stm32/basic.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,22 @@
// ----------------------------------------------------------------------------

#include "timer_{{ id }}.hpp"
#include <modm/platform/clock/rcc.hpp>

void
modm::platform::Timer{{ id }}::enable()
{
// enable clock
RCC->APB1ENR{{ apb_post }} |= RCC_APB1ENR{{ apb_post }}_TIM{{ id }}EN;
// reset timer
RCC->APB1RSTR{{ apb_post }} |= RCC_APB1RSTR{{ apb_post }}_TIM{{ id }}RST;
RCC->APB1RSTR{{ apb_post }} &= ~RCC_APB1RSTR{{ apb_post }}_TIM{{ id }}RST;
Rcc::enable<Peripheral::Tim{{id}}>();
Rcc::reset<Peripheral::Tim{{id}}>();
}

void
modm::platform::Timer{{ id }}::disable()
{
// disable clock
RCC->APB1ENR{{ apb_post }} &= ~RCC_APB1ENR{{ apb_post }}_TIM{{ id }}EN;

TIM{{ id }}->CR1 = 0;
TIM{{ id }}->DIER = 0;

Rcc::disable<Peripheral::Tim{{id}}>();
}

void
Expand Down
37 changes: 6 additions & 31 deletions src/modm/platform/timer/stm32/general_purpose.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,31 @@
*/
// ----------------------------------------------------------------------------

%% if target["family"] in ["l4"]
%% if id in range(2,8)
%% set apb = "APB1"
%% set apb_post = "1"
%% elif id in [1,8] or id in [15,16,17]
%% set apb = "APB2"
%% set apb_post = ""
%% else
#error "Don't know on which APB this timer {{ id }} is."
%% endif
%% else
%% if id in range(9,12) or id in range(15,21)
%% set apb = "APB2"
%% elif id in range(1,9) or id in range (12,15)
%% set apb = "APB1"
%% else
#error "Don't know on which APB this timer {{ id }} is."
%% endif
%% endif

#include "timer_{{ id }}.hpp"
#include <modm/platform/clock/rcc.hpp>

// ----------------------------------------------------------------------------
void
modm::platform::Timer{{ id }}::clockEnable()
{
// enable clock
RCC->{{ apb }}ENR{{ apb_post }} |= RCC_{{ apb }}ENR{{ apb_post }}_TIM{{ id }}EN;
Rcc::enable<Peripheral::Tim{{id}}>();
}

void
modm::platform::Timer{{ id }}::enable()
{
// enable clock
RCC->{{ apb }}ENR{{ apb_post }} |= RCC_{{ apb }}ENR{{ apb_post }}_TIM{{ id }}EN;

// reset timer
RCC->{{ apb }}RSTR{{ apb_post }} |= RCC_{{ apb }}RSTR{{ apb_post }}_TIM{{ id }}RST;
RCC->{{ apb }}RSTR{{ apb_post }} &= ~RCC_{{ apb }}RSTR{{ apb_post }}_TIM{{ id }}RST;
Rcc::enable<Peripheral::Tim{{id}}>();
Rcc::reset<Peripheral::Tim{{id}}>();
}

void
modm::platform::Timer{{ id }}::disable()
{
// disable clock
RCC->{{ apb }}ENR{{ apb_post }} &= ~RCC_{{ apb }}ENR{{ apb_post }}_TIM{{ id }}EN;

TIM{{ id }}->CR1 = 0;
TIM{{ id }}->DIER = 0;
TIM{{ id }}->CCER = 0;

Rcc::disable<Peripheral::Tim{{id}}>();
}

// ----------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 4a82a94

Please sign in to comment.