Chibios and the SIM900 Shiled

316 Views Asked by At

I have this problem: I have an STM32 Nucleo L152RE and a Shield SIM 900.

Now if I write this easy thread, all work well:

static THD_WORKING_AREA(waRead, 128);
static THD_FUNCTION(Thread, arg) {
    (void)arg;
    chRegSetThreadName("th_callback");
    while (TRUE) {
        /* This will wait for a character to be received */
        uint8_t c = sdGet(&SD1); // Questo prende il carattere
        sdPut(&SD2, c);  // Questo lo spara alla terminale
    }
}

When I send an AT command I see the ok answer. Now I create this buffer:

static  uint8_t bufferMsg[128];

And I use this thread for storing the answer:

static THD_WORKING_AREA(waRead5, 128);
static THD_FUNCTION(Thread5, arg) {
    chRegSetThreadName("th_Riempio_Buffer");
    msg_t charbuf;
    int count = 0;
    uint8_t c;
    event_listener_t Uart1Data;

    eventmask_t flags;
    chEvtRegisterMask((event_source_t *)chnGetEventSource(&SD1), &Uart1Data, EVENT_MASK(1));

    while (TRUE) {
        chEvtWaitOneTimeout(EVENT_MASK(1), MS2ST(10));
        chSysLock();
        flags = chEvtGetAndClearFlags(&Uart1Data);
        chSysUnlock();
        if (flags & CHN_INPUT_AVAILABLE)
        {
            do
            {
                charbuf = chnGetTimeout(&SD1, TIME_IMMEDIATE);
                if (charbuf != Q_TIMEOUT)
                {
                    while((charbuf != '\n') && (count < 128)) {
                        sdWrite(&SD2, (uint8_t *)B3, 4); // Va bene
                        bufferMsg[count] = charbuf;
                        count++;
                    }
                }
            }
            while (charbuf != Q_TIMEOUT)
                ;
        }
    }
}

These threads don't work and don't store the answer. How can I fix this?

1

There are 1 best solutions below

0
On BEST ANSWER

For me, I use,

Define

# Define buffer_size 128
char buffer[buffer_size + 1];
int nbytes = 0;

Function

void SIM_callback(){             /*  GSM900 Serial */
    char x = SIM.getc();
    buffer[nbytes] = x;
    nbytes++;
    if (nbytes > buffer_size)
        nbytes = buffer_size;
    buffer[nbytes] = '\0';
}

Main

main (){
    // Clear Buffer
    buffer[nbytes] = '\0';
    ...
    while(1)
        ;
    ...
}