Removed excess trailing spaces before new lines on licenses.
[betaflight.git] / src / main / target / NAZE / hardware_revision.c
blobbf52cd0284af87da1ee2ec2f9ac784cef201cec3
1 /*
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)
8 * any later version.
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/>.
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include <stdlib.h>
25 #include "platform.h"
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;
49 #ifdef USE_SPI
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));
67 #endif
69 const uint8_t out[] = { M25P16_INSTRUCTION_RDID, 0, 0, 0 };
70 uint8_t in[4];
71 uint32_t flash_id;
73 // try autodetect flash chip
74 delay(50); // short delay required after initialisation of SPI device instance.
75 ENABLE_SPI_CS;
76 spiTransfer(NAZE_SPI_INSTANCE, out, in, sizeof(out));
77 DISABLE_SPI_CS;
79 flash_id = in[1] << 16 | in[2] << 8 | in[3];
80 if (flash_id == FLASH_M25P16_ID)
81 return SPI_DEVICE_FLASH;
84 // try autodetect MPU
85 delay(50);
86 ENABLE_SPI_CS;
87 spiTransferByte(NAZE_SPI_INSTANCE, MPU_RA_WHO_AM_I | MPU6500_BIT_RESET);
88 in[0] = spiTransferByte(NAZE_SPI_INSTANCE, 0xff);
89 DISABLE_SPI_CS;
91 if (in[0] == MPU6500_WHO_AM_I_CONST)
92 return SPI_DEVICE_MPU;
94 return SPI_DEVICE_NONE;
97 #endif
99 void updateHardwareRevision(void)
101 #ifdef USE_SPI
102 uint8_t detectedSpiDevice = detectSpiDevice();
104 if (detectedSpiDevice == SPI_DEVICE_MPU && hardwareRevision == NAZE32_REV5)
105 hardwareRevision = NAZE32_SP;
106 #endif
109 ioTag_t selectMPUIntExtiConfigByHardwareRevision(void)
112 #ifdef AFROMINI
113 return IO_TAG(PC13);
114 #else
115 if (hardwareRevision < NAZE32_REV5) {
116 // MPU_INT output on rev4 PB13
117 return IO_TAG(PB13);
118 } else {
119 // MPU_INT output on rev5 PC13
120 return IO_TAG(PC13);
122 #endif