door_axis_controller.v 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. `timescale 1ns/1ps
  2. // Per-axis door percent FSM: cal seek-close → measure-open, then runtime
  3. // mid/endpoint motion with latest-wins replan. Position only on gated_step_rise.
  4. module door_axis_controller #(
  5. parameter integer AXIS_ID = 0,
  6. parameter integer MAX_CAL_STEPS = 5000,
  7. parameter integer MIN_TRAVEL = 2,
  8. parameter integer ENDPOINT_GUARD_EXTRA = 500,
  9. parameter integer DIR_SETUP_TICKS = 2,
  10. parameter integer PERCENT_MAX = 100
  11. ) (
  12. input wire clk,
  13. input wire rst_n,
  14. input wire open_limit,
  15. input wire close_limit,
  16. input wire limit_sync_valid,
  17. input wire gated_step,
  18. input wire gated_step_rise,
  19. input wire tick_500us,
  20. input wire request_valid,
  21. input wire [7:0] requested_percent_in,
  22. input wire watchdog_stop,
  23. output reg planner_req,
  24. output wire [7:0] planner_percent,
  25. output wire [12:0] planner_travel,
  26. input wire planner_done,
  27. input wire [1:0] planner_done_axis,
  28. input wire [7:0] planner_done_percent,
  29. input wire [12:0] planner_result,
  30. output reg step,
  31. output reg dir_close,
  32. output reg [2:0] status_state,
  33. output reg [7:0] status_flags,
  34. output reg [7:0] status_requested_percent,
  35. output reg [12:0] status_current,
  36. output reg [12:0] status_target,
  37. output reg [12:0] status_travel,
  38. output reg [7:0] status_fault_code,
  39. output wire calibrated,
  40. output wire faulted,
  41. output wire terminal
  42. );
  43. localparam [2:0] ST_CAL_SEEK_CLOSE = 3'd0;
  44. localparam [2:0] ST_CAL_MEASURE_OPEN = 3'd1;
  45. localparam [2:0] ST_IDLE = 3'd2;
  46. localparam [2:0] ST_MOVE_OPEN = 3'd3;
  47. localparam [2:0] ST_MOVE_CLOSE = 3'd4;
  48. localparam [2:0] ST_WAIT_REPLAN = 3'd5;
  49. localparam [2:0] ST_FAULT = 3'd6;
  50. localparam [2:0] FC_NONE = 3'd0;
  51. localparam [2:0] FC_BOTH_LIMITS = 3'd1;
  52. localparam [2:0] FC_CAL_CLOSE_TIMEOUT = 3'd2;
  53. localparam [2:0] FC_CAL_OPEN_TIMEOUT = 3'd3;
  54. localparam [2:0] FC_TINY_TRAVEL = 3'd4;
  55. localparam [2:0] FC_ENDPOINT_TIMEOUT = 3'd5;
  56. localparam [2:0] PH_NONE = 3'd0;
  57. localparam [2:0] PH_DIR_SETUP = 3'd1;
  58. localparam [2:0] PH_RUN = 3'd2;
  59. localparam [2:0] PH_DRAIN_HIGH = 3'd3;
  60. localparam integer CNT_W = 13;
  61. localparam [CNT_W-1:0] MAX_STEPS_W = MAX_CAL_STEPS[CNT_W-1:0];
  62. localparam [CNT_W-1:0] MIN_TRAVEL_W = MIN_TRAVEL[CNT_W-1:0];
  63. reg calibrated_r;
  64. reg [2:0] fault_code;
  65. reg [CNT_W-1:0] travel_steps;
  66. reg [CNT_W-1:0] current_position;
  67. reg [CNT_W-1:0] target_position;
  68. reg [7:0] requested_percent;
  69. reg replan_pending;
  70. reg endpoint_mode;
  71. reg target_valid;
  72. reg moving;
  73. reg [2:0] phase;
  74. reg [3:0] setup_count;
  75. reg [CNT_W-1:0] motion_count;
  76. reg allow_rise;
  77. reg stop_after_budget;
  78. reg cal_started;
  79. reg need_planner;
  80. reg wd_stopped;
  81. assign calibrated = calibrated_r;
  82. assign faulted = (status_state == ST_FAULT);
  83. assign terminal = ((status_state == ST_IDLE) && calibrated_r) || (status_state == ST_FAULT);
  84. assign planner_percent = requested_percent;
  85. assign planner_travel = travel_steps;
  86. wire both_limits = open_limit && close_limit;
  87. wire mid_request = (requested_percent > 8'd0) && (requested_percent < PERCENT_MAX[7:0]);
  88. wire is_open_endpoint = (requested_percent == 8'd0);
  89. wire is_close_endpoint = (requested_percent == PERCENT_MAX[7:0]);
  90. wire planner_accept = planner_done && (planner_done_axis == AXIS_ID[1:0]) &&
  91. (planner_done_percent == requested_percent) && calibrated_r &&
  92. (status_state != ST_FAULT) && mid_request && need_planner;
  93. wire [CNT_W-1:0] guard_limit = travel_steps + ENDPOINT_GUARD_EXTRA[CNT_W-1:0];
  94. wire in_calibration = (status_state == ST_CAL_SEEK_CLOSE) ||
  95. (status_state == ST_CAL_MEASURE_OPEN);
  96. always @* begin
  97. status_requested_percent = requested_percent;
  98. status_current = current_position;
  99. status_target = target_position;
  100. status_travel = travel_steps;
  101. status_fault_code = {5'd0, fault_code};
  102. status_flags = {
  103. 1'b0,
  104. replan_pending,
  105. target_valid,
  106. endpoint_mode,
  107. moving,
  108. calibrated_r,
  109. close_limit,
  110. open_limit
  111. };
  112. end
  113. // Shared STEP half-period helper: returns next step level
  114. function automatic step_toggle;
  115. input cur_step;
  116. input do_allow_rise;
  117. begin
  118. if (cur_step)
  119. step_toggle = 1'b0;
  120. else if (do_allow_rise)
  121. step_toggle = 1'b1;
  122. else
  123. step_toggle = 1'b0;
  124. end
  125. endfunction
  126. always @(posedge clk or negedge rst_n) begin
  127. if (!rst_n) begin
  128. status_state <= ST_CAL_SEEK_CLOSE;
  129. calibrated_r <= 1'b0;
  130. fault_code <= FC_NONE;
  131. travel_steps <= {CNT_W{1'b0}};
  132. current_position <= {CNT_W{1'b0}};
  133. target_position <= {CNT_W{1'b0}};
  134. requested_percent <= 8'd0;
  135. replan_pending <= 1'b0;
  136. endpoint_mode <= 1'b0;
  137. target_valid <= 1'b0;
  138. moving <= 1'b0;
  139. phase <= PH_NONE;
  140. setup_count <= 4'd0;
  141. motion_count <= {CNT_W{1'b0}};
  142. allow_rise <= 1'b0;
  143. stop_after_budget <= 1'b0;
  144. cal_started <= 1'b0;
  145. need_planner <= 1'b0;
  146. planner_req <= 1'b0;
  147. step <= 1'b0;
  148. dir_close <= 1'b1;
  149. wd_stopped <= 1'b0;
  150. end else if (both_limits) begin
  151. status_state <= ST_FAULT;
  152. fault_code <= FC_BOTH_LIMITS;
  153. step <= 1'b0;
  154. phase <= PH_NONE;
  155. moving <= 1'b0;
  156. allow_rise <= 1'b0;
  157. stop_after_budget <= 1'b0;
  158. planner_req <= 1'b0;
  159. need_planner <= 1'b0;
  160. replan_pending <= 1'b0;
  161. if (request_valid)
  162. requested_percent <= requested_percent_in;
  163. end else if (status_state == ST_FAULT) begin
  164. step <= 1'b0;
  165. moving <= 1'b0;
  166. planner_req <= 1'b0;
  167. if (request_valid)
  168. requested_percent <= requested_percent_in;
  169. end else begin
  170. // ----------------------------------------------------------
  171. // 1) Position / cal / guard counts on gated rise (always)
  172. // ----------------------------------------------------------
  173. if (gated_step_rise) begin
  174. if (status_state == ST_CAL_SEEK_CLOSE ||
  175. status_state == ST_CAL_MEASURE_OPEN) begin
  176. motion_count <= motion_count + 1'b1;
  177. end else if (calibrated_r) begin
  178. if (endpoint_mode)
  179. motion_count <= motion_count + 1'b1;
  180. if (dir_close) begin
  181. if (current_position < travel_steps)
  182. current_position <= current_position + 1'b1;
  183. end else begin
  184. if (current_position != {CNT_W{1'b0}})
  185. current_position <= current_position - 1'b1;
  186. end
  187. end
  188. end
  189. // ----------------------------------------------------------
  190. // 2) Host request (latest-wins). Preempts tick progression.
  191. // ----------------------------------------------------------
  192. if (request_valid && calibrated_r) begin
  193. // Resume only if new percent, after watchdog, or idle-not-satisfied.
  194. if ((requested_percent_in != requested_percent) ||
  195. wd_stopped ||
  196. ((status_state == ST_IDLE) && (
  197. ((requested_percent_in == 8'd0) && !open_limit) ||
  198. ((requested_percent_in == PERCENT_MAX[7:0]) && !close_limit) ||
  199. ((requested_percent_in > 8'd0) &&
  200. (requested_percent_in < PERCENT_MAX[7:0]) &&
  201. (current_position != target_position))
  202. ))) begin
  203. requested_percent <= requested_percent_in;
  204. wd_stopped <= 1'b0;
  205. replan_pending <= 1'b1;
  206. planner_req <= 1'b0;
  207. need_planner <= 1'b0;
  208. allow_rise <= 1'b0;
  209. // Same endpoint already in motion: do not reset guard/phase
  210. if ((requested_percent_in == requested_percent) &&
  211. endpoint_mode && moving &&
  212. ((status_state == ST_MOVE_OPEN) || (status_state == ST_MOVE_CLOSE)) &&
  213. !wd_stopped) begin
  214. // Idempotent heartbeat while correctly moving: keep going
  215. replan_pending <= 1'b0;
  216. end else begin
  217. // Materially new percent or forced replan: old target invalid
  218. target_valid <= 1'b0;
  219. if (gated_step || step) begin
  220. phase <= PH_DRAIN_HIGH;
  221. status_state <= ST_WAIT_REPLAN;
  222. end else begin
  223. step <= 1'b0;
  224. phase <= PH_NONE;
  225. moving <= 1'b0;
  226. status_state <= ST_WAIT_REPLAN;
  227. end
  228. end
  229. end else begin
  230. // Same target while already moving/planning/satisfied: no-op
  231. requested_percent <= requested_percent_in;
  232. wd_stopped <= 1'b0;
  233. end
  234. // ----------------------------------------------------------
  235. // 3) Watchdog graceful stop (request same cycle already handled)
  236. // Latch drain once; while PH_DRAIN_HIGH, fall through to §8 so
  237. // tick_500us can pull STEP low even if watchdog_stop stays high.
  238. // ----------------------------------------------------------
  239. end else if (watchdog_stop && calibrated_r && !in_calibration &&
  240. !wd_stopped &&
  241. !(status_state == ST_WAIT_REPLAN && phase == PH_DRAIN_HIGH)) begin
  242. allow_rise <= 1'b0;
  243. planner_req <= 1'b0;
  244. need_planner <= 1'b0;
  245. if (gated_step || step) begin
  246. phase <= PH_DRAIN_HIGH;
  247. status_state <= ST_WAIT_REPLAN;
  248. replan_pending <= 1'b0;
  249. end else begin
  250. step <= 1'b0;
  251. phase <= PH_NONE;
  252. moving <= 1'b0;
  253. status_state <= ST_IDLE;
  254. wd_stopped <= 1'b1;
  255. replan_pending <= 1'b0;
  256. end
  257. // ----------------------------------------------------------
  258. // 4) Planner result
  259. // ----------------------------------------------------------
  260. end else if (planner_accept) begin
  261. planner_req <= 1'b0;
  262. need_planner <= 1'b0;
  263. replan_pending <= 1'b0;
  264. target_position <= planner_result;
  265. target_valid <= 1'b1;
  266. endpoint_mode <= 1'b0;
  267. motion_count <= {CNT_W{1'b0}};
  268. step <= 1'b0;
  269. setup_count <= 4'd0;
  270. allow_rise <= 1'b0;
  271. stop_after_budget <= 1'b0;
  272. if (planner_result == current_position) begin
  273. status_state <= ST_IDLE;
  274. moving <= 1'b0;
  275. phase <= PH_NONE;
  276. end else if (planner_result > current_position) begin
  277. dir_close <= 1'b1;
  278. phase <= PH_DIR_SETUP;
  279. moving <= 1'b1;
  280. status_state <= ST_MOVE_CLOSE;
  281. end else begin
  282. dir_close <= 1'b0;
  283. phase <= PH_DIR_SETUP;
  284. moving <= 1'b1;
  285. status_state <= ST_MOVE_OPEN;
  286. end
  287. // ----------------------------------------------------------
  288. // 5) Calibration start
  289. // ----------------------------------------------------------
  290. end else if (!cal_started) begin
  291. if (limit_sync_valid) begin
  292. cal_started <= 1'b1;
  293. motion_count <= {CNT_W{1'b0}};
  294. setup_count <= 4'd0;
  295. allow_rise <= 1'b0;
  296. stop_after_budget <= 1'b0;
  297. step <= 1'b0;
  298. phase <= PH_DIR_SETUP;
  299. moving <= 1'b1;
  300. if (close_limit) begin
  301. dir_close <= 1'b0;
  302. status_state <= ST_CAL_MEASURE_OPEN;
  303. end else begin
  304. dir_close <= 1'b1;
  305. status_state <= ST_CAL_SEEK_CLOSE;
  306. end
  307. end
  308. // ----------------------------------------------------------
  309. // 6) CAL_SEEK_CLOSE
  310. // ----------------------------------------------------------
  311. end else if (status_state == ST_CAL_SEEK_CLOSE) begin
  312. if (close_limit) begin
  313. step <= 1'b0;
  314. allow_rise <= 1'b0;
  315. stop_after_budget <= 1'b0;
  316. motion_count <= {CNT_W{1'b0}};
  317. setup_count <= 4'd0;
  318. dir_close <= 1'b0;
  319. phase <= PH_DIR_SETUP;
  320. moving <= 1'b1;
  321. status_state <= ST_CAL_MEASURE_OPEN;
  322. end else if (stop_after_budget && !gated_step && !step) begin
  323. status_state <= ST_FAULT;
  324. fault_code <= FC_CAL_CLOSE_TIMEOUT;
  325. moving <= 1'b0;
  326. phase <= PH_NONE;
  327. end else begin
  328. // Use post-rise count: if count already at max, stop new rises
  329. if ((motion_count >= MAX_STEPS_W) ||
  330. (gated_step_rise && (motion_count + 1'b1 >= MAX_STEPS_W))) begin
  331. allow_rise <= 1'b0;
  332. stop_after_budget <= 1'b1;
  333. end
  334. if (phase == PH_DIR_SETUP) begin
  335. step <= 1'b0;
  336. if (tick_500us) begin
  337. if (setup_count + 1'b1 >= DIR_SETUP_TICKS[3:0]) begin
  338. phase <= PH_RUN;
  339. setup_count <= 4'd0;
  340. allow_rise <= !stop_after_budget && (motion_count < MAX_STEPS_W);
  341. end else begin
  342. setup_count <= setup_count + 1'b1;
  343. end
  344. end
  345. end else if (tick_500us) begin
  346. step <= step_toggle(step, allow_rise && !stop_after_budget &&
  347. (motion_count < MAX_STEPS_W));
  348. end
  349. end
  350. // ----------------------------------------------------------
  351. // 7) CAL_MEASURE_OPEN
  352. // ----------------------------------------------------------
  353. end else if (status_state == ST_CAL_MEASURE_OPEN) begin
  354. if (open_limit) begin
  355. // motion_count already includes this cycle's rise (NBA: see old+1)
  356. // Use effective count: if rise this cycle, +1 on current
  357. if (gated_step_rise) begin
  358. // effective = motion_count+1 (scheduled); compare with that
  359. if (((motion_count + 1'b1) >= MIN_TRAVEL_W) &&
  360. ((motion_count + 1'b1) <= MAX_STEPS_W)) begin
  361. travel_steps <= motion_count + 1'b1;
  362. current_position <= {CNT_W{1'b0}};
  363. target_position <= {CNT_W{1'b0}};
  364. requested_percent <= 8'd0;
  365. calibrated_r <= 1'b1;
  366. fault_code <= FC_NONE;
  367. target_valid <= 1'b1;
  368. endpoint_mode <= 1'b1;
  369. step <= 1'b0;
  370. phase <= PH_NONE;
  371. moving <= 1'b0;
  372. allow_rise <= 1'b0;
  373. stop_after_budget <= 1'b0;
  374. status_state <= ST_IDLE;
  375. end else begin
  376. status_state <= ST_FAULT;
  377. fault_code <= FC_TINY_TRAVEL;
  378. step <= 1'b0;
  379. moving <= 1'b0;
  380. phase <= PH_NONE;
  381. end
  382. end else begin
  383. if ((motion_count >= MIN_TRAVEL_W) &&
  384. (motion_count <= MAX_STEPS_W)) begin
  385. travel_steps <= motion_count;
  386. current_position <= {CNT_W{1'b0}};
  387. target_position <= {CNT_W{1'b0}};
  388. requested_percent <= 8'd0;
  389. calibrated_r <= 1'b1;
  390. fault_code <= FC_NONE;
  391. target_valid <= 1'b1;
  392. endpoint_mode <= 1'b1;
  393. step <= 1'b0;
  394. phase <= PH_NONE;
  395. moving <= 1'b0;
  396. allow_rise <= 1'b0;
  397. stop_after_budget <= 1'b0;
  398. status_state <= ST_IDLE;
  399. end else begin
  400. status_state <= ST_FAULT;
  401. fault_code <= FC_TINY_TRAVEL;
  402. step <= 1'b0;
  403. moving <= 1'b0;
  404. phase <= PH_NONE;
  405. end
  406. end
  407. end else if (stop_after_budget && !gated_step && !step) begin
  408. status_state <= ST_FAULT;
  409. fault_code <= FC_CAL_OPEN_TIMEOUT;
  410. moving <= 1'b0;
  411. phase <= PH_NONE;
  412. end else begin
  413. if ((motion_count >= MAX_STEPS_W) ||
  414. (gated_step_rise && (motion_count + 1'b1 >= MAX_STEPS_W))) begin
  415. allow_rise <= 1'b0;
  416. stop_after_budget <= 1'b1;
  417. end
  418. if (phase == PH_DIR_SETUP) begin
  419. step <= 1'b0;
  420. if (tick_500us) begin
  421. if (setup_count + 1'b1 >= DIR_SETUP_TICKS[3:0]) begin
  422. phase <= PH_RUN;
  423. allow_rise <= !stop_after_budget && (motion_count < MAX_STEPS_W);
  424. end else begin
  425. setup_count <= setup_count + 1'b1;
  426. end
  427. end
  428. end else if (tick_500us) begin
  429. step <= step_toggle(step, allow_rise && !stop_after_budget &&
  430. (motion_count < MAX_STEPS_W));
  431. end
  432. end
  433. // ----------------------------------------------------------
  434. // 8) WAIT_REPLAN / drain / decide
  435. // ----------------------------------------------------------
  436. end else if (status_state == ST_WAIT_REPLAN) begin
  437. if (phase == PH_DRAIN_HIGH) begin
  438. if (tick_500us)
  439. step <= 1'b0;
  440. if (!gated_step && (!step || tick_500us)) begin
  441. step <= 1'b0;
  442. phase <= PH_NONE;
  443. moving <= 1'b0;
  444. if (watchdog_stop) begin
  445. status_state <= ST_IDLE;
  446. wd_stopped <= 1'b1;
  447. replan_pending <= 1'b0;
  448. end else begin
  449. replan_pending <= 1'b1;
  450. end
  451. end
  452. end else if (replan_pending) begin
  453. replan_pending <= 1'b0;
  454. if (is_open_endpoint) begin
  455. endpoint_mode <= 1'b1;
  456. target_position <= {CNT_W{1'b0}};
  457. target_valid <= 1'b1;
  458. need_planner <= 1'b0;
  459. planner_req <= 1'b0;
  460. if (open_limit) begin
  461. current_position <= {CNT_W{1'b0}};
  462. status_state <= ST_IDLE;
  463. moving <= 1'b0;
  464. step <= 1'b0;
  465. phase <= PH_NONE;
  466. end else begin
  467. motion_count <= {CNT_W{1'b0}};
  468. dir_close <= 1'b0;
  469. step <= 1'b0;
  470. setup_count <= 4'd0;
  471. allow_rise <= 1'b0;
  472. stop_after_budget <= 1'b0;
  473. phase <= PH_DIR_SETUP;
  474. moving <= 1'b1;
  475. status_state <= ST_MOVE_OPEN;
  476. end
  477. end else if (is_close_endpoint) begin
  478. endpoint_mode <= 1'b1;
  479. target_position <= travel_steps;
  480. target_valid <= 1'b1;
  481. need_planner <= 1'b0;
  482. planner_req <= 1'b0;
  483. if (close_limit) begin
  484. current_position <= travel_steps;
  485. status_state <= ST_IDLE;
  486. moving <= 1'b0;
  487. step <= 1'b0;
  488. phase <= PH_NONE;
  489. end else begin
  490. motion_count <= {CNT_W{1'b0}};
  491. dir_close <= 1'b1;
  492. step <= 1'b0;
  493. setup_count <= 4'd0;
  494. allow_rise <= 1'b0;
  495. stop_after_budget <= 1'b0;
  496. phase <= PH_DIR_SETUP;
  497. moving <= 1'b1;
  498. status_state <= ST_MOVE_CLOSE;
  499. end
  500. end else begin
  501. endpoint_mode <= 1'b0;
  502. need_planner <= 1'b1;
  503. planner_req <= 1'b1;
  504. moving <= 1'b0;
  505. step <= 1'b0;
  506. phase <= PH_NONE;
  507. end
  508. end
  509. // ----------------------------------------------------------
  510. // 9) MOVE_OPEN / MOVE_CLOSE
  511. // ----------------------------------------------------------
  512. end else if ((status_state == ST_MOVE_OPEN) ||
  513. (status_state == ST_MOVE_CLOSE)) begin
  514. if (endpoint_mode) begin
  515. if ((dir_close && close_limit) || (!dir_close && open_limit)) begin
  516. step <= 1'b0;
  517. allow_rise <= 1'b0;
  518. stop_after_budget <= 1'b0;
  519. phase <= PH_NONE;
  520. moving <= 1'b0;
  521. if (dir_close) begin
  522. current_position <= travel_steps;
  523. target_position <= travel_steps;
  524. end else begin
  525. current_position <= {CNT_W{1'b0}};
  526. target_position <= {CNT_W{1'b0}};
  527. end
  528. status_state <= ST_IDLE;
  529. end else if (stop_after_budget && !gated_step && !step) begin
  530. status_state <= ST_FAULT;
  531. fault_code <= FC_ENDPOINT_TIMEOUT;
  532. moving <= 1'b0;
  533. phase <= PH_NONE;
  534. step <= 1'b0;
  535. end else begin
  536. if ((motion_count >= guard_limit) ||
  537. (gated_step_rise && (motion_count + 1'b1 >= guard_limit))) begin
  538. allow_rise <= 1'b0;
  539. stop_after_budget <= 1'b1;
  540. end
  541. if (phase == PH_DIR_SETUP) begin
  542. step <= 1'b0;
  543. if (tick_500us) begin
  544. if (setup_count + 1'b1 >= DIR_SETUP_TICKS[3:0]) begin
  545. phase <= PH_RUN;
  546. allow_rise <= 1'b1;
  547. end else begin
  548. setup_count <= setup_count + 1'b1;
  549. end
  550. end
  551. end else if (phase == PH_DRAIN_HIGH) begin
  552. if (tick_500us)
  553. step <= 1'b0;
  554. end else if (tick_500us) begin
  555. step <= step_toggle(step, allow_rise && !stop_after_budget &&
  556. (motion_count < guard_limit));
  557. end
  558. end
  559. end else begin
  560. // Mid-percent motion
  561. if ((dir_close && close_limit) || (!dir_close && open_limit)) begin
  562. step <= 1'b0;
  563. allow_rise <= 1'b0;
  564. if (dir_close)
  565. current_position <= travel_steps;
  566. else
  567. current_position <= {CNT_W{1'b0}};
  568. replan_pending <= 1'b1;
  569. need_planner <= 1'b0;
  570. planner_req <= 1'b0;
  571. phase <= PH_NONE;
  572. moving <= 1'b0;
  573. status_state <= ST_WAIT_REPLAN;
  574. end else begin
  575. // Effective current after this cycle's rise
  576. // Completion: compare using updated position intent
  577. if ((current_position == target_position) ||
  578. (gated_step_rise && dir_close &&
  579. (current_position + 1'b1 == target_position)) ||
  580. (gated_step_rise && !dir_close &&
  581. (current_position - 1'b1 == target_position))) begin
  582. allow_rise <= 1'b0;
  583. if (step || gated_step) begin
  584. phase <= PH_DRAIN_HIGH;
  585. if (tick_500us)
  586. step <= 1'b0;
  587. end else begin
  588. step <= 1'b0;
  589. phase <= PH_NONE;
  590. moving <= 1'b0;
  591. status_state <= ST_IDLE;
  592. end
  593. end else if (phase == PH_DIR_SETUP) begin
  594. step <= 1'b0;
  595. if (tick_500us) begin
  596. if (setup_count + 1'b1 >= DIR_SETUP_TICKS[3:0]) begin
  597. phase <= PH_RUN;
  598. allow_rise <= 1'b1;
  599. end else begin
  600. setup_count <= setup_count + 1'b1;
  601. end
  602. end
  603. end else if (phase == PH_DRAIN_HIGH) begin
  604. if (tick_500us)
  605. step <= 1'b0;
  606. if (!step && !gated_step) begin
  607. moving <= 1'b0;
  608. status_state <= ST_IDLE;
  609. phase <= PH_NONE;
  610. end else if (tick_500us && step) begin
  611. // falling this tick; next cycle check idle
  612. end
  613. end else if (tick_500us) begin
  614. step <= step_toggle(step, allow_rise);
  615. end
  616. end
  617. end
  618. // ----------------------------------------------------------
  619. // 10) IDLE
  620. // ----------------------------------------------------------
  621. end else if (status_state == ST_IDLE) begin
  622. step <= 1'b0;
  623. moving <= 1'b0;
  624. if (calibrated_r && is_open_endpoint && open_limit)
  625. current_position <= {CNT_W{1'b0}};
  626. if (calibrated_r && is_close_endpoint && close_limit)
  627. current_position <= travel_steps;
  628. if (replan_pending)
  629. status_state <= ST_WAIT_REPLAN;
  630. end
  631. end
  632. end
  633. endmodule