comparison gsm-fw/services/ffs/task.c @ 212:3ebe6409e8bc

gsm-fw/services/ffs: task.c integrated
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Mon, 06 Jan 2014 04:15:30 +0000
parents
children fee45482aa2a
comparison
equal deleted inserted replaced
211:847e2585a0f2 212:3ebe6409e8bc
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 "../../include/config.h"
12 #include "ffs.h"
13 #include "core.h"
14 #include "task.h"
15 #include "ffstrace.h"
16 #include "../../riviera/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 /******************************************************************************
32 * FFS Test Task
33 ******************************************************************************/
34
35 // For this to work, change:
36 // 1. MAX_RVF_TASKS define in rvf_target.h
37
38 // Note that the FFS_TEST_TASK_ID is set one too low! We assume we are lucky
39 // that no other task has the same ID...
40
41 #if (WITH_TFFS == 1)
42
43 #define FFS_TEST_STACK_SIZE 1024
44
45 #define FFS_TEST_TASK_ID (MAX_RVF_TASKS - 1 - 1)
46
47 void ffs_test_task(void);
48 void test_init(int keepgoing);
49 int test_run(char *testname);
50
51 static uint8 ffs_test_stack[FFS_TEST_STACK_SIZE];
52
53 // This is the string of test cases to run by calling test_run(). This
54 // string as written to from tmffs module.
55 char ffs_test_string[128];
56
57 // Delay for <delay> milliseconds
58 void tffs_delay(int delay)
59 {
60 delay = 14 * delay / 64; // approx. same as division by 60/13
61 OS_DELAY(delay);
62 }
63
64 UINT32 rvf_get_tick_count(void);
65 // Timer functions for benchmarking
66 UINT32 tffs_timer_begin(void)
67 {
68 return rvf_get_tick_count();
69 }
70
71 UINT32 tffs_timer_end(UINT32 time_begin)
72 {
73 // return current time minus time_begin
74 UINT32 ticks;
75
76 ticks = rvf_get_tick_count();
77
78 return (ticks - time_begin) * 60 / 13;
79 }
80
81 void ffs_test_task(void)
82 {
83 effs_t error;
84
85 OS_DELAY(217); // wait approx. 1000ms
86
87 ttw(str(TTrTest, "ffs_test_task()" NL));
88
89 while (1) {
90 OS_DELAY(217); // wait approx. 1000ms
91
92 // Poll to see if we have tests to run... We know that the writer of
93 // ffs_test_string has a higher priority than us, so it is properly
94 // written when we reach here.
95 if (*ffs_test_string) {
96 test_init(0);
97 error = test_run(ffs_test_string);
98 *ffs_test_string = 0;
99
100 if (error == 0)
101 ttw(str(TTrTest, "TEST succeeded" NL));
102 else
103 ttw(ttr(TTrTest, "TEST cases failed: %d" NL, error));
104 }
105 }
106 }
107
108 #endif // End of WITH_TFFS
109
110 /******************************************************************************
111 * Target Platform Abstraction Functions
112 ******************************************************************************/
113
114 req_id_t request_id_get(void)
115 {
116 extern uint32 int_disable(void);
117 extern void int_enable(uint32 tmp);
118 uint32 cprs;
119
120 // We disable interrupt to avoid any other tasks to get the same id.
121 cprs = int_disable();
122 request_id_last++;
123
124 if (request_id_last < 0)
125 request_id_last = 0;
126
127 int_enable(cprs);
128
129 return request_id_last;
130 }
131
132 void *target_malloc(unsigned int size)
133 {
134 char *buf;
135
136 #if (_RVF == 1)
137 if ((rvf_get_buf(ffs_mb_id, size, (T_RVF_BUFFER*) &buf)) == RVF_RED)
138 return 0;
139 else
140 return buf;
141 #else
142 return 0;
143 #endif
144 }
145
146 void target_free(void *buf)
147 {
148 int error;
149
150 #if (_RVF == 1)
151 if ((error = OS_FREE(buf)) != OS_OK)
152 ttw(ttr(TTrFatal, "target_free() %d (FAILED)" NL, error));
153 #endif
154 }
155
156
157 /******************************************************************************
158 * FFS Blocking Call Handling
159 ******************************************************************************/
160
161 effs_t ffs_b_begin(struct ffs_blocking_s *fb, T_RVF_MUTEX *mutex, int *result)
162 {
163 effs_t error;
164
165 if ((error = rvf_initialize_mutex(mutex)) < 0)
166 return error;
167
168 if ((error = rvf_lock_mutex(mutex)) < 0) // This will succeed
169 return error;
170
171 fb->result = result;
172 fb->mutex = mutex;
173
174 return EFFS_OK;
175 }
176
177 effs_t ffs_b_end(T_RVF_MUTEX *mutex, int result)
178 {
179 effs_t error;
180
181 // Do not lock the mutex if the message send operation failed
182 if (result >= 0)
183 // This will block the task until the mutex has been released
184 if ((error = rvf_lock_mutex(mutex)) < 0)
185 return error;
186
187 if ((error = rvf_unlock_mutex(mutex)) < 0)
188 return error;
189
190 if ((error = rvf_delete_mutex(mutex)) < 0)
191 return error;
192
193 return EFFS_OK;
194 }
195
196 /******************************************************************************
197 * FFS Task
198 ******************************************************************************/
199 /* The below access to the intenal Nucleus variable "TCD_Interrupt_Level" is
200 * a workaround for a known Nucleus BUG (see CQ SWI-FIX-17560) */
201
202 void ffs_main_init()
203 {
204 extern int TCD_Interrupt_Level;
205 int tmp_int_level;
206 ttr_init(TTrTask|TTrTest|TTrTestInfo);
207 //ttr_init(TTrTask|TTrTest|TTrTestInfo|TTrDrvErase|TTrDrvWrite|TTrTaskLow|TTrApi);
208
209 tmp_int_level = TCD_Interrupt_Level; // Backup Int level
210 TCD_Interrupt_Level = 0xC0; // The Interrups are not yet enabled...
211 ffs_init_status = ffs_initialize();
212 TCD_Interrupt_Level = tmp_int_level; // Restore Int level
213
214 #if 0
215 pcm_init(); // We have to call pcm_init() before G23 starts.
216 #endif
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 #if 0
292 // Register FFS to ETM database
293 error = etm_ffs_init();
294 #endif
295
296 while (1)
297 {
298 OS_MAIL_WAIT(OS_MAIL_EVENT_MASK);
299 request = (struct ffs_req_s *) OS_MAIL_READ();
300
301 ttw(ttr(TTrTaskLow, "ffs_task(%d):" NL, request->cmd));
302 switch (request->cmd) {
303 case WRITE: case SEEK: case CLOSE: case FTRUNC: case FDATASYNC:
304 ttw(ttr(TTrTaskLow, " fdi = %d" NL, request->fdi));
305 // Save a local copy of fdi because the task we call later can
306 // modify it and we have to use the fdi if a callback is used
307 fdi = (fd_t)request->fdi;
308 break;
309 default:
310 ttw(ttr(TTrTaskLow, " name = %s" NL, request->path));
311 break;
312 }
313 ttw(ttr(TTrTaskLow, " src = 0x%x" NL, request->src));
314 ttw(ttr(TTrTaskLow, " size = %d" NL, request->size));
315
316 if (tr_query(TTrTaskDelays))
317 OS_DELAY(5);
318
319 switch (request->cmd) {
320 case NOP: error = EFFS_OK; break;
321 case FILE_WRITE: error = task_file_write(request); break;
322 case SYMLINK: error = task_symlink(request); break;
323 case MKDIR: error = task_mkdir(request); break;
324 case REMOVE: error = task_remove(request); break;
325 case RENAME: error = task_rename(request); break;
326 case FCONTROL: error = task_fcontrol(request); break;
327 case PREFORMAT: error = task_preformat(request); break;
328 case FORMAT: error = task_format(request); break;
329 case OPEN: error = task_open(request); break;
330 case WRITE: error = task_write(request); break;
331 case SEEK: error = task_seek(request); break;
332 case CLOSE: error = task_close(request); break;
333 case TRUNC: error = task_trunc(request); break;
334 case FTRUNC: error = task_ftrunc(request); break;
335 case FDATASYNC: error = task_fdatasync(request); break;
336 default:
337 ttr(TTrFatal, "FFS FATAL: bad request: %d" NL, request->cmd);
338 break;
339 }
340
341 ttw(ttr(TTrTaskLow, "ffs_task(%d) %d" NL, request->cmd, error));
342 if (tr_query(TTrTaskDelays))
343 OS_DELAY(5);
344
345 // Call-back to caller
346 mutex_p = 0;
347 if (request->cp) {
348 free_mail = 0; // don't free mail we will reuse it
349 // We reuse the mail we received for our call-back. Due to
350 // this reuse, we must be careful with the order of
351 // assignments. If this is a stream modify function use
352 // ffs_stream_cnf else use ffs_file_cnf
353
354 temp_id = request->request_id; // Save id before we reuse the mail mem.
355 switch (request->cmd) {
356 case WRITE: case SEEK: case CLOSE: case FTRUNC: case FDATASYNC:
357 confirm_stream = (T_FFS_STREAM_CNF *) request;
358 confirm_stream->error = error;
359 confirm_stream->request_id = temp_id;
360 confirm_stream->fdi = fdi;
361 confirm_stream->header.msg_id = FFS_MESSAGE_OFFSET;
362
363 if (request->cp->callback_func) {
364 request->cp->callback_func((T_FFS_STREAM_CNF *)
365 confirm_stream);
366 }
367 else if (request->cp->addr_id) {
368 os_error = OS_MAIL_SEND(request->cp->addr_id,
369 confirm_stream);
370 }
371 else {
372 ttr(TTrFatal, "FFS WARNING: empty return path" NL);
373 free_mail = 1; // free mail
374 }
375 break;
376 default:
377 temp_path = (char *) request->path;
378 confirm_file = (T_FFS_FILE_CNF *) request;
379 confirm_file->error = error;
380 confirm_file->request_id = temp_id;
381 confirm_file->path = temp_path;
382 confirm_file->header.msg_id = FFS_MESSAGE_OFFSET;
383
384 if (request->cp->callback_func) {
385 request->cp->callback_func((T_FFS_FILE_CNF *) confirm_file);
386 }
387
388 else if (request->cp->addr_id) {
389 os_error = OS_MAIL_SEND(request->cp->addr_id,
390 confirm_file);
391 }
392 else {
393 ttr(TTrFatal, "FFS WARNING: empty return path" NL);
394 free_mail = 1; // free mail
395 }
396 break;
397 }
398
399 if (os_error != OS_OK)
400 ttr(TTrFatal, "FFS FATAL: os_send_msg() %d" NL, os_error);
401 }
402
403 // Blocking handling / unlocking mutex
404 else if (request->fb) {
405 // Save the pointer to the mutex and the error value (allocated
406 // on the stack by the macro FFS_BLOCKING_BEGIN)
407 mutex_p = request->fb->mutex;
408 *request->fb->result = error;
409 free_mail = 1; // free mail
410 }
411
412 else {
413 // The task must have been a non blocking without any callback
414 free_mail = 1; // free mail
415 }
416
417 // Free the mail memory. Note that we always free it, except when we
418 // have successfully reused it for sending a mail call-back.
419 if (free_mail == 1) {
420 os_error = OS_FREE(request);
421 if (os_error != OS_OK)
422 ttr(TTrFatal, "FFS FATAL: os_free() %d" NL, os_error);
423 }
424
425 // Blocking handling / unlocking mutex
426 if (mutex_p) {
427 // Wake up the caller task
428 if ((os_error = rvf_unlock_mutex(mutex_p)) < 0)
429 ttr(TTrFatal, "FFS FATAL: rvf_unlock_mutex() %d" NL, os_error);
430 }
431
432 tr_bstat();
433 }
434 }