-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pcon.c
230 lines (216 loc) · 7.66 KB
/
Pcon.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
/**
* This is the main pc program file.
*/
#include <stdio.h>
#include <propeller.h>
#include <unistd.h>
#include "Pcon.h"
#include "channel.h"
#include "schedule.h"
#include "char_fsm.h"
/*************************** drivers ********************************/
extern _Driver _FullDuplexSerialDriver;
extern _Driver _FileDriver;
_Driver *_driverlist[] = {
&_FullDuplexSerialDriver,
&_FileDriver,
NULL
};
/***************************** external ******************************/
// extern char *day_names[];
extern uint32_t bbb[_SCHEDULE_BUFFER];
/***************************** globals ******************************/
int char_state, cmd_state; //current state
char input_buffer[_INPUT_BUFFER], *input_buffer_ptr;
char file_set_prefix[_SCHEDULE_NAME_SIZE];
/***************** global code to text conversion ********************/
char *day_names_long[7] = {
"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
char *day_names_short[7] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
char *onoff[2] = {"off"," on"};
char *con_mode[3] = {"manual"," time","time & sensor"};
char *sch_mode[2] = {"day","week"};
/**************** real time clock cog stuff **************************/
/* beginning and ending addresses of the code for the rtc cog */
extern unsigned int _load_start_rtc_cog[];
extern unsigned int _load_stop_rtc_cog[];
/* allocate control block & stack for rtc cog */
struct {
unsigned stack[_STACK_SIZE_RTC];
volatile RTC_CB rtc;
} rtc_cb;
/* start rtc cog */
int start_rtc(volatile void *parptr)
{
int size = (_load_stop_rtc_cog - _load_start_rtc_cog)*4;//code size in bytes
printf("rtc cog code size %i bytes\n",size);
unsigned int code[size]; //allocate enough HUB to hold the COG code
memcpy(code, _load_start_rtc_cog, size); //assume xmmc
return cognew(code, parptr);
}
//**************** digital IO board cog stuff ************************/
/* begining and ending address of the code for the dio cog */
extern unsigned int _load_start_dio_cog[];
extern unsigned int _load_stop_dio_cog[];
/* allocate control block & stack for dio cog */
struct {
unsigned stack[_STACK_SIZE_DIO];
volatile DIO_CB dio;
} dio_cb;
/* start dio cog */
int start_dio(volatile void *parptr)
{
int size = (_load_stop_dio_cog - _load_start_dio_cog)*4;
printf("dio cog code size %i bytes\n",size);
unsigned int code[size];
memcpy(code, _load_start_dio_cog, size); //assume xmmc
return cognew(code, parptr);
}
/******************************* support functions *********************/
int ckeck_abort(void)
{
if(rtc_cb.rtc.abt != 0)
{
printf("\n\n*** rtc cog issued an abort\n ACK missing - message number %i\n",rtc_cb.rtc.abt);
return 1;
}
if(dio_cb.dio.abt != 0)
{
printf("\n\n*** dio cog issued an abort\n message number %i\n",dio_cb.dio.abt);
return 1;
}
return 0;
}
int sd_setup(void)
{
if(init_channel_data()) //create a channel file if one is not present
{
printf("**** init_channel_data aborted application ***\n");
return 1;
}
if(load_channel_data()) //load data from channel file into cca
{
printf("**** load_channel_data aborted application ****\n");
return 1;
}
if(init_sch(bbb)) //create a schedule file if it is not present
{
printf("**** init_sch aborted application ****\n");
return 1;
}
if(read_sch(bbb))
{
printf("**** read_sch aborted application ****\n");
return 1;
}
return 0;
}
void disp_sys(void)
{
/* display system info on serial terminal */
printf("\n*** Pcon %i.%i ***\n\n",_major_version,_minor_version);
#if _DRIVEN == _DIOB
printf("system is configured to drive a Parallax Digital IO Board\n");
#else
printf("system is configured to drive 5 IO pins\n");
#endif
return;
}
/********************************************************************/
/************************** start main *****************************/
/********************************************************************/
int main(void)
{
static int cog;
static char tbuf[_TOKEN_BUFFER];
char c;
/************************ initializations ****************************/
sleep(1); //wait for the serial terminal to start
/* display system info on serial terminal */
printf("\n*** Pcon %i.%i ***\n\n",_major_version,_minor_version);
#if _DRIVEN == _DIOB
printf("system is configured to drive a Parallax Digital IO Board\n");
#else
printf("system is configured to drive 5 IO pins\n");
#endif
/* build file set prefix */
strcat(file_set_prefix,_F_PREFIX);
strcat(file_set_prefix,_FILE_SET_ID);
printf("file set prefix <%s>\n",file_set_prefix);
/* check out the SD card */
if(sd_setup())
{
printf("**** sd_setup aborted application ****\n");
return 1;
}
/* start monitoring the real time clock DS3231 */
rtc_cb.rtc.tdb_lock = locknew();
lockclr(rtc_cb.rtc.tdb_lock);
cog = start_rtc(&rtc_cb.rtc);
if(cog == -1)
{
printf("** error attempting to start rtc cog\n cognew returned %i\n\n",cog);
return 1;
}
printf(" DS3231 monitored by code running on cog %i\n",cog);
/* setup the dio control block */
dio_cb.dio.tdb_lock = rtc_cb.rtc.tdb_lock;
dio_cb.dio.cca_lock = locknew();
lockclr(dio_cb.dio.cca_lock);
dio_cb.dio.sch_lock = locknew();
lockclr(dio_cb.dio.sch_lock);
dio_cb.dio.update_ptr = &(rtc_cb.rtc.update);
dio_cb.dio.td_ptr = &(rtc_cb.rtc.td_buffer);
*dio_cb.dio.update_ptr = 0;
dio_cb.dio.sch_ptr = bbb;
/* start the dio cog */
cog = start_dio(&dio_cb.dio);
if(cog == -1)
{
printf("** error attempting to start dio cog\n cognew returned %i\n\n",cog);
return 1;
}
#if _DRIVEN == _DIOB
printf(" DIO Board controlled by code running on cog %i\n",cog);
#else
printf(" relays controlled by code running on cog %i\n",cog);
#endif
/* set up unbuffered nonblocking io */
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
stdin->_flag &= ~_IOCOOKED;
stdin->_flag |= _IONONBLOCK;
/* initialize fsm(s) parameters */
input_buffer_ptr = input_buffer;//setup so actions routines can mess the buffer
cmd_state = 0; //set initial command parser state
char_state = 0; //set initial caracter parser state
// hold_day = -1; //force schedule load first time through the main loop
*input_buffer = ' '; //load a a blank into the buffer to force first prompt
process_buffer();
printf("initialization complete\n");
/************************************************************/
/********************** main processing loop ****************/
/************************************************************/
while(1)
{
/* check for problems */
if(ckeck_abort())
return 1;
/* check the token stack */
while(pop_cmd_q(tbuf))
{
cmd_fsm(tbuf,&cmd_state); //cycle cmd fsm until queue is empty
}
/* grab a character from the keyboard if one is present */
c = fgetc(stdin); //nonblocking read
/* process input char */
if(c!=_NO_CHAR)
{
fputc(c, stdout); // echo char
if(c==_CR) fputc(_CR, stdout); //second CR after uer input
char_fsm(char_type(c),&char_state,&c); //cycle fsm
}
};
printf("\nnormal termination\n\n");
return 0;
}