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