1 | /** |
---|
2 | * AVR M200 Punch card reader controller |
---|
3 | * Main file |
---|
4 | * |
---|
5 | * This file contains the main() routine, initializes all systems (stdio, |
---|
6 | * UART, ports, etc.) and calls the main loop in protocol.c |
---|
7 | * |
---|
8 | * - protocol.{h, c} implements the communication to the PC |
---|
9 | * - punchcard.{h, c} implements the card model |
---|
10 | * - driver.{h, c} implements the real device driver (ISR routines and ring buffer) |
---|
11 | * |
---|
12 | * The complete program is very specific to the wiring of the microcontroller, |
---|
13 | * as defined in wiring.h. It was developed for an AVR ATmega 644 with a 8Mhz |
---|
14 | * clock. |
---|
15 | * |
---|
16 | * The uC is set on an Olimex AVR-P40 board, having the TX pad connected to |
---|
17 | * PD1 (pin15) and RX pad with PD0 (pin14). |
---|
18 | * |
---|
19 | * It was compiled with AVRStudio + WinAVR (gcc 4, avr-libc required). |
---|
20 | * |
---|
21 | * This file is part of the Punched Paper Project - Punch Card Device Drivers |
---|
22 | * Copyright (C) 2009 Sven Koeppel |
---|
23 | * |
---|
24 | * This program is free software; you can redistribute it and/or |
---|
25 | * modify it under the terms of the GNU General Public License as |
---|
26 | * published by the Free Software Foundation; either version 3 of |
---|
27 | * the License, or (at your option) any later version. |
---|
28 | * |
---|
29 | * This program is distributed in the hope that it will be useful, |
---|
30 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
31 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
32 | * GNU General Public License for more details. |
---|
33 | * |
---|
34 | * You should have received a copy of the GNU General Public License |
---|
35 | * along with this program; if not, see |
---|
36 | * <http://www.gnu.org/licenses/>. |
---|
37 | **/ |
---|
38 | |
---|
39 | #define __AVR_ATmega644__ 1 |
---|
40 | #define OSCSPEED 8000000 /* in Hz */ |
---|
41 | |
---|
42 | #include <string.h> |
---|
43 | #include <stdio.h> // testweise |
---|
44 | |
---|
45 | #include "avr/io.h" |
---|
46 | #include "avr/interrupt.h" |
---|
47 | |
---|
48 | #include "punchcard.h" |
---|
49 | #include "wiring.h" |
---|
50 | #include "protocol.h" |
---|
51 | #include "driver.h" |
---|
52 | |
---|
53 | |
---|
54 | static FILE main_thread = FDEV_SETUP_STREAM(uart_transmit_main, NULL, _FDEV_SETUP_WRITE); |
---|
55 | static FILE interrupt_thread = FDEV_SETUP_STREAM(uart_transmit, NULL, _FDEV_SETUP_WRITE); |
---|
56 | |
---|
57 | /** |
---|
58 | ** UART, I/O, LED |
---|
59 | **/ |
---|
60 | |
---|
61 | |
---|
62 | void Init(void) |
---|
63 | { |
---|
64 | // Datenports einrichten |
---|
65 | |
---|
66 | PORTA = 0x00; // erste 8 Daten-Input |
---|
67 | PORTB = 0x00; // LED (auslassen, an=0x01), 3 Fehler (IN), Button (IN), ICSP |
---|
68 | PORTC = 0x00; // 4 Daten, 3 Status (IN), 1 Signal (OUT) |
---|
69 | PORTD = 0x00; // UART und -Flusssteuerung |
---|
70 | |
---|
71 | DDRA = 0x00; // 8 Daten: Input |
---|
72 | DDRB = (1 << DDB0); // Nur LED als Output, alles andere Input |
---|
73 | DDRC = (1 << DDC6); // Nur Pick Command als Output, alles andere Input |
---|
74 | DDRD = 0x00; // UART halt, IM jetzt auch |
---|
75 | |
---|
76 | // Pin Change Intterupts aufsetzen |
---|
77 | // PCMSK*: Pin Change Masks (welche Pins tragen zum Intterupt bei) |
---|
78 | PCMSK1 = (1 << PCINT_ERROR) | (1 << PCINT_HCK) | (1 << PCINT_MOCK); // PORT B |
---|
79 | PCMSK2 = (1 << PCINT_BSY) | (1 << PCINT_RDY); // PORT C |
---|
80 | // Pin Change Interrupt Control Register: PC Interrupt Enable for Port B & C |
---|
81 | PCICR = (1 << PCIE1) | (1 << PCIE2); |
---|
82 | |
---|
83 | // Ausgezeichnete Interrupts aufsetzen (Index Mark IM haengt an INT0) |
---|
84 | EICRA = (1 << ISC01) | (1 << ISC00); // rising edge von INT0 erzeugt intterupt |
---|
85 | EIMSK = (1 << INT0); // interrupt fuer INT0 anschalten |
---|
86 | |
---|
87 | // interrupt enable |
---|
88 | sei(); |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | void UartInit(uint32_t Baud) |
---|
93 | { |
---|
94 | int BaudRate = OSCSPEED / (16 * Baud) - 1; |
---|
95 | |
---|
96 | UBRR0H = (unsigned char) BaudRate>>8; |
---|
97 | UBRR0L = (unsigned char) BaudRate; |
---|
98 | //set BaudRate |
---|
99 | |
---|
100 | UCSR0B = UCSR0B | (0b00011000); |
---|
101 | // RXEN & TXEN enable (Bits 4, 3 = 1) |
---|
102 | |
---|
103 | UCSR0C = (UCSR0C | (0b10000110)); |
---|
104 | // USART Register Select (Bit 7 = 1) |
---|
105 | // 8 data bits per frame (Bit 2, 1 = 1) |
---|
106 | |
---|
107 | UCSR0C = UCSR0C & 0b11110111; |
---|
108 | // 1 Stop bit (Bit 3 = 0) |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | |
---|
113 | |
---|
114 | int main() |
---|
115 | { |
---|
116 | card_buffer_flush(); |
---|
117 | Init(); |
---|
118 | UartInit(38400); |
---|
119 | start_led(); |
---|
120 | |
---|
121 | // nette sachen machen koennen |
---|
122 | stdout = &main_thread; |
---|
123 | stderr = &interrupt_thread; |
---|
124 | |
---|
125 | // default-Werte |
---|
126 | status.out_format = M200_CLIENT_DEBUG; |
---|
127 | status.debug_output = FALSE; |
---|
128 | status.main_loop_is_printing = FALSE; |
---|
129 | status.concurrent_printing_buffer_pos = 0; |
---|
130 | |
---|
131 | |
---|
132 | user_input_loop(); |
---|
133 | return 0; |
---|
134 | } |
---|