Skip to content

Commit

Permalink
Feat/5th i2c to be merged for implementing the 5th I2C bus - first st…
Browse files Browse the repository at this point in the history
…ep (#506)
  • Loading branch information
simeonedussoni authored Aug 9, 2024
1 parent 67ad11e commit fa7f652
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ constexpr embot::app::theCANboardInfo::applicationInfo applInfo
#if defined(CUSTOMIZATION_MTB4_FOR_TLR)
embot::prot::can::versionOfAPPLICATION {20, 21, 0},
embot::prot::can::versionOfCANPROTOCOL {20, 0}
#elseif defined(USE_FIFTH_I2C)
embot::prot::can::versionOfAPPLICATION {51, 22, 0},
embot::prot::can::versionOfCANPROTOCOL {2, 0}
#else
embot::prot::can::versionOfAPPLICATION {1, 22, 0},
embot::prot::can::versionOfCANPROTOCOL {2, 0}
#endif
};

//#define USE_FIFTH_I2C

constexpr std::uint16_t threadIDLEstacksize = 512;
constexpr std::uint16_t threadINITstacksize = 2048;
constexpr std::uint16_t threadEVNTstacksize = 5*1024;
Expand Down
90 changes: 79 additions & 11 deletions emBODY/eBcode/arch-arm/board/mtb4/application/src/others/AD7147.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ if2hw_data_ad7147_t CapOffset[16][12];

triangle_cfg_t triangle_cfg_list[16];
error_cap err[16];
5th_triangle_cfg_t triangle_cfg_list[4];
5th_error_cap err[4];

#endif

Expand All @@ -80,6 +82,9 @@ static const unsigned int SHIFT = 2;
static if2hw_data_ad7147_t (*s_AD7147Registers)[12] = NULL;
static if2hw_data_ad7147_t (*s_CapOffset)[12] = NULL;

static if2hw_data_ad7147_t (*s_AD7147Registers_5th)[12] = NULL;
static if2hw_data_ad7147_t (*s_CapOffset_5th)[12] = NULL;

#else


Expand All @@ -88,9 +93,12 @@ static if2hw_data_ad7147_t (*s_CapOffset)[12] = CapOffset;

#endif


uint16_t SKV = 0;
uint16_t taxsel0 = 0;
uint16_t taxsel4 = 0;
uint16_t taxsel5 = 0;

static uint16_t triangleconnectionmask = 0xffff;
static uint32_t triangleconnectionmask = 0xffff;



Expand Down Expand Up @@ -144,10 +152,32 @@ void SetCDCoffsetOnAllTriangles(unsigned char Channel, if2hw_data_ad7147_t cdcOf
void ServiceAD7147Isr(unsigned char Channel)
{
unsigned int i=0;
for (i=0;i<4;i++)
// code added SD + Marco Accame
constexpr size_t nI2Clines {1};
constexpr size_t NumberOfRegistertoRead {12};
for (i=0;i<4;i++)// this 4 is number of addresses on each I2C line
{
ReadViaI2C(Channel,AD7147_ADD[i],(ADCRESULT_S0), 12, s_AD7147Registers[i],s_AD7147Registers[i+4],s_AD7147Registers[i+8],s_AD7147Registers[i+12], 0);
ReadViaI2C(Channel,AD7147_ADD[i],(ADCRESULT_S0), NumberOfRegistertoRead,
s_AD7147Registers[i],
s_AD7147Registers[i+4],
s_AD7147Registers[i+8],
s_AD7147Registers[i+12],
// s_AD7147Registers[i+16],
s_AD7147Registers_5th[i],
0);
}
static int x = 0;
x++;
if(x > 11) x =0;
#ifdef USE_FIFTH_I2C
SKV = s_AD7147Registers_5th[0][x];
#endif
// SKV = s_AD7147Registers[16][x];

taxsel0 = s_AD7147Registers_5th[0][0];
taxsel4 = s_AD7147Registers_5th[0][4];
taxsel5 = s_AD7147Registers_5th[0][5];

}

void TrianglesInit(unsigned char Channel, uint8_t applycdcoffset)
Expand Down Expand Up @@ -189,6 +219,30 @@ void TrianglesInit(unsigned char Channel, uint8_t applycdcoffset)
unconnect=0;
#if !defined(if2hw_common_AD7147_USE_EXTERNALCONTROLTYPES)
triangle_cfg_list[i].shift = SHIFT;
#endif
}
for (i=0;i<4;i++)
{
for (k=0;k<12;k++)
{
s_CapOffset_5th[i][k]=s_AD7147Registers_5th[i][k];
if (s_AD7147Registers_5th[i][k]==0xFFFF)
{
unconnect +=1;
}
}
if (unconnect==12) // no answer from the chip at startup
{
#if !defined(if2hw_common_AD7147_USE_EXTERNALCONTROLTYPES)
err[i].error |=error_notconnected;
#endif
// clear bit i-th
triangleconnectionmask &= (~(0x0001<<i));

}
unconnect=0;
#if !defined(if2hw_common_AD7147_USE_EXTERNALCONTROLTYPES)
5th_triangle_cfg_list[i].shift = SHIFT;
#endif
}
}
Expand All @@ -198,12 +252,16 @@ void TrianglesInit(unsigned char Channel, uint8_t applycdcoffset)
// - new api


extern void ad7147_init(if2hw_data_ad7147_t ext_rawvalues[][12], if2hw_data_ad7147_t ext_capoffsets[][12])
extern void ad7147_init(if2hw_data_ad7147_t ext_rawvalues[][12], if2hw_data_ad7147_t ext_capoffsets[][12],
if2hw_data_ad7147_t ext_rawvalues5th[][12], if2hw_data_ad7147_t ext_capoffsets5th[][12])
{

#if defined(if2hw_common_AD7147_USE_EXTERNALDATA)
s_AD7147Registers = ext_rawvalues;
s_CapOffset = ext_capoffsets;
s_AD7147Registers_5th = ext_rawvalues5th;
s_CapOffset_5th = ext_capoffsets5th;

#endif

triangleconnectionmask = 0;
Expand Down Expand Up @@ -238,23 +296,33 @@ extern void ad7147_acquire(void)
}


extern if2hw_data_ad7147_t * ad7147_get12rawvaluesoftriangle(uint8_t trg)
extern if2hw_data_ad7147_t * ad7147_get12rawvaluesoftriangle(uint16_t trg)
{
if(trg >= triangles_max_num)
if(trg >= triangles_max_num + triangles_add_num )
{
return(NULL);
}
return(s_AD7147Registers[trg]);
else if ( trg >= triangles_max_num)
{
return(s_AD7147Registers_5th[0]);//trg - triangles_max_num]);
}
else
return(s_AD7147Registers[trg]);
}


extern if2hw_data_ad7147_t * ad7147_get12capoffsetsoftriangle(uint8_t trg)
extern if2hw_data_ad7147_t * ad7147_get12capoffsetsoftriangle(uint16_t trg)
{
if(trg >= triangles_max_num)
if(trg >= triangles_max_num + triangles_add_num )
{
return(NULL);
}
return(s_CapOffset[trg]);
else if ( trg >= triangles_max_num)
{
return(s_CapOffset_5th[trg - triangles_max_num]);
}
else
return(s_CapOffset[trg]);
}


Expand Down
17 changes: 10 additions & 7 deletions emBODY/eBcode/arch-arm/board/mtb4/application/src/others/AD7147.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extern "C" {
// - public #define --------------------------------------------------------------------------------------------------

#define triangles_max_num 16
#define triangles_add_num 4
#define MAXVAL 255
#define MINVAL 0
#define NOLOAD 245
Expand All @@ -31,7 +32,7 @@ extern "C" {

enum
{
ad7147_triangles_numberof = 16,
ad7147_triangles_numberof = 20,
ad7147_dots_numberof = 12, // in each triangle
ad7147_dot_value_max = 255,
ad7147_dot_value_min = 0,
Expand Down Expand Up @@ -62,7 +63,7 @@ typedef struct
unsigned int indexInSet; //triangle index in set
unsigned int setNum; //equal to sda num
unsigned int isToUpdate; //if =1 triangle needs to be recalibrated
unsigned int CDCoffset;
unsigned int CDCoffset;
}triangle_cfg_t;

#endif // defined(if2hw_common_AD7147_USE_EXTERNALCONTROLTYPES)
Expand All @@ -78,8 +79,8 @@ typedef struct

#else

extern if2hw_data_ad7147_t AD7147Registers[16][12]; // il primo campo rappresenta il numero dei canali (non c'e' +)
extern if2hw_data_ad7147_t CapOffset[16][12];
extern if2hw_data_ad7147_t AD7147Registers[20][12]; // il primo campo rappresenta il numero dei canali (non c'e' +)
extern if2hw_data_ad7147_t CapOffset[20][12];

#endif

Expand All @@ -104,14 +105,16 @@ void SetCDCoffsetOnAllTriangles(unsigned char Channel, if2hw_data_ad7147_t cdcOf


// new api
extern void ad7147_init(if2hw_data_ad7147_t ext_rawvalues[][12], if2hw_data_ad7147_t ext_capoffsets[][12]);
extern void ad7147_init(
if2hw_data_ad7147_t ext_rawvalues[][12], if2hw_data_ad7147_t ext_capoffsets[][12],
if2hw_data_ad7147_t ext_rawvalues5th[][12], if2hw_data_ad7147_t ext_capoffsets5th[][12]);
extern void ad7147_calibrate(void);
extern void ad7147_set_cdcoffset(uint8_t trg, uint16_t cdcoffset);
extern uint16_t ad7147_gettrianglesconnectedmask(void);
extern void ad7147_acquire(void);
extern uint8_t ad7147_istriangleconnected(uint8_t trg);
extern if2hw_data_ad7147_t * ad7147_get12rawvaluesoftriangle(uint8_t trg);
extern if2hw_data_ad7147_t * ad7147_get12capoffsetsoftriangle(uint8_t trg);
extern if2hw_data_ad7147_t * ad7147_get12rawvaluesoftriangle(uint16_t trg);
extern if2hw_data_ad7147_t * ad7147_get12capoffsetsoftriangle(uint16_t trg);


#ifdef __cplusplus
Expand Down
Loading

0 comments on commit fa7f652

Please sign in to comment.