瀏覽代碼

add spi0 driver for ms5238&cs1180

Signed-off-by: huhui <huhui@sharkgulf.com>
huhui 5 年之前
父節點
當前提交
82719f5014
共有 6 個文件被更改,包括 117 次插入4 次删除
  1. 2 0
      Application/bsp/gpio.c
  2. 2 1
      Application/bsp/gpio.h
  3. 81 0
      Application/bsp/spi.c
  4. 12 0
      Application/bsp/spi.h
  5. 15 3
      Project/BMS.uvoptx
  6. 5 0
      Project/BMS.uvprojx

+ 2 - 0
Application/bsp/gpio.c

@@ -37,6 +37,8 @@ void gpio_init(void){
 
 	//ms5238 irq
 	gpio_mode_input(GPIOA, GPIO_PUPD_NONE, GPIO_PIN_12);
+	//detect cs1180 ready
+	gpio_mode_input(GPIOA, GPIO_PUPD_NONE, GPIO_PIN_0);	
 }
 
 

+ 2 - 1
Application/bsp/gpio.h

@@ -15,7 +15,8 @@
 #define UART1_IR_EN(x) gpio_bit_write(GPIOF,GPIO_PIN_0,(bit_status)(x))
 /*detect for charger in/out */
 #define IS_CHARGER_IN() !gpio_input_bit_get(GPIOB,GPIO_PIN_10)
-
+/*detect for CS1180 is ready */
+#define IS_CS1180_READY !!gpio_input_bit_get(GPIOA, GPIO_PIN_0)
 
 static __inline__ void gpio_mode_input(uint32_t gpio_periph, uint32_t pull_up_down, uint32_t pin){
 	gpio_mode_set(gpio_periph, GPIO_MODE_INPUT, pull_up_down, pin);

+ 81 - 0
Application/bsp/spi.c

@@ -0,0 +1,81 @@
+#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;
+}
+
+

+ 12 - 0
Application/bsp/spi.h

@@ -0,0 +1,12 @@
+#ifndef _SPI_H__
+#define _SPI_H__
+#include "gpio.h"
+void spi0_init(void);
+void spi0_deinit(void);
+int spi0_send_uint16(uint16_t s_data, uint8_t *r_data);
+
+#define cs1180_cs(x) gpio_bit_write(GPIOA, GPIO_PIN_9, (bit_status)x)
+#define ms5238_cs(x) gpio_bit_write(GPIOA, GPIO_PIN_15, (bit_status)x)
+
+#endif /* _SPI_H__ */
+

+ 15 - 3
Project/BMS.uvoptx

@@ -511,7 +511,7 @@
 
   <Group>
     <GroupName>BSP</GroupName>
-    <tvExp>0</tvExp>
+    <tvExp>1</tvExp>
     <tvExpOptDlg>0</tvExpOptDlg>
     <cbSel>0</cbSel>
     <RteFlg>0</RteFlg>
@@ -539,6 +539,18 @@
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
+    <File>
+      <GroupNumber>3</GroupNumber>
+      <FileNumber>30</FileNumber>
+      <FileType>1</FileType>
+      <tvExp>0</tvExp>
+      <tvExpOptDlg>0</tvExpOptDlg>
+      <bDave2>0</bDave2>
+      <PathWithFileName>..\Application\bsp\spi.c</PathWithFileName>
+      <FilenameWithoutPath>spi.c</FilenameWithoutPath>
+      <RteFlg>0</RteFlg>
+      <bShared>0</bShared>
+    </File>
   </Group>
 
   <Group>
@@ -557,7 +569,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>5</GroupNumber>
-      <FileNumber>30</FileNumber>
+      <FileNumber>31</FileNumber>
       <FileType>2</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -569,7 +581,7 @@
     </File>
     <File>
       <GroupNumber>5</GroupNumber>
-      <FileNumber>31</FileNumber>
+      <FileNumber>32</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>

+ 5 - 0
Project/BMS.uvprojx

@@ -538,6 +538,11 @@
               <FileType>1</FileType>
               <FilePath>..\Application\bsp\irqs.c</FilePath>
             </File>
+            <File>
+              <FileName>spi.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\Application\bsp\spi.c</FilePath>
+            </File>
           </Files>
         </Group>
         <Group>