|
@ -36,6 +36,9 @@ |
|
|
#define RS422_1 (&serial5) |
|
|
#define RS422_1 (&serial5) |
|
|
#define TIM_40MS (&timer2) |
|
|
#define TIM_40MS (&timer2) |
|
|
|
|
|
|
|
|
|
|
|
#define LED1_TIMER (&timer0) |
|
|
|
|
|
#define LED2_TIMER (&timer1) |
|
|
|
|
|
|
|
|
struct tag_fifo { |
|
|
struct tag_fifo { |
|
|
char *pnew; /* points to newest fifo element */ |
|
|
char *pnew; /* points to newest fifo element */ |
|
|
char *pold; /* points to oldest fifo element */ |
|
|
char *pold; /* points to oldest fifo element */ |
|
@ -301,7 +304,7 @@ static void __get_packet(struct ringbuf *ring, struct tag_fifo *f) |
|
|
/*
|
|
|
/*
|
|
|
* recv one packet from RS422, split two channel packets. |
|
|
* recv one packet from RS422, split two channel packets. |
|
|
*/ |
|
|
*/ |
|
|
static void recv_packet(struct serial *from, struct frames *f, uint32_t led) |
|
|
static int recv_packet(struct serial *from, struct frames *f, uint32_t led) |
|
|
{ |
|
|
{ |
|
|
uint8_t c; |
|
|
uint8_t c; |
|
|
struct ringbuf *rb0, *rb1; |
|
|
struct ringbuf *rb0, *rb1; |
|
@ -313,10 +316,11 @@ static void recv_packet(struct serial *from, struct frames *f, uint32_t led) |
|
|
case RS422_PKT_TYPE_DATA: |
|
|
case RS422_PKT_TYPE_DATA: |
|
|
break; |
|
|
break; |
|
|
case RS422_PKT_TYPE_CMD: /* reset command */ |
|
|
case RS422_PKT_TYPE_CMD: /* reset command */ |
|
|
return wait_to_reset(from); |
|
|
wait_to_reset(from); |
|
|
|
|
|
return 0; |
|
|
default: |
|
|
default: |
|
|
__drop_bytes(&f->f, RS422_PKT_LEN - 2); |
|
|
__drop_bytes(&f->f, RS422_PKT_LEN - 2); |
|
|
return; |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
rb0 = &f->slices[f->tail & (PKT_NLEMS - 1)]; |
|
|
rb0 = &f->slices[f->tail & (PKT_NLEMS - 1)]; |
|
@ -330,7 +334,9 @@ static void recv_packet(struct serial *from, struct frames *f, uint32_t led) |
|
|
__fifo_getc(&f->f); /* drop parity */ |
|
|
__fifo_getc(&f->f); /* drop parity */ |
|
|
|
|
|
|
|
|
gpio_toggle(led); |
|
|
gpio_toggle(led); |
|
|
|
|
|
return 1; |
|
|
} |
|
|
} |
|
|
|
|
|
return 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static int decode_packet(a3k_t to, struct frames *f, uint32_t led) |
|
|
static int decode_packet(a3k_t to, struct frames *f, uint32_t led) |
|
@ -357,28 +363,97 @@ static void __on_20ms(int ms) |
|
|
to_decode = (BIT0 | BIT1); |
|
|
to_decode = (BIT0 | BIT1); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static volatile int plugged = 0; |
|
|
static volatile int plugged_a3k0 = 0, plugged_a3k1 = 0; |
|
|
|
|
|
|
|
|
static int __on_40ms(struct timer *tm, void *user) |
|
|
static int __on_40ms(struct timer *tm, void *user) |
|
|
{ |
|
|
{ |
|
|
static int last = 0; |
|
|
static int last_a3k0 = 0, last_a3k1 = 0; |
|
|
int c; |
|
|
int c; |
|
|
|
|
|
|
|
|
to_send = (BIT0 | BIT1); |
|
|
to_send = (BIT0 | BIT1); |
|
|
|
|
|
|
|
|
if ((c = gpio_read(PF6)) != last) { |
|
|
if ((c = gpio_read(PF6)) != last_a3k0) { |
|
|
last = c; |
|
|
last_a3k0 = c; |
|
|
if (!last) { |
|
|
if (!last_a3k0) { |
|
|
plugged = 1; |
|
|
plugged_a3k0 = 1; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ((c = gpio_read(PF7)) != last_a3k1) { |
|
|
|
|
|
last_a3k1 = c; |
|
|
|
|
|
if (!last_a3k1) { |
|
|
|
|
|
plugged_a3k1 = 1; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
return 0; |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
int main() |
|
|
static int flashing(struct timer *tm, void *user) |
|
|
|
|
|
{ |
|
|
|
|
|
uint32_t led = (uint32_t)user; |
|
|
|
|
|
gpio_toggle(led); |
|
|
|
|
|
// printf("led = %x, LED3 = %x, LED5 = %x, time = %d\n",led,LED_D3,LED_D5,millis());
|
|
|
|
|
|
return 0; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int led_state(uint64_t time1, uint64_t time2) |
|
|
{ |
|
|
{ |
|
|
uint64_t steady = 0; |
|
|
uint64_t now = 0; |
|
|
|
|
|
now = millis(); |
|
|
|
|
|
|
|
|
|
|
|
if(((now - time1) < 100) && ((now - time2) < 100)) |
|
|
|
|
|
return 0; |
|
|
|
|
|
if(((now - time1) < 100) && ((now - time2) > 100)) |
|
|
|
|
|
return 1; |
|
|
|
|
|
if(((now - time1) > 100) && ((now - time2) < 100)) |
|
|
|
|
|
return 2; |
|
|
|
|
|
if(((now - time1) > 100) && ((now - time2) > 100)) |
|
|
|
|
|
return 3; |
|
|
|
|
|
return -1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int led_set(struct timer *tm, uint64_t time1, uint64_t time2, uint32_t led, int last_state) |
|
|
|
|
|
{ |
|
|
|
|
|
int now_state; |
|
|
|
|
|
now_state = led_state(time1, time2); |
|
|
|
|
|
if((now_state == 0) && (now_state != last_state)) { |
|
|
|
|
|
last_state = now_state; |
|
|
|
|
|
timer_stop(tm); |
|
|
|
|
|
gpio_clear(led); |
|
|
|
|
|
return last_state; |
|
|
|
|
|
} |
|
|
|
|
|
if((now_state == 1) && (now_state != last_state)) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
last_state = now_state; |
|
|
|
|
|
timer_stop(tm); |
|
|
|
|
|
timer_set_period(tm, 100); |
|
|
|
|
|
timer_start(tm); |
|
|
|
|
|
return last_state; |
|
|
|
|
|
} |
|
|
|
|
|
if((now_state == 2) && (now_state != last_state)) { |
|
|
|
|
|
last_state = now_state; |
|
|
|
|
|
timer_stop(tm); |
|
|
|
|
|
timer_set_period(tm, 800); |
|
|
|
|
|
timer_start(tm); |
|
|
|
|
|
return last_state; |
|
|
|
|
|
} |
|
|
|
|
|
if((now_state == 3) && (now_state != last_state)) { |
|
|
|
|
|
last_state = now_state; |
|
|
|
|
|
timer_stop(tm); |
|
|
|
|
|
gpio_clear(led); |
|
|
|
|
|
return last_state; |
|
|
|
|
|
} |
|
|
|
|
|
return last_state; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int main() |
|
|
|
|
|
{ |
|
|
|
|
|
uint64_t steady_a3k0 = 0, steady_a3k1 = 0; |
|
|
|
|
|
uint64_t last_send0 = 0, last_send1 = 0; |
|
|
|
|
|
uint64_t last_recv0 = 0, last_recv1 = 0; |
|
|
|
|
|
int last_state1 = -1, last_state2 = -1; |
|
|
/*
|
|
|
/*
|
|
|
* recovery from bad image. |
|
|
* recovery from bad image. |
|
|
*/ |
|
|
*/ |
|
@ -420,6 +495,7 @@ int main() |
|
|
gpio_write(LED_D3, 1); |
|
|
gpio_write(LED_D3, 1); |
|
|
|
|
|
|
|
|
gpio_init(PF6, GPIO_INPUT); |
|
|
gpio_init(PF6, GPIO_INPUT); |
|
|
|
|
|
gpio_init(PF7, GPIO_INPUT); |
|
|
|
|
|
|
|
|
init_frames(&rx[0], __rxbuf[0], PKT_LEN); |
|
|
init_frames(&rx[0], __rxbuf[0], PKT_LEN); |
|
|
init_frames(&rx[1], __rxbuf[1], PKT_LEN); |
|
|
init_frames(&rx[1], __rxbuf[1], PKT_LEN); |
|
@ -442,28 +518,42 @@ int main() |
|
|
timer_setup(TIM_40MS, 40, __on_40ms, NULL); |
|
|
timer_setup(TIM_40MS, 40, __on_40ms, NULL); |
|
|
timer_start(TIM_40MS); |
|
|
timer_start(TIM_40MS); |
|
|
|
|
|
|
|
|
|
|
|
timer_setup(LED1_TIMER, 100, flashing, (void *)LED_D3); |
|
|
|
|
|
timer_setup(LED2_TIMER, 400, flashing, (void *)LED_D5); |
|
|
|
|
|
|
|
|
bkp_sram_write(MAGIC_POS, MAGIC_APP); |
|
|
bkp_sram_write(MAGIC_POS, MAGIC_APP); |
|
|
|
|
|
|
|
|
printf("running\r\n...............................\r\n> "); |
|
|
printf("running\r\n...............................\r\n> "); |
|
|
while (1) { |
|
|
while (1) { |
|
|
cmd_loop(); |
|
|
cmd_loop(); |
|
|
|
|
|
|
|
|
if (plugged) { |
|
|
if (plugged_a3k0) { |
|
|
plugged = 0; |
|
|
plugged_a3k0 = 0; |
|
|
steady = millis() + 300; |
|
|
steady_a3k0 = millis() + 300; |
|
|
// a3k_reset(&a3k0, -1);
|
|
|
|
|
|
// a3k_reset(&a3k1, -1);
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (steady > 0) { |
|
|
if (steady_a3k0 > 0) { |
|
|
if (millis() > steady) { |
|
|
if (millis() > steady_a3k0) { |
|
|
a3k_setup(&a3k0, -1); |
|
|
a3k_setup(&a3k0, -1); |
|
|
|
|
|
steady_a3k0 = 0; |
|
|
|
|
|
printf("plugged\r\n"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (plugged_a3k1) { |
|
|
|
|
|
plugged_a3k1 = 0; |
|
|
|
|
|
steady_a3k1 = millis() + 300; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (steady_a3k1 > 0) { |
|
|
|
|
|
if (millis() > steady_a3k1) { |
|
|
a3k_setup(&a3k1, -1); |
|
|
a3k_setup(&a3k1, -1); |
|
|
steady = 0; |
|
|
steady_a3k1 = 0; |
|
|
printf("plugged\r\n"); |
|
|
printf("plugged\r\n"); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
encode_packet(&a3k0, &tx[0]); |
|
|
encode_packet(&a3k0, &tx[0]); |
|
|
encode_packet(&a3k1, &tx[1]); |
|
|
encode_packet(&a3k1, &tx[1]); |
|
|
|
|
|
|
|
@ -472,17 +562,21 @@ int main() |
|
|
if (to_send & BIT0) { |
|
|
if (to_send & BIT0) { |
|
|
if (send_packet(RS422_0, &tx[0], LED_D4)) { /* D4: UART4 TX */ |
|
|
if (send_packet(RS422_0, &tx[0], LED_D4)) { /* D4: UART4 TX */ |
|
|
to_send &= ~BIT0; |
|
|
to_send &= ~BIT0; |
|
|
|
|
|
last_send0 = millis(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (to_send & BIT1) { |
|
|
if (to_send & BIT1) { |
|
|
if (send_packet(RS422_1, &tx[1], LED_D6)) { /* D6: USART6 TX */ |
|
|
if (send_packet(RS422_1, &tx[1], LED_D6)) { /* D6: USART6 TX */ |
|
|
to_send &= ~BIT1; |
|
|
to_send &= ~BIT1; |
|
|
|
|
|
last_send1 = millis(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
recv_packet(RS422_0, &rx[0], LED_D3); /* D3: UART4 rx */ |
|
|
if(recv_packet(RS422_0, &rx[0], Y1_OUT)) /* D3: UART4 rx */ |
|
|
recv_packet(RS422_1, &rx[1], LED_D5); /* D5: USART6 rx */ |
|
|
last_recv0 = millis(); |
|
|
|
|
|
if(recv_packet(RS422_1, &rx[1], Y2_OUT)) /* D5: USART6 rx */ |
|
|
|
|
|
last_recv1 = millis(); |
|
|
|
|
|
|
|
|
if (to_decode & BIT0) { |
|
|
if (to_decode & BIT0) { |
|
|
if (decode_packet(&a3k0, &rx[0], Y1_OUT)) { |
|
|
if (decode_packet(&a3k0, &rx[0], Y1_OUT)) { |
|
@ -495,6 +589,8 @@ int main() |
|
|
to_decode &= ~(BIT1); |
|
|
to_decode &= ~(BIT1); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
last_state1 = led_set(LED1_TIMER,last_send0, last_recv0, LED_D3, last_state1); |
|
|
|
|
|
last_state2 = led_set(LED2_TIMER,last_send1, last_recv1, LED_D5, last_state2); |
|
|
iwdg_reset(); |
|
|
iwdg_reset(); |
|
|
} |
|
|
} |
|
|
return 0; |
|
|
return 0; |
|
|