⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dcc.c

📁 MCU123开发光盘上LPC23XX的UCOS
💻 C
字号:
/*********************************************************************
*              SEGGER MICROCONTROLLER SYSTEME GmbH                   *
*        Solutions for real time microcontroller applications        *
**********************************************************************
*                                                                    *
*           (c) 2006  SEGGER Microcontroller Systeme GmbH            *
*                                                                    *
*      Internet: www.segger.com   Support: support@segger.com        *
*                                                                    *
**********************************************************************
----------------------------------------------------------------------
File    : DCC.c
Purpose : DCC handler
---------------------------END-OF-HEADER------------------------------
*/

#pragma diag_suppress=Pe940     // IAR specific: No warning for missing return

/*********************************************************************
*
*       Defines, configurable
*
**********************************************************************
*/

#define SUPPORT_READ_U8     1   // Support byte reads if 1. 0 to disable.
#define SUPPORT_READ_U16    1   // Support half word reads if 1. 0 to disable.
#define SUPPORT_READ_U32    1   // Support word reads if 1. 0 to disable.
#define SUPPORT_WRITE_U8    1   // Support byte writes if 1. 0 to disable.
#define SUPPORT_WRITE_U16   1   // Support half word writes if 1. 0 to disable.
#define SUPPORT_WRITE_U32   1   // Support word writes if 1. 0 to disable.

/*********************************************************************
*
*       Defines, non- configurable
*
**********************************************************************
*/

#define U8  unsigned char
#define U16 unsigned short
#define U32 unsigned int

#define DCC_OP_READ_U32   0x01000000
#define DCC_OP_READ_U16   0x02000000
#define DCC_OP_READ_U8    0x04000000
#define DCC_OP_GET_CAPS   0x08000000
#define DCC_OP_WRITE_U32  0x10000000
#define DCC_OP_WRITE_U16  0x20000000
#define DCC_OP_WRITE_U8   0x40000000
#define DCC_OP_ODD_ADDR   0x80000000
#define DCC_OP_COMMAND    0x00000001

#define DCC_CAP_READ_U32  0x01
#define DCC_CAP_READ_U16  0x02
#define DCC_CAP_READ_U8   0x04
#define DCC_CAP_WRITE_U32 0x10
#define DCC_CAP_WRITE_U16 0x20
#define DCC_CAP_WRITE_U8  0x40

#define DCC_SIGNATURE     0x91CA0000

#define DCC_CONFIG       ((DCC_CAP_READ_U8   * SUPPORT_READ_U8)    \
                        | (DCC_CAP_READ_U16  * SUPPORT_READ_U16)   \
                        | (DCC_CAP_READ_U32  * SUPPORT_READ_U32)   \
                        | (DCC_CAP_WRITE_U8  * SUPPORT_WRITE_U8)   \
                        | (DCC_CAP_WRITE_U16 * SUPPORT_WRITE_U16)  \
                        | (DCC_CAP_WRITE_U32 * SUPPORT_WRITE_U32))

#define SUPPORT_WRITE   (SUPPORT_WRITE_U8 | SUPPORT_WRITE_U16 | SUPPORT_WRITE_U32)

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/

static U32 _Command;
static U32 _Addr;
static U32 _NumReadItems;

#if (SUPPORT_WRITE)
  static U32 _Data;
#endif

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/
/*********************************************************************
*
*       _ReadDCCStat
*/
static __arm int _ReadDCCStat(void) {
  __asm("mrc  P14,0,R0,C0,C0");        // Value read is in R0, which is used as return value
}

/*********************************************************************
*
*       _ReadDCC
*/
static __arm U32 _ReadDCC(void) {
  __asm("mrc  P14,0,R0,C1,C0");        // Value read is in R0, which is used as return value
}

/*********************************************************************
*
*       _WriteDCC
*/
static __arm void _WriteDCC(U32 Data) {
  __asm("mcr  P14,0,R0,C1,C0");
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       DCC_Process
*
*  Function description
*    This function should be called more or less regularily to allow
*    memory reads while the application progam is running.
*    The more often it is called, the higher the memory read speed.
*/
void DCC_Process(void) {
  U32 Data;
  if (_ReadDCCStat() & 1) {
    Data = _ReadDCC();
    if (Data & DCC_OP_COMMAND) {
      _Command = Data;
#if (DCC_CONFIG & (DCC_CAP_READ_U8 | DCC_CAP_WRITE_U8))
      if (_Command & DCC_OP_ODD_ADDR) {
        _Addr |= 1;
      }
#endif
      if (_Command & (DCC_OP_READ_U32 | DCC_OP_READ_U16 | DCC_OP_READ_U8 | DCC_OP_GET_CAPS)) {
        _NumReadItems = (_Command >> 2) & 0xffff;
      } else {
#if (DCC_CONFIG & DCC_CAP_WRITE_U32)
        if (_Command & DCC_OP_WRITE_U32) {
          _Data |= (_Command << 14) & 0xffff0000;
        } else
#endif
#if (SUPPORT_WRITE)
        {
          _Data = (_Command >> 2) & 0xffff;
        }
#endif
#if (DCC_CONFIG & DCC_CAP_WRITE_U8)
        if (_Command & DCC_OP_WRITE_U8) {
          *(U8*)_Addr = _Data;
          _Addr += 1;
        }
#endif
#if (DCC_CONFIG & DCC_CAP_WRITE_U16)
        if (_Command & DCC_OP_WRITE_U16) {
          *(U16*)_Addr = _Data;
          _Addr += 2;
        }
#endif
#if (DCC_CONFIG & DCC_CAP_WRITE_U32)
        if (_Command & DCC_OP_WRITE_U32) {
          *(U32*)_Addr =_Data;
          _Addr += 4;
        }
#endif
      }
      return;
    }
    _Addr = Data;
  }
  if (_NumReadItems) {
    if ((_ReadDCCStat() & 2) == 0) {
      Data = (DCC_CONFIG | DCC_SIGNATURE);
#if (DCC_CONFIG & DCC_CAP_READ_U8)
      if (_Command & DCC_OP_READ_U8) {
        Data = *(U8*)_Addr;
        _Addr += 1;
      }
#endif
#if (DCC_CONFIG & DCC_CAP_READ_U16)
      if (_Command & DCC_OP_READ_U16) {
        Data = *(U16*)_Addr;
        _Addr += 2;
      }
#endif
#if (DCC_CONFIG & DCC_CAP_READ_U32)
      if (_Command & DCC_OP_READ_U32) {
        Data = *(U32*)_Addr;
        _Addr += 4;
      }
#endif
      _WriteDCC(Data);
      _NumReadItems--;
    }
  }
}

/*************************** end of file ****************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -