ATmega644 Smart-Card
Loading...
Searching...
No Matches
communication.h
1
11#ifndef COMM_INTERFACE_H
12#define COMM_INTERFACE_H
13
14#ifdef __cplusplus
15extern "C"
16{
17 #include <avr/interrupt.h>
18}
19#endif
20
21#include "defs.h"
22#include "protocol.h"
23
24#ifdef DEBUG
25#include "logger.h"
26#endif
27
31enum class PinDir
32{
33 OUTPUT = 0,
34 INPUT = 1
35};
36
46{
47public:
48 // ******************************************************************************
49 // Public Methods ***************************************************************
50 // ******************************************************************************
57
64
73 void receiveDataToDecrypt(byte_t *data);
74
86 void sendDecryptedData(const byte_t *data);
87
88private:
89 // ******************************************************************************
90 // Private Subclasses ***********************************************************
91 // ******************************************************************************
100 class Timer
101 {
102 public:
103 // Public Methods ***********************************************************
113 static void init(Communication *comm);
114
118 static void stop() { CLR_BIT(TCCR1B, CS10); }
119
123 static void start() { TCNT1 = TIMER_BOTTOM; SET_BIT(TCCR1B, CS10); }
124
129 static void setMatchValue(const uint16_t matchValue) { OCR1A = matchValue; }
130
141 static constexpr uint16_t ETU = 372/1;
142
143 private:
144 // Private Attributes *******************************************************
146 static constexpr uint16_t TIMER_BOTTOM = 0x0000;
147
148 // Private Methods **********************************************************
153 static void serviceRoutine() __asm__("__vector_13") __attribute__((__signal__, __used__, __externally_visible__));
154 };
155
164 class IOPin
165 {
166 public:
167 // Public Methods ***********************************************************
176 static void init(Communication *comm);
177
182 static void setLevel(const bit_t bit);
183
188 static void setDirection(const PinDir direction);
189
194 static void setInterrupt(const bool enabled);
195
196 private:
197 // Private Attributes *******************************************************
199
200 // Private Methods **********************************************************
205 static void serviceRoutine() __asm__("__vector_5") __attribute__((__signal__, __used__, __externally_visible__));
206 };
207
208 // ******************************************************************************
209 // Private Attributes ***********************************************************
210 // ******************************************************************************
211 // Static Constexpr Attributes **************************************************
212 static constexpr bit_t START_BIT = 0;
213 static constexpr bit_t STOP_BIT = 1;
214
215 // Class Objects ****************************************************************
216 friend Timer;
217 friend IOPin;
218 #ifdef DEBUG
220 #endif
221
222 // Volatile Attributes **********************************************************
223 volatile PinDir mDirection = PinDir::OUTPUT;
224 // Output flags/data
225 volatile bool mBitSent = true;
226 volatile bit_t mOutputBit = 0;
227 // Input flags/data
228 volatile bool mByteReceived = 0;
229 volatile uint8_t mInputBitCounter = 0;
230 volatile byte_t mInputByte = 0x00;
231 // Error flags/data
232 volatile bool mCheckErrors = false;
233 volatile bit_t mErrorBit = 0;
234 volatile bool mParityError = false;
235
236 // ******************************************************************************
237 // Private Methods **************************************************************
238 // ******************************************************************************
239 // Send/Receive *****************************************************************
249 void sendBit(const bit_t bit);
250
266 void sendByte(const byte_t byte);
267
273 void sendBytes(const byte_t *bytes, const uint8_t len) { for(uint8_t i=0; i<len; i++) sendByte(bytes[i]); }
274
279 static bit_t sampleBit();
280
290 byte_t receiveByte();
291
296 void receiveProtocolHeader(const byte_t *header);
297
298 // Helper functions *************************************************************
306 static bit_t getParity(byte_t byte);
307};
308
309#endif // COMM_INTERFACE_H
Class that provides functionality for the ATmega644's PinB6, such as an ISR.
Definition communication.h:165
static void setLevel(const bit_t bit)
Set logical level of the Pin.
static Communication * mComm
Communication object to access the class methods & attributes.
Definition communication.h:198
static void init(Communication *comm)
Initialize the IOPin class.
static void setDirection(const PinDir direction)
Set the direction of the Pin.
static void setInterrupt(const bool enabled)
Enable/disable interrupts for the Pin.
static void serviceRoutine() __asm__("__vector_5") __attribute__((__signal__
Interrupt Service Routine for the IOPin. An interrupt is triggered if the logic-level of the Pin chan...
Class that provides functionality for the ATmega644's on-board 16-bit timer, such as an ISR.
Definition communication.h:101
static void serviceRoutine() __asm__("__vector_13") __attribute__((__signal__
Interrupt Service Routine for the 16-bit timer. An interrupt is triggered if the timer hits the value...
static void init(Communication *comm)
Initialize the Timer class.
static Communication * mComm
Communication object to access the class methods & attributes.
Definition communication.h:145
static constexpr uint16_t TIMER_BOTTOM
Timer bottom value.
Definition communication.h:146
static void stop()
Stop the 16-bit timer by setting no clock source.
Definition communication.h:118
static constexpr uint16_t ETU
The counter value which the timer should match.
Definition communication.h:141
static void setMatchValue(const uint16_t matchValue)
Change the value of register OCR1A.
Definition communication.h:129
static void start()
Start the timer, by setting the counter value to TIMER_BOTTOM & setting the clock source to CS10.
Definition communication.h:123
Class that implements the communication protocol between the SmartCard & the Terminal.
Definition communication.h:46
static constexpr bit_t START_BIT
The start bit of a transfer.
Definition communication.h:212
void sendDecryptedData(const byte_t *data)
Send the decrypted data to the Terminal.
volatile bool mParityError
Whether a parity error occurred while receiving a byte.
Definition communication.h:234
byte_t receiveByte()
Receive a single byte from the Terminal.
void sendBytes(const byte_t *bytes, const uint8_t len)
Send an array of bytes byte by byte.
Definition communication.h:273
static bit_t sampleBit()
Sample the IOPin 3-times for a more reliable result.
volatile byte_t mInputByte
The currently received input byte.
Definition communication.h:230
volatile bit_t mErrorBit
Error bit that is set to 0/1 during the stop bit indicating failure/success.
Definition communication.h:233
Logger mLog
Logger.
Definition communication.h:219
Communication()
Construct a new Communication object.
volatile PinDir mDirection
Current direction of the IOPin.
Definition communication.h:223
volatile bool mCheckErrors
Whether to check for errors after sending a byte.
Definition communication.h:232
volatile bool mByteReceived
Whether a byte was received successfully.
Definition communication.h:228
void sendATR()
Send the Answer-To-Reset sequence to the Terminal.
Definition communication.h:63
volatile bool mBitSent
Whether the last bit of a transmission was sent.
Definition communication.h:225
void sendBit(const bit_t bit)
Send a single bit to the Terminal.
volatile bit_t mOutputBit
The next bit to output.
Definition communication.h:226
void receiveProtocolHeader(const byte_t *header)
Receive a protocol header, which contains 5 bytes.
volatile uint8_t mInputBitCounter
Number of input bits in the current transfer.
Definition communication.h:229
static constexpr bit_t STOP_BIT
The stop bit of a transfer.
Definition communication.h:213
void receiveDataToDecrypt(byte_t *data)
Receive data to decrypt from the Terminal.
static bit_t getParity(byte_t byte)
Calculate the parity of a byte.
void sendByte(const byte_t byte)
Send a single byte to the Terminal.
Logger class that outputs logs over USART.
Definition logger.h:27
static constexpr uint8_t ATR_LENGTH
Length of the Answer-to-reset sequence.
Definition protocol.h:41
static constexpr byte_t ATR_SEQ[]
Answer-to-reset sequence, send at the start.
Definition protocol.h:40