-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
255 lines (226 loc) · 6.41 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <msp430.h>
#include <ctl_api.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <ARCbus.h>
#include <SDlib.h>
#include "timerA.h"
#include "terminal.h"
#include <Error.h>
CTL_TASK_t tasks[3];
//stacks for tasks
unsigned stack1[1+200+1];
unsigned stack2[1+700+1];
unsigned stack3[1+300+1];
CTL_EVENT_SET_t cmd_parse_evt;
unsigned char buffer[80];
int __putchar(int c){
return async_TxChar(c);
}
//handle subsystem specific commands
int SUB_parseCmd(unsigned char src,unsigned char cmd,unsigned char *dat,unsigned short len){
int i;
switch(cmd){
//Handle Print String Command
case 6:
//check packet length
if(len>sizeof(buffer)){
//return error
return ERR_PK_LEN;
}
//copy to temporary buffer
for(i=0;i<len;i++){
buffer[i]=dat[i];
}
//terminate string
buffer[i]=0;
//set event
ctl_events_set_clear(&cmd_parse_evt,0x01,0);
//Return Success
return RET_SUCCESS;
}
//Return Error
return ERR_UNKNOWN_CMD;
}
void cmd_parse(void *p) __toplevel{
unsigned int e;
//init event
ctl_events_init(&cmd_parse_evt,0);
//loop forever
for(;;){
e=ctl_events_wait(CTL_EVENT_WAIT_ANY_EVENTS_WITH_AUTO_CLEAR,&cmd_parse_evt,0x01,CTL_TIMEOUT_NONE,0);
if(e&0x01){
//print message
if(async_isOpen()){
printf("%s\r\n",buffer);
}
}
}
}
void sub_events(void *p) __toplevel{
unsigned int e,len;
int i;
unsigned char buf[10],*ptr;
extern unsigned char async_addr;
for(;;){
e=ctl_events_wait(CTL_EVENT_WAIT_ANY_EVENTS_WITH_AUTO_CLEAR,&SUB_events,SUB_EV_ALL|SUB_EV_ASYNC_OPEN|SUB_EV_ASYNC_CLOSE,CTL_TIMEOUT_NONE,0);
if(e&SUB_EV_PWR_OFF){
//print message
//puts("System Powering Down\r");
}
if(e&SUB_EV_PWR_ON){
//print message
//puts("System Powering Up\r");
}
if(e&SUB_EV_SEND_STAT){
//send status
//puts("Sending status\r");
//setup packet
//TODO: put actual command for subsystem response
ptr=BUS_cmd_init(buf,20);
//TODO: fill in telemitry data
//send command
BUS_cmd_tx(BUS_ADDR_CDH,buf,0,0,BUS_I2C_SEND_FOREGROUND);
}
if(e&SUB_EV_TIME_CHECK){
//printf("time ticker = %li\r\n",get_ticker_time());
}
if(e&SUB_EV_SPI_DAT){
//puts("SPI data recived:\r");
//get length
len=arcBus_stat.spi_stat.len;
//print out data
/*for(i=0;i<len;i++){
//printf("0x%02X ",rx[i]);
printf("%03i ",arcBus_stat.spi_stat.rx[i]);
}
printf("\r\n");*/
//free buffer
BUS_free_buffer_from_event();
}
if(e&SUB_EV_SPI_ERR_CRC){
puts("SPI bad CRC\r");
}
if(e&SUB_EV_ASYNC_OPEN){
//kill off the terminal if it is running
//ctl_task_remove(&tasks[1]);
//setup closed event
async_setup_close_event(&SUB_events,SUB_EV_ASYNC_CLOSE);
//setup async terminal
//ctl_task_run(&tasks[1],2,terminal,"\rRemote Terminal started\r\n","async_terminal",sizeof(stack2)/sizeof(stack2[0])-2,stack2+1,0);
//print message
//printf("Async Opened from 0x%02X\r\n",async_addr);
//set LED's to indicate status
#ifdef ACDS_BUILD
P4OUT&=~BIT0;
P4OUT|=BIT1;
#else
P7OUT&=~BIT0;
P7OUT|=BIT1;
#endif
}
if(e&SUB_EV_ASYNC_CLOSE){
//kill off async terminal
//ctl_task_remove(&tasks[1]);
//set LED's to indicate status
#ifdef ACDS_BUILD
P4OUT&=~BIT1;
#else
P7OUT&=~BIT1;
#endif
}
}
}
//init mmc card before starting terminal task
void sd_term(void *p) __toplevel{
int resp;
//wait for async connection to open
while(!async_isOpen()){
ctl_timeout_wait(ctl_get_current_time()+1024);
}
//setup the SD card
resp=mmcInit_card();
//check response
if(resp==MMC_SUCCESS){
printf("\rSD Card Initialized\r\n");
#ifndef ACDS_BUILD
P7OUT|=BIT7;
#endif
}else{
printf("\rError Initializing SD Card\r\n""Response = %i\r\n%s\r\n",resp,SD_error_str(resp));
#ifndef ACDS_BUILD
P7OUT|=BIT6;
#endif
}
//start terminal
terminal(p);
}
static const TERM_SPEC async_term={"SD Card Test Program Ready",async_Getc};
int main(void){
//DO this first
ARC_setup();
//setup system specific peripherals
//setup mmc interface
mmcInit_msp();
//TESTING: set log level to report everything by default
set_error_level(0);
#ifdef ACDS_BUILD
//set all driver pins low
P2OUT&=0x03;
P4OUT&=0x03;
P5OUT&=0x03;
P6OUT&=0x03;
P7OUT&=0x03;
P8OUT&=0x03;
//set pins to GPIO function
P2SEL&=0x03;
P4SEL&=0x03;
P5SEL&=0x03;
P6SEL&=0x03;
P7SEL&=0x03;
P8SEL&=0x03;
//set driver pins as outputs
P2DIR|=0xFC;
P4DIR|=0xFC;
P5DIR|=0xFC;
P6DIR|=0xFC;
P7DIR|=0xFC;
P8DIR|=0xFC;
//setup LED Pins
P2SEL&=0xFC;
P4SEL&=0xFC;
P2OUT&=0xFC;
P4OUT&=0xFC;
P2DIR|=0x03;
P4DIR|=0x03;
//turn on power LED
P2DIR|=BIT1;
//setup bus interface
initARCbus(BUS_ADDR_ACDS);
#else
//setup P8 for output
P8OUT=0x00;
P8DIR=0xFF;
P8SEL=0x00;
//Turn off LED's
P7OUT=0x00;
P7DIR=0xFF;
//turn on power LED
P7OUT|=BIT4;
//setup bus interface
initARCbus(BUS_ADDR_IMG);
#endif
//initialize stacks
memset(stack1,0xcd,sizeof(stack1)); // write known values into the stack
stack1[0]=stack1[sizeof(stack1)/sizeof(stack1[0])-1]=0xfeed; // put marker values at the words before/after the stack
memset(stack2,0xcd,sizeof(stack2)); // write known values into the stack
stack2[0]=stack2[sizeof(stack2)/sizeof(stack2[0])-1]=0xfeed; // put marker values at the words before/after the stack
memset(stack3,0xcd,sizeof(stack3)); // write known values into the stack
stack3[0]=stack3[sizeof(stack3)/sizeof(stack3[0])-1]=0xfeed; // put marker values at the words before/after the stack
//create tasks
ctl_task_run(&tasks[0],BUS_PRI_LOW,cmd_parse,NULL,"cmd_parse",sizeof(stack1)/sizeof(stack1[0])-2,stack1+1,0);
ctl_task_run(&tasks[1],BUS_PRI_NORMAL,sd_term,(void*)&async_term,"terminal",sizeof(stack2)/sizeof(stack2[0])-2,stack2+1,0);
ctl_task_run(&tasks[2],BUS_PRI_HIGH,sub_events,NULL,"sub_events",sizeof(stack3)/sizeof(stack3[0])-2,stack3+1,0);
mainLoop();
}