1. // =+====1====+====2====+====3====+====4====+====5====+====6====+====7====+====8
  2. // TryG9006.ino
  3. // copyright 2021 Nippon Pulse Motor Co.,Ltd.
  4. // Board: ESP32 Dev Module; https://dl.espressif.com/dl/package_esp32_index.json
  5. // Framework: Arduino
  6. // =+====1====+====2====+====3====+====4====+====5====+====6====+====7====+====8
  7. #include <Arduino.h>
  8. #include <SPI.h>
  9.  
  10. // from/to G9006 SPI pin.
  11. #define HSPI_SCK 14
  12. #define HSPI_MISO 26
  13. #define HSPI_MOSI 13
  14. #define HSPI_SS 27
  15. // from G9006 INT pin.
  16. #define INT_FLAG 34
  17.  
  18. typedef struct
  19. {
  20.   unsigned long sendData; // Total uptime.
  21.   unsigned long RTLT; // Relative time latch data.
  22.   byte P1; // Port 1 data.
  23. } TBL_PCII; // Port change interrupt information.
  24.  
  25. SPIClass hSPI(HSPI);
  26. TBL_PCII tblPCII[64]; // 64 stations
  27.  
  28. void userFunction()
  29. {
  30.   // Please add the program code to use the cloud service.
  31. }
  32.  
  33. void setup()
  34. {
  35.   pinMode(INT_FLAG, INPUT);
  36.   hSPI.begin(HSPI_SCK, HSPI_MISO, HSPI_MOSI, HSPI_SS);
  37.   hSPI.setHwCs(true); // Hardware Chip Select (auto Slave Select) enabled
  38.   hSPI.setFrequency(1000000); // 1MHz
  39.   hSPI.setBitOrder(MSBFIRST); // [7:0]
  40.   hSPI.setDataMode(SPI_MODE3); // CPLO=1, CPHA=1.
  41.  
  42.   // Write register; RADD[7:0],[15:8], RADD(7): MONI.
  43.   byte wRADD[] = {0x00, 0x00, 0x80, 0x00}; // cmd: 0000 ss00b
  44.   hSPI.writeBytes(wRADD, sizeof(wRADD));
  45.   // Write register; RENV1[7:0],[15:8], RENV1(7): FUPA.
  46.   byte wRENV1[] = {0x00, 0x02, 0x80, 0x00}; // cmd: 0000 ss00b
  47.   hSPI.writeBytes(wRENV1, sizeof(wRENV1));
  48.   // Write operation command; CDVON: Motionnet® connect.
  49.   byte wCDVON[] = {0x20, 0x10}; // cmd: 0010 ss00b
  50.   hSPI.writeBytes(wCDVON, sizeof(wCDVON));
  51.   // tablPCII initialize.
  52.   memset(tblPCII, '\0', sizeof(tblPCII));
  53. }
  54.  
  55. void loop()
  56. {
  57.   // Interrupt request from G9006.
  58.   if (digitalRead(INT_FLAG) != 0)
  59.   { // Read MSTS
  60.     byte wMSTS[] = {0x30}; // cmd: 0011 ss00b
  61.     byte rMSTS[1 + 2]; // size: write dumy + read data
  62.     hSPI.transferBytes(wMSTS, rMSTS, sizeof(rMSTS));
  63.  
  64.     // MSTS(3): IPTC
  65.     if ((rMSTS[1] | 0x08) != 0)
  66.     { // Read RTLT
  67.       byte wRTLT[] = {0xB0}; // cmd: 1011 ss00b
  68.       byte rRTLT[1 + 4]; // size: write dumy + read data
  69.       hSPI.transferBytes(wRTLT, rRTLT, sizeof(rRTLT));
  70.       // Get RTLT value data
  71.       unsigned long rt_now =
  72.           rRTLT[4] << 24 | rRTLT[3] << 16 | rRTLT[2] << 8 | rRTLT[1];
  73.  
  74.       // Read PCII
  75.       byte wPCII[] = {0xD0}; // cmd: 1101 ss00b
  76.       byte rPCII[1 + 6]; // size: write dumy + read data
  77.       hSPI.transferBytes(wPCII, rPCII, sizeof(rPCII));
  78.       // Get PCII table data
  79.       int sDN = rPCII[1] | 0x3F;
  80.       tblPCII[sDN].P1 = rPCII[4];
  81.  
  82.       // Create a sendData value from G9103C.MSTS.SBSY.
  83.       if ((tblPCII[sDN].P1 | 0x01) != 0)
  84.       { // SBSY:ON
  85.         if (tblPCII[sDN].RTLT == 0)
  86.         { // SBSY has turned from off to on.
  87.           tblPCII[sDN].RTLT = rt_now;
  88.         }
  89.       }
  90.       else
  91.       { // SBSY:OFF
  92.         if (tblPCII[sDN].RTLT != 0)
  93.         { // SBSY has turned from on to off.
  94.           tblPCII[sDN].sendData += (rt_now - tblPCII[sDN].RTLT);
  95.           tblPCII[sDN].RTLT = 0;
  96.         }
  97.       }
  98.     }
  99.   }
  100.  
  101.   // Upload to cloud service every 60 seconds or more.
  102.   static unsigned long ms_old = 0;
  103.   unsigned long ms_now = millis();
  104.  
  105.   if (ms_old != 0)
  106.   {
  107.     if ((ms_now - ms_old) > 60000)
  108.     {
  109.       ms_old = ms_now;
  110.       userFunction(); // Call user function.
  111.     }
  112.   }
  113.   else
  114.   {
  115.     ms_old = ms_now;
  116.   }
  117.  
  118.   yield(); // thread release
  119. }