KIT Solar Car Project / Mbed 2 deprecated BMS_v1

Dependencies:   mbed INA226

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers bms.cpp Source File

bms.cpp

00001 #include "mbed.h"
00002 #include "bms.h"
00003 
00004 SPI spi(p5,p6, p7);
00005 DigitalOut cs(p8);
00006 
00007 void cs_low(void)
00008 {
00009     cs=0;
00010 }
00011 
00012 void cs_high(void)
00013 {
00014     cs=1;
00015 }
00016 
00017 void delay_u(uint16_t micro)
00018 {
00019     wait_us(micro);
00020 }
00021 
00022 void delay_m(uint16_t milli)
00023 {
00024     wait_ms(milli);
00025 }
00026 
00027 void spi_enable(void) // Configures SCK frequency. Use constant defined in header file.
00028 {
00029     //printf("takuma");
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 /*
00038 Writes an array of bytes out of the SPI port
00039 */
00040 
00041 void spi_write_array(uint8_t len, // Option: Number of bytes to be written on the SPI port
00042                      uint8_t data[] //Array of bytes to be written on the SPI port
00043                     )
00044 {
00045     //cs=0;
00046     for (uint8_t i = 0; i < len; i++) {
00047         spi.write((int8_t)data[i]);
00048     }
00049     //cs=1;
00050 }
00051 
00052 /*
00053  Writes and read a set number of bytes using the SPI port.
00054 
00055 */
00056 
00057 void spi_write_read(uint8_t tx_Data[],//array of data to be written on SPI port
00058                     uint8_t tx_len, //length of the tx data arry
00059                     uint8_t *rx_data,//Input: array that will store the data read by the SPI port
00060                     uint8_t rx_len //Option: number of bytes to be read from the SPI port
00061                    )
00062 {
00063     //cs=0;
00064     for (uint8_t i = 0; i < tx_len; i++) {
00065         spi.write(tx_Data[i]);
00066     }
00067     //cs=1;
00068 
00069     //cs=0;
00070     for (uint8_t i = 0; i < rx_len; i++) {
00071 
00072         rx_data[i] = (uint8_t)spi.write(0xFF);
00073     }
00074     //cs=1;
00075 
00076 }
00077 
00078 
00079 uint8_t spi_read_byte(uint8_t tx_dat)
00080 {
00081     uint8_t data;
00082     //cs=0;
00083     data = (uint8_t)spi.write(0xFF);
00084     //cs=1;
00085     return(data);
00086 }