LTC6811 Battery Management System with ADuCM3029.

Committer:
APS_Lab
Date:
Fri Feb 09 04:43:04 2018 +0000
Revision:
1:4dd3e328a30b
Parent:
0:f06ed53310a3
Improved for SPI.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
APS_Lab 0:f06ed53310a3 1 #include "mbed.h"
APS_Lab 0:f06ed53310a3 2 #include "bms.h"
APS_Lab 0:f06ed53310a3 3
APS_Lab 0:f06ed53310a3 4 SPI spi(D11, D12, D13);
APS_Lab 0:f06ed53310a3 5 DigitalOut cs(D10);
APS_Lab 0:f06ed53310a3 6
APS_Lab 1:4dd3e328a30b 7 void cs_low(void)
APS_Lab 1:4dd3e328a30b 8 {
APS_Lab 1:4dd3e328a30b 9 cs=0;
APS_Lab 1:4dd3e328a30b 10 }
APS_Lab 1:4dd3e328a30b 11
APS_Lab 1:4dd3e328a30b 12 void cs_high(void)
APS_Lab 1:4dd3e328a30b 13 {
APS_Lab 1:4dd3e328a30b 14 cs=1;
APS_Lab 1:4dd3e328a30b 15 }
APS_Lab 1:4dd3e328a30b 16
APS_Lab 0:f06ed53310a3 17 void delay_u(uint16_t micro)
APS_Lab 0:f06ed53310a3 18 {
APS_Lab 0:f06ed53310a3 19 wait_us(micro);
APS_Lab 0:f06ed53310a3 20 }
APS_Lab 0:f06ed53310a3 21
APS_Lab 0:f06ed53310a3 22 void delay_m(uint16_t milli)
APS_Lab 0:f06ed53310a3 23 {
APS_Lab 0:f06ed53310a3 24 wait_ms(milli);
APS_Lab 0:f06ed53310a3 25 }
APS_Lab 0:f06ed53310a3 26
APS_Lab 0:f06ed53310a3 27 void spi_enable(void) // Configures SCK frequency. Use constant defined in header file.
APS_Lab 0:f06ed53310a3 28 {
APS_Lab 0:f06ed53310a3 29 cs = 1; //high as init for disable SPI
APS_Lab 0:f06ed53310a3 30 spi.format(8, 3); //byte width, spi mode
APS_Lab 0:f06ed53310a3 31 spi.frequency(1000000); //1MHz
APS_Lab 0:f06ed53310a3 32 }
APS_Lab 0:f06ed53310a3 33
APS_Lab 0:f06ed53310a3 34
APS_Lab 0:f06ed53310a3 35 /*
APS_Lab 0:f06ed53310a3 36 Writes an array of bytes out of the SPI port
APS_Lab 0:f06ed53310a3 37 */
APS_Lab 0:f06ed53310a3 38 void spi_write_array(uint8_t len, // Option: Number of bytes to be written on the SPI port
APS_Lab 0:f06ed53310a3 39 uint8_t data[] //Array of bytes to be written on the SPI port
APS_Lab 0:f06ed53310a3 40 )
APS_Lab 0:f06ed53310a3 41 {
APS_Lab 1:4dd3e328a30b 42 //cs=0;
APS_Lab 0:f06ed53310a3 43 for (uint8_t i = 0; i < len; i++) {
APS_Lab 0:f06ed53310a3 44 spi.write((int8_t)data[i]);
APS_Lab 0:f06ed53310a3 45 }
APS_Lab 1:4dd3e328a30b 46 //cs=1;
APS_Lab 0:f06ed53310a3 47 }
APS_Lab 0:f06ed53310a3 48
APS_Lab 0:f06ed53310a3 49 /*
APS_Lab 0:f06ed53310a3 50 Writes and read a set number of bytes using the SPI port.
APS_Lab 0:f06ed53310a3 51
APS_Lab 0:f06ed53310a3 52 */
APS_Lab 0:f06ed53310a3 53
APS_Lab 0:f06ed53310a3 54 void spi_write_read(uint8_t tx_Data[],//array of data to be written on SPI port
APS_Lab 0:f06ed53310a3 55 uint8_t tx_len, //length of the tx data arry
APS_Lab 0:f06ed53310a3 56 uint8_t *rx_data,//Input: array that will store the data read by the SPI port
APS_Lab 0:f06ed53310a3 57 uint8_t rx_len //Option: number of bytes to be read from the SPI port
APS_Lab 0:f06ed53310a3 58 )
APS_Lab 0:f06ed53310a3 59 {
APS_Lab 1:4dd3e328a30b 60 //cs=0;
APS_Lab 0:f06ed53310a3 61 for (uint8_t i = 0; i < tx_len; i++) {
APS_Lab 0:f06ed53310a3 62 spi.write(tx_Data[i]);
APS_Lab 0:f06ed53310a3 63 }
APS_Lab 1:4dd3e328a30b 64 //cs=1;
APS_Lab 0:f06ed53310a3 65
APS_Lab 1:4dd3e328a30b 66 //cs=0;
APS_Lab 0:f06ed53310a3 67 for (uint8_t i = 0; i < rx_len; i++) {
APS_Lab 0:f06ed53310a3 68
APS_Lab 0:f06ed53310a3 69 rx_data[i] = (uint8_t)spi.write(0xFF);
APS_Lab 0:f06ed53310a3 70 }
APS_Lab 1:4dd3e328a30b 71 //cs=1;
APS_Lab 0:f06ed53310a3 72
APS_Lab 0:f06ed53310a3 73 }
APS_Lab 0:f06ed53310a3 74
APS_Lab 0:f06ed53310a3 75
APS_Lab 0:f06ed53310a3 76 uint8_t spi_read_byte(uint8_t tx_dat)
APS_Lab 0:f06ed53310a3 77 {
APS_Lab 0:f06ed53310a3 78 uint8_t data;
APS_Lab 1:4dd3e328a30b 79 //cs=0;
APS_Lab 0:f06ed53310a3 80 data = (uint8_t)spi.write(0xFF);
APS_Lab 1:4dd3e328a30b 81 //cs=1;
APS_Lab 0:f06ed53310a3 82 return(data);
APS_Lab 0:f06ed53310a3 83 }