Page 1 of 1

Newbie question

Posted: Tue Aug 16, 2022 12:50 pm
by geggi73a
Hi!
This is my first posting on this forum.
I'm not very good at coding and most of the time I just find some code examples and modify it to fit my use.
Currently I'm building a datalogger card with up to 10 analogue channels. This will be used for a light measurement with four PV panels from LED garden lights.
The unit I'm trying to get working is most likely a STM32F103C6 or a fake STM32F103C8 because this is the only configuration I get working in Arduino.

I have managed to get this up and running with a serial port adapter (FTDI)

The issue is that no matter what I do the native USB will not work and I'm "loosing" two analog inputs PA8 and PA9.

Can anybody direct me in the correct direction to get the USB working?


Code: Select all

/*
--------------------------------------------------------------
  Program:      sw_voltmeter_4ch

  Description:  4 channel software voltmeter that displays
                voltage readings in a Processing application
                running on a computer.
  
  Hardware:     Arduino Uno with voltage dividers on A2 to A5.
                
  Software:     Developed using Arduino 1.0.5 software
                Should be compatible with Arduino 1.0 +
                voltmeter_4ch Processing software runs on PC

  Date:         28 May 2013
 
  Author:       W.A. Smith, http://startingelectronics.org
--------------------------------------------------------------*/

// number of analog samples to take per reading, per channel
#define NUM_SAMPLES 20
// voltage divider calibration values
#define DIV_1    11.00000000
#define DIV_2    11.00000000
#define DIV_3    11.00000000
#define DIV_4    11.00000000
#define DIV_5    11.00000000
#define DIV_6    11.00000000
#define DIV_7    11.00000000
#define DIV_8    11.00000000
#define DIV_9    11.00000000
#define DIV_10    11.00000000

// definition of number of channels
#define CH    8
// ADC reference voltage / calibration value
#define V_REF    3.390

int sum[CH] = {0};                // sums of samples taken
unsigned char sample_count = 0;  // current sample number
float voltage[CH] = {0.0};        // calculated voltages
char l_cnt = 0;                  // used in 'for' loops

void setup()
{
    Serial.begin(115200);
}

void loop()
{
    // take a number of analog samples and add them up
    while (sample_count < NUM_SAMPLES) {
        // sample each channel PA0 to PA9
        for (l_cnt = 0; l_cnt < CH; l_cnt++) {
            sum[l_cnt] += analogRead(PA0 + l_cnt);
        }
        sample_count++;
        delay(10);
    }
    // calculate the voltage for each channel
    for (l_cnt = 0; l_cnt < CH; l_cnt++) {
        voltage[l_cnt] = ((float)sum[l_cnt] / (float)NUM_SAMPLES * V_REF) / 4096.0;
    }
    
    // each voltage is multiplied by the resistor network
    // division factor to calculate the actual voltage
    voltage[0] = voltage[0] * DIV_1;
    voltage[1] = voltage[1] * DIV_2;
    voltage[2] = voltage[2] * DIV_3;
    voltage[3] = voltage[3] * DIV_4;
    voltage[4] = voltage[4] * DIV_5;
    voltage[5] = voltage[5] * DIV_6;
    voltage[6] = voltage[6] * DIV_7;
    voltage[7] = voltage[7] * DIV_8;
   // voltage[8] = voltage[8] * DIV_9;
   // voltage[9] = voltage[9] * DIV_10;
    
    // send voltages to Processing application via serial port / USB
    // voltage CH-0 (pin PA0)
    Serial.print(voltage[0], 3);
    Serial.print(",");

   // voltage CH-1 (pin PA1)
   // Serial.print("B");
    Serial.print(voltage[1], 3);
     Serial.print(",");

    // voltage CH-2 (pin PA2)
    Serial.print(voltage[2], 3);
     Serial.print(",");

    // voltage CH-3 (pin PA3)
    Serial.print(voltage[3], 3);
     Serial.print(",");

    // voltage CH-4 (pin PA4)
    Serial.print(voltage[4], 3);
    Serial.print(",");

     // voltage CH-5 (pin PA5)
    Serial.print(voltage[5], 3);
    Serial.print(",");

    // voltage CH-6 (pin PA6)
    Serial.print(voltage[6], 3);
    Serial.print(",");

    // voltage CH-7 (pin PA7)
    Serial.print(voltage[7], 3);
   // Serial.print(",");

   /*
    // voltage CH-8 (pin PA8)
    Serial.print(voltage[8], 3);
    Serial.print(",");
    
    // voltage CH-9 (pin PA9)
    Serial.print(voltage[9], 3);
    */

     Serial.println(",");
    delay(10);
    // reset count and sums
    sample_count = 0;
    for (l_cnt = 0; l_cnt < CH; l_cnt++) {
        sum[l_cnt] = 0;
    }
}

Re: Newbie question

Posted: Tue Aug 16, 2022 1:23 pm
by ag123
Welcome, hope you have reviewed the wiki
https://github.com/stm32duino/wiki/wiki

if stm32 is new to you, it would be "easier" to start with a chip / board with more resources (i.e. more flash e.g. at least 64 or 128k, more ram 20k is *tight*, more is merrier)
e.g.
the Nucleo boards, there is a list on the core website, things like a Nucleo F401/F411 RE would likely be more 'comfortable'
https://github.com/stm32duino/Arduino_Core_STM32
then there are 3rd party boards
https://www.aliexpress.com/wholesale?ca ... =stm32f401
https://www.adafruit.com/product/4382
https://store.micropython.org/
https://www.olimex.com/Products/ARM/ST/
https://www.olimex.com/Products/Duino/S ... e-hardware

note the 'venerable' *real* blue pill use a stm32f103c8 20k sram 64k flash that is still cramped, then the original maple mini use stm32f103cb 20k sram 128k flash)
if it is a stm32f103c6 you may have like 6-10k sram and 32k flash !
https://www.st.com/en/microcontrollers- ... 103c6.html
and the core tries to provide usb serial, spi, adc, gpio, i2c, uart and actually more e.g. hardware timers.
you would more then sooner run out of sram and flash.

i'm kind of a fan of the stm32F4xx series, they go from decently fast to very fast, has fpu, mostly has more ram and flash, that makes it adaptable to lots of apps, even some rather large ones e.g. micropython etc.

Re: Newbie question

Posted: Tue Aug 16, 2022 1:50 pm
by geggi73a
I got everything working except the USB so the Bluepill is currently large enough for this project.
It only uses 54% of the total memory in the chip so I dont have to upgrade for a "hotter" MCU for this application.
I only need some pointers to get the USB working! :)
Sketch uses 17880 bytes (54%) of program storage space. Maximum is 32768 bytes.
Global variables use 1504 bytes (14%) of dynamic memory, leaving 8736 bytes for local variables. Maximum is 10240 bytes.
stm32flash 0.4

http://stm32flash.googlecode.com/

Using Parser : Raw BINARY
Interface serial_w32: 115200 8E1
Version : 0x22
Option 1 : 0x00
Option 2 : 0x00
Device ID : 0x0410 (Medium-density)
- RAM : 20KiB (512b reserved by bootloader)
- Flash : 128KiB (sector size: 4x1024)
- Option RAM : 16b
- System RAM : 2KiB
Write to memory
Erasing memory
Wrote address 0x080045d8 (100.00%) Done.

Starting execution at address 0x08000000... done.

Re: Newbie question

Posted: Tue Aug 16, 2022 2:22 pm
by ag123
well, a few things

apparently you aren't running the 'official' stm core
https://github.com/stm32duino/Arduino_Core_STM32

and instead appear to be a (fork?) variant of 'libmaple' (roger's) core
https://github.com/rogerclarkmelbourne/Arduino_STM32
be sure you use this repository if you prefer this core as there are others floating around that could differ
https://github.com/rogerclarkmelbourne/ ... STM32/wiki
be sure you have reviewed the wiki and various install instructions as well

for "bluepill", generally the board is "generic stm32f103c series" and variant is "stm32f103c8 (20k ram 64k flash)
menu
menu
arduino.png (37.93 KiB) Viewed 1296 times
usb-serial is a default in the built and is accessed as Serial. so you could do Serial.print("hello world"); etc
c8 , cb is the main working configuration.
c6 probably won't work or has issues. by definition, that has a smaller sram 6-10k and 32k flash

Re: Newbie question

Posted: Tue Aug 16, 2022 2:54 pm
by GonzoG
I see that you're using Roger's core. It's functionality is quite limited.
It does not have good support for other chips than F103C8/CB and F103Rx.
Also analogRead() isn't that accurate in this core as it does not take many samples. But it's really fast. Or at least it was when I was using it.

I recommend you switch to official STM core. It does need more RAM and flash but it's more advanced and does have full hardware support for most of STM's MCUs.
In STM core analogRead() is pretty accurate as it takes way more samples, but it needs more time. I think, it's about 60us on STM32F103.

Re: Newbie question

Posted: Tue Aug 16, 2022 5:49 pm
by geggi73a
I write this up so that any other Newbie also possible can use this configuration.

The Bluepill where flashed with the bootloader "generic_boot20_pc13" found in the URL.
https://github.com/rogerclarkmelbourne/ ... bootloader
Use the one found in the binary directory.
Flashing tool is STM32cubeprogrammer downloaded at STM.


First I added " https://github.com/stm32duino/BoardMana ... index.json" in the "preferences" in Arduino.
Additional URL for boardmananger
Additional URL for boardmananger
Board Manager url.JPG (54.85 KiB) Viewed 1270 times

Then I installed the STMicroelectronics definitions in Arduino.
Installation in  Arduino Boardmanager
Installation in Arduino Boardmanager
Board mananger.JPG (85.66 KiB) Viewed 1270 times
Here is the configuration I used to do the upload and get the USB to work.
Arduino configuration for board and programming
Arduino configuration for board and programming
STM working but with USB.jpg (62.34 KiB) Viewed 1270 times

Re: Newbie question

Posted: Tue Aug 16, 2022 9:00 pm
by ozcar
Greetings,

I also suggest that you try the official ST core. I lifted up the code in your first post, and for me it compiles with no complaint using the official core. Code size and ram usage is more than you reported, but should not be an issue on a typical blue pill board. Exact numbers will no doubt change a bit depending on optimisation - set to "Smallest (-Os default)" for me.

Code: Select all

Sketch uses 29644 bytes (45%) of program storage space. Maximum is 65536 bytes.
Global variables use 3616 bytes (17%) of dynamic memory, leaving 16864 bytes for local variables. Maximum is 20480 bytes. 
I uploaded it to a blue pill board that I had lying around here (but, as mentioned over on Eevblog, I use STLINK for upload). This blue pill board is so old that it likely uses a genuine STM chip, and I think it actually has 128k flash rather than 64k. I can see that I did attend to the USB resistor on this board. I had USB support in the IDE set to "CDC (generic 'Serial' supercede U(S)ART)". This resulted in a COM port appearing in Windows, and the IDE serial monitor shows output (I have nothing connected to any of the pins that the code reads):

Code: Select all

...
0.174,0.184,0.163,0.207,1.929,0.167,0.172,0.165,
0.171,0.181,0.158,0.196,1.928,0.157,0.168,0.163,
0.171,0.179,0.160,0.206,1.927,0.164,0.170,0.164,
...

Re: Newbie question

Posted: Wed Aug 17, 2022 9:05 am
by geggi73a
I have made a description for other Newbies to get started.
It is based on my experience and hopefully can get other started.
https://geggi73a.wordpress.com/2022/08/ ... b-working/