Skip to content

Instantly share code, notes, and snippets.

@nraynaud
Created October 7, 2013 06:35
Show Gist options
  • Save nraynaud/6863375 to your computer and use it in GitHub Desktop.
Save nraynaud/6863375 to your computer and use it in GitHub Desktop.
STM32F4 MCU C++ API test.
#include "stm32f4xx_conf.h"
#include "stm32f4_discovery.h"
template<uint32_t GPIO_CLK, intptr_t GPIO_PORT_ADDR> class GPIOPort {
public:
static GPIOPort& port() {
static GPIOPort instance;
return instance;
};
GPIO_TypeDef *getAddress() {
return (GPIO_TypeDef *) GPIO_PORT_ADDR;
}
private:
GPIOPort() {
RCC_AHB1PeriphClockCmd(GPIO_CLK, ENABLE);
};
GPIOPort(GPIOPort const&); // Don't Implement
void operator = (GPIOPort const&); // Don't implement
};
typedef GPIOPort<RCC_AHB1Periph_GPIOD, (intptr_t) GPIOD> GPIOD_t;
template <uint32_t PIN, typename GPIO_PORT_T> class GPIOPin {
public:
static GPIOPin& pin() {
static GPIOPin instance(GPIO_PORT_T::port());
return instance;
};
void switchOn() {
GPIO_PORT_T::port().getAddress()->BSRRL = PIN;
}
private:
GPIOPin(GPIO_PORT_T & port) {
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(port.getAddress(), &GPIO_InitStructure);
};
GPIOPin(GPIOPin const&); // Don't Implement
void operator = (GPIOPin const&); // Don't implement
};
template <class PIN> class SLed {
public :
static SLed& led() {
static SLed instance;
return instance;
};
void switchOn(void) {
PIN::pin().switchOn();
}
private:
SLed() {
};
SLed(SLed const&); // Don't Implement
void operator = (SLed const&); // Don't implement
};
typedef SLed<GPIOPin<LED6_PIN, GPIOD_t> > Led6_t;
int main(void) {
//enable FPU
SCB->CPACR |= 0b000000000111100000000000000000000UL;
Led6_t::led().switchOn();
while (1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment