|
| 1 | +/* |
| 2 | + * Copyright (c) 2006-2022, RT-Thread Development Team |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Change Logs: |
| 7 | + * Date Author Notes |
| 8 | + * 2022-11-16 GuEe-GUI first version |
| 9 | + */ |
| 10 | + |
| 11 | +#include <rtatomic.h> |
| 12 | +#include "serial_dm.h" |
| 13 | + |
| 14 | +int serial_dev_set_name(struct rt_serial_device *sdev) |
| 15 | +{ |
| 16 | + int id = -1; |
| 17 | + static int uid_min = -1; |
| 18 | + static volatile rt_atomic_t uid = 0; |
| 19 | + |
| 20 | + RT_ASSERT(sdev != RT_NULL); |
| 21 | + |
| 22 | +#ifdef RT_USING_OFW |
| 23 | + if (sdev->parent.ofw_node) |
| 24 | + { |
| 25 | + id = rt_ofw_get_alias_id(sdev->parent.ofw_node, "serial"); |
| 26 | + |
| 27 | + if (id < 0) |
| 28 | + { |
| 29 | + id = rt_ofw_get_alias_id(sdev->parent.ofw_node, "uart"); |
| 30 | + } |
| 31 | + |
| 32 | + if (uid_min < 0) |
| 33 | + { |
| 34 | + uid_min = rt_ofw_get_alias_last_id("serial"); |
| 35 | + |
| 36 | + if (uid_min < 0) |
| 37 | + { |
| 38 | + uid_min = rt_ofw_get_alias_last_id("uart"); |
| 39 | + } |
| 40 | + |
| 41 | + uid_min = uid_min < 0 ? 0 : (uid_min + 1); |
| 42 | + |
| 43 | + rt_hw_atomic_store(&uid, uid_min); |
| 44 | + } |
| 45 | + } |
| 46 | +#endif |
| 47 | + |
| 48 | + if (id < 0) |
| 49 | + { |
| 50 | + id = (int)rt_hw_atomic_add(&uid, 1); |
| 51 | + } |
| 52 | + |
| 53 | + return rt_dm_dev_set_name(&sdev->parent, "uart%u", id); |
| 54 | +} |
| 55 | + |
| 56 | +void *serial_base_from_args(char *str) |
| 57 | +{ |
| 58 | + rt_ubase_t base = 0; |
| 59 | + |
| 60 | + while (*str && !(*str == 'x' || *str == 'X')) |
| 61 | + { |
| 62 | + ++str; |
| 63 | + } |
| 64 | + ++str; |
| 65 | + |
| 66 | + /* The str may get from bootargs that we need check it */ |
| 67 | + while (*str && *str != ',' && *str != ' ') |
| 68 | + { |
| 69 | + base <<= 4; |
| 70 | + |
| 71 | + if ((*str >= 'a' && *str <= 'f') || (*str >= 'A' && *str <= 'F')) |
| 72 | + { |
| 73 | + base += ((*str | ' ') - 'a') + 10; |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + base += *str - '0'; |
| 78 | + } |
| 79 | + |
| 80 | + ++str; |
| 81 | + } |
| 82 | + |
| 83 | + return (void *)base; |
| 84 | +} |
| 85 | + |
| 86 | +struct serial_configure serial_cfg_from_args(char *str) |
| 87 | +{ |
| 88 | + struct serial_configure cfg = RT_SERIAL_CONFIG_DEFAULT; |
| 89 | + |
| 90 | + /* Format baudrate/parity/bits/flow (BBBBPNF), Default is 115200n8 */ |
| 91 | + if (str && *str) |
| 92 | + { |
| 93 | + rt_uint32_t baudrate = 0; |
| 94 | + |
| 95 | + /* BBBB is the speed */ |
| 96 | + while (*str && (*str >= '0' && *str <= '9')) |
| 97 | + { |
| 98 | + baudrate *= 10; |
| 99 | + baudrate += *str - '0'; |
| 100 | + ++str; |
| 101 | + } |
| 102 | + |
| 103 | + if (baudrate) |
| 104 | + { |
| 105 | + cfg.baud_rate = baudrate; |
| 106 | + } |
| 107 | + |
| 108 | + /* P is parity (n/o/e) */ |
| 109 | + switch (*str) |
| 110 | + { |
| 111 | + case 'n': |
| 112 | + cfg.parity = PARITY_NONE; |
| 113 | + break; |
| 114 | + case 'o': |
| 115 | + cfg.parity = PARITY_ODD; |
| 116 | + break; |
| 117 | + case 'e': |
| 118 | + cfg.parity = PARITY_EVEN; |
| 119 | + break; |
| 120 | + default: |
| 121 | + --str; |
| 122 | + break; |
| 123 | + } |
| 124 | + ++str; |
| 125 | + |
| 126 | + /* N is number of bits */ |
| 127 | + if (*str && (*str >= '0' && *str <= '9')) |
| 128 | + { |
| 129 | + cfg.data_bits = *str; |
| 130 | + ++str; |
| 131 | + } |
| 132 | + |
| 133 | + /* F is flow ontrol ('r' for RTS) */ |
| 134 | + if (*str) |
| 135 | + { |
| 136 | + cfg.flowcontrol = (*str == 'r' ? RT_SERIAL_FLOWCONTROL_CTSRTS : RT_SERIAL_FLOWCONTROL_NONE); |
| 137 | + ++str; |
| 138 | + } |
| 139 | + |
| 140 | + #ifdef RT_USING_OFW |
| 141 | + if (*str == '\0') |
| 142 | + { |
| 143 | + const char earlycon_magic[] = { 'O', 'F', 'W', '\0' }; |
| 144 | + |
| 145 | + if (!rt_strcmp(++str, earlycon_magic)) |
| 146 | + { |
| 147 | + /* Is OFW earlycon, we should ACK it */ |
| 148 | + rt_memset(str, 0, RT_ARRAY_SIZE(earlycon_magic)); |
| 149 | + } |
| 150 | + } |
| 151 | + #endif |
| 152 | + } |
| 153 | + |
| 154 | + return cfg; |
| 155 | +} |
0 commit comments