board.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include "hal.h"
  14. /**
  15. * @brief PAL setup.
  16. * @details Digital I/O ports static configuration as defined in @p board.h.
  17. * This variable is used by the HAL when initializing the PAL driver.
  18. */
  19. #if HAL_USE_PAL || defined(__DOXYGEN__)
  20. const PALConfig pal_default_config =
  21. {
  22. {VAL_PIOA_ODSR, VAL_PIOA_OSR, VAL_PIOA_PUSR},
  23. #if (SAM7_PLATFORM == SAM7X128) || (SAM7_PLATFORM == SAM7X256) || \
  24. (SAM7_PLATFORM == SAM7X512) || (SAM7_PLATFORM == SAM7A3)
  25. {VAL_PIOB_ODSR, VAL_PIOB_OSR, VAL_PIOB_PUSR}
  26. #endif
  27. };
  28. #endif
  29. /*
  30. * SYS IRQ handling here.
  31. */
  32. static CH_IRQ_HANDLER(SYSIrqHandler) {
  33. CH_IRQ_PROLOGUE();
  34. if (AT91C_BASE_PITC->PITC_PISR & AT91C_PITC_PITS) {
  35. (void) AT91C_BASE_PITC->PITC_PIVR;
  36. chSysLockFromIsr();
  37. chSysTimerHandlerI();
  38. chSysUnlockFromIsr();
  39. }
  40. #if USE_SAM7_DBGU_UART
  41. if (AT91C_BASE_DBGU->DBGU_CSR &
  42. (AT91C_US_RXRDY | AT91C_US_TXRDY | AT91C_US_PARE | AT91C_US_FRAME |
  43. AT91C_US_OVRE | AT91C_US_RXBRK)) {
  44. sd_lld_serve_interrupt(&SDDBG);
  45. }
  46. #endif
  47. AT91C_BASE_AIC->AIC_EOICR = 0;
  48. CH_IRQ_EPILOGUE();
  49. }
  50. /*
  51. * Early initialization code.
  52. * This initialization must be performed just after stack setup and before
  53. * any other initialization.
  54. */
  55. void __early_init(void) {
  56. /* Watchdog disabled.*/
  57. AT91C_BASE_WDTC->WDTC_WDMR = AT91C_WDTC_WDDIS;
  58. at91sam7_clock_init();
  59. }
  60. #if HAL_USE_MMC_SPI
  61. /* Board-related functions related to the MMC_SPI driver.*/
  62. bool mmc_lld_is_card_inserted(MMCDriver *mmcp) {
  63. (void)mmcp;
  64. return !palReadPad(IOPORT2, PIOB_MMC_CP);
  65. }
  66. bool mmc_lld_is_write_protected(MMCDriver *mmcp) {
  67. (void)mmcp;
  68. return palReadPad(IOPORT2, PIOB_MMC_WP);
  69. }
  70. #endif
  71. /*
  72. * Board-specific initialization code.
  73. */
  74. void boardInit(void) {
  75. /*
  76. * LCD pins setup.
  77. */
  78. palClearPad(IOPORT2, PIOB_LCD_BL);
  79. palSetPadMode(IOPORT2, PIOB_LCD_BL, PAL_MODE_OUTPUT_PUSHPULL);
  80. palSetPad(IOPORT1, PIOA_LCD_RESET);
  81. palSetPadMode(IOPORT1, PIOA_LCD_RESET, PAL_MODE_OUTPUT_PUSHPULL);
  82. /*
  83. * Joystick and buttons setup.
  84. */
  85. palSetGroupMode(IOPORT1,
  86. PIOA_B1_MASK | PIOA_B2_MASK | PIOA_B3_MASK |
  87. PIOA_B4_MASK | PIOA_B5_MASK,
  88. 0,
  89. PAL_MODE_INPUT);
  90. palSetGroupMode(IOPORT2, PIOB_SW1_MASK | PIOB_SW2_MASK, 0, PAL_MODE_INPUT);
  91. /*
  92. * MMC/SD slot setup.
  93. */
  94. palSetGroupMode(IOPORT2,
  95. PIOB_MMC_WP_MASK | PIOB_MMC_CP_MASK,
  96. 0,
  97. PAL_MODE_INPUT);
  98. /*
  99. * PIT Initialization.
  100. */
  101. AIC_ConfigureIT(AT91C_ID_SYS,
  102. AT91C_AIC_SRCTYPE_HIGH_LEVEL | (AT91C_AIC_PRIOR_HIGHEST - 1),
  103. SYSIrqHandler);
  104. AIC_EnableIT(AT91C_ID_SYS);
  105. AT91C_BASE_PITC->PITC_PIMR = (MCK / 16 / CH_FREQUENCY) - 1;
  106. AT91C_BASE_PITC->PITC_PIMR |= AT91C_PITC_PITEN | AT91C_PITC_PITIEN;
  107. /*
  108. * RTS/CTS pins enabled for USART0 only.
  109. */
  110. AT91C_BASE_PIOA->PIO_PDR = AT91C_PA3_RTS0 | AT91C_PA4_CTS0;
  111. AT91C_BASE_PIOA->PIO_ASR = AT91C_PIO_PA3 | AT91C_PIO_PA4;
  112. AT91C_BASE_PIOA->PIO_PPUDR = AT91C_PIO_PA3 | AT91C_PIO_PA4;
  113. }