NTest_gpio_env.lua 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. -- Walk the GPIO subsystem through its paces, using the attached I2C GPIO chip
  2. --
  3. -- Node GPIO 13 (index 7) is connected to I2C expander channel B6; node OUT
  4. -- Node GPIO 15 (index 8) is connected to I2C expander channel B7; node IN
  5. local N = ...
  6. N = (N or require "NTest")("gpio-env")
  7. -- TODO: Preflight test that we are in the correct environment with an I2C
  8. -- expander in the right place with the right connections.
  9. -- TODO: Use the mcp23017 module in the main tree rather than hand-coding
  10. -- the commands
  11. N.test('setup', function()
  12. -- Set gpio pin directions
  13. gpio.mode(8, gpio.INPUT)
  14. gpio.mode(7, gpio.OUTPUT, gpio.FLOAT)
  15. -- Configure the I2C bus
  16. i2c.setup(0, 2, 1, i2c.FAST)
  17. -- Set the IO expander port B to channel 7 as output, 6 as input
  18. i2c.start(0)
  19. ok(i2c.address(0, 0x20, i2c.TRANSMITTER))
  20. i2c.write(0, 0x01, 0x7F)
  21. i2c.stop(0)
  22. end)
  23. local function seti2cb7(v)
  24. i2c.start(0)
  25. i2c.address(0, 0x20, i2c.TRANSMITTER)
  26. i2c.write(0, 0x15, v and 0x80 or 0x00)
  27. i2c.stop(0)
  28. end
  29. local function geti2cb6()
  30. i2c.start(0)
  31. i2c.address(0, 0x20, i2c.TRANSMITTER)
  32. i2c.write(0, 0x13)
  33. i2c.start(0)
  34. i2c.address(0, 0x20, i2c.RECEIVER)
  35. local v = i2c.read(0, 1):byte(1)
  36. i2c.stop(0)
  37. return (bit.band(v,0x40) ~= 0)
  38. end
  39. N.test('gpio read 0', function()
  40. seti2cb7(false)
  41. ok(eq(0, gpio.read(8)))
  42. end)
  43. N.test('gpio read 1', function()
  44. seti2cb7(true)
  45. ok(eq(1, gpio.read(8)))
  46. end)
  47. N.test('i2c read 0', function()
  48. gpio.write(7, 0)
  49. ok(eq(false, geti2cb6()))
  50. end)
  51. N.test('i2c read 1', function()
  52. gpio.write(7, 1)
  53. ok(eq(true, geti2cb6()))
  54. end)
  55. N.testasync('gpio toggle trigger 1', function(next)
  56. seti2cb7(false)
  57. tmr.delay(10)
  58. gpio.trig(8, "both", function(l,_,c)
  59. ok(c == 1 and l == 1)
  60. return next()
  61. end)
  62. seti2cb7(true)
  63. end, true)
  64. N.testasync('gpio toggle trigger 2', function(next)
  65. gpio.trig(8, "both", function(l,_,c)
  66. ok(c == 1 and l == 0)
  67. return next()
  68. end)
  69. seti2cb7(false)
  70. end, true)
  71. N.test('gpio toggle trigger end', function()
  72. gpio.trig(8, "none")
  73. ok(true)
  74. end)