diff --git a/drivers/bl602_driver/startup/GCC/entry.S b/drivers/bl602_driver/startup/GCC/entry.S index 74c4ec73..eb86024a 100644 --- a/drivers/bl602_driver/startup/GCC/entry.S +++ b/drivers/bl602_driver/startup/GCC/entry.S @@ -52,7 +52,6 @@ _enter: /* Intial the mtvt, MUST BE 64 bytes aligned*/ .weak __Vectors la t0, __Vectors - // From drivers/bl702_driver/risc-v/Core/Include/riscv_encoding.h csrw CSR_MTVT, t0 #ifdef __riscv_float_abi_single @@ -87,21 +86,10 @@ _enter: li a2, 0 call main - /* If we've made it back here then there's probably something wrong. We - * allow the METAL to register a handler here. */ - .weak __metal_after_main - la ra, __metal_after_main - beqz ra, 1f - jalr ra -1: + csrci mstatus, (1 << 3) - /* If that handler returns then there's not a whole lot we can do. Just - * try to make some noise. */ - la t0, 1f - csrw mtvec, t0 -1: - lw t1, 0(x0) - j 1b +__exit: + j __exit .cfi_endproc diff --git a/drivers/bl702_driver/hal_drv/inc/hal_i2s.h b/drivers/bl702_driver/hal_drv/inc/hal_i2s.h index 19488560..434fd8a3 100644 --- a/drivers/bl702_driver/hal_drv/inc/hal_i2s.h +++ b/drivers/bl702_driver/hal_drv/inc/hal_i2s.h @@ -109,7 +109,7 @@ typedef struct i2s_device { void *rx_dma; } i2s_device_t; -#define UART_DEV(dev) ((uart_device_t *)dev) +#define I2S_DEV(dev) ((i2s_device_t *)dev) int i2s_register(enum i2s_index_type index, const char *name, uint16_t flag); diff --git a/drivers/bl702_driver/hal_drv/inc/hal_usb.h b/drivers/bl702_driver/hal_drv/inc/hal_usb.h index 0223b0dd..b964cbdf 100644 --- a/drivers/bl702_driver/hal_drv/inc/hal_usb.h +++ b/drivers/bl702_driver/hal_drv/inc/hal_usb.h @@ -27,19 +27,13 @@ #include "ring_buffer.h" #include "bl702_config.h" -#define DEVICE_CTRL_USB_DC_SET_ADDR 0X10 -#define DEVICE_CTRL_USB_DC_SET_ACK 0X11 -#define DEVICE_CTRL_USB_DC_SET_NACK 0X12 -#define DEVICE_CTRL_USB_DC_SET_STALL 0X13 -#define DEVICE_CTRL_USB_DC_CLR_STALL 0X14 -#define DEVICE_CTRL_USB_DC_GET_STALL 0X15 -#define DEVICE_CTRL_USB_DC_CLR_NACK 0X16 -#define DEVICE_CTRL_USB_DC_CHECK_EPCAP 0X17 -#define DEVICE_CTRL_USB_DC_GET_TX_FIFO_CNT 0x18 -#define DEVICE_CTRL_USB_DC_GET_RX_FIFO_CNT 0x19 -#define DEVICE_CTRL_USB_DC_GET_EP_FREE 0x20 -#define DEVICE_CTRL_USB_DC_SET_TX_DMA 0x21 -#define DEVICE_CTRL_USB_DC_SET_RX_DMA 0x22 +#define DEVICE_CTRL_USB_DC_SET_ACK 0X10 +#define DEVICE_CTRL_USB_DC_ENUM_ON 0X11 +#define DEVICE_CTRL_USB_DC_ENUM_OFF 0X12 +#define DEVICE_CTRL_USB_DC_GET_EP_TX_FIFO_CNT 0x13 +#define DEVICE_CTRL_USB_DC_GET_EP_RX_FIFO_CNT 0x14 +#define DEVICE_CTRL_USB_DC_SET_TX_DMA 0x15 +#define DEVICE_CTRL_USB_DC_SET_RX_DMA 0x16 enum usb_index_type { #ifdef BSP_USING_USB @@ -166,8 +160,9 @@ enum usb_error_type { USB_DC_EP_DIR_ERR = 1, USB_DC_EP_EN_ERR = 2, USB_DC_EP_TIMEOUT_ERR = 3, - USB_DC_RB_SIZE_SMALL_ERR = 4, - USB_DC_ZLP_ERR = 5, + USB_DC_ADDR_ERR = 4, + USB_DC_RB_SIZE_SMALL_ERR = 5, + USB_DC_ZLP_ERR = 6, }; /** * @brief USB Endpoint Configuration. @@ -210,7 +205,11 @@ typedef struct usb_dc_device { int usb_dc_register(enum usb_index_type index, const char *name, uint16_t flag); +int usb_dc_set_dev_address(const uint8_t addr); int usb_dc_ep_open(struct device *dev, const struct usb_dc_ep_cfg *ep_cfg); +int usb_dc_ep_close(const uint8_t ep); +int usb_dc_ep_set_stall(const uint8_t ep); +int usb_dc_ep_clear_stall(const uint8_t ep); int usb_dc_ep_is_stalled(struct device *dev, const uint8_t ep, uint8_t *stalled); int usb_dc_ep_write(struct device *dev, const uint8_t ep, const uint8_t *data, uint32_t data_len, uint32_t *ret_bytes); int usb_dc_ep_read(struct device *dev, const uint8_t ep, uint8_t *data, uint32_t data_len, uint32_t *read_bytes); diff --git a/drivers/bl702_driver/hal_drv/src/hal_clock.c b/drivers/bl702_driver/hal_drv/src/hal_clock.c index ea787754..a2378ac5 100644 --- a/drivers/bl702_driver/hal_drv/src/hal_clock.c +++ b/drivers/bl702_driver/hal_drv/src/hal_clock.c @@ -147,8 +147,7 @@ void peripheral_clock_init(void) #endif #if defined(BSP_USING_I2S0) - GLB_Set_Chip_Out_0_CLK_Sel(GLB_CHIP_CLK_OUT_I2S_REF_CLK); - GLB_Set_I2S_CLK(ENABLE, GLB_I2S_OUT_REF_CLK_SRC); + GLB_Set_I2S_CLK(ENABLE, GLB_I2S_OUT_REF_CLK_NONE); #endif #if defined(BSP_USING_ADC0) #if BSP_ADC_CLOCK_SOURCE == ROOT_CLOCK_SOURCE_AUPLL diff --git a/drivers/bl702_driver/hal_drv/src/hal_dma.c b/drivers/bl702_driver/hal_drv/src/hal_dma.c index dcc2ac90..6d39a1bc 100644 --- a/drivers/bl702_driver/hal_drv/src/hal_dma.c +++ b/drivers/bl702_driver/hal_drv/src/hal_dma.c @@ -89,7 +89,8 @@ int dma_open(struct device *dev, uint16_t oflag) DMA_Enable(); Interrupt_Handler_Register(DMA_ALL_IRQn, DMA0_IRQ); - + /* Enable uart interrupt*/ + NVIC_EnableIRQ(DMA_ALL_IRQn); return 0; } /** @@ -110,8 +111,6 @@ int dma_control(struct device *dev, int cmd, void *args) DMA_IntMask(dma_device->ch, DMA_INT_TCOMPLETED, UNMASK); DMA_IntMask(dma_device->ch, DMA_INT_ERR, UNMASK); - /* Enable uart interrupt*/ - NVIC_EnableIRQ(DMA_ALL_IRQn); break; case DEVICE_CTRL_CLR_INT /* constant-expression */: @@ -119,8 +118,6 @@ int dma_control(struct device *dev, int cmd, void *args) DMA_IntMask(dma_device->ch, DMA_INT_TCOMPLETED, MASK); DMA_IntMask(dma_device->ch, DMA_INT_ERR, MASK); - /* Enable uart interrupt*/ - NVIC_DisableIRQ(DMA_ALL_IRQn); break; case DEVICE_CTRL_GET_INT /* constant-expression */: diff --git a/drivers/bl702_driver/hal_drv/src/hal_i2s.c b/drivers/bl702_driver/hal_drv/src/hal_i2s.c index 9f7a518a..8facad45 100644 --- a/drivers/bl702_driver/hal_drv/src/hal_i2s.c +++ b/drivers/bl702_driver/hal_drv/src/hal_i2s.c @@ -39,7 +39,7 @@ int i2s_open(struct device *dev, uint16_t oflag) I2S_FifoCfg_Type fifoCfg = { 0 }; GLB_Set_Chip_Out_0_CLK_Sel(GLB_CHIP_CLK_OUT_I2S_REF_CLK); - GLB_Set_I2S_CLK(ENABLE, GLB_I2S_OUT_REF_CLK_SRC); + GLB_Set_I2S_CLK(ENABLE, GLB_I2S_OUT_REF_CLK_NONE); /*Finding the right frequency*/ if (12288000 % (i2s_device->sampl_freq_hz) == 0) { diff --git a/drivers/bl702_driver/hal_drv/src/hal_uart.c b/drivers/bl702_driver/hal_drv/src/hal_uart.c index eec66f0a..85936671 100644 --- a/drivers/bl702_driver/hal_drv/src/hal_uart.c +++ b/drivers/bl702_driver/hal_drv/src/hal_uart.c @@ -1,24 +1,24 @@ /** * @file hal_uart.c - * @brief - * + * @brief + * * Copyright (c) 2021 Bouffalolab team - * + * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. The * ASF licenses this file to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance with the * License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. - * + * */ #include "hal_uart.h" #include "hal_dma.h" @@ -44,11 +44,11 @@ static uart_device_t uartx_device[UART_MAX_INDEX] = { #endif }; /** - * @brief - * - * @param dev - * @param oflag - * @return int + * @brief + * + * @param dev + * @param oflag + * @return int */ int uart_open(struct device *dev, uint16_t oflag) { @@ -114,10 +114,10 @@ int uart_open(struct device *dev, uint16_t oflag) return 0; } /** - * @brief - * - * @param dev - * @return int + * @brief + * + * @param dev + * @return int */ int uart_close(struct device *dev) { @@ -127,12 +127,12 @@ int uart_close(struct device *dev) return 0; } /** - * @brief - * - * @param dev - * @param cmd - * @param args - * @return int + * @brief + * + * @param dev + * @param cmd + * @param args + * @return int */ int uart_control(struct device *dev, int cmd, void *args) { @@ -254,13 +254,13 @@ int uart_control(struct device *dev, int cmd, void *args) return 0; } /** - * @brief - * - * @param dev - * @param pos - * @param buffer - * @param size - * @return int + * @brief + * + * @param dev + * @param pos + * @param buffer + * @param size + * @return int */ int uart_write(struct device *dev, uint32_t pos, const void *buffer, uint32_t size) { @@ -278,19 +278,21 @@ int uart_write(struct device *dev, uint32_t pos, const void *buffer, uint32_t si dma_channel_start(dma_ch); } return 0; - } - return UART_SendData(uart_device->id, (uint8_t *)buffer, size); + } else if (dev->oflag & DEVICE_OFLAG_STREAM_TX) { + return UART_SendData(uart_device->id, (uint8_t *)buffer, size); + } else + return -1; } /** - * @brief - * - * @param dev - * @param pos - * @param buffer - * @param size - * @return int + * @brief + * + * @param dev + * @param pos + * @param buffer + * @param size + * @return int */ -int ATTR_TCM_SECTION uart_read(struct device *dev, uint32_t pos, void *buffer, uint32_t size) +int uart_read(struct device *dev, uint32_t pos, void *buffer, uint32_t size) { uart_device_t *uart_device = (uart_device_t *)dev; if (dev->oflag & DEVICE_OFLAG_DMA_RX) { @@ -305,16 +307,23 @@ int ATTR_TCM_SECTION uart_read(struct device *dev, uint32_t pos, void *buffer, u dma_reload(dma_ch, (uint32_t)DMA_ADDR_UART1_RDR, (uint32_t)buffer, size); dma_channel_start(dma_ch); } + return 0; + } else if (dev->oflag & DEVICE_OFLAG_STREAM_RX) { + uint32_t rx_len = 0; + while (rx_len < size) { + rx_len += UART_ReceiveData(uart_device->id, (uint8_t *)buffer + rx_len, size - rx_len); + } + return 0; } - return UART_ReceiveData(uart_device->id, (uint8_t *)buffer, size); + return -1; } /** - * @brief - * - * @param index - * @param name - * @param flag - * @return int + * @brief + * + * @param index + * @param name + * @param flag + * @return int */ int uart_register(enum uart_index_type index, const char *name, uint16_t flag) { @@ -338,11 +347,11 @@ int uart_register(enum uart_index_type index, const char *name, uint16_t flag) return device_register(dev, name, flag); } /** - * @brief - * - * @param handle + * @brief + * + * @param handle */ -void ATTR_TCM_SECTION uart_isr(uart_device_t *handle) +void uart_isr(uart_device_t *handle) { uint32_t tmpVal = 0; uint32_t maskVal = 0; @@ -410,8 +419,8 @@ void ATTR_TCM_SECTION uart_isr(uart_device_t *handle) #ifdef BSP_USING_UART0 /** - * @brief - * + * @brief + * */ void UART0_IRQ(void) { @@ -420,8 +429,8 @@ void UART0_IRQ(void) #endif #ifdef BSP_USING_UART1 /** - * @brief - * + * @brief + * */ void UART1_IRQ(void) { diff --git a/drivers/bl702_driver/hal_drv/src/hal_usb.c b/drivers/bl702_driver/hal_drv/src/hal_usb.c index 59c06028..4422746e 100644 --- a/drivers/bl702_driver/hal_drv/src/hal_usb.c +++ b/drivers/bl702_driver/hal_drv/src/hal_usb.c @@ -333,72 +333,33 @@ int usb_control(struct device *dev, int cmd, void *args) offset++; } - } break; - + break; + } case DEVICE_CTRL_CLR_INT /* constant-expression */: - /* code */ - break; - case DEVICE_CTRL_GET_INT /* constant-expression */: - /* code */ - break; - - case DEVICE_CTRL_CONFIG /* constant-expression */: - break; - - case DEVICE_CTRL_GET_CONFIG /* constant-expression */: - break; - - case DEVICE_CTRL_USB_DC_SET_ADDR /* constant-expression */: - USB_Set_Device_Addr(((uint32_t)args) & 0x7f); - return 0; - case DEVICE_CTRL_USB_DC_SET_ACK /* constant-expression */: USB_Set_EPx_Status(USB_EP_GET_IDX(((uint32_t)args) & 0x7f), USB_EP_STATUS_ACK); return 0; - - case DEVICE_CTRL_USB_DC_SET_NACK /* constant-expression */: - USB_Set_EPx_Status(USB_EP_GET_IDX(((uint32_t)args) & 0x7f), USB_EP_STATUS_NACK); + case DEVICE_CTRL_USB_DC_ENUM_ON: { + uint32_t tmpVal; + tmpVal = BL_RD_REG(GLB_BASE, GLB_USB_XCVR); + tmpVal = BL_SET_REG_BITS_VAL(tmpVal, GLB_USB_ENUM, 1); + BL_WR_REG(GLB_BASE, GLB_USB_XCVR, tmpVal); return 0; - - case DEVICE_CTRL_USB_DC_SET_STALL /* constant-expression */: { - uint8_t ep_idx = USB_EP_GET_IDX(((uint32_t)args) & 0x7f); - - if (USB_EP_DIR_IS_OUT(((uint32_t)args) & 0x7f)) { - USB_Set_EPx_Status(ep_idx, USB_EP_STATUS_STALL); - usb_fs_device.out_ep[ep_idx].is_stalled = 1U; - } else { - USB_Set_EPx_Status(ep_idx, USB_EP_STATUS_STALL); - usb_fs_device.in_ep[ep_idx].is_stalled = 1U; - } } - + case DEVICE_CTRL_USB_DC_ENUM_OFF: { + uint32_t tmpVal; + tmpVal = BL_RD_REG(GLB_BASE, GLB_USB_XCVR); + tmpVal = BL_SET_REG_BITS_VAL(tmpVal, GLB_USB_ENUM, 0); + BL_WR_REG(GLB_BASE, GLB_USB_XCVR, tmpVal); return 0; - - case DEVICE_CTRL_USB_DC_CLR_STALL /* constant-expression */: { - uint8_t ep_idx = USB_EP_GET_IDX(((uint32_t)args) & 0x7f); - - if (USB_EP_DIR_IS_OUT(((uint32_t)args) & 0x7f)) { - USB_Set_EPx_Status(ep_idx, USB_EP_STATUS_NSTALL); - usb_fs_device.out_ep[ep_idx].is_stalled = 0; - } else { - USB_Set_EPx_Status(ep_idx, USB_EP_STATUS_NSTALL); - usb_fs_device.in_ep[ep_idx].is_stalled = 0; - } } - - return 0; - - case DEVICE_CTRL_USB_DC_GET_TX_FIFO_CNT: + case DEVICE_CTRL_USB_DC_GET_EP_TX_FIFO_CNT: return USB_Get_EPx_TX_FIFO_CNT(((uint32_t)args) & 0x7f); - case DEVICE_CTRL_USB_DC_GET_RX_FIFO_CNT: + case DEVICE_CTRL_USB_DC_GET_EP_RX_FIFO_CNT: return USB_Get_EPx_RX_FIFO_CNT(((uint32_t)args) & 0x7f); - - case DEVICE_CTRL_USB_DC_GET_EP_FREE: - return USB_Is_EPx_RDY_Free(((uint32_t)args) & 0x7f); - case DEVICE_CTRL_ATTACH_TX_DMA /* constant-expression */: usb_device->tx_dma = (struct device *)args; break; @@ -501,6 +462,19 @@ int usb_dc_register(enum usb_index_type index, const char *name, uint16_t flag) return device_register(dev, name, flag); } +/** + * @brief Set USB device address + * + * @param[in] addr Device address + * + * @return 0 on success, negative errno code on fail. + */ +int usb_dc_set_dev_address(const uint8_t addr) +{ + USB_Set_Device_Addr(addr); + return 0; +} + /** * @brief configure and enable endpoint * This function sets endpoint configuration according to one specified in USB @@ -581,6 +555,54 @@ int usb_dc_ep_open(struct device *dev, const struct usb_dc_ep_cfg *ep_cfg) return 0; } +int usb_dc_ep_close(const uint8_t ep) +{ + return 0; +} + +/** + * @brief Set stall condition for the selected endpoint + * + * @param[in] ep Endpoint address corresponding to the one + * listed in the device configuration table + * + * @return 0 on success, negative errno code on fail. + */ +int usb_dc_ep_set_stall(const uint8_t ep) +{ + uint8_t ep_idx = USB_EP_GET_IDX(ep); + + if (USB_EP_DIR_IS_OUT(ep)) { + USB_Set_EPx_Status(ep_idx, USB_EP_STATUS_STALL); + usb_fs_device.out_ep[ep_idx].is_stalled = 1U; + } else { + USB_Set_EPx_Status(ep_idx, USB_EP_STATUS_STALL); + usb_fs_device.in_ep[ep_idx].is_stalled = 1U; + } + return 0; +} +/** + * @brief Clear stall condition for the selected endpoint + * + * @param[in] ep Endpoint address corresponding to the one + * listed in the device configuration table + * + * @return 0 on success, negative errno code on fail. + */ +int usb_dc_ep_clear_stall(const uint8_t ep) +{ + uint8_t ep_idx = USB_EP_GET_IDX(ep); + + if (USB_EP_DIR_IS_OUT(ep)) { + USB_Set_EPx_Status(ep_idx, USB_EP_STATUS_NSTALL); + usb_fs_device.out_ep[ep_idx].is_stalled = 0; + } else { + USB_Set_EPx_Status(ep_idx, USB_EP_STATUS_NSTALL); + usb_fs_device.in_ep[ep_idx].is_stalled = 0; + } + return 0; +} + /** * @brief Check if the selected endpoint is stalled * @@ -643,12 +665,12 @@ int usb_dc_ep_write(struct device *dev, const uint8_t ep, const uint8_t *data, u /* Check if IN ep */ if (USB_EP_GET_DIR(ep) != USB_EP_DIR_IN) { - return -1; + return -USB_DC_EP_DIR_ERR; } /* Check if ep enabled */ if (!usb_ep_is_enabled(ep)) { - return -2; + return -USB_DC_EP_EN_ERR; } while (!USB_Is_EPx_RDY_Free(ep_idx)) { @@ -656,21 +678,20 @@ int usb_dc_ep_write(struct device *dev, const uint8_t ep, const uint8_t *data, u if (!timeout) { USB_DC_LOG_ERR("ep%d wait free timeout\r\n", ep); - return -3; + return -USB_DC_EP_TIMEOUT_ERR; } } - // key = irq_lock(); - if (!data) { - if (!data_len) { - /* Zero length packet */ - /* Clear NAK and enable ep */ - USB_Set_EPx_Status(USB_EP_GET_IDX(ep), USB_EP_STATUS_ACK); - return 0; - } else { - USB_DC_LOG_ERR("data is null\r\n"); - return -4; - } + if (!data_len) { + /* Zero length packet */ + /* Clear NAK and enable ep */ + USB_Set_EPx_Status(USB_EP_GET_IDX(ep), USB_EP_STATUS_ACK); + return USB_DC_OK; + } + + if (!data && data_len) { + USB_DC_LOG_ERR("data is null\r\n"); + return -USB_DC_ADDR_ERR; } if (data_len > usb_fs_device.in_ep[ep_idx].ep_cfg.ep_mps) { @@ -716,7 +737,7 @@ int usb_dc_ep_write(struct device *dev, const uint8_t ep, const uint8_t *data, u *ret_bytes = data_len; } - return 0; + return USB_DC_OK; } /** @@ -748,13 +769,13 @@ int usb_dc_ep_read(struct device *dev, const uint8_t ep, uint8_t *data, uint32_t /* Check if OUT ep */ if (USB_EP_GET_DIR(ep) != USB_EP_DIR_OUT) { USB_DC_LOG_ERR("Wrong endpoint direction\r\n"); - return -1; + return -USB_DC_EP_DIR_ERR; } /* Check if ep enabled */ if (!usb_ep_is_enabled(ep)) { USB_DC_LOG_ERR("Not enabled endpoint\r\n"); - return -1; + return -USB_DC_EP_EN_ERR; } /*common process for other ep out*/ @@ -784,7 +805,12 @@ int usb_dc_ep_read(struct device *dev, const uint8_t ep, uint8_t *data, uint32_t if (!data_len) { /* Clear NAK and enable ep */ USB_Set_EPx_Status(USB_EP_GET_IDX(ep), USB_EP_STATUS_ACK); - return 0; + return USB_DC_OK; + } + + if (!data && data_len) { + USB_DC_LOG_ERR("data is null\r\n"); + return -USB_DC_ADDR_ERR; } read_count = USB_Get_EPx_RX_FIFO_CNT(ep_idx); @@ -799,7 +825,7 @@ int usb_dc_ep_read(struct device *dev, const uint8_t ep, uint8_t *data, uint32_t *read_bytes = read_count; } - return 0; + return USB_DC_OK; } /** * @brief diff --git a/drivers/bl702_driver/startup/GCC/entry.S b/drivers/bl702_driver/startup/GCC/entry.S index 74c4ec73..3080b577 100644 --- a/drivers/bl702_driver/startup/GCC/entry.S +++ b/drivers/bl702_driver/startup/GCC/entry.S @@ -87,21 +87,10 @@ _enter: li a2, 0 call main - /* If we've made it back here then there's probably something wrong. We - * allow the METAL to register a handler here. */ - .weak __metal_after_main - la ra, __metal_after_main - beqz ra, 1f - jalr ra -1: + csrci mstatus, (1 << 3) - /* If that handler returns then there's not a whole lot we can do. Just - * try to make some noise. */ - la t0, 1f - csrw mtvec, t0 -1: - lw t1, 0(x0) - j 1b +__exit: + j __exit .cfi_endproc