door_axis_controller.v 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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. reg open_limit_armed;
  82. reg close_limit_armed;
  83. assign calibrated = calibrated_r;
  84. assign faulted = (status_state == ST_FAULT);
  85. assign terminal = ((status_state == ST_IDLE) && calibrated_r) || (status_state == ST_FAULT);
  86. assign planner_percent = requested_percent;
  87. assign planner_travel = travel_steps;
  88. wire both_limits = open_limit && close_limit;
  89. wire mid_request = (requested_percent > 8'd0) && (requested_percent < PERCENT_MAX[7:0]);
  90. wire is_open_endpoint = (requested_percent == 8'd0);
  91. wire is_close_endpoint = (requested_percent == PERCENT_MAX[7:0]);
  92. wire planner_accept = planner_done && (planner_done_axis == AXIS_ID[1:0]) &&
  93. (planner_done_percent == requested_percent) && calibrated_r &&
  94. (status_state != ST_FAULT) && mid_request && need_planner;
  95. wire [CNT_W-1:0] guard_limit = travel_steps + ENDPOINT_GUARD_EXTRA[CNT_W-1:0];
  96. wire in_calibration = (status_state == ST_CAL_SEEK_CLOSE) ||
  97. (status_state == ST_CAL_MEASURE_OPEN);
  98. wire open_assert_event = open_limit && open_limit_armed;
  99. wire close_assert_event = close_limit && close_limit_armed;
  100. wire departing_close_source =
  101. calibrated_r && !in_calibration &&
  102. ((status_state == ST_MOVE_OPEN) || (moving && !dir_close));
  103. wire departing_open_source =
  104. calibrated_r && !in_calibration &&
  105. ((status_state == ST_MOVE_CLOSE) || (moving && dir_close));
  106. wire close_snap_accept =
  107. calibrated_r && !in_calibration &&
  108. close_assert_event && !departing_close_source;
  109. wire open_snap_accept =
  110. calibrated_r && !in_calibration &&
  111. open_assert_event && !departing_open_source;
  112. // Pre-NBA view: single-limit coordinate disagrees with accepted snap event.
  113. wire limit_pos_mismatch =
  114. (close_snap_accept && (current_position != travel_steps)) ||
  115. (open_snap_accept && (current_position != {CNT_W{1'b0}}));
  116. // Combinational endpoint-effective coordinate for same-cycle planner decisions.
  117. // Use held source limit (not only one-shot snap): departure may consume arm
  118. // without snapping, so a later mid accept while close_limit stays high must
  119. // still treat the axis as at travel (never MOVE_CLOSE into the close limit).
  120. wire close_source_hold =
  121. calibrated_r && !in_calibration && close_limit && !departing_close_source;
  122. wire open_source_hold =
  123. calibrated_r && !in_calibration && open_limit && !departing_open_source;
  124. wire [CNT_W-1:0] effective_current_position =
  125. close_source_hold ? travel_steps :
  126. open_source_hold ? {CNT_W{1'b0}} :
  127. current_position;
  128. // Distinguish request bookkeeping from material replan preempt.
  129. // Idempotent same-percent heartbeat must not occupy the exclusive FSM branch.
  130. wire request_seen = request_valid && calibrated_r;
  131. wire idle_not_satisfied =
  132. (status_state == ST_IDLE) && (
  133. ((requested_percent_in == 8'd0) && !open_limit) ||
  134. ((requested_percent_in == PERCENT_MAX[7:0]) && !close_limit) ||
  135. ((requested_percent_in > 8'd0) &&
  136. (requested_percent_in < PERCENT_MAX[7:0]) &&
  137. (current_position != target_position))
  138. );
  139. wire request_requires_replan = request_seen && (
  140. (requested_percent_in != requested_percent) ||
  141. wd_stopped ||
  142. // Limit snap forces resume only when this cycle cannot accept a matching
  143. // planner result (which uses effective_current_position for DIR).
  144. (limit_pos_mismatch && !planner_accept) ||
  145. idle_not_satisfied
  146. );
  147. always @* begin
  148. status_requested_percent = requested_percent;
  149. status_current = current_position;
  150. status_target = target_position;
  151. status_travel = travel_steps;
  152. status_fault_code = {5'd0, fault_code};
  153. status_flags = {
  154. 1'b0,
  155. replan_pending,
  156. target_valid,
  157. endpoint_mode,
  158. moving,
  159. calibrated_r,
  160. close_limit,
  161. open_limit
  162. };
  163. end
  164. // Shared STEP half-period helper: returns next step level
  165. function automatic step_toggle;
  166. input cur_step;
  167. input do_allow_rise;
  168. begin
  169. if (cur_step)
  170. step_toggle = 1'b0;
  171. else if (do_allow_rise)
  172. step_toggle = 1'b1;
  173. else
  174. step_toggle = 1'b0;
  175. end
  176. endfunction
  177. always @(posedge clk or negedge rst_n) begin
  178. if (!rst_n) begin
  179. status_state <= ST_CAL_SEEK_CLOSE;
  180. calibrated_r <= 1'b0;
  181. fault_code <= FC_NONE;
  182. travel_steps <= {CNT_W{1'b0}};
  183. current_position <= {CNT_W{1'b0}};
  184. target_position <= {CNT_W{1'b0}};
  185. requested_percent <= 8'd0;
  186. replan_pending <= 1'b0;
  187. endpoint_mode <= 1'b0;
  188. target_valid <= 1'b0;
  189. moving <= 1'b0;
  190. phase <= PH_NONE;
  191. setup_count <= 4'd0;
  192. motion_count <= {CNT_W{1'b0}};
  193. allow_rise <= 1'b0;
  194. stop_after_budget <= 1'b0;
  195. cal_started <= 1'b0;
  196. need_planner <= 1'b0;
  197. planner_req <= 1'b0;
  198. step <= 1'b0;
  199. dir_close <= 1'b1;
  200. wd_stopped <= 1'b0;
  201. open_limit_armed <= 1'b1;
  202. close_limit_armed <= 1'b1;
  203. end else if (both_limits) begin
  204. status_state <= ST_FAULT;
  205. fault_code <= FC_BOTH_LIMITS;
  206. step <= 1'b0;
  207. phase <= PH_NONE;
  208. moving <= 1'b0;
  209. allow_rise <= 1'b0;
  210. stop_after_budget <= 1'b0;
  211. planner_req <= 1'b0;
  212. need_planner <= 1'b0;
  213. replan_pending <= 1'b0;
  214. if (request_valid)
  215. requested_percent <= requested_percent_in;
  216. end else if (status_state == ST_FAULT) begin
  217. step <= 1'b0;
  218. moving <= 1'b0;
  219. planner_req <= 1'b0;
  220. if (request_valid)
  221. requested_percent <= requested_percent_in;
  222. end else begin
  223. // ----------------------------------------------------------
  224. // 1) Position / cal / guard counts on gated rise (always)
  225. // ----------------------------------------------------------
  226. if (gated_step_rise) begin
  227. if (status_state == ST_CAL_SEEK_CLOSE ||
  228. status_state == ST_CAL_MEASURE_OPEN) begin
  229. motion_count <= motion_count + 1'b1;
  230. end else if (calibrated_r) begin
  231. if (endpoint_mode)
  232. motion_count <= motion_count + 1'b1;
  233. if (dir_close) begin
  234. if (current_position < travel_steps)
  235. current_position <= current_position + 1'b1;
  236. end else begin
  237. if (current_position != {CNT_W{1'b0}})
  238. current_position <= current_position - 1'b1;
  239. end
  240. end
  241. end
  242. // ----------------------------------------------------------
  243. // 1b) One-shot single-limit snap (consumes assertion interval).
  244. // Departure from a consumed source limit must not overwrite §1.
  245. // ----------------------------------------------------------
  246. if (!open_limit)
  247. open_limit_armed <= 1'b1;
  248. if (!close_limit)
  249. close_limit_armed <= 1'b1;
  250. if (open_assert_event)
  251. open_limit_armed <= 1'b0;
  252. if (close_assert_event)
  253. close_limit_armed <= 1'b0;
  254. if (close_snap_accept)
  255. current_position <= travel_steps;
  256. else if (open_snap_accept)
  257. current_position <= {CNT_W{1'b0}};
  258. // ----------------------------------------------------------
  259. // 2a) Non-destructive request bookkeeping (idempotent OK).
  260. // Does not occupy the exclusive FSM branch — heartbeat may
  261. // coexist with planner_accept / tick_500us / limit progression.
  262. // ----------------------------------------------------------
  263. if (request_seen) begin
  264. requested_percent <= requested_percent_in;
  265. wd_stopped <= 1'b0;
  266. end
  267. // ----------------------------------------------------------
  268. // 2b) Material host replan only (latest-wins). Preempts tick.
  269. // Same-percent heartbeat does NOT enter this branch.
  270. // ----------------------------------------------------------
  271. if (request_requires_replan) begin
  272. replan_pending <= 1'b1;
  273. planner_req <= 1'b0;
  274. need_planner <= 1'b0;
  275. allow_rise <= 1'b0;
  276. // Materially new percent or forced replan: old target invalid
  277. target_valid <= 1'b0;
  278. if (gated_step || step) begin
  279. phase <= PH_DRAIN_HIGH;
  280. status_state <= ST_WAIT_REPLAN;
  281. end else begin
  282. step <= 1'b0;
  283. phase <= PH_NONE;
  284. moving <= 1'b0;
  285. status_state <= ST_WAIT_REPLAN;
  286. end
  287. // ----------------------------------------------------------
  288. // 3) Watchdog graceful stop (suppressed by same-cycle SET_ALL)
  289. // Latch drain once; while PH_DRAIN_HIGH, fall through to §8 so
  290. // tick_500us can pull STEP low even if watchdog_stop stays high.
  291. // ----------------------------------------------------------
  292. end else if (watchdog_stop && calibrated_r && !in_calibration &&
  293. !wd_stopped && !request_seen &&
  294. !(status_state == ST_WAIT_REPLAN && phase == PH_DRAIN_HIGH)) begin
  295. allow_rise <= 1'b0;
  296. planner_req <= 1'b0;
  297. need_planner <= 1'b0;
  298. if (gated_step || step) begin
  299. phase <= PH_DRAIN_HIGH;
  300. status_state <= ST_WAIT_REPLAN;
  301. replan_pending <= 1'b0;
  302. end else begin
  303. step <= 1'b0;
  304. phase <= PH_NONE;
  305. moving <= 1'b0;
  306. status_state <= ST_IDLE;
  307. wd_stopped <= 1'b1;
  308. replan_pending <= 1'b0;
  309. end
  310. // ----------------------------------------------------------
  311. // 4) Planner result (uses effective_current_position for DIR)
  312. // ----------------------------------------------------------
  313. end else if (planner_accept) begin
  314. planner_req <= 1'b0;
  315. need_planner <= 1'b0;
  316. replan_pending <= 1'b0;
  317. target_position <= planner_result;
  318. target_valid <= 1'b1;
  319. endpoint_mode <= 1'b0;
  320. motion_count <= {CNT_W{1'b0}};
  321. step <= 1'b0;
  322. setup_count <= 4'd0;
  323. allow_rise <= 1'b0;
  324. stop_after_budget <= 1'b0;
  325. if (planner_result == effective_current_position) begin
  326. status_state <= ST_IDLE;
  327. moving <= 1'b0;
  328. phase <= PH_NONE;
  329. end else if (planner_result > effective_current_position) begin
  330. dir_close <= 1'b1;
  331. phase <= PH_DIR_SETUP;
  332. moving <= 1'b1;
  333. status_state <= ST_MOVE_CLOSE;
  334. end else begin
  335. dir_close <= 1'b0;
  336. phase <= PH_DIR_SETUP;
  337. moving <= 1'b1;
  338. status_state <= ST_MOVE_OPEN;
  339. end
  340. // ----------------------------------------------------------
  341. // 5) Calibration start
  342. // ----------------------------------------------------------
  343. end else if (!cal_started) begin
  344. if (limit_sync_valid) begin
  345. cal_started <= 1'b1;
  346. motion_count <= {CNT_W{1'b0}};
  347. setup_count <= 4'd0;
  348. allow_rise <= 1'b0;
  349. stop_after_budget <= 1'b0;
  350. step <= 1'b0;
  351. phase <= PH_DIR_SETUP;
  352. moving <= 1'b1;
  353. if (close_limit) begin
  354. dir_close <= 1'b0;
  355. status_state <= ST_CAL_MEASURE_OPEN;
  356. end else begin
  357. dir_close <= 1'b1;
  358. status_state <= ST_CAL_SEEK_CLOSE;
  359. end
  360. end
  361. // ----------------------------------------------------------
  362. // 6) CAL_SEEK_CLOSE
  363. // ----------------------------------------------------------
  364. end else if (status_state == ST_CAL_SEEK_CLOSE) begin
  365. if (close_limit) begin
  366. step <= 1'b0;
  367. allow_rise <= 1'b0;
  368. stop_after_budget <= 1'b0;
  369. motion_count <= {CNT_W{1'b0}};
  370. setup_count <= 4'd0;
  371. dir_close <= 1'b0;
  372. phase <= PH_DIR_SETUP;
  373. moving <= 1'b1;
  374. status_state <= ST_CAL_MEASURE_OPEN;
  375. end else if (stop_after_budget && !gated_step && !step) begin
  376. status_state <= ST_FAULT;
  377. fault_code <= FC_CAL_CLOSE_TIMEOUT;
  378. moving <= 1'b0;
  379. phase <= PH_NONE;
  380. end else begin
  381. // Use post-rise count: if count already at max, stop new rises
  382. if ((motion_count >= MAX_STEPS_W) ||
  383. (gated_step_rise && (motion_count + 1'b1 >= MAX_STEPS_W))) begin
  384. allow_rise <= 1'b0;
  385. stop_after_budget <= 1'b1;
  386. end
  387. if (phase == PH_DIR_SETUP) begin
  388. step <= 1'b0;
  389. if (tick_500us) begin
  390. if (setup_count + 1'b1 >= DIR_SETUP_TICKS[3:0]) begin
  391. phase <= PH_RUN;
  392. setup_count <= 4'd0;
  393. allow_rise <= !stop_after_budget && (motion_count < MAX_STEPS_W);
  394. end else begin
  395. setup_count <= setup_count + 1'b1;
  396. end
  397. end
  398. end else if (tick_500us) begin
  399. step <= step_toggle(step, allow_rise && !stop_after_budget &&
  400. (motion_count < MAX_STEPS_W));
  401. end
  402. end
  403. // ----------------------------------------------------------
  404. // 7) CAL_MEASURE_OPEN
  405. // ----------------------------------------------------------
  406. end else if (status_state == ST_CAL_MEASURE_OPEN) begin
  407. if (open_limit) begin
  408. // motion_count already includes this cycle's rise (NBA: see old+1)
  409. // Use effective count: if rise this cycle, +1 on current
  410. if (gated_step_rise) begin
  411. // effective = motion_count+1 (scheduled); compare with that
  412. if (((motion_count + 1'b1) >= MIN_TRAVEL_W) &&
  413. ((motion_count + 1'b1) <= MAX_STEPS_W)) begin
  414. travel_steps <= motion_count + 1'b1;
  415. current_position <= {CNT_W{1'b0}};
  416. target_position <= {CNT_W{1'b0}};
  417. requested_percent <= 8'd0;
  418. calibrated_r <= 1'b1;
  419. fault_code <= FC_NONE;
  420. target_valid <= 1'b1;
  421. endpoint_mode <= 1'b1;
  422. step <= 1'b0;
  423. phase <= PH_NONE;
  424. moving <= 1'b0;
  425. allow_rise <= 1'b0;
  426. stop_after_budget <= 1'b0;
  427. status_state <= ST_IDLE;
  428. end else begin
  429. status_state <= ST_FAULT;
  430. fault_code <= FC_TINY_TRAVEL;
  431. step <= 1'b0;
  432. moving <= 1'b0;
  433. phase <= PH_NONE;
  434. end
  435. end else begin
  436. if ((motion_count >= MIN_TRAVEL_W) &&
  437. (motion_count <= MAX_STEPS_W)) begin
  438. travel_steps <= motion_count;
  439. current_position <= {CNT_W{1'b0}};
  440. target_position <= {CNT_W{1'b0}};
  441. requested_percent <= 8'd0;
  442. calibrated_r <= 1'b1;
  443. fault_code <= FC_NONE;
  444. target_valid <= 1'b1;
  445. endpoint_mode <= 1'b1;
  446. step <= 1'b0;
  447. phase <= PH_NONE;
  448. moving <= 1'b0;
  449. allow_rise <= 1'b0;
  450. stop_after_budget <= 1'b0;
  451. status_state <= ST_IDLE;
  452. end else begin
  453. status_state <= ST_FAULT;
  454. fault_code <= FC_TINY_TRAVEL;
  455. step <= 1'b0;
  456. moving <= 1'b0;
  457. phase <= PH_NONE;
  458. end
  459. end
  460. end else if (stop_after_budget && !gated_step && !step) begin
  461. status_state <= ST_FAULT;
  462. fault_code <= FC_CAL_OPEN_TIMEOUT;
  463. moving <= 1'b0;
  464. phase <= PH_NONE;
  465. end else begin
  466. if ((motion_count >= MAX_STEPS_W) ||
  467. (gated_step_rise && (motion_count + 1'b1 >= MAX_STEPS_W))) begin
  468. allow_rise <= 1'b0;
  469. stop_after_budget <= 1'b1;
  470. end
  471. if (phase == PH_DIR_SETUP) begin
  472. step <= 1'b0;
  473. if (tick_500us) begin
  474. if (setup_count + 1'b1 >= DIR_SETUP_TICKS[3:0]) begin
  475. phase <= PH_RUN;
  476. allow_rise <= !stop_after_budget && (motion_count < MAX_STEPS_W);
  477. end else begin
  478. setup_count <= setup_count + 1'b1;
  479. end
  480. end
  481. end else if (tick_500us) begin
  482. step <= step_toggle(step, allow_rise && !stop_after_budget &&
  483. (motion_count < MAX_STEPS_W));
  484. end
  485. end
  486. // ----------------------------------------------------------
  487. // 8) WAIT_REPLAN / drain / decide
  488. // ----------------------------------------------------------
  489. end else if (status_state == ST_WAIT_REPLAN) begin
  490. if (phase == PH_DRAIN_HIGH) begin
  491. if (tick_500us)
  492. step <= 1'b0;
  493. if (!gated_step && (!step || tick_500us)) begin
  494. step <= 1'b0;
  495. phase <= PH_NONE;
  496. moving <= 1'b0;
  497. if (watchdog_stop) begin
  498. status_state <= ST_IDLE;
  499. wd_stopped <= 1'b1;
  500. replan_pending <= 1'b0;
  501. end else begin
  502. replan_pending <= 1'b1;
  503. end
  504. end
  505. end else if (replan_pending) begin
  506. replan_pending <= 1'b0;
  507. if (is_open_endpoint) begin
  508. endpoint_mode <= 1'b1;
  509. target_position <= {CNT_W{1'b0}};
  510. target_valid <= 1'b1;
  511. need_planner <= 1'b0;
  512. planner_req <= 1'b0;
  513. if (open_limit) begin
  514. current_position <= {CNT_W{1'b0}};
  515. status_state <= ST_IDLE;
  516. moving <= 1'b0;
  517. step <= 1'b0;
  518. phase <= PH_NONE;
  519. end else begin
  520. motion_count <= {CNT_W{1'b0}};
  521. dir_close <= 1'b0;
  522. step <= 1'b0;
  523. setup_count <= 4'd0;
  524. allow_rise <= 1'b0;
  525. stop_after_budget <= 1'b0;
  526. phase <= PH_DIR_SETUP;
  527. moving <= 1'b1;
  528. status_state <= ST_MOVE_OPEN;
  529. end
  530. end else if (is_close_endpoint) begin
  531. endpoint_mode <= 1'b1;
  532. target_position <= travel_steps;
  533. target_valid <= 1'b1;
  534. need_planner <= 1'b0;
  535. planner_req <= 1'b0;
  536. if (close_limit) begin
  537. current_position <= travel_steps;
  538. status_state <= ST_IDLE;
  539. moving <= 1'b0;
  540. step <= 1'b0;
  541. phase <= PH_NONE;
  542. end else begin
  543. motion_count <= {CNT_W{1'b0}};
  544. dir_close <= 1'b1;
  545. step <= 1'b0;
  546. setup_count <= 4'd0;
  547. allow_rise <= 1'b0;
  548. stop_after_budget <= 1'b0;
  549. phase <= PH_DIR_SETUP;
  550. moving <= 1'b1;
  551. status_state <= ST_MOVE_CLOSE;
  552. end
  553. end else begin
  554. endpoint_mode <= 1'b0;
  555. need_planner <= 1'b1;
  556. planner_req <= 1'b1;
  557. moving <= 1'b0;
  558. step <= 1'b0;
  559. phase <= PH_NONE;
  560. end
  561. end
  562. // ----------------------------------------------------------
  563. // 9) MOVE_OPEN / MOVE_CLOSE
  564. // ----------------------------------------------------------
  565. end else if ((status_state == ST_MOVE_OPEN) ||
  566. (status_state == ST_MOVE_CLOSE)) begin
  567. if (endpoint_mode) begin
  568. if ((dir_close && close_limit) || (!dir_close && open_limit)) begin
  569. step <= 1'b0;
  570. allow_rise <= 1'b0;
  571. stop_after_budget <= 1'b0;
  572. phase <= PH_NONE;
  573. moving <= 1'b0;
  574. if (dir_close) begin
  575. current_position <= travel_steps;
  576. target_position <= travel_steps;
  577. end else begin
  578. current_position <= {CNT_W{1'b0}};
  579. target_position <= {CNT_W{1'b0}};
  580. end
  581. status_state <= ST_IDLE;
  582. end else if (stop_after_budget && !gated_step && !step) begin
  583. status_state <= ST_FAULT;
  584. fault_code <= FC_ENDPOINT_TIMEOUT;
  585. moving <= 1'b0;
  586. phase <= PH_NONE;
  587. step <= 1'b0;
  588. end else begin
  589. if ((motion_count >= guard_limit) ||
  590. (gated_step_rise && (motion_count + 1'b1 >= guard_limit))) begin
  591. allow_rise <= 1'b0;
  592. stop_after_budget <= 1'b1;
  593. end
  594. if (phase == PH_DIR_SETUP) begin
  595. step <= 1'b0;
  596. if (tick_500us) begin
  597. if (setup_count + 1'b1 >= DIR_SETUP_TICKS[3:0]) begin
  598. phase <= PH_RUN;
  599. allow_rise <= 1'b1;
  600. end else begin
  601. setup_count <= setup_count + 1'b1;
  602. end
  603. end
  604. end else if (phase == PH_DRAIN_HIGH) begin
  605. if (tick_500us)
  606. step <= 1'b0;
  607. end else if (tick_500us) begin
  608. step <= step_toggle(step, allow_rise && !stop_after_budget &&
  609. (motion_count < guard_limit));
  610. end
  611. end
  612. end else begin
  613. // Mid-percent motion
  614. if ((dir_close && close_limit) || (!dir_close && open_limit)) begin
  615. step <= 1'b0;
  616. allow_rise <= 1'b0;
  617. if (dir_close)
  618. current_position <= travel_steps;
  619. else
  620. current_position <= {CNT_W{1'b0}};
  621. replan_pending <= 1'b1;
  622. need_planner <= 1'b0;
  623. planner_req <= 1'b0;
  624. phase <= PH_NONE;
  625. moving <= 1'b0;
  626. status_state <= ST_WAIT_REPLAN;
  627. end else begin
  628. // Effective current after this cycle's rise
  629. // Completion: compare using updated position intent
  630. if ((current_position == target_position) ||
  631. (gated_step_rise && dir_close &&
  632. (current_position + 1'b1 == target_position)) ||
  633. (gated_step_rise && !dir_close &&
  634. (current_position - 1'b1 == target_position))) begin
  635. allow_rise <= 1'b0;
  636. if (step || gated_step) begin
  637. phase <= PH_DRAIN_HIGH;
  638. if (tick_500us)
  639. step <= 1'b0;
  640. end else begin
  641. step <= 1'b0;
  642. phase <= PH_NONE;
  643. moving <= 1'b0;
  644. status_state <= ST_IDLE;
  645. end
  646. end else if (phase == PH_DIR_SETUP) begin
  647. step <= 1'b0;
  648. if (tick_500us) begin
  649. if (setup_count + 1'b1 >= DIR_SETUP_TICKS[3:0]) begin
  650. phase <= PH_RUN;
  651. allow_rise <= 1'b1;
  652. end else begin
  653. setup_count <= setup_count + 1'b1;
  654. end
  655. end
  656. end else if (phase == PH_DRAIN_HIGH) begin
  657. if (tick_500us)
  658. step <= 1'b0;
  659. if (!step && !gated_step) begin
  660. moving <= 1'b0;
  661. status_state <= ST_IDLE;
  662. phase <= PH_NONE;
  663. end else if (tick_500us && step) begin
  664. // falling this tick; next cycle check idle
  665. end
  666. end else if (tick_500us) begin
  667. step <= step_toggle(step, allow_rise);
  668. end
  669. end
  670. end
  671. // ----------------------------------------------------------
  672. // 10) IDLE
  673. // ----------------------------------------------------------
  674. end else if (status_state == ST_IDLE) begin
  675. step <= 1'b0;
  676. moving <= 1'b0;
  677. if (open_snap_accept)
  678. current_position <= {CNT_W{1'b0}};
  679. if (close_snap_accept)
  680. current_position <= travel_steps;
  681. if (replan_pending)
  682. status_state <= ST_WAIT_REPLAN;
  683. end
  684. end
  685. end
  686. endmodule