Author Topic: Blackpill STM32G431CBU6 - 170 MHz / 32k RAM / 128k ROM / 12-bit ADC and DAC,...  (Read 5523 times)

0 Members and 2 Guests are viewing this topic.

Offline radiolistenerTopic starter

  • Super Contributor
  • ***
  • Posts: 3610
  • Country: ua
Just bought pretty nice blackpill STM32G431CBU6:

https://www.aliexpress.com/item/1005005303669884.html

Allows to run core at 170 MHz, 32k RAM, 128k ROM, 12-bit ADC and 12-bit DAC on the chip, hardware floating point, hardware CORDIC and AES support...

All this just for 3 USD :)

Chinese ST-LINKv2 supports it, but it requires to update ST-LINK utility and upgrade ST-LINK firmware, I had old ST-LINK utlility and it was unable to recognize it. After ST-LINK firmware upgrade it detects it as:
Code: [Select]
Device ID:0x468
Device flash Size : 128KBytes
Device family :STM32G43x/G44x

Onboard HSE is 8 MHz (claimed as 10 ppm) also has onboard clock crystal 32.768 kHz. The module has one blue user LED (PC6) and one user key (PC13), both are duplicated on module GPIO. Also it comes with NRST button, BOOT button and USB-C connector.
« Last Edit: July 24, 2023, 12:07:54 am by radiolistener »
 
The following users thanked this post: abraxalito, Kalvin, neil555, iMo, gjvdheiden

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6093
  • Country: es
Indeed, nice MCU for the price  :)
Here's a cheaper option if first time buying (Aliexpress Welcome deal):
https://www.aliexpress.com/item/1005005451007632.html

I might prefer the stm32f411ceu6 black pill, it's slower but packs a lot of storage (128/512K), however last years it got a lot more expensive.
« Last Edit: July 24, 2023, 12:16:12 am by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline radiolistenerTopic starter

  • Super Contributor
  • ***
  • Posts: 3610
  • Country: ua
Sawtooth and sine waveform from onchip 12-bit DAC running at 2 MS/s on this STM32G431CBU6 board.
The sawtooth signal is increment on each next sample.
The sine signal is generated at realtime :)
« Last Edit: July 24, 2023, 01:40:04 am by radiolistener »
 
The following users thanked this post: bingo600, PCB.Wiz

Offline radiolistenerTopic starter

  • Super Contributor
  • ***
  • Posts: 3610
  • Country: ua
I might prefer the stm32f411ceu6 black pill, it's slower but packs a lot of storage (128/512K), however last years it got a lot more expensive.

stm32f411ceu6 doesn't have DAC, ADC only.
I bought this STM32G431CBU6 module because it has onchip ADC and DAC, so it can be used for DSP...  :)
 

Offline iMo

  • Super Contributor
  • ***
  • Posts: 4936
  • Country: vc
Do the pins have 0.1inch pitch or 0.05inch? (the picture shows "1,37mm")..
« Last Edit: July 24, 2023, 07:54:27 am by iMo »
 

Offline bingo600

  • Super Contributor
  • ***
  • Posts: 2010
  • Country: dk
Sawtooth and sine waveform from onchip 12-bit DAC running at 2 MS/s on this STM32G431CBU6 board.
The sawtooth signal is increment on each next sample.
The sine signal is generated at realtime :)

Any chance of a link/zip to the sourcecode for this demo

/Bingo
 

Offline dobsonr741

  • Frequent Contributor
  • **
  • Posts: 688
  • Country: us
 
The following users thanked this post: neil555

Offline Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 3576
  • Country: nl
I think WeAct is one of my favourite Ali stores.

For one, they have the "Blue Pill" both in STM and some other variant, which suggests that they are one of the very few that do not lie about the "Blue Pill" pcb's. But I do have plenty on storage so have not bought those.

I did buy a bunch of STM32F411 "Black Pills" halfway though Chippageddon for over EUR7 a piece. Now prices seem to have come down t below EUR4.
I also appreciate they have some github repositories with example code. It shows that they at least put in some effort beyond just delivering the bare hardware.
 

Offline ozcar

  • Frequent Contributor
  • **
  • Posts: 326
  • Country: au
Do the pins have 0.1inch pitch or 0.05inch? (the picture shows "1,37mm")..

