BMS_T2

Dependencies:   INA226

Committer:
APS_Lab
Date:
Wed Feb 07 08:26:04 2018 +0000
Revision:
0:f06ed53310a3
Child:
1:4dd3e328a30b
Ver1.0;

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 0:f06ed53310a3 7 void delay_u(uint16_t micro)
APS_Lab 0:f06ed53310a3 8 {
APS_Lab 0:f06ed53310a3 9 wait_us(micro);
APS_Lab 0:f06ed53310a3 10 }
APS_Lab 0:f06ed53310a3 11
APS_Lab 0:f06ed53310a3 12 void delay_m(uint16_t milli)
APS_Lab 0:f06ed53310a3 13 {
APS_Lab 0:f06ed53310a3 14 wait_ms(milli);
APS_Lab 0:f06ed53310a3 15 }
APS_Lab 0:f06ed53310a3 16
APS_Lab 0:f06ed53310a3 17 void spi_enable(void) // Configures SCK frequency. Use constant defined in header file.
APS_Lab 0:f06ed53310a3 18 {
APS_Lab 0:f06ed53310a3 19 cs = 1; //high as init for disable SPI
APS_Lab 0:f06ed53310a3 20 spi.format(8, 3); //byte width, spi mode
APS_Lab 0:f06ed53310a3 21 spi.frequency(1000000); //1MHz
APS_Lab 0:f06ed53310a3 22 }
APS_Lab 0:f06ed53310a3 23
APS_Lab 0:f06ed53310a3 24
APS_Lab 0:f06ed53310a3 25 /*
APS_Lab 0:f06ed53310a3 26 Writes an array of bytes out of the SPI port
APS_Lab 0:f06ed53310a3 27 */
APS_Lab 0:f06ed53310a3 28 void spi_write_array(uint8_t len, // Option: Number of bytes to be written on the SPI port
APS_Lab 0:f06ed53310a3 29 uint8_t data[] //Array of bytes to be written on the SPI port
APS_Lab 0:f06ed53310a3 30 )
APS_Lab 0:f06ed53310a3 31 {
APS_Lab 0:f06ed53310a3 32 for (uint8_t i = 0; i < len; i++) {
APS_Lab 0:f06ed53310a3 33 spi.write((int8_t)data[i]);
APS_Lab 0:f06ed53310a3 34 }
APS_Lab 0:f06ed53310a3 35 }
APS_Lab 0:f06ed53310a3 36
APS_Lab 0:f06ed53310a3 37 /*
APS_Lab 0:f06ed53310a3 38 Writes and read a set number of bytes using the SPI port.
APS_Lab 0:f06ed53310a3 39
APS_Lab 0:f06ed53310a3 40 */
APS_Lab 0:f06ed53310a3 41
APS_Lab 0:f06ed53310a3 42 void spi_write_read(uint8_t tx_Data[],//array of data to be written on SPI port
APS_Lab 0:f06ed53310a3 43 uint8_t tx_len, //length of the tx data arry
APS_Lab 0:f06ed53310a3 44 uint8_t *rx_data,//Input: array that will store the data read by the SPI port
APS_Lab 0:f06ed53310a3 45 uint8_t rx_len //Option: number of bytes to be read from the SPI port
APS_Lab 0:f06ed53310a3 46 )
APS_Lab 0:f06ed53310a3 47 {
APS_Lab 0:f06ed53310a3 48 for (uint8_t i = 0; i < tx_len; i++) {
APS_Lab 0:f06ed53310a3 49 spi.write(tx_Data[i]);
APS_Lab 0:f06ed53310a3 50 }
APS_Lab 0:f06ed53310a3 51
APS_Lab 0:f06ed53310a3 52 for (uint8_t i = 0; i < rx_len; i++) {
APS_Lab 0:f06ed53310a3 53
APS_Lab 0:f06ed53310a3 54 rx_data[i] = (uint8_t)spi.write(0xFF);
APS_Lab 0:f06ed53310a3 55 }
APS_Lab 0:f06ed53310a3 56
APS_Lab 0:f06ed53310a3 57 }
APS_Lab 0:f06ed53310a3 58
APS_Lab 0:f06ed53310a3 59
APS_Lab 0:f06ed53310a3 60 uint8_t spi_read_byte(uint8_t tx_dat)
APS_Lab 0:f06ed53310a3 61 {
APS_Lab 0:f06ed53310a3 62 uint8_t data;
APS_Lab 0:f06ed53310a3 63 data = (uint8_t)spi.write(0xFF);
APS_Lab 0:f06ed53310a3 64 return(data);
APS_Lab 0:f06ed53310a3 65 }