ATmega644 Smart-Card
communication.h
1 
11 #ifndef COMM_INTERFACE_H
12 #define COMM_INTERFACE_H
13 
14 #ifdef __cplusplus
15 extern "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 
31 enum class PinDir
32 {
33  OUTPUT = 0,
34  INPUT = 1
35 };
36 
46 {
47 public:
48  // ******************************************************************************
49  // Public Methods ***************************************************************
50  // ******************************************************************************
56  Communication();
57 
64 
73  void receiveDataToDecrypt(byte_t *data);
74 
86  void sendDecryptedData(const byte_t *data);
87 
88 private:
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
Communication::receiveByte
byte_t receiveByte()
Receive a single byte from the Terminal.
Communication::STOP_BIT
static constexpr bit_t STOP_BIT
The stop bit of a transfer.
Definition: communication.h:213
Communication::mBitSent
volatile bool mBitSent
Whether the last bit of a transmission was sent.
Definition: communication.h:225
Communication::Communication
Communication()
Construct a new Communication object.
Communication::mCheckErrors
volatile bool mCheckErrors
Whether to check for errors after sending a byte.
Definition: communication.h:232
Communication::mParityError
volatile bool mParityError
Whether a parity error occurred while receiving a byte.
Definition: communication.h:234
Communication::mErrorBit
volatile bit_t mErrorBit
Error bit that is set to 0/1 during the stop bit indicating failure/success.
Definition: communication.h:233
Communication::sendDecryptedData
void sendDecryptedData(const byte_t *data)
Send the decrypted data to the Terminal.
Communication::mOutputBit
volatile bit_t mOutputBit
The next bit to output.
Definition: communication.h:226
Communication::Timer::setMatchValue
static void setMatchValue(const uint16_t matchValue)
Change the value of register OCR1A.
Definition: communication.h:129
Communication::Timer::ETU
static constexpr uint16_t ETU
The counter value which the timer should match.
Definition: communication.h:141
Protocol::ATR_LENGTH
static constexpr uint8_t ATR_LENGTH
Length of the Answer-to-reset sequence.
Definition: protocol.h:41
Communication
Class that implements the communication protocol between the SmartCard & the Terminal.
Definition: communication.h:45
Communication::getParity
static bit_t getParity(byte_t byte)
Calculate the parity of a byte.
Communication::Timer::start
static void start()
Start the timer, by setting the counter value to TIMER_BOTTOM & setting the clock source to CS10.
Definition: communication.h:123
Communication::Timer::TIMER_BOTTOM
static constexpr uint16_t TIMER_BOTTOM
Timer bottom value.
Definition: communication.h:146
Communication::Timer::mComm
static Communication * mComm
Communication object to access the class methods & attributes.
Definition: communication.h:145
Communication::Timer
Class that provides functionality for the ATmega644's on-board 16-bit timer, such as an ISR.
Definition: communication.h:100
Communication::sendBytes
void sendBytes(const byte_t *bytes, const uint8_t len)
Send an array of bytes byte by byte.
Definition: communication.h:273
Communication::Timer::init
static void init(Communication *comm)
Initialize the Timer class.
Communication::IOPin::mComm
static Communication * mComm
Communication object to access the class methods & attributes.
Definition: communication.h:198
Communication::mByteReceived
volatile bool mByteReceived
Whether a byte was received successfully.
Definition: communication.h:228
Communication::mDirection
volatile PinDir mDirection
Current direction of the IOPin.
Definition: communication.h:223
Communication::mLog
Logger mLog
Logger.
Definition: communication.h:219
Communication::Timer::serviceRoutine
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...
Communication::IOPin
Class that provides functionality for the ATmega644's PinB6, such as an ISR.
Definition: communication.h:164
Communication::sendBit
void sendBit(const bit_t bit)
Send a single bit to the Terminal.
Communication::sendByte
void sendByte(const byte_t byte)
Send a single byte to the Terminal.
Logger
Logger class that outputs logs over USART.
Definition: logger.h:26
Protocol::ATR_SEQ
static constexpr byte_t ATR_SEQ[]
Answer-to-reset sequence, send at the start.
Definition: protocol.h:40
Communication::Timer::stop
static void stop()
Stop the 16-bit timer by setting no clock source.
Definition: communication.h:118
Communication::receiveDataToDecrypt
void receiveDataToDecrypt(byte_t *data)
Receive data to decrypt from the Terminal.
Communication::mInputByte
volatile byte_t mInputByte
The currently received input byte.
Definition: communication.h:230
Communication::mInputBitCounter
volatile uint8_t mInputBitCounter
Number of input bits in the current transfer.
Definition: communication.h:229
Communication::receiveProtocolHeader
void receiveProtocolHeader(const byte_t *header)
Receive a protocol header, which contains 5 bytes.
Communication::START_BIT
static constexpr bit_t START_BIT
The start bit of a transfer.
Definition: communication.h:212
Communication::sampleBit
static bit_t sampleBit()
Sample the IOPin 3-times for a more reliable result.
Communication::sendATR
void sendATR()
Send the Answer-To-Reset sequence to the Terminal.
Definition: communication.h:63