STM8S/A Standard Peripherals Firmware Library
|
STM8S_StdPeriph_Examples/UART1/UART1_SmartCard/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file UART1_SmartCard\main.c 00004 * @author MCD Application Team 00005 * @version V2.2.0 00006 * @date 30-September-2014 00007 * @brief This file contains the main function for UART1 Smartcard example. 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2> 00012 * 00013 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 00014 * You may not use this file except in compliance with the License. 00015 * You may obtain a copy of the License at: 00016 * 00017 * http://www.st.com/software_license_agreement_liberty_v2 00018 * 00019 * Unless required by applicable law or agreed to in writing, software 00020 * distributed under the License is distributed on an "AS IS" BASIS, 00021 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00022 * See the License for the specific language governing permissions and 00023 * limitations under the License. 00024 * 00025 ****************************************************************************** 00026 */ 00027 00028 /* Includes ------------------------------------------------------------------*/ 00029 #include "stm8s.h" 00030 00031 /** 00032 * @addtogroup UART1_SmartCard 00033 * @{ 00034 */ 00035 00036 /* Private define ------------------------------------------------------------*/ 00037 #define T0_PROTOCOL 0x00 /* T0 PROTOCOL */ 00038 #define SETUP_LENGHT 20 00039 #define HIST_LENGHT 20 00040 #define SC_Receive_Timeout 0x4000 /* direction to reader */ 00041 00042 /* Private typedef -----------------------------------------------------------*/ 00043 /* ATR STRUCTURE - ANSWER TO RESET */ 00044 typedef struct 00045 { 00046 uint8_t TS; /* Bit Convention */ 00047 uint8_t T0; /* high nibble = N. of setup byte; low nibble = N. of historical byte */ 00048 uint8_t T[SETUP_LENGHT]; /* setup array */ 00049 uint8_t H[HIST_LENGHT]; /* historical array */ 00050 uint8_t Tlenght; /* setup array dimension */ 00051 uint8_t Hlenght; /* historical array dimension */ 00052 } SC_ATR; 00053 00054 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus; 00055 00056 /* Private macro -------------------------------------------------------------*/ 00057 /* Private variables ---------------------------------------------------------*/ 00058 00059 SC_ATR SC_A2R; 00060 __IO uint32_t index = 0, Counter = 0; 00061 __IO TestStatus ATRDecodeStatus = FAILED; 00062 __IO uint32_t CardInserted = 0, CardProtocol = 1; 00063 __IO uint8_t DST_Buffer[50]= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 00064 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 00065 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 00066 00067 /* Private function prototypes -----------------------------------------------*/ 00068 /* Private functions ---------------------------------------------------------*/ 00069 void GPIO_Config(void); 00070 void Multiplexer_EvalBoard_Config(void); 00071 uint8_t SC_decode_Answer2reset(__IO uint8_t *card); 00072 00073 /** 00074 * @brief Main program. 00075 * @param None 00076 * @retval None 00077 */ 00078 void main(void) 00079 { 00080 uint32_t i = 0; 00081 /* Configure the multiplexer on the evalboard to select the smartCard*/ 00082 Multiplexer_EvalBoard_Config(); 00083 00084 /* Configure the GPIO ports */ 00085 GPIO_Config(); 00086 00087 /*High speed internal clock prescaler: 1*/ 00088 CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); 00089 00090 /* Enable general interrupts */ 00091 enableInterrupts(); 00092 00093 UART1_DeInit(); 00094 /* UART1 configuration -------------------------------------------------------*/ 00095 /* UART1 configured as follow: 00096 - Word Length = 9 Bits 00097 - 1.5 Stop Bit 00098 - Even parity 00099 - BaudRate = 10752 baud 00100 - Receive and transmit enabled 00101 - UART1 Clock enabled 00102 */ 00103 UART1_Init((uint32_t)10752, UART1_WORDLENGTH_9D, UART1_STOPBITS_1_5, UART1_PARITY_EVEN, 00104 UART1_SYNCMODE_CLOCK_ENABLE, UART1_MODE_TXRX_ENABLE); 00105 00106 /* UART1 Clock set to 4MHz (frequence master 16 MHZ / 4) */ 00107 UART1_SetPrescaler(0x02); 00108 00109 /* UART1 Guard Time set to Bit */ 00110 UART1_SetGuardTime(0x2); 00111 00112 /* Enable the UART1 Parity Error Interrupt */ 00113 UART1_ITConfig(UART1_IT_PE, ENABLE); 00114 00115 /* Enable the NACK Transmission */ 00116 UART1_SmartCardNACKCmd(ENABLE); 00117 00118 /* Enable the Smart Card Interface */ 00119 UART1_SmartCardCmd(ENABLE); 00120 00121 /* Loop while no smart card is detected */ 00122 while ((GPIO_ReadInputData(GPIOE)& 0x01) == 0x00) 00123 { 00124 } 00125 00126 /* PG7 - SmartCard_/CMDVCC: low */ 00127 GPIO_WriteLow(GPIOG, GPIO_PIN_7); 00128 00129 /* release SmartCard_RESET signal */ 00130 GPIO_WriteLow(GPIOG, GPIO_PIN_5); 00131 00132 for (i = 0; i < 6000; i++) 00133 { 00134 } 00135 /* set SmartCard_RESET signal */ 00136 GPIO_WriteHigh(GPIOG, GPIO_PIN_5); 00137 00138 00139 /* Read Smart Card ATR response */ 00140 for (index = 0; index < 40; index++) 00141 { 00142 Counter = 0; 00143 while ((UART1_GetFlagStatus(UART1_FLAG_RXNE) == RESET) && (Counter != SC_Receive_Timeout)) 00144 { 00145 Counter++; 00146 } 00147 00148 if (Counter != SC_Receive_Timeout) 00149 { 00150 DST_Buffer[index] = UART1_ReceiveData8(); 00151 } 00152 } 00153 00154 /* Decode ATR */ 00155 CardProtocol = SC_decode_Answer2reset(DST_Buffer); 00156 00157 /* Test if the inserted card is ISO7816-3 T=0 compatible */ 00158 if (CardProtocol == 0) 00159 { 00160 /* Inserted card is ISO7816-3 T=0 compatible */ 00161 ATRDecodeStatus = PASSED; 00162 } 00163 else 00164 { 00165 /* Inserted smart card is not ISO7816-3 T=0 compatible */ 00166 ATRDecodeStatus = FAILED; 00167 } 00168 00169 while (1) 00170 {} 00171 } 00172 00173 /** 00174 * @brief Configures the different GPIO ports. 00175 * @param None 00176 * @retval None 00177 */ 00178 void GPIO_Config(void) 00179 { 00180 /* Set PA5 as Output open-drain high-impedance level - SmartCard_IO(UART11_Tx)*/ 00181 GPIO_Init(GPIOA, GPIO_PIN_5, GPIO_MODE_OUT_OD_HIZ_FAST); 00182 /* Set PG5 as Output push-pull low level - SmartCard_RESET*/ 00183 GPIO_Init(GPIOG, GPIO_PIN_5, GPIO_MODE_OUT_PP_LOW_FAST); 00184 /* Set PA6 as Output push-pull low level - SmartCard_CLK*/ 00185 GPIO_Init(GPIOA, GPIO_PIN_6, GPIO_MODE_OUT_PP_LOW_FAST); 00186 /* Set PG4 as Output push-pull low level - SmartCard_5V/3V*/ 00187 GPIO_Init(GPIOG, GPIO_PIN_4, GPIO_MODE_OUT_PP_LOW_FAST); 00188 /* Set PG7 as Output push-pull high level (inactive state) - SmartCard_/CMDVCC*/ 00189 GPIO_Init(GPIOG, GPIO_PIN_7, GPIO_MODE_OUT_PP_HIGH_FAST); 00190 /* Set PE0 as Input pull-up, no external interrupt - SmartCard_OFF*/ 00191 GPIO_Init(GPIOE, GPIO_PIN_0, GPIO_MODE_IN_PU_NO_IT); 00192 /* Set RSTIN HIGH */ 00193 GPIO_WriteHigh(GPIOG, GPIO_PIN_5); 00194 00195 /* Select Smart Card CMDVCC */ 00196 GPIO_WriteHigh(GPIOG, GPIO_PIN_7); 00197 00198 /* Select 5 V */ 00199 GPIO_WriteHigh(GPIOG, GPIO_PIN_4); 00200 } 00201 00202 /** 00203 * @brief Decode the Card ATR Response. 00204 * @param None 00205 * @retval None 00206 */ 00207 uint8_t SC_decode_Answer2reset(__IO uint8_t *card) 00208 { 00209 uint32_t i = 0, flag = 0, buf = 0, protocol = 0; 00210 00211 SC_A2R.TS = 0; 00212 SC_A2R.T0 = 0; 00213 for (i = 0; i < SETUP_LENGHT; i++) 00214 { 00215 SC_A2R.T[i] = 0; 00216 } 00217 for (i = 0;i < HIST_LENGHT; i++) 00218 { 00219 SC_A2R.H[i] = 0; 00220 } 00221 SC_A2R.Tlenght = 0; 00222 SC_A2R.Hlenght = 0; 00223 00224 SC_A2R.TS = card[0]; /* INITIAL CHARACTER */ 00225 SC_A2R.T0 = card[1]; /* FORMAT CHARACTER */ 00226 00227 SC_A2R.Hlenght = (uint8_t)(SC_A2R.T0 & 0x0F); 00228 00229 if ((SC_A2R.T0 & 0x80) == 0x80) flag = 1; 00230 00231 for (i = 0; i < 4; i++) 00232 { 00233 SC_A2R.Tlenght = (uint8_t)(SC_A2R.Tlenght + (((SC_A2R.T0 & 0xF0) >> (4 + i)) & 0x1)); 00234 } 00235 00236 for (i = 0; i < SC_A2R.Tlenght; i++) 00237 { 00238 SC_A2R.T[i] = card[i + 2]; 00239 } 00240 00241 protocol = SC_A2R.T[SC_A2R.Tlenght - 1] & 0x0F; 00242 00243 while (flag) 00244 { 00245 if ((SC_A2R.T[SC_A2R.Tlenght-1] & 0x80)== 0x80) 00246 { 00247 flag = 1; 00248 } 00249 else 00250 { 00251 flag = 0; 00252 } 00253 buf = SC_A2R.Tlenght; 00254 SC_A2R.Tlenght = 0; 00255 00256 for (i = 0; i < 4; i++) 00257 { 00258 SC_A2R.Tlenght = (uint8_t)(SC_A2R.Tlenght + (((SC_A2R.T[buf - 1] & 0xF0) >> (4 + i)) & 0x1)); 00259 } 00260 for (i = 0; i < SC_A2R.Tlenght; i++) 00261 { 00262 SC_A2R.T[buf + i] = card[i + 2 + buf]; 00263 } 00264 SC_A2R.Tlenght += (uint8_t)buf; 00265 } 00266 00267 for (i = 0;i < SC_A2R.Hlenght; i++) 00268 { 00269 SC_A2R.H[i] = card[i + 2 + SC_A2R.Tlenght]; 00270 } 00271 00272 return ((uint8_t)protocol); 00273 } 00274 00275 /** 00276 * @brief Configures the Multiplexer on the evalboard to select the smard card. 00277 * @param None 00278 * @retval None 00279 */ 00280 void Multiplexer_EvalBoard_Config(void) 00281 { 00282 /* Initialize I/Os in Output PP Mode */ 00283 GPIO_Init(GPIOF, GPIO_PIN_5, GPIO_MODE_OUT_PP_LOW_FAST); 00284 GPIO_Init(GPIOF, GPIO_PIN_6, GPIO_MODE_OUT_PP_HIGH_FAST); 00285 } 00286 00287 #ifdef USE_FULL_ASSERT 00288 00289 /** 00290 * @brief Reports the name of the source file and the source line number 00291 * where the assert_param error has occurred. 00292 * @param file: pointer to the source file name 00293 * @param line: assert_param error line source number 00294 * @retval None 00295 */ 00296 void assert_failed(uint8_t* file, uint32_t line) 00297 { 00298 /* User can add his own implementation to report the file name and line number, 00299 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00300 00301 /* Infinite loop */ 00302 while (1) 00303 { 00304 } 00305 } 00306 #endif 00307 00308 /** 00309 * @} 00310 */ 00311 00312 00313 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/