matlab_23550/Inu/Src/N12_Xilinx/RS_Functions.c
Razvalyaev 7e0063eee0 #3 Скомпилилось, но пока ничего не вызывается
Все основные файлы подтянуты без изменений

Изменены (только папка main_matlab):
- заглушки для ненужных функций (main_matlab.c)
- iq библиотека (IQmathLib_matlab.c)
- библиотеки DSP281x
2025-01-13 11:09:58 +03:00

2640 lines
77 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "params.h"
#include "global_time.h"
#include "RS_Functions.h"
#include <project.h>
#if (USE_TEST_TERMINAL)
#include "RS_Function_terminal.h"
#endif
#if (USE_MODBUS_TABLE_SVU)
#include "RS_modbus_svu.h"
#endif
#if (USE_MODBUS_TABLE_PULT)
#include "RS_modbus_pult.h"
#endif
#include "CRC_Functions.h"
#include "TuneUpPlane.h" //ñâåòîäèîäèê
#include "pwm_test_lines.h"
#include "profile_interrupt.h"
#define _ENABLE_INTERRUPT_RS232_LED2 0//1
#define _USE_RS_FIFO 1
#define RS232_SPEED 57600//115200
#define COM_1 0 //1
#define COM_2 1 //2
//#define SIZE_MODBUS_TABLE 334
//#define ADR_MODBUS_TABLE 0x0001
#define BM_CHAR32 0
#define TIME_WAIT_RS232_BYTE_OUT 2000
#define ADDR_FOR_ALL_DEF 0x4
#define ADR_FOR_SPECIAL 0x100
#define BM_PACKED 1
#define ADDR_UNIVERSAL_DEF 10
#define REC_BLOC_BEGIN 0xa0000
#define REC_BLOC_END 0xeffff
#define SelectNothing() WriteOper(1, 1, 1, 1)
#define SCIa_TX_IntClear() SciaRegs.SCIFFTX.bit.TXINTCLR = 1
#define SCIb_TX_IntClear() ScibRegs.SCIFFTX.bit.TXINTCLR = 1
#define IncCountMode28() WriteOper(1, 1, 0, 0)
#define SCIa_RX_IntClear() {SciaRegs.SCIFFRX.bit.RXFFOVRCLR=1; SciaRegs.SCIFFRX.bit.RXFFINTCLR=1;}
#define SCIb_RX_IntClear() {ScibRegs.SCIFFRX.bit.RXFFOVRCLR=1; ScibRegs.SCIFFRX.bit.RXFFINTCLR=1;}
#define SCIa_SW_Reset() {SciaRegs.SCICTL1.bit.SWRESET=0; SciaRegs.SCICTL1.bit.SWRESET=1;}
#define SCIb_SW_Reset() {ScibRegs.SCICTL1.bit.SWRESET=0; ScibRegs.SCICTL1.bit.SWRESET=1;}
//#define SCIb_Get() ScibRegs.SCIRXBUF.bit.RXDT
#define SelectReset28() WriteOper(1, 1, 1, 0)
#define SelectReset28_ForLoad67() WriteOper(0, 0, 1, 0)
//#define enableUARTInt_A() SciaRegs.SCICTL2.all = 2 // recive
//#define enableUARTInt_B() ScibRegs.SCICTL2.all = 2 // recive
//#define EnableUART_IntW_A() SciaRegs.SCICTL2.all = 1 // transmit
//#define EnableUART_IntW_B() ScibRegs.SCICTL2.all = 1 // transmit
//#define SCIa_OK() SciaRegs.SCICTL2.bit.TXEMPTY
//#define SCIb_OK() ScibRegs.SCICTL2.bit.TXEMPTY
//#define SCIa_Wait4OK() while(!SCIa_OK())
#define SCIa_Send(a) SciaRegs.SCITXBUF = (unsigned char)a
//#define SCIb_Wait4OK() while(!SCIb_OK())
#define SCIb_Send(a) ScibRegs.SCITXBUF = (unsigned char)a
#define SCIa_Get() SciaRegs.SCIRXBUF.bit.RXDT
#define SCIa_RX_Error() SciaRegs.SCIRXST.bit.RXERROR
#define SCIb_RX_Error() SciaRegs.SCIRXST.bit.RXERROR
#define SetLoad28_FromResetInternalFlash() \
SelectNothing(); \
IncCountMode28();
const int CNTRL_ADDR_UNIVERSAL = ADDR_UNIVERSAL_DEF;
//RS_DATA_STRUCT RS232_A, RS232_B;
#pragma DATA_SECTION(rs_a, ".slow_vars")
#pragma DATA_SECTION(rs_b, ".slow_vars")
RS_DATA_STRUCT rs_a = RS_DATA_STRUCT_DEFAULT, rs_b = RS_DATA_STRUCT_DEFAULT;
#pragma DATA_SECTION(RS_Len, ".slow_vars")
unsigned int RS_Len[RS_LEN_CMD] = {0};
//MODBUS_REG_STRUCT modbus_table_rs_in[SIZE_MODBUS_TABLE];
//MODBUS_REG_STRUCT modbus_table_rs_out[SIZE_MODBUS_TABLE];
char size_cmd15 = 1;
char size_cmd16 = 1;
//unsigned int enable_profile_led1_rsa = 1;
//unsigned int enable_profile_led1_rsb = 1;
//unsigned int enable_profile_led2_rsa = 0;
//unsigned int enable_profile_led2_rsb = 0;
int CNTRL_ADDR = 1;
int ADDR_FOR_ALL = ADDR_FOR_ALL_DEF;
float KmodTerm = 0.0, freqTerm = 0.0;
int flag_special_mode_rs = 0;
int disable_flag_special_mode_rs = 0;
void RS_Wait4OK_TXRDY(char commnumber);
void Answer(RS_DATA_STRUCT *RS232_Arr,int n);
void EnableUART_Int_RX(char commnumber);
void EnableUART_Int_TX(char commnumber);
void RS_LineToReceive(char commnumber);
void RS_SetLineMode(char commnumber, int bit, char parity, int stop);
void RS_SetBitMode(RS_DATA_STRUCT *RS232_Arr, int n);
void clear_timer_rs_live(RS_DATA_STRUCT *rs_arr);
void clear_timer_rs_live_mpu(RS_DATA_STRUCT *rs_arr);
void SCI_Send(char commnumber, char bs);
void RS_Wait4OK(char commnumber);
void RS_LineToSend(char commnumber);
void RS_TX_Handler(RS_DATA_STRUCT *RS232_Arr);
//int RS232_BSend(RS_DATA_STRUCT *RS232_Arr,unsigned int *pBuf, unsigned long len);
void EnableReceiveRS485(void)
{
#if(_ENABLE_PWM_LINES_FOR_TESTS_RS)
PWM_LINES_TK_19_ON;
#endif
GpioDataRegs.GPBDAT.bit.GPIOB14 = 1;
}
void EnableSendRS485(void)
{
#if(_ENABLE_PWM_LINES_FOR_TESTS_RS)
PWM_LINES_TK_19_OFF;
#endif
GpioDataRegs.GPBDAT.bit.GPIOB14 = 0;
}
void T_Flash(RS_DATA_STRUCT *RS232_Arr)
{
volatile unsigned long Address1,Address2;
volatile unsigned long Length, LengthW;
unsigned int cerr, repl, count_ok, return_code;
if(!RS232_Arr->BS_LoadOK)
{
RS_LineToReceive(RS232_Arr->commnumber); // ðåæèì ïðèåìà RS485
RS_SetBitMode(RS232_Arr,9);
return;
}
Address1 = RS232_Arr->RS_Header[5] & 0xFF;
Address1 = (Address1<<8) | (RS232_Arr->RS_Header[4] & 0xFF);
Address1 = (Address1<<8) | (RS232_Arr->RS_Header[3] & 0xFF);
Address1 = (Address1<<8) | (RS232_Arr->RS_Header[2] & 0xFF);
Address2 = RS232_Arr->RS_Header[9] & 0xFF;
Address2 = (Address2<<8) | (RS232_Arr->RS_Header[8] & 0xFF);
Address2 = (Address2<<8) | (RS232_Arr->RS_Header[7] & 0xFF);
Address2 = (Address2<<8) | (RS232_Arr->RS_Header[6] & 0xFF);
Length = RS232_Arr->RS_Header[13] & 0xFF;
Length = (Length<<8) | (RS232_Arr->RS_Header[12] & 0xFF);
Length = (Length<<8) | (RS232_Arr->RS_Header[11] & 0xFF);
Length = (Length<<8) | (RS232_Arr->RS_Header[10] & 0xFF);
LengthW = Length/2;
if (LengthW*2<Length) LengthW++;
if( (Address2 < 0x100000) || (Address2 > 0x180000) || ((Address2+LengthW) > 0x180000) )
{
RS_LineToReceive(RS232_Arr->commnumber); // ðåæèì ïðèåìà RS485
RS_SetBitMode(RS232_Arr,9);
return;
}
return_code = RunFlashData(Address1,Address2, LengthW, &cerr, &repl, &count_ok );
if (return_code==0)
Answer(RS232_Arr,CMD_RS232_TFLASH);
else
{
RS_LineToReceive(RS232_Arr->commnumber); // ðåæèì ïðèåìà RS485
RS_SetBitMode(RS232_Arr,9);
return;
}
return;
}
void Upload(RS_DATA_STRUCT *RS232_Arr)
{
int32 Address, Length, crc;
Address = RS232_Arr->RS_Header[5] & 0xFF;
Address = (Address<<8) | (RS232_Arr->RS_Header[4] & 0xFF);
Address = (Address<<8) | (RS232_Arr->RS_Header[3] & 0xFF);
Address = (Address<<8) | (RS232_Arr->RS_Header[2] & 0xFF);
Length = RS232_Arr->RS_Header[9] & 0xFF;
Length = (Length<<8) | (RS232_Arr->RS_Header[8] & 0xFF);
Length = (Length<<8) | (RS232_Arr->RS_Header[7] & 0xFF);
Length = (Length<<8) | (RS232_Arr->RS_Header[6] & 0xFF);
// RS232_Arr->buffer[0] = RS232_Arr->addr_recive; //CNTRL_ADDR;
// RS232_Arr->buffer[1] = CMD_RS232_UPLOAD;
RS232_Arr->buffer_stage1[0] = RS232_Arr->addr_recive; //CNTRL_ADDR;
RS232_Arr->buffer_stage1[1] = CMD_RS232_UPLOAD;
crc = 0xffff;
crc = GetCRC16_IBM( crc, RS232_Arr->buffer_stage1, 2);
crc = GetCRC16_B( crc, (unsigned int *)Address, Length);
RS232_Arr->RS_SLength_stage1 = 2; /* Íàñòðàèâàåì ïåðåìåííûå */
RS232_Arr->pRS_SendPtr_stage1 = RS232_Arr->buffer_stage1;
RS232_Arr->RS_SendBlockMode_stage1 = BM_CHAR32;
RS232_Arr->RS_SLength_stage2 = Length; /* Íàñòðàèâàåì ïåðåìåííûå */
RS232_Arr->pRS_SendPtr_stage2 = (unsigned int*)Address;
RS232_Arr->RS_SendBlockMode_stage2 = BM_PACKED;
// RS_Send(RS232_Arr,RS232_Arr->buffer, 2); // <=2 áàéò ïî ôëàãó
// RS232_Arr->buffer[0] = CMD_RS232_UPLOAD;
// RS_Send(RS232_Arr,RS232_Arr->buffer, 1); // <=2 áàéò ïî ôëàãó
// while (RS232_Arr->RS_OnTransmitedData);
// RS_Wait4OK(RS232_Arr->commnumber);
// RS232_BSend(RS232_Arr,(unsigned int*)Address, Length);
// RS_Wait4OK(RS232_Arr->commnumber);
// while (RS232_Arr->RS_OnTransmitedData);
RS232_Arr->buffer[0] = LOBYTE(crc);
RS232_Arr->buffer[1] = HIBYTE(crc);
RS232_Arr->buffer[2] = 0;
RS232_Arr->buffer[3] = 0;
RS232_Arr->cmd_tx_stage = 2;
RS_Send(RS232_Arr,RS232_Arr->buffer, 4);
// RS232_Send_Staged(RS232_Arr,(unsigned int*)Address, Length);
// RS_Send(RS232_Arr,RS232_Arr->buffer, 4+2);
}
void SetupArrCmdLength()
{
int i;
for (i=0;i<RS_LEN_CMD;i++) RS_Len[i]=i;
RS_Len[CMD_RS232_LOAD] = 12;
RS_Len[CMD_RS232_UPLOAD] = 12;
RS_Len[CMD_RS232_RUN] = 8;
RS_Len[CMD_RS232_XFLASH] = 9;
RS_Len[CMD_RS232_TFLASH] = 16;
RS_Len[CMD_RS232_PEEK] = 8;
RS_Len[CMD_RS232_POKE] = 12;
RS_Len[CMD_RS232_INITLOAD] = 12;
RS_Len[CMD_RS232_INIT] = 0;
RS_Len[CMD_RS232_VECTOR] = size_cmd15-2; //sizeof(CMD_TO_TMS)-2;
RS_Len[CMD_RS232_STD] = size_cmd15-1; //sizeof(CMD_TO_TMS)-1;
RS_Len[CMD_RS232_TEST_ALL] = size_cmd16-1; //sizeof(CMD_TO_TMS)-1;
RS_Len[CMD_RS232_IMPULSE] = 8;
RS_Len[CMD_RS232_MODBUS_3] = 8;
RS_Len[CMD_RS232_MODBUS_16] = 13;
RS_Len[CMD_RS232_MODBUS_15] = 27;
RS_Len[CMD_RS232_EXTEND] = 18;
}
#define ClearTimerRS_Live(p) (p)->time_wait_rs_out = (p)->time_wait_rs_out_mpu = (p)->time_wait_rs_lost = 0;
void SCI_SwReset(char commnumber)
{
switch (commnumber)
{
case COM_1:
SciaRegs.SCICTL1.bit.SWRESET=0; // Relinquish SCI from Reset
SciaRegs.SCICTL1.bit.SWRESET=1; // Relinquish SCI from Reset
break;
case COM_2:
ScibRegs.SCICTL1.bit.SWRESET=0; // Relinquish SCI from Reset
ScibRegs.SCICTL1.bit.SWRESET=1; // Relinquish SCI from Reset
break;
}
}
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
//static int buf_fifo_rsa[17]={0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0};
//static int buf_fifo_rsb[17]={0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0};
static int buf_fifo_rs_ab[2][17]={ {0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0} };
#pragma CODE_SECTION(my_test_rs,".fast_run2");
int my_test_rs(int comn)
{
int cc=0;
cc = 0;
if (comn==COM_1)
{
while ((SciaRegs.SCIFFRX.bit.RXFIFST != 0) )
{
buf_fifo_rs_ab[comn][cc++] = SciaRegs.SCIRXBUF.bit.RXDT;
if (cc>=17) cc = 0;
}
return cc;
}
else
{
while ((ScibRegs.SCIFFRX.bit.RXFIFST != 0) )
{
buf_fifo_rs_ab[comn][cc++] = ScibRegs.SCIRXBUF.bit.RXDT;
if (cc>=17) cc = 0;
}
return cc;
}
}
///////////////
//#pragma CODE_SECTION(RS_RXA_Handler_fast,".fast_run2");
void RS_RXA_Handler_fast(RS_DATA_STRUCT *RS232_Arr)
{
char Rc;
char RS232_BytePtr;
int cc1, cc2,cn;
//i_led2_on_off(1);
ClearTimerRS_Live(RS232_Arr);
cn = RS232_Arr->commnumber;
cc1 = my_test_rs(cn);
cc2 = 0;
for(;;) // 'goto' ýòî íå îïåðàòîð ßçûêà Ñ
{
if (cn==COM_1)
{
if (SciaRegs.SCIRXST.bit.RXERROR)
{
// Rc = SciaRegs.SCIRXBUF.all;
(RS232_Arr->count_recive_rxerror)++;
RS232_Arr->do_resetup_rs = 1;
}
if (SciaRegs.SCIRXST.bit.RXWAKE)
{
Rc = SciaRegs.SCIRXBUF.all;
}
}
else
{
if (ScibRegs.SCIRXST.bit.RXERROR)
{
// Rc = SciaRegs.SCIRXBUF.all;
(RS232_Arr->count_recive_rxerror)++;
RS232_Arr->do_resetup_rs = 1;
}
if (ScibRegs.SCIRXST.bit.RXWAKE)
{
Rc = ScibRegs.SCIRXBUF.all;
}
}
// if (!SciaRegs.SCIRXST.bit.RXRDY)
if (cc1 == 0)
// if ((SciaRegs.SCIFFRX.bit.RXFIFST == 0) )
{
// PieCtrlRegs.PIEACK.bit.ACK9 |= 1;
// SCI_RX_IntClear(RS232_Arr->commnumber);
// SciaRegs.SCIFFRX.bit.RXFFINTCLR = 1;
//i_led2_on_off(0);
return; // êñòàòè ýòî åäèíñòâåííûé âûõîä èç ïðåðûâàíèß
}
//i_led2_on_off(1);
/* åñòü ëè îøèáêè? */
// if (SciaRegs.SCIRXST.bit.RXERROR)
// {
// Rc = SciaRegs.SCIRXBUF.all;//SciaRegs.SCIRXBUF.bit.RXDT;//RS_Get(RS232_Arr->commnumber); // ×èòàåì ñèìâîë â ëþáîì ñëó÷àå
// continue;
// }
cc1--;
// if (cn==COM_1)
Rc = buf_fifo_rs_ab[cn][cc2];//SciaRegs.SCIRXBUF.all;// SciaRegs.SCIRXBUF.bit.RXDT;//RS_Get(RS232_Arr->commnumber); // ×èòàåì ñèìâîë â ëþáîì ñëó÷àå
// else
// Rc = buf_fifo_rsb[cc2];
cc2++;
(RS232_Arr->count_recive_bytes_all)++;
//i_led2_on_off(0);
if(RS232_Arr->RS_DataReady)
{
continue; // Íå çàáðàëè äàííûå
}
if (RS232_Arr->RS_Flag9bit==1) // äëß RS485????????
{
// Èíèöèàëèçèðóåì ïåðåìåííûå è ôëàãè
RS232_Arr->RS_FlagBegin = true; // Æäåì çàãîëîâîê
RS232_Arr->RS_RecvLen = 0;
RS232_Arr->RS_FlagSkiping = false;
RS232_Arr->RS_HeaderCnt = 0;
RS232_Arr->RS_Cmd = 0;
}
//i_led2_on_off(1);
if(RS232_Arr->RS_FlagSkiping)
{
(RS232_Arr->count_recive_bytes_skipped)++;
continue; // Íå íàì
}
if (RS232_Arr->RS_FlagBegin) // Çàãîëîâîê
{
if (RS232_Arr->RS_HeaderCnt==0) // Àäðåñ êîíòðîëëåðà èëè ñòàíäàðòíàß êîìàíäà
{
//i_led2_on_off(0);
if( (Rc == CNTRL_ADDR_UNIVERSAL) || (Rc == CNTRL_ADDR && CNTRL_ADDR!=0) || ((Rc == RS232_Arr->addr_answer) && RS232_Arr->flag_LEADING)
|| ((Rc == ADDR_FOR_ALL && ADDR_FOR_ALL!=0) && !RS232_Arr->flag_LEADING))
{
RS232_Arr->addr_recive=Rc; // çàïîìíèëè àäðåñ ïî êîòîðîìó íàñ çàïðîñèëè
RS232_Arr->RS_Header[RS232_Arr->RS_HeaderCnt++] = Rc; // Ïåðâûé áàéò
// ClearTimerRS_Live(RS232_Arr);
RS_SetBitMode(RS232_Arr,8); // ïåðåñòðîèëèñü â 8-áèò ðåæèì
}
else
{
//i_led1_toggle();
RS232_Arr->RS_FlagSkiping = true; // Íå íàøåìó êîíòðîëëåðó
RS232_Arr->RS_FlagBegin = false; // îñòàëèñü â 9-áèò ðåæèìå
(RS232_Arr->count_recive_cmd_skipped)++;
//i_led1_on_off(0);
}
//i_led2_on_off(1);
}
else
{
//i_led2_on_off(0);i_led2_on_off(1);
// ClearTimerRS_Live(RS232_Arr);
RS232_Arr->RS_Header[RS232_Arr->RS_HeaderCnt++] = Rc; // Âòîðîé áàéò è ò.ä.
if (RS232_Arr->RS_HeaderCnt == 7 && !RS232_Arr->flag_LEADING)
{
switch (RS232_Arr->RS_Cmd) {
case CMD_RS232_MODBUS_16:
RS_Len[CMD_RS232_MODBUS_16] = (10+Rc); break;
case CMD_RS232_MODBUS_15:
RS_Len[CMD_RS232_MODBUS_15] = (10+Rc); break;
}
}
//i_led2_on_off(0);i_led2_on_off(1);
// åñëè âòîðîé áàéò - ýòî êîìàíäà
if (RS232_Arr->RS_HeaderCnt == 2)
{
RS232_Arr->RS_Cmd = Rc;
// Ïðîâåðêà äëèíû ïîñûëêè
// CMD_LOAD - ìëàäøàß íà äàííûé ìîìåíò
// CMD_STD_ANS - ñòàðøàß íà äàííûé ìîìåíò
// if ((RS232_Arr->RS_Cmd < 0 /*CMD_RS232_MODBUS_3*/) || (RS232_Arr->RS_Cmd > CMD_RS232_STD_ANS) || (RS_Len[RS232_Arr->RS_Cmd]<3))
if ((RS232_Arr->RS_Cmd > CMD_RS232_STD_ANS) || (RS_Len[RS232_Arr->RS_Cmd]<3))
{
RS_SetBitMode(RS232_Arr,9); // Ïîëó÷èëè âñå ïåðåñòðîèëèñü â 9-áèò äëß RS485?
RS232_Arr->RS_HeaderCnt = 0; // Ïîòîìó ÷òî êîìàíäà íå òà
RS232_Arr->RS_FlagBegin = true;
RS232_Arr->RS_FlagSkiping = false;
RS232_Arr->RS_Cmd=0;
(RS232_Arr->count_recive_bad)++;
continue;
}
if(RS232_Arr->RS_Cmd == 4) {
asm(" NOP ");
}
if (RS232_Arr->RS_Cmd == CMD_RS232_LOAD) { // Äëß ýòîé êîìàíäû çàãîëîâîê î÷åíü êîðîòêèé
RS232_Arr->RS_FlagBegin = false;// äàëüøå èäóò äàííûå
RS232_Arr->count_recive_bytes_all = 0;// âðåìåííî, äëÿ îòëàäêè
}
}
//i_led2_on_off(0);
if( (RS232_Arr->RS_HeaderCnt >= (int)RS_Len[RS232_Arr->RS_Cmd]) ||
(RS232_Arr->RS_HeaderCnt >= (int)sizeof(RS232_Arr->RS_Header)))
{ // Ïîëó÷èëè çàãîëîâîê
RS_SetBitMode(RS232_Arr,9); // Ïîëó÷èëè âñå ïåðåñòðîèëèñü â 9-áèò äëß RS485?
RS232_Arr->RS_FlagBegin = false;
RS232_Arr->RS_FlagSkiping = true;
RS232_Arr->RS_DataReady = true;
RS232_Arr->RS_Cmd=0;
(RS232_Arr->count_recive_dirty)++;
}
//i_led2_on_off(1);
}
//i_led2_on_off(0);
}
else // Ïîòîê äàííûõ
{
if(RS232_Arr->pRS_RecvPtr<(unsigned int *)REC_BLOC_BEGIN || RS232_Arr->pRS_RecvPtr>(unsigned int *)REC_BLOC_END)
RS232_Arr->pRS_RecvPtr = (unsigned int *)REC_BLOC_BEGIN; // Íà ïðîãðàììó íàäåéñß, à ñàì íå ïëîøàé
if(RS232_Arr->RS_PrevCmd != CMD_RS232_INITLOAD)
{
(RS232_Arr->count_recive_bad)++;
continue; // Ìû çäåñü îêàçàëèñü ïî êàêîé-òî ÷óäîâèùíîé îøèáêå
}
if(RS232_Arr->RS_DataReady) // Åñëè äàííûå â îñíîâíîì öèêëå íå çàáðàíû,
{ // òî ïðîïóñêàåì ñëåäóþùóþ ïîñûëêó
RS232_Arr->RS_FlagSkiping = true; // Èãíîðèðóåì äî ñëåäóþùåãî çàãîëîâêà
(RS232_Arr->count_recive_cmd_skipped)++;
continue;
}
RS232_BytePtr = RS232_Arr->RS_RecvLen++ % 2;
if(RS232_BytePtr) *RS232_Arr->pRS_RecvPtr++ |= Rc; // Ïîëó÷èëè ñëîâî
else *RS232_Arr->pRS_RecvPtr = Rc<<8;
if(RS232_Arr->RS_Length <= RS232_Arr->RS_RecvLen) // Êîíåö ïîñûëêè
{
RS232_Arr->RS_PrevCmd = RS232_Arr->RS_Header[1] = CMD_RS232_LOAD;
RS_SetBitMode(RS232_Arr,9); // Ïîëó÷èëè âñå äàííûå ïåðåñòðîèëèñü â 9-áèò äëß RS485?
RS232_Arr->RS_FlagSkiping = true; // Èãíîðèðóåì äî ñëåäóþùåãî çàãîëîâêà
RS232_Arr->RS_DataReady = true; // Ôëàã â îñíîâíîé öèêë - äàííûå ïîëó÷åíû
(RS232_Arr->count_recive_dirty)++;
}
}
}
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
int get_free_rs_fifo_tx(char commnumber)
{
if(commnumber==COM_1)
{
return (16 - SciaRegs.SCIFFTX.bit.TXFFST-1);
}
else
{
return (16 - ScibRegs.SCIFFTX.bit.TXFFST-1);
}
}
///////////////////////////////////////////////////////////
void RS_TX_Handler(RS_DATA_STRUCT *RS232_Arr)
{
char RS232_BytePtr;
unsigned int final_flag=0, free_fifo;
unsigned int i;
static unsigned int max_s_b = 1; // ïîñûëàåì ïî max_s_b øòóê
unsigned int final_free_fifo=0;
// if(RS232_Arr->RS_SendBlockMode == BM_CHAR32)
// {
free_fifo = get_free_rs_fifo_tx(RS232_Arr->commnumber);
ClearTimerRS_Live(RS232_Arr);
if (free_fifo>=max_s_b)
free_fifo=max_s_b; // ïîñûëàåì ïî max_s_b øòóê
for (i=0;i<free_fifo;i++)
{
switch(RS232_Arr->cmd_tx_stage)
{
case 2: //stage1
if (RS232_Arr->RS_SendLen >= RS232_Arr->RS_SLength_stage1)
{
RS232_Arr->cmd_tx_stage = 1;
RS232_Arr->RS_SendLen = 0;
// break;
}
else
{
if(RS232_Arr->RS_SendBlockMode_stage1 != BM_CHAR32)
{
RS232_BytePtr = (RS232_Arr->RS_SendLen) % 2;
if(RS232_BytePtr) SCI_Send(RS232_Arr->commnumber, LOBYTE( *(RS232_Arr->pRS_SendPtr_stage1++) ));
else SCI_Send(RS232_Arr->commnumber, HIBYTE( *RS232_Arr->pRS_SendPtr_stage1 ));
}
else
SCI_Send(RS232_Arr->commnumber,*(RS232_Arr->pRS_SendPtr_stage1++));
(RS232_Arr->RS_SendLen)++;
break;
}
break;
case 1: //stage2
if (RS232_Arr->RS_SendLen >= RS232_Arr->RS_SLength_stage2)
{
RS232_Arr->cmd_tx_stage = 0;
RS232_Arr->RS_SendLen = 0;
// break;
}
else
{
if(RS232_Arr->RS_SendBlockMode_stage2 != BM_CHAR32)
{
RS232_BytePtr = (RS232_Arr->RS_SendLen) % 2;
if(RS232_BytePtr) SCI_Send(RS232_Arr->commnumber, LOBYTE( *(RS232_Arr->pRS_SendPtr_stage2++) ));
else SCI_Send(RS232_Arr->commnumber, HIBYTE( *RS232_Arr->pRS_SendPtr_stage2 ));
}
else
SCI_Send(RS232_Arr->commnumber,*(RS232_Arr->pRS_SendPtr_stage2++));
(RS232_Arr->RS_SendLen)++;
break;
}
break;
case 0:
//stage 0
if (RS232_Arr->RS_SendLen >= RS232_Arr->RS_SLength)
{
final_flag = 1;
break;
}
else
{
if(RS232_Arr->RS_SendBlockMode != BM_CHAR32)
{
RS232_BytePtr = (RS232_Arr->RS_SendLen) % 2;
if(RS232_BytePtr) SCI_Send(RS232_Arr->commnumber, LOBYTE( *(RS232_Arr->pRS_SendPtr++) ));
else SCI_Send(RS232_Arr->commnumber, HIBYTE( *RS232_Arr->pRS_SendPtr ));
}
else
SCI_Send(RS232_Arr->commnumber,*(RS232_Arr->pRS_SendPtr++));
(RS232_Arr->RS_SendLen)++;
}
break;
default :
break;
}
if (final_flag)
break;
}
if (final_flag)
{
final_free_fifo = get_free_rs_fifo_tx(RS232_Arr->commnumber);
if (final_free_fifo>=15) // âñå óåõàëè? áóôåð ÷èñò?
{
if(RS232_Arr->RS_SendBlockMode == BM_CHAR32)
{
// if (max_s_b>1)
// RS_Wait4OK(RS232_Arr->commnumber);
RS_SetBitMode(RS232_Arr,9); /* Ïåðåäàëè âñå ïåðåñòðîèëèñü â 9-áèò äëß RS485?*/
RS_LineToReceive(RS232_Arr->commnumber); /* ðåæèì ïðèåìà RS485 */
RS232_Arr->flag_TIMEOUT_to_Send=false; /* ñáðîñèëè ôëàã îæèäàíèß òàéìàóòà */
}
if (RS232_Arr->RS_DataWillSend)
RS232_Arr->RS_DataSended = 1;
RS232_Arr->RS_DataWillSend = 0;
EnableUART_Int_RX(RS232_Arr->commnumber); /* Çàïðåùàåì ïðåðûâàíèß ïî ïåðåäà÷å */
}
}
}
//
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
//#pragma CODE_SECTION(RSA_TX_Handler,".fast_run2");
interrupt void RSA_TX_Handler(void)
{
// Set interrupt priority:
volatile Uint16 TempPIEIER = PieCtrlRegs.PIEIER9.all;
IER |= M_INT9;
IER &= MINT9; // Set "global" priority
PieCtrlRegs.PIEIER9.all &= MG92; // Set "group" priority
PieCtrlRegs.PIEACK.all = 0xFFFF; // Enable PIE interrupts
#if (_ENABLE_INTERRUPT_RS232_LED2)
i_led2_on_off(1);
#endif
#if (_ENABLE_INTERRUPT_PROFILE_LED1)
if (profile_interrupt.for_led1.bits.rsa)
i_led1_on_off_special(1);
#endif
#if (_ENABLE_INTERRUPT_PROFILE_LED2)
if (profile_interrupt.for_led2.bits.rsa)
i_led2_on_off_special(1);
#endif
EINT;
// i_led2_on_off(1);
// Insert ISR Code here.......
RS_TX_Handler(&rs_a);
// i_led2_on_off(0);
// Next line for debug only (remove after inserting ISR Code):
// ESTOP0;
SCIa_TX_IntClear();
// SciaRegs.SCIFFTX.bit.TXINTCLR=1; // Clear SCI Interrupt flag
PieCtrlRegs.PIEACK.all |= BIT8; // Issue PIE ACK
// Restore registers saved:
DINT;
PieCtrlRegs.PIEIER9.all = TempPIEIER;
#if (_ENABLE_INTERRUPT_PROFILE_LED1)
if (profile_interrupt.for_led1.bits.rsa)
i_led1_on_off_special(0);
#endif
#if (_ENABLE_INTERRUPT_PROFILE_LED2)
if (profile_interrupt.for_led2.bits.rsa)
i_led2_on_off_special(0);
#endif
#if (_ENABLE_INTERRUPT_RS232_LED2)
i_led2_on_off(0);
#endif
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
//#pragma CODE_SECTION(RSB_TX_Handler,".fast_run2");
interrupt void RSB_TX_Handler(void)
{
// Set interrupt priority:
volatile Uint16 TempPIEIER = PieCtrlRegs.PIEIER9.all;
IER |= M_INT9;
IER &= MINT9; // Set "global" priority
PieCtrlRegs.PIEIER9.all &= MG94; // Set "group" priority
PieCtrlRegs.PIEACK.all = 0xFFFF; // Enable PIE interrupts
#if(_ENABLE_PWM_LINES_FOR_TESTS_RS)
PWM_LINES_TK_16_ON;
#endif
#if (_ENABLE_INTERRUPT_RS232_LED2)
i_led2_on_off(1);
#endif
#if (_ENABLE_INTERRUPT_PROFILE_LED1)
if (profile_interrupt.for_led1.bits.rsb)
i_led1_on_off_special(1);
#endif
#if (_ENABLE_INTERRUPT_PROFILE_LED2)
if (profile_interrupt.for_led2.bits.rsb)
i_led2_on_off_special(1);
#endif
EINT;
// i_led2_on_off(1);
// Insert ISR Code here.......
RS_TX_Handler(&rs_b);
// i_led2_on_off(0);
// Next line for debug only (remove after inserting ISR Code):
// ESTOP0;
SCIb_TX_IntClear();
// SciaRegs.SCIFFTX.bit.TXINTCLR=1; // Clear SCI Interrupt flag
PieCtrlRegs.PIEACK.all |= BIT8; // Issue PIE ACK
// Restore registers saved:
DINT;
PieCtrlRegs.PIEIER9.all = TempPIEIER;
#if (_ENABLE_INTERRUPT_PROFILE_LED1)
if (profile_interrupt.for_led1.bits.rsb)
i_led1_on_off_special(0);
#endif
#if (_ENABLE_INTERRUPT_PROFILE_LED2)
if (profile_interrupt.for_led2.bits.rsb)
i_led2_on_off_special(0);
#endif
#if (_ENABLE_INTERRUPT_RS232_LED2)
i_led2_on_off(0);
#endif
#if(_ENABLE_PWM_LINES_FOR_TESTS_RS)
PWM_LINES_TK_16_OFF;
#endif
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
//#pragma CODE_SECTION(RSA_RX_Handler,".fast_run2");
interrupt void RSA_RX_Handler(void)
{
// Set interrupt priority:
volatile Uint16 TempPIEIER = PieCtrlRegs.PIEIER9.all;
IER |= M_INT9;
IER &= MINT9; // Set "global" priority
PieCtrlRegs.PIEIER9.all &= MG91; // Set "group" priority
PieCtrlRegs.PIEACK.all = 0xFFFF; // Enable PIE interrupts
#if (_ENABLE_INTERRUPT_RS232_LED2)
i_led2_on_off(1);
#endif
#if (_ENABLE_INTERRUPT_PROFILE_LED1)
if (profile_interrupt.for_led1.bits.rsa)
i_led1_on_off_special(1);
#endif
#if (_ENABLE_INTERRUPT_PROFILE_LED2)
if (profile_interrupt.for_led2.bits.rsa)
i_led2_on_off_special(1);
#endif
EINT;
// i_led1_on_off(1);
// Insert ISR Code here.......
// ClearTimerRS_Live(&rs_a);
// i_led2_on_off(0);
//RS_RX_Handler(&rs_a);
RS_RXA_Handler_fast(&rs_a);
// my_test_rs();
// Next line for debug only (remove after inserting ISR Code):
// ESTOP0;
// i_led2_on_off(0);
// i_led1_on_off(0);
SCIa_RX_IntClear();
// SciaRegs.SCIFFRX.bit.RXFFOVRCLR=1; // Clear Overflow flag
// SciaRegs.SCIFFRX.bit.RXFFINTCLR=1; // Clear Interrupt flag
PieCtrlRegs.PIEACK.all |= BIT8;
// Restore registers saved:
DINT;
PieCtrlRegs.PIEIER9.all = TempPIEIER;
#if (_ENABLE_INTERRUPT_PROFILE_LED1)
if (profile_interrupt.for_led1.bits.rsa)
i_led1_on_off_special(0);
#endif
#if (_ENABLE_INTERRUPT_PROFILE_LED2)
if (profile_interrupt.for_led2.bits.rsa)
i_led2_on_off_special(0);
#endif
#if (_ENABLE_INTERRUPT_RS232_LED2)
i_led2_on_off(0);
#endif
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
//#pragma CODE_SECTION(RSB_RX_Handler,".fast_run2");
interrupt void RSB_RX_Handler(void)
{
// Set interrupt priority:
volatile Uint16 TempPIEIER = PieCtrlRegs.PIEIER9.all;
IER |= M_INT9;
IER &= MINT9; // Set "global" priority
PieCtrlRegs.PIEIER9.all &= MG93; // Set "group" priority
PieCtrlRegs.PIEACK.all = 0xFFFF; // Enable PIE interrupts
#if(_ENABLE_PWM_LINES_FOR_TESTS_RS)
PWM_LINES_TK_17_ON;
#endif
#if (_ENABLE_INTERRUPT_RS232_LED2)
i_led2_on_off(1);
#endif
#if (_ENABLE_INTERRUPT_PROFILE_LED1)
if (profile_interrupt.for_led1.bits.rsb)
i_led1_on_off_special(1);
#endif
#if (_ENABLE_INTERRUPT_PROFILE_LED2)
if (profile_interrupt.for_led2.bits.rsb)
i_led2_on_off_special(1);
#endif
EINT;
// i_led1_on_off(1);
// Insert ISR Code here.......
//ClearTimerRS_Live(&rs_b);
RS_RXA_Handler_fast(&rs_b);
// i_led1_on_off(0);
// Next line for debug only (remove after inserting ISR Code):
// ESTOP0;
SCIb_RX_IntClear();
PieCtrlRegs.PIEACK.all |= BIT8;
// Restore registers saved:
DINT;
PieCtrlRegs.PIEIER9.all = TempPIEIER;
#if (_ENABLE_INTERRUPT_PROFILE_LED1)
if (profile_interrupt.for_led1.bits.rsb)
i_led1_on_off_special(0);
#endif
#if (_ENABLE_INTERRUPT_PROFILE_LED2)
if (profile_interrupt.for_led2.bits.rsb)
i_led2_on_off_special(0);
#endif
#if (_ENABLE_INTERRUPT_RS232_LED2)
i_led2_on_off(0);
#endif
#if(_ENABLE_PWM_LINES_FOR_TESTS_RS)
PWM_LINES_TK_17_OFF;
#endif
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
//////////////////////////////////////////////////////
///
//////////////////////////////////////////////////////
void RS_LineToSend(char commnumber)
{
/* Ðåæèì ïåðåäà÷è RS485. Çäåñü íå íóæåí,
ïîñêîëüêó RS485 òîëüêî íà êàíàëå B.*/
// addr_xilinx(TR485) = 0xffffffff;
// SCIa_RX_Int_disable(); // çàïðåò ïðåðûâàíèé íà ïðèåì
// SCIa_TX_Int_enable(); // ðàçðåøåíèå ïðåðûâàíèé íà ïåðåäà÷ó
if (commnumber==COM_1)
{
// ScibRegs.SCICTL1.bit.RXENA=0;
SciaRegs.SCICTL1.bit.RXENA=0;
// SciaRegs.SCICTL1.bit.TXENA=1;
}
else
{
EnableSendRS485();
ScibRegs.SCICTL1.bit.RXENA=0;
// ScibRegs.SCICTL1.bit.TXENA=1;
}
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
void RS_LineToReceive(char commnumber)
{
/* Ðåæèì ïðèåìà RS485. Çäåñü íå íóæåí,
ïîñêîëüêó RS485 òîëüêî íà êàíàëå B.*/
// addr_xilinx(TR485) = 0x0;
// SCIa_RX_Int_enable(); // ðàçðåøåíèå ïðåðûâàíèé íà ïðèåì
// SCIa_TX_Int_disable(); // çàïðåò ïðåðûâàíèé íà ïåðåäà÷ó
if (commnumber==COM_1)
{
SCIa_RX_IntClear();
}
else
{
EnableReceiveRS485();
// pause_1000(100);
SCIb_RX_IntClear();
// SCIb_TX_IntClear(); // clear TX FIFO interrupts
// my_test_rs(commnumber);
// SCIb_RX_IntClear();
// SCIb_TX_IntClear(); // clear TX FIFO interrupts
}
// pause_1000(1000);
EnableUART_Int_RX(commnumber); /* ðàçðåøåíèå ïðåðûâàíèé UART íà ïðèåì */
// my_test_rs(commnumber);
if (commnumber==COM_1)
{
// SCIa_RX_IntClear();
SciaRegs.SCICTL1.bit.RXENA=1;
}
else
{
// SCIb_RX_IntClear();
ScibRegs.SCICTL1.bit.RXENA=1;
}
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
static void RS_SetLineSpeed(char commnumber,unsigned long speed)
{
float SciBaud;
SciBaud = ((float)LSPCLK/(speed*8.0))-1.0;
if((SciBaud-(unsigned int)SciBaud)>0.5) SciBaud++;
if(commnumber==COM_1)
{
SciaRegs.SCIHBAUD = HIBYTE((int)SciBaud);
SciaRegs.SCILBAUD = LOBYTE((int)SciBaud);
}
if(commnumber==COM_2)
{
ScibRegs.SCIHBAUD = HIBYTE((int)SciBaud);
ScibRegs.SCILBAUD = LOBYTE((int)SciBaud);
}
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
//////////////////////////////////////////////////////
///
//////////////////////////////////////////////////////
void EnableUART_Int_RX(char commnumber)
{
#if(_ENABLE_PWM_LINES_FOR_TESTS_RS)
PWM_LINES_TK_18_ON;
#endif
switch (commnumber)
{
case COM_1: //SciaRegs.SCICTL2.bit.RXBKINTENA = 0;//1; //enableUARTInt_A();
SciaRegs.SCIFFRX.bit.RXFFIENA = 1;
// SciaRegs.SCICTL2.bit.TXINTENA = 1;
SciaRegs.SCIFFTX.bit.TXFFIENA = 0;
rs_a.RS_OnTransmitedData = 0;
SciaRegs.SCICTL1.bit.RXENA = 1;
SciaRegs.SCICTL1.bit.TXENA = 0;
break;
case COM_2: //ScibRegs.SCICTL2.bit.RXBKINTENA = 0;//1; //enableUARTInt_A();
ScibRegs.SCIFFRX.bit.RXFFIENA = 1;
// SciaRegs.SCICTL2.bit.TXINTENA = 1;
ScibRegs.SCIFFTX.bit.TXFFIENA = 0;
rs_b.RS_OnTransmitedData = 0;
ScibRegs.SCICTL1.bit.RXENA = 1;
ScibRegs.SCICTL1.bit.TXENA = 0;
break;
}
}
//////////////////////////////////////////////////////
///
//////////////////////////////////////////////////////
void EnableUART_Int_TX(char commnumber)
{
#if(_ENABLE_PWM_LINES_FOR_TESTS_RS)
PWM_LINES_TK_18_OFF;
#endif
switch (commnumber)
{
case COM_1: rs_a.RS_OnTransmitedData = 1;
//SciaRegs.SCICTL2.bit.RXBKINTENA = 0;
SciaRegs.SCIFFRX.bit.RXFFIENA = 0;
// SciaRegs.SCICTL2.bit.TXINTENA = 1;
SciaRegs.SCIFFTX.bit.TXFFIENA = 1;//EnableUART_IntW_A();
SciaRegs.SCICTL1.bit.RXENA = 0;
SciaRegs.SCICTL1.bit.TXENA = 1;
break;
case COM_2: rs_b.RS_OnTransmitedData = 1;
//ScibRegs.SCICTL2.bit.RXBKINTENA = 0;
ScibRegs.SCIFFRX.bit.RXFFIENA = 0;
// SciaRegs.SCICTL2.bit.TXINTENA = 1;
ScibRegs.SCIFFTX.bit.TXFFIENA = 1;//EnableUART_IntW_A();
ScibRegs.SCICTL1.bit.RXENA = 0;
ScibRegs.SCICTL1.bit.TXENA = 1;
break;
}
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
void RS_SetAdrAnswerController(RS_DATA_STRUCT *RS232_Arr,int set_addr_answer)
{
RS232_Arr->addr_answer = set_addr_answer; /* Ôëàã ðåæèìà êîíòðîëëåðà (ïî óìîë÷àíèþ âåäîìûé)*/
}
void RS_SetControllerLeading(RS_DATA_STRUCT *RS232_Arr,int bool)
{
RS232_Arr->flag_LEADING = bool; /* Ôëàã ðåæèìà êîíòðîëëåðà (ïî óìîë÷àíèþ âåäîìûé)*/
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
void RS_SetLineMode(char commnumber, int bit, char parity, int stop)
{
/*
SCICCR - SCI Communication Control Register
Bit Bit Name Designation Functions
20 SCI CHAR20 SCICHAR Select the character (data) length (one to eight bits).
3 ADDR/IDLE MODE ADDRIDLE_MODE The idle-line mode (0) is usually used for normal communications because the address-bit mode adds an extra bit to the frame. The idle-line mode does not add this extra bit and is compatible with RS-232 type communications.
4 LOOP BACK ENABLE LOOPBKENA This bit enables (1) the Loop Back test mode where the Tx pin is internally connected to the Rx pin.
5 PARITY ENABLE PARITYENA Enables the parity function if set to 1, or disables the parity function if cleared to 0.
6 EVEN/ODD PARITY PARITY If parity is enabled, selects odd parity if cleared to 0 or even parity if set to 1.
7 STOP BITS STOPBITS Determines the number of stop bits transmitted—one stop bit if cleared to 0 or two stop bits if set to 1.
*/
if (commnumber==COM_1)
{
if(bit>0 && bit<9) SciaRegs.SCICCR.bit.SCICHAR = bit-1;
switch(parity)
{
case 'N': SciaRegs.SCICCR.bit.PARITYENA = 0;
break;
case 'O': SciaRegs.SCICCR.bit.PARITYENA = 1;
SciaRegs.SCICCR.bit.PARITY = 0;
break;
case 'E': SciaRegs.SCICCR.bit.PARITYENA = 1;
SciaRegs.SCICCR.bit.PARITY = 1;
break;
}
if (stop==1) SciaRegs.SCICCR.bit.STOPBITS = 0;
if (stop==2) SciaRegs.SCICCR.bit.STOPBITS = 1;
SciaRegs.SCICCR.bit.LOOPBKENA = 0; //0
SciaRegs.SCICCR.bit.ADDRIDLE_MODE = 0;
}
if (commnumber==COM_2)
{
if(bit>0 && bit<9) ScibRegs.SCICCR.bit.SCICHAR = bit-1;
switch(parity)
{
case 'N': ScibRegs.SCICCR.bit.PARITYENA = 0;
break;
case 'O': ScibRegs.SCICCR.bit.PARITYENA = 1;
ScibRegs.SCICCR.bit.PARITY = 0;
break;
case 'E': ScibRegs.SCICCR.bit.PARITYENA = 1;
ScibRegs.SCICCR.bit.PARITY = 1;
break;
}
if (stop==1) ScibRegs.SCICCR.bit.STOPBITS = 0;
if (stop==2) ScibRegs.SCICCR.bit.STOPBITS = 1;
ScibRegs.SCICCR.bit.LOOPBKENA = 0; //0
ScibRegs.SCICCR.bit.ADDRIDLE_MODE = 0;
}
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
void RS_SetBitMode(RS_DATA_STRUCT *RS232_Arr, int n)
{
if(n == 8)
{
RS_SetLineMode(RS232_Arr->commnumber,8,'N',1); /* ðåæèì ëèíèè */
RS232_Arr->RS_Flag9bit=0;
}
if(n == 9)
{
RS_SetLineMode(RS232_Arr->commnumber,8,'N',1); /* ðåæèì ëèíèè */
RS232_Arr->RS_Flag9bit=1;
RS232_Arr->cmd_tx_stage = 0;
}
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
void clear_timer_rs_live(RS_DATA_STRUCT *rs_arr) {
ClearTimerRS_Live(rs_arr);
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
void clear_timer_rs_live_mpu(RS_DATA_STRUCT *rs_arr) {
rs_arr->time_wait_rs_out_mpu = 0;
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
void resetup_rs(RS_DATA_STRUCT *rs_arr) {
RS_LineToReceive(rs_arr->commnumber); // ðåæèì ïðèåìà RS485
RS_SetBitMode(rs_arr, 9);
RS_SetControllerLeading(rs_arr, false);
ClearTimerRS_Live(rs_arr);
// return -1;
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
void resetup_mpu_rs(RS_DATA_STRUCT *rs_arr) {
// RS_SetControllerLeading(rs_arr,false);
// RS_LineToReceive(commnumber); // ðåæèì ïðèåìà RS485
// RS_SetBitMode(&rs_b,9);
// rs_b.RS_PrevCmd = 0; // íå áûëî íèêàêèõ êîìàíä
//////
if (rs_arr->commnumber==COM_1)
SCIa_SW_Reset()
else
SCIb_SW_Reset();
RS_LineToReceive(rs_arr->commnumber); // ðåæèì ïðèåìà RS485
RS_SetBitMode(rs_arr, 9);
RS_SetControllerLeading(rs_arr, false);
ClearTimerRS_Live(rs_arr);
rs_arr->RS_PrevCmd = 0; // íå áûëî íèêàêèõ êîìàíä
rs_arr->RS_DataSended = 0;
rs_arr->RS_DataWillSend = 0;
rs_arr->RS_DataReadyAnswer = 0;
rs_arr->RS_DataReadyAnswerAnalyze = 0;
rs_arr->RS_DataReadyRequest = 0;
rs_arr->do_resetup_rs = 0;
// SCIb_RX_IntClear();
// SCIb_TX_IntClear(); // clear TX FIFO interrupts
EnableUART_Int_RX(rs_arr->commnumber); // ðàçðåøåíèå ïðåðûâàíèé UART
// return -1;
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
/* ïðîâåðêà íà æèâó÷åñòü RS */
int test_rs_live(RS_DATA_STRUCT *rs_arr)
{
if (rs_arr->time_wait_rs_out>(2*RS_TIME_OUT))
return 2; /* ïîðà ïåðåðóæàòü */
if (rs_arr->time_wait_rs_out>RS_TIME_OUT)
return 0; /* óìåð */
if (rs_arr->time_wait_rs_out_mpu>RS_TIME_OUT_MPU)
return 4; /* ïîðà ïåðåðóæàòü */
if (rs_arr->time_wait_rs_out>RS_TIME_OUT_HMI)
return 5; /* ïîðà ïîâòîðèòü çàïðîñ */
else
return 1; /* æèâ */
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
void inc_RS_timeout_cicle(void)
{
unsigned int i, t_refresh;
static unsigned int old_time_rs = 0;
t_refresh = get_delta_milisec(&old_time_rs, 1);
if (t_refresh>1000)
t_refresh = 1000;
if (t_refresh<1)
t_refresh = 1;
if (rs_a.time_wait_rs_out<RS_TIME_OUT_MAX)
rs_a.time_wait_rs_out += t_refresh;
if (rs_b.time_wait_rs_out<RS_TIME_OUT_MAX)
rs_b.time_wait_rs_out += t_refresh;
if (rs_a.time_wait_rs_out_mpu<RS_TIME_OUT_MAX)
rs_a.time_wait_rs_out_mpu += t_refresh;
if (rs_b.time_wait_rs_out_mpu<RS_TIME_OUT_MAX)
rs_b.time_wait_rs_out_mpu += t_refresh;
if ((rs_a.RS_Flag9bit==0) || rs_a.do_resetup_rs || SCIa_RX_Error())
{
if (rs_a.time_wait_rs_lost<RS_TIME_OUT_LOST)
rs_a.time_wait_rs_lost += t_refresh;
// else
// resetup_mpu_rs(&rs_a);
}
if ((rs_b.RS_Flag9bit==0) || rs_b.do_resetup_rs || SCIb_RX_Error())
{
if (rs_b.time_wait_rs_lost<RS_TIME_OUT_LOST)
rs_b.time_wait_rs_lost += t_refresh;
// else
// resetup_mpu_rs(&rs_b);
}
}
///////////////////////////////////////////////////////////
void resetup_rs_on_timeout_lost(int rsp)
{
if (rsp==COM_1)
{
if (rs_a.time_wait_rs_lost>=RS_TIME_OUT_LOST)
resetup_mpu_rs(&rs_a);
}
else
{
if (rs_b.time_wait_rs_lost>=RS_TIME_OUT_LOST)
resetup_mpu_rs(&rs_b);
}
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
void clear_buffers_rs(RS_DATA_STRUCT *rs_arr)
{
unsigned int i;
for (i=0;i<MAX_RECEIVE_LENGTH;i++)
rs_arr->RS_Header[i] = 0;
for (i=0;i<MAX_SEND_LENGTH;i++)
rs_arr->buffer[i] = 0;
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
#ifdef _USE_RS_FIFO
void SetupUART(char commnumber, unsigned long speed_baud)
{
// x1 Free run. Continues SCI operation regardless of suspend
// 10 Complete current receive/transmit sequence before stopping
// 00 Immediate stop on suspend
if(commnumber==COM_1)
{
SciaRegs.SCIPRI.bit.FREE = 1;
SciaRegs.SCIPRI.bit.SOFT = 0;
// Initialize SCI-A:
// Note: Clocks were turned on to the SCIA peripheral
// in the InitSysCtrl() function
EALLOW;
GpioMuxRegs.GPFMUX.bit.SCIRXDA_GPIOF5=1;
GpioMuxRegs.GPFMUX.bit.SCITXDA_GPIOF4=1;
EDIS;
RS_SetLineMode(commnumber,8,'N',1);
///////
SciaRegs.SCICCR.all =0x0007; // 1 stop bit, No loopback
// No parity,8 char bits,
// async mode, idle-line protocol
SciaRegs.SCICTL1.all = 0;
// SciaRegs.SCICTL1.bit.RXENA = 1; // enable RX,
// SciaRegs.SCICTL1.bit.TXENA = 1; // enable TX
SciaRegs.SCICTL1.bit.RXENA = 0; // disable RX,
SciaRegs.SCICTL1.bit.TXENA = 0; // disable TX
SciaRegs.SCICTL1.bit.RXERRINTENA = 1; //Receive error interrupt enabled
SciaRegs.SCICTL1.bit.TXWAKE = 0;
SciaRegs.SCICTL1.bit.SLEEP = 0;
SciaRegs.SCICTL2.bit.TXINTENA = 0;//1;
SciaRegs.SCICTL2.bit.RXBKINTENA = 0;//1;
RS_SetLineSpeed(commnumber,speed_baud); /* ñêîðîñòü ëèíèè */
SciaRegs.SCICCR.bit.LOOPBKENA = 0; // Enable loop back
// SciaRegs.SCIFFTX.all=0xC028;
//SCIFFTX
SciaRegs.SCIFFTX.bit.SCIFFENA=1; // SCI FIFO enable
SciaRegs.SCIFFTX.bit.TXFFILIL = 0;//1; // TXFFIL40 Transmit FIFO interrupt level bits. Transmit FIFO will generate interrupt when the FIFO
// status bits (TXFFST40) and FIFO level bits (TXFFIL40 ) match (less than or equal to).
SciaRegs.SCIFFTX.bit.TXFIFOXRESET=0;
SciaRegs.SCIFFTX.bit.TXFFST = 0;
SciaRegs.SCIFFTX.bit.TXFFINT = 0;
SciaRegs.SCIFFTX.bit.TXINTCLR = 0;
SciaRegs.SCIFFTX.bit.TXFFIENA = 0; // Transmit FIFO interrrupt enable
SciaRegs.SCIFFTX.bit.SCIRST = 1;
//SCIFFRX
// SciaRegs.SCIFFRX.all=0x0028;
// SciaRegs.SCIFFRX.all=0x0022;
SciaRegs.SCIFFRX.bit.RXFFIL = 1; // 4:0 Interrupt level
SciaRegs.SCIFFRX.bit.RXFFIENA = 1; // Interrupt enable
SciaRegs.SCIFFCT.all=0x00; // FIFO control register
SciaRegs.SCIFFCT.bit.ABDCLR=1;
SciaRegs.SCIFFCT.bit.CDC=0;
SciaRegs.SCIFFCT.bit.FFTXDLY = 0;//100;
// SciaRegs.SCICTL1.all =0x0023; // Relinquish SCI from Reset
SciaRegs.SCICTL1.bit.SWRESET = 1; // Relinquish SCI from Reset
SciaRegs.SCIFFTX.bit.TXFIFOXRESET=1;
SciaRegs.SCIFFRX.bit.RXFIFORESET=1;
EALLOW;
PieVectTable.RXAINT = &RSA_RX_Handler;
PieVectTable.TXAINT = &RSA_TX_Handler;
PieCtrlRegs.PIEIER9.bit.INTx1=1; // PIE Group 9, INT1
PieCtrlRegs.PIEIER9.bit.INTx2=1; // PIE Group 9, INT2
IER |= M_INT9; // Enable CPU INT
EDIS;
SetupArrCmdLength();
clear_buffers_rs(&rs_a);
RS_SetControllerLeading(&rs_a,false);
RS_LineToReceive(commnumber); // ðåæèì ïðèåìà RS485
EnableUART_Int_RX(commnumber); // ðàçðåøåíèå ïðåðûâàíèé UART
RS_SetBitMode(&rs_a,9);
rs_a.RS_PrevCmd = 0; // íå áûëî íèêàêèõ êîìàíä
SCIa_RX_IntClear();
SCIa_TX_IntClear(); // clear TX FIFO interrupts
resetup_mpu_rs(&rs_a);
}
if(commnumber==COM_2)
{
// Initialize SCI-B:
// Note: Clocks were turned on to the SCIA peripheral
// in the InitSysCtrl() function
ScibRegs.SCIPRI.bit.FREE = 1;
ScibRegs.SCIPRI.bit.SOFT = 0;
EALLOW;
GpioMuxRegs.GPGMUX.bit.SCIRXDB_GPIOG5=1;
GpioMuxRegs.GPGMUX.bit.SCITXDB_GPIOG4=1;
GpioMuxRegs.GPBMUX.bit.C5TRIP_GPIOB14=0;
GpioMuxRegs.GPBDIR.bit.GPIOB14=1;
EDIS;
RS_SetLineMode(commnumber,8,'N',1);
///////
ScibRegs.SCICCR.all =0x0007; // 1 stop bit, No loopback
// No parity,8 char bits,
// async mode, idle-line protocol
ScibRegs.SCICTL1.all = 0;
// SciaRegs.SCICTL1.bit.RXENA = 1; // enable RX,
// SciaRegs.SCICTL1.bit.TXENA = 1; // enable TX
ScibRegs.SCICTL1.bit.RXENA = 0; // disable RX,
ScibRegs.SCICTL1.bit.TXENA = 0; // disable TX
ScibRegs.SCICTL1.bit.RXERRINTENA = 1; //Receive error interrupt enabled
ScibRegs.SCICTL1.bit.TXWAKE = 0;
ScibRegs.SCICTL1.bit.SLEEP = 0;
ScibRegs.SCICTL2.bit.TXINTENA = 0;//1;
ScibRegs.SCICTL2.bit.RXBKINTENA =0;// 1;
RS_SetLineSpeed(commnumber,speed_baud); /* ñêîðîñòü ëèíèè */
ScibRegs.SCICCR.bit.LOOPBKENA = 0; // Enable loop back
// SciaRegs.SCIFFTX.all=0xC028;
//SCIFFTX
ScibRegs.SCIFFTX.bit.SCIFFENA=1; // SCI FIFO enable
ScibRegs.SCIFFTX.bit.TXFFILIL = 0;//1; // TXFFIL40 Transmit FIFO interrupt level bits. Transmit FIFO will generate interrupt when the FIFO
// status bits (TXFFST40) and FIFO level bits (TXFFIL40 ) match (less than or equal to).
ScibRegs.SCIFFTX.bit.TXFIFOXRESET=0;
ScibRegs.SCIFFTX.bit.TXFFST = 0;
ScibRegs.SCIFFTX.bit.TXFFINT = 0;
ScibRegs.SCIFFTX.bit.TXINTCLR = 0;
ScibRegs.SCIFFTX.bit.TXFFIENA = 0; // Transmit FIFO interrrupt enable
ScibRegs.SCIFFTX.bit.SCIRST = 1;
//SCIFFRX
// SciaRegs.SCIFFRX.all=0x0028;
// SciaRegs.SCIFFRX.all=0x0022;
ScibRegs.SCIFFRX.bit.RXFFIL = 1; // 4:0 Interrupt level
ScibRegs.SCIFFRX.bit.RXFFIENA = 1; // Interrupt enable
ScibRegs.SCIFFCT.all=0x00; // FIFO control register
ScibRegs.SCIFFCT.bit.ABDCLR=1;
ScibRegs.SCIFFCT.bit.CDC=0;
ScibRegs.SCIFFCT.bit.FFTXDLY = 0;//100;
ScibRegs.SCICTL1.bit.SWRESET = 1; // Relinquish SCI from Reset
ScibRegs.SCIFFTX.bit.TXFIFOXRESET=1;
ScibRegs.SCIFFRX.bit.RXFIFORESET=1;
EALLOW;
PieVectTable.RXBINT = &RSB_RX_Handler;
PieVectTable.TXBINT = &RSB_TX_Handler;
PieCtrlRegs.PIEIER9.bit.INTx3=1; // PIE Group 9, INT3
PieCtrlRegs.PIEIER9.bit.INTx4=1; // PIE Group 9, INT4
IER |= M_INT9; // Enable CPU INT
EDIS;
SetupArrCmdLength();
clear_buffers_rs(&rs_b);
RS_SetControllerLeading(&rs_b,false);
RS_LineToReceive(commnumber); // ðåæèì ïðèåìà RS485
EnableUART_Int_RX(commnumber); // ðàçðåøåíèå ïðåðûâàíèé UART
RS_SetBitMode(&rs_b,9);
rs_b.RS_PrevCmd = 0; // íå áûëî íèêàêèõ êîìàíä
SCIb_RX_IntClear();
SCIb_TX_IntClear(); // clear TX FIFO interrupts
resetup_mpu_rs(&rs_b);
}
}
#else
//
void SetupUART(char commnumber, unsigned long speed_baud)
{
if(commnumber==COM_1)
{
// Initialize SCI-A:
// Note: Clocks were turned on to the SCIA peripheral
// in the InitSysCtrl() function
EALLOW;
GpioMuxRegs.GPFMUX.bit.SCIRXDA_GPIOF5=1;
GpioMuxRegs.GPFMUX.bit.SCITXDA_GPIOF4=1;
EDIS;
RS_SetLineMode(commnumber,8,'N',1);
// enable TX, RX, internal SCICLK,
// Disable RX ERR, SLEEP, TXWAKE
SciaRegs.SCIFFCT.bit.ABDCLR=1;
SciaRegs.SCIFFCT.bit.CDC=0;
SciaRegs.SCICTL1.bit.RXERRINTENA=0;
SciaRegs.SCICTL1.bit.SWRESET=0;
SciaRegs.SCICTL1.bit.TXWAKE=0;
SciaRegs.SCICTL1.bit.SLEEP=0;
SciaRegs.SCICTL1.bit.TXENA=1;
SciaRegs.SCICTL1.bit.RXENA=1;
SciaRegs.SCIFFTX.bit.SCIFFENA=0; // fifo off
SciaRegs.SCIFFRX.bit.RXFFIL=1; //Äëèíà íàèìåíüøåé êîìàíäû
EALLOW;
PieVectTable.RXAINT = &RSA_RX_Handler;
PieVectTable.TXAINT = &RSA_TX_Handler;
PieCtrlRegs.PIEIER9.bit.INTx1=1; // PIE Group 9, INT1
PieCtrlRegs.PIEIER9.bit.INTx2=1; // PIE Group 9, INT2
IER |= M_INT9; // Enable CPU INT
EDIS;
SetupArrCmdLength();
RS_SetLineSpeed(commnumber,speed_baud); /* ñêîðîñòü ëèíèè */
RS_SetControllerLeading(&rs_a,false);
RS_LineToReceive(commnumber); // ðåæèì ïðèåìà RS485
EnableUART_Int(commnumber); // ðàçðåøåíèå ïðåðûâàíèé UART
RS_SetBitMode(&rs_a,9);
rs_a.RS_PrevCmd = 0; // íå áûëî íèêàêèõ êîìàíä
SCI_RX_IntClear(commnumber);
SciaRegs.SCICTL1.bit.SWRESET=1; // Relinquish SCI from Reset
// SciaRegs.SCIFFTX.bit.TXFIFOXRESET=1;
// SciaRegs.SCIFFRX.bit.RXFIFORESET=1;
}
if(commnumber==COM_2)
{
// Initialize SCI-B:
// Note: Clocks were turned on to the SCIA peripheral
// in the InitSysCtrl() function
EALLOW;
GpioMuxRegs.GPGMUX.bit.SCIRXDB_GPIOG5=1;
GpioMuxRegs.GPGMUX.bit.SCITXDB_GPIOG4=1;
GpioMuxRegs.GPBMUX.bit.C5TRIP_GPIOB14=0;
GpioMuxRegs.GPBDIR.bit.GPIOB14=1;
EDIS;
RS_SetLineMode(commnumber,8,'N',1);
// enable TX, RX, internal SCICLK,
// Disable RX ERR, SLEEP, TXWAKE
ScibRegs.SCIFFCT.bit.CDC=0;
ScibRegs.SCIFFCT.bit.ABDCLR=1;
ScibRegs.SCICTL1.bit.RXERRINTENA=0;
ScibRegs.SCICTL1.bit.SWRESET=0;
ScibRegs.SCICTL1.bit.TXWAKE=0;
ScibRegs.SCICTL1.bit.SLEEP=0;
ScibRegs.SCICTL1.bit.TXENA=1;
ScibRegs.SCICTL1.bit.RXENA=1;
ScibRegs.SCIFFTX.bit.SCIFFENA=0; // fifo off
ScibRegs.SCIFFRX.bit.RXFFIL=1; //Äëèíà íàèìåíüøåé êîìàíäû
EALLOW;
PieVectTable.RXBINT = &RSB_RX_Handler;
PieVectTable.TXBINT = &RSB_TX_Handler;
PieCtrlRegs.PIEIER9.bit.INTx3=1; // PIE Group 9, INT3
PieCtrlRegs.PIEIER9.bit.INTx4=1; // PIE Group 9, INT4
IER |= M_INT9; // Enable CPU INT
EDIS;
SetupArrCmdLength();
RS_SetLineSpeed(commnumber,speed_baud); /* ñêîðîñòü ëèíèè */
RS_SetControllerLeading(&rs_b,false);
RS_LineToReceive(commnumber); // ðåæèì ïðèåìà RS485
EnableUART_Int(commnumber); // ðàçðåøåíèå ïðåðûâàíèé UART
RS_SetBitMode(&rs_b,9);
rs_b.RS_PrevCmd = 0; // íå áûëî íèêàêèõ êîìàíä
SCI_RX_IntClear(commnumber);
ScibRegs.SCICTL1.bit.SWRESET=1; // Relinquish SCI from Reset
// SciaRegs.SCIFFTX.bit.TXFIFOXRESET=1;
// SciaRegs.SCIFFRX.bit.RXFIFORESET=1;
}
}
#endif
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
void SCI_Send(char commnumber, char bs)
{
switch (commnumber)
{
case COM_1: SCIa_Send(bs);
break;
case COM_2: SCIb_Send(bs);
break;
}
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
#ifdef _USE_RS_FIFO
void RS_Wait4OK(char commnumber)
{
switch (commnumber)
{
case COM_1: while (SciaRegs.SCIFFTX.bit.TXFFST != 0) {}//SCIa_Wait4OK();
break;
case COM_2: while (ScibRegs.SCIFFTX.bit.TXFFST != 0) {}//SCIb_Wait4OK();
break;
}
}
void RS_Wait4OK_TXRDY(char commnumber)
{
switch (commnumber)
{
case COM_1: while (SciaRegs.SCICTL2.bit.TXEMPTY != 0) {}
break;
case COM_2: while (ScibRegs.SCICTL2.bit.TXEMPTY != 0) {}
break;
}
}
#else
void RS_Wait4OK(char commnumber)
{
switch (commnumber)
{
case COM_1: SCIa_Wait4OK();
break;
case COM_2: SCIb_Wait4OK();
break;
}
}
#endif
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
void SetCntrlAddr (int cntrl_addr,int cntrl_addr_for_all)
{
CNTRL_ADDR = cntrl_addr;
ADDR_FOR_ALL = cntrl_addr_for_all;
}
void Poke(RS_DATA_STRUCT *RS232_Arr)
{
unsigned long Address;
unsigned int Data;
Address = RS232_Arr->RS_Header[5] & 0xFF;
Address = (Address<<8) | (RS232_Arr->RS_Header[4] & 0xFF);
Address = (Address<<8) | (RS232_Arr->RS_Header[3] & 0xFF);
Address = (Address<<8) | (RS232_Arr->RS_Header[2] & 0xFF);
Data = 0;
Data = (Data<<8) | (RS232_Arr->RS_Header[7] & 0xFF);
Data = (Data<<8) | (RS232_Arr->RS_Header[6] & 0xFF);
WriteMemory(Address,Data);
Answer(RS232_Arr, CMD_RS232_POKE);
}
void Peek(RS_DATA_STRUCT *RS232_Arr)
{
unsigned long Address;
unsigned int Data, crc;
Address = RS232_Arr->RS_Header[5] & 0xFF;
Address = (Address<<8) | (RS232_Arr->RS_Header[4] & 0xFF);
Address = (Address<<8) | (RS232_Arr->RS_Header[3] & 0xFF);
Address = (Address<<8) | (RS232_Arr->RS_Header[2] & 0xFF);
Data = 0;
Data = i_ReadMemory(Address);
RS232_Arr->buffer[0] = RS232_Arr->addr_recive; //CNTRL_ADDR;
RS232_Arr->buffer[1] = CMD_RS232_PEEK;
RS232_Arr->buffer[2] = LOBYTE(Data);
RS232_Arr->buffer[3] = HIBYTE(Data);
RS232_Arr->buffer[4] = 0;
RS232_Arr->buffer[5] = 0;
crc = 0xffff;
crc = GetCRC16_IBM(crc, RS232_Arr->buffer, 6);
RS232_Arr->buffer[6] = LOBYTE(crc);
RS232_Arr->buffer[7] = HIBYTE(crc);
RS232_Arr->buffer[8] = 0;
RS232_Arr->buffer[9] = 0;
RS_Send(RS232_Arr,RS232_Arr->buffer, 10);
}
static char _GetByte(unsigned int *addr, int32 offs)
{
unsigned int *address;
unsigned int byte;
address = addr + offs/2;
byte = *address;
if(offs%2) return LOBYTE(byte);
else return HIBYTE(byte);
}
void Answer(RS_DATA_STRUCT *RS232_Arr,int n)
{
int crc;
RS232_Arr->buffer[0] = RS232_Arr->addr_recive; //CNTRL_ADDR;
RS232_Arr->buffer[1] = n;
crc = 0xffff;
crc = GetCRC16_IBM( crc, RS232_Arr->buffer, 2);
RS232_Arr->buffer[2] = LOBYTE(crc);
RS232_Arr->buffer[3] = HIBYTE(crc);
RS232_Arr->buffer[4] = 0;
RS232_Arr->buffer[5] = 0;
RS_Send(RS232_Arr,RS232_Arr->buffer, 6);
}
void Load(RS_DATA_STRUCT *RS232_Arr)
{
unsigned int rcrc, crc;
crc = (_GetByte(RS232_Arr->pRecvPtr, RS232_Arr->RS_Length-1) << 8) +
_GetByte(RS232_Arr->pRecvPtr, RS232_Arr->RS_Length-2);
RS232_Arr->RS_Header[0] = RS232_Arr->addr_recive;
// CNTRL_ADDR;
RS232_Arr->RS_Header[1]=CMD_RS232_LOAD;
rcrc = 0xffff;
rcrc = GetCRC16_IBM( rcrc, RS232_Arr->RS_Header, 2);
rcrc = GetCRC16_B( rcrc, RS232_Arr->pRecvPtr, RS232_Arr->RS_Length-2);
if(rcrc == crc)
{
Answer(RS232_Arr,CMD_RS232_LOAD);
RS232_Arr->BS_LoadOK = true;
}
else
{
RS232_Arr->BS_LoadOK = false;
RS_LineToReceive(RS232_Arr->commnumber); // ðåæèì ïðèåìà RS485
RS_SetBitMode(RS232_Arr,9);
}
}
void InitLoad(RS_DATA_STRUCT *RS232_Arr)
{
unsigned long Address;
Address = RS232_Arr->RS_Header[5] & 0xFF;
Address = (Address<<8) | (RS232_Arr->RS_Header[4] & 0xFF);
Address = (Address<<8) | (RS232_Arr->RS_Header[3] & 0xFF);
Address = (Address<<8) | (RS232_Arr->RS_Header[2] & 0xFF);
RS232_Arr->RS_Length = RS232_Arr->RS_Header[9] & 0xFF;
RS232_Arr->RS_Length = (RS232_Arr->RS_Length<<8) | (RS232_Arr->RS_Header[8] & 0xFF);
RS232_Arr->RS_Length = (RS232_Arr->RS_Length<<8) | (RS232_Arr->RS_Header[7] & 0xFF);
RS232_Arr->RS_Length = (RS232_Arr->RS_Length<<8) | (RS232_Arr->RS_Header[6] & 0xFF);
RS232_Arr->RS_Length += 2;
RS232_Arr->pRS_RecvPtr = (unsigned int *)Address; //(unsigned int *)Address;
RS232_Arr->pRecvPtr = (unsigned int *)Address; //(unsigned int *)Address;
Answer(RS232_Arr,CMD_RS232_INITLOAD);
}
int GetCommand(RS_DATA_STRUCT *RS232_Arr)
{
int cmd;
unsigned int crc, rcrc;
if(RS232_Arr->RS_DataReady) // Äàííûå ïî RS ïðèøëè
{
RS232_Arr->RS_DataReady = false;
cmd = RS232_Arr->RS_Header[1]; // Ïðî÷èòàëè íîìåð êîìàíäû
// Ïðîâåðßåì äëèíó êîìàíäû äëß ñ÷èòûâàíèß CRC
if((RS_Len[cmd]<3) || (RS_Len[cmd]>MAX_RECEIVE_LENGTH))
{
(RS232_Arr->count_recive_bad)++;
RS_LineToReceive(RS232_Arr->commnumber); // ðåæèì ïðèåìà RS485
RS_SetBitMode(RS232_Arr,9);
if (RS232_Arr->RS_DataSended)
RS232_Arr->RS_DataSended = 0;
return -1;
}
if(cmd == 4) {
asm(" NOP ");
}
if(cmd == CMD_RS232_LOAD) // Åñëè êîìàíäà çàãðóçêè
{
RS232_Arr->RS_PrevCmd = cmd;
return cmd; // Íåò ïðîâåðêè crc
}
else // Âñå îñòàëüíûå êîìàíäû
{
// Ñ÷èòûâàåì crc èç ïîñûëêè
crc = (RS232_Arr->RS_Header[RS_Len[cmd]-1] << 8) |
(RS232_Arr->RS_Header[RS_Len[cmd]-2]) ;
}
// Ðàññ÷èòûâàåì crc èç ïîñûëêè
rcrc = 0xffff;
rcrc = GetCRC16_IBM( rcrc, RS232_Arr->RS_Header, (RS_Len[cmd]-2) );
if(crc == rcrc) // Ïðîâåðßåì crc
{
(RS232_Arr->count_recive_ok)++;
RS232_Arr->RS_PrevCmd = cmd;
if (RS232_Arr->RS_DataSended)
{
RS232_Arr->RS_DataSended = 0;
RS232_Arr->RS_DataReadyAnswer = 1;
}
return cmd;
}
else
{
(RS232_Arr->count_recive_error_crc)++;
RS_SetAdrAnswerController(RS232_Arr,0);
RS_SetControllerLeading(RS232_Arr, false);
RS_LineToReceive(RS232_Arr->commnumber); // ðåæèì ïðèåìà RS485
RS_SetBitMode(RS232_Arr,9);
if (RS232_Arr->RS_DataSended)
RS232_Arr->RS_DataSended = 0;
} }
return -1;
}
void ExtendBios(RS_DATA_STRUCT *RS232_Arr)
{
volatile unsigned long Address1,Address2;
volatile unsigned long Length, LengthW,i;
unsigned int code1,code2, crc;
unsigned long AdrOut1,AdrOut2,LengthOut;
unsigned int cerr, repl, count_ok, return_code, old_started;
volatile unsigned int go_to_reset = 0, go_to_set_baud = 0;
unsigned long set_baud;
//int code_eeprom;
old_started = x_parallel_bus_project.flags.bit.started;
Address1 = RS232_Arr->RS_Header[5] & 0xFF;
Address1 = (Address1<<8) | (RS232_Arr->RS_Header[4] & 0xFF);
Address1 = (Address1<<8) | (RS232_Arr->RS_Header[3] & 0xFF);
Address1 = (Address1<<8) | (RS232_Arr->RS_Header[2] & 0xFF);
Address2 = RS232_Arr->RS_Header[9] & 0xFF;
Address2 = (Address2<<8) | (RS232_Arr->RS_Header[8] & 0xFF);
Address2 = (Address2<<8) | (RS232_Arr->RS_Header[7] & 0xFF);
Address2 = (Address2<<8) | (RS232_Arr->RS_Header[6] & 0xFF);
Length = RS232_Arr->RS_Header[13] & 0xFF;
Length = (Length<<8) | (RS232_Arr->RS_Header[12] & 0xFF);
Length = (Length<<8) | (RS232_Arr->RS_Header[11] & 0xFF);
Length = (Length<<8) | (RS232_Arr->RS_Header[10] & 0xFF);
LengthW = Length/2;
if (LengthW*2<Length) LengthW++;
code1=RS232_Arr->RS_Header[14] & 0xFF;
code2=RS232_Arr->RS_Header[15] & 0xFF;
AdrOut1 = 0;
AdrOut2 = 0;
LengthOut = 0;
for (i=0;i<1000;i++)
{
DINT;
}
DRTM;
DINT;
switch ( code1 )
{
// ãðóçèì Xilinx Èç flash
case 1:
x_parallel_bus_project.stop(&x_parallel_bus_project);
load_xilinx_new(0x130000,SIZE_XILINX200);
project.reload_all_plates_with_reset_no_stop_error();
project.clear_errors_all_plates();
if (old_started) project_start_parallel_bus();
break;
/* ãðóçèì 6713 èç Flash
çàãðóçêà ïðîèñõîäèò ñ ðåñåòîì 2812 !!!!
*/
//case 2: select_reset28_for_load67(); break; // íàñòðîéêà çàãðóçêè
// ãðóçèì Xilinx Èç RAM
case 3: load_xilinx_new(0x80000,SIZE_XILINX200); break;
//4 è 5 êîìàíäà çàíßòû ïîä EEPROM äëß Slave
case 6:
// for Spartan2
x_parallel_bus_project.stop(&x_parallel_bus_project);
xflash_remote_eeprom(code2, Address1, Address2, LengthW,&AdrOut1,&AdrOut2,&LengthOut );
project.reload_all_plates_with_reset_no_stop_error();
project.clear_errors_all_plates();
if (old_started) project_start_parallel_bus();
break;
case 7:
x_parallel_bus_project.stop(&x_parallel_bus_project);
xread_remote_eeprom(code2, Address1, Address2, LengthW, &AdrOut1,&AdrOut2,&LengthOut );
project.reload_all_plates_with_reset_no_stop_error();
project.clear_errors_all_plates();
if (old_started) project_start_parallel_bus();
// xread_remote_eeprom_byte(code2, Address1, Address2, Length, &AdrOut1,&AdrOut2,&LengthOut );
break;
case 17:
x_parallel_bus_project.stop(&x_parallel_bus_project);
xverify_remote_eeprom(code2, Address1, Address2, LengthW, &AdrOut1,&AdrOut2,&LengthOut );
project.reload_all_plates_with_reset_no_stop_error();
project.clear_errors_all_plates();
if (old_started) project_start_parallel_bus();
// xread_remote_eeprom_byte(code2, Address1, Address2, Length, &AdrOut1,&AdrOut2,&LengthOut );
break;
case 8:
// reload al plates
x_parallel_bus_project.stop(&x_parallel_bus_project);
project.reload_all_plates_with_reset_no_stop_error();
project.clear_errors_all_plates();
if (old_started) project_start_parallel_bus();
break;
case 9:
go_to_reset = 1;
// SetLoad28_FromResetInternalFlash();
// SelectReset28();
break;
case 10:
// for Spartan6
memWrite(code2, Address1, Address2, LengthW,&AdrOut1,&AdrOut2,&LengthOut);
break;
case 11:
// flash ñî ñòàòèñòèêîé
if(!RS232_Arr->BS_LoadOK)
{
RS_LineToReceive(RS232_Arr->commnumber); // ðåæèì ïðèåìà RS485
RS_SetBitMode(RS232_Arr,9);
return;
}
return_code = RunFlashData(Address1, Address2, LengthW, &cerr, &repl, &count_ok );
AdrOut1 = cerr;
AdrOut2 = repl;
LengthOut = return_code+1;
break;
case 12:
// Verify flash ñî ñòàòèñòèêîé
if(!RS232_Arr->BS_LoadOK)
{
RS_LineToReceive(RS232_Arr->commnumber); // ðåæèì ïðèåìà RS485
RS_SetBitMode(RS232_Arr,9);
return;
}
return_code = VerifyFlashData(Address1, Address2, LengthW, &cerr, &repl, &count_ok );
AdrOut1 = cerr;
AdrOut2 = repl;
LengthOut = return_code+1;
break;
case 100:
go_to_set_baud = 1;
set_baud = Address1;
// SetLoad28_FromResetInternalFlash();
// SelectReset28();
//speed_baud = Address1;
break;
default: break;
}
EnableInterrupts();
ERTM; // Enable Global realtime interrupt DBGM
RS232_Arr->buffer[0] = RS232_Arr->addr_recive; //CNTRL_ADDR;
RS232_Arr->buffer[1] = CMD_RS232_EXTEND;
RS232_Arr->buffer[2] = BYTE0(AdrOut1);
RS232_Arr->buffer[3] = BYTE1(AdrOut1);
RS232_Arr->buffer[4] = BYTE2(AdrOut1);
RS232_Arr->buffer[5] = BYTE3(AdrOut1);
RS232_Arr->buffer[6] = BYTE0(AdrOut2);
RS232_Arr->buffer[7] = BYTE1(AdrOut2);
RS232_Arr->buffer[8] = BYTE2(AdrOut2);
RS232_Arr->buffer[9] = BYTE3(AdrOut2);
RS232_Arr->buffer[10] = BYTE0(LengthOut);
RS232_Arr->buffer[11] = BYTE1(LengthOut);
RS232_Arr->buffer[12] = BYTE2(LengthOut);
RS232_Arr->buffer[13] = BYTE3(LengthOut);
crc = 0xffff;
crc = GetCRC16_IBM(crc, RS232_Arr->buffer, 14);
RS232_Arr->buffer[14] = LOBYTE(crc);
RS232_Arr->buffer[15] = HIBYTE(crc);
RS232_Arr->buffer[16] = 0;
RS232_Arr->buffer[17] = 0;
RS_Send(RS232_Arr,RS232_Arr->buffer, 18);
if (go_to_reset)
{
for (i=0;i<2;i++)
DELAY_US(1000000);
DRTM;
DINT;
for (i=0;i<2;i++)
DELAY_US(1000000);
SetLoad28_FromResetInternalFlash();
SelectReset28();
go_to_reset = 0;
}
if (go_to_set_baud)
{
// for (i=0;i<2;i++)
DELAY_US(1000000);
// DRTM;
// DINT;
// for (i=0;i<2;i++)
// DELAY_US(1000000);
RS_SetLineSpeed(RS232_Arr->commnumber, set_baud); /* ñêîðîñòü ëèíèè */
go_to_set_baud = 0;
}
return;
}
void create_uart_vars(char size_cmd15_set, char size_cmd16_set)
{
size_cmd15=size_cmd15_set;
size_cmd16=size_cmd16_set;
rs_a.commnumber=COM_1;
rs_b.commnumber=COM_2;
}
//////////////////////////////////////////////////////
///
//////////////////////////////////////////////////////
int RS_Send(RS_DATA_STRUCT *RS232_Arr,unsigned int *pBuf,unsigned long len)
{
unsigned int i;
if (RS232_Arr->RS_DataWillSend)
{
// RS232_Arr->RS_DataReadyAnswer = 0;
RS232_Arr->RS_DataReadyAnswer = 0;
RS232_Arr->RS_DataSended = 0;
}
//for (i=0; i <= 30000; i++){} /* Ïàóçà äëß PC 30000*/
RS_LineToSend(RS232_Arr->commnumber); /* ðåæèì ïåðåäà÷è RS485 */
//for (i=0; i <= 10000; i++){} /* Ïàóçà äëß PC10000 */
RS232_Arr->RS_SLength = len; /* Íàñòðàèâàåì ïåðåìåííûå */
// RS232_Arr->pRS_SendPtr = pBuf + 1;
RS232_Arr->pRS_SendPtr = pBuf;
RS232_Arr->RS_SendBlockMode = BM_CHAR32;
// RS_Wait4OK(RS232_Arr->commnumber); /* Äîæèäàåìñß óõîäà */
RS_SetBitMode(RS232_Arr,8); /* Îñòàëüíûå â 8-áèò ðåæèìå */
RS232_Arr->RS_SendLen = 0; /* Äâà áàéòà óæå ïåðåäàëè */
// if(len > 1)
// {
EnableUART_Int_TX(RS232_Arr->commnumber); /* Ðàçðåøàåì ïðåðûâàíèß ïî ïåðåäà÷å */
// SCI_Send(RS232_Arr->commnumber, *pBuf); // Ïåðåäàåì âòîðîé áàéò ïî ïðåðûâàíèþ
// }
// else
// {
// SCI_Send(RS232_Arr->commnumber, *pBuf); // Ïåðåäàåì âòîðîé áàéò ïî ïðåðûâàíèþ
// RS_Wait4OK(RS232_Arr->commnumber); /* Äîæèäàåìñß óõîäà áåç ïðåðûâàíèß */
// for (i=0; i <= TIME_WAIT_RS232_BYTE_OUT; i++){} /* Ïàóçà äëß PC */
// RS_SetBitMode(RS232_Arr,9); /* Îáðàòíî â 9-áèò ðåæèì */
// RS_LineToReceive(RS232_Arr->commnumber); /* ðåæèì ïðèåìà RS485 */
// }
return 0;
}
void RS232_TuneUp(unsigned long speed_baud_a, unsigned long speed_baud_b)
{
#if (USE_TEST_TERMINAL)
create_uart_vars(sizeof(CMD_TO_TMS_STRUCT), sizeof(CMD_TO_TMS_TEST_ALL_STRUCT));
#else
create_uart_vars(100, 100);
#endif
SetupUART(COM_1, speed_baud_a);
SetupUART(COM_2, speed_baud_b);
}
void RS232_WorkingWith(unsigned int enable_rs_a, unsigned int enable_rs_b, unsigned int enable_int_timeout)
{
if (enable_int_timeout)
inc_RS_timeout_cicle();
if (enable_rs_a)
{
resetup_rs_on_timeout_lost(COM_1);
switch (GetCommand(&rs_a))
{
case CMD_RS232_INIT: break;
case CMD_RS232_INITLOAD:
if (disable_flag_special_mode_rs)
break;
flag_special_mode_rs = 1;
InitLoad(&rs_a);
break;
case CMD_RS232_LOAD:
if (disable_flag_special_mode_rs)
break;
flag_special_mode_rs = 1;
Load(&rs_a);
break;
case CMD_RS232_RUN: break;
case CMD_RS232_PEEK:
if (disable_flag_special_mode_rs)
break;
flag_special_mode_rs = 1;
Peek(&rs_a);
//Led1_Toggle();
break;
//#if USE_MODBUS_TABLE_SVU
// case CMD_RS232_MODBUS_3:
// if (rs_a.flag_LEADING)
// ReceiveAnswerCommandModbus3(&rs_a);
// else
// ReceiveCommandModbus3(&rs_a);
// break;
// case CMD_RS232_MODBUS_16:
// if (rs_a.flag_LEADING)
// ReceiveAnswerCommandModbus16(&rs_a);
// else
// ReceiveCommandModbus16(&rs_a);
// break;
//#endif
#if (USE_TEST_TERMINAL)
case CMD_RS232_STD:
rs_a.RS_DataReadyRequest = 1;
flag_special_mode_rs = 0;
ReceiveCommand(&rs_a);
break;
case CMD_RS232_TEST_ALL:
if (disable_flag_special_mode_rs)
break;
flag_special_mode_rs = 1;
ReceiveCommandTestAll(&rs_a);
break;
#endif
case CMD_RS232_POKE:
if (disable_flag_special_mode_rs)
break;
flag_special_mode_rs = 1;
Poke(&rs_a);
Led2_Toggle();
break;
case CMD_RS232_UPLOAD:
// flag_special_mode_rs = 1;
Upload(&rs_a);
break;
case CMD_RS232_TFLASH:
if (disable_flag_special_mode_rs)
break;
flag_special_mode_rs = 1;
T_Flash(&rs_a);
break;
case CMD_RS232_EXTEND:
if (disable_flag_special_mode_rs)
break;
flag_special_mode_rs = 1;
ExtendBios(&rs_a);
break;
default: break;
} // end switch
} // end if (enable_rs_a)
if (enable_rs_b)
{
resetup_rs_on_timeout_lost(COM_2);
switch (GetCommand(&rs_b))
{
case CMD_RS232_INIT: break;
case CMD_RS232_INITLOAD:
if (disable_flag_special_mode_rs)
break;
flag_special_mode_rs = 1;
InitLoad(&rs_b);
break;
case CMD_RS232_LOAD:
if (disable_flag_special_mode_rs)
break;
flag_special_mode_rs = 1;
Load(&rs_b);
break;
case CMD_RS232_RUN: break;
case CMD_RS232_PEEK:
if (disable_flag_special_mode_rs)
break;
flag_special_mode_rs = 1;
Peek(&rs_b);
break;
#if USE_MODBUS_TABLE_SVU
case CMD_RS232_MODBUS_1:
if (rs_b.flag_LEADING)
{
ModbusRTUreceiveAnswer1(&rs_b);
rs_b.RS_DataReadyAnswerAnalyze = 1;
rs_b.RS_DataReadyRequest = 1;
}
break;
case CMD_RS232_MODBUS_3:
if (rs_b.flag_LEADING)
{
ModbusRTUreceiveAnswer3(&rs_b);
rs_b.RS_DataReadyAnswerAnalyze = 1;
rs_b.RS_DataReadyRequest = 1;
}
else
ModbusRTUreceive3(&rs_b);
break;
case CMD_RS232_MODBUS_4:
if (rs_b.flag_LEADING)
{
ModbusRTUreceiveAnswer4(&rs_b);
rs_b.RS_DataReadyAnswerAnalyze = 1;
rs_b.RS_DataReadyRequest = 1;
}
else
ModbusRTUreceive4(&rs_b);
break;
case CMD_RS232_MODBUS_5:
if (rs_b.flag_LEADING)
{
ModbusRTUreceiveAnswer5(&rs_b);
rs_b.RS_DataReadyAnswerAnalyze = 1;
rs_b.RS_DataReadyRequest = 1;
}
break;
case CMD_RS232_MODBUS_6:
if (rs_b.flag_LEADING)
{
ModbusRTUreceiveAnswer6(&rs_b);
rs_b.RS_DataReadyAnswerAnalyze = 1;
rs_b.RS_DataReadyRequest = 1;
}
break;
case CMD_RS232_MODBUS_15:
if (rs_b.flag_LEADING)
{
//#if (USE_CONTROL_STATION==1)
// control_station.count_error_modbus_15[CONTROL_STATION_INGETEAM_PULT_RS485]--;
//#endif
ModbusRTUreceiveAnswer15(&rs_b);
rs_b.RS_DataReadyAnswerAnalyze = 1;
rs_b.RS_DataReadyRequest = 1;
}
else
ModbusRTUreceive15(&rs_b);
break;
case CMD_RS232_MODBUS_16:
if (rs_b.flag_LEADING)
{
//#if (USE_CONTROL_STATION==1)
// control_station.count_error_modbus_16[CONTROL_STATION_INGETEAM_PULT_RS485]--;
//#endif
ModbusRTUreceiveAnswer16(&rs_b);
rs_b.RS_DataReadyAnswerAnalyze = 1;
rs_b.RS_DataReadyRequest = 1;
}
else
ModbusRTUreceive16(&rs_b);
break;
#endif
#if (USE_TEST_TERMINAL)
case CMD_RS232_STD: flag_special_mode_rs = 0;
ReceiveCommand(&rs_b);
break;
case CMD_RS232_TEST_ALL:
if (disable_flag_special_mode_rs)
break;
flag_special_mode_rs = 1;
ReceiveCommandTestAll(&rs_b);
break;
#endif
case CMD_RS232_POKE:
if (disable_flag_special_mode_rs)
break;
flag_special_mode_rs = 1;
Poke(&rs_b);
break;
case CMD_RS232_UPLOAD:
// flag_special_mode_rs = 1;
Upload(&rs_b);
break;
case CMD_RS232_TFLASH:
if (disable_flag_special_mode_rs)
break;
flag_special_mode_rs = 1;
T_Flash(&rs_b);
break;
case CMD_RS232_EXTEND:
if (disable_flag_special_mode_rs)
break;
flag_special_mode_rs = 1;
ExtendBios(&rs_b);
break;
default: break;
} // end switch
} // end if (enable_rs_b)
}