/** * @file * @brief Contains API of i2c_drv.c. * @details This driver offers functions to send and receive messages * via the I2C1 module. The */ #ifndef I2C_DRV_H #define I2C_DRV_H #ifdef __cplusplus extern "C" { #endif #include "xc.h" #include "exit_state.h" #include "logic.h" #include "exit_state.h" #include "ringbuffer.h" //----------------------------------------------------------------------------// //public constants------------------------------------------------------------// //----------------------------------------------------------------------------// //----------------------------------------------------------------------------// //data declarations-----------------------------------------------------------// //----------------------------------------------------------------------------// //----------------------------------------------------------------------------// //data definitions------------------------------------------------------------// //----------------------------------------------------------------------------// //----------------------------------------------------------------------------// //public function declarations------------------------------------------------// //----------------------------------------------------------------------------// /** * @brief Initializes I2C1 module. * * @details Does following initalization steps: * -# Resets I2C1CON. * -# Turn of in idle mode. * -# Turn of I2C1 module. * -# Disable slew rate so 100kHz mode is possible. * -# Set I2C1BRG to 110 so it runs in 100kHz mode. * -# Enable I2C1 module. * * @return For further information see exit_state.h */ exit_state i2c_drv_init(void); /** * @brief Deinitializes and disables I2C1 module. * * @return For further information see exit_state.h * * @pre I2c_drv_init must be called previous. */ exit_state i2c_drv_deinit(void); //start-----------------------------------------------------------------------// /** * @brief Triggers start sequence of I2C1. * * @details Set I2C1CONbits.SEN bit if I2C1 module is ready for new * instructions. * * @return For further information see exit_state.h * - EXIT_FAILURE Driver is busy and cannot trigger sequence. * - EXIT_SUCCESS Driver triggered sequence succesfull. * * @pre i2c_drv_init must be called previous. */ exit_state i2c_drv_tx_start(void); /** * @brief Checks if start sequence of I2C1 is finished. * * @details Checks if I2C1CONbits.SEN is cleared yet. * * @return For further information see exit_state.h * - EXIT_FAILURE Start sequence is still in progress. * - EXIT_SUCCESS Start sequence is finished. * * @pre i2c_drv_init must be called previous. * @pre i2c_drv_start_set should be called succesfully previous. */ exit_state i2c_drv_tx_start_handler(void); //restart---------------------------------------------------------------------// /** * @brief Triggers restart sequence of I2C1. * * @details Set I2C1CONbits.RSEN bit if I2C1 module is ready for new * instructions. * * @return For further information see exit_state.h * - EXIT_FAILURE Driver is busy and cannot trigger sequence. * - EXIT_SUCCESS Driver triggered sequence succesfull. * * @pre i2c_drv_init must be called previous. */ exit_state i2c_drv_tx_restart(void); /** * @brief Checks if restart sequence of I2C1 is finished. * * @details Checks if I2C1CONbits.RSEN is cleared yet. * * @return For further information see exit_state.h * - EXIT_FAILURE Restart sequence is still in progress. * - EXIT_SUCCESS Restart sequence is finished. * * @pre i2c_drv_init must be called previous. * @pre i2c_drv_restart_set should be called succesfully previous. */ exit_state i2c_drv_tx_restart_handler(void); //stop------------------------------------------------------------------------// /** * @brief Triggers stop sequence of I2C1. * * @details Set I2C1CONbits.PEN bit if I2C1 module is ready for new * instructions. * * @return For further information see exit_state.h * - EXIT_FAILURE Driver is busy and cannot trigger sequence. * - EXIT_SUCCESS Driver triggered sequence succesfull. * * @pre i2c_drv_init must be called previous. */ exit_state i2c_drv_tx_stop(void); /** * @brief Checks if stop sequence of I2C1 is finished. * * @details Checks if I2C1CONbits.PEN is cleared yet. * * @return For further information see exit_state.h * - EXIT_FAILURE Stop sequence is still in progress. * - EXIT_SUCCESS Stop sequence is finished. * * @pre i2c_drv_init must be called previous. * @pre i2c_drv_stop_set should be called succesfully previous. */ exit_state i2c_drv_tx_stop_handler(void); //ack-------------------------------------------------------------------------// //set /** * @brief Triggers ack sequence of I2C1. * * @details Set I2C1CONbits.ACKEN bit if I2C1 module is ready for new * instructions. And sets I2C1CONbits.ACKDT so the next send * acknowledge to ack. * * @return For further information see exit_state.h * - EXIT_FAILURE Driver is busy and cannot trigger sequence. * - EXIT_SUCCESS Driver triggered sequence succesfull. * * @pre i2c_drv_init must be called previous. */ exit_state i2c_drv_tx_ack(void); /** * @brief Checks if ack sequence of I2C1 is finished. * * @details Checks if I2C1CONbits.ACKEN is cleared yet. * * @return For further information see exit_state.h * - EXIT_FAILURE Ack sequence is still in progress. * - EXIT_SUCCESS Ack sequence is finished. * * @pre i2c_drv_init must be called previous. * @pre i2c_drv_ack_set should be called succesfully previous. */ exit_state i2c_drv_tx_ack_handler(void); //get /** * @brief Checks if ack was received. * * @details Read I2C1STATbits.ACKSTAT and return if ack or nack was * received during acknowledge sequence. * * @return For further information see exit_state.h * - EXIT_FAILURE Nack was received during acknowledge. * - EXIT_SUCCESS Ack was received during acknowledge. * * @pre i2c_drv_init must be called previous. * @pre i2c_drv_ack_get_handler must be called succesfully previous. */ exit_state i2c_drv_rx_ack(void); /** * @brief Checks if acknowledge sequence is finished. * * @details Read I2C1STATbits.TRSTAT and check if it is cleared yet. * Hence this function returns EXIT_SUCCESS as soon as the * last transmission was fully progressed. * * @return For further information see exit_state.h * - EXIT_FAILURE No acknowledge was received yet. * - EXIT_SUCCESS An acknowledge was received. * * @pre i2c_drv_init must be called previous. */ exit_state i2c_drv_rx_ack_handler(void); //nack------------------------------------------------------------------------// //Set /** * @brief Triggers nack sequence of I2C1. * * @details Set I2C1CONbits.ACKEN bit if I2C1 module is ready for new * instructions. And sets I2C1CONbits.ACKDT so the next send * acknowledge to nack. * * @return For further information see exit_state.h * - EXIT_FAILURE Driver is busy and cannot trigger sequence. * - EXIT_SUCCESS Driver triggered sequence succesfull. * * @pre i2c_drv_init must be called previous. */ exit_state i2c_drv_tx_nack(void); /** * @brief Checks if nack sequence of I2C1 is finished. * * @details Checks if I2C1CONbits.ACKEN is cleared yet. * * @return For further information see exit_state.h * - EXIT_FAILURE Nack sequence is still in progress. * - EXIT_SUCCESS Nack sequence is finished. * * @pre i2c_drv_init must be called previous. * @pre i2c_drv_nack_set should be called succesfully previous. */ exit_state i2c_drv_tx_nack_handler(void); //get /** * @brief Checks if nack was received. * * @details Read I2C1STATbits.ACKSTAT and return if ack or nack was * received during acknowledge sequence. * * @return For further information see exit_state.h * - EXIT_FAILURE Ack was received during acknowledge. * - EXIT_SUCCESS Nack was received during acknowledge. * * @pre i2c_drv_init must be called previous. * @pre i2c_drv_ack_get_handler must be called succesfully previous. */ exit_state i2c_drv_rx_nack(void); /** * @brief Checks if acknowledge sequence is finished. * * @details Read I2C1STATbits.TRSTAT and check if it is cleared yet. * Hence this function returns EXIT_SUCCESS as soon as the * last transmission was fully progressed. * * @return For further information see exit_state.h * - EXIT_FAILURE No acknowledge was received yet. * - EXIT_SUCCESS An acknowledge was received. * * @pre i2c_drv_init must be called previous. */ exit_state i2c_drv_rx_nack_handler(void); //transmit--------------------------------------------------------------------// /** * @brief Triggers transmission sequence of I2C1. * * @details Write from ringbuffer into I2C1TRN register. * * @param[in] data Data to be transmited. * * @return For further information see exit_state.h * - EXIT_FAILURE Driver is busy and cannot trigger sequence. * - EXIT_SUCCESS Driver triggered sequence succesfull. * * @pre i2c_drv_init must be called previous. */ exit_state i2c_drv_tx_data(uint8_t data); /** * @brief Checks if transmission of I2C1 is fully transmitted. * * @details - Check I2C1STATbits.TBF bit if transmit buffer is empty. * - Check I2C1STATbits.TRSTAT bit if transmission is finished. * * @return For further information see exit_state.h * - EXIT_FAILURE Transmission is still in progress. * - EXIT_SUCCESS Transmission is finished. * * @pre i2c_drv_init must be called previous. * @pre i2c_drv_data_set must be called succesfully previous. */ exit_state i2c_drv_tx_data_handler(void); //receive---------------------------------------------------------------------// /** * @brief Triggers receiving sequence of I2C1. * * @details Write from I2C1RCV into ringbuffer register. * * @param[in] data Pointer to the addres to write received data. * * @return For further information see exit_state.h * - EXIT_FAILURE Driver is busy and cannot trigger sequence. * - EXIT_SUCCESS Driver triggered sequence succesfull. * * @pre i2c_drv_init must be called previous. * @pre i2c_drv_data_get_handler must be called sucessfully previous. */ exit_state i2c_drv_rx_data(uint8_t* data); /** * @brief Checks if receiving of I2C1 is fully received. * * @details - Check I2C1STATbits.RBF bit if receive buffer is full. * - Check I2C1CONbits.RCEN bit if receiving is triggered. * Triggers receiving with first call. * * @return For further information see exit_state.h * - EXIT_FAILURE Transmission is still in progress. * - EXIT_SUCCESS Transmission is finished. * * @pre i2c_drv_init must be called previous. * @pre i2c_drv_data_set must be called succesfully previous. * * @warning Must not be recalled after return EXIT_SUCCESS until data * was read out of I2C1RCV. */ exit_state i2c_drv_rx_data_handler(void); //error management------------------------------------------------------------// /** * @brief Checks if i2c error occured. * * @details - Checks if I2C1STATbits.IWCOL is set. * - Checks if I2C1STATbits.BCL is set. * - Checks if I2C1STATbits.I2COV is set. * * @return For further information see exit_state.h * - EXIT_FAILURE Collision occured. Message will be resend. * - EXIT_SUCCESS No collision detected. * * @pre i2c_drv_init must be called previous. */ exit_state i2c_drv_error_check(void); /** * @brief * * @details * * @param * * @return * * @pre * * @warning */ exit_state i2c_drv_module_check(void); /* * @description : Checks if i2c hardware is ready for next instructions. * @precondition : i2c_drv_init must be called previous. * @version : 1.0 * @category : hardware driver * @parameter : {void} * @return : {exit_state} for further information see exit_state.h * @example : none */ /** * @brief Checks if i2c hardware is ready for next instructions. * * @details - Check the first 5 bits of I2C1CON so queueing of commands * directly in the module is prevented. * * @return For further information see exit_state.h * - EXIT_FAILURE Module is ready. * - EXIT_SUCCESS Module is busy. * * @pre i2c_drv_init must be called previous. */ exit_state i2c_drv_busy_check(void); #ifdef __cplusplus } #endif #endif /* I2C_DRV_H */