picoos-micro  1.xxxx
picoos-u.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2014, Ari Suutari <ari@stonepile.fi>.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote
15  * products derived from this software without specific prior written
16  * permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef _PICOOS_U_H
32 #define _PICOOS_U_H
33 
81 #ifdef __cplusplus
82 extern "C"
83 {
84 #endif /* __cplusplus */
85 
86 #include "uoscfg.h"
87 #include <stdint.h>
88 #include <stdbool.h>
89 
90 #ifndef UOSCFG_MAX_MOUNT
91 #define UOSCFG_MAX_MOUNT 2
92 #endif
93 
103 void uosInit(void);
104 
109 void uosBootDiag(void);
110 
116 void uosResourceDiag(void);
117 
122 void uosSpinInit(void);
123 
124 #if defined(__MSP430__) && UOSCFG_SPIN_USECS == 2
125 
126 #define uosSpinUSecs(t) __delay_cycles(PORTCFG_CPU_CLOCK_MHZ * (t))
127 
128 #else
129 
135 void uosSpinUSecs(uint16_t uSecs);
136 
137 #endif
138 
141 #if UOSCFG_SPI_BUS > 0 || DOX == 1
142 
148 struct uosSpiBus;
149 struct uosSpiDev;
150 
154 typedef struct uosSpiBusConf {
155 
156  void (*init)(struct uosSpiBus* bus);
157  void (*control)(struct uosSpiBus* bus, bool fullSpeed);
158  void (*cs)(struct uosSpiBus* bus, bool select);
159  uint8_t (*xchg)(const struct uosSpiBus* bus, uint8_t data);
160  void (*xmit)(const struct uosSpiBus*, const uint8_t* data, int len);
161  void (*rcvr)(const struct uosSpiBus*, uint8_t* data, int len);
162 } UosSpiBusConf;
163 
167 typedef struct uosSpiBus {
168 
169  const UosSpiBusConf* cf;
170  POSMUTEX_t busMutex;
171  struct uosSpiDev* currentDev;
172  bool active;
173 } UosSpiBus;
174 
178 typedef struct __attribute__((aligned(4))) uosSpiDevConf {
179 #ifdef UOSCFG_SPI_CS_TYPE
180  UOSCFG_SPI_CS_TYPE cs;
181 #endif
182 } UosSpiDevConf;
183 
187 typedef struct uosSpiDev {
188 
189  const UosSpiDevConf* cf;
190  UosSpiBus* bus;
191 } UosSpiDev;
192 
196 void uosSpiInit(UosSpiBus* bus, const UosSpiBusConf* cf);
197 
201 void uosSpiDevInit(UosSpiDev* dev, const UosSpiDevConf* cf, UosSpiBus* bus);
202 
206 void uosSpiControl(UosSpiBus* bus, bool fullSpeed);
207 
211 void uosSpiBeginNoCS(UosSpiDev* dev);
212 
216 void uosSpiBegin(UosSpiDev* dev);
217 
221 void uosSpiCS(UosSpiDev* dev, bool select);
222 
226 uint8_t uosSpiXchg(UosSpiDev* dev, uint8_t data);
227 
231 void uosSpiXmit(UosSpiDev* dev, const uint8_t* data, int len);
232 
236 void uosSpiRcvr(UosSpiDev* dev, uint8_t* data, int len);
237 
242 void uosSpiEnd(UosSpiDev* dev);
243 
246 #endif
247 
248 #if UOSCFG_MAX_OPEN_FILES > 0 || DOX == 1
249 
250 #if !defined(UOSCFG_NEWLIB_SYSCALLS) || UOSCFG_NEWLIB_SYSCALLS == 0
251 
252 #define O_RDONLY 0
253 #define O_WRONLY 1
254 #define O_RDWR 2
255 #define O_APPEND 0x0008
256 #define O_CREAT 0x0200
257 #define O_TRUNC 0x0400
258 
259 #endif
260 
261 struct uosFile;
262 struct uosFS;
263 struct uosDisk;
264 
270 #define UOS_BITTAB_TABLE(type, size) \
271  typedef struct { \
272  uint8_t bitmap[size / 8 + 1]; \
273  type table[size]; \
274  } type##Bittab
275 
280 #define UOS_BITTAB_INIT(bm) memset(&bm.bitmap, '\0', sizeof(bm.bitmap))
281 
286 #define UOS_BITTAB_SLOT(bm, elem) (elem == NULL ? -1 : (elem - bm.table))
287 
291 #define UOS_BITTAB_ELEM(bm, slot) (slot == -1 ? NULL : (bm.table + slot))
292 
296 #define UOS_BITTAB_ALLOC(bm) uosBitTabAlloc(bm.bitmap, sizeof(bm.table)/sizeof(bm.table[0]))
297 
301 #define UOS_BITTAB_FREE(bm, slot) uosBitTabFree(bm.bitmap, slot)
302 
306 #define UOS_BITTAB_IS_FREE(bm, slot) uosBitTabIsFree(bm.bitmap, slot)
307 
311 int uosBitTabAlloc(uint8_t* bitmap, int size);
312 
316 void uosBitTabFree(uint8_t* bitmap, int slot);
317 
321 bool uosBitTabIsFree(uint8_t* bitmap, int slot);
322 
331 typedef struct uosFileInfo {
332  bool isDir;
333  bool isSocket;
334  int size;
335 } UosFileInfo;
336 
341 typedef struct uosFileConf {
342 
343  int (*read)(struct uosFile* file, char* buf, int max);
344  int (*write)(struct uosFile* file, const char* buf, int len);
345  int (*close)(struct uosFile* file);
346  int (*fstat)(struct uosFile* file, UosFileInfo* st);
347  int (*lseek)(struct uosFile* file, int offset, int whence);
348  int (*sync)(struct uosFile* file);
349 } UosFileConf;
350 
355 typedef struct uosFSConf {
356 
357  int (*init)(const struct uosFS* mount);
358  int (*open)(const struct uosFS* mount, struct uosFile* file, const char* filename, int flags, int mode);
359  int (*stat)(const struct uosFS* mount, const char* filename, UosFileInfo* st);
360  int (*unlink)(const struct uosFS* mount, const char* name);
361 } UosFSConf;
362 
367 typedef struct uosDiskConf {
368 
369  int (*init)(const struct uosDisk* disk);
370  int (*status)(const struct uosDisk* disk);
371  int (*read)(const struct uosDisk* disk, uint8_t* buff, int sector, int count);
372  int (*write)(const struct uosDisk* disk, const uint8_t* buff, int sector, int count);
373  int (*ioctl)(const struct uosDisk* disk, uint8_t cmd, void* buff);
374 } UosDiskConf;
375 
379 typedef struct uosDisk {
380 
381  const UosDiskConf* cf;
382 } UosDisk;
383 
387 typedef struct uosFS {
388 
389  const UosFSConf* cf;
390  const char* mountPoint;
391 } UosFS;
392 
396 typedef struct uosFile {
397 
398  const UosFileConf* cf;
399  const UosFS* fs;
400  union {
401  void* fsPriv;
402  int fsPrivFd;
403  };
404 
405 } UosFile;
406 
411 void uosFileInit(void);
412 
416 int uosFile2Slot(UosFile* file);
417 
421 UosFile* uosSlot2File(int fd);
422 
426 int uosMount(const UosFS* mount);
427 
431 UosFile* uosFileAlloc(void);
432 
436 int uosFileFree(UosFile* file);
437 
441 UosFile* uosFileOpen(const char* fileName, int flags, int mode);
442 
446 int uosFileRead(UosFile* file, char* buf, int max);
447 
451 int uosFileWrite(UosFile* file, const char* buf, int len);
452 
456 int uosFileClose(UosFile* file);
457 
461 int uosFileStat(const char* filename, UosFileInfo* st);
462 
466 int uosFileFStat(UosFile* file, UosFileInfo* st);
467 
471 int uosFileSeek(UosFile* file, int offset, int whence);
472 
476 int uosFileUnlink(const char* filename);
477 
481 int uosFileSync(UosFile* file);
482 
486 int uosAddDisk(const UosDisk* disk);
487 
491 const UosDisk* uosGetDisk(int diskNumber);
492 
493 #if UOSCFG_FAT > 0 || DOX == 1
494 
498 int uosMountFat(const char* mountPoint, int diskNumber);
499 
500 #if UOSCFG_FAT_MMC > 0 || DOX == 1
501 
502 extern const UosDiskConf uosMmcDiskConf;
503 
504 struct uosMmcDisk;
505 
509 typedef struct uosMmcSpiConf {
510 
511  void (*open)(const struct uosMmcDisk* disk);
512  void (*close)(const struct uosMmcDisk* disk);
513 } UosMmcSpiConf;
514 
518 typedef struct uosMmcDisk {
519 
520  UosDisk base;
521  const UosMmcSpiConf* cf;
522  UosSpiDev* dev;
523 } UosMmcDisk;
524 
528 void uosMmcSpiXmit(const UosMmcDisk*, const uint8_t* data, int len);
529 
533 void uosMmcSpiRcvr(const UosMmcDisk*, uint8_t* data, int len);
534 
535 #endif
536 #endif
537 
538 #if UOSCFG_FS_ROM > 0 || DOX == 1
539 
540 typedef struct {
541  const char* fileName;
542  const uint8_t* contents;
543  int size;
544 } UosRomFile;
545 
549 int uosMountRom(const char* mountPoint, const UosRomFile* data);
550 
551 #endif
552 
555 #endif
556 
560 void uosNewlibInit(void);
561 
562 #if UOSCFG_RING > 0 || DOX == 1
563 
569 typedef struct uosRing UosRing;
570 
574 UosRing* uosRingCreate(int msgSize, int msgCount);
575 
581 bool uosRingPut(UosRing* ring, const void *msg, UINT_t timeout);
582 
588 bool uosRingGet(UosRing* ring, void *msg, UINT_t timeout);
589 
593 void uosRingDestroy(UosRing* ring);
594 
597 #endif
598 
599 #if UOSCFG_NEWLIB_SYSCALLS == 1
600 int fsync(int);
601 #endif
602 
608 #define UOS_CONFIG_KEYSIZE 20
609 #define UOS_CONFIG_VALUESIZE 40
610 
614 typedef struct _uosConfigKeyValue {
615 
616  char key[UOS_CONFIG_KEYSIZE];
617  char value[UOS_CONFIG_VALUESIZE];
618  struct _uosConfigKeyValue* next;
619 
621 
625 typedef int (*UosConfigSaver)(void* context, const char* key, const char* value);
626 
632 const char* uosConfigGet(const char* key);
633 
638 const char* uosConfigSet(const char* key, const char* value);
639 
644 void uosConfigInit(void);
645 
652 int uosConfigSaveEntries(void* context, UosConfigSaver saver);
653 
654 #if UOSCFG_MAX_OPEN_FILES > 0 || DOX == 1
655 
659 int uosConfigSave(const char* filename);
660 
664 int uosConfigLoad(const char* filename);
665 
666 #endif
667 
670 #ifdef __cplusplus
671 } // extern "C"
672 #endif /* __cplusplus */
673 #endif
void uosSpinInit(void)
void uosMmcSpiRcvr(const UosMmcDisk *, uint8_t *data, int len)
int uosFileSync(UosFile *file)
const char * uosConfigGet(const char *key)
Definition: picoos-u.h:518
struct uosFileInfo UosFileInfo
void uosInit(void)
struct _uosConfigKeyValue UosConfigKeyValue
bool uosBitTabIsFree(uint8_t *bitmap, int slot)
int uosMountFat(const char *mountPoint, int diskNumber)
UosRing * uosRingCreate(int msgSize, int msgCount)
struct uosDiskConf UosDiskConf
const UosDisk * uosGetDisk(int diskNumber)
struct uosFSConf UosFSConf
Definition: picoos-u.h:367
int uosBitTabAlloc(uint8_t *bitmap, int size)
Definition: picoos-u.h:167
void uosFileInit(void)
struct uosDisk UosDisk
void uosBitTabFree(uint8_t *bitmap, int slot)
void uosMmcSpiXmit(const UosMmcDisk *, const uint8_t *data, int len)
UosFile * uosFileOpen(const char *fileName, int flags, int mode)
const char * uosConfigSet(const char *key, const char *value)
int uosMount(const UosFS *mount)
void uosSpiRcvr(UosSpiDev *dev, uint8_t *data, int len)
int(* UosConfigSaver)(void *context, const char *key, const char *value)
Definition: picoos-u.h:625
struct uosSpiBus UosSpiBus
int uosFileClose(UosFile *file)
picoos-micro library configuration file
struct uosFile UosFile
int uosFileRead(UosFile *file, char *buf, int max)
bool uosRingPut(UosRing *ring, const void *msg, UINT_t timeout)
void uosSpiBeginNoCS(UosSpiDev *dev)
void uosConfigInit(void)
int uosConfigSaveEntries(void *context, UosConfigSaver saver)
void uosSpiCS(UosSpiDev *dev, bool select)
void uosNewlibInit(void)
Definition: picoos-u.h:614
int uosConfigSave(const char *filename)
Definition: picoos-u.h:509
void uosSpiInit(UosSpiBus *bus, const UosSpiBusConf *cf)
void uosResourceDiag(void)
int uosFile2Slot(UosFile *file)
struct uosSpiBusConf UosSpiBusConf
Definition: picoos-u.h:154
int uosFileWrite(UosFile *file, const char *buf, int len)
int uosFileUnlink(const char *filename)
void uosBootDiag(void)
bool uosRingGet(UosRing *ring, void *msg, UINT_t timeout)
Definition: picoos-u.h:355
struct uosFS UosFS
struct uosMmcDisk UosMmcDisk
struct __attribute__((aligned(4))) uosSpiDevConf
Definition: picoos-u.h:178
void uosSpiControl(UosSpiBus *bus, bool fullSpeed)
int uosFileSeek(UosFile *file, int offset, int whence)
int uosConfigLoad(const char *filename)
uint8_t uosSpiXchg(UosSpiDev *dev, uint8_t data)
int uosAddDisk(const UosDisk *disk)
struct uosSpiDev UosSpiDev
Definition: picoos-u.h:379
int uosFileFree(UosFile *file)
Definition: picoos-u.h:341
void uosSpiEnd(UosSpiDev *dev)
void uosRingDestroy(UosRing *ring)
UosFile * uosFileAlloc(void)
int uosMountRom(const char *mountPoint, const UosRomFile *data)
void uosSpiBegin(UosSpiDev *dev)
UosFile * uosSlot2File(int fd)
Definition: picoos-u.h:387
int uosFileFStat(UosFile *file, UosFileInfo *st)
void uosSpiXmit(UosSpiDev *dev, const uint8_t *data, int len)
void uosSpinUSecs(uint16_t uSecs)
Definition: picoos-u.h:331
void uosSpiDevInit(UosSpiDev *dev, const UosSpiDevConf *cf, UosSpiBus *bus)
struct uosMmcSpiConf UosMmcSpiConf
Definition: picoos-u.h:396
Definition: picoos-u.h:187
int uosFileStat(const char *filename, UosFileInfo *st)
struct uosFileConf UosFileConf