请选择 进入手机版 | 继续访问电脑版

930电子网

 找回密码
 立即注册
艾克姆科技推出最新产品STC32G12K128开发板
查看: 13049|回复: 3

TWI总线传输错误

[复制链接]

4

主题

7

帖子

58

积分

注册会员

Rank: 2

积分
58
发表于 2020-7-16 17:31:10 | 显示全部楼层 |阅读模式
本帖最后由 NRF_ab 于 2020-7-16 17:32 编辑

外设传感初始化,写寄存器的时候,总是卡在71行那里,似乎是总线传输错误,请问为什么会出现这种错误,都有些什么原因呢,我该怎么解决。谢谢

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

52

主题

339

帖子

6487

积分

版主

Rank: 7Rank: 7Rank: 7

积分
6487
发表于 2020-7-16 17:37:00 | 显示全部楼层
这是因为发送失败,导致无法进入事件处理函数修改判断的标志,可能的原因:
1:检查引脚配置是不是对的。
2:I2C地址是不是对的。
回复 支持 反对

使用道具 举报

4

主题

7

帖子

58

积分

注册会员

Rank: 2

积分
58
 楼主| 发表于 2020-7-20 16:54:23 | 显示全部楼层
强光手电 发表于 2020-7-16 17:37
这是因为发送失败,导致无法进入事件处理函数修改判断的标志,可能的原因:
1:检查引脚配置是不是对的。
...

您好,引脚配置是对的,读写寄存器的地址也没问题,更改TWI的配置做了更改依然还是卡在那里。
代码是驱动max30102的,根据在github找了两个NRF52调max30102的代码改动了一下,这是驱动代码。请您帮忙看看
  1. #ifndef AT24C02_H__
  2. #define AT24C02_H__
  3. #include "nrf_delay.h"

  4. //I2C引脚定义
  5. #define TWI_SCL_M           25         //I2C SCL引脚
  6. #define TWI_SDA_M           27         //I2C SDA引脚


  7. #define max30102_INT     8

  8. #define max30102_ADDRESS        0x57

  9. #define max30102_WR_address 0xAE

  10. #define I2C_WRITE_ADDR (0xAE)
  11. #define I2C_READ_ADDR  (0xAF)

  12. #define REG_INTR_STATUS_1 0x00
  13. #define REG_INTR_STATUS_2 0x01
  14. #define REG_INTR_ENABLE_1 0x02
  15. #define REG_INTR_ENABLE_2 0x03
  16. #define REG_FIFO_WR_PTR 0x04
  17. #define REG_OVF_COUNTER 0x05
  18. #define REG_FIFO_RD_PTR 0x06
  19. #define REG_FIFO_DATA 0x07
  20. #define REG_FIFO_CONFIG 0x08
  21. #define REG_MODE_CONFIG 0x09
  22. #define REG_SPO2_CONFIG 0x0A
  23. #define REG_LED1_PA 0x0C
  24. #define REG_LED2_PA 0x0D
  25. #define REG_PILOT_PA 0x10
  26. #define REG_MULTI_LED_CTRL1 0x11
  27. #define REG_MULTI_LED_CTRL2 0x12
  28. #define REG_TEMP_INTR 0x1F
  29. #define REG_TEMP_FRAC 0x20
  30. #define REG_TEMP_CONFIG 0x21
  31. #define REG_PROX_INT_THRESH 0x30

  32. #define REG_REV_ID 0xFE
  33. #define REG_PART_ID 0xFF

  34. void twi_master_init(void);
  35. bool max30102_register_write(uint8_t register_address, uint8_t value);
  36. bool max30102_register_read(uint8_t register_address, uint8_t * destination, uint8_t number_of_bytes);

  37. void max30102_fifo_read (void);

  38. void max30102_setup (void);
  39. void max30102_reset (void);
  40. void max30102_init (void);

  41. #endif








  42. #include <stdbool.h>
  43. #include <stdint.h>
  44. #include <string.h>
  45. #include "nrf_drv_twi.h"
  46. //#include "twi_master.h"
  47. //#include "nrf_drv_timer.h"
  48. #include "max30102.h"
  49. #include "SEGGER_RTT.h"

  50. //TWI驱动程序实例ID,ID和外设编号对应,0:TWI0  1:TWI1
  51. #define TWI_INSTANCE_ID     0

  52. //TWI传输完成标志
  53. static volatile bool m_xfer_done = false;
  54. //定义TWI驱动程序实例,名称为m_twi
  55. static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);

  56. //TWI事件处理函数
  57. void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
  58. {
  59.     //判断TWI事件类型
  60.           switch (p_event->type)
  61.     {
  62.         //传输完成事件
  63.                           case NRF_DRV_TWI_EVT_DONE:
  64.             m_xfer_done = true;//置位传输完成标志
  65.             break;
  66.         default:
  67.             break;
  68.     }
  69. }
  70. //TWI初始化
  71. void twi_master_init(void)
  72. {
  73.     ret_code_t err_code;
  74.     //定义并初始化TWI配置结构体
  75.     const nrf_drv_twi_config_t twi_config = {
  76.        .scl                = TWI_SCL_M,  //定义TWI SCL引脚
  77.        .sda                = TWI_SDA_M ,  //定义TWI SDA引脚
  78.        .frequency          = NRF_DRV_TWI_FREQ_400K, //TWI速率
  79.        .interrupt_priority = APP_IRQ_PRIORITY_HIGH, //TWI优先级
  80.        .clear_bus_init     = true//初始化期间不发送9个SCL时钟
  81.     };
  82.     //初始化TWI
  83.     err_code = nrf_drv_twi_init(&m_twi, &twi_config, twi_handler, NULL);
  84.         //检查返回的错误代码
  85.     APP_ERROR_CHECK(err_code);
  86.     //使能TWI
  87.     nrf_drv_twi_enable(&m_twi);
  88. }

  89. bool max30102_register_write(uint8_t register_address, uint8_t value)
  90. {
  91.           ret_code_t err_code;
  92.           uint8_t tx_buf[2];
  93.        
  94.           //准备写入的数据
  95.                 tx_buf[0] = register_address;
  96.     tx_buf[1] = value;
  97.           //TWI传输完成标志设置为false
  98.                 m_xfer_done = false;
  99.                 //写入数据
  100.     err_code = nrf_drv_twi_tx(&m_twi, max30102_ADDRESS, tx_buf, sizeof(tx_buf), false);
  101.           //等待TWI总线传输完成
  102.                 while (m_xfer_done == false){}
  103.           if (NRF_SUCCESS != err_code)
  104.     {
  105.         return false;
  106.     }
  107.                 return true;       
  108. }

  109. bool max30102_register_read(uint8_t register_address, uint8_t * destination, uint8_t number_of_bytes)
  110. {
  111.           ret_code_t err_code;
  112.           //TWI传输完成标志设置为false
  113.                 m_xfer_done = false;
  114.           err_code = nrf_drv_twi_tx(&m_twi, max30102_ADDRESS, &register_address, 1, true);
  115.           //等待TWI总线传输完成
  116.                 while (m_xfer_done == false){}
  117.     if (NRF_SUCCESS != err_code)
  118.     {
  119.         return false;
  120.     }
  121.                 //TWI传输完成标志设置为false
  122.                 m_xfer_done = false;
  123.           err_code = nrf_drv_twi_rx(&m_twi, max30102_ADDRESS, destination, number_of_bytes);
  124.                 //等待TWI总线传输完成
  125.                 while (m_xfer_done == false){}
  126.                 if (NRF_SUCCESS != err_code)
  127.     {
  128.         return false;
  129.     }
  130.                 return true;
  131. }



  132. void max30102_setup (void)
  133. {
  134.        


  135.        
  136. //        max30102_register_write(REG_MODE_CONFIG,0x40);  //reset
  137. //        max30102_register_write(REG_MODE_CONFIG,0x40);  //reset
  138.        
  139.         (void)max30102_register_write(REG_INTR_ENABLE_1, 0xc0);

  140.         (void)max30102_register_write(REG_INTR_ENABLE_2, 0x00);
  141.        
  142.        
  143.         (void)max30102_register_write(REG_FIFO_WR_PTR, 0x00);        // Reset FIFO Write pointer
  144.         SEGGER_RTT_printf(0,"115\n");
  145.         (void)max30102_register_write(REG_OVF_COUNTER, 0x00); // Reset OVERFLOW_CTR pointer
  146.         (void)max30102_register_write(REG_FIFO_RD_PTR, 0x00);         // Reset FIFO READ pointer
  147.        
  148.         (void)max30102_register_write(REG_FIFO_CONFIG, 0x4F); //average sampling  = 4
  149.         (void)max30102_register_write(REG_MODE_CONFIG, 0x03); // ADC range = 4096nA | Sampling Rate = 100 Hz | LED pulseWidth = 411uS
  150.        
  151.         (void)max30102_register_write(REG_SPO2_CONFIG, 0x27); // 400 samples per second
  152.         (void)max30102_register_write(REG_LED1_PA, 0x5F); // LED1 current = 19 mA
  153.         (void)max30102_register_write(REG_LED2_PA, 0x5F); // LED2 current = 19 mA
  154.         (void)max30102_register_write(REG_PILOT_PA, 0x7F); // Pilot LED ~ 25mA
  155.        
  156.          
  157. }

  158. void max30102_reset (void)
  159.         {       
  160.        
  161.     max30102_register_write(REG_MODE_CONFIG, 0x40);       
  162.        
  163. }

  164. void max30102_init (void)
  165. {

  166.     uint8_t revID;
  167.     uint8_t partID;
  168.                 ret_code_t err_code;
  169.     err_code  = max30102_register_read (REG_PART_ID, &partID,sizeof(partID));
  170.                 APP_ERROR_CHECK(err_code);
  171.        
  172.     printf("MAX30102 PART ID: 0x%02x \r\n", partID);
  173.    
  174.        
  175.     err_code = max30102_register_read (REG_REV_ID, &revID,sizeof(revID));
  176.                 APP_ERROR_CHECK(err_code);
  177.        
  178.     printf("MAX30102 REV ID: 0x%02x \r\n", revID);
  179. }

  180. void max30102_fifo_read ()
  181. {
  182.           uint8_t clearINT1;
  183.                 uint8_t clearINT2;
  184.     uint32_t LEDdata=0;
  185.     uint8_t dataArray[6];
  186.     ret_code_t err_code;
  187. //    uint8_t FIFO_data;
  188.    
  189.                 //Clear Interrupts
  190.     err_code = max30102_register_read(REG_INTR_STATUS_1, &clearINT1,sizeof(clearINT1));
  191.                 err_code = max30102_register_read(REG_INTR_STATUS_2, &clearINT2,sizeof(clearINT2));
  192.   
  193.                
  194.                 err_code =max30102_register_read(REG_FIFO_DATA,dataArray,sizeof(dataArray));
  195.     APP_ERROR_CHECK(err_code);       
  196.                
  197.                 //Extract 3 bytes for IR LED
  198.     LEDdata=dataArray[0]<<16|dataArray[1]<<8 |dataArray[2];
  199.   
  200.                 printf("%d,",LEDdata) ;           // RED LED data               

  201.     LEDdata=0;

  202.                 //Extract 3 bytes for RED LED
  203.     LEDdata=dataArray[3]<<16|dataArray[4]<<8 |dataArray[5];
  204.           printf("%d\r\n", LEDdata);        // IR LED data
  205.        
  206.        
  207.        
  208.        
  209. }
复制代码
回复 支持 反对

使用道具 举报

1

主题

5

帖子

31

积分

新手上路

Rank: 1

积分
31
发表于 2021-12-17 17:52:06 | 显示全部楼层
大佬解决了吗,我也一直卡在这个地方,引脚没配错的话,那就考虑从设备地址有没有设错,我要是从设备地址都射错了的话,直接报错误,根本不会卡在这一句,大佬咋解决的
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|官方淘宝店|930电子网 ( 皖ICP备16000695号-2 )

GMT+8, 2024-3-29 00:21 , Processed in 0.072174 second(s), 22 queries .

快速回复 返回顶部 返回列表