Sakura IoT Platform b

Dependencies:   SakuraIO_official mbed

Fork of SakuraIO_Standard by SAKURA Internet

Committer:
sakurafan
Date:
Mon Nov 21 14:12:05 2016 +0000
Revision:
2:ca774356e697
Parent:
0:40aec5afc650
fix spi;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
spiralray 0:40aec5afc650 1 #include "mbed.h"
spiralray 0:40aec5afc650 2 #include "SakuraIO.h"
spiralray 0:40aec5afc650 3
spiralray 0:40aec5afc650 4 Serial pc(USBTX, USBRX);
spiralray 0:40aec5afc650 5 DigitalOut myled(LED1);
spiralray 0:40aec5afc650 6
spiralray 0:40aec5afc650 7 int main()
spiralray 0:40aec5afc650 8 {
spiralray 0:40aec5afc650 9
spiralray 0:40aec5afc650 10
spiralray 0:40aec5afc650 11 SPI spi(p5, p6, p7); // mosi, miso, sclk
spiralray 0:40aec5afc650 12 I2C i2c(p9, p10); // sda, scl
spiralray 0:40aec5afc650 13
sakurafan 2:ca774356e697 14 //SakuraIO_SPI sakuraio( spi, p8 );
spiralray 0:40aec5afc650 15 SakuraIO_I2C sakuraio(i2c);
spiralray 0:40aec5afc650 16
spiralray 0:40aec5afc650 17
spiralray 0:40aec5afc650 18 pc.printf("Waiting to come online");
spiralray 0:40aec5afc650 19 for(;;) {
spiralray 0:40aec5afc650 20 if( (sakuraio.getConnectionStatus() & 0x80) == 0x80 ) break;
spiralray 0:40aec5afc650 21 pc.printf(".");
spiralray 0:40aec5afc650 22 wait_ms(1000);
spiralray 0:40aec5afc650 23 }
spiralray 0:40aec5afc650 24 pc.printf("\r\n");
spiralray 0:40aec5afc650 25
spiralray 0:40aec5afc650 26 uint8_t counter = 0;
spiralray 0:40aec5afc650 27
spiralray 0:40aec5afc650 28 while(1) {
spiralray 0:40aec5afc650 29 pc.printf("\r\n%02x\r\n",counter);
spiralray 0:40aec5afc650 30
spiralray 0:40aec5afc650 31 uint8_t request[33] = {};
spiralray 0:40aec5afc650 32 uint8_t response[33] = {};
spiralray 0:40aec5afc650 33
spiralray 0:40aec5afc650 34 // Echoback
spiralray 0:40aec5afc650 35 request[0] = counter;
spiralray 0:40aec5afc650 36 sakuraio.echoback(1, request, response);
spiralray 0:40aec5afc650 37 pc.printf("%02x\r\n",request[0]);
spiralray 0:40aec5afc650 38 pc.printf(" -> ");
spiralray 0:40aec5afc650 39 pc.printf("%02x\r\n",response[0]);
spiralray 0:40aec5afc650 40
spiralray 0:40aec5afc650 41 // Unixtime
spiralray 0:40aec5afc650 42 uint32_t unixtime = (uint32_t)(sakuraio.getUnixtime()/1000UL);
spiralray 0:40aec5afc650 43 pc.printf("Unixtime ");
spiralray 0:40aec5afc650 44 pc.printf("%d\r\n",unixtime);
spiralray 0:40aec5afc650 45
spiralray 0:40aec5afc650 46 // ADC
spiralray 0:40aec5afc650 47 uint16_t adc;
spiralray 0:40aec5afc650 48 adc = sakuraio.getADC(0);
spiralray 0:40aec5afc650 49 pc.printf("ADC0 ");
spiralray 0:40aec5afc650 50 pc.printf("%d\r\n",adc);
spiralray 0:40aec5afc650 51 adc = sakuraio.getADC(1);
spiralray 0:40aec5afc650 52 pc.printf("ADC1 ");
spiralray 0:40aec5afc650 53 pc.printf("%d\r\n",adc);
spiralray 0:40aec5afc650 54
spiralray 0:40aec5afc650 55 // ProductID
spiralray 0:40aec5afc650 56 uint16_t pid = sakuraio.getProductID();
spiralray 0:40aec5afc650 57 pc.printf("PID ");
spiralray 0:40aec5afc650 58 pc.printf("%d\r\n",pid);
spiralray 0:40aec5afc650 59
spiralray 0:40aec5afc650 60 // UniqueID
spiralray 0:40aec5afc650 61 sakuraio.getUniqueID((char *)response);
spiralray 0:40aec5afc650 62 pc.printf("UID ");
spiralray 0:40aec5afc650 63 pc.printf("%s\r\n",response);
spiralray 0:40aec5afc650 64
spiralray 0:40aec5afc650 65 // Version
spiralray 0:40aec5afc650 66 sakuraio.getFirmwareVersion((char *)response);
spiralray 0:40aec5afc650 67 pc.printf("Ver ");
spiralray 0:40aec5afc650 68 pc.printf("%s\r\n",response);
spiralray 0:40aec5afc650 69
spiralray 0:40aec5afc650 70 // Connection Status
spiralray 0:40aec5afc650 71 uint8_t connectionStatus = sakuraio.getConnectionStatus();
spiralray 0:40aec5afc650 72 pc.printf("Status ");
spiralray 0:40aec5afc650 73 pc.printf("%s\r\n",response);
spiralray 0:40aec5afc650 74
spiralray 0:40aec5afc650 75 //getSignalQuarity
spiralray 0:40aec5afc650 76 uint8_t signalQuarity = sakuraio.getSignalQuarity();
spiralray 0:40aec5afc650 77 pc.printf("Quality ");
spiralray 0:40aec5afc650 78 pc.printf("%s\r\n",response);
spiralray 0:40aec5afc650 79
spiralray 0:40aec5afc650 80
spiralray 0:40aec5afc650 81 // Tx Queue
spiralray 0:40aec5afc650 82 uint8_t ret;
spiralray 0:40aec5afc650 83 ret = sakuraio.enqueueTx((uint8_t)0, (int32_t)counter);
spiralray 0:40aec5afc650 84 pc.printf("Enqueue ");
spiralray 0:40aec5afc650 85 pc.printf("%s\r\n",response);
spiralray 0:40aec5afc650 86
spiralray 0:40aec5afc650 87 uint8_t avail;
spiralray 0:40aec5afc650 88 uint8_t queued;
spiralray 0:40aec5afc650 89 sakuraio.getTxQueueLength(&avail, &queued);
spiralray 0:40aec5afc650 90 pc.printf("Tx Available=");
spiralray 0:40aec5afc650 91 pc.printf("%d",avail);
spiralray 0:40aec5afc650 92 pc.printf(" Queued=");
spiralray 0:40aec5afc650 93 pc.printf("%d\r\n",queued);
spiralray 0:40aec5afc650 94
spiralray 0:40aec5afc650 95 if(queued >= 30) {
spiralray 0:40aec5afc650 96 ret = sakuraio.clearTx();
spiralray 0:40aec5afc650 97 pc.printf("Clear ");
spiralray 0:40aec5afc650 98 pc.printf("%02x\r\n",ret);
spiralray 0:40aec5afc650 99 } else if(queued >= 5) {
spiralray 0:40aec5afc650 100 ret = sakuraio.send();
spiralray 0:40aec5afc650 101 pc.printf("Send ");
spiralray 0:40aec5afc650 102 pc.printf("%02x\r\n",ret);
spiralray 0:40aec5afc650 103 }
spiralray 0:40aec5afc650 104
spiralray 0:40aec5afc650 105 // Rx Queue
spiralray 0:40aec5afc650 106 sakuraio.getRxQueueLength(&avail, &queued);
spiralray 0:40aec5afc650 107 pc.printf("Rx Available=");
spiralray 0:40aec5afc650 108 pc.printf("%d",avail);
spiralray 0:40aec5afc650 109 pc.printf(" Queued=");
spiralray 0:40aec5afc650 110 pc.printf("%d\r\n",queued);
spiralray 0:40aec5afc650 111
spiralray 0:40aec5afc650 112 for(uint8_t i=0; i<queued; i++) {
spiralray 0:40aec5afc650 113 uint8_t channel;
spiralray 0:40aec5afc650 114 uint8_t type;
spiralray 0:40aec5afc650 115 uint8_t values[8];
spiralray 0:40aec5afc650 116 int64_t offset;
spiralray 0:40aec5afc650 117 ret = sakuraio.dequeueRx(&channel, &type, values, &offset);
spiralray 0:40aec5afc650 118 pc.printf("Dequeue ");
spiralray 0:40aec5afc650 119 pc.printf("%02x",ret);
spiralray 0:40aec5afc650 120 if(ret == 0x01) {
spiralray 0:40aec5afc650 121 pc.printf(" ch=");
spiralray 0:40aec5afc650 122 pc.printf("%d",channel);
spiralray 0:40aec5afc650 123 pc.printf(" type=");
spiralray 0:40aec5afc650 124 pc.printf("%c",type);
spiralray 0:40aec5afc650 125 pc.printf(" values=[");
spiralray 0:40aec5afc650 126 for(uint8_t b=0; b<7; b++) {
spiralray 0:40aec5afc650 127 pc.printf("%02x ",values[b]);
spiralray 0:40aec5afc650 128 }
spiralray 0:40aec5afc650 129 pc.printf("%02x",values[7]);
spiralray 0:40aec5afc650 130 pc.printf("] offset=");
spiralray 0:40aec5afc650 131 pc.printf("%d\r\n",(int32_t)offset);
spiralray 0:40aec5afc650 132 } else {
spiralray 0:40aec5afc650 133 pc.printf(" ERROR\r\n");
spiralray 0:40aec5afc650 134 }
spiralray 0:40aec5afc650 135 }
spiralray 0:40aec5afc650 136
spiralray 0:40aec5afc650 137 wait_ms(1000);
spiralray 0:40aec5afc650 138 }
spiralray 0:40aec5afc650 139
spiralray 0:40aec5afc650 140 }