From 23c8899f0f66421cea537c4e50b591ae6360696a Mon Sep 17 00:00:00 2001 From: kerem Date: Mon, 1 Aug 2022 16:06:19 +0200 Subject: [PATCH] driver level has no more connection with the hardware side of the i2c bus, state machine implmentation is working but needs some more states --- ked/csl/interfaces/i2c.h | 139 ++++++++++++++++++++------------ ked/csl/stm32f042/Src/imp_i2c.c | 71 +++++++++------- ked/drivers/i2c.c | 85 ++++++++++--------- main.c | 8 +- 4 files changed, 180 insertions(+), 123 deletions(-) diff --git a/ked/csl/interfaces/i2c.h b/ked/csl/interfaces/i2c.h index c1d8a2f..56e10a3 100755 --- a/ked/csl/interfaces/i2c.h +++ b/ked/csl/interfaces/i2c.h @@ -104,26 +104,40 @@ typedef enum{ /*! typedef for the i2c states*/ typedef enum { - i2cNotInitialized, /*!< Default Peripheral is not yet Initialized **DEFAULT** */ - i2cInitialized, /*!< I2C CHannle is initilized but not neceserly ready */ - i2cStartGenrated, /*!< Generated the star condition */ - i2cSlaveAddressSent, /*!< The Salve Address Was Sent to the bus */ - i2cOutputBufferFull, /*!< The output buffer of the I2C Periferal is Full */ - i2cOutputBufferEmpty, /*!< The output buffer of the I2C Periferal is Empty */ - i2cInputBufferFull, /*!< The input buffer of the I2C Periferal Is Full */ - i2cInputBufferEmpty, /*!< The input buffer of the I2C Periferal Is Empty */ - i2cReady, /*!< Peripheral Initialized and ready for use */ - i2cBusy, /*!< An internal process is ongoing */ - i2cSlaveNotFound, /*!< Desired Slave was not able to be found */ - i2cTransmitting, /*!< Data Transmission process is ongoing */ - i2cRecieving, /*!< Data Reception process is ongoing */ - i2cListening, /*!< Address Listen Mode is ongoing */ - i2cListeningAndTransmitting,/*!< Address Listen Mode and ongoing Data Transmission */ - i2cListeningAndRecieveing, /*!< Address Listen Mode and ongoing Data Reception */ - i2cAbort, /*!< Abort user request ongoing */ - i2cTimeout, /*!< Timeout state */ - i2cError /*!< Error */ -} i2cState_t; + i2cHwIdle, /*!< Hardware is in Idle Mode **DEFAULT** */ + i2cHwStartGenerated, /*!< Generated the star condition */ + i2cHwOutputBufferFull, /*!< The output buffer of the I2C Periferal is Full */ + i2cHwOutputBufferEmpty, /*!< The output buffer of the I2C Periferal is Empty */ + i2cHwInputBufferFull, /*!< The input buffer of the I2C Periferal Is Full */ + i2cHwInputBufferEmpty, /*!< The input buffer of the I2C Periferal Is Empty */ + i2cHwSentRead, /*!< Sent read request */ + i2cHwSentWrite, /*!< Sent write request */ + i2cHwGotAck, /*!< Recieved ACK */ + i2cHwGotNack, /*!< Recieved NACK */ + i2cHwSentAck, /*!< Sent ACK */ + i2cHwSentNack, /*!< Sent NACK */ + i2cHwStopGenerated, /*!< Generated the star condition */ + i2cHwError /*!< Error */ +} i2cHardwareState_t; + +typedef enum +{ + i2cPerifNotInitialized, /*!< Default Peripheral is not yet Initialized **DEFAULT** */ + i2cPerifInitialized, /*!< I2C CHannle is initilized but not neceserly ready */ + i2cPerifSlaveAddressSent, /*!< The Salve Address Was Sent to the bus */ + i2cPerifTransferComplete, /*!< A full communication process is complete (wire or Read) */ + i2cPerifTransferOngoign, /*!< A full communication process is in progress (wire or Read) */ + i2cPerifReady, /*!< Peripheral Initialized and ready for use */ + i2cPerifSlaveNotFound, /*!< Desired Slave was not able to be found */ + i2cPerifTransmitting, /*!< Data Transmission process is ongoing */ + i2cPerifRecieving, /*!< Data Reception process is ongoing */ + i2cPerifListening, /*!< Address Listen Mode is ongoing */ + i2cPerifListeningAndTransmitting, /*!< Address Listen Mode and ongoing Data Transmission */ + i2cPerifListeningAndRecieveing, /*!< Address Listen Mode and ongoing Data Reception */ + i2cPerifAbort, /*!< Abort user request ongoing */ + i2cPerifTimeout, /*!< Timeout state */ + i2cPerifError /*!< Error */ +} i2cPeriferalState_t; /*! Struture a an i2c channel with all the required propereties*/ typedef struct i2c_t @@ -139,7 +153,8 @@ typedef struct i2c_t i2cClockStretching_t stretching; /*!< Clock Stretching enable onyl in slave mode */ i2cWakeUpTypes_t wakeOn; /*!< Define on which type of action the i2c channel should wake up. Only if de prefiral goes to sleep */ - i2cState_t state; /*!< The current sitate of the I2C Bus */ + i2cHardwareState_t hardwareState; /*!< The current sitate of the I2C Bus */ + i2cPeriferalState_t periferalState; /*!< The current sitate of the I2C Bus */ }i2c_t; /*************************************************************************************************** @@ -273,12 +288,12 @@ void i2cDisablePeriferal(i2c_t *i2cHardware); * Depending if the I2C channel was initiated as slave or master. * this function will adapt himself to the selected oppeartion mode (Polling / Int / DMA) * @param i2cHardware is the beforehand declared i2c channel with his opperation modes - * @param devAddress is the address of the device to which the read command will be sent + * @param slaveAddress is the address of the device to which the read command will be sent * @param registerAddress is the regiter to be red from the device * @param data is the data to be written * @param dataLenght is the total quantity of 8 bit data to be written. */ -void i2cWrite(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data, uint8_t dataLenght); +void i2cWrite(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data, uint8_t *dataLenght); /** * @brief Reads the given amount of data to the selected device. This function will @@ -286,51 +301,51 @@ void i2cWrite(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress * Depending if the I2C channel was initiated as slave or master. * this function will adapt himself to the selected oppeartion mode (Polling / Int / DMA) * @param i2cHardware is the beforehand declared i2c channel with his opperation modes - * @param devAddress is the address of the device to which the read command will be sent + * @param slaveAddress is the address of the device to which the read command will be sent * @param registerAddress is the regiter to be red from the device * @param data is the data which has been red * @paran dataLenght is the total quantity of 8 bit data to be red */ -void i2cRead(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data, uint8_t dataLenght); +void i2cRead(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data, uint8_t *dataLenght); /** * @brief Recieve a Single Byte as master from from the given devices register. * this function will adapt himself to the selected oppeartion mode (Polling / Int / DMA) * @param i2cHardware is the beforehand declared i2c channel with his opperation modes - * @param devAddress is the address of the device to which the read command will be sent + * @param slaveAddress is the address of the device to which the read command will be sent * @param registerAddress is the regiter to be red from the device * @param data is the data whic has been red */ -void i2cMasterRecieve(i2c_t *i2cHardware, uint16_t slaveAddress, uint8_t registerAddress, uint8_t *data); +void i2cMasterRecieve(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data); /** * @brief Sends a Single Byte as master to the given devices register. * this function will adapt himself to the selected oppeartion mode (Polling / Int / DMA) * @param i2cHardware is the beforehand declared i2c channel with his opperation modes - * @param devAddress is the address of the device to which the send command will be sent + * @param slaveAddress is the address of the device to which the send command will be sent * @param registerAddress is the regiter to be written to the device * @param data is the data to be sent */ -void i2cMasterSend(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data); +void i2cMasterSend(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data); /** * @brief Recieve a Single Byte as slave from given devices register. * this function will adapt himself to the selected oppeartion mode (Polling / Int / DMA) * @param i2cHardware is the beforehand declared i2c channel with his opperation modes - * @param devAddress is the address of the device to which the read command will be sent + * @param slaveAddress is the address of the device to which the read command will be sent * @param registerAddress is the regiter to be red from the device * @param data is the data whic has been red */ -void i2cSlaveRecieve(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data); +void i2cSlaveRecieve(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data); /** * @brief Recieve a Single Byte as slave from the given devices register. * this function will adapt himself to the selected oppeartion mode (Polling / Int / DMA) * @param i2cHardware is the beforehand declared i2c channel with his opperation modes - * @param devAddress is the address of the device to which the read command will be sent + * @param slaveAddress is the address of the device to which the read command will be sent * @param registerAddress is the regiter to be red from the device * @param data is the data whic has been red */ -void i2cSlaveSend(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data); +void i2cSlaveSend(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data); /************************************************************************************************** I2C Hardware functions @@ -341,12 +356,6 @@ void i2cSlaveSend(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAdd */ void i2cIsPeriferalReady(i2c_t *i2cHardware); -/** - * @brief Checks if the device is ready for any type of communication - * @param i2cHardware is the beforehand declared i2c channel with his opperation modes - */ -void i2cWaitForPeriferal(i2c_t *i2cHardware); - /** * @brief Generates a Start condition. * @param i2cHardware is the beforehand declared i2c channel with his opperation modes @@ -377,14 +386,14 @@ void i2cGenerateAck(i2c_t *i2cHardware); * @param i2cHardware is the beforehand declared i2c channel with his opperation modes * @param slaveAddress The address of the slave to be communicated */ -void i2cSendSlaveAddress(i2c_t *i2cHardware, uint16_t slaveAddress); +void i2cSendSlaveAddress(i2c_t *i2cHardware, uint16_t *slaveAddress); /** * @brief Send the register that we want to read or write. * @param i2cHardware is the beforehand declared i2c channel with his opperation modes * @param registerAddress the register that need to be accessed */ -void i2cSendRegisterAddress(i2c_t *i2cHardware, uint8_t registerAddress); +void i2cSendRegisterAddress(i2c_t *i2cHardware, uint8_t *registerAddress); /** * @brief Initiates a Write command with the previously set slave address. @@ -411,6 +420,30 @@ void i2cIsOutputBufferEmpty(i2c_t *i2cHardware); * @param i2cHardware is the beforehand declared i2c channel with his opperation modes */ void i2cIsInputBufferEmpty(i2c_t *i2cHardware); + +/** + * @brief reads the I2C input buffer + * is sent to the i2c BUS. + * @param i2cHardware is the beforehand declared i2c channel with his opperation modes + * @param data pointer to the data that need to be read and returned + */ +void i2cReadInputRegister(i2c_t *i2cHardware, uint8_t *data); + +/** + * @brief writes to the I2C output buffer + * is sent to the i2c BUS. + * @param i2cHardware is the beforehand declared i2c channel with his opperation modes + * @param data pointer to the data that need to be read and returned + */ +void i2cSetOutputRegister(i2c_t *i2cHardware, uint8_t *data); + +/** + * @brief Checks is transfer is complete + * is sent to the i2c BUS. + * @param i2cHardware is the beforehand declared i2c channel with his opperation modes + * @param data pointer to the data that need to be read and returned + */ +void i2cIsTransferComplete(i2c_t *i2cHardware); /************************************************************************************************** I2C Communication functions Polling / Blocking Mode ***************************************************************************************************/ @@ -418,37 +451,37 @@ void i2cIsInputBufferEmpty(i2c_t *i2cHardware); /** * @brief Recieve a Single Byte in polling mode as master from from the given devices register. * @param i2cHardware is the beforehand declared i2c channel with his opperation modes - * @param devAddress is the address of the device to which the read command will be sent + * @param slaveAddress is the address of the device to which the read command will be sent * @param registerAddress is the regiter to be red from the device * @param data is the data whic has been red */ -void i2cMasterRecievePolling(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data); +void i2cMasterRecievePolling(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data); /** * @brief Sends a Single Byte in polling mode as master to the given devices register. * @param i2cHardware is the beforehand declared i2c channel with his opperation modes - * @param devAddress is the address of the device to which the send command will be sent + * @param slaveAddress is the address of the device to which the send command will be sent * @param registerAddress is the regiter to be written to the device * @param data is the data to be sent */ -void i2cMasterSendPolling(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data); +void i2cMasterSendPolling(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data); /** * @brief Recieve a Single Byte in polling mide as slave from the given devices register. * @param i2cHardware is the beforehand declared i2c channel with his opperation modes - * @param devAddress is the address of the device to which the read command will be sent + * @param slaveAddress is the address of the device to which the read command will be sent * @param registerAddress is the regiter to be red from the device * @param data is the data whic has been red */ -void i2cSlaveRecievePolling(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data); +void i2cSlaveRecievePolling(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data); /** * @brief Recieve a Single Byte in polling mode as slave from the given devices register. * @param i2cHardware is the beforehand declared i2c channel with his opperation modes - * @param devAddress is the address of the device to which the read command will be sent + * @param slaveAddress is the address of the device to which the read command will be sent * @param registerAddress is the regiter to be red from the device * @param data is the data whic has been red */ -void i2cSlaveSendPolling(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data); +void i2cSlaveSendPolling(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data); /************************************************************************************************** I2C Arbitration Functions for Multymaster mode and slaves clock stretching @@ -478,17 +511,17 @@ void i2cDiscoverDevices(i2c_t *i2cHardware, uint16_t *devices, uint8_t deviceCou * @brief This function will try to communicate with a device with every speed * allowed by his hardware to find out the maximum communication sppeed possible. * @param i2cHardware is the beforehand declared i2c channel with his opperation modes - * @param devAddress is the address of the device to which the read command will be sent + * @param slaveAddress is the address of the device to which the read command will be sent */ -i2cSpeed_t i2cTestDeviceSpeed(i2c_t *i2cHardware, uint16_t *devAddress); +i2cSpeed_t i2cTestDeviceSpeed(i2c_t *i2cHardware, uint16_t *slaveAddress); /** * @brief This function will read the device info register (if awailable) as follows : * I2c Standart : 3 Bytes (24 bits) | 12 Bits : Manufacturer info | 9 Bits: Part Identification | 3 Bits DIE Rev. * @param i2cHardware is the beforehand declared i2c channel with his opperation modes - * @param devAddress is the address of the device to be red + * @param slaveAddress is the address of the device to be red */ -uint32_t i2cReadDeviceInfo(i2c_t *i2cHardware, uint16_t *devAddress); +uint32_t i2cReadDeviceInfo(i2c_t *i2cHardware, uint16_t *slaveAddress); /** * @brief The selected i2c channel is put on sleep. diff --git a/ked/csl/stm32f042/Src/imp_i2c.c b/ked/csl/stm32f042/Src/imp_i2c.c index 0f1c9c7..607356e 100644 --- a/ked/csl/stm32f042/Src/imp_i2c.c +++ b/ked/csl/stm32f042/Src/imp_i2c.c @@ -22,6 +22,8 @@ #include "i2c.h" +uint8_t temp8 = 0; +uint16_t temp16 = 0; void i2cEnableHardware(i2c_t *i2cHardware) { @@ -142,40 +144,26 @@ void i2cConfigureFilters(i2c_t *i2cHardware) } - - /************************************************************************************************** I2C Hardware functions ***************************************************************************************************/ -void i2cWaitForPeriferal(i2c_t *i2cHardware) -{ - while(i2cHardware->state != i2cReady) - { - i2cIsPeriferalReady(i2cHardware); - } -} - void i2cIsPeriferalReady(i2c_t *i2cHardware) { if((I2C_BASE->ISR & (I2C_ISR_BUSY))!=I2C_ISR_BUSY) { - i2cHardware->state = i2cReady; - } - else - { - i2cHardware->state = i2cBusy; + i2cHardware->periferalState = i2cPerifReady; } } -void i2cSendSlaveAddress(i2c_t *i2cHardware, uint16_t slaveAddress) +void i2cSendSlaveAddress(i2c_t *i2cHardware, uint16_t *slaveAddress) { if(i2cHardware->addressSize == i2cAddressSizeSevenBits) { // The Slave addrress is automatically place on the output buffer (must be done before the start condition) - I2C_BASE->CR2 |= (slaveAddress & 0xff) << 1; // The bit no 0 is not taken in concideration in 7bit mode + I2C_BASE->CR2 |= (*slaveAddress & 0xff) << 1; // The bit no 0 is not taken in concideration in 7bit mode //Set Buffer size / which is alredy full with the device address to be sent I2C_BASE->CR2 |= 1 << I2C_CR2_NBYTES_Pos; @@ -188,7 +176,7 @@ void i2cSendSlaveAddress(i2c_t *i2cHardware, uint16_t slaveAddress) i2cGenerateStart(i2cHardware); // Wait until the data in the ouput buffer is put to the i2c BUS - while(i2cHardware->state == i2cOutputBufferFull) + while(i2cHardware->hardwareState == i2cHwOutputBufferFull) { i2cIsOutputBufferEmpty(i2cHardware); } @@ -200,15 +188,16 @@ void i2cGenerateStart(i2c_t *i2cHardware) //Wait until the start condition in generated. while(!(I2C_BASE->ISR & (I2C_ISR_BUSY))); - i2cHardware->state = i2cStartGenrated; + i2cHardware->hardwareState = i2cHwStartGenerated; // This device places the salve address automaticalyy in the buffer before sending the star condition - i2cHardware->state = i2cOutputBufferFull; + i2cHardware->hardwareState = i2cHwOutputBufferFull; } void i2cGenerateStop(i2c_t *i2cHardware) { // Sned stop command I2C_BASE->CR2 |= I2C_CR2_STOP; + i2cHardware->hardwareState = i2cHwStopGenerated; } void i2cInitiateWriteCommand(i2c_t *i2cHardware) @@ -225,11 +214,11 @@ void i2cIsOutputBufferEmpty(i2c_t *i2cHardware) { if((I2C_BASE->ISR & (I2C_ISR_TXE)) == I2C_ISR_TXE) { - i2cHardware->state = i2cOutputBufferEmpty; + i2cHardware->hardwareState = i2cHwOutputBufferEmpty; } else { - i2cHardware->state = i2cOutputBufferFull; + i2cHardware->hardwareState = i2cHwOutputBufferFull; } } @@ -237,32 +226,54 @@ void i2cIsInputBufferEmpty(i2c_t *i2cHardware) { if((I2C_BASE->ISR & (I2C_ISR_RXNE)) == I2C_ISR_RXNE) { - i2cHardware->state = i2cInputBufferFull; + i2cHardware->hardwareState = i2cHwInputBufferFull; } else { - i2cHardware->state = i2cInputBufferEmpty; + i2cHardware->hardwareState = i2cHwInputBufferEmpty; } } -void i2cSendRegisterAddress(i2c_t *i2cHardware, uint8_t registerAddress) +void i2cSendRegisterAddress(i2c_t *i2cHardware, uint8_t *registerAddress) { - //Register to be sent - I2C_BASE->TXDR |= registerAddress; - - i2cHardware->state = i2cOutputBufferFull; + i2cSetOutputRegister(i2cHardware,registerAddress); + + i2cHardware->hardwareState = i2cHwOutputBufferFull; - while(i2cHardware->state == i2cOutputBufferFull) + while(i2cHardware->hardwareState == i2cHwOutputBufferFull) { i2cIsOutputBufferEmpty(i2cHardware); } } +void i2cReadInputRegister(i2c_t *i2cHardware, uint8_t *data) +{ + *data = I2C_BASE->RXDR; +} + +void i2cSetOutputRegister(i2c_t *i2cHardware, uint8_t *data) +{ + I2C_BASE->TXDR |= *data; +} + +void i2cIsTransferComplete(i2c_t *i2cHardware) +{ + if((I2C_BASE->ISR & (I2C_ISR_TC)) == I2C_ISR_TC) + { + i2cHardware->periferalState = i2cPerifTransferComplete; + } + else + { + i2cHardware->periferalState = i2cPerifTransferOngoign; + } +} + void i2cGenerateNack(i2c_t *i2cHardware) { if(i2cHardware->mode == i2cModeMaster) { //IN master mode this ic geretes NACK's otomatically + i2cHardware->hardwareState = i2cHwSentNack; } else if(i2cHardware->mode == i2cModeSlave) { diff --git a/ked/drivers/i2c.c b/ked/drivers/i2c.c index 972e6ca..8001c0d 100644 --- a/ked/drivers/i2c.c +++ b/ked/drivers/i2c.c @@ -22,7 +22,8 @@ void i2cInit( i2c_t *i2cHardware, /*!< Pointer to I2C hardware Object * i2cHardware->opperationMode = opperationMode; i2cHardware->stretching = stretching; i2cHardware->wakeOn = wakeOn; - i2cHardware->state = i2cNotInitialized; + i2cHardware->hardwareState = i2cHwIdle; + i2cHardware->periferalState = i2cPerifNotInitialized; i2cEnableHardware(i2cHardware); @@ -42,7 +43,7 @@ void i2cInit( i2c_t *i2cHardware, /*!< Pointer to I2C hardware Object * i2cEnablePeriferal(i2cHardware); - i2cHardware->state = i2cInitialized; + i2cHardware->periferalState = i2cPerifNotInitialized; } @@ -51,23 +52,44 @@ void i2cDeInit(i2c_t *i2cHardware) } -void i2cWrite( i2c_t *i2cHardware, - uint16_t *devAddress, - uint8_t *registerAddress, - uint8_t *data, - uint8_t dataLenght) -{ +void i2cRead(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data, uint8_t *dataLenght) +{ + switch(i2cHardware->mode) + { + case i2cModeMaster: + i2cMasterRecieve(i2cHardware, slaveAddress, registerAddress, data); + break; + case i2cModeSlave: + break; + case i2cModeMultyMaster: + break; + } } - // this function still doesn't implment 10 bit oopeartion TODO -void i2cMasterRecieve(i2c_t *i2cHardware, uint16_t slaveAddress, uint8_t registerAddress, uint8_t *data) +void i2cMasterRecieve(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data) +{ + switch(i2cHardware->opperationMode) + { + case i2cOpperationPolling : + i2cMasterRecievePolling(i2cHardware, slaveAddress, registerAddress, data); + break; + case i2cOpperationInt: + break; + case i2cOpperationDma: + break; + } +} + +void i2cMasterRecievePolling(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data) { - // Wait until no communication is ongoign - i2cWaitForPeriferal(i2cHardware); + while(i2cHardware->periferalState != i2cPerifReady) + { + i2cIsPeriferalReady(i2cHardware); + } - i2cHardware->state = i2cTransmitting; + i2cHardware->periferalState = i2cPerifTransmitting; i2cInitiateWriteCommand(i2cHardware); @@ -75,40 +97,29 @@ void i2cMasterRecieve(i2c_t *i2cHardware, uint16_t slaveAddress, uint8_t registe i2cSendRegisterAddress(i2cHardware,registerAddress); - //Is the tranfes complete ? - while(!(I2C_BASE->ISR & (I2C_ISR_TC))); + while(i2cHardware->periferalState != i2cPerifTransferComplete) + { + i2cIsTransferComplete(i2cHardware); + } - i2cHardware->state = i2cRecieving; + i2cHardware->periferalState = i2cPerifRecieving; i2cInitiateReadCommand(i2cHardware); i2cSendSlaveAddress(i2cHardware, slaveAddress); - //Wait for the input buffer to be full - while(!(I2C_BASE->ISR & (I2C_ISR_RXNE))); - + while(i2cHardware->hardwareState != i2cHwInputBufferFull) + { + i2cIsInputBufferEmpty(i2cHardware); + } + i2cGenerateNack(i2cHardware); i2cGenerateStop(i2cHardware); - //read dtaa from the input register to clear it out - *data = I2C_BASE->RXDR; + i2cReadInputRegister(i2cHardware, data); - i2cHardware->state = i2cReady; + i2cHardware->periferalState = i2cPerifReady; + i2cHardware->hardwareState = i2cHwIdle; } - -void i2cMasterSend(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data) -{ - -} - -void i2cSlaveRecieve(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data) -{ - -} - -void i2cSlaveSend(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data) -{ - -} diff --git a/main.c b/main.c index 035392f..06ba472 100644 --- a/main.c +++ b/main.c @@ -151,12 +151,14 @@ int main(int argc, char *argv[]) print_Usart(usart2, "\n\r"); i2cInit(&i2c_1, I2C_CH_1, i2cModeMaster, 0x00,0x00, i2cAddressCountSingle, i2cAddressSizeSevenBits, i2cSpeedStandart, i2cOpperationPolling, i2cClockStretchingDisable, i2cWakeUpDisabled); - i2cMasterRecieve(&i2c_1,slaveAddress,registerToRead,&i2cRecieved); + i2cRead(&i2c_1, &slaveAddress, ®isterToRead, &i2cRecieved,1); printBinary8(i2cRecieved,0); print_Usart(usart2, "\n\r"); - - i2cMasterRecieve(&i2c_1,slaveAddress,registerToRead+1,&i2cRecieved); + + registerToRead += 1; + + i2cRead(&i2c_1, &slaveAddress, ®isterToRead, &i2cRecieved,1); printBinary8(i2cRecieved,0); print_Usart(usart2, "\n\r");