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