BMS_T2
Dependencies: INA226
bms.cpp
00001 00002 #include "mbed.h" 00003 #include "bms.h" 00004 00005 SPI spi(p5,p6,p7); 00006 DigitalOut cs(p8); 00007 00008 void cs_low(void) 00009 { 00010 cs=0; 00011 } 00012 00013 void cs_high(void) 00014 { 00015 cs=1; 00016 } 00017 00018 void delay_u(uint16_t micro) 00019 { 00020 wait_us(micro); 00021 } 00022 00023 void delay_m(uint16_t milli) 00024 { 00025 wait_ms(milli); 00026 } 00027 00028 void spi_enable(void) // Configures SCK frequency. Use constant defined in header file. 00029 { 00030 cs = 1; //high as init for disable SPI 00031 spi.format(8, 3); //byte width, spi mode 00032 spi.frequency(1000000); //1MHz 00033 } 00034 00035 00036 /* 00037 Writes an array of bytes out of the SPI port 00038 */ 00039 void spi_write_array(uint8_t len, // Option: Number of bytes to be written on the SPI port 00040 uint8_t data[] //Array of bytes to be written on the SPI port 00041 ) 00042 { 00043 //cs=0; 00044 for (uint8_t i = 0; i < len; i++) { 00045 spi.write((int8_t)data[i]); 00046 } 00047 //cs=1; 00048 } 00049 00050 /* 00051 Writes and read a set number of bytes using the SPI port. 00052 00053 */ 00054 00055 void spi_write_read(uint8_t tx_Data[],//array of data to be written on SPI port 00056 uint8_t tx_len, //length of the tx data arry 00057 uint8_t *rx_data,//Input: array that will store the data read by the SPI port 00058 uint8_t rx_len //Option: number of bytes to be read from the SPI port 00059 ) 00060 { 00061 //cs=0; 00062 for (uint8_t i = 0; i < tx_len; i++) { 00063 spi.write(tx_Data[i]); 00064 } 00065 //cs=1; 00066 00067 //cs=0; 00068 for (uint8_t i = 0; i < rx_len; i++) { 00069 00070 rx_data[i] = (uint8_t)spi.write(0xFF); 00071 } 00072 //cs=1; 00073 00074 } 00075 00076 00077 uint8_t spi_read_byte(uint8_t tx_dat) 00078 { 00079 uint8_t data; 00080 //cs=0; 00081 data = (uint8_t)spi.write(0xFF); 00082 //cs=1; 00083 return(data); 00084 }
Generated on Tue Jul 12 2022 21:25:39 by 1.7.2