-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
178 lines (140 loc) · 3.43 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* ************************************************************************** */
/** main
@Company
ShimaneJohoshoriCenter.inc
@File Name
main.c
@Summary
mruby/c firmware for Rboard.
@Description
main routine.
*/
/* ************************************************************************** */
#include <xc.h>
#include <sys/attribs.h>
#include <string.h>
#include "pic32mx.h"
#include "gpio.h"
#include "uart.h"
#include "mrubyc.h"
#include "model_dependent.c" // include system (CPU) related functions.
// mruby/c heap
#if !defined(MRBC_MEMORY_SIZE)
#define MRBC_MEMORY_SIZE (1024*40)
#endif
uint8_t memory_pool[MRBC_MEMORY_SIZE];
// function prototypes.
void tick_timer_init( void );
void mrbc_init_class_adc(void);
void mrbc_init_class_pwm(void);
void mrbc_init_class_i2c(void);
void mrbc_init_class_spi(void);
void add_code(void);
//================================================================
/*
HAL functions.
*/
int hal_write(int fd, const void *buf, int nbytes) {
return uart_write( UART_HANDLE_CONSOLE, buf, nbytes );
}
int hal_flush(int fd) {
return 0;
}
void hal_abort( const char *s )
{
if( s ) {
hal_write( 0, s, strlen(s) );
}
__delay_ms(5000);
system_reset();
}
void _mon_putc( char c )
{
uart_write( UART_HANDLE_CONSOLE, &c, 1 );
}
//================================================================
/*
on board devices
*/
/*! control the onboard LEDs
leds_write( n ) # n = 0 to 0b1111 (bit mapped)
*/
static void c_leds_write(mrbc_vm *vm, mrbc_value v[], int argc)
{
int led = GET_INT_ARG(1);
onboard_led( 1, led & 0x01 );
onboard_led( 2, led & 0x02 );
onboard_led( 3, led & 0x04 );
onboard_led( 4, led & 0x08 );
}
/*! read the onboard switch
x = sw()
*/
static void c_sw(mrbc_vm *vm, mrbc_value v[], int argc)
{
SET_INT_RETURN( onboard_sw(1) );
}
/*!
Choose to enter programming mode or run mode.
*/
static int check_timeout( void )
{
for( int i = 0; i < 50; i++ ) {
onboard_led( 1, 1 );
__delay_ms( 30 );
onboard_led( 1, 0 );
__delay_ms( 30 );
if( uart_can_read_line( UART_HANDLE_CONSOLE ) ) return 1;
}
return 0;
}
/*!
main function
*/
int main(void)
{
/* module init */
system_init();
uart_init();
if( check_timeout() ) {
/* IDE code */
add_code();
memset( memory_pool, 0, sizeof(memory_pool) );
}
mrbc_printf("\r\n\x1b(B\x1b)B\x1b[0m\x1b[2JRboard v2.1.0, mruby/c v3.3.1 start.\n");
/* start mruby/c */
mrbc_init(memory_pool, MRBC_MEMORY_SIZE);
mrbc_init_class_gpio();
mrbc_init_class_uart();
mrbc_init_class_adc();
mrbc_init_class_pwm();
mrbc_init_class_i2c();
mrbc_init_class_spi();
mrbc_define_method(0, 0, "leds_write", c_leds_write);
mrbc_define_method(0, 0, "sw", c_sw);
tick_timer_init();
#if 1
const uint8_t *fl_addr = (uint8_t*)FLASH_SAVE_ADDR;
static const char RITE[4] = "RITE";
while( strncmp( (const char *)fl_addr, RITE, sizeof(RITE)) == 0 ) {
if( ! mrbc_create_task(fl_addr, 0) ) return 1;
// get a next irep.
uint32_t size = 0;
for( int i = 0; i < 4; i++ ) {
size = (size << 8) | fl_addr[8 + i];
}
fl_addr += FLASH_ALIGN_ROW_SIZE( size );
}
#else
/* Or run the prepared bytecode.
How to create "prepared_bytecode.c"
mrbc --remove-lv -B bytecode -o prepared_bytecode.c *.rb
*/
#include "prepared_bytecode.c"
mrbc_printf("prepared bytecode executing.\n");
mrbc_create_task(bytecode, 0);
#endif
/* and run! */
mrbc_run();
return 1;
}