Bläddra i källkod

add ml5238 & cs1180, not completed

Signed-off-by: huhui <huhui@sharkgulf.com>
huhui 5 år sedan
förälder
incheckning
8cdaeac87a

+ 57 - 0
Application/bsp/cs1180.c

@@ -0,0 +1,57 @@
+#include <string.h>
+#include "spi.h"
+#include "cs1180.h"
+#define CS1180_RDATA    0X01
+#define CS1180_RDATAC   0X03
+#define CS1180_STOPC    0x0f
+#define CS1180_RREG     0x10
+#define CS1180_WREG     0x05
+#define CS1180_CALSELF  0xf0
+#define CS1180_OCALSELF 0xf1
+#define CS1180_SLFGCAL  0xf2
+#define CS1180_OCALSYS  0xf3
+#define CS1180_GCALSYS  0xf4
+#define CS1180_WAKEUP   0xfb
+#define CS1180_SYNC     0xfc
+#define CS1180_SLEEP    0xfd
+#define CS1180_RESET    0xfe
+
+
+static void _spi_write_data(uint8_t data);
+static uint8_t _spi_read_data(uint8_t data);
+
+
+void cs1180_init(void){
+	spi1_init();
+	
+	_spi_read_data(0xFF);
+}
+
+static void _spi_write_data(uint8_t data){
+	spi1_send_byte(data, NULL);
+}
+
+static uint8_t _spi_read_data(uint8_t data){
+	uint8_t r_data = 0xFF;
+	spi1_send_byte(data, &r_data);
+	return r_data;
+}
+
+void cs1180_osalsys(void)
+{
+		//------×ÔУ׼
+	cs1180_cs(0);
+	_spi_write_data(CS1180_OCALSYS);
+	cs1180_cs(1);
+}
+
+void cs1180_calibSelf(void)
+{
+		//------×ÔУ׼
+	cs1180_cs(0);
+	_spi_write_data(CS1180_CALSELF);
+	cs1180_cs(1);
+}
+
+
+

+ 7 - 0
Application/bsp/cs1180.h

@@ -0,0 +1,7 @@
+#ifndef _CS1180_H__
+#define _CS1180_H__
+
+void cs1180_init(void);
+
+#endif /* _CS1180_H__ */
+

+ 5 - 1
Application/bsp/gpio.c

