2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
27 #include "build/build_config.h"
29 #include "drivers/accgyro/accgyro.h"
30 #include "drivers/accgyro/accgyro_mpu.h"
31 #include "drivers/accgyro/accgyro_mpu6500.h"
32 #include "drivers/bus_spi.h"
33 #include "drivers/io.h"
34 #include "drivers/time.h"
35 #include "drivers/system.h"
37 #include "hardware_revision.h"
39 uint8_t hardwareRevision
= UNKNOWN
;
41 void detectHardwareRevision(void)
43 if (hse_value
== 8000000)
44 hardwareRevision
= NAZE32
;
45 else if (hse_value
== 12000000)
46 hardwareRevision
= NAZE32_REV5
;
51 #define DISABLE_SPI_CS IOHi(nazeSpiCsPin)
52 #define ENABLE_SPI_CS IOLo(nazeSpiCsPin)
54 #define SPI_DEVICE_NONE (0)
55 #define SPI_DEVICE_FLASH (1)
56 #define SPI_DEVICE_MPU (2)
58 #define M25P16_INSTRUCTION_RDID 0x9F
59 #define FLASH_M25P16_ID (0x202015)
61 static IO_t nazeSpiCsPin
= IO_NONE
;
63 uint8_t detectSpiDevice(void)
65 #ifdef NAZE_SPI_CS_PIN
66 nazeSpiCsPin
= IOGetByTag(IO_TAG(NAZE_SPI_CS_PIN
));
69 const uint8_t out
[] = { M25P16_INSTRUCTION_RDID
, 0, 0, 0 };
73 // try autodetect flash chip
74 delay(50); // short delay required after initialisation of SPI device instance.
76 spiTransfer(NAZE_SPI_INSTANCE
, out
, in
, sizeof(out
));
79 flash_id
= in
[1] << 16 | in
[2] << 8 | in
[3];
80 if (flash_id
== FLASH_M25P16_ID
)
81 return SPI_DEVICE_FLASH
;
87 spiTransferByte(NAZE_SPI_INSTANCE
, MPU_RA_WHO_AM_I
| MPU6500_BIT_RESET
);
88 in
[0] = spiTransferByte(NAZE_SPI_INSTANCE
, 0xff);
91 if (in
[0] == MPU6500_WHO_AM_I_CONST
)
92 return SPI_DEVICE_MPU
;
94 return SPI_DEVICE_NONE
;
99 void updateHardwareRevision(void)
102 uint8_t detectedSpiDevice
= detectSpiDevice();
104 if (detectedSpiDevice
== SPI_DEVICE_MPU
&& hardwareRevision
== NAZE32_REV5
)
105 hardwareRevision
= NAZE32_SP
;
109 ioTag_t
selectMPUIntExtiConfigByHardwareRevision(void)
115 if (hardwareRevision
< NAZE32_REV5
) {
116 // MPU_INT output on rev4 PB13
119 // MPU_INT output on rev5 PC13