uint8_t readStatusMessage(void){ uint8_t u8MessageLength; uint8_t errorByte; uint8_t i; xSemaphoreTake(xSemaphoreMGC3130DataReady,10*TICKS_PER_MS); /*Pull the TS Line Low*/ MGC3130_TS_PIN_LOW; /*Check to see if the Send the start bit.*/ if(PLIB_I2C_BusIsIdle(I2C_MGC3130)){ PLIB_I2C_MasterStart(I2C_MGC3130); } xSemaphoreTake(xSemaphoreI2CHandler,portMAX_DELAY); /*Send the slave address*/ if(PLIB_I2C_TransmitterIsReady(I2C_MGC3130)){ PLIB_I2C_TransmitterByteSend(I2C_MGC3130, MGC3130_ADDRESS_READ); } xSemaphoreTake(xSemaphoreI2CHandler,portMAX_DELAY); /*Get the I2C module to clock out to receive a byte.*/ if(PLIB_I2C_TransmitterByteWasAcknowledged(I2C_MGC3130)){ PLIB_I2C_MasterReceiverClock1Byte(I2C_MGC3130); } xSemaphoreTake(xSemaphoreI2CHandler,portMAX_DELAY); /*The first byte from the MGC3130 is the length of the message*/ u8MessageLength = PLIB_I2C_ReceivedByteGet(I2C_MGC3130); for(i = 1; i<u8MessageLength; i++){ PLIB_I2C_ReceivedByteAcknowledge(I2C_MGC3130, true); xSemaphoreTake(xSemaphoreI2CHandler,portMAX_DELAY);//Ack completed PLIB_I2C_MasterReceiverClock1Byte(I2C_MGC3130); xSemaphoreTake(xSemaphoreI2CHandler,portMAX_DELAY);//Byte Received if(i == 6){ errorByte = PLIB_I2C_ReceivedByteGet(I2C_MGC3130); } else{ PLIB_I2C_ReceivedByteGet(I2C_MGC3130); } } PLIB_I2C_ReceivedByteAcknowledge(I2C_MGC3130, false); xSemaphoreTake(xSemaphoreI2CHandler,portMAX_DELAY); PLIB_I2C_MasterStop(I2C_MGC3130); xSemaphoreTake(xSemaphoreI2CHandler,portMAX_DELAY);//Stop bit complete MGC3130_TS_PIN_HIGH; return errorByte; }
/* Function: void initMaster (int baudRate, int clockFrequency) Summary: Initializes I2C1 module as the master. */ void initMaster(int baudRate, int clockFrequency) { PLIB_I2C_TransmitterByteSend(I2C_ID_1, 0); PLIB_I2C_SlaveMask7BitSet(I2C_ID_1, 0); PLIB_I2C_SlaveAddress7BitSet(I2C_ID_1, 0); PLIB_I2C_BaudRateSet(I2C_ID_1, clockFrequency, baudRate); PLIB_I2C_Enable(I2C_ID_1); }
void configureMGC3130(unsigned char * message){ uint8_t i; xSemaphoreTake(xSemaphoreMGC3130DataReady,10); /*Pull the TS Line Low*/ MGC3130_TS_PIN_LOW; /*Check to see if the Send the start bit.*/ if(PLIB_I2C_BusIsIdle(I2C_MGC3130)){ PLIB_I2C_MasterStart(I2C_MGC3130); u8Count = 0; } xSemaphoreTake(xSemaphoreI2CHandler,portMAX_DELAY); /*Send the slave address*/ if(PLIB_I2C_TransmitterIsReady(I2C_MGC3130)){ PLIB_I2C_TransmitterByteSend(I2C_MGC3130, MGC3130_ADDRESS_WRITE); } /*Transmit the message*/ for(i=0; i<message[0];i++){ xSemaphoreTake(xSemaphoreI2CHandler,portMAX_DELAY); if(PLIB_I2C_TransmitterByteWasAcknowledged(I2C_MGC3130)){ PLIB_I2C_TransmitterByteSend(I2C_MGC3130, message[i]); // xSemaphoreTake(xSemaphoreI2CHandler,portMAX_DELAY);//Wait for transmit } } xSemaphoreTake(xSemaphoreI2CHandler,portMAX_DELAY); PLIB_I2C_MasterStop(I2C_MGC3130); xSemaphoreTake(xSemaphoreI2CHandler,portMAX_DELAY);//Stop bit complete MGC3130_TS_PIN_HIGH; }
/* Function: void initSlave (int baudRate, int clockFrequency, int address) Summary: Initializes I2C2 module as the slave. */ void initSlave(int baudRate, int clockFrequency, int address) { PLIB_I2C_TransmitterByteSend(I2C_ID_2, 0); PLIB_I2C_SlaveMask7BitSet(I2C_ID_2, 0); PLIB_I2C_BaudRateSet(I2C_ID_2, clockFrequency, baudRate); PLIB_I2C_SlaveAddress7BitSet(I2C_ID_2, address); PLIB_I2C_SlaveClockStretchingEnable(I2C_ID_2); PLIB_I2C_SlaveClockRelease(I2C_ID_2); IEC1 = (1 << 12); // enable i2c2 slave interrupts IPC8 = (2 << 10); // priority level to 2 /* Enable multi-vectored interrupts, enable the generation of interrupts to the CPU */ PLIB_INT_MultiVectorSelect(INT_ID_0); PLIB_INT_Enable(INT_ID_0); PLIB_I2C_Enable(I2C_ID_2); }
bool DRV_I2C0_ByteWrite(const uint8_t byte) { /* Check for receive overflow */ if ( PLIB_I2C_ReceiverOverflowHasOccurred(I2C_ID_4)) { PLIB_I2C_ReceivedByteGet(I2C_ID_4); PLIB_I2C_ReceiverOverflowClear(I2C_ID_4); return false; } /* Check for transmit overflow */ if (PLIB_I2C_TransmitterOverflowHasOccurred(I2C_ID_4)) { PLIB_I2C_TransmitterOverflowClear(I2C_ID_4); return false; } /* Transmit byte */ PLIB_I2C_TransmitterByteSend(I2C_ID_4, byte); return true; }
void __attribute__((interrupt, no_auto_psv)) _MI2C2Interrupt(void) #endif #endif { ACCEL_BOOL polling; ACCEL_CMD_DATA *pCmdData; uint32_t *pIndex; // uint8_t **pRxBuffer; uint8_t *pRxBuffer; // if(I2CPriority == ACCEL_PRIORIY) // { // pCmdData = accelData; // pIndex = &accelIdx; //// pRxBuffer = &AccelRxBuffer; // pRxBuffer = AccelRxBuffer; // } // else // { //// pCmdData = (ACCEL_CMD_DATA *)EepromI2CData; //// pIndex = &EepromI2CIdx; //// pRxBuffer = &EepromRxBuffer; // } pCmdData = accelData; pIndex = &accelIdx; pRxBuffer = AccelRxBuffer; // AccelRxBuffer += 4; if(SYS_INT_SourceStatusGet(ACCEL_BMA250_I2C_SOURCE)) { polling = ACCEL_FALSE; switch(pCmdData[*pIndex].cmd) { case ACCEL_CMD_START: PLIB_I2C_MasterStart(ACCEL_BMA250_I2C_MODULE); break; case ACCEL_CMD_RESTART: PLIB_I2C_MasterStartRepeat(ACCEL_BMA250_I2C_MODULE); break; case ACCEL_CMD_STOP: PLIB_I2C_MasterStop(ACCEL_BMA250_I2C_MODULE); break; case ACCEL_CMD_TX_BYTE: PLIB_I2C_TransmitterByteSend(ACCEL_BMA250_I2C_MODULE, pCmdData[*pIndex].data); break; case ACCEL_CMD_EN_RX: PLIB_I2C_MasterReceiverClock1Byte(ACCEL_BMA250_I2C_MODULE); break; case ACCEL_CMD_RX_BYTE: // **pRxBuffer = PLIB_I2C_ReceivedByteGet(ACCEL_BMA250_I2C_MODULE); // (*pRxBuffer)++; *pRxBuffer = PLIB_I2C_ReceivedByteGet(ACCEL_BMA250_I2C_MODULE); pRxBuffer++; break; case ACCEL_CMD_ACK: PLIB_I2C_ReceivedByteAcknowledge(ACCEL_BMA250_I2C_MODULE, pCmdData[*pIndex].data); break; case ACCEL_CMD_ACK_POLL: polling = ACCEL_TRUE; if(!PLIB_I2C_TransmitterByteWasAcknowledged(ACCEL_BMA250_I2C_MODULE)) { (*pIndex) -= 4; } break; default: accelData[*pIndex].cmd = ACCEL_DONE; case ACCEL_DONE: #ifndef ACCEL_USE_EXTERNAL_INTERUPT_HANDLER SYS_INT_SourceDisable(ACCEL_BMA250_I2C_SOURCE); accelData[0].cmd = ACCEL_DONE; break; #else *pIndex = ACCEL_INVALID_IDX; return; #endif } SYS_INT_SourceStatusClear(ACCEL_BMA250_I2C_SOURCE); if(pCmdData[*pIndex].cmd != ACCEL_DONE) { if((pCmdData[*pIndex].cmd == ACCEL_CMD_RX_BYTE) || (polling)) { SYS_INT_SourceStatusSet(ACCEL_BMA250_I2C_SOURCE); } (*pIndex)++; }else { *pIndex = ACCEL_INVALID_IDX; I2CPriority = FREE_PRIORITY; } } }
uint8_t mgc3130StateMachine(pos_and_gesture_data * pos_and_gesture_struct){ uint8_t u8Success = FALSE; int i; //Send the start bit xSemaphoreTake(xSemaphoreMGC3130DataReady,10*TICKS_PER_MS); if(TS_LINE_IS_LOW){ /*Pull the TS Line Low*/ MGC3130_TS_PIN_LOW; /*Check to see if the Send the start bit.*/ if(PLIB_I2C_BusIsIdle(I2C_MGC3130)){ PLIB_I2C_MasterStart(I2C_MGC3130); } xSemaphoreTake(xSemaphoreI2CHandler,portMAX_DELAY);//Start transmitted /*Send the slave address*/ if(PLIB_I2C_TransmitterIsReady(I2C_MGC3130)){ PLIB_I2C_TransmitterByteSend(I2C_MGC3130, MGC3130_ADDRESS_READ); } xSemaphoreTake(xSemaphoreI2CHandler,portMAX_DELAY);//Address transmitted /*Get the I2C module to clock out to receive a byte.*/ if(PLIB_I2C_TransmitterByteWasAcknowledged(I2C_MGC3130)){ PLIB_I2C_MasterReceiverClock1Byte(I2C_MGC3130); } xSemaphoreTake(xSemaphoreI2CHandler,portMAX_DELAY);//Message Length Received. /*The first byte from the MGC3130 is the length of the message*/ /*The first byte from the MGC3130 is the length of the message*/ u8ReceiveBuffer[0] = PLIB_I2C_ReceivedByteGet(I2C_MGC3130); u8MessageLength = u8ReceiveBuffer[0]; if(u8MessageLength > 0 && u8MessageLength < 0xFF){ for(i = 1; i<u8MessageLength; i++){ PLIB_I2C_ReceivedByteAcknowledge(I2C_MGC3130, true); xSemaphoreTake(xSemaphoreI2CHandler,portMAX_DELAY);//Ack completed PLIB_I2C_MasterReceiverClock1Byte(I2C_MGC3130); xSemaphoreTake(xSemaphoreI2CHandler,portMAX_DELAY);//Byte Received u8ReceiveBuffer[i] = PLIB_I2C_ReceivedByteGet(I2C_MGC3130); } PLIB_I2C_ReceivedByteAcknowledge(I2C_MGC3130, false); } else{ PLIB_I2C_ReceivedByteAcknowledge(I2C_MGC3130, false); } xSemaphoreTake(xSemaphoreI2CHandler,portMAX_DELAY); PLIB_I2C_MasterStop(I2C_MGC3130); xSemaphoreTake(xSemaphoreI2CHandler,portMAX_DELAY);//Stop bit complete MGC3130_TS_PIN_HIGH; if(u8MessageLength > 0){ u8Success = mgc3130_dynamic_extract_pos_and_gest_data(u8ReceiveBuffer, pos_and_gesture_struct); return u8Success; } else{ u8Success = FALSE; } } return u8Success;//Initialized as false. }