basemon.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * Copyright (C) 2001-2008 by egnite Software GmbH
  3. * Copyright (C) 2009 by egnite GmbH
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. */
  35. /*
  36. * $Id: basemon.c 4116 2012-04-12 22:35:23Z olereinhardt $
  37. *
  38. * WARNING! Do not use any part of Basemon for your own applications. WARNING!
  39. *
  40. * This is not a typical application sample. It overrides parts of Nut/OS to
  41. * keep it running on broken hardware.
  42. */
  43. /*!
  44. * \example basemon/basemon.c
  45. *
  46. * Basic hardware test monitor for AVR targets.
  47. */
  48. #include <stdio.h>
  49. #include <string.h>
  50. #include "rtlregs.h"
  51. #include <dev/debug.h>
  52. #include <sys/version.h>
  53. #include <sys/heap.h>
  54. #include <sys/thread.h>
  55. #include <sys/timer.h>
  56. #include <sys/confnet.h>
  57. #include <sys/confos.h>
  58. #include <sys/socket.h>
  59. #include <arpa/inet.h>
  60. #include <net/if_var.h>
  61. #include "realtek.h"
  62. #include "smsc.h"
  63. #include "webdemo.h"
  64. #include "xmemtest.h"
  65. #include "dataflash.h"
  66. #include "uart.h"
  67. #include "utils.h"
  68. #if defined(__GNUC__) && defined(__AVR__)
  69. void NutInit(void) __attribute__ ((naked)) __attribute__ ((section(".init8")));
  70. extern int NutAppMain(void) __attribute__ ((noreturn));
  71. #endif
  72. int uart_bs;
  73. uint8_t nic;
  74. static char *version = "4.4.0";
  75. static size_t sram;
  76. static uint8_t banks;
  77. static size_t banksize;
  78. static long srom;
  79. char my_ip[32];
  80. char my_mask[32];
  81. char my_gate[32];
  82. uint8_t my_mac[32];
  83. uint8_t eg_mac[] = { 0x00, 0x06, 0x98, 0x00, 0x00, 0x00 };
  84. volatile uint8_t ms62_5;
  85. THREAD(idle, arg)
  86. {
  87. NutTimerInit();
  88. NutThreadCreate("main", WebDemo, 0, 768);
  89. NutThreadSetPriority(254);
  90. for (;;) {
  91. #ifdef HEARTBEAT_BIT
  92. HeartBeat();
  93. #endif
  94. NutThreadYield();
  95. }
  96. }
  97. /*
  98. * Test ports.
  99. */
  100. int TestPorts(void)
  101. {
  102. #if defined(__AVR__)
  103. uint8_t pat;
  104. uint8_t ipat;
  105. uint8_t val;
  106. uint8_t bpat = 0;
  107. uint8_t dpat = 0;
  108. uint8_t epat = 0;
  109. uint8_t fpat = 0;
  110. /*
  111. * Port B.
  112. */
  113. for (pat = 1; pat; pat <<= 1) {
  114. ipat = ~pat;
  115. /* Keep dataflash select low. */
  116. ipat &= 0xEF;
  117. outb(DDRB, pat | 0x10);
  118. outb(PORTB, ipat);
  119. Delay(100);
  120. val = inb(PINB);
  121. if (val != ipat) {
  122. bpat |= (ipat ^ val);
  123. }
  124. }
  125. outb(DDRB, 0);
  126. if (bpat)
  127. printf("PORTB bits 0x%02X\n\x07", bpat);
  128. #ifndef INTECH21
  129. /*
  130. * Port D.
  131. */
  132. for (pat = 1; pat; pat <<= 1) {
  133. #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega2561__)
  134. /* Exclude PD5 used for RS485 direction select. */
  135. if (pat & 0x20) {
  136. continue;
  137. }
  138. #endif
  139. ipat = ~pat;
  140. outb(DDRD, pat);
  141. outb(PORTD, ipat);
  142. Delay(1000);
  143. if ((val = inb(PIND) | 0x20) != ipat) {
  144. printf("[%02X-%02X]", ipat, val);
  145. dpat |= pat;
  146. }
  147. }
  148. outb(DDRD, 0);
  149. if (dpat)
  150. printf("PORTD bits 0x%02X\n\x07", dpat);
  151. /*
  152. * Port E. Exclude PE0, PE1 and PE5.
  153. */
  154. for (pat = 4; pat; pat <<= 1) {
  155. if (pat == 0x20)
  156. continue;
  157. ipat = ~pat;
  158. outb(DDRE, pat);
  159. outb(PORTE, ipat);
  160. Delay(1000);
  161. if ((val = inb(PINE) | 0x23) != ipat) {
  162. epat |= pat;
  163. }
  164. }
  165. outb(DDRE, 0);
  166. if (epat)
  167. printf("PORTE bits 0x%02X\n\x07", epat);
  168. #ifdef __AVR_ENHANCED__
  169. /*
  170. * Port F. Exclude PF4, PF5, PF6 and PF7.
  171. */
  172. for (pat = 1; pat & 0x0F; pat <<= 1) {
  173. ipat = ~pat;
  174. outb(DDRF, pat);
  175. outb(PORTF, ipat);
  176. Delay(1000);
  177. if ((val = inb(PINF) | 0xF0) != ipat) {
  178. fpat |= pat;
  179. }
  180. }
  181. outb(DDRF, 0);
  182. if (fpat)
  183. printf("PORTF bits 0x%02X\n\x07", fpat);
  184. #endif
  185. #endif /* INTECH21 */
  186. if (bpat || dpat || epat || fpat)
  187. return -1;
  188. #endif /* __AVR__ */
  189. return 0;
  190. }
  191. /*
  192. * Basic monitor function.
  193. */
  194. void BaseMon(void)
  195. {
  196. char ch;
  197. int i;
  198. int n;
  199. char hostname[sizeof(confos.hostname)];
  200. static prog_char menu1_P[] = "\nPress any of the following keys:";
  201. static prog_char menu2_P[] = " B - Send broadcasts";
  202. static prog_char menu3_P[] = " E - Ethernet controller read/write\n"
  203. " J - Jump to bootloader\n"
  204. " S - SRAM read/write";
  205. static prog_char menu4_P[] = " X - Exit BaseMon, configure network and start WebServer";
  206. /*
  207. * Print banner.
  208. */
  209. printf("\n\nBaseMon %s\nNut/OS %s\n", version, NutVersionString());
  210. #ifdef INTECH21
  211. puts("Customized for Intech21");
  212. #endif
  213. printf("Compiled by ");
  214. #ifdef __IMAGECRAFT__
  215. printf("ICCAVR");
  216. #else
  217. printf("AVRGCC");
  218. #endif
  219. printf(" for ");
  220. #if defined(__AVR_ATmega128__)
  221. puts("ATmega128");
  222. #elif defined(__AVR_ATmega103__)
  223. puts("ATmega103");
  224. #elif defined(__AVR_AT90CAN128__)
  225. puts("AT90CAN128");
  226. #elif defined(__AVR_ATmega2561__)
  227. puts("ATmega2561");
  228. #else
  229. puts("unknown");
  230. #endif
  231. if (uart_bs >= 0) {
  232. printf("Baudrate select = %d\n", uart_bs);
  233. }
  234. else {
  235. puts("No user input detected");
  236. }
  237. /*
  238. * Test external SRAM.
  239. */
  240. printf("External RAM Test... ");
  241. sram = XMemTest();
  242. printf("%u bytes\n", (unsigned int)sram);
  243. printf("Banked RAM Test... ");
  244. banksize = sram;
  245. banks = XMemBankTest(&banksize);
  246. if (banks)
  247. printf("%u banks, %u bytes ea.\n", banks, (unsigned int)banksize);
  248. else
  249. puts("none");
  250. /*
  251. * Test external Flash.
  252. */
  253. printf("Serial FLASH... ");
  254. srom = SpiMemTest();
  255. printf("%lu bytes\n", srom);
  256. /*
  257. * Test Ethernet controller hardware.
  258. */
  259. printf("Detecting NIC... ");
  260. if (SmscDetect()) {
  261. if (RealtekDetect()) {
  262. nic = 0;
  263. puts("none\x07");
  264. } else {
  265. nic = 1;
  266. puts("RTL8019AS");
  267. }
  268. } else {
  269. nic = 2;
  270. puts("LAN91C111");
  271. }
  272. if (nic) {
  273. printf("Testing NIC... ");
  274. if (nic == 1) {
  275. RealtekTest();
  276. } else {
  277. SmscTest();
  278. }
  279. }
  280. /*
  281. * Test I/O ports.
  282. */
  283. #if defined(ETHERNUT1) || defined(ETHERNUT2)
  284. printf("I/O Port Test... ");
  285. if (TestPorts() == 0)
  286. puts("OK");
  287. #endif
  288. /*
  289. * Return if running without serial port.
  290. */
  291. if (uart_bs < 0)
  292. return;
  293. for (;;) {
  294. for (;;) {
  295. while (GetChar());
  296. puts_P(menu1_P);
  297. if (sram)
  298. puts_P(menu2_P);
  299. puts_P(menu3_P);
  300. if (sram > 8191 && nic)
  301. puts_P(menu4_P);
  302. while ((ch = GetChar()) == 0 || ch == ' ');
  303. if (sram > 8191 && (ch == 'x' || ch == 'X'))
  304. break;
  305. if (sram && (ch == 'b' || ch == 'B')) {
  306. if (nic == 1)
  307. RealtekSend();
  308. else
  309. SmscSend();
  310. } else if (ch == 'e' || ch == 'E') {
  311. if (nic == 1) {
  312. RealtekLoop();
  313. } else {
  314. SmscLoop();
  315. }
  316. } else if (ch == 'j' || ch == 'J') {
  317. puts("Booting...");
  318. #if defined(__AVR__)
  319. asm("jmp 0x1F000");
  320. #endif
  321. } else if (ch == 's' || ch == 'S')
  322. LoopSRAM();
  323. }
  324. /*
  325. * Input MAC address.
  326. */
  327. for (;;) {
  328. printf("\nMAC address (%02X%02X%02X%02X%02X%02X): ", my_mac[0], my_mac[1], my_mac[2], my_mac[3], my_mac[4],
  329. my_mac[5]);
  330. GetLine(inbuff, sizeof(inbuff));
  331. if ((n = strlen(inbuff)) == 0)
  332. break;
  333. for (i = 0; i < n; i++)
  334. if (inbuff[i] < '0' ||
  335. (inbuff[i] > '9' && inbuff[i] < 'A') || (inbuff[i] > 'F' && inbuff[i] < 'a') || inbuff[i] > 'f')
  336. n = 13;
  337. if (n <= 12 && (n & 1) == 0) {
  338. n >>= 1;
  339. for (i = 0; i < n; i++)
  340. my_mac[6 - n + i] = hex2bin(inbuff[i * 2]) * 16 + hex2bin(inbuff[i * 2 + 1]);
  341. break;
  342. }
  343. printf("Bad MAC address");
  344. }
  345. /*
  346. * Input host name.
  347. */
  348. printf("Host name (%s): ", confos.hostname);
  349. GetLine(hostname, sizeof(confos.hostname) - 1);
  350. /*
  351. * Input IP address.
  352. */
  353. GetIP("IP address", my_ip);
  354. /*
  355. * Input netmask and gateway, if non-zero IP address.
  356. */
  357. if (inet_addr(my_ip)) {
  358. GetIP("Net mask", my_mask);
  359. GetIP("Default route", my_gate);
  360. if (inet_addr(my_gate) == 0 || (inet_addr(my_ip) & inet_addr(my_mask)) == (inet_addr(my_gate) & inet_addr(my_mask)))
  361. break;
  362. } else {
  363. printf("Using DHCP or ARP method (Y): ");
  364. GetLine(inbuff, sizeof(inbuff));
  365. if (strlen(inbuff) == 0 || inbuff[0] == 'Y' || inbuff[0] == 'y')
  366. break;
  367. }
  368. }
  369. puts("\n");
  370. confnet.cdn_cip_addr = inet_addr(my_ip);
  371. confnet.cdn_ip_mask = inet_addr(my_mask);
  372. confnet.cdn_gateway = inet_addr(my_gate);
  373. memcpy(confnet.cdn_mac, my_mac, sizeof(confnet.cdn_mac));
  374. #ifdef HEARTBEAT_BIT
  375. HeartBeat();
  376. #endif
  377. NutNetSaveConfig();
  378. if (strlen(hostname) > 0) {
  379. strncpy(confos.hostname, hostname, sizeof(confos.hostname) - 1);
  380. confos.hostname[sizeof(confos.hostname) - 1] = 0;
  381. }
  382. #ifdef HEARTBEAT_BIT
  383. HeartBeat();
  384. #endif
  385. NutSaveConfig();
  386. }
  387. void NutInit(void)
  388. {
  389. extern void *__bss_end;
  390. /*
  391. * Make sure that stack pointer points into internal RAM.
  392. */
  393. #if defined(__AVR__)
  394. outb(SPH, inb(SPH) & (RAMEND >> 8));
  395. #endif
  396. /*
  397. * Use the rest of our internal RAM for our heap. Re-opening
  398. * standard output will use malloc. We do not use any external
  399. * RAM before passing the memory test.
  400. */
  401. NutHeapAdd(&__bss_end, (uintptr_t) RAMEND - 256 - (uintptr_t) (&__bss_end));
  402. /*
  403. * Use the debug UART driver for output. In opposite
  404. * to the standard UART driver, it uses non-buffered
  405. * polling mode, which is better suited to run on
  406. * untested hardware.
  407. */
  408. #if defined(__AVR__)
  409. NutRegisterDevice(&devDebug0, 0, 0);
  410. #endif
  411. uart_bs = DetectSpeed();
  412. freopen("uart0", "w", stdout);
  413. /*
  414. * Load os configuration from EEPROM.
  415. */
  416. if (NutLoadConfig())
  417. strcpy (confos.hostname, "ethernut");
  418. /*
  419. * Load network configuration from EEPROM.
  420. */
  421. NutNetLoadConfig("eth0");
  422. if ((confnet.cdn_mac[0] & confnet.cdn_mac[1] & confnet.cdn_mac[2]) == 0xFF ||
  423. (confnet.cdn_mac[0] | confnet.cdn_mac[1] | confnet.cdn_mac[2]) == 0x00) {
  424. memcpy(confnet.cdn_mac, eg_mac, sizeof(confnet.cdn_mac));
  425. }
  426. strcpy(my_ip, inet_ntoa(confnet.cdn_cip_addr));
  427. strcpy(my_mask, inet_ntoa(confnet.cdn_ip_mask));
  428. strcpy(my_gate, inet_ntoa(confnet.cdn_gateway));
  429. memcpy(my_mac, confnet.cdn_mac, 6);
  430. /*
  431. * Perform basic monitor functions.
  432. */
  433. BaseMon();
  434. /*
  435. * Initialize heap and create the idle thread. This will in turn
  436. * start the WebDemo thread.
  437. */
  438. if (sram) {
  439. NutHeapAdd((void *) (RAMEND + 1), sram);
  440. NutThreadCreate("idle", idle, 0, 384);
  441. }
  442. NutThreadSetPriority(128);
  443. for (;;) {
  444. #ifdef HEARTBEAT_BIT
  445. HeartBeat();
  446. #endif
  447. NutSleep(500);
  448. }
  449. }
  450. #ifdef __IMAGECRAFT__
  451. void main(void)
  452. {
  453. NutInit();
  454. }
  455. #endif