分页和buffer_cache关系

通过试验展示分页查询过程中缓存利用效率,分析每次查询时Buffer Cache中的内容变化,验证分页查询是否能有效提升性能。
今天老大说分页时候会不会cache select语句的信息,然后再到下一页会不会快点,我说不会,我做个试验给他看看
首先清理内存中信息(重启数据库或者手动情况都一样)

alter system flush buffer_cache;

做一个10046时间,观察trace内容

set linesize 200
set timing on
alter session set tracefile_identifier='fenyetest0';
alter session set events '10046 trace name context forever,level 4';
variable start_num  number;
variable end_num number;

--1. 10000-2000数据
exec :start_num := 10000;
exec :end_num := 20000;

select *
       from (
             select rownum rn,t.*
             from
             (select  OPERATE_TIME,SERVER_INFO ,ACCESS_IP ,USERID from bi_logfile where    OPERATE_TIME>=(sysdate-10)) t
             where rownum<:end_num)
       where rn >:start_num;  

--查看buffer_cache中的内容
SQL> SELECT o.OBJECT_NAME, COUNT(*) NUMBER_OF_BLOCKS
     FROM DBA_OBJECTS o, V$BH bh
    WHERE o.DATA_OBJECT_ID = bh.OBJD
      AND o.OWNER        ='JSCNBI'
    GROUP BY o.OBJECT_NAME
    ORDER BY COUNT(*);   

OBJECT_NAME                              NUMBER_OF_BLOCKS
---------------------------------------- ----------------
BI_LOGFILE_IDX1                                       128
BI_LOGFILE                                           1160

--2. 20000-3000数据
exec :start_num := 20000;
exec :end_num := 30000;
select *
       from (
             select rownum rn,t.*
             from
             (select  OPERATE_TIME,SERVER_INFO ,ACCESS_IP ,USERID from bi_logfile where    OPERATE_TIME>=(sysdate-10)) t
             where rownum<:end_num)
       where rn >:start_num;  

--查看buffer_cache中的内容
SQL> SELECT o.OBJECT_NAME, COUNT(*) NUMBER_OF_BLOCKS
     FROM DBA_OBJECTS o, V$BH bh
    WHERE o.DATA_OBJECT_ID = bh.OBJD
      AND o.OWNER        ='JSCNBI'
    GROUP BY o.OBJECT_NAME
    ORDER BY COUNT(*);     2    3    4    5    6

OBJECT_NAME                              NUMBER_OF_BLOCKS
---------------------------------------- ----------------
BI_LOGFILE_IDX1                                       197
BI_LOGFILE                                           1665

--3. 30000-40000数据
exec :start_num := 30000;
exec :end_num := 40000;
select *
       from (
             select rownum rn,t.*
             from
             (select  OPERATE_TIME,SERVER_INFO ,ACCESS_IP ,USERID from bi_logfile where    OPERATE_TIME>=(sysdate-10)) t
             where rownum<:end_num)
       where rn >:start_num;  

--查看buffer_cache中的内容
SQL> SELECT o.OBJECT_NAME, COUNT(*) NUMBER_OF_BLOCKS
     FROM DBA_OBJECTS o, V$BH bh
    WHERE o.DATA_OBJECT_ID = bh.OBJD
      AND o.OWNER        ='JSCNBI'
    GROUP BY o.OBJECT_NAME
    ORDER BY COUNT(*);  2    3    4    5    6

OBJECT_NAME                              NUMBER_OF_BLOCKS
---------------------------------------- ----------------
BI_LOGFILE_IDX1                                       222
BI_LOGFILE                                           1983


alter session set events '10046 trace name context off';


格式化trace内容,格式的时候不合并相同的sql,也不查看sys sql
[oracle@DB-87 udump]$ tkprof jscnbi_ora_27002_fenyetest0.trc fenyetest0.sql sys=no aggregate=no

