6783 views|2 replies

2870

Posts

4

Resources
The OP
 

Is there anyone who can run the SPI of CH32V103? [Copy link]

 

Has anyone run SPI bus successfully these days? I have been running SPI for three days, but I can't even find the signal through the oscilloscope. If you know, please give me some tips

PA5 -->SPI_CLK

PA7 -->SPI_MOSI

/*******************************************************************************
* Function Name  : SPI_FullDuplex_Init
* Description    : Configuring the SPI for full-duplex communication.
* Input          : None
* Return         : None
*******************************************************************************/
void SPI_FullDuplex_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    SPI_InitTypeDef SPI_InitStructure;

    RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE );

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init( GPIOA, &GPIO_InitStructure );

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init( GPIOA, &GPIO_InitStructure );

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init( GPIOA, &GPIO_InitStructure );

    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;


    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
    SPI_InitStructure.SPI_CRCPolynomial = 7;
    SPI_Init( SPI1, &SPI_InitStructure );

    SPI_Cmd( SPI1, ENABLE );
}

// Send a byte to the command register
void ssd1306_WriteCommand(uint8_t byte) {
    GPIO_WriteBit(SSD1306_CS_Port, SSD1306_CS_Pin, GPIO_PIN_RESET); // select OLED
    GPIO_WriteBit(SSD1306_DC_Port, SSD1306_DC_Pin, GPIO_PIN_RESET); // command
    SPI_I2S_SendData(SSD1306_SPI_PORT,byte);
    GPIO_WriteBit(SSD1306_CS_Port, SSD1306_CS_Pin, GPIO_PIN_SET); // un-select OLED
}

void SPI_Transmit(SPI_TypeDef* SPIx,u8* TxData, u16 TxData_size)
{
    u16 i = 0;
    for(i=0;i<TxData_size;i++)
    {
        if( SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE ) != RESET )
        {
             SPI_I2S_SendData(SPIx, TxData[i]);
        }
    }
}

// Send data
void ssd1306_WriteData(u8 * TxData, u16 TxData_size) {

    GPIO_WriteBit(SSD1306_CS_Port, SSD1306_CS_Pin, GPIO_PIN_RESET); // select OLED
    GPIO_WriteBit(SSD1306_DC_Port, SSD1306_DC_Pin, GPIO_PIN_SET); // data
    SPI_Transmit(SSD1306_SPI_PORT, TxData, TxData_size);
    GPIO_WriteBit(SSD1306_CS_Port, SSD1306_CS_Pin, GPIO_PIN_SET); // un-select OLED
    // value = memcmp( TxData, RxData, Size );
}

The calling situation in the main program. I call SPI to send a signal in the ssd1306_Init program.

	u8 i=0;
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    Delay_Init();
	USART_Printf_Init(115200);
	printf("SystemClk:%d\r\n",SystemCoreClock);
	GPIO_Toggle_INIT();
	SPI_FullDuplex_Init();

	Delay_Ms(2000);
	printf("This is SPI  example\r\n");
	GPIO_WriteBit(GPIOB, GPIO_Pin_11, Bit_SET);
	ssd1306_Init();
	//GPIO_WriteBit(GPIOB, GPIO_Pin_11, Bit_RESET);
	printf("Host Mode\r\n");

The initialization of the pins is like this.

/*******************************************************************************
* Function Name  : GPIO_Toggle_INIT
* Description    : Initializes GPIOA.0
* Input          : None
* Return         : None
*******************************************************************************/
void GPIO_Toggle_INIT(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;
 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
      GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
      GPIO_Init(GPIOA, &GPIO_InitStructure);


  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
      GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
      GPIO_Init(GPIOA, &GPIO_InitStructure);


  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
      GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
      GPIO_Init(GPIOB, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
      GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
      GPIO_Init(GPIOB, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);


  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
     GPIO_Init(GPIOA, &GPIO_InitStructure);
    
}

This post is from Domestic Chip Exchange

Latest reply

I also can't use the hardware spi driver, and it reports a port error when I try to debug. Have you solved it?   Details Published on 2021-2-8 20:11
 
 

2870

Posts

4

Resources
2
 

The OLED screen is tested and is good on the STM32G431 development board

This post is from Domestic Chip Exchange
 
 
 

58

Posts

0

Resources
3
 

I also can't use the hardware spi driver, and it reports a port error when I try to debug. Have you solved it?

This post is from Domestic Chip Exchange
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

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