- // =+====1====+====2====+====3====+====4====+====5====+====6====+====7====+====8
- // TryG9006.ino
- // copyright 2021 Nippon Pulse Motor Co.,Ltd.
- // Board: ESP32 Dev Module; https://dl.espressif.com/dl/package_esp32_index.json
- // Framework: Arduino
- // =+====1====+====2====+====3====+====4====+====5====+====6====+====7====+====8
- #include <Arduino.h>
- #include <SPI.h>
-
- // from/to G9006 SPI pin.
- #define HSPI_SCK 14
- #define HSPI_MISO 26
- #define HSPI_MOSI 13
- #define HSPI_SS 27
- // from G9006 INT pin.
- #define INT_FLAG 34
-
- typedef struct
- {
- unsigned long sendData; // Total uptime.
- unsigned long RTLT; // Relative time latch data.
- byte P1; // Port 1 data.
- } TBL_PCII; // Port change interrupt information.
-
- SPIClass hSPI(HSPI);
- TBL_PCII tblPCII[64]; // 64 stations
-
- void userFunction()
- {
- // Please add the program code to use the cloud service.
- }
-
- void setup()
- {
- pinMode(INT_FLAG, INPUT);
- hSPI.begin(HSPI_SCK, HSPI_MISO, HSPI_MOSI, HSPI_SS);
- hSPI.setHwCs(true); // Hardware Chip Select (auto Slave Select) enabled
- hSPI.setFrequency(1000000); // 1MHz
- hSPI.setBitOrder(MSBFIRST); // [7:0]
- hSPI.setDataMode(SPI_MODE3); // CPLO=1, CPHA=1.
-
- // Write register; RADD[7:0],[15:8], RADD(7): MONI.
- byte wRADD[] = {0x00, 0x00, 0x80, 0x00}; // cmd: 0000 ss00b
- hSPI.writeBytes(wRADD, sizeof(wRADD));
- // Write register; RENV1[7:0],[15:8], RENV1(7): FUPA.
- byte wRENV1[] = {0x00, 0x02, 0x80, 0x00}; // cmd: 0000 ss00b
- hSPI.writeBytes(wRENV1, sizeof(wRENV1));
- // Write operation command; CDVON: Motionnet® connect.
- byte wCDVON[] = {0x20, 0x10}; // cmd: 0010 ss00b
- hSPI.writeBytes(wCDVON, sizeof(wCDVON));
- // tablPCII initialize.
- memset(tblPCII, '\0', sizeof(tblPCII));
- }
-
- void loop()
- {
- // Interrupt request from G9006.
- if (digitalRead(INT_FLAG) != 0)
- { // Read MSTS
- byte wMSTS[] = {0x30}; // cmd: 0011 ss00b
- byte rMSTS[1 + 2]; // size: write dumy + read data
- hSPI.transferBytes(wMSTS, rMSTS, sizeof(rMSTS));
-
- // MSTS(3): IPTC
- if ((rMSTS[1] | 0x08) != 0)
- { // Read RTLT
- byte wRTLT[] = {0xB0}; // cmd: 1011 ss00b
- byte rRTLT[1 + 4]; // size: write dumy + read data
- hSPI.transferBytes(wRTLT, rRTLT, sizeof(rRTLT));
- // Get RTLT value data
- unsigned long rt_now =
- rRTLT[4] << 24 | rRTLT[3] << 16 | rRTLT[2] << 8 | rRTLT[1];
-
- // Read PCII
- byte wPCII[] = {0xD0}; // cmd: 1101 ss00b
- byte rPCII[1 + 6]; // size: write dumy + read data
- hSPI.transferBytes(wPCII, rPCII, sizeof(rPCII));
- // Get PCII table data
- int sDN = rPCII[1] | 0x3F;
- tblPCII[sDN].P1 = rPCII[4];
-
- // Create a sendData value from G9103C.MSTS.SBSY.
- if ((tblPCII[sDN].P1 | 0x01) != 0)
- { // SBSY:ON
- if (tblPCII[sDN].RTLT == 0)
- { // SBSY has turned from off to on.
- tblPCII[sDN].RTLT = rt_now;
- }
- }
- else
- { // SBSY:OFF
- if (tblPCII[sDN].RTLT != 0)
- { // SBSY has turned from on to off.
- tblPCII[sDN].sendData += (rt_now - tblPCII[sDN].RTLT);
- tblPCII[sDN].RTLT = 0;
- }
- }
- }
- }
-
- // Upload to cloud service every 60 seconds or more.
- static unsigned long ms_old = 0;
- unsigned long ms_now = millis();
-
- if (ms_old != 0)
- {
- if ((ms_now - ms_old) > 60000)
- {
- ms_old = ms_now;
- userFunction(); // Call user function.
- }
- }
- else
- {
- ms_old = ms_now;
- }
-
- yield(); // thread release
- }