source: azure_iot_hub_f767zi/trunk/asp_baseplatform/gdic/flash_file/flash_file.c@ 457

Last change on this file since 457 was 457, checked in by coas-nagasima, 4 years ago

ファイルを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 8.4 KB
Line 
1/*
2 * TOPPERS/ASP Kernel
3 * Toyohashi Open Platform for Embedded Real-Time Systems/
4 * Advanced Standard Profile Kernel
5 *
6 * Copyright (C) 2008-2011 by Embedded and Real-Time Systems Laboratory
7 * Graduate School of Information Science, Nagoya Univ., JAPAN
8 * Copyright (C) 2015-2018 by TOPPERS PROJECT Educational Working Group.
9 *
10 * 上記著作権者は,以下の (1)~(4) の条件か,Free Software Foundation
11 * によって公表されている GNU General Public License の Version 2 に記
12 * 述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
13 * を改変したものを含む.以下同じ)を使用・複製・改変・再配布(以下,
14 * 利用と呼ぶ)することを無償で許諾する.
15 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
16 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
17 * スコード中に含まれていること.
18 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
19 * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
20 * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
21 * の無保証規定を掲載すること.
22 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
23 * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
24 * と.
25 * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
26 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
27 * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
28 * 報告すること.
29 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
30 * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
31 *
32 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
33 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
34 * 含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
35 * 接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
36 *
37 * @(#) $Id$
38 */
39
40#include <kernel.h>
41#include <t_syslog.h>
42#include <t_stdlib.h>
43#include <stdio.h>
44#include <string.h>
45#include <target_syssvc.h>
46#include "syssvc/serial.h"
47#include "syssvc/syslog.h"
48#include "device.h"
49#include "qspi.h"
50#include <fcntl.h>
51#include "storagedevice.h"
52#include "flash_file.h"
53
54#define MIN(a, b) ((a) > (b) ? (b) : (a))
55
56#define QSPIBUFLENGTH 512
57
58static QSPI_Handle_t *hqspi;
59static int num_files = 0;
60static int dirreadcount;
61static fs_descrip_t fs_descrip_table[MAX_DESCRIP];
62static uint32_t qspibuff[(QSPIBUFLENGTH+8)/sizeof(uint32_t)];
63
64static void *ffs_opendir(const char *pathname);
65static int ffs_closedir(void *dir);
66static int ffs_readdir(void *dir, void *pdirent);
67static int ffs_open(int devno, const char *pathname, int flags);
68static int ffs_close(int fd);
69static int ffs_fstat(int fd, struct stat *buf);
70static off_t ffs_lseek(int fd, off_t offset, int whence, int *res);
71static long ffs_read(int fd, void *buf, long count, int *res);
72
73static const StorageDeviceFileFunc_t romSDeviceFunc = {
74 ffs_opendir,
75 ffs_closedir,
76 ffs_readdir,
77 0,
78 0,
79 0,
80 0,
81 0,
82 0,
83 0,
84 ffs_open,
85 ffs_close,
86 ffs_fstat,
87 ffs_lseek,
88 ffs_read,
89 0,
90 0
91};
92
93
94/*
95 * FLASH-ROMファイル関数郡の初期化
96 */
97ER
98flash_file_init(void *h)
99{
100 StorageDevice_t *psdev;
101 int result;
102
103 num_files = 0;
104 memset(fs_descrip_table, 0, sizeof(fs_descrip_table));
105 result = SDMSetupDevice(DEFAULT_DEVNO, &psdev);
106 if(result == E_OK){
107 psdev->pdevff = (StorageDeviceFileFunc_t *)&romSDeviceFunc;
108 psdev->_sdev_attribute |= SDEV_EMPLOY;
109 hqspi = (QSPI_Handle_t *)h;
110 return MAX_DESCRIP;
111 }
112 else
113 return 0;
114}
115
116/*
117 * FLASH-ROMファイルの生成
118 */
119int
120create_flash_file(const char *pathname, uint32_t start, uint32_t size)
121{
122 fs_descrip_t *fdp;
123 int cd = -1;
124 int i, j;
125
126 dis_dsp(); /* disable dispatch */
127 for(i = 0 ; i < MAX_DESCRIP ; i++){
128 fdp = &fs_descrip_table[i];
129 if(fdp->create == 0){
130 fdp->create = 1;
131 fdp->inuse = 0;
132 fdp->fp = 0;
133 fdp->addr = start;
134 fdp->size = size;
135 for(j = 0 ; j < ROMFILE_SIZE && pathname[j] != 0 ; j++)
136 fdp->filename[j] = (char)pathname[j];
137 fdp->filename[j] = 0;
138 cd = i;
139 num_files++;
140 break;
141 }
142 }
143 ena_dsp(); /* enable dispatch */
144 return cd;
145}
146
147/*
148 * FLASH-ROMファイルディレクトリオープン
149 */
150static void *
151ffs_opendir(const char *pathname)
152{
153 dirreadcount = 0;
154 return (void *)&num_files;
155}
156
157/*
158 * FLASH-ROMファイルディレクトリクローズ
159 */
160static int
161ffs_closedir(void *dir)
162{
163 return 0;
164}
165
166/*
167 * FLASH-ROMファイルディレクトリ読み込み
168 */
169static int
170ffs_readdir(void *dir, void *pdirent)
171{
172 fs_descrip_t *pf;
173 struct dirent2 *pd = pdirent;
174 unsigned int i;
175
176 if(dirreadcount >= num_files)
177 return 0;
178 else{
179 pf = &fs_descrip_table[dirreadcount];
180 pd->d_fsize = pf->size;
181 pd->d_date = 0;
182 pd->d_time = 0;
183 pd->d_type = AM_RDO;
184 for(i = 0 ; i < 255 && pf->filename[i] != 0 ; i++){
185 pd->d_name[i] = pf->filename[i];
186 }
187 pd->d_name[i] = 0;
188 dirreadcount++;
189 return dirreadcount;
190 }
191}
192
193/*
194 * FLASH-ROMファイルオープン
195 */
196static int
197ffs_open(int devno, const char *pathname, int flags)
198{
199 fs_descrip_t *fdp = 0;
200 int fd;
201
202 SDMGetDeviceNo(&pathname);
203 if(*pathname == '/')
204 pathname++;
205 dis_dsp(); /* disable dispatch */
206 for(fd = 0 ; fd < num_files ; fd++){
207 fdp = &fs_descrip_table[fd];
208 if(fdp->create && strcmp(pathname, fdp->filename) == 0){
209 break;
210 }
211 }
212 if(fd < num_files && (flags & 0x0f) == O_RDONLY && fdp->inuse == 0){
213 fdp->inuse = 1;
214 fdp->fp = 0;
215 ena_dsp(); /* enable dispatch */
216 return fd;
217 }
218 else{
219 ena_dsp(); /* enable dispatch */
220 return -1;
221 }
222}
223
224/*
225 * FLASH-ROMファイルクローズ
226 */
227static int
228ffs_close(int fd)
229{
230 fs_descrip_t *fdp;
231
232 if(fd < 0 || num_files < fd)
233 return -1;
234 fdp = &fs_descrip_table[fd];
235 if(fdp->inuse == 1){
236 fdp->inuse = 0;
237 return 0;
238 }
239 else
240 return -1;
241}
242
243/*
244 * FLASH-ROMファイルステート取得
245 */
246static int
247ffs_fstat(int fd, struct stat *buf)
248{
249 fs_descrip_t *fdp;
250
251 if(fd < 0 || num_files < fd)
252 return -1;
253 fdp = &fs_descrip_table[fd];
254 if(fdp->create == 1){
255 memset(buf, 0, sizeof(struct stat));
256 buf->st_size = fdp->size;
257 return 0;
258 }
259 else
260 return -1;
261}
262
263/*
264 * FLASH-ROMファイルシーク
265 */
266static off_t
267ffs_lseek(int fd, off_t offset, int whence, int *res)
268{
269 fs_descrip_t *fdp;
270
271 *res = -1;
272 if(fd < 0 || num_files < fd)
273 return -1;
274 fdp = &fs_descrip_table[fd];
275 if(fdp->inuse == 1){
276 switch (whence) {
277 case SEEK_SET:
278 fdp->fp = offset;
279 break;
280 case SEEK_CUR:
281 fdp->fp += offset;
282 break;
283 case SEEK_END:
284 fdp->fp = fdp->size + offset;
285 break;
286 }
287 offset = fdp->fp;
288 *res = 0;
289 return offset;
290 }
291 else
292 return -1;
293}
294
295/*
296 * FLASH-ROMファイル読みだし
297 */
298static long
299ffs_read(int fd, void *buf, long count, int *res)
300{
301 fs_descrip_t*fdp;
302 int readsize, length;
303 uint32_t fp, rcount, off;
304 ER ercd = E_OK;
305
306 *res = -1;
307 if(fd < 0 || num_files < fd)
308 return -1;
309 fdp = &fs_descrip_table[fd];
310 if(fdp->inuse == 1){
311 if(fdp->fp < fdp->size){
312 if((fdp->fp + count) <= fdp->size)
313 readsize = count; /* all counts can be read */
314 else
315 readsize = fdp->size - fdp->fp; /* a part of counts can be read */
316 length = readsize;
317 fp = fdp->fp;
318 while(length > 0){
319 off = ((unsigned int)fdp->addr + fp) & 3;
320 rcount = MIN(QSPIBUFLENGTH, length);
321 ercd = qspi_read(hqspi, (void *)qspibuff, (uint32_t)(fdp->addr + fp - off), ((rcount+off+3) & ~3));
322 if(ercd != E_OK){
323 syslog_4(LOG_ERROR, "ffs_read(%d, %08x, %d, %08x) ERROR !", fd, buf, count, res);
324 break;
325 }
326 memcpy(buf, ((uint8_t *)qspibuff)+off, rcount);
327 fp += rcount;
328 buf += rcount;
329 length -= rcount;
330 }
331 fdp->fp += readsize;
332 }
333 else
334 readsize = 0; /* no data can be read */
335 *res = 0;
336 if(ercd == E_OK)
337 return readsize;
338 else
339 return 0;
340 }
341 else
342 return -1;
343}
344
Note: See TracBrowser for help on using the repository browser.