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

0 Members and 2 Guests are viewing this topic.

Online bingo600

  • Super Contributor
  • ***
  • Posts: 1989
  • Country: dk

So, any ideas? What programmer (if any) I need in order to write this firmware to a bluepill / blackpill? Will my ST-Link v2 clone work?
https://github.com/RadioOperator/STM32F103C8T6_CMSIS-DAP_SWO

Your ST_link should be able to program a "Bluepill" with the above firmware.

I'd suggest you program the "remap hex" from here : https://github.com/RadioOperator/STM32F103C8T6_CMSIS-DAP_SWO/tree/master/build
Then the picts on the above site seem to match your new cmsis-dap bluepill board.


Unless you're a Linux user.
I can't help with "how to do it" ...


This post might come in handy, when needing the PY devicepacks for pyocd
https://www.eevblog.com/forum/microcontrollers/$0-11-py32f002a-m0-24mhz-320kb-actually-324kb-more-peripherals/msg5213643/#msg5213643


/Bingo
« Last Edit: April 22, 2024, 05:31:47 pm by bingo600 »
 

Offline Sacodepatatas

  • Regular Contributor
  • *
  • Posts: 83
  • Country: es
Has anybody noticed any funcional difference between the PY32F002BF15P and the PY32L020F15P apart from the specified max speed? I would like to know if these two chips are both interchangeable.
What I know about PY32L020 is that it has an extra "deep stop" mode while PY32F002B only has "stop" mode.
If you don't need the low power feature, they are the same.
BTW, PY32F002BF15P can also run in 48MHz, add a "#define RCC_HSI48M_SUPPORT" in CMSIS/Device/PY32F0xx/Include/py32f002bx5.h will enable it.

Very informative. Thank you so much.
 

Offline John Celo

  • Contributor
  • Posts: 21
  • Country: lt
Hi, another newbie/amateur here, hopefully you still have some patience for one of those  :D
I'd like some help writing to the flash of the PY32F002AL15S6TU (That is, the SOP-8 package).
After reading this thread and finding out that only the TSSOP-20 package supports ISP, I tried first with my PY32F002AF15P6TU (TSSOP-20) using Arduino as my IDE and writing to the PY32F002AF15P6TU directly from Arduino using a USB->TTL adapter with BOOT0 pulled high and everything worked well on the first try (yes, I was also surprised :) ) - Got some LEDs blinking and some buzzers beeping and was happy.

But now I want to return to my original goal of flashing the SOP-8 version. The thing is, I haven't touched a traditional IDE since collage (more than 3 decades ago) and I really don't want to start installing and then messing with CubeIDE, Keil, Eclipse, Python stuff, or any other heavy software that requires fiddling with definitions, settings and configuration files. Is there a simple solution like a standalone software such as PuyaISP in the the sense of just running an EXE (I'm on Windows) and writing? Or better yet, writing directly from the Arduino IDE ?
I'm an absolutest newbie/amateur, and I did get SOP-8 flashing, used J-Link OB (with keil uvision) and CMSIS/DAP-link (via pyOCD which is a command line flasher, it's not hard to use at all!).

That being said, SOP-8 is pain in the ass to work with, you functionally have 3 pins left (VCC/GND/SWC/SWD/NRST) if you don't remap SWC/SWD/NRST - and I dont think it's worth bothering with that. SOP-16 is just a better fit for a beginner, since you can hook up uart and have some extra printfs diagnostics messages ON TOP of debugger. I also use uart to send commands from my computer to the chip.

Frankly, if you want Arduino as an IDE, I'd suggest to stick with tssop-20 package, it takes as much space as SOP-8 anyway and costs the same.

I did go the absolute opposite route - I do want a debugger and prefer an IDE, so I use keil uVision (but might move to another IDE).
This sop16 chip is my first step outside of Arduino environment.

So far I've got multiple ADCs working in continuous mode, uart and i2c is also working (although i2c is a bit finicky) using provided high-level HAL (as far as I can tell it's similar to hal used by stm32), I do intend to move to lower level HAL as I find them easier to reason about with reference sheet in hand.

These chips are kind of slow, so if you sample multiple ADCs at total sample rate of 400kHz, and it runs at 24mHz, you only get ~60 cycles
to work for every sample! If you have some external timing sensitive signals that trigger an interrupt, those can easily take 16 cycles or more if you account for storing/restoring registers state, etc! (I have to time this!)

I've yet to try the "hidden" PY32F002A features like DMA and clocking it to 48mhz (is this hard to do?).
« Last Edit: April 23, 2024, 01:37:01 am by John Celo »
 

