| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- #include "i2c_target.h"
- #include "battery.h"
- #include "adc.h"
- #include "ti/driverlib/dl_i2c.h"
- #include "ti_msp_dl_config.h"
- #include <stdio.h>
- #include <stdint.h>
- //Global buffer for I2C
- BatteryData battery_data;
- BatteryLimitMsg battery_limits;
- //Flag for Pi INTERRUPTS:
- volatile bool piRxComplete;
- volatile bool piTxComplete;
- uint8_t piTxPacket[I2C_TX_MAX_PACKET_SIZE_PI];
- uint8_t piRxPacket[I2C_RX_MAX_PACKET_SIZE_PI];
- uint32_t piTxLen, piTxCount;
- uint32_t piRxLen, piRxCount;
- /*
- - command: as defined in the Wiki for Pi 0x01, 0x02, 0x03, refer to i2c_target.h file
- - slot_id: battery slot numner from 0 to NUM_SLOTS-1
- - data: pointer to SET battery limits
- - len: length of the data
- */
- void Battery_ReadState(uint8_t slot_id){
-
- uint16_t voltage_mv= batteries[slot_id].voltage;
- uint16_t min_voltage= batteries[slot_id].min_voltage;
- uint16_t max_voltage= batteries[slot_id].max_voltage;
- //Testing:
- if(voltage_mv< 500){
- batteries[slot_id].state= STATE_EMPTY;
- }
- else if(voltage_mv>=500 && voltage_mv< 3000){
- batteries[slot_id].state= STATE_BATTERY_DETECTED;
- }
- else if(voltage_mv >=3000 && voltage_mv< 4200){
- batteries[slot_id].state= STATE_MEASUREMENT_IN_PROGRESS;
- }
- // once MCU is done reading ADC:
- else if(!(DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS)) {
- batteries[slot_id].state= STATE_MEASUREMENT_DONE;
- }
- else{
- batteries[slot_id].state= STATE_OVERCHARGING;
- }
- }
- void Battery_StateUpdate(){
- piTxCount= 0;
- piTxLen = NUM_SLOTS;
- for(uint8_t slot=0; slot< piTxLen; slot++){
- Battery_ReadState(slot);
- piTxPacket[slot]= batteries[slot].state;
- }
- }
- /*
- {
- if(receivedCommand == CMD_SET_BATTERY_LIMIT){
- BatteryLimitMsg battery_limits;
- //requestedSlot= DL_I2C_receiveTargetData(I2C_target_INST);
- for(uint8_t slot_id=0; slot_id< NUM_SLOTS; slot++){
-
- }
-
- if(requestedSlot < NUM_SLOTS){
- batteries[requestedSlot].min_voltage= (DL_I2C_receiveTargetData(I2C_target_INST)<< 8)|DL_I2C_receiveTargetData(I2C_target_INST);
- batteries[requestedSlot].max_voltage= (DL_I2C_receiveTargetData(I2C_target_INST)<< 8)|DL_I2C_receiveTargetData(I2C_target_INST);
- batteries[requestedSlot].cut_off_current= (DL_I2C_receiveTargetData(I2C_target_INST)<< 8)|DL_I2C_receiveTargetData(I2C_target_INST);
- batteries[requestedSlot].capacitance= (DL_I2C_receiveTargetData(I2C_target_INST)<< 8)|DL_I2C_receiveTargetData(I2C_target_INST);
- }
- }
- }
- break;
- Battery_StateUpdate();
- uint8_t battery_slots_status[NUM_SLOTS];
- for(uint8_t slot= 0; slot< NUM_SLOTS; slot++){
- battery_slots_status[slot]= batteries[slot].state;
- }
- while (DL_I2C_getTargetStatus(I2C_target_INST) & DL_I2C_TARGET_STATUS_BUS_BUSY);
- DL_I2C_fillTargetTXFIFO(I2C_target_INST, battery_slots_status, NUM_SLOTS);
- while (DL_I2C_getTargetStatus(I2C_target_INST) & DL_I2C_TARGET_STATUS_BUS_BUSY);
- */
-
|