PScript – Part 4 – Built-In Functions

To adapt PScript to the current platform I add an interface to add “built-in” content and a C++ library with some content prepared. This is to make PScript as scalable as possible as built-in content needs to be platform specific and depending on how much Flash/SRAM that is available.


Records are bit-packed and can have fixed addresses making them excellent to overlay IO registers. This open for two types of interface to hardware – one there PScript go low level through pre-defined records and a more abstract interface through functions.

PScript is not object oriented, but to access HW IO we use record names as prefix separated with a “.” (dot) as we would have done if record was object variables.



while true
    GPIOA.digitalWrite(0, HIGH)
    GPIOA.digitalWrite(0, LOW)

This example will toggle GPIOA, pin 0 high/low at ca 1Hz frequency.

To support this I will need (1) a method to add constants like OUTPUT, PULLDOWN etc, (2) a method to add records and a method to add functions with and without object reference. I will return to this as I discuss PScript API. Focusing on records and functions I plan the following list. Arduino have a well designed interface, so using that as a starting guideline will be good. This proposal is based on STM32F405RG, so low level content like registers will differ from MCU to MCU.


GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF etc GPIO ports with member variables to match registers on MCU.
ADC1, ADC2, ADC3 Analogue to digital conversion.
Serial1, Serial2, Serial3, Serial4, Serial5, Serial6 Serial ports.
SPI1, SPI2, SPI3 SPI ports
I2C1, I2C2, I2C3 I2C ports
DAC1, DAC2 Digital to Analogue conversion.
RTC Real Time Clock
TIM1 to TIM14 HW Timers
CAN1, CAN2 CAN ports
I2S1, I2S3 I2S ports
RNG Random Number Generator
ETC Elapsed Timer

The only records I am holding back on are some of the core content like clock, dma and wathdogs etc as I will cover those differently. I need to evaluate a bit as I go.

The records are as mentioned MCU specific due to their content, but the interface functions are designed to be portable with one exception – pinMode will be MCU specific. The list below is a brief intro – I will describe syntax and modules in more details later.

pinMode Define what we use a specific pin for.
digitalWrite Set GPIO pins
digitalRead Read GPIO pins
analogRead ADC
analogWrite DAC
serial1.Open Open a serial port. Both USB, Serial, SPI, I2C, CAN and I2S can be treated as serial ports with a bytes in/out concept.
serial1.Close| Close a serial port
serial1.Send Send bytes on a serial port
serial1.Receive Receive bytes on a serial port
tim1.start Start a timer
tim1.stop Stop a timer
rtc.SetTime Set time
rtc.GetTime Get current time
rtc.SetAlarm Set alarm clock
etc.millis Get elapsed time in ms.
etc.micros Get elapsed time in micro seconds.
rtos.StartThread Start a function as a thread.
rtos.StopThread Stop a function as a thread
rtos.Lock Lock processing to this thread
rtos.Unlock Unlock so threads/events can execute
print, println Print and print line
math Math functions like cos, tan, sin etc.

The list will be evaluated and subject for changes. The objective is to provide some functionality to PScript, but at the same time avoid making the package to large.

Leave a Reply