Skip to content

Commit

Permalink
Add comment lines for explaining meaning of different registries for …
Browse files Browse the repository at this point in the history
…the TDB and its I2C protocol
  • Loading branch information
MSECode authored and valegagge committed May 24, 2024
1 parent 5ad3d2b commit b10757f
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 91 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#
#Thu Dec 21 15:24:27 CET 2023
#Thu May 23 11:01:33 CEST 2024
default.languagetoolchain.version=1.24
default.Pack.dfplocation=C\:\\Program Files\\Microchip\\MPLABX\\v6.15\\packs\\Microchip\\dsPIC33F-GP-MC_DFP\\1.3.64
conf.ids=default
default.languagetoolchain.dir=C\:\\Program Files (x86)\\Microchip\\xc16\\v1.24\\bin
host.id=2o04-h5mj-i
host.id=jxni-i95q-5
default.platformTool.md5=null
configurations-xml=686f227af389f0381a28d871fb823d8b
com-microchip-mplab-nbide-embedded-makeproject-MakeProject.md5=e62346c0c0ecee2637e613b49cb7b7fa
proj.dir=C\:\\Users\\ascalzo\\Documents\\Work\\Repos\\TESTANDDELETE\\icub-firmware\\emBODY\\eBcode\\arch-dspic\\board\\2foc\\appl\\2FOC-V3\\2FOC-V3.X
proj.dir=C\:\\Users\\jlosi\\Workspace\\ICUB-FIRM\\icub-firmware\\emBODY\\eBcode\\arch-dspic\\board\\2foc\\appl\\2FOC-V3\\2FOC-V3.X
host.platform=windows
default.com-microchip-mplab-nbide-toolchain-xc16-XC16LanguageToolchain.md5=07f7da95e66d00aa4668de5175f752c5
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
<file>file:/C:/Users/ascalzo/Documents/Work/Repos/TESTANDDELETE/icub-firmware/emBODY/eBcode/arch-dspic/board/2foc/appl/2FOC-V3/src/qep.c</file>
<file>file:/C:/Users/ascalzo/Documents/Work/Repos/TESTANDDELETE/icub-firmware/emBODY/eBcode/arch-dspic/board/2foc/appl/2FOC-V3/include/qep.h</file>
<file>file:/C:/Users/ascalzo/Documents/Work/Repos/TESTANDDELETE/icub-firmware/emBODY/eBcode/arch-dspic/board/2foc/appl/2FOC-V3/src/2FOC.c</file>
<file>file:/C:/Users/ascalzo/Documents/Work/Repos/TESTANDDELETE/icub-firmware/emBODY/eBcode/arch-dspic/board/2foc/appl/2FOC-V3/src/System.c</file>
<file>file:/C:/Users/ascalzo/Documents/Work/Repos/TESTANDDELETE/icub-firmware/emBODY/eBcode/arch-dspic/board/2foc/appl/2FOC-V3/src/can_icubProto_trasmitter.c</file>
<file>file:/C:/Users/ascalzo/Documents/Work/Repos/TESTANDDELETE/icub-firmware/emBODY/eBcode/arch-dspic/board/2foc/appl/2FOC-V3/include/can_icubProto.h</file>
<file>file:/C:/Users/jlosi/Workspace/ICUB-FIRM/icub-firmware/emBODY/eBcode/arch-dspic/board/2foc/appl/2FOC-V3/src/Faults.c</file>
<file>file:/C:/Users/jlosi/Workspace/ICUB-FIRM/icub-firmware/emBODY/eBcode/arch-dspic/board/2foc/appl/2FOC-V3/src/ecan.c</file>
<file>file:/C:/Users/jlosi/Workspace/ICUB-FIRM/icub-firmware/emBODY/eBcode/arch-dspic/board/2foc/appl/2FOC-V3/src/System.c</file>
<file>file:/C:/Users/jlosi/Workspace/ICUB-FIRM/icub-firmware/emBODY/eBcode/arch-dspic/board/2foc/appl/2FOC-V3/include/i2cTsens.h</file>
<file>file:/C:/Users/jlosi/Workspace/ICUB-FIRM/icub-firmware/emBODY/eBcode/arch-dspic/board/2foc/appl/2FOC-V3/include/Faults.h</file>
<file>file:/C:/Users/jlosi/Workspace/ICUB-FIRM/icub-firmware/emBODY/eBcode/arch-dspic/board/2foc/appl/2FOC-V3/include/can_icubProto.h</file>
<file>file:/C:/Users/jlosi/Workspace/ICUB-FIRM/icub-firmware/emBODY/eBcode/arch-dspic/board/2foc/appl/2FOC-V3/src/can_icubProto_trasmitter.c</file>
<file>file:/C:/Users/jlosi/Workspace/ICUB-FIRM/icub-firmware/emBODY/eBcode/arch-dspic/board/2foc/appl/2FOC-V3/src/can_icubProto_parser.c</file>
<file>file:/C:/Users/jlosi/Workspace/ICUB-FIRM/icub-firmware/emBODY/eBcode/arch-dspic/board/2foc/appl/2FOC-V3/src/2FOC.c</file>
<file>file:/C:/Users/jlosi/Workspace/ICUB-FIRM/icub-firmware/emBODY/eBcode/arch-dspic/board/2foc/appl/2FOC-V3/include/System.h</file>
<file>file:/C:/Users/jlosi/Workspace/ICUB-FIRM/icub-firmware/emBODY/eBcode/arch-dspic/board/2foc/appl/2FOC-V3/include/ecan.h</file>
<file>file:/C:/Users/jlosi/Workspace/ICUB-FIRM/icub-firmware/emBODY/eBcode/arch-dspic/board/2foc/appl/2FOC-V3/include/can_icubProto_parser.h</file>
<file>file:/C:/Users/jlosi/Workspace/ICUB-FIRM/icub-firmware/emBODY/eBcode/arch-dspic/board/2foc/appl/2FOC-V3/include/2FOC.h</file>
<file>file:/C:/Users/jlosi/Workspace/ICUB-FIRM/icub-firmware/emBODY/eBcode/arch-dspic/board/2foc/appl/2FOC-V3/src/qep.c</file>
<file>file:/C:/Users/jlosi/Workspace/ICUB-FIRM/icub-firmware/emBODY/eBcode/arch-dspic/board/2foc/appl/2FOC-V3/include/qep.h</file>
<file>file:/C:/Users/jlosi/Workspace/ICUB-FIRM/icub-firmware/emBODY/eBcode/arch-dspic/board/2foc/appl/2FOC-V3/src/i2cTsens.c</file>
</group>
</open-files>
</project-private>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extern "C" {
int setupI2CTsens(void);
int readI2CTsens(volatile int* temperature);
// function used for generating synthetic temperature data useful for testing
void generateI2CTsensSynthetic(volatile int* temperature);
void generateI2CTsensSynthetic(volatile int* temperature, volatile int spikeTemp);

#ifdef __cplusplus
}
Expand Down
15 changes: 8 additions & 7 deletions emBODY/eBcode/arch-dspic/board/2foc/appl/2FOC-V3/src/System.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void __attribute__((__interrupt__, no_auto_psv)) _T1Interrupt(void)
overheating = TRUE;
}
else if (!err)
{
{
if (gTemperature > gTemperatureLimit)
{
++gTemperatureOverheatingCounter;
Expand All @@ -180,26 +180,27 @@ void __attribute__((__interrupt__, no_auto_psv)) _T1Interrupt(void)
SysError.OverHeatingFailure = TRUE;
FaultConditionsHandler();
}
isTemperatureRead = TRUE;

// simulating spikes
// Synthetic data generation
/*
generateI2CTsensSynthetic(&gTemperature);
/**
generateI2CTsensSynthetic(&gTemperature, gTemperatureSpikeSimulated);
if (gTemperature > gTemperatureLimit)
{
overheating = TRUE;
}
else if (gTemperature < (gTemperatureLimit-gTemperatureLimit/8))
else if (gTemperature < gTemperatureLimit)
{
overheating = FALSE;
}
if (overheating && !SysError.OverHeatingFailure)
if ( (overheating) && (!SysError.OverHeatingFailure))
{
SysError.OverHeatingFailure = TRUE;
FaultConditionsHandler();
}
*/

isTemperatureRead = TRUE;
}
}

Expand Down
134 changes: 92 additions & 42 deletions emBODY/eBcode/arch-dspic/board/2foc/appl/2FOC-V3/src/i2cTsens.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@ void I2CSequentialReadReg(char addr, char byteHigh, char byteLow, char* buff, in



volatile int synthTempCounter = 0;//timer for generation of synthetic data
volatile int synthTempCounter = 0; //timer for generation of synthetic data
int I2Cwdog = 0;
volatile int I2Ccomerrwdog = 0; //counts number of continuos communication error
volatile int I2Cerrcode = 0;
volatile int I2Ccomerrwdog = 0; //counts number of continuos communication error
volatile int I2Cerrcode = 0; //integer for communication error
volatile char I2Cdead = 0;
volatile uint16_t I2Cerrors = 0;
volatile BOOL overheating = FALSE;
Expand All @@ -203,30 +203,31 @@ static int config_sensor(void)
{
I2Cerrcode = 0;

// start
I2C1CONbits.ACKDT = 0; // Reset any ACK
// start phase
I2C1CONbits.ACKDT = 0; // Reset any ACK
I2C1CONbits.SEN = 1;
WHILE(I2C1CONbits.SEN, -1, config_error)
WHILE(I2C1CONbits.SEN, -1, config_error) // SEN: Start Condition Enable bit

// transmit 0xD2
I2C1TRN = 0xD2;
WHILE(I2C1STATbits.TBF, -2, config_error)
// At each transmission phase you have to wait idle and then ack after transmission
// transmit 0xD2 phase
I2C1TRN = 0xD2; // D2 = 1101 0010 --> TABLE 5-1: WRITE CONFIGURATION BITS: MCP3421 device code is 1101. 0010 Initiate Continuous Conversion. Ready bit to zero
WHILE(I2C1STATbits.TBF, -2, config_error) // TBF: Transmit Buffer Full Status bit
// wait Idle
WHILE(I2C1STATbits.TRSTAT, -3, config_error);
WHILE(I2C1STATbits.TRSTAT, -3, config_error); // TRSTAT: Transmit Status bit
// wait ACK
WHILE(I2C1STATbits.ACKSTAT, -4, config_error)
WHILE(I2C1STATbits.ACKSTAT, -4, config_error) // ACKSTAT: Acknowledge Status bit

// transmit 0x19
I2C1TRN = 0x19;
I2C1TRN = 0x19; // 0001 1001 --> PGA: 2x, 15SPS (16 bits), 1 CONT CONV, 5-6 not affected, 7-> RDY -> 0
WHILE(I2C1STATbits.TBF, -5, config_error)
// wait Idle
WHILE(I2C1STATbits.TRSTAT, -6, config_error);
// wait ACK
WHILE(I2C1STATbits.ACKSTAT, -7, config_error)

// stop
I2C1CONbits.RCEN = 0;
I2C1CONbits.PEN=1;
// stop phase
I2C1CONbits.RCEN = 0; // RCEN: Receive Enable bit
I2C1CONbits.PEN=1; // PEN: Stop Condition Enable bit
WHILE(I2C1CONbits.PEN, -8, config_error)

config_error:
Expand All @@ -236,6 +237,7 @@ static int config_sensor(void)

int setupI2CTsens(void)
{
// Need to check these registries
TRISBbits.TRISB8 = 0;
RPOR4bits.RP8R = 0;

Expand All @@ -246,13 +248,13 @@ int setupI2CTsens(void)
ODCBbits.ODCB9=1;

I2C1BRG = 196;//393; // @100kHz; (FCY/FSCL - FCY/1e7) - 1
I2C1CONbits.I2CEN = 0; // Disable I2C
I2C1CONbits.DISSLW = 1; // Disable slew rate control
I2C1CONbits.A10M = 0; // 7-bit slave addr
I2C1CONbits.SCLREL = 1; // SCL release control
I2C1CONbits.I2CEN = 1; // Enable I2C
IEC1bits.MI2C1IE = 0; // Master I2C interrupt
IFS1bits.MI2C1IF = 0; // MI2C Flag
I2C1CONbits.I2CEN = 0; // Disable I2C
I2C1CONbits.DISSLW = 1; // Disable slew rate control
I2C1CONbits.A10M = 0; // 7-bit slave addr
I2C1CONbits.SCLREL = 1; // SCL release control
I2C1CONbits.I2CEN = 1; // Enable I2C
IEC1bits.MI2C1IE = 0; // Master I2C interrupt
IFS1bits.MI2C1IF = 0; // MI2C Flag

__delay32(5000);

Expand Down Expand Up @@ -294,32 +296,40 @@ int readI2CTsens(volatile int* temperature)
*temperature = DEF_VAL_TEMP;

// start
I2C1CONbits.ACKDT = 0; // Reset any ACK
I2C1CONbits.SEN = 1;
I2C1CONbits.ACKDT = 0; // Reset any ACK
// SEN: Start Condition Enable bit (when operating as I2C master)
I2C1CONbits.SEN = 1; // 1 = Initiate Start condition on SDAx and SCLx pins. Hardware clear at end of master Start sequence
WHILE(I2C1CONbits.SEN, -9, I2Ctimeout)

// transmit 0xD3
I2C1TRN = 0xD3;
WHILE(I2C1STATbits.TBF, -10, I2Ctimeout)
I2C1TRN = 0xD3; // D3 = 1101 0011 --> TABLE 5-1: WRITE CONFIGURATION BITS: MCP3421 device code is 1101. 0 1 1 Initiate Continuous Conversion. ready bit to 1.
// TBF: Transmit Buffer Full Status bit
WHILE(I2C1STATbits.TBF, -10, I2Ctimeout) // 1 = Transmit in progress, I2CxTRN is full - 0 = Transmit complete, I2CxTRN is empty
// wait Idle
WHILE(I2C1STATbits.TRSTAT, -11, I2Ctimeout)
// TRSTAT: Transmit Status bit (when operating as I2C master, applicable to master transmit operation)
WHILE(I2C1STATbits.TRSTAT, -11, I2Ctimeout) // 1 = Master transmit is in progress (8 bits + ACK) - 0 = Master transmit is not in progress
// wait ACK
//WHILE(I2C1STATbits.ACKSTAT, -12, I2Ctimeout)

if (!I2C1STATbits.ACKSTAT)
// ACKSTAT: Acknowledge Status bit
if (!I2C1STATbits.ACKSTAT) // Hardware set or clear at end of slave Acknowledge. - 1 = NACK received from slave 0 = ACK received from slave
{
unsigned int buffer[3]={0xFF,0xFF,0xFF};
int b;

for (b=0; b<3; ++b)
{
// receive enable
I2C1CONbits.RCEN = 1;
// RCEN: Receive Enable bit (when operating as I2C master)
I2C1CONbits.RCEN = 1; // 1 = Enables Receive mode for I2C. Hardware clear at end of eighth bit of master receive data byte
Nop();
WHILE(!I2C1STATbits.RBF, -13, I2Ctimeout)
// RBF: Receive Buffer Full Status bit
WHILE(!I2C1STATbits.RBF, -13, I2Ctimeout) // 1 = Receive complete, I2CxRCV is full
buffer[b]=I2C1RCV;
I2C1CONbits.ACKDT = (b==2); // Send ACK/NACK
I2C1CONbits.ACKEN = 1; // Initiate Acknowledge and transmit ACKDT
// ACKDT: Acknowledge Data bit (when operating as I2C master, applicable during master receive)
I2C1CONbits.ACKDT = (b==2); // Send ACK/NACK
// ACKEN: Acknowledge Sequence Enable bit
I2C1CONbits.ACKEN = 1; // Initiate Acknowledge sequence on SDAx and SCLx pins and transmit ACKDT
WHILE(I2C1CONbits.ACKEN, -14, I2Ctimeout)
}

Expand Down Expand Up @@ -373,8 +383,9 @@ int readI2CTsens(volatile int* temperature)
}

// stop
I2C1CONbits.RCEN = 0;
I2C1CONbits.PEN = 1;
I2C1CONbits.RCEN = 0; // RCEN: Receive Enable bit
// PEN: Stop Condition Enable bit (when operating as I2C master)
I2C1CONbits.PEN = 1; // 1 = Initiate Stop condition on SDAx and SCLx pins. Hardware clear at end of master Stop sequence
WHILE(I2C1CONbits.PEN, -15, I2Ctimeout)

return I2Cerrcode;
Expand All @@ -384,7 +395,8 @@ int readI2CTsens(volatile int* temperature)
++I2Cerrors;

// recover
I2C1CONbits.I2CEN = 0;
// I2CEN: I2Cx Enable bit
I2C1CONbits.I2CEN = 0; // 0 = Disables the I2Cx module. All I2C? pins are controlled by port functions

TRISBbits.TRISB8 = 0;
RPOR4bits.RP8R = 0;
Expand All @@ -396,12 +408,12 @@ int readI2CTsens(volatile int* temperature)
PORTBbits.RB8 = c%2;
__delay32(400);
}

I2C1CONbits.I2CEN = 1;
I2C1CONbits.I2CEN = 1; // 1 = Enables the I2Cx module and configures the SDAx and SCLx pins as serial port pins

__delay32(400);

I2C1CON &= 0xFFE0;
I2C1CON &= 0xFFE0; // 1111 1111 1110 0000
I2C1CONbits.PEN = 1;
WHILE(I2C1CONbits.PEN, -16, fatal_error)

Expand All @@ -420,19 +432,57 @@ int readI2CTsens(volatile int* temperature)
return I2Cerrcode;
}

/*
static int rampCounter = 0;
static int savedTemp = 0;
// function used for generating synthetic temperature data useful for testing
// the increase is done in raw value for a PT1000 and the value is 10 deg each 2 call
void generateI2CTsensSynthetic(volatile int* temperature)
void generateI2CTsensSynthetic(volatile int* temperature, volatile int spikeTemp)
{

// increase temp of 10 raw unit
if(synthTempCounter >= 2)
if(synthTempCounter >= 100)
{
*temperature += 897;
if(rampCounter == 0)
{
*temperature += INCR_DEG_10;
if(*temperature > 7573)
{
savedTemp = *temperature;
*temperature = spikeTemp;
rampCounter = 1;
}
}
else if(rampCounter == 1)
{
*temperature = ERR_NUM_10;
rampCounter = 2;
}
else if(rampCounter == 2)
{
*temperature = DEF_VAL_TEMP;
rampCounter = 3;
}
else if(rampCounter == 3)
{
*temperature = savedTemp;
rampCounter = 4;
}
else if(rampCounter == 4)
{
*temperature -= INCR_DEG_10;
if(*temperature < 2647)
{
rampCounter = 0;
}
}
synthTempCounter = 0;
}
else
{
++synthTempCounter;
}
}
*/

0 comments on commit b10757f

Please sign in to comment.