I think the 1.37mm is the distance from the outer row to the edge of the board. Assuming that to be the case, from the other dimensions given, I calculate the spacing is 2.54mm, so, yeah 0.1 inch.

For the sort of things I do I prefer boards with a single row of pins on each side though.
 
The following users thanked this post: iMo

Offline Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 3576
  • Country: nl
For the sort of things I do I prefer boards with a single row of pins on each side though.


Yeah, I also thought of that.
In the comment on the We Act store, there is also a wish for being "breadboard compatible".

Then I had an epiphany...
You can solder half the header pins on the bottom, and the other half on the top.
Then, with the bottom pins you can stick it in a breadboard, and you can use the top pins and "Dupont Wires" to bugger off to some other place on your breadboard.
 
The following users thanked this post: ag123

Offline iMo

  • Super Contributor
  • ***
  • Posts: 4936
  • Country: vc
.. while reading the github docs - there is a warning of fakes already :)

Quote
Important note

If there is no WeAct and version number on the back of the board, the chip batch is 537 or old, the stitches and board are packaged in one bag, the board screen printing is wrong(3V3 is going to be labeled V3V), it is definitely pirated.Please comment on issues or let us know by email ..
 

Offline radiolistenerTopic starter

  • Super Contributor
  • ***
  • Posts: 3610
  • Country: ua
Do the pins have 0.1inch pitch or 0.05inch? (the picture shows "1,37mm")..

Just measured it, the distance between first pin and last pin is about ~27.95 mm / 11 (12 pins) = 2.54 mm between pins. The step between pins is the same as on original blue pill. But holes are very small, so they have very tight contact even without soldering it...
« Last Edit: July 24, 2023, 04:43:39 pm by radiolistener »
 

Offline radiolistenerTopic starter

  • Super Contributor
  • ***
  • Posts: 3610
  • Country: ua
Important note

If there is no WeAct and version number on the back of the board, the chip batch is 537 or old, the stitches and board are packaged in one bag, the board screen printing is wrong(3V3 is going to be labeled V3V), it is definitely pirated.Please comment on issues or let us know by email ..

I bought it here: https://www.aliexpress.com/item/1005005303669884.html

It has WeAct label and version V1.0. It comes with PLS headers for GPIO and for SWD but they are not soldered, so you can solder it as you wish. I soldered just SWD header.

The board is very small. It has extreme small smd components, so if you want to resolder something, it will be not so easy.

Actual HSE crystal frequency on my instance is 8000054 Hz (6 ppm). I measured it by enable MCO output of HSE signal on PA8 pin.

For the sort of things I do I prefer boards with a single row of pins on each side though.

Yeah, I also stuck on that. If I solder double row pls headers, it will be problematic to deal with all gpio on breadboard. So, I'm thinking about soldering just a single row headers...
« Last Edit: July 24, 2023, 05:02:34 pm by radiolistener »
 

Offline tszaboo

  • Super Contributor
  • ***
  • Posts: 7614
  • Country: nl
  • Current job: ATEX product design
That looks like a really cool part. Searched the price on LCSC, 1.7USD for a single piece. Same price on Digikey, 4 EUR if you buy a whole reel.
Something is really broken with our pricing.
 
The following users thanked this post: jas

Offline radiolistenerTopic starter

  • Super Contributor
  • ***
  • Posts: 3610
  • Country: ua
Any chance of a link/zip to the sourcecode for this demo

ok, I will publish it later, currently it uses hand made header files, I want to replace it with standard one...
 
The following users thanked this post: bingo600

Offline radiolistenerTopic starter

  • Super Contributor
  • ***
  • Posts: 3610
  • Country: ua
Sawtooth and sine waveform from onchip 12-bit DAC running at 2 MS/s on this STM32G431CBU6 board.
The sawtooth signal is increment on each next sample.
The sine signal is generated at realtime :)

Any chance of a link/zip to the sourcecode for this demo

/Bingo

here is source code for this test demo which generate sine wave with hardcoded frequency on PA4 DAC output, also it put HSE clock to PA8 and use LED blink in background.

A short description: PLL is configured to be clocked from external HSE 8 MHz clock and produce 170 MHz clock, which is used for SYSCLK and as a source for TIM2. TIM2 is configured to be triggered at 2 MHz rate. TIM2 interrupt handler contains code to calculate sine wave and update DAC with a new value.

