3

bcm2835 ライブラリで mlx90614 のスレーブ アドレスを変更するにはどうすればよいですか? 次のコードを試しました...

int main()
{
   // Buffer, where I store data which I'll send
   unsigned char buf[6];

   // bcm2835 i2c module intialisation code
   bcm2835_init();
   bcm2835_i2c_begin();
   bcm2835_i2c_set_baudrate(25000);
   bcm2835_i2c_setSlaveAddress(0x00);

   // For debug purposes, I read what reason codes operations give.
   bcm2835I2CReasonCodes why;
   bcm2835_i2c_begin();

   // function which reads and prints what value eeprom address 0x0e has. 
   // See below the main.

   printf("Initial check\n");
   check(); // this time it prints a factory default value 0x5a.

   // To access eeprom, the command must start with 0x2X, where x determines the          
   // address, resulting 0x2e.
   buf[0] = 0x2e;

   // According to datasheet, I first have to clear the address before 
   // real write operation.
   buf[1] = 0x00;
   buf[2] = 0x00;
   why = bcm2835_i2c_write(buf,3);
   reason(why); // resolves and prints the reason code. This time it prints OK

   // according to datasheet, eeprom needs 5ms to make a write operation,
   // but I give it 2 seconds.       
   sleep(2); 

   // Then I check did the value in eeprom 0x0e change. IT DOESN'T!
   printf("Check after clear\n");       
   check();

   // Then I try to write a new address to the eeprom but since the clearing 
   // the register didn't work, this is very unlikely to work either.
   buf[0] = 0x2e;
   buf[1] = 0x4b;
   buf[2] = 0x00;
   why = bcm2835_i2c_write(buf,3);
   reason(why);
   sleep(2); 

   // The datasheet says that I have to reset the power supply and after that
   // the device should respond to the new slave address.
   // I do that by pluging off the jumper wires and reconnecting them 
   // after the program has finnished.
   bcm2835_i2c_end();
   return 0;
}

// The function I use to determine what the reason code was.
void reason(bcm2835I2CReasonCodes why)
{
   printf("Reason is: ");
   if(why == BCM2835_I2C_REASON_OK)
   {
      printf("OK");
   }else if(why == BCM2835_I2C_REASON_ERROR_NACK){
      printf("NACK");
   }else if(why == BCM2835_I2C_REASON_ERROR_CLKT){
      printf("Clock stretch");
   }else if(why == BCM2835_I2C_REASON_ERROR_DATA ){
      printf("Data error");
   }else{
      printf("Dunno lol");
   }
   printf("\n");
   return;
}

// Here I read eeprom 0x2e.
void check()
{
   unsigned char buf[6];
   unsigned char reg = 0x2e;
   bcm2835I2CReasonCodes why;
   // better safe than sorry with the buffer :)
   buf[0] = 0;
   buf[1] = 0;
   buf[2] = 0;
   why = bcm2835_i2c_write (&reg, 1);
   reason(why);
   why = bcm2835_i2c_read_register_rs(&reg,&buf[0],3);
   reason(why);
   printf("Buffer values are: %x ; %x ; %x \n", buf[0], buf[1], buf[2]);
}

プログラムの出力は次のとおりです。

Initial check
Reason is: OK
Reason is: OK
Buffer values are: 5a ; be ; dc
Reason is: OK
Check after clear
Reason is: OK
Reason is: OK
Buffer values are: 5a ; be ; dc
Reason is: OK

その後 i2cdetect -y 1 を実行すると、デバイスはテーブルに表示されませんが、0x00 または 0x5a から呼び出すプログラムに応答します。このようなプログラムを使用した後、i2cdetect はアドレス 0x5a からデバイスを正常に検出します。

本当の問題は、なぜ eeprom 0x0e を消去して書き換えることができないのかということだと思います。

Mlx90614 SMBus 通信の説明は以下にあります。最も関連性の高いページは IMO の 19 ページで、実際に私がやろうとしていることの疑似コードの例を示しています。 http://www.melexis.com/Assets/SMBus-communication-with-MLX90614-5207.aspx

mlx90614 のデータシートはこちら http://www.melexis.com/Assets/IR-sensor-thermometer-MLX90614-Datasheet-5152.aspx

bcm2835 のドキュメントはこちら www.airspayce.com/mikem/bcm2835/group__i2c.html

4

3 に答える 3

0

私のmlx90614sとまったく同じ問題に苦労しました。これを解決するために使用した書き込みルーチンを次に示します (ルーチンを呼び出す前に、bcm2835 ライブラリが適切に初期化されていることに注意してください)。

最初に、「正しい」スレーブアドレス、コマンド = 0x2E (EEPROMAccess | SMBusAddressReg)、およびデータ = 0x0000 (消去用) で書き込みルーチンを呼び出しました。「正しい」スレーブ アドレスは、0x00 または工場出荷時のデフォルト 0x5a (またはチップの真のアドレス) です。

消去後、同じ書き込みルーチンを使用しましたが、現在は data=0x005b で、工場出荷時のデフォルト 0x5a から 0x5b に変更し、パワーオフ リセット (POR) を実行し、i2cdetect を使用してデバイスが新しいアドレス (0x5b) で表示されました。

uint8_t memWriteI2C16(uint8_t SlaveAddress, uint8_t command, uint16_t data)
{    
unsigned char arr[5];
uint8_t status;

//Prepare for CRC8 calc
arr[0] = SlaveAddress<<1;        //NB! 7 bit address + a 0 write bit.      
arr[1] = command;                //Command byte in packet       
arr[2] = *((uint8_t *)(&data));  //Extract data low byte
arr[3] = *((uint8_t *)(&data)+1);//Extract data high byte
arr[4] = crc8(&arr[0],4)&0xFF;   //Calculate PEC by CRC8

bcm2835_i2c_setSlaveAddress(SlaveAddress);//Transmit address byte to I2C/SMBus
status = bcm2835_i2c_write (&arr[1], 4);  //Transmit Command,DataL, DataH and PEC       
bcm2835_delay(5);                         //Delay at least 5ms
return (status);
}

私が使用したCRC8ルーチンは次のとおりです。

// Return CRC-8 of the data, using x^8 + x^2 + x + 1 polynomial.  
// A table-based algorithm would be faster, but for only a few bytes 
// it isn't worth the code size. 
// Ref: https://chromium.googlesource.com/chromiumos/platform/vboot_reference/+/master/firmware/lib/crc8.c
uint8_t crc8(const void *vptr, int len)
{
 const uint8_t *data = vptr;
 unsigned crc = 0;
 int i, j;
 for (j = len; j; j--, data++) {
    crc ^= (*data << 8);
    for(i = 8; i; i--) {
        if (crc & 0x8000)
            crc ^= (0x1070 << 3);
        crc <<= 1;
    }
 }
 return (uint8_t)(crc >> 8);
}

さらに、mlx90614 のデータシートによると、電源投入後の工場出荷時のデフォルト状態は PWM 出力です。工場出荷時の PWM 状態の mlx90614 を RPi2 の I2C バスに接続すると、i2cdetect はバス上の何百もの I2C デバイスを報告します。bcm2835-library を使用して mlx90614 にアクセスしようとすると失敗します。必要なことは、SCL を少なくとも 2 ミリ秒の間 Low に保持することによって、mlx90614 を PWM 状態から強制的に解除することです。これが私がしたことです:

uint8_t mlx90614SMBusInit()
{
//Hold SCL low for at leat 2ms in order to force the mlx90614 into SMBus-mode
//Ref Melix app note regarding SMBus comm chapter 6.1 and table 5. 
uint8_t SCL1 = 3; //BCM2835 pin no 3 -RPi2 and RevB+. Use if i2cdetect -y 1
uint8_t SCL0 = 1; //BCM2835 pin no 1 -RPi2 and RevB+. Use if i2cdetect -y 0
uint8_t SCL;

SCL = SCL1; 
bcm2835_gpio_fsel(SCL, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_write(SCL ,LOW);
bcm2835_delay( 3); //Delay >2 ms
bcm2835_gpio_write(SCL ,HIGH); 
return (1);
}

ただし、これは次の電源投入までしか保持されません。したがって、mlx90614 の eeprom の pwmctrl レジスタに書き込む必要があります (pwm 出力を無効にし、SDA を OpenDrain に強制します)。前に説明したように command=0x22 (つまり、EEPROMAccess | PWMCTRLAddressRegister) で書き込みルーチンを使用し、pwmctrl レジスタの内容を消去した後、それに 0x0200 を書き込みました (私のデバイスでは最初の 3 ニブルは 020 でした...)。パワー オフ リセット (POR) とデバイスが SMBus モードで開始されました (I2C バスの妨害なし)。mlx90614 はトリッキーな小さなコンポーネントです...

于 2015-05-10T11:54:16.120 に答える
0

エラーバイトを追加する必要があります。説明については、この Web サイトをご覧ください: https://sf264.wordpress.com/2011/03/10/howto-mlx90614-und-pwm/

の CRC-8 を計算すると、00002e4b00が得られます0xa3

CRC-8 の計算には、この Web サイトを使用しました: http://smbus.org/faq/crc8Applet.htm

私はこれをテストしていませんが、これはうまくいくと思います:

buf[0] = 0x2e;
buf[1] = 0x4b;
buf[2] = 0x00;
buf[3] = 0xa3;
why = bcm2835_i2c_write(buf,4);
于 2015-05-03T11:39:28.393 に答える