2974 views|5 replies

3

Posts

0

Resources
The OP
 

stm8l151C6 RC522 physical SPI has no SCK clock [Copy link]

This post was last edited by lmy11224433 on 2018-3-2 15:04 I have been debugging for several days and have tried many methods on the Internet but it still doesn’t work. I hope some experts can take a look at whether the code is written correctly. Please read on: Hardware connection: The microcontroller used is stm8l151C6, and the physical SPI is connected to a RC522 chip PB4: NSS chip select PB5: SCK PB6: MOSI PB7: MISO PC4: RST (reset) PD6: pwr (power supply, designed by myself for low power consumption) Problem: After the physical SPI is initialized and enabled, it keeps writing data. The SCK pin clock is detected with an oscilloscope and it is found that there is no clock signal. The program is always stuck in while (SPI_GetFlagStatus(SPI1, SPI_FLAG_RXNE) == RESET) There is no problem in simulating SPI communication. The code is as follows:
  1. void main(void) { hardware_init(); //enableInterrupts(); while (1) { // ReadRawRC(0x05); SPIWritebyte(0xAA); // BSP_RFID_WriteRegister(CommandReg,0xAA); // gpio_set_low(PORTB, PIN5); // gpio_set_high(PORTB, PIN5); } } void hardware_init(void) { u8 rv = 0; u8 ctrl_reg = 0, ver_reg = 0; // gpio_set_high(PORTD, PIN7); // system clock init sysclk_init_hsi(CLK_SYSCLKDiv_1); //CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); /* Select HSE as system clock source */ // CLK_SYSCLKSourceSwitchCmd(ENABLE); // CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSE); /*High speed external clock prescaler: 1*/ // CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); // com load configuration & init com_load_config(com_cfg, 1); com_init(COM1, 2400, USART_WordLength_9b, USART_StopBits_1, USART_Parity_Even, USART_Mode_All, ENABLE, 0); com_recv_init(COM1); /* Check if the system has resumed from IWDG reset */ if (RST_GetFlagStatus(RST_FLAG_IWDGF) != RESET) { /* Clear IWDGF Flag */ RST_ClearFlag(RST_FLAG_IWDGF); printf("RST_FLAG_IWDGF\r\n"); } // rtc init rtc_init(); BSP1_RFID_LoadConfig(&rfid_config); //Load pin configuration CLK_PeripheralClockConfig(CLK_Peripheral_SPI1, ENABLE); GPIO_Init(GPIOB, GPIO_Pin_4, GPIO_Mode_Out_PP_High_Fast); //NSS chip select GPIO_Init(GPIOB, GPIO_Pin_5, GPIO_Mode_Out_PP_High_Fast); //SCK GPIO_Init(GPIOB, GPIO_Pin_6, GPIO_Mode_Out_PP_High_Fast); //MOSI //Host mode, configured as input GPIO_Init(GPIOB, GPIO_Pin_7, GPIO_Mode_In_PU_No_IT); //MISO BSP1_RFID_PowerOn(); //Power on the RC522 chip//BSP1_RFID_PowerOff(); SPI_DeInit(SPI1); SPI_Init(SPI1, SPI_FirstBit_MSB, SPI_BaudRatePrescaler_8, SPI_Mode_Master,SPI_CPOL_Low, SPI_CPHA_1Edge,SPI_Direction_2Lines_FullDuplex, SPI_NSS_Soft, 0x07); //SPI_Init(SPI1, SPI_FirstBit_MSB, SPI_BaudRatePrescaler_128, SPI_Mode_Master,SPI_CPOL_High, SPI_CPHA_2Edge,SPI_Direction_2Lines_FullDuplex, SPI_NSS_Soft, 0x07); SPI_Cmd(SPI1, ENABLE); /* Enable SPI */ rv = BSP1_RFID_Reset(&ctrl_reg, &ver_reg); //Exactly the same as the PcdReset library function printf("ctrl_reg:%02X, ver_reg:%02X\r\n", ctrl_reg, ver_reg); if (rv != 0) { printf("rfid init error\r\n"); } BSP_RFID_AntennaOff(); delay_ms(1); BSP_RFID_AntennaOn(); BSP_RFID_ConfigISOType('A'); //Initialize for ISO14443A cardreturn; } /*!\brief
  2. *       rc522 reset
  3. * \return
  4. *       MI_OK       - success
  5. *       MI_ERR      - failure
  6. */
  7. char BSP1_RFID_Reset(u8 *ctrl_reg, u8 *version)
  8. {
  9.     unsigned char val = 0;
  10.     /* set rc522 rst to push pull output state , and set it to 0 */
  11.     BSP_GPIO_Init(rfid_config_handler->rst.port, rfid_config_handler->rst.pin, GPIO_Mode_Out_PP_Low_Fast);
  12. //    /* set rs522 nss to push pull output state, and set it to 1, disable sck,mosi,miso pin */
  13. //    BSP_GPIO_Init(rfid_config_handler->nss.port, rfid_config_handler->nss.pin, GPIO_Mode_Out_PP_High_Fast);
  14. //    /* set rc522 sck to push pull output state, and set it to 1 */
  15. //    BSP_GPIO_Init(rfid_config_handler->sck.port, rfid_config_handler->sck.pin, GPIO_Mode_Out_PP_High_Fast);
  16. //    /* set rc522 miso to push up input state*/
  17. //    BSP_GPIO_Init(rfid_config_handler->miso.port, rfid_config_handler->miso.pin, GPIO_Mode_In_PU_No_IT);
  18. //    //BSP_GPIO_Init(rfid_config_handler->miso.port, rfid_config_handler->miso.pin, GPIO_Mode_Out_PP_High_Fast);
  19. //    /* set rc522 mosi to push pull output state, and set it to 1 */
  20. //    BSP_GPIO_Init(rfid_config_handler->mosi.port, rfid_config_handler->mosi.pin, GPIO_Mode_Out_PP_High_Fast);
  21.     delay_us(10);
  22.     /* rs522 will be reset by setting rst to execute 1-0-1 sequence. */
  23.     BSP_GPIO_SetHigh(rfid_config_handler->rst.port, rfid_config_handler->rst.pin);
  24.     delay_us(10);
  25.     BSP_GPIO_SetLow(rfid_config_handler->rst.port, rfid_config_handler->rst.pin);
  26.     delay_us(10);
  27.     BSP_GPIO_SetHigh(rfid_config_handler->rst.port, rfid_config_handler->rst.pin);
  28.     delay_us(10);
  29.     //    RC522_RST_H();
  30.     //    delay_us(10);
  31.     //    RC522_RST_L();
  32.     //    delay_us(10);
  33.     //    RC522_RST_H();
  34.     //    delay_us(10);
  35.     WriteRawRC(CommandReg,PCD_RESETPHASE);
  36.     delay_us(10);
  37.     WriteRawRC(ModeReg,0x3D);            //和Mifare卡通讯,CRC初始值0x6363
  38.     WriteRawRC(TReloadRegL,30);
  39.     WriteRawRC(TReloadRegH,0);
  40.     WriteRawRC(TModeReg,0x8D);
  41.     WriteRawRC(TPrescalerReg,0x3E);
  42.     WriteRawRC(TxAutoReg,0x40);//必须要
  43.     val = ReadRawRC(ControlReg);
  44.     if (val == 0xFF)
  45.     {
  46.         //printf("CtlReg:%02X\r\n", val);
  47.         return MI_ERR;
  48.     }
  49.    
  50.     *ctrl_reg = val;
  51.     val = ReadRawRC(VersionReg);
  52.     if (val == 0xFF)
  53.     {
  54.         //printf("Ver:%02X\r\n", val);
  55.         return MI_ERR;
  56.     }
  57.    
  58.     *version = val;
  59. //
  60. //    BSP_RFID_AntennaOff();
  61. //    BSP_RFID_ConfigISOType('A');
  62. //    delay_ms(1);
  63. //    BSP_RFID_AntennaOn();
  64.    
  65.     return MI_OK;
  66. }
  67. void SPIWritebyte(u8 Byte)
  68. {
  69.   u16 retry=0;
  70. //  u8 rv = 0;
  71.   
  72.   while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET)
  73.   {
  74.     retry++;
  75.     if(retry>200)
  76.     {
  77.       return;
  78.     }
  79.   }
  80.   SPI_SendData(SPI1, Byte);
  81.   retry=0;
  82.   //while (SPI_GetFlagStatus(SPI1, SPI_FLAG_RXNE) == RESET)
  83.   while (SPI_GetFlagStatus(SPI1, SPI_FLAG_BSY) == SET)
  84.   {
  85.     retry++;
  86.     if(retry>400)return;
  87.   }
  88.   while (SPI_GetFlagStatus(SPI1,SPI_FLAG_RXNE) == RESET) { retry++; if(retry>400)return; } SPI_ReceiveData(SPI1); }
