Skip to content

Instantly share code, notes, and snippets.

@circuit4u-medium
Last active April 21, 2021 15:52
Show Gist options
  • Save circuit4u-medium/9dd247817394a688a41fac3a10b252dc to your computer and use it in GitHub Desktop.
Save circuit4u-medium/9dd247817394a688a41fac3a10b252dc to your computer and use it in GitHub Desktop.
FT232H with AD5933
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Import libftd2xx"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
":dep libftd2xx"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"use libftd2xx::{Ftdi, FtdiCommon, BitMode, FtdiMpsse};\n",
"use std::time::Duration;\n",
"use std::thread;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## open FTDI FT2232H with serial number"
]
},
{
"cell_type": "code",
"execution_count": 130,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Device information: DeviceInfo { port_open: true, speed: None, device_type: FT232H, vendor_id: 0x0403, product_id: 0x6014, serial_number: AD5933, description: USB <-> Serial Converter }\n"
]
}
],
"source": [
"let mut ft = Ftdi::new()?;\n",
"//let mut ft = Ftdi::with_serial_number(\"MLTS91A\")?; //use FTDI \n",
"let info = ft.device_info()?;\n",
"println!(\"Device information: {:?}\", info);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### **defered**"
]
},
{
"cell_type": "code",
"execution_count": 150,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Ok(())"
]
},
"execution_count": 150,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ft.close()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## functions"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"const SCK:u8 = 0x01u8; //AD0 \n",
"const SDO:u8 = 0x02u8;//AD1\n",
"\n",
"\n",
"pub trait I2C: FtdiCommon {\n",
" fn init_i2c(&mut self){\n",
" self.set_bit_mode(0, BitMode::Reset);\n",
" self.set_bit_mode(0, BitMode::Mpsse);\n",
" self.write(&[0x8A, 0x97, 0x8C, 0x86,200,0,0x85]); //200 for 100k I2C bit rate \n",
" self.write(&[0x80,SCK+SDO,SCK+SDO]);//both high\n",
" }\n",
" \n",
" fn start_i2c(&mut self){\n",
" self.write(&[0x80,SCK+SDO,SCK+SDO]);\n",
" self.write(&[0x80,SCK,SCK+SDO]); //SDA falling edge\n",
" self.write(&[0x80,0,SCK+SDO]); //zero both\n",
" }\n",
" \n",
" fn stop_i2c(&mut self){ \n",
" self.write(&[0x80,0,SCK+SDO]); //zero both\n",
" self.write(&[0x80,SCK,SCK+SDO]); //sck up first\n",
" self.write(&[0x80,SCK+SDO,SCK+SDO]); //SDA rising edge\n",
" self.write(&[0x80,0,0]); //release bus\n",
" }\n",
" \n",
" fn cmd_write_reg(&mut self, addr:u8, reg: &[u8]){\n",
" //set addr. pointer\n",
" self.start_i2c();\n",
" self.send_byte(0x1a); //0001_101_0 write\n",
" self.send_byte(0xb0); //pointer cmd\n",
" self.send_byte(addr); \n",
" self.stop_i2c();\n",
" //block write n-byte\n",
" self.start_i2c();\n",
" self.send_byte(0x1a); //0001_101_0 write\n",
" self.send_byte(0xa0); //block write\n",
" self.send_byte(reg.len() as u8);//n-byte \n",
" for &x in reg {\n",
" self.send_byte(x); \n",
" } \n",
" self.stop_i2c(); \n",
" }\n",
" fn cmd_write_byte(&mut self, addr:u8, b:u8){\n",
" self.start_i2c();\n",
" self.send_byte(0x1a); //0001_101_0 write\n",
" self.send_byte(addr); //reg addr\n",
" self.send_byte(b); //reset\n",
" self.stop_i2c();\n",
" }\n",
" \n",
" fn cmd_f1(&mut self, freq:f64, amp:u8, gain:u8){\n",
" self.cmd_write_reg(0x80,&[0xb0]);//standby\n",
" let p = ((freq/16e6*4.0)*134217728.0) as u32;\n",
" self.cmd_write_reg(0x82,&[((p>>16) & 0xff) as u8, ((p>>8) & 0xff) as u8, (p & 0xff) as u8]);//start freq.\n",
" self.cmd_write_reg(0x80, &[0x10+amp*2+gain]); //start exercising at f1\n",
" } \n",
" fn cmd_f_step(&mut self, freq:f64){\n",
" let p = ((freq/16e6*4.0)*134217728.0) as u32;\n",
" self.cmd_write_reg(0x85,&[((p>>16) & 0xff) as u8, ((p>>8) & 0xff) as u8, (p & 0xff) as u8]);//freq. increment\n",
" }\n",
" \n",
" fn cmd_reset(&mut self){ \n",
" self.cmd_write_byte(0x81,0x10); \n",
" }\n",
" \n",
" fn cmd_f_op(&mut self, op:u8, amp :u8, gain :u8){\n",
" self.cmd_write_byte(0x80, op + amp*2 + gain); \n",
" }\n",
" \n",
" fn cmd_temp(&mut self)-> f64{\n",
" self.cmd_write_byte(0x80,0x90); // measure temp.\n",
" thread::sleep(Duration::from_millis(10));\n",
" //set read addr. pointer \n",
" let buf = self.cmd_read_reg(0x92); \n",
" let t = if buf[0] & (1<<5) == 0 {((buf[0] as f64)*((1<<8) as f64) + (buf[1] as f64))/32.0} else {((buf[0] as f64)*((1<<8) as f64) + (buf[1] as f64)-16384.0)/32.0};\n",
" t\n",
" }\n",
" fn cmd_z(&mut self) -> f64{\n",
" let a: [u8;2] = self.cmd_read_reg(0x94);\n",
" let b: [u8;2] = self.cmd_read_reg(0x96); \n",
" let x:i32;\n",
" let y:i32;\n",
" \n",
" \n",
" if a[0] & (1<<7) == 0 {\n",
" x = (((a[0] as u32)<<8) + (a[1] as u32)) as i32;\n",
" }else{\n",
" x = 65536 - ((((a[0] as u32)<<8) + (a[1] as u32)) as i32);\n",
" }\n",
" \n",
" if b[0] & (1<<7) == 0 {\n",
" y = (((b[0] as u32)<<8) + (b[1] as u32)) as i32;\n",
" }\n",
" else{\n",
" y = 65536 - ((((b[0] as u32)<<8) + (b[1] as u32)) as i32); \n",
" }\n",
" println!(\"r={},{},{};i={},{},{}\", a[0],a[1],x, b[0],b[1],y);\n",
" 1.0/((x as f64).powi(2) + (y as f64).powi(2)).sqrt()\n",
" }\n",
" \n",
" fn read_2byte(&mut self) -> [u8;2]{\n",
" self.write(&[0x80,0,SCK,0x20,0,0,0x80,SDO,SDO+SCK, 0x13,0,0]); // ftdi ack \n",
" self.write(&[0x80,0,SCK,0x20,0,0,0x80,SDO,SDO+SCK, 0x13,0,0xff,0x80,0,SCK,0x87]); //ftdi nack \n",
" let mut buf:[u8;2] = [0;2];\n",
" self.read(&mut buf);\n",
" buf \n",
" }\n",
" fn read_byte(&mut self) -> [u8;1]{ \n",
" self.write(&[0x80,0,SCK,0x20,0,0,0x80,SDO,SDO+SCK, 0x13,0,0xff,0x80,0,SCK,0x87]); //ftdi nack \n",
" let mut buf:[u8;1] = [0;1];\n",
" self.read(&mut buf);\n",
" buf \n",
" }\n",
" \n",
"\n",
" \n",
" fn cmd_read_reg(&mut self, addr:u8)->[u8;2]{\n",
" //set addr. pointer\n",
" self.start_i2c();\n",
" self.send_byte(0x1a); //0001_101_0 write\n",
" self.send_byte(0xb0); //pointer cmd\n",
" self.send_byte(addr); \n",
" self.stop_i2c();\n",
" //block read 2-byte\n",
" self.start_i2c();\n",
" self.send_byte(0x1a); //0001_101_0 write\n",
" self.send_byte(0xa1); //block read\n",
" self.send_byte(0x02);//2-byte\n",
" self.start_i2c();\n",
" self.send_byte(0x1b); //0001_101_1\n",
" let mut buf = self.read_2byte();\n",
" self.stop_i2c(); \n",
" buf\n",
" }\n",
" \n",
"// \n",
" \n",
" \n",
" fn read_status(&mut self)->[u8;1] {\n",
" self.start_i2c();\n",
" self.send_byte(0x1a); //0001_101_0 write \n",
" self.send_byte(0xb0); //pointer cmd\n",
" self.send_byte(0x8f); //temp data\n",
" self.stop_i2c();\n",
" //block read 2-byte\n",
" self.start_i2c(); \n",
" self.send_byte(0x1b); //0001_101_1\n",
" let mut r = self.read_byte();\n",
" self.stop_i2c();\n",
" r\n",
" }\n",
" \n",
" fn send_byte(&mut self, byte_to_send: u8) -> bool { \n",
" self.write(&[0x11u8, 0,0, byte_to_send,0x80,0,SCK,0x22,0,0x87]); //-ve byte out \n",
" let mut buf:[u8;1] = [0;1];\n",
" self.read(&mut buf);\n",
" self.write(&vec![0x80,SDO, SDO+SCK]);\n",
" //println!(\"{}\",buf[0]);\n",
" if buf[0] & 1u8 != 0u8{\n",
" return false\n",
" }else{\n",
" return true\n",
" }\n",
" } \n",
" \n",
" fn cal(&mut self, amp:u8, gain:u8) -> [f64;5] {\n",
" self.cmd_f1(10.0e3,amp,gain);\n",
" self.cmd_f_op(0x20, amp,gain); //start \n",
" let mut cal: [f64; 5] = [0.0; 5];\n",
" for i in 0..5{\n",
" cal[i] = self.cmd_z();\n",
" println!(\"{}\",cal[i]);\n",
" self.cmd_f_op(0x30,amp,gain); //step \n",
" self.cmd_f_op(0x40,amp,gain); //repeat\n",
" } \n",
" self.cmd_reset();\n",
" cal\n",
" }\n",
" \n",
" fn mea(&mut self, amp:u8, gain:u8, cal:[f64;5]){\n",
" self.cmd_f1(10.0e3,amp,gain);\n",
" self.cmd_f_op(0x20, amp,gain); //start \n",
" for i in 0..5{\n",
" println!(\"{}\",330.0*self.cmd_z()/cal[i]); \n",
" self.cmd_f_op(0x30,amp,gain) //step \n",
" //self.cmd_f_op(0x40,amp,gain); //repeat\n",
" } \n",
" self.cmd_reset(); \n",
" }\n",
" \n",
" \n",
" \n",
"}\n",
"impl I2C for Ftdi {}"
]
},
{
"cell_type": "code",
"execution_count": 131,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"()"
]
},
"execution_count": 131,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ft.init_i2c()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### measure temperature"
]
},
{
"cell_type": "code",
"execution_count": 132,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"22.75"
]
},
"execution_count": 132,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ft.cmd_temp()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### configure frequency steps"
]
},
{
"cell_type": "code",
"execution_count": 133,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"()"
]
},
"execution_count": 133,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ft.cmd_write_reg(0x88,&[0,1])"
]
},
{
"cell_type": "code",
"execution_count": 134,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"()"
]
},
"execution_count": 134,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ft.cmd_write_reg(0x8a,&[0,25])"
]
},
{
"cell_type": "code",
"execution_count": 135,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"()"
]
},
"execution_count": 135,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ft.cmd_f1(2e3,2,0)"
]
},
{
"cell_type": "code",
"execution_count": 145,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"()"
]
},
"execution_count": 145,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ft.cmd_f_op(0x40,3,0)"
]
},
{
"cell_type": "code",
"execution_count": 138,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"()"
]
},
"execution_count": 138,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ft.cmd_f_op(0x20,3,0)"
]
},
{
"cell_type": "code",
"execution_count": 144,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"r=254,140,372;i=241,4,3836\n"
]
},
{
"data": {
"text/plain": [
"0.00025947099425345337"
]
},
"execution_count": 144,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ft.cmd_z()"
]
},
{
"cell_type": "code",
"execution_count": 146,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"r=253,32,736;i=202,13,13811\n"
]
},
{
"data": {
"text/plain": [
"0.0000723034580884913"
]
},
"execution_count": 146,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ft.cmd_z()"
]
},
{
"cell_type": "code",
"execution_count": 149,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1191.6666666666665"
]
},
"execution_count": 149,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"0.00026/0.000072*330.0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### take measurements"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1k cal. 400mVp-p"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"r=255,29,227;i=254,221,291\n",
"0.002709539605694229\n",
"r=0,38,38;i=254,227,285\n",
"0.0034779926339741567\n",
"r=0,45,45;i=254,226,286\n",
"0.0034540099247042043\n",
"r=0,48,48;i=254,227,285\n",
"0.003460041907589366\n",
"r=0,68,68;i=254,235,277\n",
"0.0035060105097941326\n"
]
}
],
"source": [
"let cc = ft.cal(3,1);"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"65535"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"let a:[u8;2] = [0xff,0xff];\n",
"((a[0] as u32)<<8) + (a[1] as u32)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"make measurements"
]
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"r=0,76,76;i=254,221,291\n",
"404.94621703953277\n",
"r=237,45,4819;i=254,225,287\n",
"19.65438762936756\n",
"r=0,61,61;i=254,228,284\n",
"328.91093983472297\n",
"r=0,68,68;i=254,236,276\n",
"335.52655713751557\n",
"r=0,68,68;i=254,236,276\n",
"331.1273441884662\n"
]
}
],
"source": [
"ft.mea(3,1,cc);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"@webio": {
"lastCommId": null,
"lastKernelId": null
},
"kernelspec": {
"display_name": "Rust",
"language": "rust",
"name": "rust"
},
"language_info": {
"codemirror_mode": "rust",
"file_extension": ".rs",
"mimetype": "text/rust",
"name": "Rust",
"pygment_lexer": "rust",
"version": ""
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment