platform.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include <rtthread.h>
  2. #include <board.h>
  3. #ifdef RT_USING_LWIP
  4. #include "stm32_eth.h"
  5. #endif /* RT_USING_LWIP */
  6. #ifdef RT_USING_SPI
  7. #include "rt_stm32f10x_spi.h"
  8. #if defined(RT_USING_DFS) && defined(RT_USING_SPI_MSD)
  9. #include "spi_msd.h"
  10. #endif /* RT_USING_DFS */
  11. /*
  12. * SPI1_MOSI: PA7
  13. * SPI1_MISO: PA6
  14. * SPI1_SCK : PA5
  15. *
  16. * CS0: PA4 SD card.
  17. */
  18. static void rt_hw_spi_init(void)
  19. {
  20. #ifdef RT_USING_SPI1
  21. /* register spi bus */
  22. {
  23. static struct stm32_spi_bus stm32_spi;
  24. GPIO_InitTypeDef GPIO_InitStructure;
  25. /* Enable GPIO clock */
  26. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO,
  27. ENABLE);
  28. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  29. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  30. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  31. GPIO_Init(GPIOA, &GPIO_InitStructure);
  32. stm32_spi_register(SPI1, &stm32_spi, "spi1");
  33. }
  34. /* attach cs */
  35. {
  36. static struct rt_spi_device spi_device;
  37. static struct stm32_spi_cs spi_cs;
  38. GPIO_InitTypeDef GPIO_InitStructure;
  39. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  40. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  41. /* spi21: PG10 */
  42. spi_cs.GPIOx = GPIOA;
  43. spi_cs.GPIO_Pin = GPIO_Pin_4;
  44. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  45. GPIO_InitStructure.GPIO_Pin = spi_cs.GPIO_Pin;
  46. GPIO_SetBits(spi_cs.GPIOx, spi_cs.GPIO_Pin);
  47. GPIO_Init(spi_cs.GPIOx, &GPIO_InitStructure);
  48. rt_spi_bus_attach_device(&spi_device, "spi10", "spi1", (void*)&spi_cs);
  49. }
  50. #endif /* RT_USING_SPI1 */
  51. }
  52. #endif /* RT_USING_SPI */
  53. void rt_platform_init(void)
  54. {
  55. #ifdef RT_USING_SPI
  56. rt_hw_spi_init();
  57. #if defined(RT_USING_DFS) && defined(RT_USING_SPI_MSD)
  58. /* init sdcard driver */
  59. {
  60. extern void rt_hw_msd_init(void);
  61. GPIO_InitTypeDef GPIO_InitStructure;
  62. /* PC4 : SD Power */
  63. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  64. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  65. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  66. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  67. GPIO_Init(GPIOC, &GPIO_InitStructure);
  68. /* SD card power on. */
  69. GPIO_ResetBits(GPIOC, GPIO_Pin_4);
  70. rt_thread_delay(2);
  71. msd_init("sd0", "spi10");
  72. }
  73. #endif /* RT_USING_DFS && RT_USING_SPI_MSD */
  74. #endif // RT_USING_SPI
  75. #ifdef RT_USING_LWIP
  76. /* initialize eth interface */
  77. rt_hw_stm32_eth_init();
  78. #endif /* RT_USING_LWIP */
  79. }