已合并
【bdb7c1d2f4f09a47e729b24135d73d72】支持Gpio对象CSR初始电平(InitialLevel)配置 #1370
liuyanyu09创建于 26 天前
【bdb7c1d2f4f09a47e729b24135d73d72】支持Gpio对象CSR初始电平(InitialLevel)配置 #1370
已合并
共 10 个文件变更+283-25
| @@ -1,5 +1,8 @@ | |||
| 1 | # Changelog | 1 | # Changelog |
| 2 | All notable changes to this project will be documented in this file. | 2 | All notable changes to this project will be documented in this file. |
| 3 | +## [1.2.544] - 2026-10-23 | ||
| 4 | +- feat(gpio): 新增 CSR 初始电平(InitialLevel)配置:仅输出方向单管脚模式(Id!=255)生效,输入方向忽略并告警(不校验取值),组模式(Id==255)不解析该属性、完全不受影响;未配置(属性回落无效默认值 0xFF,与显式配置 0 区分)不写管脚,输出方向配置非法值或下发失败均记 error 日志并跳过下发(不阻断初始化);补充极性反转(含 0 值反转)、组模式、输入方向(含非法值忽略)与写失败的单元测试。 | ||
| 5 | + | ||
| 3 | ## [1.2.543] - 2026-10-23 | 6 | ## [1.2.543] - 2026-10-23 |
| 4 | - feat(ssu): 支持设置ssu的UB端口速率 | 7 | - feat(ssu): 支持设置ssu的UB端口速率 |
| 5 | 8 | ||
| @@ -39,6 +39,6 @@ bool Gpio_object::init(mc::mutable_dict& csr_object, const mc::dict&) | |||
| 39 | 39 | ||
| 40 | MC_REFLECT(dev::csr1::Gpio_object, | 40 | MC_REFLECT(dev::csr1::Gpio_object, |
| 41 | ((m_interface_bus, "bmc.kepler.Bus"))((Id, "Id"))((Direction, "Direction"))((ReverseBit, "ReverseBit"))( | 41 | ((m_interface_bus, "bmc.kepler.Bus"))((Id, "Id"))((Direction, "Direction"))((ReverseBit, "ReverseBit"))( |
| 42 | - (Bit0GpioNum, "Bit0GpioNum"))((Bit1GpioNum, "Bit1GpioNum"))((Bit2GpioNum, "Bit2GpioNum"))( | 42 | + (InitialLevel, "InitialLevel"))((Bit0GpioNum, "Bit0GpioNum"))((Bit1GpioNum, "Bit1GpioNum"))( |
| 43 | - (Bit3GpioNum, "Bit3GpioNum"))((Bit4GpioNum, "Bit4GpioNum"))((Bit5GpioNum, "Bit5GpioNum"))( | 43 | + (Bit2GpioNum, "Bit2GpioNum"))((Bit3GpioNum, "Bit3GpioNum"))((Bit4GpioNum, "Bit4GpioNum"))( |
| 44 | - (Bit6GpioNum, "Bit6GpioNum"))((Bit7GpioNum, "Bit7GpioNum"))) | 44 | + (Bit5GpioNum, "Bit5GpioNum"))((Bit6GpioNum, "Bit6GpioNum"))((Bit7GpioNum, "Bit7GpioNum"))) |
| @@ -28,6 +28,7 @@ public: | |||
| 28 | mc::engine::property<uint8_t> Id{0}; | 28 | mc::engine::property<uint8_t> Id{0}; |
| 29 | mc::engine::property<uint8_t> Direction{0}; | 29 | mc::engine::property<uint8_t> Direction{0}; |
| 30 | mc::engine::property<uint8_t> ReverseBit{0}; | 30 | mc::engine::property<uint8_t> ReverseBit{0}; |
| 31 | + mc::engine::property<uint8_t> InitialLevel{0xFF}; // 0xFF 表示未配置(无效默认值,区分显式配置 0) | ||
| 31 | mc::engine::property<uint8_t> Bit0GpioNum{0}; | 32 | mc::engine::property<uint8_t> Bit0GpioNum{0}; |
| 32 | mc::engine::property<uint8_t> Bit1GpioNum{0}; | 33 | mc::engine::property<uint8_t> Bit1GpioNum{0}; |
| 33 | mc::engine::property<uint8_t> Bit2GpioNum{0}; | 34 | mc::engine::property<uint8_t> Bit2GpioNum{0}; |
| @@ -29,6 +29,16 @@ bool bus_gpio::is_group_mode() const | |||
| 29 | return m_id == GPIO_GROUP_MODE_ID; | 29 | return m_id == GPIO_GROUP_MODE_ID; |
| 30 | } | 30 | } |
| 31 | 31 | ||
| 32 | +uint8_t bus_gpio::apply_output_pol_reversal(uint8_t level) const | ||
| 33 | +{ | ||
| 34 | + return (m_output_pol_reversal != 0) ? static_cast<uint8_t>(level == 0 ? 1 : 0) : level; | ||
| 35 | +} | ||
| 36 | + | ||
| 37 | +uint8_t bus_gpio::apply_input_pol_reversal(uint8_t level) const | ||
| 38 | +{ | ||
| 39 | + return (m_input_pol_reversal != 0) ? static_cast<uint8_t>(level == 0 ? 1 : 0) : level; | ||
| 40 | +} | ||
| 41 | + | ||
| 32 | bool bus_gpio::init(mc::mutable_dict& csr_object) | 42 | bool bus_gpio::init(mc::mutable_dict& csr_object) |
| 33 | { | 43 | { |
| 34 | try { | 44 | try { |
| @@ -59,6 +69,31 @@ bool bus_gpio::init(mc::mutable_dict& csr_object) | |||
| 59 | 69 | ||
| 60 | if (!is_group_mode()) { | 70 | if (!is_group_mode()) { |
| 61 | m_gpio_drv->init(m_id, m_direction); | 71 | m_gpio_drv->init(m_id, m_direction); |
| 72 | + // InitialLevel 仅输出方向会真正使用:合法性校验只针对输出方向, | ||
| 73 | + // 输入方向无论配置什么都忽略并告警,与「输入忽略」语义保持一致; | ||
| 74 | + // 是否配置由 csr_object 是否含 InitialLevel 字段决定,不再用 0xFF 哨兵区分, | ||
| 75 | + // 避免显式配置 255 被静默当作未配置 | ||
| 76 | + if (csr_object.contains("InitialLevel")) { | ||
| 77 | + uint8_t initial_level = csr_object["InitialLevel"].as<uint8_t>(); | ||
| 78 | + if (m_direction != GPIO_DIRECTION_OUTPUT) { | ||
| 79 | + wlog("Gpio InitialLevel ignored for input pin, object: ${object}, id: ${id}, level: ${level}", | ||
| 80 | + ("object", m_object_name)("id", m_id)("level", initial_level)); | ||
| 81 | + } else if (initial_level > 1) { | ||
| 82 | + // 配置异常只记 error 日志并跳过下发,不阻断器件初始化;0xFF 亦属非法值 | ||
| 83 | + elog("Invalid Gpio InitialLevel, object: ${object}, id: ${id}, level: ${level}, " | ||
| 84 | + "expect 0 or 1, skip applying", | ||
| 85 | + ("object", m_object_name)("id", m_id)("level", initial_level)); | ||
| 86 | + } else { | ||
| 87 | + // InitialLevel 为逻辑电平,下发前按 ReverseBit 输出极化转换为物理电平; | ||
| 88 | + // 下发失败只记 error 日志,不阻断器件初始化 | ||
| 89 | + try { | ||
| 90 | + m_gpio_drv->write(m_id, apply_output_pol_reversal(initial_level)); | ||
| 91 | + } catch (const std::exception& e) { | ||
| 92 | + elog("Gpio InitialLevel apply failed, object: ${object}, id: ${id}, error: ${error}", | ||
| 93 | + ("object", m_object_name)("id", m_id)("error", e.what())); | ||
| 94 | + } | ||
| 95 | + } | ||
| 96 | + } | ||
| 62 | } else { | 97 | } else { |
| 63 | for (int i = 0; i < 8; ++i) { | 98 | for (int i = 0; i < 8; ++i) { |
| 64 | if (m_gpio_group[i] != GPIO_INVALID_PIN) { | 99 | if (m_gpio_group[i] != GPIO_INVALID_PIN) { |
| @@ -70,7 +105,8 @@ bool bus_gpio::init(mc::mutable_dict& csr_object) | |||
| 70 | return true; | 105 | return true; |
| 71 | } catch (const std::exception& e) { | 106 | } catch (const std::exception& e) { |
| 72 | m_gpio_drv.reset(); | 107 | m_gpio_drv.reset(); |
| 73 | - elog("Gpio init failed, exception: ${exception}", ("exception", e.what())); | 108 | + elog("Gpio init failed, object: ${object}, id: ${id}, exception: ${exception}", |
| 109 | + ("object", m_object_name)("id", m_id)("exception", e.what())); | ||
| 74 | return false; | 110 | return false; |
| 75 | } | 111 | } |
| 76 | } | 112 | } |
| @@ -110,10 +146,7 @@ uint32_t bus_gpio::read(ACCESS_DATA_S& input) | |||
| 110 | if (is_group_mode()) { | 146 | if (is_group_mode()) { |
| 111 | value = gpio_group_read(); | 147 | value = gpio_group_read(); |
| 112 | } else { | 148 | } else { |
| 113 | - value = m_gpio_drv->read(m_id, m_direction); | 149 | + value = apply_input_pol_reversal(m_gpio_drv->read(m_id, m_direction)); |
| 114 | - if (m_input_pol_reversal != 0) { | ||
| 115 | - value = (value == 0) ? 1 : 0; | ||
| 116 | - } | ||
| 117 | } | 150 | } |
| 118 | // 将读取的值存入 out_buffer,返回状态码 | 151 | // 将读取的值存入 out_buffer,返回状态码 |
| 119 | input.out_buffer.clear(); | 152 | input.out_buffer.clear(); |
| @@ -143,10 +176,7 @@ uint32_t bus_gpio::write(ACCESS_DATA_S& input) | |||
| 143 | return HAS_OK; | 176 | return HAS_OK; |
| 144 | } | 177 | } |
| 145 | 178 | ||
| 146 | - if (m_output_pol_reversal != 0) { | 179 | + m_gpio_drv->write(m_id, apply_output_pol_reversal(data)); |
| 147 | - data = (data == 0) ? 1 : 0; | ||
| 148 | - } | ||
| 149 | - m_gpio_drv->write(m_id, data); | ||
| 150 | return HAS_OK; | 180 | return HAS_OK; |
| 151 | } catch (const std::exception& e) { | 181 | } catch (const std::exception& e) { |
| 152 | MC_THROW(mc::invalid_op_exception, "GPIO write failed, object: ${object}, id: ${id}, error: ${error}", | 182 | MC_THROW(mc::invalid_op_exception, "GPIO write failed, object: ${object}, id: ${id}, error: ${error}", |
| @@ -55,6 +55,12 @@ private: | |||
| 55 | std::unique_ptr<DRIVER_GPIO::Gpio> m_gpio_drv; | 55 | std::unique_ptr<DRIVER_GPIO::Gpio> m_gpio_drv; |
| 56 | 56 | ||
| 57 | bool is_group_mode() const; | 57 | bool is_group_mode() const; |
| 58 | + | ||
| 59 | + // 输出方向:按输出极性反转位(ReverseBit bit1)将逻辑电平转换为物理电平;仅单管脚路径使用 | ||
| 60 | + uint8_t apply_output_pol_reversal(uint8_t level) const; | ||
| 61 | + | ||
| 62 | + // 输入方向:按输入极性反转位(ReverseBit bit0)将物理电平转换为逻辑电平;仅单管脚路径使用 | ||
| 63 | + uint8_t apply_input_pol_reversal(uint8_t level) const; | ||
| 58 | }; | 64 | }; |
| 59 | 65 | ||
| 60 | } // namespace dev | 66 | } // namespace dev |
| @@ -3,7 +3,7 @@ | |||
| 3 | "type": "application", | 3 | "type": "application", |
| 4 | "language": "cpp", | 4 | "language": "cpp", |
| 5 | "deployConfig": "", | 5 | "deployConfig": "", |
| 6 | - "version": "1.2.543", | 6 | + "version": "1.2.544", |
| 7 | "license": "", | 7 | "license": "", |
| 8 | "description": "", | 8 | "description": "", |
| 9 | "options": { | 9 | "options": { |
| @@ -52,7 +52,9 @@ uint8_t Gpio::read(uint8_t gpio_num, uint8_t) | |||
| 52 | 52 | ||
| 53 | void Gpio::write(uint8_t gpio_num, uint8_t value) | 53 | void Gpio::write(uint8_t gpio_num, uint8_t value) |
| 54 | { | 54 | { |
| 55 | - g_state.last_gpio_num = gpio_num; | 55 | + g_state.last_gpio_num = gpio_num; |
| 56 | + g_state.last_write_value = value; | ||
| 57 | + ++g_state.write_count; | ||
| 56 | if (g_state.fail_write) { | 58 | if (g_state.fail_write) { |
| 57 | MC_THROW(mc::invalid_op_exception, "stub gpio write failed"); | 59 | MC_THROW(mc::invalid_op_exception, "stub gpio write failed"); |
| 58 | } | 60 | } |
| @@ -20,15 +20,17 @@ | |||
| 20 | namespace DRIVER_GPIO { | 20 | namespace DRIVER_GPIO { |
| 21 | 21 | ||
| 22 | struct TestState { | 22 | struct TestState { |
| 23 | - bool fail_init = false; | 23 | + bool fail_init = false; |
| 24 | - bool fail_read = false; | 24 | + bool fail_read = false; |
| 25 | - bool fail_write = false; | 25 | + bool fail_write = false; |
| 26 | - uint8_t next_value = 0; | 26 | + uint8_t next_value = 0; |
| 27 | - uint8_t last_gpio_num = 0xFF; | 27 | + uint8_t last_gpio_num = 0xFF; |
| 28 | - uint8_t last_direction = 0; | 28 | + uint8_t last_direction = 0; |
| 29 | - std::array<uint8_t, 8> gpio_values = {0, 0, 0, 0, 0, 0, 0, 0}; | 29 | + uint8_t last_write_value = 0xFF; |
| 30 | - int lock_count = 0; | 30 | + int write_count = 0; |
| 31 | - int unlock_count = 0; | 31 | + std::array<uint8_t, 8> gpio_values = {0, 0, 0, 0, 0, 0, 0, 0}; |
| 32 | + int lock_count = 0; | ||
| 33 | + int unlock_count = 0; | ||
| 32 | }; | 34 | }; |
| 33 | 35 | ||
| 34 | TestState& test_state(); | 36 | TestState& test_state(); |
| @@ -512,10 +512,46 @@ TEST_F(Csr1GpioAbiTest, same_gpio_claimed_by_two_buses_without_conflict_detectio | |||
| 512 | TEST(Csr1GpioReflectionTest, private_properties_are_reflected) | 512 | TEST(Csr1GpioReflectionTest, private_properties_are_reflected) |
| 513 | { | 513 | { |
| 514 | dev::csr1::Gpio_object object; | 514 | dev::csr1::Gpio_object object; |
| 515 | - for (const auto* name : {"Id", "Direction", "ReverseBit", "Bit0GpioNum", "Bit1GpioNum", "Bit2GpioNum", | 515 | + for (const auto* name : |
| 516 | - "Bit3GpioNum", "Bit4GpioNum", "Bit5GpioNum", "Bit6GpioNum", "Bit7GpioNum"}) { | 516 | + {"Id", "Direction", "ReverseBit", "InitialLevel", "Bit0GpioNum", "Bit1GpioNum", "Bit2GpioNum", "Bit3GpioNum", |
| 517 | + "Bit4GpioNum", "Bit5GpioNum", "Bit6GpioNum", "Bit7GpioNum"}) { | ||
| 517 | EXPECT_TRUE(object.has_property(name, {})); | 518 | EXPECT_TRUE(object.has_property(name, {})); |
| 518 | } | 519 | } |
| 519 | } | 520 | } |
| 520 | 521 | ||
| 522 | +// InitialLevel 经 MC_REFLECT 注册后,应由 mc::from_variant 解析到对象属性上 | ||
| 523 | +TEST_F(Csr1GpioAbiTest, init_parses_initial_level_property) | ||
| 524 | +{ | ||
| 525 | + auto csr = create_bus_csr("initial_level_parse"); | ||
| 526 | + csr["InitialLevel"] = static_cast<uint8_t>(1); | ||
| 527 | + auto bus = dev::device_manager::get_instance().create_bus("initial_level_parse", "Gpio", csr); | ||
| 528 | + ASSERT_NE(bus, nullptr); | ||
| 529 | + | ||
| 530 | + auto device = m_driver->ctor(m_service.get(), "initial_level_gpio"); | ||
| 531 | + ASSERT_NE(device, nullptr); | ||
| 532 | + | ||
| 533 | + auto connector = create_connector(); | ||
| 534 | + ASSERT_EQ(m_driver->init(device, &csr, &connector), STATUS_OK); | ||
| 535 | + | ||
| 536 | + auto* gpio_obj = static_cast<dev::csr1::Gpio_object*>(device); | ||
| 537 | + EXPECT_EQ(gpio_obj->InitialLevel.get_value().as<uint8_t>(), 1); | ||
| 538 | +} | ||
🔵 Low Priority 本 diff 在 tests/drivers/csr1/bus/gpio/test_gpio_abi.cpp 新增 证据链:
建议:确认 tests/drivers/csr1/bus/gpio/meson.build 中 ![]() ![]() 不准确? | |||
| 539 | + | ||
| 540 | +// CSR 未配置时属性回落无效默认值 0xFF,用于区分「未配置」与「显式配置 0」 | ||
| 541 | +TEST_F(Csr1GpioAbiTest, initial_level_defaults_to_invalid_when_absent) | ||
| 542 | +{ | ||
| 543 | + auto csr = create_bus_csr("initial_level_default"); | ||
| 544 | + auto bus = dev::device_manager::get_instance().create_bus("initial_level_default", "Gpio", csr); | ||
| 545 | + ASSERT_NE(bus, nullptr); | ||
| 546 | + | ||
| 547 | + auto device = m_driver->ctor(m_service.get(), "initial_level_default_gpio"); | ||
| 548 | + ASSERT_NE(device, nullptr); | ||
| 549 | + | ||
| 550 | + auto connector = create_connector(); | ||
| 551 | + ASSERT_EQ(m_driver->init(device, &csr, &connector), STATUS_OK); | ||
| 552 | + | ||
| 553 | + auto* gpio_obj = static_cast<dev::csr1::Gpio_object*>(device); | ||
| 554 | + EXPECT_EQ(gpio_obj->InitialLevel.get_value().as<uint8_t>(), 0xFF); | ||
| 555 | +} | ||
| 556 | + | ||
| 521 | } // namespace | 557 | } // namespace |


🔵 Low Priority
新增的 InitialLevel 处理块整体位于
if (!is_group_mode())分支内(第 74~85 行),组模式分支(第 86~92 行)对has_initial_level不做任何处理。证据链:
建议:在组模式分支(
} else {之后、循环之前)补一条与输入方向分支对称的告警,例如:if (has_initial_level) { wlog("Gpio InitialLevel ignored in group mode, object: ${object}, id: ${id}", ("object", m_object_name)("id", m_id)); },使配置被丢弃时可从日志定位。