spi.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include <string.h>
  2. #include "spi.h"
  3. /*
  4. * spi driver for ML5238 and CS1180
  5. */
  6. void spi0_init(void){
  7. rcu_periph_clock_enable(RCU_GPIOA);
  8. rcu_periph_clock_enable(RCU_GPIOB);
  9. rcu_periph_clock_enable(RCU_SPI0);
  10. //spi clk
  11. gpio_mode_output(GPIOB, GPIO_PUPD_NONE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_3);
  12. //spi MISO
  13. gpio_mode_input(GPIOB, GPIO_PUPD_NONE, GPIO_PIN_4);
  14. //spi MOSI
  15. gpio_mode_output(GPIOB, GPIO_PUPD_NONE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5);
  16. gpio_af_set(GPIOB, GPIO_AF_0, GPIO_PIN_3);
  17. gpio_af_set(GPIOB, GPIO_AF_0, GPIO_PIN_4);
  18. gpio_af_set(GPIOB, GPIO_AF_0, GPIO_PIN_5);
  19. //ML5238 cs
  20. gpio_mode_output(GPIOA, GPIO_PUPD_NONE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_15);
  21. ml5238_cs(1);
  22. spi_parameter_struct spi_init_struct;
  23. /* SPI0 parameter config */
  24. spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
  25. spi_init_struct.device_mode = SPI_MASTER;
  26. spi_init_struct.frame_size = SPI_FRAMESIZE_16BIT;
  27. spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
  28. spi_init_struct.nss = SPI_NSS_SOFT;
  29. spi_init_struct.prescale = SPI_PSC_2 ;
  30. spi_init_struct.endian = SPI_ENDIAN_MSB;
  31. spi_init(SPI0, &spi_init_struct);
  32. /* set crc polynomial */
  33. spi_crc_polynomial_set(SPI0,7);
  34. /* enable SPI0 */
  35. spi_enable(SPI0);
  36. }
  37. void spi0_deinit(void){
  38. spi_disable(SPI0);
  39. rcu_periph_clock_disable(RCU_SPI0);
  40. }
  41. void spi1_init(void){
  42. rcu_periph_clock_enable(RCU_GPIOA);
  43. rcu_periph_clock_enable(RCU_GPIOB);
  44. rcu_periph_clock_enable(RCU_SPI1);
  45. //spi clk
  46. gpio_mode_output(GPIOB, GPIO_PUPD_NONE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13);
  47. //spi MISO
  48. gpio_mode_input(GPIOB, GPIO_PUPD_NONE, GPIO_PIN_14);
  49. //spi MOSI
  50. gpio_mode_output(GPIOB, GPIO_PUPD_NONE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_15);
  51. gpio_af_set(GPIOB, GPIO_AF_0, GPIO_PIN_13);
  52. gpio_af_set(GPIOB, GPIO_AF_0, GPIO_PIN_14);
  53. gpio_af_set(GPIOB, GPIO_AF_0, GPIO_PIN_15);
  54. //CS1180 cs
  55. gpio_mode_output(GPIOA, GPIO_PUPD_NONE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
  56. cs1180_cs(1);
  57. spi_parameter_struct spi_init_struct;
  58. /* SPI1 parameter config */
  59. spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
  60. spi_init_struct.device_mode = SPI_MASTER;
  61. spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
  62. spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_2EDGE;
  63. spi_init_struct.nss = SPI_NSS_SOFT;
  64. spi_init_struct.prescale = SPI_PSC_128 ;
  65. spi_init_struct.endian = SPI_ENDIAN_MSB;
  66. spi_init(SPI1, &spi_init_struct);
  67. /* set crc polynomial */
  68. spi_crc_polynomial_set(SPI1,7);
  69. /* enable SPI1 */
  70. spi_enable(SPI1);
  71. }
  72. void spi1_deinit(void){
  73. spi_disable(SPI1);
  74. rcu_periph_clock_disable(RCU_SPI1);
  75. }
  76. #define max_wait_count 1024
  77. int spi0_send_uint16(uint16_t s_data, uint8_t *r_data)
  78. {
  79. /* loop while data register in not emplty */
  80. int count = 0;
  81. while (RESET == spi_i2s_flag_get(SPI0,SPI_FLAG_TBE) && count < max_wait_count){
  82. count ++;
  83. };
  84. if (RESET == spi_i2s_flag_get(SPI0,SPI_FLAG_TBE)){
  85. return -1;
  86. }
  87. /* send byte through the SPI0 peripheral */
  88. spi_i2s_data_transmit(SPI0,s_data);
  89. /* wait to receive a byte */
  90. count = 0;
  91. while(RESET == spi_i2s_flag_get(SPI0,SPI_FLAG_RBNE) && count < max_wait_count){
  92. count ++;
  93. };
  94. if (RESET == spi_i2s_flag_get(SPI0,SPI_FLAG_RBNE)){
  95. return -1;
  96. }
  97. /* return the byte read from the SPI bus */
  98. uint8_t r = spi_i2s_data_receive(SPI0);
  99. if (r_data != NULL){
  100. *r_data = r;
  101. }
  102. return 0;
  103. }
  104. int spi1_send_byte(uint8_t byte, uint8_t *r_data)
  105. {
  106. /* loop while data register in not emplty */
  107. int count = 0;
  108. while (RESET == spi_i2s_flag_get(SPI1,SPI_FLAG_TBE) && count < max_wait_count){
  109. count ++;
  110. };
  111. if (RESET == spi_i2s_flag_get(SPI1,SPI_FLAG_TBE)){
  112. return -1;
  113. }
  114. /* send byte through the SPI1 peripheral */
  115. spi_i2s_data_transmit(SPI1,byte);
  116. /* wait to receive a byte */
  117. count = 0;
  118. while(RESET == spi_i2s_flag_get(SPI1,SPI_FLAG_RBNE) && count < max_wait_count){
  119. count ++;
  120. };
  121. if (RESET == spi_i2s_flag_get(SPI1,SPI_FLAG_RBNE)){
  122. return -1;
  123. }
  124. /* return the byte read from the SPI bus */
  125. uint8_t r = spi_i2s_data_receive(SPI1);
  126. if (r_data != NULL){
  127. *r_data = r;
  128. }
  129. return 0;
  130. }