Why does MCP2515 always return to configuration mode?

Dear all,   I use LPC1768 to communicate with MCP2515 via SPI. SPI works fine as it can set the mode of MCP2515 as well as read the registers of MCP2515.  MCP2515 can changed from configuration mode to normal mode/loop mode when it is initialed, but quickly it will switch to configuration mode automatically. This cost me lots of time , and I have no ideas now, please give me a hand!  Many thanks in advance!
 
Bellow shows some source code:
In main loop :
   
  SPI_Init (); 
  SPI_ConfigClockRate (SPI_CLOCKRATE_LOW);  // SPI rate 400KHz 
   
  SPI_CS_Low(); 
 delay_us(100); 
 uchar flag = MCP2515_Init(CAN_125KBPS);     
 delay_us(100); 
 SPI_CS_High (); 
   
 while(1){ 
                   if(needspican == 0){ 
                           needspican = 1;   // will be changed per second in timer  
                           SPI_CS_Low(); 
                           delay_us(100); 
                             res = mcp2515_read_register(CANSTAT);  // shows is configuration mode           
                          delay_us(100);   // delay 100us 
                           SPI_CS_High (); 
                           } 
                   } 
  
 


 
MCP2515  initial code:
 uchar MCP2515_Init(const uchar CanSpeed) 
{ 
  uchar res; 
  res = mcp2515_read_register(CANCTRL);  
  res = mcp2515_read_register(CANSTAT);  
  MCP2515_RESET();  
       delay_us(1000); 
  res = mcp2515_read_register(CANSTAT);  //配置模式 
  mcp2515_bit_modify(CANCTRL,MODE_MASK,CAN_SETUP_MODE); 
  delay_us(1000); 
  while((mcp2515_read_register(CANSTAT) & MODE_MASK) != CAN_SETUP_MODE){    
          delay_us(100); 
    } 
  res = MCP2515_ConfigRate(CanSpeed); 
  delay_us(100); 
  if(res == MCP2515_OK) 
  { 
     MCP2515_PinCfg(); 
    delay_us(100); 
    MCP2515_InitCanBuffers(); 
  } 
  
  mcp2515_bit_modify(CANCTRL,MODE_MASK,CAN_NORMAL_MODE); 
  delay_us(1000); 
  while((res& MODE_MASK) != CAN_NORMAL_MODE){ 
 res = mcp2515_read_register(CANSTAT);  
   delay_us(100); 
  } 
    
  res = MCP2515_OK; 
  return res; 
} 
 

 
CF1  CF2 CF3  configuration:
// 125KBPS,NBT:PS:PS1:PS2 = 16:6(PS2):7(ps1):2(Ps):1    sampling bit locate at  62.5%
#define MCP_12MHZ_125KBPS_CFG1 0X02 //(00(SJW)  00001 (BRP))
#define MCP_12MHZ_125KBPS_CFG2 0XB1 //(1 0  110(PS1 length)  001(propSeg))
#define MCP_12MHZ_125KBPS_CFG3 0X05 //(0 0  000 101(PS2 length)) 
  
  
 
Re:Why does MCP2515 always return to configuration mode? 9 hrs. ago ( permalink)
5
> quickly it will switch to configuration mode automatically.
 
Automatically? That's not a documented function.

There are only three documented ways for the chip to get into Configuration Mode:
 
1 - Hardware Reset (Reset pin forced low),
2 - Software Reset by SPI command,
3 - Setting the CANCTRL REQOP bits.
 
So if you're not accidentally resetting it in Software, maybe you've got the Reset pin floating, or connected to something that isn't a proper Reset signal.
 
Tom 
 
 
 
 
Thanks for your replay.  But I checked the three ways, it is fine for the code. 
There are only three documented ways for the chip to get into Configuration Mode: 
1 - Hardware Reset (Reset pin forced low),    
Pin 19(Reset Pin) is connected with MCU, and it keeps 3.3V high level when runing

2 - Software Reset by SPI command
There are reset command and Pin reset  at the beginning of MCP2515_Init(). At the end of MCP2515_Init(), only switch into normal mode can MCP2515 jump out this function.  This is confirmed by oscilloscope.

3 - Setting the CANCTRL REQOP bits.
  This is only excute in MCP2515_Init(), after this function, it goes to loop task. And in the loop task , it only read the CANSTAT register.   But I found MCP2515 REQOP bits will be changed every time it is readed. I do not know why?
loop task( excute once per second)
 
[style="color: #ff0000;"][style="color: #ff0000;"][style="color: #ff0000;"][style="color: #ff0000;"][style="color: #ff0000;"][style="color: #ff0000;"][style="color: #ff0000;"][style="color: #888888;"][style="color: #ff0000;"][style="color: #ff0000;"][style="color: #ff0000;"][style="color: #ff0000;"][style="color: #ff0000;"][style="color: #888888;"][style="color: #ff0000;"][style="color: #ff0000;"][style="color: #ff0000;"][style="color: #ff0000;"][style="color: #ff0000;"][style="color: #ff0000;"][style="color: #888888;"][style="color: #888888;"]
  
   
SPI_CS_Low();  
delay_us(100);  
 res = mcp2515_read_register(CANSTAT);  
 delay_us(1000);  
 res = mcp2515_read_register(CANSTAT);  
 delay_us(1000);  
 res = mcp2515_read_register(CANSTAT);  
 delay_us(1000);  
 res = mcp2515_read_register(CANSTAT);  
delay_us(100);  
SPI_CS_High (); 
 
 
 
 
 
OK, looks like an obvious bug, but I'm having to guess because you haven't supplied the majority of your code. 
  
Like the source for mcp2515_read_register(). 
  
Assuming it does what I think it does... 
  
Look at 'FIGURE 12-2: READ INSTRUCTION" in the MCP2515 manual. 
  
Note that "CS" goes DOWN, there are then three bytes in the SPI sequence (command, address, data) and then SPI goes UP to mark the end of the cycle. 
  
You code shows "SPI_CS_Low() and "SPI_CS_High" separated by 3.2ms with four "mcp2515_read_register()" calls in the middle. 
  
Which will probably result in 12 SPI bytes transferred between the CS LOW and CS HIGH. The MCP2525 should interpret that as a read of CANSTAT, followed by reads of the next NINE registers in sequence. The data in "res" at the end of that will not be what you expect, but will be the contents of RXF4EID0. 
  
Multiple-reading isn't shown in the diagram, but is detailed in "12.3 READ Instruction". In there: 

"The internal Address Pointer is automatically incremented to the next address once each byte of data is shifted out. Therefore, it is possible to read the next consecutive register address by continuing to provide clock pulses. Any number of consecutive register locations can be read sequentially using this method. The read operation is terminated by raising the CS pin". 
  
Obviously that's VERY useful for writing messages and reading messages. It means you can read 16 bytes of message ID and data in (2 + 16 = 18) SPI bytes rather than (16 * 3 = 48) cycles. 
  
If any of your other code is doing the same thing (multiple attempted transfers between SPI low and high) it could explain what you're seeing. You may be reading or writing the wrong registers. 
  
Tom 
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值