当前位置:网站首页>Comparison between hardware SPI and software analog SPI rate
Comparison between hardware SPI and software analog SPI rate
2022-07-28 01:58:00 【freemote】
1、 Preface
This article USES the AT32F425 drive RC522 To test SPI1 Rate , About RC522 The detailed code operation of can be found in the post 【GD32L233C-START】 Hardware SPI1 drive RC522.
2、 About AT32F425 Of SPI

AT32F425 The series has at most 3 individual spi, This article USES the SPI1.
3、SPI1 The maximum rate of

SPI1 Hang on APB2 On the bus ,APB2 The maximum speed of the bus is 96MHz;
SPI The maximum rate of is fPCLK/2, namely 48Mhz.
4、 Code implementation
/*SPI1 :
PA4/CS
PA5/SCK
PA6/MISO
PA7/MOSI
*/
(1) simulation SPI
#define SET_SPI_CS() gpio_bits_set(GPIOA, GPIO_PINS_4)
#define CLR_SPI_CS() gpio_bits_reset(GPIOA, GPIO_PINS_4)
#define SET_SPI_SCLK() gpio_bits_set(GPIOA,GPIO_PINS_5)
#define CLR_SPI_SCLK() gpio_bits_reset(GPIOA,GPIO_PINS_5)//SCL=SCLK
#define SET_SPI_MOSI() gpio_bits_set(GPIOA,GPIO_PINS_7)
#define CLR_SPI_MOSI() gpio_bits_reset(GPIOA,GPIO_PINS_7)//SDO=MOSI
#define READ_SPI_MISO() gpio_input_data_bit_read(GPIOA,GPIO_PINS_6) //SDI=MISO
void SpiIoInit(void)
{
crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
gpio_init_type gpio_initstructure;
/* configure the lcd gpio */
gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_initstructure.gpio_mode = GPIO_MODE_OUTPUT;
gpio_initstructure.gpio_pins = GPIO_PINS_4|GPIO_PINS_5|GPIO_PINS_7;
gpio_initstructure.gpio_pull = GPIO_PULL_NONE;
gpio_init(GPIOA, &gpio_initstructure);
gpio_bits_set(GPIOA,GPIO_PINS_4|GPIO_PINS_5|GPIO_PINS_7);
gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_initstructure.gpio_mode = GPIO_MODE_INPUT;
gpio_initstructure.gpio_pins = GPIO_PINS_6;
gpio_initstructure.gpio_pull = GPIO_PULL_NONE;
gpio_init(GPIOA, &gpio_initstructure);
gpio_bits_set(GPIOA,GPIO_PINS_6);
}
uint8_t SPIWriteByte(uint8_t dat)
{
uint16_t ret=0;
u8 i;
CLR_SPI_CS();
for(i=0;i<8;i++)
{
if(dat&0x80)
{
SET_SPI_MOSI();
}
else
{
CLR_SPI_MOSI();
}
dat<<=1;
// delay_us(1);
SET_SPI_SCLK();
ret<<=1;
if(READ_SPI_MISO())
{
ret+=1;
}
// delay_us(1);
CLR_SPI_SCLK();
}
SET_SPI_CS();
return (ret&0x0ff);
}
(2) Hardware SPI
void SpiIoInit(void)
{
gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_initstructure.gpio_pull = GPIO_PULL_UP;
gpio_initstructure.gpio_mode = GPIO_MODE_OUTPUT;
gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_initstructure.gpio_pins = GPIO_PINS_4;
gpio_init(GPIOA, &gpio_initstructure);
gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE5, GPIO_MUX_0);
gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE6, GPIO_MUX_0);
gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE7, GPIO_MUX_0);
gpio_default_para_init(&gpio_initstructure);
gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_initstructure.gpio_pull = GPIO_PULL_DOWN;
gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_initstructure.gpio_pins = GPIO_PINS_5;
gpio_init(GPIOA, &gpio_initstructure);
gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_initstructure.gpio_pull = GPIO_PULL_UP;
gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_initstructure.gpio_pins = GPIO_PINS_6;
gpio_init(GPIOA, &gpio_initstructure);
gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_initstructure.gpio_pull = GPIO_PULL_UP;
gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_initstructure.gpio_pins = GPIO_PINS_7;
gpio_init(GPIOA, &gpio_initstructure);
spi_init_type spi_init_struct;
crm_periph_clock_enable(CRM_SPI1_PERIPH_CLOCK, TRUE);
spi_default_para_init(&spi_init_struct);
spi_init_struct.transmission_mode = SPI_TRANSMIT_FULL_DUPLEX;
spi_init_struct.master_slave_mode = SPI_MODE_MASTER;
spi_init_struct.mclk_freq_division = SPI_MCLK_DIV_8;
spi_init_struct.first_bit_transmission = SPI_FIRST_BIT_MSB;
spi_init_struct.frame_bit_num = SPI_FRAME_8BIT;
spi_init_struct.clock_polarity = SPI_CLOCK_POLARITY_LOW;
spi_init_struct.clock_phase = SPI_CLOCK_PHASE_1EDGE;
spi_init_struct.cs_mode_selection = SPI_CS_SOFTWARE_MODE;
spi_init(SPI1, &spi_init_struct);
spi_enable(SPI1, TRUE);
}
uint8_t SPIWriteByte(uint8_t dat)
{
uint16_t ret=0;
CLR_SPI_CS();
while(spi_i2s_flag_get(SPI1, SPI_I2S_TDBE_FLAG) == RESET) {
};
spi_i2s_data_transmit(SPI1, dat);
while(spi_i2s_flag_get(SPI1, SPI_I2S_RDBF_FLAG) == RESET) {
};
ret=spi_i2s_data_receive(SPI1);
SET_SPI_CS();
return (ret&0x0ff);
}
Use hardware SPI When testing ,RC522 The maximum speed of can only reach 12MHZ, That is to say 8 frequency division , The rate is too high, which leads to communication failure .
5、 Rate measurement
Here, use an oscilloscope directly CLK Waveform of , Grasp the whole with logical analysis SPI Waveform of .
(1) simulation SPI

It can be seen that , Using simulation SPI The time is 1.8MHz, Grabbing with logic analyzer is also the result .
(2) Hardware SPI

It can be seen that , Use hardware SPI The time is 12MHz, Consistent with the software settings , Grabbing with logic analyzer is also the result .
Comprehensive comparative measurement , You can find , Hardware SPI The speed is much higher than that of software SPI, So in order to improve cpu Utilization ratio , Try to use hardware SPI.
边栏推荐
猜你喜欢

Lambda expressions and stream streams

Enterprise operation and maintenance practice - using aliyun container image service to pull and build images of overseas GCR and quay warehouses

Leveraging the blue ocean of household appliances consumption with "digital channels", the dealer online system enables enterprises to further their business

HCIP第十二天笔记

Real time synchronization and conversion of massive data based on Flink CDC

物企大变局时代,SRM供应商采购系统助力企业打造物业采购数字化标杆

开源飞控(PX4、ardupilot)

网易云仿写

What is method and methodology: understand the underlying logic of self-improvement

Niuke net question brushing training (III)
随机推荐
Lambda expressions and stream streams
企业运维实践-使用Aliyun容器镜像服务对海外gcr、quay仓库镜像进行镜像拉取构建
Solution of digital commerce cloud supply chain centralized purchase management system: centralized purchase system management mode, digital control of enterprise materials
GBase 8c 事务ID和快照(六)
Discussion on PHP using some functions bypass WAF
自定义类型:结构体,枚举,联合
The story of the third uncle
HRD 1. a simple and reliable HRD detection method
Recursion related exercises
小散量化炒股记|量化系统中数据是源头,教你搭建一款普适的数据源框架
Interview question 01.09. string rotation
VLAN实验
数据安全与隐私计算峰会-可证明安全:学习
Prediction of charitable donation behavior by EEG multivariate model analysis
马克的故事
幸福的晚年
EEG多元模式分析预测慈善捐赠行为
HCIP第十五天
企业运维实践-使用Aliyun容器镜像服务对海外gcr、quay仓库镜像进行镜像拉取构建
Gbase 8C transaction ID and snapshot (VI)