Semaphore problem?

hi, i have a simple code snippet as this

s32 sem;
bool cmdRspHandler(adl_atResponse_t *rsp) {
	TRACE((1,rsp->StrData));
	if (strstr(rsp->StrData, "OK") || strstr(rsp->StrData, "ERROR")) {
		adl_semProduce(sem);
	}
	return FALSE;
}

void action ()
{
    sem = adl_semSubscribe(1);
    int i;
    for(i=0;i<10;i++){
    	TRACE((1,"consume %d",i));
    	adl_semConsume(sem);
    	adl_atCmdCreate("at+csq",ADL_AT_PORT_TYPE(ADL_AT_UART1,ADL_AT_RSP),cmdRspHandler,"*",NULL);
    }
    TRACE((1,"finished"));
}

as it, i want execute 10 times of command “at+csq” with adl_atCmdCreate, and it must wait for each command finish (response handler receive “OK” or “ERROR” string) before continue next one. So i use a semaphore (act as a event object), but i don’t know why it’ seem be frozen at line adl_atCmdCreate(…), could anyone show me where i’m wrong?
thanks very much.

Hiya,

I haven’t run your code, but it looks like you have a deadlock occurring.

Don’t forget that the call to adl_atCmdCreate() is non-blocking - that is, it will return immediately but the AT command will be executed sometime later - so your semaphores will be consumed (in the for() loop) before they are produced (in the AT event handler).

Also, reading the API for adl_semConsume() indicates that the calling task will be suspended when the number of remaining semaphores becomes less than zero.

ciao, Dave

oh, yes, that’s my purpose, so next time it’ll need to wait for previous command is finish ( the handler produced semaphore when i reach “OK” or “ERROR” message )
i searched and found this viewtopic.php?f=11&t=1692, is commandHandler a high level interruption?, maybe it is problem :frowning:
thanks you.

Hiya,

If that’s the case, why don’t you just start the next AT command from the AT command event handler? That way you will be sure that the first command has finished.

ciao, Dave