main_target.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. delay_cycles(320000*4);
  49. slot.set_current = 800;
  50. while (1) {
  51. if (handle_read_pending_slot != -1) {
  52. mcu_i2c_handle_read(I2C_target_INST);
  53. handle_read_pending_slot = -1;
  54. }
  55. // step 1: update the voltage readings
  56. slot_read_state();
  57. // step 2: control loop to adjust the dac / adc values,
  58. // but only if no error happens
  59. // (0x80 is the error flag of the state)
  60. if ((*slot.state & 0x80) == 0) {
  61. slot_adjust_current();
  62. } else {
  63. slot_disable();
  64. }
  65. delay_cycles(320000*4);
  66. }
  67. }