layer.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * layer.c - PCB layers on a pad
  3. *
  4. * Written 2009-2012 by Werner Almesberger
  5. * Copyright 2009-2012 by Werner Almesberger
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. /*
  13. * We don't reject solder paste pads that don't cover anything yet.
  14. * That way, things can be constructed step by step without getting blue
  15. * screens all the time.
  16. */
  17. #include <stdlib.h>
  18. #include "error.h"
  19. #include "overlap.h"
  20. #include "inst.h"
  21. #include "obj.h"
  22. #include "layer.h"
  23. /*
  24. * Shorthands for the layers we use in a general sense.
  25. */
  26. #define LAYER_COPPER_TOP (1 << layer_top)
  27. #define LAYER_PASTE_TOP (1 << layer_paste_top)
  28. #define LAYER_MASK_TOP (1 << layer_mask_top)
  29. #define LAYER_COPPER_BOTTOM (1 << layer_bottom)
  30. #define LAYER_PASTE_BOTTOM (1 << layer_paste_bottom)
  31. #define LAYER_MASK_BOTTOM (1 << layer_mask_bottom)
  32. /* ----- Conversion between pad types and layer sets ----------------------- */
  33. layer_type pad_type_to_layers(enum pad_type type)
  34. {
  35. layer_type layers = 0;
  36. switch (type) {
  37. case pt_normal:
  38. layers = LAYER_PASTE_TOP;
  39. /* fall through */
  40. case pt_bare:
  41. layers |= LAYER_COPPER_TOP | LAYER_MASK_TOP;
  42. break;
  43. case pt_trace:
  44. layers = LAYER_COPPER_TOP;
  45. break;
  46. case pt_paste:
  47. layers = LAYER_PASTE_TOP;
  48. break;
  49. case pt_mask:
  50. layers = LAYER_MASK_TOP;
  51. break;
  52. default:
  53. abort();
  54. }
  55. return layers;
  56. }
  57. enum pad_type layers_to_pad_type(layer_type layers)
  58. {
  59. if (layers & LAYER_COPPER_TOP) {
  60. if (layers & LAYER_PASTE_TOP)
  61. return pt_normal;
  62. if (layers & LAYER_MASK_TOP)
  63. return pt_bare;
  64. return pt_trace;
  65. } else {
  66. if (layers & LAYER_PASTE_TOP)
  67. return pt_paste;
  68. if (layers & LAYER_MASK_TOP)
  69. return pt_mask;
  70. abort();
  71. }
  72. }
  73. const char *pad_type_name(enum pad_type type)
  74. {
  75. switch (type) {
  76. case pt_normal:
  77. return "normal";
  78. case pt_bare:
  79. return "bare";
  80. case pt_trace:
  81. return "trace";
  82. case pt_paste:
  83. return "paste";
  84. case pt_mask:
  85. return "mask";
  86. default:
  87. abort();
  88. }
  89. }
  90. /* ----- layers in mechanical holes ---------------------------------------- */
  91. layer_type mech_hole_layers(void)
  92. {
  93. return LAYER_MASK_TOP | LAYER_MASK_BOTTOM;
  94. }
  95. /* ----- Refine layers after instantiation --------------------------------- */
  96. static int refine_overlapping(struct inst *copper, struct inst *other)
  97. {
  98. if (other->u.pad.layers & LAYER_PASTE_TOP) {
  99. copper->u.pad.layers &= ~LAYER_PASTE_TOP;
  100. if (!inside(other, copper)) {
  101. fail("solder paste without copper underneath "
  102. "(\"%s\" line %d, \"%s\" line %d)",
  103. copper->u.pad.name, copper->obj->lineno,
  104. other->u.pad.name, other->obj->lineno);
  105. instantiation_error = other->obj;
  106. return 0;
  107. }
  108. }
  109. if (other->u.pad.layers & LAYER_MASK_TOP)
  110. copper->u.pad.layers &= ~LAYER_MASK_TOP;
  111. return 1;
  112. }
  113. static int refine_copper(const struct pkg *pkg_copper, struct inst *copper,
  114. enum allow_overlap allow)
  115. {
  116. const struct pkg *pkg;
  117. struct inst *other;
  118. for (pkg = pkgs; pkg; pkg = pkg->next) {
  119. /*
  120. * Pads in distinct packages can happily coexist.
  121. */
  122. if (pkg != pkgs && pkg_copper != pkgs && pkg_copper != pkg)
  123. continue;
  124. for (other = pkg->insts[ip_pad_copper]; other;
  125. other = other->next)
  126. if (copper != other && overlap(copper, other, allow)) {
  127. fail("overlapping copper pads "
  128. "(\"%s\" line %d, \"%s\" line %d)",
  129. copper->u.pad.name, copper->obj->lineno,
  130. other->u.pad.name, other->obj->lineno);
  131. instantiation_error = copper->obj;
  132. return 0;
  133. }
  134. for (other = pkg->insts[ip_pad_special]; other;
  135. other = other->next)
  136. if (overlap(copper, other, ao_none))
  137. if (!refine_overlapping(copper, other))
  138. return 0;
  139. }
  140. return 1;
  141. }
  142. static void mirror_layers(layer_type *layers)
  143. {
  144. if (*layers & LAYER_COPPER_TOP)
  145. *layers |= LAYER_COPPER_BOTTOM;
  146. if (*layers & LAYER_PASTE_TOP)
  147. *layers |= LAYER_PASTE_BOTTOM;
  148. if (*layers & LAYER_MASK_TOP)
  149. *layers |= LAYER_MASK_BOTTOM;
  150. }
  151. int refine_layers(enum allow_overlap allow)
  152. {
  153. const struct pkg *pkg;
  154. struct inst *copper;
  155. for (pkg = pkgs; pkg; pkg = pkg->next)
  156. for (copper = pkg->insts[ip_pad_copper]; copper;
  157. copper = copper->next) {
  158. if (!refine_copper(pkg, copper, allow))
  159. return 0;
  160. if (copper->u.pad.hole)
  161. mirror_layers(&copper->u.pad.layers);
  162. }
  163. return 1;
  164. }