| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- #include "src/battery_data/battery.h"
- #include "ti_msp_dl_config.h"
- //#include "ti/driverlib/dl_i2c.h"
- #include <stdio.h>
- #include "src/peripherals/adc/adc.h"
- #include "src/peripherals/adc/adc_interface.h"
- #include "src/i2c_comm/mcu_slave_interface.h"
- /*
- DL_TimerG_startCounter(PWM_0_INST);
- DL_TimerA_setCaptureCompareValue(PWM_1_INST, pwm_count,
- DL_TIMER_CC_0_INDEX); // update ccr0 value
- */
- #define DELAY_CYCLE (10000000)
- volatile bool mcu_CommandPending= false;
- /*
- Scans all the addresses of the peripherals:
- */
- /*void I2C_scanBus(I2C_Regs *i2c) {
- printf("1");
- // **Step 1: Reset I2C Controller if Busy**
- if (DL_I2C_getControllerStatus(i2c) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS) {
- printf("I2C Bus Busy! Resetting I2C Controller...\n");
- DL_I2C_disableController(i2c); // Disable I2C
- delay_cycles(20000);
- DL_I2C_enableController(i2c); // Re-enable I2C
- delay_cycles(20000);
- }
- uint32_t i2c_status;
- // **Step 2: Scan I2C Bus**
- for (uint8_t addr = 0x08; addr < 0x78; addr++) { // Valid I2C Address Range
- printf("Scanning 0x%02X\n", addr);
- DL_I2C_startControllerTransfer(i2c, addr, DL_I2C_CONTROLLER_DIRECTION_RX, 1);
- delay_cycles(5000);
- if (addr != 0x60 && addr != 0x68) {
- continue;
- }
- i2c_status = DL_I2C_getControllerStatus(i2c);
- printf("DL_I2C_getControllerStatus(i2c): %d\n", i2c_status);
- printf("busy?: %d\n", (i2c_status & DL_I2C_CONTROLLER_STATUS_BUSY));
- printf("error?: %d\n", (i2c_status & DL_I2C_CONTROLLER_STATUS_ERROR));
- printf("addr_ack?: %d\n", (i2c_status & DL_I2C_CONTROLLER_STATUS_ADDR_ACK));
- printf("data_ack?: %d\n", (i2c_status & DL_I2C_CONTROLLER_STATUS_DATA_ACK));
- printf("arb_lost?: %d\n", (i2c_status & DL_I2C_CONTROLLER_STATUS_ARBITRATION_LOST));
- printf("idle?: %d\n", (i2c_status & DL_I2C_CONTROLLER_STATUS_IDLE));
- printf("busy_bus?: %d\n", (i2c_status & DL_I2C_CONTROLLER_STATUS_BUSY_BUS));
-
- if (!(DL_I2C_getControllerStatus(i2c) & DL_I2C_CONTROLLER_STATUS_ERROR)) {
- printf("Device found at: 0x%02X\n", addr);
- DL_I2C_disableController(i2c);
- DL_I2C_enableController(i2c);
- }else {
- // Clear the error by resetting the I2C controller
- printf("Device not found...\n");
- DL_I2C_disableController(i2c);
- DL_I2C_enableController(i2c);
- }
- }
- //printf("I2C Scan Complete!\n");
- }
- */
- void I2C_controller_INST_IRQHandler(void) {
- // printf("I2C Interrupt Triggered to ADC!\n");
- switch (DL_I2C_getPendingInterrupt(I2C_controller_INST)) {
-
- case DL_I2C_IIDX_CONTROLLER_START:
- gRxADCcount = 0;
- DL_I2C_flushControllerTXFIFO(I2C_controller_INST);
- break;
- case DL_I2C_IIDX_CONTROLLER_RXFIFO_TRIGGER:
- /* Store bytes received from target in Rx Msg Buffer */
- while (DL_I2C_isControllerRXFIFOEmpty(I2C_controller_INST) != true) {
- if (gRxADCcount < gRxADClen) {
- gRxPacket[gRxADCcount] =
- DL_I2C_receiveControllerData(I2C_controller_INST);
- gRxADCcount++;
- } else {
- /* Ignore and remove from FIFO if the buffer is full */
- DL_I2C_receiveControllerData(I2C_controller_INST);
- }
- }
- if (gRxADCcount >= gRxADClen) {
- gRxComplete = true;
- DL_I2C_enableInterrupt(I2C_controller_INST,
- DL_I2C_INTERRUPT_CONTROLLER_STOP);
- }
- break;
- /*TRANSMIT data to ADC*/
- case DL_I2C_IIDX_CONTROLLER_TXFIFO_TRIGGER:
- if (gTxADCcount < gTxADClen) {
- gTxPacket[gTxADCcount] = DL_I2C_fillControllerTXFIFO(I2C_controller_INST,
- &gTxPacket[gTxADCcount],
- (gTxADClen - gTxADCcount));
- gTxADCcount++;
- } else {
- /*Prevent overflow and just ignore data*/
- DL_I2C_fillTargetTXFIFO(I2C_controller_INST, (uint8_t[]){0x00}, 1);
- gTxComplete = true;
- }
- if(gTxADCcount >= gTxADClen){
- gTxComplete= true;
- }
- break;
- /*STOP condition*/
- case DL_I2C_IIDX_CONTROLLER_STOP:
- gTxComplete = true;
- gRxComplete = true;
- break;
- case DL_I2C_IIDX_CONTROLLER_ARBITRATION_LOST:
- break;
- case DL_I2C_IIDX_CONTROLLER_NACK:
- break;
- default:
- break;
- }
- }
- /**** Interrupt for Pi to MCU ****/
- void I2C_target_INST_IRQHandler(void) {
- uint32_t status = DL_I2C_getPendingInterrupt(I2C_target_INST);
- switch (status) {
- case DL_I2C_IIDX_TARGET_START:
- DL_I2C_flushTargetTXFIFO(I2C_target_INST);
- break;
- case DL_I2C_IIDX_TARGET_STOP:
- mcu_CommandPending= true;
- DL_I2C_flushTargetTXFIFO(I2C_target_INST);
- DL_I2C_flushTargetRXFIFO(I2C_target_INST);
- break;
- case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
- if (DL_I2C_isTargetRXFIFOEmpty(I2C_target_INST)) {
- return;
- }
- mcu_CommandPending= true;
- break;
- case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
- mcu_CommandPending= true;
- break;
- case DL_I2C_IIDX_TARGET_ARBITRATION_LOST:
- break;
- default:
- break;
- }
- }
- int main(void)
- {
- SYSCFG_DL_init();
- Battery_Init();
- NVIC_EnableIRQ(I2C_controller_INST_INT_IRQN);
- //NVIC_EnableIRQ(I2C_target_INST_INT_IRQN);
- /*printf("&gRxPacket: %p\n", (void*)gRxPacket);
- printf("&batteries[0]: %p\n", (void*)&batteries[0]);
- printf("&gRxADClen: %p\n", (void*)&gRxADClen);
- printf("&gTxPacket: %p\n", (void*)gTxPacket);
- printf("&gTxADClen: %p\n", (void*)&gTxADClen);
- */
-
- printf("Memory address of batteries: %p\n", &batteries[0]);
- while (1) {
- /*if(mcu_CommandPending){
- mcu_i2c_handle(I2C_target_INST);
- mcu_CommandPending= false;
- continue;
- }*/
-
- for(uint8_t slot= 0; slot< NUM_SLOTS; slot++){
- for(uint8_t channel= 0; channel< 2; channel++){
- updateADCReading_multichannel(slot, channel);
- }
- }
-
- delay_cycles(DELAY_CYCLE);
- }
- }
|