0

I'm working on a school project where we want to monitor the energy consumption using the atmel M90e26s chip.

I used the mcp2210 library and wrote this little testscript:

void talk(hid_device* handle) {
ChipSettingsDef chipDef;

//set GPIO pins to be CS
chipDef = GetChipSettings(handle);

for (int i = 0; i < 9; i++) {
    chipDef.GP[i].PinDesignation = GP_PIN_DESIGNATION_CS;
    chipDef.GP[i].GPIODirection = GPIO_DIRECTION_OUTPUT;
    chipDef.GP[i].GPIOOutput = 1;
}
int r = SetChipSettings(handle, chipDef);

//configure SPI
SPITransferSettingsDef def;
def = GetSPITransferSettings(handle);

//chip select is GP4
def.ActiveChipSelectValue = 0xffef;
def.IdleChipSelectValue = 0xffff;
def.BitRate = 50000l;
def.SPIMode = 4; 

//enable write
byte spiCmdBuffer[3];


//read 8 bytes
def.BytesPerSPITransfer = 3;
r = SetSPITransferSettings(handle, def);
if (r != 0) {
    printf("Errror setting SPI parameters.\n");
    return;
}

spiCmdBuffer[0] = 0x01; //0000 0011 read
spiCmdBuffer[1] = 0x00; //address 0x00

SPIDataTransferStatusDef def1 = SPISendReceive(handle, spiCmdBuffer, 3);

for (int i = 0; i < 8; i++)
    printf("%hhu\n", def1.DataReceived[i]);
}

Any address I try, I get no respons. The problem seems this:

spiCmdBuffer[0] = 0x01; //0000 0011 read
spiCmdBuffer[1] = 0x00; //address 0x00

I know from the datasheet that the spi interface looks like this: spi interface

Can somebody help me to find the address registers from the atm90e26? All the addresses look like '01H', but that is not hexadecimal and it's not 7 bit either.

Gus Vanherf
  • 53
  • 1
  • 6

1 Answers1

0

Yes, as you suspected the problem is in how you set the contents of spiCmdBuffer. The ATM90E26 expects both the read/write flag and the register address to be in the first byte of the SPI transaction: the read/write flag must be put at the most significant bit (value 1 to read from a register, value 0 to write to a register), while the register address is in the 7 remaining bits. So for example to read the register at address 0x01 (SysStatus) the code would look like:

spiCmdBuffer[0] = 0x80 | 0x01;  // read System Status

The 0x80 value sets the read/write flag in the most significant bit, and the other value indicates the register address. The second and third bytes of a 3-byte read sequence don't need to be set to anything, since they are ignored by the ATM90E26.

After calling SPISendReceive(), to extract the register contents (16 bits) you have to read the second and third byte (MSB first) from the data received in the read transaction, like below:

uint16_t regValue = (((uint16_t)def1.DataReceived[1]) << 8) | def1.DataReceived[2];
Francesco Lavra
  • 809
  • 6
  • 16