|
|
@ -6,7 +6,9 @@ |
|
|
|
#include "a3kpacket.h" |
|
|
|
#include "gpio.h" |
|
|
|
#include "a3k.h" |
|
|
|
#include "tick.h" |
|
|
|
|
|
|
|
#define WAIT_TIMEOUT (50) /* 500ms */ |
|
|
|
#define RATE_PINS (6) |
|
|
|
|
|
|
|
struct fifo_tag { |
|
|
@ -73,7 +75,7 @@ static void __send_all(a3k_t tag, const uint8_t *p, int len) |
|
|
|
while (len > 0) { |
|
|
|
n = serial_send_buffered(tag->uart, p, len); |
|
|
|
p += n; |
|
|
|
len -= 0; |
|
|
|
len -= n; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -182,13 +184,18 @@ static void put_packet(a3k_t a3k, char *packet, char *end, char type, char parit |
|
|
|
* that the fifo contains at leas one character. If it does not the |
|
|
|
* function returns a null character. |
|
|
|
*/ |
|
|
|
static void __fifo_waitpacket(a3k_t tag) |
|
|
|
static int __fifo_waitpacket(a3k_t tag) |
|
|
|
{ |
|
|
|
int n, len; |
|
|
|
char *old; |
|
|
|
FIFO *f = &tag->rx; |
|
|
|
uint64_t end = millis() + WAIT_TIMEOUT; |
|
|
|
|
|
|
|
restart: |
|
|
|
if (millis() > end) { |
|
|
|
return -1; |
|
|
|
} |
|
|
|
|
|
|
|
/* try to receive from serial */ |
|
|
|
while (f->blen != f->nelem) { |
|
|
|
len = f->bend - f->pnew; |
|
|
@ -228,6 +235,8 @@ restart: |
|
|
|
if (f->nelem < (len + 4)) { |
|
|
|
goto restart; |
|
|
|
} |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
static int get_packet(a3k_t a3k, char *packet) |
|
|
@ -235,7 +244,9 @@ static int get_packet(a3k_t a3k, char *packet) |
|
|
|
#if 0 |
|
|
|
return __getpacket(a3k, packet); |
|
|
|
#else |
|
|
|
__fifo_waitpacket(a3k); |
|
|
|
if (__fifo_waitpacket(a3k)) { |
|
|
|
return 0; |
|
|
|
} |
|
|
|
return __getpacket(a3k, packet); |
|
|
|
#endif |
|
|
|
} |
|
|
@ -243,10 +254,9 @@ static int get_packet(a3k_t a3k, char *packet) |
|
|
|
static int wait_a3k_ready(a3k_t tag) |
|
|
|
{ |
|
|
|
char packet[30]; |
|
|
|
short i; |
|
|
|
for (i = 0;i < 30; i++) |
|
|
|
packet[i] = 0xFF; |
|
|
|
|
|
|
|
|
|
|
|
memset(packet, 0, sizeof packet); |
|
|
|
|
|
|
|
get_packet(tag, packet); |
|
|
|
|
|
|
|
if (packet[0] != 0x61) |
|
|
@ -303,13 +313,12 @@ int get_cfg(a3k_t a3k, short * cfg0, short * cfg1, short * cfg2) |
|
|
|
|
|
|
|
static int packet_reset_a3k(a3k_t a3k) |
|
|
|
{ |
|
|
|
int i; |
|
|
|
uint64_t end = millis() + 5; |
|
|
|
|
|
|
|
gpio_clear(a3k->rst); |
|
|
|
|
|
|
|
for (i = 0; i < 1024; ++i) { |
|
|
|
__asm__ volatile ("nop"); |
|
|
|
} |
|
|
|
while (millis() < end) |
|
|
|
; |
|
|
|
|
|
|
|
serial_rx_buffer_clear(a3k->uart); |
|
|
|
serial_tx_buffer_clear(a3k->uart); |
|
|
@ -839,7 +848,10 @@ start: |
|
|
|
|
|
|
|
int a3k_setup(a3k_t tag, int baudrate) |
|
|
|
{ |
|
|
|
int i; |
|
|
|
int err = 0; |
|
|
|
#ifdef STM32F429_439xx |
|
|
|
int i; |
|
|
|
#endif |
|
|
|
|
|
|
|
__fifo_init(&tag->rx, FORMAT3KBLEN, (void *)tag->_buf); |
|
|
|
|
|
|
@ -853,6 +865,7 @@ int a3k_setup(a3k_t tag, int baudrate) |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
#ifdef STM32F429_439xx |
|
|
|
gpio_init(tag->rst, GPIO_OUTPUT | GPIO_FLAG_PP | GPIO_FLAG_PU); |
|
|
|
gpio_init(tag->rts, GPIO_INPUT | GPIO_FLAG_PP | GPIO_FLAG_PU | GPIO_SPEED_FAST); |
|
|
|
|
|
|
@ -865,10 +878,14 @@ int a3k_setup(a3k_t tag, int baudrate) |
|
|
|
} |
|
|
|
|
|
|
|
pins_set(tag, 0x2c, 0, 1, 0); //ec, ns, es
|
|
|
|
#endif |
|
|
|
|
|
|
|
ticks_init(1); |
|
|
|
|
|
|
|
if (packet_reset_a3k(tag)) { |
|
|
|
printf("reset a3k failed.\r\n"); |
|
|
|
return -1; |
|
|
|
err = -1; |
|
|
|
goto tail; |
|
|
|
} |
|
|
|
/*
|
|
|
|
if (stop_a3k_codec(tag)) { |
|
|
@ -877,22 +894,27 @@ int a3k_setup(a3k_t tag, int baudrate) |
|
|
|
*/ |
|
|
|
if (config_a3k(tag, -1, 0, 0)) { /* ratet, input_gain, output_gain */ |
|
|
|
printf("config a3k failed.\r\n"); |
|
|
|
return -2; |
|
|
|
err = -2; |
|
|
|
goto tail; |
|
|
|
} |
|
|
|
|
|
|
|
if (config_a3k_codec(tag)) { |
|
|
|
printf("config a3k codec failed.\r\n"); |
|
|
|
return -3; |
|
|
|
err = -3; |
|
|
|
goto tail; |
|
|
|
} |
|
|
|
|
|
|
|
read_cfg(tag); |
|
|
|
// read_cfg(tag);
|
|
|
|
|
|
|
|
if (start_a3k_codec(tag, 0, 0, 0)) { |
|
|
|
printf("start a3k codec mode failed.\r\n"); |
|
|
|
return -4; |
|
|
|
err = -4; |
|
|
|
goto tail; |
|
|
|
} |
|
|
|
|
|
|
|
return 0; |
|
|
|
tail: |
|
|
|
ticks_close(); |
|
|
|
return err; |
|
|
|
} |
|
|
|
|
|
|
|
void a3k_close(a3k_t tag) |
|
|
|