Adapted the simple SD test to the new driver and its interface.

This commit is contained in:
nvt-se 2009-09-22 15:37:18 +00:00
parent 12c6dbb7c6
commit f698ce2577

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2007 Swedish Institute of Computer Science.
* Copyright (c) 2009, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -28,77 +28,64 @@
*
* This file is part of the Contiki operating system.
*
* $Id: sd-test.c,v 1.4 2009/05/26 13:23:59 nvt-se Exp $
*
* \file
* A simple example of using the SD card on the MSB430 platform.
* \author
* Nicolas Tsiftes <nvt@sics.se>
* $Id: sd-test.c,v 1.5 2009/09/22 15:37:18 nvt-se Exp $
*/
/**
* \file
* Test for an SD driver.
* \author
* Nicolas Tsiftes <nvt@sics.se>
*/
#include "contiki.h"
#include "dev/sd/sd.h"
#include "dev/msb430-uart1.h"
#include "dev/sd.h"
#include "lib/random.h"
#include "net/rime.h"
#include <stdio.h>
#include <string.h>
#define BLOCK_SIZE 512UL
PROCESS(sd_test, "SD test process");
AUTOSTART_PROCESSES(&sd_test);
#define CALM_MODE 1
#define ALTER_OFFSET 0
/*---------------------------------------------------------------------------*/
PROCESS(test_sd_process, "SD test");
AUTOSTART_PROCESSES(&test_sd_process);
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(test_sd_process, ev, data)
PROCESS_THREAD(sd_test, event, data)
{
static char buf[BLOCK_SIZE];
#if CALM_MODE
static struct etimer et;
#endif
static int r;
static unsigned long iter;
static uint32_t offset;
static unsigned long offset;
char buf[SD_BLOCK_SIZE];
static struct etimer et;
int r, buflen;
PROCESS_BEGIN();
printf("starting the SD test\n");
etimer_set(&et, CLOCK_SECOND);
while(1) {
printf("\n\nIteration %lu\n", ++iter);
sprintf(buf, "Testing the SD memory #%lu.", iter);
#if CALM_MODE
etimer_set(&et, CLOCK_SECOND);
offset = 0;
for(iter = 1;; iter++) {
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
#else
PROCESS_PAUSE();
#endif
offset = BLOCK_SIZE;
#if ALTER_OFFSET
offset *= (random_rand() & 0xff);
#endif
r = sd_write_block(offset, &buf);
if(r != SD_WRITE_SUCCESS) {
printf("writing failed: %d\n", r);
memset(buf, 0, sizeof(buf));
buflen = sprintf(buf, "(%ld) Testing the SD card (%ld)", iter, iter);
if((iter & 7) == 0) {
offset = random_rand() & 0xffff;
} else {
offset += random_rand() & 0xff;
}
memset(buf, 0, sizeof (buf));
r = sd_read_block(&buf, offset);
r = sd_write(offset, buf, buflen + 1);
if(r > 0) {
printf("Read %d bytes\n", r);
buf[sizeof(buf) - 1] = '\0';
printf("Contents of the buffer: %s\n", buf);
} else {
printf("reading failed\n");
memset(buf, 0, sizeof(buf));
r = sd_read(offset, buf, buflen + 1);
if(r > 0) {
printf("read %s (offset %lu)\n", buf, offset);
} else {
printf("read error: %d (%s)\n", r, sd_error_string(r));
}
etimer_reset(&et);
}
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/