| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- #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
- */
- // for testing:
- //Battery batteries[NUM_SLOTS] = {
- // {0, STATE_BATTERY_DETECTED, 3700, 500, 25, 3000, 4200, 2000, 10000, 80}
- //};
- 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;
- }
- }
-
|