Browse Source

add the led of send and receive

master
zhaijunyuan 6 years ago
parent
commit
69e1eb4e8f
  1. 12
      lib/config.h
  2. 33
      lib/timer.c
  3. 14
      lib/timer.h
  4. 138
      targets/vocoder/main.c
  5. 3
      targets/vocoder/target.h

12
lib/config.h

@ -66,6 +66,18 @@ extern "C" {
#define TARGET_SPI_FLASH_PAGE_SIZE (4096)
#endif
#ifndef TARGET_HAS_TIM1
#define TARGET_HAS_TIM1 0
#endif
#ifndef TARGET_HAS_TIM2
#define TARGET_HAS_TIM2 0
#endif
#ifndef TARGET_HAS_TIM3
#define TARGET_HAS_TIM3 0
#endif
#ifdef __cplusplus
}
#endif

33
lib/timer.c

@ -51,6 +51,18 @@ int timer_setup(struct timer *tm, unsigned int ms, int (*irq)(struct timer *, vo
return 0;
}
void timer_set_period(struct timer *tm, unsigned int ms)
{
TIM_TimeBaseInitTypeDef tim_conf;
TIM_TimeBaseStructInit(&tim_conf);
tim_conf.TIM_Period = ms * 10 - 1; /* period */
tim_conf.TIM_Prescaler = SystemCoreClock / (2 * 10000) - 1;
tim_conf.TIM_ClockDivision = TIM_CKD_DIV1;
tim_conf.TIM_CounterMode = TIM_CounterMode_Up;
tim_conf.TIM_RepetitionCounter = 1;
TIM_TimeBaseInit(tm->tim, &tim_conf);
}
void timer_start(struct timer *tm)
{
TIM_SetCounter(tm->tim, 0);
@ -107,6 +119,27 @@ void TIM1_UP_TIM10_IRQHandler()
}
#endif
#if (TARGET_HAS_TIM2)
static void tim2_setup(struct timer *tm)
{
RCC_APB1PeriphClockCmd(tm->rcc, ENABLE);
}
struct timer timer1 = {
.name = "TIM2",
.tim = TIM2,
.setup = tim2_setup,
.rcc = RCC_APB1Periph_TIM2,
.irq = TIM2_IRQn,
.irq_priority = -1,
};
void TIM2_IRQHandler()
{
tim_isr(&timer1);
}
#endif
#if (TARGET_HAS_TIM3)
static void tim3_setup(struct timer *tm)
{

14
lib/timer.h

@ -3,20 +3,14 @@
#include "config.h"
#ifndef TARGET_HAS_TIM1
#define TARGET_HAS_TIM1 0
#endif
#ifndef TARGET_HAS_TIM3
#define TARGET_HAS_TIM3 0
#endif
struct timer;
int timer_setup(struct timer *tm, unsigned int ms, int (*irq)(struct timer *, void *user), void *user);
void timer_start(struct timer *tm);
void timer_set_period(struct timer *tm, unsigned int ms);
void timer_stop(struct timer *tm);
void timer_close(struct timer *tm);
@ -25,6 +19,10 @@ void timer_close(struct timer *tm);
extern struct timer timer0;
#endif
#if (TARGET_HAS_TIM2)
extern struct timer timer1;
#endif
#if (TARGET_HAS_TIM3)
extern struct timer timer2;
#endif

138
targets/vocoder/main.c

@ -36,6 +36,9 @@
#define RS422_1 (&serial5)
#define TIM_40MS (&timer2)
#define LED1_TIMER (&timer0)
#define LED2_TIMER (&timer1)
struct tag_fifo {
char *pnew; /* points to newest 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.
*/
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;
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:
break;
case RS422_PKT_TYPE_CMD: /* reset command */
return wait_to_reset(from);
wait_to_reset(from);
return 0;
default:
__drop_bytes(&f->f, RS422_PKT_LEN - 2);
return;
return 0;
}
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 */
gpio_toggle(led);
return 1;
}
return 0;
}
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);
}
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 last = 0;
static int last_a3k0 = 0, last_a3k1 = 0;
int c;
to_send = (BIT0 | BIT1);
if ((c = gpio_read(PF6)) != last) {
last = c;
if (!last) {
plugged = 1;
if ((c = gpio_read(PF6)) != last_a3k0) {
last_a3k0 = c;
if (!last_a3k0) {
plugged_a3k0 = 1;
}
}
if ((c = gpio_read(PF7)) != last_a3k1) {
last_a3k1 = c;
if (!last_a3k1) {
plugged_a3k1 = 1;
}
}
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.
*/
@ -420,6 +495,7 @@ int main()
gpio_write(LED_D3, 1);
gpio_init(PF6, GPIO_INPUT);
gpio_init(PF7, GPIO_INPUT);
init_frames(&rx[0], __rxbuf[0], 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_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);
printf("running\r\n...............................\r\n> ");
while (1) {
cmd_loop();
if (plugged) {
plugged = 0;
steady = millis() + 300;
// a3k_reset(&a3k0, -1);
// a3k_reset(&a3k1, -1);
if (plugged_a3k0) {
plugged_a3k0 = 0;
steady_a3k0 = millis() + 300;
}
if (steady > 0) {
if (millis() > steady) {
if (steady_a3k0 > 0) {
if (millis() > steady_a3k0) {
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);
steady = 0;
steady_a3k1 = 0;
printf("plugged\r\n");
}
}
encode_packet(&a3k0, &tx[0]);
encode_packet(&a3k1, &tx[1]);
@ -472,17 +562,21 @@ int main()
if (to_send & BIT0) {
if (send_packet(RS422_0, &tx[0], LED_D4)) { /* D4: UART4 TX */
to_send &= ~BIT0;
last_send0 = millis();
}
}
if (to_send & BIT1) {
if (send_packet(RS422_1, &tx[1], LED_D6)) { /* D6: USART6 TX */
to_send &= ~BIT1;
last_send1 = millis();
}
}
recv_packet(RS422_0, &rx[0], LED_D3); /* D3: UART4 rx */
recv_packet(RS422_1, &rx[1], LED_D5); /* D5: USART6 rx */
if(recv_packet(RS422_0, &rx[0], Y1_OUT)) /* D3: UART4 rx */
last_recv0 = millis();
if(recv_packet(RS422_1, &rx[1], Y2_OUT)) /* D5: USART6 rx */
last_recv1 = millis();
if (to_decode & BIT0) {
if (decode_packet(&a3k0, &rx[0], Y1_OUT)) {
@ -495,6 +589,8 @@ int main()
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();
}
return 0;

3
targets/vocoder/target.h

@ -54,7 +54,8 @@
#define TARGET_HAS_A3K_0 1
#define TARGET_HAS_A3K_1 1
#define TARGET_HAS_TIM1 0
#define TARGET_HAS_TIM1 1
#define TARGET_HAS_TIM2 1
#define TARGET_HAS_TIM3 1
#define TARGET_HAS_I2C1 1

Loading…
Cancel
Save