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.
47 * \author Daniele Basile <asterix@develer.com>
50 #include "hw/hw_dataflash.h"
51 #include "cfg/cfg_dataflash.h"
52 #include "cfg/cfg_proc.h"
55 #include <cfg/debug.h>
56 #include <cfg/module.h>
58 // Define logging setting (for cfg/log.h module).
59 #define LOG_LEVEL DATAFLASH_LOG_LEVEL
60 #define LOG_FORMAT DATAFLASH_LOG_FORMAT
61 #include <cfg/log.h> // for logging system
63 #include <drv/timer.h>
65 #include <drv/dataflash.h>
67 #include <kern/proc.h>
73 * Settings for dataflash test
77 // Datafalsh type memory to test (see drv/dataflash.h for supported memory types)
78 #define DATAFLASH_MEM_MODEL DFT_AT45DB642D
80 // Function to set CS, this is typically implement in hw/hw_dataflash.{c, h}
81 #define DATAFLASH_FUNC_CS_SET dataflash_hw_setCS
83 // Function to reset memery, this is typically implement in hw/hw_dataflash.{c, h}
84 #define DATAFLASH_FUNC_RESET NULL
86 // Buffer len to test dataflash
87 #define DATAFLASH_TEST_STR_LEN 12307
89 // If you want use a rand function of standard library set to 1.
90 #define DATAFLASH_USE_RAND_FUNC 0
94 * Kfile structure to test a dataflash.
97 static DataFlash dflash_fd;
100 * Define tmp buffer to stora data for
101 * write and read flash memory test.
103 static uint8_t test_buf[DATAFLASH_TEST_STR_LEN];
104 static uint8_t save_buf[DATAFLASH_TEST_STR_LEN];
107 * Setup all needed to test dataflash memory
110 int dataflash_testSetup(void)
113 LOG_INFO("KFILE setup..ok\n");
115 LOG_INFO("Check if kernel is enable (if enable you should see the assert message.)\n");
116 SILENT_ASSERT("bertos/drv/dataflash_test.c:119: Assertion failed: !CONFIG_KERN");
117 ASSERT(!CONFIG_KERN);
120 * This test use a kfile_test module,
121 * so should include source in your makefile.
123 MOD_CHECK(kfile_test);
126 LOG_INFO("Timer init..ok\n");
129 * Init SPI module and dataflash driver.
131 // Open SPI comunication channel
132 spimaster_init(&spi_fd, 0);
133 LOG_INFO("SPI0 init..ok\n");
135 ser_setbaudrate(&spi_fd, 5000000UL);
136 LOG_INFO("SPI0 set baudrate..ok\n");
138 //Init dataflash memory
140 LOG_INFO("DATAFLASH HW..ok\n");
142 if (dataflash_init(&dflash_fd, &spi_fd.fd, DATAFLASH_MEM_MODEL, DATAFLASH_FUNC_CS_SET, DATAFLASH_FUNC_RESET))
143 LOG_INFO("DATAFLASH init..ok\n");
145 LOG_ERR("DATAFLASH init..fail\n");
148 //Fill tmp buffer with rand chars.
149 for (int i = 0; i < DATAFLASH_TEST_STR_LEN; i++)
151 #if DATAFLASH_USE_RAND_FUNC
152 #include <stdlib.h> //Rand()
154 test_buf[i] = (uint8_t)rand();
156 test_buf[i] = (i & 0xff);
160 LOG_INFO("Fill tmp buff..ok\n");
167 * Run dataflash test memory
170 int dataflash_testRun(void)
172 LOG_INFO("Run KFILE test.\n");
174 SILENT_ASSERT("bertos/drv/dataflash.c:405: Assertion failed: fd->fd.seek_pos + size <= fd->fd.size");
175 if (kfile_testRunGeneric(&dflash_fd.fd, test_buf, save_buf, sizeof(test_buf)) != EOF)
177 LOG_INFO("KFILE test..ok\n");
181 LOG_ERR("KFILE test..fail!\n");
189 * End a dataflash Test.
192 int dataflash_testTearDown(void)
201 * Look it as exmple, or use it if
202 * you want test a data flash driver stand alone.
214 if (!dataflash_testSetup())
216 LOG_INFO("DATAFLASH setup..ok\n");
220 LOG_ERR("DATAFLASH setup..fail!\n");