复制代码
I hope everyone will participate actively!

This post is from stm32/stm8

Latest reply

That's very strange. Is it a problem with the CPU itself? Can you post a screenshot of your hardware circuit diagram?  Details Published on 2018-3-7 09:46
 

4177

Posts

9

Resources
2
 
I haven't played with stm8, I think it's similar to stm32. 1. SPI_CPOL_Low, SPI_CPHA_1Edge, one is to set the clock polarity, the other is to set the clock phase, you must look at the timing diagram in the RC522 datasheet. 2. Similarly, there is SPI_FirstBit_MSB, this also needs to look at the datasheet of the peripheral to be driven;
This post is from stm32/stm8
 
 

3

Posts

0

Resources
3
 
huaiqiao posted on 2018-3-2 17:19 I haven't played with stm8, I think it's similar to stm32. 1. SPI_CPOL_Low, SPI_CPHA_1Edge, one is to set the clock polarity, the other is...
I have also seen these, and they are configured like this
This post is from stm32/stm8

Comments

Have you checked the configuration of the IO corresponding to CLK? Have you ruled out the hardware problem? Don't worry about it for a long time, there is a problem with the hardware. Let me give you an example. In very rare cases, the hardware has IO but the output is abnormal. We usually set it to a normal IO, and then output high and low levels to test I  Details Published on 2018-3-5 11:36
 
 

