working on migrate hci from uart to usb

This commit is contained in:
hathach 2018-04-09 18:52:46 +07:00
parent bee4488811
commit a9267cd9ff
4 changed files with 42 additions and 35 deletions

View File

@ -44,6 +44,8 @@
#include "app_uart.h" #include "app_uart.h"
#include "nrf_error.h" #include "nrf_error.h"
#include "tusb.h"
#define APP_SLIP_END 0xC0 /**< SLIP code for identifying the beginning and end of a packet frame.. */ #define APP_SLIP_END 0xC0 /**< SLIP code for identifying the beginning and end of a packet frame.. */
#define APP_SLIP_ESC 0xDB /**< SLIP escape code. This code is used to specify that the following character is specially encoded. */ #define APP_SLIP_ESC 0xDB /**< SLIP escape code. This code is used to specify that the following character is specially encoded. */
#define APP_SLIP_ESC_END 0xDC /**< SLIP special code. When this code follows 0xDB, this character is interpreted as payload data 0xC0.. */ #define APP_SLIP_ESC_END 0xDC /**< SLIP special code. When this code follows 0xDB, this character is interpreted as payload data 0xC0.. */
@ -117,10 +119,14 @@ static uint32_t send_tx_byte_end(void);
*/ */
uint32_t (*send_tx_byte) (void) = send_tx_byte_default; uint32_t (*send_tx_byte) (void) = send_tx_byte_default;
static uint32_t usb_uart_put(char ch)
{
return tud_cdc_write_char(ch) ? NRF_SUCCESS : NRF_ERROR_NO_MEM;
}
static uint32_t send_tx_byte_end(void) static uint32_t send_tx_byte_end(void)
{ {
uint32_t err_code = app_uart_put(APP_SLIP_END); uint32_t err_code = usb_uart_put(APP_SLIP_END);
if ((err_code == NRF_SUCCESS) && (m_tx_buffer_index == 0)) if ((err_code == NRF_SUCCESS) && (m_tx_buffer_index == 0))
{ {
@ -134,7 +140,7 @@ static uint32_t send_tx_byte_end(void)
static uint32_t send_tx_byte_default(void) static uint32_t send_tx_byte_default(void)
{ {
uint32_t err_code = app_uart_put(mp_tx_buffer[m_tx_buffer_index]); uint32_t err_code = usb_uart_put(mp_tx_buffer[m_tx_buffer_index]);
if (err_code == NRF_SUCCESS) if (err_code == NRF_SUCCESS)
{ {
@ -152,11 +158,11 @@ static uint32_t send_tx_byte_encoded(void)
switch (mp_tx_buffer[m_tx_buffer_index]) switch (mp_tx_buffer[m_tx_buffer_index])
{ {
case APP_SLIP_END: case APP_SLIP_END:
err_code = app_uart_put(APP_SLIP_ESC_END); err_code = usb_uart_put(APP_SLIP_ESC_END);
break; break;
case APP_SLIP_ESC: case APP_SLIP_ESC:
err_code = app_uart_put(APP_SLIP_ESC_ESC); err_code = usb_uart_put(APP_SLIP_ESC_ESC);
break; break;
default: default:
@ -176,7 +182,7 @@ static uint32_t send_tx_byte_encoded(void)
static uint32_t send_tx_byte_esc(void) static uint32_t send_tx_byte_esc(void)
{ {
uint32_t err_code = app_uart_put(APP_SLIP_ESC); uint32_t err_code = usb_uart_put(APP_SLIP_ESC);
if (err_code == NRF_SUCCESS) if (err_code == NRF_SUCCESS)
{ {
@ -333,7 +339,7 @@ static bool rx_buffer_overflowed(void)
return false; return false;
} }
#if 0
/** @brief Function for handling the UART module event. It parses events from the UART when /** @brief Function for handling the UART module event. It parses events from the UART when
* bytes are received/transmitted. * bytes are received/transmitted.
* *
@ -379,10 +385,26 @@ static uint32_t slip_uart_open(void)
{ {
m_current_state = SLIP_READY; m_current_state = SLIP_READY;
} }
return err_code; return err_code;
} }
#else
static uint32_t slip_uart_open(void)
{
m_current_state = SLIP_READY;
return NRF_SUCCESS;
}
void tud_cdc_rx_cb(uint8_t port)
{
while ( tud_cdc_available() )
{
int8_t ch = tud_cdc_read_char();
handle_rx_byte((uint8_t) ch);
}
}
#endif
uint32_t hci_slip_evt_handler_register(hci_slip_event_handler_t event_handler) uint32_t hci_slip_evt_handler_register(hci_slip_event_handler_t event_handler)
{ {
@ -411,9 +433,13 @@ uint32_t hci_slip_open()
uint32_t hci_slip_close() uint32_t hci_slip_close()
{ {
m_current_state = SLIP_OFF; m_current_state = SLIP_OFF;
#if 0
uint32_t err_code = app_uart_close(); uint32_t err_code = app_uart_close();
return err_code; return err_code;
#else
return NRF_SUCCESS;
#endif
} }
@ -454,4 +480,6 @@ uint32_t hci_slip_rx_buffer_register(uint8_t * p_buffer, uint32_t length)
handle_rx_byte = handle_rx_byte_wait_start; handle_rx_byte = handle_rx_byte_wait_start;
return NRF_SUCCESS; return NRF_SUCCESS;
} }
#endif //NRF_MODULE_ENABLED(HCI_SLIP) #endif //NRF_MODULE_ENABLED(HCI_SLIP)

View File

@ -29,6 +29,8 @@
#include "app_timer.h" #include "app_timer.h"
#include "tusb.h"
#define APP_TIMER_PRESCALER 0 #define APP_TIMER_PRESCALER 0
#define IRQ_ENABLED 0x01 /**< Field identifying if an interrupt is enabled. */ #define IRQ_ENABLED 0x01 /**< Field identifying if an interrupt is enabled. */
@ -122,6 +124,9 @@ static void wait_for_events(void)
// Event received. Process it from the scheduler. // Event received. Process it from the scheduler.
app_sched_execute(); app_sched_execute();
// USB stack
tusb_task();
if ((m_update_status == BOOTLOADER_COMPLETE) || if ((m_update_status == BOOTLOADER_COMPLETE) ||
(m_update_status == BOOTLOADER_TIMEOUT) || (m_update_status == BOOTLOADER_TIMEOUT) ||
(m_update_status == BOOTLOADER_RESET)) (m_update_status == BOOTLOADER_RESET))

@ -1 +1 @@
Subproject commit b9fc708e41a8e2cc3135aa2ff07567e6daa61097 Subproject commit a74b2d34e5467d913d86abf3331eab2ccd2def92

View File

@ -360,26 +360,6 @@ int main(void)
// Init usb stack // Init usb stack
tusb_init(); tusb_init();
// TODO temporarily code to test usb cdc
while( true )
{
sd_app_evt_wait();
app_sched_execute();
tusb_task();
// connected and there are data available -> echo back
if ( tud_mounted() && tud_cdc_available() )
{
uint8_t buf[64];
// read and echo back
uint32_t count = tud_cdc_read(buf, sizeof(buf));
tud_cdc_write(buf, count);
}
}
/*------------- Determine DFU mode (Serial, OTA, FRESET or normal) -------------*/ /*------------- Determine DFU mode (Serial, OTA, FRESET or normal) -------------*/
/* For metro52 LED_BLUE is muxed with FRESET. We only init FRESET BUTTON /* For metro52 LED_BLUE is muxed with FRESET. We only init FRESET BUTTON
@ -544,12 +524,6 @@ void tud_umount_cb(uint8_t port)
{ {
} }
void tud_cdc_rx_cb(uint8_t port)
{
}
uint32_t tusb_hal_millis(void) uint32_t tusb_hal_millis(void)
{ {
return ( ( ((uint64_t)app_timer_cnt_get())*1000*(APP_TIMER_CONFIG_RTC_FREQUENCY+1)) / APP_TIMER_CLOCK_FREQ ); return ( ( ((uint64_t)app_timer_cnt_get())*1000*(APP_TIMER_CONFIG_RTC_FREQUENCY+1)) / APP_TIMER_CLOCK_FREQ );