[Atria Development Board AT32F421 Review] SPI Usage
[Copy link]
This post was last edited by eew_Violet on 2021-4-18 23:38
There is one thing you need to pay special attention to when using domestic substitution. Look at the block diagram below. The APB1 and APB2 of this device can run at full frequency, while the APB1 of S*M is divided by 2. Sometimes after transplanting, you find that it worked before, but now it doesn’t work. You will wonder what you missed. Sometimes checking the frequency of APB1 will bring unexpected gains.
Here we see that SPI2 is connected to APB1, so, needless to say, find system_at32f4xx.c under CMSIS, where we can see its clock configuration. In addition, many clock configurations have been written in system_at32f4xx.c, just define whichever you need.
Next, we can start writing the SPI initialization function, which I have written in at32_board.c.
void SPI2_Init()
{
GPIO_InitType GPIO_InitStructure;
SPI_InitType SPI_InitStructure;
/* 启用时钟 */
RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_SPI2, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPERIPH_GPIOB , ENABLE);
/* 配置 SPI2引脚:
* SPI2_CLK-->PB13
* SPI2_MOSI-->PB15
* SPI2_NSS-->PB12
* LCD_RS-->PB14
* LCD_DC-->PB11
*/
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_13| GPIO_Pins_15;
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
GPIO_InitStructure.GPIO_OutType = GPIO_OutType_PP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pull = GPIO_Pull_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_11|GPIO_Pins_12|GPIO_Pins_14;
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
GPIO_InitStructure.GPIO_OutType = GPIO_OutType_PP;
GPIO_InitStructure.GPIO_Pull = GPIO_Pull_NOPULL;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinsSource13, GPIO_AF_0);
GPIO_PinAFConfig(GPIOB, GPIO_PinsSource15, GPIO_AF_0);
/* SPI2配置*/
SPI_DefaultInitParaConfig(&SPI_InitStructure);
SPI_InitStructure.SPI_TransMode = SPI_TRANSMODE_TX_HALFDUPLEX ;
SPI_InitStructure.SPI_Mode = SPI_MODE_MASTER;
SPI_InitStructure.SPI_FrameSize = SPI_FRAMESIZE_8BIT;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_LOW;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1EDGE;
SPI_InitStructure.SPI_NSSSEL = SPI_NSSSEL_SOFT;
SPI_InitStructure.SPI_MCLKP = SPI_MCLKP_2;
SPI_InitStructure.SPI_FirstBit = SPI_FIRSTBIT_MSB;
SPI_InitStructure.SPI_CPOLY = 7;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Enable(SPI2, ENABLE);
}
The writing style is familiar to you. There is one more thing to note. Although it looks the same, the actual capitalization and the presence or absence of the suffix are different. Before configuring the members, the GPIO_StructInit(); and SPI_DefaultInitParaConfig(); functions are called to fill in the default values. Sometimes we may not configure every member, so it is a good habit to restore the defaults before configuring. The next step is to add the peripheral driver code to the project.
.c and .h have been added to the project. Next, we need to rewrite the header file in oled.h and modify the IO to be used.
This is basically completed. If there is no error in the compilation, you can write main. Here I took the following template myself
unsigned char COAlarm16[10][16]={
{0x00,0xC2,0xBA,0x82,0x82,0xFE,0xA2,0x90,0xFC,0x4B,0x48,0xF9,0x4A,0x48,0x08,0x00},
{0x20,0x10,0x08,0x46,0x81,0x7F,0x00,0x00,0xFF,0x22,0x22,0x3F,0x22,0x22,0x20,0x00},/*"雅",0*/
/* (16 X 16 , 宋体 )*/
{0x40,0x3C,0x10,0xFF,0x10,0x10,0x40,0x48,0x48,0x48,0x7F,0x48,0xC8,0x48,0x40,0x00},
{0x02,0x06,0x02,0xFF,0x01,0x01,0x00,0x02,0x0A,0x12,0x42,0x82,0x7F,0x02,0x02,0x00},/*"特",1*/
/* (16 X 16 , 宋体 )*/
{0x00,0x10,0x10,0x10,0x10,0x10,0xFF,0x10,0x10,0x10,0x10,0x10,0xF0,0x00,0x00,0x00},
{0x00,0x80,0x40,0x20,0x18,0x06,0x01,0x00,0x20,0x40,0x80,0x40,0x3F,0x00,0x00,0x00},/*"力",2*/
/* (16 X 16 , 宋体 )*/
{0x24,0x24,0xA4,0xFE,0xA3,0x22,0x00,0x22,0xCC,0x00,0x00,0xFF,0x00,0x00,0x00,0x00},
{0x08,0x06,0x01,0xFF,0x00,0x01,0x04,0x04,0x04,0x04,0x04,0xFF,0x02,0x02,0x02,0x00},/*"科",3*/
/* (16 X 16 , 宋体 )*/
{0x10,0x10,0x10,0xFF,0x10,0x90,0x08,0x88,0x88,0x88,0xFF,0x88,0x88,0x88,0x08,0x00},
{0x04,0x44,0x82,0x7F,0x01,0x80,0x80,0x40,0x43,0x2C,0x10,0x28,0x46,0x81,0x80,0x00},/*"技",4*/
/* (16 X 16 , 宋体 )*/
};
Finally, add main
int main(void)
{
while (1)
{
OLED_ShowString(30,0,(uint8_t*)"Arterytek",16);
OLED_ShowChinese(11,26,0,16);//雅
OLED_ShowChinese(33,26,1,16);//特
OLED_ShowChinese(55,26,2,16);//力
OLED_ShowChinese(77,26,3,16);//科
OLED_ShowChinese(99,26,4,16);//技
OLED_ShowNum(12,50,86,2,12);//电话
OLED_ShowChar(26,50,'-',12);
OLED_ShowNum(36,50,23,2,12);
OLED_ShowChar(50,50,'-',12);
OLED_ShowNum(60,50,6868,4,12);
OLED_ShowNum(92,50,8899,4,12);
OLED_Refresh();
return 0;
}
}
Also put SPI2_Init and OLED_Init in rt_hw_board_init, so that they will be initialized when the system starts.
Burn it,
This indicates that SPI is working normally.
|