格式化以后的结果如下:
TKPROF: Release 10.2.0.4.0 - Production on Fri Jul 13 13:57:48 2012

Copyright (c) 1982, 2007, Oracle.  All rights reserved.

Trace file: jscnbi_ora_27002_fenyetest0.trc
Sort options: default

********************************************************************************
count    = number of times OCI procedure was executed
cpu      = cpu time in seconds executing
elapsed  = elapsed time in seconds executing
disk     = number of physical reads of buffers from disk
query    = number of buffers gotten for consistent read
current  = number of buffers gotten in current mode (usually for update)
rows     = number of rows processed by the fetch or execute call
--------------------------------------------------------------------------------

*** SESSION ID:(309.11) 2012-07-13 13:56:17.864

********************************************************************************

BEGIN :start_num := 10000; END;


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           1
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        2      0.00       0.00          0          0          0           1

Misses in library cache during parse: 1
Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 59  
********************************************************************************

BEGIN :end_num := 20000; END;


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           1
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        2      0.00       0.00          0          0          0           1

Misses in library cache during parse: 1
Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 59  
********************************************************************************

select *
       from (
             select rownum rn,t.*
             from
             (select  OPERATE_TIME,SERVER_INFO ,ACCESS_IP ,USERID from bi_logfile where    OPERATE_TIME>=(sysdate-10)) t
             where rownum<:end_num)
       where rn >:start_num

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch      668      0.12       0.09       1274      11081          0        9999
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total      670      0.12       0.10       1274      11081          0        9999

Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 59  

Rows     Row Source Operation
-------  ---------------------------------------------------
   9999  VIEW  (cr=11081 pr=1274 pw=0 time=84845 us)
  19999   COUNT STOPKEY (cr=11081 pr=1274 pw=0 time=81070 us)
  19999    TABLE ACCESS BY INDEX ROWID BI_LOGFILE (cr=11081 pr=1274 pw=0 time=81068 us)
  19999     INDEX RANGE SCAN BI_LOGFILE_IDX1 (cr=743 pr=127 pw=0 time=676 us)(object id 52405)

********************************************************************************

BEGIN :start_num := 20000; END;


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           1
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        2      0.00       0.00          0          0          0           1

Misses in library cache during parse: 1
Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 59  
********************************************************************************

BEGIN :end_num := 30000; END;


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           1
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        2      0.00       0.00          0          0          0           1

Misses in library cache during parse: 1
Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 59  
********************************************************************************

select *
       from (
             select rownum rn,t.*
             from
             (select  OPERATE_TIME,SERVER_INFO ,ACCESS_IP ,USERID from bi_logfile where    OPERATE_TIME>=(sysdate-10)) t
             where rownum<:end_num)
       where rn >:start_num

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch      668      0.15       0.11        586      16100          0        9999
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total      670      0.15       0.11        586      16100          0        9999

Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 59  

Rows     Row Source Operation
-------  ---------------------------------------------------
   9999  VIEW  (cr=16100 pr=586 pw=0 time=111363 us)
  29999   COUNT STOPKEY (cr=16100 pr=586 pw=0 time=120067 us)
  29999    TABLE ACCESS BY INDEX ROWID BI_LOGFILE (cr=16100 pr=586 pw=0 time=90064 us)
  29999     INDEX RANGE SCAN BI_LOGFILE_IDX1 (cr=781 pr=78 pw=0 time=74 us)(object id 52405)

********************************************************************************

BEGIN :start_num := 30000; END;


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           1
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        2      0.00       0.00          0          0          0           1

Misses in library cache during parse: 1
Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 59  
********************************************************************************

BEGIN :end_num := 40000; END;


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           1
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        2      0.00       0.00          0          0          0           1

Misses in library cache during parse: 1
Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 59  
********************************************************************************

