-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathccs811_firmware_upgrade.py
67 lines (55 loc) · 1.7 KB
/
ccs811_firmware_upgrade.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Program to upgrade the firmware of the ccs811 sensor.
Based on ams application note AN000371: Downloading new Application
Firmware (v2)
"""
from machine import I2C, Pin
import utime
import config
import uio
import binascii
ADDRESS = 0x5A
i2c = I2C(scl=config.scl, sda=config.sda, freq=100000)
def firmware_upgrade(firmware_file):
"""Upgrade ccs811 firmware.
For example: firmware_upgrade('CCS811_SW000246_1-00.bin')
"""
reset = Pin(config.rst2)
wake = config.wake
# Pulse Reset pin
print("Enter boot mode...")
reset.value(0)
utime.sleep_ms(100)
reset.value(1)
utime.sleep_ms(100)
# Set Wake low
wake.value(0)
utime.sleep_ms(100)
# Erase application
print("Erase application...")
i2c.writeto_mem(ADDRESS, 0xf1, bytearray([0xe7, 0xa7, 0xe6, 0x09]))
utime.sleep_ms(500)
# Send firmware to ccs811
print("Load firmware...")
with uio.open(firmware_file, mode='rb') as firmware:
while True:
bytes = firmware.read(8)
if len(bytes) == 0:
break
payload = bytearray([0xf2])
for b in bytes:
payload.append(b)
i2c.writeto(ADDRESS, payload)
print(binascii.hexlify(payload))
# i2c.writeto_mem(ADDRESS, 0xf2, bytearray(bytes))
utime.sleep_ms(50)
# Verify application - this does not work, always prints 0
# Power off to complete
print("Verify...")
i2c.writeto_mem(ADDRESS, 0xf3, bytearray([0x01]))
utime.sleep_ms(500)
status = i2c.readfrom_mem(ADDRESS, 0x00, 1)
print(status[0])
print(status[0] & 0x30)
# Set Wake low
wake.value(1)
print("Done - Power device off and on again")