| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- #include "i2c_pi_target.h"
- #include "src/config.h"
- #include "ti/driverlib/dl_i2c.h"
- #include "src/battery_data/battery.h"
- #include "ti_msp_dl_config.h"
- #include <stdint.h>
- #include <stdio.h>
- #include <string.h>
- // Global extern variable for buffers are defined in config.h and declared in i2c_hal.c:
- void pi_i2c_mcu(){
- uint8_t receivedCommand= DL_I2C_receiveTargetData(I2C_0_INST);
- if(receivedCommand == CMD_GET_BATTERY_STATUS){
- uint8_t status_buffer[NUM_SLOTS];
- //GET battery state from battery.c file
- for(uint8_t slot=0; slot<NUM_SLOTS; slot++){
- status_buffer[slot] = battery_data[slot].battery_state;
- }
- DL_I2C_fillTargetTXFIFO(I2C_0_INST, status_buffer, NUM_SLOTS);
- while(DL_I2C_transmitTargetDataCheck(I2C_0_INST, 0x00)!= false);
- }
- //bitmasked GET command:
- else if((receivedCommand & 0xF0)== 0x20){
- //Get Battery Measurement data: Voltage, Current, Tempertaure
- uint8_t requestedSlot = receivedCommand & 0x0F;
- if(requestedSlot >= NUM_SLOTS){
- DL_I2C_flushTargetRXFIFO(I2C_0_INST);
- return;
- }
- //Check for the requested slot and get the battery measurement data
- BatteryMeasurement *measurement = &battery_data[requestedSlot].battery_measurement;
- //memcpy(tx_packet.txBuffer, &battery->battery_measurement, sizeof(BatteryMeasurement));
- DL_I2C_fillTargetTXFIFO(I2C_0_INST, (uint8_t *)measurement, sizeof(BatteryMeasurement));
- DL_I2C_flushTargetTXFIFO(I2C_0_INST);
- }
- else if(receivedCommand== CMD_SET_BATTERY_LIMIT){
- uint8_t rx_index= 0;
- while(rx_index < sizeof(BatteryLimits)){
- if(!DL_I2C_isTargetRXFIFOEmpty(I2C_0_INST)){
- rx_packet.rxBuffer[rx_index] = DL_I2C_receiveTargetData(I2C_0_INST);
- rx_index++;
- }
- }
- //Check if all the data is received then store the battery limits in BatteryInfo struct:
- if(rx_index== (sizeof(BatteryLimits)+1)){
- BatteryInfo *battery = &battery_data[rx_packet.rxBuffer[0]];
- uint8_t slot_id= rx_packet.rxBuffer[0];
- printf("Battery slot: %u\n", slot_id);
- if(slot_id <= NUM_SLOTS){
- //2bytes of max and min voltage: uint16_t
- /*battery->battery_limits.min_voltage = rx_packet.rxBuffer[1] | (rx_packet.rxBuffer[2] << 8);
- battery->battery_limits.max_voltage = rx_packet.rxBuffer[3]|(rx_packet.rxBuffer[4] << 8);
- //1 byte of cut off current: uint8_t
- battery->battery_limits.cut_off_current = rx_packet.rxBuffer[5];
- //2 bytes of capacitance:uint16_t
- battery->battery_limits.capacitance = rx_packet.rxBuffer[6] | (rx_packet.rxBuffer[7] << 8);
- //1 byte of charge fraction: uint8_t
- battery->battery_limits.charge_fraction = rx_packet.rxBuffer[8];
- //Set the battery state to "STATE_BATTERY_DETECTED"
- battery->batteryLimitRecieved = true;*/
- memcpy(&battery_data[slot_id].battery_limits, &rx_packet.rxBuffer[1], sizeof(BatteryLimits));
- battery_data[slot_id].batteryLimitRecieved= true;
- }
- }
- rx_index= 0;
- DL_I2C_flushTargetRXFIFO(I2C_0_INST);
- }
- }
|