4177

Posts

9

Resources
4
 
lmy11224433 posted on 2018-3-2 17:59 I have also seen these, and they are configured like this
Have you checked the configuration of the IO corresponding to the CLK? Have you ruled out the hardware problem? Don’t spend so much time on it, there is a problem with the hardware. . . . . . Let me give you an example. In very rare cases, the hardware has IO but the output is abnormal. We usually set it to normal IO, and then output high and low levels to test whether the IO is normal. In addition, the design of the hardware may also affect the IO. I haven’t seen your circuit, so I can only say that it is possible.
This post is from stm32/stm8
 
 
 

3

Posts

0

Resources
5
 
huaiqiao posted on 2018-3-5 11:36 Have you checked the configuration of the IO corresponding to CLK? Have you ruled out the hardware problem? Don't spend so much time on it, there is a problem with the hardware. . . . . . I...
I have tested the SCK pin with the GPIO pull-up and pull-down functions, and it can control the high and low levels of the pin.
This post is from stm32/stm8

Comments

That's very strange. Is it a problem with the CPU itself? Can you post a screenshot of your hardware circuit diagram?  Details Published on 2018-3-7 09:46
 
 
 

4177

Posts

9

Resources
6
 
lmy11224433 posted on 2018-3-7 09:36 I have tested the SCK pin with the GPIO pull-up and pull-down functions, and it can control the high and low levels of the pin
That's very strange. Is it a problem with the CPU itself? Can you post a screenshot of your hardware circuit diagram?
This post is from stm32/stm8
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list