adc.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include <adc.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include "battery.h"
  5. #include "ti/driverlib/dl_i2c.h"
  6. #include "ti/driverlib/m0p/dl_core.h"
  7. #include "ti_msp_dl_config.h"
  8. volatile bool gRxComplete;
  9. volatile bool gTxComplete;
  10. uint8_t gTxPacket[I2C_TX_MAX_PACKET_SIZE];
  11. uint8_t gRxPacket[I2C_RX_MAX_PACKET_SIZE];
  12. uint32_t gTxADClen, gTxADCcount;
  13. uint32_t gRxADClen, gRxADCcount;
  14. uint8_t ADC_ConstructConfigBytes(ADC_PARAMS params) {
  15. uint8_t config = 0;
  16. config |= ((params.channel - 1) << 5); // Channel Selection (Bits 6-5)
  17. if (params.continuous) config |= (1 << 4); // Continuous Mode (Bit 4)
  18. switch (params.resolution) {
  19. case 12: config |= (0b00 << 2); break;
  20. case 14: config |= (0b01 << 2); break;
  21. case 16: config |= (0b10 << 2); break;
  22. default: printf("ERROR: Invalid Resolution!\n"); return 0;
  23. }
  24. switch (params.gain) {
  25. case 1: config |= (0b00); break;
  26. case 2: config |= (0b01); break;
  27. case 4: config |= (0b10); break;
  28. default: printf("ERROR: Invalid Gain!\n"); return 0;
  29. }
  30. return config;
  31. }
  32. /* Tansmit Data from MCU to ADC: Function to SET configuration to ADC over I2C*/
  33. void ADC_SetConfigurationBytes(ADC_PARAMS params) {
  34. // **Construct Configuration Byte**
  35. uint8_t config_byte = ADC_ConstructConfigBytes(params);
  36. //printf("Writing Config: 0x%02X to ADC (0x%X)...\n", config_byte, ADC_TARGET_BASE_ADDRESS);
  37. // Wait for I2C Bus to be Free**
  38. while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
  39. // **Start I2C Write Transaction**
  40. // Prepare TX Buffer:
  41. gTxPacket[0]= config_byte;
  42. gTxADClen= 1;
  43. gTxADCcount= 0;
  44. gTxComplete= true;
  45. DL_I2C_startControllerTransfer(I2C_controller_INST, ADC_TARGET_BASE_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_TX, gTxADClen);
  46. DL_I2C_enableInterrupt(I2C_controller_INST, DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
  47. while(!gTxComplete);
  48. // **Ensure STOP Condition is Sent**
  49. while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
  50. printf("Configuration Sent Successfully for 0x%X!\n", config_byte);
  51. }
  52. /*
  53. READY BIT:
  54. This bit is the data ready flag. In read mode, this bit indicates if the output register has been updated
  55. with a latest conversion result. In One-Shot Conversion mode, writing this bit to “1” initiates a new
  56. conversion.
  57. 1= Output Register has not been updated
  58. 0= Output Register has been updated
  59. */
  60. bool ADC_CheckReadyBit(uint8_t slot_id, ADC_PARAMS params){
  61. uint8_t config_byte = ADC_ConstructConfigBytes(params);
  62. DL_I2C_startControllerTransfer(I2C_controller_INST, ADC_TARGET_BASE_ADDRESS+slot_id, DL_I2C_CONTROLLER_DIRECTION_RX, 1);
  63. config_byte= DL_I2C_receiveControllerData(I2C_controller_INST);
  64. bool ready= (config_byte & 0x80)==0;
  65. printf("Slot: %d | Config Byte: 0x%02X | READY Bit: %d\n", slot_id, config_byte, ready);
  66. return ready;
  67. }
  68. /*
  69. Function to read ADC DATA: This function simply retrieves the ADC raw digital output as 16-bit signed integer.
  70. * The value returned is not yet converted to VOLTAGE.
  71. * adc_data[2]: Buffer with an array of size 2, to store two bytes of ADC data.
  72. * Since, the ADC output consists of two 8-bit bytes:
  73. * - adc_data[0] (MSB)
  74. * - adc_data[1] (LSB)
  75. * Next, we verify if the the I2C bus is busy using the function in the driverlib: DL_I2C_get ControllerStatus
  76. * - Prevents collisions by ensuring no two I2C device is using the same bus before initializing.
  77. * BEGIN I2C READ operation to request 2 bytes from the ADC-> DL_I2C_startControllerTransfer()
  78. * Parameters:
  79. - I2C_controller_INST: Refererence to the I2C instance bring used.
  80. - DEF_TARGET_ADDR_ADC: Address of the ADC
  81. - DL_I2C_CONTROLLER_DIRECTION_RX: Indicates that we want to receive the data from ADC
  82. - 2: Specifies that we expect 2 bytes from the ADC
  83. * WAIT for the data to be received: (DL_I2C_getControllerStatus())
  84. - Waits until the ADC sends the data over I2C.
  85. - Ensures that the data transfer is complete before proceeding.
  86. * READ the two bytes of ADc Data:
  87. - adc_data[0] : MSB
  88. - adc_data[1] : LSB
  89. - adc_data[2] : config_bytes
  90. ADC sends its 16-bit value in two parts over 8-bit I2C data frames.
  91. * COMBINE the two bytes into 16-bit Integer:
  92. - Shifts the MSB(first byte) left by 8 bits to make space for LSB.
  93. - Performs a bitwise OR operation to combine MSB and LSB into a single 16-bit number.
  94. * PRINT HEXADEC and DECIMAL format for DEBUGGING.
  95. * Output code is in binary and is proportional to the Input Voltage and PGA settings.
  96. * From the datasheet: https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/22226a.pdf
  97. - Equation 4.4 is being used to convert the output codes to input voltage
  98. */
  99. int16_t ADC_ReadData(uint8_t slot_id, ADC_PARAMS params)
  100. {
  101. while(!ADC_CheckReadyBit(slot_id, params)){
  102. delay_cycles(50000);
  103. }
  104. uint8_t adc_data[3] = {0}; // Buffer for ADC data (MSB, LSB, Config Byte)
  105. int16_t raw_adc = 0;
  106. /*The ACK bits after conversion is issued by the Master, when the device receives a READ command, it outputs two data bytes
  107. followed by a configuration register
  108. in 16 bit conversion mode the MSB(=sign bit) of the first data type is D15.
  109. */
  110. uint8_t bytes_to_read= 3;
  111. uint8_t adc_address= ADC_TARGET_BASE_ADDRESS + slot_id;
  112. //printf("Reading ADC Data from MCP3428 for channel: %u\n", params.channel);
  113. if(DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS){
  114. printf("Error: I2C bus stuck! Resetting..\n");
  115. DL_I2C_resetControllerTransfer(I2C_controller_INST);
  116. }
  117. gRxADClen= bytes_to_read;
  118. gRxADCcount= 0;
  119. gRxComplete= false;
  120. DL_I2C_startControllerTransfer(I2C_controller_INST, adc_address, DL_I2C_CONTROLLER_DIRECTION_RX, gRxADClen);
  121. DL_I2C_enableInterrupt(I2C_controller_INST, DL_I2C_INTERRUPT_CONTROLLER_RXFIFO_TRIGGER);
  122. while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
  123. delay_cycles(100000);
  124. while(!gRxComplete);
  125. while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
  126. uint8_t msb= gRxPacket[0];
  127. uint8_t lsb= gRxPacket[1];
  128. uint8_t config_adc_byte= gRxPacket[2];
  129. uint8_t gain_setting = (config_adc_byte & 0x03);
  130. uint8_t gain_multiplier = (1 << gain_setting); // Gain values: 1, 2, 4, 8
  131. if (params.resolution==16){
  132. raw_adc= (msb << 8)| lsb;
  133. if(raw_adc > 32767) raw_adc-= 65536;
  134. }
  135. else if (params.resolution == 14) {
  136. raw_adc= ((msb & 0b00111111) << 8)| lsb;
  137. if(raw_adc > 8191) raw_adc-= 16384;
  138. }
  139. else if(params.resolution == 12){
  140. raw_adc = ((msb & 0b00001111) << 8) | lsb;
  141. if (raw_adc > 2047) raw_adc -= 4096;
  142. }
  143. //printf("Gain %d: \n", gain_multiplier);
  144. printf("Raw ADC Value: 0x%0X (%d)\n", raw_adc, raw_adc);
  145. return raw_adc;
  146. }
  147. /* Function to Convert ADC Reading to Voltage */
  148. uint16_t ADC_ConvertToVoltage(int16_t adc_value, ADC_PARAMS params) {
  149. uint16_t measured_voltage= 0;
  150. uint16_t LSB= 0;
  151. uint32_t max_adc_value= 1;
  152. switch (params.resolution) {
  153. case 12: // 12-bit
  154. max_adc_value = 4095;
  155. break;
  156. case 14: // 14-bit
  157. max_adc_value = 16383;
  158. break;
  159. case 16: // 16-bit
  160. max_adc_value = 65535;
  161. break;
  162. default:
  163. printf("Error: Unknown ADC Resolution!\n");
  164. return 0;
  165. }
  166. measured_voltage= (((uint32_t)adc_value * ADC_VREF_MV) * 3)/max_adc_value;
  167. /*Formula for ADC to Voltage Conversion*/
  168. //voltage = (adc_value / max_adc_value)* VREF/gain;
  169. //printf("Converted Voltage: %d mV\n", measured_voltage);
  170. return (uint16_t)measured_voltage;
  171. }
  172. /* Function to Convert ADC Reading to Voltage */
  173. uint16_t ADC_ConvertToCurrent(int16_t adc_value, ADC_PARAMS params) {
  174. int16_t current_mA= 0;
  175. //uint8_t r_shunt= 0.1;
  176. //Convert ADC value to voltage across shunt resistor:
  177. uint16_t voltage_mV= ADC_ConvertToVoltage(adc_value, params);
  178. //Convert voltage drop across shunt resistor to current
  179. current_mA= (voltage_mV)/ 0.1;
  180. //printf("Converted Current: %d mA.\n", current_mA);
  181. return (int16_t)current_mA;
  182. }
  183. void Battery_UpdateVoltage(ADC_PARAMS params){
  184. for(uint8_t slot=0; slot< NUM_SLOTS; slot++ ){
  185. //CH1: Voltage Setup
  186. int16_t raw_adc_voltage= ADC_ReadData(slot, params);
  187. batteries[slot].voltage= ADC_ConvertToVoltage(raw_adc_voltage, params);
  188. printf("Battery Voltage for slot %d is %u mV.\n", slot, batteries[slot].voltage);
  189. }
  190. }
  191. void Battery_UpdateCurrent(ADC_PARAMS params){
  192. for(uint8_t slot=0; slot< NUM_SLOTS; slot++ ){
  193. //CH2: Charge Current
  194. int16_t raw_adc_current= ADC_ReadData(slot, params);
  195. batteries[slot].current= ADC_ConvertToCurrent(raw_adc_current, params);
  196. printf("Battery current for slot %d is %u mA.\n", slot, batteries[slot].current);
  197. }
  198. }