Merge branch 'master' into ble-api-cleanup

This commit is contained in:
Matthias Ringwald 2015-12-18 20:21:18 +01:00
commit e955494fd9
3 changed files with 39 additions and 38 deletions

View File

@ -53,6 +53,11 @@
#include "hal_uart_dma.h"
// assert pre-buffer for packet type is available
#if !defined(HCI_OUTGOING_PRE_BUFFER_SIZE) || (HCI_OUTGOING_PRE_BUFFER_SIZE == 0)
#error HCI_OUTGOING_PRE_BUFFER_SIZE not defined. Please update hci.h
#endif
typedef enum {
H4_W4_PACKET_TYPE = 1,
H4_W4_EVENT_HEADER,
@ -63,7 +68,6 @@ typedef enum {
typedef enum {
TX_IDLE = 1,
TX_W4_HEADER_SENT,
TX_W4_PACKET_SENT,
TX_DONE
} TX_STATE;
@ -97,9 +101,6 @@ static uint8_t * hci_packet = &hci_packet_prefixed[HCI_INCOMING_PRE_BUFFER_SIZE]
// tx state
static TX_STATE tx_state;
static uint8_t tx_packet_type;
static uint8_t *tx_data;
static uint16_t tx_len;
// static hci_transport_h4_t * hci_transport_h4 = NULL;
static void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = dummy_handler;
@ -220,11 +221,6 @@ static void h4_block_received(void){
static void h4_block_sent(void){
switch (tx_state){
case TX_W4_HEADER_SENT:
tx_state = TX_W4_PACKET_SENT;
// h4 packet type + actual packet
hal_uart_dma_send_block(tx_data, tx_len);
break;
case TX_W4_PACKET_SENT:
tx_state = TX_DONE;
// trigger run loop
@ -266,12 +262,16 @@ static int h4_send_packet(uint8_t packet_type, uint8_t *packet, int size){
return -1;
}
tx_packet_type = packet_type;
// store packet type before actual data and increase size
size++;
packet--;
*packet = packet_type;
tx_data = packet;
tx_len = size;
tx_state = TX_W4_HEADER_SENT;
hal_uart_dma_send_block(&tx_packet_type, 1);
tx_state = TX_W4_PACKET_SENT;
hal_uart_dma_send_block(tx_data, tx_len);
return 0;
}

View File

@ -58,9 +58,10 @@
#include "hal_uart_dma.h"
// #include <libopencm3/stm32/gpio.h>
// #define GPIO_DEBUG_0 GPIO1
// #define GPIO_DEBUG_1 GPIO2
// assert pre-buffer for packet type is available
#if !defined(HCI_OUTGOING_PRE_BUFFER_SIZE) || (HCI_OUTGOING_PRE_BUFFER_SIZE == 0)
#error HCI_OUTGOING_PRE_BUFFER_SIZE not defined. Please update hci.h
#endif
// #define LOG_EHCILL
@ -82,7 +83,6 @@ typedef enum {
typedef enum {
TX_IDLE = 1,
TX_W4_WAKEUP, // eHCILL only
TX_W4_HEADER_SENT,
TX_W4_PACKET_SENT,
TX_W2_EHCILL_SEND,
TX_W4_EHCILL_SENT,
@ -138,9 +138,8 @@ static void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t si
// H4: tx state
static volatile TX_STATE tx_state; // updated from block_sent callback
static volatile int tx_send_packet_sent; // updated from block_sent callback
static uint8_t tx_packet_type; // 0 == no outgoing packet
static uint8_t * tx_data;
static uint16_t tx_len;
static uint16_t tx_len; // 0 == no outgoing packet
// work around for eHCILL problem
static timer_source_t ehcill_sleep_ack_timer;
@ -204,7 +203,7 @@ static int h4_open(void *transport_config){
// init state machines
h4_rx_init_sm();
tx_state = TX_IDLE;
tx_packet_type = 0;
tx_len = 0;
return 0;
}
@ -303,7 +302,7 @@ static int h4_can_send_packet_now(uint8_t packet_type){
}
static int h4_outgoing_packet_ready(void){
return tx_packet_type != 0;
return tx_len != 0;
}
static void ehcill_sleep_ack_timer_handler(timer_source_t * timer){
@ -345,14 +344,9 @@ static void echill_send_wakeup_ind(void){
static void h4_block_sent(void){
int command;
switch (tx_state){
case TX_W4_HEADER_SENT:
tx_state = TX_W4_PACKET_SENT;
// h4 packet type + actual packet
hal_uart_dma_send_block(tx_data, tx_len);
break;
case TX_W4_PACKET_SENT:
// packet fully sent, reset state
tx_packet_type = 0;
tx_len = 0;
// now, send pending ehcill command if neccessary
switch (ehcill_command_to_send){
case EHCILL_GO_TO_SLEEP_ACK:
@ -509,8 +503,8 @@ static void ehcill_handle(uint8_t action){
#ifdef LOG_EHCILL
log_info("EHCILL: WAKE_UP_IND or ACK");
#endif
tx_state = TX_W4_HEADER_SENT;
hal_uart_dma_send_block(&tx_packet_type, 1);
tx_state = TX_W4_PACKET_SENT;
hal_uart_dma_send_block(tx_data, tx_len);
ehcill_state = EHCILL_STATE_AWAKE;
break;
@ -527,7 +521,6 @@ static int ehcill_send_packet(uint8_t packet_type, uint8_t *packet, int size){
// check for ongoing write
switch (tx_state){
case TX_W4_WAKEUP:
case TX_W4_HEADER_SENT:
case TX_W4_PACKET_SENT:
// we should not arrive here, log state
log_error("h4_send_packet with tx_state = %u, ehcill_state %u, packet type %u, data %02x %02x %02x", tx_state, ehcill_state, packet_type, packet[0], packet[1], packet[2]);
@ -539,15 +532,19 @@ static int ehcill_send_packet(uint8_t packet_type, uint8_t *packet, int size){
break;
}
// store packet type before actual data and increase size
size++;
packet--;
*packet = packet_type;
// store request
tx_packet_type = packet_type;
tx_data = packet;
tx_len = size;
// if awake, just start sending
if (!ehcill_sleep_mode_active()){
tx_state = TX_W4_HEADER_SENT;
hal_uart_dma_send_block(&tx_packet_type, 1);
tx_state = TX_W4_PACKET_SENT;
hal_uart_dma_send_block(tx_data, tx_len);
return 0;
}

View File

@ -56,6 +56,11 @@
#include "hci.h"
#include "hci_transport.h"
// assert pre-buffer for packet type is available
#if !defined(HCI_OUTGOING_PRE_BUFFER_SIZE) || (HCI_OUTGOING_PRE_BUFFER_SIZE == 0)
#error HCI_OUTGOING_PRE_BUFFER_SIZE not defined. Please update hci.h
#endif
static int h4_process(struct data_source *ds);
static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size);
static hci_uart_config_t *hci_uart_config;
@ -206,14 +211,13 @@ static int h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){
if (hci_transport_h4->ds == NULL) return -1;
if (hci_transport_h4->uart_fd == 0) return -1;
// store packet type before actual data and increase size
char *data = (char*) packet;
int bytes_written = write(hci_transport_h4->uart_fd, &packet_type, 1);
while (bytes_written < 1) {
usleep(5000);
bytes_written = write(hci_transport_h4->uart_fd, &packet_type, 1);
};
size++;
data--;
*data = packet_type;
while (size > 0) {
bytes_written = write(hci_transport_h4->uart_fd, data, size);
int bytes_written = write(hci_transport_h4->uart_fd, data, size);
if (bytes_written < 0) {
usleep(5000);
continue;