Add test module for dataflash. Add cfg and use log macros instead kprintf.
[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/debug.h>
55 #include <cfg/module.h>
56
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
61
62 #include <drv/timer.h>
63 #include <drv/ser.h>
64 #include <drv/dataflash.h>
65
66
67 #include <kern/kfile.h>
68
69 #include <string.h>
70
71 /*
72  * Settings for dataflash test
73  *
74  * \{
75  */
76 // Datafalsh type memory to test (see drv/dataflash.h for supported memory types)
77 #define DATAFLASH_MEM_MODEL            DFT_AT45DB642D
78
79 // Function to set CS, this is typically implement in hw/hw_dataflash.{c, h}
80 #define DATAFLASH_FUNC_CS_SET     &dataflash_hw_setCS
81
82 // Function to reset memery, this is typically implement in hw/hw_dataflash.{c, h}
83 #define DATAFLASH_FUNC_RESET                     NULL
84
85 // Buffer len to test dataflash
86 #define DATAFLASH_TEST_STR_LEN                  12307
87
88 // If you want use a rand function of standard library set to 1.
89 #define DATAFLASH_USE_RAND_FUNC                     1
90 /* \} */
91
92 /*
93  * Kfile structure to test a dataflash.
94  */
95 static KFileSerial spi_fd;
96 static KFileDataflash dflash_fd;
97
98 /*
99  * Define tmp buffer to stora data for
100  * write and read flash memory test.
101  */
102 static uint8_t test_buf[DATAFLASH_TEST_STR_LEN];
103 static uint8_t save_buf[DATAFLASH_TEST_STR_LEN];
104
105 /**
106  * Setup all needed to test dataflash memory
107  *
108  */
109 int dataflash_testSetup(void)
110 {
111         /*
112          * This test use a kfile_test module,
113          * so should include source in your makefile.
114          */
115         MOD_CHECK(kfile_test);
116
117         timer_init();
118         LOG_INFO("Timer init..ok\n");
119
120         /*
121          * Init SPI module and dataflash driver.
122          */
123         // Open SPI comunication channel
124         spimaster_init(&spi_fd, SER_SPI0);
125         LOG_INFO("SPI0 init..ok\n");
126
127         ser_setbaudrate(&spi_fd, 5000000UL);
128         LOG_INFO("SPI0 set baudrate..ok\n");
129
130         //Init dataflash memory
131         dataflash_hw_init();
132         LOG_INFO("DATAFLASH HW..ok\n");
133
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");
136         else
137                 LOG_ERR("DATAFLASH init..fail\n");
138
139
140         //Fill tmp buffer with rand chars.
141         for (int i = 0; i < DATAFLASH_TEST_STR_LEN; i++)
142         {
143                 #if DATAFLASH_USE_RAND_FUNC
144                         #include <stdlib.h> //Rand()
145
146                         test_buf[i] = (uint8_t)rand();
147                 #else
148                         test_buf[i] = (i & 0xff);
149                 #endif
150         }
151
152         LOG_INFO("Fill tmp buff..ok\n");
153
154         return 0;
155 }
156
157
158 /**
159  * Run dataflash test memory
160  *
161  */
162 int dataflash_testRun(void)
163 {
164         if (!dataflash_testSetup())
165         {
166                 LOG_INFO("DATAFLASH setup..ok\n");
167         }
168         else
169         {
170                 LOG_ERR("DATAFLASH setup..fail!\n");
171                 return EOF;
172         }
173
174         LOG_INFO("Run KFILE test.\n");
175
176         if (kfile_testRun(&dflash_fd.fd, test_buf, save_buf, sizeof(test_buf)))
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         dataflash_testRun();
212
213         for(;;)
214         {
215         }
216
217 }
218 #endif