/******************************************************************************* * Function Name: I2C_1_ScbModeEnableIntr ******************************************************************************** * * Summary: * Enables an interrupt for a specific mode. * * Parameters: * None * * Return: * None * *******************************************************************************/ static void I2C_1_ScbEnableIntr(void) { #if(I2C_1_SCB_IRQ_INTERNAL) #if(I2C_1_SCB_MODE_UNCONFIG_CONST_CFG) /* Enable interrupt in NVIC */ if(0u != I2C_1_scbEnableIntr) { I2C_1_EnableInt(); } #else I2C_1_EnableInt(); #endif /* (I2C_1_SCB_MODE_UNCONFIG_CONST_CFG) */ #endif /* (I2C_1_SCB_IRQ_INTERNAL) */ }
//Internal I2C: IMU, Safety-CoP, potentiometers void init_i2c1(void) { #ifdef USE_I2C_INT I2C_1_EnableInt(); I2C_1_Start(); #endif //USE_I2C_INT }
/******************************************************************************* * Function Name: I2C_1_I2CMasterClearWriteBuf ******************************************************************************** * * Summary: * Resets the write buffer pointer back to the first byte in the buffer. * * Parameters: * None * * Return: * None * * Global variables: * I2C_1_mstrRdBufIndex - used to current index within master read * buffer. * I2C_1_mstrStatus - used to store current status of I2C Master. * *******************************************************************************/ void I2C_1_I2CMasterClearWriteBuf(void) { I2C_1_DisableInt(); /* Lock from interruption */ I2C_1_mstrWrBufIndex = 0u; I2C_1_mstrStatus &= (uint16) ~I2C_1_I2C_MSTAT_WR_CMPLT; I2C_1_EnableInt(); /* Release lock */ }
/******************************************************************************* * Function Name: I2C_1_Start ******************************************************************************** * * Summary: * Starts the I2C hardware. Enables Active mode power template bits or clock * gating as appropriate. It is required to be executed before I2C bus * operation. * The I2C interrupt remains disabled after this function call. * * Parameters: * None * * Return: * None * * Side Effects: * This component automatically enables it's interrupt. If I2C is enabled * without the interrupt enabled, it could lock up the I2C bus. * * Global variables: * I2C_1_initVar - used to check initial configuration, modified * on first function call. * * Reentrant: * No * *******************************************************************************/ void I2C_1_Start(void) { /* Initialize I2C registers, reset I2C buffer index and clears status */ if(0u == I2C_1_initVar) { I2C_1_Init(); I2C_1_initVar = 1u; /* Component initialized */ } I2C_1_Enable(); I2C_1_EnableInt(); }
/******************************************************************************* * Function Name: I2C_1_I2CSlaveClearReadStatus ******************************************************************************** * * Summary: * Clears the read status flags and returns their values. No other status flags * are affected. * * Parameters: * None * * Return: * Current read status of I2C slave. * * Global variables: * I2C_1_slStatus - used to store current status of I2C slave. * *******************************************************************************/ uint32 I2C_1_I2CSlaveClearReadStatus(void) { uint32 status; I2C_1_DisableInt(); /* Lock from interruption */ /* Mask of transfer complete flag and error status */ status = ((uint32) I2C_1_slStatus & I2C_1_I2C_SSTAT_RD_MASK); I2C_1_slStatus &= (uint8) ~I2C_1_I2C_SSTAT_RD_CLEAR; I2C_1_EnableInt(); /* Release lock */ return(status); }
/******************************************************************************* * Function Name: I2C_1_I2CSlaveInitWriteBuf ******************************************************************************** * * Summary: * Sets the buffer pointer and size of the read buffer. This function also * resets the transfer count returned with the I2C_SlaveGetReadBufSize function. * * Parameters: * writeBuf: Pointer to the data buffer to be read by the master. * bufSize: Size of the buffer exposed to the I2C master. * * Return: * None * * Global variables: * I2C_1_slWrBufPtr - used to store pointer to slave write buffer. * I2C_1_slWrBufSize - used to store salve write buffer size. * I2C_1_slWrBufIndex - used to store current index within slave * write buffer. * * Side Effects: * If this function is called during a bus transaction, data from the previous * buffer location and the beginning of current buffer may be transmitted. * *******************************************************************************/ void I2C_1_I2CSlaveInitWriteBuf(uint8 * wrBuf, uint32 bufSize) { /* Check buffer pointer */ if(NULL != wrBuf) { I2C_1_DisableInt(); /* Lock from interruption */ I2C_1_slWrBufPtr = (volatile uint8 *) wrBuf; /* Set buffer pointer */ I2C_1_slWrBufSize = bufSize; /* Set buffer size */ I2C_1_slWrBufIndex = 0u; /* Clear buffer index */ I2C_1_EnableInt(); /* Release lock */ } }
/******************************************************************************* * Function Name: I2C_1_I2CMasterClearStatus ******************************************************************************** * * Summary: * Clears all status flags and returns the master status. * * Parameters: * None * * Return: * Current status of I2C master. * * Global variables: * I2C_1_mstrStatus - used to store current status of I2C Master. * *******************************************************************************/ uint32 I2C_1_I2CMasterClearStatus(void) { uint32 status; I2C_1_DisableInt(); /* Lock from interruption */ /* Read and clear master status */ status = (uint32) I2C_1_mstrStatus; I2C_1_mstrStatus = I2C_1_I2C_MSTAT_CLEAR; I2C_1_EnableInt(); /* Release lock */ return(status); }
/******************************************************************************* * Function Name: I2C_1_I2CSlaveInitReadBuf ******************************************************************************** * * Summary: * Sets the buffer pointer and size of the read buffer. This function also * resets the transfer count returned with the I2C_SlaveGetReadBufSize function. * * Parameters: * readBuf: Pointer to the data buffer to be read by the master. * bufSize: Size of the read buffer exposed to the I2C master. * * Return: * None * * Global variables: * I2C_1_slRdBufPtr - used to store pointer to slave read buffer. * I2C_1_slRdBufSize - used to store salve read buffer size. * I2C_1_slRdBufIndex - used to store current index within slave * read buffer. * * Side Effects: * If this function is called during a bus transaction, data from the previous * buffer location and the beginning of current buffer may be transmitted. * *******************************************************************************/ void I2C_1_I2CSlaveInitReadBuf(uint8 * rdBuf, uint32 bufSize) { /* Check for proper buffer */ if(NULL != rdBuf) { I2C_1_DisableInt(); /* Lock from interruption */ I2C_1_slRdBufPtr = (volatile uint8 *) rdBuf; /* Set buffer pointer */ I2C_1_slRdBufSize = bufSize; /* Set buffer size */ I2C_1_slRdBufIndex = 0u; /* Clear buffer index */ I2C_1_slRdBufIndexTmp = 0u; /* Clear buffer index */ I2C_1_EnableInt(); /* Release lock */ } }
/******************************************************************************* * Function Name: I2C_1_I2CMasterStatus ******************************************************************************** * * Summary: * Returns the master's communication status. * * Parameters: * None * * Return: * Current status of I2C master. * * Global variables: * I2C_1_mstrStatus - used to store current status of I2C Master. * *******************************************************************************/ uint32 I2C_1_I2CMasterStatus(void) { uint32 status; I2C_1_DisableInt(); /* Lock from interruption */ status = (uint32) I2C_1_mstrStatus; if (I2C_1_CHECK_I2C_MASTER_ACTIVE) { /* Add status of master pending transaction: MSTAT_XFER_INP */ status |= (uint32) I2C_1_I2C_MSTAT_XFER_INP; } I2C_1_EnableInt(); /* Release lock */ return(status); }
/******************************************************************************* * Function Name: I2C_1_I2CMasterWriteBuf ******************************************************************************** * * Summary: * Automatically writes an entire buffer of data to a slave device. * Once the data transfer is initiated by this function, further data transfer * is handled by the included ISR. * Enables the I2C interrupt and clears SCB_ I2C_MSTAT_WR_CMPLT status. * * Parameters: * slaveAddr: 7-bit slave address. * xferData: Pointer to buffer of data to be sent. * cnt: Size of buffer to send. * mode: Transfer mode defines: start or restart condition generation at * begin of the transfer and complete the transfer or halt before * generating a stop. * * Return: * Error status. * * Global variables: * I2C_1_mstrStatus - used to store current status of I2C Master. * I2C_1_state - used to store current state of software FSM. * I2C_1_mstrControl - used to control master end of transaction with * or without the Stop generation. * I2C_1_mstrWrBufPtr - used to store pointer to master write buffer. * I2C_1_mstrWrBufIndex - used to current index within master write * buffer. * I2C_1_mstrWrBufSize - used to store master write buffer size. * *******************************************************************************/ uint32 I2C_1_I2CMasterWriteBuf(uint32 slaveAddress, uint8 * wrData, uint32 cnt, uint32 mode) { uint32 errStatus; errStatus = I2C_1_I2C_MSTR_NOT_READY; if(NULL != wrData) /* Check buffer pointer */ { /* Check FSM state and bus before generating Start/ReStart condition */ if(I2C_1_CHECK_I2C_FSM_IDLE) { I2C_1_DisableInt(); /* Lock from interruption */ /* Check bus state */ errStatus = I2C_1_CHECK_I2C_STATUS(I2C_1_I2C_STATUS_BUS_BUSY) ? I2C_1_I2C_MSTR_BUS_BUSY : I2C_1_I2C_MSTR_NO_ERROR; } else if(I2C_1_CHECK_I2C_FSM_HALT) { I2C_1_mstrStatus &= (uint16) ~I2C_1_I2C_MSTAT_XFER_HALT; errStatus = I2C_1_I2C_MSTR_NO_ERROR; } else { /* Unexpected FSM state: exit */ } } /* Check if master is ready to start */ if(I2C_1_I2C_MSTR_NO_ERROR == errStatus) /* No error proceed */ { #if (!I2C_1_CY_SCBIP_V0 && \ I2C_1_I2C_MULTI_MASTER_SLAVE_CONST && I2C_1_I2C_WAKE_ENABLE_CONST) I2C_1_I2CMasterDisableEcAm(); #endif /* (!I2C_1_CY_SCBIP_V0) */ /* Set up write transaction */ I2C_1_state = I2C_1_I2C_FSM_MSTR_WR_ADDR; I2C_1_mstrWrBufIndexTmp = 0u; I2C_1_mstrWrBufIndex = 0u; I2C_1_mstrWrBufSize = cnt; I2C_1_mstrWrBufPtr = (volatile uint8 *) wrData; I2C_1_mstrControl = (uint8) mode; slaveAddress = I2C_1_GET_I2C_8BIT_ADDRESS(slaveAddress); I2C_1_mstrStatus &= (uint16) ~I2C_1_I2C_MSTAT_WR_CMPLT; I2C_1_ClearMasterInterruptSource(I2C_1_INTR_MASTER_ALL); I2C_1_ClearTxInterruptSource(I2C_1_INTR_TX_UNDERFLOW); /* The TX and RX FIFO have to be EMPTY */ /* Generate Start or ReStart */ if(I2C_1_CHECK_I2C_MODE_RESTART(mode)) { I2C_1_I2C_MASTER_GENERATE_RESTART; I2C_1_TX_FIFO_WR_REG = slaveAddress; } else { I2C_1_TX_FIFO_WR_REG = slaveAddress; I2C_1_I2C_MASTER_GENERATE_START; } /* Catch when address is sent */ I2C_1_SetTxInterruptMode(I2C_1_INTR_TX_UNDERFLOW); } I2C_1_EnableInt(); /* Release lock */ return(errStatus); }
/******************************************************************************* * Function Name: I2C_1_I2CMasterReadBuf ******************************************************************************** * * Summary: * Automatically reads an entire buffer of data from a slave device. * Once the data transfer is initiated by this function, further data transfer * is handled by the included ISR. * Enables the I2C interrupt and clears SCB_ I2C_MSTAT_RD_CMPLT status. * * Parameters: * slaveAddr: 7-bit slave address. * xferData: Pointer to buffer where to put data from slave. * cnt: Size of buffer to read. * mode: Transfer mode defines: start or restart condition generation at * begin of the transfer and complete the transfer or halt before * generating a stop. * * Return: * Error status. * * Global variables: * I2C_1_mstrStatus - used to store current status of I2C Master. * I2C_1_state - used to store current state of software FSM. * I2C_1_mstrControl - used to control master end of transaction with * or without the Stop generation. * I2C_1_mstrRdBufPtr - used to store pointer to master write buffer. * I2C_1_mstrRdBufIndex - used to current index within master write * buffer. * I2C_1_mstrRdBufSize - used to store master write buffer size. * *******************************************************************************/ uint32 I2C_1_I2CMasterReadBuf(uint32 slaveAddress, uint8 * rdData, uint32 cnt, uint32 mode) { uint32 errStatus; errStatus = I2C_1_I2C_MSTR_NOT_READY; if(NULL != rdData) { /* Check FSM state and bus before generating Start/ReStart condition */ if(I2C_1_CHECK_I2C_FSM_IDLE) { I2C_1_DisableInt(); /* Lock from interruption */ /* Check bus state */ errStatus = I2C_1_CHECK_I2C_STATUS(I2C_1_I2C_STATUS_BUS_BUSY) ? I2C_1_I2C_MSTR_BUS_BUSY : I2C_1_I2C_MSTR_NO_ERROR; } else if(I2C_1_CHECK_I2C_FSM_HALT) { I2C_1_mstrStatus &= (uint16) ~I2C_1_I2C_MSTAT_XFER_HALT; errStatus = I2C_1_I2C_MSTR_NO_ERROR; } else { /* Unexpected FSM state: exit */ } } /* Check master ready to proceed */ if(I2C_1_I2C_MSTR_NO_ERROR == errStatus) /* No error proceed */ { #if (!I2C_1_CY_SCBIP_V0 && \ I2C_1_I2C_MULTI_MASTER_SLAVE_CONST && I2C_1_I2C_WAKE_ENABLE_CONST) I2C_1_I2CMasterDisableEcAm(); #endif /* (!I2C_1_CY_SCBIP_V0) */ /* Set up read transaction */ I2C_1_state = I2C_1_I2C_FSM_MSTR_RD_ADDR; I2C_1_mstrRdBufIndex = 0u; I2C_1_mstrRdBufSize = cnt; I2C_1_mstrRdBufPtr = (volatile uint8 *) rdData; I2C_1_mstrControl = (uint8) mode; slaveAddress = (I2C_1_GET_I2C_8BIT_ADDRESS(slaveAddress) | I2C_1_I2C_READ_FLAG); I2C_1_mstrStatus &= (uint16) ~I2C_1_I2C_MSTAT_RD_CMPLT; I2C_1_ClearMasterInterruptSource(I2C_1_INTR_MASTER_ALL); /* TX and RX FIFO have to be EMPTY */ /* Generate Start or ReStart */ if(I2C_1_CHECK_I2C_MODE_RESTART(mode)) { I2C_1_I2C_MASTER_GENERATE_RESTART; I2C_1_TX_FIFO_WR_REG = (slaveAddress); } else { I2C_1_TX_FIFO_WR_REG = (slaveAddress); I2C_1_I2C_MASTER_GENERATE_START; } /* Prepare reading */ if(I2C_1_mstrRdBufSize < I2C_1_I2C_FIFO_SIZE) /* Reading byte-by-byte */ { I2C_1_SetRxInterruptMode(I2C_1_INTR_RX_NOT_EMPTY); } else /* Receive RX FIFO chunks */ { I2C_1_ENABLE_MASTER_AUTO_DATA_ACK; I2C_1_SetRxInterruptMode(I2C_1_INTR_RX_FULL); } } I2C_1_EnableInt(); /* Release lock */ return(errStatus); }
void main() { uint8 ButtonPressFlag = 0; int32 temp=0; uint32 lowest=24,i; uint32 increment=2; uint32 battery_volts; volatile uint32 counter=0; CYGlobalIntDisable; /* Intitalize hardware */ LEDControlReg_Write(0xff); /* Turn off the LEDs on PORT2(pin 0-3) and PORT4 pin(0-3) */ PSU_Enable_Write(3); // AMux_1_Start(); /* Enable THe analog mux input to the ADC */ // AcclADC_Start(); /* Start ADC */ // VDAC8_1_Start(); /* Start and configure the VDAC used to measure the Thermistor */ // VDAC8_1_SetRange(VDAC8_1_RANGE_1V); // VDAC8_1_SetValue(200 ); PWM_0_Start(); PWM_1_Start(); PWM_2_Start(); PWM_3_Start(); PWM_4_Start(); PWM_5_Start(); PWM_6_Start(); PWM_7_Start(); // VBATT_ADC_Start(); //VBATT_ADC_StartConvert(); // VBATT_ADC_Stop(); //debugging // UART_1_Start(); I2C_1_Start(); I2C_1_EnableInt(); Button_ClearInterrupt(); ALERT2_ClearPending(); ALERT1_ClearPending(); ALERT1_StartEx(ALERT1_ISR); Button_Pressed_StartEx(Button_Press_ISR); ALERT2_StartEx(ALERT2_ISR); CYGlobalIntEnable; /* Enable global interrupt */ I2C_1_MasterClearStatus(); while(set_ina226(CH1)!=CYRET_SUCCESS); while(set_tmp100(CH1)!=CYRET_SUCCESS); while(1) { if(0) //if(Status_Reg_1_Read()&1) { read_tmp100(CH1); read_ina226(CH1); } LEDControlReg_Write(((uint8)~(PSU_Enable_Read()))); for(i=0;i<65000;i++); LEDControlReg_Write(((uint8)~(PSU_Enable_Read())) & ~(1<<7)); for(i=0;i<65000;i++); // CyPmSaveClocks(); // CyPmSleep(PM_SLEEP_TIME_NONE, PM_SLEEP_SRC_PICU); // CyPmRestoreClocks(); // battery_volts = VBATT_ADC_GetResult32(); if(reset==1) { for(i=0;i<65000;i++); //delay for half a second. PSU_Enable_Write(3); //Both PSU ON I2C_1_Start(); I2C_1_EnableInt(); while(set_ina226(CH1)!=CYRET_SUCCESS); while(set_tmp100(CH1)!=CYRET_SUCCESS); reset=0; } } while(1) { /* Calculate the current board temperature */ temp = Thermistor_TemperatureCompute() / 10; //we get 24.1 as 241. We drop fractionals. uint32 barrels_above= (temp-lowest)/increment; if(temp<=lowest) barrels_above=0; //negative temperatures are too low! if(barrels_above>8) barrels_above=8; LEDControlReg_Write(1<<barrels_above); } }