diff --git a/README.md b/README.md index afc6358..a9d4dc2 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,8 @@ Directory: /examples/hello Modifications necessary: None This example does not use radio, it just periodically logs a counter value. -Can be used to checked if the timer implementation on RPi works as expected. +Can be used to checked if the timer implementation on RPi works as expected +and if SPI communication with the radio board is possible. cd examples/hello make clean @@ -46,6 +47,7 @@ Can be used to checked if the timer implementation on RPi works as expected. Possible output: 000000000 HAL: Initializing ... + 000000003 HAL: Detected SX1276 radio module. 000000003 HAL: Set radio RST pin to 0x00 000000003 HAL: Wait until 000000004 ms 000000005 HAL: Set radio RST pin to 0x02 @@ -109,6 +111,7 @@ work with a Single Channel Gateway. Possible outpout: 000000000 HAL: Initializing ... + 000000000 HAL: Detected SX1276 radio module. 000000001 HAL: Set radio RST pin to 0x00 000000002 HAL: Wait until 000000002 ms 000000003 HAL: Set radio RST pin to 0x02 @@ -152,6 +155,7 @@ every 60 seconds as an unconfirmed message with a payload of 2 bytes. Possible output: 000000000 HAL: Initializing ... + 000000004 HAL: Detected SX1276 radio module. 000000004 HAL: Set radio RST pin to 0x00 000000005 HAL: Wait until 000000006 ms 000000006 HAL: Set radio RST pin to 0x02 diff --git a/lora_gps_hat/hal.c b/lora_gps_hat/hal.c index ff800b4..688ed21 100644 --- a/lora_gps_hat/hal.c +++ b/lora_gps_hat/hal.c @@ -98,6 +98,34 @@ void hal_init () { hal_failed(); } + // Make sure that SPI communication with the radio module works + // by reading the "version" register 0x42 of the radio module. + hal_pin_nss(0); + hal_spi(0x42 & 0x7F); + u1_t val = hal_spi(0x00); + hal_pin_nss(1); + + if (0 == val) { + fprintf(stderr, "HAL: There is an issue with the SPI communication to the radio module.\n"); + fprintf(stderr, "HAL: Make sure that \n"); + fprintf(stderr, "HAL: * The radio module is attached to your Raspberry Pi\n"); + fprintf(stderr, "HAL: * The power supply provides enough power\n"); + fprintf(stderr, "HAL: * SPI is enabled on your Raspberry Pi. Use the tool \"raspi-config\" to enable it.\n"); + hal_failed(); + } + +#ifdef DEBUG_HAL + if (0x12 == val) { + fprintf(stdout, "%09d HAL: Detected SX1276 radio module.\n", osticks2ms(hal_ticks())); + } + else if (0x22 == val) { + fprintf(stdout, "%09d HAL: Detected SX1272 radio module.\n", osticks2ms(hal_ticks())); + } + else { + fprintf(stdout, "%09d HAL: Detected unknown radio module: 0x%02x\n", osticks2ms(hal_ticks()), val); + } +#endif + } void hal_failed () {