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

0 Members and 2 Guests are viewing this topic.

Online DavidAlfaTopic starter

  • Super Contributor
  • ***
  • Posts: 5913
  • Country: es
So these are are my locking / unlocking routines:
Code: [Select]
// Call with lock=1 to set RDP1, also disabling RESET pin and bootloader
// Call with lock=0 to disable, though if already locked it will erase everything, nothing can be done about it.
void rdp_set(bool lock)
{
  FLASH_OBProgramInitTypeDef pOBInit;
  LL_FLASH_OBGetConfig(&pOBInit);
  bool lock_state = (pOBInit.RDPLevel == OB_RDP_LEVEL_1);

  if(lock_state != lock)                                                              // current state not same as requested
  {
    pOBInit.OptionType = OPTIONBYTE_RDP | OPTIONBYTE_USER;                            // Update user and RDP bits
    pOBInit.RDPLevel = lock ? OB_RDP_LEVEL_1 : OB_RDP_LEVEL_0;                        // Set RDP mode

    if(lock)
    {
      pOBInit.USERConfig =  OB_RESET_MODE_GPIO | \                                    // Disable Reset function, enable BOR
                            OB_BOR_ENABLE | OB_BOR_LEVEL_3p1_3p2 | \                  // Enable BOR to 3.2V
                            OB_IWDG_SW | OB_WWDG_SW | \                               // Disable HW watchdog enforcement
                            OB_BOOT1_SRAM;                                            // When BOOT1=SRAM, BOOT0=1 boots from SRAM instead the bootloader
    }
    else
    {

      pOBInit.USERConfig =  OB_RESET_MODE_RESET | \                                   // Disable Reset function, enable BOR
                            OB_BOR_DISABLE | OB_BOR_LEVEL_3p1_3p2 | \                 // Disable BOR, set default level
                            OB_IWDG_SW | OB_WWDG_SW | \                               // Disable HW watchdog enforcement
                            OB_BOOT1_SYSTEM;                                          // When BOOT1=SYSTEM, BOOT0=1 boots the bootloader
    }
   
    LL_FLASH_Unlock();                                                                // Unlock Flash
    LL_FLASH_OB_Unlock();                                                             // Unlock OB
    LL_FLASH_OBProgram(&pOBInit);                                                     // Program config. Going RDP1->RDP0 instantly wipes the entire system, but still need a cycling the power to reload the RDP config.
    LL_FLASH_OB_Launch();                                                             // Force OB reload (This normally restarts the system)
    NVIC_SystemReset();                                                               // Just in case
  }
}
« Last Edit: August 24, 2023, 07:36:05 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: bingo600

Online DavidAlfaTopic starter

  • Super Contributor
  • ***
  • Posts: 5913
  • Country: es
Bingo600, as you asked in PM, here's a very simple example showing how to lock and unlock RDP.
PB0 = button to GND.
PB1 = RED led ( To indicate lock)
PB3 = GREEN led (Activity and unlocked indicator)

LEDS are active low - Connect between VCC and the mcu pin through a 470R-1K resistor.


Simply flash it - RED will be steady on, GREEN blinking as "I'm alive".
Press button - GREEN will be steady on, RED off. MCU will be wiped, cycle power to update RDP security, Jlink will be able to program it again.

