Hi,
I'm new with ChibiOS, I want to understand how to configure and use the serial driver for send and receive integers of 32 bits. I read the example http://www.chibios.org/dokuwiki/doku.ph ... _discovery but I couldn't understand very well. I would appreciate if you could help me and maybe if you have an example code I will very grateful.
Regards
Serial driver help
Moderators: RoccoMarco, lbednarz, tfAteba
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: Serial driver help
Hi,
You may send your data in ASCII and separate the values using a separator, LF for example.This way you would be writing/reading simple lines of text.
Giovanni
You may send your data in ASCII and separate the values using a separator, LF for example.This way you would be writing/reading simple lines of text.
Giovanni
Re: Serial driver help
Giovanny,
Thank you very much for your help, I would like to know how can I send those ASCII characters for example using chSequentialStreamWrite, I have this Code but when I compile it I get an error:
arm-none-eabi-gcc build/obj/crt0.o build/obj/vectors.o build/obj/chcore.o build/obj/chcore_v7m.o build/obj/nvic.o build/obj/chsys.o build/obj/chdebug.o build/obj/chlists.o build/obj/chvt.o build/obj/chschd.o build/obj/chthreads.o build/obj/chdynamic.o build/obj/chregistry.o build/obj/chsem.o build/obj/chmtx.o build/obj/chcond.o build/obj/chevents.o build/obj/chmsg.o build/obj/chmboxes.o build/obj/chqueues.o build/obj/chmemcore.o build/obj/chheap.o build/obj/chmempools.o build/obj/test.o build/obj/testthd.o build/obj/testsem.o build/obj/testmtx.o build/obj/testmsg.o build/obj/testmbox.o build/obj/testevt.o build/obj/testheap.o build/obj/testpools.o build/obj/testdyn.o build/obj/testqueues.o build/obj/testbmk.o build/obj/hal.o build/obj/adc.o build/obj/can.o build/obj/ext.o build/obj/gpt.o build/obj/i2c.o build/obj/icu.o build/obj/mac.o build/obj/mmc_spi.o build/obj/mmcsd.o build/obj/pal.o build/obj/pwm.o build/obj/rtc.o build/obj/sdc.o build/obj/serial.o build/obj/serial_usb.o build/obj/spi.o build/obj/tm.o build/obj/uart.o build/obj/usb.o build/obj/stm32_dma.o build/obj/hal_lld.o build/obj/adc_lld.o build/obj/ext_lld.o build/obj/gpt_lld.o build/obj/i2c_lld.o build/obj/icu_lld.o build/obj/mac_lld.o build/obj/pwm_lld.o build/obj/serial_lld.o build/obj/spi_lld.o build/obj/uart_lld.o build/obj/pal_lld.o build/obj/usb_lld.o build/obj/rtc_lld.o build/obj/sdc_lld.o build/obj/board.o build/obj/chprintf.o build/obj/main.o -mcpu=cortex-m4 -nostartfiles -T../..//os/ports/GCC/ARMCMx/STM32F4xx/ld/STM32F407xG.ld -Wl,-Map=build/main.map,--cref,--no-warn-mismatch,--gc-sections -mno-thumb-interwork -mthumb -lm -o build/main.elf
build/obj/main.o: In function `main':
/home/kari/Documents/Stamps/ChibiOS/ChibiOS2.5/demos/serial_chibios/main.c:24: multiple definition of `main'
build/obj/serial.o:/home/kari/Documents/Stamps/ChibiOS/ChibiOS2.5/demos/serial_chibios/serial.c:24: first defined here
build/obj/serial.o: In function `main':
serial.c:(.text.startup.main+0x10): undefined reference to `sdStart'
build/obj/hal.o: In function `halInit':
/home/kari/Documents/Stamps/ChibiOS/ChibiOS2.5/demos/serial_chibios/../..//os/hal/src/hal.c:96: undefined reference to `sdInit'
build/obj/serial_lld.o: In function `serve_interrupt':
/home/kari/Documents/Stamps/ChibiOS/ChibiOS2.5/demos/serial_chibios/../..//os/hal/platforms/STM32/serial_lld.c:294: undefined reference to `sdIncomingDataI'
collect2: error: ld returned 1 exit status
make: *** [build/main.elf] Error 1
I'm using ChibiOS V 2.5 over an STM32F4.
Thank you very much for your help, I would like to know how can I send those ASCII characters for example using chSequentialStreamWrite, I have this Code but when I compile it I get an error:
Code: Select all
#include "ch.h"
#include "hal.h"
#include "chprintf.h"
#include <math.h>
static WORKING_AREA(waThread1, 128);
msg_t TestThread(void *p);
static msg_t Thread1(void *arg) {
(void)arg;
uint8_t send;
while (TRUE) {
send = 65;
chSequentialStreamWrite((BaseSequentialStream *)&SD1,&send, sizeof(send));
chThdSleepMilliseconds(100);
}
return(0);
}
/*
* Application entry point.
*/
int main(void) {
/*
* System initializations.
* - HAL initialization, this also initializes the configured device drivers
* and performs the board-specific initializations.
* - Kernel initialization, the main() function becomes a thread and the
* RTOS is active.
*/
halInit();
chSysInit();
/*
* Activates the serial driver 1 using the driver default configuration.
*/
//UART
sdStart(&SD1, NULL);
chprintf((BaseSequentialStream *)&SD1,"Starting1");
TestThread(&SD1);
/*
* Creates the blinker thread.
*/
chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
/*
* Normal main() thread activity, in this demo it does nothing.
*/
while (TRUE) {
TestThread(&SD1);
chThdSleepMilliseconds(1000);
}
}arm-none-eabi-gcc build/obj/crt0.o build/obj/vectors.o build/obj/chcore.o build/obj/chcore_v7m.o build/obj/nvic.o build/obj/chsys.o build/obj/chdebug.o build/obj/chlists.o build/obj/chvt.o build/obj/chschd.o build/obj/chthreads.o build/obj/chdynamic.o build/obj/chregistry.o build/obj/chsem.o build/obj/chmtx.o build/obj/chcond.o build/obj/chevents.o build/obj/chmsg.o build/obj/chmboxes.o build/obj/chqueues.o build/obj/chmemcore.o build/obj/chheap.o build/obj/chmempools.o build/obj/test.o build/obj/testthd.o build/obj/testsem.o build/obj/testmtx.o build/obj/testmsg.o build/obj/testmbox.o build/obj/testevt.o build/obj/testheap.o build/obj/testpools.o build/obj/testdyn.o build/obj/testqueues.o build/obj/testbmk.o build/obj/hal.o build/obj/adc.o build/obj/can.o build/obj/ext.o build/obj/gpt.o build/obj/i2c.o build/obj/icu.o build/obj/mac.o build/obj/mmc_spi.o build/obj/mmcsd.o build/obj/pal.o build/obj/pwm.o build/obj/rtc.o build/obj/sdc.o build/obj/serial.o build/obj/serial_usb.o build/obj/spi.o build/obj/tm.o build/obj/uart.o build/obj/usb.o build/obj/stm32_dma.o build/obj/hal_lld.o build/obj/adc_lld.o build/obj/ext_lld.o build/obj/gpt_lld.o build/obj/i2c_lld.o build/obj/icu_lld.o build/obj/mac_lld.o build/obj/pwm_lld.o build/obj/serial_lld.o build/obj/spi_lld.o build/obj/uart_lld.o build/obj/pal_lld.o build/obj/usb_lld.o build/obj/rtc_lld.o build/obj/sdc_lld.o build/obj/board.o build/obj/chprintf.o build/obj/main.o -mcpu=cortex-m4 -nostartfiles -T../..//os/ports/GCC/ARMCMx/STM32F4xx/ld/STM32F407xG.ld -Wl,-Map=build/main.map,--cref,--no-warn-mismatch,--gc-sections -mno-thumb-interwork -mthumb -lm -o build/main.elf
build/obj/main.o: In function `main':
/home/kari/Documents/Stamps/ChibiOS/ChibiOS2.5/demos/serial_chibios/main.c:24: multiple definition of `main'
build/obj/serial.o:/home/kari/Documents/Stamps/ChibiOS/ChibiOS2.5/demos/serial_chibios/serial.c:24: first defined here
build/obj/serial.o: In function `main':
serial.c:(.text.startup.main+0x10): undefined reference to `sdStart'
build/obj/hal.o: In function `halInit':
/home/kari/Documents/Stamps/ChibiOS/ChibiOS2.5/demos/serial_chibios/../..//os/hal/src/hal.c:96: undefined reference to `sdInit'
build/obj/serial_lld.o: In function `serve_interrupt':
/home/kari/Documents/Stamps/ChibiOS/ChibiOS2.5/demos/serial_chibios/../..//os/hal/platforms/STM32/serial_lld.c:294: undefined reference to `sdIncomingDataI'
collect2: error: ld returned 1 exit status
make: *** [build/main.elf] Error 1
I'm using ChibiOS V 2.5 over an STM32F4.
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: Serial driver help
Hi,
Is the serial driver enabled in halconf.h? also make sure to enable the serial port you want to use in mcuconf.h.
Giovanni
Is the serial driver enabled in halconf.h? also make sure to enable the serial port you want to use in mcuconf.h.
Giovanni
Re: Serial driver help
Thank You,
In halfconf.h I have:
/**
* @brief Enables the SERIAL subsystem.
*/
#if !defined(HAL_USE_SERIAL) || defined(__DOXYGEN__)
#define HAL_USE_SERIAL TRUE
#endif
and
#if !defined(SERIAL_DEFAULT_BITRATE) || defined(__DOXYGEN__)
#define SERIAL_DEFAULT_BITRATE 9600
#endif
In mcuconf.n I have:
/*
* SERIAL driver system settings.
*/
#define STM32_SERIAL_USE_USART1 TRUE
#define STM32_SERIAL_USE_USART2 FALSE
#define STM32_SERIAL_USE_USART3 FALSE
#define STM32_SERIAL_USE_UART4 FALSE
#define STM32_SERIAL_USE_UART5 FALSE
#define STM32_SERIAL_USE_USART6 FALSE
#define STM32_SERIAL_USART1_PRIORITY 12
#define STM32_SERIAL_USART2_PRIORITY 12
#define STM32_SERIAL_USART3_PRIORITY 12
#define STM32_SERIAL_UART4_PRIORITY 12
#define STM32_SERIAL_UART5_PRIORITY 12
#define STM32_SERIAL_USART6_PRIORITY 12
In halfconf.h I have:
/**
* @brief Enables the SERIAL subsystem.
*/
#if !defined(HAL_USE_SERIAL) || defined(__DOXYGEN__)
#define HAL_USE_SERIAL TRUE
#endif
and
#if !defined(SERIAL_DEFAULT_BITRATE) || defined(__DOXYGEN__)
#define SERIAL_DEFAULT_BITRATE 9600
#endif
In mcuconf.n I have:
/*
* SERIAL driver system settings.
*/
#define STM32_SERIAL_USE_USART1 TRUE
#define STM32_SERIAL_USE_USART2 FALSE
#define STM32_SERIAL_USE_USART3 FALSE
#define STM32_SERIAL_USE_UART4 FALSE
#define STM32_SERIAL_USE_UART5 FALSE
#define STM32_SERIAL_USE_USART6 FALSE
#define STM32_SERIAL_USART1_PRIORITY 12
#define STM32_SERIAL_USART2_PRIORITY 12
#define STM32_SERIAL_USART3_PRIORITY 12
#define STM32_SERIAL_UART4_PRIORITY 12
#define STM32_SERIAL_UART5_PRIORITY 12
#define STM32_SERIAL_USART6_PRIORITY 12
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: Serial driver help
It should work, are you using the standard makefiles?
Try upgrading to version 2.6.0 too.
Giovanni
Try upgrading to version 2.6.0 too.
Giovanni
Re: Serial driver help
Hi Giovanni,
Thanks for your help finally I could use the serial driver. The problem was the interface I was using to see the data "A". It was working all the time, sorry for the mistake.
I would like to ask you about the IRQ to the serial driver and about a problem that I have with EXT driver. I'm reading the serial port into a static Thread but I would like to read the port only when it receives the data using an interruption, but I really don't know how doing it. The other question is about EXT driver, I'm reading 8 Digital inputs but unfortunately two of them are in the same channel 13 I had already seen in the forum that for ST32F4 it is not possible to do that, but I have already the hardware, maybe you could advice to me another way pretty close to an interruption for reading this PIN.
I hope you could help me, thanks a lot.
Thanks for your help finally I could use the serial driver. The problem was the interface I was using to see the data "A". It was working all the time, sorry for the mistake.
I would like to ask you about the IRQ to the serial driver and about a problem that I have with EXT driver. I'm reading the serial port into a static Thread but I would like to read the port only when it receives the data using an interruption, but I really don't know how doing it. The other question is about EXT driver, I'm reading 8 Digital inputs but unfortunately two of them are in the same channel 13 I had already seen in the forum that for ST32F4 it is not possible to do that, but I have already the hardware, maybe you could advice to me another way pretty close to an interruption for reading this PIN.
Code: Select all
#include "ch.h"
#include "hal.h"
#include "chprintf.h"
#include <stdio.h>
#include <string.h>
uint8_t DI = 0, DO1 = 0;
uint16_t DI1E=0,DI2E=0,DIC=0;
uint16_t AI1=0, AI2=0, AI3=0, AI4=0;
char message[11];
char receive;
static void adccb(ADCDriver *adcp, adcsample_t *buffer, size_t n);
/*Data to Send*/
struct frame {
uint16_t AInput1;
uint16_t AInput2;
uint16_t AInput3;
uint16_t AInput4;
uint8_t DInput;
uint8_t state;
} values;
/* Total number of channels to be sampled by a single ADC operation.*/
#define ADC_GRP1_NUM_CHANNELS 4
/* Depth of the conversion buffer, channels are sampled four times each.*/
#define ADC_GRP1_BUF_DEPTH 32
#define N 5 // 2^N = define ADC_GRP1_BUF_DEPTH
/*
* ADC samples buffer.
*/
static adcsample_t samples[ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH];
/*
* ADC conversion group.
* Mode: Linear buffer, ADC_GRP1_BUF_DEPTH samples of 4 channels, SW triggered.
* Channels: IN10 (48 cycles sample time)
* Channels: IN11 (48 cycles sample time)
* Channels: IN12 (48 cycles sample time)
* Channels: IN13 (48 cycles sample time)
*/
static const ADCConversionGroup adcgrpcfg = {
FALSE,
ADC_GRP1_NUM_CHANNELS,
adccb,
NULL,
/* HW dependent part.*/
0,
ADC_CR2_SWSTART,
ADC_SMPR1_SMP_AN13(ADC_SAMPLE_56) | ADC_SMPR1_SMP_AN12(ADC_SAMPLE_56) | ADC_SMPR1_SMP_AN11(ADC_SAMPLE_56) | ADC_SMPR1_SMP_AN10(ADC_SAMPLE_56),
0,
ADC_SQR1_NUM_CH(ADC_GRP1_NUM_CHANNELS),
0,
ADC_SQR3_SQ4_N(ADC_CHANNEL_IN13) | ADC_SQR3_SQ3_N(ADC_CHANNEL_IN12) | ADC_SQR3_SQ2_N(ADC_CHANNEL_IN11) | ADC_SQR3_SQ1_N(ADC_CHANNEL_IN10)
};
/*
* ADC end conversion callback.
* The PWM channels are reprogrammed using the latest ADC samples.
* The latest samples are transmitted into a single SPI transaction.
*/
void adccb(ADCDriver *adcp, adcsample_t *buffer, size_t n) {
(void) buffer; (void) n;
/* Note, only in the ADC_COMPLETE state because the ADC driver fires an
intermediate callback when the buffer is half full.*/
if (adcp->state == ADC_COMPLETE) {
int i;
uint32_t avg1=0,avg2=0,avg3=0,avg4=0;
/* Calculates the average values from the ADC samples.*/
for (i=0;i<ADC_GRP1_BUF_DEPTH;i++){
avg1 += samples[4*i];
avg2 += samples[4*i+1];
avg3 += samples[4*i+2];
avg4 += samples[4*i+3];
}
AI1 = avg1 >> N;
AI2 = avg2 >> N;
AI3 = avg3 >> N;
AI4 = avg4 >> N;
adcStopConversion(&ADCD1);
//chSysUnlockFromIsr();
}
}
static WORKING_AREA(waThread2, 128);
static msg_t Thread2(void *arg) {
(void)arg;
//int i=0;
while (TRUE) {
values.AInput1 = AI1;
values.AInput2 = AI2;
values.AInput3 = AI3;
values.AInput4 = AI4;
values.DInput = DI;
values.state = 0;
memcpy(message, &values, sizeof (values));
chSequentialStreamWrite((BaseSequentialStream *)&SD1, (const uint8_t *)&message, 10);
chSequentialStreamRead((BaseSequentialStream *)&SD1, (uint8_t *)&receive,4);
DO1 = receive;
if (DO1 == 10)
palClearPad(GPIOE, GPIOE_DO1);
else if (DO1 == 20)
palSetPad(GPIOE, GPIOE_DO1);
else if (DO1 == 30)
palClearPad(GPIOE, GPIOE_DO2);
else if (DO1 == 40)
palSetPad(GPIOE, GPIOE_DO2);
else if (DO1 == 50)
palClearPad(GPIOE, GPIOE_DO3);
else if (DO1 == 60)
palSetPad(GPIOE, GPIOE_DO3);
else if (DO1 == 70)
palClearPad(GPIOE, GPIOE_DO4);
else if (DO1 == 80)
palSetPad(GPIOE, GPIOE_DO4);
adcStartConversionI(&ADCD1, &adcgrpcfg, samples, ADC_GRP1_BUF_DEPTH);
chThdSleepMilliseconds(1000);
}
return(0);
}
/* Digital Inputs*/
static void led5off(void *arg) {
(void)arg;
palClearPad(GPIOA, GPIO_LED);
}
static void extcb1(EXTDriver *extp, expchannel_t channel) {
static VirtualTimer vt4;
(void)extp;
(void)channel;
DI1E = palReadPort(GPIOE);
DI2E = ((DI1E & 0x5C)>>2) | (DI1E & 0x80) | ((DI1E & 0x3000)>>7);
DI = DI2E;
//chSysLockFromIsr();
//chSysUnlockFromIsr();
}
static const EXTConfig extcfg = {
{
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
{EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
{EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
{EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
{EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_DISABLED, NULL},
}
};
/*
* Application entry point.
*/
int main(void) {
/*
* System initializations.
* - HAL initialization, this also initializes the configured device drivers
* and performs the board-specific initializations.
* - Kernel initialization, the main() function becomes a thread and the
* RTOS is active.
*/
halInit();
chSysInit();
/*
* Activates the serial driver 1 using the driver default configuration.
*/
sdStart(&SD1, NULL);
extStart(&EXTD1, &extcfg);
adcStart(&ADCD1, NULL);
palSetGroupMode(GPIOC, PAL_PORT_BIT(0) | PAL_PORT_BIT(1) | PAL_PORT_BIT(2) | PAL_PORT_BIT(3),0,PAL_MODE_INPUT_ANALOG);
palClearPad(GPIOE, GPIOE_DO1);
palClearPad(GPIOE, GPIOE_DO2);
palClearPad(GPIOE, GPIOE_DO3);
palClearPad(GPIOE, GPIOE_DO4);
chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO, Thread2, NULL);
while (TRUE) {
}
}I hope you could help me, thanks a lot.
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: Serial driver help
Hi,
This is not possible using the serial driver, you can use events, the driver generates an event on incoming data, please look at the many other posts regarding serial driver and events.
You can't use EXT for equally numbered pins, if one of those pins is also a TIM input as alternate function you may program the timer to generate an interruption on a state change however I never did that. You may use a capture mode for that. Alternatively you may poll the pin using a virtual timer or a GPT timer.
Giovanni
adnil wrote:but I would like to read the port only when it receives the data using an interruption
This is not possible using the serial driver, you can use events, the driver generates an event on incoming data, please look at the many other posts regarding serial driver and events.
You can't use EXT for equally numbered pins, if one of those pins is also a TIM input as alternate function you may program the timer to generate an interruption on a state change however I never did that. You may use a capture mode for that. Alternatively you may poll the pin using a virtual timer or a GPT timer.
Giovanni