Offline John_doe

  • Regular Contributor
  • *
  • Posts: 52
  • Country: il
Your ST_link should be able to program a "Bluepill" with the above firmware.

I'd suggest you program the "remap hex" from here : https://github.com/RadioOperator/STM32F103C8T6_CMSIS-DAP_SWO/tree/master/build
Then the picts on the above site seem to match your new cmsis-dap bluepill board.
Thanks Bingo, I was able to write the "remap" firmware to the blue pill with 'STM32 ST-LINK utility'.
I also had success writing to the SOP-8 chip with OpenOCD. I will write a summery of what I did in a new message.

I'm an absolutest newbie/amateur, and I did get SOP-8 flashing, used J-Link OB (with keil uvision) and CMSIS/DAP-link (via pyOCD which is a command line flasher, it's not hard to use at all!).
Thank you, but I already managed to write to the chip with OpenOCD, and a good thing I did, because I HATE Python with a vengeance!  >:D

That being said, SOP-8 is pain in the ass to work with, you functionally have 3 pins left (VCC/GND/SWC/SWD/NRST) if you don't remap SWC/SWD/NRST - and I dont think it's worth bothering with that. SOP-16 is just a better fit for a beginner, since you can hook up uart and have some extra printfs diagnostics messages ON TOP of debugger. I also use uart to send commands from my computer to the chip.
Wait, only 3 pins? Does that mean that if I use the SWC/SWD pins in my Arduino sketch, I won't be able to write to the chip again  ???

I've yet to try the "hidden" PY32F002A features like DMA and clocking it to 48mhz (is this hard to do?).
No idea, but I would also like to know how to do that. Maybe you just select PY32F030 as your target chip when writing?
 

Offline John Celo

  • Contributor
  • Posts: 21
  • Country: lt
That being said, SOP-8 is pain in the ass to work with, you functionally have 3 pins left (VCC/GND/SWC/SWD/NRST) if you don't remap SWC/SWD/NRST - and I dont think it's worth bothering with that. SOP-16 is just a better fit for a beginner, since you can hook up uart and have some extra printfs diagnostics messages ON TOP of debugger. I also use uart to send commands from my computer to the chip.
Wait, only 3 pins? Does that mean that if I use the SWC/SWD pins in my Arduino sketch, I won't be able to write to the chip again  ???
The chip is gone if you reassign both SWC/SWD and NRST.
If you have SWC/SWD reassigned, but NRST is still available, you can still flash the chip if you pull NRST low (which forces the SWC/SWD pins to get reassigned to defaults).

What I mean by saying "3 pins" is - that during development, you will probably want to have SWC/SWD connected all the time as you iterate and work on your design & code.

If you reassign SWC/SWD you'll have to have some way to share those programming wires with whatever peripherals you want there and have some way to connect/reconnect those - not to interfere with flashing, as well as have to hold NRST while programming.

It just seems like a hassle for no real benefit? Price is the same, and package is roughly the same size as tssop20.

I bought a few py32 sop8 chips myself, and retrospectively that was a mistake, with so few pins there's not much I can use them for.

 

Offline John_doe

  • Regular Contributor
  • *
  • Posts: 52
  • Country: il
I'm writing a summery of what I did to program these PY32F002A chips. Maybe it'll help some other n00b to save some time:

1. I installed PY32Duino for the Arduino IDE. I just followed the instructions here (used Google to translate the site to English):
https://arduino.py32.org/

2. Wrote a simple test sketch and compiled it. To upload it to the TSSOP-20 version, I just connected a USB to TTL module (costs less than $1 on AliExpress) and uploaded from the Arduino GUI. Instructions on how to connect the module to the chip can be found here:
https://github.com/wagiminator/MCU-Flash-Tools?tab=readme-ov-file#puyaisp

3. To write to the chip packages that do not have a BOOT0 pin, a programmer was needed, I didn't have a suitable one, such as these that were recommended in this thread:
https://www.aliexpress.com/item/1005003620126365.html
https://www.aliexpress.com/item/1005005179354439.html
So I used an ST-Link v2 clone to write this firmware to an STM32F103 "Blue Pill":
https://github.com/RadioOperator/STM32F103C8T6_CMSIS-DAP_SWO/blob/master/build/F103-DAP-SWO-CDC-BLUEPILL-SWD_REMAP.hex
To do that I used STM32 ST-LINK utility from this link:
https://www.st.com/en/development-tools/stsw-link004.html
I had to hold the reset button while pressing Target>Connect . Connections from ST-Link to blue pill are 3.3v->3.3V ; GND ->GND ; SWCLK->SWCLK ; SWDIO->SWDIO.

4. After I had a working CMSIS-DAP, I took the compiled Arduino hex file (if you don't know where to find it, google it) and used OpenOCD. To do that I downloaded the OpenOCD package that already includes the PY32 support files from the PUYA gitee:
https://gitee.com/puya-semiconductor/tools-and-software/blob/master/PY32_GCC/openocd-0.12.0.zip
(Thanks IOsetting and DavidAlfa for the link)
I had to modify the "openocd_run.bat" file to include the ".\bin" folder:
Code: [Select]
.\bin\openocd.exe -s ./scripts -f ./scripts/interface/cmsis-dap.cfg -f ./scripts/target/py32f002x.cfg
5. Had to enable Telnet for my Windows 10 (google it if you don't know how) from the control panel.

6. To connect the blue pill CMSIS-DAP to the IC, I connected the USB of it to my PC and from the blue pill to the PY32F002A the connections are: 3.3V->VCC ; GND->GND ;  SWCLK->SWC ; SWDIO->SWD. For the SOP-8 Package, the pins are 1 ; 8 ; 4 ; 5, respectively.

7. To use OpenOCD, I ran the batch file and in a new command prompt window I entered:
Code: [Select]
telnet localhost 4444And then the following commands:
Code: [Select]
reset halt
flash write_image erase c:/myfolder/myfile.hex
Notice the forward slash in the path to hex file. The command will fail if a Backslash is used.
 
The following users thanked this post: IOsetting, John Celo, JimNickerson

Offline prosper

  • Regular Contributor
  • *
  • Posts: 80
  • Country: ca
Thanks for the write up. I was excited about openocd for the PY32.... but there's (still) no source and no linux version. Ah well.

Also may be worth noting: beyond openocd and pyocd, there's also probe-rs which supports the py32. Probe-rs has similar capabilities to openocd and pyocd, but has a much nicer CLI than openocd (more like pyocd). It's also pretty quick (not being python). The only real downside that I've found is that its GDB stub is pretty limited (you can't 'load,' for example), so its not as good as a debug server. [though it does have a vscode extension which supports the microsoft DAP protocol directly, if vscode is your thang]
« Last Edit: April 24, 2024, 04:08:37 pm by prosper »
 

Offline John_doe

  • Regular Contributor
  • *
  • Posts: 52
  • Country: il
The chip is gone if you reassign both SWC/SWD and NRST.
If you have SWC/SWD reassigned, but NRST is still available, you can still flash the chip if you pull NRST low (which forces the SWC/SWD pins to get reassigned to defaults).
Thanks for the useful tip! I tried it, that is, pinMode() for both the SWD and SWC pins and indeed couldn't connect to the chip anymore. Holding NRST low (and releasing it after the command "reset halt") worked like a charm!

Thanks for the write up. I was excited about openocd for the PY32.... but there's (still) no source and no linux version. Ah well.
Wait, so the PY32 support of openOCD is built into the executable? I didn't know that... I thought that if you download the official Linux version and add the *.cfg files to it from the gitee archive it'll work.

Also may be worth noting: beyond openocd and pyocd, there's also probe-rs which supports the py32.
Thanks, right now I got myself busy with something else but when I'll return to the PY32F002A project I'll be sure to try probe-rs.
« Last Edit: April 25, 2024, 04:37:01 am by John_doe »
 

Offline prosper

  • Regular Contributor
  • *
  • Posts: 80
  • Country: ca
Quote
Wait, so the PY32 support of openOCD is built into the executable? I didn't know that... I thought that if you download the official Linux version and add the *.cfg files to it from the gitee archive it'll work.

That's right. You need the configs, but they just specify which functions to use. The functions themselves are in the binary.
 

Offline IOsetting

  • Regular Contributor
  • *
  • Posts: 58
  • Country: cn
I doubt 002b also supports "Super Deep Sleep" but I didn't test it.
it's very likely that 002B is an equivalent of L020, I just run the Deep Stop example on a TSSOP20 PY32F002b, the current consumption is much lower than the Stop mode ( 0.6 uA vs 1.6 uA).
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11277
  • Country: us
    • Personal site
FYI, I just added PY32F002A to my programming tool https://github.com/ataradov/edbg . It works with any CMSIS-DAP debugger and is just a simple standalone executable with no dependencies. Binaries for Windows/Linux/Mac are available.

I'm happy to add other parts, but nowhere I can find IDCODE values for them. They are not documented anywhere. So, if you have other devices and need support, run the tool, it will report the ID for the unsupported device.

BTW, 002B does not officially document code read protection. There is no RDP field and all text about read protection is removed. No idea what this is all about, seems rather strange.
« Last Edit: April 29, 2024, 06:57:18 pm by ataradov »
Alex
 
The following users thanked this post: bingo600

Offline DavidAlfaTopic starter

  • Super Contributor
  • ***
  • Posts: 5931
  • Country: es
Very nice tool  :-+
AFAIK the 002B is the same thing, just a different pinout, making the higher end 030 a direct swap.
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline John_doe

  • Regular Contributor
  • *
  • Posts: 52
  • Country: il
That's right. You need the configs, but they just specify which functions to use. The functions themselves are in the binary.
Thank you for clarifying.

...I just added PY32F002A to my programming tool https://github.com/ataradov/edbg . It works with any CMSIS-DAP debugger and is just a simple standalone executable with no dependencies.
Phenomenal! Just tried EDBG and it works like a charm (see attachment). This is much more convenient than OpenOCD for me. Hopefully the guys on this forum will help you with the missing ID codes.
« Last Edit: April 30, 2024, 12:44:49 am by John_doe »
 

Offline IOsetting

  • Regular Contributor
  • *
  • Posts: 58
  • Country: cn
BTW, 002B does not officially document code read protection. There is no RDP field and all text about read protection is removed. No idea what this is all about, seems rather strange.
I checked this with their tech, FLASH_SDKR in option bytes can be used for read protection. They gave a short video https://v.douyin.com/i2R2Ecd7/ (in Chinese) to demonstrate this feature. They use an offline programmer to write the option bytes.
« Last Edit: April 30, 2024, 03:12:01 am by IOsetting »
 

Offline Sacodepatatas

  • Regular Contributor
  • *
  • Posts: 83
  • Country: es
it's very likely that 002B is an equivalent of L020, I just run the Deep Stop example on a TSSOP20 PY32F002b, the current consumption is much lower than the Stop mode ( 0.6 uA vs 1.6 uA).

In fact, I checked this header file of the LL driver in your repo...

https://github.com/IOsetting/py32f0-template/blob/main/Libraries/PY32F002B_LL_Driver/Inc/py32f002b_ll_pwr.h

And i found an interesting paragraph...

Quote
#if defined(PWR_DEEPSTOP_SUPPORT)
/**
  * @brief  Set SRAM retention voltage control in deep stop mode.
  * @note   Depending on devices and packages, Deep Low Power Run mode may not be available.
  *         Refer to device datasheet for Deep Low Power Run mode availability.
  * @param  VoltCtrl This parameter can be one of the following values:
  *         @arg @ref LL_PWR_SRAM_RETENTION_VOLT_CTRL_LDO
  *         @arg @ref LL_PWR_SRAM_RETENTION_VOLT_CTRL_LOW
  * @retval None
  */
....

So if you define PWR_DEEPSTOP_SUPPORT in the code, you can use the Deep Stop functions in the library. However It remains knowing if lower pin count packages support this in practice, because of lack of those packages in the L020.

Very nice tool  :-+
AFAIK the 002B is the same thing, just a different pinout, making the higher end 030 a direct swap.

It looks like the F002B/L020 is aimed for replacing existing 8 bit low end MCUs. If you notice, GPIOs and AFs range from 0 to 7, and pinouts of various packages match the ones of other 8 bit MCUs such as STM8S, Nuvoton, FMD, etc.

I noticed there is a new PY32T020 whose doc is only in chinese. AFAIK the T means "touch button" capability. I wonder if there is any correspondence to the ones we know about at silicon level...
 

Offline John Celo

  • Contributor
  • Posts: 21
  • Country: lt
I can confirm that all three DMA channels of DMA1 work, USART with DMA works, PLL works (48mhz), continuous ADC sampling with DMA also works.

All what has to be changed is you have to use the startup .s file from py32f030 (startup_py32f030xx.s) which contains all the extra interrupt vectors, and adjust RAM/ROM size (although this is not strictly necessary since base address is the same). And it "just" works!

I've also switched over from using high level HAL to low level hal. Makes it way easier to understand things!

For beginners out there: don't be afraid of the disassembly, turns out these Cortex-M0+ chips have very simple instruction set:
https://developer.arm.com/documentation/ddi0484/c/Programmers-Model/Instruction-set-summary

This includes timings! Two stage-pipeline!
These chips are great! Way less complicated peripherals than all the fancy cortex-m7 chips, which makes them a great starting point!

« Last Edit: April 30, 2024, 03:02:31 pm by John Celo »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf