| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- #include <string.h>
- #include "spi.h"
- /*
- * spi driver for MS5238 and CS1180
- */
- void spi0_init(void){
- rcu_periph_clock_enable(RCU_GPIOA);
- rcu_periph_clock_enable(RCU_GPIOB);
- rcu_periph_clock_enable(RCU_SPI0);
- //spi clk
- gpio_mode_output(GPIOB, GPIO_PUPD_NONE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_3);
- //spi MISO
- gpio_mode_input(GPIOB, GPIO_PUPD_NONE, GPIO_PIN_4);
- //spi MOSI
- gpio_mode_output(GPIOB, GPIO_PUPD_NONE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5);
- gpio_af_set(GPIOB, GPIO_AF_0, GPIO_PIN_3);
- gpio_af_set(GPIOB, GPIO_AF_0, GPIO_PIN_4);
- gpio_af_set(GPIOB, GPIO_AF_0, GPIO_PIN_5);
- //MS5238 cs
- gpio_mode_output(GPIOA, GPIO_PUPD_NONE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_15);
- ms5238_cs(1);
- //CS1180 cs
- gpio_mode_output(GPIOA, GPIO_PUPD_NONE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
- cs1180_cs(1);
- spi_parameter_struct spi_init_struct;
- /* SPI0 parameter config */
- spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
- spi_init_struct.device_mode = SPI_MASTER;;
- spi_init_struct.frame_size = SPI_FRAMESIZE_16BIT;
- spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
- spi_init_struct.nss = SPI_NSS_SOFT;
- spi_init_struct.prescale = SPI_PSC_2 ;
- spi_init_struct.endian = SPI_ENDIAN_MSB;
- spi_init(SPI0, &spi_init_struct);
- /* set crc polynomial */
- spi_crc_polynomial_set(SPI0,7);
- /* enable SPI0 */
- spi_enable(SPI0);
- }
- void spi0_deinit(void){
- spi_disable(SPI0);
- rcu_periph_clock_disable(RCU_SPI0);
- }
- #define max_wait_count 1024
- int spi0_send_uint16(uint16_t s_data, uint8_t *r_data)
- {
- /* loop while data register in not emplty */
- int count = 0;
- while (RESET == spi_i2s_flag_get(SPI0,SPI_FLAG_TBE) && count < max_wait_count){
- count ++;
- };
- if (RESET == spi_i2s_flag_get(SPI0,SPI_FLAG_TBE)){
- return -1;
- }
- /* send byte through the SPI0 peripheral */
- spi_i2s_data_transmit(SPI0,s_data);
- /* wait to receive a byte */
- count = 0;
- while(RESET == spi_i2s_flag_get(SPI0,SPI_FLAG_RBNE) && count < max_wait_count){
- count ++;
- };
- if (RESET == spi_i2s_flag_get(SPI0,SPI_FLAG_RBNE)){
- return -1;
- }
- /* return the byte read from the SPI bus */
- uint8_t r = spi_i2s_data_receive(SPI0);
- if (r_data != NULL){
- *r_data = r;
- }
- return 0;
- }
|