Run mruby / c on PSoC 5

In this article, I will show you the procedure to run the α version of mruby / c with PSoC5 Prototyping Kit (CY8CKIT-059).

PSoC4 Prototyping Kit (CY8CKIT-049) can be used in the same procedure.

mruby / c is an implementation of mruby that requires less resources at runtime. It is assumed to operate at 64KB or less. Currently, the α version is being distributed.

For information on obtaining the mruby / c α version, see Mruby / c Initiatives of Shimane Software Development Center (ITOC). -Please see itoc.jp/news/10).

Things necessary

Overall flow

  1. Create a simple C program project
  2. Add mruby / c to 1 project
  3. Compile the mruby program and embed it in 2.

Project creation

Create an empty project.

img01.png

If "CY8CKIT-059" does not appear in "Target hardware", select "CY8C5888LTI-LP097" in "Target device".

In the case of PSoC4, it will be "CY8CKIT-049" or "CY8C4247AZI-M485".

On the next screen, select "Empty Schematic". Next, enter the project name and so on. Here, the project name is "mrubyc_sample".

img02.png

Blink LED

Add Digital Output Pin to the schematic.

img03.png

Double-click this Pin to set it.

img04.png

--Set "Name" to "LED" --Uncheck "HW connection"

Set the pin

Open the .cydwr file and set the LED port to "P2 [1]".

img05.png

In the case of PSoC4, the LED port is "P1 [6]".

Build

Build the project here.

By building, the code related to the LED output set earlier will be automatically generated.

Creating a C program

Create the main function of main.c as follows.

main function


int main()
{
    CyGlobalIntEnable; /* Enable global interrupts. */

    /* Place your initialization/startup code here (e.g. MyInst_Start()) */

    for(;;)
    {
        /* Place your application code here. */
        LED_Write(1);
        CyDelay(200);
        LED_Write(0);
        CyDelay(200);
    }
}

Run

Build and write to the board by selecting [Debug]-[Program]. It is OK if the LED on the board blinks.


Copy the mruby / c files

All the following files (xxx.c and xxx.h) in the src folder obtained by unzipping the mruby / c α version are copied to the PSoC Creator project folder (the folder containing main.c).

Add files to your project

Select [Project]-[Existing Item ...] to add the mruby / c α version file (xxx.c). You can select multiple files and add them at once.

Add method

Make the LED_Write function and CyDelay function used in the C program available from mruby. Add each method to the Object class.

Open the class.c file and add or modify the following.

Header file to add


#include "LED.h"
#include "CyLib.h"

Function to add


mrb_value c_led(struct VM *vm, mrb_value v)
{
  LED_Write( v.value.i );
  return v;
}

mrb_value c_delay(struct VM *vm, mrb_value v)
{
  CyDelay(v.value.i);
  return v;
}

mrb_init_class_Rewrite object function


static void mrb_init_class_object(void)
{
  // Class
  static_class_object = mrb_class_alloc("Object", -1);
  // Methods
  mrb_define_method(static_class_object, "led", c_led);
  mrb_define_method(static_class_object, "delay", c_delay);
}

The mrb_define_method function in the mrb_init_class_object function associates the C function with the mruby method.


Creating a mruby program

Create the following mruby program as sample.rb. The LED blinks at a different timing than the program created in C.

sample.rb


led 1
delay 100
led 0
delay 900

Compile with mrbc

Compile sample.rb with mruby (OSS version) to create sample.c. Copy sample.c to the same folder as main.c in the PSoC project.

  mrbc -E -Bary sample.rb 

Rewrite main.c and call the mruby program from C

Add the following to main.c.

Add header file


#include "vm.h" 
#include "load.h" 
#include "errorcode.h" 
#include "static.h" 
#include "sample.c"

Add the mrubyc function.

mrubyc function


int mrubyc(void)
{
  struct VM *vm;

  init_static();

  vm = vm_open();
  if( vm == 0 ){
    printf("VM open Error\n");
    return -1;
  }

  int ret = loca_mrb_array(vm, ary);
  if( ret != NO_ERROR ){
    printf("MRB Load Error (%04x_%04x)\n", ret>>16, ret&0xffff);
    return -1;
  }

  vm_boot( vm );

  int keep_execute = 1;
  while( keep_execute ){
    if( vm_run_step(vm) < 0 ){
      keep_execute = 0;
    }
  }

  vm_close( vm );

  return 0;
}

main function


int main()
{
    CyGlobalIntEnable; /* Enable global interrupts. */

    /* Place your initialization/startup code here (e.g. MyInst_Start()) */

    for(;;)
    {
        /* Place your application code here. */
        mrubyc();
    }
}

Build and run

Build and write to the board by selecting [Debug]-[Program]. It is OK if the LED on the board blinks.


It's been a little long explanation, but now you can run the mruby / c α version on PSoC5.

Recommended Posts

Run mruby / c on PSoC 5
Run C binaries on AWS Lambda
Run STS4 on Mac
Test run on rails
Run Processing on Ant
Run tiscamera on Ubuntu 18.04
Run phpunit on Docker
Run VS Code on Docker
Run openvpn on Docker (windows)
Run chromium-mir-kiosk on Ubuntu Core
Run java applet on ubuntu
Run Java EE applications on CICS
Run SSE (Server-Sent-Event) samples on docker
Steps to run docker on Mac
Run puppeteer-core on Heroku (Docker edition)
Run React on a Docker container
Run the AWS CLI on Docker
How to run JavaFX on Docker
Run GUI application on Docker container
Run PureScript on a Docker container
Run tomcat shell script on java8
Easily try C # 9 (.NET 5) on Docker
Run GPU-required batch processing on AWS