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).
Create an empty project.
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".
Add Digital Output Pin to the schematic.
Double-click this Pin to set it.
--Set "Name" to "LED" --Uncheck "HW connection"
Open the
In the case of PSoC4, the LED port is "P1 [6]".
Build the project here.
By building, the code related to the LED output set earlier will be automatically generated.
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);
}
}
Build and write to the board by selecting [Debug]-[Program]. It is OK if the LED on the board blinks.
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).
Select [Project]-[Existing Item ...] to add the mruby / c α version file (xxx.c). You can select multiple files and add them at once.
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.
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 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
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 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