-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
300 lines (252 loc) · 6.1 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
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
294
295
296
297
298
299
300
/* ***********************************************************************
** Log wireless heart rate signal to SD/MMC card
** Copyright (C) 2009 Michael Spiceland
*************************************************************************
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation,
** Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**
*************************************************************************/
#define F_CPU 8000000
#define BAUD_RATE 4800 // desired baud rate
#define UBRR_DATA (F_CPU/(BAUD_RATE)-1)/16 // sets baud rate
#define HR_FACTOR F_CPU/256.0*60.0
#define ALL_INPUT 0x00 // 0000 0000
#define ALL_OUTPUT 0xFF // 1111 1111
#define LED_ON(x) (PORTC &= ~(1 << x))
#define LED_OFF(x) (PORTC |= (1 << x))
#include <avr/io.h>
#include <avr/interrupt.h>
#include <inttypes.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <util/delay.h>
#include "mmc_if.h"
#include "tff.h"
FATFS fatfs;
FIL logfile;
#define BUFFER_SIZE 32
volatile uint8_t readptr = 0;
volatile uint8_t writeptr = 0;
volatile uint16_t hr_buffer[BUFFER_SIZE];
inline void beep(void)
{
PORTD |= (1 << 7); // start beep
_delay_ms(100);
PORTD &= ~(1 << 7); // clear beep
}
void send_serial(unsigned char byte)
{
while (!(UCSR0A & (1 << UDRE0)));
UDR0 = byte;
}
void init_serial(void)
{
UCSR0B = _BV(RXEN0) | _BV(TXEN0) | (1 << RXCIE0);
UBRR0L = UBRR_DATA;
}
/***************************************************************************
* double2string
* convert a double to a string and place it in a pre-allocated space
* note: we only need this to be efficient with code space
***************************************************************************/
inline void double2string (double actualTemp, uint8_t* string)
{
int temp;
/* prep the string */
string[4] = '\0';
temp = (int16_t)(actualTemp); // to include decimal point for display
string[2] = ((uint8_t)(temp % 10)) | 0x30;
temp = temp / 10;
string[1] = ((uint8_t)(temp % 10)) | 0x30;
temp = temp / 10;
string[0] = ((uint8_t)(temp % 10)) | 0x30;
temp = temp / 10;
if ('0' == string[0])
{
string[0] = ' ';
if ('0' == string[1])
string[1] = ' ';
}
if (('9' == string[0]) && ('0' == string[1]) && ('8' == string[2]))
{
string[0] = 'E';
string[1] = 'R';
string[2] = 'R';
}
}
/* Empty on purpose for now */
SIGNAL(SIG_USART_RECV)
{
}
SIGNAL(SIG_OVERFLOW0)
{
static uint16_t count = 0;
count++;
if (count % 31)
{
return;
}
if (~PINC & (1 << 4)) // if LED_ON(4)
{
hr_buffer[writeptr] = 27;
writeptr = (writeptr + 1) % BUFFER_SIZE;
beep();
}
}
SIGNAL(SIG_OVERFLOW1)
{
LED_ON(4);
}
SIGNAL(SIG_INTERRUPT1)
{
uint16_t hr;
/* measure heartbeat */
hr = TCNT1L;
hr |= (TCNT1H << 8);
hr_buffer[writeptr] = hr;
writeptr = (writeptr + 1) % BUFFER_SIZE;
/* reset things */
TCNT1H = 0;
TCNT1L = 0;
TCNT0 = 0; // reset status checker
LED_OFF(4);
}
inline double sample2heartrate (uint16_t sample)
{
return (HR_FACTOR / (double)sample);
}
int main(void)
{
int i;
FRESULT res;
uint16_t bytes_written;
uint8_t tmp_string[] = "xxx\n";
char filename[] = "heartlog001.txt";
uint8_t fc = 0; // used for file name inc
double hr;
DDRC = ALL_OUTPUT;
DDRD &= ~(1 << 3); // INT1
DDRD |= (1 << 7); // buzzer
/* 8-bit timer for error detection */
TCCR0B |= _BV(CS02) | _BV(CS00); // CLK / 1024
TCNT0 = 0; // reset the timer
TIMSK0 |= _BV(TOIE0); // interrupt on overflow
/* set up external interrupts */
EICRA |= _BV(ISC10) | _BV(ISC11); // interrupt on rising edge of INT1
EIMSK |= _BV(INT1); // enable int1 interrupts
/* 16-bit timer for the PWM out */
TCCR1B |= _BV(CS12); // clk/256
TCNT1H = 0;
TCNT1L = 0;
TIMSK1 |= _BV(TOIE1); // interrupt on overflow
/* 8-bit timer for about 8khz timer */
LED_OFF(1); // power
LED_ON(2); // sd status
LED_ON(3); // hr
LED_ON(4); // hr error
LED_ON(1);
// init the uart
init_serial();
// setup callbacks for stdio use
fdevopen(send_serial, NULL);
// say hello
printf("\n\r\n\r** heart rate logger **\n\r");
// init mmc card and report status
//printf("mmc_init returns %d\n\r", i = mmc_init() );
i = mmc_init();
if (i)
{
LED_ON(2);
}
else
{
LED_OFF(2);
}
if (f_mount(0, &fatfs))
{
LED_ON(2);
printf ("mount failed\n\r");
}
else
{
//LED_ON(2);
printf ("mount successfull\n\r");
}
res = FR_NO_FILE;
while ((FR_OK != res) && (fc < 250)) // we only try 250 times
{
snprintf(filename, sizeof(filename), "hlog%03d.txt", fc++);
printf("res = %d trying %s\n\r", res, filename);
res = f_open(&logfile, filename, FA_CREATE_NEW | FA_WRITE);
}
if (res)
{
printf("file open failed\n\r");
LED_ON(2);
cli();
}
else
{
printf("file opened succesfully\n\r");
sei();
}
//sei();
while (1)
{
/* pull value out of buffer */
while (writeptr == readptr); // block waiting
hr = sample2heartrate(hr_buffer[readptr]);
double2string(hr, tmp_string);
if ((hr < 40) || (hr > 180)) // invalid hr
{
beep();
}
readptr = (readptr + 1) % BUFFER_SIZE;
printf("%s\n\r", tmp_string);
if (PIND & (1 << 3))
{
LED_ON(3);
}
else
{
LED_OFF(3);
}
tmp_string[3] = '\n';
if ((f_write(&logfile, tmp_string, 4, &bytes_written) == FR_OK)
&& (4 == bytes_written))
{
LED_OFF(2);
}
else
{
LED_ON(2);
cli(); // disable interrupts
printf("Oops, only wrote %d bytes\n\r", bytes_written);
}
/* sync the data in case we loose power */
if (f_sync(&logfile) == FR_OK)
{
LED_OFF(2);
}
else
{
LED_ON(2);
cli(); // disable interrupts
printf("error syncing to disk.\n\r");
}
}
f_mount(0, NULL);
}