251 lines
8.0 KiB
C
251 lines
8.0 KiB
C
/* ==================================================================================
|
|
Copyright 2017 ÀÎ "ÍÈÈÝÒ" è ÎÎÎ "ÍÏÔ ÂÅÊÒÎÐ"
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
|
|
//File name: CANBlockTransferInterface.c
|
|
//
|
|
//Originator: Aliamkin Dmitry I
|
|
//
|
|
//Description: âñå âçàèìîäåéñòâèÿ ñ áëî÷íîé ïåðåäà÷åé ïî CAN îñóùåñòâëÿþòñÿ
|
|
â ÄÀ äàííîãî ôàéëà
|
|
//ÏÐÈÌÅ×ÀÍÈÅ:
|
|
//=====================================================================================
|
|
//-------------------------------------------------------------------------------------
|
|
01.09.2016 Release Rev 1.05
|
|
Ñòýéò CAN ïðîøèâàòîðà ÷àñòè÷íî ïåðåíåñåí â ôîí äëÿ ïðàâèëüíîãî ïåðåõîäà â äðóãîé ïðîåêò (àêòóàëüíî äëÿ M4F)
|
|
20.09.2012 Release Rev 1.04
|
|
Äîáàâèë àâòîìàòè÷åñêîå îñâîáîæäåíèå èíåòðïðåòàòîðà ïðè ïðîñòîå â CANBT_INTERFACE_BUSY áîëåå 5 ñåêóíä
|
|
20.09.2012 Release Rev 1.03
|
|
Ïðàâèëüíåå ñäåëàí âûõîä èç ñîñòîÿíèÿ CANBT_INTERFACE_DATALOG
|
|
19.09.2012 Release Rev 1.02
|
|
Äîáàâëåíà ïîääåðæêà ñêà÷êè êàíàëîâ äàòàëîããåðà ÷åðåç áëî÷íóþ ïåðåäà÷ó
|
|
// 26.12.2011 Release Rev 1.01
|
|
//----------------------------------------------------------------------------------*/
|
|
#include "DSP.h"
|
|
#include "main.h"
|
|
#include "co_ODvars.h"
|
|
#include "CANOpen_drv.h"
|
|
#include "CANBlockTransferInterface.h"
|
|
|
|
void SMCanBTInterface_ms(volatile struct SCanBTInterface* p, Uint32 time, TCo_OdVars* co_ptr)
|
|
{
|
|
//îòäåëüíóþ ôóíêöèþ äëÿ èíèöèàëèçàöèè íå äåëàåì, ò.ê. íåèçâåñòíî, ÷òî ïîëüçîâàòåëü ðàíüøå âûçîâåò
|
|
//èíèò CANOpen-à èëè äàííîãî ìîäóëÿ, ÷òî äåëàåò ïîâåäåíèå àâòîìàòà íåïðåäñêàçóåìûì èç-çà òîãî, ÷òî
|
|
//â SPI ìîæåò áûòü ÷òî-òî ñîõðàíåíî (íàïðèìåð ïðîæèã ïî CAN :))
|
|
if(p->alreadyInit == 0)
|
|
{
|
|
co_ptr->co_blockTransferCommand = CANBT_INTERFACE_FREE;
|
|
p->time_prev = time;
|
|
p->alreadyInit = 1;
|
|
}
|
|
//ñ÷èòàåì äèñêðåòíûé àâòîìàò
|
|
p->state_shadow = co_ptr->co_blockTransferCommand; //êîïèðóåì ïåðåìåííóþ ñîñòîÿíèÿ, òàê êàê state ìîæåò áûòü èçìåíåí èçâíå âíåçàïíî
|
|
//äàëåå âñ¸ àíàëèçèðóåì ïî state_shadow
|
|
if (p->state_prev!=p->state_shadow)
|
|
{
|
|
p->E=1;//ôëàã ïåðâîãî âõîæäåíèÿ (E - ENTRY)
|
|
p->state_time = 0;
|
|
}
|
|
else
|
|
p->E=0;
|
|
p->state_prev=p->state_shadow;
|
|
|
|
//âðåìÿ íàõîæäåíèÿ â òåêóùåì ñîñòîÿíèè
|
|
p->state_time += p->time_prev - time;
|
|
p->time_prev = time;
|
|
|
|
switch (p->state_shadow)//â çàâèñèìîñòè îò ñîñòîÿíèÿ:
|
|
{
|
|
case CANBT_INTERFACE_FREE:
|
|
{
|
|
if (p->E)
|
|
{
|
|
//ENTRY
|
|
}
|
|
break;
|
|
}
|
|
case CANBT_INTERFACE_BUSY:
|
|
{
|
|
if (p->E)
|
|
{
|
|
//ENTRY
|
|
}
|
|
//åñëè íàõîäèìñÿ â çàíÿòîì ñîñòîÿíèè ñëèøêîì äîëãî, çíà÷èò êòî òî ïðîñòî çàáûë î÷èñòèòü èíåòðïåðòàòîð
|
|
if(p->state_time > (150000*5000)) //5 ñåêóíä
|
|
co_ptr->co_blockTransferCommand = CANBT_INTERFACE_FREE;
|
|
break;
|
|
}
|
|
case CANBT_INTERFACE_FLASH_PROG:
|
|
{//êåéç â ìñ íóæåí ÷òîáû íå ïîïàäàòü â äàííîì ñâè÷å â default:
|
|
if (p->E)
|
|
{
|
|
//ENTRY
|
|
}
|
|
|
|
//îñòàíàâëèâàåì óïðàâëåíèå êëþ÷àìè
|
|
FP_BLOCK_PWM;
|
|
//ïîïóòíî íåïðåðûâíî ñáðàñûâàåì sm_ctrl è âûñòàâëÿåì àâàðèþ, ÷òîá óæ íàâåðíÿêà âñå ñòîÿëî
|
|
FP_CTRL_STOP;
|
|
FP_FAULT_SET;
|
|
|
|
//ïåðåõîä â ïðîøèâàëüùèê áóäåò âûïîëíåí â ñëîó êàëêå.
|
|
|
|
break;
|
|
}
|
|
case CANBT_INTERFACE_DATALOG1:
|
|
{
|
|
if (p->E)
|
|
{
|
|
// Êîïèðóåì äàííûå èç ìàññèâà îñöèëëîãðàììû â ìàññèâ äëÿ îòïðàâêè ñî ñìåùåíèåì.
|
|
#if DLOG_DATA_SIZE == 16
|
|
//DINT;
|
|
//Uint32 t0 = CpuTimer2Regs.TIM.all;
|
|
/*
|
|
// 'memcpy' êîïèðóåò 16-áèòíûìè ñëîâàìè, ÷òî ïîçâîëÿåò èñïîëüçîâàòü è íå÷¸òíûå çíà÷åíèÿ
|
|
// 'dlog.first_point_written', íî ýòî ðàáîòàåò ìåäëåííåå, ÷åì âàðèàíò ñ öèêëîì 'for'
|
|
// (ñì. íèæå) ãäå êîïèðóþòñÿ 32-áèòíûå ñëîâà.
|
|
size_t n = 256 - dlog.first_point_written;
|
|
memcpy((void*)&dlog.upload_buff[0],
|
|
(void*)&dlog.buff[dlog.first_point_written + (dlog.buff_num << 8)], n);
|
|
memcpy((void*)&dlog.upload_buff[n],
|
|
(void*)&dlog.buff[dlog.buff_num << 8], dlog.first_point_written);
|
|
//Uint32 t1 = t0 - CpuTimer2Regs.TIM.all;
|
|
*/
|
|
//t0 = CpuTimer2Regs.TIM.all;
|
|
Uint32* CopyPtrFrom = (Uint32*)(&dlog.buff[dlog.first_point_written + (dlog.buff_num << 8)]);
|
|
Uint32* CopyPtrTo = (Uint32*)(&dlog.upload_buff[0]);
|
|
for (register int i = 128 - (dlog.first_point_written >> 1); i > 0; i--)
|
|
*CopyPtrTo++ = *CopyPtrFrom++;
|
|
CopyPtrFrom = (Uint32*)(&dlog.buff[dlog.buff_num << 8]); //óêàçàòåëü ìàññèâà-èñòî÷íèêà ïåðåíîñèì íà íà÷àëî, äëÿ ìàññèâà-ïðèåìíèêà îí è òàê, ãäå íàäî
|
|
for (register int i = dlog.first_point_written >> 1; i > 0; i--)
|
|
*CopyPtrTo++ = *CopyPtrFrom++;
|
|
//Uint32 t2 = t0 - CpuTimer2Regs.TIM.all;
|
|
//EINT;
|
|
#endif
|
|
#if DLOG_DATA_SIZE == 32
|
|
//DINT;
|
|
//Uint32 t0 = CpuTimer2Regs.TIM.all;
|
|
Uint32* CopyPtrFrom = (Uint32*)(&dlog.buff[dlog.first_point_written + (dlog.buff_num << 8)]);
|
|
Uint32* CopyPtrTo = (Uint32*)(&dlog.upload_buff[0]);
|
|
for (register int i = 256 - dlog.first_point_written; i > 0; i--)
|
|
*CopyPtrTo++ = *CopyPtrFrom++;
|
|
CopyPtrFrom = (Uint32*)(&dlog.buff[dlog.buff_num << 8]);
|
|
for (register int i = dlog.first_point_written; i > 0; i--)
|
|
*CopyPtrTo++ = *CopyPtrFrom++;
|
|
//Uint32 t1 = t0 - CpuTimer2Regs.TIM.all;
|
|
//EINT;
|
|
#endif
|
|
CANBlockTransferInitTX(co_ptr, 256, (Uint16*)&dlog.upload_buff[0]);
|
|
}
|
|
|
|
// Åñëè ÄÀ áëî÷íîé ïåðåäà÷è îñâîáîäèëñÿ, îñâîáîæäàåì è èíòåðôåéñ.
|
|
if (co_ptr->BT->tx_status != CANBT_STATUS_BUSY)
|
|
{
|
|
co_ptr->co_blockTransferCommand = CANBT_INTERFACE_FREE;
|
|
}
|
|
break;
|
|
}
|
|
#if DLOG_DATA_SIZE == 32
|
|
case CANBT_INTERFACE_DATALOG2:
|
|
{
|
|
if (p->E)
|
|
{
|
|
// Èíèöèèðóåì ïåðåäà÷ó âòîðîé ïîëîâèíû áóôåðà äàòàëîããåðà.
|
|
CANBlockTransferInitTX(co_ptr, 256, (Uint16*)&dlog.upload_buff[128]);
|
|
}
|
|
// Åñëè ÄÀ áëî÷íîé ïåðåäà÷è îñâîáîäèëñÿ, îñâîáîæäàåì è èíòåðôåéñ.
|
|
if (co_ptr->BT->tx_status != CANBT_STATUS_BUSY)
|
|
{
|
|
co_ptr->co_blockTransferCommand = CANBT_INTERFACE_FREE;
|
|
}
|
|
break;
|
|
}
|
|
#endif
|
|
default:
|
|
co_ptr->co_blockTransferCommand = CANBT_INTERFACE_FREE;
|
|
}
|
|
}
|
|
|
|
void SMCanBTInterface_slow(volatile struct SCanBTInterface* p)
|
|
{
|
|
//ñ÷èòàåì äèñêðåòíûé àâòîìàò
|
|
switch (p->state_shadow)//â çàâèñèìîñòè îò ñîñòîßíèß:
|
|
{
|
|
case CANBT_INTERFACE_FLASH_PROG:
|
|
{
|
|
if (p->E)
|
|
{
|
|
//ENTRY
|
|
}
|
|
|
|
//îñòàíàâëèâàåì óïðàâëåíèå êëþ÷àìè
|
|
FP_BLOCK_PWM;
|
|
//ïîïóòíî íåïðåðûâíî ñáðàñûâàåì sm_ctrl è âûñòàâëßåì àâàðèþ, ÷òîá óæ íàâåðíßêà âñå ñòîßëî
|
|
FP_CTRL_STOP;
|
|
FP_FAULT_SET;
|
|
|
|
//æäåì ìàëåíüêî (100ìñ), ÷òîá âñå ÄÀ îäóïëèëèñü è èäåì â ïðîæèã
|
|
if(p->state_time > (150000*100))
|
|
StartFlashProgrammer();
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
//êîëáýê äëÿ áëî÷íîé ïåðåäà÷è îïèñàí òóò, ÷òîáû èçáàâèòü ïîëüçîâàòåëÿ îò íèõ
|
|
void callback_BTcmd(Uint16 par, Uint16 tag_CANnum)
|
|
{
|
|
#ifdef CAN_1_ENABLE
|
|
if(tag_CANnum == 1)
|
|
{
|
|
if(par==0)
|
|
{
|
|
if(co1_vars.co_blockTransferCommand == 0)
|
|
co1_vars.co_blockTransferCommand = 0x8000;
|
|
}
|
|
if(par==1)
|
|
{
|
|
if(co1_vars.co_blockTransferCommand == 1)
|
|
{ //ïî 1-ìó CANó õîòÿò ïðîøèâàòüñÿ, îòðàçèì ýòî â íàñòðîéêàõ ïðîøèâàëüùèêà
|
|
FPsettings.CANa_enable = 1;
|
|
FPsettings.CANb_enable = 0;
|
|
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
|
|
#ifdef CAN_2_ENABLE
|
|
if(tag_CANnum == 2)
|
|
{
|
|
if(par==0)
|
|
{
|
|
if(co2_vars.co_blockTransferCommand == 0)
|
|
co2_vars.co_blockTransferCommand = 0x8000;
|
|
}
|
|
if(par==1)
|
|
{
|
|
if(co2_vars.co_blockTransferCommand == 1)
|
|
{ //ïî 2-ìó CANó õîòÿò ïðîøèâàòüñÿ, îòðàçèì ýòî â íàñòðîéêàõ ïðîøèâàëüùèêà
|
|
FPsettings.CANb_enable = 1;
|
|
FPsettings.CANa_enable = 0;
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
|
|
|