29. PikaScript configuration manual

29.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

29.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

29.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

29.2.2. Configuration items

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

https://gitee.com/Lyon1998/pikascript/blob/master/src/pika_config_valid.h

Intercept the important part for explanation:

  1. /* default configuration */
  2. #define PIKA_LINE_BUFF_SIZE 128
  3. #define PIKA_SPRINTF_BUFF_SIZE 128
  4. #define PIKA_STACK_BUFF_SIZE 256
  5. #define PIKA_NAME_BUFF_SIZE 32
  6. #define PIKA_PATH_BUFF_SIZE 64
  7. #define PIKA_ARG_ALIGN_ENABLE 1
  8. #define PIKA_METHOD_CACHE_ENABLE 0
  9. /* optimize options */
  10. #define PIKA_OPTIMIZE_SIZE 0
  11. #define PIKA_OPTIMIZE_SPEED 1
  12. /* default optimize */
  13. #define PIKA_OPTIMIZE PIKA_OPTIMIZE_SIZE
  14. /* use user config */
  15. #ifdef PIKA_CONFIG_ENABLE
  16. #include "pika_config.h"
  17. #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. #undef PIKA_STACK_BUFF_SIZE
  2. #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 use Write in `pika_config.h

  1. #undef PIKA_OPTIMIZE
  2. #define PIKA_OPTIMIZE PIKA_OPTIMIZE_SPEED

29.2.3. Sample code

https://gitee.com/Lyon1998/pikascript/blob/master/bsp/stm32g070cb/Booter/pika_config.h

29.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);

29.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

29.3.2. Sample code: