IEEE1888 GateWay sample that handles multiple sensors.
Dependencies: EthernetInterface Fiap HTTPClientForFIAP NTPClient mbed-rtos mbed spxml
Fork of IEEE1888_TEMP_NODE by
This program sends data to FIAP storage with IEEE1888 format.
Note: You should locate storage.txt into mbed memory at least before using this program. The storage.txt contains storage url, see example at main.h
You can launch an AMI ( Amazon Machine Image ) on Amazon Web Service ( http://aws.amazon.com/ ) as the FIAP storage. The AMI is named as "IEEE1888 FiapStorage2 Sample", registered at Tokyo region (ap-northeast-1) .
If You need more information about the AMI, see http://d.hatena.ne.jp/satoruyoshida/20130611/1370961807 .
This image shows sample data of IEEE1888 format.
Note: this image is shown if FIAP_DEBUG_MODE is set to true.
You can define COUNT_OF_POINTS constant as number of analog sensors and digital inputs. In this program, I use P19 and P20 as analog inputs from 2 sensors. So I define the COUNT_OF_POINTS as 2.
P19 is connected to illuminance sensor, S9648 (HAMAMATSU PHOTONICS K.K.). And P20 is connected to temperature sensor, LM35 (National Semiconductor Corporation) .
You can also use another analog sensors and digital inputs, for example LM61BIZ instead of LM35.
from here, in Japanese. このプログラムは IEEE1888 形式で FIAP Storage にデータ送信する例です。 このプログラムを使用する前に、少なくとも FIAP Storage の url を記載した sotrage.txt をメモリに格納しておく必要があります。 url の例は、 main.h ファイルにあります。
Amazon Web Service に用意した AMI を利用して FIAP Storage を起動することができます。IEEE1888 FiapStorage2 Sample という名前の公開 AMI です。
この AMI について詳しくは、http://d.hatena.ne.jp/satoruyoshida/20130611/1370961807 をご覧ください。
なお、main.h にはほかにも定数があります。例えば、IEEE1888 (FIAP) の通信内容をコンソールで確認したい場合は、FIAP_DEBUG_MODE を true にします。 また、アナログ・センサーやデジタル入力の個数に沿って、COUNT_OF_POINTS を定義します。
このプログラム例では、P19 および P20 をセンサーからのアナログ入力として使用しています。そのため、COUNT_OF_POINTS を 2 と定義しています。 P19 には浜松ホトニクス社製の照度センサー S9648、P20 にはナショナル・セミコンダクターの温度センサー LM35 を接続しています。 参考までに LM35 をより安価な LM61BIZ に置き換えた場合の設定値もコメントアウト状態で入れてあります
(ここから日本語文のみの内容) トランジスタ技術 2012年 2月号 の P189 - P195 に、Arduino、Ethernet シールド、温度センサー(LM35)、照度センサー(S9648-100)、DIP スイッチおよびトグルスイッチを用いたネットワーク温度&照度計の記事が掲載されています(落合 秀也 様/井上 博之 様)。 このプログラムでは、照度センサーは S9648-100 ではなく、より安価な S9648 で代用し、スイッチ 2 種類は省略していますのでご注意ください。 S9648 の負荷抵抗 1KΩ に並列に入れたコンデンサーは、手元に余っていた 0.1μF です。この容量が妥当かどうかは未検証です。
LM35 の出力をアンプで6倍に増幅してみたときのメモがここにあります。
https://mbed.org/users/strysd/notebook/memo_analog_input_from_lm35dz_via_nju7032d/
照度センサーを S9648-100 に変えて、抵抗値も検討したときのメモはこちら。
https://mbed.org/users/strysd/notebook/difference_s9648_and_s9648_100/
main.cpp@25:929a72bcd8d2, 2013-04-30 (annotated)
- Committer:
- strysd
- Date:
- Tue Apr 30 00:45:19 2013 +0000
- Revision:
- 25:929a72bcd8d2
- Parent:
- 24:06545ba01cf4
- Child:
- 26:54b10f53bf41
Automatically generates code required for analog input port P15 - P20
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
strysd | 6:cccef292257b | 1 | #include "main.h" |
yueee_yt | 0:27cf9683af17 | 2 | #include "mbed.h" |
yueee_yt | 0:27cf9683af17 | 3 | #include "EthernetInterface.h" |
yueee_yt | 0:27cf9683af17 | 4 | #include "NTPClient.h" |
yueee_yt | 0:27cf9683af17 | 5 | #include "fiap.h" |
yueee_yt | 0:27cf9683af17 | 6 | |
yueee_yt | 0:27cf9683af17 | 7 | EthernetInterface eth; |
yueee_yt | 0:27cf9683af17 | 8 | time_t ctTime; |
strysd | 18:f277fbbcea4b | 9 | Serial pc(USBTX, USBRX); |
strysd | 18:f277fbbcea4b | 10 | FIAP fiap; |
yueee_yt | 0:27cf9683af17 | 11 | |
strysd | 25:929a72bcd8d2 | 12 | #ifdef P15_POINT_ID |
strysd | 25:929a72bcd8d2 | 13 | AnalogIn ain_p15(p15); |
strysd | 25:929a72bcd8d2 | 14 | #endif |
strysd | 25:929a72bcd8d2 | 15 | #ifdef P16_POINT_ID |
strysd | 25:929a72bcd8d2 | 16 | AnalogIn ain_p16(p16); |
strysd | 25:929a72bcd8d2 | 17 | #endif |
strysd | 25:929a72bcd8d2 | 18 | #ifdef P17_POINT_ID |
strysd | 25:929a72bcd8d2 | 19 | AnalogIn ain_p17(p17); |
strysd | 25:929a72bcd8d2 | 20 | #endif |
strysd | 25:929a72bcd8d2 | 21 | #ifdef P18_POINT_ID |
strysd | 25:929a72bcd8d2 | 22 | AnalogIn ain_p18(p18); |
strysd | 25:929a72bcd8d2 | 23 | #endif |
strysd | 25:929a72bcd8d2 | 24 | #ifdef P19_POINT_ID |
strysd | 13:4e51ac520b33 | 25 | AnalogIn ain_p19(p19); |
strysd | 25:929a72bcd8d2 | 26 | #endif |
strysd | 25:929a72bcd8d2 | 27 | #ifdef P20_POINT_ID |
strysd | 13:4e51ac520b33 | 28 | AnalogIn ain_p20(p20); |
strysd | 25:929a72bcd8d2 | 29 | #endif |
strysd | 4:f50c307c5eda | 30 | |
strysd | 13:4e51ac520b33 | 31 | DigitalOut led1(LED1);//error while connecting to NTP server or FIAPStorage |
strysd | 8:e482433c9593 | 32 | DigitalOut led3(LED3);//LAN speed (1 = error) |
strysd | 8:e482433c9593 | 33 | DigitalOut led4(LED4);//LAN link (1 = error) |
strysd | 4:f50c307c5eda | 34 | |
strysd | 8:e482433c9593 | 35 | DigitalIn lnk(P1_25);//LAN link |
strysd | 8:e482433c9593 | 36 | DigitalIn spd(P1_26);//LAN speed |
strysd | 25:929a72bcd8d2 | 37 | |
strysd | 25:929a72bcd8d2 | 38 | #if USE_RJ45_LAMP |
strysd | 9:0d34968a5de1 | 39 | DigitalOut speed(p29);//LAN speed for RJ45 (1 = OK) |
strysd | 9:0d34968a5de1 | 40 | DigitalOut link(p30); //LAN link for RJ45 (1 = OK) |
strysd | 25:929a72bcd8d2 | 41 | #endif |
strysd | 8:e482433c9593 | 42 | |
strysd | 8:e482433c9593 | 43 | int exitTimer; |
strysd | 17:868a61434584 | 44 | int i; |
strysd | 22:07b3f2643582 | 45 | int this_year, this_mon; |
strysd | 13:4e51ac520b33 | 46 | |
strysd | 20:44214878a602 | 47 | //I define longer than actual length. |
strysd | 20:44214878a602 | 48 | //because last characters are chopped when I define just length, 64. |
strysd | 20:44214878a602 | 49 | #define STORAGE_URL_SIZE 100 |
strysd | 20:44214878a602 | 50 | char storage_url[STORAGE_URL_SIZE]; |
strysd | 18:f277fbbcea4b | 51 | struct fiap_element element[]= { |
strysd | 18:f277fbbcea4b | 52 | {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}, |
strysd | 18:f277fbbcea4b | 53 | }; |
strysd | 18:f277fbbcea4b | 54 | struct tm t; |
strysd | 18:f277fbbcea4b | 55 | |
strysd | 25:929a72bcd8d2 | 56 | #ifdef P15_POINT_ID |
strysd | 17:868a61434584 | 57 | char val_p15[6]; |
strysd | 17:868a61434584 | 58 | float float_p15_wrk; |
strysd | 25:929a72bcd8d2 | 59 | float float_p15; |
strysd | 25:929a72bcd8d2 | 60 | #endif |
strysd | 25:929a72bcd8d2 | 61 | #ifdef P16_POINT_ID |
strysd | 25:929a72bcd8d2 | 62 | char val_p16[6]; |
strysd | 17:868a61434584 | 63 | float float_p16_wrk; |
strysd | 25:929a72bcd8d2 | 64 | float float_p16; |
strysd | 25:929a72bcd8d2 | 65 | #endif |
strysd | 25:929a72bcd8d2 | 66 | #ifdef P17_POINT_ID |
strysd | 25:929a72bcd8d2 | 67 | char val_p17[6]; |
strysd | 17:868a61434584 | 68 | float float_p17_wrk; |
strysd | 17:868a61434584 | 69 | float float_p17; |
strysd | 25:929a72bcd8d2 | 70 | #endif |
strysd | 25:929a72bcd8d2 | 71 | #ifdef P18_POINT_ID |
strysd | 25:929a72bcd8d2 | 72 | char val_p18[6]; |
strysd | 25:929a72bcd8d2 | 73 | float float_p18_wrk; |
strysd | 17:868a61434584 | 74 | float float_p18; |
strysd | 25:929a72bcd8d2 | 75 | #endif |
strysd | 25:929a72bcd8d2 | 76 | #ifdef P19_POINT_ID |
strysd | 25:929a72bcd8d2 | 77 | char val_p19[6]; |
strysd | 25:929a72bcd8d2 | 78 | float float_p19_wrk; |
strysd | 13:4e51ac520b33 | 79 | float float_p19; |
strysd | 25:929a72bcd8d2 | 80 | #endif |
strysd | 25:929a72bcd8d2 | 81 | #ifdef P20_POINT_ID |
strysd | 25:929a72bcd8d2 | 82 | char val_p20[6]; |
strysd | 25:929a72bcd8d2 | 83 | float float_p20_wrk; |
strysd | 13:4e51ac520b33 | 84 | float float_p20; |
strysd | 25:929a72bcd8d2 | 85 | #endif |
strysd | 9:0d34968a5de1 | 86 | |
strysd | 18:f277fbbcea4b | 87 | int syncNTP(void) |
strysd | 18:f277fbbcea4b | 88 | { |
strysd | 18:f277fbbcea4b | 89 | NTPClient ntp; |
strysd | 18:f277fbbcea4b | 90 | if (ntp.setTime(NTP_SERVER) != 0) { |
strysd | 18:f277fbbcea4b | 91 | return -1; |
strysd | 18:f277fbbcea4b | 92 | } |
strysd | 18:f277fbbcea4b | 93 | ctTime = time(NULL); |
strysd | 18:f277fbbcea4b | 94 | ctTime+= TIME_ZONE_SECONDS; |
strysd | 18:f277fbbcea4b | 95 | set_time(ctTime); |
strysd | 18:f277fbbcea4b | 96 | return 0; |
strysd | 18:f277fbbcea4b | 97 | } |
strysd | 18:f277fbbcea4b | 98 | |
strysd | 18:f277fbbcea4b | 99 | int setFIAPStorage(void ) |
strysd | 18:f277fbbcea4b | 100 | { |
strysd | 18:f277fbbcea4b | 101 | LocalFileSystem local("local"); |
strysd | 18:f277fbbcea4b | 102 | FILE *fp; |
strysd | 18:f277fbbcea4b | 103 | fp = fopen("/local/storage.txt", "r"); |
strysd | 18:f277fbbcea4b | 104 | if(fp == NULL){ |
strysd | 18:f277fbbcea4b | 105 | return -1; |
strysd | 18:f277fbbcea4b | 106 | } |
strysd | 20:44214878a602 | 107 | fgets(storage_url, STORAGE_URL_SIZE, fp); |
strysd | 18:f277fbbcea4b | 108 | fiap.setStorage(storage_url); |
strysd | 18:f277fbbcea4b | 109 | fclose(fp); |
strysd | 18:f277fbbcea4b | 110 | |
strysd | 18:f277fbbcea4b | 111 | fiap.debug_mode=FIAP_DEBUG_MODE; |
strysd | 18:f277fbbcea4b | 112 | return 0; |
strysd | 18:f277fbbcea4b | 113 | } |
strysd | 18:f277fbbcea4b | 114 | |
strysd | 13:4e51ac520b33 | 115 | int sendFIAP(void ) |
yueee_yt | 0:27cf9683af17 | 116 | { |
yueee_yt | 0:27cf9683af17 | 117 | ctTime = time(NULL); |
strysd | 4:f50c307c5eda | 118 | t = *localtime(&ctTime); |
strysd | 10:51ef659414a9 | 119 | |
strysd | 25:929a72bcd8d2 | 120 | #ifdef P15_POINT_ID |
strysd | 25:929a72bcd8d2 | 121 | float_p15_wrk = ain_p15;//dummy read |
strysd | 25:929a72bcd8d2 | 122 | float_p15_wrk = ain_p15; |
strysd | 25:929a72bcd8d2 | 123 | float_p15_wrk += ain_p15; |
strysd | 25:929a72bcd8d2 | 124 | float_p15 = float_p15_wrk/2 * P15_COEFFICIENT + P15_CONSTANT; |
strysd | 25:929a72bcd8d2 | 125 | #endif |
strysd | 25:929a72bcd8d2 | 126 | #ifdef P16_POINT_ID |
strysd | 25:929a72bcd8d2 | 127 | float_p16_wrk = ain_p16;//dummy read |
strysd | 25:929a72bcd8d2 | 128 | float_p16_wrk = ain_p16; |
strysd | 25:929a72bcd8d2 | 129 | float_p16_wrk += ain_p16; |
strysd | 25:929a72bcd8d2 | 130 | float_p16 = float_p16_wrk/2 * P16_COEFFICIENT + P16_CONSTANT; |
strysd | 25:929a72bcd8d2 | 131 | #endif |
strysd | 25:929a72bcd8d2 | 132 | #ifdef P17_POINT_ID |
strysd | 25:929a72bcd8d2 | 133 | float_p17_wrk = ain_p17;//dummy read |
strysd | 25:929a72bcd8d2 | 134 | float_p17_wrk = ain_p17; |
strysd | 25:929a72bcd8d2 | 135 | float_p17_wrk += ain_p17; |
strysd | 25:929a72bcd8d2 | 136 | float_p17 = float_p17_wrk/2 * P17_COEFFICIENT + P17_CONSTANT; |
strysd | 25:929a72bcd8d2 | 137 | #endif |
strysd | 25:929a72bcd8d2 | 138 | #ifdef P18_POINT_ID |
strysd | 25:929a72bcd8d2 | 139 | float_p18_wrk = ain_p18;//dummy read |
strysd | 25:929a72bcd8d2 | 140 | float_p18_wrk = ain_p18; |
strysd | 25:929a72bcd8d2 | 141 | float_p18_wrk += ain_p18; |
strysd | 25:929a72bcd8d2 | 142 | float_p18 = float_p18_wrk/2 * P18_COEFFICIENT + P18_CONSTANT; |
strysd | 25:929a72bcd8d2 | 143 | #endif |
strysd | 25:929a72bcd8d2 | 144 | #ifdef P19_POINT_ID |
strysd | 25:929a72bcd8d2 | 145 | float_p19_wrk = ain_p19;//dummy read |
strysd | 25:929a72bcd8d2 | 146 | float_p19_wrk = ain_p19; |
strysd | 25:929a72bcd8d2 | 147 | float_p19_wrk += ain_p19; |
strysd | 25:929a72bcd8d2 | 148 | float_p19 = float_p19_wrk/2 * P19_COEFFICIENT + P19_CONSTANT; |
strysd | 25:929a72bcd8d2 | 149 | #endif |
strysd | 25:929a72bcd8d2 | 150 | #ifdef P20_POINT_ID |
strysd | 25:929a72bcd8d2 | 151 | float_p20_wrk = ain_p20;//dummy read |
strysd | 25:929a72bcd8d2 | 152 | float_p20_wrk = ain_p20; |
strysd | 25:929a72bcd8d2 | 153 | float_p20_wrk += ain_p20; |
strysd | 25:929a72bcd8d2 | 154 | float_p20 = float_p20_wrk/2 * P20_COEFFICIENT + P20_CONSTANT; |
strysd | 25:929a72bcd8d2 | 155 | #endif |
strysd | 17:868a61434584 | 156 | |
strysd | 25:929a72bcd8d2 | 157 | #ifdef P15_POINT_ID |
strysd | 25:929a72bcd8d2 | 158 | sprintf(val_p15,"%2.2f",float_p15); |
strysd | 25:929a72bcd8d2 | 159 | #endif |
strysd | 25:929a72bcd8d2 | 160 | #ifdef P16_POINT_ID |
strysd | 25:929a72bcd8d2 | 161 | sprintf(val_p16,"%2.2f",float_p16); |
strysd | 25:929a72bcd8d2 | 162 | #endif |
strysd | 25:929a72bcd8d2 | 163 | #ifdef P17_POINT_ID |
strysd | 25:929a72bcd8d2 | 164 | sprintf(val_p17,"%2.2f",float_p17); |
strysd | 25:929a72bcd8d2 | 165 | #endif |
strysd | 25:929a72bcd8d2 | 166 | #ifdef P18_POINT_ID |
strysd | 25:929a72bcd8d2 | 167 | sprintf(val_p18,"%2.2f",float_p18); |
strysd | 25:929a72bcd8d2 | 168 | #endif |
strysd | 25:929a72bcd8d2 | 169 | #ifdef P19_POINT_ID |
strysd | 25:929a72bcd8d2 | 170 | if(float_p19 > 100.0){ |
strysd | 25:929a72bcd8d2 | 171 | sprintf(val_p19,"%4.0f",float_p19); |
strysd | 25:929a72bcd8d2 | 172 | } else { |
strysd | 25:929a72bcd8d2 | 173 | sprintf(val_p19,"%2.1f",float_p19); |
strysd | 22:07b3f2643582 | 174 | } |
strysd | 25:929a72bcd8d2 | 175 | #endif |
strysd | 25:929a72bcd8d2 | 176 | #ifdef P20_POINT_ID |
strysd | 24:06545ba01cf4 | 177 | sprintf(val_p20,"%2.2f",float_p20); |
strysd | 25:929a72bcd8d2 | 178 | #endif |
strysd | 13:4e51ac520b33 | 179 | |
strysd | 22:07b3f2643582 | 180 | this_year = t.tm_year + 1900; |
strysd | 22:07b3f2643582 | 181 | this_mon = t.tm_mon + 1; |
strysd | 24:06545ba01cf4 | 182 | |
strysd | 25:929a72bcd8d2 | 183 | #ifdef P15_POINT_ID |
strysd | 25:929a72bcd8d2 | 184 | struct fiap_element P15_element = { |
strysd | 25:929a72bcd8d2 | 185 | P15_POINT_ID, val_p15, this_year, this_mon, t.tm_mday, |
strysd | 25:929a72bcd8d2 | 186 | t.tm_hour, t.tm_min, t.tm_sec, TIME_ZONE_CHAR}; |
strysd | 25:929a72bcd8d2 | 187 | #endif |
strysd | 25:929a72bcd8d2 | 188 | #ifdef P16_POINT_ID |
strysd | 25:929a72bcd8d2 | 189 | struct fiap_element P16_element = { |
strysd | 25:929a72bcd8d2 | 190 | P16_POINT_ID, val_p16, this_year, this_mon, t.tm_mday, |
strysd | 25:929a72bcd8d2 | 191 | t.tm_hour, t.tm_min, t.tm_sec, TIME_ZONE_CHAR}; |
strysd | 25:929a72bcd8d2 | 192 | #endif |
strysd | 25:929a72bcd8d2 | 193 | #ifdef P17_POINT_ID |
strysd | 25:929a72bcd8d2 | 194 | struct fiap_element P17_element = { |
strysd | 25:929a72bcd8d2 | 195 | P17_POINT_ID, val_p17, this_year, this_mon, t.tm_mday, |
strysd | 25:929a72bcd8d2 | 196 | t.tm_hour, t.tm_min, t.tm_sec, TIME_ZONE_CHAR}; |
strysd | 25:929a72bcd8d2 | 197 | #endif |
strysd | 25:929a72bcd8d2 | 198 | #ifdef P18_POINT_ID |
strysd | 25:929a72bcd8d2 | 199 | struct fiap_element P18_element = { |
strysd | 25:929a72bcd8d2 | 200 | P18_POINT_ID, val_p18, this_year, this_mon, t.tm_mday, |
strysd | 25:929a72bcd8d2 | 201 | t.tm_hour, t.tm_min, t.tm_sec, TIME_ZONE_CHAR}; |
strysd | 25:929a72bcd8d2 | 202 | #endif |
strysd | 25:929a72bcd8d2 | 203 | #ifdef P19_POINT_ID |
strysd | 22:07b3f2643582 | 204 | struct fiap_element P19_element = { |
strysd | 22:07b3f2643582 | 205 | P19_POINT_ID, val_p19, this_year, this_mon, t.tm_mday, |
strysd | 22:07b3f2643582 | 206 | t.tm_hour, t.tm_min, t.tm_sec, TIME_ZONE_CHAR}; |
strysd | 25:929a72bcd8d2 | 207 | #endif |
strysd | 25:929a72bcd8d2 | 208 | #ifdef P20_POINT_ID |
strysd | 22:07b3f2643582 | 209 | struct fiap_element P20_element = { |
strysd | 22:07b3f2643582 | 210 | P20_POINT_ID, val_p20, this_year, this_mon, t.tm_mday, |
strysd | 22:07b3f2643582 | 211 | t.tm_hour, t.tm_min, t.tm_sec, TIME_ZONE_CHAR}; |
strysd | 25:929a72bcd8d2 | 212 | #endif |
strysd | 17:868a61434584 | 213 | |
strysd | 25:929a72bcd8d2 | 214 | i = 0; |
strysd | 25:929a72bcd8d2 | 215 | #ifdef P15_POINT_ID |
strysd | 25:929a72bcd8d2 | 216 | element[i++] = P15_element; |
strysd | 25:929a72bcd8d2 | 217 | #endif |
strysd | 25:929a72bcd8d2 | 218 | #ifdef P16_POINT_ID |
strysd | 25:929a72bcd8d2 | 219 | element[i++] = P16_element; |
strysd | 25:929a72bcd8d2 | 220 | #endif |
strysd | 25:929a72bcd8d2 | 221 | #ifdef P17_POINT_ID |
strysd | 25:929a72bcd8d2 | 222 | element[i++] = P17_element; |
strysd | 25:929a72bcd8d2 | 223 | #endif |
strysd | 25:929a72bcd8d2 | 224 | #ifdef P18_POINT_ID |
strysd | 25:929a72bcd8d2 | 225 | element[i++] = P18_element; |
strysd | 25:929a72bcd8d2 | 226 | #endif |
strysd | 25:929a72bcd8d2 | 227 | #ifdef P19_POINT_ID |
strysd | 25:929a72bcd8d2 | 228 | element[i++] = P19_element; |
strysd | 25:929a72bcd8d2 | 229 | #endif |
strysd | 25:929a72bcd8d2 | 230 | #ifdef P20_POINT_ID |
strysd | 25:929a72bcd8d2 | 231 | element[i++] = P20_element; |
strysd | 25:929a72bcd8d2 | 232 | #endif |
strysd | 25:929a72bcd8d2 | 233 | return fiap.post(element, i); |
yueee_yt | 0:27cf9683af17 | 234 | } |
yueee_yt | 0:27cf9683af17 | 235 | |
strysd | 25:929a72bcd8d2 | 236 | #if FIAP_DEBUG_MODE |
strysd | 25:929a72bcd8d2 | 237 | #else |
strysd | 17:868a61434584 | 238 | /* stop USB Interface */ |
strysd | 17:868a61434584 | 239 | #define USR_POWERDOWN (0x104) |
strysd | 17:868a61434584 | 240 | int semihost_powerdown() { |
strysd | 17:868a61434584 | 241 | uint32_t arg; |
strysd | 17:868a61434584 | 242 | return __semihost(USR_POWERDOWN, &arg); |
strysd | 17:868a61434584 | 243 | } |
strysd | 25:929a72bcd8d2 | 244 | #endif |
strysd | 17:868a61434584 | 245 | |
strysd | 20:44214878a602 | 246 | /** Frequency Control |
strysd | 20:44214878a602 | 247 | * @see ClockControl/ClockControl.cpp by Michael Wei |
strysd | 20:44214878a602 | 248 | */ |
strysd | 18:f277fbbcea4b | 249 | void setPLL0Frequency(unsigned char clkSrc, unsigned short M, unsigned char N) |
strysd | 18:f277fbbcea4b | 250 | { |
strysd | 18:f277fbbcea4b | 251 | LPC_SC->CLKSRCSEL = clkSrc; |
strysd | 18:f277fbbcea4b | 252 | LPC_SC->PLL0CFG = (((unsigned int)N-1) << 16) | M-1; |
strysd | 18:f277fbbcea4b | 253 | LPC_SC->PLL0CON = 0x01; |
strysd | 18:f277fbbcea4b | 254 | LPC_SC->PLL0FEED = 0xAA; |
strysd | 18:f277fbbcea4b | 255 | LPC_SC->PLL0FEED = 0x55; |
strysd | 18:f277fbbcea4b | 256 | while (!(LPC_SC->PLL0STAT & (1<<26))); |
strysd | 18:f277fbbcea4b | 257 | |
strysd | 18:f277fbbcea4b | 258 | LPC_SC->PLL0CON = 0x03; |
strysd | 18:f277fbbcea4b | 259 | LPC_SC->PLL0FEED = 0xAA; |
strysd | 18:f277fbbcea4b | 260 | LPC_SC->PLL0FEED = 0x55; |
strysd | 18:f277fbbcea4b | 261 | } |
strysd | 18:f277fbbcea4b | 262 | |
strysd | 18:f277fbbcea4b | 263 | unsigned int setSystemFrequency(unsigned char clkDivider, unsigned char clkSrc, unsigned short M, unsigned char N) |
strysd | 18:f277fbbcea4b | 264 | { |
strysd | 18:f277fbbcea4b | 265 | setPLL0Frequency(clkSrc, M, N); |
strysd | 18:f277fbbcea4b | 266 | LPC_SC->CCLKCFG = clkDivider - 1; |
strysd | 18:f277fbbcea4b | 267 | SystemCoreClockUpdate(); |
strysd | 18:f277fbbcea4b | 268 | return SystemCoreClock; |
strysd | 18:f277fbbcea4b | 269 | } |
strysd | 18:f277fbbcea4b | 270 | |
yueee_yt | 0:27cf9683af17 | 271 | int main() |
yueee_yt | 0:27cf9683af17 | 272 | { |
strysd | 20:44214878a602 | 273 | setSystemFrequency(0x3, 0x1, 6, 1);//clock down from 96MHz to 48MHz |
strysd | 25:929a72bcd8d2 | 274 | led1 = led3 = led4 = 0; |
strysd | 9:0d34968a5de1 | 275 | |
strysd | 25:929a72bcd8d2 | 276 | #if USE_RJ45_LAMP |
strysd | 25:929a72bcd8d2 | 277 | speed = link = 0; |
strysd | 25:929a72bcd8d2 | 278 | #endif |
strysd | 25:929a72bcd8d2 | 279 | |
strysd | 25:929a72bcd8d2 | 280 | pc.baud(USB_PAUD_RATE); |
strysd | 4:f50c307c5eda | 281 | |
yueee_yt | 0:27cf9683af17 | 282 | eth.init(); //Use DHCP |
yueee_yt | 0:27cf9683af17 | 283 | eth.connect(); |
strysd | 10:51ef659414a9 | 284 | printf("IP: %s\r\n", eth.getIPAddress()); |
strysd | 18:f277fbbcea4b | 285 | |
strysd | 10:51ef659414a9 | 286 | led1 = 1; |
strysd | 18:f277fbbcea4b | 287 | if(syncNTP() != 0) { |
strysd | 18:f277fbbcea4b | 288 | printf("NTP Sync Error\r\n"); |
strysd | 18:f277fbbcea4b | 289 | return -1; |
strysd | 18:f277fbbcea4b | 290 | } |
strysd | 18:f277fbbcea4b | 291 | ctTime = time(NULL); |
strysd | 18:f277fbbcea4b | 292 | printf("Time: %s\r\n", ctime(&ctTime)); |
strysd | 18:f277fbbcea4b | 293 | |
strysd | 18:f277fbbcea4b | 294 | if(setFIAPStorage() != 0) { |
strysd | 18:f277fbbcea4b | 295 | printf("Check if storage.txt exists in mbed memory.\r\n"); |
yueee_yt | 0:27cf9683af17 | 296 | return -1; |
yueee_yt | 0:27cf9683af17 | 297 | } |
strysd | 10:51ef659414a9 | 298 | led1 = 0; |
strysd | 18:f277fbbcea4b | 299 | |
strysd | 25:929a72bcd8d2 | 300 | #if FIAP_DEBUG_MODE |
strysd | 25:929a72bcd8d2 | 301 | #else |
strysd | 18:f277fbbcea4b | 302 | semihost_powerdown();//Stop also USB Interface |
strysd | 25:929a72bcd8d2 | 303 | #endif |
strysd | 13:4e51ac520b33 | 304 | |
strysd | 13:4e51ac520b33 | 305 | exitTimer = RETRY_TIMES_ON_ERROR; |
strysd | 8:e482433c9593 | 306 | while(exitTimer > 0) { |
strysd | 8:e482433c9593 | 307 | led3 = spd; |
strysd | 8:e482433c9593 | 308 | led4 = lnk; |
strysd | 25:929a72bcd8d2 | 309 | |
strysd | 25:929a72bcd8d2 | 310 | #if USE_RJ45_LAMP |
strysd | 25:929a72bcd8d2 | 311 | speed = !spd; |
strysd | 25:929a72bcd8d2 | 312 | link = !lnk; |
strysd | 25:929a72bcd8d2 | 313 | #endif |
strysd | 25:929a72bcd8d2 | 314 | |
strysd | 8:e482433c9593 | 315 | if (led4 == 1 || led3 == 1) { |
strysd | 8:e482433c9593 | 316 | exitTimer--; |
strysd | 13:4e51ac520b33 | 317 | wait(WAIT_NEXT_SEND); |
strysd | 13:4e51ac520b33 | 318 | continue; |
strysd | 13:4e51ac520b33 | 319 | } |
strysd | 13:4e51ac520b33 | 320 | |
strysd | 18:f277fbbcea4b | 321 | if(sendFIAP() != 0){ |
strysd | 13:4e51ac520b33 | 322 | led1 = 1; |
strysd | 13:4e51ac520b33 | 323 | exitTimer--; |
strysd | 8:e482433c9593 | 324 | } else { |
strysd | 13:4e51ac520b33 | 325 | led1 = 0; |
strysd | 13:4e51ac520b33 | 326 | exitTimer = RETRY_TIMES_ON_ERROR;//initialize |
strysd | 8:e482433c9593 | 327 | } |
strysd | 13:4e51ac520b33 | 328 | wait(WAIT_NEXT_SEND); |
yueee_yt | 0:27cf9683af17 | 329 | } |
strysd | 8:e482433c9593 | 330 | eth.disconnect(); |
strysd | 25:929a72bcd8d2 | 331 | #if FIAP_DEBUG_MODE |
strysd | 25:929a72bcd8d2 | 332 | printf("Disconnected because of error\r\n"); |
strysd | 25:929a72bcd8d2 | 333 | #endif |
strysd | 13:4e51ac520b33 | 334 | |
strysd | 8:e482433c9593 | 335 | return -1; |
strysd | 5:720a29128f41 | 336 | } |