main_target.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "src/battery_data/battery.h"
  2. #include "ti/driverlib/dl_i2c.h"
  3. #include "ti_msp_dl_config.h"
  4. //#include "ti/driverlib/dl_i2c.h"
  5. #include <stdio.h>
  6. #include "src/interfaces/i2c_target.h"
  7. #include "src/config.h"
  8. #include "src/peripherals/adc.h"
  9. int8_t handle_read_pending_slot = -1;
  10. /**** Interrupt for Pi to MCU ****/
  11. void I2C_target_INST_IRQHandler(void) {
  12. uint32_t status = DL_I2C_getPendingInterrupt(I2C_target_INST);
  13. switch (status) {
  14. case DL_I2C_IIDX_TARGET_START:
  15. break;
  16. case DL_I2C_IIDX_TARGET_STOP:
  17. break;
  18. case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
  19. // only use this function if we are async (filling buffers) - the rest is handled by the mainloop
  20. if (handle_read_pending_slot == -1) {
  21. handle_read_pending_slot = mcu_i2c_handle(I2C_target_INST);
  22. }
  23. break;
  24. case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
  25. break;
  26. case DL_I2C_IIDX_TARGET_ARBITRATION_LOST:
  27. break;
  28. case DL_I2C_IIDX_TIMEOUT_A:
  29. case DL_I2C_IIDX_TIMEOUT_B:
  30. DL_I2C_flushTargetRXFIFO(I2C_target_INST);
  31. DL_I2C_flushTargetTXFIFO(I2C_target_INST);
  32. default:
  33. break;
  34. }
  35. }
  36. _iq15 qVTrim;
  37. int main(void)
  38. {
  39. SYSCFG_DL_init();
  40. NVIC_EnableIRQ(I2C_target_INST_INT_IRQN);
  41. NVIC_EnableIRQ(ADC12_0_INST_INT_IRQN);
  42. slot_init();
  43. initialize_target_address();
  44. qVTrim = _IQ15div(_IQ15mpy((_IQ15(DL_SYSCTL_getTempCalibrationConstant()) -
  45. _IQ15(0.5)), _IQ15(ADC_TEMP_VREF_VOLTAGE)), ((uint32_t)(1) << 27));
  46. DL_ADC12_startConversion(ADC12_0_INST);
  47. DL_ADC12_enableConversions(ADC12_0_INST);
  48. while (1) {
  49. if (handle_read_pending_slot != -1) {
  50. mcu_i2c_handle_read(I2C_target_INST);
  51. handle_read_pending_slot = -1;
  52. }
  53. // step 1: update the voltage readings
  54. slot_read_state();
  55. // step 2: control loop to adjust the dac / adc values,
  56. // but only if no error happens
  57. // (0x80 is the error flag of the state)
  58. if ((*slot.state & 0x80) == 0) {
  59. slot_adjust_current();
  60. } else {
  61. slot_disable();
  62. }
  63. }
  64. }