spi.c 4.1 KB

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