test_spi.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 == f"/dev/spidev{bus}.{chip_select}"
  32. assert transceiver._spi_max_speed_hz == 55700
  33. transceiver._spi.open.side_effect = SystemExit
  34. with pytest.raises(SystemExit):
  35. with transceiver:
  36. pass
  37. transceiver._spi.open.assert_called_once_with(bus, chip_select)
  38. def test__read_status_register(transceiver):
  39. transceiver._spi.xfer.return_value = [0, 20]
  40. transceiver._read_status_register(cc1101.addresses.StatusRegisterAddress.VERSION)
  41. transceiver._spi.xfer.assert_called_once_with([0x31 | 0xC0, 0])
  42. def test__command_strobe(transceiver):
  43. transceiver._spi.xfer.return_value = [15]
  44. transceiver._command_strobe(cc1101.addresses.StrobeAddress.STX)
  45. transceiver._spi.xfer.assert_called_once_with([0x35 | 0x00])
  46. def test__reset(transceiver):
  47. transceiver._spi.xfer.return_value = [15]
  48. transceiver._reset()
  49. transceiver._spi.xfer.assert_called_once_with([0x30 | 0x00])
  50. @pytest.mark.parametrize("chip_version", [0x14, 0x04])
  51. def test___enter__(transceiver, chip_version):
  52. with unittest.mock.patch.object(
  53. transceiver, "_read_status_register"
  54. ) as read_status_register_mock, unittest.mock.patch.object(
  55. transceiver, "_reset"
  56. ) as reset_mock, unittest.mock.patch.object(
  57. transceiver, "_set_modulation_format"
  58. ) as set_modulation_format_mock, unittest.mock.patch.object(
  59. transceiver, "_set_power_amplifier_setting_index"
  60. ) as set_pa_setting_mock, unittest.mock.patch.object(
  61. transceiver, "_disable_data_whitening"
  62. ) as disable_whitening_mock, unittest.mock.patch.object(
  63. transceiver, "_write_burst"
  64. ) as write_burst_mock, unittest.mock.patch.object(
  65. transceiver,
  66. "get_main_radio_control_state_machine_state",
  67. return_value=cc1101.MainRadioControlStateMachineState.IDLE,
  68. ):
  69. read_status_register_mock.side_effect = lambda r: {
  70. cc1101.addresses.StatusRegisterAddress.PARTNUM: 0,
  71. cc1101.addresses.StatusRegisterAddress.VERSION: chip_version,
  72. }[r]
  73. with transceiver as transceiver_context:
  74. assert transceiver == transceiver_context
  75. transceiver._spi.open.assert_called_once_with(0, 0)
  76. assert transceiver._spi.max_speed_hz == 55700
  77. assert transceiver._spi.max_speed_hz == transceiver._spi_max_speed_hz
  78. reset_mock.assert_called_once_with()
  79. set_modulation_format_mock.assert_called_once_with(
  80. cc1101.options.ModulationFormat.ASK_OOK
  81. )
  82. set_pa_setting_mock.assert_called_once_with(1)
  83. disable_whitening_mock.assert_called_once_with()
  84. assert write_burst_mock.call_args_list == [
  85. unittest.mock.call(0x18, [0b010100]),
  86. unittest.mock.call(0x02, [0b000001]),
  87. ]
  88. @pytest.mark.parametrize("spi_max_speed_hz", [55700, 500000])
  89. def test___enter__spi_max_speed(spi_max_speed_hz):
  90. with unittest.mock.patch("spidev.SpiDev"):
  91. transceiver = cc1101.CC1101(spi_max_speed_hz=spi_max_speed_hz)
  92. assert transceiver._spi_max_speed_hz == spi_max_speed_hz
  93. with unittest.mock.patch.object(
  94. transceiver, "_reset", side_effect=SystemExit
  95. ), pytest.raises(SystemExit):
  96. with transceiver:
  97. pass
  98. assert transceiver._spi.max_speed_hz == spi_max_speed_hz
  99. def test___enter___unsupported_partnum(transceiver):
  100. with unittest.mock.patch.object(
  101. transceiver, "_read_status_register"
  102. ) as read_status_register_mock, unittest.mock.patch.object(transceiver, "_reset"):
  103. read_status_register_mock.side_effect = lambda r: {
  104. cc1101.addresses.StatusRegisterAddress.PARTNUM: 21
  105. }[r]
  106. with pytest.raises(ValueError, match=r"^unexpected chip part number "):
  107. with transceiver:
  108. pass
  109. def test___enter___unsupported_chip_version(transceiver):
  110. with unittest.mock.patch.object(
  111. transceiver, "_read_status_register"
  112. ) as read_status_register_mock, unittest.mock.patch.object(transceiver, "_reset"):
  113. read_status_register_mock.side_effect = lambda r: {
  114. cc1101.addresses.StatusRegisterAddress.PARTNUM: 0,
  115. cc1101.addresses.StatusRegisterAddress.VERSION: 0x15,
  116. }[r]
  117. with pytest.raises(
  118. ValueError,
  119. match=r"^{}$".format( # pylint: disable=consider-using-f-string
  120. re.escape(
  121. "Unsupported chip version 0x15 (expected one of [0x04, 0x14])"
  122. )
  123. ),
  124. ):
  125. with transceiver:
  126. pass
  127. def test___enter___chip_version_zero(transceiver):
  128. with unittest.mock.patch.object(
  129. transceiver, "_read_status_register"
  130. ) as read_status_register_mock, unittest.mock.patch.object(transceiver, "_reset"):
  131. read_status_register_mock.side_effect = lambda r: {
  132. cc1101.addresses.StatusRegisterAddress.PARTNUM: 0x00,
  133. cc1101.addresses.StatusRegisterAddress.VERSION: 0x00,
  134. }[r]
  135. with pytest.raises(
  136. ValueError,
  137. match=r"^Unsupported chip version 0x00 \(expected one of \[0x04, 0x14\]\)"
  138. r"\n\nPlease verify that all required pins are connected"
  139. r" \(see https://github\.com/fphammerle/python\-cc1101\#wiring\-raspberry\-pi\)"
  140. r" and that you selected the correct SPI bus and chip/slave select line\.$",
  141. ):
  142. with transceiver:
  143. pass
  144. @pytest.mark.parametrize("bus", [0, 1])
  145. @pytest.mark.parametrize("chip_select", [0, 2])
  146. def test___enter__permission_error(transceiver, bus, chip_select):
  147. transceiver._spi.open.side_effect = PermissionError("[Errno 13] Permission denied")
  148. transceiver._spi_bus = bus
  149. transceiver._spi_chip_select = chip_select
  150. with pytest.raises(PermissionError, match=f"\\s/dev/spidev{bus}.{chip_select}\\s"):
  151. with transceiver:
  152. pass
  153. def test___enter__non_idle(transceiver):
  154. with unittest.mock.patch.object(
  155. transceiver,
  156. "get_main_radio_control_state_machine_state",
  157. return_value=cc1101.MainRadioControlStateMachineState.TX,
  158. ), unittest.mock.patch.object(transceiver, "_reset"), unittest.mock.patch.object(
  159. transceiver, "_verify_chip"
  160. ), unittest.mock.patch.object(
  161. transceiver, "_configure_defaults"
  162. ):
  163. with pytest.raises(
  164. ValueError, match=r"^expected marcstate idle \(actual: TX\)$"
  165. ):
  166. with transceiver:
  167. pass