Bluepill setup on Arduino on Ubuntu linux

This is a work in progress.
  1. STlinkV2 adapter’s pinout on the plastic case is WRONG. the actual pin details are printed on the PCB.
  2.  I installed the stlink (the stlink should be added to a special group that allows access to the ‘modem’/’usb’)
    git clone https://github.com/texane/stlink.git
    cd stlink/build
    cmake ..
    make && sudo make install
  3.  We run lsusb. It didn’t work on USB 3.0 port.  The ST-Link shows up now when plugged into a different USB port as:
    farhan@Farhan:~/Documents/ham/stlink$ lsusb
    Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
    Bus 001 Device 005: ID 06cb:00a2 Synaptics, Inc. 
    Bus 001 Device 004: ID 04f2:b604 Chicony Electronics Co., Ltd 
    Bus 001 Device 002: ID 046d:c534 Logitech, Inc. Unifying Receiver
    Bus 001 Device 020: ID 0483:3748 STMicroelectronics ST-LINK/V2
    Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
  4. Started to install Arduino support for the blue pill :
    https://github.com/rogerclarkmelbourne/Arduino_STM32/wiki/Installation
  5. I tried running st-link from command line. It failed to enable it to find the libstlink,
    export LD_LIBRARY_PATH=/usr/local/lib
    sudo ldconfig
  6. Now, sudo stlink works!
  7.  Downloaded the bootloader from https://github.com/rogerclarkmelbourne/STM32duino- bootloader/blob/master/binaries/generic_boot20_pc13.bin as this has the LED on PC13.
  8. Next, we plug in the stlinkV2 adapter and probe for the chip ,then flash the progam into it.
    farhan@Farhan:~/Documents/ham/stm32$ sudo st-info --probe
    Found 1 stlink programmers
     serial: 2d0c02012612344d314b4e00
     hla-serial: "\x2d\x0c\x02\x01\x26\x12\x34\x4d\x31\x4b\x4e\x00"
     flash: 131072 (pagesize: 1024)
     sram: 20480
     chipid: 0x0410
     descr: F1xx Medium-density
    farhan@Farhan:~/Documents/ham/stm32$ st-flash --reset write generic_boot20_pc13.bin 0x8000000
    st-flash 1.6.0-392-g31b1fa1
    2020-06-30T15:42:38 INFO common.c: F1xx Medium-density: 20 KiB SRAM, 128 KiB flash in at least 1 KiB pages.
    file generic_boot20_pc13.bin md5 checksum: f6d7639c776ddc06d62a1b612ef7876, stlink checksum: 0x00192077
    2020-06-30T15:42:38 INFO common.c: Attempting to write 22268 (0x56fc) bytes to stm32 address: 134217728 (0x8000000)
    2020-06-30T15:42:38 INFO common.c: Flash page at addr: 0x08000000 erased
    2020-06-30T15:42:38 INFO common.c: Flash page at addr: 0x08000400 erased
    2020-06-30T15:42:38 INFO common.c: Flash page at addr: 0x08000800 erased
    2020-06-30T15:42:38 INFO common.c: Flash page at addr: 0x08000c00 erased
    2020-06-30T15:42:38 INFO common.c: Flash page at addr: 0x08001000 erased
    2020-06-30T15:42:38 INFO common.c: Flash page at addr: 0x08001400 erased
    2020-06-30T15:42:38 INFO common.c: Flash page at addr: 0x08001800 erased
    2020-06-30T15:42:38 INFO common.c: Flash page at addr: 0x08001c00 erased
    2020-06-30T15:42:38 INFO common.c: Flash page at addr: 0x08002000 erased
    2020-06-30T15:42:38 INFO common.c: Flash page at addr: 0x08002400 erased
    2020-06-30T15:42:38 INFO common.c: Flash page at addr: 0x08002800 erased
    2020-06-30T15:42:38 INFO common.c: Flash page at addr: 0x08002c00 erased
    2020-06-30T15:42:39 INFO common.c: Flash page at addr: 0x08003000 erased
    2020-06-30T15:42:39 INFO common.c: Flash page at addr: 0x08003400 erased
    2020-06-30T15:42:39 INFO common.c: Flash page at addr: 0x08003800 erased
    2020-06-30T15:42:39 INFO common.c: Flash page at addr: 0x08003c00 erased
    2020-06-30T15:42:39 INFO common.c: Flash page at addr: 0x08004000 erased
    2020-06-30T15:42:39 INFO common.c: Flash page at addr: 0x08004400 erased
    2020-06-30T15:42:39 INFO common.c: Flash page at addr: 0x08004800 erased
    2020-06-30T15:42:39 INFO common.c: Flash page at addr: 0x08004c00 erased
    2020-06-30T15:42:39 INFO common.c: Flash page at addr: 0x08005000 erased
    2020-06-30T15:42:39 INFO common.c: Flash page at addr: 0x08005400 erased
    2020-06-30T15:42:39 INFO common.c: Finished erasing 22 pages of 1024 (0x400) bytes
    2020-06-30T15:42:39 INFO common.c: Starting Flash write for VL/F0/F3/F1_XL core id
    2020-06-30T15:42:39 INFO flash_loader.c: Successfully loaded flash loader in sram
     22/22 pages written
  9. This concludes installation of the bootloader and any other program into the Blue pill if we already have a .BIN file. Whew!

