123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- #include "sdk_common.h"
- #if NRF_MODULE_ENABLED(HCI_SLIP)
- #include "hci_slip.h"
- #include <stdlib.h>
- #include "app_uart.h"
- #include "nrf_error.h"
- #define APP_SLIP_END 0xC0
- #define APP_SLIP_ESC 0xDB
- #define APP_SLIP_ESC_END 0xDC
- #define APP_SLIP_ESC_ESC 0xDD
- typedef enum
- {
- SLIP_OFF,
- SLIP_READY,
- SLIP_TRANSMITTING,
- } slip_states_t;
- static slip_states_t m_current_state = SLIP_OFF;
- static hci_slip_event_handler_t m_slip_event_handler;
- static const uint8_t * mp_tx_buffer;
- static uint32_t m_tx_buffer_length;
- static volatile uint32_t m_tx_buffer_index;
- static uint8_t * mp_rx_buffer;
- static uint32_t m_rx_buffer_length;
- static uint32_t m_rx_received_count;
- static void handle_rx_byte_default(uint8_t byte);
- static void handle_rx_byte_wait_start(uint8_t byte);
- static void handle_rx_byte_esc(uint8_t byte);
- static void (*handle_rx_byte) (uint8_t byte) = handle_rx_byte_wait_start;
- static uint32_t send_tx_byte_default(void);
- static uint32_t send_tx_byte_esc(void);
- static uint32_t send_tx_byte_encoded(void);
- static uint32_t send_tx_byte_end(void);
- uint32_t (*send_tx_byte) (void) = send_tx_byte_default;
- static uint32_t send_tx_byte_end(void)
- {
- uint32_t err_code = app_uart_put(APP_SLIP_END);
- if ((err_code == NRF_SUCCESS) && (m_tx_buffer_index == 0))
- {
-
- send_tx_byte = send_tx_byte_default;
- }
- return err_code;
- }
- static uint32_t send_tx_byte_default(void)
- {
- uint32_t err_code = app_uart_put(mp_tx_buffer[m_tx_buffer_index]);
- if (err_code == NRF_SUCCESS)
- {
- m_tx_buffer_index++;
- }
- return err_code;
- }
- static uint32_t send_tx_byte_encoded(void)
- {
- uint32_t err_code;
- switch (mp_tx_buffer[m_tx_buffer_index])
- {
- case APP_SLIP_END:
- err_code = app_uart_put(APP_SLIP_ESC_END);
- break;
- case APP_SLIP_ESC:
- err_code = app_uart_put(APP_SLIP_ESC_ESC);
- break;
- default:
- err_code = NRF_ERROR_NO_MEM;
- break;
- }
- if (err_code == NRF_SUCCESS)
- {
- m_tx_buffer_index++;
- send_tx_byte = send_tx_byte_default;
- }
- return err_code;
- }
- static uint32_t send_tx_byte_esc(void)
- {
- uint32_t err_code = app_uart_put(APP_SLIP_ESC);
- if (err_code == NRF_SUCCESS)
- {
- send_tx_byte = send_tx_byte_encoded;
- }
- return err_code;
- }
- static void transmit_buffer(void)
- {
- uint32_t err_code = NRF_SUCCESS;
- while (m_tx_buffer_index < m_tx_buffer_length)
- {
- if ((mp_tx_buffer[m_tx_buffer_index] == APP_SLIP_END ||
- mp_tx_buffer[m_tx_buffer_index] == APP_SLIP_ESC) &&
- send_tx_byte == send_tx_byte_default)
- {
- send_tx_byte = send_tx_byte_esc;
- }
- err_code = send_tx_byte();
- if (err_code == NRF_ERROR_NO_MEM || err_code == NRF_ERROR_BUSY)
- {
-
- return;
- }
- }
- send_tx_byte = send_tx_byte_end;
- err_code = send_tx_byte();
- if (err_code == NRF_SUCCESS)
- {
-
- m_current_state = SLIP_READY;
- if (m_slip_event_handler != NULL)
- {
- hci_slip_evt_t event = {HCI_SLIP_TX_DONE, mp_tx_buffer, m_tx_buffer_index};
- m_slip_event_handler(event);
- }
- }
- }
- static void handle_slip_end(void)
- {
- if (m_rx_received_count > 0)
- {
-
- if (m_slip_event_handler != NULL)
- {
- hci_slip_evt_t event = {HCI_SLIP_RX_RDY, mp_rx_buffer, m_rx_received_count};
- m_rx_received_count = 0;
- mp_rx_buffer = NULL;
- m_slip_event_handler(event);
- }
- }
- }
- static void handle_rx_byte_esc(uint8_t byte)
- {
- switch (byte)
- {
- case APP_SLIP_END:
- handle_slip_end();
- break;
- case APP_SLIP_ESC_END:
- mp_rx_buffer[m_rx_received_count++] = APP_SLIP_END;
- break;
- case APP_SLIP_ESC_ESC:
- mp_rx_buffer[m_rx_received_count++] = APP_SLIP_ESC;
- break;
- default:
- mp_rx_buffer[m_rx_received_count++] = byte;
- break;
- }
- handle_rx_byte = handle_rx_byte_default;
- }
- static void handle_rx_byte_default(uint8_t byte)
- {
- switch (byte)
- {
- case APP_SLIP_END:
- handle_slip_end();
- break;
- case APP_SLIP_ESC:
- handle_rx_byte = handle_rx_byte_esc;
- break;
- default:
- mp_rx_buffer[m_rx_received_count++] = byte;
- break;
- }
- }
- static void handle_rx_byte_wait_start(uint8_t byte)
- {
- if (byte == APP_SLIP_END)
- {
- handle_rx_byte = handle_rx_byte_default;
- }
- }
- static bool rx_buffer_overflowed(void)
- {
- if (mp_rx_buffer == NULL || m_rx_received_count >= m_rx_buffer_length)
- {
- if (m_slip_event_handler != NULL)
- {
- hci_slip_evt_t event = {HCI_SLIP_RX_OVERFLOW, mp_rx_buffer, m_rx_received_count};
- m_slip_event_handler(event);
- }
- return true;
- }
- return false;
- }
- static void slip_uart_eventhandler(app_uart_evt_t * uart_event)
- {
- if (uart_event->evt_type == APP_UART_TX_EMPTY && m_current_state == SLIP_TRANSMITTING)
- {
- transmit_buffer();
- }
- if ((uart_event->evt_type == APP_UART_DATA) && (!rx_buffer_overflowed()))
- {
- handle_rx_byte(uart_event->data.value);
- }
- }
- static uint32_t slip_uart_open(void)
- {
- uint32_t err_code;
- app_uart_comm_params_t comm_params =
- {
- HCI_UART_RX_PIN,
- HCI_UART_TX_PIN,
- HCI_UART_RTS_PIN,
- HCI_UART_CTS_PIN,
- (app_uart_flow_control_t)HCI_UART_FLOW_CONTROL,
- false,
- HCI_UART_BAUDRATE
- };
- err_code = app_uart_init(&comm_params,
- NULL,
- slip_uart_eventhandler,
- APP_IRQ_PRIORITY_LOWEST);
- if (err_code == NRF_SUCCESS)
- {
- m_current_state = SLIP_READY;
- }
- return err_code;
- }
- uint32_t hci_slip_evt_handler_register(hci_slip_event_handler_t event_handler)
- {
- m_slip_event_handler = event_handler;
- return NRF_SUCCESS;
- }
- uint32_t hci_slip_open()
- {
- switch (m_current_state)
- {
- case SLIP_OFF:
- return slip_uart_open();
- default:
-
- break;
- }
- return NRF_SUCCESS;
- }
- uint32_t hci_slip_close()
- {
- m_current_state = SLIP_OFF;
- uint32_t err_code = app_uart_close();
- return err_code;
- }
- uint32_t hci_slip_write(const uint8_t * p_buffer, uint32_t length)
- {
- if (p_buffer == NULL)
- {
- return NRF_ERROR_INVALID_ADDR;
- }
- switch (m_current_state)
- {
- case SLIP_READY:
- m_tx_buffer_index = 0;
- m_tx_buffer_length = length;
- mp_tx_buffer = p_buffer;
- m_current_state = SLIP_TRANSMITTING;
- send_tx_byte = send_tx_byte_end;
- transmit_buffer();
- return NRF_SUCCESS;
- case SLIP_TRANSMITTING:
- return NRF_ERROR_NO_MEM;
- case SLIP_OFF:
- default:
- return NRF_ERROR_INVALID_STATE;
- }
- }
- uint32_t hci_slip_rx_buffer_register(uint8_t * p_buffer, uint32_t length)
- {
- mp_rx_buffer = p_buffer;
- m_rx_buffer_length = length;
- m_rx_received_count = 0;
- handle_rx_byte = handle_rx_byte_wait_start;
- return NRF_SUCCESS;
- }
- #endif
|