1. 概述
根据之前的功能实现需求,主要需要实现以下外设的基础功能:
CAN接口的接收和发送; flash 擦除、读写操作; 2. CAN接口调试 2.1 CAN功能开启设置通用过滤器;
指定CAN接收buffer;
开启CAN_FIFO0 接收通路
void CanStart( void ) { CanCfgIdListFilter(&hcan); hcan.pRxMsg = &gCanRx; mStatus = HAL_CAN_Receive_IT(&hcan, CAN_FIFO0); if ( mStatus != HAL_OK ) { CanRecvErro(); } } 2.2 CAN接收调试(1)CAN接收设置过滤器
CAN接收问题:必须设置过滤器,不然无法触发接收中断
__weak void CanCfgIdListFilter( CAN_HandleTypeDef* hcan ) { uint16_t mStdIdArray[10] ={0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19}; uint16_t *pId = mStdIdArray; uint32_t i=0,mNum = sizeof(mStdIdArray)>>1; pId++; for ( i = 0 ; i <5 ; i++ ) { *pId++ += 0; } for ( i = 0 ; i < mNum ; i=i+4 ) { CanIdListFilter(hcan,i>>2,&mStdIdArray[i],CAN_FIFO0); } }(2)CAN接收中断设置flag判断标志符,用于CAN 命令解析用
void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef* hcan) { HAL_StatusTypeDef mStatus = HAL_OK; CAN1_CanRxMsgFlag = 1; mStatus = HAL_CAN_Receive_IT(hcan, CAN_FIFO0); if ( mStatus != HAL_OK ) { CanRecvErro(); } } 2.3 CAN发送调试CAN发送直接配置完调用Hal层接口即可,无需其他操作; 配置内容包括如下:
标准帧还是扩展帧 帧ID 远程帧标志位 长度 8 bit data示例:
void CanSend( void ) { static uint32_t mIdx = 0; HAL_StatusTypeDef mStatus; uint32_t mCnt = 0; TxBuf.IDE = CAN_ID_STD; TxBuf.StdId = 0x10; TxBuf.RTR = CAN_RTR_DATA; TxBuf.DLC = 1; TxBuf.Data[0] = 1; do { mStatus = HAL_CAN_Transmit_IT(&hcan); } while ( HAL_OK != mStatus ); } 3. Flash接口调试 (1) 擦除操作擦除指定扇区区间的flash数据 StartPageAddr 起始扇区地址 EndPageAddr 结束扇区地址
FLASH_Status CAN_BOOT_ErasePage(uint32_t StartPageAddr,uint32_t EndPageAddr) { uint32_t i; FLASH_Status FLASHStatus=HAL_OK; HAL_FLASH_Unlock(); FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR); //FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR); for(i=StartPageAddr;i<=EndPageAddr;i+=PAGE_SIZE) { FLASH_PageErase(i); FLASHStatus = FLASH_WaitForLastOperation(0x8000); if(FLASHStatus!=HAL_OK) { HAL_FLASH_Lock(); return FLASHStatus; } } HAL_FLASH_Lock(); return FLASHStatus; } (2)flash写入函数 STMFLASH_Write(uint32_t WriteAddr,uint32_t *pBuffer,uint32_t NumToWrite)WriteAddr:要写入flash中的首地址,stm32的flash地址起始于0x0800 0000 pBuffer:数据地址。 NumToWrite:数据长度。
(3)flash读取函数 STMFLASH_Read(uint32_t ReadAddr,uint8_t *pBuffer,uint32_t NumToRead)ReadAddr:数据的首地址 pBuffer:buffer地址 NumToRead:数据长度
免责声明:文章内容来自互联网,本站不对其真实性负责,也不承担任何法律责任,如有侵权等情况,请与本站联系删除。
转载请注明出处:STM32 CANbootloader 升级功能设计开发(2)功能接口调试验证 https://www.yhzz.com.cn/a/13887.html