this does not work !!! if scanf is activate, then over the terminal does stop und doesn’t work!!!
//scanf(“%inr”,&e);
//printf(“%i”,e);
And using with getchar the task is working very good!
I am using ARM COrtex M4F!
Maybe this is about problem with uart()!
Thanks!
reagrds Kamran
// =========================================================
// Copyright(C) 2014 Kamran
// Beschreibung: Abfrage + Ausgabe + DAC + ADC + Pin D8
// + FrreRTOS
// =========================================================
//Example code to loop back the data sent to USART2 on STM32F4DISCOVERY
//Inlcude header files
include “stm32f4xx.h”
include “FreeRTOS.h”
include “task.h”
include <math.h>
include <stdio.h>
include <stdlib.h>
include <string.h>
include “stm32f4xx.h”
include “stm32f4xx_conf.h”
include “stm32f4xx_usart.h”
include “stm32f4xx_rcc.h”
include “stm32f4xx_gpio.h”
include “system_stm32f4xx.h”
include “FreeRTOSConfig.h”
include “stm32f4xx_tim.h”
include “uart.h”
//Task For Sending Data Via USART
static void UsartTask(void *pvParameters)
{
int x;
printf(“rn”); // malloc
printf(“>Herorn”); // malloc 2
putchar(‘t’);
putchar(‘y’);
printf(“rn”); // malloc 3
while(1) {
// this does not work !!! if scanf is activate, then over the //terminal stop und doesn’t work!!!
//scanf(“%inr”,&e);
//printf(“%i”,e);
int c;
printf("rn");
c = getchar();
putchar(c);
printf("rn");
printf("das war c: %c nr", c);
}while(1);
}
//Initialize GPIO and USART6
void initx(void)
{
uart_init(9600);
}
//Main Function
int main(void)
{
char buffer[256];
//Call initx(); To Initialize USART & GPIO
initx();
//Create Task For USART
xTaskCreate(UsartTask, (signed char*)"UsartTask", 128, NULL, tskIDLE_PRIORITY+1, NULL);
//Call Scheduler
vTaskStartScheduler();
}
/***************************************************************/
include “uart.h”
include “stm32f4xx.h”
include “ringbuf.h”
include <stdlib.h>
include <stdio.h>
include <errno.h>
define RX_SIZE 128
define TX_SIZE 128
struct ringbuf rx
buf = { .buf = (char[RXSIZE]) {}, .bufsize = RX
SIZE };
struct ringbuf txbuf = { .buf = (char[TX
SIZE]) {}, .bufsize = TXSIZE };
static volatile struct uart
stats {
uint32t rx
overrun;
uint32t rx
bytes;
uint32t tx
bytes;
} uartstats;
void USART6
IRQHandler(void)
{
if (USART6->SR & USARTSR
RXNE) {
char c = USART6->DR;
if (!rbputc(&rx
buf, c))
uartstats.rx
overrun++;
else {
#if 0
/* ECHO */
rbputc(&tx
buf, c);
USART6->CR1 |= USARTCR1
TXEIE;
#endif
uartstats.rx_bytes++;
}
}
if (USART6->SR & USART_SR_TXE) {
char c;
if (rb_getc(&tx_buf, &c)) {
// send a queued byte
//
USART6->DR = c;
}
else {
// nothing to send, disable interrupt
//
USART6->CR1 &= ~USART_CR1_TXEIE;
}
uart_stats.tx_bytes++;
}
}
int uart
charsavail(void)
{
return rx_buf.len;
}
ssize
t uartwrite
r(struct _reent *r, int fd, const void *ptr, sizet len)
{
const char
c = (const char) ptr;
for (int i = 0; i < len; i++) {
while (!rb_putc(&tx_buf, *c));
c++;
// Enable TX empty interrupt
USART6->CR1 |= USART_CR1_TXEIE; // CR1 -> SR
}
return len;
}
ssize
t uartread
r(struct _reent *r, int fd, void *ptr, sizet len)
{
while (!rx_buf.len);
if (len > rx_buf.len)
len = rx_buf.len;
char *c = (char*)ptr;
for (int i = 0; i < len; i++)
rb_getc(&rx_buf, c++);
return len;
}
void uart
pollsend(const char
ch)
{
while (ch) {
USART6->DR = *ch++ & 0xff;
while (!(USART6->SR & USART_FLAG_TXE));
uart_stats.tx_bytes++;
}
}
/**
* Initialize UART.
*
* param baudrate Baudrate
*
* PC6 USART6
TXD
* PC7 USART7RXD
*
*/
void uart_init(int baudrate)
{
// Enable peripheral clocks
//
RCC->AHB1ENR |= RCC_AHB1Periph_GPIOC;
RCC->APB2ENR |= RCC_APB2Periph_USART6;
#if 0
// Initialize Serial Port
//
GPIO_Init(GPIOC, &(GPIO_InitTypeDef) {
.GPIO_Pin = GPIO_Pin_6,
.GPIO_Speed = GPIO_Speed_50MHz,
.GPIO_Mode = GPIO_Mode_AF,
.GPIO_OType = GPIO_OType_PP
});
GPIO_Init(GPIOC, &(GPIO_InitTypeDef) {
.GPIO_Pin = GPIO_Pin_7,
.GPIO_Mode = GPIO_Mode_IN,
.GPIO_PuPd = GPIO_PuPd_UP
});
#endif
#if 1
// Initialize Serial Port
//
GPIO_Init(GPIOC, &(GPIO_InitTypeDef) {
.GPIO_Pin = GPIO_Pin_6,
.GPIO_Speed = GPIO_Speed_50MHz,
.GPIO_Mode = GPIO_Mode_AF,
.GPIO_PuPd = GPIO_PuPd_NOPULL,
.GPIO_OType = GPIO_OType_PP
});
GPIO_Init(GPIOC, &(GPIO_InitTypeDef) {
.GPIO_Pin = GPIO_Pin_7,
.GPIO_Speed = GPIO_Speed_50MHz,
.GPIO_Mode = GPIO_Mode_AF,
.GPIO_PuPd = GPIO_PuPd_NOPULL,
.GPIO_OType = GPIO_OType_PP
});
#endif
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);
USART_Init(USART6, &(USART_InitTypeDef) {
.USART_BaudRate = baudrate,
.USART_WordLength = USART_WordLength_8b,
.USART_StopBits = USART_StopBits_1,
.USART_Parity = USART_Parity_No ,
.USART_HardwareFlowControl = USART_HardwareFlowControl_None,
.USART_Mode = USART_Mode_Rx | USART_Mode_Tx
});
NVIC_Init(&(NVIC_InitTypeDef) {
.NVIC_IRQChannel = USART6_IRQn,
.NVIC_IRQChannelPreemptionPriority = configLIBRARY_KERNEL_INTERRUPT_PRIORITY,
.NVIC_IRQChannelSubPriority = 0,
.NVIC_IRQChannelCmd = ENABLE
});
USART_ITConfig(USART6, USART_IT_RXNE , ENABLE);
USART_Cmd(USART6, ENABLE);
}