|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Pavithra CP, BeagleBoard.org |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include "greybus/greybus_messages.h" |
| 8 | +#include "greybus/greybus_protocols.h" |
| 9 | +#include <zephyr/ztest.h> |
| 10 | +#include <greybus/greybus.h> |
| 11 | +#include <zephyr/kernel.h> |
| 12 | +#include <zephyr/device.h> |
| 13 | +#include <zephyr/sys/byteorder.h> |
| 14 | +#include <zephyr/logging/log.h> |
| 15 | + |
| 16 | +LOG_MODULE_REGISTER(greybus_pwm_test, LOG_LEVEL_INF); |
| 17 | + |
| 18 | +struct gb_msg_with_cport gb_transport_get_message(void); |
| 19 | + |
| 20 | +static void *pwm_setup(void) |
| 21 | +{ |
| 22 | + struct gb_msg_with_cport resp; |
| 23 | + struct gb_control_version_request *ver_req; |
| 24 | + struct gb_message *msg; |
| 25 | + |
| 26 | + /* |
| 27 | + * Greybus Control Protocol Handshake. |
| 28 | + * We (the Test/Host) must send a Version Request to transition the |
| 29 | + * Greybus service from 'Initialized' to 'Active'. |
| 30 | + * Without this, the stack may reject requests on other CPorts. |
| 31 | + */ |
| 32 | + msg = gb_message_request_alloc(sizeof(*ver_req), GB_CONTROL_TYPE_VERSION, false); |
| 33 | + ver_req = (struct gb_control_version_request *)msg->payload; |
| 34 | + ver_req->major = 0; |
| 35 | + ver_req->minor = 1; |
| 36 | + |
| 37 | + greybus_rx_handler(0, msg); |
| 38 | + resp = gb_transport_get_message(); |
| 39 | + zassert_not_null(resp.msg, "No version response received"); |
| 40 | + |
| 41 | + zassert_equal(resp.msg->header.result, GB_OP_SUCCESS, "Version handshake failed"); |
| 42 | + gb_message_dealloc(resp.msg); |
| 43 | + return NULL; |
| 44 | +} |
| 45 | + |
| 46 | +ZTEST_SUITE(greybus_pwm_tests, NULL, pwm_setup, NULL, NULL, NULL); |
| 47 | + |
| 48 | +/*CPort 1 is assigned to the first bundle (PWM) by the Greybus Manifest */ |
| 49 | +static const int pwm_cport = 1; |
| 50 | + |
| 51 | +ZTEST(greybus_pwm_tests, test_pwm_count) |
| 52 | +{ |
| 53 | + struct gb_msg_with_cport resp; |
| 54 | + struct gb_message *req = gb_message_request_alloc(0, GB_PWM_TYPE_PWM_COUNT, false); |
| 55 | + |
| 56 | + greybus_rx_handler(pwm_cport, req); |
| 57 | + resp = gb_transport_get_message(); |
| 58 | + |
| 59 | + /*transport check */ |
| 60 | + if (resp.msg == NULL) { |
| 61 | + zassert_not_null(resp.msg, "Greybus stack did not respond on CPort %d", pwm_cport); |
| 62 | + } |
| 63 | + zassert_equal(resp.cport, pwm_cport, "Response received on wrong CPort"); |
| 64 | + |
| 65 | + zassert_equal(resp.msg->header.type, GB_RESPONSE(GB_PWM_TYPE_PWM_COUNT), |
| 66 | + "Wrong response type"); |
| 67 | + zassert_equal(resp.msg->header.result, GB_OP_SUCCESS, "Operation failed with error: %d", |
| 68 | + resp.msg->header.result); |
| 69 | + |
| 70 | + struct gb_pwm_count_response *pwm_resp = (struct gb_pwm_count_response *)resp.msg->payload; |
| 71 | + |
| 72 | + /*The vnd,pwm driver on native_sim might report 0 channels depending on the overlay |
| 73 | + * configuration. We verify that the value is readable (greater than 0).*/ |
| 74 | + LOG_INF("PWM Driver reported %d channels", pwm_resp->count); |
| 75 | + |
| 76 | + zassert_true(pwm_resp->count >= 0, "PWM driver returned invalid count!"); |
| 77 | + |
| 78 | + gb_message_dealloc(resp.msg); |
| 79 | +} |
| 80 | + |
| 81 | +ZTEST(greybus_pwm_tests, test_pwm_config) |
| 82 | +{ |
| 83 | + struct gb_message *req; |
| 84 | + struct gb_msg_with_cport resp; |
| 85 | + struct gb_pwm_config_request *cfg_req; |
| 86 | + |
| 87 | + req = gb_message_request_alloc(sizeof(*cfg_req), GB_PWM_TYPE_CONFIG, false); |
| 88 | + cfg_req = (struct gb_pwm_config_request *)req->payload; |
| 89 | + |
| 90 | + /* Standard Test Config: 1 kHz Frequency, 50% Duty Cycle |
| 91 | + Standard for verify basic PWM operation. |
| 92 | + */ |
| 93 | + cfg_req->which = 0; |
| 94 | + cfg_req->duty = sys_cpu_to_le32(500000); |
| 95 | + cfg_req->period = sys_cpu_to_le32(1000000); |
| 96 | + |
| 97 | + greybus_rx_handler(pwm_cport, req); |
| 98 | + resp = gb_transport_get_message(); |
| 99 | + |
| 100 | + zassert_not_null(resp.msg, "No config response received"); |
| 101 | + zassert_equal(resp.msg->header.type, GB_RESPONSE(GB_PWM_TYPE_CONFIG), "Wrong Type"); |
| 102 | + |
| 103 | + if (resp.msg->header.result != GB_OP_SUCCESS) { |
| 104 | + LOG_WRN("Config failed (Expected if channel count is 0): %d", |
| 105 | + resp.msg->header.result); |
| 106 | + } else { |
| 107 | + zassert_equal(resp.msg->header.result, GB_OP_SUCCESS, "Config failed"); |
| 108 | + } |
| 109 | + |
| 110 | + gb_message_dealloc(resp.msg); |
| 111 | +} |
| 112 | + |
| 113 | +ZTEST(greybus_pwm_tests, test_pwm_enable) |
| 114 | +{ |
| 115 | + struct gb_message *req; |
| 116 | + struct gb_msg_with_cport resp; |
| 117 | + struct gb_pwm_enable_request *en_req; |
| 118 | + |
| 119 | + req = gb_message_request_alloc(sizeof(*en_req), GB_PWM_TYPE_ENABLE, false); |
| 120 | + en_req = (struct gb_pwm_enable_request *)req->payload; |
| 121 | + en_req->which = 0; /*enabling channel 0*/ |
| 122 | + |
| 123 | + greybus_rx_handler(pwm_cport, req); |
| 124 | + resp = gb_transport_get_message(); |
| 125 | + |
| 126 | + zassert_not_null(resp.msg, "No enable response received"); |
| 127 | + zassert_equal(resp.msg->header.type, GB_RESPONSE(GB_PWM_TYPE_ENABLE), "Wrong Type"); |
| 128 | + |
| 129 | + if (resp.msg->header.result != GB_OP_SUCCESS) { |
| 130 | + LOG_WRN("Enable failed (Expected if channel count is 0): %d", |
| 131 | + resp.msg->header.result); |
| 132 | + } else { |
| 133 | + zassert_equal(resp.msg->header.result, GB_OP_SUCCESS, "Enable failed"); |
| 134 | + } |
| 135 | + |
| 136 | + gb_message_dealloc(resp.msg); |
| 137 | +} |
| 138 | + |
| 139 | +ZTEST(greybus_pwm_tests, test_pwm_disable) |
| 140 | +{ |
| 141 | + struct gb_message *req; |
| 142 | + struct gb_msg_with_cport resp; |
| 143 | + struct gb_pwm_disable_request *dis_req; |
| 144 | + |
| 145 | + req = gb_message_request_alloc(sizeof(*dis_req), GB_PWM_TYPE_DISABLE, false); |
| 146 | + dis_req = (struct gb_pwm_disable_request *)req->payload; |
| 147 | + dis_req->which = 0; |
| 148 | + |
| 149 | + greybus_rx_handler(pwm_cport, req); |
| 150 | + resp = gb_transport_get_message(); |
| 151 | + |
| 152 | + zassert_not_null(resp.msg, "No disable response received"); |
| 153 | + zassert_equal(resp.msg->header.type, GB_RESPONSE(GB_PWM_TYPE_DISABLE), "Wrong Type"); |
| 154 | + |
| 155 | + /*soft warn/check for now*/ |
| 156 | + if (resp.msg->header.result != GB_OP_SUCCESS) { |
| 157 | + LOG_WRN("Disable failed (Expected if channel count is 0): %d", |
| 158 | + resp.msg->header.result); |
| 159 | + } else { |
| 160 | + zassert_equal(resp.msg->header.result, GB_OP_SUCCESS, "Disable failed"); |
| 161 | + } |
| 162 | + |
| 163 | + gb_message_dealloc(resp.msg); |
| 164 | +} |
| 165 | + |
| 166 | +ZTEST(greybus_pwm_tests, test_pwm_polarity) |
| 167 | +{ |
| 168 | + struct gb_message *req; |
| 169 | + struct gb_msg_with_cport resp; |
| 170 | + struct gb_pwm_polarity_request *pol_req; |
| 171 | + |
| 172 | + req = gb_message_request_alloc(sizeof(*pol_req), GB_PWM_TYPE_POLARITY, false); |
| 173 | + pol_req = (struct gb_pwm_polarity_request *)req->payload; |
| 174 | + pol_req->which = 0; |
| 175 | + pol_req->polarity = 1; /*Inversed*/ |
| 176 | + |
| 177 | + greybus_rx_handler(pwm_cport, req); |
| 178 | + resp = gb_transport_get_message(); |
| 179 | + |
| 180 | + zassert_not_null(resp.msg, "No polarity response received"); |
| 181 | + zassert_equal(resp.msg->header.type, GB_RESPONSE(GB_PWM_TYPE_POLARITY), "Wrong Type"); |
| 182 | + |
| 183 | + if (resp.msg->header.result != GB_OP_SUCCESS) { |
| 184 | + LOG_WRN("Polarity failed (Expected if channel count is 0): %d", |
| 185 | + resp.msg->header.result); |
| 186 | + } else { |
| 187 | + zassert_equal(resp.msg->header.result, GB_OP_SUCCESS, "Polarity failed"); |
| 188 | + } |
| 189 | + |
| 190 | + gb_message_dealloc(resp.msg); |
| 191 | +} |
0 commit comments