PY32F0 Template Project

repository·main·Indexed 43 days ago

https://github.com/iosetting/py32f0-template

A template project for developing applications on the Puya PY32F0 family of Arm Cortex-M0+ microcontrollers. It provides a complete build environment including HAL/LL libraries, FreeRTOS support, and debugging configurations for VSCode.

Tokens
5.7K
Snippets
17
Records
24
Agent score
55%

What's inside PY32F0 Template

  1. Understand the ComputeLibrary modifications in CMSIS-DSP

    main

    The Libraries/CMSIS/DSP/ComputeLibrary directory contains a modified version of the original ARM ComputeLibrary, adapted for CMSIS-DSP. The primary changes involve the file structure and language used to support C-based CMSIS-DSP environments instead of the original C++ implementation.

    Key differences to note:

    • File Consolidation: The original NEMath.inl (included by NEMath.h) has been removed. Its content is now directly integrated into NEMath.h.
    • Table Storage: The lookup tables originally found in NEMath.inl have been moved to a dedicated C file: arm_cl_tables.c.
    • Language: While the original ComputeLibrary uses C++, these files are implemented in C to be compatible with the CMSIS-DSP library.
    • Functionality: The features and optimized Neon function implementations remain identical to the original library.
    • Licensing: These specific files are licensed under the MIT license, whereas other parts of CMSIS-DSP typically use the Apache-2.0 license.
  2. Understand the FreeRTOS Kernel repository structure

    main

    The repository is organized into three main sections:

    • Kernel Core: The root directory contains list.c, queue.c, and tasks.c. These three files constitute the core kernel. croutine.c is also located here and provides optional co-routine functionality for memory-constrained systems.
    • Portable Layer: The ./portable directory contains files specific to particular microcontrollers and/or compilers. Refer to the README within the ./portable directory for specific details.
    • Headers: The ./include directory contains the real-time kernel header files required for API usage.
  3. Project File Structure Overview

    main

    Understanding the repository layout is essential for finding examples and managing user code:

    • Build/: Contains build artifacts.
    • Docs/: Datasheets and User Manuals.
    • Examples/: Subdivided by MCU family (PY32F002B, PY32F07x, PY32F0xx) and library type (HAL, LL, FreeRTOS).
    • Libraries/: Core libraries including CMSIS, FreeRTOS, and device-specific HAL/LL drivers and BSPs.
    • Misc/: Contains flash loaders (JLinkDevices), PyOCD packs, and SVD files for debugging.
    • User/: This is where your application code lives. To use an example, copy its files into this directory.
    • Makefile & rules.mk: Build configuration files.
  4. Configure VSCode for Cortex Debugging

    main

    To debug your PY32F0 project in VSCode, install the Cortex Debug extension and create a configuration in launch.json. You must specify the armToolchainPath, the device name, the servertype (e.g., jlink or pyocd), and the svdFile path for peripheral debugging.

    {
        "armToolchainPath": "/opt/gcc-arm/arm-gnu-toolchain-12.2.rel1-x86_64-arm-none-eabi/bin/",
        "toolchainPrefix": "arm-none-eabi",
        "name": "Cortex Debug",
        "cwd": "${workspaceFolder}",
        "executable": "${workspaceFolder}/Build/app.elf",
        "request": "launch",
        "type": "cortex-debug",
        "runToEntryPoint": "Reset_Handler",
        "servertype": "jlink",
        "device": "PY32F030X8",
        "interface": "swd",
        "preLaunchTask": "build",
        "svdFile": "${workspaceFolder}/Misc/SVD/py32f030xx.svd",
        "showDevDebugOutput": "vscode",
        "swoConfig": {
            "enabled": true,
            "cpuFrequency": 8000000,
            "swoFrequency": 4000000,
            "source": "probe",
            "decoders": [
                {
                    "label": "ITM port 0 output",
                    "type": "console",
                    "port": 0,
                    "showOnStartup": true,
                    "encoding": "ascii"
                }
            ]
        }
    }
  5. Install the GNU Arm Embedded Toolchain

    main

    To compile code for the PY32F0 MCU, you must install the GNU Arm Embedded Toolchain. Download the appropriate version for your architecture from the Arm GNU Toolchain Downloads page and extract it to /opt/gcc-arm/.

    sudo mkdir -p /opt/gcc-arm/
    sudo tar xvf arm-gnu-toolchain-12.2.rel1-x86_64-arm-none-eabi.tar.xz -C /opt/gcc-arm/
    cd /opt/gcc-arm/
    sudo chown -R root:root arm-gnu-toolchain-12.2.rel1-x86_64-arm-none-eabi/
  6. Configure Makefile for PY32F003W16S6TU SOP16

    main

    To run the Si5351A I2C example on the PY32F003W16S6TU SOP16 chip, you must modify the Makefile to update the target device, the linker script, and the assembly startup file.

    Required changes:

    1. Set PYOCD_DEVICE to py32f003x6.
    2. Update LDSCRIPT to use Libraries/LDScripts/py32f003x6.ld.
    3. Update AFILES to use Libraries/CMSIS/Device/PY32F0xx/Source/gcc/startup_py32f003.s.
    --- a/Makefile
    +++ b/Makefile
    @@ -38,13 +38,13 @@
    -PYOCD_DEVICE   ?= py32f030x8
    +PYOCD_DEVICE   ?= py32f003x6
     
     ##### Paths ############
     
     # Link descript file: py32f002x5.ld, py32f003x6.ld, py32f003x8.ld, py32f030x6.ld, py32f030x8.ld
    -LDSCRIPT               = Libraries/LDScripts/py32f030x8.ld
    +LDSCRIPT               = Libraries/LDScripts/py32f003x6.ld
     # Library build flags:
     #   PY32F002x5, PY32F002Ax5,
     #   PY32F003x4, PY32F003x6, PY32F003x8,
    @@ -61,7 +61,7 @@ CFILES :=
     # ASM source folders
     ADIRS  := User
     # ASM single files
    -AFILES := Libraries/CMSIS/Device/PY32F0xx/Source/gcc/startup_py32f030.s
    +AFILES := Libraries/CMSIS/Device/PY32F0xx/Source/gcc/startup_py32f003.s
     
     # Include paths
     INCLUDES       := Libraries/CMSIS/Core/Include \
  7. Install SEGGER J-Link Programmer

    main

    If using a J-Link programmer, download and install the software from SEGGER. After installation, you must copy the project's specific J-Link device support files to your user configuration directory to ensure the programmer recognizes the PY32F0 chips.

    Note: The default installation directory is /opt/SEGGER.

    # installation command for .deb
    sudo dpkg -i JLink_Linux_V784f_x86_64.deb
    
    # Copy JLinkDevices to your config folder
    cd py32f0-template
    cp -r Misc/Flash/JLinkDevices/ ~/.config/SEGGER/
  8. Enable printf-based debugging

    main

    To enable printf debugging via USART, patch the Libraries/BSP/Inc/py32f0xx_bsp_printf.h file to map the debug USART TX to the correct GPIO pin and Alternate Function (AF) for your hardware configuration. For the target chip in this example, change the pin to GPIO_PIN_7 and the AF to GPIO_AF8_USART1.

    --- a/Libraries/BSP/Inc/py32f0xx_bsp_printf.h
    +++ b/Libraries/BSP/Inc/py32f0xx_bsp_printf.h
    @@ -48,8 +48,8 @@ extern "C" {
     
     #define DEBUG_USART_TX_GPIO_PORT                GPIOA
     #define DEBUG_USART_TX_GPIO_CLK_ENABLE()        __HAL_RCC_GPIOA_CLK_ENABLE()
    -#define DEBUG_USART_TX_PIN                      GPIO_PIN_2
    -#define DEBUG_USART_TX_AF                       GPIO_AF1_USART1
    +#define DEBUG_USART_TX_PIN                      GPIO_PIN_7
    +#define DEBUG_USART_TX_AF                       GPIO_AF8_USART1
  9. Configure PAN2110 Wireless Recommended Frequencies

    main

    When implementing the PAN2110 Wireless example using the PY32F002B, you must select appropriate radio frequencies based on your hardware's crystal oscillator (XTAL) configuration. Using the correct frequency list ensures optimal wireless performance.

    Refer to the Panchip BLE-Lite documentation for detailed technical specifications: https://wiki.panchip.com/ble-lite/2-4g-t-rx/

    ## 32MHz XTAL
    2402, 2403, 2405, 2406,
    2410, 2411, 2413, 2414,
    2418, 2419, 2421, 2422,
    2426, 2427, 2429, 2430,
    2434, 2435, 2437, 2438,
    2442, 2443, 2445, 2446,
    2450, 2451, 2453, 2454,
    2458, 2459, 2461, 2462,
    2466, 2467, 2469, 2470,
    2474, 2475, 2477, 2478,
    2482, 2483
    
    ## 16MHz XTAL
    2401, 2402, 2403, 2405, 2406, 2407,
    2409, 2410, 2411, 2413, 2414, 2415,
    2417, 2418, 2419, 2421, 2422, 2423,
    2425, 2426, 2427, 2429, 2430, 2431,
    2433, 2434, 2435, 2437, 2438, 2438,
    2441, 2442, 2443, 2445, 2446, 2447,
    2449, 2450, 2451, 2453, 2454, 2455,
    2457, 2458, 2459, 2461, 2462, 2463,
    2465, 2466, 2467, 2469, 2470, 2471,
    2473, 2474, 2475, 2477, 2478, 2479
    2481, 2482, 2483
  10. Configure Makefile for PCF8574 I2C LCD Example

    main

    To run the PCF8574 I2C LCD example on specific PY32F0xx hardware, you must modify the Makefile to match your target device's PyOCD identifier, linker script, and startup assembly file.

    Specifically, for a py32f003x6 target, update the following variables:

    • PYOCD_DEVICE: Set to your specific device identifier (e.g., py32f003x6).
    • LDSCRIPT: Point to the correct linker script in Libraries/LDScripts/ (e.g., py32f003x6.ld).
    • AFILES: Update the path to the correct CMSIS startup assembly file (e.g., Libraries/CMSIS/Device/PY32F0xx/Source/gcc/startup_py32f003.s).
    -PYOCD_DEVICE   ?= py32f030x8
    +PYOCD_DEVICE   ?= py32f003x6
    
    -LDSCRIPT               = Libraries/LDScripts/py32f030x8.ld
    +LDSCRIPT               = Libraries/LDScripts/py32f003x6.ld
    
    -AFILES := Libraries/CMSIS/Device/PY32F0xx/Source/gcc/startup_py32f030.s
    +AFILES := Libraries/CMSIS/Device/PY32F0xx/Source/gcc/startup_py32f003.s