comparison chipsetsw/drivers/drv_app/ffs/board/task.c @ 0:509db1a7b7b8

initial import: leo2moko-r1
author Space Falcon <falcon@ivan.Harhan.ORG>
date Mon, 01 Jun 2015 03:24:05 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:509db1a7b7b8
1 /******************************************************************************
2 * Flash File System (ffs)
3 * Idea, design and coding by Mads Meisner-Jensen, mmj@ti.com
4 *
5 * FFS task. ONLY for target!
6 *
7 * $Id: task.c 1.48.1.1.1.24 Thu, 18 Dec 2003 10:50:52 +0100 tsj $
8 *
9 ******************************************************************************/
10
11 #include "board.cfg"
12 #include "ffs/ffs.h"
13 #include "ffs/board/core.h"
14 #include "ffs/board/task.h"
15 #include "ffs/board/ffstrace.h"
16 #include "rvm/rvm_use_id_list.h"
17
18 /******************************************************************************
19 * Globals and function prototypes
20 ******************************************************************************/
21
22 extern UINT8 pcm_init(void);
23 void ffs_task(void);
24
25 static effs_t ffs_init_status;
26 req_id_t request_id_last = 0;
27
28 T_OS_MB_ID ffs_mb_id;
29 UINT16 ffs_addr_id;
30
31 extern struct ffs_blocking_s;
32
33 /******************************************************************************
34 * FFS Test Task
35 ******************************************************************************/
36
37 // For this to work, change:
38 // 1. MAX_RVF_TASKS define in rvf_target.h
39
40 // Note that the FFS_TEST_TASK_ID is set one too low! We assume we are lucky
41 // that no other task has the same ID...
42
43 #if (WITH_TFFS == 1)
44
45 #define FFS_TEST_STACK_SIZE 1024
46
47 #define FFS_TEST_TASK_ID (MAX_RVF_TASKS - 1 - 1)
48
49 void ffs_test_task(void);
50 void test_init(int keepgoing);
51 int test_run(char *testname);
52
53 static uint8 ffs_test_stack[FFS_TEST_STACK_SIZE];
54
55 // This is the string of test cases to run by calling test_run(). This
56 // string as written to from tmffs module.
57 char ffs_test_string[128];
58
59 // Delay for <delay> milliseconds
60 void tffs_delay(int delay)
61 {
62 delay = 14 * delay / 64; // approx. same as division by 60/13
63 OS_DELAY(delay);
64 }
65
66 UINT32 rvf_get_tick_count(void);
67 // Timer functions for benchmarking
68 UINT32 tffs_timer_begin(void)
69 {
70 return rvf_get_tick_count();
71 }
72
73 UINT32 tffs_timer_end(UINT32 time_begin)
74 {
75 // return current time minus time_begin
76 UINT32 ticks;
77
78 ticks = rvf_get_tick_count();
79
80 return (ticks - time_begin) * 60 / 13;
81 }
82
83 void ffs_test_task(void)
84 {
85 effs_t error;
86
87 OS_DELAY(217); // wait approx. 1000ms
88
89 ttw(str(TTrTest, "ffs_test_task()" NL));
90
91 while (1) {
92 OS_DELAY(217); // wait approx. 1000ms
93
94 // Poll to see if we have tests to run... We know that the writer of
95 // ffs_test_string has a higher priority than us, so it is properly
96 // written when we reach here.
97 if (*ffs_test_string) {
98 test_init(0);
99 error = test_run(ffs_test_string);
100 *ffs_test_string = 0;
101
102 if (error == 0)
103 ttw(str(TTrTest, "TEST succeeded" NL));
104 else
105 ttw(ttr(TTrTest, "TEST cases failed: %d" NL, error));
106 }
107 }
108 }
109
110 #endif // End of WITH_TFFS
111
112 /******************************************************************************
113 * Target Platform Abstraction Functions
114 ******************************************************************************/
115
116 req_id_t request_id_get(void)
117 {
118 extern uint32 int_disable(void);
119 extern void int_enable(uint32 tmp);
120 uint32 cprs;
121
122 // We disable interrupt to avoid any other tasks to get the same id.
123 cprs = int_disable();
124 request_id_last++;
125
126 if (request_id_last < 0)
127 request_id_last = 0;
128
129 int_enable(cprs);
130
131 return request_id_last;
132 }
133
134 void *target_malloc(unsigned int size)
135 {
136 char *buf;
137
138 #if (_RVF == 1)
139 if ((rvf_get_buf(ffs_mb_id, size, (T_RVF_BUFFER*) &buf)) == RVF_RED)
140 return 0;
141 else
142 return buf;
143 #else
144 return 0;
145 #endif
146 }
147
148 void target_free(void *buf)
149 {
150 int error;
151
152 #if (_RVF == 1)
153 if ((error = OS_FREE(buf)) != OS_OK)
154 ttw(ttr(TTrFatal, "target_free() %d (FAILED)" NL, error));
155 #endif
156 }
157
158
159 /******************************************************************************
160 * FFS Blocking Call Handling
161 ******************************************************************************/
162
163 effs_t ffs_b_begin(struct ffs_blocking_s *fb, T_RVF_MUTEX *mutex, int *result)
164 {
165 effs_t error;
166
167 if ((error = rvf_initialize_mutex(mutex)) < 0)
168 return error;
169
170 if ((error = rvf_lock_mutex(mutex)) < 0) // This will succeed
171 return error;
172
173 fb->result = result;
174 fb->mutex = mutex;
175
176 return EFFS_OK;
177 }
178
179 effs_t ffs_b_end(T_RVF_MUTEX *mutex, int result)
180 {
181 effs_t error;
182
183 // Do not lock the mutex if the message send operation failed
184 if (result >= 0)
185 // This will block the task until the mutex has been released
186 if ((error = rvf_lock_mutex(mutex)) < 0)
187 return error;
188
189 if ((error = rvf_unlock_mutex(mutex)) < 0)
190 return error;
191
192 if ((error = rvf_delete_mutex(mutex)) < 0)
193 return error;
194
195 return EFFS_OK;
196 }
197
198 /******************************************************************************
199 * FFS Task
200 ******************************************************************************/
201 /* The below access to the intenal Nucleus variable "TCD_Interrupt_Level" is
202 * a workaround for a known Nucleus BUG (see CQ SWI-FIX-17560) */
203
204 void ffs_main_init()
205 {
206 extern int TCD_Interrupt_Level;
207 int tmp_int_level;
208 ttr_init(TTrTask|TTrTest|TTrTestInfo);
209 //ttr_init(TTrTask|TTrTest|TTrTestInfo|TTrDrvErase|TTrDrvWrite|TTrTaskLow|TTrApi);
210
211 tmp_int_level = TCD_Interrupt_Level; // Backup Int level
212 TCD_Interrupt_Level = 0xC0; // The Interrups are not yet enabled...
213 ffs_init_status = ffs_initialize();
214 TCD_Interrupt_Level = tmp_int_level; // Restore Int level
215
216 pcm_init(); // We have to call pcm_init() before G23 starts.
217 }
218
219 void ffs_task_init(T_OS_MB_ID mbid, T_RVF_ADDR_ID addr_id)
220 {
221 int error;
222
223 ffs_mb_id = mbid;
224 ffs_addr_id = addr_id;
225
226 #if (WITH_TFFS == 1)
227 error =
228 OS_CREATE_TASK((TASKPTR) ffs_test_task, FFS_TEST_TASK_ID, "TFFS",
229 ffs_test_stack, FFS_TEST_STACK_SIZE,
230 249, // priority
231 4, // ET4_TASK type
232 0, // no time slicing
233 RUNNING); // start state
234
235 ttw(ttr(TTrTask, "os_create_task('TFFS') %d" NL, error));
236 #endif
237 }
238
239 void ffs_task()
240 {
241 extern int etm_ffs_init(void);
242 struct ffs_req_s *request;
243 T_FFS_FILE_CNF *confirm_file;
244 T_FFS_STREAM_CNF *confirm_stream;
245 T_RVF_MUTEX *mutex_p;
246 int error, free_mail, os_error = OS_OK;
247 uint16 revision, manufacturer_id, device_id;
248 uint32 base;
249 fd_t fdi;
250 char *temp_path;
251 req_id_t temp_id;
252
253 #if (BOARD == 34)
254 // Non formatted FFS should be formatted
255 // So we don't have to use PCTM to format it
256 if (fs.initerror == EFFS_NOFORMAT)
257 {
258 ffs_format_nb("/", 0x2BAD, 0);
259 }
260
261 ffs_InitRFCap();
262 #endif
263
264 ttr(TTrTask, "ffs_init() %d" NL, ffs_init_status);
265
266 ffs_query(Q_FFS_REVISION, &revision);
267 ttr(TTrTask,"FFS revision: 0x%x" NL, revision);
268
269 ffs_query(Q_DEV_MANUFACTURER, &manufacturer_id);
270 ffs_query(Q_DEV_DEVICE, &device_id);
271 ffs_query(Q_DEV_DRIVER, &revision);
272 ttr(TTrTask,"FFS device, driver: 0x%02x, 0x%04x, %d" NL,
273 manufacturer_id, device_id, revision);
274
275 ffs_query(Q_DEV_BASE, &base);
276 ffs_query(Q_DEV_BLOCKS, &revision);
277 ttr(TTrTask,"FFS base, blocks: 0x%x, %d" NL, base, revision);
278
279 ffs_query(Q_FFS_FORMAT_READ, &manufacturer_id);
280 ffs_query(Q_FFS_FORMAT_WRITE, &device_id);
281 ttr(TTrTask,"FFS format read, write: 0x%x, 0x%x" NL NL,
282 manufacturer_id, device_id);
283
284 // If some blocks has been marked for reclaim, reclaim them now...
285 blocks_reclaim();
286
287 // We can only mkdir("pcm") *after* our mailbox has been allocated
288 // otherwise mkdir("pcm") will fail.
289 error = ffs_mkdir_nb("/pcm", 0);
290
291 // Register FFS to ETM database
292 error = etm_ffs_init();
293
294 while (1)
295 {
296 OS_MAIL_WAIT(OS_MAIL_EVENT_MASK);
297 request = (struct ffs_req_s *) OS_MAIL_READ();
298
299 ttw(ttr(TTrTaskLow, "ffs_task(%d):" NL, request->cmd));
300 switch (request->cmd) {
301 case WRITE: case SEEK: case CLOSE: case FTRUNC: case FDATASYNC:
302 ttw(ttr(TTrTaskLow, " fdi = %d" NL, request->fdi));
303 // Save a local copy of fdi because the task we call later can
304 // modify it and we have to use the fdi if a callback is used
305 fdi = (fd_t)request->fdi;
306 break;
307 default:
308 ttw(ttr(TTrTaskLow, " name = %s" NL, request->path));
309 break;
310 }
311 ttw(ttr(TTrTaskLow, " src = 0x%x" NL, request->src));
312 ttw(ttr(TTrTaskLow, " size = %d" NL, request->size));
313
314 if (tr_query(TTrTaskDelays))
315 OS_DELAY(5);
316
317 switch (request->cmd) {
318 case NOP: error = EFFS_OK; break;
319 case FILE_WRITE: error = task_file_write(request); break;
320 case SYMLINK: error = task_symlink(request); break;
321 case MKDIR: error = task_mkdir(request); break;
322 case REMOVE: error = task_remove(request); break;
323 case RENAME: error = task_rename(request); break;
324 case FCONTROL: error = task_fcontrol(request); break;
325 case PREFORMAT: error = task_preformat(request); break;
326 case FORMAT: error = task_format(request); break;
327 case OPEN: error = task_open(request); break;
328 case WRITE: error = task_write(request); break;
329 case SEEK: error = task_seek(request); break;
330 case CLOSE: error = task_close(request); break;
331 case TRUNC: error = task_trunc(request); break;
332 case FTRUNC: error = task_ftrunc(request); break;
333 case FDATASYNC: error = task_fdatasync(request); break;
334 default:
335 ttr(TTrFatal, "FFS FATAL: bad request: %d" NL, request->cmd);
336 break;
337 }
338
339 ttw(ttr(TTrTaskLow, "ffs_task(%d) %d" NL, request->cmd, error));
340 if (tr_query(TTrTaskDelays))
341 OS_DELAY(5);
342
343 // Call-back to caller
344 mutex_p = 0;
345 if (request->cp) {
346 free_mail = 0; // don't free mail we will reuse it
347 // We reuse the mail we received for our call-back. Due to
348 // this reuse, we must be careful with the order of
349 // assignments. If this is a stream modify function use
350 // ffs_stream_cnf else use ffs_file_cnf
351
352 temp_id = request->request_id; // Save id before we reuse the mail mem.
353 switch (request->cmd) {
354 case WRITE: case SEEK: case CLOSE: case FTRUNC: case FDATASYNC:
355 confirm_stream = (T_FFS_STREAM_CNF *) request;
356 confirm_stream->error = error;
357 confirm_stream->request_id = temp_id;
358 confirm_stream->fdi = fdi;
359 confirm_stream->header.msg_id = FFS_MESSAGE_OFFSET;
360
361 if (request->cp->callback_func) {
362 request->cp->callback_func((T_FFS_STREAM_CNF *)
363 confirm_stream);
364 }
365 else if (request->cp->addr_id) {
366 os_error = OS_MAIL_SEND(request->cp->addr_id,
367 confirm_stream);
368 }
369 else {
370 ttr(TTrFatal, "FFS WARNING: empty return path" NL);
371 free_mail = 1; // free mail
372 }
373 break;
374 default:
375 temp_path = (char *) request->path;
376 confirm_file = (T_FFS_FILE_CNF *) request;
377 confirm_file->error = error;
378 confirm_file->request_id = temp_id;
379 confirm_file->path = temp_path;
380 confirm_file->header.msg_id = FFS_MESSAGE_OFFSET;
381
382 if (request->cp->callback_func) {
383 request->cp->callback_func((T_FFS_FILE_CNF *) confirm_file);
384 }
385
386 else if (request->cp->addr_id) {
387 os_error = OS_MAIL_SEND(request->cp->addr_id,
388 confirm_file);
389 }
390 else {
391 ttr(TTrFatal, "FFS WARNING: empty return path" NL);
392 free_mail = 1; // free mail
393 }
394 break;
395 }
396
397 if (os_error != OS_OK)
398 ttr(TTrFatal, "FFS FATAL: os_send_msg() %d" NL, os_error);
399 }
400
401 // Blocking handling / unlocking mutex
402 else if (request->fb) {
403 // Save the pointer to the mutex and the error value (allocated
404 // on the stack by the macro FFS_BLOCKING_BEGIN)
405 mutex_p = request->fb->mutex;
406 *request->fb->result = error;
407 free_mail = 1; // free mail
408 }
409
410 else {
411 // The task must have been a non blocking without any callback
412 free_mail = 1; // free mail
413 }
414
415 // Free the mail memory. Note that we always free it, except when we
416 // have successfully reused it for sending a mail call-back.
417 if (free_mail == 1) {
418 os_error = OS_FREE(request);
419 if (os_error != OS_OK)
420 ttr(TTrFatal, "FFS FATAL: os_free() %d" NL, os_error);
421 }
422
423 // Blocking handling / unlocking mutex
424 if (mutex_p) {
425 // Wake up the caller task
426 if ((os_error = rvf_unlock_mutex(mutex_p)) < 0)
427 ttr(TTrFatal, "FFS FATAL: rvf_unlock_mutex() %d" NL, os_error);
428 }
429
430 tr_bstat();
431 }
432 }