| 1 | --- |
| 2 | title: Adding a new built-in target |
| 3 | --- |
| 4 | |
| 5 | This guide describes how to manually add support for a new target and/or board to pyOCD. In most |
| 6 | cases you do not need to add a builtin target anymore, and can use pyOCD's support for CMSIS |
| 7 | Device Family Packs. |
| 8 | |
| 9 | For background information, review the [architecture overview]({% link _docs/architecture.md %}) document first. The |
| 10 | [CMSIS Pack documentation](https://arm-software.github.io/CMSIS_5/Pack/html/index.html) may also be helpful. |
| 11 | |
| 12 | |
| 13 | ### Device Family Pack intro |
| 14 | |
| 15 | The instructions below assume you have a CMSIS Device Family Pack (DFP) available for your target. See the |
| 16 | [list of all publicly available Packs](https://www.keil.com/dd2/pack/) to find and download the DFP for your |
| 17 | target. |
| 18 | |
| 19 | A DFP is simply a zip file with a .pack extension. To extract the contents you can change the extension to |
| 20 | .zip and extract with your favourite archive utility. |
| 21 | |
| 22 | For this context, the most important thing inside the DFP are .FLM files that contain the flash programming |
| 23 | algorithms used in step 5 below. The .pdsc file can also be useful. It is an XML file that contains details |
| 24 | such as the memory map for the target devices described by the DFP. |
| 25 | |
| 26 | |
| 27 | ### Steps to add a new target |
| 28 | |
| 29 | 1. Create a new `CoreSightTarget` subclass in a file under `pyocd/target/builtin/`. You can copy one of the |
| 30 | existing target files like `pyocd/target/builtin/target_ncs36510.py` and rename the class. |
| 31 | |
| 32 | The target source file name must follow the pattern "target\_\<device>.py", where "\<device>" is the |
| 33 | device's `Dname` or `Dvariant` part number value from the appropriate CMSIS Device Family Pack (DFP). For |
| 34 | instance, `target_LPC54608J512ET180.py`. You may substitute an "x" for certain fields in the part number, |
| 35 | such as a package or pin count code, temperature code, or memory size (if multiple memory sizes are |
| 36 | supported via classes within the one source file). For instance, `target_STM32F412xx.py`. If the device |
| 37 | doesn't have a DFP, then use a similar, complete part number. |
| 38 | |
| 39 | 2. Set the `VENDOR` class attribute on the `CoreSightTarget` subclass. The vendor name must be one |
| 40 | of the standard values defined by the |
| 41 | [`DeviceVendorEnum`](http://arm-software.github.io/CMSIS_5/Pack/html/pdsc_family_pg.html#DeviceVendorEnum) |
| 42 | type for CMSIS Packs. (If the vendor is not listed, please contact Arm.) |
| 43 | |
| 44 | 3. You may optionally add `PART_NUMBER` and/or `PART_FAMILIES` class attributes to your target class. T |
| 45 | |
| 46 | 4. Create the target's memory map from information in the device's reference manual. The memory map |
| 47 | should be a `MEMORY_MAP` class attribute of the target class. Modifying an existing memory map is |
| 48 | easiest, and there are many examples in the other targets. |
| 49 | |
| 50 | 5. To create the flash algo, the recommended method is to use the |
| 51 | [`scripts/generate_flash_algo_.py`](https://github.com/pyocd/pyocd/scripts/generate_flash_algo_.py) script |
| 52 | included in the pyocd repo. This script will generate an output file in the form required for pyocd from |
| 53 | an .FLM file that is included as part of a CMSIS DFP. |
| 54 | |
| 55 | 1. Locate the correct .FLM file from the DFP for your target. |
| 56 | |
| 57 | 2. Run `scripts/generate_flash_algo_.py \<path to FLM>`. It will write the output files to the working directory |
| 58 | from where you called the script. |
| 59 | |
| 60 | 3. The `pyocd_blob.py` output file contains the Python code for the flash algo. Copy the `FLASH_ALGO_*` |
| 61 | dictionary (the name will have a suffix based on the FLM name) into the target source file. |
| 62 | |
| 63 | 4. Review the addresses in the `flash_algo` dictionary to make sure they are valid. The memory |
| 64 | layout should look like: |
| 65 | |
| 66 | |----------------|-------------|------------|-----|-----------------|-----------------| |
| 67 | | load_address | static_base | << (stack) | ... | page_buffers[0] | page_buffers[1] | |
| 68 | |----------------|-------------|------------|-----|-----------------|-----------------| |
| 69 | ^ ^ |
| 70 | RAM start begin_stack (grows down) |
| 71 | |
| 72 | Each of the addresses in the `page_buffers` list points to a buffer of the maximum page |
| 73 | size of any flash region. If there is not enough RAM to hold two page buffers, then remove |
| 74 | one of the addresses from the list. This will disable double buffered flash programming. |
| 75 | |
| 76 | 5. To enable efficient scanning for modified pages via CRC checking, you can set the |
| 77 | `analyzer_supported` key to True and the `analyzer_address` to the start address for an |
| 78 | unused range of (1224 + 4 * number-of-flash-pages) bytes of RAM. |
| 79 | |
| 80 | 6. Pass the `FLASH_ALGO_*` dict for the `algo` parameter to the `FlashRegion` constructor in |
| 81 | your memory map. This binds the flash algo to that flash memory region. |
| 82 | |
| 83 | 6. If the target has multiple flash algos for different flash types, repeat step 5 as necessary. |
| 84 | |
| 85 | 7. Edit `pyocd/target/builtin/__init__.py` to import your target source file and add your new target |
| 86 | to the `BUILTIN_TARGETS` dict. |
| 87 | |
| 88 | 8. You or your employer own the copyright on the new code, so make sure you set the copyright on the new target file. |
| 89 | You should also add a copyright to any existing files you modified. |
| 90 | |
| 91 | Now your new target is available for use via the `--target` command line option! You can test its availability |
| 92 | by running `pyocd list --targets --name <target-name>`. |
| 93 | |
| 94 | |
| 95 | ### Steps to add a new board |
| 96 | |
| 97 | This section only applies if your board has an on-board debug probe that either: |
| 98 | |
| 99 | - Uses the [Arm DAPLink](https://github.com/ARMmbed/DAPLink) firmware. DAPLink presents the board ID |
| 100 | as the first 4 characters of the USB serial number. |
| 101 | - Uses the STLinkV2-1 or STLinkV3 firmware and the board is Mbed-enabled. STLink presents the board ID |
| 102 | as the first 4 characters of the code in the HTML file on the USB mass storage volume. |
| 103 | |
| 104 | If neither applies, then pyOCD will be unable to automatically detect the board type. However, you |
| 105 | can still use the target by passing the `--target` argument to pyOCD. |
| 106 | |
| 107 | Follow these steps: |
| 108 | |
| 109 | 1. Identify the 4-character board ID. |
| 110 | |
| 111 | 2. Insert a row into the `BOARD_ID_TO_INFO` table in `pyocd/board/board_ids.py` with the board ID, |
| 112 | board name, target type, and test binary file name. |
| 113 | |
| 114 | The new row should look similar to this: |
| 115 | |
| 116 | "0205": BoardInfo( "FRDM-KL28Z", "kl28z", "l1_kl28z.bin", ), |
| 117 | |
| 118 | Be sure to insert the row in sorted order by board ID, and please align columns. |
| 119 | |
| 120 | 3. Place a test firmware binary file listed in the board info into the `test/data/binaries/` |
| 121 | directory. The test firmware can be nothing more than a tiny LED blinky demo. It must not require |
| 122 | any user input, and should provide immediate visual feedback that the code is successfully |
| 123 | running, assuming there are LEDs on the board. |