11#include "i2c.h"
2+ #include "../../bus/uart/uart1.h"
23#include "../../helpers/delay.h"
34#include "../../registers/system/pin_manager.h"
45
@@ -96,6 +97,7 @@ typedef enum {
9697// the I2C module and determine next states.
9798#define I2C_WRITE_COLLISION_STATUS_BIT I2C2STATbits.IWCOL // Defines the write collision status bit.
9899#define I2C_ACKNOWLEDGE_STATUS_BIT I2C2STATbits.ACKSTAT // I2C ACK status bit.
100+ #define I2C_MASTER_TRANSMISSION_STATUS_BIT I2C2STATbits.TRSTAT // I2C master transmission in progress.
99101
100102#define I2C_START_CONDITION_ENABLE_BIT I2C2CONbits.SEN // I2C START control bit.
101103#define I2C_REPEAT_START_CONDITION_ENABLE_BIT I2C2CONbits.RSEN // I2C Repeated START control bit.
@@ -104,6 +106,9 @@ typedef enum {
104106#define I2C_ACKNOWLEDGE_ENABLE_BIT I2C2CONbits.ACKEN // I2C ACK start control bit.
105107#define I2C_ACKNOWLEDGE_DATA_BIT I2C2CONbits.ACKDT // I2C ACK data control bit.
106108
109+ #define I2C_TRANSMIT_BUFFER_STATUS I2C2STATbits.TBF // Flag for Tx buffer status
110+ #define I2C_RECEIVE_BUFFER_STATUS I2C2STATbits.RBF // Flag for Rx buffer status
111+
107112/**
108113 Section: Local Functions
109114 */
@@ -125,6 +130,8 @@ static uint8_t i2c_trb_count;
125130static I2C_TRANSACTION_REQUEST_BLOCK * p_i2c_trb_current ;
126131static I2C_TR_QUEUE_ENTRY * p_i2c_current = NULL ;
127132
133+ static uint16_t counter ;
134+
128135/**
129136 Section: Driver Interface
130137 */
@@ -660,4 +667,239 @@ response_t I2C_BulkRead(uint8_t *start, uint16_t address, uint8_t *pdata, uint8_
660667 return FAILED ;
661668 }
662669 return SUCCESS ;
663- }
670+ }
671+
672+ response_t I2C_CommandStart (void ) {
673+
674+ uint8_t address = UART1_Read ();
675+
676+ I2C_InitializeIfNot (I2C_GetBaudRate (), DISABLE_INTERRUPTS );
677+ I2C_StartSignal ();
678+ I2C_Transmit (address );
679+ if (I2C_ACKNOWLEDGE_STATUS_BIT && I2C2STATbits .BCL ) {
680+ return FAILED ;
681+ }
682+ return SUCCESS ;
683+ }
684+
685+ response_t I2C_CommandStop (void ) {
686+ I2C_StopSignal ();
687+ return SUCCESS ;
688+ }
689+
690+ response_t I2C_CommandWait (void ) {
691+ I2C_WaitSignal ();
692+ return SUCCESS ;
693+ }
694+
695+ response_t I2C_CommandSend (void ) {
696+
697+ uint8_t data = UART1_Read ();
698+ I2C_Transmit (data );
699+ if (I2C_ACKNOWLEDGE_STATUS_BIT && I2C2STATbits .BCL ) {
700+ return FAILED ;
701+ }
702+ return SUCCESS ;
703+ }
704+
705+ response_t I2C_CommandSendBurst (void ) {
706+
707+ uint8_t data = UART1_Read ();
708+ I2C_Transmit (data );
709+
710+ return DO_NOT_BOTHER ;
711+ }
712+
713+ response_t I2C_CommandRestart (void ) {
714+
715+ uint8_t address = UART1_Read ();
716+ I2C_RestartSignal ();
717+ I2C_Transmit (address );
718+ if (I2C_ACKNOWLEDGE_STATUS_BIT && I2C2STATbits .BCL ) {
719+ return FAILED ;
720+ }
721+ return SUCCESS ;
722+ }
723+
724+ response_t I2C_CommandReadMore (void ) {
725+
726+ uint8_t data = I2C_Receive (I2C_RESPONSE_ACKNOWLEDGE );
727+ UART1_Write (data );
728+
729+ return SUCCESS ;
730+ }
731+
732+ response_t I2C_CommandReadEnd (void ) {
733+
734+ uint8_t data = I2C_Receive (I2C_RESPONSE_NEGATIVE_ACKNOWLEDGE );
735+ UART1_Write (data );
736+
737+ return SUCCESS ;
738+ }
739+
740+ response_t I2C_CommandConfig (void ) {
741+
742+ uint16_t baud_rate = UART1_ReadInt ();
743+ I2C_InitializeIfNot (baud_rate , DISABLE_INTERRUPTS );
744+
745+ return SUCCESS ;
746+ }
747+
748+ response_t I2C_CommandStatus (void ) {
749+
750+ UART1_WriteInt (I2C2STAT );
751+
752+ return SUCCESS ;
753+ }
754+
755+ response_t I2C_CommandReadBulk (void ) {
756+
757+ uint8_t device = UART1_Read ();
758+ uint8_t address = UART1_Read ();
759+ uint8_t count = UART1_Read ();
760+
761+ I2C_StartSignal ();
762+ I2C_Transmit (device << 1 );
763+ I2C_Transmit (address );
764+ I2C_RestartSignal ();
765+ I2C_Transmit ((device << 1 ) | 1 );
766+
767+ uint8_t i ;
768+ for (i = 0 ; i < count ; i ++ ) {
769+ UART1_Write (I2C_Receive (I2C_RESPONSE_ACKNOWLEDGE ));
770+ }
771+ UART1_Write (I2C_Receive (I2C_RESPONSE_NEGATIVE_ACKNOWLEDGE ));
772+
773+ I2C_StopSignal ();
774+
775+ return SUCCESS ;
776+ }
777+
778+ response_t I2C_CommandWriteBulk (void ) {
779+
780+ uint8_t device = UART1_Read ();
781+ uint8_t count = UART1_Read ();
782+
783+ I2C_StartSignal ();
784+ I2C_Transmit (device << 1 );
785+
786+ uint8_t i ;
787+ for (i = 0 ; i < count ; i ++ ) {
788+ I2C_Transmit (UART1_Read ());
789+ }
790+
791+ I2C_StopSignal ();
792+
793+ return SUCCESS ;
794+ }
795+
796+ response_t I2C_CommandEnableSMBus (void ) {
797+
798+ I2C_InitializeSTAT ();
799+ // Enable SMBus input thresholds
800+ I2C2CONbits .SMEN = 1 ;
801+ // Enables I2C2 module and configure SDA2 and SCL2 as serial port pins
802+ I2C2CONbits .I2CEN = 1 ;
803+ }
804+
805+ response_t I2C_CommandDisableSMBus (void ) {
806+
807+ I2C_InitializeSTAT ();
808+ // Disable SMBus input thresholds
809+ I2C2CONbits .SMEN = 0 ;
810+ // Enables I2C2 module and configure SDA2 and SCL2 as serial port pins
811+ I2C2CONbits .I2CEN = 1 ;
812+ return SUCCESS ;
813+ }
814+
815+ response_t I2C_CommandInit (void ) {
816+
817+ I2C_InitializeIfNot (I2C_GetBaudRate (), DISABLE_INTERRUPTS );
818+ return SUCCESS ;
819+ }
820+
821+ response_t I2C_CommandPullDown (void ) {
822+
823+ uint16_t delay = UART1_ReadInt ();
824+
825+ I2C_SCL_SetDigitalOutput ();
826+ I2C_SCL_SetLow ();
827+ DELAY_us (delay );
828+ I2C_SCL_SetHigh ();
829+ I2C_SCL_SetDigitalInput ();
830+
831+ return SUCCESS ;
832+ }
833+
834+ void I2C_StartSignal (void ) {
835+
836+ I2C_START_CONDITION_ENABLE_BIT = 1 ;
837+
838+ counter = 1000 ;
839+ while (I2C_START_CONDITION_ENABLE_BIT && counter -- ) DELAY_us (1 );
840+ }
841+
842+ void I2C_StopSignal (void ) {
843+
844+ I2C_STOP_CONDITION_ENABLE_BIT = 1 ;
845+
846+ counter = 1000 ;
847+ while (I2C_STOP_CONDITION_ENABLE_BIT && counter -- ) DELAY_us (1 );
848+ }
849+
850+ void I2C_RestartSignal (void ) {
851+
852+ I2C_REPEAT_START_CONDITION_ENABLE_BIT = 1 ;
853+
854+ counter = 1000 ;
855+ while (I2C_REPEAT_START_CONDITION_ENABLE_BIT && counter -- ) DELAY_us (1 );
856+ }
857+
858+ void I2C_AcknowledgeSignal (void ) {
859+
860+ I2C_ACKNOWLEDGE_DATA_BIT = 0 ;
861+ I2C_ACKNOWLEDGE_ENABLE_BIT = 1 ;
862+
863+ counter = 1000 ;
864+ while (I2C_ACKNOWLEDGE_ENABLE_BIT && counter -- ) DELAY_us (1 );
865+ }
866+
867+ void I2C_NAcknowledgeSignal (void ) {
868+
869+ I2C_ACKNOWLEDGE_DATA_BIT = 1 ;
870+ I2C_ACKNOWLEDGE_ENABLE_BIT = 1 ;
871+
872+ counter = 1000 ;
873+ while (I2C_ACKNOWLEDGE_ENABLE_BIT && counter -- ) DELAY_us (1 );
874+ }
875+
876+ void I2C_WaitSignal () {
877+
878+ counter = 1000 ;
879+ while (I2C_TRANSMIT_BUFFER_STATUS && counter -- ) DELAY_us (1 );
880+ }
881+
882+ void I2C_Transmit (uint8_t data ) {
883+
884+ I2C_TRANSMIT_REG = data ;
885+ counter = 1000 ;
886+ while (I2C_MASTER_TRANSMISSION_STATUS_BIT && counter -- ) DELAY_us (1 );
887+ }
888+
889+ uint8_t I2C_Receive (I2C_RESPONSE r ) {
890+
891+ I2C_WaitSignal ();
892+ I2C_RECEIVE_ENABLE_BIT = 1 ;
893+
894+ counter = 1000 ;
895+ while (I2C_RECEIVE_ENABLE_BIT && counter -- ) DELAY_us (1 );
896+ while (!I2C_RECEIVE_BUFFER_STATUS && counter -- ) DELAY_us (1 );
897+
898+ uint8_t data = I2C_RECEIVE_REG ;
899+
900+ r == I2C_RESPONSE_ACKNOWLEDGE
901+ ? I2C_AcknowledgeSignal ()
902+ : I2C_NAcknowledgeSignal ();
903+
904+ return data ;
905+ }
0 commit comments