xeye_ atsu
/
FRDM_RaVem_JVM
JVM test
device_depend.cpp@8:d9e205196dfc, 2013-08-29 (annotated)
- Committer:
- lynxeyed_atsu
- Date:
- Thu Aug 29 14:15:47 2013 +0000
- Revision:
- 8:d9e205196dfc
- Parent:
- 7:2a384a077520
- Child:
- 9:4ea7773ea2b0
fixed port API
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lynxeyed_atsu | 5:047542b65d00 | 1 | #include <stdio.h> |
lynxeyed_atsu | 5:047542b65d00 | 2 | #include <string.h> |
lynxeyed_atsu | 5:047542b65d00 | 3 | #include "device_depend.h" |
lynxeyed_atsu | 5:047542b65d00 | 4 | |
lynxeyed_atsu | 6:b9d0d96b052f | 5 | #define bc_str_length 1024 // from 32 to 1024 |
lynxeyed_atsu | 6:b9d0d96b052f | 6 | |
lynxeyed_atsu | 8:d9e205196dfc | 7 | BusInOut ledport(LED1, LED2, LED3, LED4); |
lynxeyed_atsu | 6:b9d0d96b052f | 8 | Serial pc(USBTX, USBRX); // tx, rx |
lynxeyed_atsu | 6:b9d0d96b052f | 9 | LocalFileSystem local("local"); |
lynxeyed_atsu | 6:b9d0d96b052f | 10 | FILE* ReadFile; |
lynxeyed_atsu | 6:b9d0d96b052f | 11 | Ticker tick; |
lynxeyed_atsu | 5:047542b65d00 | 12 | |
lynxeyed_atsu | 5:047542b65d00 | 13 | volatile static unsigned long msTicks; /* counts 1ms timeTicks */ |
lynxeyed_atsu | 6:b9d0d96b052f | 14 | char bc_str[bc_str_length]; |
lynxeyed_atsu | 6:b9d0d96b052f | 15 | int last_bc_num = 0; |
lynxeyed_atsu | 5:047542b65d00 | 16 | |
lynxeyed_atsu | 5:047542b65d00 | 17 | void hardware_init(void){ |
lynxeyed_atsu | 5:047542b65d00 | 18 | setup_systick(); |
lynxeyed_atsu | 8:d9e205196dfc | 19 | ledport.output(); |
lynxeyed_atsu | 5:047542b65d00 | 20 | } |
lynxeyed_atsu | 5:047542b65d00 | 21 | |
lynxeyed_atsu | 5:047542b65d00 | 22 | void uart_print(char *str){ |
lynxeyed_atsu | 5:047542b65d00 | 23 | pc.printf(str); |
lynxeyed_atsu | 5:047542b65d00 | 24 | return; |
lynxeyed_atsu | 5:047542b65d00 | 25 | } |
lynxeyed_atsu | 5:047542b65d00 | 26 | |
lynxeyed_atsu | 5:047542b65d00 | 27 | void uart_init(int baud_rate){ |
lynxeyed_atsu | 5:047542b65d00 | 28 | |
lynxeyed_atsu | 5:047542b65d00 | 29 | } |
lynxeyed_atsu | 5:047542b65d00 | 30 | |
lynxeyed_atsu | 5:047542b65d00 | 31 | int uart_read(void){ |
lynxeyed_atsu | 5:047542b65d00 | 32 | return pc.getc(); |
lynxeyed_atsu | 5:047542b65d00 | 33 | } |
lynxeyed_atsu | 5:047542b65d00 | 34 | |
lynxeyed_atsu | 5:047542b65d00 | 35 | int time_millis(void){ |
lynxeyed_atsu | 5:047542b65d00 | 36 | return msTicks; |
lynxeyed_atsu | 5:047542b65d00 | 37 | } |
lynxeyed_atsu | 5:047542b65d00 | 38 | |
lynxeyed_atsu | 5:047542b65d00 | 39 | void port_write(int port, int bit, int value){ |
lynxeyed_atsu | 8:d9e205196dfc | 40 | if((bit < 0)||(bit > 3))return; |
lynxeyed_atsu | 8:d9e205196dfc | 41 | int i = ledport; |
lynxeyed_atsu | 8:d9e205196dfc | 42 | if(value) |
lynxeyed_atsu | 8:d9e205196dfc | 43 | i |= (1 << bit); |
lynxeyed_atsu | 8:d9e205196dfc | 44 | else |
lynxeyed_atsu | 8:d9e205196dfc | 45 | i &= ~(1 << bit); |
lynxeyed_atsu | 8:d9e205196dfc | 46 | |
lynxeyed_atsu | 8:d9e205196dfc | 47 | ledport = i; |
lynxeyed_atsu | 7:2a384a077520 | 48 | return; |
lynxeyed_atsu | 5:047542b65d00 | 49 | } |
lynxeyed_atsu | 5:047542b65d00 | 50 | |
lynxeyed_atsu | 6:b9d0d96b052f | 51 | void bytecode_read_init(void){ |
lynxeyed_atsu | 6:b9d0d96b052f | 52 | |
lynxeyed_atsu | 6:b9d0d96b052f | 53 | if(NULL == (ReadFile = fopen ("/local/Test.cla","rb"))){ uart_print(" ERROR:Can't read class File.\r\n"); while(1); } |
lynxeyed_atsu | 6:b9d0d96b052f | 54 | fseek(ReadFile, 0, SEEK_SET); |
lynxeyed_atsu | 6:b9d0d96b052f | 55 | // I don't know why, but fseek does not work right. So I use fgetc instead. |
lynxeyed_atsu | 6:b9d0d96b052f | 56 | //fgets(bc_str, bc_str_length, ReadFile); |
lynxeyed_atsu | 6:b9d0d96b052f | 57 | printf("loading..."); |
lynxeyed_atsu | 6:b9d0d96b052f | 58 | for(int i = 0; i < bc_str_length; i++) bc_str[i] = fgetc(ReadFile); |
lynxeyed_atsu | 6:b9d0d96b052f | 59 | printf("end\r\n"); |
lynxeyed_atsu | 6:b9d0d96b052f | 60 | //last_bc_num = bc_str_length; |
lynxeyed_atsu | 6:b9d0d96b052f | 61 | } |
lynxeyed_atsu | 6:b9d0d96b052f | 62 | |
lynxeyed_atsu | 6:b9d0d96b052f | 63 | char* bytecode_read(int bc_num, int length){ |
lynxeyed_atsu | 6:b9d0d96b052f | 64 | |
lynxeyed_atsu | 6:b9d0d96b052f | 65 | if((last_bc_num <= bc_num)&&(bc_num < last_bc_num + bc_str_length)&&(length <= bc_str_length)){ // exists data in buffer |
lynxeyed_atsu | 6:b9d0d96b052f | 66 | return &bc_str[bc_num - last_bc_num]; |
lynxeyed_atsu | 6:b9d0d96b052f | 67 | }else |
lynxeyed_atsu | 6:b9d0d96b052f | 68 | { |
lynxeyed_atsu | 6:b9d0d96b052f | 69 | fseek(ReadFile, bc_num, SEEK_SET); |
lynxeyed_atsu | 6:b9d0d96b052f | 70 | for(int i = 0; i < bc_str_length; i++) |
lynxeyed_atsu | 6:b9d0d96b052f | 71 | { |
lynxeyed_atsu | 6:b9d0d96b052f | 72 | bc_str[i] = fgetc(ReadFile); |
lynxeyed_atsu | 6:b9d0d96b052f | 73 | } |
lynxeyed_atsu | 6:b9d0d96b052f | 74 | last_bc_num = bc_num; |
lynxeyed_atsu | 6:b9d0d96b052f | 75 | } |
lynxeyed_atsu | 6:b9d0d96b052f | 76 | return bc_str; |
lynxeyed_atsu | 6:b9d0d96b052f | 77 | } |
lynxeyed_atsu | 5:047542b65d00 | 78 | |
lynxeyed_atsu | 5:047542b65d00 | 79 | /*---------------------------------------------------------------------------- |
lynxeyed_atsu | 5:047542b65d00 | 80 | SysTick_Handler |
lynxeyed_atsu | 5:047542b65d00 | 81 | *----------------------------------------------------------------------------*/ |
lynxeyed_atsu | 5:047542b65d00 | 82 | void SysTickCount(void) { |
lynxeyed_atsu | 5:047542b65d00 | 83 | msTicks++; /* increment counter necessary in Delay() */ |
lynxeyed_atsu | 5:047542b65d00 | 84 | } |
lynxeyed_atsu | 5:047542b65d00 | 85 | |
lynxeyed_atsu | 5:047542b65d00 | 86 | void setup_systick (void) { |
lynxeyed_atsu | 5:047542b65d00 | 87 | msTicks = 0; |
lynxeyed_atsu | 5:047542b65d00 | 88 | tick.attach_us(&SysTickCount, 1000); //set 32kHz/8(word FIFO) sampling data } |
lynxeyed_atsu | 5:047542b65d00 | 89 | } |
lynxeyed_atsu | 5:047542b65d00 | 90 |