Part 2 Setting up the Arduino IDE

Here, we will figure out how to compile our blink with an output to the USB’s serial port.
  1. The Arduino_STM32 folder has to be copied into a newly created ‘hardware’ folder inside the ‘Arduino’ folder in the home directory.
  2. We checked that lsusb lists the port as
    farhan@Farhan:~/Documents/ham/stm32$ lsusb
    Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
    Bus 001 Device 005: ID 06cb:00a2 Synaptics, Inc. 
    Bus 001 Device 004: ID 04f2:b604 Chicony Electronics Co., Ltd 
    Bus 001 Device 002: ID 046d:c534 Logitech, Inc. Unifying Receiver
    Bus 001 Device 026: ID 1eaf:0004 
    Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
  3.  Under tools > port, the Bluepill appears as /dev/ttyACM0 (Maple Mini)
  4. The blink was compiled, and uploaded through “STM32duino Bootloader” . The board had to be reset for it to be recognized, then it uploaded properly.
  5. Here is the test sketch (the potentiometer was connected to A7 and between GND and 3.3V).
    const int analogInPin = PA7; // Analog input pin that the potentiometer
     // is attached to
    
    const int pwmOutPin = 9; // PWM pin that the LED is attached to
    
    // These variables will change:
    int sensorValue = 0; // value read from the pot
    int outputValue = 0; // value output to the PWM
    
    void setup() {
     // Configure the ADC pin
     pinMode(analogInPin, INPUT_ANALOG);
     // Configure LED pin
     pinMode(pwmOutPin, PWM);
     Serial.begin(115200); // Ignored by Maple. But needed by boards using Hardware serial via a USB to Serial Adaptor
    }
    
    void loop() {
     // read the analog in value:
     sensorValue = analogRead(analogInPin);
     // map it to the range of the analog out:
     outputValue = map(sensorValue, 0, 1023, 0, 65535);
     // change the analog out value:
     pwmWrite(pwmOutPin, outputValue);
    
    // print the results to the serial monitor:
     Serial.print("sensor = " );
     Serial.print(sensorValue);
     Serial.print("\t output = ");
     Serial.println(outputValue);
    }
  6. The output came out on the serial port alright.
  7. While uploading be sure to press the RESET button the bluepill simultaneously with the upload button on the Arduino IDE, this will catch the bootstrap loader before the serial port opens itself up for debug messaging.

Step 3 – Measure the analog conversion rate

  1. The ADC example is on https://github.com/rogerclarkmelbourne/Arduino_STM32/blob/master/STM32F1/libraries/STM32ADC/examples/MultiChannelContinuousConversion/MultiChannelContinuousConversion.ino
  2. It needs to kickstart the conversion with a startConversion call:
    uint8_t pins[] = {PA7, PA6};
    const int MAX_SAMPLES = 8;
    uint16_t data_points[MAX_SAMPLES];
    
    void setup() {
     // put your setup code here, to run once:
     my_adc.calibrate();
    
    for (unsigned int j = 0; j <8; j++) 
     pinMode(pins[j], INPUT_ANALOG);
     
     my_adc.setSampleRate(ADC_SMPR_1_5);//set the Sample Rate
     my_adc.setScanMode(); //set the ADC in Scan mode. 
     my_adc.setPins(pins, 2); //set how many and which pins to convert.
     my_adc.setContinuous(); //set the ADC in continuous mode.
    
    //set the DMA transfer for the ADC. 
    //in this case we want to increment the memory side and run it in circular mode
    //By doing this, we can read the last value sampled from the channels by reading the dataPoints array
     my_adc.setDMA(data_points, 2, (DMA_MINC_MODE | DMA_CIRC_MODE), NULL);
     my_adc.startConversion();
    
    Serial.begin(115200); // Ignored by Maple. But needed by boards using Hardware serial via a USB to S
    }

Leave a Reply

Your email address will not be published. Required fields are marked *