📄 ext_svr_transport.c
字号:
//UploadCancelLogging();
//break;
} else {
/* buffer not wrapped (head > tail) - this implies that there's enough space (see above)
or buffer wrapped and enough space between head and tail to store another telegram
fw-03-05 */
(void) memcpy((char_T *) (ringBuf.buf + ringBuf.head), bufMem->section1, (uint16_T)bufMem->nBytes1);
ringBuf.head += bufMem->nBytes1;
if (bufMem->section2 != NULL) {
(void) memcpy((char_T *) (ringBuf.buf + ringBuf.head), bufMem->section2, (uint16_T)bufMem->nBytes2);
ringBuf.head += bufMem->nBytes2;
}
#if DEBUG2SCI0 >= 2
if ((signed)(ringBuf.head - ringBuf.tail) < 0) {
SCI0_OutString("head : ");
SCI0_OutUDec((uint16_T)ringBuf.head);
SCI0_OutString(" tail : ");
SCI0_OutUDec((uint16_T)ringBuf.tail);
SCI0_OutString(" space : ");
SCI0_OutUDec((uint16_T)(ringBuf.tail - ringBuf.head));
} else {
SCI0_OutString("head : ");
SCI0_OutUDec((uint16_T)ringBuf.head);
SCI0_OutString(" tail : ");
SCI0_OutUDec((uint16_T)ringBuf.tail);
SCI0_OutString(" space : ");
SCI0_OutUDec((uint16_T)(ringBuf.size - ringBuf.head));
}
#endif
/* increase message counter -- fw-03-05 */
ringBuf.nRecords++;
#if DEBUG2SCI0 >= 2
SCI0_OutString(" nRecs : ");
SCI0_OutUDec((uint16_T)ringBuf.nRecords);
if ((((signed) (ringBuf.head - ringBuf.tail)) < 0) && \
((ringBuf.head + bufMem->nBytes1 + bufMem->nBytes2 + 3 + 9) > ringBuf.tail )) {
SCI0_OutString(" (full)\n\r");
} else {
SCI0_OutString("\n\r");
}
#endif
}
/* comfirm that the data was buffered. */
UploadBufDataSent(upList.tids[i]);
} /* for */
UploadBufGetData(&upList);
} /* while */
} /* End put_data_buffer */
/* Function: ExtInit ===========================================================
* Abstract:
* Called once at program startup to do any initialization related to external
* mode.
*
* NOTES:
* o This function should not block.
*/
PUBLIC void ExtInit(void) {
/* initialize reception ring buffer -- make sure this hasn't been done before */
/* debug stage... avoid macros (not used) -- fw-02-05 */
#define noMACROS
#ifdef noMACROS
/* initialise serial reception ring buffer */
if((RecBufPtr = (struct BufferTyp *)malloc(sizeof(struct BufferTyp))) == NULL)
abort_LED(80);
else
{
RecBufPtr->ElemLength = (BYTE)(sizeof(char_T));
if((RecBufPtr->Buffer = (char *)malloc(RecBufPtr->ElemLength * RBUFSIZE)) == NULL)
abort_LED(81);
else
{
ClearBuffer(RecBufPtr);
RecBufPtr->BufLength = RBUFSIZE;
}
}
#else
/* macro : InitBuffer */
if(RecBufPtr == NULL) {
InitBuffer(RecBufPtr, RBUFSIZE, char_T);
}
#endif
/* make sure we're using the right port... */
#if SCI1_COMMS == 1
SCI1_Init(BAUDRATE);
/* promote SCI1 interrupt REQUESTS over timer interrupt */
HPRIO = 0xD4; /* 0xFFD4 -> SCI1 interrupt = highest priority */
#else /* SCI0_COMMS == 1 */
SCI0_Init(BAUDRATE);
/* promote SCI0 interrupt REQUESTS over timer interrupt */
HPRIO = 0xD2; /* 0xFFD2 -> SCI0 interrupt = highest priority */
#endif
/* initialize timeout timer (RTI) */
/* base clock is OSCCLK = 250 nsec
RTR[6:4] Real Time Interrupt Prescale Rate Select Bits
000 off
001 divide by 1024
010 divide by 2048
011 divide by 4096
100 divide by 8192
101 divide by 16384
110 divide by 32768
111 divide by 65536
RTR[3:0] Real Time Interrupt Modulus Counter Select Bits
0000 divide by 1
0001 divide by 2
0010 divide by 3
0011 divide by 4
0100 divide by 5
0101 divide by 6
0110 divide by 7
0111 divide by 8
...
1111 divide by 16
*/
//RTICTL = 0; /* RTI stopped */
//CRGINT = 0x80; /* CRG interrupt enable register, RTIE=1 -> enable RTI interrupts */
/* initialize RTI counter variable */
//RTI_counter = 0;
} /* end ExtInit */
/* Function: ExtRemoveReceptionBuffer ========================================
* Abstract:
* Clears the reception buffer (only called in case of a communication problem
* fw-02-05
*/
PUBLIC void ExtRemoveReceptionBuffer(void) {
/* debug stage... avoid macros (not used) -- fw-02-05 */
#define noMACROS
#ifdef noMACROS
free((void *)RecBufPtr->Buffer);
free((void *)RecBufPtr);
#else
/* macro : RemoveBuffer */
RemoveBuffer(RecBufPtr);
#endif
RecBufPtr = NULL;
} /* end ExtRemoveReceptionBuffer */
/* Function: ExtClearReceptionBuffer ========================================
* Abstract:
* Clears the reception buffer (only called in case of a communication problem
* fw-02-05
*/
PUBLIC void ExtClearReceptionBuffer(void) {
if(NotEmptyBuffer(RecBufPtr)) {
ClearBuffer(RecBufPtr);
}
} /* end ExtClearReceptionBuffer */
/* Function: ExtWaitForStartMsgFromHost ========================================
* Abstract:
* Return true if the model should not start executing until told to do so
* by the host.
*/
PUBLIC boolean_T ExtWaitForStartMsgFromHost(ExtUserData *UD) {
return(UD->waitForStartMsg);
} /* end ExtWaitForStartMsgFromHost */
/* Function: ExtUserDataCreate =================================================
* Abstract:
* Create the user data.
*/
PUBLIC ExtUserData *ExtUserDataCreate(void) {
return((ExtUserData *)calloc(1, sizeof(ExtUserData)));
} /* end ExtUserDataCreate */
/* Function: ExtUserDataDestroy ================================================
* Abstract:
* Destroy the user data.
*/
PUBLIC void ExtUserDataDestroy(ExtUserData *UD) {
if (UD != NULL) {
free(UD);
}
} /* end ExtUserDataDestroy */
/* Function: ExtGetHostMsg =====================================================
* Abstract:
* Attempts to get the specified number of bytes from the msg line. The number
* of bytes read is returned via the 'nBytesGot' parameter. EXT_NO_ERROR is
* returned on success, EXT_ERROR is returned on failure.
*
* NOTES:
* o it is not an error for 'nBytesGot' to be returned as 0
* o blocks if no data available and EXT_BLOCKING == 1, polls otherwise
* o not guaranteed to read total requested number of bytes
*/
PUBLIC boolean_T ExtGetHostMsg(
const ExtUserData *UD,
const int_T nBytesToGet,
int_T *nBytesGot, /* out */
char_T *dst) /* out */
{
int_T nBuf;
/* Get message from host through RS232 serial port; return value = # of bytes currently in RecBuf */
if(nBytesToGet > 0) {
/* get data from the ring buffer */
nBuf = get_data_sp(nBytesToGet, nBytesGot, dst);
}
else {
/* nBytesToGet = 0 -> just send positive acknowledgement */
*nBytesGot = 0; /* force below's loop to send 'ACK_SUCCESS' */
}
/* do we need to send acknowledgements? */
if(UD->acknowledgeMsg == TRUE) {
/* send acknowledgement */
if(*nBytesGot == nBytesToGet) {
/* signal successful receipt of message telegram */
const char_T ack_byte = ACK_SUCCESS; /* 'S' */
int_T nBytesSet;
/* signal ACK_SUCCESS */
put_data_sp(1, &ack_byte, &nBytesSet);
return EXT_NO_ERROR;
}
else {
/* get_data_sp unsuccessful... */
/* something in the buffer? */
if(nBuf > 0) {
/* received something, but less than sizeof(MsgHdr) bytes [8] */
const char_T ack_byte = ACK_NO_SUCCESS; /* 'N' */
char_T discard;
int_T nBytesSet;
int_T i;
/* communication stuck -> remove 'nBuf' bytes from the buffer */
for(i=0; i<nBuf; i++) {
OutBuffer(RecBufPtr, discard, char_T);
}
/* signal ACK_NO_SUCCESS */
put_data_sp(1, &ack_byte, &nBytesSet);
/* and return without error... */
//abort_LED(88);
return EXT_NO_ERROR;
}
else {
/* nothing in the buffer... -> just return */
return EXT_NO_ERROR;
}
} /* nBytesGot == nBytesToGet (else) */
} /* acknowledgeMsG == TRUE */
else {
/* no acknowledgements required -> just return (at the mo.) */
return EXT_NO_ERROR;
} /* acknowledgeMsg == FALSE */
} /* end ExtGetHostMsg */
/* Function: ExtSetHostMsg =====================================================
* Abstract:
* Sets (sends) the specified number of bytes on the msg line. As long as
* an error does not occur, this function is guaranteed to set the requested
* number of bytes. The number of bytes set is returned via the 'nBytesSet'
* parameter. EXT_NO_ERROR is returned on success, EXT_ERROR is returned on
* failure.
*
* NOTES:
* o it is always o.k. for this function to block if no room is available
*/
PUBLIC boolean_T ExtSetHostMsg(
const ExtUserData *UD,
const int_T nBytesToSet,
const char_T *src,
int_T *nBytesSet) /* out */
{
const char_T msg_byte = MSG; /* start of transmission */
/* send MSG byte (35) -- unique symbol preceeding all message telegrams */
put_data_sp(1, &msg_byte, nBytesSet);
/* send message */
put_data_sp(nBytesToSet, src, nBytesSet);
return EXT_NO_ERROR;
} /* end ExtSetHostMsg */
/* Function: ExtSetHostData ====================================================
* Abstract:
* Sets (sends) the specified number of bytes on the data upload line. As long
* as an error does not occur, this function is guaranteed to set the
* requested number of bytes. The number of bytes set is returned via the
* 'nBytesSet' parameter. EXT_NO_ERROR is returned on success, EXT_ERROR is
* returned on failure.
*
* NOTES:
* o it is always o.k. for this function to block if no room is available
*/
PUBLIC boolean_T ExtSetHostData(
const ExtUserData *UD,
const int_T nBytesToSet,
const char_T *src,
int_T *nBytesSet) /* out */
{
const char_T data_byte = DAT;
/* preceed messages sent through the data channel by a positive acknowledgement (ACK_SUCCESS) */
if(nBytesToSet == 8) {
const char_T ack_byte = ACK_SUCCESS; /* successful transmission of TXactive message */
/* release waiting host */
put_data_sp(1, &ack_byte, nBytesSet);
}
/* send DAT byte (34) -- unique symbol preceeding all data telegrams */
put_data_sp(1, &data_byte, nBytesSet);
/* transmit data */
put_data_sp(nBytesToSet, src, nBytesSet);
return EXT_NO_ERROR;
} /* end ExtSetHostData */
/* Function: ExtSetHostUserData ===================================================
* Abstract:
* Sets (sends) the specified number of bytes on the msg line. As long as
* an error does not occur, this function is guaranteed to set the requested
* number of bytes. The number of bytes set is returned via the 'nBytesSet'
* parameter. EXT_NO_ERROR is returned on success, EXT_ERROR is returned on
* failure.
*
* NOTES:
* o it is always o.k. for this function to block if no room is available
*/
PUBLIC boolean_T ExtSetHostUserData(
const int_T nBytesToSet,
const char_T *src)
{
const char_T usr_byte = USR; /* start of transmission */
int_T nBytesSet;
/* send USR byte (33) -- unique symbol preceeding all user telegrams */
put_data_sp(1, &usr_byte, &nBytesSet);
/* send user data telegram */
put_data_sp(nBytesToSet, src, &nBytesSet);
return EXT_NO_ERROR;
} /* end ExtSetHostUserData */
/* Function: ExtModeSleep ======================================================
* Abstract:
* Called by grt_main, ert_main, and grt_malloc_main to "pause" (hopefully in
* a way that does not hog the CPU) execution.
*
* currently an empty function (FW-02-03)
*
*/
PUBLIC void ExtModeSleep(
const ExtUserData *UD,
const long sec, /* # of secs to wait */
const long usec) /* # of micros secs to wait */
{
/* called from rt_ExtModePauseIfNeeded() [ext_work.c], currently non-functional */
} /* end ExtModeSleep */
/* [EOF] ext_svr_transport.c */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -