Author Topic: $0.11 PY32F002A: Cortex-M0+ MCU, actually a PY32F030! 32/4KB, 48MHz, PLL, DMA...  (Read 21382 times)

0 Members and 2 Guests are viewing this topic.

Online DavidAlfaTopic starter

  • Super Contributor
  • ***
  • Posts: 5730
  • Country: es
To avoid bloating my HK32030M thread, I'll be posting the PY32F002A bits here.

Links:
Puya gitee repo: Docs, libs, software...
Puya support pack 1: Docs, libs, ISP software...
Puya support pack 2: Similar contents but newer, has english datasheets & RM.
IOSetting's repo containing py32 template and libs.
MCU-Flash-Tools. Python-based ISP bootloader tool for WCH and PY32 mcus, removing the dependency of Puya-ISP.

Tested so far:
- Bootloader. Pulling PB6 (BOOT0) high before power-on (Or resetting afterwards) enters the embedded bootloader, just like in STM32.
  Connect RX/TX to PA2/PA3, open Puya ISP, everything works.
  Thread showing PuyaISP:  https://www.elektroda.pl/rtvforum/topic3951956.html


- STM32 CubeIDE: The patch works, I was able to program & debug using JLink.
  CubeIDE Patch adding support for several devices, including PY32.
  PY32 folder with docs and CubeIDE PY32F030 sample project. Patch must be applied before attempting to open this project, otherwise CubeIDE will crash.

  Jlink doesn't support this chip yet so you must manually add it. Place IOSetting's PY32 folder following this instructions:
Quote
OS                     Location
Windows             C:\Users\<USER>\AppData\Roaming\SEGGER\JLinkDevices
Linux                  $HOME/.config/SEGGER/JLinkDevices
macOS               $HOME/Library/Application Support/SEGGER/JLinkDevices
Alternatively, you can convert a ST-Link into JLinkEDU, but it will block any attempts to use non-ST devices.
The solution is to edit the XML file and change the vendor to ST (ChipInfo Vendor="ST" ).
But if the device already exists in Jlink software, then you must change the device name, for example "_py32f002af15p6".
Then set this device name under Jlink configuration.
DO NOT do this for commercial purposes. Most people can't afford $100 programmers to just blink some leds.

So far this chip seems to contain the same die as the PY32F030x6, with the same 4/32KB. PLL, DMA, 2nd UART & SPI...
More info: https://www.elektroda.com/rtvforum/topic3946116.html

Tested so far:
 - 32KB flash
 - 4KB RAM
 - PLL (48MHz)
 - 2xSPI, 2xUSART
 - DMA

After setting RDP1, I could no longer program or erase it with J-Link, I had to use PUYA ISP, setting BOOT=1 to enter the serial bootloader and running "Clear chip".


Edit: Fix PCB:
- Missing GND trace for CH340E !!
- CH340E RTS/DTR not working as expected, removed them.
- Fix BOOT0 button, pull pin high instead low.

Quote
Basic BOM:

CH340E
PY32F002AF15P6TU
HK32F030MF4P6
SX3M32.000M20F30TNN
TYPE-C-31-M-12
ME6211A33PG-N
1N5819WT
HS91L02W2C01
XL-3216RGBC
CTS-3425J-V-TR
Rest are common parts easily sourced anywhere, 0402 resistors / capacitors, pin headers, 25xx/24xx memories...

The board doesn't need to be fully populated. Actually just the LDO and few caps and you'll have a working board.
You can add the rest of functionalities depending on your needs.


I also extracted the factory bootloader located at 0x1FFF0000, just in case anyone wants to tinker with it in Ghidra/IDA.
« Last Edit: January 01, 2024, 12:10:28 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 
The following users thanked this post: thm_w, edavid, iMo, zrq, IOsetting, Nikki Smith

Online DavidAlfaTopic starter

  • Super Contributor
  • ***
  • Posts: 5730
  • Country: es
The USART2 is definitely there!
I'm toggling the pin AF before transmitting, PA2 AF1=TX1, AF4=TX2.

Edit:
Confirmed the PLL is also present and works, the system boosts to 48MHz.
Overclockability beyond this point is weak, not reaching even 64MHz without crashing.
Could It be the reason it's being sold as a 24MHz part? Perhabs.
As stated in the Elektroda thread, the HSI TRIM value is not restored on system reset, so be careful (PLL has no issues here).


Edit2: Also DMA works. This chip has an amazing value for the cost!
« Last Edit: July 10, 2023, 09:04:38 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline prosper

  • Regular Contributor
  • *
  • Posts: 63
  • Country: ca
i played with iosetting's environment and its examples, and can confirm that the 002 can be treated as an 030. pll, dma, flash, and sram were tested and work for me.
 
The following users thanked this post: DavidAlfa

Online DavidAlfaTopic starter

  • Super Contributor
  • ***
  • Posts: 5730
  • Country: es
I finished adding CubeIDE support for all PY32F0xx devices.
Updated the CubeIDE patch and added a PY323F030 sample project in the main message.
IOSetting's LL libraty worked great after setting the project up.
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Online iMo

  • Super Contributor
  • ***
  • Posts: 4575
  • Country: np
  • It's important to try new things..
Does the IDE support double precision floating point math on this device?
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 3935
  • Country: nz
Does the IDE support double precision floating point math on this device?

It would be very strange if it didn't. Using the compiler's runtime library software emulation, of course. Which might eat half of your flash.
 

Online iMo

  • Super Contributor
  • ***
  • Posts: 4575
  • Country: np
  • It's important to try new things..
Yep, would be great to test it somehow, how it fits.
64kB flash would be handier, however..

PS: the + - * / sqrt would be ok for most cases (ie while messing with 6-7digits DMM values).

« Last Edit: July 21, 2023, 11:35:19 am by iMo »
 

Online iMo

  • Super Contributor
  • ***
  • Posts: 4575
  • Country: np
  • It's important to try new things..
Like this test for example:
Code: [Select]
// Double precision math TEST by iMo and AI

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>

#define NUM_SAMPLES 100

double calculate_mean(double* data, int n) {
    double sum = 0.0;
    for (int i = 0; i < n; i++) {
        sum += data[i];
    }
    return sum / n;
}

double calculate_standard_deviation(double* data, int n, double mean) {
    double variance = 0.0;
    for (int i = 0; i < n; i++) {
        variance += (data[i] - mean)*(data[i] - mean);
    }
    variance /= n;
    return sqrt(variance);
}

int main() {
    // Seed the random number generator
    //srand(time(NULL));

    double random_numbers[NUM_SAMPLES];

    // The rand() function computes a new pseudo-random number in the interval 0 to RAND_MAX
    // #define RAND_MAX  0x3fffffff

    // Generate 100 random double fp numbers between 9.99999 and 10.0
    for (int i = 0; i < NUM_SAMPLES; i++) {
        random_numbers[i] = 9.99999 + ((double)rand() / RAND_MAX) * (10.0 - 9.99999);
    }

    // Calculate the mean of the 100 samples
    double mean = calculate_mean(random_numbers, NUM_SAMPLES);

    // Calculate the standard deviation of the 100 samples
    double std_deviation = calculate_standard_deviation(random_numbers, NUM_SAMPLES, mean);

    //printf("Sample Mean: %lld\n", (long long)(mean*1e15));
    //printf("Sample Standard Deviation: %lld\n", (long long)(std_deviation*1e15));

    printf("Sample Mean: %d\n", (int)(mean*1e8));
    printf("Sample Standard Deviation: %d\n", (int)(std_deviation*1e14));

    return 0;
}

Sample Mean: 999999533
Sample Standard Deviation: 293668347

//Sample Mean: 9.999999533124507
//Sample Standard Deviation: 0.000000293668347
« Last Edit: July 21, 2023, 03:14:26 pm by iMo »
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 3935
  • Country: nz
Now you're including not only DP calculation, but also printf that supports DP FP. That's another thing again.
 

Online iMo

  • Super Contributor
  • ***
  • Posts: 4575
  • Country: np
  • It's important to try new things..
ok fixed above
PS: alternative int32 print, such we can see something printed out, what is the "test" is how it fits into the flash of these CM0 gadgets..

Code: [Select]
    printf("Sample Mean: %d\n", (int)(mean*1e8));
    printf("Sample Standard Deviation: %d\n", (int)(std_deviation*1e14));

Sample Mean: 999999533
Sample Standard Deviation: 293668347
« Last Edit: July 21, 2023, 03:16:01 pm by iMo »
 

Online DavidAlfaTopic starter

  • Super Contributor
  • ***
  • Posts: 5730
  • Country: es
It's ARM and GCC, not a strange architecture with propietary compiler, so of course it supports that.
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Online iMo

  • Super Contributor
  • ***
  • Posts: 4575
  • Country: np
  • It's important to try new things..
And how does it fit inside? Provided you already work with the gcc and the chip - could you cut and paste the source above, build it and look at the flash mem used, plz?
 

Online DavidAlfaTopic starter

  • Super Contributor
  • ***
  • Posts: 5730
  • Country: es
Like this test for example
You know this is a pretty terrible example for a small MCU, where you normally would try to avoid printf etc at all costs  ;)
But anyways, compiled with -Os. Without your code, flash usage is 508 Bytes, of which 192 are the ISR vectors.
« Last Edit: July 21, 2023, 08:42:45 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 
The following users thanked this post: iMo

Online iMo

  • Super Contributor
  • ***
  • Posts: 4575
  • Country: np
  • It's important to try new things..
