gonzui


Format: Advanced Search

mtkernel_3/device/adc/adc.cbare sourcepermlink (0.04 seconds)

Search this content:

    1: /*
    2:  *----------------------------------------------------------------------
    3:  *    Device Driver for micro T-Kernel for μT-Kernel 3.0
    4:  *
    5:  *    Copyright (C) 2020-2021 by Ken Sakamura.
    6:  *    This software is distributed under the T-License 2.2.
    7:  *----------------------------------------------------------------------
    8:  *
    9:  *    Released by TRON Forum(http://www.tron.org) at 2021/08.
   10:  *
   11:  *----------------------------------------------------------------------
   12:  */
   13: #include <sys/machine.h>
   14: #include <tk/tkernel.h>
   15: #include <tstdlib.h>
   16: 
   17: #include "adc.h"
   18: #include "../include/dev_def.h"
   19: #if DEV_ADC_ENABLE
   20: /*
   21:  *      adc.c
   22:  *      A/D converter device driver 
   23: */
   24: 
   25: 
   26: /*----------------------------------------------------------------------*/
   27: /* Device driver Control block
   28:  */
   29: #if TK_SUPPORT_MEMLIB
   30: 
   31: LOCAL T_ADC_DCB *dev_adc_cb[DEV_ADC_UNITNM];
   32: #define         get_dcb_ptr(unit)      (dev_adc_cb[unit])
   33: #define         get_dcb_mem(unit,a)    (dev_adc_cb[unit]->a)
   34: 
   35: #else
   36: 
   37: LOCAL T_ADC_DCB dev_adc_cb[DEV_ADC_UNITNM];
   38: #define         get_dcb_ptr(unit)      (&dev_adc_cb[unit])
   39: #define         get_dcb_mem(unit,a)    (dev_adc_cb[unit].a)
   40: 
   41: #endif  /* TK_SUPPORT_MEMLIB */
   42: 
   43: /*----------------------------------------------------------------------*/
   44: /* Attribute data control
   45:  */
   46: LOCAL ER read_atr(T_ADC_DCB *p_dcb, T_DEVREQ *req)
   47: {
   48:         ER     err = E_OK;
   49: 
   50:         switch(req->start) {
   51:         case TDN_EVENT:                        /* MBF ID for event notification */
   52:                 if(req->size >= sizeof(ID)) {
   53:                         *(ID*)req->buf = p_dcb->evtmbfid;
   54:                 } else if(req->size != 0) {
   55:                         err = E_PAR;
   56:                         break;
   57:                 }
   58:                 req->asize = sizeof(ID);
   59:                 break;
   60:         default:
   61:                 err = E_PAR;
   62:                 break;
   63:         }
   64: 
   65:         return err;
   66: }
   67: 
   68: 
   69: LOCAL ER write_atr(T_ADC_DCB *p_dcb, T_DEVREQ *req)
   70: {
   71:         ER     err = E_OK;
   72: 
   73:         switch(req->start) {
   74:         case TDN_EVENT:                        /* MBF ID for event notification */
   75:                 if(req->size >= sizeof(ID)) {
   76:                         p_dcb->evtmbfid = *(ID*)req->buf;
   77:                 } else if(req->size != 0) {
   78:                         err = E_PAR;
   79:                         break;
   80:                 }
   81:                 req->asize = sizeof(ID);
   82:                 break;
   83:         default:
   84:                 err = E_PAR;
   85:                 break;
   86:         }
   87: 
   88:         return err;
   89: }
   90: 
   91: /*----------------------------------------------------------------------*/
   92: /* Device-specific data control
   93:  */
   94: LOCAL ER read_data(T_ADC_DCB *p_dcb, T_DEVREQ *req)
   95: {
   96:         W      rtn;
   97:         ER     err = E_OK;
   98: 
   99:         if(req->size) {
  100:                 rtn = dev_adc_llctl( p_dcb->unit, LLD_ADC_READ, req->start, req->size, req->buf);
  101:                 if(rtn > 0) {
  102:                         req->asize = rtn;
  103:                 } else {
  104:                         err = (ER)rtn;
  105:                 }
  106:         } else {
  107:                 req->asize = dev_adc_llctl( p_dcb->unit, LLD_ADC_RSIZE, req->start, 0, 0);
  108:         }
  109: 
  110:         return err;
  111: }
  112: 
  113: 
  114: /*----------------------------------------------------------------------
  115:  * mSDI I/F function
  116:  */
  117: /*----------------------------------------------------------------------
  118:  * Open device
  119:  */
  120: ER dev_adc_openfn( ID devid, UINT omode, T_MSDI *msdi)
  121: {
  122:         T_ADC_DCB      *p_dcb;
  123:         ER             err;
  124: 
  125:         p_dcb = (T_ADC_DCB*)(msdi->dmsdi.exinf);
  126: 
  127:         /* Device Open operation */
  128:         p_dcb->omode = omode;
  129:         err = (ER)dev_adc_llctl( p_dcb->unit, LLD_ADC_OPEN, 0, 0, 0);
  130: 
  131:         return err;
  132: }
  133: 
  134: /*----------------------------------------------------------------------
  135:  * Close Device
  136:  */
  137: ER dev_adc_closefn( ID devid, UINT option, T_MSDI *msdi)
  138: {
  139:         T_ADC_DCB      *p_dcb;
  140:         ER             err;
  141: 
  142:         p_dcb = (T_ADC_DCB*)(msdi->dmsdi.exinf);
  143: 
  144:         /* Device Close operation */
  145:         err =(ER)dev_adc_llctl( p_dcb->unit, LLD_ADC_CLOSE, 0, 0, 0);
  146:         
  147:         return err;
  148: }
  149: 
  150: /*----------------------------------------------------------------------
  151:  * Read Device
  152:  */
  153: ER dev_adc_readfn( T_DEVREQ *req, T_MSDI *p_msdi)
  154: {
  155:         T_ADC_DCB      *p_dcb;
  156:         ER             err;
  157: 
  158:         p_dcb = (T_ADC_DCB*)(p_msdi->dmsdi.exinf);
  159: 
  160:         if(req->start >= 0) {  // Device specific data
  161:                 if( p_dcb->omode & TD_READ ) {
  162:                         err = read_data( p_dcb, req);
  163:                 } else {
  164:                         err = E_OACV;
  165:                 }
  166:         } else {               // Device attribute data
  167:                 err = read_atr( p_dcb, req);
  168:         }
  169: 
  170:         return err;
  171: }
  172: 
  173: /*----------------------------------------------------------------------
  174:  * Write Device
  175:  */
  176: ER dev_adc_writefn( T_DEVREQ *req, T_MSDI *p_msdi)
  177: {
  178:         T_ADC_DCB      *p_dcb;
  179:         ER             err;
  180: 
  181:         p_dcb = (T_ADC_DCB*)(p_msdi->dmsdi.exinf);
  182: 
  183:         if(req->start >= 0) {  // Device specific data
  184:                 err = E_PAR;
  185:         } else {               // Device attribute data
  186:                 err = write_atr( p_dcb, req);
  187:         }
  188: 
  189:         return err;
  190: }
  191: 
  192: /*----------------------------------------------------------------------
  193:  * Event Device
  194:  */
  195: ER dev_adc_eventfn( INT evttyp, void *evtinf, T_MSDI *msdi)
  196: {
  197:         return E_NOSPT;
  198: }
  199: 
  200: /*----------------------------------------------------------------------
  201:  * Serial communication Device initialization and registration
  202:  */
  203: EXPORT ER dev_init_adc( UW unit )
  204: {
  205:         T_ADC_DCB      *p_dcb;
  206:         T_IDEV         idev;
  207:         T_MSDI         *p_msdi;
  208:         T_DMSDI                dmsdi;
  209:         ER             err;
  210:         INT            i;
  211: 
  212:         if( unit >= DEV_ADC_UNITNM) return E_PAR;
  213: 
  214: #if TK_SUPPORT_MEMLIB
  215:         p_dcb = (T_ADC_DCB*)Kmalloc(sizeof(T_ADC_DCB));
  216:         if( p_dcb == NULL) return E_NOMEM;
  217:         dev_adc_cb[unit]       = p_dcb;
  218: #else
  219:         p_dcb = &dev_adc_cb[unit];
  220: #endif  /* TK_SUPPORT_MEMLIB */
  221: 
  222:         /* Device registration information */
  223:         dmsdi.exinf    = p_dcb;
  224:         dmsdi.drvatr   = 0;                      /* Driver attributes */
  225:         dmsdi.devatr   = TDK_UNDEF;              /* Device attributes */
  226:         dmsdi.nsub     = 0;                        /* Number of subunits */
  227:         dmsdi.blksz    = 1;                       /* Unique data block size (-1 = unknown) */
  228:         dmsdi.openfn   = dev_adc_openfn;
  229:         dmsdi.closefn  = dev_adc_closefn;
  230:         dmsdi.readfn   = dev_adc_readfn;
  231:         dmsdi.writefn  = dev_adc_writefn;
  232:         dmsdi.eventfn  = dev_adc_eventfn;
  233:         
  234:         knl_strcpy( (char*)dmsdi.devnm, DEVCNF_ADC_DEVNAME);
  235:         i = knl_strlen(DEVCNF_ADC_DEVNAME);
  236:         dmsdi.devnm[i] = (UB)('a' + unit);
  237:         dmsdi.devnm[i+1] = 0;
  238: 
  239:         err = msdi_def_dev( &dmsdi, &idev, &p_msdi);
  240:         if(err != E_OK) goto err_2;
  241: 
  242:         p_dcb->unit    = unit;
  243:         p_dcb->evtmbfid = idev.evtmbfid;
  244: 
  245:         /* Low-level device initialization */
  246:         err = (ER)dev_adc_llinit( get_dcb_ptr(unit));
  247:         if(err != E_OK) goto err_1;
  248: 
  249:         return E_OK;
  250: 
  251: err_1:
  252:         msdi_del_dev(p_msdi);
  253: err_2:
  254:         Kfree(p_dcb);
  255:         return err;
  256: }
  257: 
  258: #endif  /* DEV_ADC_ENABLE */