gonzui


Format: Advanced Search

mtkernel_3/device/i2c/i2c.cbare sourcepermlink (0.05 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 "i2c.h"
   18: #include "../include/dev_def.h"
   19: #if DEV_IIC_ENABLE
   20: 
   21: /*
   22:  *      i2c.c
   23:  *      I2C device driver
   24: */
   25: 
   26: /*---------------------------------------------------------------------*/
   27: /*I2C Device driver Control block
   28:  */
   29: #if TK_SUPPORT_MEMLIB
   30: 
   31: LOCAL T_I2C_DCB *dev_i2c_cb[DEV_I2C_UNITNM];
   32: #define         get_dcb_ptr(unit)      (dev_i2c_cb[unit])
   33: #define         get_dcb_mem(unit,a)    (&dev_i2c_cb[unit]->a)
   34: 
   35: #else
   36: 
   37: LOCAL T_I2C_DCB dev_i2c_cb[DEV_I2C_UNITNM];
   38: #define         get_dcb_ptr(unit)      (&dev_I2C_cb[unit])
   39: #define         get_dcb_mem(unit,a)    (&dev_i2c_cb[unit].a)
   40: 
   41: #endif
   42: 
   43: /*---------------------------------------------------------------------*/
   44: /* Attribute data control
   45:  */
   46: LOCAL ER read_atr(T_I2C_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_I2C_DCB *p_dcb, T_DEVREQ *req)
   70: {
   71:         T_I2C_EXEC     *p_ex;
   72:         W              rtn;
   73:         ER             err        = E_OK;
   74: 
   75:         switch(req->start) {
   76:         case TDN_EVENT:                        /* MBF ID for event notification */
   77:                 if(req->size >= sizeof(ID)) {
   78:                         p_dcb->evtmbfid = *(ID*)(req->buf);
   79:                 } else if(req->size != 0) {
   80:                         err = E_PAR;
   81:                         break;
   82:                 }
   83:                 req->asize = sizeof(ID);
   84:                 break;
   85:         case TDN_I2C_EXEC:
   86:                 if(req->size >= sizeof(T_I2C_EXEC)) {
   87:                         p_ex = (T_I2C_EXEC*)(req->buf);
   88:                         if(p_ex->snd_size <=0 || p_ex->snd_size > DEVCNF_I2C_MAX_SDATSZ) return E_PAR;
   89:                         if(p_ex->rcv_size <=0 || p_ex->rcv_size > DEVCNF_I2C_MAX_RDATSZ) return E_PAR;
   90: 
   91:                         rtn = dev_i2c_llctl(p_dcb->unit, LLD_I2C_EXEC, req->start, req->size, (UW*)p_ex);
   92:                         if(rtn > 0) {
   93:                                 req->asize = sizeof(T_I2C_EXEC);
   94:                         } else {
   95:                                 err = (ER)rtn;
   96:                         }
   97:                 } else if(req->size == 0){
   98:                         req->asize = sizeof(T_I2C_EXEC);
   99:                 } else {
  100:                         err = E_PAR;
  101:                 }
  102:                 break;
  103: 
  104:         default:
  105:                 err = E_PAR;
  106:                 break;
  107:         }
  108: 
  109:         return err;
  110: }
  111: 
  112: /*---------------------------------------------------------------------*/
  113: /*Device-specific data control
  114:  */
  115: LOCAL ER read_data(T_I2C_DCB *p_dcb, T_DEVREQ *req)
  116: {
  117:         W      rtn;
  118:         ER     err = E_OK;
  119: 
  120:         if(req->size <=0 || req->size > DEVCNF_I2C_MAX_RDATSZ) return E_PAR;
  121: 
  122:         if(req->size) {
  123:                 rtn = dev_i2c_llctl(p_dcb->unit, LLD_I2C_READ, req->start, req->size, req->buf);
  124:                 if(rtn > 0) {
  125:                         req->asize = rtn;
  126:                 } else {
  127:                         err = (ER)rtn;
  128:                 }
  129: 
  130:         } else {
  131:                 req->asize = req->size;
  132:         }
  133: 
  134:         return err;
  135: }
  136: 
  137: 
  138: LOCAL ER write_data(T_I2C_DCB *p_dcb, T_DEVREQ *req)
  139: {
  140:         W      rtn;
  141:         ER     err = E_OK;
  142: 
  143:         if(req->size <=0 || req->size > DEVCNF_I2C_MAX_SDATSZ) return E_PAR;
  144: 
  145:         if(req->size) {
  146:                 rtn = dev_i2c_llctl(p_dcb->unit, LLD_I2C_WRITE, req->start, req->size, req->buf);
  147:                 if(rtn > 0) {
  148:                         req->asize = rtn;
  149:                 } else {
  150:                         err = (ER)rtn;
  151:                 }
  152: 
  153:         } else {
  154:                 req->asize = req->size;
  155:         }
  156: 
  157: 
  158:         return err;
  159: }
  160: 
  161: /*-------------------------------------------------------
  162:  * mSDI I/F function
  163:  *-------------------------------------------------------
  164:  */
  165: /*----------------------------------------------------------------------
  166:  * Open device
  167:  */
  168: ER dev_i2c_openfn( ID devid, UINT omode, T_MSDI *msdi)
  169: {
  170:         T_I2C_DCB      *p_dcb;
  171:         ER             err;
  172: 
  173:         p_dcb = (T_I2C_DCB*)(msdi->dmsdi.exinf);
  174: 
  175:         /* Device Open operation */
  176:         p_dcb->omode = omode;
  177:         err = (ER)dev_i2c_llctl(p_dcb->unit, LLD_I2C_OPEN, 0, 0, 0);
  178: 
  179:         return err;
  180: }
  181: 
  182: /*----------------------------------------------------------------------
  183:  * Close Device
  184:  */
  185: ER dev_i2c_closefn( ID devid, UINT option, T_MSDI *msdi)
  186: {
  187:         T_I2C_DCB      *p_dcb;
  188:         ER             err;
  189: 
  190:         p_dcb = (T_I2C_DCB*)(msdi->dmsdi.exinf);
  191: 
  192:         /* Device Close operation */
  193:         err = (ER)dev_i2c_llctl(p_dcb->unit, LLD_I2C_CLOSE, 0, 0, 0);
  194: 
  195:         return err;
  196: }
  197: 
  198: /*----------------------------------------------------------------------
  199:  * Read Device
  200:  */
  201: ER dev_i2c_readfn( T_DEVREQ *req, T_MSDI *p_msdi)
  202: {
  203:         T_I2C_DCB      *p_dcb;
  204:         ER             err;
  205: 
  206:         p_dcb = (T_I2C_DCB*)(p_msdi->dmsdi.exinf);
  207: 
  208:         if(req->start >= 0) {
  209:                 // Device specific data
  210:                 err = read_data( p_dcb, req);
  211:         } else {
  212:                 // Device attribute data
  213:                 err = read_atr( p_dcb, req);
  214:         }
  215: 
  216:         return err;
  217: }
  218: 
  219: /*----------------------------------------------------------------------
  220:  * Write Device
  221:  */
  222: ER dev_i2c_writefn( T_DEVREQ *req, T_MSDI *p_msdi)
  223: {
  224:         T_I2C_DCB      *p_dcb;
  225:         ER             rtn;
  226: 
  227:         p_dcb = (T_I2C_DCB*)(p_msdi->dmsdi.exinf);
  228: 
  229:         if(req->start >= 0) {
  230:                 // Device specific data
  231:                 rtn = write_data( p_dcb, req);
  232:         } else {
  233:                 // Device attribute data
  234:                 rtn = write_atr( p_dcb, req);
  235:         }
  236: 
  237:         return rtn;
  238: }
  239: 
  240: /*----------------------------------------------------------------------
  241:  * Event Device
  242:  */
  243: ER dev_i2c_eventfn( INT evttyp, void *evtinf, T_MSDI *msdi)
  244: {
  245:         return E_NOSPT;
  246: }
  247: 
  248: /*----------------------------------------------------------------------
  249:  * Serial communication Device initialization and registration
  250:  */
  251: EXPORT ER dev_init_i2c( UW unit )
  252: {
  253:         T_I2C_DCB      *p_dcb;
  254:         T_IDEV         idev;
  255:         T_MSDI         *p_msdi;
  256:         T_DMSDI                dmsdi;
  257:         ER             err;
  258:         INT            i;
  259: 
  260:         if( unit >= DEV_I2C_UNITNM) return E_PAR;
  261: 
  262: #if TK_SUPPORT_MEMLIB
  263:         p_dcb = (T_I2C_DCB*)Kmalloc(sizeof(T_I2C_DCB));
  264:         if( p_dcb == NULL) return E_NOMEM;
  265:         dev_i2c_cb[unit]       = p_dcb;
  266: #else
  267:         p_dcb = &dev_i2c_cb[unit];
  268: #endif
  269: 
  270:         /* Device registration information */
  271:         dmsdi.exinf    = p_dcb;
  272:         dmsdi.drvatr   = 0;                      /* Driver attributes */
  273:         dmsdi.devatr   = TDK_UNDEF;              /* Device attributes */
  274:         dmsdi.nsub     = 0;                        /* Number of sub units */
  275:         dmsdi.blksz    = 1;                       /* Unique data block size (-1 = unknown) */
  276:         dmsdi.openfn   = dev_i2c_openfn;
  277:         dmsdi.closefn  = dev_i2c_closefn;
  278:         dmsdi.readfn   = dev_i2c_readfn;
  279:         dmsdi.writefn  = dev_i2c_writefn;
  280:         dmsdi.eventfn  = dev_i2c_eventfn;
  281:         
  282:         knl_strcpy( (char*)dmsdi.devnm, DEVCNF_I2C_DEVNAME);
  283:         i = knl_strlen(DEVCNF_I2C_DEVNAME);
  284:         dmsdi.devnm[i] = (UB)('a' + unit);
  285:         dmsdi.devnm[i+1] = 0;
  286: 
  287:         err = msdi_def_dev( &dmsdi, &idev, &p_msdi);
  288:         if(err != E_OK) goto err_2;
  289: 
  290:         p_dcb->unit    = unit;
  291:         p_dcb->evtmbfid = idev.evtmbfid;
  292: 
  293:         /* Low-level device initialization */
  294:         err = dev_i2c_llinit( get_dcb_ptr(unit));
  295:         if(err != E_OK) goto err_1;
  296: 
  297:         /* Set default value for attribute data */
  298: 
  299:         return E_OK;
  300: 
  301: err_1:
  302:         msdi_del_dev(p_msdi);
  303: err_2:
  304:         Kfree(p_dcb);
  305:         return err;
  306: }
  307: 
  308: #endif          /* DEV_IIC_ENABLE */