source: projects/punch-card/driver/documation-m200/src/punchcard.h @ 46

Last change on this file since 46 was 46, checked in by sven, 10 years ago

First import of the AVR Documenta M200 Punch Card Reader Controller. This project was started at 09.11.2009. Current status: Working (Beta status). Pending work: Cleaning up, find bug in circular buffer system, implementing a protocol (FTP/Hayes command set like).
Work was done on Windows 2000 with WinAVR and AVR Studio 4 (ISP programming).

File size: 5.2 KB
Line 
1/**
2 * AVR M200 Punch card reader controller
3 *
4 * punchcard.h: Defines a storage layout for punch cards, suitable for
5 *              low memory low level internal processing.
6 *
7 * This file defines:
8 *   * Some primitive type definitions: byte_t, bool_t
9 *   * Some punch card data types:
10 *     - Column stores the content of a column in 16 bit
11 *     - Card   stores the content of a 80 column card, including an
12 *       card offset (for internal processing and status information),
13 *       requiring overall 161 bytes.
14 *     - card_buffer: global static circular buffer for Card structures
15 *       with a static length, read and write offsets.
16 *
17 * There are some debugging output function implementations and the
18 * circular buffer implementation in the punchcard.c file. Everything else
19 * is implemented as macros. That makes card processing very fast.
20 *
21 *
22 * This file is part of the Punched Paper Project - Punch Card Device Drivers
23 * Copyright (C) 2009  Sven Koeppel
24 *
25 * This program is free software; you can redistribute it and/or
26 * modify it under the terms of the GNU General Public License as
27 * published by the Free Software Foundation; either version 3 of
28 * the License, or (at your option) any later version.
29 *
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
34 *
35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, see
37 * <http://www.gnu.org/licenses/>.
38 *
39 **/
40
41#include <string.h> // memset
42#include <avr/pgmspace.h> // zeichentabelle am Ende
43
44// using the avr-libc: Setting up types
45#include <stdint.h> // int8_t, uint16_t
46typedef unsigned char byte_t;
47
48// some <bool.h> definitions:
49typedef unsigned char bool_t;
50#define TRUE  1
51#define FALSE 0
52
53// ================= COLUMN STORAGE MODEL =================
54typedef uint16_t Column;
55/* storage layout: after douglas jones. punch card layout:
56 *
57 *      Top                  Bottom
58 *      _ _ _ _ _ _ _ _ _ _ _ _
59 *     |_|_|_|_|_|_|_|_|_|_|_|_|
60 *     12 11 0 1 2 3 4 5 6 7 8 9
61 *     |     |                 |
62 *     |Zone |     Numeric     |
63 *
64 *
65 * mapped on the bit positions in uint16:
66 * 16 15 14... ..9 8 7 6 5 4 3 2 1 0
67 * |_ _ _ _._ _ _ _|_ _ _ _ _ _ _ _
68 * |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
69 * |       12 11 0 1 2 3 4 5 6 7 8 9
70 * |       |   punch card area     |
71 *
72 * Leaving 4 bits empty.
73 *
74 */
75
76#define  COL0  9
77#define  COL1  8
78#define  COL2  7
79#define  COL3  6
80#define  COL4  5
81#define  COL5  4
82#define  COL6  3
83#define  COL7  2
84#define  COL8  1
85#define  COL9  0
86#define COL11 10
87#define COL12 11
88
89// set a column from input
90//void column_set_from_input(Column* c);
91// wrong place. this header file is about the MODEL, not the hardware wiring...
92
93// print column out (debugging)
94void column_print(Column* c);
95
96
97// ================= CARD STORAGE MODEL =================
98
99// the number of columns on a card
100#define CARD_LEN 80
101
102// magic values for Card.offset
103#define CARD_EMPTY  0  // card is empty
104#define CARD_READY CARD_LEN  // card fully read in, ready for be written out
105#define CARD_DONE  -9  // card already written out, ready to be deleted
106
107// the card structure
108typedef struct punchcard_t {
109        Column columns[CARD_LEN];
110        // the current column writer position
111        int8_t offset;
112} Card;
113
114// call with Card, returns if card is finished
115//#define card_is_finished(c) ( c.offset == CARD_FINISHED )
116//#define card_is_empty(c) ( c.offset == CARD_EMPTY )
117//#define card_set_finished(c) ( c.offset = CARD_FINISHED )
118
119// aufruf mit Pointer auf Karte
120//#define card_finished(c) ( c->column_offset == CARD_LEN )
121
122// ================= GLOBAL CARD BUFFER =================
123
124#define BUFFER_LEN 4
125
126// VOLATILE -- damits immer ausm RAM geladen wird, weil durch ISR veraendert!!!
127// (wichtig bei geteilten variablen)
128volatile struct card_buffer_t {
129        Card cards[BUFFER_LEN];
130        int8_t write_offset;
131        int8_t read_offset;
132} card_buffer;
133
134#define card_buffer_flush()  memset(&card_buffer, 0, sizeof(struct card_buffer_t) )
135
136// erhm:
137// init function not neccessary since avr-libc assures that
138// all global storages are set 0.
139
140// Anzahl belegter Karten zurueckgeben
141#define card_buffer_count() abs(card_buffer.write_offset - card_buffer.read_offset)
142
143// Anzahl freier Karten zurueckgeben
144#define card_buffer_count_free() ( BUFFER_LEN - card_buffer_count() )
145
146// shorthands for cards and columns
147#define current_read_card ( card_buffer.cards[card_buffer.read_offset] )
148#define current_write_card ( card_buffer.cards[card_buffer.write_offset] )
149#define current_write_column ( current_write_card.columns[ current_write_card.offset ] )
150
151// Functions that implement read_offset++ and write_offset++, just
152// better. If buffer is full/empty, they will return these errors:
153
154#define BUFFER_EMPTY  -20
155#define BUFFER_FULL   -30
156#define SUCCESS       0
157
158// card_buffer.read_offset++ just better:
159// @returns 0 if okay, negative value if error
160int8_t card_buffer_read_advance();
161int8_t card_buffer_write_advance();
Note: See TracBrowser for help on using the repository browser.
© 2008 - 2013 technikum29 • Sven Köppel • Some rights reserved
Powered by Trac
Expect where otherwise noted, content on this site is licensed under a Creative Commons 3.0 License