door_axis_controller.v 34 KB

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