CH32V203 中 CAN 測試模式疑問

CH32V203 中 CAN 在測試模式中,比如開啟靜默環(huán)回模式。重新進入初始化模式,對寄存器 CAN_BTIMR 的 SILM 和 LBKM 位置0(寄存器置0成功),退出初始化模式,靜默環(huán)回模式依舊存在,需重新上下電才能恢復,請問這是特性嗎?

/*?官方程序?*/
uint8_t?CAN_Init(CAN_TypeDef*?CANx,?CAN_InitTypeDef*?CAN_InitStruct)
{
??uint8_t?InitStatus?=?CAN_InitStatus_Failed;
??uint32_t?wait_ack?=?0x00000000;

??CANx->CTLR?&=?(~(uint32_t)CAN_CTLR_SLEEP);
??CANx->CTLR?|=?CAN_CTLR_INRQ?;

??while?(((CANx->STATR?&?CAN_STATR_INAK)?!=?CAN_STATR_INAK)?&&?(wait_ack?!=?INAK_TIMEOUT))
??{
????wait_ack++;
??}

??if?((CANx->STATR?&?CAN_STATR_INAK)?!=?CAN_STATR_INAK)
??{
????InitStatus?=?CAN_InitStatus_Failed;
??}
??else?
??{
????if?(CAN_InitStruct->CAN_TTCM?==?ENABLE)
????{
??????CANx->CTLR?|=?CAN_CTLR_TTCM;
????}
????else
????{
??????CANx->CTLR?&=?~(uint32_t)CAN_CTLR_TTCM;
????}

????if?(CAN_InitStruct->CAN_ABOM?==?ENABLE)
????{
??????CANx->CTLR?|=?CAN_CTLR_ABOM;
????}
????else
????{
??????CANx->CTLR?&=?~(uint32_t)CAN_CTLR_ABOM;
????}

????if?(CAN_InitStruct->CAN_AWUM?==?ENABLE)
????{
??????CANx->CTLR?|=?CAN_CTLR_AWUM;
????}
????else
????{
??????CANx->CTLR?&=?~(uint32_t)CAN_CTLR_AWUM;
????}

????if?(CAN_InitStruct->CAN_NART?==?ENABLE)
????{
??????CANx->CTLR?|=?CAN_CTLR_NART;
????}
????else
????{
??????CANx->CTLR?&=?~(uint32_t)CAN_CTLR_NART;
????}

????if?(CAN_InitStruct->CAN_RFLM?==?ENABLE)
????{
??????CANx->CTLR?|=?CAN_CTLR_RFLM;
????}
????else
????{
??????CANx->CTLR?&=?~(uint32_t)CAN_CTLR_RFLM;
????}

????if?(CAN_InitStruct->CAN_TXFP?==?ENABLE)
????{
??????CANx->CTLR?|=?CAN_CTLR_TXFP;
????}
????else
????{
??????CANx->CTLR?&=?~(uint32_t)CAN_CTLR_TXFP;
????}

????CANx->BTIMR?=?(uint32_t)((uint32_t)CAN_InitStruct->CAN_Mode?<<?30)?|?\
????????????????((uint32_t)CAN_InitStruct->CAN_SJW?<<?24)?|?\
????????????????((uint32_t)CAN_InitStruct->CAN_BS1?<<?16)?|?\
????????????????((uint32_t)CAN_InitStruct->CAN_BS2?<<?20)?|?\
????????????????((uint32_t)CAN_InitStruct->CAN_Prescaler?-?1);
????CANx->CTLR?&=?~(uint32_t)CAN_CTLR_INRQ;
????wait_ack?=?0;

????while?(((CANx->STATR?&?CAN_STATR_INAK)?==?CAN_STATR_INAK)?&&?(wait_ack?!=?INAK_TIMEOUT))
????{
??????wait_ack++;
????}

????if?((CANx->STATR?&?CAN_STATR_INAK)?==?CAN_STATR_INAK)
????{
??????InitStatus?=?CAN_InitStatus_Failed;
????}
????else
????{
??????InitStatus?=?CAN_InitStatus_Success?;
????}
??}

??return?InitStatus;
}


您好,CH32V203 CAN有3種工作模式:初始化模式、睡眠模式和正常模式3種模式。只能在初始化模式下修改CAN_BTIMR 的 SILM 和 LBKM位配置來選擇一種測試模式,測試模式有3種:靜默模式,環(huán)回模式,靜默環(huán)回模式。若要切換工作模式,需要對寄存器CAN_CTLR 中的 SLEEP 或 INRQ 位進行操作切換3種工作模式,具體可參考CH32V203應用手冊介紹,如下圖。后續(xù)若有問題,可通過郵箱和我溝通(lzs@wch.cn)

CH32V203應用手冊:http://www.findthetime.net/downloads/CH32FV2x_V3xRM_PDF.html?

image.png


int?main(void)
{
????u8?i;
????u8?cnt?=?0;
????u8?tx,?rx;
????u8?txbuf[8];
????u8?rxbuf[8];

????Delay_Init();
????USART_Printf_Init(115200);
????printf("SystemClk:%d\r\n",?SystemCoreClock);
/*
Bps?=?250Kbps?
選擇靜默環(huán)回模式
*/
????CAN_Test_Mode_Init(CAN_SJW_1tq,?CAN_BS2_5tq,?CAN_BS1_6tq,?12,?CAN_Mode_Silent_LoopBack);
????
/*?再選擇正常模式,發(fā)現(xiàn)靜默環(huán)回依舊存在?*/
????CAN_Test_Mode_Init(CAN_SJW_1tq,?CAN_BS2_5tq,?CAN_BS1_6tq,?12,?CAN_Mode_Normal);

????while(1)
????{
????????for(i?=?0;?i?<?8;?i++)?{
????????????txbuf[i]?=?cnt?+?i;
????????}

????????tx?=?CAN_Send_Msg(txbuf,?8);

????????if(tx)
????????{
????????????printf("Send?Failed\r\n");
????????}
????????else
????????{
????????????printf("Send?Success\r\n");
????????????printf("Send?Data:\r\n");

????????????for(i?=?0;?i?<?8;?i++)?{
????????????????printf("%02x\r\n",?txbuf[i]);
????????????}
????????}

????????rx?=?CAN_Receive_Msg(rxbuf);

????????if(rx)
????????{
????????????printf("Receive?Data:\r\n");

????????????for(i?=?0;?i?<?8;?i++)?{
????????????????printf("%02x\r\n",?txbuf[i]);
????????????}
????????}
????????else
????????{
????????????printf("No?Receive?Data\r\n");
????????}

????????Delay_Ms(1000);
????????cnt++;
????????if(cnt?==?0xFF)
????????{
????????????cnt?=?0;
????????}
????}
}

是的,我知道怎么切換工作模式,但是在切換的過程中出現(xiàn)了問題,靜默環(huán)回無法退出。


您好,你可以按照下圖配置一下就可以了,加個CAN_DeInit函數(shù)

image.png


謝謝,已解決。確實是我能力不夠,沒有想到這方面。


只有登錄才能回復,可以選擇微信賬號登錄

国产91精品新入口,国产成人综合网在线播放,九热这里只有精品,本道在线观看,美女视频a美女视频,韩国美女激情视频,日本美女pvp视频