| #include <assert.h> |
| #include <stdint.h> |
| #include <ctype.h> |
|
|
| #include "board.h" |
|
|
| #include "csi_core.h" |
| #include "bflb_gpio.h" |
| #include "bflb_uart.h" |
| #include "bflb_mtimer.h" |
| #include "usbd_core.h" |
| #include "usbd_cdc.h" |
| #include "ring_buffer.h" |
|
|
| |
| |
| |
|
|
| |
| |
| static volatile bool bootloader_active = false; |
| static volatile uint64_t bootloader_activity; |
|
|
| Ring_Buffer_Type uart1_rx_rb; |
| static volatile struct cdc_line_coding s_cdc_line_coding; |
|
|
| static struct bflb_device_s *gpio; |
| static struct bflb_device_s *uart1; |
|
|
| static void uart1_gpio_init(void); |
| static void uart1_init(void); |
|
|
| |
| static int reset_state = 0; |
| static uint64_t reset_timer; |
| static int reset_ms; |
|
|
| void schedule_reset(bool bootloader) { |
| if(reset_state) return; |
| |
| if(bootloader) { |
| bflb_gpio_set(gpio, GPIO_PIN_BOOT); |
| bflb_gpio_reset(gpio, GPIO_PIN_27); |
| bootloader_activity = bflb_mtimer_get_time_ms(); |
| bootloader_active = true; |
| } else { |
| bootloader_active = false; |
| bflb_gpio_reset(gpio, GPIO_PIN_BOOT); |
| bflb_gpio_set(gpio, GPIO_PIN_27); |
| } |
| |
| reset_state = 1; |
| reset_timer = bflb_mtimer_get_time_ms(); |
| reset_ms = 10; |
| bflb_gpio_reset(gpio, GPIO_PIN_28); |
| } |
|
|
| void trigger_bootloader(void) { |
| |
| if(bootloader_active) |
| bootloader_activity = bflb_mtimer_get_time_ms(); |
| } |
| |
| int main(void) |
| { |
| board_init(); |
| uart1_gpio_init(); |
|
|
| static uint8_t uartx_rx_buffer[2 * 512]; |
| Ring_Buffer_Init(&uart1_rx_rb, uartx_rx_buffer, sizeof(uartx_rx_buffer), NULL, NULL); |
| uart1_init(); |
|
|
| extern void usbd_cdc_acm_template_init(void); |
| usbd_cdc_acm_template_init(); |
|
|
| for (size_t loop_count = 0;;) { |
|
|
| |
| |
| |
| if(bootloader_active) { |
| |
| |
| |
| |
| int t = bflb_mtimer_get_time_ms() - bootloader_activity; |
| if(t > 1000) { |
| bootloader_active = 0; |
| schedule_reset(false); |
| } |
| } |
| |
| |
| if(reset_state) { |
| |
| if(bflb_mtimer_get_time_ms() - reset_timer >= reset_ms) { |
| switch(reset_state) { |
| case 1: |
| |
| bflb_gpio_reset(gpio, GPIO_PIN_EN); |
| reset_timer = bflb_mtimer_get_time_ms(); |
| reset_ms = 500; |
| reset_state = 2; |
| break; |
| |
| case 2: |
| |
| bflb_gpio_set(gpio, GPIO_PIN_EN); |
| reset_state = 0; |
| bflb_gpio_set(gpio, GPIO_PIN_28); |
| break; |
| } |
| } |
| } |
|
|
| extern volatile bool ep_tx_busy_flag; |
| if (ep_tx_busy_flag) |
| continue; |
|
|
| size_t uart1_rx_rb_len = Ring_Buffer_Get_Length(&uart1_rx_rb); |
| if (!uart1_rx_rb_len) |
| continue; |
|
|
| if (uart1_rx_rb_len < 512 && loop_count++ < 1000) { |
| continue; |
| } |
| loop_count = 0; |
|
|
| uint8_t data[uart1_rx_rb_len]; |
| size_t uart1_rx_rb_len_acc = Ring_Buffer_Read(&uart1_rx_rb, data, uart1_rx_rb_len); |
| if (!uart1_rx_rb_len_acc) |
| continue; |
|
|
| csi_dcache_clean_invalid_range(data, uart1_rx_rb_len_acc); |
| ep_tx_busy_flag = true; |
| usbd_ep_start_write(CDC_IN_EP, data, uart1_rx_rb_len_acc); |
|
|
| trigger_bootloader(); |
| } |
| } |
|
|
| void usbd_cdc_acm_bulk_out_cb(uint8_t *buf, size_t len, uint8_t ep) |
| { |
| assert(ep == CDC_OUT_EP); |
| bflb_uart_txint_mask(uart1, false); |
| for (size_t i = 0; i < len; i++) |
| bflb_uart_putchar(uart1, buf[i]); |
|
|
| trigger_bootloader(); |
| } |
|
|
| static void uart1_gpio_init(void) |
| { |
| gpio = bflb_device_get_by_name("gpio"); |
| bflb_gpio_uart_init(gpio, GPIO_PIN_UART1_TX, GPIO_UART_FUNC_UART1_TX); |
| bflb_gpio_uart_init(gpio, GPIO_PIN_UART1_RX, GPIO_UART_FUNC_UART1_RX); |
|
|
| |
| bflb_gpio_init(gpio, GPIO_PIN_27, GPIO_OUTPUT | GPIO_PULLUP | GPIO_DRV_0); |
| bflb_gpio_init(gpio, GPIO_PIN_28, GPIO_OUTPUT | GPIO_PULLUP | GPIO_DRV_0); |
|
|
| |
| bflb_gpio_set(gpio, GPIO_PIN_27); |
| bflb_gpio_set(gpio, GPIO_PIN_28); |
|
|
| |
| |
| bflb_gpio_init(gpio, GPIO_PIN_BOOT, GPIO_OUTPUT | GPIO_PULLDOWN | GPIO_DRV_0); |
| bflb_gpio_reset(gpio, GPIO_PIN_BOOT); |
|
|
| bflb_gpio_init(gpio, GPIO_PIN_EN, GPIO_OUTPUT | GPIO_PULLUP | GPIO_DRV_0); |
| bflb_gpio_set(gpio, GPIO_PIN_EN); |
| } |
|
|
| static void uart_isr(int irq, void *arg) |
| { |
| uint32_t intstatus = bflb_uart_get_intstatus(uart1); |
|
|
| |
| if (intstatus & UART_INTSTS_RX_FIFO) { |
| while (bflb_uart_rxavailable(uart1)) { |
| char c = bflb_uart_getchar(uart1); |
| Ring_Buffer_Write_Byte(&uart1_rx_rb, c); |
| } |
| } |
| |
| if (intstatus & UART_INTSTS_RTO) { |
| while (bflb_uart_rxavailable(uart1)) { |
| char c = bflb_uart_getchar(uart1); |
| Ring_Buffer_Write_Byte(&uart1_rx_rb, c); |
| } |
| bflb_uart_int_clear(uart1, UART_INTCLR_RTO); |
| } |
| |
| if (intstatus & UART_INTSTS_TX_FIFO) { |
| bflb_uart_txint_mask(uart1, true); |
| } |
| } |
|
|
| static void uart1_init(void) |
| { |
| uart1 = bflb_device_get_by_name("uart1"); |
|
|
| |
| s_cdc_line_coding.dwDTERate = 9600; |
| s_cdc_line_coding.bDataBits = 8; |
| s_cdc_line_coding.bParityType = 0; |
| s_cdc_line_coding.bCharFormat = 0; |
| |
| bflb_uart_init( |
| uart1, |
| &(struct bflb_uart_config_s){ |
| .baudrate = (s_cdc_line_coding.dwDTERate), |
| .data_bits = UART_DATA_BITS_5 + (s_cdc_line_coding.bDataBits - 5), |
| .stop_bits = UART_STOP_BITS_0_5 + (s_cdc_line_coding.bCharFormat + 1), |
| .parity = UART_PARITY_NONE + (s_cdc_line_coding.bParityType), |
| .flow_ctrl = 0, |
| .tx_fifo_threshold = 7, |
| .rx_fifo_threshold = 0, |
| }); |
| |
| bflb_irq_attach(uart1->irq_num, uart_isr, NULL); |
| bflb_irq_enable(uart1->irq_num); |
| bflb_uart_rxint_mask(uart1, false); |
| } |
|
|
| |
| static bool dtr_state = false; |
| static bool rts_state = false; |
|
|
| void usbd_cdc_acm_set_dtr(uint8_t intf, bool dtr) { |
| #ifdef DEBUG_RTS_DTR |
| if(dtr != dtr_state) |
| printf("DTR %d\r\n", dtr); |
| #endif |
| |
| dtr_state = dtr; |
| trigger_bootloader(); |
| } |
|
|
| void usbd_cdc_acm_set_rts(uint8_t intf, bool rts) { |
| #ifdef DEBUG_RTS_DTR |
| if(rts != rts_state) |
| printf("RTS %d\r\n", rts); |
| #endif |
| |
| |
| if(rts && !rts_state) |
| if(!dtr_state) |
| schedule_reset(true); |
| |
| rts_state = rts; |
| trigger_bootloader(); |
| } |
|
|
| void usbd_cdc_acm_set_line_coding(uint8_t intf, struct cdc_line_coding *line_coding) |
| { |
| #ifdef DEBUG_LINE |
| char *stop_str[] = { "1", "1.5", "2" }; |
| char *parity_str[] = { "N", "O", "E" }; |
| |
| printf("SET %lu %d%s%s\r\n", line_coding->dwDTERate, line_coding->bDataBits, |
| parity_str[line_coding->bParityType], stop_str[line_coding->bCharFormat]); |
| #endif |
| |
| memcpy((void *)&s_cdc_line_coding, line_coding, sizeof(struct cdc_line_coding)); |
| |
| |
| if(line_coding->dwDTERate == 1200) |
| schedule_reset(false); |
| |
| bflb_uart_disable(uart1); |
| bflb_uart_feature_control(uart1, UART_CMD_SET_BAUD_RATE, (s_cdc_line_coding.dwDTERate)); |
| bflb_uart_feature_control(uart1, UART_CMD_SET_DATA_BITS, (s_cdc_line_coding.bDataBits - 5)); |
| bflb_uart_feature_control(uart1, UART_CMD_SET_STOP_BITS, (s_cdc_line_coding.bCharFormat + 1)); |
| bflb_uart_feature_control(uart1, UART_CMD_SET_PARITY_BITS, (s_cdc_line_coding.bParityType)); |
| bflb_uart_enable(uart1); |
|
|
| trigger_bootloader(); |
| } |
|
|
| void usbd_cdc_acm_get_line_coding(uint8_t intf, struct cdc_line_coding *line_coding) |
| { |
| #ifdef DEBUG_LINE |
| printf("get_line_coding(intf %d)\r\n", intf); |
| #endif |
| memcpy(line_coding, (void *)&s_cdc_line_coding, sizeof(struct cdc_line_coding)); |
| } |
|
|