1 DHT11
在Win10 Python中连续执行2次USB bulk传输,两次报文发送的token时间间隔大于100us。
_ __20-40us__ __80us__
| | | | |
|__18ms__| |_80us_| |__
'0'
_ __26-28us__
| | | |
|__50us__| |_50us_|
'1'
_ ___70us___
| | | |
|__50us__| |_50us_|
每次读5个字节,buf[0]是湿度字节,buf[2]是温度字节。
EP0BUF, EP2FIFOBUF, EP6FIFOBUF.
3.3V output.
sbit DQ = IOB ^ 0;
IFCONFIG &= ~0x3;
OEB |= 0x01;
2 Cypress EZ USB
2.1 Timer
标准C51单片机定时器时钟源是外接的12MHz晶振。而FX2LP定时器的时钟源是CLKOUT,CLKOUT的值与CPU的运行频率相等(参考fx2_trm),并且定时器只能对CLKOUT的频率进行4或者12分频。Four clocks per instruction cycle,这里的4个clock指的是外部24MHz晶振频率的1/4而不是CPU运行频率的1/4。
void isr_timer0(void) interrupt 1 using 1
{
TR0 = 0;
TH0 = 0;
TL0 = 0;
TR0 = 1;
timer0_tick++; // 16ms
if ((timer0_tick % 63) == 0)
{
DQ = !DQ;
if (EP2468STAT & bmEP6EMPTY)
{
EP6FIFOBUF[0] = DQ;
EP6BCH = 0;
SYNCDELAY;
EP6BCL = 1;
SYNCDELAY;
}
}
}
void tm_init(void)
{
DQ_OUTPUT;
DQ = 1;
TR0 = 0;
TF0 = 0;
CKCON |= 0x03;
TMOD &= ~0xF;
TMOD |= 0x1;
// CLKOUT = 48MHz
// CLKOUT / 12 = 4MHz = 0.25us
// 0.25us per step.
TH0 = 0;
TL0 = 0;
ET0 = 1;
TR0 = 1;
}
2.2 DVK
CY3684 EZ-USB FX2LP Development Kit (Rev. B) or
CY3684 EZ-USB FX2LP DVK Setup
firmware development: 4KB code size Keil v2
Keil uv2中找到如下的菜单,设置正确的BIN、INC和LIB的路径,否则编译失败。
[uv2][Project][Components, Environment and Books][Folders/Extensions]
自定义头文件搜索路径设置。
[uv2][Project][Options for Target][C51][Include Paths]
要确保Keil uv2编译无任何warning,否则放在RAM中的全局变量的值无论如何改动,都维持不变。
firmware download: Windows Applications\Application Source files\c_sharp\controlcenter\bin\Release\CyControl
[Program][FX2][Small EEPROM]
HEX文件下载到CY7C68013 RAM中,IIC文件下载到外部EEPROM中,命令hex2bix将HEX文件转换为IIC文件。
CY7C68013没有EEPROM时,运行bootROM代码执行USB下载程序,其VID是0x04B4,PID是0x8613,Win10驱动是cyusb3.inf;如果有EEPROM,并且其包含了用户固件,Win10驱动使用Zadig安装。
不需要写任何固件代码,CY7C68013在任何状态下都可以使用如下函数将固件下载到从0x0000开始的SRAM区域。从sourceforge下载hex2bin工具将hex文件转换成bin文件。
def ezusb_fw_dnl(d):
d.ctrl_transfer(
0x40,
0xA0, 0xE600, 0, '\x01')
offset = 0
try:
with open('bulkloop.bin',
'rb') as f:
while True:
bs = f.read(2048)
if not bs:
f.close()
break
d.ctrl_transfer(
0x40, 0xA0,
offset, 0, bs)
offset += len(bs)
except Exception as e:
print(e)
d.ctrl_transfer(
0x40,
0xA0, 0xE600, 0, '\x00')
2.3 Win10 pyusb的安装及使用
1)安装Python 3.8
安装时选择安装pip工具
2)pyusb
https://github.com/pyusb/pyusb
python -m pip install pyusb
或者
cd PATH_TO\PyUSB
python setup.py install
3)libusb
https://libusb.info/
打开压缩包,选择VS2015-x64\dll\libusb-1.0.dll,复制到C:\Windows\System32,System32 for both 32bit and 64bit applications。
选择同目录下的VS2015-x64\dll\libusb-1.0.lib,复制到C:\Python38\Lib。使用命令where python查找python在Win10的安装路径。
4)showcase
from threading import Thread
import os
# os.environ['PYUSB_DEBUG'] = 'debug'
import signal
import sys, time
import usb.core
import usb.util
thread_run = True
def bulk_task(d):
global thread_run
while (thread_run):
try:
data = d.read(0x86, 512,
2000)
print(bytes(data).hex())
except Exception as e:
pass
def dht11_read(d):
bmRequestType = \
usb.util.build_request_type(
usb.util.CTRL_IN,
usb.util.CTRL_TYPE_VENDOR,
usb.util.CTRL_RECIPIENT_INTERFACE)
data = d.ctrl_transfer(
bmRequestType,
0x59, 0, 0, 5)
hex_array = [hex(x) for x in data]
print(' '.join(hex_array))
# comment ENABLE_7_SEG_DISPLAY
# in TD_Poll, otherwise pyusb
# will print below error message.
# usb.core.USBTimeoutError:
# [Errno 10060] Operation timed out
def dht11_start(d):
d.write(0x2, '\x01', 1000)
def dht11_stop(d):
d.write(0x2, '\x00', 1000)
def signal_handler(a, b):
global thread_run
thread_run = False
sys.exit(0)
def main():
global thread_run
d = usb.core.find(idVendor = 0x1234,
idProduct = 0x1111)
if d is None:
raise ValueError('No device found')
#print(d)
signal.signal(
signal.SIGINT,
signal_handler)
thread = Thread(
target = bulk_task,
args = (d,))
thread.start()
while (True):
print('1 start')
print('2 stop')
print('3 rd')
print('4 exit')
num = input('make your choice: ')
try:
if (num == '1'):
dht11_start(d)
elif (num == '2'):
dht11_stop(d)
elif (num == '3'):
dht11_read(d)
elif (num == '4'):
thread_run = False
sys.exit(0)
except Exception as e:
print(e)
if __name__ == '__main__':
main()
3 Host MP3
MVSILICON 2006, Rohm 2007, Sunplus 2006.
Chery QQ: MB95F16BJA, BU72432KV
Toshiba USB host T5CJ3, used in CD player.
CY7C68013固件开发
于 2023-09-13 09:30:07 首次发布