Code: Select all
#include "ch.h"
#include "hal.h"
static virtual_timer_t vt3, vt4, vt5;
static uint8_t message[] = "CIAO\r";
static uint8_t buffer[16];
static void led3off(void *p) {
(void)p;
palClearPad(GPIOA, GPIOA_LED_GREEN);
}
static void led4off(void *p) {
(void)p;
palClearPad(GPIOA, GPIOA_LED_GREEN);
}
static void led5off(void *p) {
(void)p;
palClearPad(GPIOA, GPIOA_LED_GREEN);
}
/*
* This callback is invoked when a transmission buffer has been completely
* read by the driver.
*/
static void txend1(UARTDriver *uartp) {
(void)uartp;
}
/*
* This callback is invoked when a transmission has physically completed.
*/
static void txend2(UARTDriver *uartp) {
(void)uartp;
palSetPad(GPIOA, GPIOA_LED_GREEN);
chSysLockFromISR();
chVTResetI(&vt5);
chVTSetI(&vt5, MS2ST(200), led5off, NULL);
chSysUnlockFromISR();
}
/*
* This callback is invoked on a receive error, the errors mask is passed
* as parameter.
*/
static void rxerr(UARTDriver *uartp, uartflags_t e) {
(void)uartp;
(void)e;
}
/*
* This callback is invoked when a character is received but the application
* was not ready to receive it, the character is passed as parameter.
*/
static void rxchar(UARTDriver *uartp, uint16_t c) {
(void)uartp;
(void)c;
/* Flashing the LED each time a character is received.*/
palSetPad(GPIOA, GPIOA_LED_GREEN);
chSysLockFromISR();
chVTResetI(&vt4);
chVTSetI(&vt4, MS2ST(200), led4off, NULL);
chSysUnlockFromISR();
}
/*
* This callback is invoked when a receive buffer has been completely written.
*/
static void rxend(UARTDriver *uartp) {
(void)uartp;
/* Flashing the LED each time a character is received.*/
palSetPad(GPIOA, GPIOA_LED_GREEN);
chSysLockFromISR();
chVTResetI(&vt3);
chVTSetI(&vt3, MS2ST(200), led3off, NULL);
chSysUnlockFromISR();
}
/*
* UART driver configuration structure.
*/
static UARTConfig uart_cfg_2 = {
txend1,
txend2,
rxend,
rxchar,
rxerr,
9600,
0,
USART_CR2_LINEN,
0
};
void controllo(uint8_t *m)
{
while(*m!=0x0D) {
if(*m==0x43) //C 0x43
{
chSysLockFromISR();
uartStartSendI(&UARTD2, 2, "O1");
chSysUnlockFromISR();
}
chThdSleepMilliseconds(50);
if(*m==0x41) //A 0x41
{
chSysLockFromISR();
uartStartSendI(&UARTD2, 2, "O2");
chSysUnlockFromISR();
}
*m++;
}
}
/*
* Application entry point.
*/
int main(void) {
halInit();
chSysInit();
uartStart(&UARTD2, &uart_cfg_2);
palSetPadMode(GPIOA, 2, PAL_MODE_ALTERNATE(7));
palSetPadMode(GPIOA, 3, PAL_MODE_ALTERNATE(7));
while (true) {
uartStopReceive(&UARTD2);
uartStopSend(&UARTD2);
uartStartReceive(&UARTD2, 5, buffer);
uartStartSend(&UARTD2, 4, message);
if(UARTD2.rxstate==2)
{
chThdSleepMilliseconds(50);
controllo(&buffer);
chThdSleepMilliseconds(200);
}
chThdSleepMilliseconds(500);
}
}
i see the string ciao but i don't have nothing in the buffer, can you help me?
best regards
F.