This is how I get around the startup timing issues when dealing with SIM initialization. gprs_openBearer is called from my 10 second start delay timer routine…
mySimState = adl_simGetState();
TRACE (( MAIN_TRACE_GPRS, "(gprs_openBearer)SIM State = %d", mySimState ));
// need to wait for SIM to initialize
// and subscribe to SIM handler
// test if SIM required in gprs_simHandler
adl_simSubscribe( gprs_simHandler, (ascii *)NULL);
if( mySimState == ADL_SIM_EVENT_FULL_INIT ) // SIM already OK, go straight to AT+CREG?
gprs_pollCreg( 0 ); /* argument 0 is dummy, see poll_reg() "Object" comment */
if ( mySimState == ADL_SIM_EVENT_PIN_WAIT ) // waiting for SIM PIN
Note that this code is based upon the WIP sample code provided with the following mods:
- Check the state of the SIM first off. If it is already initialized, you will not get the ADL_SIM_EVENT_FULL_INIT event in the sim handler, so we need to deal with this state ourselves
- When subscribing to the SIM handler, DON’T set a PIN - even if the SIM card requires a PIN. This will ensure that we will get ALL SIM events for the duration of the subscription
- Check if the SIM state is ADL_SIM_EVENT_FULL_INIT - if it is, then a SIM PIN is not required so head off to check that the Network (AT+CREG?) is up and continue processing
- Check if the SIM state is ADL_SIM_EVENT_PIN_WAIT - if so, programmatically enter the PIN (using adl_simEnterPIN()), and then the ADL_SIM_EVENT_FULL_INIT will be caught by the sim handler subscribed earlier
This works fine for me - every time - if there is a SIM PIN required or not.