APS Lab / Mbed OS BMS_3029
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(D11, D12, D13);
00005 DigitalOut cs(D10);
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     cs = 1;                     //high as init for disable SPI
00030     spi.format(8, 3);               //byte width, spi mode
00031     spi.frequency(1000000);         //1MHz
00032 }
00033     
00034 
00035 /*
00036 Writes an array of bytes out of the SPI port
00037 */
00038 void spi_write_array(uint8_t len, // Option: Number of bytes to be written on the SPI port
00039                      uint8_t data[] //Array of bytes to be written on the SPI port
00040                     )
00041 {
00042     //cs=0;
00043     for (uint8_t i = 0; i < len; i++) {
00044         spi.write((int8_t)data[i]);
00045     }
00046     //cs=1;
00047 }
00048 
00049 /*
00050  Writes and read a set number of bytes using the SPI port.
00051 
00052 */
00053 
00054 void spi_write_read(uint8_t tx_Data[],//array of data to be written on SPI port
00055                     uint8_t tx_len, //length of the tx data arry
00056                     uint8_t *rx_data,//Input: array that will store the data read by the SPI port
00057                     uint8_t rx_len //Option: number of bytes to be read from the SPI port
00058                    )
00059 {
00060     //cs=0;
00061     for (uint8_t i = 0; i < tx_len; i++) {
00062         spi.write(tx_Data[i]);
00063     }
00064     //cs=1;
00065 
00066     //cs=0;
00067     for (uint8_t i = 0; i < rx_len; i++) {
00068 
00069         rx_data[i] = (uint8_t)spi.write(0xFF);
00070     }
00071     //cs=1;
00072 
00073 }
00074 
00075 
00076 uint8_t spi_read_byte(uint8_t tx_dat)
00077 {
00078     uint8_t data;
00079     //cs=0;
00080     data = (uint8_t)spi.write(0xFF);
00081     //cs=1;
00082     return(data);
00083 }