main_target.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. int main(void)
  37. {
  38. SYSCFG_DL_init();
  39. NVIC_EnableIRQ(I2C_target_INST_INT_IRQN);
  40. NVIC_EnableIRQ(ADC12_0_INST_INT_IRQN);
  41. slot_init();
  42. initialize_target_address();
  43. DL_ADC12_startConversion(ADC12_0_INST);
  44. DL_ADC12_enableConversions(ADC12_0_INST);
  45. while (1) {
  46. if (handle_read_pending_slot != -1) {
  47. mcu_i2c_handle_read(I2C_target_INST);
  48. handle_read_pending_slot = -1;
  49. }
  50. // step 1: update the voltage readings
  51. slot_read_state();
  52. // step 2: control loop to adjust the dac / adc values,
  53. // but only if no error happens
  54. // (0x80 is the error flag of the state)
  55. if ((*slot.state & 0x80) == 0) {
  56. slot_adjust_current();
  57. } else {
  58. slot_disable();
  59. }
  60. }
  61. }