/******************************************************************************/
//
// Software License Agreement
//
// The software supplied herewith by Microchip Technology Incorporated
// (the Company) for its PICmicro® Microcontroller is intended and
// supplied to you, the Companys customer, for use solely and
// exclusively on Microchip PICmicro Microcontroller products. The
// software is owned by the Company and/or its supplier, and is
// protected under applicable copyright laws. All rights are reserved.
// Any use in violation of the foregoing restrictions may subject the
// user to criminal sanctions under applicable laws, as well as to
// civil liability for the breach of the terms and conditions of this
// license.
//
// THIS SOFTWARE IS PROVIDED IN AN AS IS CONDITION. NO WARRANTIES,
// WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
// TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
// IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
// CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
//
/*****************************************************************************/
// File: RTDM.c
//
// This program along with MPLAB DMCI ( MPLAB 8.10 or higher) create an alternative link
//between Host PC and target device for debugging applications in real-time.
//It is required to include the RTDM.C file and RTDM.h into the application project
//in order to send/receive data through the UART to/from the host PC running under
//MPLAB (release 8.10 or higher) DMCI environment.
// NOTE:DMCI included in MPLAB 8.10 or higher is ready and enabled to support data exchange
//between the host PC and target device. Previous versions of DMCI do not support this feature.
// NOTE: RTDM is currently supported by PIC24H, dsPIC30F, dsPIC33F and dsPIC33E processors
//
//
// Written By: M.Ellis, D. Torres,
// Microchip Technology Inc
//
//
// The following files should be included in the MPLAB project:
//
// RTDM.c -- RTDM source code file
// RTDM.h -- RTDM header file
// RTDMUSER.h -- RTDM user definitions file
// libpXXXX-coff.a -- Your dsPIC/PIC24 Peripheral Library
// pXXXXXXX.gld -- your dsPIC/PIC24 Linker script file
//
//
/*****************************************************************************/
//
// Revision History
//
// 4/7/08 -- First Version Release
/****************************************************************************/
//
// Revision History
//
// 4/12/10 -- Added support for dsPIC33E and PIC24E (supports UART3 and UART4)
/****************************************************************************/
#include "RTDM.h"
/*+++++++++++++++++++++++++++++++ RTDM Variables ++++++++++++++++++++++++++++++++++++++++*/
/* Received data is stored in array RTDMRxBuffer */
unsigned char RTDMRxBuffer[RTDM_RXBUFFERSIZE];
unsigned char * RTDMRxBufferLoLimit = RTDMRxBuffer;
unsigned char * RTDMRxBufferHiLimit = RTDMRxBuffer + RTDM_RXBUFFERSIZE - 1;
unsigned char * RTDMRxBufferIndex = RTDMRxBuffer;
unsigned char * RTDMRxBufferStartMsgPointer;
unsigned char * RTDMRxBufferEndMsgPointer;
/* Data to be transmitted using UART communication module */
const unsigned char RTDMTxdata[] = {'R','T','D','M','\0'};
const unsigned char RTDMSanityCheckOK[] = {'+','$','R','T','D','M','#',0x1B,0x86,'\0'};
const unsigned char RTDMWriteMemoryOK[] = {'+','$','O','K','#',0x4C,0x08,'\0'};
const unsigned char RTDMErrorIllegalFunction[] = {'-','$','E',0x01,'#',0xD3,0x6A,'\0'};
unsigned char RTDMErrorFrame[] = {'-','$','E',0,'#',0,0,'\0'};
/* Temp variables used to calculate the CRC16*/
unsigned int RTDMcrcTemp,RTDMcrcTempH,RTDMcrcTempL;
/*Structure enclosing the RTDM flags*/
struct {
unsigned MessageReceived : 1;
unsigned TransmitNow : 1;
unsigned unused : 14;
} RTDMFlags;
/* UART Configuration data */
/* Holds the value of uart config reg */
unsigned int RTDM_UART_MODE_VALUE;
/* Holds the information regarding uart TX & RX interrupt modes */
unsigned int RTDM_UART_STA_VALUE;
#if (RTDM_UART == 1)
/******************************************************************************
* Function: RTDM_Start()
*
* Output: return 0 if no errors
*
* Overview: Here is where the RTDM code initilizes the UART to be used to
* exchange data wiht the host PC
*
* Note: Some processors may have more UART modules, that is why it is required to
* specify wich UART module is going to be used by RTDM
*******************************************************************************/
#if defined (RTDM_UART_V2)
int RTDM_Start()
{
/********************** UART CONFIGURATIION ***************************/
/* Turn off UART1 module */
CloseUART1();
/* Configure UART2 receive and transmit interrupt */
ConfigIntUART1(UART_RX_INT_EN & (UART_RX_INT_PR0+RTDM_UART_PRIORITY) & UART_TX_INT_DIS & UART_TX_INT_PR2);
/* Configure UART1 module to transmit 8 bit data with one stopbit. */
RTDM_UART_MODE_VALUE = UART_EN & UART_IDLE_CON & UART_IrDA_DISABLE &
UART_MODE_FLOW & UART_UEN_00 & UART_DIS_WAKE &
UART_DIS_LOOPBACK & UART_DIS_ABAUD & UART_UXRX_IDLE_ONE &
UART_BRGH_SIXTEEN & UART_NO_PAR_8BIT & UART_1STOPBIT;
RTDM_UART_STA_VALUE = UART_INT_TX_BUF_EMPTY & UART_IrDA_POL_INV_ZERO &
UART_SYNC_BREAK_DISABLED & UART_TX_ENABLE & UART_INT_RX_CHAR &
UART_ADR_DETECT_DIS & UART_RX_OVERRUN_CLEAR;
OpenUART1(RTDM_UART_MODE_VALUE, RTDM_UART_STA_VALUE, RTDM_BRG);
/************* RTDM Flags Configuration & Initial Values *****************/
RTDMFlags.MessageReceived = 0;
RTDMFlags.MessageReceived = 0;
RTDMRxBufferIndex = RTDMRxBufferLoLimit;
RTDMRxBufferStartMsgPointer = RTDMRxBufferLoLimit;
RTDMRxBufferEndMsgPointer = RTDMRxBufferLoLimit;
return 0;
}
#elif defined (RTDM_UART_V1)
int RTDM_Start()
{
/********************** UART CONFIGURATIION ***************************/
/* Turn off UART1 module */
CloseUART1();
/* Configure UART2 receive and transmit interrupt */
ConfigIntUART1(UART_RX_INT_EN & (UART_RX_INT_PR0+RTDM_UART_PRIORITY) & UART_TX_INT_DIS & UART_TX_INT_PR2);
/* Configure UART1 module to transmit 8 bit data with one stopbit. */
RTDM_UART_MODE_VALUE = UART_EN & UART_IDLE_CON &
UART_DIS_WAKE & UART_DIS_LOOPBACK &
UART_DIS_ABAUD & UART_NO_PAR_8BIT &
UART_1STOPBIT;
RTDM_UART_STA_VALUE = UART_INT_TX_BUF_EMPTY & UART_TX_PIN_NORMAL &
UART_TX_ENABLE & UART_INT_RX_CHAR &
UART_ADR_DETECT_DIS & UART_RX_OVERRUN_CLEAR;
OpenUART1(RTDM_UART_MODE_VALUE, RTDM_UART_STA_VALUE, RTDM_BRG);
/************* RTDM Flags Configuration & Initial Values *****************/
RTDMFlags.MessageReceived = 0;
RTDMFlags.MessageReceived = 0;
RTDMRxBufferIndex = RTDMRxBufferLoLimit;
RTDMRxBufferStartMsgPointer = RTDMRxBufferLoLimit;
RTDMRxBufferEndMsgPointer = RTDMRxBufferLoLimit;
return 0;
}
#endif
/******************************************************************************
* Function: CloseRTDM()
*
* Output: return 0 if no errors
*
* Overview: Here is where the RTDM code closes the UART used to
* exchange data wiht the host PC
*
* Note: Some processors may have more UART modules, that is why it is required to
* specify wich UART module is going to be used by RTDM
*******************************************************************************/
int CloseRTDM()
{
int nRet = 0;
CloseUART1();
return nRet;
}
/******************************************************************************
* Function: RTDM_ProcessMsgs()
*
* Output: return 0 if no errors
*
* Overview: Here is where th
评论2