PLL frequency is the following:

Fout = ((Finput / PLLM) * PLLN) / PLLR

Since Finput= 8 MHz, we use PLLM=2, PLLN=85 and PLLR=2, so the PLL output frequency will be:

Fout = ((8 MHz / 2) * 85) / 2 = 170 MHz

You can change sine frequency on DAC output in init_sine argument. Note that you're needs to update sample rate in init_sine arguments if you change TIM2 divider.

Header files consume 13 MB of space even without HAL, but I think it's better to include it. All required files are included in archive.

for GCC compiller just enter to GCC folder and type make.
for EWARM enter to EWARM folder and use included workspace file.

here is main.c:
Code: [Select]
#include <math.h>
#include <stm32g431.h>


// test code for STM32G431CBU6 module
// [url]https://www.eevblog.com/forum/microcontrollers/blackpill-stm32g431cbu6-170-mhz-32k-ram-128k-rom-12-bit-adc-and-dac/[/url]
//
// PC6  = LED
// PC13 = KEY
// PA4  = DAC out
// PA8  = HSE out


void init_gpio(void)
{
    // Enable GPIOC clock
    SET_BIT(RCC->AHB2ENR, RCC_AHB2ENR_GPIOCEN);

    // PC6  = OUT (LED)
    MODIFY_REG(GPIOC->MODER,
        GPIO_MODER_MODE6,
        1 << GPIO_MODER_MODE6_Pos);
    // PC6 speed
    MODIFY_REG(GPIOC->OSPEEDR,
        GPIO_OSPEEDR_OSPEED6_Msk,
        GPIO_SPEED_FREQ_VERY_HIGH << GPIO_OSPEEDR_OSPEED6_Pos);
   
    // PC13 = IN  (KEY)
    MODIFY_REG(GPIOC->MODER,
        GPIO_MODER_MODE13,
        0 << GPIO_MODER_MODE13_Pos);

    // Enable GPIOA clock
    SET_BIT(RCC->AHB2ENR, RCC_AHB2ENR_GPIOAEN);

    // PA8 = ALTFN MCO
    MODIFY_REG(GPIOA->MODER,
        GPIO_MODER_MODE8,
        2 << GPIO_MODER_MODE8_Pos);
    // PA8 alternate function MCO
    MODIFY_REG(GPIOA->AFR[8 >> 3U],
        GPIO_AFRH_AFSEL8_Msk,                   // 0xFU << ((8 & 0x07U) * 4U),
        GPIO_AF0_MCO << GPIO_AFRH_AFSEL8_Pos);  // GPIO_AF0_MCO << ((8 & 0x07U) * 4U));
    // PA8 speed
    MODIFY_REG(GPIOA->OSPEEDR,
        GPIO_OSPEEDR_OSPEED8_Msk,
        GPIO_SPEED_FREQ_VERY_HIGH << GPIO_OSPEEDR_OSPEED8_Pos);
    // PA8 pull mode
    MODIFY_REG(GPIOA->PUPDR,
        GPIO_PUPDR_PUPD8_Msk,
        GPIO_NOPULL << GPIO_PUPDR_PUPD8_Pos);
       
    // Configure MCO = HSE / 1
    MODIFY_REG(RCC->CFGR,
        RCC_CFGR_MCOSEL | RCC_CFGR_MCOPRE,
        RCC_MCO1SOURCE_HSE | RCC_MCODIV_1);
}

