BMS_T2
Dependencies: INA226
Diff: bms.cpp
- Revision:
- 0:f06ed53310a3
- Child:
- 1:4dd3e328a30b
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bms.cpp Wed Feb 07 08:26:04 2018 +0000 @@ -0,0 +1,65 @@ +#include "mbed.h" +#include "bms.h" + +SPI spi(D11, D12, D13); +DigitalOut cs(D10); + +void delay_u(uint16_t micro) +{ + wait_us(micro); +} + +void delay_m(uint16_t milli) +{ + wait_ms(milli); +} + +void spi_enable(void) // Configures SCK frequency. Use constant defined in header file. +{ + cs = 1; //high as init for disable SPI + spi.format(8, 3); //byte width, spi mode + spi.frequency(1000000); //1MHz +} + + +/* +Writes an array of bytes out of the SPI port +*/ +void spi_write_array(uint8_t len, // Option: Number of bytes to be written on the SPI port + uint8_t data[] //Array of bytes to be written on the SPI port + ) +{ + for (uint8_t i = 0; i < len; i++) { + spi.write((int8_t)data[i]); + } +} + +/* + Writes and read a set number of bytes using the SPI port. + +*/ + +void spi_write_read(uint8_t tx_Data[],//array of data to be written on SPI port + uint8_t tx_len, //length of the tx data arry + uint8_t *rx_data,//Input: array that will store the data read by the SPI port + uint8_t rx_len //Option: number of bytes to be read from the SPI port + ) +{ + for (uint8_t i = 0; i < tx_len; i++) { + spi.write(tx_Data[i]); + } + + for (uint8_t i = 0; i < rx_len; i++) { + + rx_data[i] = (uint8_t)spi.write(0xFF); + } + +} + + +uint8_t spi_read_byte(uint8_t tx_dat) +{ + uint8_t data; + data = (uint8_t)spi.write(0xFF); + return(data); +}