driver level has no more connection with the hardware side of the i2c bus, state machine implmentation is working but needs some more states

i2c
kerem 3 years ago
parent 5192d05b68
commit 23c8899f0f

@ -104,26 +104,40 @@ typedef enum{
/*! typedef for the i2c states*/ /*! typedef for the i2c states*/
typedef enum typedef enum
{ {
i2cNotInitialized, /*!< Default Peripheral is not yet Initialized **DEFAULT** */ i2cHwIdle, /*!< Hardware is in Idle Mode **DEFAULT** */
i2cInitialized, /*!< I2C CHannle is initilized but not neceserly ready */ i2cHwStartGenerated, /*!< Generated the star condition */
i2cStartGenrated, /*!< Generated the star condition */ i2cHwOutputBufferFull, /*!< The output buffer of the I2C Periferal is Full */
i2cSlaveAddressSent, /*!< The Salve Address Was Sent to the bus */ i2cHwOutputBufferEmpty, /*!< The output buffer of the I2C Periferal is Empty */
i2cOutputBufferFull, /*!< The output buffer of the I2C Periferal is Full */ i2cHwInputBufferFull, /*!< The input buffer of the I2C Periferal Is Full */
i2cOutputBufferEmpty, /*!< The output buffer of the I2C Periferal is Empty */ i2cHwInputBufferEmpty, /*!< The input buffer of the I2C Periferal Is Empty */
i2cInputBufferFull, /*!< The input buffer of the I2C Periferal Is Full */ i2cHwSentRead, /*!< Sent read request */
i2cInputBufferEmpty, /*!< The input buffer of the I2C Periferal Is Empty */ i2cHwSentWrite, /*!< Sent write request */
i2cReady, /*!< Peripheral Initialized and ready for use */ i2cHwGotAck, /*!< Recieved ACK */
i2cBusy, /*!< An internal process is ongoing */ i2cHwGotNack, /*!< Recieved NACK */
i2cSlaveNotFound, /*!< Desired Slave was not able to be found */ i2cHwSentAck, /*!< Sent ACK */
i2cTransmitting, /*!< Data Transmission process is ongoing */ i2cHwSentNack, /*!< Sent NACK */
i2cRecieving, /*!< Data Reception process is ongoing */ i2cHwStopGenerated, /*!< Generated the star condition */
i2cListening, /*!< Address Listen Mode is ongoing */ i2cHwError /*!< Error */
i2cListeningAndTransmitting,/*!< Address Listen Mode and ongoing Data Transmission */ } i2cHardwareState_t;
i2cListeningAndRecieveing, /*!< Address Listen Mode and ongoing Data Reception */
i2cAbort, /*!< Abort user request ongoing */ typedef enum
i2cTimeout, /*!< Timeout state */ {
i2cError /*!< Error */ i2cPerifNotInitialized, /*!< Default Peripheral is not yet Initialized **DEFAULT** */
} i2cState_t; 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*/ /*! Struture a an i2c channel with all the required propereties*/
typedef struct i2c_t typedef struct i2c_t
@ -139,7 +153,8 @@ typedef struct i2c_t
i2cClockStretching_t stretching; /*!< Clock Stretching enable onyl in slave mode */ i2cClockStretching_t stretching; /*!< Clock Stretching enable onyl in slave mode */
i2cWakeUpTypes_t wakeOn; /*!< Define on which type of action the i2c channel should i2cWakeUpTypes_t wakeOn; /*!< Define on which type of action the i2c channel should
wake up. Only if de prefiral goes to sleep */ 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; }i2c_t;
/*************************************************************************************************** /***************************************************************************************************
@ -273,12 +288,12 @@ void i2cDisablePeriferal(i2c_t *i2cHardware);
* Depending if the I2C channel was initiated as slave or master. * Depending if the I2C channel was initiated as slave or master.
* this function will adapt himself to the selected oppeartion mode (Polling / Int / DMA) * 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 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 registerAddress is the regiter to be red from the device
* @param data is the data to be written * @param data is the data to be written
* @param dataLenght is the total quantity of 8 bit 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 * @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. * Depending if the I2C channel was initiated as slave or master.
* this function will adapt himself to the selected oppeartion mode (Polling / Int / DMA) * 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 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 registerAddress is the regiter to be red from the device
* @param data is the data which has been red * @param data is the data which has been red
* @paran dataLenght is the total quantity of 8 bit data to be 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. * @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) * 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 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 registerAddress is the regiter to be red from the device
* @param data is the data whic has been red * @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. * @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) * 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 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 registerAddress is the regiter to be written to the device
* @param data is the data to be sent * @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. * @brief Recieve a Single Byte as slave from given devices register.
* this function will adapt himself to the selected oppeartion mode (Polling / Int / DMA) * 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 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 registerAddress is the regiter to be red from the device
* @param data is the data whic has been red * @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. * @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) * 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 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 registerAddress is the regiter to be red from the device
* @param data is the data whic has been red * @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 I2C Hardware functions
@ -341,12 +356,6 @@ void i2cSlaveSend(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAdd
*/ */
void i2cIsPeriferalReady(i2c_t *i2cHardware); 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. * @brief Generates a Start condition.
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes * @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 i2cHardware is the beforehand declared i2c channel with his opperation modes
* @param slaveAddress The address of the slave to be communicated * @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. * @brief Send the register that we want to read or write.
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes * @param i2cHardware is the beforehand declared i2c channel with his opperation modes
* @param registerAddress the register that need to be accessed * @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. * @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 * @param i2cHardware is the beforehand declared i2c channel with his opperation modes
*/ */
void i2cIsInputBufferEmpty(i2c_t *i2cHardware); 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 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. * @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 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 registerAddress is the regiter to be red from the device
* @param data is the data whic has been red * @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. * @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 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 registerAddress is the regiter to be written to the device
* @param data is the data to be sent * @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. * @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 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 registerAddress is the regiter to be red from the device
* @param data is the data whic has been red * @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. * @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 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 registerAddress is the regiter to be red from the device
* @param data is the data whic has been red * @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 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 * @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. * 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 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 : * @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. * 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 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. * @brief The selected i2c channel is put on sleep.

@ -22,6 +22,8 @@
#include "i2c.h" #include "i2c.h"
uint8_t temp8 = 0;
uint16_t temp16 = 0;
void i2cEnableHardware(i2c_t *i2cHardware) void i2cEnableHardware(i2c_t *i2cHardware)
{ {
@ -142,40 +144,26 @@ void i2cConfigureFilters(i2c_t *i2cHardware)
} }
/************************************************************************************************** /**************************************************************************************************
I2C Hardware functions I2C Hardware functions
***************************************************************************************************/ ***************************************************************************************************/
void i2cWaitForPeriferal(i2c_t *i2cHardware)
{
while(i2cHardware->state != i2cReady)
{
i2cIsPeriferalReady(i2cHardware);
}
}
void i2cIsPeriferalReady(i2c_t *i2cHardware) void i2cIsPeriferalReady(i2c_t *i2cHardware)
{ {
if((I2C_BASE->ISR & (I2C_ISR_BUSY))!=I2C_ISR_BUSY) if((I2C_BASE->ISR & (I2C_ISR_BUSY))!=I2C_ISR_BUSY)
{ {
i2cHardware->state = i2cReady; i2cHardware->periferalState = i2cPerifReady;
}
else
{
i2cHardware->state = i2cBusy;
} }
} }
void i2cSendSlaveAddress(i2c_t *i2cHardware, uint16_t slaveAddress) void i2cSendSlaveAddress(i2c_t *i2cHardware, uint16_t *slaveAddress)
{ {
if(i2cHardware->addressSize == i2cAddressSizeSevenBits) if(i2cHardware->addressSize == i2cAddressSizeSevenBits)
{ {
// The Slave addrress is automatically place on the output buffer (must be done before the start condition) // 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 //Set Buffer size / which is alredy full with the device address to be sent
I2C_BASE->CR2 |= 1 << I2C_CR2_NBYTES_Pos; I2C_BASE->CR2 |= 1 << I2C_CR2_NBYTES_Pos;
@ -188,7 +176,7 @@ void i2cSendSlaveAddress(i2c_t *i2cHardware, uint16_t slaveAddress)
i2cGenerateStart(i2cHardware); i2cGenerateStart(i2cHardware);
// Wait until the data in the ouput buffer is put to the i2c BUS // Wait until the data in the ouput buffer is put to the i2c BUS
while(i2cHardware->state == i2cOutputBufferFull) while(i2cHardware->hardwareState == i2cHwOutputBufferFull)
{ {
i2cIsOutputBufferEmpty(i2cHardware); i2cIsOutputBufferEmpty(i2cHardware);
} }
@ -200,15 +188,16 @@ void i2cGenerateStart(i2c_t *i2cHardware)
//Wait until the start condition in generated. //Wait until the start condition in generated.
while(!(I2C_BASE->ISR & (I2C_ISR_BUSY))); 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 // 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) void i2cGenerateStop(i2c_t *i2cHardware)
{ {
// Sned stop command // Sned stop command
I2C_BASE->CR2 |= I2C_CR2_STOP; I2C_BASE->CR2 |= I2C_CR2_STOP;
i2cHardware->hardwareState = i2cHwStopGenerated;
} }
void i2cInitiateWriteCommand(i2c_t *i2cHardware) void i2cInitiateWriteCommand(i2c_t *i2cHardware)
@ -225,11 +214,11 @@ void i2cIsOutputBufferEmpty(i2c_t *i2cHardware)
{ {
if((I2C_BASE->ISR & (I2C_ISR_TXE)) == I2C_ISR_TXE) if((I2C_BASE->ISR & (I2C_ISR_TXE)) == I2C_ISR_TXE)
{ {
i2cHardware->state = i2cOutputBufferEmpty; i2cHardware->hardwareState = i2cHwOutputBufferEmpty;
} }
else 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) if((I2C_BASE->ISR & (I2C_ISR_RXNE)) == I2C_ISR_RXNE)
{ {
i2cHardware->state = i2cInputBufferFull; i2cHardware->hardwareState = i2cHwInputBufferFull;
} }
else 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 i2cSetOutputRegister(i2cHardware,registerAddress);
I2C_BASE->TXDR |= registerAddress;
i2cHardware->state = i2cOutputBufferFull; i2cHardware->hardwareState = i2cHwOutputBufferFull;
while(i2cHardware->state == i2cOutputBufferFull) while(i2cHardware->hardwareState == i2cHwOutputBufferFull)
{ {
i2cIsOutputBufferEmpty(i2cHardware); 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) void i2cGenerateNack(i2c_t *i2cHardware)
{ {
if(i2cHardware->mode == i2cModeMaster) if(i2cHardware->mode == i2cModeMaster)
{ {
//IN master mode this ic geretes NACK's otomatically //IN master mode this ic geretes NACK's otomatically
i2cHardware->hardwareState = i2cHwSentNack;
} }
else if(i2cHardware->mode == i2cModeSlave) else if(i2cHardware->mode == i2cModeSlave)
{ {

@ -22,7 +22,8 @@ void i2cInit( i2c_t *i2cHardware, /*!< Pointer to I2C hardware Object *
i2cHardware->opperationMode = opperationMode; i2cHardware->opperationMode = opperationMode;
i2cHardware->stretching = stretching; i2cHardware->stretching = stretching;
i2cHardware->wakeOn = wakeOn; i2cHardware->wakeOn = wakeOn;
i2cHardware->state = i2cNotInitialized; i2cHardware->hardwareState = i2cHwIdle;
i2cHardware->periferalState = i2cPerifNotInitialized;
i2cEnableHardware(i2cHardware); i2cEnableHardware(i2cHardware);
@ -42,7 +43,7 @@ void i2cInit( i2c_t *i2cHardware, /*!< Pointer to I2C hardware Object *
i2cEnablePeriferal(i2cHardware); 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 // 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)
{
while(i2cHardware->periferalState != i2cPerifReady)
{ {
// Wait until no communication is ongoign i2cIsPeriferalReady(i2cHardware);
i2cWaitForPeriferal(i2cHardware); }
i2cHardware->state = i2cTransmitting; i2cHardware->periferalState = i2cPerifTransmitting;
i2cInitiateWriteCommand(i2cHardware); i2cInitiateWriteCommand(i2cHardware);
@ -75,40 +97,29 @@ void i2cMasterRecieve(i2c_t *i2cHardware, uint16_t slaveAddress, uint8_t registe
i2cSendRegisterAddress(i2cHardware,registerAddress); i2cSendRegisterAddress(i2cHardware,registerAddress);
//Is the tranfes complete ? while(i2cHardware->periferalState != i2cPerifTransferComplete)
while(!(I2C_BASE->ISR & (I2C_ISR_TC))); {
i2cIsTransferComplete(i2cHardware);
}
i2cHardware->state = i2cRecieving; i2cHardware->periferalState = i2cPerifRecieving;
i2cInitiateReadCommand(i2cHardware); i2cInitiateReadCommand(i2cHardware);
i2cSendSlaveAddress(i2cHardware, slaveAddress); i2cSendSlaveAddress(i2cHardware, slaveAddress);
//Wait for the input buffer to be full while(i2cHardware->hardwareState != i2cHwInputBufferFull)
while(!(I2C_BASE->ISR & (I2C_ISR_RXNE))); {
i2cIsInputBufferEmpty(i2cHardware);
}
i2cGenerateNack(i2cHardware); i2cGenerateNack(i2cHardware);
i2cGenerateStop(i2cHardware); i2cGenerateStop(i2cHardware);
//read dtaa from the input register to clear it out i2cReadInputRegister(i2cHardware, data);
*data = I2C_BASE->RXDR;
i2cHardware->state = i2cReady;
}
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)
{
i2cHardware->periferalState = i2cPerifReady;
i2cHardware->hardwareState = i2cHwIdle;
} }
void i2cSlaveSend(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data)
{
}

@ -151,12 +151,14 @@ int main(int argc, char *argv[])
print_Usart(usart2, "\n\r"); print_Usart(usart2, "\n\r");
i2cInit(&i2c_1, I2C_CH_1, i2cModeMaster, 0x00,0x00, i2cAddressCountSingle, i2cAddressSizeSevenBits, i2cSpeedStandart, i2cOpperationPolling, i2cClockStretchingDisable, i2cWakeUpDisabled); 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, &registerToRead, &i2cRecieved,1);
printBinary8(i2cRecieved,0); printBinary8(i2cRecieved,0);
print_Usart(usart2, "\n\r"); print_Usart(usart2, "\n\r");
i2cMasterRecieve(&i2c_1,slaveAddress,registerToRead+1,&i2cRecieved); registerToRead += 1;
i2cRead(&i2c_1, &slaveAddress, &registerToRead, &i2cRecieved,1);
printBinary8(i2cRecieved,0); printBinary8(i2cRecieved,0);
print_Usart(usart2, "\n\r"); print_Usart(usart2, "\n\r");

Loading…
Cancel
Save