* @brief Enable serial receive mode. * @param dev The pointer of device driver structure * @param rx_oflag The flag of that the serial port opens. * @return Return the status of the operation. */
| 947 | * @return Return the status of the operation. |
| 948 | */ |
| 949 | static rt_err_t rt_serial_rx_enable(struct rt_device *dev, |
| 950 | rt_uint16_t rx_oflag) |
| 951 | { |
| 952 | struct rt_serial_device *serial; |
| 953 | struct rt_serial_rx_fifo *rx_fifo = RT_NULL; |
| 954 | rt_size_t rx_fifo_size = 0; |
| 955 | |
| 956 | RT_ASSERT(dev != RT_NULL); |
| 957 | serial = (struct rt_serial_device *)dev; |
| 958 | |
| 959 | if (serial->config.rx_bufsz == 0) |
| 960 | { |
| 961 | /* Cannot use RT_SERIAL_RX_NON_BLOCKING when rx_bufsz is 0 */ |
| 962 | if (rx_oflag == RT_SERIAL_RX_NON_BLOCKING) |
| 963 | { |
| 964 | LOG_E("(%s) serial device with misconfigure: rx_bufsz = 0", |
| 965 | dev->parent.name); |
| 966 | return -RT_EINVAL; |
| 967 | } |
| 968 | |
| 969 | #ifndef RT_USING_DEVICE_OPS |
| 970 | dev->read = _serial_poll_rx; |
| 971 | #endif |
| 972 | |
| 973 | dev->open_flag |= RT_SERIAL_RX_BLOCKING; |
| 974 | return RT_EOK; |
| 975 | } |
| 976 | /* Limits the minimum value of rx_bufsz */ |
| 977 | if (serial->config.rx_bufsz < RT_SERIAL_RX_MINBUFSZ) |
| 978 | serial->config.rx_bufsz = RT_SERIAL_RX_MINBUFSZ; |
| 979 | |
| 980 | #ifdef RT_SERIAL_USING_DMA |
| 981 | if (serial->config.dma_ping_bufsz < RT_SERIAL_RX_MINBUFSZ / 2) |
| 982 | serial->config.dma_ping_bufsz = RT_SERIAL_RX_MINBUFSZ / 2; |
| 983 | rx_fifo_size = sizeof(struct rt_serial_rx_fifo) + serial->config.rx_bufsz + serial->config.dma_ping_bufsz; |
| 984 | #else |
| 985 | rx_fifo_size = sizeof(struct rt_serial_rx_fifo) + serial->config.rx_bufsz; |
| 986 | #endif |
| 987 | |
| 988 | rx_fifo = (struct rt_serial_rx_fifo *)rt_malloc(rx_fifo_size); |
| 989 | RT_ASSERT(rx_fifo != RT_NULL); |
| 990 | rt_memset(rx_fifo, RT_NULL, rx_fifo_size); |
| 991 | |
| 992 | rt_ringbuffer_init(&rx_fifo->rb, |
| 993 | (rt_uint8_t *)rx_fifo + sizeof(struct rt_serial_rx_fifo), |
| 994 | serial->config.rx_bufsz); |
| 995 | |
| 996 | #ifdef RT_SERIAL_USING_DMA |
| 997 | rt_ringbuffer_init(&rx_fifo->dma_ping_rb, |
| 998 | (rt_uint8_t *)rx_fifo + sizeof(struct rt_serial_rx_fifo) + serial->config.rx_bufsz, |
| 999 | serial->config.dma_ping_bufsz); |
| 1000 | #endif |
| 1001 | |
| 1002 | serial->serial_rx = rx_fifo; |
| 1003 | |
| 1004 | #ifndef RT_USING_DEVICE_OPS |
| 1005 | dev->read = _serial_fifo_rx; |
| 1006 | #endif |
no test coverage detected