diff --git a/.claude/settings.local.json b/.claude/settings.local.json index 9af76e0..e3e39ce 100644 --- a/.claude/settings.local.json +++ b/.claude/settings.local.json @@ -4,7 +4,8 @@ "Bash(cmake --build build)", "Bash(cmake -G \"Ninja\" -DCMAKE_TOOLCHAIN_FILE=../../cmake/gcc-arm-none-eabi.cmake ../..)", "Bash(ninja)", - "Bash(ninja clean *)" + "Bash(ninja clean *)", + "Bash(git add *)" ] } } diff --git a/.gitignore b/.gitignore index 6f88e9b..baa5a3f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ build mx.scratch !.settings -temp \ No newline at end of file +temp +compile_commands.json +*.bak \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index f66e8b8..587c826 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -8,9 +8,9 @@ ], "stm32cube-ide-clangd.path": "cube", "stm32cube-ide-clangd.arguments": [ - "--compile-commands-dir=${workspaceFolder}/build/Debug", "starm-clangd", "--query-driver=${env:CUBE_BUNDLE_PATH}/gnu-tools-for-stm32/14.3.1+st.2/bin/arm-none-eabi-gcc*", - "--query-driver=${env:CUBE_BUNDLE_PATH}/gnu-tools-for-stm32/14.3.1+st.2/bin/arm-none-eabi-g++*" + "--query-driver=${env:CUBE_BUNDLE_PATH}/gnu-tools-for-stm32/14.3.1+st.2/bin/arm-none-eabi-g++*", + "--compile-commands-dir=${workspaceFolder}/build/Debug/" ] } \ No newline at end of file diff --git a/modules/sensor/imu/j901p_reg.h b/3rdparty/jy901p/REG.h similarity index 100% rename from modules/sensor/imu/j901p_reg.h rename to 3rdparty/jy901p/REG.h diff --git a/3rdparty/jy901p/wit_c_sdk.c b/3rdparty/jy901p/wit_c_sdk.c new file mode 100644 index 0000000..5490601 --- /dev/null +++ b/3rdparty/jy901p/wit_c_sdk.c @@ -0,0 +1,502 @@ +#include "wit_c_sdk.h" + +static SerialWrite p_WitSerialWriteFunc = NULL; +static WitI2cWrite p_WitI2cWriteFunc = NULL; +static WitI2cRead p_WitI2cReadFunc = NULL; +static CanWrite p_WitCanWriteFunc = NULL; +static RegUpdateCb p_WitRegUpdateCbFunc = NULL; +static DelaymsCb p_WitDelaymsFunc = NULL; + +static uint8_t s_ucAddr = 0xff; +static uint8_t s_ucWitDataBuff[WIT_DATA_BUFF_SIZE]; +static uint32_t s_uiWitDataCnt = 0, s_uiProtoclo = 0, s_uiReadRegIndex = 0; +int16_t sReg[REGSIZE]; + + +#define FuncW 0x06 +#define FuncR 0x03 + +static const uint8_t __auchCRCHi[256] = { + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, + 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, + 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, + 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, + 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, + 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, + 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, + 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, + 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, + 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, + 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, + 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, + 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, + 0x40 +}; +static const uint8_t __auchCRCLo[256] = { + 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4, + 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, + 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, + 0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, + 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7, + 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, + 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, + 0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, + 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2, + 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, + 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, + 0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, + 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91, + 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, + 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, + 0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, + 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, + 0x40 +}; + + +static uint16_t __CRC16(uint8_t *puchMsg, uint16_t usDataLen) +{ + uint8_t uchCRCHi = 0xFF; + uint8_t uchCRCLo = 0xFF; + uint8_t uIndex; + int i = 0; + uchCRCHi = 0xFF; + uchCRCLo = 0xFF; + for (; i= 11) + { + ucSum = __CaliSum(s_ucWitDataBuff, 10); + if(ucSum != s_ucWitDataBuff[10]) + { + s_uiWitDataCnt--; + memcpy(s_ucWitDataBuff, &s_ucWitDataBuff[1], s_uiWitDataCnt); + return ; + } + usData[0] = ((uint16_t)s_ucWitDataBuff[3] << 8) | (uint16_t)s_ucWitDataBuff[2]; + usData[1] = ((uint16_t)s_ucWitDataBuff[5] << 8) | (uint16_t)s_ucWitDataBuff[4]; + usData[2] = ((uint16_t)s_ucWitDataBuff[7] << 8) | (uint16_t)s_ucWitDataBuff[6]; + usData[3] = ((uint16_t)s_ucWitDataBuff[9] << 8) | (uint16_t)s_ucWitDataBuff[8]; + CopeWitData(s_ucWitDataBuff[1], usData, 4); + s_uiWitDataCnt = 0; + } + break; + case WIT_PROTOCOL_MODBUS: + if(s_uiWitDataCnt > 2) + { + if(s_ucWitDataBuff[1] != FuncR) + { + s_uiWitDataCnt--; + memcpy(s_ucWitDataBuff, &s_ucWitDataBuff[1], s_uiWitDataCnt); + return ; + } + if(s_uiWitDataCnt < (s_ucWitDataBuff[2] + 5))return ; + usTemp = ((uint16_t)s_ucWitDataBuff[s_uiWitDataCnt-2] << 8) | s_ucWitDataBuff[s_uiWitDataCnt-1]; + usCRC16 = __CRC16(s_ucWitDataBuff, s_uiWitDataCnt-2); + if(usTemp != usCRC16) + { + s_uiWitDataCnt--; + memcpy(s_ucWitDataBuff, &s_ucWitDataBuff[1], s_uiWitDataCnt); + return ; + } + usTemp = s_ucWitDataBuff[2] >> 1; + for(i = 0; i < usTemp; i++) + { + sReg[i+s_uiReadRegIndex] = ((uint16_t)s_ucWitDataBuff[(i<<1)+3] << 8) | s_ucWitDataBuff[(i<<1)+4]; + } + p_WitRegUpdateCbFunc(s_uiReadRegIndex, usTemp); + s_uiWitDataCnt = 0; + } + break; + case WIT_PROTOCOL_CAN: + case WIT_PROTOCOL_I2C: + s_uiWitDataCnt = 0; + break; + } + if(s_uiWitDataCnt == WIT_DATA_BUFF_SIZE)s_uiWitDataCnt = 0; +} +int32_t WitI2cFuncRegister(WitI2cWrite write_func, WitI2cRead read_func) +{ + if(!write_func)return WIT_HAL_INVAL; + if(!read_func)return WIT_HAL_INVAL; + p_WitI2cWriteFunc = write_func; + p_WitI2cReadFunc = read_func; + return WIT_HAL_OK; +} +int32_t WitCanWriteRegister(CanWrite Write_func) +{ + if(!Write_func)return WIT_HAL_INVAL; + p_WitCanWriteFunc = Write_func; + return WIT_HAL_OK; +} +void WitCanDataIn(uint8_t ucData[8], uint8_t ucLen) +{ + uint16_t usData[3]; + if(p_WitRegUpdateCbFunc == NULL)return ; + if(ucLen < 8)return ; + switch(s_uiProtoclo) + { + case WIT_PROTOCOL_CAN: + if(ucData[0] != 0x55)return ; + usData[0] = ((uint16_t)ucData[3] << 8) | ucData[2]; + usData[1] = ((uint16_t)ucData[5] << 8) | ucData[4]; + usData[2] = ((uint16_t)ucData[7] << 8) | ucData[6]; + CopeWitData(ucData[1], usData, 3); + break; + case WIT_PROTOCOL_NORMAL: + case WIT_PROTOCOL_MODBUS: + case WIT_PROTOCOL_I2C: + break; + } +} +int32_t WitRegisterCallBack(RegUpdateCb update_func) +{ + if(!update_func)return WIT_HAL_INVAL; + p_WitRegUpdateCbFunc = update_func; + return WIT_HAL_OK; +} +int32_t WitWriteReg(uint32_t uiReg, uint16_t usData) +{ + uint16_t usCRC; + uint8_t ucBuff[8]; + if(uiReg >= REGSIZE)return WIT_HAL_INVAL; + switch(s_uiProtoclo) + { + case WIT_PROTOCOL_NORMAL: + if(p_WitSerialWriteFunc == NULL)return WIT_HAL_EMPTY; + ucBuff[0] = 0xFF; + ucBuff[1] = 0xAA; + ucBuff[2] = uiReg & 0xFF; + ucBuff[3] = usData & 0xff; + ucBuff[4] = usData >> 8; + p_WitSerialWriteFunc(ucBuff, 5); + break; + case WIT_PROTOCOL_MODBUS: + if(p_WitSerialWriteFunc == NULL)return WIT_HAL_EMPTY; + ucBuff[0] = s_ucAddr; + ucBuff[1] = FuncW; + ucBuff[2] = uiReg >> 8; + ucBuff[3] = uiReg & 0xFF; + ucBuff[4] = usData >> 8; + ucBuff[5] = usData & 0xff; + usCRC = __CRC16(ucBuff, 6); + ucBuff[6] = usCRC >> 8; + ucBuff[7] = usCRC & 0xff; + p_WitSerialWriteFunc(ucBuff, 8); + break; + case WIT_PROTOCOL_CAN: + if(p_WitCanWriteFunc == NULL)return WIT_HAL_EMPTY; + ucBuff[0] = 0xFF; + ucBuff[1] = 0xAA; + ucBuff[2] = uiReg & 0xFF; + ucBuff[3] = usData & 0xff; + ucBuff[4] = usData >> 8; + p_WitCanWriteFunc(s_ucAddr, ucBuff, 5); + break; + case WIT_PROTOCOL_I2C: + if(p_WitI2cWriteFunc == NULL)return WIT_HAL_EMPTY; + ucBuff[0] = usData & 0xff; + ucBuff[1] = usData >> 8; + if(p_WitI2cWriteFunc(s_ucAddr << 1, uiReg, ucBuff, 2) != 1) + { + //printf("i2c write fail\r\n"); + } + break; + default: + return WIT_HAL_INVAL; + } + return WIT_HAL_OK; +} +int32_t WitReadReg(uint32_t uiReg, uint32_t uiReadNum) +{ + uint16_t usTemp, i; + uint8_t ucBuff[8]; + if((uiReg + uiReadNum) >= REGSIZE)return WIT_HAL_INVAL; + switch(s_uiProtoclo) + { + case WIT_PROTOCOL_NORMAL: + if(uiReadNum > 4)return WIT_HAL_INVAL; + if(p_WitSerialWriteFunc == NULL)return WIT_HAL_EMPTY; + ucBuff[0] = 0xFF; + ucBuff[1] = 0xAA; + ucBuff[2] = 0x27; + ucBuff[3] = uiReg & 0xff; + ucBuff[4] = uiReg >> 8; + p_WitSerialWriteFunc(ucBuff, 5); + break; + case WIT_PROTOCOL_MODBUS: + if(p_WitSerialWriteFunc == NULL)return WIT_HAL_EMPTY; + usTemp = uiReadNum << 1; + if((usTemp + 5) > WIT_DATA_BUFF_SIZE)return WIT_HAL_NOMEM; + ucBuff[0] = s_ucAddr; + ucBuff[1] = FuncR; + ucBuff[2] = uiReg >> 8; + ucBuff[3] = uiReg & 0xFF; + ucBuff[4] = uiReadNum >> 8; + ucBuff[5] = uiReadNum & 0xff; + usTemp = __CRC16(ucBuff, 6); + ucBuff[6] = usTemp >> 8; + ucBuff[7] = usTemp & 0xff; + p_WitSerialWriteFunc(ucBuff, 8); + break; + case WIT_PROTOCOL_CAN: + if(uiReadNum > 3)return WIT_HAL_INVAL; + if(p_WitCanWriteFunc == NULL)return WIT_HAL_EMPTY; + ucBuff[0] = 0xFF; + ucBuff[1] = 0xAA; + ucBuff[2] = 0x27; + ucBuff[3] = uiReg & 0xff; + ucBuff[4] = uiReg >> 8; + p_WitCanWriteFunc(s_ucAddr, ucBuff, 5); + break; + case WIT_PROTOCOL_I2C: + if(p_WitI2cReadFunc == NULL)return WIT_HAL_EMPTY; + usTemp = uiReadNum << 1; + if(WIT_DATA_BUFF_SIZE < usTemp)return WIT_HAL_NOMEM; + if(p_WitI2cReadFunc(s_ucAddr << 1, uiReg, s_ucWitDataBuff, usTemp) == 1) + { + if(p_WitRegUpdateCbFunc == NULL)return WIT_HAL_EMPTY; + for(i = 0; i < uiReadNum; i++) + { + sReg[i+uiReg] = ((uint16_t)s_ucWitDataBuff[(i<<1)+1] << 8) | s_ucWitDataBuff[i<<1]; + } + p_WitRegUpdateCbFunc(uiReg, uiReadNum); + } + + break; + default: + return WIT_HAL_INVAL; + } + s_uiReadRegIndex = uiReg; + + return WIT_HAL_OK; +} +int32_t WitInit(uint32_t uiProtocol, uint8_t ucAddr) +{ + if(uiProtocol > WIT_PROTOCOL_I2C)return WIT_HAL_INVAL; + s_uiProtoclo = uiProtocol; + s_ucAddr = ucAddr; + s_uiWitDataCnt = 0; + return WIT_HAL_OK; +} +void WitDeInit(void) +{ + p_WitSerialWriteFunc = NULL; + p_WitI2cWriteFunc = NULL; + p_WitI2cReadFunc = NULL; + p_WitCanWriteFunc = NULL; + p_WitRegUpdateCbFunc = NULL; + s_ucAddr = 0xff; + s_uiWitDataCnt = 0; + s_uiProtoclo = 0; +} + +int32_t WitDelayMsRegister(DelaymsCb delayms_func) +{ + if(!delayms_func)return WIT_HAL_INVAL; + p_WitDelaymsFunc = delayms_func; + return WIT_HAL_OK; +} + +char CheckRange(short sTemp,short sMin,short sMax) +{ + if ((sTemp>=sMin)&&(sTemp<=sMax)) return 1; + else return 0; +} +/*Acceleration calibration demo*/ +int32_t WitStartAccCali(void) +{ +/* + First place the equipment horizontally, and then perform the following operations +*/ + if(WitWriteReg(KEY, KEY_UNLOCK) != WIT_HAL_OK) return WIT_HAL_ERROR;// unlock reg + if(s_uiProtoclo == WIT_PROTOCOL_MODBUS) p_WitDelaymsFunc(20); + else if(s_uiProtoclo == WIT_PROTOCOL_NORMAL) p_WitDelaymsFunc(1); + else ; + if(WitWriteReg(CALSW, CALGYROACC) != WIT_HAL_OK) return WIT_HAL_ERROR; + return WIT_HAL_OK; +} +int32_t WitStopAccCali(void) +{ + if(WitWriteReg(CALSW, NORMAL) != WIT_HAL_OK) return WIT_HAL_ERROR; + if(s_uiProtoclo == WIT_PROTOCOL_MODBUS) p_WitDelaymsFunc(20); + else if(s_uiProtoclo == WIT_PROTOCOL_NORMAL) p_WitDelaymsFunc(1); + else ; + if(WitWriteReg(SAVE, SAVE_PARAM) != WIT_HAL_OK) return WIT_HAL_ERROR; + return WIT_HAL_OK; +} +/*Magnetic field calibration*/ +int32_t WitStartMagCali(void) +{ + if(WitWriteReg(KEY, KEY_UNLOCK) != WIT_HAL_OK) return WIT_HAL_ERROR; + if(s_uiProtoclo == WIT_PROTOCOL_MODBUS) p_WitDelaymsFunc(20); + else if(s_uiProtoclo == WIT_PROTOCOL_NORMAL) p_WitDelaymsFunc(1); + else ; + if(WitWriteReg(CALSW, CALMAGMM) != WIT_HAL_OK) return WIT_HAL_ERROR; + return WIT_HAL_OK; +} +int32_t WitStopMagCali(void) +{ + if(WitWriteReg(KEY, KEY_UNLOCK) != WIT_HAL_OK) return WIT_HAL_ERROR; + if(s_uiProtoclo == WIT_PROTOCOL_MODBUS) p_WitDelaymsFunc(20); + else if(s_uiProtoclo == WIT_PROTOCOL_NORMAL) p_WitDelaymsFunc(1); + else ; + if(WitWriteReg(CALSW, NORMAL) != WIT_HAL_OK) return WIT_HAL_ERROR; + return WIT_HAL_OK; +} +/*change Band*/ +int32_t WitSetUartBaud(int32_t uiBaudIndex) +{ + if(!CheckRange(uiBaudIndex,WIT_BAUD_4800,WIT_BAUD_230400)) + { + return WIT_HAL_INVAL; + } + if(WitWriteReg(KEY, KEY_UNLOCK) != WIT_HAL_OK) return WIT_HAL_ERROR; + if(s_uiProtoclo == WIT_PROTOCOL_MODBUS) p_WitDelaymsFunc(20); + else if(s_uiProtoclo == WIT_PROTOCOL_NORMAL) p_WitDelaymsFunc(1); + else ; + if(WitWriteReg(BAUD, uiBaudIndex) != WIT_HAL_OK) return WIT_HAL_ERROR; + return WIT_HAL_OK; +} +/*change Can Band*/ +int32_t WitSetCanBaud(int32_t uiBaudIndex) +{ + if(!CheckRange(uiBaudIndex,CAN_BAUD_1000000,CAN_BAUD_3000)) + { + return WIT_HAL_INVAL; + } + if(WitWriteReg(KEY, KEY_UNLOCK) != WIT_HAL_OK) return WIT_HAL_ERROR; + if(s_uiProtoclo == WIT_PROTOCOL_MODBUS) p_WitDelaymsFunc(20); + else if(s_uiProtoclo == WIT_PROTOCOL_NORMAL) p_WitDelaymsFunc(1); + else ; + if(WitWriteReg(BAUD, uiBaudIndex) != WIT_HAL_OK) return WIT_HAL_ERROR; + return WIT_HAL_OK; +} +/*change Bandwidth*/ +int32_t WitSetBandwidth(int32_t uiBaudWidth) +{ + if(!CheckRange(uiBaudWidth,BANDWIDTH_256HZ,BANDWIDTH_5HZ)) + { + return WIT_HAL_INVAL; + } + if(WitWriteReg(KEY, KEY_UNLOCK) != WIT_HAL_OK) return WIT_HAL_ERROR; + if(s_uiProtoclo == WIT_PROTOCOL_MODBUS) p_WitDelaymsFunc(20); + else if(s_uiProtoclo == WIT_PROTOCOL_NORMAL) p_WitDelaymsFunc(1); + else ; + if(WitWriteReg(BANDWIDTH, uiBaudWidth) != WIT_HAL_OK) return WIT_HAL_ERROR; + return WIT_HAL_OK; +} + +/*change output rate */ +int32_t WitSetOutputRate(int32_t uiRate) +{ + if(!CheckRange(uiRate,RRATE_02HZ,RRATE_NONE)) + { + return WIT_HAL_INVAL; + } + if(WitWriteReg(KEY, KEY_UNLOCK) != WIT_HAL_OK) return WIT_HAL_ERROR; + if(s_uiProtoclo == WIT_PROTOCOL_MODBUS) p_WitDelaymsFunc(20); + else if(s_uiProtoclo == WIT_PROTOCOL_NORMAL) p_WitDelaymsFunc(1); + else ; + if(WitWriteReg(RRATE, uiRate) != WIT_HAL_OK) return WIT_HAL_ERROR; + return WIT_HAL_OK; +} + +/*change WitSetContent */ +int32_t WitSetContent(int32_t uiRsw) +{ + if(!CheckRange(uiRsw,RSW_TIME,RSW_MASK)) + { + return WIT_HAL_INVAL; + } + if(WitWriteReg(KEY, KEY_UNLOCK) != WIT_HAL_OK) return WIT_HAL_ERROR; + if(s_uiProtoclo == WIT_PROTOCOL_MODBUS) p_WitDelaymsFunc(20); + else if(s_uiProtoclo == WIT_PROTOCOL_NORMAL) p_WitDelaymsFunc(1); + else ; + if(WitWriteReg(RSW, uiRsw) != WIT_HAL_OK) return WIT_HAL_ERROR; + return WIT_HAL_OK; +} + + + diff --git a/modules/sensor/imu/j901p.h b/3rdparty/jy901p/wit_c_sdk.h similarity index 99% rename from modules/sensor/imu/j901p.h rename to 3rdparty/jy901p/wit_c_sdk.h index df1ed8d..9455ff6 100644 --- a/modules/sensor/imu/j901p.h +++ b/3rdparty/jy901p/wit_c_sdk.h @@ -8,7 +8,7 @@ extern "C" { #include #include #include -#include "j901p_reg.h" +#include "REG.h" #define WIT_HAL_OK (0) /**< There is no error */ diff --git a/CloudPlant.ioc b/CloudPlant.ioc index 669c9ed..f10ec4b 100644 --- a/CloudPlant.ioc +++ b/CloudPlant.ioc @@ -42,19 +42,24 @@ Mcu.IP7=USART1 Mcu.IPNb=8 Mcu.Name=STM32F407Z(E-G)Tx Mcu.Package=LQFP144 -Mcu.Pin0=PF0 -Mcu.Pin1=PF1 -Mcu.Pin10=VP_FREERTOS_VS_CMSIS_V2 -Mcu.Pin11=VP_SYS_VS_tim1 -Mcu.Pin2=PH0-OSC_IN -Mcu.Pin3=PH1-OSC_OUT -Mcu.Pin4=PB12 -Mcu.Pin5=PB13 -Mcu.Pin6=PA9 -Mcu.Pin7=PA10 -Mcu.Pin8=PD0 -Mcu.Pin9=PD1 -Mcu.PinsNb=12 +Mcu.Pin0=PE3 +Mcu.Pin1=PE4 +Mcu.Pin10=PA9 +Mcu.Pin11=PA10 +Mcu.Pin12=PD0 +Mcu.Pin13=PD1 +Mcu.Pin14=PG9 +Mcu.Pin15=VP_FREERTOS_VS_CMSIS_V2 +Mcu.Pin16=VP_SYS_VS_tim1 +Mcu.Pin2=PF0 +Mcu.Pin3=PF1 +Mcu.Pin4=PH0-OSC_IN +Mcu.Pin5=PH1-OSC_OUT +Mcu.Pin6=PF12 +Mcu.Pin7=PF14 +Mcu.Pin8=PB12 +Mcu.Pin9=PB13 +Mcu.PinsNb=17 Mcu.ThirdPartyNb=0 Mcu.UserConstants= Mcu.UserName=STM32F407ZGTx @@ -92,10 +97,38 @@ PD0.Signal=CAN1_RX PD1.Locked=true PD1.Mode=CAN_Activate PD1.Signal=CAN1_TX +PE3.GPIOParameters=GPIO_Label +PE3.GPIO_Label=LED0 +PE3.Locked=true +PE3.Signal=GPIO_Output +PE4.GPIOParameters=GPIO_Label +PE4.GPIO_Label=LED1 +PE4.Locked=true +PE4.Signal=GPIO_Output PF0.Mode=I2C PF0.Signal=I2C2_SDA PF1.Mode=I2C PF1.Signal=I2C2_SCL +PF12.GPIOParameters=GPIO_Speed,PinState,GPIO_PuPd,GPIO_Label,GPIO_ModeDefaultOutputPP +PF12.GPIO_Label=SDA +PF12.GPIO_ModeDefaultOutputPP=GPIO_MODE_OUTPUT_PP +PF12.GPIO_PuPd=GPIO_NOPULL +PF12.GPIO_Speed=GPIO_SPEED_FREQ_VERY_HIGH +PF12.Locked=true +PF12.PinState=GPIO_PIN_SET +PF12.Signal=GPIO_Output +PF14.GPIOParameters=GPIO_Speed,PinState,GPIO_PuPd,GPIO_Label,GPIO_ModeDefaultOutputPP +PF14.GPIO_Label=SCL +PF14.GPIO_ModeDefaultOutputPP=GPIO_MODE_OUTPUT_PP +PF14.GPIO_PuPd=GPIO_NOPULL +PF14.GPIO_Speed=GPIO_SPEED_FREQ_VERY_HIGH +PF14.Locked=true +PF14.PinState=GPIO_PIN_SET +PF14.Signal=GPIO_Output +PG9.GPIOParameters=GPIO_Label +PG9.GPIO_Label=LED2 +PG9.Locked=true +PG9.Signal=GPIO_Output PH0-OSC_IN.Mode=HSE-External-Oscillator PH0-OSC_IN.Signal=RCC_OSC_IN PH1-OSC_OUT.Mode=HSE-External-Oscillator @@ -134,7 +167,7 @@ ProjectManager.ToolChainLocation= ProjectManager.UAScriptAfterPath= ProjectManager.UAScriptBeforePath= ProjectManager.UnderRoot=false -ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_I2C2_Init-I2C2-false-HAL-true,4-MX_USART1_UART_Init-USART1-false-HAL-true,5-MX_CAN2_Init-CAN2-false-HAL-true +ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_I2C2_Init-I2C2-false-HAL-true,4-MX_USART1_UART_Init-USART1-false-HAL-true,5-MX_CAN2_Init-CAN2-false-HAL-true,6-MX_CAN1_Init-CAN1-false-HAL-true RCC.48MHZClocksFreq_Value=84000000 RCC.AHBFreq_Value=168000000 RCC.APB1CLKDivider=RCC_HCLK_DIV4 diff --git a/Core/Inc/main.h b/Core/Inc/main.h index 06878af..34f76ff 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -57,6 +57,16 @@ void Error_Handler(void); /* USER CODE END EFP */ /* Private defines -----------------------------------------------------------*/ +#define LED0_Pin GPIO_PIN_3 +#define LED0_GPIO_Port GPIOE +#define LED1_Pin GPIO_PIN_4 +#define LED1_GPIO_Port GPIOE +#define SDA_Pin GPIO_PIN_12 +#define SDA_GPIO_Port GPIOF +#define SCL_Pin GPIO_PIN_14 +#define SCL_GPIO_Port GPIOF +#define LED2_Pin GPIO_PIN_9 +#define LED2_GPIO_Port GPIOG /* USER CODE BEGIN Private defines */ diff --git a/Core/Src/gpio.c b/Core/Src/gpio.c index efa60d4..fb60db0 100644 --- a/Core/Src/gpio.c +++ b/Core/Src/gpio.c @@ -42,12 +42,46 @@ void MX_GPIO_Init(void) { + GPIO_InitTypeDef GPIO_InitStruct = {0}; + /* GPIO Ports Clock Enable */ + __HAL_RCC_GPIOE_CLK_ENABLE(); __HAL_RCC_GPIOF_CLK_ENABLE(); __HAL_RCC_GPIOH_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOD_CLK_ENABLE(); + __HAL_RCC_GPIOG_CLK_ENABLE(); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(GPIOE, LED0_Pin|LED1_Pin, GPIO_PIN_RESET); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(GPIOF, SDA_Pin|SCL_Pin, GPIO_PIN_SET); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_RESET); + + /*Configure GPIO pins : LED0_Pin LED1_Pin */ + GPIO_InitStruct.Pin = LED0_Pin|LED1_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); + + /*Configure GPIO pins : SDA_Pin SCL_Pin */ + GPIO_InitStruct.Pin = SDA_Pin|SCL_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); + + /*Configure GPIO pin : LED2_Pin */ + GPIO_InitStruct.Pin = LED2_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(LED2_GPIO_Port, &GPIO_InitStruct); } diff --git a/Core/Src/stm32f4xx_it.c b/Core/Src/stm32f4xx_it.c index 119c5d2..7e573c0 100644 --- a/Core/Src/stm32f4xx_it.c +++ b/Core/Src/stm32f4xx_it.c @@ -190,4 +190,6 @@ void I2C2_EV_IRQHandler(void) /* USER CODE BEGIN 1 */ + + /* USER CODE END 1 */ diff --git a/TangYuJieBoard.ioc b/TangYuJieBoard.ioc new file mode 100644 index 0000000..669c9ed --- /dev/null +++ b/TangYuJieBoard.ioc @@ -0,0 +1,179 @@ +#MicroXplorer Configuration settings - do not modify +CAD.formats= +CAD.pinconfig= +CAD.provider= +CAN1.BS1=CAN_BS1_2TQ +CAN1.BS2=CAN_BS2_4TQ +CAN1.CalculateBaudRate=1000000 +CAN1.CalculateTimeBit=1000 +CAN1.CalculateTimeQuantum=142.85714285714286 +CAN1.IPParameters=CalculateTimeQuantum,CalculateTimeBit,CalculateBaudRate,BS1,BS2,Prescaler +CAN1.Prescaler=6 +CAN2.BS1=CAN_BS1_2TQ +CAN2.BS2=CAN_BS2_4TQ +CAN2.CalculateBaudRate=1000000 +CAN2.CalculateTimeBit=1000 +CAN2.CalculateTimeQuantum=142.85714285714286 +CAN2.IPParameters=CalculateTimeQuantum,CalculateTimeBit,CalculateBaudRate,Prescaler,BS2,BS1,NART +CAN2.NART=ENABLE +CAN2.Prescaler=6 +FREERTOS.FootprintOK=true +FREERTOS.IPParameters=Tasks01,configENABLE_FPU,configUSE_NEWLIB_REENTRANT,configMINIMAL_STACK_SIZE,configTOTAL_HEAP_SIZE,FootprintOK +FREERTOS.Tasks01=monitorTask,8,256,StartMonitorTask,Default,NULL,Dynamic,NULL,NULL;controlTask,24,256,StartControlTask,Default,NULL,Dynamic,NULL,NULL;sensorTask,40,256,StartSensorTask,Default,NULL,Dynamic,NULL,NULL +FREERTOS.configENABLE_FPU=1 +FREERTOS.configMINIMAL_STACK_SIZE=256 +FREERTOS.configTOTAL_HEAP_SIZE=32768 +FREERTOS.configUSE_NEWLIB_REENTRANT=1 +File.Version=6 +GPIO.groupedBy= +I2C2.I2C_Mode=I2C_Fast +I2C2.IPParameters=I2C_Mode +KeepUserPlacement=false +Mcu.CPN=STM32F407ZGT6 +Mcu.Family=STM32F4 +Mcu.IP0=CAN1 +Mcu.IP1=CAN2 +Mcu.IP2=FREERTOS +Mcu.IP3=I2C2 +Mcu.IP4=NVIC +Mcu.IP5=RCC +Mcu.IP6=SYS +Mcu.IP7=USART1 +Mcu.IPNb=8 +Mcu.Name=STM32F407Z(E-G)Tx +Mcu.Package=LQFP144 +Mcu.Pin0=PF0 +Mcu.Pin1=PF1 +Mcu.Pin10=VP_FREERTOS_VS_CMSIS_V2 +Mcu.Pin11=VP_SYS_VS_tim1 +Mcu.Pin2=PH0-OSC_IN +Mcu.Pin3=PH1-OSC_OUT +Mcu.Pin4=PB12 +Mcu.Pin5=PB13 +Mcu.Pin6=PA9 +Mcu.Pin7=PA10 +Mcu.Pin8=PD0 +Mcu.Pin9=PD1 +Mcu.PinsNb=12 +Mcu.ThirdPartyNb=0 +Mcu.UserConstants= +Mcu.UserName=STM32F407ZGTx +MxCube.Version=6.14.0 +MxDb.Version=DB.6.0.140 +NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false +NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false +NVIC.ForceEnableDMAVector=true +NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false +NVIC.I2C2_EV_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true +NVIC.MemoryManagement_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false +NVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false +NVIC.PendSV_IRQn=true\:15\:0\:false\:false\:false\:true\:false\:false\:false +NVIC.PriorityGroup=NVIC_PRIORITYGROUP_4 +NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:false\:false\:false\:false\:false +NVIC.SavedPendsvIrqHandlerGenerated=true +NVIC.SavedSvcallIrqHandlerGenerated=true +NVIC.SavedSystickIrqHandlerGenerated=true +NVIC.SysTick_IRQn=true\:15\:0\:false\:false\:false\:true\:false\:true\:false +NVIC.TIM1_UP_TIM10_IRQn=true\:15\:0\:false\:false\:true\:false\:false\:true\:true +NVIC.TimeBase=TIM1_UP_TIM10_IRQn +NVIC.TimeBaseIP=TIM1 +NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false +PA10.Mode=Asynchronous +PA10.Signal=USART1_RX +PA9.Mode=Asynchronous +PA9.Signal=USART1_TX +PB12.Mode=CAN_Activate +PB12.Signal=CAN2_RX +PB13.Mode=CAN_Activate +PB13.Signal=CAN2_TX +PD0.Locked=true +PD0.Mode=CAN_Activate +PD0.Signal=CAN1_RX +PD1.Locked=true +PD1.Mode=CAN_Activate +PD1.Signal=CAN1_TX +PF0.Mode=I2C +PF0.Signal=I2C2_SDA +PF1.Mode=I2C +PF1.Signal=I2C2_SCL +PH0-OSC_IN.Mode=HSE-External-Oscillator +PH0-OSC_IN.Signal=RCC_OSC_IN +PH1-OSC_OUT.Mode=HSE-External-Oscillator +PH1-OSC_OUT.Signal=RCC_OSC_OUT +PinOutPanel.RotationAngle=0 +ProjectManager.AskForMigrate=true +ProjectManager.BackupPrevious=true +ProjectManager.CompilerLinker=GCC +ProjectManager.CompilerOptimize=6 +ProjectManager.ComputerToolchain=false +ProjectManager.CoupleFile=true +ProjectManager.CustomerFirmwarePackage= +ProjectManager.DefaultFWLocation=true +ProjectManager.DeletePrevious=true +ProjectManager.DeviceId=STM32F407ZGTx +ProjectManager.FirmwarePackage=STM32Cube FW_F4 V1.28.3 +ProjectManager.FreePins=false +ProjectManager.HalAssertFull=false +ProjectManager.HeapSize=0x200 +ProjectManager.KeepUserCode=true +ProjectManager.LastFirmware=true +ProjectManager.LibraryCopy=0 +ProjectManager.MainLocation=Core/Src +ProjectManager.MultiThreaded=true +ProjectManager.NoMain=false +ProjectManager.PreviousToolchain= +ProjectManager.ProjectBuild=false +ProjectManager.ProjectFileName=CloudPlant.ioc +ProjectManager.ProjectName=CloudPlant +ProjectManager.ProjectStructure= +ProjectManager.RegisterCallBack= +ProjectManager.StackSize=0x400 +ProjectManager.TargetToolchain=CMake +ProjectManager.ThreadSafeStrategy=Cortex-M4NS\:GenericStrategy2, +ProjectManager.ToolChainLocation= +ProjectManager.UAScriptAfterPath= +ProjectManager.UAScriptBeforePath= +ProjectManager.UnderRoot=false +ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_I2C2_Init-I2C2-false-HAL-true,4-MX_USART1_UART_Init-USART1-false-HAL-true,5-MX_CAN2_Init-CAN2-false-HAL-true +RCC.48MHZClocksFreq_Value=84000000 +RCC.AHBFreq_Value=168000000 +RCC.APB1CLKDivider=RCC_HCLK_DIV4 +RCC.APB1Freq_Value=42000000 +RCC.APB1TimFreq_Value=84000000 +RCC.APB2CLKDivider=RCC_HCLK_DIV2 +RCC.APB2Freq_Value=84000000 +RCC.APB2TimFreq_Value=168000000 +RCC.CortexFreq_Value=168000000 +RCC.EthernetFreq_Value=168000000 +RCC.FCLKCortexFreq_Value=168000000 +RCC.FamilyName=M +RCC.HCLKFreq_Value=168000000 +RCC.HSE_VALUE=8000000 +RCC.HSICalibrationValue=16 +RCC.HSI_VALUE=16000000 +RCC.I2SClocksFreq_Value=192000000 +RCC.IPParameters=48MHZClocksFreq_Value,AHBFreq_Value,APB1CLKDivider,APB1Freq_Value,APB1TimFreq_Value,APB2CLKDivider,APB2Freq_Value,APB2TimFreq_Value,CortexFreq_Value,EthernetFreq_Value,FCLKCortexFreq_Value,FamilyName,HCLKFreq_Value,HSE_VALUE,HSICalibrationValue,HSI_VALUE,I2SClocksFreq_Value,LSE_VALUE,LSI_VALUE,MCO2PinFreq_Value,PLLCLKFreq_Value,PLLM,PLLN,PLLQCLKFreq_Value,PLLSourceVirtual,RTCFreq_Value,RTCHSEDivFreq_Value,SYSCLKFreq_VALUE,SYSCLKSource,VCOI2SOutputFreq_Value,VCOInputFreq_Value,VCOOutputFreq_Value,VcooutputI2S +RCC.LSE_VALUE=32768 +RCC.LSI_VALUE=32000 +RCC.MCO2PinFreq_Value=168000000 +RCC.PLLCLKFreq_Value=168000000 +RCC.PLLM=4 +RCC.PLLN=168 +RCC.PLLQCLKFreq_Value=84000000 +RCC.PLLSourceVirtual=RCC_PLLSOURCE_HSE +RCC.RTCFreq_Value=32000 +RCC.RTCHSEDivFreq_Value=4000000 +RCC.SYSCLKFreq_VALUE=168000000 +RCC.SYSCLKSource=RCC_SYSCLKSOURCE_PLLCLK +RCC.VCOI2SOutputFreq_Value=384000000 +RCC.VCOInputFreq_Value=2000000 +RCC.VCOOutputFreq_Value=336000000 +RCC.VcooutputI2S=192000000 +USART1.IPParameters=VirtualMode +USART1.VirtualMode=VM_ASYNC +VP_FREERTOS_VS_CMSIS_V2.Mode=CMSIS_V2 +VP_FREERTOS_VS_CMSIS_V2.Signal=FREERTOS_VS_CMSIS_V2 +VP_SYS_VS_tim1.Mode=TIM1 +VP_SYS_VS_tim1.Signal=SYS_VS_tim1 +board=custom +rtos.0.ip=FREERTOS diff --git a/app/app.c b/app/app.c deleted file mode 100644 index 47fbfb4..0000000 --- a/app/app.c +++ /dev/null @@ -1,102 +0,0 @@ -#include "led.h" -#include "mf4010v2.h" -#include "j901p_module.h" -#include "hal_i2c.h" -#include "hal_can.h" -#include "freertos.h" -#include "pid.h" -#include -#include -#include "cmsis_os.h" - -static led_t led0; -static mf4010v2_t motor1; // Pitch 轴电机 -static mf4010v2_t motor2; // Roll 轴电机 -static PIDController_t pid_pitch; -static PIDController_t pid_roll; - -// 云台自稳 PID 参数 (需要根据实际机械结构调优) -// 比例增益:角度误差对应的速度指令 -#define PITCH_Kp 50.0f // Pitch 轴比例增益 -#define PITCH_Ki 0.5f // Pitch 轴积分增益 -#define PITCH_Kd 10.0f // Pitch 轴微分增益 - -#define ROLL_Kp 50.0f // Roll 轴比例增益 -#define ROLL_Ki 0.5f // Roll 轴积分增益 -#define ROLL_Kd 10.0f // Roll 轴微分增益 - -// 最大速度限制 (0.01 DPS) -#define MAX_SPEED 30000 // 300 DPS 最大速度 - -void app_init(void) -{ - // 初始化硬件抽象层 - hal_i2c_init_all(); - hal_can_init(0); - - // 初始化外设 - led_init(&led0, 0); - mf4010v2_init(&motor1, 0x141, 0); // Pitch 轴电机 ID: 0x141, CAN1 - mf4010v2_init(&motor2, 0x142, 0); // Roll 轴电机 ID: 0x142, CAN1 - - // 初始化 IMU - j901p_module_init(&g_imu, 1, 0x50); - - // 初始化 PID 控制器 - PIDController_Init(&pid_pitch); - pid_pitch.Kp = PITCH_Kp; - pid_pitch.Ki = PITCH_Ki; - pid_pitch.Kd = PITCH_Kd; - pid_pitch.output_max = MAX_SPEED; - - PIDController_Init(&pid_roll); - pid_roll.Kp = ROLL_Kp; - pid_roll.Ki = ROLL_Ki; - pid_roll.Kd = ROLL_Kd; - pid_roll.output_max = MAX_SPEED; - - // 启动电机 - mf4010v2_run(&motor1); - mf4010v2_run(&motor2); -} - -void sensor_task(void) -{ - while (1) { - j901p_module_read(&g_imu); - vTaskDelay(pdMS_TO_TICKS(10)); // 100Hz 更新率 - } -} - -void control_task(void) -{ - float target_pitch = 0.0f; // 目标 Pitch 角 = 0 度(水平) - float target_roll = 0.0f; // 目标 Roll 角 = 0 度(水平) - while (1) { - // 云台自稳控制:目标是使 Pitch 和 Roll 保持 0 度(水平) - - - // 从 IMU 读取当前角度 (0.01 度单位) - float current_pitch = g_imu.angle[1] / 100.0f; // Pitch = angle[1] - float current_roll = g_imu.angle[0] / 100.0f; // Roll = angle[0] - - // 计算 PID 输出 (速度指令,0.01 DPS 单位) - float pitch_speed = PIDController_Compute(&pid_pitch, target_pitch, current_pitch); - float roll_speed = PIDController_Compute(&pid_roll, target_roll, current_roll); - - // 发送速度指令给电机 - mf4010v2_set_speed(&motor1, (int32_t)pitch_speed); - mf4010v2_set_speed(&motor2, (int32_t)roll_speed); - - vTaskDelay(pdMS_TO_TICKS(10)); // 100Hz 控制循环 - } -} - -void monitor_task(void) -{ - while (1) { - // 简单的监控任务:每秒打印一次当前状态 - printf("Pitch: %.2f deg, Roll: %.2f deg\n", g_imu.angle[1] / 100.0f, g_imu.angle[0] / 100.0f); - vTaskDelay(pdMS_TO_TICKS(1000)); // 1Hz 更新率 - } -} \ No newline at end of file diff --git a/app/app.h b/app/app.h index 7c432ba..81e3c5e 100644 --- a/app/app.h +++ b/app/app.h @@ -1,6 +1,19 @@ #ifndef __APP_H__ #define __APP_H__ - +#include "hal_gpio.h" +#include "led.h" +#include "mf4010v2.h" +#include "hal_i2c.h" +#include "hal_can.h" +#include "hal_led.h" +#include "freertos.h" +#include "pid.h" +#include +#include +#include "cmsis_os.h" +#include "projdefs.h" +#include "wit_c_sdk.h" +#include "soft_i2c.h" void app_init(void); void sensor_task(void); void control_task(void); diff --git a/app/app_init.c b/app/app_init.c new file mode 100644 index 0000000..39ec6ef --- /dev/null +++ b/app/app_init.c @@ -0,0 +1,48 @@ +#include "app.h" + + +static led_t led0, led1, led2; + + +soft_i2c_t i2c; + + + + +void app_init(void) +{ + // 初始化硬件抽象层 + hal_i2c_init_all(); + hal_led_init_all(); + hal_can_init_all(); + hal_gpio_init_all(); + // 初始化外设 + led_init(&led0, 0); + led_init(&led1, 1); + led_init(&led2, 2); + led_on(&led0); + led_on(&led1); + led_on(&led2); + soft_i2c_config_t config = { + .sda_gpio_ch = 0, /* 根据实际硬件修改 */ + .scl_gpio_ch = 1, + .delay_us = 5, /* 100kHz */ + .open_drain = true + }; + soft_i2c_init(&i2c, &config); + + printf("Hardware initialized\r\n"); +} + + + + + +void monitor_task(void) +{ + while (1) { + // 简单的监控任务:每秒打印一次当前状态 + // printf("Pitch: %.2f deg, Roll: %.2f deg\n", g_imu.angle[1] / 100.0f, g_imu.angle[0] / 100.0f); + vTaskDelay(pdMS_TO_TICKS(1000)); // 1Hz 更新率 + } +} \ No newline at end of file diff --git a/app/sensor_task.c b/app/sensor_task.c new file mode 100644 index 0000000..5b1bb23 --- /dev/null +++ b/app/sensor_task.c @@ -0,0 +1,133 @@ +#include +#include +#include "cmsis_os2.h" +#include "sensor_i2c_port.h" + +#define ACC_UPDATE 0x01 +#define GYRO_UPDATE 0x02 +#define ANGLE_UPDATE 0x04 +#define MAG_UPDATE 0x08 +#define READ_UPDATE 0x80 +static volatile char s_cDataUpdate = 0, s_cCmd = 0xff; + +static void CopeSensorData(uint32_t uiReg, uint32_t uiRegNum) +{ + int i; + for(i = 0; i < uiRegNum; i++) + { + switch(uiReg) + { +// case AX: +// case AY: + case AZ: + s_cDataUpdate |= ACC_UPDATE; + break; +// case GX: +// case GY: + case GZ: + s_cDataUpdate |= GYRO_UPDATE; + break; +// case HX: +// case HY: + case HZ: + s_cDataUpdate |= MAG_UPDATE; + break; +// case Roll: +// case Pitch: + case Yaw: + s_cDataUpdate |= ANGLE_UPDATE; + break; + default: + s_cDataUpdate |= READ_UPDATE; + break; + } + uiReg++; + } +} +static void ShowHelp(void) +{ + printf("\r\n************************ WIT_SDK_DEMO ************************"); + printf("\r\n************************ HELP ************************\r\n"); + printf("UART SEND:a\\r\\n Acceleration calibration.\r\n"); + printf("UART SEND:m\\r\\n Magnetic field calibration,After calibration send: e\\r\\n to indicate the end\r\n"); + printf("UART SEND:U\\r\\n Bandwidth increase.\r\n"); + printf("UART SEND:u\\r\\n Bandwidth reduction.\r\n"); + printf("UART SEND:B\\r\\n Baud rate increased to 115200.\r\n"); + printf("UART SEND:b\\r\\n Baud rate reduction to 9600.\r\n"); + printf("UART SEND:h\\r\\n help.\r\n"); + printf("******************************************************************************\r\n"); +} +static void AutoScanSensor(void) +{ + int i, iRetry; + + for(i = 0; i < 0x7F; i++) + { + WitInit(WIT_PROTOCOL_I2C, i); + iRetry = 2; + do + { + s_cDataUpdate = 0; + WitReadReg(AX, 3); + osDelay(5); + if(s_cDataUpdate != 0) + { + printf("find %02X addr sensor\r\n", i); + ShowHelp(); + return ; + } + iRetry--; + }while(iRetry); + } + printf("can not find sensor\r\n"); + printf("please check your connection\r\n"); +} + +static void Delayms(uint16_t ucMs) +{ + osDelay(pdMS_TO_TICKS(ucMs)); +} + +float fAcc[3], fGyro[3], fAngle[3]; +void sensor_task(void) +{ + int i; + WitInit(WIT_PROTOCOL_I2C, 0x50); + WitI2cFuncRegister(IICwriteBytes, IICreadBytes); + WitRegisterCallBack(CopeSensorData); + WitDelayMsRegister(Delayms); + AutoScanSensor(); + while (1) { + WitReadReg(AX, 12); + vTaskDelay(pdMS_TO_TICKS(500)); + if(s_cDataUpdate) + { + for(i = 0; i < 3; i++) + { + fAcc[i] = sReg[AX+i] / 32768.0f * 16.0f; + fGyro[i] = sReg[GX+i] / 32768.0f * 2000.0f; + fAngle[i] = sReg[Roll+i] / 32768.0f * 180.0f; + } + if(s_cDataUpdate & ACC_UPDATE) + { + printf("acc:%.3f %.3f %.3f\r\n", fAcc[0], fAcc[1], fAcc[2]); + s_cDataUpdate &= ~ACC_UPDATE; + } + if(s_cDataUpdate & GYRO_UPDATE) + { + printf("gyro:%.3f %.3f %.3f\r\n", fGyro[0], fGyro[1], fGyro[2]); + s_cDataUpdate &= ~GYRO_UPDATE; + } + if(s_cDataUpdate & ANGLE_UPDATE) + { + printf("angle:%.3f %.3f %.3f\r\n", fAngle[0], fAngle[1], fAngle[2]); + s_cDataUpdate &= ~ANGLE_UPDATE; + } + if(s_cDataUpdate & MAG_UPDATE) + { + printf("mag:%d %d %d\r\n", sReg[HX], sReg[HY], sReg[HZ]); + s_cDataUpdate &= ~MAG_UPDATE; + } + } + } +} \ No newline at end of file diff --git a/bsp/stm32f4/can/bsp_can.c b/bsp/stm32f4/can/bsp_can.c index b0fa42c..cd20c5d 100644 --- a/bsp/stm32f4/can/bsp_can.c +++ b/bsp/stm32f4/can/bsp_can.c @@ -2,14 +2,14 @@ #include "can.h" // CAN 通道句柄数组 -void *g_bsp_can_handle[] = { - &hcan1, - &hcan2 +static bsp_can_t cans[2] = { + { &hcan1 }, + { &hcan2 } }; -void* bsp_can_get_handle(int ch) +bsp_can_t* bsp_can_get_handle(int ch) { - if (ch < 0 || ch >= 2) - return 0; - return g_bsp_can_handle[ch]; + if (ch < 0 || ch >= sizeof(cans)/sizeof(cans[0])) + return NULL; + return &cans[ch]; } diff --git a/bsp/stm32f4/can/bsp_can.h b/bsp/stm32f4/can/bsp_can.h index c66b8bc..ac6fc42 100644 --- a/bsp/stm32f4/can/bsp_can.h +++ b/bsp/stm32f4/can/bsp_can.h @@ -1,15 +1,16 @@ #ifndef __BSP_CAN__H__ #define __BSP_CAN__H__ - +#include "stm32f4xx_hal.h" #ifdef __cplusplus extern "C" { #endif +typedef struct{ + CAN_HandleTypeDef *hcan; +} bsp_can_t; -// CAN 通道句柄引用 -extern void *g_bsp_can_handle[]; // 获取 CAN 句柄 -void* bsp_can_get_handle(int ch); +bsp_can_t* bsp_can_get_handle(int ch); #ifdef __cplusplus } diff --git a/bsp/stm32f4/gpio/bsp_gpio.c b/bsp/stm32f4/gpio/bsp_gpio.c new file mode 100644 index 0000000..7d801d3 --- /dev/null +++ b/bsp/stm32f4/gpio/bsp_gpio.c @@ -0,0 +1,13 @@ +#include "bsp_gpio.h" +#include "gpio.h" +#include "stm32f4xx_hal_def.h" +static bsp_gpio_t gpios[] = { + {GPIOF, SDA_Pin}, + {GPIOF, SCL_Pin} +}; + +bsp_gpio_t* bsp_gpio_get_handle(int ch) { + if (ch < 0 || ch >= (int)(sizeof(gpios) / sizeof(gpios[0]))) + return NULL; + return &gpios[ch]; +} \ No newline at end of file diff --git a/bsp/stm32f4/gpio/bsp_gpio.h b/bsp/stm32f4/gpio/bsp_gpio.h new file mode 100644 index 0000000..36fe43a --- /dev/null +++ b/bsp/stm32f4/gpio/bsp_gpio.h @@ -0,0 +1,12 @@ +#ifndef __BSP_GPIO_H__ +#define __BSP_GPIO_H__ + +#include "main.h" + +typedef struct { + GPIO_TypeDef *port; + uint16_t pin; +} bsp_gpio_t; + +bsp_gpio_t* bsp_gpio_get_handle(int ch); +#endif diff --git a/bsp/stm32f4/i2c/bsp_i2c.c b/bsp/stm32f4/i2c/bsp_i2c.c index 3dfe098..23cb61c 100644 --- a/bsp/stm32f4/i2c/bsp_i2c.c +++ b/bsp/stm32f4/i2c/bsp_i2c.c @@ -1,18 +1,19 @@ #include "bsp_i2c.h" #include "i2c.h" -static void *g_bsp_i2c_handle[] = { -#ifdef I2C1_HANDLE - &hi2c1, -#else - 0, -#endif - &hi2c2 +static bsp_i2c_t i2cs[] = { + { + .i2c = NULL + }, + { + .i2c = &hi2c2 + } }; -void* bsp_i2c_get_handle(int ch) + +bsp_i2c_t* bsp_i2c_get_handle(int ch) { - if (ch < 0 || ch >= 2) + if (ch < 0 || ch >= (int)(sizeof(i2cs)/sizeof(i2cs[0]))) return 0; - return g_bsp_i2c_handle[ch]; + return &i2cs[ch]; } diff --git a/bsp/stm32f4/i2c/bsp_i2c.h b/bsp/stm32f4/i2c/bsp_i2c.h index 1f82729..08d54f1 100644 --- a/bsp/stm32f4/i2c/bsp_i2c.h +++ b/bsp/stm32f4/i2c/bsp_i2c.h @@ -1,11 +1,16 @@ #ifndef __BSP_I2C_H__ #define __BSP_I2C_H__ +#include "i2c.h" +#include "stm32f4xx_hal_def.h" +#include "stm32f4xx_hal_i2c.h" #ifdef __cplusplus extern "C" { #endif - -void* bsp_i2c_get_handle(int ch); +typedef struct { + I2C_HandleTypeDef* i2c; +} bsp_i2c_t; +bsp_i2c_t* bsp_i2c_get_handle(int ch); #ifdef __cplusplus } diff --git a/flash.py b/flash.py new file mode 100644 index 0000000..267bf09 --- /dev/null +++ b/flash.py @@ -0,0 +1,149 @@ +#!/usr/bin/env python3 +""" +STM32 Flash Script using STM32CubeProgrammer CLI +Target: STM32F407xx +""" + +import subprocess +import sys +import os +import argparse +from pathlib import Path + + +SCRIPT_DIR = Path(__file__).parent.resolve() +DEFAULT_ELF = SCRIPT_DIR / "build" / "Debug" / "CloudPlant.elf" +STM_PROGRAMMER_CLI = r"C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeProgrammer\bin\STM32_Programmer_CLI.exe" + + +def convert_elf_to_bin(elf_path): + """Convert ELF to binary""" + bin_path = str(elf_path).replace(".elf", ".bin") + + try: + cmd = ["arm-none-eabi-objcopy", "-O", "binary", str(elf_path), bin_path] + result = subprocess.run(cmd, capture_output=True, text=True) + + if result.returncode != 0: + print(f"Conversion failed: {result.stderr}") + return None + + print(f"Converted ELF to BIN: {bin_path}") + print(f"Binary size: {os.path.getsize(bin_path)} bytes") + return bin_path + except FileNotFoundError: + print("arm-none-eabi-objcopy not found!") + print("Please install GNU Arm Embedded Toolchain or add to PATH") + return None + + +def flash_with_cube(bin_path, port="SWD"): + """Flash using STM32CubeProgrammer CLI""" + if not os.path.exists(STM_PROGRAMMER_CLI): + print(f"STM32CubeProgrammer not found at: {STM_PROGRAMMER_CLI}") + return False + + bin_path_abs = Path(bin_path).resolve() + cmd = [ + STM_PROGRAMMER_CLI, + "-c", + f"port={port}", + "-w", + str(bin_path_abs), + "0x08000000", + "-v", + "-rst", + ] + + print(f"Flashing {bin_path}...") + print(f"Port: {port}") + print(f"Address: 0x08000000") + print("-" * 50) + + result = subprocess.run(cmd, capture_output=True, text=True) + print(result.stdout) + if result.stderr: + print(result.stderr) + + if result.returncode == 0: + print("Flash successful!") + return True + else: + print(f"Flash failed (exit code: {result.returncode})") + return False + + +def erase_with_cube(port="SWD"): + """Erase chip using STM32CubeProgrammer CLI""" + if not os.path.exists(STM_PROGRAMMER_CLI): + print(f"STM32CubeProgrammer not found at: {STM_PROGRAMMER_CLI}") + return False + + cmd = [STM_PROGRAMMER_CLI, "-c", f"port={port}", "-e", "all"] + + print("Erasing chip...") + result = subprocess.run(cmd, capture_output=True, text=True) + print(result.stdout) + + if result.returncode == 0: + print("Erase successful!") + return True + else: + print(f"Erase failed") + return False + + +def main(): + parser = argparse.ArgumentParser( + description="STM32 Flash Script using STM32CubeProgrammer CLI" + ) + parser.add_argument( + "elf_file", + nargs="?", + default=str(DEFAULT_ELF), + help=f"ELF file to flash (default: {DEFAULT_ELF})", + ) + parser.add_argument("--port", default="SWD", help="ST-Link port (default: SWD)") + parser.add_argument( + "--erase-only", action="store_true", help="Only erase chip, do not flash" + ) + + args = parser.parse_args() + + print("=" * 50) + print("STM32 Flash Script (STM32CubeProgrammer CLI)") + print("=" * 50) + print(f"Target: STM32F407xx") + print(f"ELF: {args.elf_file}") + print(f"Programmer: {STM_PROGRAMMER_CLI}") + print("=" * 50) + + if not os.path.exists(args.elf_file): + print(f"ELF file not found: {args.elf_file}") + sys.exit(1) + + if not os.path.exists(STM_PROGRAMMER_CLI): + print(f"STM32CubeProgrammer not found: {STM_PROGRAMMER_CLI}") + print("Please install STM32CubeProgrammer") + sys.exit(1) + + if args.erase_only: + success = erase_with_cube(args.port) + sys.exit(0 if success else 1) + + bin_path = convert_elf_to_bin(args.elf_file) + if not bin_path: + sys.exit(1) + + success = flash_with_cube(bin_path, args.port) + + try: + os.remove(bin_path) + except: + pass + + sys.exit(0 if success else 1) + + +if __name__ == "__main__": + main() diff --git a/hal/stm32f4/can/hal_can.c b/hal/stm32f4/can/hal_can.c index c4c5a80..4c706fa 100644 --- a/hal/stm32f4/can/hal_can.c +++ b/hal/stm32f4/can/hal_can.c @@ -1,24 +1,24 @@ #include "can_if.h" #include "bsp_can.h" #include "can.h" +#include "stm32f4xx_hal_can.h" -static int do_hal_can_init(int ch) + +static int hal_can_init(int ch) { - CAN_HandleTypeDef *hcan = (CAN_HandleTypeDef*)bsp_can_get_handle(ch); + CAN_HandleTypeDef *hcan = bsp_can_get_handle(ch)->hcan; if (!hcan) return -1; - // 配置滤波器:接收所有帧(掩码模式) CAN_FilterTypeDef sFilterConfig; sFilterConfig.FilterBank = 0; sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK; - sFilterConfig.FilterScale = CAN_FILTERSCALE_16BIT; + sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT; sFilterConfig.FilterIdHigh = 0x0000; sFilterConfig.FilterIdLow = 0x0000; sFilterConfig.FilterMaskIdHigh = 0x0000; sFilterConfig.FilterMaskIdLow = 0x0000; - sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0; + sFilterConfig.FilterFIFOAssignment = CAN_FILTER_FIFO0; sFilterConfig.FilterActivation = CAN_FILTER_ENABLE; - sFilterConfig.SlaveStartFilterBank = 14; if (HAL_CAN_ConfigFilter(hcan, &sFilterConfig) != HAL_OK) return -1; @@ -35,11 +35,12 @@ static int hal_can_send(int ch, const can_message_t *msg) if (!msg || msg->length > 8) return -1; - CAN_HandleTypeDef *hcan = (CAN_HandleTypeDef*)bsp_can_get_handle(ch); + CAN_HandleTypeDef *hcan = bsp_can_get_handle(ch)->hcan; if (!hcan) return -1; CAN_TxHeaderTypeDef TxHeader; TxHeader.StdId = msg->id & 0x7FF; + TxHeader.ExtId = 0; TxHeader.IDE = CAN_ID_STD; TxHeader.RTR = CAN_RTR_DATA; TxHeader.DLC = msg->length; @@ -53,6 +54,9 @@ static int hal_can_send(int ch, const can_message_t *msg) while (HAL_CAN_IsTxMessagePending(hcan, TxMailbox)) ; + if (HAL_CAN_GetTxMailboxesFreeLevel(hcan) == 0) { + /* 发送完成,邮箱变为空闲 */ + } return 0; } @@ -61,7 +65,7 @@ static int hal_can_recv(int ch, can_message_t *msg) if (!msg) return -1; - CAN_HandleTypeDef *hcan = (CAN_HandleTypeDef*)bsp_can_get_handle(ch); + CAN_HandleTypeDef *hcan = bsp_can_get_handle(ch)->hcan; if (!hcan) return -1; // 检查是否有消息 @@ -80,7 +84,7 @@ static int hal_can_recv(int ch, can_message_t *msg) static int hal_can_filter_config(int ch, uint8_t filter_id, uint32_t id, uint32_t mask) { - CAN_HandleTypeDef *hcan = (CAN_HandleTypeDef*)bsp_can_get_handle(ch); + CAN_HandleTypeDef *hcan = bsp_can_get_handle(ch)->hcan; if (!hcan) return -1; CAN_FilterTypeDef sFilterConfig; @@ -91,7 +95,7 @@ static int hal_can_filter_config(int ch, uint8_t filter_id, uint32_t id, uint32_ sFilterConfig.FilterIdLow = id & 0xFFFF; sFilterConfig.FilterMaskIdHigh = (mask >> 16) & 0xFFFF; sFilterConfig.FilterMaskIdLow = mask & 0xFFFF; - sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0; + sFilterConfig.FilterFIFOAssignment = CAN_FILTER_FIFO0; sFilterConfig.FilterActivation = CAN_FILTER_ENABLE; sFilterConfig.SlaveStartFilterBank = 14; @@ -103,7 +107,7 @@ static int hal_can_filter_config(int ch, uint8_t filter_id, uint32_t id, uint32_ // 注册接口 static const can_ops_t ops = { - .init = do_hal_can_init, + .init = hal_can_init, .send = hal_can_send, .recv = hal_can_recv, .filter_config = hal_can_filter_config @@ -114,9 +118,3 @@ void hal_can_init_all(void) can_register_ops(&ops); } -int hal_can_init(int ch) -{ - (void)ch; // 暂时不使用参数 - // 初始化指定通道 - 已经在 hal_can_init_all 中注册 - return 0; -} diff --git a/hal/stm32f4/can/hal_can.h b/hal/stm32f4/can/hal_can.h index 32a42ac..1778f93 100644 --- a/hal/stm32f4/can/hal_can.h +++ b/hal/stm32f4/can/hal_can.h @@ -8,8 +8,6 @@ extern "C" { // 初始化所有 CAN 通道(注册 ops 到接口层) void hal_can_init_all(void); -// 初始化指定 CAN 通道 -int hal_can_init(int ch); #ifdef __cplusplus } diff --git a/hal/stm32f4/gpio/hal_gpio.c b/hal/stm32f4/gpio/hal_gpio.c new file mode 100644 index 0000000..607223a --- /dev/null +++ b/hal/stm32f4/gpio/hal_gpio.c @@ -0,0 +1,39 @@ +#include "hal_gpio.h" +#include "bsp_gpio.h" +#include "gpio_if.h" +#include "stm32f4xx_hal_def.h" +#include "stm32f4xx_hal_gpio.h" + +static int hal_gpio_init(int ch) { + bsp_gpio_t *gpio = bsp_gpio_get_handle(ch); + if (gpio == NULL) + return -1; + return 0; +} + +static int hal_gpio_read(int ch) { + bsp_gpio_t *gpio = bsp_gpio_get_handle(ch); + if (gpio == NULL) + return -1; + return HAL_GPIO_ReadPin(gpio->port, gpio->pin); +} + +static int hal_gpio_write(int ch, int val) { + bsp_gpio_t *gpio = bsp_gpio_get_handle(ch); + if (gpio == NULL) + return -1; + HAL_GPIO_WritePin(gpio->port, gpio->pin, val ? GPIO_PIN_SET : GPIO_PIN_RESET); + return 0; +} + +static const gpio_ops_t ops = { + .init = hal_gpio_init, + .write = hal_gpio_write, + .read = hal_gpio_read, +}; + + + +void hal_gpio_init_all() { + gpio_register_ops(&ops); +} \ No newline at end of file diff --git a/hal/stm32f4/gpio/hal_gpio.h b/hal/stm32f4/gpio/hal_gpio.h new file mode 100644 index 0000000..d5a29ce --- /dev/null +++ b/hal/stm32f4/gpio/hal_gpio.h @@ -0,0 +1,6 @@ +#ifndef __HAL_GPIO_H__ +#define __HAL_GPIO_H__ + + +void hal_gpio_init_all(); +#endif diff --git a/hal/stm32f4/i2c/hal_i2c.c b/hal/stm32f4/i2c/hal_i2c.c index c48b288..10d5eaa 100644 --- a/hal/stm32f4/i2c/hal_i2c.c +++ b/hal/stm32f4/i2c/hal_i2c.c @@ -4,14 +4,14 @@ static int hal_i2c_init(int ch) { - I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef*)bsp_i2c_get_handle(ch); + I2C_HandleTypeDef *hi2c = bsp_i2c_get_handle(ch)->i2c; if (!hi2c) return -1; return 0; } static int hal_i2c_mem_write(int ch, uint16_t dev_addr, uint16_t mem_addr, uint16_t mem_add_size, const uint8_t *data, uint16_t size, uint32_t timeout) { - I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef*)bsp_i2c_get_handle(ch); + I2C_HandleTypeDef *hi2c = bsp_i2c_get_handle(ch)->i2c; if (!hi2c || !data) return -1; if (HAL_I2C_Mem_Write(hi2c, dev_addr, mem_addr, mem_add_size, (uint8_t*)data, size, timeout) != HAL_OK) @@ -22,7 +22,7 @@ static int hal_i2c_mem_write(int ch, uint16_t dev_addr, uint16_t mem_addr, uint1 static int hal_i2c_mem_read(int ch, uint16_t dev_addr, uint16_t mem_addr, uint16_t mem_add_size, uint8_t *data, uint16_t size, uint32_t timeout) { - I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef*)bsp_i2c_get_handle(ch); + I2C_HandleTypeDef *hi2c = bsp_i2c_get_handle(ch)->i2c; if (!hi2c || !data) return -1; if (HAL_I2C_Mem_Read(hi2c, dev_addr, mem_addr, mem_add_size, data, size, timeout) != HAL_OK) @@ -33,7 +33,7 @@ static int hal_i2c_mem_read(int ch, uint16_t dev_addr, uint16_t mem_addr, uint16 static int hal_i2c_write(int ch, uint16_t dev_addr, const uint8_t *data, uint16_t size, uint32_t timeout) { - I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef*)bsp_i2c_get_handle(ch); + I2C_HandleTypeDef *hi2c = bsp_i2c_get_handle(ch)->i2c; if (!hi2c || !data) return -1; if (HAL_I2C_Master_Transmit(hi2c, dev_addr, (uint8_t*)data, size, timeout) != HAL_OK) @@ -44,7 +44,7 @@ static int hal_i2c_write(int ch, uint16_t dev_addr, const uint8_t *data, uint16_ static int hal_i2c_read(int ch, uint16_t dev_addr, uint8_t *data, uint16_t size, uint32_t timeout) { - I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef*)bsp_i2c_get_handle(ch); + I2C_HandleTypeDef *hi2c = bsp_i2c_get_handle(ch)->i2c; if (!hi2c || !data) return -1; if (HAL_I2C_Master_Receive(hi2c, dev_addr, data, size, timeout) != HAL_OK) diff --git a/hal/stm32f4/led/hal_led.c b/hal/stm32f4/led/hal_led.c index 0a8212f..387a9c7 100644 --- a/hal/stm32f4/led/hal_led.c +++ b/hal/stm32f4/led/hal_led.c @@ -1,33 +1,10 @@ #include "led_if.h" #include "bsp_led.h" -static void hal_led_enable_gpio_clock(GPIO_TypeDef *port) -{ - if (port == GPIOA) __HAL_RCC_GPIOA_CLK_ENABLE(); - else if (port == GPIOB) __HAL_RCC_GPIOB_CLK_ENABLE(); - else if (port == GPIOC) __HAL_RCC_GPIOC_CLK_ENABLE(); - else if (port == GPIOD) __HAL_RCC_GPIOD_CLK_ENABLE(); - else if (port == GPIOE) __HAL_RCC_GPIOE_CLK_ENABLE(); - else if (port == GPIOF) __HAL_RCC_GPIOF_CLK_ENABLE(); - else if (port == GPIOG) __HAL_RCC_GPIOG_CLK_ENABLE(); -} - static int hal_led_init(int id) { bsp_led_t *led = bsp_led_get(id); if (!led) return -1; - - GPIO_InitTypeDef GPIO_InitStruct = {0}; - - hal_led_enable_gpio_clock(led->port); - - GPIO_InitStruct.Pin = led->pin; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - - HAL_GPIO_Init(led->port, &GPIO_InitStruct); - return 0; } @@ -35,10 +12,9 @@ static int hal_led_write(int id, int state) { bsp_led_t *led = bsp_led_get(id); if (!led) return -1; - + HAL_GPIO_WritePin(led->port, led->pin, state ? GPIO_PIN_SET : GPIO_PIN_RESET); - return 0; } @@ -46,8 +22,7 @@ static int hal_led_read(int id) { bsp_led_t *led = bsp_led_get(id); if (!led) return -1; - - return HAL_GPIO_ReadPin(led->port, led->pin); + return HAL_GPIO_ReadPin(led->port, led->pin) == GPIO_PIN_SET ? 1 : 0; } static int hal_led_toggle(int id) diff --git a/interfaces/gpio/gpio_if.c b/interfaces/gpio/gpio_if.c new file mode 100644 index 0000000..93a853a --- /dev/null +++ b/interfaces/gpio/gpio_if.c @@ -0,0 +1,23 @@ +#include + +static const gpio_ops_t* g_gpio_ops = 0; +void gpio_register_ops(const gpio_ops_t *ops) { + g_gpio_ops = ops; +} + +int gpio_if_init(int ch) { + if (!g_gpio_ops || !g_gpio_ops->init) + return -1; + return g_gpio_ops->init(ch); +} +int gpio_if_write(int ch, int val) { + if (!g_gpio_ops || !g_gpio_ops->write) + return -1; + return g_gpio_ops->write(ch, val); + +} +int gpio_if_read(int ch) { + if (!g_gpio_ops || !g_gpio_ops->read) + return -1; + return g_gpio_ops->read(ch); +} \ No newline at end of file diff --git a/interfaces/gpio/gpio_if.h b/interfaces/gpio/gpio_if.h new file mode 100644 index 0000000..99279a5 --- /dev/null +++ b/interfaces/gpio/gpio_if.h @@ -0,0 +1,16 @@ +#ifndef __GPIO_IF_H__ +#define __GPIO_IF_H__ + + +typedef struct { + int (*init)(int ch); + int (*write)(int ch, int val); + int (*read)(int ch); +} gpio_ops_t; + +void gpio_register_ops(const gpio_ops_t *ops); + +int gpio_if_init(int ch); +int gpio_if_write(int ch, int val); +int gpio_if_read(int ch); +#endif diff --git a/modules/sensor/imu/j901p_hal.c b/modules/sensor/imu/j901p_hal.c deleted file mode 100644 index 9c0dce7..0000000 --- a/modules/sensor/imu/j901p_hal.c +++ /dev/null @@ -1,33 +0,0 @@ -#include "j901p_hal.h" -#include "j901p.h" -#include "i2c_if.h" - -#define J901P_I2C_CH 1 -#define I2C_MEMADD_SIZE_8BIT ((uint16_t)0x0001) - -int32_t J901P_I2C_Write(uint8_t ucAddr, uint8_t ucReg, uint8_t *p_ucVal, uint32_t uiLen) -{ - if (i2c_if_mem_write(J901P_I2C_CH, ucAddr, ucReg, I2C_MEMADD_SIZE_8BIT, p_ucVal, uiLen, 100) == 0) - return 1; - return 0; -} - -int32_t J901P_I2C_Read(uint8_t ucAddr, uint8_t ucReg, uint8_t *p_ucVal, uint32_t uiLen) -{ - if (i2c_if_mem_read(J901P_I2C_CH, ucAddr, ucReg, I2C_MEMADD_SIZE_8BIT, p_ucVal, uiLen, 100) == 0) - return 1; - return 0; -} - -int32_t J901P_HAL_Init(uint8_t i2cAddr) -{ - WitI2cFuncRegister(J901P_I2C_Write, J901P_I2C_Read); - WitInit(WIT_PROTOCOL_I2C, i2cAddr); - return WIT_HAL_OK; -} - -void J901P_Delay(uint16_t ms) -{ - extern void HAL_Delay(uint32_t Delay); - HAL_Delay(ms); -} diff --git a/modules/sensor/imu/j901p_hal.h b/modules/sensor/imu/j901p_hal.h deleted file mode 100644 index 6dc99d0..0000000 --- a/modules/sensor/imu/j901p_hal.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef __J901P_HAL_H__ -#define __J901P_HAL_H__ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -int32_t J901P_HAL_Init(uint8_t i2cAddr); -void J901P_Delay(uint16_t ms); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/modules/sensor/imu/j901p_module.c b/modules/sensor/imu/j901p_module.c deleted file mode 100644 index f3f7190..0000000 --- a/modules/sensor/imu/j901p_module.c +++ /dev/null @@ -1,112 +0,0 @@ -#include "j901p_module.h" -#include "j901p_hal.h" -#include "i2c_if.h" -#include "j901p_reg.h" -#include "j901p.h" - -j901p_module_t g_imu = {0}; - -static void on_reg_update(uint32_t uiReg, uint32_t uiRegNum) -{ - // 根据寄存器地址更新对应的数据 - switch (uiReg) { - case AX: - if (uiRegNum >= 3) { - g_imu.acc[0] = sReg[AX]; - g_imu.acc[1] = sReg[AY]; - g_imu.acc[2] = sReg[AZ]; - } - break; - case GX: - if (uiRegNum >= 3) { - g_imu.gyro[0] = sReg[GX]; - g_imu.gyro[1] = sReg[GY]; - g_imu.gyro[2] = sReg[GZ]; - } - break; - case Roll: - if (uiRegNum >= 3) { - g_imu.angle[0] = sReg[Roll]; // Roll - g_imu.angle[1] = sReg[Pitch]; // Pitch - g_imu.angle[2] = sReg[Yaw]; // Yaw - } - break; - case HX: - if (uiRegNum >= 3) { - g_imu.mag[0] = sReg[HX]; - g_imu.mag[1] = sReg[HY]; - g_imu.mag[2] = sReg[HZ]; - } - break; - } -} - -int j901p_module_init(j901p_module_t *dev, int i2c_ch, uint8_t i2c_addr) -{ - if (!dev) return -1; - - dev->ch = i2c_ch; - dev->i2c_addr = i2c_addr; - dev->initialized = 0; - - // 复制地址到全局变量 - g_imu.ch = i2c_ch; - g_imu.i2c_addr = i2c_addr; - - int ret = i2c_if_init(i2c_ch); - if (ret != 0) { - return -1; - } - - WitRegisterCallBack(on_reg_update); - ret = J901P_HAL_Init(i2c_addr); - if (ret != WIT_HAL_OK) { - return -1; - } - - dev->initialized = 1; - g_imu.initialized = 1; - return 0; -} - -int j901p_module_read(j901p_module_t *dev) -{ - if (!dev || !dev->initialized) return -1; - - // 读取加速度计、陀螺仪、角度、磁力计 - WitReadReg(AX, 3); - WitReadReg(GX, 3); - WitReadReg(Roll, 3); - WitReadReg(HX, 3); - - return 0; -} - -void j901p_module_register_callback(void) -{ - WitRegisterCallBack(on_reg_update); -} - -int j901p_module_start_acc_cali(j901p_module_t *dev) -{ - if (!dev || !dev->initialized) return -1; - return WitStartAccCali(); -} - -int j901p_module_stop_acc_cali(j901p_module_t *dev) -{ - if (!dev || !dev->initialized) return -1; - return WitStopAccCali(); -} - -int j901p_module_start_mag_cali(j901p_module_t *dev) -{ - if (!dev || !dev->initialized) return -1; - return WitStartMagCali(); -} - -int j901p_module_stop_mag_cali(j901p_module_t *dev) -{ - if (!dev || !dev->initialized) return -1; - return WitStopMagCali(); -} diff --git a/modules/sensor/imu/j901p_module.h b/modules/sensor/imu/j901p_module.h deleted file mode 100644 index 7b5fe09..0000000 --- a/modules/sensor/imu/j901p_module.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef __J901P_MODULE_H__ -#define __J901P_MODULE_H__ - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -typedef struct { - int ch; - uint8_t i2c_addr; - int initialized; - // 原始数据 (0.01 度/0.01DPS 单位) - int16_t acc[3]; // AX, AY, AZ - int16_t gyro[3]; // GX, GY, GZ - int16_t angle[3]; // Roll, Pitch, Yaw (0.01 度) - int16_t mag[3]; // HX, HY, HZ -} j901p_module_t; - -// 全局 IMU 实例 (供回调使用) -extern j901p_module_t g_imu; - -int j901p_module_init(j901p_module_t *dev, int i2c_ch, uint8_t i2c_addr); -int j901p_module_read(j901p_module_t *dev); -void j901p_module_register_callback(void); -int j901p_module_start_acc_cali(j901p_module_t *dev); -int j901p_module_stop_acc_cali(j901p_module_t *dev); -int j901p_module_start_mag_cali(j901p_module_t *dev); -int j901p_module_stop_mag_cali(j901p_module_t *dev); - -#ifdef __cplusplus -} -#endif - -#endif