Skip to content

Commit

Permalink
Initial refactoring of STM32F4 to STM32 common
Browse files Browse the repository at this point in the history
for compatibility check, added support of STM32F205
  • Loading branch information
svhelper committed Sep 8, 2018
1 parent 9ced5b9 commit 72b779a
Show file tree
Hide file tree
Showing 29 changed files with 1,593 additions and 382 deletions.
16 changes: 15 additions & 1 deletion aprinter/base/Preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
#ifndef APRINTER_PREPROCESSOR_H
#define APRINTER_PREPROCESSOR_H

#define APRINTER_JOIN_HELPER(x, y) x##y
#define APRINTER_JOIN_HELPER2(x, y) x##y
#define APRINTER_JOIN_HELPER1(x, y) APRINTER_JOIN_HELPER2(x, y)
#define APRINTER_JOIN_HELPER(x, y) APRINTER_JOIN_HELPER1(x, y)
#define APRINTER_JOIN(x, y) APRINTER_JOIN_HELPER(x, y)

#define AMBRO_STRINGIFY_HELPER(x) #x
Expand All @@ -35,6 +37,18 @@

#define APRINTER_ARRAY_LEN(arr) (sizeof(arr) / sizeof((arr)[0]))


#define APRINTER_HASPAREN(definition) APRINTER_HASPAREN1(definition)
#define APRINTER_HASPAREN_HELPER(...) 0,
#define APRINTER_HASPAREN1(definition) APRINTER_HASPAREN2(APRINTER_HASPAREN_HELPER definition)
#define APRINTER_HASPAREN2(comma_or_junk) APRINTER_HASPAREN3(comma_or_junk YES, NO)
#define APRINTER_HASPAREN3(ignore, check, ...) check

#define APRINTER_IF_DEFINED_NO(...)
#define APRINTER_IF_DEFINED_YES(...) __VA_ARGS__
#define APRINTER_IF_DEFINED(deinition) APRINTER_JOIN(APRINTER_IF_DEFINED_, APRINTER_HASPAREN(deinition))


#include "Preprocessor_MacroMap.h"

#define APRINTER_USE_TYPE1(namespace, type_name) using type_name = typename namespace::type_name;
Expand Down
8 changes: 4 additions & 4 deletions aprinter/board/stm32f429i_discovery_pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
#ifndef AMBROLIB_STM32F429I_DISCOVERY_PINS_H
#define AMBROLIB_STM32F4DISCOVERY_PINS_H

#include <aprinter/hal/stm32/Stm32f4Pins.h>
#include <aprinter/hal/stm32/Stm32Pins.h>