@@ -24,6 +24,7 @@ void gpio_init(void){
 	gpio_mode_output(GPIOB, GPIO_PUPD_NONE, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_2);
 	//aux 12v lock detect
 	gpio_mode_input(GPIOB, GPIO_PUPD_NONE, GPIO_PIN_11);
+#if 0	
 	//LED 0,1,2
 	gpio_mode_output(GPIOB, GPIO_PUPD_NONE, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_12);
 	gpio_mode_output(GPIOB, GPIO_PUPD_NONE, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_13);
@@ -31,7 +32,10 @@ void gpio_init(void){
 	//LED3,4
 	gpio_mode_output(GPIOA, GPIO_PUPD_NONE, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_8);
 	gpio_mode_output(GPIOA, GPIO_PUPD_NONE, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_9);
-	
+#endif
+	//cs1180 power enable
+	gpio_mode_output(GPIOB, GPIO_PUPD_NONE, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_12);
+
 	//12v DC-DC enable
 	gpio_mode_output(GPIOA, GPIO_PUPD_NONE, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_11);
 

+ 3 - 1
Application/bsp/gpio.h

@@ -15,8 +15,10 @@
 #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)
+/*power switch for cs110, low active */
+#define CS1180_PWR_ENABLE(x) gpio_bit_write(GPIOB,GPIO_PIN_12, x==1?RESET:SET)
 /*detect for CS1180 is ready */
-#define IS_CS1180_READY !!gpio_input_bit_get(GPIOA, GPIO_PIN_0)
+#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);

+ 36 - 0
Application/bsp/ml5238.c

@@ -0,0 +1,36 @@
+#include <string.h>
+#include "spi.h"
+#include "ml5238.h"
+#include "ml5238_reg.h"
+
+void ml5238_init(void){
+	spi0_init();
+	ml5238_softreset();
+}
+
+void ml5238_softreset(void)
+{
+	unsigned char i;
+	
+	for(i=0u;i<0x0Au;i++)
+	{
+		ml5238_write((uint8_t)(ML5238_VMON + i), 0x00u);	
+	}
+}
+
+
+int ml5238_write(uint8_t regaddr, uint8_t data){
+	uint16_t send_data=(((uint16_t)regaddr)<<(0x08+1u))|((uint16_t)data);
+	ml5238_cs(0);
+	int ret = spi0_send_uint16(send_data, NULL);
+	ml5238_cs(1);
+	return ret;
+}
+
+int ml5238_read(uint8_t regaddr, uint8_t *data){
+	uint16_t send_data=((((uint16_t)regaddr)<<(0x08+1u))|0x0100u)|((uint16_t)0x00u);
+	ml5238_cs(0);
+	int ret = spi0_send_uint16(send_data, data);
+	ml5238_cs(1);
+	return ret;
+}

+ 9 - 0
Application/bsp/ml5238.h

@@ -0,0 +1,9 @@
+#ifndef _ML5238_H__
+#define _ML5238_H__
+void ml5238_init(void);
+void ml5238_softreset(void);
+int ml5238_write(uint8_t regaddr, uint8_t data);
+int ml5238_read(uint8_t regaddr, uint8_t *data);
+
+#endif /* _ML5238_H__ */
+

+ 140 - 0
Application/bsp/ml5238_reg.h

@@ -0,0 +1,140 @@
+/*****************************************************************************
+	ml5238_reg.h
+
+	Copyright (C) 2012 LAPIS Semiconductor Co., Ltd.
+	All rights reserved.
+
+	LAPIS Semiconductor shall not be liable for any direct, indirect, 
+	consequential or incidental damages arising from using or modifying this 
+	program.
+
+    History
+    2012.11.20  ver.2.00
+    2012.09.13  ver.1.00
+******************************************************************************/
+#ifndef _ML5238_REG_H_
+#define _ML5238_REG_H_
+
+#define ML5238_NOOP			(0x00u)
+#define ML5238_VMON			(0x01u)
+#define ML5238_IMON			(0x02u)
+#define ML5238_FET			(0x03u)
+#define ML5238_PSENSE		(0x04u)
+#define ML5238_RSENSE		(0x05u)
+#define ML5238_POWER		(0x06u)
+#define ML5238_STATUS		(0x07u)
+#define ML5238_CBALH		(0x08u)
+#define ML5238_CBALL		(0x09u)
+#define ML5238_SETSC		(0x0Au)
+
+/**********************************
+           NOOP(0x00)
+**********************************/
+#define NOOP_NO0	(0x01u)
+#define NOOP_NO1	(0x02u)
+#define NOOP_NO2	(0x04u)
+#define NOOP_NO3	(0x08u)
+#define NOOP_NO4	(0x10u)
+#define NOOP_NO5	(0x20u)
+#define NOOP_NO6	(0x40u)
+#define NOOP_NO7	(0x80u)
+
+/**********************************
+           VMON(0x01)
+**********************************/
+#define VMON_CN0	(0x01u)
+#define VMON_CN1	(0x02u)
+#define VMON_CN2	(0x04u)
+#define VMON_CN3	(0x08u)
+#define VMON_OUT	(0x10u)
+
+/**********************************
+           IMON(0x02)
+**********************************/
+#define IMON_GIM	(0x01u)
+#define IMON_ZERO	(0x02u)
+#define IMON_GCAL0	(0x04u)
+#define IMON_GCAL1	(0x08u)
+#define IMON_OUT	(0x10u)
+
+/**********************************
+           FET(0x03)
+**********************************/
+#define FET_DF		(0x01u)
+#define FET_CF		(0x02u)
+#define FET_DRV		(0x10u)
+
+/**********************************
+           PSENSE(0x04)
+**********************************/
+#define PSENSE_PSL	(0x01u)
+#define PSENSE_RPSL	(0x02u)
+#define PSENSE_IPSL	(0x04u)
+#define PSENSE_EPSL	(0x08u)
+#define PSENSE_PSH	(0x10u)
+#define PSENSE_RPSH	(0x20u)
+#define PSENSE_IPSH	(0x40u)
+#define PSENSE_EPSH	(0x80u)
+
+/**********************************
+           RSENSE(0x05)
+**********************************/
+#define RSENSE_RS	(0x01u)
+#define RSENSE_RRS	(0x02u)
+#define RSENSE_IRS	(0x04u)
+#define RSENSE_ERS	(0x08u)
+#define RSENSE_SC	(0x10u)
+#define RSENSE_RSC	(0x20u)
+#define RSENSE_ISC	(0x40u)
+#define RSENSE_ESC	(0x80u)
+
+/**********************************
+           POWER(0x06)
+**********************************/
+#define POWER_PSV	(0x01u)
+#define POWER_PDWN	(0x10u)
+#define POWER_PUPIN	(0x80u)
+
+/**********************************
+           STATUS(0x07)
+**********************************/
+#define STATUS_DF	(0x01u)
+#define STATUS_CF	(0x02u)
+#define STATUS_PSV	(0x04u)
+#define STATUS_INT	(0x08u)
+#define STATUS_RPSL	(0x10u)
+#define STATUS_RPSH	(0x20u)
+#define STATUS_RRS	(0x40u)
+#define STATUS_RSC	(0x80u)
+
+/**********************************
+           CBALH(0x08)
+**********************************/
+#define CBALH_SW9	(0x01u)
+#define CBALH_SW10	(0x02u)
+#define CBALH_SW11	(0x04u)
+#define CBALH_SW12	(0x08u)
+#define CBALH_SW13	(0x10u)
+#define CBALH_SW14	(0x20u)
+#define CBALH_SW15	(0x40u)
+#define CBALH_SW16	(0x80u)
+
+/**********************************
+           CBALL(0x09)
+**********************************/
+#define CBALL_SW1	(0x01u)
+#define CBALL_SW2	(0x02u)
+#define CBALL_SW3	(0x04u)
+#define CBALL_SW4	(0x08u)
+#define CBALL_SW5	(0x10u)
+#define CBALL_SW6	(0x20u)
+#define CBALL_SW7	(0x40u)
+#define CBALL_SW8	(0x80u)
+
+/**********************************
+           SETSC(0x0A)
+**********************************/
+#define SETSC_SC0	(0x01u)
+#define SETSC_SC1	(0x02u)
+
+#endif /*_ML5238_REG_H_*/

+ 77 - 8
Application/bsp/spi.c

@@ -1,7 +1,7 @@
 #include <string.h>
 #include "spi.h"
 /*
-* spi driver for MS5238 and CS1180
+* spi driver for ML5238 and CS1180
 */
 void spi0_init(void){
 	rcu_periph_clock_enable(RCU_GPIOA);
@@ -17,18 +17,15 @@ void spi0_init(void){
 	gpio_af_set(GPIOB, GPIO_AF_0, GPIO_PIN_4);
 	gpio_af_set(GPIOB, GPIO_AF_0, GPIO_PIN_5);
 
-	//MS5238 cs
+	//ML5238 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);
+	ml5238_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.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;
@@ -42,12 +39,54 @@ void spi0_init(void){
     spi_enable(SPI0);	
 }
 
-
 void spi0_deinit(void){
 	spi_disable(SPI0);
 	rcu_periph_clock_disable(RCU_SPI0);
 }
 
+void spi1_init(void){
+	rcu_periph_clock_enable(RCU_GPIOA);
+	rcu_periph_clock_enable(RCU_GPIOB);
+	rcu_periph_clock_enable(RCU_SPI1);
+	//spi clk
+	gpio_mode_output(GPIOB, GPIO_PUPD_NONE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13);
+	//spi MISO
+	gpio_mode_input(GPIOB, GPIO_PUPD_NONE, GPIO_PIN_14);
+	//spi MOSI
+	gpio_mode_output(GPIOB, GPIO_PUPD_NONE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_15);
+	gpio_af_set(GPIOB, GPIO_AF_0, GPIO_PIN_13);
+	gpio_af_set(GPIOB, GPIO_AF_0, GPIO_PIN_14);
+	gpio_af_set(GPIOB, GPIO_AF_0, GPIO_PIN_15);
+	
+	//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;
+
+    /* SPI1 parameter config */
+    spi_init_struct.trans_mode           = SPI_TRANSMODE_FULLDUPLEX;
+    spi_init_struct.device_mode          = SPI_MASTER;
+    spi_init_struct.frame_size           = SPI_FRAMESIZE_8BIT;
+    spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_2EDGE;
+    spi_init_struct.nss                  = SPI_NSS_SOFT;
+    spi_init_struct.prescale             = SPI_PSC_128 ;
+    spi_init_struct.endian               = SPI_ENDIAN_MSB;
+    spi_init(SPI1, &spi_init_struct);
+
+    /* set crc polynomial */
+    spi_crc_polynomial_set(SPI1,7);
+    /* enable SPI1 */
+    spi_enable(SPI1);	
+
+}
+
+void spi1_deinit(void){
+	spi_disable(SPI1);
+	rcu_periph_clock_disable(RCU_SPI1);
+}
+
+
 #define max_wait_count 1024
 int spi0_send_uint16(uint16_t s_data, uint8_t *r_data)
 {
@@ -79,3 +118,33 @@ int spi0_send_uint16(uint16_t s_data, uint8_t *r_data)
 }
 
 
+int spi1_send_byte(uint8_t byte, uint8_t *r_data)
+{
+    /* loop while data register in not emplty */
+	int count = 0;
+    while (RESET == spi_i2s_flag_get(SPI1,SPI_FLAG_TBE) && count < max_wait_count){
+		count ++;
+	};
+	if (RESET == spi_i2s_flag_get(SPI1,SPI_FLAG_TBE)){
+		return -1;
+	}
+    /* send byte through the SPI1 peripheral */
+    spi_i2s_data_transmit(SPI1,byte);
+
+    /* wait to receive a byte */
+	count = 0;
+    while(RESET == spi_i2s_flag_get(SPI1,SPI_FLAG_RBNE) && count < max_wait_count){
+		count ++;
+	};
+	if (RESET == spi_i2s_flag_get(SPI1,SPI_FLAG_RBNE)){
+		return -1;
+	}
+    /* return the byte read from the SPI bus */
+	uint8_t r = spi_i2s_data_receive(SPI1);
+	if (r_data != NULL){
+		*r_data = r;
+	}
+    return 0;
+}
+
+

+ 5 - 1
Application/bsp/spi.h

@@ -5,8 +5,12 @@ void spi0_init(void);
 void spi0_deinit(void);
 int spi0_send_uint16(uint16_t s_data, uint8_t *r_data);
 
+void spi1_init(void);
+void spi1_deinit(void);
+int spi1_send_byte(uint8_t byte, 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)
+#define ml5238_cs(x) gpio_bit_write(GPIOA, GPIO_PIN_15, (bit_status)x)
 
 #endif /* _SPI_H__ */
 

+ 26 - 2
Project/BMS.uvoptx

@@ -551,6 +551,30 @@
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
+    <File>
+      <GroupNumber>3</GroupNumber>
+      <FileNumber>31</FileNumber>
+      <FileType>1</FileType>
+      <tvExp>0</tvExp>
+      <tvExpOptDlg>0</tvExpOptDlg>
+      <bDave2>0</bDave2>
+      <PathWithFileName>..\Application\bsp\cs1180.c</PathWithFileName>
+      <FilenameWithoutPath>cs1180.c</FilenameWithoutPath>
+      <RteFlg>0</RteFlg>
+      <bShared>0</bShared>
+    </File>
+    <File>
+      <GroupNumber>3</GroupNumber>
+      <FileNumber>32</FileNumber>
+      <FileType>1</FileType>
+      <tvExp>0</tvExp>
+      <tvExpOptDlg>0</tvExpOptDlg>
+      <bDave2>0</bDave2>
+      <PathWithFileName>..\Application\bsp\ml5238.c</PathWithFileName>
+      <FilenameWithoutPath>ml5238.c</FilenameWithoutPath>
+      <RteFlg>0</RteFlg>
+      <bShared>0</bShared>
+    </File>
   </Group>
 
   <Group>
@@ -569,7 +593,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>5</GroupNumber>
-      <FileNumber>31</FileNumber>
+      <FileNumber>33</FileNumber>
       <FileType>2</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -581,7 +605,7 @@
     </File>
     <File>
       <GroupNumber>5</GroupNumber>
-      <FileNumber>32</FileNumber>
+      <FileNumber>34</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>

+ 10 - 0
Project/BMS.uvprojx

@@ -543,6 +543,16 @@
               <FileType>1</FileType>
               <FilePath>..\Application\bsp\spi.c</FilePath>
             </File>
+            <File>
+              <FileName>cs1180.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\Application\bsp\cs1180.c</FilePath>
+            </File>
+            <File>
+              <FileName>ml5238.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\Application\bsp\ml5238.c</FilePath>
+            </File>
           </Files>
         </Group>
         <Group>