MLX90640_I2C_Driver.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * @copyright (C) 2017 Melexis N.V.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. #include "luat_base.h"
  18. #include "luat_mem.h"
  19. #include "MLX90640_I2C_Driver.h"
  20. extern mlx90640_i2c_t mlx90640_i2c;
  21. void MLX90640_I2CInit(void)
  22. {
  23. }
  24. int MLX90640_I2CRead(uint8_t slaveAddr, uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data)
  25. {
  26. int ret = 0;
  27. int cnt = 0;
  28. int i = 0;
  29. char* i2cData = NULL;
  30. uint16_t *p;
  31. p = data;
  32. char cmd[2] = {0,0};
  33. cmd[0] = startAddress >> 8;
  34. cmd[1] = startAddress & 0x00FF;
  35. i2cData = (char *)luat_heap_malloc(2*nMemAddressRead);
  36. if (mlx90640_i2c.i2c_id == -1){
  37. ret = i2c_soft_send(mlx90640_i2c.ei2c, slaveAddr, (char*)cmd, 2,0);
  38. ret = i2c_soft_recv(mlx90640_i2c.ei2c, slaveAddr, (char*)i2cData, 2*nMemAddressRead);
  39. }else{
  40. ret = luat_i2c_transfer(mlx90640_i2c.i2c_id, slaveAddr, (uint8_t*)cmd, 2, (uint8_t*)i2cData, 2*nMemAddressRead);
  41. }
  42. if (ret != 0){
  43. luat_heap_free(i2cData);
  44. return -1;
  45. }
  46. for(cnt=0; cnt < nMemAddressRead; cnt++)
  47. {
  48. i = cnt << 1;
  49. *p++ = (uint16_t)i2cData[i]*256 + (uint16_t)i2cData[i+1];
  50. }
  51. luat_heap_free(i2cData);
  52. return 0;
  53. }
  54. int MLX90640_I2CWrite(uint8_t slaveAddr, uint16_t writeAddress, uint16_t data)
  55. {
  56. int ret = 0;
  57. static uint16_t dataCheck;
  58. uint8_t cmd[4];
  59. cmd[0] = writeAddress >> 8;
  60. cmd[1] = writeAddress & 0x00FF;
  61. cmd[2] = data >> 8;
  62. cmd[3] = data & 0x00FF;
  63. if (mlx90640_i2c.i2c_id == -1){
  64. ret = i2c_soft_send(mlx90640_i2c.ei2c, slaveAddr, (char*)cmd, 4,0);
  65. }else{
  66. ret = luat_i2c_send(mlx90640_i2c.i2c_id, slaveAddr, cmd, 4,0);
  67. if (ret != 0)return -1;
  68. }
  69. ret = MLX90640_I2CRead(slaveAddr,writeAddress,1, &dataCheck);
  70. if (ret != 0)return -1;
  71. if ( dataCheck != data)
  72. {
  73. return -2;
  74. }
  75. return 0;
  76. }