1
0

ls1c_spi.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * File : ls1c_spi.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2017-10-23 勤为本 first version
  23. */
  24. // 硬件spi接口的头文件
  25. #ifndef __OPENLOONGSON_SPI_H
  26. #define __OPENLOONGSON_SPI_H
  27. // SPI模块编号
  28. #define LS1C_SPI_0 (0)
  29. #define LS1C_SPI_1 (1)
  30. // 片选
  31. #define LS1C_SPI_CS_0 (0)
  32. #define LS1C_SPI_CS_1 (1)
  33. #define LS1C_SPI_CS_2 (2)
  34. #define LS1C_SPI_CS_3 (3)
  35. // 时钟极性和相位
  36. #define SPI_CPOL_1 (1)
  37. #define SPI_CPOL_0 (0)
  38. #define SPI_CPHA_1 (1)
  39. #define SPI_CPHA_0 (0)
  40. // 寄存器偏移
  41. #define LS1C_SPI_SPCR_OFFSET (0) // 控制寄存器
  42. #define LS1C_SPI_SPSR_OFFSET (1) // 状态寄存器
  43. #define LS1C_SPI_TxFIFO_OFFSET (2) // 发送的数据寄存器,与接收数据寄存器的偏移相同
  44. #define LS1C_SPI_RxFIFO_OFFSET (2) // 接收的数据寄存器,与发送数据寄存器的偏移相同
  45. #define LS1C_SPI_SPER_OFFSET (3) // 外部寄存器
  46. #define LS1C_SPI_SFC_PARAM_OFFSET (4) // 参数控制寄存器
  47. #define LS1C_SPI_SFC_SOFTCS_OFFSET (5) // 片选控制寄存器
  48. #define LS1C_SPI_SFC_TIMING_OFFSET (6) // 时序控制寄存器
  49. // 寄存器SPCR中的位域
  50. #define LS1C_SPI_SPCR_SPIE_BIT (7)
  51. #define LS1C_SPI_SPCR_SPIE_MASK (0x01 << LS1C_SPI_SPCR_SPIE_BIT)
  52. #define LS1C_SPI_SPCR_SPE_BIT (6)
  53. #define LS1C_SPI_SPCR_SPE_MASK (0x01 << LS1C_SPI_SPCR_SPE_BIT)
  54. #define LS1C_SPI_SPCR_CPOL_BIT (3)
  55. #define LS1C_SPI_SPCR_CPOL_MASK (0x01 << LS1C_SPI_SPCR_CPOL_BIT)
  56. #define LS1C_SPI_SPCR_CPHA_BIT (2)
  57. #define LS1C_SPI_SPCR_CPHA_MASK (0x01 << LS1C_SPI_SPCR_CPHA_BIT)
  58. #define LS1C_SPI_SPCR_SPR_BIT (0)
  59. #define LS1C_SPI_SPCR_SPR_MASK (0x03 << LS1C_SPI_SPCR_SPR_BIT)
  60. // 寄存器SPSR中的位域
  61. #define LS1C_SPI_SPSR_SPIF_BIT (7)
  62. #define LS1C_SPI_SPSR_SPIF_MASK (0x01 << LS1C_SPI_SPSR_SPIF_BIT)
  63. #define LS1C_SPI_SPSR_WCOL_BIT (6)
  64. #define LS1C_SPI_SPSR_WCOL_MASK (0x01 << LS1C_SPI_SPSR_WCOL_BIT)
  65. // 寄存器SPER中的位域
  66. #define LS1C_SPI_SPER_SPRE_BIT (0)
  67. #define LS1C_SPI_SPER_SPRE_MASK (0x3 << LS1C_SPI_SPER_SPRE_BIT)
  68. // 寄存器SFC_SOFTCS的位域
  69. #define LS1C_SPI_SFC_SOFTCS_CSN_BIT (4)
  70. #define LS1C_SPI_SFC_SOFTCS_CSN_MASK (0x0f << LS1C_SPI_SFC_SOFTCS_CSN_BIT)
  71. #define LS1C_SPI_SFC_SOFTCS_CSEN_BIT (0)
  72. #define LS1C_SPI_SFC_SOFTCS_CSEN_MASK (0x0f << LS1C_SPI_SFC_SOFTCS_CSEN_BIT)
  73. // 发送超时的门限值
  74. #define LS1C_SPI_TX_TIMEOUT (20000)
  75. /*
  76. * 获取指定SPI模块的基地址
  77. * @SPIx SPI模块的编号
  78. */
  79. inline void *ls1c_spi_get_base(unsigned char SPIx);
  80. /*
  81. * 设置时钟
  82. * @spi_base 基地址
  83. * @max_hz 最大频率,单位hz
  84. */
  85. void ls1c_spi_set_clock(void *spi_base, unsigned long max_hz);
  86. /*
  87. * 设置通信模式(时钟极性和相位)
  88. * @spi_base 基地址
  89. * @cpol 时钟极性
  90. * @cpha 时钟相位
  91. */
  92. void ls1c_spi_set_mode(void *spi_base, unsigned char cpol, unsigned char cpha);
  93. /*
  94. * 设置指定片选为指定状态
  95. * @spi_base 基地址
  96. * @cs 片选
  97. * @new_status 片选引脚的新状态,取值为0或1,即高电平或低电平
  98. */
  99. void ls1c_spi_set_cs(void *spi_base, unsigned char cs, int new_status);
  100. /*
  101. * 通过指定SPI发送接收一个字节
  102. * 注意,在多任务的系统中,此函数需要互斥。
  103. * 即保证在和某个从设备收发某个字节的过程中,不能被切换到其它任务同时与另外的在同一个SPI总线上的从设备通信
  104. * 因为龙芯1c的每路SPI上可能接有不同的从设备,通信频率、模式等可能不同
  105. * @spi_base 基地址
  106. * @tx_ch 待发送的数据
  107. * @ret 收到的数据
  108. */
  109. unsigned char ls1c_spi_txrx_byte(void *spi_base, unsigned char tx_ch);
  110. /*
  111. * 打印指定SPI模块的所有寄存器的值
  112. * @spi_base 基地址
  113. */
  114. void ls1c_spi_print_all_regs_info(void *spi_base);
  115. #endif