Refactor to use new protocol module and sipo.
[bertos.git] / boards / triface / examples / triface / main.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2003, 2004, 2006, 2008, 2010, 2012 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2000 Bernie Innocenti <bernie@codewiz.org>
31  *
32  * -->
33  *
34  * \author Marco Benelli <marco@develer.com>
35  * \author Bernie Innocenti <bernie@codewiz.org>
36  * \author Daniele Basile <asterix@develer.com>
37  *
38  * \brief Triface application.
39  *
40  * This application manage some devices, like buzzer, digital in/out or analogic input.
41  * To controll all devices the application provide the interactive shell
42  * that allow the user to send commands to the board. All commands are generate
43  * throuth the macro, that convert its parameters, at compile time, to C function.
44  * In this way is more simple to implement the new command. Other that, the application,
45  * automaticly, parse this new command from the shell and call the our function.
46  * Actually this application runs on custom hardware, that you can see the scheme on bertos
47  * site, this hardware is able to read the tags string that come from rfid reader and
48  * print this on another serial port. The main scope of this function is to create
49  * the automatic opendoor, reading the tag key that will pass on the rfid reader.
50  */
51
52 #include "cmd.h"
53
54 #include "hw/hw_input.h"
55
56 #include <cfg/macros.h>
57
58 #include <drv/timer.h>
59 #include <drv/buzzer.h>
60 #include <drv/ser.h>
61 #include <drv/adc.h>
62 #include <drv/wdt_avr.h>
63
64 #include <mware/parser.h>
65
66 #include <net/keytag.h>
67 #include <net/protocol.h>
68
69 static Serial fd_ser;
70 static Serial tag_ser;
71 static Sipo fd_sipo;
72
73 static void init(void)
74 {
75         IRQ_ENABLE;
76         kdbg_init();
77         timer_init();
78         adc_init();
79         buz_init();
80         MCUSR = 0;
81         wdt_disable();
82
83         INPUT_INIT();
84 }
85
86 int main(void)
87 {
88         init();
89
90         /* Initialize Tag serial port and data structure */
91         TagPacket pkt;
92
93         /* SPI Port Initialization */
94     sipo_init(&fd_sipo, TRIFACE_DOUT, SIPO_DATAORDER_LSB | SIPO_START_LOW | SIPO_LOW_TO_HIGH);
95
96         /* Open the main communication port */
97         ser_init(&fd_ser, SER_UART1);
98         ser_setbaudrate(&fd_ser, 115200);
99
100         /* Tag reader serial port */
101         ser_init(&tag_ser, SER_UART0);
102         ser_setbaudrate(&tag_ser, 9600);
103
104         /* Init the tag parser */
105         keytag_init(&pkt, &fd_ser.fd, &tag_ser.fd);
106
107         protocol_init(&fd_ser.fd, cmd_register);
108
109         while (1)
110         {
111                 protocol_run(&fd_ser.fd);
112
113                 uint8_t buf[CONFIG_TAG_MAX_LEN];
114                 int len;
115                 /* Read the new tag, if this is avaible */
116                 if ((len = keytag_recv(&pkt, buf, sizeof(buf))) != EOF)
117                         kfile_write(&fd_ser.fd, buf, len);
118         }
119
120         return 0;
121 }
122