Skip to content

Instantly share code, notes, and snippets.

@circuit4u-medium
Created December 25, 2020 15:03
Show Gist options
  • Save circuit4u-medium/a9d8a757cdcf3d19f8ca06848af5c22d to your computer and use it in GitHub Desktop.
Save circuit4u-medium/a9d8a757cdcf3d19f8ca06848af5c22d to your computer and use it in GitHub Desktop.
FT232H and HT16K33
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Import libftd2xx"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
":dep libftd2xx"
]
},
{
"cell_type": "code",
"execution_count": null,
"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": 4,
"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: FTU85S8E, description: UM232H }\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": 5,
"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(&[0x11u8,0,0,byte_to_send,0x80,0,SCK,0x22,0,0x87]); //-ve single byte out; MSB 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",
" fn send_frame(&mut self, &byte_16: &[u8;16]){\n",
" self.start_i2c();\n",
" self.send_byte(0xe0);\n",
" self.send_byte(0x00);\n",
" for &x in byte_16.iter(){\n",
" self.send_byte(x);\n",
" }\n",
" self.stop_i2c(); \n",
" }\n",
"}\n",
"\n",
"impl I2C for Ftdi {}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Utility functions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Initial I2C"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"()"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ft.init_i2c()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### test address"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"30,true\n"
]
},
{
"data": {
"text/plain": [
"()"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ft.start_i2c();ft.send_byte(0xe0);ft.stop_i2c()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### init HT16K33"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"turn on internal oscillator"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"60,true\n",
"120,true\n"
]
},
{
"data": {
"text/plain": [
"()"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ft.start_i2c();ft.send_byte(0xe0);ft.send_byte(0x21);ft.stop_i2c()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"set display=on"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"240,true\n",
"224,true\n"
]
},
{
"data": {
"text/plain": [
"()"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ft.start_i2c();ft.send_byte(0xe0);ft.send_byte(0x81);ft.stop_i2c()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"set dimmer (duty cycle)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"192,true\n",
"128,true\n"
]
},
{
"data": {
"text/plain": [
"()"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ft.start_i2c();ft.send_byte(0xe0);ft.send_byte(0xe8);ft.stop_i2c()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Display digits"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"clear 8x8 screen"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0,true\n",
"0,true\n",
"0,true\n",
"0,true\n",
"0,true\n",
"0,true\n",
"0,true\n",
"0,true\n",
"0,true\n",
"0,true\n",
"0,true\n",
"0,true\n",
"0,true\n",
"0,true\n",
"0,true\n",
"0,true\n",
"0,true\n",
"0,true\n"
]
},
{
"data": {
"text/plain": [
"()"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ft.send_frame(&[0;16])"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0,true\n",
"0,true\n",
"0,true\n"
]
},
{
"data": {
"text/plain": [
"()"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ft.start_i2c();ft.send_byte(0xe0);ft.send_byte(0x00);ft.send_byte(0x70);ft.stop_i2c()"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0,true\n",
"0,true\n",
"0,true\n"
]
},
{
"data": {
"text/plain": [
"()"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ft.start_i2c();ft.send_byte(0xe0);ft.send_byte(0x01);ft.send_byte(0x01);ft.stop_i2c()"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0,true\n",
"0,true\n",
"0,true\n"
]
},
{
"data": {
"text/plain": [
"()"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ft.start_i2c();ft.send_byte(0xe0);ft.send_byte(0x01);ft.send_byte(0x24);ft.stop_i2c()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ft.start_i2c();ft.send_byte(0xe0);ft.send_byte(0x02);ft.send_byte(0xff);ft.stop_i2c()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ft.start_i2c();ft.send_byte(0xe0);ft.send_byte(0x0e);ft.send_byte(0xff);ft.stop_i2c()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fn dot_x_y(mut a:[u8;16],x:usize,y:usize,color:u8) -> [u8;16]{\n",
" //let mut a = [0;16];\n",
" if (color & 0x01)==0x01 {\n",
" a[2*y] |= (1<<x);\n",
" }\n",
" if (color & 0x02) == 0x02 {\n",
" a[2*y+1] |= (1<<x);\n",
" }\n",
" a\n",
"} "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"let a = dot_x_y([0;16],0,0,1);\n",
"let a = dot_x_y(a,7,0,2);\n",
"let a = dot_x_y(a,0,7,3);\n",
"let a = dot_x_y(a,7,7,2);\n",
"ft.send_frame(&a)"
]
},
{
"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