From a9267cd9fffbeca1f9500982dd8482742934c10a Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 9 Apr 2018 18:52:46 +0700 Subject: [PATCH] working on migrate hci from uart to usb --- lib/sdk/components/libraries/hci/hci_slip.c | 44 +++++++++++++++---- .../libraries/bootloader_dfu/bootloader.c | 5 +++ lib/tinyusb | 2 +- src/main.c | 26 ----------- 4 files changed, 42 insertions(+), 35 deletions(-) diff --git a/lib/sdk/components/libraries/hci/hci_slip.c b/lib/sdk/components/libraries/hci/hci_slip.c index 18657db..4dd23e3 100644 --- a/lib/sdk/components/libraries/hci/hci_slip.c +++ b/lib/sdk/components/libraries/hci/hci_slip.c @@ -44,6 +44,8 @@ #include "app_uart.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_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.. */ @@ -117,10 +119,14 @@ static uint32_t send_tx_byte_end(void); */ 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) { - 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)) { @@ -134,7 +140,7 @@ static uint32_t send_tx_byte_end(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) { @@ -152,11 +158,11 @@ static uint32_t send_tx_byte_encoded(void) switch (mp_tx_buffer[m_tx_buffer_index]) { case APP_SLIP_END: - err_code = app_uart_put(APP_SLIP_ESC_END); + err_code = usb_uart_put(APP_SLIP_ESC_END); break; case APP_SLIP_ESC: - err_code = app_uart_put(APP_SLIP_ESC_ESC); + err_code = usb_uart_put(APP_SLIP_ESC_ESC); break; default: @@ -176,7 +182,7 @@ static uint32_t send_tx_byte_encoded(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) { @@ -333,7 +339,7 @@ static bool rx_buffer_overflowed(void) return false; } - +#if 0 /** @brief Function for handling the UART module event. It parses events from the UART when * bytes are received/transmitted. * @@ -379,10 +385,26 @@ static uint32_t slip_uart_open(void) { m_current_state = SLIP_READY; } - 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) { @@ -411,9 +433,13 @@ uint32_t hci_slip_open() uint32_t hci_slip_close() { m_current_state = SLIP_OFF; +#if 0 uint32_t err_code = app_uart_close(); - 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; return NRF_SUCCESS; } + + #endif //NRF_MODULE_ENABLED(HCI_SLIP) diff --git a/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c b/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c index cdc9c50..7983fa6 100644 --- a/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c +++ b/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c @@ -29,6 +29,8 @@ #include "app_timer.h" +#include "tusb.h" + #define APP_TIMER_PRESCALER 0 #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. app_sched_execute(); + // USB stack + tusb_task(); + if ((m_update_status == BOOTLOADER_COMPLETE) || (m_update_status == BOOTLOADER_TIMEOUT) || (m_update_status == BOOTLOADER_RESET)) diff --git a/lib/tinyusb b/lib/tinyusb index b9fc708..a74b2d3 160000 --- a/lib/tinyusb +++ b/lib/tinyusb @@ -1 +1 @@ -Subproject commit b9fc708e41a8e2cc3135aa2ff07567e6daa61097 +Subproject commit a74b2d34e5467d913d86abf3331eab2ccd2def92 diff --git a/src/main.c b/src/main.c index 831e7fa..fcf8edc 100644 --- a/src/main.c +++ b/src/main.c @@ -360,26 +360,6 @@ int main(void) // Init usb stack 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) -------------*/ /* 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) { return ( ( ((uint64_t)app_timer_cnt_get())*1000*(APP_TIMER_CONFIG_RTC_FREQUENCY+1)) / APP_TIMER_CLOCK_FREQ );