void init_clock(void)
{
    // Select SYSCLK source = HSI
    MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_CFGR_SW_HSI);
    while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI);

    // Stop PLL
    CLEAR_BIT(RCC->CR, RCC_CR_PLLON);
    while ((RCC->CR & RCC_CR_PLLRDY) != 0);
   
    // Enable high voltage for power regulator to 1.28 V
    CLEAR_BIT(PWR->CR5, PWR_CR5_R1MODE);

    // Configure FLASH latency = 4
    FLASH->ACR = FLASH_ACR_DBG_SWEN  | FLASH_ACR_DCEN | FLASH_ACR_ICEN | FLASH_ACR_LATENCY_4WS ;
    while ((FLASH->ACR & FLASH_ACR_LATENCY_Msk) != FLASH_ACR_LATENCY_4WS);

    // Power on HSE crystal
    SET_BIT(RCC->CR, RCC_CR_HSEON);
    while ((RCC->CR & RCC_CR_HSERDY) == 0);

    // Configure PLL used for SYSCLK
    // PLLM=2 (value 1), PLLN=85 (value 85), PLLR=2 (value 0)
    MODIFY_REG(RCC->PLLCFGR,
        RCC_PLLCFGR_PLLSRC | RCC_PLLCFGR_PLLM | RCC_PLLCFGR_PLLN | RCC_PLLCFGR_PLLR,
        RCC_PLLCFGR_PLLSRC_HSE | (1 <<RCC_PLLCFGR_PLLM_Pos) | (85 << RCC_PLLCFGR_PLLN_Pos) | (0 << RCC_PLLCFGR_PLLR_Pos));

    // Start PLL
    SET_BIT(RCC->CR, RCC_CR_PLLON);
    while ((RCC->CR & RCC_CR_PLLRDY) == 0);

    // Enable PLL output for SYSCLK
    SET_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLREN);

    // Select SYSCLK source = PLL
    MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_CFGR_SW_PLL);
    while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL);
}

void init_TIM2(void)
{
    SET_BIT(RCC->APB1ENR1, RCC_APB1ENR1_TIM2EN); // Enable TIM2 clock
    TIM2->ARR = 85 - 1;                 // Auto-reload register f = 170 000 000/(TIM2_ARR+1), 2 MHz -> (85 - 1)

    SET_BIT(NVIC->ISER[0], (1 << 28)); // Enable NVIC interrupt #28 (TIM2)
    SET_BIT(TIM2->DIER, TIM_DIER_UIE); // Enable timer overflow interrupt
    SET_BIT(TIM2->CR1, TIM_CR1_CEN);    // Enable timer
}


// DAC1 on PA4
void init_DAC1(void)
{
SET_BIT(RCC->AHB2ENR, RCC_AHB2ENR_DAC1EN);     // Enable DAC1 clock
SET_BIT(DAC1->CR, DAC_CR_EN1);    // Enable DAC1 output
DAC1->DHR12R1 = 0;    // Write DAC1 = 0
}

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

float R;
float V;
float X;
float S;

void init_sine(float freq, uint32_t sample_rate)
{
    // init sine oscillator
    R = pow(2.0 * sin(freq * M_PI / sample_rate), 2.0);    // = 4.0 * (freq * M_PI / sample_rate) * (freq * M_PI / sample_rate);
    V = 0;
    X = 2000;   // initial value (amplitude)
    S = 2048;   // offset
}

void main()
{
    init_sine(10000.0, 2000000);

    init_gpio();
    init_clock();
    init_DAC1();
    init_TIM2();

    for (;;)
    {
        for (uint32_t i=1000000; i > 0; i--);   // delay
        GPIOC->ODR ^= GPIO_ODR_OD6;             // change LED state
    }
}


void TIM2_IRQHandler(void)
{
CLEAR_BIT(TIM2->SR, TIM_SR_UIF); // Reset TIM2 overflow flag
   
    // update sine oscillator
V -= X * R;
X += V;
   
DAC1->DHR12R1 = (uint32_t)(X + S);
}

//----------------------------------------------------
void NMI_Handler(void)
{
    __asm("BKPT 0");
}
void HardFault_Handler(void)
{
    __asm("BKPT 0");
}
void MemManage_Handler(void)
{
    __asm("BKPT 0");
}
void BusFault_Handler(void)
{
    __asm("BKPT 0");
}
void UsageFault_Handler(void)
{
    __asm("BKPT 0");
}

PS: stm32g431.h is my file, it includes standard stm32g4xx.h and stm32g431xx.h and also add some constants taken from HAL headers. I don't include HAL, because it eats > 70 MB of space.

PPS: in order to build it on linux you're needs to install:
Code: [Select]
sudo pacman -S arm-none-eabi-gcc arm-none-eabi-newlib
« Last Edit: July 25, 2023, 11:05:42 am by radiolistener »
 
The following users thanked this post: bingo600, thm_w, gjvdheiden

Offline dobsonr741

  • Frequent Contributor
  • **
  • Posts: 688
  • Country: us
How come not calling the sine() function in the interrupt handler, neither using pre calculated sine table values?
 

Offline radiolistenerTopic starter

  • Super Contributor
  • ***
  • Posts: 3610
  • Country: ua
How come not calling the sine() function in the interrupt handler, neither using pre calculated sine table values?

precalculated table for sine will leads to a very dirty sine with high distortions and harmonics. Especially when sample rate is not integer multiply of sine frequency. I'm using sine calculated at runtime to avoid distortions and harmonics to get more clean sine. It eats just 3 arithmetic operations, don't eats memory and allows to produce any fractional sine frequency. :)

Adding call for sine calculation leads to additional tacts delay so it reduces maximum possible sample rate for DAC, because ISR handler needs more time to execute call for sine().
« Last Edit: July 25, 2023, 11:11:23 am by radiolistener »
 

Offline iMo

  • Super Contributor
  • ***
  • Posts: 4936
  • Country: vc
He is using a trick, imho - something like hacked optimized cordic :)
And with the single precision FPU enabled it is pretty fast..

« Last Edit: July 25, 2023, 11:24:49 am by iMo »
 

Offline dobsonr741

  • Frequent Contributor
  • **
  • Posts: 688
  • Country: us
Yes, and it works great with fixed point too, not just with floating. Just run it in Python/Fxp.
 

Offline iMo

  • Super Contributor
  • ***
  • Posts: 4936
  • Country: vc
There are freqs where it gets out of range, like for 222222 you get 4107, -10, etc..
 

Offline radiolistenerTopic starter

  • Super Contributor
  • ***
  • Posts: 3610
  • Country: ua
There are freqs where it gets out of range, like for 222222 you get 4107, -10, etc..

You can fix it with reduce amplitude in X variable or offset in S variable.

Also it has some frequency error because R is calculated with approximation.  If you use this one:
Code: [Select]
R = 4.0 * (freq * M_PI / sample_rate) * (freq * M_PI / sample_rate);
it gives you pretty high frequency error (about 85 kHz instead of 80 kHz), but doesn't needs to use sin function.

If you use this one:
Code: [Select]
R = pow(2.0 * sin(freq * M_PI / sample_rate), 2.0);
it gives you better frequency precision, but requires sin function. But also has some frequency error, just smaller.
If you're needs a fixed frequency, you can adjust the frequency manually in order to get a more accurate result
« Last Edit: July 25, 2023, 08:23:33 pm by radiolistener »
 

Offline peter-h

  • Super Contributor
  • ***
  • Posts: 3832
  • Country: gb
  • Doing electronics since the 1960s...
Is this CPU made by ST, or is it a chinese replica?

It is very cheap.

You can do arbitrary waveform generation by using a timer to trigger a DAC fed with DMA. I am doing that on my 32F417, two concurrent channels.
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Offline ag123

  • Contributor
  • Posts: 39
  • Country: 00
For the sort of things I do I prefer boards with a single row of pins on each side though.


Yeah, I also thought of that.
In the comment on the We Act store, there is also a wish for being "breadboard compatible".

Then I had an epiphany...
You can solder half the header pins on the bottom, and the other half on the top.
Then, with the bottom pins you can stick it in a breadboard, and you can use the top pins and "Dupont Wires" to bugger off to some other place on your breadboard.

that is 'a pretty good idea'  :)
but that these days 'dupont wires' 'nearly replaced' breadboards for some of my projects, I actually bothered to buy those 'dupont pins'
https://www.aliexpress.com/w/wholesale-dupont-pins.html
and crimped my own wires, I made do without shells.
as for shells, a good replacement is simply heat shrink tubes 1 per pin, they save a lot in the overall height of the pins without the shells
and for 'modular' wires, I sometimes used ribbon cables with the pins, e.g. with LCDs that often is a 'bunch' of wires.
so these days it becomes a 'mess of wires', but that I'm finding it 'better' than breadboards as the whole circuit is complete without needing a breadboard.
they can practically be placed into (project) boxes
 

Offline radiolistenerTopic starter

  • Super Contributor
  • ***
  • Posts: 3610
  • Country: ua
Is this CPU made by ST, or is it a chinese replica?

It is very cheap.

Who knows, but it looks good and works great, so I don't see reason to worry about it. :)
I think they sell it with cheap price to make it popular so many users will request large quantity from factory.

I hear that fake STM32 chips are made with different brand name. So, if it marked with original ST, most of all it's original.
« Last Edit: July 29, 2023, 06:13:27 am by radiolistener »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf