STM8L libraries for CR95HF 1.0
CR95HF/inc/CR95HF_command.h
Go to the documentation of this file.
00001 
00023 #ifndef __CR95HF_COMMAND_H
00024 #define __CR95HF_COMMAND_H
00025 
00026 /* Includes ------------------------------------------------------------------*/
00027 // Same level
00028 
00029 
00030 
00031 
00032 //Lower level
00033 #include "cr95hf_structure.h"
00034 #include "cr95hf_driver.h"
00035 
00036 // Generic
00037 #include "string.h"
00038 
00039 
00040 /* Exported constants --------------------------------------------------------*/
00041 
00042 
00044 #define CR95HF_CMD_IDN                          0x01 
00045 #define CR95HF_CMD_PROT_SEL             0x02 
00046 #define CR95HF_CMD_SEND_REC     0x04 
00047 #define CR95HF_CMD_IDLE                         0x07 
00048 #define CR95HF_CMD_RDREG                        0x08 
00049 #define CR95HF_CMD_WRT_REG              0x09 
00050 #define CR95HF_CMD_BAUDRATE     0x0A 
00051 #define CR95HF_CMD_ECHO         0x55 
00052 
00053 //---------------------------------------------------------------------------
00054 // CUSTOM COMMANDS CODE  ST-RESERVED
00055 #define CR95HF_CMD_POLL_FIELD           0x03 
00056 #define CR95HF_CMD_LISTEN                               0x05
00057 #define CR95HF_CMD_SEND                                 0x06 
00058 #define CR95HF_CMD_SUB_FREQ_RES 0x0B 
00059 #define CR95HF_CMD_AC_FILTER            0x0D 
00060 //----------------------------------------------------------------------------
00061 
00063 #define CR95HF_CMD_RD_REG_DATALENGTH                                                                    0x03 
00064 #define CR95HF_CMD_BAUD_RATE_DATALENGTH                                                         0x01 
00065 #define CR95HF_CMD_IDLE_DATALENGTH                                                                              0x0E 
00066 #define CR95HF_PARAMETERSLENGTH_FIELDOFF                                                        0x01 
00067 #define CR95HF_PARAMETERSLENGTH_PROT_ISO15693                                   0x01 
00068 #define CR95HF_PARAMETERSLENGTH_PROT_ISO14443_BYTE0     0x01  
00069 #define CR95HF_PARAMETERSLENGTH_PROT_ISO14443_BYTE1     0x02 
00070 #define CR95HF_PARAMETERSLENGTH_PROT_ISO14443_BYTE2     0x03 
00071 #define CR95HF_PARAMETERSLENGTH_PROT_FELICA                                             0x02 
00072 
00073 // PROTOCOLS
00074 #define CR95HF_PROT_SEL_FIELDOFF                        0x00
00075 #define CR95HF_PROT_SEL_ISO15693                        0x01
00076 #define CR95HF_PROT_SEL_ISO14443_A              0x02
00077 #define CR95HF_PROT_SEL_ISO14443_B              0x03
00078 #define CR95HF_PROT_SEL_FELICA                          0x04
00079 
00080 
00081 // FIELD OFF PARAMETER BYTE
00082 #define CR95HF_FIELDOFF_PARAMETER_BYTE 0x00
00083 
00084 
00085 // PROTOCOL ISO15693 PARAMETERS 
00086 #define CR95HF_BIT7_6_RFU                                                               0x00
00087 #define CR95HF_BIT4_5_DATARATE_26KBITS          0x00
00088 #define CR95HF_BIT4_5_DATARATE_52KBITS          0x10
00089 #define CR95HF_BIT4_5_DATARATE_6KBITS                   0x20
00090 #define CR95HF_BIT4_5_DATARATE_RFU                              0x30
00091 #define CR95HF_BIT3_RESPECT312_DELAY                    0x00
00092 #define CR95HF_BIT3_WAIT_FOR_SOF                                        0x08
00093 #define CR95HF_BIT2_MODULATION_100                              0x00
00094 #define CR95HF_BIT2_MODULATION_10                                       0x04
00095 #define CR95HF_BIT1_SINGLE_CARRIER                              0x00
00096 #define CR95HF_BIT1_DUAL_CARRIERS                                       0x02
00097 #define CR95HF_BIT0_APPEND_CRC                                          0x01
00098 
00099 #define CR95HF_DATARATE_MASK                                                    0x30
00100 #define CR95HF_SHIFT_DATARATE_INFO_TO_PARAMETERS        0x04 // to shift the result of the get datarate command to a byte to use in the protocol select command
00101 
00102 // CR95HF RESPONSE CODE
00103 #define CR95HF_SUCCESS_CODE                                     0x00 //generic success code for command 
00104 #define CR95HF_ERROR_CODE                                               0x01 //generic errorcode for command
00105 
00106 // INIT VALUE
00107 #define CR95HF_INIT_ResponseReceiving                   0xEC // Error Check to prevent error from previous command 
00108 
00109 // IDN command
00110 #define CR95HF_IDN_SUCCESS_CODE                                         0x00
00111 #define CR95HF_IDN_ROMCRC_CURSOR                                        0x02
00112 
00113 // Protocol select command
00114 #define CR95HF_PROTOCOL_SUCCESS_CODE                    0x00
00115 #define CR95HF_ERROR_INVALID_PROTOCOL                   0x83
00116 #define CR95HF_ERROR_INVALID_CMD_LENGTH         0x82
00117 
00118 // SEND_RECV command
00119 #define CR95HF_SENDRECV_SUCCESS_CODE                            0x80
00120 // most of the error described below could be used for other commands
00121 #define CR95HF_ERROR_4BYTE_FRAME                                                0x90
00122 #define CR95HF_ERROR_COMMUNICATION                                      0x86
00123 #define CR95HF_ERROR_NOTAG_OR_TIMEOUT                           0x87
00124 #define CR95HF_ERROR_INVALID_SOF                                                0x88
00125 #define CR95HF_ERROR_BUFFER_OVERFLOW                            0x89
00126 #define CR95HF_ERROR_FRAMING                                                            0x8A
00127 #define CR95HF_EROR_EGT_TIMEOUT                                                 0x8B
00128 #define CR95HF_ERROR_INVALID_LENGTH_FELICA      0x8C
00129 #define CR95HF_ERROR_CRC_FELICA                                                 0x8D
00130 #define CR95HF_ERROR_RECEPTION_LOST                                     0x8E
00131 
00132 // Read Register commands
00133 #define CR95HF_RD_REG_SUCCESS_CODE                                              0x00
00134 #define CR95HF_RD_REG_ST_RESERVED                                                       0x00
00135 // wake up register
00136 #define CR95HF_WAKE_UP_REGISTER_CURSOR                          0x62
00137 #define CR95HF_WAKE_UP_REGISTER_SIZE                                    0x01
00138 // Analog configuration register
00139 #define CR95HF_ANALOG_CONFIG_REGISTER_CURSOR    0x69
00140 #define CR95HF_ANALOG_CONFIG_REGISTER_SIZE              0x01
00141 
00142 // Write register commands
00143 #define CR95HF_ANALOG_CONFIG_REGISTER_INDEX             0x68
00144 #define CR95HF_ANALOG_CONFIG_INDEX                                              0x01
00145 
00146 //  Write RCB Register
00147 #define CR95HF_WR_REG_LENGTH_OF_CMD                                             0x04
00148 #define CR95HF_WR_REG_FLAG_INCREMENT                                    0x01
00149 
00150 //  Set RCB Index command
00151 #define CR95HF_SET_INDEX_LENGTH_OF_CMD                          0x03
00152 #define CR95HF_SET_INDEX_FLAG_INCREMENT                         0x00
00153 
00154 // To compare with register value in order to determine what leaded to exit the last idle mode
00155 #define CR95HF_WAKE_UP_BECAUSE_TAG_DETECTION    0x02 
00156 #define CR95HF_WAKE_UP_BECAUSE_TIMEOUT                          0x01
00157 
00158 // Baud rate command 
00159 #define CR95HF_DEFAULT_BAUD_RATE                                                                        0x75
00160 #define CR95HF_BAUD_RATE_FAILED_DEFAULT_VALUE_ON        0x10
00161 #define CR95HF_BAUD_RATE_FAILED_PREVIOUS_VALUE_ON       0x20
00162 
00163 // ECHO command
00164 #define CR95HF_ECHO_SUCCESS_CODE                                        0x55
00165 
00166 // Baud rate error
00167 #define CR95HF_BAUD_RATE_ERROR          0xBE
00168 
00169 // TimeOut error
00170 #define CR95HF_COMMUNICATION_WITH_MCU_FAILED    0xCE // Communication Error 
00171 
00172 // Protocol parameters response for Get request
00173 // Invalid protocol
00174 #define CR95HF_INVALID_PROTOCOL                 0xE0
00175 #define CR95HF_PARAMETER_BYTE0                  0x00
00176 #define CR95HF_PARAMETER_BYTE1                  0xE0
00177 #define CR95HF_PARAMETER_BYTE2                  0xE0
00178 // ISO15693 parameters response for getting info functions
00179 #define CR95HF_DATARATE_26KBITS                 0x00
00180 #define CR95HF_DATARATE_52KBITS                 0x01 
00181 #define CR95HF_DATARATE_6KBITS                  0x02 
00182 #define CR95HF_DATARATE_RFU                                     0x03 
00183 #define CR95HF_RESPECT312_DELAY                 0x00
00184 #define CR95HF_WAIT_FOR_SOF                                     0x01
00185 #define CR95HF_MODULATION_100                           0x00
00186 #define CR95HF_MODULATION_10                            0x01 
00187 #define CR95HF_SINGLE_CARRIER                           0x00
00188 #define CR95HF_DUAL_CARRIERS                            0x01 
00189 
00190 
00191 // Idle command 
00192 #define CR95HF_CMD_IDLE_SUCCESS_CODE                    0x00
00193 //defaults parameters and flags paramaters
00194 #define CR95HF_IDLE_LEAVE_CTRL_L                                        0x18
00195 #define CR95HF_IDLE_LEAVE_CTRL_H                                        0x00
00196 #define CR95HF_IDLE_OSC_START                                           0x60
00197 #define CR95HF_IDLE_DAC_START                                           0x60
00198 #define CR95HF_IDLE_SWING_CNT                                           0x3F
00199 
00200 // Bits for Ctrl_L and Ctrl_H
00201 /*
00202 CtrlL   
00203          7 – Initial DAC compare index
00204    6 – DAC enable                       5 – LFO enable
00205    4 – HFO enable                       3 – VddA enable
00206    2 – Hibernate enable         1 – Idle mode enable
00207    0 – Sleep mode enable
00208 
00209 CtrlH   
00210    7 – RFU  6 – GtmStop (when set, can save ~3uA in Sleep (WU by SS will be disabled))
00211    5 – CLK source (0 = HFO; 1 = CLK_REC) Must be activated independently!
00212    4 – Card clamp enable                                3 – Card RX chain enable
00213    2 – Reader RX chain enable   1 – FDet enable
00214    0 – IRef enable
00215 */
00216 #define CR95HF_IDLE_CRTRL_L_INIT_DAC_CMP_IDX    0x80
00217 #define CR95HF_IDLE_CRTRL_L_DAC_ENABLE                          0x40    
00218 #define CR95HF_IDLE_CRTRL_L_LFO_ENABLE                          0x20
00219 #define CR95HF_IDLE_CRTRL_L_HFO_ENABLE                          0x10
00220 #define CR95HF_IDLE_CRTRL_L_VDDA_ENABLE                         0x08
00221 #define CR95HF_IDLE_CRTRL_L_HIBERNATE_ENABLE    0x04
00222 #define CR95HF_IDLE_CRTRL_L_IDLE_MODE_ENABLE    0x02
00223 #define CR95HF_IDLE_CRTRL_L_SLEEP_MODE_ENABLE   0x01
00224 
00225 #define CR95HF_IDLE_CRTRL_H_GTM_STOP                                                            0x40
00226 #define CR95HF_IDLE_CRTRL_H_CLK_SRC_HFO                                                 0x00
00227 #define CR95HF_IDLE_CRTRL_H_CLK_SRC_CLK_REC                                     0x02
00228 #define CR95HF_IDLE_CRTRL_H_CARD_CLAMP_ENABLE                           0x01    
00229 #define CR95HF_IDLE_CRTRL_H_CARD_RX_CHAIN_ENABLE                0x08
00230 #define CR95HF_IDLE_CRTRL_H_READER_RX_CHAIN_ENABLE      0x04
00231 #define CR95HF_IDLE_CRTRL_H_F_DET_ENABLE                                                0x02
00232 #define CR95HF_IDLE_CRTRL_H_IRef_ENABLE                                                 0x01
00233 
00234 
00235 #define CR95HF_IDLE_MASK_WAKE_UP_FLAGS                          0x0F
00236 
00237 #define CR95HF_IDLE_WUFLAGS_BIT0_TIMER 0x01 
00238 /* CR95HF wakes up  and returns in ready state at the end of  a predefine cycle Time Out is defined by Max sleep and Wake Up period  , 
00239 TO = (Maxsleep *( WuPeriod+1)*RT ,     RT= 256*TL = 8ms
00240 This bit must be set when using theTimer as a possible Wake-Up source .
00241  It must be set during TAG calibration to force Wake Up after the first Tag detection trial.*/
00242 #define CR95HF_IDLE_WUFLAGS_BIT1_TAG_DETECTED   0x02 
00243 /*Wake up when a Tag is detected ,  must also be set during Tag detector Calibration or Tag detection .*/
00244 #define CR95HF_IDLE_WUFLAGS_BIT2_RFU 0x00
00245 #define CR95HF_IDLE_WUFLAGS_BIT3_EXTERNAL_INTERRUPT_IRQ_IN 0x08
00246 /*Wake Up by an external Interrupt , low level on |IRQ_IN (IO7) , 
00247 useful in SPI It is recommended to set this bit in case of system crash .*/
00248 #define CR95HF_IDLE_WUFLAGS_BIT4_EXTERNAL_INTERRUPT_NSS          0x10
00249 /*Wake Up by an external Interrupt , low level on |SS (IO57) , Useful in UART*/
00250 #define CR95HF_IDLE_WUFLAGS_BIT5_RFU 0x00
00251 #define CR95HF_IDLE_WUFLAGS_BIT6_RFU 0x00
00252 #define CR95HF_IDLE_WUFLAGS_BIT7_RFU 0x00
00253 
00254 #define CR95HF_IDLE_WUFLAGS_BIT_RESET 0x00
00255 
00256 // Detector calibration parameters value
00257 #define CR95HF_DETECTOR_CALIBRATION_EnterCtrl_L         0xA2
00258 #define CR95HF_DETECTOR_CALIBRATION_EnterCtrl_H         0x00
00259 #define CR95HF_DETECTOR_CALIBRATION_WUCtrl_L                    0xF8
00260 #define CR95HF_DETECTOR_CALIBRATION_WUCtrl_H                    0x01
00261 #define CR95HF_DETECTOR_CALIBRATION_LeaveCtrl_L         0x18
00262 #define CR95HF_DETECTOR_CALIBRATION_LeaveCtrl_H         0x00
00263 #define CR95HF_DETECTOR_CALIBRATION_WUPeriod                    0x01
00264 #define CR95HF_DETECTOR_CALIBRATION_Max_Sleep                   0x01
00265 #define CR95HF_DETECTOR_CALIBRATION_REF_0         0x3F
00266 #define CR95HF_DETECTOR_CALIBRATION_LOOP_CNT_INIT 0x01
00267 #define CR95HF_DETECTOR_CALIBRATION_LOOP_CNT_END        0x06
00268 #define CR95HF_DETECTOR_CALIBRATION_DACDATA_H_MIN       0x00
00269 #define CR95HF_DETECTOR_CALIBRATION_DACDATA_H_MAX       0x3F
00270 #define CR95HF_DETECTOR_CALIBRATION_DACDATA_L                   0x00
00271 #define CR95HF_NUMBER_OF_WAITING_PERIOD                                         0x01// for tag calibration procedure
00272 
00273 
00274 //--------------------------------------------------------------------
00275 // CUSTOM CONSTANTS CODE ST-RESERVED
00276 
00277 // ERROR
00278 #define CR95HF_ERROR            0xEE // To detect error for comands which do not return error (for instance : poll field, baud rate)
00279 #define CR95HF_POLL_FIELD_CMD_FLAG_SET                          0x01
00280 #define CR95HF_POLL_FIELD_CMD_FLAG_RESET                        0x00
00281 #define CR95HF_POLL_FIELD_CMD_SUCCESS_CODE              0x00
00282 #define CR95HF_POLL_FIELD_CMD_DATALENGTH                        0x01
00283 
00284 #define CR95HF_LISTEN_CMD_SUCCESS_CODE                          0x00
00285 #define CR95HF_LISTEN_CMD_COMMUNICATION_SUCCESS_CODE            0x80
00286 #define CR95HF_LISTEN_ERROR_NOFIELD                                             0x8F
00287 
00288 #define CR95HF_SEND_CMD_SUCCESS_CODE                                    0x00
00289 #define CR95HF_SEND_CMD_RESPONSE_DATALENGTH             0x00
00290 
00291 #define CR95HF_WRITE_REG_CMD_SUCCESS_CODE                       0x00
00292 #define CR95HF_SUB_FREQ_RES_CMD_SUCCESS_CODE    0x00
00293 #define CR95HF_SUB_FREQ_RES_CMD_RESPONSE_DATALENGTH             0x01
00294 
00295 #define  CR95HF_ACFILTER_CMD_SUCCESS_CODE       0x00
00296 #define  CR95HF_ACFILTER_ATQA_LENGTH                    0x02
00297 #define  CR95HF_ACFILTER_UID_PART_LENGTH        0x04
00298 #define  CR95HF_ACFILTER_LENGTH_7                                       0x07
00299 #define  CR95HF_ACFILTER_LENGTH_11                              0x0B
00300 
00301 //--------------------------------------------------------------
00302 
00303 //--------------------------------------------------------------------
00304 //functions ----------------------------------------------------------  
00305 
00306 
00307 // Command functions
00308 
00309 u8 CR95HF_IDN (CR95HF_ResponseReceiving* Response);
00310 
00311 u8 CR95HF_ProtocolSelect (const u8 Protocol, const u8 ParametersLength,const u8* Parameters, CR95HF_Protocol_Config* Protocol_Settings);
00312 
00313 u8 CR95HF_SendRecv(CR95HF_ResponseReceiving* Response,const  CR95HF_CommandSending* Command);
00314 
00315 u8 CR95HF_Idle (const  u8 WakeUpFlags,const  u8 EnterCtrlL,const  u8 EnterCtrlH, const u8 WUCtrlL,const  u8 WUCtrlH,const  u8 LeaveCtrlL,const  u8  LeaveCtrlH ,const u8 WUPeriod,const  u8 OscStart,const  u8 DacStart,const  u8 DacDataL,const  u8 DacDataH,const  u8 SwingsCnt,const  u8 MaxSleep, CR95HF_Idle_Config* Idle_Config);
00316 
00317 u8 CR95HF_Rd_Wake_up_Reg(CR95HF_ResponseReceiving* Response,CR95HF_Idle_Config* Idle_Config);
00318 
00319 u8 CR95HF_Rd_Analog_Register_Config_B(CR95HF_ResponseReceiving* Response);
00320 
00321 u8 CR95HF_Set_Analog_Register_Config_B_Index( void);
00322 
00323 u8 CR95HF_Wr_Analog_Register_Config_B_Reg (u8 Analog_Configuration_Register);
00324 
00325 u8 CR95HF_Baud_Rate(const u8 New_Baud_Rate);
00326 
00327 u8 CR95HF_Echo (void);
00328 
00329 u8 CR95HF_FieldOff(CR95HF_Protocol_Config* Protocol_Settings);
00330 
00331 u8 CR95HF_SendEOF(CR95HF_ResponseReceiving* Response);
00332 
00333 u8 CR95HF_Modify_Baud_Rate(const u8 New_Baud_Rate);
00334 
00335 // Support functions 
00336 
00337 u8 CR95HF_IsReaderResultOK(CR95HF_ResponseReceiving* Response,const u8 Code_OK);
00338 
00339 u8 CR95HF_ISO15693_ComputeParameterByte_ProtocolSelect
00340 ( u8 Bit7_6,u8 Bit5_4,u8 Bit3,u8 Bit2,u8 Bit1);
00341 
00342 static void CR95HF_Update_Protocol_Selected( CR95HF_Protocol_Config* Protocol_Settings, u8 Status,
00343 u8 Protocol,u8 parameters_Length, u8* Parameters);
00344 
00345 u8 CR95HF_Is_IRQin_Interrupt_Wake_up_Condition(const u8 WUFlags);
00346 
00347 u8 CR95HF_Is_NSS_Interrupt_Wake_up_Condition(const u8 WUFlags);
00348 
00349 u8 CR95HF_Is_Tag_Detected_Wake_up_Condition(const u8 WUFlags);
00350 
00351 u8 CR95HF_Is_Timeout_Wake_up_Condition(const u8 WUFlags);
00352 
00353 u8 CR95HF_Idle_Detector_Calibration(CR95HF_Protocol_Config* Protocol_Settings,CR95HF_Idle_Config* Idle_Config);
00354 
00355 static u8 CR95HF_Tagcal(const u8 DacDataL, const u8 DacDataH);
00356 
00357 u8 CR95HF_Tagdet(const u8 DacDataL, const u8 DacDataH);
00358 
00359 u8 CR95HF_Wait_Wake_up_from_Idle(CR95HF_ResponseReceiving* CR95HF_Response_Idle,CR95HF_Idle_Config* Idle_Config, u8 Number_of_DELAY_BEFORE_TIMEOUT_250MS);
00360 
00361 u8 CR95HF_Wake_up_CR95HF(CR95HF_Idle_Config* Idle_Config);
00362 
00363 
00364 // private functions
00365 
00366 void CR95HF_SendCommand(CR95HF_CommandSending* Cmd);
00367 
00368 CR95HF_ResponseReceiving CR95HF_ReceiveAnswer(void);
00369 
00370 static CR95HF_ResponseReceiving CR95HF_SendReceive_Cmd(CR95HF_CommandSending* Cmd);
00371 
00372 void  CR95HF_Wait_Receive_Answer(CR95HF_ResponseReceiving* Response);
00373 
00374 void CR95HF_Init_ResponseReceiving(CR95HF_ResponseReceiving* Response);
00375 
00376 void CR95HF_Add_Data_ToCommand(CR95HF_CommandSending* Cmd, u8* Cptr_ToIncrement, u8  DataLength, u8* Data);
00377 
00378 // Custom Commands ST-Reserved
00379 
00380 #ifdef ST_RESERVED_FUNCTIONS_AVAILABLE
00381 CR95HF_ResponseReceiving CR95HF_Poll_Field(void);
00382 
00383 u8 CR95HF_Listen(CR95HF_ResponseReceiving* Response);
00384 
00385 u8 CR95HF_Send( CR95HF_CommandSending* Command);
00386 
00387 u8 CR95HF_Rd_Reg( CR95HF_ResponseReceiving* Response, u8 Address, u8 RegCount, u8  Flags);
00388 
00389 u8 CR95HF_Wr_Reg( u8* Reg_Data, u8 Address, u8 RegCount, u8  Flags);
00390 
00391 u8 CR95HF_SubFreqRes(u8* Sub_freq_divider);
00392 
00393 u8 CR95HF_AcFilter(u8 Length, u8* ATQA, u8 SAK, u8* UID_part_1, u8* UID_part_2, u8* UID_part_3 );
00394 #endif 
00395 
00396 #endif /*__CR95HF_COMMAND_H */
00397         
00398         /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
 All Data Structures Files Functions Variables Defines