4.1. Deploy to new platform in ten minutes

4.1.1. How to choose a platform that can run pikascript

  • PikaPython can run on all bare metal and operating systems that support libc.

  • The compiler needs to be able to support the C99 standard.

  • Supports 32bit/64bit kernel, does not support 8bit kernel.

  • resource occupancy

    • If it is an arm kernel, considering the resource consumption of the expansion module, it should be equipped with a minimum of 64k flash and 8k ram.

    • If it is a risc-v kernel, you need 128k flash and 8k ram , because of the gcc optimization of the risc-v kernel and the problem of code density, the code size is much larger than that of the arm kernel.

    • If it is other kernel, you can refer to the configuration requirements of risc-v.

  • If it is a PC/server platform, linux/windows can be used.

4.1.2. Deployment operation process

In this document, we will describe how to deploy PikaPython for new platforms.

PikaPython has almost no global variables and macros, and only depends on the standard library, so it is very easy to deploy PikaPython for new platforms.

Here are the specific steps to deploy PikaPython

4.1.2.1. Prepare template project

Your template project just needs to include a serial port initialization that supports printf, and then you can happily use pikascript.

The usual script interpreters rely on the operating system and file system, and pikascript does not need these, if you have deployed other scripting engines, you will find that PikaScrip has real super Lightweight features.

4.1.2.2. Get PikaPython source code and toolset

To get PikaScsript, you can use the pika package manager (option 1), or use the project generator on the official website (option 2).

Option 2 is an automated version of Option 1. It is recommended that newbies use Option 1 when deploying for the first time to familiarize themselves with the package manager.

4.1.2.2.1. Download PikaPython Package Manager

PikaPython package manager can help you pull all source code and tools needed by pikascript, and provide version selection function, which is convenient for you to switch versions.

And the PikaPython package manager uses gitee source, which can be used smoothly in the mainland, does not require scientific Internet access.

Enter the PikaPython main repository

https://github.com/pikastech/pikascript

or:

https://gitee.com/lyon1998/pikascript

Download the PikaPython package manager PikaPackage.exe

https://user-images.githubusercontent.com/88232613/171088180-661d9b90-f6f9-4e75-9927-6e5d4917e8ea.png

Then open the project you want to deploy, create a new pikascript folder in the root directory of the project, and copy PikaPackage.exe into it.

_images/142019778-c727336c-9b6a-4a82-b813-1671f4120152.png

4.1.2.2.2. Pull source code

Next, with the help of PikaPackage.exe, we can easily pull the source code and modules of the specified version.

Pull the source code and modules through a requestment.txt file.

If you are familiar with python’s pip package manager, you will find that the requestment.txt file format of pikascript is the same as that of pip.

Create a new requestment.txt file in the pikascript folder of the project, and write the following content.

pikascript-core
PikaStdLib

The requestment.txt file indicates the installation of the pikascript-core interpreter kernel and the PikaStdLib standard library. The interpreter kernel and the standard library are mandatory, while the other modules can be added optionally, and only the kernel and the standard library should be added during the initial deployment to avoid compatibility issues.

You can copy the requestment.txt kernel and standard library version of stm32g070, which is an officially supported development board The version used by Pika Pie-Zero.

And all optional versions can be viewed in the packages.toml file.

The lts2021 version refers to the long-term support version released at the end of 2021, and the support period is within 2022.

The usual version number is v1.x.x, the lts2021 version is based on v1.3.5 with stability patches.

The pikascript folder now has two files, pikaPackage.exe and requestment.txt. Double-click to run pikaPackage.exe, and the source code and modules specified in requestmemt.txt will be pulled down.

_images/142022949-54585060-8612-42a8-a585-8c3e514a8be3.png

The pulled files are shown in the figure below, pikascript-core is the kernel source code, pikascript-lib is the module library, pikascript-api is the module API, and rust-msc-latest-win10.exe is the dedicated precompiler for pikascript.

