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

Last change on this file since 50 was 50, checked in by sven, 10 years ago
  • The PC Documation M200 uC Serial Communication Protocol: Initial write
  • driver/documation-m200: Implemented the protocol (code compiles, but not tested yet on device)

-- Sven @ p3

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