select *
       from (
             select rownum rn,t.*
             from
             (select  OPERATE_TIME,SERVER_INFO ,ACCESS_IP ,USERID from bi_logfile where    OPERATE_TIME>=(sysdate-10)) t
             where rownum<:end_num)
       where rn >:start_num

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch      668      0.13       0.11        345      20964          0        9999
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total      670      0.13       0.11        345      20964          0        9999

Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 59  

Rows     Row Source Operation
-------  ---------------------------------------------------
   9999  VIEW  (cr=20964 pr=345 pw=0 time=109073 us)
  39999   COUNT STOPKEY (cr=20964 pr=345 pw=0 time=120041 us)
  39999    TABLE ACCESS BY INDEX ROWID BI_LOGFILE (cr=20964 pr=345 pw=0 time=120035 us)
  39999     INDEX RANGE SCAN BI_LOGFILE_IDX1 (cr=817 pr=16 pw=0 time=109 us)(object id 52405)

********************************************************************************

alter session set events '10046 trace name context off'


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        2      0.00       0.00          0          0          0           0

Misses in library cache during parse: 0
Parsing user id: 59  



********************************************************************************

OVERALL TOTALS FOR ALL NON-RECURSIVE STATEMENTS

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse       10      0.00       0.00          0          0          0           0
Execute     10      0.01       0.01          0          0          0           6
Fetch     2004      0.41       0.32       2205      48145          0       29997
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total     2024      0.42       0.34       2205      48145          0       30003

Misses in library cache during parse: 7
Misses in library cache during execute: 6


OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse       15      0.00       0.00          0          0          0           0
Execute    220      0.06       0.06          0          0          0           0
Fetch      236      0.01       0.01         48        699          0        2077
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total      471      0.08       0.08         48        699          0        2077

Misses in library cache during parse: 13
Misses in library cache during execute: 13

   10  user  SQL statements in session.
  220  internal SQL statements in session.
  230  SQL statements in session.
********************************************************************************
Trace file: jscnbi_ora_27002_fenyetest0.trc
Trace file compatibility: 10.01.00
Sort options: default

       1  session in tracefile.
      10  user  SQL statements in trace file.
     220  internal SQL statements in trace file.
     230  SQL statements in trace file.
      21  unique SQL statements in trace file.
    7288  lines in trace file.
      35  elapsed seconds in trace file.



从上面的结果看出,1、2、3次分页过程Fetch disk中内容并没有减少。

后来想想其实根本没有必要这么麻烦,我们只要取0行数据,看buffer_cache有没有就ok了,
--取0行数据
select *
       from (
             select rownum rn,t.*
             from
             (select  OPERATE_TIME,SERVER_INFO ,ACCESS_IP ,USERID from bi_logfile where    OPERATE_TIME>=(sysdate-10)) t
             where rownum<2)
       where rn >1;
--查看buffer cache中有没有bi_logfile表的数据

SQL> SELECT o.OBJECT_NAME, COUNT(*) NUMBER_OF_BLOCKS
     FROM DBA_OBJECTS o, V$BH bh
    WHERE o.DATA_OBJECT_ID = bh.OBJD
      AND o.OWNER        ='JSCNBI'
    GROUP BY o.OBJECT_NAME
    ORDER BY COUNT(*); 


结果里面是没有的,说明我的推断是正确的。

#include "spi_master_server.h" // SPI 头文件 #include <SPI.h> #include "helper.h" #include <Adafruit_SPIDevice.h> // Adafruit SPIDevice 封装类对象 Adafruit_SPIDevice *spi_dev = nullptr; #define SPI_MASTER_ENABLE 1 // 自定义引脚定义(与原从机对应) #define MY_CS 12 #define MY_SCK 13 #define MY_MOSI 14 #define MY_MISO 11 // 超时时间 #define SPI_READ_TIMEROUT 999999 // us // 缓冲区大小:32 字节 static constexpr size_t SPI_BUFFER_SIZE = 16 * 2; static constexpr size_t QUEUE_SIZE = 1; // 保留兼容字段,实际主机不用队列 // 发送/接收缓冲区 static uint8_t spi_tx_buf[SPI_BUFFER_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8}; // 测试数据 static uint8_t spi_rx_buf[SPI_BUFFER_SIZE] = {0}; // 命令发送缓冲区 static uint8_t spi_tx_cmd_buf[SPI_BUFFER_SIZE] = {0}; // 发送缓存配置 #define SPI_TX_CACHE_BUF_LEN 1024 #define SPI_TX_PAGE_BUF_LEN (SPI_BUFFER_SIZE - 3) // 协议头占3字节: 0xAB + len + seq // 用户命令定义 #define SPI_USER_CMD_NULL 0 #define SPI_USER_CMD_READ 1 #define SPI_USER_CMD_WRITE 2 static volatile int spi_current_cmd = 0; // 全局缓存及状态 static volatile uint8_t spi_tx_cache_buf[SPI_TX_CACHE_BUF_LEN] = {0}; static volatile int spi_tx_cache_buf_cnt = 0; static volatile int spi_current_cmd = 0; static volatile int spi_current_cmd_len = 0; // 标志位 static uint8_t spi_send_busy = 0; static uint8_t spi_send_mode = 1; // 默认使用 SPI 发送 int spi_master_data_cache_add(uint8_t *data, uint16_t len) { if (len > 1024) len = 1024; memcpy((void*)spi_tx_cache_buf, data, len); spi_tx_cache_buf_cnt = len; return 0; } void IRAM_ATTR my_pre_transfer_cb() { int page_len = SPI_BUFFER_SIZE - 3; int total_pages = (spi_tx_cache_buf_cnt + page_len - 1) / page_len; int current_page = spi_current_cmd_len; memset(spi_tx_buf, 0, SPI_BUFFER_SIZE); spi_tx_buf[0] = 0xAB; if (current_page >= total_pages) { spi_tx_buf[1] = 0; // no data spi_tx_buf[2] = current_page; return; } int offset = current_page * page_len; int data_len = min(page_len, spi_tx_cache_buf_cnt - offset); spi_tx_buf[1] = data_len; spi_tx_buf[2] = current_page; for (int i = 0; i < data_len; i++) { spi_tx_buf[3 + i] = spi_tx_cache_buf[offset + i]; } spi_current_cmd_len++; } static void spi_master_task(void *pvParameters) { while (1) { if (digitalRead(MY_CS) == LOW) { // 从机请求通信 memset(spi_rx_buf, 0, SPI_BUFFER_SIZE); // 使用 writeThenRead 模拟全双工传输 bool success = spi_dev->write_then_read( spi_tx_buf, SPI_BUFFER_SIZE, // 发送的数据长度 spi_rx_buf, SPI_BUFFER_SIZE, // 接收缓冲区长度 0 // 命令/地址字节个数(无) ); if (success) { printf_log_hex("RX from slave", spi_rx_buf, SPI_BUFFER_SIZE); printf_log_hex("TX to slave", spi_tx_buf, SPI_BUFFER_SIZE); if (spi_rx_buf[1] == 0xAA && spi_rx_buf[2] == 0x01) { spi_current_cmd = SPI_USER_CMD_READ; } } else { printf("SPI transfer failed!\n"); } } vTaskDelay(5 / portTICK_PERIOD_MS); } } void spi_master_client_init(void) { #if SPI_MASTER_ENABLE // Step 1: 配置硬件 SPI 总线(HSPI) SPIClass hspi(HSPI); // 创建 HSPI 实例 hspi.begin(MY_SCK, MY_MISO, MY_MOSI, MY_CS); // 设置引脚 // Step 2: 创建 Adafruit_SPIDevice 对象 spi_dev = new Adafruit_SPIDevice( MY_CS, // CS pin 1000000, // 频率 1MHz SPI_BITORDER_MSBFIRST, SPI_MODE3, // CPOL=1, CPHA=1 &hspi // 使用自定义 SPIClass ); if (!spi_dev->begin()) { printf("SPI Device init failed!\n"); return; } pinMode(MY_CS, INPUT_PULLUP); // 创建任务 xTaskCreate(spi_master_task, "spi_master_task", 8192, NULL, 2, NULL); #endif }
09-26
#include "spi_slave_server.h" // spi #include <SPI.h> #include <ESP32SPISlave.h> #include "helper.h" ESP32SPISlave slave; // 创建ESP32SPISlave对象 #define SPI_SPLAVE_ENABLE 1 // Adafruit_SPIDevice spi_dev = Adafruit_SPIDevice(SPIDEVICE_CS, 1000000, SPI_BITORDER_MSBFIRST, SPI_MODE0); #define MY_CS 12 #define MY_SCK 13 #define MY_MOSI 14 #define MY_MISO 11 // 自定义spi引脚 // SPIClass my_spi(HSPI); // 创建SPIClass对象my_spi #define SPI_READ_TIMEROUT 999999 // 10 // 50ms static constexpr size_t SPI_BUFFER_SIZE = 16 * 2; // 上位机 测试42 字节数据正常,模式1,定义SPI通信缓冲区大小为32字节 static constexpr size_t QUEUE_SIZE = 1; static uint8_t spi_tx_buf[SPI_BUFFER_SIZE]{1, 2, 3, 4, 5, 6, 7, 8};//发送缓冲区,预置测试数据1-8 static uint8_t spi_rx_buf[SPI_BUFFER_SIZE]{0, 0, 0, 0, 0, 0, 0, 0};//接收缓冲区,初始化为全0 static uint8_t spi_tx_cmd_buf[SPI_BUFFER_SIZE]{0, 0, 0, 0, 0, 0, 0, 0};//命令发送缓冲区,初始化为全0 #define SPI_TX_CACHE_BUF_LEN 1024//发送缓存总大小为1024字节 #define SPI_TX_PAGE_BUF_LEN (SPI_BUFFER_SIZE - 3) // spi 回复数据的最大长度,单次SPI通信有效数据长度 #define SPI_USER_CMD_NULL 0//空指令 #define SPI_USER_CMD_READ 1//读数据 #define SPI_USER_CMD_WRITE 2//写数据 static volatile uint8_t spi_tx_cache_buf[SPI_TX_CACHE_BUF_LEN]{0, 0, 0, 0, 0, 0, 0, 0}; static volatile int spi_tx_cache_buf_cnt = 0; // spi 发送缓存区数据长度 // static int spi_tx_page_cnt=0;//spi 发送缓存区数据长度 static volatile int spi_current_cmd = 0, spi_current_cmd_len = 0; static uint8_t spi_send_busy = 0;//发送忙标志位 static uint8_t spi_send_mode = 1; // 发送模式选择发串口 int spi_slave_data_cache_add(uint8_t *data, uint16_t len) { if (len > SPI_TX_CACHE_BUF_LEN) { /* code */ len = SPI_TX_CACHE_BUF_LEN; } for (int i = 0; i < len; i++) { /* code */ spi_tx_cache_buf[i] = data[i]; } spi_tx_cache_buf_cnt = len; return 0; } void IRAM_ATTR my_post_setup_cb(spi_slave_transaction_t *trans)//强制将函数编译到内部RAM,确保中断快速响应 { static uint8_t test_send = 0x11;//静态测试变量 int over_len = spi_tx_cache_buf_cnt - spi_current_cmd_len * SPI_TX_PAGE_BUF_LEN; // 剩余待发送数据量 = 总缓存量 - 已发送页数×单页容量 // Serial.printf("spi slave read over_len = %d cache= %d\r\n", over_len,spi_tx_cache_buf_cnt); if (over_len < 0)//数据已全部发送完成时的保护判断 { return; } if (over_len > SPI_TX_PAGE_BUF_LEN)//单包数据超限时截断处理 { over_len = SPI_TX_PAGE_BUF_LEN; } memset((void *)spi_tx_buf, 0, SPI_BUFFER_SIZE);//清空发送缓冲区(32字节全置0) spi_tx_buf[0] = 0xab; spi_tx_buf[1] = over_len; // 实际长度 spi_tx_buf[2] = spi_current_cmd_len; // 包号 for (int i = 0; i < over_len; i++)//将缓存数据拷贝到发送缓冲区(偏移3字节协议头) { /* code */ spi_tx_buf[3 + i] = spi_tx_cache_buf[spi_current_cmd_len * SPI_TX_PAGE_BUF_LEN + i]; } spi_current_cmd_len++;//包序号自增 if (spi_current_cmd_len >= 3)//包序号归零 { /* code */ spi_current_cmd_len = 0; } // Serial.printf("spi slave read cmd_len = %d ,%02x %02x %02x\n", over_len,spi_tx_buf[1], spi_tx_buf[2], spi_tx_buf[3]); } /*** * spi slave 任务 * 模式设置4 字节 * spi cmd: 0xaa 0x01 ,切换模式1,发串口1数据 * * 数据读取 64字节,发aa 01 0xff ...... * spi ack: 0xaa 0x01 ,剩余字节全部回复串口数据 */ static void spi_slave_task(void *pvParameters)//FreeRTOS任务函数标准定义 { size_t received_bytes; // 记录接收到的数据字节数 while (1)//无限循环,保持任务持续运行 { /* code */ // initializeBuffers(tx_buf, rx_buf, BUFFER_SIZE); int cs = digitalRead(MY_CS);//读取片选信号(CS)引脚状态,检测主设备是否选中本从设备 #if SPI_SPLAVE_ENABLE//条件编译开关,控制SPI从设备功能是否启用 if (cs == 0)//当片选信号为低电平时(表示被主设备选中)执行通信 { memset(spi_rx_buf, 0, SPI_BUFFER_SIZE);//清空接受缓存区 // start and wait to complete one BIG transaction (same data will be received from slave) received_bytes = slave.transfer(spi_tx_buf, spi_rx_buf, SPI_BUFFER_SIZE, SPI_READ_TIMEROUT); //tx_buf:发送缓冲区,rx_buf:接收缓冲区,BUFFER_SIZE:传输长度,READ_TIMEROUT:超时时间 // verify and dump difference with received data if (received_bytes > 0)//成功接收到数据时处理数据 { printf_log_hex("slave", spi_rx_buf, received_bytes); printf_log_hex("slave", spi_tx_buf, received_bytes); if (spi_rx_buf[1] == 0xaa && spi_rx_buf[2] == 0x01) // 当收到0xAA01时设置为读取模式,读取数据cmd { spi_current_cmd = SPI_USER_CMD_READ; // spi_current_cmd_len = spi_rx_buf[3]; } my_post_setup_cb(NULL);//调用之前分析的发送回调函数 } } #endif vTaskDelay(5 / portTICK_PERIOD_MS);//任务延时5ms } } /** * spi slave 初始化 * SPI_MODE1 * sck 13 * miso 14 * mosi 11 * ss 12 * */ void spi_slave_server_init(void) { #if SPI_SPLAVE_ENABLE slave.setDataMode(SPI_MODE3); // default: SPI_MODE1 slave.setQueueSize(QUEUE_SIZE); // default: 1 slave.begin(HSPI, 13, 11, 14, 12); // default: HSPI (please refer README for pin assignments) // pinMode(10, INPUT); // slave.setPostSetupCb(my_post_setup_cb); xTaskCreate(spi_slave_task, "spi_slave_task", 8 * 1024, NULL, 2, NULL); #endif } 仿照上述格式,写spi主机代码
09-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值