/Solutions/STM32F429IDISCOVERY/DeviceCode/Init/IO_Init.cpp

https://github.com/NETMF/netmf-interpreter · C++ · 105 lines · 42 code · 17 blank · 46 comment · 1 complexity · ab23b973b5672c52f10817e4992a12a4 MD5 · raw file

  1. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  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 http://www.apache.org/licenses/LICENSE-2.0
  6. //
  7. // Copyright (c) Microsoft Corporation. All rights reserved.
  8. // Implementation for the MCBSTM32F400 board (STM32F4): Copyright (c) Oberon microsystems, Inc.
  9. //
  10. // *** STM32F429IDISCOVERY Board specific IO Port Initialization ***
  11. //
  12. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  13. #include <tinyhal.h>
  14. #include "..\..\..\..\DeviceCode\Targets\Native\STM32F4\DeviceCode\stm32f4xx.h"
  15. // Define the generic port table, only one generic extensionn port type supported
  16. // and that is the ITM hardware trace port on Channel 0.
  17. extern GenericPortTableEntry const Itm0GenericPort;
  18. extern GenericPortTableEntry const* const g_GenericPorts[TOTAL_GENERIC_PORTS] = { &Itm0GenericPort };
  19. // Workaround, since CPU_GPIO_DisablePin() does not correctly initialize pin speeds
  20. extern void STM32F4_GPIO_Pin_Config(GPIO_PIN pin, UINT32 mode, GPIO_RESISTOR resistor, UINT32 alternate);
  21. /////////////////////////////////////////////////////////////////////////////
  22. // IS42S16400J 64Mb (1M x 16 x 4 bits)
  23. void __section("SectionForBootstrapOperations") InitSram()
  24. {
  25. // FMC - 429, vs. FMSC - 40x only
  26. RCC->AHB3ENR |= RCC_AHB3ENR_FMCEN;
  27. //#if defined (STM32F427_437xx) || defined (STM32F429_439xx)
  28. //#define RCC_AHB3ENR_FMCEN ((uint32_t)0x00000001)
  29. //#endif /* STM32F427_437xx || STM32F429_439xx */
  30. /* Setup SRAM control register*/
  31. //sram_fsmc_bcr1 |= FSMC_BCR1_MWID_0; /* 16-bit data bus */
  32. //sram_fsmc_bcr1 |= FSMC_BCR1_WREN; /* Write enable */
  33. //
  34. //FSMC_Bank1->BTCR[4] = sram_fsmc_bcr1;
  35. //
  36. ///* Setup SRAM timing register*/
  37. //sram_fsmc_btr1 |= 0x02 << 0; /* Address setup phase duration should be >8nS. With the value set to 0x02 and a 168Mhz clock, the duration becomes 11.9nS */
  38. //sram_fsmc_btr1 |= 0x01 << 8; /* Data-phase duration should be >8nS. With the value set to 0x01 and a 168Mhz clock, the duration becomes 11.9nS */
  39. //
  40. //FSMC_Bank1->BTCR[5] = sram_fsmc_btr1;
  41. //
  42. ///* Enable memory bank 3 for SRAM*/
  43. //FSMC_Bank1->BTCR[4] |= FSMC_BCR1_MBKEN;
  44. }
  45. void __section("SectionForBootstrapOperations") BootstrapCode_GPIO()
  46. {
  47. // GPIO pins connected to SRAM
  48. const BYTE pins[] = {
  49. // PB5, 6
  50. PORT_PIN(GPIO_PORTB, 5), PORT_PIN(GPIO_PORTB, 6),
  51. // PC0
  52. PORT_PIN(GPIO_PORTC, 0),
  53. // PD0, 1, 8-10, 14, 15
  54. PORT_PIN(GPIO_PORTD, 0), PORT_PIN(GPIO_PORTD, 1), PORT_PIN(GPIO_PORTD, 8), PORT_PIN(GPIO_PORTD, 9),
  55. PORT_PIN(GPIO_PORTD, 10), PORT_PIN(GPIO_PORTD, 14), PORT_PIN(GPIO_PORTD, 15),
  56. // PE0, 1, 7-15
  57. PORT_PIN(GPIO_PORTE, 0), PORT_PIN(GPIO_PORTE, 1), PORT_PIN(GPIO_PORTE, 7), PORT_PIN(GPIO_PORTE, 8),
  58. PORT_PIN(GPIO_PORTE, 9), PORT_PIN(GPIO_PORTE, 10), PORT_PIN(GPIO_PORTE, 11), PORT_PIN(GPIO_PORTE, 12),
  59. PORT_PIN(GPIO_PORTE, 13), PORT_PIN(GPIO_PORTE, 14), PORT_PIN(GPIO_PORTE, 15),
  60. // PF0-5, 11-15
  61. PORT_PIN(GPIO_PORTF, 0), PORT_PIN(GPIO_PORTF, 1), PORT_PIN(GPIO_PORTF, 2), PORT_PIN(GPIO_PORTF, 3),
  62. PORT_PIN(GPIO_PORTF, 4), PORT_PIN(GPIO_PORTF, 5), PORT_PIN(GPIO_PORTF, 11), PORT_PIN(GPIO_PORTF, 12),
  63. PORT_PIN(GPIO_PORTF, 13), PORT_PIN(GPIO_PORTF, 14), PORT_PIN(GPIO_PORTF, 15),
  64. // PG0, 1, 4, 5, 8, 15
  65. PORT_PIN(GPIO_PORTG, 0), PORT_PIN(GPIO_PORTG, 1), PORT_PIN(GPIO_PORTG, 4), PORT_PIN(GPIO_PORTG, 5),
  66. PORT_PIN(GPIO_PORTG, 8), PORT_PIN(GPIO_PORTG, 15)
  67. };
  68. // Enable GPIO clocks for ports A - G
  69. RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOBEN | RCC_AHB1ENR_GPIOCEN
  70. | RCC_AHB1ENR_GPIODEN | RCC_AHB1ENR_GPIOEEN | RCC_AHB1ENR_GPIOFEN
  71. | RCC_AHB1ENR_GPIOGEN;
  72. // Initialize SRAM pins
  73. for(int i = 0; i < ARRAYSIZE(pins); i++)
  74. {
  75. const uint32_t pinConfig = 0x3C2; // Speed 100Mhz, AF12 FSMC, Alternate Mode
  76. const uint32_t pinMode = pinConfig & 0xF;
  77. const GPIO_ALT_MODE alternateMode = (GPIO_ALT_MODE)pinConfig;
  78. const GPIO_RESISTOR resistorConfig = RESISTOR_PULLUP;
  79. // TODO: Why RESISTOR_PULLUP? Why not disabled?
  80. CPU_GPIO_ReservePin(pins[i], TRUE);
  81. CPU_GPIO_DisablePin(pins[i], resistorConfig, 0, alternateMode);
  82. STM32F4_GPIO_Pin_Config(pins[i], pinMode, resistorConfig, pinConfig); // Workaround, since CPU_GPIO_DisablePin() does not correctly initialize pin speeds
  83. }
  84. InitSram();
  85. // TODO: Restore at the end of bootloader?
  86. CPU_GPIO_EnableOutputPin(LED3, FALSE);
  87. CPU_GPIO_EnableOutputPin(LED4, FALSE);
  88. }