Private
Public Access
2
0

mock: added I2C functionality

Signed-off-by: Alex Tereschenko <alext.mkrs@gmail.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Alex Tereschenko
2016-07-21 22:04:05 +02:00
committed by Brendan Le Foll
parent 26718a67a2
commit 346f447c4d
21 changed files with 1106 additions and 5 deletions

View File

@@ -10,6 +10,18 @@ add_test (NAME py_gpio_mode COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT
add_test (NAME py_aio COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/aio_checks.py)
add_test (NAME py_i2c_freq COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/i2c_checks_freq.py)
add_test (NAME py_i2c_addr COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/i2c_checks_addr.py)
add_test (NAME py_i2c_read COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/i2c_checks_read.py)
add_test (NAME py_i2c_write COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/i2c_checks_write.py)
add_test (NAME py_i2c_read_byte COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/i2c_checks_read_byte.py)
add_test (NAME py_i2c_write_byte COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/i2c_checks_write_byte.py)
add_test (NAME py_i2c_read_byte_data COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/i2c_checks_read_byte_data.py)
add_test (NAME py_i2c_write_byte_data COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/i2c_checks_write_byte_data.py)
add_test (NAME py_i2c_read_bytes_data COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/i2c_checks_read_bytes_data.py)
add_test (NAME py_i2c_read_word_data COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/i2c_checks_read_word_data.py)
add_test (NAME py_i2c_write_word_data COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/i2c_checks_write_word_data.py)
set_tests_properties(py_general
py_platform
py_gpio_basic
@@ -19,4 +31,15 @@ set_tests_properties(py_general
py_gpio_isr
py_gpio_mode
py_aio
py_i2c_freq
py_i2c_addr
py_i2c_read
py_i2c_write
py_i2c_read_byte
py_i2c_write_byte
py_i2c_read_byte_data
py_i2c_write_byte_data
py_i2c_read_bytes_data
py_i2c_read_word_data
py_i2c_write_word_data
PROPERTIES ENVIRONMENT "PYTHONPATH=${PYTHON_DEFAULT_PYTHONPATH}")

View File

@@ -0,0 +1,52 @@
#!/usr/bin/env python
# Author: Alex Tereschenko <alext.mkrs@gmail.com>
# Copyright (c) 2016 Alex Tereschenko.
#
# 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.
import mraa as m
import unittest as u
from i2c_checks_shared import *
class I2cChecksAddr(u.TestCase):
def setUp(self):
self.i2c = m.I2c(MRAA_I2C_BUS_NUM)
def tearDown(self):
del self.i2c
def test_i2c_address(self):
self.assertEqual(self.i2c.address(0x10),
m.SUCCESS,
"Setting address to 0x10 did not return success")
def test_i2c_address_invalid_bigger_than_max(self):
# For standard 7-bit addressing 0x7F is max address
self.assertEqual(self.i2c.address(0xFF),
m.ERROR_INVALID_PARAMETER,
"Setting address to 0xFF did not return INVALID_PARAMETER")
def test_i2c_address_invalid_smaller_than_min(self):
self.assertRaises(OverflowError, self.i2c.address, -100)
if __name__ == "__main__":
u.main()

View File

@@ -0,0 +1,63 @@
#!/usr/bin/env python
# Author: Alex Tereschenko <alext.mkrs@gmail.com>
# Copyright (c) 2016 Alex Tereschenko.
#
# 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.
import mraa as m
import unittest as u
from i2c_checks_shared import *
class I2cChecksFreq(u.TestCase):
def setUp(self):
self.i2c = m.I2c(MRAA_I2C_BUS_NUM)
def tearDown(self):
del self.i2c
def test_i2c_frequency_STD(self):
self.assertEqual(self.i2c.frequency(m.I2C_STD),
m.SUCCESS,
"Setting frequency to I2C_STD did not return success")
def test_i2c_frequency_FAST(self):
self.assertEqual(self.i2c.frequency(m.I2C_FAST),
m.SUCCESS,
"Setting frequency to I2C_FAST did not return success")
def test_i2c_frequency_HIGH(self):
self.assertEqual(self.i2c.frequency(m.I2C_HIGH),
m.SUCCESS,
"Setting frequency to I2C_HIGH did not return success")
def test_i2c_frequency_invalid_bigger_than_max(self):
self.assertEqual(self.i2c.frequency(100),
m.ERROR_INVALID_PARAMETER,
"Setting frequency to 100 did not return INVALID_PARAMETER")
def test_i2c_frequency_invalid_smaller_than_min(self):
self.assertEqual(self.i2c.frequency(-100),
m.ERROR_INVALID_PARAMETER,
"Setting frequency to -100 did not return INVALID_PARAMETER")
if __name__ == "__main__":
u.main()

View File

@@ -0,0 +1,58 @@
#!/usr/bin/env python
# Author: Alex Tereschenko <alext.mkrs@gmail.com>
# Copyright (c) 2016 Alex Tereschenko.
#
# 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.
import mraa as m
import unittest as u
from i2c_checks_shared import *
class I2cChecksRead(u.TestCase):
def setUp(self):
self.i2c = m.I2c(MRAA_I2C_BUS_NUM)
def tearDown(self):
del self.i2c
def test_i2c_read_full_reg_range(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR)
expected_res = bytearray([MRAA_MOCK_I2C_DATA_INIT_BYTE for i in range(MRAA_MOCK_I2C_DATA_LEN)])
res = self.i2c.read(MRAA_MOCK_I2C_DATA_LEN)
self.assertEqual(res, expected_res, "I2C read() of full register range returned unexpected data")
def test_i2c_read_part_reg_range(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR)
expected_res = bytearray([MRAA_MOCK_I2C_DATA_INIT_BYTE for i in range(MRAA_MOCK_I2C_DATA_LEN - 1)])
res = self.i2c.read(MRAA_MOCK_I2C_DATA_LEN - 1)
self.assertEqual(res, expected_res, "I2C read() of partial register range returned unexpected data")
def test_i2c_read_invalid_addr(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR - 1)
self.assertRaises(IOError, self.i2c.read, MRAA_MOCK_I2C_DATA_LEN)
def test_i2c_read_invalid_len_bigger_than_max(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR)
self.assertRaises(IOError, self.i2c.read, MRAA_MOCK_I2C_DATA_LEN + 1)
if __name__ == "__main__":
u.main()

View File

@@ -0,0 +1,48 @@
#!/usr/bin/env python
# Author: Alex Tereschenko <alext.mkrs@gmail.com>
# Copyright (c) 2016 Alex Tereschenko.
#
# 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.
import mraa as m
import unittest as u
from i2c_checks_shared import *
class I2cChecksReadByte(u.TestCase):
def setUp(self):
self.i2c = m.I2c(MRAA_I2C_BUS_NUM)
def tearDown(self):
del self.i2c
def test_i2c_read_byte(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR)
expected_res = MRAA_MOCK_I2C_DATA_INIT_BYTE
res = self.i2c.readByte()
self.assertEqual(res, expected_res, "I2C readByte() returned unexpected data")
def test_i2c_read_byte_invalid_addr(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR - 1)
self.assertRaises(ValueError, self.i2c.readByte)
if __name__ == "__main__":
u.main()

View File

@@ -0,0 +1,52 @@
#!/usr/bin/env python
# Author: Alex Tereschenko <alext.mkrs@gmail.com>
# Copyright (c) 2016 Alex Tereschenko.
#
# 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.
import mraa as m
import unittest as u
from i2c_checks_shared import *
class I2cChecksReadByteData(u.TestCase):
def setUp(self):
self.i2c = m.I2c(MRAA_I2C_BUS_NUM)
def tearDown(self):
del self.i2c
def test_i2c_read_byte_data(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR)
expected_res = MRAA_MOCK_I2C_DATA_INIT_BYTE
res = self.i2c.readReg(MRAA_MOCK_I2C_DATA_LEN - 1)
self.assertEqual(res, expected_res, "I2C readReg() returned unexpected data")
def test_i2c_read_byte_data_invalid_addr(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR - 1)
self.assertRaises(ValueError, self.i2c.readReg, MRAA_MOCK_I2C_DATA_LEN - 1)
def test_i2c_read_byte_data_invalid_reg(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR)
self.assertRaises(ValueError, self.i2c.readReg, MRAA_MOCK_I2C_DATA_LEN)
if __name__ == "__main__":
u.main()

View File

@@ -0,0 +1,81 @@
#!/usr/bin/env python
# Author: Alex Tereschenko <alext.mkrs@gmail.com>
# Copyright (c) 2016 Alex Tereschenko.
#
# 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.
import mraa as m
import unittest as u
from i2c_checks_shared import *
class I2cChecksReadBytesData(u.TestCase):
def setUp(self):
self.i2c = m.I2c(MRAA_I2C_BUS_NUM)
def tearDown(self):
del self.i2c
def test_i2c_read_bytes_data(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR)
# Generate unique data bytes
data_to_write = bytearray([0xEE+i for i in range(MRAA_MOCK_I2C_DATA_LEN)])
self.i2c.write(data_to_write)
# We expect to read the last two bytes
expected_res = bytearray(data_to_write[-2:])
test_reg_addr = MRAA_MOCK_I2C_DATA_LEN - 2
test_read_len = 2
self.assertEqual(self.i2c.readBytesReg(test_reg_addr, test_read_len),
expected_res,
"I2C readBytesReg() returned unexpected data")
def test_i2c_read_bytes_data_length_bigger_than_max(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR)
# Generate unique data bytes
data_to_write = bytearray([0xEE+i for i in range(MRAA_MOCK_I2C_DATA_LEN)])
self.i2c.write(data_to_write)
# We expect to read the last two bytes
expected_res = bytearray(data_to_write[-2:])
test_reg_addr = MRAA_MOCK_I2C_DATA_LEN - 2
# Set the read length bigger than our data length
test_read_len = MRAA_MOCK_I2C_DATA_LEN + 2
self.assertEqual(self.i2c.readBytesReg(test_reg_addr, test_read_len),
expected_res,
"I2C readBytesReg() returned unexpected data")
def test_i2c_read_bytes_data_zero_length(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR)
self.assertRaises(IOError, self.i2c.readBytesReg, 0x0, 0)
def test_i2c_read_bytes_data_negative_length(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR)
self.assertRaises(ValueError, self.i2c.readBytesReg, 0x0, -1)
def test_i2c_read_bytes_data_invalid_addr(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR - 1)
self.assertRaises(IOError, self.i2c.readBytesReg, 0x0, MRAA_MOCK_I2C_DATA_LEN - 1)
def test_i2c_read_bytes_data_invalid_reg(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR)
self.assertRaises(IOError, self.i2c.readBytesReg, MRAA_MOCK_I2C_DATA_LEN, MRAA_MOCK_I2C_DATA_LEN - 1)
if __name__ == "__main__":
u.main()

View File

@@ -0,0 +1,56 @@
#!/usr/bin/env python
# Author: Alex Tereschenko <alext.mkrs@gmail.com>
# Copyright (c) 2016 Alex Tereschenko.
#
# 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.
import mraa as m
import unittest as u
from i2c_checks_shared import *
class I2cChecksReadWordData(u.TestCase):
def setUp(self):
self.i2c = m.I2c(MRAA_I2C_BUS_NUM)
def tearDown(self):
del self.i2c
def test_i2c_read_word_data(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR)
high_byte = 0xAA
low_byte = 0xBB
expected_res = (high_byte << 8) + low_byte
self.i2c.writeReg(MRAA_MOCK_I2C_DATA_LEN - 2, high_byte)
self.i2c.writeReg(MRAA_MOCK_I2C_DATA_LEN - 1, low_byte)
res = self.i2c.readWordReg(MRAA_MOCK_I2C_DATA_LEN - 2)
self.assertEqual(res, expected_res, "I2C readWordReg() returned unexpected data")
def test_i2c_read_word_data_invalid_addr(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR - 1)
self.assertRaises(ValueError, self.i2c.readWordReg, MRAA_MOCK_I2C_DATA_LEN - 2)
def test_i2c_read_word_data_invalid_reg(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR)
self.assertRaises(ValueError, self.i2c.readReg, MRAA_MOCK_I2C_DATA_LEN)
if __name__ == "__main__":
u.main()

View File

@@ -0,0 +1,29 @@
#!/usr/bin/env python
# Author: Alex Tereschenko <alext.mkrs@gmail.com>
# Copyright (c) 2016 Alex Tereschenko.
#
# 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.
MRAA_I2C_BUS_NUM = 0
# These are defined in mock_board.c
MRAA_MOCK_I2C_ADDR = 0x33
MRAA_MOCK_I2C_DATA_LEN = 10
MRAA_MOCK_I2C_DATA_INIT_BYTE = 0xAB

View File

@@ -0,0 +1,78 @@
#!/usr/bin/env python
# Author: Alex Tereschenko <alext.mkrs@gmail.com>
# Copyright (c) 2016 Alex Tereschenko.
#
# 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.
import mraa as m
import unittest as u
from i2c_checks_shared import *
class I2cChecksWrite(u.TestCase):
def setUp(self):
self.i2c = m.I2c(MRAA_I2C_BUS_NUM)
def tearDown(self):
del self.i2c
def test_i2c_write_full_reg_range(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR)
data_to_write = bytearray([0xEE for i in range(MRAA_MOCK_I2C_DATA_LEN)])
self.assertEqual(self.i2c.write(data_to_write),
m.SUCCESS,
"I2C write() of full register range did not return success")
res = self.i2c.read(MRAA_MOCK_I2C_DATA_LEN)
self.assertEqual(res,
data_to_write,
"I2C read() after write() of full register range returned unexpected data")
def test_i2c_write_part_reg_range(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR)
data_to_write = bytearray([0xEE for i in range(MRAA_MOCK_I2C_DATA_LEN - 1)])
self.assertEqual(self.i2c.write(data_to_write),
m.SUCCESS,
"I2C write() of partial register range did not return success")
res = self.i2c.read(MRAA_MOCK_I2C_DATA_LEN - 1)
self.assertEqual(res,
data_to_write,
"I2C read() after write() of partial register range returned unexpected data")
def test_i2c_write_len_bigger_than_max(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR)
data_to_write = bytearray([0xEE for i in range(MRAA_MOCK_I2C_DATA_LEN + 1)])
self.assertEqual(self.i2c.write(data_to_write),
m.SUCCESS,
"I2C write() with length bigger than max did not return success")
res = self.i2c.read(MRAA_MOCK_I2C_DATA_LEN)
self.assertEqual(res,
data_to_write[:-1],
"I2C read() after write() with length bigger than max returned unexpected data")
def test_i2c_write_invalid_addr(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR - 1)
data_to_write = bytearray([0xEE for i in range(MRAA_MOCK_I2C_DATA_LEN)])
self.assertEqual(self.i2c.write(data_to_write),
m.ERROR_UNSPECIFIED,
"I2C write() to invalid address did not return error")
if __name__ == "__main__":
u.main()

View File

@@ -0,0 +1,54 @@
#!/usr/bin/env python
# Author: Alex Tereschenko <alext.mkrs@gmail.com>
# Copyright (c) 2016 Alex Tereschenko.
#
# 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.
import mraa as m
import unittest as u
from i2c_checks_shared import *
class I2cChecksWriteByte(u.TestCase):
def setUp(self):
self.i2c = m.I2c(MRAA_I2C_BUS_NUM)
def tearDown(self):
del self.i2c
def test_i2c_write_byte(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR)
test_byte = 0xEE
self.assertEqual(self.i2c.writeByte(test_byte),
m.SUCCESS,
"I2C writeByte() did not return success")
self.assertEqual(self.i2c.readByte(),
test_byte,
"I2C readByte() after writeByte() returned unexpected data")
def test_i2c_write_byte_invalid_addr(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR - 1)
self.assertEqual(self.i2c.writeByte(0xEE),
m.ERROR_UNSPECIFIED,
"I2C writeByte() to invalid address did not return error")
if __name__ == "__main__":
u.main()

View File

@@ -0,0 +1,65 @@
#!/usr/bin/env python
# Author: Alex Tereschenko <alext.mkrs@gmail.com>
# Copyright (c) 2016 Alex Tereschenko.
#
# 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.
import mraa as m
import unittest as u
from i2c_checks_shared import *
class I2cChecksWriteByteData(u.TestCase):
def setUp(self):
self.i2c = m.I2c(MRAA_I2C_BUS_NUM)
def tearDown(self):
del self.i2c
def test_i2c_write_byte_data(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR)
test_byte = 0xEE
reg = MRAA_MOCK_I2C_DATA_LEN - 1
self.assertEqual(self.i2c.writeReg(reg, test_byte),
m.SUCCESS,
"I2C writeReg() did not return success")
self.assertEqual(self.i2c.readReg(reg),
test_byte,
"I2C readReg() after writeReg() returned unexpected data")
def test_i2c_write_byte_data_invalid_addr(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR - 1)
test_byte = 0xEE
reg = MRAA_MOCK_I2C_DATA_LEN - 1
self.assertEqual(self.i2c.writeReg(reg, test_byte),
m.ERROR_UNSPECIFIED,
"I2C writeReg() to invalid address did not return error")
def test_i2c_write_byte_data_invalid_reg(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR)
test_byte = 0xEE
reg = MRAA_MOCK_I2C_DATA_LEN
self.assertEqual(self.i2c.writeReg(reg, test_byte),
m.ERROR_UNSPECIFIED,
"I2C writeReg() with invalid register did not return error")
if __name__ == "__main__":
u.main()

View File

@@ -0,0 +1,70 @@
#!/usr/bin/env python
# Author: Alex Tereschenko <alext.mkrs@gmail.com>
# Copyright (c) 2016 Alex Tereschenko.
#
# 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.
import mraa as m
import unittest as u
from i2c_checks_shared import *
class I2cChecksWriteWordData(u.TestCase):
def setUp(self):
self.i2c = m.I2c(MRAA_I2C_BUS_NUM)
def tearDown(self):
del self.i2c
def test_i2c_write_word_data(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR)
high_byte = 0xAA
low_byte = 0xBB
test_word = (high_byte << 8) + low_byte
reg = MRAA_MOCK_I2C_DATA_LEN - 2
self.assertEqual(self.i2c.writeWordReg(reg, test_word),
m.SUCCESS,
"I2C writeWordReg() did not return success")
self.assertEqual(self.i2c.readReg(reg),
high_byte,
"I2C readReg() of higher byte after writeWordReg() returned unexpected data")
self.assertEqual(self.i2c.readReg(reg + 1),
low_byte,
"I2C readReg() of lower byte after writeWordReg() returned unexpected data")
def test_i2c_write_word_data_invalid_addr(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR - 1)
test_word = 0xAABB
reg = MRAA_MOCK_I2C_DATA_LEN - 2
self.assertEqual(self.i2c.writeWordReg(reg, test_word),
m.ERROR_UNSPECIFIED,
"I2C writeWordReg() to invalid address did not return error")
def test_i2c_write_word_data_invalid_reg(self):
self.i2c.address(MRAA_MOCK_I2C_ADDR)
test_word = 0xAABB
reg = MRAA_MOCK_I2C_DATA_LEN
self.assertEqual(self.i2c.writeWordReg(reg, test_word),
m.ERROR_UNSPECIFIED,
"I2C writeWordReg() with invalid register did not return error")
if __name__ == "__main__":
u.main()

View File

@@ -27,7 +27,7 @@
import mraa as m
import unittest as u
PLATFORM_PINCOUNT = 2
PLATFORM_PINCOUNT = 4
PLATFORM_STD_ADC_RES_BITS = 10
PLATFORM_MAX_ADC_RES_BITS = 12