2.3.6.8.3. I2C (HW api for 250-12 only)
2.3.6.8.3.1. Description
This 250-12 board example shows how to switch AC/DC modes over I2C using the SMBUS protocol.
2.3.6.8.3.2. Required hardware
Red Pitaya 250-12
2.3.6.8.3.3. Code - C
Note
Although the C code examples don’t require the use of the SCPI server, we have included them here to demonstrate how the same functionality can be achieved with different programming languages. Instructions on how to compile the code are here.
/* @brief This is a simple application for testing I2C communication on a RedPitaya
*
* (c) Red Pitaya http://www.redpitaya.com
*
* This part of code is written in C programming language.
* Please visit http://en.wikipedia.org/wiki/C_(programming_language)
* for more details on the language used herein.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "rp_hw.h"
int main(int argc, char *argv[]){
int res = rp_I2C_InitDevice("/dev/i2c-0",0x21); // Init i2c api.
printf("Init result: %d\n",res);
res = rp_I2C_setForceMode(true); // Set force mode.
printf("Set force mode: %d\n",res);
printf("Turn on AC/DC ch1 & ch2\n");
uint16_t value = 0x0055;
res = rp_I2C_SMBUS_WriteWord(0x02,value);
printf("Write 2 bytes: %d\n",res);
usleep(1000000);
value = value & ~ 0x000F;
res = rp_I2C_SMBUS_WriteWord(0x02,value);
printf("Write 2 bytes: %d\n",res);
usleep(3000000);
printf("Turn off AC/DC ch1 & ch2\n");
value = 0x00AA;
res = rp_I2C_SMBUS_WriteWord(0x02,value);
printf("Write 2 bytes: %d\n",res);
usleep(1000000);
value = value & ~ 0x000F;
res = rp_I2C_SMBUS_WriteWord(0x02,value);
printf("Write 2 bytes: %d\n",res);
uint16_t read_value = 0;
res = rp_I2C_SMBUS_ReadWord(0x02,&read_value);
printf("Read 2 bytes: 0x%x (res: %d)\n",read_value, res);
return 0;
}
2.3.6.8.3.4. Code - MATLAB®
%% Define Red Pitaya as TCP/IP object
IP = '192.168.178.56'; % Input IP of your Red Pitaya...
port = 5000;
RP = tcpclient(IP, port);
%% Open connection with your Red Pitaya
RP.ByteOrder = "big-endian";
configureTerminator(RP,"CR/LF");
% working with RP 250-12 v1.2. For RP version 1.1 need replace dev address to 32 (0x20)
writeline(RP,'I2C:DEV33 "/dev/i2c-0"');
writeline(RP,'I2C:FMODE ON'); % set force mode
fprintf('Turn on AC/DC ch1 & ch2\n');
value = 0x55;
writeline(RP, sprintf('I2C:S:W2 %d', value)); % write 2 bytes in i2c throw SMBUS
java.lang.Thread.sleep(1000);
value = value & ~ 0x0F;
writeline(RP, sprintf('I2C:S:W2 %d', value)); % write 2 bytes in i2c throw SMBUS
java.lang.Thread.sleep(3000);
fprintf('Turn off AC/DC ch1 & ch2\n');
value = 0xAA;
writeline(RP, sprintf('I2C:S:W2 %d', value)); % write 2 bytes in i2c throw SMBUS
java.lang.Thread.sleep(1000);
value = value & ~ 0x0F;
writeline(RP, sprintf('I2C:S:W2 %d', value)); % write 2 bytes in i2c throw SMBUS
java.lang.Thread.sleep(1000);
writeline(RP,'I2C:S:R2?'); % read 2 bytes from reg 0x02 throw SMBUS
value = str2num(writeread(RP,'I2C:S:R2?'));
fprintf('Reg 0x02: %x\n', value);
%% Close connection with Red Pitaya
clear RP;
2.3.6.8.3.5. Code - Python
#!/usr/bin/env python3
import sys
import time
from struct import *
import redpitaya_scpi as scpi
rp_s = scpi.scpi(sys.argv[1])
# working with RP 250-12 v1.2. For RP version 1.1 need replace dev address to 32
rp_s.tx_txt('I2C:DEV33 "/dev/i2c-0"')
print("Init I2C")
rp_s.tx_txt('I2C:FMODE ON')
print("Set force mode")
# Swich AC_DC for In 1
print("Turn on AC/DC ch1 & ch2")
value = 0x55;
rp_s.tx_txt('I2C:S:W2 ' + str(value)) # write to i2c
print("Write value for reg 0x2",value)
time.sleep(1)
value = (value & ~0x0F);
rp_s.tx_txt('I2C:S:W2 ' + str(value)) # write to i2c
print("Write value for reg 0x2",value)
time.sleep(3)
print("Turn off AC/DC ch1 & ch2")
value = 0xAA;
rp_s.tx_txt('I2C:S:W2 ' + str(value)) # write to i2c
print("Write value for reg 0x2",value)
time.sleep(1)
value = (value & ~0x0F);
rp_s.tx_txt('I2C:S:W2 ' + str(value)) # write to i2c
print("Write value for reg 0x2",value)
rp_s.tx_txt('I2C:S:R2?')
value = int(rp_s.rx_txt())
print("Read value for reg 0x2",value)
Note
The Python functions are accessible with the latest version of the redpitaya_scpi.py document available on our GitHub. The functions represent a quality-of-life improvement as they combine the SCPI commands in an optimal order. The code should function at approximately the same speed without them.
For further information on functions please consult the redpitaya_scpi.py code.