ls1c_spi.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. typedef enum
  29. {
  30. LS1C_SPI_0 = 0,
  31. LS1C_SPI_1,
  32. }ls1c_spi_t;
  33. // 片选
  34. #define LS1C_SPI_INVALID_CS (-1)
  35. #define LS1C_SPI_CS_0 (0)
  36. #define LS1C_SPI_CS_1 (1)
  37. #define LS1C_SPI_CS_2 (2)
  38. #define LS1C_SPI_CS_3 (3)
  39. // 时钟极性和相位
  40. #define SPI_CPOL_1 (1)
  41. #define SPI_CPOL_0 (0)
  42. #define SPI_CPHA_1 (1)
  43. #define SPI_CPHA_0 (0)
  44. // 硬件SPI信息
  45. typedef struct
  46. {
  47. ls1c_spi_t SPIx; // SPI模块编号
  48. unsigned long max_speed_hz; // 最大通信速度,单位hz
  49. unsigned char cs; // 片选
  50. unsigned char cpol; // 时钟极性
  51. unsigned char cpha; // 时钟相位
  52. }ls1c_spi_info_t;
  53. /*
  54. * 初始化指定SPI模块
  55. * @spi_info_p SPI模块信息
  56. */
  57. void spi_init(ls1c_spi_info_t *spi_info_p);
  58. /*
  59. * 设置指定片选为指定状态
  60. * @spi_info_p SPI模块信息
  61. * @new_status 片选引脚的新状态,取值为0或1,即高电平或低电平
  62. */
  63. void spi_set_cs(ls1c_spi_info_t *spi_info_p, int new_status);
  64. /*
  65. * 通过指定SPI发送接收一个字节
  66. * 注意,在多任务的系统中,此函数需要互斥。
  67. * 即保证在和某个从设备收发某个字节的过程中,不能被切换到其它任务同时与另外的在同一个SPI总线上的从设备通信
  68. * 因为龙芯1c的每路SPI上可能接有不同的从设备,通信频率、模式等可能不同
  69. * @spi_info_p SPI接口
  70. * @tx_ch 待发送的数据
  71. * @ret 收到的数据
  72. */
  73. unsigned char spi_txrx_byte(ls1c_spi_info_t *spi_info_p, unsigned char tx_ch);
  74. /*
  75. * 打印指定SPI模块的所有寄存器的值
  76. * @spi_info_p SPI模块信息
  77. */
  78. void spi_print_all_regs_info(ls1c_spi_info_t *spi_info_p);
  79. #endif