Just in case, I'm only setting RDP1 in this test, not disabling nRST or BOOT0, at $0.11 it's cheap but better to not brick any! (I'm using the "full" mode myself, works perfect).
« Last Edit: August 24, 2023, 07:39:15 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: bingo600

Offline bingo600

  • Super Contributor
  • ***
  • Posts: 1989
  • Country: dk
Thanx David  :-+

I'll see if i can do a test in the weekend.

What pin should i apply the 9KHz signal to ??

Ahh .. This guy doesn't seem to have the timer stuff ...
Well i'll give it a shot this weekend ...

I'll have to setup Cube .... According to your instructions...
I'm (ATM) more of a "makefile guy"  :scared:

Do you use "just" CubeMX , or also CubeIDE ?
I don't even think i have CubeIDE downloaded.

/Bingo
« Last Edit: August 24, 2023, 08:24:13 pm by bingo600 »
 

Online DavidAlfaTopic starter

  • Super Contributor
  • ***
  • Posts: 5913
  • Country: es
I use the IDE as only that - IDE.
Provides editor, compiler and debugger all in one - that's why I patched it.
CubeMX is for ST products, I don't have interest in it, the M0+ is pretty easy to setup using low level libs,  that's what I use.

Yeah sorry I forgot! Will make it tomorrow.
For now just test that the unlocking works, it should!
It's not speed dependand, I crashed it with only few Hz, 100Hz would do it very quick.
Seems something related to the capture edge happening at precise wrong moment.

Normally this would read a IR signal with NEC protocol, the fastest pulse is 500-600us, this would crash it in the first remote button press (Doing fine now).


But as I say just anything at CCP crashed it with RDP set.
Maybe a sync bug, I don't know.
Anyways, it's fine now!
« Last Edit: August 24, 2023, 08:41:33 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: bingo600

Offline bingo600

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

I'm postponing the 1GB  :scared:
CubeIDE install for now ....

I "whipped up" a makefile instead ...
Btw .. It expect the "Arm GCC bin" directory to be on the path

I also included the BIN & HEX it created ... Mine is 2.4K with  -oS

Made by : gcc-arm-none-eabi-8-2018-q4-major
Code: [Select]
  LD Build/main.elf
Memory region         Used Size  Region Size  %age Used
             RAM:         336 B         4 KB      8.20%
           FLASH:        2360 B        32 KB      7.20%
             RDP:          0 GB          4 B      0.00%
  OBJCP BIN Build/main.bin
  OBJCP HEX Build/main.hex

made by :   arm-gnu-toolchain-12.3.rel1-x86_64-arm-none-eabi
Code: [Select]
  LD Build/main.elf
/home/cfo/arm/arm-gcc/arm-gnu-toolchain-12.3.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/12.3.1/../../../../arm-none-eabi/bin/ld: warning: Build/main.elf has a LOAD segment with RWX permissions
Memory region         Used Size  Region Size  %age Used
             RAM:         336 B         4 KB      8.20%
           FLASH:        2420 B        32 KB      7.39%
             RDP:          0 GB          4 B      0.00%
  OBJCP BIN Build/main.bin
  OBJCP HEX Build/main.hex


I would have expected 12 to produce less code than 8q4

Well ...

Made by: arm-gnu-toolchain-11.3.rel1-x86_64-arm-none-eabi
Code: [Select]
  LD Build/main.elf
Memory region         Used Size  Region Size  %age Used
             RAM:         336 B         4 KB      8.20%
           FLASH:        2408 B        32 KB      7.35%
             RDP:          0 GB          4 B      0.00%
  OBJCP BIN Build/main.bin
  OBJCP HEX Build/main.hex


Made by: gcc-arm-none-eabi-10-2020-q4-major
Code: [Select]
  LD Build/main.elf
Memory region         Used Size  Region Size  %age Used
             RAM:         336 B         4 KB      8.20%
           FLASH:        2336 B        32 KB      7.13%
             RDP:          0 GB          4 B      0.00%
  OBJCP BIN Build/main.bin
  OBJCP HEX Build/main.hex


Made by: gcc-arm-none-eabi-9-2019-q4-major
Code: [Select]
  LD Build/main.elf
Memory region         Used Size  Region Size  %age Used
             RAM:         336 B         4 KB      8.20%
           FLASH:        2344 B        32 KB      7.15%
             RDP:          0 GB          4 B      0.00%
  OBJCP BIN Build/main.bin
  OBJCP HEX Build/main.hex



/Bingo
« Last Edit: August 25, 2023, 06:27:27 pm by bingo600 »
 

Online DavidAlfaTopic starter

  • Super Contributor
  • ***
  • Posts: 5913
  • Country: es
Ahh 1GB.. it's 2023 not 1999!  :P
Yeah yu don't really need the IDE for anything, but you'll have to write the makefile then, I prefer it to be done automatically ;)
« Last Edit: August 25, 2023, 01:42:47 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline bingo600

  • Super Contributor
  • ***
  • Posts: 1989
  • Country: dk
