board.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * at32f4/board.c
  3. *
  4. * Board-specific setup and management.
  5. *
  6. * Written & released by Keir Fraser <keir.xen@gmail.com>
  7. *
  8. * This is free and unencumbered software released into the public domain.
  9. * See the file COPYING for more details, or visit <http://unlicense.org>.
  10. */
  11. #define gpio_led gpiob
  12. #define pin_led 13
  13. const static struct pin_mapping _msel_pins[] = {
  14. { 10, _A, 3 },
  15. { 12, _B, 9 },
  16. { 14, _A, 4 },
  17. { 16, _A, 1 },
  18. { 0, 0, 0 }
  19. };
  20. const static struct pin_mapping _user_pins[] = {
  21. { 2, _A, 6 },
  22. { 4, _A, 5 },
  23. { 6, _A, 7 },
  24. { 0, 0, 0 }
  25. };
  26. const static struct board_config _board_config = {
  27. .flippy = TRUE,
  28. .user_pins = _user_pins,
  29. .msel_pins = _msel_pins
  30. };
  31. /* Blink the activity LED to indicate fatal error. */
  32. void early_fatal(int blinks)
  33. {
  34. int i;
  35. rcc->apb2enr |= RCC_APB2ENR_IOPBEN;
  36. delay_ticks(10);
  37. gpio_configure_pin(gpio_led, pin_led, GPO_pushpull(IOSPD_LOW, HIGH));
  38. for (;;) {
  39. for (i = 0; i < blinks; i++) {
  40. gpio_write_pin(gpiob, 13, LOW);
  41. early_delay_ms(150);
  42. gpio_write_pin(gpiob, 13, HIGH);
  43. early_delay_ms(150);
  44. }
  45. early_delay_ms(2000);
  46. }
  47. }
  48. void identify_board_config(void)
  49. {
  50. uint16_t low, high;
  51. uint8_t id = 0;
  52. int i;
  53. rcc->apb2enr |= RCC_APB2ENR_IOPCEN;
  54. early_delay_us(2);
  55. /* Pull PC[15:13] low, and check which are tied HIGH. */
  56. for (i = 0; i < 3; i++)
  57. gpio_configure_pin(gpioc, 13+i, GPI_pull_down);
  58. early_delay_us(10);
  59. high = (gpioc->idr >> 13) & 7;
  60. /* Pull PC[15:13] high, and check which are tied LOW. */
  61. for (i = 0; i < 3; i++)
  62. gpio_configure_pin(gpioc, 13+i, GPI_pull_up);
  63. early_delay_us(10);
  64. low = (~gpioc->idr >> 13) & 7;
  65. /* Each PCx pin defines a 'trit': 0=float, 1=low, 2=high.
  66. * We build a 3^3 ID space from the resulting three-trit ID. */
  67. for (i = 0; i < 3; i++) {
  68. id *= 3;
  69. switch ((high>>1&2) | (low>>2&1)) {
  70. case 0: break; /* float = 0 */
  71. case 1: id += 1; break; /* LOW = 1 */
  72. case 2: id += 2; break; /* HIGH = 2 */
  73. case 3: early_fatal(1); /* cannot be tied HIGH *and* LOW! */
  74. }
  75. high <<= 1;
  76. low <<= 1;
  77. }
  78. /* Panic if the ID is unrecognised. */
  79. if (id != 0)
  80. early_fatal(2);
  81. /* Single static config. */
  82. gw_info.hw_submodel = id;
  83. board_config = &_board_config;
  84. }
  85. static void mcu_board_init(void)
  86. {
  87. gpio_pull_up_pins(gpioa, 0x0101); /* PA0,8 */
  88. gpio_pull_up_pins(gpiob, 0x1803); /* PB0-1,11-12 */
  89. gpio_pull_up_pins(gpioc, 0xffff); /* PC0-15 */
  90. /* Flippy TRK0_DISABLE output: Set inactive (LOW). */
  91. gpio_configure_pin(gpiob, 14, GPO_pushpull(IOSPD_LOW, LOW));
  92. /* /RDY input line is externally pulled up. */
  93. gpio_configure_pin(gpiob, 15, GPI_floating);
  94. }
  95. /*
  96. * Local variables:
  97. * mode: C
  98. * c-file-style: "Linux"
  99. * c-basic-offset: 4
  100. * tab-width: 4
  101. * indent-tabs-mode: nil
  102. * End:
  103. */