4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2008 Develer S.r.l. (http://www.develer.com/)
33 * \brief DataFlash test.
35 * This module test the dataflash memory among the supported memory (see drv/dataflash.h
37 * To test memory we fill one buffer with casual char, and write it in different
38 * part of memory. After every write we read the data that we have been write
39 * and compare this with test buffer, checking if write and read command work
40 * correclty. We also check if driver work properly when we make a write out the
41 * limit of memory size.
43 * Note: dataflash driver use a kfile interface, so for write/read test
44 * we use a kfile_test module that perform some generic test.
49 * \author Daniele Basile <asterix@develer.com>
52 #include "hw/hw_dataflash.h"
53 #include "cfg/cfg_dataflash.h"
54 #include <cfg/debug.h>
55 #include <cfg/module.h>
57 // Define logging setting (for cfg/log.h module).
58 #define LOG_LEVEL DATAFLASH_LOG_LEVEL
59 #define LOG_VERBOSITY DATAFLASH_LOG_VERBOSITY
60 #include <cfg/log.h> // for logging system
62 #include <drv/timer.h>
64 #include <drv/dataflash.h>
67 #include <kern/kfile.h>
72 * Settings for dataflash test
76 // Datafalsh type memory to test (see drv/dataflash.h for supported memory types)
77 #define DATAFLASH_MEM_MODEL DFT_AT45DB642D
79 // Function to set CS, this is typically implement in hw/hw_dataflash.{c, h}
80 #define DATAFLASH_FUNC_CS_SET &dataflash_hw_setCS
82 // Function to reset memery, this is typically implement in hw/hw_dataflash.{c, h}
83 #define DATAFLASH_FUNC_RESET NULL
85 // Buffer len to test dataflash
86 #define DATAFLASH_TEST_STR_LEN 12307
88 // If you want use a rand function of standard library set to 1.
89 #define DATAFLASH_USE_RAND_FUNC 1
93 * Kfile structure to test a dataflash.
95 static KFileSerial spi_fd;
96 static KFileDataflash dflash_fd;
99 * Define tmp buffer to stora data for
100 * write and read flash memory test.
102 static uint8_t test_buf[DATAFLASH_TEST_STR_LEN];
103 static uint8_t save_buf[DATAFLASH_TEST_STR_LEN];
106 * Setup all needed to test dataflash memory
109 int dataflash_testSetup(void)
112 * This test use a kfile_test module,
113 * so should include source in your makefile.
115 MOD_CHECK(kfile_test);
118 LOG_INFO("Timer init..ok\n");
121 * Init SPI module and dataflash driver.
123 // Open SPI comunication channel
124 spimaster_init(&spi_fd, SER_SPI0);
125 LOG_INFO("SPI0 init..ok\n");
127 ser_setbaudrate(&spi_fd, 5000000UL);
128 LOG_INFO("SPI0 set baudrate..ok\n");
130 //Init dataflash memory
132 LOG_INFO("DATAFLASH HW..ok\n");
134 if (dataflash_init(&dflash_fd, &spi_fd.fd, DATAFLASH_MEM_MODEL, DATAFLASH_FUNC_CS_SET, DATAFLASH_FUNC_RESET))
135 LOG_INFO("DATAFLASH init..ok\n");
137 LOG_ERR("DATAFLASH init..fail\n");
140 //Fill tmp buffer with rand chars.
141 for (int i = 0; i < DATAFLASH_TEST_STR_LEN; i++)
143 #if DATAFLASH_USE_RAND_FUNC
144 #include <stdlib.h> //Rand()
146 test_buf[i] = (uint8_t)rand();
148 test_buf[i] = (i & 0xff);
152 LOG_INFO("Fill tmp buff..ok\n");
159 * Run dataflash test memory
162 int dataflash_testRun(void)
164 if (!dataflash_testSetup())
166 LOG_INFO("DATAFLASH setup..ok\n");
170 LOG_ERR("DATAFLASH setup..fail!\n");
174 LOG_INFO("Run KFILE test.\n");
176 if (kfile_testRun(&dflash_fd.fd, test_buf, save_buf, sizeof(test_buf)))
178 LOG_INFO("KFILE test..ok\n");
182 LOG_ERR("KFILE test..fail!\n");
190 * End a dataflash Test.
193 int dataflash_testTearDown(void)
202 * Look it as exmple, or use it if
203 * you want test a data flash driver stand alone.