Ahh 1GB.. it's 2023 not 1999!  :P
Yeah yu don't really need the IDE for anything, but you'll have to write the makefile then, I prefer it to be done automatically ;)

I even think CubeIDE can export a Makefile or ???

Btw: I'm quite sure the STM Doc said i needed 15G disk.
I might install it anyways ... Later ...

« Last Edit: August 25, 2023, 06:12:14 pm by bingo600 »
 

Online DavidAlfaTopic starter

  • Super Contributor
  • ***
  • Posts: 5913
  • Country: es
So here's the CCP test.
I attached two versions of the binary, one placing irq vectors in flash, other placing them in sram (Which should never fail).

Usage is pretty simple:
- Any falling edge at CCP pin pulses the green led for 1-2ms (Not precise as CCP input it's not synced with systick).
- If there's no CCP activity for >1 second, the green led starts pulsing at 1Hz pulse to show it's alive.
- Pressing the button will toggle RDP, blinking the red led if setting RDP1 or the green led for RDP0.
   - It'll keep blinking until you release the button + 1 second.
   - Doing RDP1->RDP0 wipes the system, needs power cycling to update the RDP security.
- The red led turns ON if RDP1 is set.

With FLASH vectors and RDP1, at some point you'll notice the system completely dies when toggling the CCP input.

For your PY32F003 chips: Use the same pin names (PA1, PA5, PB0, PB1), not the pin numbers!
« Last Edit: August 26, 2023, 01:35:20 am 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: bingo600

Offline bingo600

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

Will solder a PY32F003F18P6TU  on a breakout board today.
And might solder a PY32F002AW15S6TU on a board too (SOP-16 should be "easy")

Just for fun i added some arm-gcc compile results of the RDP program to the post above.
I am now using the arm-gcc 10 , as the default.  What version do yo use ??


This is what gcc-arm-none-eabi-10-2020-q4-major results in with the CCP program

Code: [Select]
  LD Build/main.elf
Memory region         Used Size  Region Size  %age Used
             RAM:         208 B         4 KB      5.08%
           FLASH:        2764 B      32640 B      8.47%
        SETTINGS:          0 GB        128 B      0.00%
  OBJCP BIN Build/main.bin
  OBJCP HEX Build/main.hex

/Bingo
 

Online DavidAlfaTopic starter

  • Super Contributor
  • ***
  • Posts: 5913
  • Country: es
ST's GCC version is a bit more optimized, I always had slighly lower usage with it.
The good part is you can call it externally just like any other gcc compiler.
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline bingo600

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

I have modified both examples RDP + CCP to use PA7 for BTN , and PB0 for Red / PB1 for Green.
My PY32F002AW15S6TU (SOP16) doesn't have PA5, and if you'd not commected out the "lock code" my SOP16 MCU would have been locked forever.

RDP
Code: [Select]
//Buttons
#define BTN_Port        GPIOA
  #define BTN_Pin       LL_GPIO_PIN_7

// LEDs
#define RED_Port        GPIOB
  #define RED_Pin       LL_GPIO_PIN_0
#define GREEN_Port      GPIOB
  #define GREEN_Pin     LL_GPIO_PIN_1

CCP
Code: [Select]
// CCP pin
#define   CCP_Port      GPIOA
  #define CCP_Pin       LL_GPIO_PIN_1
  #define CCP_AF        LL_GPIO_AF13_TIM1
  #define CCP_TIM       TIM1
  #define CCP_CH        LL_TIM_CHANNEL_CH4

// Buttons
#define BTN_Port        GPIOA
  #define BTN_Pin       LL_GPIO_PIN_7

// LEDs
#define RED_Port        GPIOB
  #define RED_Pin       LL_GPIO_PIN_0
#define GREEN_Port      GPIOB
  #define GREEN_Pin     LL_GPIO_PIN_1

// SWD interface
#define   SWC_Port      GPIOA
  #define SWC_Pin       LL_GPIO_PIN_14
#define   SWD_Port      GPIOA
  #define SWD_Pin       LL_GPIO_PIN_13


/Bingo
« Last Edit: August 26, 2023, 05:50:57 am by bingo600 »
 

Offline bingo600

  • Super Contributor
  • ***
  • Posts: 1989
  • Country: dk
I just tried the RDP on a PY32F002AW15S6TU (SOP-16)
Changed leds to active high.

It given an error during programming (pyOCD - DAP)

Code: [Select]
$ make flash
pyocd erase -t py32f030x6 --chip --config ./Misc/pyocd.yaml
0000872 I Erasing chip... [eraser]
0001113 I Chip erase complete [eraser]
pyocd load ./Build/main.hex -t py32f030x6 --config ./Misc/pyocd.yaml
0000874 I Loading /home/cfo/1-Arm/py32f0/git/David/PY32F030_test-cfo/Build/main.hex [load_cmd]
[==================================================] 100%
0002048 I Erased 4096 bytes (1 sector), programmed 2432 bytes (19 pages), skipped 0 bytes (0 pages) at 2.03 kB/s [loader]
0002058 E Error during board uninit: [session]

But i suppose it's ok  unint prob involve Reset , that has just been deactivated ....
Hmm RDP just sets RDP (Flas lock) ...

My "Dual led" toggles between RED & Orange (Orange = Red + Green)

When i GND PA7 , LED gets Green

Ohh ... And i have to Power reset to get swd to work again
Well ... I didn't have DAP reset connected , so the programmer couldn't reset it.

Connected DAP Reset to MCU
Code: [Select]
$ make flash
pyocd erase -t py32f030x6 --chip --config ./Misc/pyocd.yaml
0000806 I Erasing chip... [eraser]
0001051 I Chip erase complete [eraser]
pyocd load ./Build/main.hex -t py32f030x6 --config ./Misc/pyocd.yaml
0000824 I Loading /home/cfo/1-Arm/py32f0/git/David/PY32F030_test-cfo/Build/main.hex [load_cmd]
[==================================================] 100%
0001969 I Erased 4096 bytes (1 sector), programmed 2432 bytes (19 pages), skipped 0 bytes (0 pages) at 2.09 kB/s [loader]
0001975 E Error during board uninit: [session]
0001977 E Probe error during disconnect: [session]
Now 2 errors during programming

Still can't program (after PA7 Clear of RDP)
Code: [Select]
$ make flash
pyocd erase -t py32f002ax5 --chip --config ./Misc/pyocd.yaml
0000843 I Erasing chip... [eraser]
0001076 C target was not halted as expected after calling flash algorithm routine (IPSR=3) [__main__]

POR Reset works.


/Bingo
« Last Edit: August 26, 2023, 08:37:23 am by bingo600 »
 

Offline bingo600

  • Super Contributor
  • ***
  • Posts: 1989
  • Country: dk
I just tried the CCP on a PY32F002AW15S6TU (SOP-16)
Wo. any external pulse , it blinks briefly green (continuously).


That one behaves a bit strange , when PA7 is GND'ed


When i GND PA7 it blinks RED Continuously.
When i remove PA7 (GND) , it lights mostly RED , short pulses of green (i think)

I can't erase ....

If i connect PA7 again , it blinks green , when i remove PA7 , it lights steady green.
I can erase program chip.
Seems like it needs to times PA7 to GND
Yepp ... 2 x PA7 GND works (permanent Green) , and unlocks.
I can erase/reflash.


Program CCP - POR Reset (No external pulse)  (flow)
Green constantly blinks briefly.
PA7 GND , Red blinks , then becomes "almost steady" , short green blinks alco i think
Another PA7 GND , it blinks Green , and then steady Green.

After POR i can reflash.

Edit: Remember i have nothing connected to CCP pin .... Maybe it sees some noise (the brief green pulses)

/Bingo
« Last Edit: August 26, 2023, 09:11:47 am by bingo600 »
 

Offline bingo600

  • Super Contributor
  • ***
  • Posts: 1989
  • Country: dk
Now to whip up a 9KHz signal ..... Using my other py32F002 (TSSOP20) board.

I'll have to do a bit of DS studying ....
 

Online DavidAlfaTopic starter

  • Super Contributor
  • ***
  • Posts: 5913
  • Country: es
100Hz is enough! Or a simple push button, it'll crash really easily.

It's not behaving strangely, that's how it's supposed to work.

Activate the button (PA7), sets rdp1, reboots and turns the red led on.
Now you can't erase as it seems the device pack programming algorithm is bugged.
This didn't happen in the HKL32F030M, I clearly got a warning about it, advicing to erase the device.

Activate the button again and rdp1 is cleared, wiping the system and enabling the green led.

Same about the green led behaviour.
Have you read anything where I posted the test? ;)

About PyOCD, I can't help. I tried using it but cmsis pack didn't work nor it was found anywhere, though it was clearly installed.


Attached same fw with button in PA7, both flash and sram vectors versions.
« Last Edit: August 26, 2023, 01:19:19 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Online DavidAlfaTopic starter

  • Super Contributor
  • ***
  • Posts: 5913
  • Country: es
Gotta love chinese stuff.
I disassembled the FLM in ghidra, look at the blank checking function!  :-DD
- Is it blank?
- Looks at the sky while puffing on a cigar - Sure bro!
« Last Edit: August 26, 2023, 01:42:55 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline bingo600

  • Super Contributor
  • ***
  • Posts: 1989
  • Country: dk
I did do a "quick read" of your description of the CCP test program.
But apparently not close enough.

What i described was my experience , in no way any critisism.

Re: poOCD ... You need to put the pyocd yaml in the "main directory" , or use the --pack to point at the pack
The pack is in IOsettings Misc dir , so is the yaml file

Use 'pyocd list --targets' to see available targets.

Code: [Select]
pyocd list --targets --pack Misc/Puya.PY32F0xx_DFP.1.1.3.pack | grep -i py32
  py32f002ax5               Puya                     PY32F002Ax5                  PY32F0 Series, PY32F002     pack     
  py32f002x5                Puya                     PY32F002x5                   PY32F0 Series, PY32F002     pack     
  py32f003x4                Puya                     PY32F003x4                   PY32F0 Series, PY32F003     pack     
  py32f003x6                Puya                     PY32F003x6                   PY32F0 Series, PY32F003     pack     
  py32f003x8                Puya                     PY32F003x8                   PY32F0 Series, PY32F003     pack     
  py32f030x3                Puya                     PY32F030x3                   PY32F0 Series, PY32F030     pack     
  py32f030x4                Puya                     PY32F030x4                   PY32F0 Series, PY32F030     pack     
  py32f030x6                Puya                     PY32F030x6                   PY32F0 Series, PY32F030     pack     
  py32f030x7                Puya                     PY32F030x7                   PY32F0 Series, PY32F030     pack     
  py32f030x8                Puya                     PY32F030x8                   PY32F0 Series, PY32F030     pack     
  py32f072xb                Puya                     PY32F072xB                   PY32F0 Series, PY32F072     pack   


Still working on modifying your CCP program from input capture to output compare w. pin toggle - 9KHz  - Thats my learning task for now .... As mentioned i'm quite green with ARM ie. STM/PY.

I have never used AF pins before , and have quite some issue figuring out why you did select : LL_GPIO_AF_13
And not ie. LL_GPIO_AF_1.

Is the AF just a "Pin rerouting Matrix" , where you could have chosen any that has tim1 in the name ??
#define LL_GPIO_AF2_TIM1          LL_GPIO_AF_2   /*!< TIM1 Alternate Function mapping */

I don't understand the logic , and the PY RM has a Matric that doesn't help (me) much.

Re: Black check
I suppose FLM = Flash loader module.
Is that in the chip or some of the bin (elf) used for Jlink or ?

/Bingo
« Last Edit: August 26, 2023, 02:35:19 pm by bingo600 »
 

Online DavidAlfaTopic starter

  • Super Contributor
  • ***
  • Posts: 5913
  • Country: es
The AF pins are better described in the Datasheet.
You have a simple table showing how each AF source connects the physical pins to the different peripherals.


AF13 connects PA1 to TIM1_CH4.
AF1 would connect it to USART1_CTS!

LL_GPIO_AF13  and LL_GPIO_AF_13_TIM1 is the same thing, it's just a way for you to easily see wat are you interfacing to.
Does LL_GPIO_AF2 say anything to you? Not to me! I'd need to open the datasheet and look it out.
LL_GPIO_AF_13_TIM1 makes it a bit easier. I would even have used LL_GPIO_AF_13_TIM1_CH4.

« Last Edit: August 26, 2023, 03:37:36 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: bingo600

Offline bingo600

  • Super Contributor
  • ***
  • Posts: 1989
  • Country: dk
Thanx for the hint with the AF in the DS.
Btw do you exclusively look at the F030 documentation for the F002 ?

Re DAPLINK
I use an Air32F103 with both the (top/bottom) 5 Pins soldered at the end.   ... Think you have some of them
Use V1 hexfile ... V2 doesn't work
https://www.eevblog.com/forum/microcontrollers/air105-air32f103-new-chinese-arm-chips/msg5014411/#msg5014411


Quote
**** DON'T use the "silkscreened swd/swclk" , that you use to program the board... Won't work

Pinout as a DAPLink
PB13: SWCLK
PB14: SWDIO
PB0: RESET
PA2: TX
PA3: RX

/Bingo
« Last Edit: August 26, 2023, 03:02:36 pm by bingo600 »
 

Online DavidAlfaTopic starter

  • Super Contributor
  • ***
  • Posts: 5913
  • Country: es
Yeah I normally use the 030 docs. The 002 and 003 docs have the same thing, just cutting down some features.
I do check the "official" specs in case something doesn't work, like in this RDP problem, I removed all non officlal 002A features to make sure it wasn't my fault for using them, but surprise, no change! :D
« Last Edit: August 26, 2023, 03:38:58 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline bingo600

  • Super Contributor
  • ***
  • Posts: 1989
  • Country: dk
I got something kludged together to generate ~9KHz pulses on PA1
Used your CCP as base

Code: [Select]
/*--------------------------------------------------*/
void system_init(void)
{
  LL_RCC_HSI_SetCalibFreq(LL_RCC_HSICALIBRATION_24MHz);
  while (LL_RCC_HSI_IsReady() != 1);

  LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA | LL_IOP_GRP1_PERIPH_GPIOB | LL_IOP_GRP1_PERIPH_GPIOF);
  LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_TIM1);

  // LL_GPIO_Init(SWC_Port,   &(LL_GPIO_InitTypeDef){ SWC_Pin,  LL_GPIO_MODE_ANALOG });         // Disable SWD interface access
  // LL_GPIO_Init( SWD_Port,  &(LL_GPIO_InitTypeDef){ SWD_Pin,  LL_GPIO_MODE_ANALOG });

  LL_GPIO_Init(CCP_Port,   &(LL_GPIO_InitTypeDef){ CCP_Pin,  LL_GPIO_MODE_ALTERNATE, LL_GPIO_SPEED_FREQ_VERY_HIGH, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_NO, CCP_AF });
  LL_GPIO_Init(RED_Port,   &(LL_GPIO_InitTypeDef){ RED_Pin, LL_GPIO_MODE_OUTPUT, LL_GPIO_SPEED_FREQ_VERY_HIGH, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_NO });
  LL_GPIO_Init(GREEN_Port, &(LL_GPIO_InitTypeDef){ GREEN_Pin, LL_GPIO_MODE_OUTPUT, LL_GPIO_SPEED_FREQ_VERY_HIGH, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_NO });
  LL_GPIO_Init(BTN_Port,   &(LL_GPIO_InitTypeDef){ BTN_Pin, LL_GPIO_MODE_INPUT, 0, 0, LL_GPIO_PULL_UP });


//https://community.st.com/t5/stm32-mcu-products/setting-the-timer-tim1-in-stm32f407/td-p/395256

  //LL_TIM_Init(CCP_TIM, &(LL_TIM_InitTypeDef){ 23, LL_TIM_COUNTERDIRECTION_UP, 65535, LL_TIM_CLOCKDIVISION_DIV1, 0});                              // 1MHz clock, 65ms overflow
  LL_TIM_Init(CCP_TIM, &(LL_TIM_InitTypeDef){ 11, LL_TIM_COUNTERDIRECTION_UP, 111, LL_TIM_CLOCKDIVISION_DIV1, 0});                              // 1MHz clock, 65ms overflow
  //LL_TIM_IC_Init(CCP_TIM, CCP_CH, &(LL_TIM_IC_InitTypeDef){ LL_TIM_IC_POLARITY_FALLING, LL_TIM_ACTIVEINPUT_DIRECTTI, LL_TIM_ICPSC_DIV1, 4 });      // Falling edge
  LL_TIM_OC_Init(CCP_TIM, CCP_CH, &(LL_TIM_OC_InitTypeDef){ LL_TIM_OCMODE_TOGGLE, LL_TIM_OCSTATE_ENABLE, LL_TIM_OCSTATE_ENABLE, 111,
                                                            LL_TIM_OCPOLARITY_HIGH, LL_TIM_OCPOLARITY_HIGH, LL_TIM_OCIDLESTATE_LOW, LL_TIM_OCIDLESTATE_LOW });      // Falling edge

  // Generates 8.97 KHz pulses

  LL_TIM_EnableCounter(CCP_TIM);
  LL_TIM_CC_EnableChannel(CCP_TIM, CCP_CH);
  LL_TIM_EnableAllOutputs(CCP_TIM);

  NVIC_SetPriority(TIM1_CC_IRQn, 1);
  //NVIC_EnableIRQ(TIM1_CC_IRQn);

  SysTick_Config(24000);      // 24MHz cpu clock, 1ms period.
}


But it seems like it's the below LL_TIM_Init that controls the Pulse duration

24M/12/111 ~18KHz ==> 9KHz Toggled
Code: [Select]
  LL_TIM_Init(CCP_TIM, &(LL_TIM_InitTypeDef){ 11, LL_TIM_COUNTERDIRECTION_UP, 111, LL_TIM_CLOCKDIVISION_DIV1, 0});                              // 1MHz clock, 65ms overflow

Whar does the 111 in this one (LL_TIM_OC_Init) do then ?
Code: [Select]
LL_TIM_OC_Init(CCP_TIM, CCP_CH, &(LL_TIM_OC_InitTypeDef){ LL_TIM_OCMODE_TOGGLE, LL_TIM_OCSTATE_ENABLE, LL_TIM_OCSTATE_ENABLE, 111,
                                                            LL_TIM_OCPOLARITY_HIGH, LL_TIM_OCPOLARITY_HIGH, LL_TIM_OCIDLESTATE_LOW, LL_TIM_OCIDLESTATE_LOW });      // Falling edge

I would have expected that one :
Code: [Select]
  uint32_t CompareValue;  /*!< Specifies the Compare value to be loaded into the Capture Compare Register.
                               This parameter can be a number between Min_Data=0x0000 and Max_Data=0xFFFF.

To control the "compare" ...


Will try tomorrow, with the 9KHz from my F002 dev board as pulse source.

Ps:
Thank you for your examples & guidance

/Bingo
« Last Edit: August 26, 2023, 06:04:41 pm by bingo600 »
 

Online DavidAlfaTopic starter

  • Super Contributor
  • ***
  • Posts: 5913
  • Country: es
What are you taking about? CCP doesn't generate pulses, it captures edges and triggers an interrupt.
You're using in your own way (OCP) for no reason, beyond the scope of this issue.
Please don't do that. If we're talking about apples don't come saying you got oranges ;)
OCP is triggered internally, won't cause any issue. The issue with CCP seems caused by external, unsynced signal.

That's not what I wrote! First 11 is the prescaler,  timer freq. is 24MHz/(prescaler+1). The second is the overflow value, so the timer resets at  timer freq / (overflow+1).

Code: [Select]
  LL_TIM_Init(CCP_TIM, &(LL_TIM_InitTypeDef){ 11, LL_TIM_COUNTERDIRECTION_UP, 111, LL_TIM_CLOCKDIVISION_DIV1, 0});                              // 1MHz clock, 65ms overflow

In that example you made: 24MHz/(11+1) = 2MHz clock, timer overflows at 2MHz/(111+1) = 17.857KHz.


Whar does the 111 in this one (LL_TIM_OC_Init) do then ?
Code: [Select]
LL_TIM_OC_Init(CCP_TIM, CCP_CH, &(LL_TIM_OC_InitTypeDef){ LL_TIM_OCMODE_TOGGLE, LL_TIM_OCSTATE_ENABLE, LL_TIM_OCSTATE_ENABLE, 111,
                                                            LL_TIM_OCPOLARITY_HIGH, LL_TIM_OCPOLARITY_HIGH, LL_TIM_OCIDLESTATE_LOW, LL_TIM_OCIDLESTATE_LOW });      // Falling edge

It's the CompareValue:
Quote
typedef struct
{
  uint32_t OCMode;        /*!< Specifies the output mode.
                               This parameter can be a value of @ref TIM_LL_EC_OCMODE.

                               This feature can be modified afterwards using unitary function @ref LL_TIM_OC_SetMode().*/

  uint32_t OCState;       /*!< Specifies the TIM Output Compare state.
                               This parameter can be a value of @ref TIM_LL_EC_OCSTATE.

                               This feature can be modified afterwards using unitary functions @ref LL_TIM_CC_EnableChannel() or @ref LL_TIM_CC_DisableChannel().*/

  uint32_t OCNState;      /*!< Specifies the TIM complementary Output Compare state.
                               This parameter can be a value of @ref TIM_LL_EC_OCSTATE.

                               This feature can be modified afterwards using unitary functions @ref LL_TIM_CC_EnableChannel() or @ref LL_TIM_CC_DisableChannel().*/

  uint32_t CompareValue;  /*!< Specifies the Compare value to be loaded into the Capture Compare Register.
                               This parameter can be a number between Min_Data=0x0000 and Max_Data=0xFFFF.

                               This feature can be modified afterwards using unitary function LL_TIM_OC_SetCompareCHx (x=1..6).*/

  uint32_t OCPolarity;    /*!< Specifies the output polarity.
                               This parameter can be a value of @ref TIM_LL_EC_OCPOLARITY.

                               This feature can be modified afterwards using unitary function @ref LL_TIM_OC_SetPolarity().*/

  uint32_t OCNPolarity;   /*!< Specifies the complementary output polarity.
                               This parameter can be a value of @ref TIM_LL_EC_OCPOLARITY.

                               This feature can be modified afterwards using unitary function @ref LL_TIM_OC_SetPolarity().*/


  uint32_t OCIdleState;   /*!< Specifies the TIM Output Compare pin state during Idle state.
                               This parameter can be a value of @ref TIM_LL_EC_OCIDLESTATE.

                               This feature can be modified afterwards using unitary function @ref LL_TIM_OC_SetIdleState().*/

  uint32_t OCNIdleState;  /*!< Specifies the TIM Output Compare pin state during Idle state.
                               This parameter can be a value of @ref TIM_LL_EC_OCIDLESTATE.

                               This feature can be modified afterwards using unitary function @ref LL_TIM_OC_SetIdleState().*/
} LL_TIM_OC_InitTypeDef;

What are you tring to do? Why the fixation of generating 9KHz for no reason? :D

100Hz is enough! Or a simple push button, it'll crash really easily.
« Last Edit: August 26, 2023, 06:40:11 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline bingo600

  • Super Contributor
  • ***
  • Posts: 1989
  • Country: dk
I'm talking about my need for generate pulses for the capture port on the  PY32 running your CCP program.
So i modified your CCP program a bit to do OCP instead of ICP, for the PY32 i use as pulse generator.

I'm not going to modify your CCP program on the DUT, just use it as base for a pulse generator for pulsing the DUT.

 

Online DavidAlfaTopic starter

  • Super Contributor
  • ***
  • Posts: 5913
  • Country: es
Just use a simple push button LOL, I will repeat again, no need for any KHz signal.
Connect a button, press it few times, it will crash after few presses if RDP is set.
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline bingo600

  • Super Contributor
  • ***
  • Posts: 1989
  • Country: dk
I'm not sure where i got the 9KHz from  :-X  :-//
But as i'm also using this as a PY32/STM + pyOCD/DAP-Programmer learningproject, i wanted to try OCP as pulse source.

I'll modify the pulse source to 200Hz
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf