Skip to content

Commit

Permalink
inital commit
Browse files Browse the repository at this point in the history
  • Loading branch information
avsa242 committed Jan 26, 2023
0 parents commit a6f2d33
Show file tree
Hide file tree
Showing 5 changed files with 354 additions and 0 deletions.
17 changes: 17 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Copyright 2023 Jesse Burt

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# veml7700-spin
---------------

This is a P8X32A/Propeller, P2X8C4M64P/Propeller 2 driver object for the VEML7700 ALS/Lux sensor.

**IMPORTANT**: This software is meant to be used with the [spin-standard-library](https://github.com/avsa242/spin-standard-library) (P8X32A) or [p2-spin-standard-library](https://github.com/avsa242/p2-spin-standard-library) (P2X8C4M64P). Please install the applicable library first before attempting to use this code, otherwise you will be missing several files required to build the project.

## Salient Features

* I2C connection at ~28kHz (P1: bytecode I2C) or up to 400kHz (P1: native code I2C)

## Requirements

P1/SPIN1:
* spin-standard-library
* 1 additional core/cog for the PASM-based I2C engine (or none if the bytecode-based engine is used)

P2/SPIN2:
* ~~p2-spin-standard-library~~ _(not yet implemented)_

## Compiler Compatibility

| Processor | Language | Compiler | Backend | Status |
|-----------|----------|------------------------|-------------|-----------------------|
| P1 | SPIN1 | FlexSpin (5.9.25-beta) | Bytecode | OK |
| P1 | SPIN1 | FlexSpin (5.9.25-beta) | Native code | OK |
| P1 | SPIN1 | OpenSpin (1.00.81) | Bytecode | Untested (deprecated) |
| P2 | SPIN2 | FlexSpin (5.9.25-beta) | NuCode | Not yet implemented |
| P2 | SPIN2 | FlexSpin (5.9.25-beta) | Native code | Not yet implemented |
| P1 | SPIN1 | Brad's Spin Tool (any) | Bytecode | Unsupported |
| P1, P2 | SPIN1, 2 | Propeller Tool (any) | Bytecode | Unsupported |
| P1, P2 | SPIN1, 2 | PNut (any) | Bytecode | Unsupported |

## Limitations

* Very early in development - may malfunction, or outright fail to build
* TBD

74 changes: 74 additions & 0 deletions VEML7700-Demo.spin
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
--------------------------------------------
Filename: VEML7700-Demo.spin
Description: Demo of the VEML7700 driver
Author: Jesse Burt
Copyright (c) 2023
Started Jan 25, 2023
Updated Jan 26, 2023
See end of file for terms of use.
--------------------------------------------
}

CON

_clkmode = cfg#_clkmode
_xinfreq = cfg#_xinfreq

' -- User-defined constants
SER_BAUD = 115_200

SCL_PIN = 28
SDA_PIN = 29
I2C_FREQ = 400_000

' --

OBJ

cfg : "boardcfg.flip"
ser : "com.serial.terminal.ansi"
time : "time"
sensor: "sensor.light.veml7700"

PUB main{}

setup{}

repeat
ser.pos_xy(0, 3)
ser.hexs(sensor.als_data{}, 4)

PUB setup

ser.start(SER_BAUD)
time.msleep(30)
ser.clear
ser.strln(string("Serial terminal started"))

if (sensor.startx(SCL_PIN, SDA_PIN, I2C_FREQ))
ser.strln(string("VEML7700 driver started"))
else
ser.strln(string("VEML7700 driver failed to start - halting"))
repeat

sensor.powered(true)

DAT
{
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}

80 changes: 80 additions & 0 deletions core.con.veml7700.spin
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
--------------------------------------------
Filename: core.con.veml7700.spin
Description: VEML7700-specific constants
Author: Jesse Burt
Copyright (c) 2023
Started Jan 25, 2023
Updated Jan 26, 2023
See end of file for terms of use.
--------------------------------------------
}

CON

' I2C Configuration
I2C_MAX_FREQ = 400_000 ' device max I2C bus freq
SLAVE_ADDR = $10 << 1 ' 7-bit format slave address
T_POR = 1_000 ' startup time (usecs)


' Register definitions
ALS_CONF_0 = $00
ALS_CONF_0_MASK = $1BF3
ALS_GAIN = 11
ALS_GAIN_BITS = %11
ALS_GAIN_MASK = (ALS_GAIN_BITS << ALS_GAIN) & !ALS_CONF_0_MASK
ALS_IT = 6
ALS_IT_BITS = %1111
ALS_IT_MASK = (ALS_IT_BITS << ALS_IT) & !ALS_CONF_0_MASK
ALS_PERS = 4
ALS_PERS_BITS = %11
ALS_PERS_MASK = (ALS_PERS_BITS << ALS_PERS) & !ALS_CONF_0_MASK
ALS_INT_EN = 1
ALS_INT_EN_MASK = (1 << ALS_INT_EN) & !ALS_CONF_0_MASK
ALS_SD = 0
ALS_SD_MASK = 1 & !ALS_CONF_0_MASK

