test_spi.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. 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. assert write_burst_mock.call_args_list == [
  83. unittest.mock.call(0x18, [0b010100]),
  84. unittest.mock.call(0x02, [0b000001]),
  85. ]
  86. def test___enter___unsupported_partnum(transceiver):
  87. with unittest.mock.patch.object(
  88. transceiver, "_read_status_register"
  89. ) as read_status_register_mock, unittest.mock.patch.object(transceiver, "_reset"):
  90. read_status_register_mock.side_effect = lambda r: {
  91. cc1101.addresses.StatusRegisterAddress.PARTNUM: 21
  92. }[r]
  93. with pytest.raises(ValueError, match=r"^unexpected chip part number "):
  94. with transceiver:
  95. pass
  96. def test___enter___unsupported_chip_version(transceiver):
  97. with unittest.mock.patch.object(
  98. transceiver, "_read_status_register"
  99. ) as read_status_register_mock, unittest.mock.patch.object(transceiver, "_reset"):
  100. read_status_register_mock.side_effect = lambda r: {
  101. cc1101.addresses.StatusRegisterAddress.PARTNUM: 0,
  102. cc1101.addresses.StatusRegisterAddress.VERSION: 0x15,
  103. }[r]
  104. with pytest.raises(
  105. ValueError,
  106. match=r"^{}$".format( # pylint: disable=consider-using-f-string
  107. re.escape(
  108. "Unsupported chip version 0x15 (expected one of [0x04, 0x14])"
  109. )
  110. ),
  111. ):
  112. with transceiver:
  113. pass
  114. def test___enter___chip_version_zero(transceiver):
  115. with unittest.mock.patch.object(
  116. transceiver, "_read_status_register"
  117. ) as read_status_register_mock, unittest.mock.patch.object(transceiver, "_reset"):
  118. read_status_register_mock.side_effect = lambda r: {
  119. cc1101.addresses.StatusRegisterAddress.PARTNUM: 0x00,
  120. cc1101.addresses.StatusRegisterAddress.VERSION: 0x00,
  121. }[r]
  122. with pytest.raises(
  123. ValueError,
  124. match=r"^Unsupported chip version 0x00 \(expected one of \[0x04, 0x14\]\)"
  125. r"\n\nPlease verify that all required pins are connected"
  126. r" \(see https://github\.com/fphammerle/python\-cc1101\#wiring\-raspberry\-pi\)"
  127. r" and that you selected the correct SPI bus and chip/slave select line\.$",
  128. ):
  129. with transceiver:
  130. pass
  131. @pytest.mark.parametrize("bus", [0, 1])
  132. @pytest.mark.parametrize("chip_select", [0, 2])
  133. def test___enter__permission_error(transceiver, bus, chip_select):
  134. transceiver._spi.open.side_effect = PermissionError("[Errno 13] Permission denied")
  135. transceiver._spi_bus = bus
  136. transceiver._spi_chip_select = chip_select
  137. with pytest.raises(PermissionError, match=f"\\s/dev/spidev{bus}.{chip_select}\\s"):
  138. with transceiver:
  139. pass
  140. def test___enter__non_idle(transceiver):
  141. with unittest.mock.patch.object(
  142. transceiver,
  143. "get_main_radio_control_state_machine_state",
  144. return_value=cc1101.MainRadioControlStateMachineState.TX,
  145. ), unittest.mock.patch.object(transceiver, "_reset"), unittest.mock.patch.object(
  146. transceiver, "_verify_chip"
  147. ), unittest.mock.patch.object(
  148. transceiver, "_configure_defaults"
  149. ):
  150. with pytest.raises(
  151. ValueError, match=r"^expected marcstate idle \(actual: TX\)$"
  152. ):
  153. with transceiver:
  154. pass