I2C_demo.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /** @file
  2. I2C_demo.c
  3. @brief
  4. This file is demo of I2C.
  5. */
  6. /*================================================================
  7. Copyright (c) 2020 Quectel Wireless Solution, Co., Ltd. All Rights Reserved.
  8. Quectel Wireless Solution Proprietary and Confidential.
  9. =================================================================*/
  10. /*=================================================================
  11. EDIT HISTORY FOR MODULE
  12. This section contains comments describing changes made to the module.
  13. Notice that changes are listed in reverse chronological order.
  14. WHEN WHO WHAT, WHERE, WHY
  15. ------------ ------- -------------------------------------------------------------------------------
  16. =================================================================*/
  17. /*===========================================================================
  18. * include files
  19. ===========================================================================*/
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include "ql_api_osi.h"
  24. #include "ql_log.h"
  25. #include "ql_api_camera.h"
  26. #include "ql_i2c.h"
  27. #include "I2C_demo.h"
  28. /*===========================================================================
  29. * Macro Definition
  30. ===========================================================================*/
  31. #define QL_APP_I2C_LOG_LEVEL QL_LOG_LEVEL_INFO
  32. #define QL_APP_I2C_LOG(msg, ...) QL_LOG(QL_APP_I2C_LOG_LEVEL, "QL_APP_I2C", msg, ##__VA_ARGS__)
  33. #define QL_APP_I2C_LOG_PUSH(msg, ...) QL_LOG_PUSH("QL_APP_I2C", msg, ##__VA_ARGS__)
  34. #define QL_I2C_TASK_STACK_SIZE 1024
  35. #define QL_I2C_TASK_PRIO APP_PRIORITY_NORMAL
  36. #define QL_I2C_TASK_EVENT_CNT 5
  37. #define SalveAddr_w_8bit (0x42 >> 1)
  38. #define SalveAddr_r_8bit (0x43 >> 1)
  39. /*reserved*/
  40. #define SalveAddr_w_16bit (0xff >> 1)
  41. #define SalveAddr_r_16bit (0xff >> 1)
  42. #define demo_for_8bit_or_16bit (1) //1:test 8bit register address 0:test 16bit register address
  43. /*===========================================================================
  44. * Struct
  45. ===========================================================================*/
  46. /*===========================================================================
  47. * Enum
  48. ===========================================================================*/
  49. /*===========================================================================
  50. * Variate
  51. ===========================================================================*/
  52. /*===========================================================================
  53. * Functions
  54. ===========================================================================*/
  55. void ql_i2c_demo_thread(void *param)
  56. {
  57. #if demo_for_8bit_or_16bit
  58. /*test 8bit register address*/
  59. uint8_t read_data = 0;
  60. uint8_t data = 0xaa;
  61. /*operate the camera for the example*/
  62. ql_CamInit(320, 240);
  63. ql_CamPowerOn();
  64. ql_I2cInit(i2c_1, STANDARD_MODE);
  65. while(1)
  66. {
  67. QL_APP_I2C_LOG("I2C read_data = 0x%x", read_data);
  68. ql_I2cRead(i2c_1, SalveAddr_r_8bit, 0xf0, &read_data, 1);
  69. QL_APP_I2C_LOG("I2C read_data = 0x%x", read_data);
  70. ql_I2cWrite(i2c_1, SalveAddr_w_8bit, 0x55, &data, 1);
  71. read_data = 0;
  72. ql_rtos_task_sleep_ms(200);
  73. }
  74. #else
  75. /*test 16bit register address*/
  76. uint8_t read_data = 0;
  77. uint8_t data = 0xff;
  78. /*set i2c pin function*/
  79. ql_pin_set_func(41, 0);
  80. ql_pin_set_func(42, 0);
  81. ql_I2cInit(i2c_1, STANDARD_MODE);
  82. while(1)
  83. {
  84. QL_APP_I2C_LOG("I2C read_data = 0x%x", read_data);
  85. ql_I2cRead_16bit_addr(i2c_1, SalveAddr_r_16bit, 0xffff, &read_data, 1);
  86. QL_APP_I2C_LOG("I2C read_data = 0x%x", read_data);
  87. ql_I2cWrite_16bit_addr(i2c_1, SalveAddr_w_16bit, 0xffff, &data, 1);
  88. read_data = 0;
  89. ql_rtos_task_sleep_ms(200);
  90. }
  91. #endif
  92. }
  93. void ql_i2c_demo_init(void)
  94. {
  95. QlI2CStatus err = QL_OSI_SUCCESS;
  96. ql_task_t i2c_task = NULL;
  97. err = ql_rtos_task_create(&i2c_task, QL_I2C_TASK_STACK_SIZE, QL_I2C_TASK_PRIO, "I2C DEMO", ql_i2c_demo_thread, NULL, QL_I2C_TASK_EVENT_CNT);
  98. if (err != QL_OSI_SUCCESS)
  99. {
  100. QL_APP_I2C_LOG("i2ctest1 demo task created failed");
  101. }
  102. }