DemoPublic/MCU/DevDriverDemo/Device.c

168 lines
2.7 KiB
C

/**
* @file Device.c
* @author Rick 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; i<EC_DEVID_END; i++)
{
if(NULL!=Devices[i] && NULL!=Devices[i]->Init)
Devices[i]->Init(Devices[i]);
}
}
EU_ERR DevOpen(EU_DEVID id)
{
EU_ERR er = EC_OK;
ST_DEVICE* dev;
ASSERT(EC_DEVID_FST<id);
ASSERT(EC_DEVID_END>id);
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_FST<id);
ASSERT(EC_DEVID_END>id);
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_FST<id);
ASSERT(EC_DEVID_END>id);
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_FST<id);
ASSERT(EC_DEVID_END>id);
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_FST<id);
ASSERT(EC_DEVID_END>id);
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_FST<id);
ASSERT(EC_DEVID_END>id);
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_FST<id);
ASSERT(EC_DEVID_END>id);
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_FST<id);
ASSERT(EC_DEVID_END>id);
dev = Devices[id];
ASSERT(NULL!=dev);
ASSERT(NULL!=dev->DisInt);
if(!dev->Opened)
return EC_ENOPEN;
return dev->DisInt(dev, ex);
}