gonzui


Format: Advanced Search

tkernel_2/driver/tef_em1d/clk/src/rtc.cbare sourcepermlink (0.02 seconds)

Search this content:

    1: /*
    2:  *----------------------------------------------------------------------
    3:  *    T-Kernel 2.0 Software Package
    4:  *
    5:  *    Copyright 2011 by Ken Sakamura.
    6:  *    This software is distributed under the latest version of T-License 2.x.
    7:  *----------------------------------------------------------------------
    8:  *
    9:  *    Released by T-Engine Forum(http://www.t-engine.org/) at 2011/05/17.
   10:  *    Modified by T-Engine Forum at 2014/07/28.
   11:  *    Modified by TRON Forum(http://www.tron.org/) at 2015/06/01.
   12:  *
   13:  *----------------------------------------------------------------------
   14:  */
   15: 
   16: /*
   17:  *      rtc.c        (clkdrv)
   18:         clock driver RTC handler
   19:  *
   20:  */
   21: 
   22: #include "clkdrv.h"
   23: #include <stdlib.h>
   24: #include <device/em1d512_iic.h>
   25: 
   26: /* RX-4581NB RTC definitions */
   27: #define rxSEC           0x0
   28: #define rxMIN           0x1
   29: #define rxHOUR          0x2
   30: #define rxWEEK          0x3
   31: #define rxDAY           0x4
   32: #define rxMONTH         0x5
   33: #define rxYEAR          0x6
   34: #define rxEXT           0xd
   35: #define rxFLAG          0xe
   36: #define rxCTRL          0xf
   37: 
   38: /* number of retries */
   39: #define RETRY           4
   40: 
   41: /* SPI channel used by RTC */
   42: #define SPICH_RTC       2     // SP0:CS2
   43: 
   44: /* binary <-> BCD conversion */
   45: #define BCDtoBIN(v)     (((v) >> 4) * 10 + ((v) & 0xF))
   46: #define BINtoBCD(v)     ((((v) / 10) << 4) + ((v) % 10))
   47: 
   48: 
   49: /* RTC access interface */
   50: LOCAL   W rtcRead(W reg)
   51: {
   52:         W      i, er;
   53:         UB     xmit[2], recv[2];
   54: 
   55:         xmit[0] = recv[0] = (reg & 0x0f) | 0x80;
   56:         xmit[1] = recv[1] = ~0;
   57: 
   58:         for (i = 0; i < RETRY; i++) {
   59:                 er = em1d512_spixfer(SPICH_RTC, xmit, recv, sizeof(xmit));
   60:                 if (er >= E_OK) break;
   61:         }
   62: 
   63:         return recv[1];
   64: }
   65: 
   66: LOCAL   void      rtcWrite(W reg, W dat)
   67: {
   68:         W      i, er;
   69:         UB     xmit[2];
   70: 
   71:         xmit[0] = reg & 0x0f;
   72:         xmit[1] = dat;
   73: 
   74:         for (i = 0; i < RETRY; i++) {
   75:                 er = em1d512_spixfer(SPICH_RTC, xmit, NULL, sizeof(xmit));
   76:                 if (er >= E_OK) break;
   77:         }
   78: 
   79:         return;
   80: }
   81: 
   82: /* set current date time */
   83: EXPORT  ER       cdSetDateTime(void *date_tim)
   84: {
   85:         DATE_TIM       dt;
   86:         INT            year;
   87: 
   88:         /* read calendar date time */
   89:         dt = *((DATE_TIM *)date_tim);
   90: 
   91:         /* BIN -> BCD conversion */
   92:         year = (dt.d_year + 1900) % 100;
   93:         dt.d_year = BINtoBCD(year);
   94:         dt.d_month = BINtoBCD(dt.d_month);
   95:         dt.d_wday = BINtoBCD(dt.d_wday);
   96:         dt.d_day = BINtoBCD(dt.d_day);
   97:         dt.d_hour = BINtoBCD(dt.d_hour);
   98:         dt.d_min = BINtoBCD(dt.d_min);
   99:         dt.d_sec = BINtoBCD(dt.d_sec);
  100: 
  101:         /* stop counter temporarily */
  102:         rtcWrite(rxCTRL, 0x02);
  103: 
  104:         /* set current date time */
  105:         rtcWrite(rxSEC, dt.d_sec);
  106:         rtcWrite(rxMIN, dt.d_min);
  107:         rtcWrite(rxHOUR,  dt.d_hour);
  108:         rtcWrite(rxWEEK, 0x01);                // we do not use day of the week
  109:         rtcWrite(rxDAY, dt.d_day);
  110:         rtcWrite(rxMONTH,  dt.d_month);
  111:         rtcWrite(rxYEAR, dt.d_year);
  112: 
  113:         /* resume counter */
  114:         rtcWrite(rxCTRL, 0x00);
  115: 
  116:         return E_OK;
  117: }
  118: 
  119: /* get current time */
  120: EXPORT  ER       cdGetDateTime(void *date_tim)
  121: {
  122:         DATE_TIM       dt;
  123:         INT    sec;
  124: 
  125:         /* get current time */
  126:         do {
  127:                 dt.d_sec   = rtcRead(rxSEC);
  128:                 dt.d_min   = rtcRead(rxMIN);
  129:                 dt.d_hour  = rtcRead(rxHOUR);
  130:                 dt.d_day   = rtcRead(rxDAY);
  131:                 dt.d_month = rtcRead(rxMONTH);
  132:                 dt.d_year  = rtcRead(rxYEAR);
  133:                 sec        = rtcRead(rxSEC);
  134:         } while (sec != dt.d_sec);     /* make sure data is read consistently in a whole second */
  135: 
  136:         dt.d_wday = 0; // not supported (0 - pretend it is sunday)
  137:         dt.d_days = 0; // not used
  138:         dt.d_week = 0; // not used
  139: 
  140:         dt.d_year  = BCDtoBIN(dt.d_year) + 100;        /* 00-99 -> 2000-2099 */
  141:         dt.d_month = BCDtoBIN(dt.d_month);
  142:         dt.d_wday  = BCDtoBIN(dt.d_wday);
  143:         dt.d_day   = BCDtoBIN(dt.d_day);
  144:         dt.d_hour  = BCDtoBIN(dt.d_hour);
  145:         dt.d_min   = BCDtoBIN(dt.d_min);
  146:         dt.d_sec   = BCDtoBIN(dt.d_sec);
  147: 
  148:         *((DATE_TIM *)date_tim) = dt;
  149: 
  150:         return E_OK;
  151: }
  152: 
  153: /* set / get automatic power-on time (not supported) */
  154: EXPORT  ER       cdSetAutoPwOn(void *date_tim) {return E_NOSPT;}
  155: EXPORT  ER       cdGetAutoPwOn(void *date_tim) {return E_NOSPT;}
  156: 
  157: /* read / write non-volatile register */
  158: EXPORT  INT      cdSetRegister(void *buf, INT size)   {return E_NOSPT;}
  159: EXPORT  INT      cdGetRegister(void *buf, INT size)   {return E_NOSPT;}
  160: 
  161: /* hardware initialization */
  162: EXPORT  ER       cdInitHardware(void)
  163: {
  164:         ER     er;
  165: 
  166:         er = em1d512_iicspi_svc(TRUE);
  167: 
  168:         if (er >= E_OK) {
  169:                 rtcWrite(rxEXT, 0x00);
  170:                 rtcWrite(rxFLAG, 0x00);
  171:                 rtcWrite(rxCTRL, 0x00);
  172:         }
  173: 
  174:         return er;
  175: }
  176: 
  177: /* hardware stop processing */
  178: EXPORT  ER       cdFinishHardware(void)
  179: {
  180:         /* Don't finish, because other function may use SPI/IIC service */
  181:         /* em1d512_iicspi_svc(FALSE); */
  182:         return E_OK;
  183: }