I've installed new Cube and built my example for an STM32F030R8 with the HAL there -> 17.3kB flash. Lot of HAL stuff re system config and UART. You've done it with LL thus you've saved precious ~4kB :)..
 

Online DavidAlfaTopic starter

  • Super Contributor
  • ***
  • Posts: 5730
  • Country: es
I did with LL, but using PY32 lib, not CubeMX.
Also there was no system initialization (Clock, etc), just the main() with your code.
Some HAL functions can get really heavy, I remember seeing 1KB used by the oscillator/clock initialization alone!
« Last Edit: July 23, 2023, 03:33:30 am by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Online iMo

  • Super Contributor
  • ***
  • Posts: 4575
  • Country: np
  • It's important to try new things..
1020bytes oscRCC, pretty heavy, indeed..
 

Online DavidAlfaTopic starter

  • Super Contributor
  • ***
  • Posts: 5730
  • Country: es
Plus the aeabi routines, also used by that function.
If you tweak so it only handles the default speed instead all combinations, it'll get a lot smaller.
Or use LL for the clock, it might improve.
« Last Edit: July 28, 2023, 04:27:07 am by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Online iMo

  • Super Contributor
  • ***
  • Posts: 4575
  • Country: np
  • It's important to try new things..
I've patched the latest Cube with your above patches. Now - does it allow to see the PY/HK chips in the mcu type selector? Or how do you use the patches actually?? I am happy to use them with the Cube's HAL at this stage.
 

Online DavidAlfaTopic starter

  • Super Contributor
  • ***
  • Posts: 5730
  • Country: es
Yes, check the new picture I added to the 1st message.
But anyways you will likely want to use the libs, check the 1st message again with the py32 folder.
« Last Edit: July 23, 2023, 02:46:06 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline danymogh

  • Regular Contributor
  • *
  • Posts: 51
  • Country: ge
which programmer are you using to connect to the PY32?

I have a st link v2 which i converted to DAP link but neither KEIL nor puya programmer don't detect it.

in the vendor pack there are 2 manuals regarding this PY-LINK programmer which seems to be a modified Jlink but I found no resources or a place to buy it.

in the manual, It just selects DAP-link to connect to the MCU so I suppose any DAP link would be ok, however, it seems that's not the case.

so which clone programmers are you using to connect to it? would the Jlink OB work?
 

Online DavidAlfaTopic starter

  • Super Contributor
  • ***
  • Posts: 5730
  • Country: es
More important, what software are you using? The programmer is just the interface!

I'm using patched CubeIDE (But anything able to use Jlink will work).
For DIY /Learing you can convert a STLINK into JLINK, though you'll need custom JLinkDevices to fake them as ST parts.
Not hard, please use ethically.

Alternatively, if you are only programming (Not debugging), then simply use a serial converter and Puya ISP!
« Last Edit: August 05, 2023, 06:01:22 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline danymogh

  • Regular Contributor
  • *
  • Posts: 51
  • Country: ge
Thanks for the tip.

I ended up finding this repo
https://github.com/RadioOperator/STM32F103C8T6_CMSIS-DAP_SWO
and will turn one of my bluepills to CMSIS-DAP and will test it.

Is there any program that supports the Puya programmer software? where is the Py-link available ?
 

Online DavidAlfaTopic starter

  • Super Contributor
  • ***
  • Posts: 5730
  • Country: es
Puya ISP is the software itself. Connect the serial converter to Rx/Tx, set boot=1, that's all it takes.
I have no idea what to use for PyLink.
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline danymogh

  • Regular Contributor
  • *
  • Posts: 51
  • Country: ge
I finally managed to get it working.

I first used a USB TTL as you described and connected to it by Puya ISP. I had to pull up Boot0 pin.
The software is a bit tricky. I had to reset the VDD after every program or chip cleared. (I'm not using DTR or RST).
Also if you attempt to readout a read-protected chip it automatically clears the chip!

Then I converted a Bluepill to a CMISIS-DAP and managed to get it to work with KEIL. the standard SWD, SWCK, VDD, GND, and Reset.

I can now use the debug features. however, It doesn't work with Puya Programmer.

Is there any way to set the option bytes with CMSIS-DAP?
 

Online DavidAlfaTopic starter

  • Super Contributor
  • ***
  • Posts: 5730
  • Country: es
Tricky? Couldn't be easier!

Mcu info doesn't clear the chip automatically, just shows it's protected.
Download code does... exactly that!
It will reset automatically on erasing/programming, if you permanently pull boot high, it'll enter bootloader again.
(So if you plan to do multiple operations, leave boot high all the time)
If you want it to execute the program automatically without releasing boot0, tick "Use GO command".
« Last Edit: August 06, 2023, 03:30:19 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf