test_e_module.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* @brief This is a simple application for testing IIC communication on a RedPitaya
  2. * @Author Luka Golinar <luka.golinar@redpitaya.com>
  3. *
  4. * (c) Red Pitaya http://www.redpitaya.com
  5. *
  6. * This part of code is written in C programming language.
  7. * Please visit http://en.wikipedia.org/wiki/C_(programming_language)
  8. * for more details on the language used herein.
  9. */
  10. #include <fcntl.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <linux/ioctl.h>
  14. #include <sys/ioctl.h>
  15. #include <linux/i2c-dev.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #include <stdint.h>
  20. #include <math.h>
  21. #define I2C_SLAVE_FORCE 0x0706
  22. #define EXPANDER_ADDR 0x20
  23. int main(int argc, char *argv[])
  24. {
  25. //int main(){
  26. //int k;
  27. //for (k=0;k<=5;k++)
  28. //{
  29. sleep(3);
  30. //printf("%i\n", k );
  31. int dat;
  32. int fd;
  33. int status;
  34. char str [1+2*11];
  35. //parse input arguments
  36. dat = (int)strtol(argv[1], NULL, 0);
  37. dat = (1<<dat);
  38. // Open the device.
  39. fd = open("/dev/i2c-0", O_RDWR);
  40. if (fd < 0) {
  41. printf("Cannot open the IIC device\n");
  42. return 1;
  43. }
  44. // set slave address
  45. status = ioctl(fd, I2C_SLAVE_FORCE, EXPANDER_ADDR);
  46. if (status < 0) {
  47. printf("Unable to set the I2C address\n");
  48. return -1;
  49. }
  50. // Write to expander
  51. str [0] = 0; // set address to 0
  52. str [1+0x00] = 0x00; // IODIRA - set all to output
  53. str [1+0x01] = 0x00; // IODIRB - set all to output
  54. str [1+0x02] = 0x00; // IPOLA
  55. str [1+0x03] = 0x00; // IPOLB
  56. str [1+0x04] = 0x00; // GPINTENA
  57. str [1+0x05] = 0x00; // GPINTENB
  58. str [1+0x06] = 0x00; // DEFVALA
  59. str [1+0x07] = 0x00; // DEFVALB
  60. str [1+0x08] = 0x00; // INTCONA
  61. str [1+0x09] = 0x00; // INTCONB
  62. str [1+0x0A] = 0x00; // IOCON
  63. str [1+0x0B] = 0x00; // IOCON
  64. str [1+0x0C] = 0x00; // GPPUA
  65. str [1+0x0D] = 0x00; // GPPUB
  66. str [1+0x0E] = 0x00; // INTFA
  67. str [1+0x0F] = 0x00; // INTFB
  68. str [1+0x10] = 0x00; // INTCAPA
  69. str [1+0x11] = 0x00; // INTCAPB
  70. str [1+0x12] = (dat >> 0) & 0xff; // GPIOA
  71. str [1+0x13] = (dat >> 8) & 0xff; // GPIOB
  72. str [1+0x14] = (dat >> 0) & 0xff; // OLATA
  73. str [1+0x15] = (dat >> 8) & 0xff; // OLATB
  74. status = write(fd, str, 1+2*11);
  75. //printf("Resistor data write = 0x%04x.\n", dat);
  76. // if (status) printf("Resistor write => %.1f\n", powf(10.0,(float)k+1));
  77. //else
  78. // printf("Error write\n");
  79. close(fd);
  80. //}
  81. return 0;
  82. }