Skip to content

Instantly share code, notes, and snippets.

@circuit4u-medium
Last active October 13, 2020 14:00
Show Gist options
  • Save circuit4u-medium/40c45d6fe0e51b64f0e6fe2219a14ab1 to your computer and use it in GitHub Desktop.
Save circuit4u-medium/40c45d6fe0e51b64f0e6fe2219a14ab1 to your computer and use it in GitHub Desktop.
FT2232 with SHT21
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Import libftd2xx"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
":dep libftd2xx"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"use libftd2xx::{Ftdi, FtdiCommon, BitMode};\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": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Device information: DeviceInfo { port_open: true, speed: None, device_type: FT2232H, vendor_id: 0x0403, product_id: 0x6010, serial_number: MLTS91A, description: FT2232H MiniModule A }\n"
]
}
],
"source": [
"//let mut ft = Ftdi::new()?;\n",
"\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": null,
"metadata": {},
"outputs": [],
"source": [
"ft.close()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## functions"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"const SCK:u8 = 0x01u8; //AD0 \n",
"const SDO:u8 = 0x02u8;//AD1\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",
" \n",
" fn read_sht21(&mut self) -> [u8;3]{\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,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;3] = [0;3];\n",
" self.read(&mut buf);\n",
" buf \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",
" fn read_t(&mut self)->f64{\n",
" self.start_i2c();\n",
" self.send_byte(0x80);\n",
" self.send_byte(0xf3);\n",
" self.stop_i2c(); \n",
" thread::sleep(Duration::from_millis(100));\n",
" self.start_i2c();\n",
" self.send_byte(0x81);\n",
" let mut r = self.read_sht21();\n",
" self.stop_i2c();\n",
" ((r[0] as f64)*256.0 + (r[1] as f64)) * 175.72/2f64.powi(16) -46.85\n",
" }\n",
" fn read_rh(&mut self)->f64{\n",
" self.start_i2c();\n",
" self.send_byte(0x80);\n",
" self.send_byte(0xf5);\n",
" self.stop_i2c(); \n",
" thread::sleep(Duration::from_millis(100));\n",
" self.start_i2c();\n",
" self.send_byte(0x81);\n",
" let mut r = self.read_sht21();\n",
" self.stop_i2c();\n",
" ((r[0] as f64)*256.0 + (r[1] as f64)) * 125.0/2f64.powi(16)-6.0\n",
" }\n",
" \n",
"}\n",
"impl I2C for Ftdi {}"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"()"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ft.init_i2c()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### measure temperature and RH"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"234\n",
"212\n",
"168\n"
]
},
{
"data": {
"text/plain": [
"22.6700830078125"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ft.read_t()"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"242\n",
"228\n",
"200\n"
]
},
{
"data": {
"text/plain": [
"28.381866455078125"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ft.read_rh()"
]
},
{
"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