smartcard2  2.1.0.0
smartcard2.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2020 MikroElektronika d.o.o.
3 ** Contact: https://www.mikroe.com/contact
4 **
5 ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 ** of this software and associated documentation files (the "Software"), to deal
7 ** in the Software without restriction, including without limitation the rights
8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 ** copies of the Software, and to permit persons to whom the Software is
10 ** furnished to do so, subject to the following conditions:
11 ** The above copyright notice and this permission notice shall be
12 ** included in all copies or substantial portions of the Software.
13 **
14 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 ** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 ** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19 ** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 ** USE OR OTHER DEALINGS IN THE SOFTWARE.
21 ****************************************************************************/
22 
28 #ifndef SMARTCARD2_H
29 #define SMARTCARD2_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
35 #include "mikrosdk_version.h"
36 
37 #ifdef __GNUC__
38 #if mikroSDK_GET_VERSION < 20800ul
39 #include "rcu_delays.h"
40 #else
41 #include "delays.h"
42 #endif
43 #endif
44 
45 #include "drv_digital_out.h"
46 #include "drv_digital_in.h"
47 #include "drv_uart.h"
48 
69 #define SMARTCARD2_CMD_ICC_POWER_ON 0x62
70 #define SMARTCARD2_CMD_ICC_POWER_OFF 0x63
71 #define SMARTCARD2_CMD_GET_SLOT_STATUS 0x65
72 #define SMARTCARD2_CMD_XFR_BLOCK 0x6F
73 #define SMARTCARD2_CMD_GET_PARAMETERS 0x6C
74 #define SMARTCARD2_CMD_RESET_PARAMETERS 0x6D
75 #define SMARTCARD2_CMD_SET_PARAMETERS 0x61
76 #define SMARTCARD2_CMD_ESCAPE 0x6B
77 #define SMARTCARD2_CMD_ICC_ESCAPE 0x6E
78 #define SMARTCARD2_CMD_T0_APDU 0x6A
79 #define SMARTCARD2_CMD_SECURE 0x69
80 #define SMARTCARD2_CMD_MECHANICAL 0x71
81 #define SMARTCARD2_CMD_ABORT 0x72
82 #define SMARTCARD2_CMD_SET_DATA_RATE_CLK_FREQ 0x73
83 
88 #define SMARTCARD2_RSP_DATA_BLOCK 0x80
89 #define SMARTCARD2_RSP_SLOT_STATUS 0x81
90 #define SMARTCARD2_RSP_PARAMETERS 0x82
91 #define SMARTCARD2_RSP_ESCAPE 0x83
92 #define SMARTCARD2_RSP_DATA_RATE_CLK_FREQ 0x84
93 
98 #define SMARTCARD2_EVT_NOTIFY_SLOT_CHANGE 0x50
99 #define SMARTCARD2_CARD_ABSENT 0x02
100 #define SMARTCARD2_CARD_PRESENT 0x03
101 #define SMARTCARD2_EVT_HARDWARE_ERROR 0x51
102 #define SMARTCARD2_OVERCURRENT 0x01
103 
108 #define SMARTCARD2_SYNC 0x03
109 #define SMARTCARD2_CTRL_ACK 0x06
110 #define SMARTCARD2_CTRL_NACK 0x15
111 #define SMARTCARD2_SHORT_APDU_MAX_SIZE 261
112 // sync + ack + 10-byte ccid header + short APDU size + checksum
113 #define SMARTCARD2_MAX_BUF_SIZE ( SMARTCARD2_SHORT_APDU_MAX_SIZE + 13 )
114 #define SMARTCARD2_TIMEOUT_MS 5000
115 
120 #define SMARTCARD2_CLK_STATUS_RUNNING 0x00
121 #define SMARTCARD2_CLK_STATUS_STATE_L 0x01
122 #define SMARTCARD2_CLK_STATUS_STATE_H 0x02
123 
128 #define SMARTCARD2_POWER_SEL_AUTO 0x00
129 #define SMARTCARD2_POWER_SEL_5V 0x01
130 #define SMARTCARD2_POWER_SEL_3V 0x02
131 #define SMARTCARD2_POWER_SEL_1_8V 0x03
132 
137 #define SMARTCARD2_ICC_ACTIVE 0
138 #define SMARTCARD2_ICC_PRESENT 1
139 #define SMARTCARD2_ICC_ABSENT 2
140 #define SMARTCARD2_ICC_STATUS_MASK 0x03
141 
147 #define SMARTCARD2_TX_DRV_BUFFER_SIZE SMARTCARD2_MAX_BUF_SIZE
148 #define SMARTCARD2_RX_DRV_BUFFER_SIZE ( SMARTCARD2_MAX_BUF_SIZE * 2 )
149  // smartcard2_cmd
151 
166 #define SMARTCARD2_MAP_MIKROBUS( cfg, mikrobus ) \
167  cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
168  cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
169  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST );
170  // smartcard2_map // smartcard2
173 
178 typedef struct
179 {
180  uint8_t type;
181  uint32_t payload_size;
182  uint8_t slot_num;
183  uint8_t seq_num;
184  uint8_t spec_bytes[ 3 ];
186  uint8_t payload[ SMARTCARD2_SHORT_APDU_MAX_SIZE ];
189 
194 typedef struct
195 {
196  // Output pins
197  digital_out_t rst;
199  // Modules
200  uart_t uart;
202  // Buffers
203  char uart_rx_buffer[ SMARTCARD2_RX_DRV_BUFFER_SIZE ];
204  char uart_tx_buffer[ SMARTCARD2_TX_DRV_BUFFER_SIZE ];
206  uint8_t seq_num;
209 } smartcard2_t;
210 
215 typedef struct
216 {
217  // Communication gpio pins
218  pin_name_t rx_pin;
219  pin_name_t tx_pin;
221  // Additional gpio pins
222  pin_name_t rst;
224  // Static variable
225  uint32_t baud_rate;
227  uart_data_bits_t data_bit;
228  uart_parity_t parity_bit;
229  uart_stop_bits_t stop_bit;
232 
237 typedef enum
238 {
244 
246 
263 
278 
291 err_t smartcard2_generic_write ( smartcard2_t *ctx, char *data_in, uint16_t len );
292 
305 err_t smartcard2_generic_read ( smartcard2_t *ctx, char *data_out, uint16_t len );
306 
316 
326 
340 
357 
373 err_t smartcard2_icc_power_on ( smartcard2_t *ctx, uint8_t power_sel );
374 
387 
400 
401 #ifdef __cplusplus
402 }
403 #endif
404 #endif // SMARTCARD2_H
405  // smartcard2
407 
408 // ------------------------------------------------------------------------ END
smartcard2_t
Smart Card 2 Click context object.
Definition: smartcard2.h:195
smartcard2_enable_device
void smartcard2_enable_device(smartcard2_t *ctx)
Smart Card 2 enable device function.
SMARTCARD2_ERROR
@ SMARTCARD2_ERROR
Definition: smartcard2.h:240
smartcard2_icc_power_on
err_t smartcard2_icc_power_on(smartcard2_t *ctx, uint8_t power_sel)
Smart Card 2 icc power on function.
smartcard2_ccid_t
Smart Card 2 Click context object.
Definition: smartcard2.h:179
smartcard2_cfg_t::data_bit
uart_data_bits_t data_bit
Definition: smartcard2.h:227
smartcard2_ccid_t::slot_num
uint8_t slot_num
Definition: smartcard2.h:182
smartcard2_icc_power_off
err_t smartcard2_icc_power_off(smartcard2_t *ctx)
Smart Card 2 icc power off function.
smartcard2_t::rst
digital_out_t rst
Definition: smartcard2.h:197
smartcard2_ccid_t::type
uint8_t type
Definition: smartcard2.h:180
smartcard2_generic_write
err_t smartcard2_generic_write(smartcard2_t *ctx, char *data_in, uint16_t len)
Smart Card 2 data writing function.
smartcard2_read_ccid
err_t smartcard2_read_ccid(smartcard2_t *ctx, smartcard2_ccid_t *data_out)
Smart Card 2 read ccid function.
smartcard2_send_ccid
err_t smartcard2_send_ccid(smartcard2_t *ctx, smartcard2_ccid_t *data_in)
Smart Card 2 send ccid function.
SMARTCARD2_CHECKSUM_ERROR
@ SMARTCARD2_CHECKSUM_ERROR
Definition: smartcard2.h:242
smartcard2_ccid_t::seq_num
uint8_t seq_num
Definition: smartcard2.h:183
smartcard2_cfg_t::stop_bit
uart_stop_bits_t stop_bit
Definition: smartcard2.h:229
SMARTCARD2_OK
@ SMARTCARD2_OK
Definition: smartcard2.h:239
smartcard2_cfg_t
Smart Card 2 Click configuration object.
Definition: smartcard2.h:216
smartcard2_disable_device
void smartcard2_disable_device(smartcard2_t *ctx)
Smart Card 2 disable device function.
SMARTCARD2_READ_ERROR
@ SMARTCARD2_READ_ERROR
Definition: smartcard2.h:241
smartcard2_cfg_t::uart_blocking
bool uart_blocking
Definition: smartcard2.h:226
SMARTCARD2_TIMEOUT_ERROR
@ SMARTCARD2_TIMEOUT_ERROR
Definition: smartcard2.h:243
SMARTCARD2_TX_DRV_BUFFER_SIZE
#define SMARTCARD2_TX_DRV_BUFFER_SIZE
Smart Card 2 driver buffer size.
Definition: smartcard2.h:147
smartcard2_return_value_t
smartcard2_return_value_t
Smart Card 2 Click return value data.
Definition: smartcard2.h:238
smartcard2_t::seq_num
uint8_t seq_num
Definition: smartcard2.h:206
smartcard2_cfg_t::tx_pin
pin_name_t tx_pin
Definition: smartcard2.h:219
smartcard2_cfg_setup
void smartcard2_cfg_setup(smartcard2_cfg_t *cfg)
Smart Card 2 configuration object setup function.
smartcard2_get_slot_status
err_t smartcard2_get_slot_status(smartcard2_t *ctx)
Smart Card 2 get slot status function.
smartcard2_generic_read
err_t smartcard2_generic_read(smartcard2_t *ctx, char *data_out, uint16_t len)
Smart Card 2 data reading function.
smartcard2_ccid_t::payload_size
uint32_t payload_size
Definition: smartcard2.h:181
SMARTCARD2_SHORT_APDU_MAX_SIZE
#define SMARTCARD2_SHORT_APDU_MAX_SIZE
Definition: smartcard2.h:111
smartcard2_t::uart
uart_t uart
Definition: smartcard2.h:200
smartcard2_cfg_t::baud_rate
uint32_t baud_rate
Definition: smartcard2.h:225
smartcard2_cfg_t::rst
pin_name_t rst
Definition: smartcard2.h:222
smartcard2_cfg_t::parity_bit
uart_parity_t parity_bit
Definition: smartcard2.h:228
smartcard2_cfg_t::rx_pin
pin_name_t rx_pin
Definition: smartcard2.h:218
smartcard2_init
err_t smartcard2_init(smartcard2_t *ctx, smartcard2_cfg_t *cfg)
Smart Card 2 initialization function.
SMARTCARD2_RX_DRV_BUFFER_SIZE
#define SMARTCARD2_RX_DRV_BUFFER_SIZE
Definition: smartcard2.h:148