-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLEDL.c
More file actions
295 lines (257 loc) · 11.8 KB
/
Copy pathLEDL.c
File metadata and controls
295 lines (257 loc) · 11.8 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <msp430.h>
#include <string.h>
#include <stdio.h>
#include <ctl.h>
#include <ARCbus.h>
#include "log_data.h"
#include <SDlib.h>
#include <Error.h>
#include "SensorsOnOff.h"
#include "I2C_sensor.h"
#include "LEDL.h"
void CLYDE_STAT_LED_toggle(void){
//P4OUT^=BIT3;
}
void LEDL_STAT_LED_toggle(void){
//P4OUT^=BIT2;
}
void ERR_LED_on(void)
{
//P4OUT^=BIT5;
}
void sub_events(void *p) __toplevel{
unsigned int e,len;
int i,resp;
extern CTL_TASK_t tasks[3];
extern startup_mode;
LEDL_STAT ledl_status;
CLYDE_STAT clyde_status;
unsigned char buf[BUS_I2C_HDR_LEN+sizeof(LEDL_STAT)+BUS_I2C_CRC_LEN],*ptr;
if (P1IN&BIT0)
{
ctl_events_set_clear(&SUB_events,SUB_EV_INT_0,0);
BUS_lp_mode=ML_LP_EXIT;
}
//initialize EPS mutex
ctl_mutex_init(&EPS_mutex);
for(;;){
e=ctl_events_wait(CTL_EVENT_WAIT_ANY_EVENTS_WITH_AUTO_CLEAR,&SUB_events,SUB_EV_ALL,CTL_TIMEOUT_NONE,0);
if(e&SUB_EV_INT_0){
//CDH is on
//switch has just been selected, switch_is_on is checked for initial conditions and enters this code to begin taking data.
volatile unsigned int *arr = &ADC12MEM8;// creates a pointer, looks at the memory of mem0
int resp;
int *buffer=NULL;//changed from char to int instead of each char being 1 byte their are now two bytes, resulting in being half as many things to index
unsigned long SDaddr_for_total_blocks_used=0;
LEDL_TEST_LAUNCH *launch_detect_data;
int mmcReturnValue;
BUS_lp_mode=ML_LP_EXIT;
startup_mode=MODE_ORBIT;
switch_is_on=0;//turn off launch code
LED_3_ON();
//turn up the voltage
//NEED TO BLOW FUSE CODE
// LEDL_BLOW_FUSE();
//SWITCH TO CLYDE POWER
LEDL_SWITCH_TO_EPS();
//need to have a setteling time for the voltage regulator
//then initalize the 16 Mhz clock
ctl_timeout_wait(ctl_get_current_time()+5);//4.88 MS
SENSORSon();//turn on sensor
//turn on sensors here
MAGon();//turn on magnetometers
GyroWakeUp();
initCLK();
ADC12CTL0&=~ENC;
ADC12CTL1|=SHS_0|CONSEQ_1;//sets adc to sample and hold souce select on adc12sc, and conseq choses sequence of channels
ADC12CTL0|=ENC;//enc enables the adc, and ADC12sc starts the sample and conversion
ADC12IFG=0;//CLEAR ALL FLAGS
ADC12IE = BIT8|BITA; // ENABLE THE INTERUPTS FOR IE7
//initalize the SD card
ctl_timeout_wait(ctl_get_current_time()+100);//wait for voltage on sd card to stabalize
mmc_pins_on();
mmcReturnValue=mmcInit_card();
if (mmcReturnValue==MMC_SUCCESS){
printf("\rCard initalized Sucessfully\r\n");
}
else {
printf("\rERROR initalizing SD card""\r\n Response = %i\r\n %s", mmcReturnValue,SD_error_str(mmcReturnValue));
}
//need to take data so that LEDL is ready to send data when cdh requests it
ADC12CTL0&=~ENC; //disable ADC
ADC12CTL1&=~CSTARTADD_15; //clear CSTARTADD
ADC12CTL1|= CSTARTADD_8; // CSTARTADD
ADC12CTL0|=ADC12SC|ENC;//need another comment
clyde_take_data(clyde_data);
Temp_I2C_sensor(temp_measure);
//ledl_status.gyro_data[0]=arr[0];
//ledl_status.gyro_data[1]=arr[1];
//ledl_status.gyro_data[2]=arr[2];
for (i=0;i<7;i++)
{//convert temperature to 8 bit value, do we want to lose the quarter degree resolution
}
//get how many blocks have been written to the SD card
//get buffer, set a timeout of 2 secconds
//get buffer, set a timeout of 2 secconds
buffer=BUS_get_buffer(CTL_TIMEOUT_DELAY,2048);
launch_detect_data=(LEDL_TEST_LAUNCH*)buffer;
//check for error
if(buffer==NULL){
printf("Error : Timeout while waiting for buffer.\r\n");
//TODO: SPECIFY A ERROR FUNCTIION FOR THIS
//return -1;
}else{
//read from SD card
resp=mmcReadBlock(SDaddr_for_total_blocks_used,(unsigned char*)buffer);
//store the last SDcard location that is stored in memory, this will either be by the end of logging data when Clyde turns on, or if ledl resets if
//it has decided that logging data has happened or if ledl has had a restart
ledl_status.SDaddress=launch_detect_data->dat.detect_dat.SD_last_address;
//print response from SD card
printf("%s\r\n",SD_error_str(resp));
BUS_free_buffer();
}
//if we get ride of the quarter degree resolution than the structure has to be changed to support 8 bit value, or be clever. 2 data in one slot...
for(i=0;i<7;i++){
ledl_status.temp_data[i]=temp_measure[i];
}
for(i=0;i<2;i++)
{
ledl_status.clyde_array_two_int[i]=clyde_data[i+15];
ledl_status.clyde_array_two_char[i]=clyde_data[i+17];//need to verify this
}
ledl_status.clyde_ttc=clyde_data[19];//need to verify this
for(i=0;i<15;i++)
{
clyde_status.clyde_array_fifteen[i]=clyde_data[i];
}
//free alarm so that we can reset it
BUS_free_alarm(BUS_ALARM_0);
//set alarm to trigger 100s from now
BUS_set_alarm(BUS_ALARM_0,get_ticker_time()+100ul*1024ul,&handle_get_I2C_data,LEDL_EV_STATUS_TIMEOUT);
}
if(e&SUB_EV_PWR_OFF){
//print message
puts("System Powering Down\r\n");
}
if(e&SUB_EV_PWR_ON){
//print message
puts("System Powering Up\r\n");
}
if((e&SUB_EV_SEND_STAT&&(startup_mode==MODE_ORBIT))){
//set up stuff to take data agian
//retrive data for status
volatile unsigned int *arr = &ADC12MEM8;// creates a pointer, looks at the memory of mem0
int resp;
int *buffer=NULL;//changed from char to int instead of each char being 1 byte their are now two bytes, resulting in being half as many things to index
LEDL_TEST_LAUNCH *launch_detect_data;
LED_3_ON();
ADC12CTL0&=~ENC; //disable ADC
ADC12CTL1&=~CSTARTADD_15; //clear CSTARTADD
ADC12CTL1|= CSTARTADD_8; // CSTARTADD
ADC12CTL0|=ADC12SC|ENC;//need another comment
//TURN ON I2C LINE FOR CLYDE
P7DIR |= BIT4;
P7OUT |= BIT4;
//send ledl_status
puts("Sending ledl_status\r\n");
//get ledl_status data
//setup packet
ptr=BUS_cmd_init(buf,CMD_LEDL_STAT);
//fill in telemitry data
for(i=0;i<sizeof(LEDL_STAT);i++){
ptr[i]=((unsigned char*)(&ledl_status))[i];
}
//wait to avoid conflicts
ctl_timeout_wait(ctl_get_current_time()+50);
//send command
//resp=BUS_cmd_tx(BUS_ADDR_CDH,buf,sizeof(LEDL_STAT),0,BUS_I2C_SEND_FOREGROUND);
resp=BUS_cmd_tx(BUS_ADDR_CDH,buf,18,0,BUS_I2C_SEND_FOREGROUND);
if(resp!=RET_SUCCESS){
//report error
//report_error(ERR_LEV_ERROR,LEDL_ERR_SRC_SUBSYSTEM,LEDL_ERR_SUB_STAT_TX,resp);
//turn on error LED
ERR_LED_on();
}else{
LEDL_STAT_LED_toggle();
}
//send clyde_status
puts("Sending clyde_status\r\n");
//get clyde_status data
//setup packet
ptr=BUS_cmd_init(buf,CMD_EPS_STAT);
//fill in telemitry data
for(i=0;i<sizeof(LEDL_STAT);i++){
ptr[i]=((unsigned char*)(&clyde_status))[i];
}
//wait to avoid conflicts
ctl_timeout_wait(ctl_get_current_time()+50);
//send command
resp=BUS_cmd_tx(BUS_ADDR_CDH,buf,sizeof(CLYDE_STAT),0,BUS_I2C_SEND_FOREGROUND);
if(resp!=RET_SUCCESS){
//report error
//report_error(ERR_LEV_ERROR,LEDL_ERR_SRC_SUBSYSTEM,LEDL_ERR_SUB_STAT_TX,resp);
//turn on error LED
ERR_LED_on();
}else{
CLYDE_STAT_LED_toggle();
}
clyde_take_data(clyde_data);
Temp_I2C_sensor(temp_measure);
//ledl_status.gyro_data[0]=arr[0];
//ledl_status.gyro_data[1]=arr[1];
//ledl_status.gyro_data[2]=arr[2];
for (i=0;i<7;i++)
{//convert temperature to 8 bit value, do we want to lose the quarter degree resolution
}
//get how many blocks have been written to the SD card
//get buffer, set a timeout of 2 secconds
//get buffer, set a timeout of 2 secconds
buffer=BUS_get_buffer(CTL_TIMEOUT_DELAY,2048);
launch_detect_data=(LEDL_TEST_LAUNCH*)buffer;
//check for error
if(buffer==NULL){
printf("Error : Timeout while waiting for buffer.\r\n");
//TODO: SPECIFY A ERROR FUNCTIION FOR THIS
//return -1;
}else{
//read from SD card
resp=mmcReadBlock(SD_BECON_DATA ,(unsigned char*)buffer);
//store the last SDcard location that is stored in memory, this will either be by the end of logging data when Clyde turns on, or if ledl resets if
//it has decided that logging data has happened or if ledl has had a restart
ledl_status.SDaddress=launch_detect_data->dat.detect_dat.SD_last_address;
//print response from SD card
printf("%s\r\n",SD_error_str(resp));
BUS_free_buffer();
}
//if we get ride of the quarter degree resolution than the structure has to be changed to support 8 bit value, or be clever. 2 data in one slot...
for(i=0;i<7;i++){
ledl_status.temp_data[i]=temp_measure[i];
}
for(i=0;i<2;i++)
{
ledl_status.clyde_array_two_int[i]=clyde_data[i+15];
ledl_status.clyde_array_two_char[i]=clyde_data[i+17];//need to verify this
}
ledl_status.clyde_ttc=clyde_data[21];//need to verify this
for(i=0;i<15;i++)
{
clyde_status.clyde_array_fifteen[i]=clyde_data[i];
}
//free alarm so that we can reset it
BUS_free_alarm(BUS_ALARM_0);
//set alarm to trigger 100s from now
BUS_set_alarm(BUS_ALARM_0,get_ticker_time()+100ul*1024ul,&handle_get_I2C_data,LEDL_EV_STATUS_TIMEOUT);
}
if(e&SUB_EV_SPI_DAT){
puts("SPI data recived:\r\n");
//free buffer
BUS_free_buffer_from_event();
}
if(e&SUB_EV_SPI_ERR_CRC){
puts("SPI bad CRC\r\n");
}
if(e&SUB_EV_SPI_ERR_BUSY){
puts("SPI packet lost\r\n");
}
}
}