door_motion_controller.v 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. `timescale 1ns/1ps
  2. // Three-axis door motion: Scheme A full-feature
  3. // - per-axis safety kernels
  4. // - serial X→Y→Z calibration
  5. // - shared 25 us DDA (active A1 sequential) + shared profile/rate builders
  6. module door_motion_controller #(
  7. parameter integer MAX_CAL_STEPS = 5000,
  8. parameter integer MIN_TRAVEL = 2,
  9. parameter integer ENDPOINT_GUARD_EXTRA = 500,
  10. parameter integer DIR_SETUP_TICKS = 2,
  11. parameter integer PERCENT_MAX = 100,
  12. parameter integer STATUS_PAYLOAD_LENGTH = 31,
  13. parameter integer MOTION_SERVICE_CLKS = 625,
  14. parameter integer PHASE_FACTOR = 5120,
  15. parameter integer CAL_RAMP_UNIT = 8
  16. ) (
  17. input wire clk,
  18. input wire rst_n,
  19. input wire tick_500us,
  20. input wire limit_sync_valid,
  21. input wire [2:0] open_limit,
  22. input wire [2:0] close_limit,
  23. input wire [2:0] gated_step,
  24. input wire [2:0] gated_step_rise,
  25. input wire motor_request_valid,
  26. input wire [7:0] percent_x,
  27. input wire [7:0] percent_y,
  28. input wire [7:0] percent_z,
  29. input wire watchdog_stop,
  30. input wire watchdog_tripped,
  31. output wire [2:0] step,
  32. output wire [2:0] dir_close,
  33. output reg startup_complete,
  34. output wire [2:0] calibrated_mask,
  35. output wire [2:0] fault_mask,
  36. input wire status_capture,
  37. input wire [7:0] status_rd_addr,
  38. output wire [7:0] status_rd_data
  39. );
  40. wire planner_start;
  41. wire [1:0] planner_axis_id;
  42. wire [7:0] planner_percent_i;
  43. wire [12:0] planner_travel_i;
  44. wire planner_busy;
  45. wire planner_done;
  46. wire [12:0] planner_result;
  47. wire [1:0] planner_done_axis;
  48. wire [7:0] planner_done_percent;
  49. wire req_x, req_y, req_z;
  50. wire [7:0] pct_req_x, pct_req_y, pct_req_z;
  51. wire [12:0] trav_req_x, trav_req_y, trav_req_z;
  52. wire [2:0] st_state_x, st_state_y, st_state_z;
  53. wire [7:0] st_flags_x, st_flags_y, st_flags_z;
  54. wire [7:0] st_pct_x, st_pct_y, st_pct_z;
  55. wire [12:0] st_cur_x, st_cur_y, st_cur_z;
  56. wire [12:0] st_tgt_x, st_tgt_y, st_tgt_z;
  57. wire [12:0] st_trav_x, st_trav_y, st_trav_z;
  58. wire [7:0] st_fault_x, st_fault_y, st_fault_z;
  59. wire cal_x, cal_y, cal_z;
  60. wire fault_x, fault_y, fault_z;
  61. wire term_x, term_y, term_z;
  62. wire mov_x, mov_y, mov_z;
  63. wire allow_x, allow_y, allow_z;
  64. wire in_cal_x, in_cal_y, in_cal_z;
  65. wire crawl_x, crawl_y, crawl_z;
  66. wire dda_run_x, dda_run_y, dda_run_z;
  67. wire [3:0] lvl_x, lvl_y, lvl_z;
  68. wire [20:0] rq_x, rq_y, rq_z;
  69. wire [2:0] cal_enable;
  70. wire profile_start;
  71. wire profile_ready;
  72. wire profile_busy;
  73. wire profile_done;
  74. wire [12:0] max_travel;
  75. wire [24:0] phase_threshold;
  76. wire service_ce;
  77. wire [2:0] toggle_pulse;
  78. wire dda_busy;
  79. wire dda_overrun;
  80. wire all_axes_terminal_unused;
  81. wire [12:0] profile_distance_unused_x;
  82. wire [12:0] profile_done_steps_unused_x;
  83. wire [12:0] profile_ramp_unit_unused_x;
  84. wire [12:0] profile_distance_unused_y;
  85. wire [12:0] profile_done_steps_unused_y;
  86. wire [12:0] profile_ramp_unit_unused_y;
  87. wire [12:0] profile_distance_unused_z;
  88. wire [12:0] profile_done_steps_unused_z;
  89. wire [12:0] profile_ramp_unit_unused_z;
  90. // Shared rate refresh: travel * profile_scale[level] (or scale alone in cal).
  91. // Exact 21-bit product via door_rate_quantum_dispatcher (atomic launch record).
  92. wire rate_busy;
  93. wire rate_done;
  94. wire [1:0] rate_axis_done;
  95. wire [12:0] rate_travel_done;
  96. wire [8:0] rate_scale_done;
  97. wire [20:0] rate_result;
  98. wire rate_valid_x;
  99. wire rate_valid_y;
  100. wire rate_valid_z;
  101. function automatic [8:0] f_profile_scale;
  102. input [3:0] level;
  103. begin
  104. case (level)
  105. 4'd0: f_profile_scale = 9'd64;
  106. 4'd1: f_profile_scale = 9'd66;
  107. 4'd2: f_profile_scale = 9'd73;
  108. 4'd3: f_profile_scale = 9'd84;
  109. 4'd4: f_profile_scale = 9'd98;
  110. 4'd5: f_profile_scale = 9'd114;
  111. 4'd6: f_profile_scale = 9'd132;
  112. 4'd7: f_profile_scale = 9'd150;
  113. 4'd8: f_profile_scale = 9'd170;
  114. 4'd9: f_profile_scale = 9'd188;
  115. 4'd10: f_profile_scale = 9'd206;
  116. 4'd11: f_profile_scale = 9'd222;
  117. 4'd12: f_profile_scale = 9'd236;
  118. 4'd13: f_profile_scale = 9'd247;
  119. 4'd14: f_profile_scale = 9'd254;
  120. default: f_profile_scale = 9'd256;
  121. endcase
  122. end
  123. endfunction
  124. assign calibrated_mask = {cal_z, cal_y, cal_x};
  125. assign fault_mask = {fault_z, fault_y, fault_x};
  126. door_percent_planner u_planner (
  127. .clk(clk), .rst_n(rst_n),
  128. .start(planner_start),
  129. .axis_id(planner_axis_id),
  130. .percent(planner_percent_i),
  131. .travel(planner_travel_i),
  132. .busy(planner_busy),
  133. .done(planner_done),
  134. .result(planner_result),
  135. .done_axis_id(planner_done_axis),
  136. .done_percent(planner_done_percent)
  137. );
  138. reg grant_start;
  139. reg [1:0] grant_axis;
  140. reg [7:0] grant_percent;
  141. reg [12:0] grant_travel;
  142. reg [2:0] grant_hold;
  143. assign planner_start = grant_start;
  144. assign planner_axis_id = grant_axis;
  145. assign planner_percent_i = grant_percent;
  146. assign planner_travel_i = grant_travel;
  147. always @(posedge clk or negedge rst_n) begin
  148. if (!rst_n) begin
  149. grant_start <= 1'b0;
  150. grant_axis <= 2'd0;
  151. grant_percent <= 8'd0;
  152. grant_travel <= 13'd0;
  153. grant_hold <= 3'b000;
  154. end else begin
  155. grant_start <= 1'b0;
  156. if (!req_x) grant_hold[0] <= 1'b0;
  157. if (!req_y) grant_hold[1] <= 1'b0;
  158. if (!req_z) grant_hold[2] <= 1'b0;
  159. if (!planner_busy && !grant_start) begin
  160. if (req_x && !grant_hold[0]) begin
  161. grant_start <= 1'b1;
  162. grant_hold[0] <= 1'b1;
  163. grant_axis <= 2'd0;
  164. grant_percent <= pct_req_x;
  165. grant_travel <= trav_req_x;
  166. end else if (req_y && !grant_hold[1]) begin
  167. grant_start <= 1'b1;
  168. grant_hold[1] <= 1'b1;
  169. grant_axis <= 2'd1;
  170. grant_percent <= pct_req_y;
  171. grant_travel <= trav_req_y;
  172. end else if (req_z && !grant_hold[2]) begin
  173. grant_start <= 1'b1;
  174. grant_hold[2] <= 1'b1;
  175. grant_axis <= 2'd2;
  176. grant_percent <= pct_req_z;
  177. grant_travel <= trav_req_z;
  178. end
  179. end
  180. end
  181. end
  182. door_motion_service_tick #(.SERVICE_CLKS(MOTION_SERVICE_CLKS)) u_svc (
  183. .clk(clk), .rst_n(rst_n), .service_ce(service_ce)
  184. );
  185. door_calibration_sequencer u_cal_seq (
  186. .clk(clk), .rst_n(rst_n),
  187. .limit_sync_valid(limit_sync_valid),
  188. .term_x(term_x), .term_y(term_y), .term_z(term_z),
  189. .cal_ok_x(cal_x), .cal_ok_y(cal_y), .cal_ok_z(cal_z),
  190. .profile_busy(profile_busy),
  191. .profile_done(profile_done),
  192. .cal_enable(cal_enable),
  193. .profile_start(profile_start),
  194. .profile_ready(profile_ready),
  195. .all_axes_terminal(all_axes_terminal_unused)
  196. );
  197. door_profile_quantum_builder #(
  198. .PHASE_FACTOR(PHASE_FACTOR)
  199. ) u_profile (
  200. .clk(clk), .rst_n(rst_n),
  201. .start(profile_start),
  202. .travel_x(st_trav_x), .travel_y(st_trav_y), .travel_z(st_trav_z),
  203. .cal_ok_x(cal_x), .cal_ok_y(cal_y), .cal_ok_z(cal_z),
  204. .busy(profile_busy), .done(profile_done),
  205. .max_travel(max_travel),
  206. .phase_threshold(phase_threshold)
  207. );
  208. wire [2:0] dda_active = {dda_run_z, dda_run_y, dda_run_x};
  209. wire [2:0] dda_allow = {allow_z, allow_y, allow_x};
  210. wire [2:0] dda_cal = {in_cal_z, in_cal_y, in_cal_x};
  211. door_shared_dda_scheduler #(
  212. .CAL_PHASE_THRESHOLD(PHASE_FACTOR)
  213. ) u_dda (
  214. .clk(clk), .rst_n(rst_n),
  215. .service_ce(service_ce),
  216. .active(dda_active),
  217. .allow_toggle(dda_allow),
  218. .cal_mode(dda_cal),
  219. .rate0(rq_x), .rate1(rq_y), .rate2(rq_z),
  220. .phase_threshold(phase_threshold),
  221. .toggle_pulse(toggle_pulse),
  222. .busy(dda_busy),
  223. .service_overrun(dda_overrun)
  224. );
  225. wire [8:0] scale_x_w = f_profile_scale(crawl_x ? 4'd0 : lvl_x);
  226. wire [8:0] scale_y_w = f_profile_scale(crawl_y ? 4'd0 : lvl_y);
  227. wire [8:0] scale_z_w = f_profile_scale(crawl_z ? 4'd0 : lvl_z);
  228. // Round-robin shared rate builder with atomic launch record (no axis-tag skew).
  229. door_rate_quantum_dispatcher u_rate_disp (
  230. .clk(clk), .rst_n(rst_n),
  231. .travel_x(st_trav_x), .travel_y(st_trav_y), .travel_z(st_trav_z),
  232. .scale_x(scale_x_w),
  233. .scale_y(scale_y_w),
  234. .scale_z(scale_z_w),
  235. .cal_x(in_cal_x), .cal_y(in_cal_y), .cal_z(in_cal_z),
  236. .busy(rate_busy),
  237. .done(rate_done),
  238. .rate_valid_x(rate_valid_x),
  239. .rate_valid_y(rate_valid_y),
  240. .rate_valid_z(rate_valid_z),
  241. .axis_id_out(rate_axis_done),
  242. .travel_out(rate_travel_done),
  243. .scale_out(rate_scale_done),
  244. .product(rate_result)
  245. );
  246. // Silence unused capture ports (identity checked in dedicated TB).
  247. wire _unused_rate_tags = |{rate_travel_done, rate_scale_done, rate_busy};
  248. door_axis_controller #(
  249. .AXIS_ID(0),
  250. .MAX_CAL_STEPS(MAX_CAL_STEPS),
  251. .MIN_TRAVEL(MIN_TRAVEL),
  252. .ENDPOINT_GUARD_EXTRA(ENDPOINT_GUARD_EXTRA),
  253. .DIR_SETUP_TICKS(DIR_SETUP_TICKS),
  254. .PERCENT_MAX(PERCENT_MAX),
  255. .CAL_RAMP_UNIT(CAL_RAMP_UNIT)
  256. ) u_axis_x (
  257. .clk(clk), .rst_n(rst_n),
  258. .open_limit(open_limit[0]), .close_limit(close_limit[0]),
  259. .limit_sync_valid(limit_sync_valid),
  260. .cal_enable(cal_enable[0]),
  261. .gated_step(gated_step[0]), .gated_step_rise(gated_step_rise[0]),
  262. .tick_500us(tick_500us),
  263. .step_toggle_pulse(toggle_pulse[0]),
  264. .request_valid(motor_request_valid),
  265. .requested_percent_in(percent_x),
  266. .watchdog_stop(watchdog_stop),
  267. .rate_quantum_in(rate_result),
  268. .rate_quantum_valid(rate_valid_x),
  269. .planner_req(req_x),
  270. .planner_percent(pct_req_x),
  271. .planner_travel(trav_req_x),
  272. .planner_done(planner_done),
  273. .planner_done_axis(planner_done_axis),
  274. .planner_done_percent(planner_done_percent),
  275. .planner_result(planner_result),
  276. .step(step[0]), .dir_close(dir_close[0]),
  277. .status_state(st_state_x), .status_flags(st_flags_x),
  278. .status_requested_percent(st_pct_x),
  279. .status_current(st_cur_x), .status_target(st_tgt_x),
  280. .status_travel(st_trav_x), .status_fault_code(st_fault_x),
  281. .calibrated(cal_x), .faulted(fault_x), .terminal(term_x),
  282. .axis_moving(mov_x), .axis_allow_rise(allow_x),
  283. .axis_in_calibration(in_cal_x), .axis_endpoint_crawl(crawl_x),
  284. .axis_dda_run(dda_run_x),
  285. .profile_level(lvl_x),
  286. .profile_distance(profile_distance_unused_x), .profile_done_steps(profile_done_steps_unused_x), .profile_ramp_unit(profile_ramp_unit_unused_x),
  287. .rate_quantum(rq_x)
  288. );
  289. door_axis_controller #(
  290. .AXIS_ID(1),
  291. .MAX_CAL_STEPS(MAX_CAL_STEPS),
  292. .MIN_TRAVEL(MIN_TRAVEL),
  293. .ENDPOINT_GUARD_EXTRA(ENDPOINT_GUARD_EXTRA),
  294. .DIR_SETUP_TICKS(DIR_SETUP_TICKS),
  295. .PERCENT_MAX(PERCENT_MAX),
  296. .CAL_RAMP_UNIT(CAL_RAMP_UNIT)
  297. ) u_axis_y (
  298. .clk(clk), .rst_n(rst_n),
  299. .open_limit(open_limit[1]), .close_limit(close_limit[1]),
  300. .limit_sync_valid(limit_sync_valid),
  301. .cal_enable(cal_enable[1]),
  302. .gated_step(gated_step[1]), .gated_step_rise(gated_step_rise[1]),
  303. .tick_500us(tick_500us),
  304. .step_toggle_pulse(toggle_pulse[1]),
  305. .request_valid(motor_request_valid),
  306. .requested_percent_in(percent_y),
  307. .watchdog_stop(watchdog_stop),
  308. .rate_quantum_in(rate_result),
  309. .rate_quantum_valid(rate_valid_y),
  310. .planner_req(req_y),
  311. .planner_percent(pct_req_y),
  312. .planner_travel(trav_req_y),
  313. .planner_done(planner_done),
  314. .planner_done_axis(planner_done_axis),
  315. .planner_done_percent(planner_done_percent),
  316. .planner_result(planner_result),
  317. .step(step[1]), .dir_close(dir_close[1]),
  318. .status_state(st_state_y), .status_flags(st_flags_y),
  319. .status_requested_percent(st_pct_y),
  320. .status_current(st_cur_y), .status_target(st_tgt_y),
  321. .status_travel(st_trav_y), .status_fault_code(st_fault_y),
  322. .calibrated(cal_y), .faulted(fault_y), .terminal(term_y),
  323. .axis_moving(mov_y), .axis_allow_rise(allow_y),
  324. .axis_in_calibration(in_cal_y), .axis_endpoint_crawl(crawl_y),
  325. .axis_dda_run(dda_run_y),
  326. .profile_level(lvl_y),
  327. .profile_distance(profile_distance_unused_y), .profile_done_steps(profile_done_steps_unused_y), .profile_ramp_unit(profile_ramp_unit_unused_y),
  328. .rate_quantum(rq_y)
  329. );
  330. door_axis_controller #(
  331. .AXIS_ID(2),
  332. .MAX_CAL_STEPS(MAX_CAL_STEPS),
  333. .MIN_TRAVEL(MIN_TRAVEL),
  334. .ENDPOINT_GUARD_EXTRA(ENDPOINT_GUARD_EXTRA),
  335. .DIR_SETUP_TICKS(DIR_SETUP_TICKS),
  336. .PERCENT_MAX(PERCENT_MAX),
  337. .CAL_RAMP_UNIT(CAL_RAMP_UNIT)
  338. ) u_axis_z (
  339. .clk(clk), .rst_n(rst_n),
  340. .open_limit(open_limit[2]), .close_limit(close_limit[2]),
  341. .limit_sync_valid(limit_sync_valid),
  342. .cal_enable(cal_enable[2]),
  343. .gated_step(gated_step[2]), .gated_step_rise(gated_step_rise[2]),
  344. .tick_500us(tick_500us),
  345. .step_toggle_pulse(toggle_pulse[2]),
  346. .request_valid(motor_request_valid),
  347. .requested_percent_in(percent_z),
  348. .watchdog_stop(watchdog_stop),
  349. .rate_quantum_in(rate_result),
  350. .rate_quantum_valid(rate_valid_z),
  351. .planner_req(req_z),
  352. .planner_percent(pct_req_z),
  353. .planner_travel(trav_req_z),
  354. .planner_done(planner_done),
  355. .planner_done_axis(planner_done_axis),
  356. .planner_done_percent(planner_done_percent),
  357. .planner_result(planner_result),
  358. .step(step[2]), .dir_close(dir_close[2]),
  359. .status_state(st_state_z), .status_flags(st_flags_z),
  360. .status_requested_percent(st_pct_z),
  361. .status_current(st_cur_z), .status_target(st_tgt_z),
  362. .status_travel(st_trav_z), .status_fault_code(st_fault_z),
  363. .calibrated(cal_z), .faulted(fault_z), .terminal(term_z),
  364. .axis_moving(mov_z), .axis_allow_rise(allow_z),
  365. .axis_in_calibration(in_cal_z), .axis_endpoint_crawl(crawl_z),
  366. .axis_dda_run(dda_run_z),
  367. .profile_level(lvl_z),
  368. .profile_distance(profile_distance_unused_z), .profile_done_steps(profile_done_steps_unused_z), .profile_ramp_unit(profile_ramp_unit_unused_z),
  369. .rate_quantum(rq_z)
  370. );
  371. // startup_complete only after serial cal terminal + profile ready
  372. always @(posedge clk or negedge rst_n) begin
  373. if (!rst_n)
  374. startup_complete <= 1'b0;
  375. else if (!startup_complete && profile_ready && term_x && term_y && term_z)
  376. startup_complete <= 1'b1;
  377. end
  378. reg [7:0] snap [0:30];
  379. integer si;
  380. assign status_rd_data = (status_rd_addr < STATUS_PAYLOAD_LENGTH[7:0]) ?
  381. snap[status_rd_addr] : 8'd0;
  382. always @(posedge clk or negedge rst_n) begin
  383. if (!rst_n) begin
  384. for (si = 0; si < STATUS_PAYLOAD_LENGTH; si = si + 1)
  385. snap[si] <= 8'd0;
  386. end else if (status_capture) begin
  387. snap[0] <= {watchdog_tripped, startup_complete, fault_mask, calibrated_mask};
  388. snap[1] <= {5'd0, st_state_x};
  389. snap[2] <= st_flags_x;
  390. snap[3] <= st_pct_x;
  391. snap[4] <= st_cur_x[7:0];
  392. snap[5] <= {3'd0, st_cur_x[12:8]};
  393. snap[6] <= st_tgt_x[7:0];
  394. snap[7] <= {3'd0, st_tgt_x[12:8]};
  395. snap[8] <= st_trav_x[7:0];
  396. snap[9] <= {3'd0, st_trav_x[12:8]};
  397. snap[10] <= st_fault_x;
  398. snap[11] <= {5'd0, st_state_y};
  399. snap[12] <= st_flags_y;
  400. snap[13] <= st_pct_y;
  401. snap[14] <= st_cur_y[7:0];
  402. snap[15] <= {3'd0, st_cur_y[12:8]};
  403. snap[16] <= st_tgt_y[7:0];
  404. snap[17] <= {3'd0, st_tgt_y[12:8]};
  405. snap[18] <= st_trav_y[7:0];
  406. snap[19] <= {3'd0, st_trav_y[12:8]};
  407. snap[20] <= st_fault_y;
  408. snap[21] <= {5'd0, st_state_z};
  409. snap[22] <= st_flags_z;
  410. snap[23] <= st_pct_z;
  411. snap[24] <= st_cur_z[7:0];
  412. snap[25] <= {3'd0, st_cur_z[12:8]};
  413. snap[26] <= st_tgt_z[7:0];
  414. snap[27] <= {3'd0, st_tgt_z[12:8]};
  415. snap[28] <= st_trav_z[7:0];
  416. snap[29] <= {3'd0, st_trav_z[12:8]};
  417. snap[30] <= st_fault_z;
  418. end
  419. end
  420. endmodule