PRIMER - Zeke133/stm32 GitHub Wiki

Here are some examples of code for quick start


Initialisation and LED blinking

Classic "Hello World!" for embedded. Configuring System Clocks. Creating instanse of Delayer for precise time measurement. Configuring General Input Output port for LED handling. ... and just blinking )

#include <SysClock.h>
#include <delay.h>
#include <led.h>

int main(void) {

    SetSysClockTo72();
    LED led(GPIOC, GPIO_Pin_13);
    Delay delayer;

    led.on();

    while (1) {

        delayer.ms(100);
        led.invert();
    }
}

USART with DMA, Output stream and Real Time Clock

Here we configure USART1 using Direct Memory Access controller instance previously created for required device. DMA will let really quick data output, because processor will not wait in loop for data transmission end. Procedure just needs to configure DMA and USART, and than data is transmitted using hardware. Than configure OStream instance to use our serial port for output. This allows usage of easy text output in C++ style. And at last configuring Real Time Clock and bring time to serial port.

#include <SysClock.h>
#include <delay.h>

#include <dma.h>
#include <usart.h>
#include <rtc.h>
#include <oStream.h>

int main(void) {

    SetSysClockTo72();
    Delay delayer;

    DMA dmaForUsart(DMA::Device::USART1_TX);
    Usart usart1(1, dmaForUsart, 115200);
    OStream cout(usart1);

    cout << "\nSTM32F103C8T6. USART1 is ready.\nFirmware: ";
    cout << __DATE__ << ' ' << __TIME__;

    RealTimeClock rtc(delayer);
    DateTime dt;
    rtc.getTime(dt);

    cout << OStream::OutSet::dec;
    cout << "\nRTC: " << dt.date << "." << dt.month << "." << dt.year;
    cout << " " << dt.hours << ":" << dt.minutes << "." << dt.seconds;
}

I2C with DMA, SSD1306 controller based OLED display, Text Render for graphic displays

Here we configure I2C using Direct Memory Access controller instance previously created for required device. DMA will let really quick data output, because processor will not wait in loop for data transmission end. Procedure just needs to configure DMA and I2C, and than data is transmitted using hardware. Creating instance of Text Render with required Font and displaying info.

#include <SysClock.h>
#include <delay.h>

#include <dma.h>
#include <i2c.h>
#include <ssd1306.h>
#include <textRender.h>
#include <pixelFont7x10.h>

int main(void) {

    SetSysClockTo72();
    Delay delayer;

    DMA dmaForI2c1(DMA::Device::I2C1_TX);
    I2c i2cPort(1, dmaForI2c1);
    Ssd1306 oled(i2cPort, delayer);

    oled.fill(0);

    Font_7x10 fontS;
    TextRender textRender(oled, fontS);

    textRender.puts("Firmware: ");
    textRender.puts(__DATE__);
    textRender.putc(' ');
    textRender.puts(__TIME__);

    oled.update();
}

DS18B20 temperature sensor, Analog to Digital Converter and OStream output to USART

Here we configure 1-Wire software interface based on one of GPIO ports. Use it for DS18B20 temperature sensor and put temperature to serial port in easy way. Configure Analog to Digital Converter and get measurements from built in temperature sensor and reference voltage.

#include <SysClock.h>
#include <delay.h>

#include <dma.h>
#include <usart.h>
#include <oStream.h>
#include <oneWire.h>
#include <ds18b20.h>
#include <adc.h>

int main(void) {

    SetSysClockTo72();
    Delay delayer;

    DMA dmaForUsart(DMA::Device::USART1_TX);
    Usart usart1(1, dmaForUsart, 115200);
    OStream cout(usart1);

    uint8_t rom[8] = {0x28, 0x82, 0x65, 0x5B, 0x05, 0x00, 0x00, 0x20};
    OneWire oneWire(delayer, GPIOA, GPIO_Pin_8);
    Ds18b20 tempSensor(oneWire, delayer, rom);

    cout << "\nDs18b20 stateIs " << (tempSensor.isErrorState() ? "err" : "ok");
    cout << "\nTemp: " << (tempSensor.getTemperature());

    uint8_t channels[] = {16, 17};
    ADC adc1(   1, 
                RCC_PCLK2_Div6,
                ADC::ResultStoreMode::Injected,
                2, 
                channels);

    cout << "\nADC test: ";
    cout << OStream::OutSet::dec;
    cout << adc1.getValue(0) << ", ";    // temp
    cout << adc1.getValue(1);            // Vref
}
⚠️ **GitHub.com Fallback** ⚠️