https://user-images.githubusercontent.com/88232613/171088378-ea6385fb-56aa-4b87-afae-891d69b2f783.png

After installation, the package manager will automatically lock the version and the requirement.txt will look like this

pikascript-core==v1.11.0
PikaStdLib==v1.11.0

If you want to upgrade the version, modify the version number in requestment.txt and run pikaPackage.exe again, the original version will be overwritten.

After pulling the source code, the next step is to write the python script that pikascript runs.

We create a new main.py file in the pikascript folder.

Then write:

import PikaStdLib

print('hello PikaPython!')

Among them, import PikaStdLib means importing the standard library.

The standard library must be imported, even if it is not used directly, and print('hello PikaPython!') is used to test whether pikascript is started normally.

4.1.2.3. Precompile modules

Next, run rust-msc-latest-win10.exe to precompile main.py and imported modules into pikascript api files.

The precompiled files are in the pikascript-api folder.

https://user-images.githubusercontent.com/88232613/171088576-8bbb420b-c2c5-4f01-b280-f88d45c89f9e.png

We open the pikascript-api folder and find that there are some .c and .h files in it, which means that the precompile is successful.

The pikascript precompiler can precompile C modules into .c and .h files.

_images/1644385073271-a39b8d40-f722-4a0c-9b50-e7357f0eae48.png

4.1.2.4. Add source code

Create three new groups in Project, it is recommended to name them pikascript-core, pikascript-api and pikascript-lib

_images/130967351-597b8f6b-cc4e-4bc3-9cb6-2f335e5dccea.png

Then add all the .c files in the three subfolders of the pikascript folder (including the subfolder in pikascript-lib) to the keil project (the actual number of .c files may not match the screenshot, just add them all.)

_images/130971776-41d8c940-42d0-407d-872e-53525ce299a6.png

Then add include paths for pikascript-core and pikascript-api folders.

_images/130967813-94016b8a-e408-4b49-b1e1-76a5df5fe984.png

_images/130967949-8399c65b-5584-4674-a947-e40103d953ea.png

4.1.2.5. Adjust stack

Open the project’s startup file, in stm32, this is a startup_stm32xxxx.s file, and on other platforms, you have to figure out how to adjust the stack yourself.

_images/130966276-24014a0a-90a6-4bd7-96b7-fde54806b8c3.png

It is recommended to allocate 4K stack space and 16K heap space, and at least 1K stack space and 4K heap space need to be allocated

4K stack space corresponds to 0x1000, 16K heap space corresponds to 0x4000, as shown in the following figure

_images/130967178-a985a4f5-730c-47fd-9317-68f33bc00066.png

4.1.2.6. Start PikaPython

Add the startup code of PikaPython in the initialization code of main.c.

  • add header files

add in header file

 #include "pikascript.h"

_images/130969048-4def9902-5f36-4798-9eac-ebbb1441087f.png

  • initialize pikaScript and get the pointer to the pikascript main object pikaMain

Add a startup code to the main function

PikaObj* pikaMain = pikaScriptInit();

_images/130969274-ff2fdf6f-2389-466b-b51e-e7bc33472558.png

ended? Yes, it’s over, it’s that simple, isn’t it amazing.

This is because the precompiler does a lot of auxiliary work behind the scenes, including the automatic generation of the pikaScriptInit() function.

4.1.2.7. compile source code

When compiling the source code, you need to check the C99 standard, and the compilation optimization level can be selected arbitrarily, and pikascript supports it.

_images/130968626-7d8d4f46-eb0c-4ccd-9c34-eab160b290f5.png

Then you can compile it directly. Generally speaking, it can be passed directly.

You can use compiler version 5 or compiler version 6.

4.1.2.8. Contribute BSP

We sincerely appreciate your contribution, by contributing code, you can help PikaPython run on more platforms, and more developers will benefit from you.

Please see the operation method:

4.1.2.9. Add peripheral support

PikaPython manages peripherals through packages. To add peripheral support to the platform, please refer to the following documents: