added stm32 GPIO example for register modeling

master
polymurph 4 years ago
parent ee90ae423b
commit bb70c272ee

@ -0,0 +1,24 @@
cpp_src = $(wildcard *.cpp)
cpp_src += $(wildcard ./utils/*.cpp)
cpp_src += $(wildcard ./driver/*.cpp)
cpp_obj = $(cpp_src:.cpp=.o)
c_obj = $(c_src:.c=.o)
CC = g++
CFLAGS = -Wall -pedantic -li2c
LDFLAGS =
EXEC = runtest
all : $(EXEC)
$(EXEC): $(cpp_obj) $(c_obj)
$(CC) -o $@ $^ $(LDFLAGS)
clean:
rm -rf $(c_obj) $(cpp_obj) $(EXEC)
clear
cleanall:
rm -rf $(c_obj) $(cpp_obj) $(EXEC)
clear

@ -0,0 +1,105 @@
#include <iostream>
#include <stdint.h>
#include "utils/BitField.hpp"
union GPIO_MODER
{
struct MODER{
enum{
INPUT_MODE,
OUTPUT,
ALT_FUNCTION,
ANALOG
};
};
struct MODER_0 : public MODER{
typedef BitField<uint32_t, 0, 2> Bits;
};
struct MODER_1 : public MODER{
typedef BitField<uint32_t, 2, 2> Bits;
};
struct MODER_2 : public MODER{
typedef BitField<uint32_t, 4, 2> Bits;
};
struct MODER_3 : public MODER{
typedef BitField<uint32_t, 6, 2> Bits;
};
struct MODER_4 : public MODER{
typedef BitField<uint32_t, 8, 2> Bits;
};
struct MODER_5 : public MODER{
typedef BitField<uint32_t, 10, 2> Bits;
};
struct MODER_6 : public MODER{
typedef BitField<uint32_t, 12, 2> Bits;
};
struct MODER_7 : public MODER{
typedef BitField<uint32_t, 14, 2> Bits;
};
struct MODER_8 : public MODER{
typedef BitField<uint32_t, 16, 2> Bits;
};
struct MODER_9 : public MODER{
typedef BitField<uint32_t, 18, 2> Bits;
};
struct MODER_10 : public MODER{
typedef BitField<uint32_t, 20, 2> Bits;
};
struct MODER_11 : public MODER{
typedef BitField<uint32_t, 22, 2> Bits;
};
struct MODER_12 : public MODER{
typedef BitField<uint32_t, 24, 2> Bits;
};
struct MODER_13 : public MODER{
typedef BitField<uint32_t, 26, 2> Bits;
};
struct MODER_14 : public MODER{
typedef BitField<uint32_t, 28, 2> Bits;
};
struct MODER_15 : public MODER{
typedef BitField<uint32_t, 30, 2> Bits;
};
union{
GPIO_MODER::MODER_0::Bits MODER_0;
GPIO_MODER::MODER_1::Bits MODER_1;
GPIO_MODER::MODER_2::Bits MODER_2;
GPIO_MODER::MODER_3::Bits MODER_3;
GPIO_MODER::MODER_4::Bits MODER_4;
GPIO_MODER::MODER_5::Bits MODER_5;
GPIO_MODER::MODER_6::Bits MODER_6;
GPIO_MODER::MODER_7::Bits MODER_7;
GPIO_MODER::MODER_8::Bits MODER_8;
GPIO_MODER::MODER_9::Bits MODER_9;
GPIO_MODER::MODER_10::Bits MODER_10;
GPIO_MODER::MODER_11::Bits MODER_11;
GPIO_MODER::MODER_12::Bits MODER_12;
GPIO_MODER::MODER_13::Bits MODER_13;
GPIO_MODER::MODER_14::Bits MODER_14;
GPIO_MODER::MODER_15::Bits MODER_15;
}bits;
uint32_t raw;
GPIO_MODER(uint32_t v = 0x00) : raw(v) {}
operator uint32_t() {return raw;}
void operator = (uint32_t v) {raw = v;}
};
int main(void)
{
GPIO_MODER MODER_A;
// for reference use the STM32F0 reference manualchapter GPIO
MODER_A.bits.MODER_0 = GPIO_MODER::MODER_0::OUTPUT;
MODER_A.bits.MODER_5 = GPIO_MODER::MODER_5::ALT_FUNCTION;
std::cout << +MODER_A << std::endl;
return 0;
}

@ -0,0 +1,79 @@
/**
* @file BitField.h
* @author Edwin Koch (eddyed.k@gmail.com)
* @brief
* @version 0.1
* @date 2020-12-19
*
* @copyright Copyright (c) 2020
*
*/
// Based on:
// https://www.youtube.com/watch?v=TYqbgvHfxjM
// https://stackoverflow.com/questions/31726191/is-there-a-portable-alternative-to-c-bitfields
// https://stackoverflow.com/questions/1392059/algorithm-to-generate-bit-mask
#ifndef _BITFIELDS_HPP_
#define _BITFIELDS_HPP_
#include <stdint.h>
/**
* @brief Template class for portable Bitfields
*
* @tparam T type of variable in which the bitfield resides
* @tparam START bit index starting from LSB where the bitfield starts
* @tparam SIZE number of bits
*/
template<typename T, uint8_t START, uint8_t SIZE>
struct BitField
{
/**
* @brief Construct a new Bit Field object
*
*/
BitField()
{
static_assert(SIZE != 0, "Bitfield SIZE must be > 0!");
static_assert(START < sizeof(T) * 8, "START exceeds number of bits of the chosen typename T!");
}
/**
* @brief assignment operator
*
* @param v value to be written in the bitfield
* @return BitField&
*/
BitField& operator =(T v)
{
// use bit band alias if system permits
_raw = ((v & ((1ULL << SIZE)-1)) << START) | (_raw & ~(((1ULL << SIZE)-1) << START));
return *this;
}
/**
* @brief return the value inside the bitfield
*
* @return T
*/
operator T() const
{
return (_raw >> START) & ((1ULL << SIZE) - 1);
}
/**
* @brief return the raw value
*
* @return T
*/
T operator ()() const
{
return (_raw >> START) & ((1ULL << SIZE) - 1);
}
private:
T _raw;
};
#endif // _BITFIELDS_HPP_
Loading…
Cancel
Save