8.1. PikaScript configuration manual

8.1.1. When to configure

PikaScript itself is configuration-free, so usually you don’t need to know this part.

You can consider configuring PikaScript when you have the following requirements:

  • faster speed

  • Smaller memory footprint

  • Replace dependencies (libc, pinrtf, etc.)

  • Replace memory management algorithm ( malloc )

  • Safer interruption protection

8.1.2. Optimization

[Note]: For optimized configuration, the kernel version needs to be at least v1.5.4

Similar to GCC, PikaScript also provides different optimization modes. The currently available optimization modes are:

  • PIKA_OPTIMIZE_SIZE volume mode minimizes running memory

  • PIKA_OPTIMIZE_SPEED performance mode maximizes running speed

8.1.2.1. Enable user configuration

User configuration is not enabled by default. The way to enable user configuration is to add compile-time macro definition PIKA_CONFIG_ENABLE. Then create the pika_config.h header file.

It should be noted that the PIKA_CONFIG_ENABLE macro should be added to the compile options, such as keil:

_images/160849244-40fe7fa8-0e93-4791-8f14-bc044bbd0d59.png

8.1.2.2. Configuration items

Available configuration items and default configuration are in the pika_config_valid.h header file.

https://github.com/pikastech/pikascript/blob/master/src/pika_config_valid.h

Intercept the important part for explanation:

  1. /* optimize options */
  2. #define PIKA_OPTIMIZE_SIZE 0
  3. #define PIKA_OPTIMIZE_SPEED 1
  4. /* syntax support level */
  5. #define PIKA_SYNTAX_LEVEL_MINIMAL 0
  6. #define PIKA_SYNTAX_LEVEL_MAXIMAL 1
  7. /* use user config */
  8. #ifdef PIKA_CONFIG_ENABLE
  9. #include "pika_config.h"
  10. #endif
  11. /* default optimize */
  12. #ifndef PIKA_OPTIMIZE
  13. #define PIKA_OPTIMIZE PIKA_OPTIMIZE_SIZE
  14. #endif
  15. /* default syntax support level */
  16. #ifndef PIKA_SYNTAX_LEVEL
  17. #define PIKA_SYNTAX_LEVEL PIKA_SYNTAX_LEVEL_MAXIMAL
  18. #endif
  19. ...
  20. /* default configuration */
  21. #ifndef PIKA_STACK_BUFF_SIZE
  22. #define PIKA_STACK_BUFF_SIZE 256
  23. #endif

default configuration is the default value of the configuration item. When the PIKA_CONFIG_ENABLE macro is defined, pika_config_valid.h will import pika_config.h, so User can override the above default configuration in pika_config.h.

For example, if you want to increase the runtime stack of the PikaScript virtual machine, you can write in pika_config.h

  1. #define PIKA_STACK_BUFF_SIZE 512

As can be seen from pika_config_valid.h, the default optimization option of PikaScript PIKA_OPTIMIZE is PIKA_OPTIMIZE_SIZE, if you need to switch to speed optimization, you can write in pika_config.h

  1. #define PIKA_OPTIMIZE PIKA_OPTIMIZE_SPEED

8.1.2.3. Sample code

https://github.com/pikastech/pikascript/blob/master/bsp/stm32g070cb/Booter/pika_config.h

8.1.3. Dependency configuration

PikaScript can be configured by creating pika_config.c, rewriting the weak functions in PikaPlagform.h ‘s dependencies.

  1. /* interrupt config */
  2. void __platform_enable_irq_handle(void);
  3. void __platform_disable_irq_handle(void);
  4. /* printf family config */
  5. #ifndef __platform_printf
  6. void __platform_printf(char* fmt, ...);
  7. #endif
  8. int __platform_sprintf(char* buff, char* fmt, ...);
  9. int __platform_vsprintf(char* buff, char* fmt, va_list args);
  10. int __platform_vsnprintf(char* buff,
  11. size_t size,
  12. const char* fmt,
  13. va_list args);
  14. /* libc config */
  15. void* __platform_malloc(size_t size);
  16. void __platform_free(void* ptr);
  17. void* __platform_memset(void* mem, int ch, size_t size);
  18. void* __platform_memcpy(void* dir, const void* src, size_t size);
  19. /* pika memory pool config */
  20. void __platform_wait(void);
  21. uint8_t __is_locked_pikaMemory(void);
  22. /* support shell */
  23. char __platform_getchar(void);
  24. /* file API */
  25. FILE* __platform_fopen(const char* filename, const char* modes);
  26. int __platform_fclose(FILE* stream);
  27. size_t __platform_fwrite(const void* ptr, size_t size, size_t n, FILE* stream);
  28. /* error */
  29. void __platform_error_handle(void);

8.1.3.1. Configuration items:

  • Interrupt Protection - Provides an interrupt master switch to protect PikaScript memory safety

  • libC - select the implementation of libC

  • Memory management - replace malloc free memory management algorithm

8.1.3.2. Sample code: