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.
48 * \author Daniele Basile <asterix@develer.com>
51 #include "hw/hw_dataflash.h"
52 #include "cfg/cfg_dataflash.h"
53 #include "cfg/cfg_proc.h"
56 #include <cfg/debug.h>
57 #include <cfg/module.h>
59 // Define logging setting (for cfg/log.h module).
60 #define LOG_LEVEL DATAFLASH_LOG_LEVEL
61 #define LOG_FORMAT DATAFLASH_LOG_FORMAT
62 #include <cfg/log.h> // for logging system
64 #include <drv/timer.h>
66 #include <drv/dataflash.h>
68 #include <kern/proc.h>
69 #include <kern/kfile.h>
74 * Settings for dataflash test
78 // Datafalsh type memory to test (see drv/dataflash.h for supported memory types)
79 #define DATAFLASH_MEM_MODEL DFT_AT45DB642D
81 // Function to set CS, this is typically implement in hw/hw_dataflash.{c, h}
82 #define DATAFLASH_FUNC_CS_SET dataflash_hw_setCS
84 // Function to reset memery, this is typically implement in hw/hw_dataflash.{c, h}
85 #define DATAFLASH_FUNC_RESET NULL
87 // Buffer len to test dataflash
88 #define DATAFLASH_TEST_STR_LEN 12307
90 // If you want use a rand function of standard library set to 1.
91 #define DATAFLASH_USE_RAND_FUNC 0
95 * Kfile structure to test a dataflash.
98 static DataFlash dflash_fd;
101 * Define tmp buffer to stora data for
102 * write and read flash memory test.
104 static uint8_t test_buf[DATAFLASH_TEST_STR_LEN];
105 static uint8_t save_buf[DATAFLASH_TEST_STR_LEN];
108 * Setup all needed to test dataflash memory
111 int dataflash_testSetup(void)
114 LOG_INFO("KFILE setup..ok\n");
116 LOG_INFO("Check if kernel is enable (if enable you should see the assert message.)\n");
117 SILENT_ASSERT("bertos/drv/dataflash_test.c:119: Assertion failed: !CONFIG_KERN");
118 ASSERT(!CONFIG_KERN);
121 * This test use a kfile_test module,
122 * so should include source in your makefile.
124 MOD_CHECK(kfile_test);
127 LOG_INFO("Timer init..ok\n");
130 * Init SPI module and dataflash driver.
132 // Open SPI comunication channel
133 spimaster_init(&spi_fd, 0);
134 LOG_INFO("SPI0 init..ok\n");
136 ser_setbaudrate(&spi_fd, 5000000UL);
137 LOG_INFO("SPI0 set baudrate..ok\n");
139 //Init dataflash memory
141 LOG_INFO("DATAFLASH HW..ok\n");
143 if (dataflash_init(&dflash_fd, &spi_fd.fd, DATAFLASH_MEM_MODEL, DATAFLASH_FUNC_CS_SET, DATAFLASH_FUNC_RESET))
144 LOG_INFO("DATAFLASH init..ok\n");
146 LOG_ERR("DATAFLASH init..fail\n");
149 //Fill tmp buffer with rand chars.
150 for (int i = 0; i < DATAFLASH_TEST_STR_LEN; i++)
152 #if DATAFLASH_USE_RAND_FUNC
153 #include <stdlib.h> //Rand()
155 test_buf[i] = (uint8_t)rand();
157 test_buf[i] = (i & 0xff);
161 LOG_INFO("Fill tmp buff..ok\n");
168 * Run dataflash test memory
171 int dataflash_testRun(void)
173 LOG_INFO("Run KFILE test.\n");
175 SILENT_ASSERT("bertos/drv/dataflash.c:405: Assertion failed: fd->fd.seek_pos + size <= fd->fd.size");
176 if (kfile_testRunGeneric(&dflash_fd.fd, test_buf, save_buf, sizeof(test_buf)) != EOF)
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.
215 if (!dataflash_testSetup())
217 LOG_INFO("DATAFLASH setup..ok\n");
221 LOG_ERR("DATAFLASH setup..fail!\n");