Commit 961c1c72 authored by devsaurus's avatar devsaurus
Browse files

Add ow module.

parent a3dc13e3
...@@ -97,6 +97,12 @@ config LUA_MODULE_NODE ...@@ -97,6 +97,12 @@ config LUA_MODULE_NODE
help help
Includes the node module (recommended). Includes the node module (recommended).
config LUA_MODULE_OW
bool "1-Wire module"
default "y"
help
Includes the 1-Wire (ow) module (recommended).
config LUA_MODULE_SIGMA_DELTA config LUA_MODULE_SIGMA_DELTA
bool "Sigma-Delta module" bool "Sigma-Delta module"
default "n" default "n"
......
// Module for interfacing with the OneWire interface
#include "module.h"
#include "lauxlib.h"
#include <stdint.h>
#include "platform.h"
static int ow_bus_table_ref;
// Lua: ow.setup( pin )
static int ow_setup( lua_State *L )
{
int pin = luaL_checkint( L, 1 );
luaL_argcheck( L, pin >= 0, 1, "invalid pin" );
if (platform_onewire_init( pin ) != PLATFORM_OK)
luaL_error( L, "failed" );
return 0;
}
// Lua: r = ow.reset( pin )
static int ow_reset( lua_State *L )
{
int pin = luaL_checkint( L, 1 );
luaL_argcheck( L, pin >= 0, 1, "invalid pin" );
uint8_t presence;
if (platform_onewire_reset( pin, &presence ) != PLATFORM_OK)
luaL_error( L, "failed" );
lua_pushinteger( L, presence );
return 1;
}
// Lua: ow.write( pin, v, power)
static int ow_write( lua_State *L )
{
int stack = 0;
int pin = luaL_checkint( L, ++stack );
luaL_argcheck( L, pin >= 0, stack, "invalid pin" );
int data = luaL_checkint( L, ++stack );
luaL_argcheck( L, data >= 0 && data < 256, stack, "invalid data" );
int power = luaL_optint( L, ++stack, 0 ) != 0 ? 1 : 0;
uint8_t d = data;
if (platform_onewire_write_bytes( pin, &d, 1, power ) != PLATFORM_OK)
luaL_error( L, "failed" );
return 0;
}
// Lua: ow.write_bytes( pin, buf, power)
static int ow_write_bytes( lua_State *L )
{
int stack = 0;
size_t datalen;
int pin = luaL_checkint( L, ++stack );
luaL_argcheck( L, pin >= 0, stack, "invalid pin" );
const char *pdata = luaL_checklstring( L, ++stack, &datalen );
int power = luaL_optint( L, ++stack, 0 ) != 0 ? 1 : 0;
platform_onewire_write_bytes( pin, (uint8_t *)pdata, datalen, power);
return 0;
}
// Lua: r = ow.read( pin )
static int ow_read( lua_State *L )
{
int pin = luaL_checkint( L, 1 );
luaL_argcheck( L, pin >= 0, 1, "invalid pin" );
uint8_t data;
if (platform_onewire_read_bytes( pin, &data, 1 ) != PLATFORM_OK)
luaL_error( L, "failed" );
lua_pushinteger( L, data );
return 1;
}
// Lua: r = ow.read_bytes( pin, size )
static int ow_read_bytes( lua_State *L )
{
int stack = 0;
int pin = luaL_checkint( L, ++stack );
luaL_argcheck( L, pin >= 0, stack, "invalid pin" );
int size = luaL_checkint( L, ++stack );
if (size == 0)
return 0;
luaL_argcheck( L, size <= LUAL_BUFFERSIZE, stack, "Attempt to read too many characters" );
luaL_Buffer b;
luaL_buffinit( L, &b );
char *p = luaL_prepbuffer( &b );
if (platform_onewire_read_bytes( pin, (uint8_t *)p, size ) != PLATFORM_OK)
luaL_error( L, "failed" );
luaL_addsize( &b, size );
luaL_pushresult( &b );
return 1;
}
// Lua: ow.select( pin, buf[8] )
static int ow_select( lua_State *L )
{
int stack = 0;
uint8_t rom[1+8];
size_t datalen;
int numdata, i;
const char *pdata;
int pin = luaL_checkint( L, ++stack );
luaL_argcheck( L, pin >= 0, stack, "invalid pin" );
if( lua_istable( L, ++stack ) )
{
datalen = lua_objlen( L, stack );
luaL_argcheck( L, datalen == 8, stack, "wrong arg range" );
for (i = 0; i < datalen; i ++) {
lua_rawgeti( L, stack, i + 1 );
numdata = ( int )luaL_checkinteger( L, -1 );
lua_pop( L, 1 );
if (numdata > 255)
return luaL_error( L, "wrong arg range" );
rom[1+i] = (uint8_t)numdata;
}
} else {
pdata = luaL_checklstring( L, stack, &datalen );
luaL_argcheck( L, datalen == 8, stack, "wrong arg range" );
for (i = 0; i < datalen; i++) {
rom[1+i] = pdata[i];
}
}
rom[0] = 0x55; // select command
if (platform_onewire_write_bytes( pin, rom, 1+8, 0 ) != PLATFORM_OK)
luaL_error( L, "write failed" );
return 0;
}
// Lua: ow.skip( pin )
static int ow_skip( lua_State *L )
{
int pin = luaL_checkint( L, 1 );
luaL_argcheck( L, pin >= 0, 1, "invalid pin" );
const uint8_t cmd = 0xCC; // skip command
if (platform_onewire_write_bytes( pin, &cmd, 1, 0 ) != PLATFORM_OK)
luaL_error( L, "write failed" );
return 0;
}
// Lua: ow.depower( pin )
static int ow_depower( lua_State *L )
{
int pin = luaL_checkint( L, 1 );
luaL_argcheck( L, pin >= 0, 1, "invalid pin" );
platform_onewire_depower( pin );
return 0;
}
// Clear the search state so that if will start from the beginning again.
// Lua: ow.reset_search( pin )
static int ow_reset_search( lua_State *L )
{
int pin = luaL_checkint( L, 1 );
luaL_argcheck( L, pin >= 0, 1, "invalid pin" );
// set up new userdata for this pin
lua_rawgeti( L, LUA_REGISTRYINDEX, ow_bus_table_ref );
platform_onewire_bus_t *bus = (platform_onewire_bus_t *)lua_newuserdata( L, sizeof( platform_onewire_bus_t ));
lua_rawseti( L, -2, pin );
// remove table from stack
lua_pop( L, 1 );
platform_onewire_reset_search( bus );
return 0;
}
// Setup the search to find the device type 'family_code' on the next call
// to search(*newAddr) if it is present.
// Lua: ow.target_search( pin, family_code )
static int ow_target_search( lua_State *L )
{
int stack = 0;
int pin = luaL_checkint( L, ++stack );
luaL_argcheck( L, pin >= 0, stack, "invalid pin" );
int code = (int)luaL_checkinteger( L, ++stack );
luaL_argcheck( L, code >= 0 && code < 256, stack, "wrong arg range" );
// set up new userdata for this pin
lua_rawgeti( L, LUA_REGISTRYINDEX, ow_bus_table_ref );
platform_onewire_bus_t *bus = (platform_onewire_bus_t *)lua_newuserdata( L, sizeof( platform_onewire_bus_t ));
lua_rawseti( L, -2, pin );
// remove table from stack
lua_pop( L, 1 );
platform_onewire_target_search( (uint8_t)code, bus );
return 0;
}
// Look for the next device. Returns 1 if a new address has been
// returned. A zero might mean that the bus is shorted, there are
// no devices, or you have already retrieved all of them. It
// might be a good idea to check the CRC to make sure you didn't
// get garbage. The order is deterministic. You will always get
// the same devices in the same order.
// Lua: r = ow.search( pin )
static int ow_search( lua_State *L )
{
int pin = luaL_checkint( L, 1 );
luaL_argcheck( L, pin >= 0, 1, "invalid pin" );
// look-up bus userdata for this pin
lua_rawgeti( L, LUA_REGISTRYINDEX, ow_bus_table_ref );
lua_rawgeti( L, -1, pin );
platform_onewire_bus_t *bus = (platform_onewire_bus_t *)lua_touserdata( L, -1 );
if (!bus)
return luaL_error( L, "search not initialized" );
// removed temps from stack
lua_pop( L, 2 );
luaL_Buffer b;
luaL_buffinit( L, &b );
char *p = luaL_prepbuffer( &b );
if (platform_onewire_search( pin, (uint8_t *)p, bus )){
luaL_addsize( &b, 8 );
luaL_pushresult( &b );
} else {
luaL_pushresult( &b ); /* close buffer */
lua_pop( L, 1 );
// remove bus userdata from table
lua_rawgeti( L, LUA_REGISTRYINDEX, ow_bus_table_ref );
lua_pushnil( L );
lua_rawseti( L, -2, pin );
// remove table from stack
lua_pop( L, 1 );
lua_pushnil( L );
}
return 1;
}
// uint8_t onewire_crc8(const uint8_t *addr, uint8_t len);
// Lua: r = ow.crc8( buf )
static int ow_crc8( lua_State *L )
{
size_t datalen;
const char *pdata = luaL_checklstring( L, 1, &datalen );
if(datalen > 255)
return luaL_error( L, "wrong arg range" );
lua_pushinteger( L, platform_onewire_crc8((uint8_t *)pdata, datalen) );
return 1;
}
// bool onewire_check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc);
// Lua: b = ow.check_crc16( buf, inverted_crc0, inverted_crc1, crc )
static int ow_check_crc16( lua_State *L )
{
size_t datalen;
uint8_t inverted_crc[2];
const char *pdata = luaL_checklstring( L, 1, &datalen );
if(datalen > 65535)
return luaL_error( L, "wrong arg range" );
int crc = 0;
crc = luaL_checkinteger( L, 2 );
if(datalen > 255)
return luaL_error( L, "wrong arg range" );
inverted_crc[0] = (uint8_t)crc;
crc = luaL_checkinteger( L, 3 );
if(datalen > 255)
return luaL_error( L, "wrong arg range" );
inverted_crc[1] = (uint8_t)crc;
crc = 0;
if(lua_isnumber(L, 4))
crc = lua_tointeger(L, 4);
if(crc > 65535)
return luaL_error( L, "wrong arg range" );
lua_pushboolean( L, platform_onewire_check_crc16((uint8_t *)pdata, datalen, inverted_crc, crc) );
return 1;
}
// uint16_t onewire_crc16(const uint8_t* input, uint16_t len, uint16_t crc);
// Lua: r = ow.crc16( buf, crc )
static int ow_crc16( lua_State *L )
{
int stack = 0;
size_t datalen;
const char *pdata = luaL_checklstring( L, ++stack, &datalen );
luaL_argcheck( L, datalen <= 65535, stack, "wrong arg range" );
int crc = 0;
if(lua_isnumber(L, ++stack))
crc = lua_tointeger(L, stack);
luaL_argcheck( L, crc >= 0 && crc <= 65535, stack, "wrong arg range" );
lua_pushinteger( L, platform_onewire_crc16((uint8_t *)pdata, datalen, crc) );
return 1;
}
static const LUA_REG_TYPE ow_map[] = {
{ LSTRKEY( "setup" ), LFUNCVAL( ow_setup ) },
{ LSTRKEY( "reset" ), LFUNCVAL( ow_reset ) },
{ LSTRKEY( "skip" ), LFUNCVAL( ow_skip ) },
{ LSTRKEY( "select" ), LFUNCVAL( ow_select ) },
{ LSTRKEY( "write" ), LFUNCVAL( ow_write ) },
{ LSTRKEY( "write_bytes" ), LFUNCVAL( ow_write_bytes ) },
{ LSTRKEY( "read" ), LFUNCVAL( ow_read ) },
{ LSTRKEY( "read_bytes" ), LFUNCVAL( ow_read_bytes ) },
{ LSTRKEY( "depower" ), LFUNCVAL( ow_depower ) },
{ LSTRKEY( "reset_search" ), LFUNCVAL( ow_reset_search ) },
{ LSTRKEY( "target_search" ), LFUNCVAL( ow_target_search ) },
{ LSTRKEY( "search" ), LFUNCVAL( ow_search ) },
{ LSTRKEY( "crc8" ), LFUNCVAL( ow_crc8 ) },
{ LSTRKEY( "check_crc16" ), LFUNCVAL( ow_check_crc16 ) },
{ LSTRKEY( "crc16" ), LFUNCVAL( ow_crc16 ) },
{ LNILKEY, LNILVAL }
};
int luaopen_ow( lua_State *L )
{
// create the table to store bus userdata for search operations
lua_newtable( L );
ow_bus_table_ref = luaL_ref( L, LUA_REGISTRYINDEX );
return 0;
}
NODEMCU_MODULE(OW, "ow", ow_map, luaopen_ow);
...@@ -115,6 +115,31 @@ int platform_i2c_send_byte( unsigned id, uint8_t data, int ack_check_en ); ...@@ -115,6 +115,31 @@ int platform_i2c_send_byte( unsigned id, uint8_t data, int ack_check_en );
int platform_i2c_recv_byte( unsigned id, int ack_val ); int platform_i2c_recv_byte( unsigned id, int ack_val );
// *****************************************************************************
// Onewire platform interface
typedef struct {
unsigned char ROM_NO[8];
uint8_t LastDiscrepancy;
uint8_t LastFamilyDiscrepancy;
uint8_t LastDeviceFlag;
uint8_t power;
} platform_onewire_bus_t;
int platform_onewire_init( uint8_t gpio_num );
int platform_onewire_reset( uint8_t gpio_num, uint8_t *presence );
int platform_onewire_write_bytes( uint8_t gpio_num, const uint8_t *buf, uint16_t count, bool power );
int platform_onewire_depower( uint8_t gpio_num );
int platform_onewire_read_bytes( uint8_t gpio_num, uint8_t *buf, uint16_t count );
int platform_onewire_depower( uint8_t gpio_num );
void platform_onewire_reset_search( platform_onewire_bus_t *bus );
void platform_onewire_target_search( uint8_t family_code, platform_onewire_bus_t *bus );
uint8_t platform_onewire_search( uint8_t pin, uint8_t *newAddr, platform_onewire_bus_t *bus );
uint8_t platform_onewire_crc8( const uint8_t *addr, uint8_t len );
uint8_t platform_onewire_crc8( const uint8_t *addr, uint8_t len );
bool platform_onewire_check_crc16( const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc );
uint16_t platform_onewire_crc16( const uint8_t* input, uint16_t len, uint16_t crc );
// Internal flash erase/write functions // Internal flash erase/write functions
uint32_t platform_flash_get_sector_of_address( uint32_t addr ); uint32_t platform_flash_get_sector_of_address( uint32_t addr );
......
This diff is collapsed.
# 1-Wire Module
| Since | Origin / Contributor | Maintainer | Source |
| :----- | :-------------------- | :---------- | :------ |
| 2014-12-22 | [Zeroday](https://github.com/funshine) | [Zeroday](https://github.com/funshine) | [ow.c](../../../components/modules/ow.c)|
This module provides functions to work with the [1-Wire](https://en.wikipedia.org/wiki/1-Wire) device communications bus system.
## ow.check_crc16()
Computes the 1-Wire CRC16 and compare it against the received CRC.
#### Syntax
`ow.check_crc16(buf, inverted_crc0, inverted_crc1[, crc])`
#### Parameters
- `buf` string value, data to be calculated check sum in string
- `inverted_crc0` LSB of received CRC
- `inverted_crc1` MSB of received CRC
- `crc` CRC starting value (optional)
#### Returns
true if the CRC matches, false otherwise
## ow.crc16()
Computes a Dallas Semiconductor 16 bit CRC. This is required to check the integrity of data received from many 1-Wire devices. Note that the CRC computed here is **not** what you'll get from the 1-Wire network, for two reasons:
1. The CRC is transmitted bitwise inverted.
2. Depending on the endian-ness of your processor, the binary representation of the two-byte return value may have a different byte order than the two bytes you get from 1-Wire.
#### Syntax
`ow.crc16(buf[, crc])`
#### Parameters
- `buf` string value, data to be calculated check sum in string
- `crc` CRC starting value (optional)
#### Returns
the CRC16 as defined by Dallas Semiconductor
## ow.crc8()
Computes a Dallas Semiconductor 8 bit CRC, these are used in the ROM and scratchpad registers.
#### Syntax
`ow.crc8(buf)`
#### Parameters
`buf` string value, data to be calculated check sum in string
#### Returns
CRC result as byte
## ow.depower()
Stops forcing power onto the bus. You only need to do this if you used the 'power' flag to `ow.write()` or used a `ow.write_bytes()` and aren't about to do another read or write.
#### Syntax
`ow.depower(pin)`
#### Parameters
`pin` 0~33, I/O index
#### Returns
`nil`
####See also
- [ow.write()](#owwrite)
- [ow.write_bytes()](#owwrite_bytes)
## ow.read()
Reads a byte.
####Syntax
`ow.read(pin)`
#### Parameters
`pin` 0~33, I/O index
#### Returns
byte read from slave device
## ow.read_bytes()
Reads multi bytes.
#### Syntax
`ow.read_bytes(pin, size)`
#### Parameters
- `pin` 0~33, I/O index
- `size` number of bytes to be read from slave device (up to 256)
#### Returns
`string` bytes read from slave device
## ow.reset()
Performs a 1-Wire reset cycle.
#### Syntax
`ow.reset(pin)`
#### Parameters
`pin` 0~33, I/O index
#### Returns
- `1` if a device responds with a presence pulse
- `0` if there is no device or the bus is shorted or otherwise held low for more than 250 µS
## ow.reset_search()
Clears the search state so that it will start from the beginning again.
#### Syntax
`ow.reset_search(pin)`
#### Parameters
`pin` 0~33, I/O index
#### Returns
`nil`
## ow.search()
Looks for the next device.
#### Syntax
`ow.search(pin)`
#### Parameters
`pin` 0~33, I/O index
#### Returns
`rom_code` string with length of 8 upon success. It contains the rom code of slave device. Returns `nil` if search was unsuccessful.
#### See also
[ow.target_search()](#owtargetsearch)
## ow.select()
Issues a 1-Wire rom select command. Make sure you do the `ow.reset(pin)` first.
#### Syntax
`ow.select(pin, rom)`
#### Parameters
- `pin` 0~33, I/O index
- `rom` string value, len 8, rom code of the slave device
#### Returns
`nil`
#### Example
```lua
-- 18b20 Example
pin = 9
ow.setup(pin)
count = 0
repeat
count = count + 1
addr = ow.reset_search(pin)
addr = ow.search(pin)
until (addr ~= nil) or (count > 100)
if addr == nil then
print("No more addresses.")
else
print(addr:byte(1,8))
crc = ow.crc8(string.sub(addr,1,7))
if crc == addr:byte(8) then
if (addr:byte(1) == 0x10) or (addr:byte(1) == 0x28) then
print("Device is a DS18S20 family device.")
repeat
ow.reset(pin)
ow.select(pin, addr)
ow.write(pin, 0x44, 1)
tmr.delay(1000000)
present = ow.reset(pin)
ow.select(pin, addr)
ow.write(pin,0xBE,1)
print("P="..present)
data = nil
data = string.char(ow.read(pin))
for i = 1, 8 do
data = data .. string.char(ow.read(pin))
end
print(data:byte(1,9))
crc = ow.crc8(string.sub(data,1,8))
print("CRC="..crc)
if crc == data:byte(9) then
t = (data:byte(1) + data:byte(2) * 256) * 625
t1 = t / 10000
t2 = t % 10000
print("Temperature="..t1.."."..t2.."Centigrade")
end
until false
else
print("Device family is not recognized.")
end
else
print("CRC is not valid!")
end
end
```
####See also
[ow.reset()](#owreset)
## ow.setup()
Sets a pin in onewire mode.
#### Syntax
`ow.setup(pin)`
#### Parameters
`pin` 0~33, I/O index
#### Returns
`nil`
## ow.skip()
Issues a 1-Wire rom skip command, to address all on bus.
#### Syntax
`ow.skip(pin)`
#### Parameters
`pin` 0~33, I/O index
#### Returns
`nil`
## ow.target_search()
Sets up the search to find the device type `family_code`. The search itself has to be initiated with a subsequent call to `ow.search()`.
#### Syntax
`ow.target_search(pin, family_code)`
#### Parameters
- `pin` 0~33, I/O index
- `family_code` byte for family code
#### Returns
`nil`
####See also
[ow.search()](#owsearch)
## ow.write()
Writes a byte. If `power` is 1 then the wire is held high at the end for parasitically powered devices. You are responsible for eventually depowering it by calling `ow.depower()` or doing another read or write.
#### Syntax
`ow.write(pin, v, power)`
#### Parameters
- `pin` 0~33, I/O index
- `v` byte to be written to slave device
- `power` 1 for wire being held high for parasitically powered devices
#### Returns
`nil`
####See also
[ow.depower()](#owdepower)
## ow.write_bytes()
Writes multi bytes. If `power` is 1 then the wire is held high at the end for parasitically powered devices. You are responsible for eventually depowering it by calling `ow.depower()` or doing another read or write.
#### Syntax
`ow.write_bytes(pin, buf, power)`
#### Parameters
- `pin` 0~33, IO index
- `buf` string to be written to slave device
- `power` 1 for wire being held high for parasitically powered devices
#### Returns
`nil`
####See also
[ow.depower()](#owdepower)
...@@ -35,6 +35,7 @@ pages: ...@@ -35,6 +35,7 @@ pages:
- 'file': 'en/modules/file.md' - 'file': 'en/modules/file.md'
- 'i2c': 'en/modules/i2c.md' - 'i2c': 'en/modules/i2c.md'
- 'node': 'en/modules/node.md' - 'node': 'en/modules/node.md'
- 'ow (1-Wire)': 'en/modules/ow.md'
- 'sigma delta': 'en/modules/sigma-delta.md' - 'sigma delta': 'en/modules/sigma-delta.md'
- 'struct': 'en/modules/struct.md' - 'struct': 'en/modules/struct.md'
- 'tmr': 'en/modules/tmr.md' - 'tmr': 'en/modules/tmr.md'
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment