diff --git a/MCU/DevDriverDemo/Demo0.c b/MCU/DevDriverDemo/Demo0.c new file mode 100644 index 0000000..88503db --- /dev/null +++ b/MCU/DevDriverDemo/Demo0.c @@ -0,0 +1,140 @@ +/** + * @file Demo0.c + * @author Rich Chan + * @brief + * @version 0.1 + * @date 2020-08-01 + * + * @copyright Copyright (c) 2020 + * + */ +#include "Device.h" + +typedef struct DEMO0_OBJ +{ + // Some private data for instance + uint8_t ID; +}ST_DEMO0_OBJ; + + +ST_DEMO0_OBJ Demo0Obj[2]; + +static EU_ERR Demo0Init(ST_DEVICE* dev) +{ + ST_DEMO0_OBJ* obj = (ST_DEMO0_OBJ*)dev->Prv; + + if(&Demo0Obj[0]==obj) + { + // Do something. + return EC_OK; + } + else if(&Demo0Obj[1]==obj) + { + // Do something. + return EC_OK; + } + return EC_ERESOURCES; +} + +static EU_ERR Demo0Open(ST_DEVICE* dev) +{ + ST_DEMO0_OBJ* obj = (ST_DEMO0_OBJ*)dev->Prv; + + if(&Demo0Obj[0]==obj) + { + // Do something. + return EC_OK; + } + else if(&Demo0Obj[1]==obj) + { + // Do something. + return EC_OK; + } + return EC_ERESOURCES; +} + +/** + * @brief + * + * @param dev + * @param ex contains additional control information. + * @param buf data pointer to be write. + * @param nbytes data size. + * @param timeout + * @return int64_t real size readed. + */ +static int64_t Demo0Read(ST_DEVICE* dev, void* ex, void* buf, int64_t nbytes, uint32_t timeout) +{ + ST_DEMO0_OBJ* obj = (ST_DEMO0_OBJ*)dev->Prv; + int64_t rsize; + + UNUSED(timeout); + if(&Demo0Obj[0]==obj) + { + // Do something. + return nbytes; + } + else if(&Demo0Obj[1]==obj) + { + // Do something. + return nbytes; + } + return EC_EPAM; +} + +/** + * @brief Same as Demo0Read + * + * @param dev + * @param ex Same as Demo0Read + * @param buf + * @param nbytes + * @param timeout + * @return int64_t + */ +static int64_t Demo0Write(ST_DEVICE* dev, void* ex, void* buf, int64_t nbytes, uint32_t timeout) +{ + ST_MFIS_OBJ* obj = (ST_MFIS_OBJ*)dev->Prv; + + UNUSED(timeout); + if(&Demo0Obj[0]==obj) + { + // Do something. + return nbytes; + } + else if(&Demo0Obj[1]==obj) + { + // Do something. + return nbytes; + } + return EC_EPAM; +} + +ST_DEVICE Demo0[2] = { + { + .Init = Demo0Init, + .Open = Demo0Open, + .Close = NULL, + .Read = Demo0Read, + .Write = Demo0Write, + .Seek = NULL, + .IOCtl = NULL, + .PwCtl = NULL, + .EnInt = NULL, + .DisInt = NULL, + .Prv = (void*)&Demo0Obj[0], + }, + { + .Init = Demo0Init, + .Open = Demo0Open, + .Close = NULL, + .Read = Demo0Read, + .Write = Demo0Write, + .Seek = NULL, + .IOCtl = NULL, + .PwCtl = NULL, + .EnInt = NULL, + .DisInt = NULL, + .Prv = (void*)&Demo0Obj[1], + } +}; diff --git a/MCU/DevDriverDemo/Demo1.c b/MCU/DevDriverDemo/Demo1.c new file mode 100644 index 0000000..6088bbc --- /dev/null +++ b/MCU/DevDriverDemo/Demo1.c @@ -0,0 +1,140 @@ +/** + * @file Demo1.c + * @author Rich Chan + * @brief + * @version 0.1 + * @date 2020-08-01 + * + * @copyright Copyright (c) 2020 + * + */ +#include "Device.h" + +typedef struct DEMO1_OBJ +{ + // Some private data for instance + uint8_t ID; +}ST_DEMO1_OBJ; + + +ST_DEMO1_OBJ Demo1Obj[2]; + +static EU_ERR Demo1Init(ST_DEVICE* dev) +{ + ST_DEMO1_OBJ* obj = (ST_DEMO1_OBJ*)dev->Prv; + + if(&Demo1Obj[0]==obj) + { + // Do something. + return EC_OK; + } + else if(&Demo1Obj[1]==obj) + { + // Do something. + return EC_OK; + } + return EC_ERESOURCES; +} + +static EU_ERR Demo1Open(ST_DEVICE* dev) +{ + ST_DEMO1_OBJ* obj = (ST_DEMO1_OBJ*)dev->Prv; + + if(&Demo1Obj[0]==obj) + { + // Do something. + return EC_OK; + } + else if(&Demo1Obj[1]==obj) + { + // Do something. + return EC_OK; + } + return EC_ERESOURCES; +} + +/** + * @brief + * + * @param dev + * @param ex contains additional control information. + * @param buf data pointer to be write. + * @param nbytes data size. + * @param timeout + * @return int64_t real size readed. + */ +static int64_t Demo1Read(ST_DEVICE* dev, void* ex, void* buf, int64_t nbytes, uint32_t timeout) +{ + ST_DEMO1_OBJ* obj = (ST_DEMO1_OBJ*)dev->Prv; + int64_t rsize; + + UNUSED(timeout); + if(&Demo1Obj[0]==obj) + { + // Do something. + return nbytes; + } + else if(&Demo1Obj[1]==obj) + { + // Do something. + return nbytes; + } + return EC_EPAM; +} + +/** + * @brief Same as Demo1Read + * + * @param dev + * @param ex Same as Demo1Read + * @param buf + * @param nbytes + * @param timeout + * @return int64_t + */ +static int64_t Demo1Write(ST_DEVICE* dev, void* ex, void* buf, int64_t nbytes, uint32_t timeout) +{ + ST_MFIS_OBJ* obj = (ST_MFIS_OBJ*)dev->Prv; + + UNUSED(timeout); + if(&Demo1Obj[0]==obj) + { + // Do something. + return nbytes; + } + else if(&Demo1Obj[1]==obj) + { + // Do something. + return nbytes; + } + return EC_EPAM; +} + +ST_DEVICE Demo1[2] = { + { + .Init = Demo1Init, + .Open = Demo1Open, + .Close = NULL, + .Read = Demo1Read, + .Write = Demo1Write, + .Seek = NULL, + .IOCtl = NULL, + .PwCtl = NULL, + .EnInt = NULL, + .DisInt = NULL, + .Prv = (void*)&Demo1Obj[0], + }, + { + .Init = Demo1Init, + .Open = Demo1Open, + .Close = NULL, + .Read = Demo1Read, + .Write = Demo1Write, + .Seek = NULL, + .IOCtl = NULL, + .PwCtl = NULL, + .EnInt = NULL, + .DisInt = NULL, + .Prv = (void*)&Demo1Obj[1], + } +}; diff --git a/MCU/DevDriverDemo/Device.c b/MCU/DevDriverDemo/Device.c new file mode 100644 index 0000000..9a86e28 --- /dev/null +++ b/MCU/DevDriverDemo/Device.c @@ -0,0 +1,168 @@ +/** + * @file Device.c + * @author Rich Chan + * @brief + * @version 0.1 + * @date 2020-04-01 + * + * @copyright Copyright (c) 2020 + * + */ +#include "Device.h" + +extern ST_DEVICE Demo0[2]; +extern ST_DEVICE Demo1[2]; + +ST_DEVICE* Devices[EC_DEV_END] = { + &Demo0[0], + &Demo0[1], + &Demo1[0], + &Demo1[1], +}; + +void DevInit(void) +{ + EU_DEVID i; + + for(i=EC_DEVID_FST+1; iInit) + Devices[i]->Init(Devices[i]); + } +} + +EU_ERR DevOpen(EU_DEVID id) +{ + EU_ERR er = EC_OK; + ST_DEVICE* dev; + + ASSERT(EC_DEVID_FSTid); + dev = Devices[id]; + ASSERT(NULL!=dev); + ASSERT(NULL!=dev->Open); + + if(!dev->Opened) + er = dev->Open(dev); + if(EC_OK==er) + dev->Opened++; + + return er; +} + +/** + * @brief DevClose Device close function is not unnecessary. + * @param id + */ +void DevClose(EU_DEVID id) +{ + ST_DEVICE* dev; + + ASSERT(EC_DEVID_FSTid); + dev = Devices[id]; + ASSERT(NULL!=dev); + + if(!dev->Opened) + return; + + dev->Opened--; + if(!dev->Opened && NULL!=dev->Close) + dev->Close(dev); +} + +int64_t DevRead(EU_DEVID id, void* ex, void* buf, int64_t nbytes, uint32_t timeout) +{ + ST_DEVICE* dev; + + ASSERT(EC_DEVID_FSTid); + dev = Devices[id]; + ASSERT(NULL!=dev); + ASSERT(NULL!=dev->Read); + + if(!dev->Opened) + return EC_ENOPEN; + + return dev->Read(dev, ex, buf, nbytes, timeout); +} + +int64_t DevWrite(EU_DEVID id, void* ex, void* buf, int64_t nbytes, uint32_t timeout) +{ + ST_DEVICE* dev; + + ASSERT(EC_DEVID_FSTid); + dev = Devices[id]; + ASSERT(NULL!=dev); + ASSERT(NULL!=dev->Write); + + if(!dev->Opened) + return EC_ENOPEN; + + return dev->Write(dev, ex, buf, nbytes, timeout); +} + +EU_ERR DevIOCtl(EU_DEVID id, uint32_t ctl, void* ex, void* data) +{ + ST_DEVICE* dev; + + ASSERT(EC_DEVID_FSTid); + dev = Devices[id]; + ASSERT(NULL!=dev); + ASSERT(NULL!=dev->IOCtl); + + if(!dev->Opened) + return EC_ENOPEN; + + return dev->IOCtl(dev, ctl, ex, data); +} + +EU_ERR DevPwCtl(EU_DEVID id, EU_PW_LEVEL level) +{ + ST_DEVICE* dev; + + ASSERT(EC_DEVID_FSTid); + dev = Devices[id]; + ASSERT(NULL!=dev); + ASSERT(NULL!=dev->PwCtl); + + if(!dev->Opened) + return EC_ENOPEN; + + return dev->PwCtl(dev, level); +} + +EU_ERR DevEnInt(EU_DEVID id, void* ex, IntHdl handle, void* arg) +{ + ST_DEVICE* dev; + + ASSERT(EC_DEVID_FSTid); + dev = Devices[id]; + ASSERT(NULL!=dev); + ASSERT(NULL!=dev->EnInt); + + if(!dev->Opened) + return EC_ENOPEN; + + return dev->EnInt(dev, ex, handle, arg); +} + +EU_ERR DevDisInt(EU_DEVID id, void* ex) +{ + ST_DEVICE* dev; + + ASSERT(EC_DEVID_FSTid); + dev = Devices[id]; + ASSERT(NULL!=dev); + ASSERT(NULL!=dev->DisInt); + + if(!dev->Opened) + return EC_ENOPEN; + + return dev->DisInt(dev, ex); +} \ No newline at end of file diff --git a/MCU/DevDriverDemo/Device.h b/MCU/DevDriverDemo/Device.h new file mode 100644 index 0000000..81d5894 --- /dev/null +++ b/MCU/DevDriverDemo/Device.h @@ -0,0 +1,71 @@ + /** + * @file Device.h + * @author Rich Chan + * @brief + * @version 0.1 + * @date 2020-04-01 + * + * @copyright Copyright (c) 2020 + * + */ + #ifndef DEVICE_H + #define DEVICE_H + + #include "Type.h" + + typedef enum DEVID + { + EC_DEV_FST = -1, + EC_DEV_DEMO00 = 0, + EC_DEV_DEMO01 = 1, + EC_DEV_DEMO10 = 2, + EC_DEV_DEMO11 = 3, + EC_DEV_END + }EU_DEVID; + + typedef void (*IntHdl)(uint32_t id, void* arg); + + typedef enum PW_LEVEL + { + EC_PW_S0, // Normal + EC_PW_S1, // Sleep + EC_PW_S2, // Stop + EC_PW_S3, // Standby + EC_PW_S4, // Poweroff + }EU_PW_LEVEL; + + typedef struct DEVICE + { + uint32_t Opened; + EU_ERR (*Init)(struct DEVICE*); // Unnecessary + EU_ERR (*Open)(struct DEVICE*); // Necessary + void (*Close)(struct DEVICE*); // Unnecessary + int64_t (*Read)(struct DEVICE*, void*, void*, int64_t, uint32_t); // Unnecessary + int64_t (*Write)(struct DEVICE*, void*, void*, int64_t, uint32_t); // Unnecessary + int (*Seek)(struct DEVICE*, int); // Unnecessary + EU_ERR (*IOCtl)(struct DEVICE*, uint32_t, void*, void*); // Unnecessary + EU_ERR (*PwCtl)(struct DEVICE*, EU_PW_LEVEL); // Unnecessary + EU_ERR (*EnInt)(struct DEVICE*, void*, IntHdl, void*); // Unnecessary + EU_ERR (*DisInt)(struct DEVICE*, void*); // Unnecessary + void* Prv; // Unnecessary + }ST_DEVICE; + + + void DevInit(void); + EU_ERR DevOpen(EU_DEVID id); + void DevClose(EU_DEVID id); + int64_t DevRead(EU_DEVID id, void* ex, void* buf, int64_t nbytes, uint32_t timeout); + int64_t DevWrite(EU_DEVID id, void* ex, void* buf, int64_t nbytes, uint32_t timeout); + EU_ERR DevIOCtl(EU_DEVID id, uint32_t ctl, void* ex, void* data); + EU_ERR DevPwCtl(EU_DEVID id, EU_PW_LEVEL level); + EU_ERR DevEnInt(EU_DEVID id, void* ex, IntHdl handle, void* arg); + EU_ERR DevDisInt(EU_DEVID id, void* ex); + + typedef enum IOCTL + { + EC_IOCTL_DEMO_CTL0, + EC_IOCTL_DEMO_CTL1, + EC_IOCTL_DEM1_CTL1 + }EU_IOCTL; + + #endif // DEVICE_H diff --git a/MCU/DevDriverDemo/Type.h b/MCU/DevDriverDemo/Type.h new file mode 100644 index 0000000..4ba10d7 --- /dev/null +++ b/MCU/DevDriverDemo/Type.h @@ -0,0 +1,32 @@ +/** + * @file Type.h + * @author Rich Chan (chenyang@auotai.com) + * @brief + * @version 0.1 + * @date 2020-03-27 + * + * @copyright Copyright (c) 2020 + * + */ +#ifndef TYPE_H +#define TYPE_H + +#include +#include + +#define UNUSED(x) ((void)(x)) + +typedef enum ERR +{ + EC_BUSY = -8, // Resource is busy. + EC_ERESOURCES = -7, // No Resources. + EC_EOF = -6, // End. + EC_ENSUPPORT = -5, // Not supported. + EC_ENOPEN = -4, // Not opened. + EC_ETIMEOUT = -3, // Timeout. + EC_EPAM = -2, // Wrong parameter. + EC_ERROR = -1, // Generic error. + EC_OK = 0 // No error. +}EU_ERR; + +#endif // TYPE_H \ No newline at end of file diff --git a/MCU/DevDriverDemo/main.c b/MCU/DevDriverDemo/main.c new file mode 100644 index 0000000..662cf23 --- /dev/null +++ b/MCU/DevDriverDemo/main.c @@ -0,0 +1,25 @@ +/** + * @file main.c + * @author Rich Chan + * @brief + * @version 0.1 + * @date 2020-08-01 + * + * @copyright Copyright (c) 2020 + * + */ +#include "Device.h" + +int main(void) +{ + uint8_t data[32]; + + // Demo Device 0 obj 0 write. + DevOpen(EC_DEV_DEMO00); + DevWrite(EC_DEV_DEMO00, NULL, data, 32, 0); + + // Demo Device 1 obj 1 write. + DevOpen(EC_DEV_DEMO11); + DevWrite(EC_DEV_DEMO11, NULL, data, 32, 0); + reutn 0; +}