Add FRAM driver

This commit is contained in:
Michal Moskal 2016-04-05 17:25:45 -07:00
parent 7e1248b8dc
commit 65d48f4b02
3 changed files with 82 additions and 0 deletions

54
libs/i2c-fram/fram.ts Normal file
View File

@ -0,0 +1,54 @@
namespace i2c_fram {
const devaddr = 0x50;
//% shim=ksrt::panic
function panic(code: number) { }
function die() { panic(142) }
export function readByte(addr: number) {
if (addr < 0 || addr > 0xffff)
die();
let buf = pins.createBuffer(2)
buf[0] = (addr >> 8) & 0xff;
buf[1] = addr & 0xff;
pins.i2cWriteBuffer(devaddr, buf);
buf = pins.i2cReadBuffer(devaddr, 1);
return buf[0];
}
export function writeByte(addr: number, val: number) {
if (addr < 0 || addr > 0xffff)
die();
if (val < 0 || val > 0xff)
die();
let buf = pins.createBuffer(3)
buf[0] = (addr >> 8) & 0xff;
buf[1] = addr & 0xff;
buf[2] = val;
pins.i2cWriteBuffer(devaddr, buf)
}
export function readBuffer(addr: number, length: number) {
if (addr < 0 || length < 0 || (addr + length) > 0xffff)
die();
let buf = pins.createBuffer(length)
for (let i = 0; i < length; ++i)
buf[i] = readByte(addr + i)
return buf
}
export function writeBuffer(addr:number, buf: Buffer) {
if (addr < 0 || (addr + buf.length) > 0xffff)
die();
for (let i = 0; i < buf.length; ++i)
writeByte(addr + i, buf[i])
}
}

14
libs/i2c-fram/ftest.ts Normal file
View File

@ -0,0 +1,14 @@
i2c_fram.writeByte(100, 42)
i2c_fram.writeByte(101, 108)
function toBuf(arr: number[]) {
let buf = pins.createBuffer(arr.length)
for (let i = 0; i < arr.length; ++i)
buf[i] = arr[i]
return buf
}
i2c_fram.writeBuffer(98, toBuf([1,2,3,4,5,6,7]))
console.log("100:" + i2c_fram.readByte(100))
console.log("101:" + i2c_fram.readByte(101))

14
libs/i2c-fram/kind.json Normal file
View File

@ -0,0 +1,14 @@
{
"name": "i2c-fram",
"description": "AdaFruit I2C FRAM driver for micro:bit",
"files": [
"fram.ts"
],
"testFiles": [
"ftest.ts"
],
"public": true,
"dependencies": {
"microbit": "file:../microbit"
}
}