test_spi.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # python-cc1101 - Python Library to Transmit RF Signals via CC1101 Transceivers
  2. #
  3. # Copyright (C) 2020 Fabian Peter Hammerle <fabian@hammerle.me>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import re
  18. import unittest.mock
  19. import pytest
  20. import cc1101
  21. import cc1101.addresses
  22. import cc1101.options
  23. # pylint: disable=protected-access
  24. @pytest.mark.parametrize("bus", [0, 1])
  25. @pytest.mark.parametrize("chip_select", [0, 2])
  26. def test___init__select_device(bus, chip_select):
  27. with unittest.mock.patch("spidev.SpiDev"):
  28. transceiver = cc1101.CC1101(spi_bus=bus, spi_chip_select=chip_select)
  29. assert transceiver._spi_bus == bus
  30. assert transceiver._spi_chip_select == chip_select
  31. assert transceiver._spi_device_path == "/dev/spidev{}.{}".format(bus, chip_select)
  32. transceiver._spi.open.side_effect = SystemExit
  33. with pytest.raises(SystemExit):
  34. with transceiver:
  35. pass
  36. transceiver._spi.open.assert_called_once_with(bus, chip_select)
  37. def test__read_status_register(transceiver):
  38. transceiver._spi.xfer.return_value = [0, 20]
  39. transceiver._read_status_register(cc1101.addresses.StatusRegisterAddress.VERSION)
  40. transceiver._spi.xfer.assert_called_once_with([0x31 | 0xC0, 0])
  41. def test__command_strobe(transceiver):
  42. transceiver._spi.xfer.return_value = [15]
  43. transceiver._command_strobe(cc1101.addresses.StrobeAddress.STX)
  44. transceiver._spi.xfer.assert_called_once_with([0x35 | 0x00])
  45. def test__reset(transceiver):
  46. transceiver._spi.xfer.return_value = [15]
  47. transceiver._reset()
  48. transceiver._spi.xfer.assert_called_once_with([0x30 | 0x00])
  49. @pytest.mark.parametrize("chip_version", [0x14, 0x04])
  50. def test___enter__(transceiver, chip_version):
  51. with unittest.mock.patch.object(
  52. transceiver, "_read_status_register"
  53. ) as read_status_register_mock, unittest.mock.patch.object(
  54. transceiver, "_reset"
  55. ) as reset_mock, unittest.mock.patch.object(
  56. transceiver, "_set_modulation_format"
  57. ) as set_modulation_format_mock, unittest.mock.patch.object(
  58. transceiver, "_set_power_amplifier_setting_index"
  59. ) as set_pa_setting_mock, unittest.mock.patch.object(
  60. transceiver, "_disable_data_whitening"
  61. ) as disable_whitening_mock, unittest.mock.patch.object(
  62. transceiver, "_write_burst"
  63. ) as write_burst_mock, unittest.mock.patch.object(
  64. transceiver,
  65. "get_main_radio_control_state_machine_state",
  66. return_value=cc1101.MainRadioControlStateMachineState.IDLE,
  67. ):
  68. read_status_register_mock.side_effect = lambda r: {
  69. cc1101.addresses.StatusRegisterAddress.PARTNUM: 0,
  70. cc1101.addresses.StatusRegisterAddress.VERSION: chip_version,
  71. }[r]
  72. with transceiver as transceiver_context:
  73. assert transceiver == transceiver_context
  74. transceiver._spi.open.assert_called_once_with(0, 0)
  75. assert transceiver._spi.max_speed_hz == 55700
  76. reset_mock.assert_called_once_with()
  77. set_modulation_format_mock.assert_called_once_with(
  78. cc1101.options.ModulationFormat.ASK_OOK
  79. )
  80. set_pa_setting_mock.assert_called_once_with(1)
  81. disable_whitening_mock.assert_called_once_with()
  82. write_burst_mock.assert_called_once_with(0x18, [0b010100])
  83. def test___enter___unsupported_partnum(transceiver):
  84. with unittest.mock.patch.object(
  85. transceiver, "_read_status_register"
  86. ) as read_status_register_mock, unittest.mock.patch.object(transceiver, "_reset"):
  87. read_status_register_mock.side_effect = lambda r: {
  88. cc1101.addresses.StatusRegisterAddress.PARTNUM: 21
  89. }[r]
  90. with pytest.raises(ValueError, match=r"^unexpected chip part number "):
  91. with transceiver:
  92. pass
  93. def test___enter___unsupported_chip_version(transceiver):
  94. with unittest.mock.patch.object(
  95. transceiver, "_read_status_register"
  96. ) as read_status_register_mock, unittest.mock.patch.object(transceiver, "_reset"):
  97. read_status_register_mock.side_effect = lambda r: {
  98. cc1101.addresses.StatusRegisterAddress.PARTNUM: 0,
  99. cc1101.addresses.StatusRegisterAddress.VERSION: 0x15,
  100. }[r]
  101. with pytest.raises(
  102. ValueError,
  103. match=r"^{}$".format(
  104. re.escape(
  105. "Unsupported chip version 0x15 (expected one of [0x04, 0x14])"
  106. )
  107. ),
  108. ):
  109. with transceiver:
  110. pass
  111. def test___enter___chip_version_zero(transceiver):
  112. with unittest.mock.patch.object(
  113. transceiver, "_read_status_register"
  114. ) as read_status_register_mock, unittest.mock.patch.object(transceiver, "_reset"):
  115. read_status_register_mock.side_effect = lambda r: {
  116. cc1101.addresses.StatusRegisterAddress.PARTNUM: 0x00,
  117. cc1101.addresses.StatusRegisterAddress.VERSION: 0x00,
  118. }[r]
  119. with pytest.raises(
  120. ValueError,
  121. match=r"^Unsupported chip version 0x00 \(expected one of \[0x04, 0x14\]\)"
  122. r"\n\nPlease verify that all required pins are connected"
  123. r" \(see https://github\.com/fphammerle/python\-cc1101\#wiring\-raspberry\-pi\)"
  124. r" and that you selected the correct SPI bus and chip/slave select line\.$",
  125. ):
  126. with transceiver:
  127. pass
  128. @pytest.mark.parametrize("bus", [0, 1])
  129. @pytest.mark.parametrize("chip_select", [0, 2])
  130. def test___enter__permission_error(transceiver, bus, chip_select):
  131. transceiver._spi.open.side_effect = PermissionError("[Errno 13] Permission denied")
  132. transceiver._spi_bus = bus
  133. transceiver._spi_chip_select = chip_select
  134. with pytest.raises(
  135. PermissionError, match=r"\s/dev/spidev{}.{}\s".format(bus, chip_select)
  136. ):
  137. with transceiver:
  138. pass
  139. def test___enter__non_idle(transceiver):
  140. with unittest.mock.patch.object(
  141. transceiver,
  142. "get_main_radio_control_state_machine_state",
  143. return_value=cc1101.MainRadioControlStateMachineState.TX,
  144. ), unittest.mock.patch.object(transceiver, "_reset"), unittest.mock.patch.object(
  145. transceiver, "_verify_chip"
  146. ), unittest.mock.patch.object(
  147. transceiver, "_configure_defaults"
  148. ):
  149. with pytest.raises(
  150. ValueError, match=r"^expected marcstate idle \(actual: TX\)$"
  151. ):
  152. with transceiver:
  153. pass