source: UsbWattMeter/trunk/src/usb_watt_meter/main.c@ 164

Last change on this file since 164 was 164, checked in by coas-nagasima, 6 years ago

TOPPERS/ECNLサンプルアプリ「USB充電器電力計」を追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 27.8 KB
Line 
1/*
2 * TOPPERS ECHONET Lite Communication Middleware
3 *
4 * Copyright (C) 2016 Cores Co., Ltd. Japan
5 *
6 * ã‹L’˜ìŒ ŽÒ‚́CˆÈ‰º‚Ì(1)`(4)‚ÌðŒ‚ð–ž‚½‚·ê‡‚ÉŒÀ‚èC–{ƒ\ƒtƒgƒEƒF
7 * ƒAi–{ƒ\ƒtƒgƒEƒFƒA‚ð‰ü•Ï‚µ‚½‚à‚Ì‚ðŠÜ‚ށDˆÈ‰º“¯‚¶j‚ðŽg—pE•¡»E‰ü
8 * •ÏEÄ”z•ziˆÈ‰ºC—˜—p‚ƌĂԁj‚·‚邱‚Ƃ𖳏ž‚Å‹–‘ø‚·‚éD
9 * (1) –{ƒ\ƒtƒgƒEƒFƒA‚ðƒ\[ƒXƒR[ƒh‚ÌŒ`‚Å—˜—p‚·‚éê‡‚ɂ́Cã‹L‚Ì’˜ì
10 * Œ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’肪C‚»‚Ì‚Ü‚Ü‚ÌŒ`‚Ń\[
11 * ƒXƒR[ƒh’†‚ÉŠÜ‚Ü‚ê‚Ä‚¢‚邱‚ƁD
12 * (2) –{ƒ\ƒtƒgƒEƒFƒA‚ðCƒ‰ƒCƒuƒ‰ƒŠŒ`Ž®‚ȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
13 * —p‚Å‚«‚éŒ`‚ōĔz•z‚·‚éê‡‚ɂ́CÄ”z•z‚É”º‚¤ƒhƒLƒ…
14ƒƒ“ƒgi—˜—p
15 * ŽÒƒ}ƒjƒ…
16ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L
17 * ‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
18 * (3) –{ƒ\ƒtƒgƒEƒFƒA‚ðC‹@Ší‚É‘g‚ݍž‚ނȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
19 * —p‚Å‚«‚È‚¢Œ`‚ōĔz•z‚·‚éê‡‚ɂ́CŽŸ‚Ì‚¢‚¸‚ê‚©‚ÌðŒ‚ð–ž‚½‚·‚±
20 * ‚ƁD
21 * (a) Ä”z•z‚É”º‚¤ƒhƒLƒ…
22ƒƒ“ƒgi—˜—pŽÒƒ}ƒjƒ…
23ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜
24 * ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
25 * (b) Ä”z•z‚ÌŒ`‘Ô‚ðC•Ê‚É’è‚ß‚é•û–@‚É‚æ‚Á‚āCTOPPERSƒvƒƒWƒFƒNƒg‚É
26 * •ñ‚·‚邱‚ƁD
27 * (4) –{ƒ\ƒtƒgƒEƒFƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚é‚¢‚©‚Ȃ鑹
28 * ŠQ‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð–Ɛӂ·‚邱‚ƁD
29 * ‚Ü‚½C–{ƒ\ƒtƒgƒEƒFƒA‚̃†[ƒU‚Ü‚½‚̓Gƒ“ƒhƒ†[ƒU‚©‚ç‚Ì‚¢‚©‚Ȃ闝
30 * —R‚ÉŠî‚­¿‹‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð
31 * –Ɛӂ·‚邱‚ƁD
32 *
33 * –{ƒ\ƒtƒgƒEƒFƒA‚́C–³•ÛØ‚Å’ñ‹Ÿ‚³‚ê‚Ä‚¢‚é‚à‚Ì‚Å‚ ‚éDã‹L’˜ìŒ ŽÒ‚¨
34 * ‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚́C–{ƒ\ƒtƒgƒEƒFƒA‚ÉŠÖ‚µ‚āC“Á’è‚ÌŽg—p–Ú“I
35 * ‚ɑ΂·‚é“K‡«‚àŠÜ‚߂āC‚¢‚©‚È‚é•ÛØ‚às‚í‚È‚¢D‚Ü‚½C–{ƒ\ƒtƒgƒEƒF
36 * ƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚½‚¢‚©‚Ȃ鑹ŠQ‚ÉŠÖ‚µ‚Ä‚àC‚»
37 * ‚̐ӔC‚𕉂í‚È‚¢D
38 *
39 * @(#) $Id: main.c 164 2016-03-07 11:33:50Z coas-nagasima $
40 */
41
42/*
43 * ƒTƒ“ƒvƒ‹ƒvƒƒOƒ‰ƒ€(1)‚Ì–{‘Ì
44 */
45
46#include <kernel.h>
47#include <t_syslog.h>
48#include <t_stdlib.h>
49#include <sil.h>
50#include <string.h>
51#include "syssvc/serial.h"
52#include "syssvc/syslog.h"
53#include "kernel_cfg.h"
54#include "main.h"
55#include "echonet_cfg.h"
56#ifdef __RX
57#include "rx630_ccrx/rx630.h"
58#else
59#include "rx630_msvc/rx630.h"
60#endif
61#include "lwip/netif.h"
62#include "lwip/tcpip.h"
63#include "lwip/dhcp.h"
64#include "sntp/sntp.h"
65#include "ff.h"
66#include "diskio.h"
67#include "wolfssl/wolfcrypt/settings.h"
68#include "client.h"
69
70/* TODO: MACƒAƒhƒŒƒX‚ðÝ’è */
71uint8_t mac_addr[6] = { 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A };
72
73struct lwip_task_t {
74 struct netif netif;
75 ID sem_rev; /* struct ethernetif */
76 uint32_t ethernetif[8];
77} lwiptsk = {{0},SEM_IF_RX62N_RBUF_READY};
78
79/* TODO: ƒ[ƒJ[ƒR[ƒh‚ðÝ’è */
80#define MAKER_CODE 0x00, 0x00, 0xB3 /* TOPPERSƒvƒƒWƒFƒNƒg */
81
82/* ƒm[ƒhƒvƒƒtƒ@ƒCƒ‹ƒIƒuƒWƒFƒNƒg */
83struct node_profile_object_t local_node_data = {
84 0x30, /* “®ìó‘Ô */
85 { 0x01, 0x0A, 0x01, 0x00 }, /* ‚u‚…
86‚’‚“‚‰‚‚Žî•ñ */
87 {
88 0xFE, /* ‰ºˆÊ’ʐM‘wIDƒtƒB[ƒ‹ƒh */
89 { MAKER_CODE }, /* ƒ[ƒJ[ƒR[ƒh */
90 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }, /* ƒ†ƒj[ƒNID•”(ƒ[ƒJ[“ÆŽ©) */
91 },
92 0,
93 { MAKER_CODE }, /* ƒ[ƒJ[ƒR[ƒh */
94};
95
96struct watt_hour_meter_t electric_energy_meter_data[6] = {
97 /* “d—͗ʃ[ƒ^‚P */
98 {
99 0x30, /* “®ìó‘Ô */
100 0x00, /* Ý’uêŠ */
101 { 0x00, 0x00, 'C', 0x00 }, /* ‹KŠi‚u‚…
102‚’‚“‚‰‚‚Žî•ñ */
103 0x41, /* ˆÙí”­¶ó‘Ô */
104 { MAKER_CODE }, /* ƒ[ƒJ[ƒR[ƒh */
105 0x02, /* ÏŽZ“d—Í—Ê’PˆÊ */
106 0x00000000, /* ÏŽZ“d—Í—ÊŒv‘ª’l */
107 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, }, /* ÏŽZ“d—Í—ÊŒv‘ª’l—š—ð‚P */
108 },
109 /* “d—͗ʃ[ƒ^‚Q */
110 {
111 0x30, /* “®ìó‘Ô */
112 0x00, /* Ý’uêŠ */
113 {0x00, 0x00, 'C', 0x00}, /* ‹KŠi‚u‚…
114‚’‚“‚‰‚‚Žî•ñ */
115 0x41, /* ˆÙí”­¶ó‘Ô */
116 {MAKER_CODE}, /* ƒ[ƒJ[ƒR[ƒh */
117 0x02, /* ÏŽZ“d—Í—Ê’PˆÊ */
118 0x00000000, /* ÏŽZ“d—Í—ÊŒv‘ª’l */
119 {0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,}, /* ÏŽZ“d—Í—ÊŒv‘ª’l—š—ð‚P */
120 },
121 /* “d—͗ʃ[ƒ^‚R */
122 {
123 0x30, /* “®ìó‘Ô */
124 0x00, /* Ý’uêŠ */
125 {0x00, 0x00, 'C', 0x00}, /* ‹KŠi‚u‚…
126‚’‚“‚‰‚‚Žî•ñ */
127 0x41, /* ˆÙí”­¶ó‘Ô */
128 {MAKER_CODE}, /* ƒ[ƒJ[ƒR[ƒh */
129 0x02, /* ÏŽZ“d—Í—Ê’PˆÊ */
130 0x00000000, /* ÏŽZ“d—Í—ÊŒv‘ª’l */
131 {0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,}, /* ÏŽZ“d—Í—ÊŒv‘ª’l—š—ð‚P */
132 },
133 /* “d—͗ʃ[ƒ^‚S */
134 {
135 0x30, /* “®ìó‘Ô */
136 0x00, /* Ý’uêŠ */
137 {0x00, 0x00, 'C', 0x00}, /* ‹KŠi‚u‚…
138‚’‚“‚‰‚‚Žî•ñ */
139 0x41, /* ˆÙí”­¶ó‘Ô */
140 {MAKER_CODE}, /* ƒ[ƒJ[ƒR[ƒh */
141 0x02, /* ÏŽZ“d—Í—Ê’PˆÊ */
142 0x00000000, /* ÏŽZ“d—Í—ÊŒv‘ª’l */
143 {0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,}, /* ÏŽZ“d—Í—ÊŒv‘ª’l—š—ð‚P */
144 },
145 /* “d—͗ʃ[ƒ^‚T */
146 {
147 0x30, /* “®ìó‘Ô */
148 0x00, /* Ý’uêŠ */
149 {0x00, 0x00, 'C', 0x00}, /* ‹KŠi‚u‚…
150‚’‚“‚‰‚‚Žî•ñ */
151 0x41, /* ˆÙí”­¶ó‘Ô */
152 {MAKER_CODE}, /* ƒ[ƒJ[ƒR[ƒh */
153 0x02, /* ÏŽZ“d—Í—Ê’PˆÊ */
154 0x00000000, /* ÏŽZ“d—Í—ÊŒv‘ª’l */
155 {0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,}, /* ÏŽZ“d—Í—ÊŒv‘ª’l—š—ð‚P */
156 },
157 /* “d—͗ʃ[ƒ^‚U */
158 {
159 0x30, /* “®ìó‘Ô */
160 0x00, /* Ý’uêŠ */
161 {0x00, 0x00, 'C', 0x00}, /* ‹KŠi‚u‚…
162‚’‚“‚‰‚‚Žî•ñ */
163 0x41, /* ˆÙí”­¶ó‘Ô */
164 {MAKER_CODE}, /* ƒ[ƒJ[ƒR[ƒh */
165 0x02, /* ÏŽZ“d—Í—Ê’PˆÊ */
166 0x00000000, /* ÏŽZ“d—Í—ÊŒv‘ª’l */
167 {0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,}, /* ÏŽZ“d—Í—ÊŒv‘ª’l—š—ð‚P */
168 }
169};
170
171/*
172 * “®ìó‘ÔON/OFFÝ’èŠÖ”i0x30, 0x31‚̂ݎ󂯕t‚¯j
173 */
174int onoff_prop_set(const EPRPINIB *item, const void *src, int size, bool_t *anno)
175{
176 /* ƒTƒCƒY‚ª‚PˆÈŠO‚͎󂯕t‚¯‚È‚¢ */
177 if(size != 1)
178 return 0;
179
180 *anno = *((uint8_t*)item->exinf) != *((uint8_t*)src);
181
182 switch(*(uint8_t *)src){
183 /* ON‚̏ꍇ */
184 case 0x30:
185 *((uint8_t *)item->exinf) = *((uint8_t *)src);
186 /* LED‚Ì"."‚ðON */
187 sil_wrb_mem((uint8_t *)PORTE_PODR_ADDR, sil_reb_mem((uint8_t *)PORTE_PODR_ADDR) | 0x01);
188 break;
189 /* OFF‚̏ꍇ */
190 case 0x31:
191 *((uint8_t *)item->exinf) = *((uint8_t *)src);
192 /* LED‚Ì"."‚ðOFF */
193 sil_wrb_mem((uint8_t *)PORTE_PODR_ADDR, sil_reb_mem((uint8_t *)PORTE_PODR_ADDR) & ~0x01);
194 break;
195 /* 0x30‚©0x31ˆÈŠO‚͎󂯕t‚¯‚È‚¢ */
196 default:
197 return 0;
198 }
199
200 return 1;
201}
202
203/*
204 * ˆÙí”­¶ó‘ԐݒèŠÖ”i0x41, 0x42‚̂ݎ󂯕t‚¯j
205 */
206int alarm_prop_set(const EPRPINIB *item, const void *src, int size, bool_t *anno)
207{
208 /* ƒTƒCƒY‚ª‚PˆÈŠO‚͎󂯕t‚¯‚È‚¢ */
209 if(size != 1)
210 return 0;
211
212 *anno = *((uint8_t *)item->exinf) != *((uint8_t *)src);
213
214 switch(*(uint8_t *)src){
215 /* ˆÙí”­¶‚ ‚è‚̏ꍇ */
216 case 0x41:
217 /* ˆÙí”­¶‚È‚µ‚̏ꍇ */
218 case 0x42:
219 *((uint8_t *)item->exinf) = *((uint8_t *)src);
220 break;
221 /* 0x41‚©0x42ˆÈŠO‚͎󂯕t‚¯‚È‚¢ */
222 default:
223 return 0;
224 }
225
226 return 1;
227}
228
229/*
230 * Œ»ÝŽžÝ’èŠÖ”
231 */
232int time_prop_set(const EPRPINIB *item, const void *src, int size, bool_t *anno)
233{
234 uint8_t *p_src;
235
236 if (size != 2)
237 return 0;
238
239 /* ŽžÝ’è */
240 p_src = (uint8_t *)src;
241 sil_wrb_mem((uint8_t *)RTC_RHRCNT_ADDR, *p_src++);
242 sil_wrb_mem((uint8_t *)RTC_RMINCNT_ADDR, *p_src++);
243 sil_wrb_mem((uint8_t *)RTC_RSECCNT_ADDR, 0x00);
244
245 return (intptr_t)p_src - (intptr_t)src;
246}
247
248/*
249 * Œ»ÝŽžŽæ“¾ŠÖ”
250 */
251int time_prop_get(const EPRPINIB *item, void *dst, int size)
252{
253 uint8_t *p_dst;
254
255 if (size != 2)
256 return 0;
257
258 /* ŽžÝ’è */
259 p_dst = (uint8_t *)dst;
260 *p_dst++ = sil_reb_mem((uint8_t *)RTC_RHRCNT_ADDR);
261 *p_dst++ = sil_reb_mem((uint8_t *)RTC_RMINCNT_ADDR);
262
263 return (intptr_t)p_dst - (intptr_t)dst;
264}
265
266/*
267 * Œ»Ý”NŒŽ“úÝ’èŠÖ”
268 */
269int date_prop_set(const EPRPINIB *item, const void *src, int size, bool_t *anno)
270{
271 uint8_t *p_src;
272
273 if (size != 4)
274 return 0;
275
276 /* ”NŒŽ“úÝ’è */
277 p_src = (uint8_t *)src;
278 p_src++; /* 20XX */
279 sil_wrb_mem((uint8_t *)RTC_RYRCNT_ADDR, *p_src++);
280 sil_wrb_mem((uint8_t *)RTC_RMONCNT_ADDR, *p_src++);
281 sil_wrb_mem((uint8_t *)RTC_RDAYCNT_ADDR, *p_src++);
282
283 return (intptr_t)p_src - (intptr_t)src;
284}
285
286/*
287 * Œ»Ý”NŒŽ“úŽæ“¾ŠÖ”
288 */
289int date_prop_get(const EPRPINIB *item, void *dst, int size)
290{
291 uint8_t *p_dst;
292
293 if (size != 4)
294 return 0;
295
296 p_dst = (uint8_t *)dst;
297 *p_dst++ = 0x20;
298 *p_dst++ = sil_reb_mem((uint8_t *)RTC_RYRCNT_ADDR);
299 *p_dst++ = sil_reb_mem((uint8_t *)RTC_RMONCNT_ADDR);
300 *p_dst++ = sil_reb_mem((uint8_t *)RTC_RDAYCNT_ADDR);
301
302 return (intptr_t)p_dst - (intptr_t)dst;
303}
304
305/*
306 * ÏŽZ“d—Í—ÊŒv‘ª’lŽæ“¾ŠÖ”
307 */
308int watt_hour_meter_integral_electric_energy_measurement_value_get(const EPRPINIB *item, void *dst, int size)
309{
310 struct watt_hour_meter_t *meter = (struct watt_hour_meter_t *)(item->exinf - (intptr_t)&((struct watt_hour_meter_t *)0)->integral_electric_energy_measurement_value);
311
312 /* ƒTƒCƒY‚ª4ˆÈŠO‚͎󂯕t‚¯‚È‚¢ */
313 if(size != 4)
314 return 0;
315
316 *((uint32_t *)dst) = meter->integral_electric_energy_measurement_value;
317
318 return 4;
319}
320
321int watt_hour_meter_integral_electric_energy_measurement_log_get(const EPRPINIB *item, void *dst, int size)
322{
323 struct watt_hour_meter_t *meter = (struct watt_hour_meter_t *)(item->exinf - (intptr_t)&((struct watt_hour_meter_t *)0)->integral_electric_energy_measurement_log);
324 int len;
325
326 /* ƒTƒCƒY‚ª192ˆÈŠO‚͎󂯕t‚¯‚È‚¢ */
327 if (size != 192)
328 return 0;
329
330 wai_sem(MAIN_SEMAPHORE);
331
332 len = 48 - meter->current_pos;
333 if(len > 0)
334 memcpy(dst, &meter->integral_electric_energy_measurement_log[meter->current_pos], sizeof(uint32_t) * len);
335 dst = &((uint8_t *)dst)[len];
336 len = 48 - len;
337 if (len > 0)
338 memcpy(dst, &meter->integral_electric_energy_measurement_log[0], sizeof(uint32_t) * len);
339
340 sig_sem(MAIN_SEMAPHORE);
341
342 return 192;
343}
344
345static uint32_t whm_add_integral_value(struct watt_hour_meter_t *meter, uint32_t watt_hour)
346{
347 bool_t carry;
348 uint32_t value = watt_hour;
349 int current_pos = meter->current_pos;
350 current_pos++;
351 if (current_pos >= 48)
352 current_pos = 0;
353
354 carry = value >= 100000000;
355 if (carry)
356 value -= 100000000;
357
358 wai_sem(MAIN_SEMAPHORE);
359
360 meter->integral_electric_energy_measurement_value = value;
361 meter->integral_electric_energy_measurement_log[current_pos] = value;
362 meter->current_pos = current_pos;
363
364 sig_sem(MAIN_SEMAPHORE);
365
366 return carry ? 100000000 : 0;
367}
368
369static void fatfs_initialize();
370static void netif_initialize();
371static void netif_status_callback(struct netif *netif);
372static void netif_link_callback(struct netif *netif);
373static void main_initialize();
374static TMO main_get_timer();
375static void main_progress(TMO interval);
376static void main_recv_esv(T_EDATA *esv);
377static void main_break_wait(uint8_t *brkdat, int32_t len);
378static void main_timeout();
379
380/*
381 * ƒƒCƒ“ƒ^ƒXƒN
382 */
383void main_task(intptr_t exinf)
384{
385 ER ret, ret2;
386 SYSTIM prev, now;
387 TMO timer;
388 T_EDATA *esv;
389 uint8_t brkdat[64];
390 int32_t len;
391
392 /* ƒtƒ@ƒCƒ‹ƒVƒXƒeƒ€‚̏‰Šú‰» */
393 fatfs_initialize();
394
395 /* ƒvƒƒgƒRƒ‹ƒXƒ^ƒbƒN‚̏‰Šú‰» */
396 netif_initialize();
397
398 /* ƒAƒvƒŠƒP[ƒVƒ‡ƒ“‚̏‰Šú‰» */
399 main_initialize();
400
401 ret2 = get_tim(&now);
402 if (ret2 != E_OK){
403 syslog(LOG_ERROR, "get_tim");
404 return;
405 }
406
407 for(;;){
408 prev = now;
409
410 /* ƒ^ƒCƒ}[Žæ“¾ */
411 timer = main_get_timer();
412
413 /* ‰ž“š“d•¶‘Ò‚¿ */
414 ret = ecn_trcv_esv(&esv, timer);
415 if ((ret != E_OK) && (ret != E_WBLK) && (ret != E_TMOUT)){
416 syslog(LOG_ERROR, "ecn_trcv_esv");
417 break;
418 }
419
420 ret2 = get_tim(&now);
421 if (ret2 != E_OK){
422 syslog(LOG_ERROR, "get_tim");
423 break;
424 }
425
426 /* ŽžŠÔŒo‰ß */
427 main_progress(now - prev);
428
429 /* Echonet“d•¶ŽóM‚̏ꍇ */
430 if (ret == E_OK) {
431 /* Echonet“d•¶ŽóMˆ— */
432 main_recv_esv(esv);
433
434 /* —̈æ‰ð•ú */
435 ret = ecn_rel_esv(esv);
436 if (ret != E_OK){
437 syslog(LOG_ERROR, "ecn_rel_esv");
438 break;
439 }
440 }
441 /* ‰ž“š“d•¶‘Ò‚¿‚ÌŠ„‚荞‚݂̏ꍇ */
442 else if (ret == E_WBLK) {
443 /* ‰ž“š“d•¶‘Ò‚¿‚ÌŠ„‚荞‚݃f[ƒ^Žæ“¾ */
444 ret = ecn_get_brk_dat(esv, brkdat, sizeof(brkdat), &len);
445 if (ret != E_OK){
446 syslog(LOG_ERROR, "ecn_get_brk_dat");
447 break;
448 }
449
450 /* ‰ž“š“d•¶‘Ò‚¿‚ÌŠ„‚荞‚ݏˆ— */
451 main_break_wait(brkdat, len);
452
453 /* —̈æ‰ð•ú */
454 ret = ecn_rel_esv(esv);
455 if (ret != E_OK){
456 syslog(LOG_ERROR, "ecn_rel_esv");
457 break;
458 }
459 }
460
461 /* ƒ^ƒCƒ€ƒAƒEƒgˆ— */
462 main_timeout();
463 }
464}
465
466FATFS FatFs[_VOLUMES]; /* File system object for logical drive */
467
468static void fatfs_initialize()
469{
470 DSTATUS dst;
471 FRESULT res;
472 BYTE pdrv = 0, type;
473
474 if ((dst = disk_initialize(pdrv)) != RES_OK) {
475 syslog(LOG_ERROR, "disk_initialize %d", dst);
476 return;
477 }
478
479 if ((dst = disk_ioctl(pdrv, MMC_GET_TYPE, &type)) != RES_OK) {
480 syslog(LOG_ERROR, "disk_ioctl MMC_GET_TYPE NG %d", dst);
481 }
482 else {
483 switch(type){
484 case CT_MMC:
485 syslog(LOG_NOTICE, "MMC ver 3");
486 break;
487 case CT_SD1:
488 syslog(LOG_NOTICE, "SD ver 1");
489 break;
490 case CT_SD2:
491 syslog(LOG_NOTICE, "SD ver 2");
492 break;
493 case CT_SDC:
494 syslog(LOG_NOTICE, "SD");
495 break;
496 case CT_SD2 | CT_BLOCK:
497 syslog(LOG_NOTICE, "SD ver 2 (Block addressing)");
498 break;
499 default:
500 syslog(LOG_NOTICE, "???(%x)", type);
501 break;
502 }
503 }
504
505 if ((res = f_mount(&FatFs[0], "", 1)) != FR_OK) {
506 syslog(LOG_ERROR, "f_mount %d", res);
507 return;
508 }
509}
510
511bool_t started = false;
512struct dhcp dhcp;
513bool_t dhcp_enable = true;
514
515static void netif_initialize()
516{
517 extern err_t ethernetif_init(struct netif *netif);
518 ER ret;
519 struct ip_addr xIpAddr, xNetMast, xGateway;
520 uint8_t data[32], c;
521 int i, j;
522
523 for (i = 0, j = 0; i < sizeof(mac_addr); i++) {
524 c = mac_addr[i] >> 4;
525 data[j++] = (c < 10) ? ('0' + c) : ('A' - 10 + c);
526 c = mac_addr[i] & 0xF;
527 data[j++] = (c < 10) ? ('0' + c) : ('A' - 10 + c);
528 data[j++] = ':';
529 }
530 data[--j] = '\0';
531
532 syslog(LOG_NOTICE, "mac_addr %s %s", data, dhcp_enable ? "dhcp" : "static");
533
534 /* libcurl‚ÆWolfSSL‚ÆlwIP‚̏‰Šú‰» */
535 client_init();
536
537 IP4_ADDR(&xIpAddr, 192, 168, 2, 110);
538 IP4_ADDR(&xNetMast, 255, 255, 255, 0);
539 IP4_ADDR(&xGateway, 192, 168, 2, 1);
540
541 /* Create and configure the EMAC interface. */
542 netif_add(&lwiptsk.netif, &xIpAddr, &xNetMast,
543 &xGateway, NULL, ethernetif_init, tcpip_input);
544
545 netif_set_link_callback(&lwiptsk.netif, netif_link_callback);
546 netif_set_status_callback(&lwiptsk.netif, netif_status_callback);
547
548 dhcp_set_struct(&lwiptsk.netif, &dhcp);
549
550 /* make it the default interface */
551 netif_set_default(&lwiptsk.netif);
552
553 ret = act_tsk(LWIP_TASK);
554 if (ret != E_OK)
555 return;
556}
557
558static void netif_link_callback(struct netif *netif)
559{
560 if ((lwiptsk.netif.flags & NETIF_FLAG_LINK_UP) == 0)
561 return;
562
563 dhcp_start(&lwiptsk.netif);
564}
565
566static void netif_status_callback(struct netif *netif)
567{
568 ER ret;
569 bool_t up;
570
571 up = (lwiptsk.netif.flags & NETIF_FLAG_UP) != 0;
572 if (up && !started) {
573 started = true;
574
575 /* ECHONETƒ~ƒhƒ‹ƒEƒFƒA‚ð‹N“® */
576 ret = ecn_sta_svc();
577 if (ret != E_OK)
578 return;
579
580 /* SNTP‚ðŠJŽn */
581 sntp_init();
582 }
583
584 /* ƒƒCƒ“ƒ^ƒXƒN‚É’Ê’m */
585 uint8_t data[2];
586 data[0] = 0x01;
587 data[1] = up ? 0x01 : 0x02;
588 ret = ecn_brk_wai(data, sizeof(data));
589 if (ret != E_OK) {
590 syslog(LOG_ERROR, "ecn_brk_wai");
591 return;
592 }
593}
594
595enum main_state_t{
596 main_state_idle,
597};
598
599TMO main_timer = TMO_FEVR;
600enum main_state_t main_state = main_state_idle;
601TMO main_led_timer = TMO_FEVR;
602TMO main_adv_timer = TMO_FEVR;
603int main_whm_counter;
604
605bool_t main_btn1_state;
606int main_btn1_count = 0;
607bool_t main_btn2_state;
608int main_btn2_count = 0;
609
610/*
611 * ‰Šú‰»
612 */
613static void main_initialize()
614{
615 uint8_t btn;
616
617 /*
618 * ƒ‚ƒWƒ…
619[ƒ‹ƒXƒgƒbƒv‹@”\‚̐ݒè(S12AD)
620 */
621 sil_wrh_mem((uint16_t *)SYSTEM_PRCR_ADDR, (uint16_t)0xA502); /* ‘ž‚Ý‹–‰Â */
622 sil_wrw_mem((uint32_t *)SYSTEM_MSTPCRA_ADDR,
623 sil_rew_mem((uint32_t *)SYSTEM_MSTPCRA_ADDR) & ~SYSTEM_MSTPCRA_MSTPA17_BIT);
624 sil_wrh_mem((uint16_t *)SYSTEM_PRCR_ADDR, (uint16_t)0xA500); /* ‘ž‚Ý‹ÖŽ~ */
625
626 /* 7seg_1100—pƒ|[ƒgo—͐ݒè */
627 sil_wrb_mem((uint8_t *)PORT2_PDR_ADDR, sil_reb_mem((uint8_t *)PORT2_PDR_ADDR) | PORT_PDR_B4_BIT | PORT_PDR_B3_BIT);
628 /* 7seg_0010—pƒ|[ƒgo—͐ݒè */
629 sil_wrb_mem((uint8_t *)PORT5_PDR_ADDR, sil_reb_mem((uint8_t *)PORT5_PDR_ADDR) | PORT_PDR_B3_BIT);
630 /* 7seg_0001—pƒ|[ƒgo—͐ݒè */
631 sil_wrb_mem((uint8_t *)PORTC_PDR_ADDR, sil_reb_mem((uint8_t *)PORTC_PDR_ADDR) | PORT_PDR_B1_BIT);
632 /* 7seg_led—pƒ|[ƒgo—͐ݒè */
633 sil_wrb_mem((uint8_t *)PORTD_PDR_ADDR, 0xFF);
634 /* SWITCH—pƒ|[ƒg“ü—́ALED—pƒ|[ƒgo—͐ݒè */
635 sil_wrb_mem((uint8_t *)PORTE_PDR_ADDR, 0x0F);
636
637 /* LED‚ð"0000"‚Æ•\Ž¦ */
638 sil_wrb_mem((uint8_t *)PORTE_PDR_ADDR, 0x0F);
639
640 /* 3msŒã‚É7SEG•\Ž¦‚ðXV */
641 main_led_timer = 3;
642
643 /* 10msŒã‚Ƀ{ƒ^ƒ“ó‘Ô‚ðŠm”F */
644 main_timer = 10;
645
646 /* 12bitADC‰Šú‰» */
647 sil_wrh_mem((uint16_t *)S12AD_ADEXICR_ADDR, 0x0000); /* ‰·“xƒZƒ“ƒTo—́A“à•”Šî€“dˆ³”ñ‘I‘ð */
648 sil_wrh_mem((uint16_t *)S12AD_ADANS0_ADDR, 0x00FF); /* •ÏŠ·ƒ|[ƒgAN000`AN007‘I‘ðAAN008`AN015”ñ‘I‘ð */
649 sil_wrh_mem((uint16_t *)S12AD_ADANS1_ADDR, 0x0000); /* •ÏŠ·ƒ|[ƒgAN016`AN020”ñ‘I‘ð */
650
651 /* ƒ{ƒ^ƒ“ó‘ԓǂݍž‚Ý */
652 btn = sil_reb_mem((uint8_t *)PORT0_PIDR_ADDR);
653 main_btn1_state = (btn & 0x20) != 0;
654 main_btn2_state = (btn & 0x80) != 0;
655
656 /* 1•bŒã‚É•\Ž¦‚ðXV */
657 main_adv_timer = 1000;
658}
659
660/*
661 * ƒ^ƒCƒ}[Žæ“¾
662 */
663static TMO main_get_timer()
664{
665 TMO result = main_timer;
666
667 if((result == TMO_FEVR)
668 || ((main_led_timer != TMO_FEVR) && (main_led_timer < result))){
669 result = main_led_timer;
670 }
671
672 if ((result == TMO_FEVR)
673 || ((main_adv_timer != TMO_FEVR) && (main_adv_timer < result))) {
674 result = main_adv_timer;
675 }
676
677 return result;
678}
679
680/*
681 * ŽžŠÔŒo‰ß
682 */
683static void main_progress(TMO interval)
684{
685 if(main_timer != TMO_FEVR){
686 main_timer -= interval;
687 if(main_timer < 0){
688 main_timer = 0;
689 }
690 }
691
692 if(main_led_timer != TMO_FEVR){
693 main_led_timer -= interval;
694 if(main_led_timer < 0){
695 main_led_timer = 0;
696 }
697 }
698
699 if (main_adv_timer != TMO_FEVR) {
700 main_adv_timer -= interval;
701 if (main_adv_timer < 0) {
702 main_adv_timer = 0;
703 }
704 }
705}
706
707/*
708 * Echonet“d•¶ŽóMˆ—
709 */
710static void main_recv_esv(T_EDATA *esv)
711{
712 ER ret;
713 uint8_t epc;
714 uint8_t pdc;
715 uint8_t p_edt[256];
716 T_ENUM_EPC enm;
717
718 ret = ecn_itr_ini(&enm, esv);
719 if(ret != E_OK){
720 syslog(LOG_ERROR, "ecn_itr_ini");
721 return;
722 }
723
724 for(;;) {
725 while((ret = ecn_itr_nxt(&enm, &epc, &pdc, p_edt)) == E_OK) {
726 switch (epc) {
727 case 0x80:
728 break;
729 case 0x81:
730 break;
731 }
732 }
733 if(ret != E_BOVR){
734 syslog(LOG_ERROR, "ecn_itr_nxt");
735 break;
736 }
737 if(enm.is_eof)
738 break;
739 }
740}
741
742/*
743 * ‰ž“š“d•¶‘Ò‚¿‚ÌŠ„‚荞‚ݏˆ—
744 */
745static void main_break_wait(uint8_t *brkdat, int32_t len)
746{
747 char ip_addr[16], netmask[16], gateway[16];
748
749 switch(main_state){
750 case main_state_idle:
751 if (len == 0)
752 break;
753
754 switch (brkdat[0]) {
755 case 1:
756 switch (brkdat[0]) {
757 case 1:
758 ipaddr_ntoa_r(&lwiptsk.netif.ip_addr, ip_addr, sizeof(ip_addr));
759 ipaddr_ntoa_r(&lwiptsk.netif.netmask, netmask, sizeof(netmask));
760 ipaddr_ntoa_r(&lwiptsk.netif.gw, gateway, sizeof(gateway));
761
762 syslog(LOG_NOTICE, "ip_addr %s, netmask %s, gateway %s", ip_addr, netmask, gateway);
763 tslp_tsk(100); /* ƒƒO‚ªo—Í‚³‚ê‚é‚Ü‚Å‚¿‚å‚Á‚Æ‘Ò‚Â */
764 break;
765 case 2:
766 syslog(LOG_NOTICE, "netif down");
767 break;
768 }
769 break;
770 }
771 break;
772 }
773}
774
775static void main_ontimer();
776static void main_led_ontimer();
777static void main_adv_ontimer();
778
779/*
780 * ƒ^ƒCƒ€ƒAƒEƒgˆ—
781 */
782static void main_timeout()
783{
784 if(main_timer == 0){
785 main_ontimer();
786 }
787
788 if(main_led_timer == 0){
789 main_led_ontimer();
790 }
791
792 if (main_adv_timer == 0) {
793 main_adv_ontimer();
794 }
795}
796
797#define main_7seg_count 4
798static uint8_t main_7seg[main_7seg_count] = { 0xDA, 0xF2, 0xB6, 0xB6 };
799static int main_7seg_pos = 0;
800const uint8_t led_disp[] = { 0xFC, 0x60, 0xDA, 0xF2, 0x66, 0xB6, 0xBE, 0xE4, 0xFE, 0xF6, 0xEE, 0x3E, 0x1A, 0x7A, 0x9E, 0x8E };
801
802static void main_led_ontimer()
803{
804 /* 7Seg•\Ž¦ */
805 sil_wrb_mem((uint8_t *)PORTD_PODR_ADDR, ~main_7seg[main_7seg_pos]);
806
807 if (main_7seg_pos == 0)
808 sil_wrb_mem((uint8_t *)PORT2_PODR_ADDR, sil_reb_mem((uint8_t *)PORT2_PODR_ADDR)
809 & ~PORT_PODR_B4_BIT);
810 else
811 sil_wrb_mem((uint8_t *)PORT2_PODR_ADDR, sil_reb_mem((uint8_t *)PORT2_PODR_ADDR)
812 | PORT_PODR_B4_BIT);
813 if (main_7seg_pos == 1)
814 sil_wrb_mem((uint8_t *)PORT2_PODR_ADDR, sil_reb_mem((uint8_t *)PORT2_PODR_ADDR)
815 & ~PORT_PODR_B3_BIT);
816 else
817 sil_wrb_mem((uint8_t *)PORT2_PODR_ADDR, sil_reb_mem((uint8_t *)PORT2_PODR_ADDR)
818 | PORT_PODR_B3_BIT);
819 if (main_7seg_pos == 2)
820 sil_wrb_mem((uint8_t *)PORT5_PODR_ADDR, sil_reb_mem((uint8_t *)PORT5_PODR_ADDR)
821 & ~PORT_PODR_B3_BIT);
822 else
823 sil_wrb_mem((uint8_t *)PORT5_PODR_ADDR, sil_reb_mem((uint8_t *)PORT5_PODR_ADDR)
824 | PORT_PODR_B3_BIT);
825 if (main_7seg_pos == 3)
826 sil_wrb_mem((uint8_t *)PORTC_PODR_ADDR, sil_reb_mem((uint8_t *)PORTC_PODR_ADDR)
827 & ~PORT_PODR_B1_BIT);
828 else
829 sil_wrb_mem((uint8_t *)PORTC_PODR_ADDR, sil_reb_mem((uint8_t *)PORTC_PODR_ADDR)
830 | PORT_PODR_B1_BIT);
831
832 main_7seg_pos = (main_7seg_pos + 1) & 0x3;
833
834 /* 3msŒã‚É7SEG•\Ž¦‚ðXV */
835 main_led_timer = 3;
836}
837
838#define main_ad_count 8
839#define main_ad_table_count 64
840static uint16_t main_ad_table[main_ad_count][main_ad_table_count];
841static int main_ad_pos = 0;
842static uint32_t main_ad_avelage[main_ad_count];
843static uint64_t main_ad_integral[main_ad_count];
844volatile uint16_t *const main_ad_addr[main_ad_count] = {
845 (uint16_t *)S12AD_ADDR0_ADDR,
846 (uint16_t *)S12AD_ADDR1_ADDR,
847 (uint16_t *)S12AD_ADDR2_ADDR,
848 (uint16_t *)S12AD_ADDR3_ADDR,
849 (uint16_t *)S12AD_ADDR4_ADDR,
850 (uint16_t *)S12AD_ADDR5_ADDR,
851 (uint16_t *)S12AD_ADDR6_ADDR,
852 (uint16_t *)S12AD_ADDR7_ADDR,
853};
854
855uint8_t main_port_no = 1;
856static void main_7seg_view(int port_no);
857static void main_btn1_change(bool_t push);
858static void main_btn2_change(bool_t push);
859
860/*
861 * ƒ^ƒCƒ€ƒAƒEƒgˆ—
862 */
863static void main_ontimer()
864{
865 uint8_t btn;
866 uint16_t ad_value;
867
868 switch(main_state){
869 case main_state_idle:
870 /* 10msŒã‚Ƀ{ƒ^ƒ“ó‘Ô‚ðŠm”F */
871 main_timer = 10;
872
873 /* ADC‚Ì•ÏŠ·Œ‹‰ÊŽæ“¾ */
874 if((sil_reb_mem((uint8_t *)S12AD_ADCSR_ADDR) & S12AD_ADCSR_ADST_BIT) == 0){
875 for (int i = 0; i < main_ad_count; i++) {
876 main_ad_avelage[i] -= main_ad_table[i][main_ad_pos];
877 ad_value = sil_reh_mem(main_ad_addr[i]); // 12bit’lŽæ“¾
878 main_ad_integral[i] += 5/*[V]*/ * ad_value/*[A]*/; // 12bitÏŽZ’l
879 ad_value = (ad_value << 4) | (ad_value >> 12); // 16bitŠg’£
880 main_ad_table[i][main_ad_pos] = ad_value;
881 main_ad_avelage[i] += ad_value;
882 }
883
884 main_ad_pos++;
885 if (main_ad_pos >= main_ad_table_count) {
886 main_ad_pos = 0;
887 }
888
889 /* •ÏŠ·ŠJŽniƒVƒ“ƒOƒ‹ƒXƒLƒƒƒ“ƒ‚[ƒhj */
890 sil_wrb_mem((uint8_t *)S12AD_ADCSR_ADDR, S12AD_ADCSR_ADST_BIT);
891 }
892
893 main_whm_counter += 10;
894 if (main_whm_counter >= 1250/* 180000=30•ª */) {
895 main_whm_counter = 0;
896
897 for (int i = 0; i < 6; i++) {
898 uint64_t carry;
899 carry = whm_add_integral_value(&electric_energy_meter_data[i],
900 main_ad_integral[i] / ((uint64_t)(10LL/* 0.01kWh */ * 360000LL/*=hour*/ * ((1 << 12) - 1) / 2.4/*4095=2.4A*/)));
901
902 main_ad_integral[i] -= carry * ((uint64_t)(10LL/* 0.01kWh */ * 360000LL/*=hour*/ * ((1 << 12) - 1) / 2.4/*4095=2.4A*/));
903 }
904
905 if (electric_energy_meter_data[0].current_pos == 0) {
906 act_tsk(CLIENT_TASK);
907 }
908 }
909
910 /* ƒ{ƒ^ƒ“ó‘ԓǂݍž‚Ý */
911 btn = sil_reb_mem((uint8_t *)PORT0_PIDR_ADDR);
912
913 /* ƒ{ƒ^ƒ“‚P‚̏ˆ— */
914 if(((btn & 0x20) != 0) && !main_btn1_state){
915 main_btn1_count++;
916 if(main_btn1_count > 10){
917 main_btn1_count = 0;
918 main_btn1_state = true;
919
920 main_btn1_change(true);
921 }
922 }
923 else if(((btn & 0x20) == 0) && main_btn1_state){
924 main_btn1_count++;
925 if(main_btn1_count > 10){
926 main_btn1_count = 0;
927 main_btn1_state = false;
928
929 main_btn1_change(false);
930 }
931 }
932
933 /* ƒ{ƒ^ƒ“‚Q‚̏ˆ— */
934 if(((btn & 0x80) != 0) && !main_btn2_state){
935 main_btn2_count++;
936 if(main_btn2_count > 10){
937 main_btn2_count = 0;
938 main_btn2_state = true;
939
940 main_btn2_change(true);
941 }
942 }
943 else if(((btn & 0x80) == 0) && main_btn2_state){
944 main_btn2_count++;
945 if(main_btn2_count > 10){
946 main_btn2_count = 0;
947 main_btn2_state = false;
948
949 main_btn2_change(false);
950 }
951 }
952
953 main_7seg_view(main_port_no);
954
955 break;
956 }
957}
958
959bool_t main_rev = false;
960
961/*
962 * ƒ{ƒ^ƒ“‚Pó‘ԕω»ˆ—
963 */
964static void main_btn1_change(bool_t push)
965{
966 /* ‰Ÿ‚³‚ê‚Ä–ß‚Á‚½Žž‚ɏˆ—‚·‚é */
967 if(push)
968 return;
969
970 /* •\Ž¦ƒ|[ƒg”ԍ† */
971 if(!main_rev){
972 main_port_no--;
973 if(main_port_no < 1){
974 main_port_no = 1;
975 main_rev = true;
976 main_adv_timer = 2000;
977 }
978 }
979 else{
980 main_port_no++;
981 if(main_port_no > main_ad_count){
982 main_port_no = main_ad_count;
983 main_rev = false;
984 main_adv_timer = 2000;
985 }
986 }
987}
988
989/*
990 * ƒ{ƒ^ƒ“‚Qó‘ԕω»ˆ—
991 */
992static void main_btn2_change(bool_t push)
993{
994 /* ‰Ÿ‚³‚ê‚Ä–ß‚Á‚½Žž‚ɏˆ—‚·‚é */
995 if(push)
996 return;
997
998 int swt = sil_reb_mem((uint8_t *)PORTE_PIDR_ADDR);
999 if ((swt & 0x20) == 0) {
1000 return;
1001 }
1002
1003 /* •\Ž¦ƒ|[ƒg”ԍ† */
1004 if(!main_rev){
1005 main_port_no++;
1006 if(main_port_no > main_ad_count){
1007 main_port_no = main_ad_count;
1008 main_rev = false;
1009 main_adv_timer = 2000;
1010 }
1011 }
1012 else{
1013 main_port_no--;
1014 if(main_port_no < 1){
1015 main_port_no = 1;
1016 main_rev = true;
1017 main_adv_timer = 2000;
1018 }
1019 }
1020}
1021
1022static void main_adv_ontimer()
1023{
1024 int swt = sil_reb_mem((uint8_t *)PORTE_PIDR_ADDR);
1025 if (swt & 0x20)
1026 return;
1027
1028 main_port_no++;
1029 if(main_port_no > main_ad_count)
1030 main_port_no = 1;
1031
1032 main_adv_timer = 2000;
1033}
1034
1035static void main_7seg_view(int port_no)
1036{
1037 if ((port_no < 1) || (port_no > main_ad_count))
1038 return;
1039
1040 int swt = sil_reb_mem((uint8_t *)PORTE_PIDR_ADDR);
1041
1042 port_no--;
1043
1044 uint32_t adv = (main_ad_avelage[port_no] / main_ad_table_count);
1045
1046 if (swt & 0x10) {
1047 main_7seg[0] = led_disp[port_no + 1] | 1;
1048 main_7seg[1] = led_disp[(adv & 0xF000) >> 12];
1049 main_7seg[2] = led_disp[(adv & 0x0F00) >> 8];
1050 main_7seg[3] = led_disp[(adv & 0x00F0) >> 4];
1051 }
1052 else {
1053 uint32_t tmp = adv / 26214;
1054
1055 main_7seg[0] = led_disp[port_no + 1] | 1;
1056 main_7seg[1] = led_disp[tmp] | 1;
1057 adv = 10 * (adv - tmp * 26214);
1058 tmp = adv / 26214;
1059 main_7seg[2] = led_disp[tmp];
1060 adv = 10 * (adv - tmp * 26214);
1061 tmp = adv / 26214;
1062 main_7seg[3] = led_disp[tmp];
1063 }
1064}
1065
1066#ifdef __RX
1067void abort()
1068{
1069 TOPPERS_assert_abort();
1070}
1071#endif
Note: See TracBrowser for help on using the repository browser.