Created
October 11, 2020 12:39
-
-
Save circuit4u-medium/f2abec51b2a5e9a4f746fd59c79549f8 to your computer and use it in GitHub Desktop.
FT232H and TM1637
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Import libftd2xx" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
":dep libftd2xx" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"use libftd2xx::{Ftdi, FtdiCommon, BitMode};\n", | |
"use std::time::Duration;" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## open FTDI FT223H device (serial number)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"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", | |
"//let mut ft = Ftdi::with_serial_number(\"MLTS91A\")?; //use serial #\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": [ | |
"## I2C functions" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"const SCK:u8 = 0x01u8; //AD0 of FT232H\n", | |
"const SDO:u8 = 0x02u8; //AD1 of FT232H\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, 0x8C, 0x86,200,0]); //60MHz; 3-phase clock; set div=200 for 100k I2C clk rate \n", | |
" self.write(&[0x80,SCK+SDO,SCK+SDO]);//set both output high\n", | |
" }\n", | |
" \n", | |
" fn start_i2c(&mut self){ \n", | |
" self.write(&[0x80,SCK+SDO,SCK+SDO]);//set both output high\n", | |
" self.write(&[0x80,SCK,SCK+SDO]); //SDA falling edge\n", | |
" self.write(&[0x80,0,SCK+SDO]); //drop SCK as well\n", | |
" }\n", | |
" \n", | |
" fn stop_i2c(&mut self){ \n", | |
" self.write(&[0x80,0,SCK+SDO]); //set both output low\n", | |
" self.write(&[0x80,SCK,SCK+SDO]); //SCK high first\n", | |
" self.write(&[0x80,SCK+SDO,SCK+SDO]); //SDA rising edge\n", | |
" self.write(&[0x80,0,0]); //set both input; release bus\n", | |
" }\n", | |
" \n", | |
" \n", | |
" fn send_byte(&mut self, byte_to_send: u8) -> bool { \n", | |
" \n", | |
" self.write(&[0x19u8,0,0,byte_to_send,0x80,0,SCK,0x22,0,0x87]); //-ve single byte out; LSB first; then release SDA; read ACK \n", | |
" \n", | |
" let mut buf = vec![0;1];\n", | |
" self.read(&mut buf); \n", | |
" self.write(&[0x80,SDO, SDO+SCK]);//set SCK low; SDA high\n", | |
" \n", | |
" if (buf[0] & 1u8) != 0u8{\n", | |
" println!(\"{},{}\",buf[0],false);\n", | |
" return false\n", | |
" }else{\n", | |
" println!(\"{},{}\",buf[0],true);\n", | |
" return true\n", | |
" }\n", | |
" } \n", | |
"}\n", | |
"\n", | |
"impl I2C for Ftdi {}" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Initial I2C" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"()" | |
] | |
}, | |
"execution_count": 27, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ft.init_i2c()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Turn TM1637 display ON/OFF" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"()" | |
] | |
}, | |
"execution_count": 37, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ft.start_i2c();ft.send_byte(0x8f);ft.stop_i2c()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"()" | |
] | |
}, | |
"execution_count": 34, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ft.start_i2c();ft.send_byte(0x80);ft.stop_i2c()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Display digits" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 49, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"const digits:[u8;10] = [0x3f,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F];" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"enter data mode" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 35, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"()" | |
] | |
}, | |
"execution_count": 35, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ft.start_i2c();ft.send_byte(0x40);ft.stop_i2c()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 52, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0\n", | |
"0\n", | |
"0\n", | |
"0\n", | |
"0\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"()" | |
] | |
}, | |
"execution_count": 52, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ft.start_i2c();ft.send_byte(0xc0);ft.send_byte(digits[3]);ft.send_byte(digits[1]);ft.send_byte(digits[4]);ft.send_byte(digits[1]);ft.stop_i2c()" | |
] | |
}, | |
{ | |
"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