90f24ecbaab817fcc0343be174556284d4ae869d
[bertos.git] / bertos / drv / dataflash_test.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  *
49  * \author Daniele Basile <asterix@develer.com>
50  */
51
52 #include "hw/hw_dataflash.h"
53 #include "cfg/cfg_dataflash.h"
54 #include "cfg/cfg_kern.h"
55
56 #include <cfg/test.h>
57 #include <cfg/debug.h>
58 #include <cfg/module.h>
59
60 // Define logging setting (for cfg/log.h module).
61 #define LOG_LEVEL         DATAFLASH_LOG_LEVEL
62 #define LOG_VERBOSITY     DATAFLASH_LOG_VERBOSITY
63 #include <cfg/log.h>   // for logging system
64
65 #include <drv/timer.h>
66 #include <drv/ser.h>
67 #include <drv/dataflash.h>
68
69 #include <kern/proc.h>
70 #include <kern/kfile.h>
71
72 #include <string.h>
73
74 /*
75  * Settings for dataflash test
76  *
77  * \{
78  */
79 // Datafalsh type memory to test (see drv/dataflash.h for supported memory types)
80 #define DATAFLASH_MEM_MODEL            DFT_AT45DB642D
81
82 // Function to set CS, this is typically implement in hw/hw_dataflash.{c, h}
83 #define DATAFLASH_FUNC_CS_SET     &dataflash_hw_setCS
84
85 // Function to reset memery, this is typically implement in hw/hw_dataflash.{c, h}
86 #define DATAFLASH_FUNC_RESET                     NULL
87
88 // Buffer len to test dataflash
89 #define DATAFLASH_TEST_STR_LEN                  12307
90
91 // If you want use a rand function of standard library set to 1.
92 #define DATAFLASH_USE_RAND_FUNC                     0
93 /* \} */
94
95 /*
96  * Kfile structure to test a dataflash.
97  */
98 static Serial spi_fd;
99 static DataFlash dflash_fd;
100
101 /*
102  * Define tmp buffer to stora data for
103  * write and read flash memory test.
104  */
105 static uint8_t test_buf[DATAFLASH_TEST_STR_LEN];
106 static uint8_t save_buf[DATAFLASH_TEST_STR_LEN];
107
108 /**
109  * Setup all needed to test dataflash memory
110  *
111  */
112 int dataflash_testSetUp(void)
113 {
114         kfile_testSetUp();
115         LOG_INFO("KFILE setup..ok\n");
116
117         LOG_INFO("Check if kernel is enable (if enable you should see the assert message.)\n");
118         SILENT_ASSERT("bertos/drv/dataflash_test.c:119: Assertion failed: !CONFIG_KERNEL");
119         ASSERT(!CONFIG_KERNEL);
120
121         /*
122          * This test use a kfile_test module,
123          * so should include source in your makefile.
124          */
125         MOD_CHECK(kfile_test);
126
127         timer_init();
128         LOG_INFO("Timer init..ok\n");
129
130         /*
131          * Init SPI module and dataflash driver.
132          */
133         // Open SPI comunication channel
134         spimaster_init(&spi_fd, CONFIG_SPI_PORT);
135         LOG_INFO("SPI0 init..ok\n");
136
137         ser_setbaudrate(&spi_fd, CONFIG_SPI_BAUDRATE);
138         LOG_INFO("SPI0 set baudrate..ok\n");
139
140         //Init dataflash memory
141         dataflash_hw_init();
142         LOG_INFO("DATAFLASH HW..ok\n");
143
144         if (dataflash_init(&dflash_fd, &spi_fd.fd, DATAFLASH_MEM_MODEL, DATAFLASH_FUNC_CS_SET, DATAFLASH_FUNC_RESET))
145                 LOG_INFO("DATAFLASH init..ok\n");
146         else
147                 LOG_ERR("DATAFLASH init..fail\n");
148
149
150         //Fill tmp buffer with rand chars.
151         for (int i = 0; i < DATAFLASH_TEST_STR_LEN; i++)
152         {
153                 #if DATAFLASH_USE_RAND_FUNC
154                         #include <stdlib.h> //Rand()
155
156                         test_buf[i] = (uint8_t)rand();
157                 #else
158                         test_buf[i] = (i & 0xff);
159                 #endif
160         }
161
162         LOG_INFO("Fill tmp buff..ok\n");
163
164         return 0;
165 }
166
167
168 /**
169  * Run dataflash test memory
170  *
171  */
172 int dataflash_testRun(void)
173 {
174         LOG_INFO("Run KFILE test.\n");
175
176         SILENT_ASSERT("bertos/drv/dataflash.c:405: Assertion failed: fd->fd.seek_pos + size <= fd->fd.size");
177         if (kfile_testRun(&dflash_fd.fd, test_buf, save_buf, sizeof(test_buf)) != EOF)
178         {
179                 LOG_INFO("KFILE test..ok\n");
180         }
181         else
182         {
183                 LOG_ERR("KFILE test..fail!\n");
184                 return EOF;
185         }
186
187         return 0;
188 }
189
190 /**
191  * End a dataflash Test.
192  * (Unused)
193  */
194 int dataflash_testTearDown(void)
195 {
196         /*    */
197         return 0;
198 }
199
200 /*
201  * Empty main.
202  *
203  * Look it as exmple, or use it if
204  * you want test a data flash driver stand alone.
205  */
206 #if 0
207 int main(void)
208 {
209         IRQ_ENABLE;
210         kdbg_init();
211
212         #if CONFIG_KERNEL
213                 proc_init();
214         #endif
215
216         if (!dataflash_testSetUp())
217         {
218                 LOG_INFO("DATAFLASH setup..ok\n");
219         }
220         else
221         {
222                 LOG_ERR("DATAFLASH setup..fail!\n");
223                 return EOF;
224         }
225
226                 dataflash_testRun();
227
228         for(;;)
229         {
230         }
231
232 }
233 #endif