00001
00063 #include "sceptre.h"
00064
00065 #include <time.h>
00066 #include <string.h>
00067 #include <stdio.h>
00068 #include "..\core\filesystem\sdcard_driver.h"
00069 #include "..\core\filesystem\sdcard.h"
00070 #include "..\core\thermometer\ds18b20.h"
00071
00072
00073 const char *p_hello = "Sceptre rulez!\n";
00074 const char *p_version = "V2010.01.31\n\n";
00075 const char *p_name1 = "a_log.txt";
00076 const char *p_name2 = "t_log.txt";
00077 const char *p_bluetooth_test = "test_bt.pls";
00078 rtc_time_t rtc;
00079 char bt_name[16+1];
00080
00081
00082 void main_send_string(const char *p_str);
00083 void main_10ms_timer(void);
00084 FIL *main_file_open(FIL *p_file, const char *p_name, uint8_t flags);
00085 bool_t main_file_exist(const char *p_name);
00086 void main_log_data(const char *p_name_1, const char *p_name_2);
00087
00088
00089 #define LED_PIN 30
00090 #define LED_ON IOSET1 = (1<<LED_PIN)
00091 #define LED_OFF IOCLR1 = (1<<LED_PIN)
00092 void led_init(void);
00093 void led_tick(void);
00094
00095 void led_init(void)
00096 {
00097 IODIR1 |= (1<<LED_PIN);
00098 LED_OFF;
00099 }
00100
00101 void led_tick(void)
00102 {
00103 static int ms = 0;
00104
00105 ms += 10;
00106 if (ms>=500)
00107 {
00108 ms = 0;
00109 if ((IOPIN1&(1<<LED_PIN))==0) LED_ON;
00110 else LED_OFF;
00111 }
00112 }
00113
00114
00115 void main_send_string(const char *p_str)
00116 {
00117 uart0_send((const uint8_t *)p_str,strlen(p_str),true);
00118 }
00119
00120
00121 void main_10ms_timer(void)
00122 {
00123 disk_timerproc();
00124 led_tick();
00125 }
00126
00127
00128 FIL *main_file_open(FIL *p_file, const char *p_name, uint8_t flags)
00129 {
00130 if (f_open(p_file,p_name,flags)!=FR_OK)
00131 {
00133 printf("Could not open file \"%s\"\n",p_name);
00134 return NULL;
00135 }
00136 return p_file;
00137 }
00138
00139
00141 bool_t main_file_exist(const char *p_name)
00142 {
00143 FIL file;
00144 if (f_open(&file,p_name,FA_OPEN_EXISTING|FA_READ)!=FR_OK) return false;
00145 unsigned int bytes_read = 0;
00146 f_read(&file,bt_name,16,&bytes_read);
00147 bt_name[MIN(16,bytes_read)] = 0;
00148 f_close(&file);
00149 return true;
00150 }
00151
00152
00153 uint8_t Buffer[1024] __attribute__ ((aligned (4))) ;
00154 void put_dump(FIL *p_file, const uint8_t *p_data, uint32_t ofs, int cnt);
00155 void main_sys_info(char *p_name);
00156 void main_disk_info(char *p_name);
00157
00158 void put_dump(FIL *p_file, const uint8_t *p_data, uint32_t ofs, int cnt)
00159 {
00160 int i;
00161
00162 f_printf(p_file,"%08lX ",ofs);
00163 for (i=0; i<cnt; i++)
00164 {
00165 f_printf(p_file," %02X",p_data[i]);
00166 }
00167 f_putc(' ',p_file);
00168 for (i=0; i<cnt; i++)
00169 {
00170 if (p_data[i]<0x20 || p_data[i]>=0x7f) f_putc('.',p_file);
00171 else f_putc(p_data[i],p_file);
00172 }
00173 f_putc('\n',p_file);
00174 }
00175
00176
00177 void main_sys_info(char *p_name)
00178 {
00179 FIL file;
00180 if (main_file_open(&file,p_name,FA_OPEN_ALWAYS|FA_WRITE)==NULL) return;
00181
00182 uint8_t serial[8];
00183 ds18b20_read_serial(serial);
00184 int i;
00185 f_printf(&file,"1-wire serial number\t");
00186 for (i=0; i<8; i++)
00187 {
00188 f_printf(&file,"%02X",serial[i]);
00189 }
00190 f_printf(&file,"\n");
00191
00192 int vpbdiv = VPBDIV&0x00000003;
00193 int pllcfg = PLLCFG&0x0000007f;
00194 f_printf(&file,"VPBDIV\t%02X\n",vpbdiv);
00195 f_printf(&file,"PLLCFG\t%02X\n",pllcfg);
00196 int m = (pllcfg&0x1f) + 1;
00197 int p = (pllcfg>>5)&0x03;
00198 f_printf(&file,"Fcco\t%ld Hz (m=%d, p=%d)\n",Fosc*m*2*p,m,p);
00199
00200 long int fcclk = Fosc*m;
00201 f_printf(&file,"Fcclk\t%ld Hz\n",fcclk);
00202
00203 if (vpbdiv==0) f_printf(&file,"Fpclk\t%ld Hz\n",fcclk/4);
00204 else if (vpbdiv==1) f_printf(&file,"Fpclk\t%ld Hz\n",fcclk);
00205 else if (vpbdiv==2) f_printf(&file,"Fpclk\t%ld Hz\n",fcclk/2);
00206 else f_printf(&file,"Illegal VPBDIV value\n");
00207
00208 f_printf(&file,"MAMTIM\t%08X\n",MAMTIM);
00209 f_printf(&file,"MEMMAP\t%08X\n",MEMMAP);
00210 f_printf(&file,"CLOCKS_PER_SEC\t%d\n",CLOCKS_PER_SEC);
00211
00212 f_close(&file);
00213 printf("Wrote file \"%s\"\n",p_name);
00214 }
00215
00216
00217 void main_disk_info(char *p_name)
00218 {
00219 FIL file;
00220 if (main_file_open(&file,p_name,FA_OPEN_ALWAYS|FA_WRITE)==NULL) return;
00221
00222 long l;
00223 uint8_t b;
00224 uint16_t w;
00225
00226 if (disk_ioctl(0,GET_SECTOR_COUNT,&l)==RES_OK)
00227 {
00228 f_printf(&file,"Drive size: %lu sectors\n",l);
00229 }
00230 if (disk_ioctl(0,GET_SECTOR_SIZE,&w)==RES_OK)
00231 {
00232 f_printf(&file,"Sector size: %u\n",w);
00233 }
00234 if (disk_ioctl(0,GET_BLOCK_SIZE,&l)==RES_OK)
00235 {
00236 f_printf(&file,"Erase block size: %lu sectors\n",l);
00237 }
00238 if (disk_ioctl(0,MMC_GET_TYPE,&b)==RES_OK)
00239 {
00240 f_printf(&file,"MMC/SDC type: %u\n",b);
00241 }
00242 if (disk_ioctl(0,MMC_GET_CSD,Buffer)==RES_OK)
00243 {
00244 f_puts("CSD:\n",&file);
00245 put_dump(&file,Buffer, 0, 16);
00246 }
00247 if (disk_ioctl(0,MMC_GET_CID,Buffer)==RES_OK)
00248 {
00249 f_puts("CID:\n",&file);
00250 put_dump(&file,Buffer,0,16);
00251 }
00252 if (disk_ioctl(0,MMC_GET_OCR,Buffer)==RES_OK)
00253 {
00254 f_puts("OCR:\n",&file);
00255 put_dump(&file,Buffer,0,4);
00256 }
00257 if (disk_ioctl(0,MMC_GET_SDSTAT,Buffer)==RES_OK)
00258 {
00259 f_puts("SD Status:\n",&file);
00260 for (l=0; l<64; l+=16) put_dump(&file,Buffer+l,l,16);
00261 }
00262
00263 f_close(&file);
00264 printf("Wrote file \"%s\"\n",p_name);
00265 }
00266
00267
00268 void main_log_data(const char *p_name_1, const char *p_name_2)
00269 {
00270 FIL file1;
00271 FIL file2;
00272 FIL *p_file1 = main_file_open(&file1,p_name_1,FA_OPEN_ALWAYS|FA_WRITE);
00273 FIL *p_file2 = main_file_open(&file2,p_name_2,FA_OPEN_ALWAYS|FA_WRITE);
00274
00275 if (p_file1==NULL && p_file2==NULL)
00276 {
00277 printf("Could not open log files. Logging canceled.\n");
00278 return;
00279 }
00280
00281 int delay = 10;
00282 int samples = 0;
00283 uint32_t last_count = 0;
00284
00285 printf("Logging");
00286
00287 while (samples<300)
00288 {
00289 if (timer_get_10ms_counter()>=last_count+delay)
00290 {
00291 last_count = timer_get_10ms_counter();
00292
00293 if (samples%60==0) printf("\n");
00294 samples += 1;
00295 uart0_putc('.');
00296
00297 rtc_get_time(&rtc);
00298
00299 float acc[3];
00300 accelerometer_read(acc);
00301 if (p_file1!=NULL)
00302 {
00303 f_printf(p_file1,"%02d\tX=%d\tY=%d\tZ=%d\n",rtc.seconds,(int)(1000.0*acc[0]),(int)(1000.0*acc[1]),(int)(1000.0*acc[2]));
00304 }
00305
00306 float t = thermometer_read();
00307 if (p_file2!=NULL)
00308 {
00309 f_printf(p_file2,"%02d\tT=%d.%03d°C\n",rtc.seconds,(int)t,(int)((t-(int)t)*1000.0));
00310 }
00311 }
00312 }
00313
00314 f_close(&file1);
00315 f_close(&file2);
00316
00317 printf("\ndone.\n");
00318 }
00319
00320
00321 int main(void)
00322 {
00323
00324 target_init();
00325 irq_vic_init();
00326
00327
00328 timer_init(SYSTEM_TIMER,Fpclk/1000 - 1);
00329 timer_enable(SYSTEM_TIMER);
00330
00331
00332 rtc.seconds = 0;
00333 rtc.minutes = 0;
00334 rtc.hours = 0;
00335 rtc.day_of_month = 1;
00336 rtc.day_of_week = 4;
00337 rtc.day_of_year = 1;
00338 rtc.month = 1;
00339 rtc.year = 2010;
00340 rtc_init();
00341 rtc_set_time(&rtc);
00342 rtc_start();
00343
00344
00345 led_init();
00346 timer_set_10ms_hook(main_10ms_timer);
00347
00348
00349 uart0_init(115200,8,'n',1);
00350
00351
00353 device_init();
00354
00355 main_send_string(p_hello);
00356 main_send_string(p_version);
00357 timer_wait_ms(1500);
00358
00359
00360 printf("You can now spin, flip & rotate the Sceptre.\n");
00361 printf("During 30 seconds data will be logged to the SD card.\n");
00362 printf("Accelerometer data will be logged to \"%s\".\n",p_name1);
00363 printf("Thermometer data will be logged to \"%s\".\n",p_name2);
00364 printf("After logging a USB mass storage device will be started.\n");
00365 printf("Provide a file named \"%s\" to activate Bluetooth module.\n",p_bluetooth_test);
00366 printf("\n");
00367 timer_wait_ms(3500);
00368
00369
00370 if (thermometer_init()==false)
00371 {
00372 printf("thermometer_init failed.\n");
00373 }
00374
00375
00376 if (accelerometer_init()==false)
00377 {
00378 printf("accelerometer_init failed.\n");
00379 }
00380
00381
00382 FATFS fs;
00383 bool_t sd_wp = false;
00384 if (sdcard_detect()==true)
00385 {
00386 printf("SD card detected.\n");
00387 sd_wp = sdcard_write_protected();
00388 if (sd_wp==true)
00389 {
00390 printf("SD card is write protected.\n");
00391 printf("Logging will be skipped.\n");
00392 }
00393 if (disk_initialize(0)==0)
00394 {
00395
00396 f_mount(0,&fs);
00397 }
00398 else
00399 {
00400 printf("Could not initialize disk.\n");
00401 }
00402 }
00403
00404 if (sd_wp==false)
00405 {
00406 main_sys_info("sys_info.txt");
00407 main_disk_info("sd_info.txt");
00408 }
00409
00410 bool_t disconnect = true;
00411 bool_t has_bluetooth = main_file_exist(p_bluetooth_test);
00412 if (has_bluetooth==true)
00413 {
00414 printf("Initializing Bluetooth module...\n");
00415 bluetooth_init(19200);
00416 bluetooth_power(BLUETOOTH_ON);
00417 timer_wait_ms(5000);
00418 printf(" done\n");
00419 printf("Bluetooth waiting for connection (pin=1234).\n");
00420 printf("Friendly name is \"%s\"\n",bt_name);
00421 bluetooth_set_friendly_name(bt_name);
00422
00423 bluetooth_connect(true,"1234",0,100,true);
00424 printf("\n");
00425 }
00426 else
00427 {
00428 printf("Skipping Bluetooth test.\n");
00429 }
00430
00431 if (sd_wp==false) main_log_data(p_name1,p_name2);
00432
00433 printf("Starting USB mass storage device.\n");
00434 usb_mass_storage_init();
00435
00436 uint32_t last_count = 0;
00437
00438 while (1)
00439 {
00440 usb_mass_storage_tick();
00441
00442 if (timer_get_10ms_counter()>=last_count+100)
00443 {
00444 last_count = timer_get_10ms_counter();
00445 rtc_get_time(&rtc);
00446
00447 float acc[3];
00448 accelerometer_read(acc);
00449 float t = thermometer_read();
00450 printf("%02d:%02d:%02d\tT=%0.4f°C\tX=%f\tY=%f\tZ=%f\n",
00451 (int)rtc.hours,(int)rtc.minutes,(int)rtc.seconds,t,acc[0],acc[1],acc[2]);
00452
00453 if (has_bluetooth==true)
00454 {
00455 if (bluetooth_is_connected()==true)
00456 {
00457 uint32_t ch;
00458 ch = 0;
00459
00460 while (uart1_getc(&ch,0)==true)
00461 {
00462 uart0_putc(ch);
00463 }
00464 if (ch!=0) printf("\n");
00465
00466 while (uart0_getc(&ch,0)==true)
00467 {
00468 uart1_putc(ch,true);
00469 }
00470 disconnect = false;
00471 }
00472 else if (disconnect==false)
00473 {
00474 printf("Bluetooth disconnected\n");
00475 disconnect = true;
00476 }
00477 }
00478 }
00479 }
00480
00481 return 0;
00482 }