ALS_WH = $01

ALS_WL = $02

PWR_SAVING = $03
PWR_SAVING_MASK = $0007
PSM = 1
PSM_BITS = %11
PSM_MASK = (PSM_BITS << PSM) & !PWR_SAVING_MASK
PSM_EN = 0
PSM_EN_MASK = 1 & !PWR_SAVING_MASK

ALS = $04

WHITE = $05

ALS_INT = $06
ALS_INT_MASK = $C000
INT_TH_LOW = 15
INT_TH_HI = 14


PUB null{}
' This is not a top-level object

DAT
{
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}

145 changes: 145 additions & 0 deletions sensor.light.veml7700.spin
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
{
--------------------------------------------
Filename: sensor.light.veml7700.spin
Description: VEML7700 ALS/Lux sensor driver
Author: Jesse Burt
Copyright (c) 2023
Started Jan 25, 2023
Updated Jan 26, 2023
See end of file for terms of use.
--------------------------------------------
}

CON

SLAVE_WR = core#SLAVE_ADDR
SLAVE_RD = core#SLAVE_ADDR|1

DEF_SCL = 28
DEF_SDA = 29
DEF_HZ = 100_000
I2C_MAX_FREQ = core#I2C_MAX_FREQ

VAR


OBJ

{ decide: Bytecode I2C engine, or PASM? Default is PASM if BC isn't specified }
#ifdef VEML7700_I2C_BC
i2c : "com.i2c.nocog" ' BC I2C engine
#else
i2c : "com.i2c" ' PASM I2C engine
#endif
core: "core.con.veml7700.spin" ' hw-specific low-level const's
time: "time" ' basic timing functions

PUB null{}
' This is not a top-level object

PUB start{}: status
' Start using "standard" Propeller I2C pins and 100kHz
return startx(DEF_SCL, DEF_SDA, DEF_HZ)

PUB startx(SCL_PIN, SDA_PIN, I2C_HZ): status
' Start using custom IO pins and I2C bus frequency
if (lookdown(SCL_PIN: 0..31) and lookdown(SDA_PIN: 0..31))
if (status := i2c.init(SCL_PIN, SDA_PIN, I2C_HZ))
time.usleep(core#T_POR) ' wait for device startup
if (present{}) ' test device bus presence
return
' if this point is reached, something above failed
' Re-check I/O pin assignments, bus speed, connections, power
' Lastly - make sure you have at least one free core/cog
return FALSE

PUB stop{}
' Stop the driver
i2c.deinit{}

PUB defaults{}
' Set factory defaults

PUB preset_active{}
' Like default settings, but enable sensor power
powered(true)

PUB present{}: ack | tmp
' Check for device presence
i2c.start{}
tmp := i2c.write(SLAVE_WR)
i2c.stop{}
return (tmp == i2c.ACK)

PUB als_data{}: als_adc
' Read Ambient Light Sensor data
' Returns:
readreg(core#ALS, 2, @als_adc)

PUB powered(state): curr_state
' Enable sensor power
' Valid values: TRUE (-1 or 1), FALSE (0)
' Any other value polls the chip and returns the current setting
curr_state := 0
readreg(core#ALS_CONF_0, 2, @curr_state)
case ||(state)
0, 1:
{ ALS_SD is worded as a 'shut down' field, so 0 = power on, 1 = power off;
flip the bit here before writing it back to the sensor }
state := ((curr_state & core#ALS_SD_MASK) | ( (state ^ 1) & 1))
writereg(core#ALS_CONF_0, 2, @state)
other:
return ((curr_state & 1) == 1)

PUB white_data{}: white_adc
' Read Ambient Light Sensor data
' Returns:
readreg(core#WHITE, 2, @als_adc)

PRI readreg(reg_nr, nr_bytes, ptr_buff) | cmd_pkt
' Read nr_bytes from the device into ptr_buff
case reg_nr ' validate register num
$00..$06:
cmd_pkt.byte[0] := SLAVE_WR
cmd_pkt.byte[1] := reg_nr
i2c.start{}
i2c.wrblock_lsbf(@cmd_pkt, 2)
i2c.start{}
i2c.wr_byte(SLAVE_RD)
i2c.rdblock_lsbf(ptr_buff, nr_bytes, i2c#NAK)
i2c.stop{}
other: ' invalid reg_nr
return

PRI writereg(reg_nr, nr_bytes, ptr_buff) | cmd_pkt
' Write nr_bytes to the device from ptr_buff
case reg_nr
$00..$02:
cmd_pkt.byte[0] := SLAVE_WR
cmd_pkt.byte[1] := reg_nr
i2c.start{}
i2c.wrblock_lsbf(@cmd_pkt, 2)
i2c.wrblock_lsbf(ptr_buff, nr_bytes)
i2c.stop{}
other:
return


DAT
{
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}

0 comments on commit a6f2d33

Please sign in to comment.