* @brief Enable serial transmit 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. */
| 811 | * @return Return the status of the operation. |
| 812 | */ |
| 813 | static rt_err_t rt_serial_tx_enable(struct rt_device *dev, |
| 814 | rt_uint16_t tx_oflag) |
| 815 | { |
| 816 | struct rt_serial_device *serial; |
| 817 | struct rt_serial_tx_fifo *tx_fifo = RT_NULL; |
| 818 | rt_err_t control_result = RT_EOK; |
| 819 | |
| 820 | RT_ASSERT(dev != RT_NULL); |
| 821 | serial = (struct rt_serial_device *)dev; |
| 822 | |
| 823 | if (serial->config.tx_bufsz == 0) |
| 824 | { |
| 825 | /* Cannot use RT_SERIAL_TX_NON_BLOCKING when tx_bufsz is 0 */ |
| 826 | if (tx_oflag == RT_SERIAL_TX_NON_BLOCKING) |
| 827 | { |
| 828 | LOG_E("(%s) serial device with misconfigure: tx_bufsz = 0", |
| 829 | dev->parent.name); |
| 830 | return -RT_EINVAL; |
| 831 | } |
| 832 | |
| 833 | #ifndef RT_USING_DEVICE_OPS |
| 834 | dev->write = _serial_poll_tx; |
| 835 | #endif |
| 836 | |
| 837 | dev->open_flag |= RT_SERIAL_TX_BLOCKING; |
| 838 | return RT_EOK; |
| 839 | } |
| 840 | /* Limits the minimum value of tx_bufsz */ |
| 841 | if (serial->config.tx_bufsz < RT_SERIAL_TX_MINBUFSZ) |
| 842 | serial->config.tx_bufsz = RT_SERIAL_TX_MINBUFSZ; |
| 843 | |
| 844 | if (tx_oflag == RT_SERIAL_TX_BLOCKING) |
| 845 | { |
| 846 | /* When using RT_SERIAL_TX_BLOCKING, it is necessary to determine |
| 847 | * whether serial device needs to use buffer */ |
| 848 | /* Call the Control() API to get the operating mode */ |
| 849 | control_result = serial->ops->control(serial, |
| 850 | RT_DEVICE_CHECK_OPTMODE, |
| 851 | (void *)RT_DEVICE_FLAG_TX_BLOCKING); |
| 852 | if (control_result < 0) |
| 853 | { |
| 854 | return control_result; |
| 855 | } |
| 856 | |
| 857 | rt_err_t optmode = control_result; |
| 858 | if (optmode == RT_SERIAL_TX_BLOCKING_BUFFER) |
| 859 | { |
| 860 | /* If use RT_SERIAL_TX_BLOCKING_BUFFER, the ringbuffer is initialized */ |
| 861 | tx_fifo = (struct rt_serial_tx_fifo *)rt_malloc(sizeof(struct rt_serial_tx_fifo) + serial->config.tx_bufsz); |
| 862 | RT_ASSERT(tx_fifo != RT_NULL); |
| 863 | rt_memset(tx_fifo, RT_NULL, sizeof(struct rt_serial_tx_fifo) + serial->config.tx_bufsz); |
| 864 | rt_ringbuffer_init(&tx_fifo->rb, |
| 865 | (rt_uint8_t *)tx_fifo + sizeof(struct rt_serial_tx_fifo), |
| 866 | serial->config.tx_bufsz); |
| 867 | serial->serial_tx = tx_fifo; |
| 868 | |
| 869 | #ifndef RT_USING_DEVICE_OPS |
| 870 | dev->write = _serial_fifo_tx_blocking_buf; |
no test coverage detected