Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. void user_delay_ms(uint32_t period) {
  2. os_thread_sleep(os_msec_to_ticks(period));
  3. }
  4.  
  5. int8_t user_i2c_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len) {
  6. int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */
  7.  
  8. mdev_t *i2c_dev = i2c_drv_open(I2C1_PORT, dev_id);
  9.  
  10.  
  11. uint8_t write_data[1];
  12. write_data[0] = reg_addr;
  13. /*wmprintf("user_i2c_read\r\n");
  14. wmprintf("Writing reg_addr: %d\r\n", reg_addr);
  15. wmprintf("write_data[0]: %d\r\n", write_data[0]);*/
  16.  
  17. int write_len = i2c_drv_write(i2c_dev, write_data, 1);
  18. if (write_len != 1) {
  19. wmprintf("error BME680 write_len is %d\r\n", write_len);
  20. i2c_drv_close(i2c_dev);
  21. return WM_FAIL;
  22. }
  23.  
  24. int read_len = i2c_drv_read(i2c_dev, reg_data, len);
  25. if (read_len != len) {
  26. wmprintf("error BME680 read_len is %d\r\n", read_len);
  27. i2c_drv_close(i2c_dev);
  28. return WM_FAIL;
  29. }
  30.  
  31. /*wmprintf("Reading reg_data: ");
  32. int s;
  33. for(s=0; s<len;s++){
  34. wmprintf("%02X ", reg_data[s]);
  35. }
  36. wmprintf("\r\n");*/
  37.  
  38. i2c_drv_close(i2c_dev);
  39.  
  40. return rslt;
  41. }
  42.  
  43. int8_t user_i2c_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len) {
  44. int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */
  45.  
  46. mdev_t *i2c_dev = i2c_drv_open(I2C1_PORT, dev_id);
  47.  
  48. uint8_t write_data[len + 1];
  49. write_data[0] = reg_addr;
  50. memcpy(&write_data[1], reg_data, len);
  51.  
  52. /*wmprintf("user_i2c_write\r\n");
  53. wmprintf("Writing reg_addr: %d\r\n", reg_addr);
  54. wmprintf("Writing reg_data: ");
  55. int k;
  56. for (k=0; k<len; k++){
  57. wmprintf("%d ", reg_data[k]);
  58. }
  59. wmprintf("\r\n");
  60.  
  61. wmprintf("total buffer\r\n");
  62. for (k=0; k<len+1; k++){
  63. wmprintf("%d ", write_data[k]);
  64. }
  65. wmprintf("\r\n");*/
  66.  
  67. int write_len = i2c_drv_write(i2c_dev, write_data, len);
  68.  
  69. if (write_len != len) {
  70. //wmprintf("error ENS210_REG_SYS_CTRL write_len is %d\r\n", write_len);
  71. return -WM_FAIL;
  72. }
  73.  
  74. i2c_drv_close(i2c_dev);
  75.  
  76. return rslt;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement