fa5f480eea2cae9dc4de6beb3f5ffa726ca4b18b
[bertos.git] / bertos / drv / dataflash_hwtest.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2008 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief DataFlash test.
34  *
35  * This module test the dataflash memory among the supported memory (see drv/dataflash.h
36  * for more detail).
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.
42  *
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.
45  *
46  *
47  * \version $Id$
48  * \author Daniele Basile <asterix@develer.com>
49  */
50
51 #include "hw/hw_dataflash.h"
52 #include "cfg/cfg_dataflash.h"
53 #include "cfg/cfg_proc.h"
54
55 #include <cfg/test.h>
56 #include <cfg/debug.h>
57 #include <cfg/module.h>
58
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
63
64 #include <drv/timer.h>
65 #include <drv/ser.h>
66 #include <drv/dataflash.h>
67
68 #include <kern/proc.h>
69 #include <kern/kfile.h>
70
71 #include <string.h>
72
73 /*
74  * Settings for dataflash test
75  *
76  * \{
77  */
78 // Datafalsh type memory to test (see drv/dataflash.h for supported memory types)
79 #define DATAFLASH_MEM_MODEL            DFT_AT45DB642D
80
81 // Function to set CS, this is typically implement in hw/hw_dataflash.{c, h}
82 #define DATAFLASH_FUNC_CS_SET      dataflash_hw_setCS
83
84 // Function to reset memery, this is typically implement in hw/hw_dataflash.{c, h}
85 #define DATAFLASH_FUNC_RESET                     NULL
86
87 // Buffer len to test dataflash
88 #define DATAFLASH_TEST_STR_LEN                  12307
89
90 // If you want use a rand function of standard library set to 1.
91 #define DATAFLASH_USE_RAND_FUNC                     0
92 /* \} */
93
94 /*
95  * Kfile structure to test a dataflash.
96  */
97 static Serial spi_fd;
98 static DataFlash dflash_fd;
99
100 /*
101  * Define tmp buffer to stora data for
102  * write and read flash memory test.
103  */
104 static uint8_t test_buf[DATAFLASH_TEST_STR_LEN];
105 static uint8_t save_buf[DATAFLASH_TEST_STR_LEN];
106
107 /**
108  * Setup all needed to test dataflash memory
109  *
110  */
111 int dataflash_testSetup(void)
112 {
113         kfile_testSetup();
114         LOG_INFO("KFILE setup..ok\n");
115
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);
119
120         /*
121          * This test use a kfile_test module,
122          * so should include source in your makefile.
123          */
124         MOD_CHECK(kfile_test);
125
126         timer_init();
127         LOG_INFO("Timer init..ok\n");
128
129         /*
130          * Init SPI module and dataflash driver.
131          */
132         // Open SPI comunication channel
133         spimaster_init(&spi_fd, 0);
134         LOG_INFO("SPI0 init..ok\n");
135
136         ser_setbaudrate(&spi_fd, 5000000UL);
137         LOG_INFO("SPI0 set baudrate..ok\n");
138
139         //Init dataflash memory
140         dataflash_hw_init();
141         LOG_INFO("DATAFLASH HW..ok\n");
142
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");
145         else
146                 LOG_ERR("DATAFLASH init..fail\n");
147
148
149         //Fill tmp buffer with rand chars.
150         for (int i = 0; i < DATAFLASH_TEST_STR_LEN; i++)
151         {
152                 #if DATAFLASH_USE_RAND_FUNC
153                         #include <stdlib.h> //Rand()
154
155                         test_buf[i] = (uint8_t)rand();
156                 #else
157                         test_buf[i] = (i & 0xff);
158                 #endif
159         }
160
161         LOG_INFO("Fill tmp buff..ok\n");
162
163         return 0;
164 }
165
166
167 /**
168  * Run dataflash test memory
169  *
170  */
171 int dataflash_testRun(void)
172 {
173         LOG_INFO("Run KFILE test.\n");
174
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)
177         {
178                 LOG_INFO("KFILE test..ok\n");
179         }
180         else
181         {
182                 LOG_ERR("KFILE test..fail!\n");
183                 return EOF;
184         }
185
186         return 0;
187 }
188
189 /**
190  * End a dataflash Test.
191  * (Unused)
192  */
193 int dataflash_testTearDown(void)
194 {
195         /*    */
196         return 0;
197 }
198
199 /*
200  * Empty main.
201  *
202  * Look it as exmple, or use it if
203  * you want test a data flash driver stand alone.
204  */
205 #if 0
206 int main(void)
207 {
208         IRQ_ENABLE;
209         kdbg_init();
210
211         #if CONFIG_KERN
212         proc_init();
213     #endif
214
215         if (!dataflash_testSetup())
216         {
217                         LOG_INFO("DATAFLASH setup..ok\n");
218         }
219         else
220         {
221                         LOG_ERR("DATAFLASH setup..fail!\n");
222                         return EOF;
223         }
224
225         dataflash_testRun();
226
227         for(;;)
228         {
229         }
230 }
231 #endif