namespace APrinter {

using DiscoveryPinLedGreen = Stm32f4Pin<Stm32f4PortG, 13>;
using DiscoveryPinLedRed = Stm32f4Pin<Stm32f4PortG, 14>;
using DiscoveryPinButtonUser = Stm32f4Pin<Stm32f4PortA, 0>;
using DiscoveryPinLedGreen = Stm32Pin<Stm32PortG, 13>;
using DiscoveryPinLedRed = Stm32Pin<Stm32PortG, 14>;
using DiscoveryPinButtonUser = Stm32Pin<Stm32PortA, 0>;

/*
List of internally connected pins:
Expand Down
12 changes: 6 additions & 6 deletions aprinter/board/stm32f4discovery_pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
#ifndef AMBROLIB_STM32F4DISCOVERY_PINS_H
#define AMBROLIB_STM32F4DISCOVERY_PINS_H

#include <aprinter/hal/stm32/Stm32f4Pins.h>
#include <aprinter/hal/stm32/Stm32Pins.h>

namespace APrinter {

using DiscoveryPinLedGreen = Stm32f4Pin<Stm32f4PortD, 12>;
using DiscoveryPinLedOrange = Stm32f4Pin<Stm32f4PortD, 13>;
using DiscoveryPinLedRed = Stm32f4Pin<Stm32f4PortD, 14>;
using DiscoveryPinLedBlue = Stm32f4Pin<Stm32f4PortD, 15>;
using DiscoveryPinButtonUser = Stm32f4Pin<Stm32f4PortA, 0>;
using DiscoveryPinLedGreen = Stm32Pin<Stm32PortD, 12>;
using DiscoveryPinLedOrange = Stm32Pin<Stm32PortD, 13>;
using DiscoveryPinLedRed = Stm32Pin<Stm32PortD, 14>;
using DiscoveryPinLedBlue = Stm32Pin<Stm32PortD, 15>;
using DiscoveryPinButtonUser = Stm32Pin<Stm32PortA, 0>;

/*
List of internally connected pins or otherwise possibly used:
Expand Down
137 changes: 80 additions & 57 deletions aprinter/hal/stm32/Stm32f4Adc.h → aprinter/hal/stm32/Stm32Adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef AMBROLIB_STM32F4_ADC_H
#define AMBROLIB_STM32F4_ADC_H
#ifndef AMBROLIB_STM32_ADC_H
#define AMBROLIB_STM32_ADC_H

#include <stdint.h>

Expand All @@ -37,12 +37,12 @@
#include <aprinter/meta/ServiceUtils.h>
#include <aprinter/base/Object.h>
#include <aprinter/base/DebugObject.h>
#include <aprinter/hal/stm32/Stm32f4Pins.h>
#include <aprinter/hal/stm32/Stm32Pins.h>
#include <aprinter/system/InterruptLock.h>

namespace APrinter {

#define STM32F4ADC_DEFINE_SUBADC(AdcDefName, AdcNumber, DmaNumber, DmaStreamNumber, DmaChannelNumber) \
#define STM32ADC_DEFINE_SUBADC(AdcDefName, AdcNumber, DmaNumber, DmaStreamNumber, DmaChannelNumber) \
struct AdcDefName { \
using Number = WrapInt<AdcNumber>; \
static void adc_clk_enable () { __HAL_RCC_ADC##AdcNumber##_CLK_ENABLE(); } \
Expand All @@ -54,7 +54,7 @@ struct AdcDefName { \
};

template <typename Arg>
class Stm32f4Adc {
class Stm32Adc {
using Context = typename Arg::Context;
using ParentObject = typename Arg::ParentObject;
using ParamsPinsList = typename Arg::PinsList;
Expand All @@ -78,60 +78,83 @@ class Stm32f4Adc {
template <int...Numbers>
using AdcNumbers = MakeTypeList<WrapInt<Numbers>...>;

#if defined(STM32F429xx) || defined(STM32F407xx)
STM32F4ADC_DEFINE_SUBADC(AdcDef1, 1, 2, 0, 0)
STM32F4ADC_DEFINE_SUBADC(AdcDef2, 2, 2, 2, 1)
STM32F4ADC_DEFINE_SUBADC(AdcDef3, 3, 2, 1, 2)
#if defined(STM32F205xx)
STM32ADC_DEFINE_SUBADC(AdcDef1, 1, 2, 0, 0)
STM32ADC_DEFINE_SUBADC(AdcDef2, 2, 2, 2, 1)
STM32ADC_DEFINE_SUBADC(AdcDef3, 3, 2, 1, 2)
using AdcDefList = MakeTypeList<AdcDef1, AdcDef2, AdcDef3>;
using PinDefList = MakeTypeList<
TypeDictEntry<Stm32f4Pin<Stm32f4PortF, 3>, AdcMapping<AdcNumbers<3>, 9>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortF, 4>, AdcMapping<AdcNumbers<3>, 14>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortF, 5>, AdcMapping<AdcNumbers<3>, 15>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortF, 6>, AdcMapping<AdcNumbers<3>, 4>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortF, 7>, AdcMapping<AdcNumbers<3>, 5>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortF, 8>, AdcMapping<AdcNumbers<3>, 6>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortF, 9>, AdcMapping<AdcNumbers<3>, 7>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortF, 10>, AdcMapping<AdcNumbers<3>, 8>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortC, 0>, AdcMapping<AdcNumbers<1,2,3>, 10>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortC, 1>, AdcMapping<AdcNumbers<1,2,3>, 11>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortC, 2>, AdcMapping<AdcNumbers<1,2,3>, 12>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortC, 3>, AdcMapping<AdcNumbers<1,2,3>, 13>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortA, 0>, AdcMapping<AdcNumbers<1,2,3>, 0>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortA, 1>, AdcMapping<AdcNumbers<1,2,3>, 1>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortA, 2>, AdcMapping<AdcNumbers<1,2,3>, 2>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortA, 3>, AdcMapping<AdcNumbers<1,2,3>, 3>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortA, 4>, AdcMapping<AdcNumbers<1,2>, 4>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortA, 5>, AdcMapping<AdcNumbers<1,2>, 5>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortA, 6>, AdcMapping<AdcNumbers<1,2>, 6>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortA, 7>, AdcMapping<AdcNumbers<1,2>, 7>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortC, 4>, AdcMapping<AdcNumbers<1,2>, 14>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortC, 5>, AdcMapping<AdcNumbers<1,2>, 15>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortB, 0>, AdcMapping<AdcNumbers<1,2>, 8>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortB, 1>, AdcMapping<AdcNumbers<1,2>, 9>>
TypeDictEntry<Stm32Pin<Stm32PortA, 0>, AdcMapping<AdcNumbers<1,2,3>, 0>>,
TypeDictEntry<Stm32Pin<Stm32PortA, 1>, AdcMapping<AdcNumbers<1,2,3>, 1>>,
TypeDictEntry<Stm32Pin<Stm32PortA, 2>, AdcMapping<AdcNumbers<1,2,3>, 2>>,
TypeDictEntry<Stm32Pin<Stm32PortA, 3>, AdcMapping<AdcNumbers<1,2,3>, 3>>,
TypeDictEntry<Stm32Pin<Stm32PortA, 4>, AdcMapping<AdcNumbers<1,2>, 4>>,
TypeDictEntry<Stm32Pin<Stm32PortA, 5>, AdcMapping<AdcNumbers<1,2>, 5>>,
TypeDictEntry<Stm32Pin<Stm32PortA, 6>, AdcMapping<AdcNumbers<1,2>, 6>>,
TypeDictEntry<Stm32Pin<Stm32PortA, 7>, AdcMapping<AdcNumbers<1,2>, 7>>,
TypeDictEntry<Stm32Pin<Stm32PortB, 0>, AdcMapping<AdcNumbers<1,2>, 8>>,
TypeDictEntry<Stm32Pin<Stm32PortB, 1>, AdcMapping<AdcNumbers<1,2>, 9>>,
TypeDictEntry<Stm32Pin<Stm32PortC, 0>, AdcMapping<AdcNumbers<1,2,3>, 10>>,
TypeDictEntry<Stm32Pin<Stm32PortC, 1>, AdcMapping<AdcNumbers<1,2,3>, 11>>,
TypeDictEntry<Stm32Pin<Stm32PortC, 2>, AdcMapping<AdcNumbers<1,2,3>, 12>>,
TypeDictEntry<Stm32Pin<Stm32PortC, 3>, AdcMapping<AdcNumbers<1,2,3>, 13>>,
TypeDictEntry<Stm32Pin<Stm32PortC, 4>, AdcMapping<AdcNumbers<1,2>, 14>>,
TypeDictEntry<Stm32Pin<Stm32PortC, 5>, AdcMapping<AdcNumbers<1,2>, 15>>
>;
#elif defined(STM32F429xx) || defined(STM32F407xx)
STM32ADC_DEFINE_SUBADC(AdcDef1, 1, 2, 0, 0)
STM32ADC_DEFINE_SUBADC(AdcDef2, 2, 2, 2, 1)
STM32ADC_DEFINE_SUBADC(AdcDef3, 3, 2, 1, 2)
using AdcDefList = MakeTypeList<AdcDef1, AdcDef2, AdcDef3>;
using PinDefList = MakeTypeList<
TypeDictEntry<Stm32Pin<Stm32PortF, 3>, AdcMapping<AdcNumbers<3>, 9>>,
TypeDictEntry<Stm32Pin<Stm32PortF, 4>, AdcMapping<AdcNumbers<3>, 14>>,
TypeDictEntry<Stm32Pin<Stm32PortF, 5>, AdcMapping<AdcNumbers<3>, 15>>,
TypeDictEntry<Stm32Pin<Stm32PortF, 6>, AdcMapping<AdcNumbers<3>, 4>>,
TypeDictEntry<Stm32Pin<Stm32PortF, 7>, AdcMapping<AdcNumbers<3>, 5>>,
TypeDictEntry<Stm32Pin<Stm32PortF, 8>, AdcMapping<AdcNumbers<3>, 6>>,
TypeDictEntry<Stm32Pin<Stm32PortF, 9>, AdcMapping<AdcNumbers<3>, 7>>,
TypeDictEntry<Stm32Pin<Stm32PortF, 10>, AdcMapping<AdcNumbers<3>, 8>>,
TypeDictEntry<Stm32Pin<Stm32PortC, 0>, AdcMapping<AdcNumbers<1,2,3>, 10>>,
TypeDictEntry<Stm32Pin<Stm32PortC, 1>, AdcMapping<AdcNumbers<1,2,3>, 11>>,
TypeDictEntry<Stm32Pin<Stm32PortC, 2>, AdcMapping<AdcNumbers<1,2,3>, 12>>,
TypeDictEntry<Stm32Pin<Stm32PortC, 3>, AdcMapping<AdcNumbers<1,2,3>, 13>>,
TypeDictEntry<Stm32Pin<Stm32PortA, 0>, AdcMapping<AdcNumbers<1,2,3>, 0>>,
TypeDictEntry<Stm32Pin<Stm32PortA, 1>, AdcMapping<AdcNumbers<1,2,3>, 1>>,
TypeDictEntry<Stm32Pin<Stm32PortA, 2>, AdcMapping<AdcNumbers<1,2,3>, 2>>,
TypeDictEntry<Stm32Pin<Stm32PortA, 3>, AdcMapping<AdcNumbers<1,2,3>, 3>>,
TypeDictEntry<Stm32Pin<Stm32PortA, 4>, AdcMapping<AdcNumbers<1,2>, 4>>,
TypeDictEntry<Stm32Pin<Stm32PortA, 5>, AdcMapping<AdcNumbers<1,2>, 5>>,
TypeDictEntry<Stm32Pin<Stm32PortA, 6>, AdcMapping<AdcNumbers<1,2>, 6>>,
TypeDictEntry<Stm32Pin<Stm32PortA, 7>, AdcMapping<AdcNumbers<1,2>, 7>>,
TypeDictEntry<Stm32Pin<Stm32PortC, 4>, AdcMapping<AdcNumbers<1,2>, 14>>,
TypeDictEntry<Stm32Pin<Stm32PortC, 5>, AdcMapping<AdcNumbers<1,2>, 15>>,
TypeDictEntry<Stm32Pin<Stm32PortB, 0>, AdcMapping<AdcNumbers<1,2>, 8>>,
TypeDictEntry<Stm32Pin<Stm32PortB, 1>, AdcMapping<AdcNumbers<1,2>, 9>>
>;
#elif defined(STM32F411xE)
STM32F4ADC_DEFINE_SUBADC(AdcDef1, 1, 2, 0, 0)
STM32ADC_DEFINE_SUBADC(AdcDef1, 1, 2, 0, 0)
using AdcDefList = MakeTypeList<AdcDef1>;
using PinDefList = MakeTypeList<
TypeDictEntry<Stm32f4Pin<Stm32f4PortC,0>, AdcMapping<AdcNumbers<1>, 10>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortC,1>, AdcMapping<AdcNumbers<1>, 11>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortC,2>, AdcMapping<AdcNumbers<1>, 12>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortC,3>, AdcMapping<AdcNumbers<1>, 13>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortA,0>, AdcMapping<AdcNumbers<1>, 0>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortA,1>, AdcMapping<AdcNumbers<1>, 1>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortA,2>, AdcMapping<AdcNumbers<1>, 2>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortA,3>, AdcMapping<AdcNumbers<1>, 3>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortA,4>, AdcMapping<AdcNumbers<1>, 4>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortA,5>, AdcMapping<AdcNumbers<1>, 5>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortA,6>, AdcMapping<AdcNumbers<1>, 6>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortA,7>, AdcMapping<AdcNumbers<1>, 7>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortC,4>, AdcMapping<AdcNumbers<1>, 14>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortC,5>, AdcMapping<AdcNumbers<1>, 15>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortB,0>, AdcMapping<AdcNumbers<1>, 8>>,
TypeDictEntry<Stm32f4Pin<Stm32f4PortB,1>, AdcMapping<AdcNumbers<1>, 9>>
TypeDictEntry<Stm32Pin<Stm32PortC,0>, AdcMapping<AdcNumbers<1>, 10>>,
TypeDictEntry<Stm32Pin<Stm32PortC,1>, AdcMapping<AdcNumbers<1>, 11>>,
TypeDictEntry<Stm32Pin<Stm32PortC,2>, AdcMapping<AdcNumbers<1>, 12>>,
TypeDictEntry<Stm32Pin<Stm32PortC,3>, AdcMapping<AdcNumbers<1>, 13>>,
TypeDictEntry<Stm32Pin<Stm32PortA,0>, AdcMapping<AdcNumbers<1>, 0>>,
TypeDictEntry<Stm32Pin<Stm32PortA,1>, AdcMapping<AdcNumbers<1>, 1>>,
TypeDictEntry<Stm32Pin<Stm32PortA,2>, AdcMapping<AdcNumbers<1>, 2>>,
TypeDictEntry<Stm32Pin<Stm32PortA,3>, AdcMapping<AdcNumbers<1>, 3>>,
TypeDictEntry<Stm32Pin<Stm32PortA,4>, AdcMapping<AdcNumbers<1>, 4>>,
TypeDictEntry<Stm32Pin<Stm32PortA,5>, AdcMapping<AdcNumbers<1>, 5>>,
TypeDictEntry<Stm32Pin<Stm32PortA,6>, AdcMapping<AdcNumbers<1>, 6>>,
TypeDictEntry<Stm32Pin<Stm32PortA,7>, AdcMapping<AdcNumbers<1>, 7>>,
TypeDictEntry<Stm32Pin<Stm32PortC,4>, AdcMapping<AdcNumbers<1>, 14>>,
TypeDictEntry<Stm32Pin<Stm32PortC,5>, AdcMapping<AdcNumbers<1>, 15>>,
TypeDictEntry<Stm32Pin<Stm32PortB,0>, AdcMapping<AdcNumbers<1>, 8>>,
TypeDictEntry<Stm32Pin<Stm32PortB,1>, AdcMapping<AdcNumbers<1>, 9>>
>;
#else
#error Chip not supported by Stm32f4Adc
#error Chip not supported by Stm32Adc
#endif

template <typename AdcNumber>
Expand Down Expand Up @@ -295,7 +318,7 @@ class Stm32f4Adc {
};
using AdcPinList = IndexElemList<AssignedPinIndices, AdcPin>;

struct Object : public ObjBase<Adc, typename Stm32f4Adc::Object, EmptyTypeList> {
struct Object : public ObjBase<Adc, typename Stm32Adc::Object, EmptyTypeList> {
DMA_HandleTypeDef dma;
uint16_t adc_values[NumAdcPins];
};
Expand Down Expand Up @@ -364,13 +387,13 @@ class Stm32f4Adc {
}

public:
struct Object : public ObjBase<Stm32f4Adc, ParentObject, JoinTypeLists<
struct Object : public ObjBase<Stm32Adc, ParentObject, JoinTypeLists<
UsedAdcList,
MakeTypeList<TheDebugObject>
>> {};
};

APRINTER_ALIAS_STRUCT_EXT(Stm32f4AdcService, (
APRINTER_ALIAS_STRUCT_EXT(Stm32AdcService, (
APRINTER_AS_VALUE(int, ClockDivider),
APRINTER_AS_VALUE(int, SampleTimeSelection)
), (
Expand All @@ -379,12 +402,12 @@ APRINTER_ALIAS_STRUCT_EXT(Stm32f4AdcService, (
APRINTER_AS_TYPE(ParentObject),
APRINTER_AS_TYPE(PinsList)
), (
using Params = Stm32f4AdcService;
APRINTER_DEF_INSTANCE(Adc, Stm32f4Adc)
using Params = Stm32AdcService;
APRINTER_DEF_INSTANCE(Adc, Stm32Adc)
))
))

#define APRINTER_STM32F4_ADC_GLOBAL(adc, context) \
#define APRINTER_STM32_ADC_GLOBAL(adc, context) \
extern "C" \
__attribute__((used)) \
void ADC_IRQHandler (void) \
Expand Down
Loading

0 comments on commit 72b779a

Please sign in to comment.