Skip to content

Instantly share code, notes, and snippets.

@circuit4u-medium
Created October 26, 2020 19:31
Show Gist options
  • Save circuit4u-medium/0eac25247274e66a9945338d2b272a31 to your computer and use it in GitHub Desktop.
Save circuit4u-medium/0eac25247274e66a9945338d2b272a31 to your computer and use it in GitHub Desktop.
FT232H with MAX31855 thermocouple read
Display the source blob
Display the rendered blob
Raw
{
"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};"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## open FTDI FT2232H with 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: FTY4S2TQ, description: UM232H }\n"
]
}
],
"source": [
"let mut ft = Ftdi::new()?;\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": [
"## MISO read"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"const MOSI:u8 = 0x02u8; //AD1\n",
"const SCK:u8 = 0x01u8; //AD0\n",
"const CS:u8 = 0x20u8; //AC5"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"ft.set_bit_mode(0, BitMode::Mpsse);\n",
"ft.write(&[0x8A, 0x86,11,0]); //2.5MHz SPI clk rate"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"read 4 bytes"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [],
"source": [
"ft.write(&[0x82,0, CS, 0x21,3,0,0x82, CS,CS, 0x87]); //cs low; read 4 bytes; cs high; send back\n",
"let mut buf:[u8;4] = [0;4];\n",
"ft.read(&mut buf);"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 240, 23, 64]"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"buf"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"format data"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"31.0"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t = if buf[0] & (1<<7) == 0 {((buf[0] as f64)*((1<<6) as f64) + ((buf[1]>>2) as f64))/4.0} else {((buf[0] as f64)*((1<<6) as f64) + ((buf[1]>>2) as f64)-32768.0)/4.0}"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"23.25"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t_amb = if buf[2] & (1<<7) == 0 {((buf[2] as f64)*((1<<4) as f64) + ((buf[3]>>4) as f64))/16.0} else {((buf[2] as f64)*((1<<4) as f64) + ((buf[3]>>4) as f64)-8192.0)/16.0}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"fault bit"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"buf[1] &0x01"
]
},
{
"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