Files
templates/c/set-protocol/src/set_emu_igbt.c
T

149 lines
6.3 KiB
C

/*
* Модель каналов IGBT для стенда: входные состояния, задержки и сформированные выходы. Логика
* времени отделена от аппаратного порта; модель предназначена для воспроизводимого обмена с
* эмулятором.
*/
#include "set_emu_igbt.h"
#include <string.h>
static void output(set_igbt_emu *s)
{
unsigned ch;s->rx=0;
if(!s->enabled)return;
for(ch=0;ch<SET_IGBT_CHANNELS;++ch) {
set_igbt_channel *c=&s->channel[ch];uint8_t bit=(uint8_t)(1u<<ch),level;
if(!(c->flags&2))continue;
if(c->mode==1)level=0;
else if(c->mode==2)level=1;
else {level=(uint8_t)(c->mode==3?0:!!(s->logical_rx&bit));if(c->flags&4)level^=1;}
if(level)s->rx|=bit;
}
}
void set_igbt_init(set_igbt_emu *s)
{
unsigned ch;memset(s,0,sizeof(*s));s->enabled=1;s->delay_ms=1;s->width_ms=1;
for(ch=0;ch<SET_IGBT_CHANNELS;++ch) {
set_igbt_channel *c=&s->channel[ch];
c->flags=3;c->on_delay=c->off_delay=1;c->on_width=c->off_width=1;
c->tx_width=10;c->edges=3;
}
}
void set_igbt_tick(set_igbt_emu *s,uint8_t pins)
{
unsigned ch;uint8_t changed;
for(ch=0;ch<SET_IGBT_CHANNELS;++ch)
if(s->pulse[ch]&&--s->pulse[ch]==0)s->injected_tx&=(uint8_t)~(1u<<ch);
s->tx=s->input_source?s->injected_tx:pins;
for(ch=0;ch<SET_IGBT_CHANNELS;++ch)
if(!(s->channel[ch].flags&1))s->tx&=(uint8_t)~(1u<<ch);
changed=(uint8_t)(s->tx^s->previous_tx);s->previous_tx=s->tx;
if(!s->enabled){output(s);return;}
for(ch=0;ch<SET_IGBT_CHANNELS;++ch) {
set_igbt_channel *c=&s->channel[ch];uint8_t bit=(uint8_t)(1u<<ch);
unsigned on=!!(s->tx&bit);
if(s->active[ch]&&(!(s->fault&bit)||c->fault_width)&&--s->active[ch]==0)
s->logical_rx&=(uint8_t)~bit;
if(s->pending[ch]&&--s->pending[ch]==0) {
s->logical_rx|=bit;s->active[ch]=s->response_width[ch];
}
if(changed&bit) {
++s->event_count;++c->count;
if(!(c->flags&1)||!(c->flags&2)||c->mode||!(c->edges&(on?1u:2u)))continue;
s->pending[ch]=on?c->on_delay:c->off_delay;
s->response_width[ch]=(s->fault&bit)&&c->fault_width?c->fault_width:(on?c->on_width:c->off_width);
if(!s->pending[ch]){s->logical_rx|=bit;s->active[ch]=s->response_width[ch];}
}
}
output(s);
}
unsigned set_igbt_read(void *ctx,uint16_t off,uint16_t count,uint16_t *out)
{
set_igbt_emu *s=(set_igbt_emu *)ctx;
uint16_t w[SET_IGBT_WORDS]={0};unsigned i,ch;
if(!s||!out||!count||(uint32_t)off+count>SET_IGBT_WORDS)return 2;
w[0]=0x4947;w[1]=3;w[2]=s->enabled;w[3]=s->tx;w[4]=s->rx;
w[6]=s->fault;w[7]=s->delay_ms;w[8]=s->width_ms;w[9]=s->event_count;
w[10]=8;w[11]=1;w[12]=s->input_source;w[13]=s->injected_tx;
w[15]=SET_IGBT_CHANNEL_WORDS;
for(ch=0;ch<SET_IGBT_CHANNELS;++ch) {
set_igbt_channel *c=&s->channel[ch];unsigned b=16+ch*SET_IGBT_CHANNEL_WORDS;
w[b]=c->flags;w[b+1]=c->mode;w[b+2]=c->on_delay;w[b+3]=c->on_width;
w[b+4]=c->off_delay;w[b+5]=c->off_width;w[b+6]=c->tx_width;
w[b+7]=c->edges;w[b+8]=c->fault_width;w[b+9]=c->count;
w[b+10]=s->pending[ch];w[b+11]=s->active[ch];
}
for(i=0;i<count;++i)out[i]=w[off+i];
return 0;
}
unsigned set_igbt_write(void *ctx,uint16_t off,uint16_t value)
{
set_igbt_emu *s=(set_igbt_emu *)ctx;unsigned ch;uint8_t released;
if(!s)return 3;
if(off>=16) {
unsigned field=(off-16)%SET_IGBT_CHANNEL_WORDS;
set_igbt_channel *c;
ch=(off-16)/SET_IGBT_CHANNEL_WORDS;if(ch>=8)return 2;c=&s->channel[ch];
switch(field) {
case 0:if(value>7)return 3;c->flags=value;break;
case 1:if(value>3)return 3;c->mode=value;break;
case 2:if(value>1000)return 3;c->on_delay=value;break;
case 3:if(!value||value>1000)return 3;c->on_width=value;break;
case 4:if(value>1000)return 3;c->off_delay=value;break;
case 5:if(!value||value>1000)return 3;c->off_width=value;break;
case 6:if(!value||value>60000)return 3;c->tx_width=value;break;
case 7:if(value>3)return 3;c->edges=value;break;
case 8:if(value>60000)return 3;c->fault_width=value;break;
default:return 2;
}
/* Mode/enable changes discard pending responses. */
if(field<=1) {s->pending[ch]=s->active[ch]=0;s->logical_rx&=(uint8_t)~(1u<<ch);}
output(s);return 0;
}
switch(off) {
case 2:
if(value>1)return 3;
s->enabled=(uint8_t)value;
if(!value) {s->logical_rx=0;
for(ch=0;ch<8;++ch)if(s->pulse[ch])s->injected_tx&=(uint8_t)~(1u<<ch);
memset(s->pending,0,sizeof(s->pending));
memset(s->active,0,sizeof(s->active));memset(s->pulse,0,sizeof(s->pulse));}
break;
case 3:case 13:
if(value>255)return 3;
s->injected_tx=(uint8_t)value;memset(s->pulse,0,sizeof(s->pulse));break;
case 6:
if(value>255)return 3;
released=(uint8_t)(s->fault&~value);s->fault=(uint8_t)value;
s->logical_rx&=(uint8_t)~released;
for(ch=0;ch<8;++ch)if(released&(1u<<ch))s->active[ch]=0;
break;
case 7:
if(value>1000)return 3;
s->delay_ms=value;
for(ch=0;ch<8;++ch)s->channel[ch].on_delay=s->channel[ch].off_delay=value;
break;
case 8:
if(!value||value>255)return 3;
s->width_ms=value;
for(ch=0;ch<8;++ch)s->channel[ch].on_width=s->channel[ch].off_width=value;
break;
case 9:
if(value)return 3;
s->event_count=0;for(ch=0;ch<8;++ch)s->channel[ch].count=0;break;
case 12:
if(value>1)return 3;
s->input_source=(uint8_t)value;memset(s->pulse,0,sizeof(s->pulse));break;
case 14:
if(value>255)return 3;
if(!s->enabled||!s->input_source)return 6;
/* A pulse must start from low; retrigger without an edge is refused. */
if(value&(s->injected_tx|s->tx))return 6;
for(ch=0;ch<8;++ch)if((value&(1u<<ch))&&!(s->channel[ch].flags&1))return 6;
s->injected_tx|=(uint8_t)value;
for(ch=0;ch<8;++ch)if(value&(1u<<ch))s->pulse[ch]=(uint16_t)(s->channel[ch].tx_width+1u);
break;
default:return 2;
}
output(s);return 0;
}