- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I still have problems with UART communication, I read the datasheet and some post on this forum but I can't do something on my own, I would like just to collect some byte in an array of char, make it a string and send it back.
for example send H-e-l-l-o and create Hello\0. What's wrong in my code? why the programm doesn't stop if I dont put a char in the Filling function loop? I have no idea.
Solved! Go to Solution.
- Labels:
-
PSoC 5 Device Programming
-
PSoC 5LP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
As It bothered me that I was not using tx_isr in my previous message I re-activated tx_isr.
======================================
#include "project.h"
#include<stdio.h>
#define SIZE 6
char str[128] ; /* print buffer */
int rx_flag = 0 ;
int tx_flag = 0 ;
int buffer_full = 0 ;
char* Filling(char* Buffer, char* pTail, char* pHead);
char* Reading(char* Buffer, char* pTail, char* pHead);
CY_ISR(Rx_isr_handling) { //ISR to check if a char was collect
Rx_isr_ClearPending() ;
rx_flag = 1 ;
}
CY_ISR(Tx_isr_handling){ //ISR to check if a char was sent
Tx_isr_ClearPending() ;
tx_flag = 1 ;
}
int main(void){
char Buffer[SIZE];
char* BufRxIndx=Buffer;
char* BufWxIndx=Buffer;
CyGlobalIntEnable;
UART_Start();
sprintf(str, "Uart test program (%s %s)\n", __DATE__, __TIME__ ) ;
UART_PutString(str) ;
Rx_isr_StartEx(Rx_isr_handling);
Tx_isr_StartEx(Tx_isr_handling) ;
for(;;){
if (rx_flag) { /* received a char */
if (buffer_full) {
UART_PutString("Buffer Over Run!\n") ;
} else {
BufRxIndx=Filling(Buffer, BufRxIndx, BufWxIndx);
rx_flag = 0 ;
}
}
if (tx_flag) { /* tx buffer empty */
if (buffer_full) { /* write only when the buffer is full */
BufWxIndx=Reading(Buffer, BufRxIndx, BufWxIndx);
BufRxIndx = Buffer ;
BufWxIndx = Buffer ;
buffer_full = 0 ;
tx_flag = 0 ;
}
}
}
}
//Get char by char and and build a string
char* Filling(char* Buffer, char* pTail, char* pHead){
(void) Buffer ;
while(((pTail - pHead) < SIZE - 1)&&UART_GetRxBufferSize()){
UART_PutString("Print a char: ");
*pTail=UART_GetChar();
UART_PutChar(*pTail) ;
UART_PutString("\n") ;
pTail++;
}
if ((pTail - pHead) >= (SIZE-1)) {
*pTail = '\0' ;
buffer_full = 1 ; /* need to write out the buffer */
}
return(pTail); //Back to RxBufIndx the address of pTail
}
//Read the string
char* Reading(char* Buffer, char* pTail, char* pHead){
(void) Buffer ;
if ((pTail - pHead) > 0) { /* anything in the buffer? */
UART_PutString("Buffer reading procedure\n");
UART_PutString("Buffer keeps the following string: ");
UART_PutString(pHead);
UART_PutString("\n") ;
}
return(pHead); //Back to WxBufIndx the address of pHead
}
======================================
moto
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
In your interrupt handler you just reset the status, but you do not fetch a character. The delay within an interrupt handler will stall the system, do not use that.
As a first approach I would suggest you to increase the component's buffer sizes to ~80. This will enable an internal interrupt which will fetch all data from Rx. Using UART_GetRxBufferSize() API will tell you how many bytes have been fetched already. UART_UART_GetByte() will retrieve them.
Depending on your Cypress kit you use the Rx and Tx pins should be connected to the UART-USB bridge on your kitprog.Consult schematics and kitprog documentation.
Happy coding
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I played with your project and applied "some" modification(s).
First of all, please note that if you pass a pointer to a function
the function can modify the contents of the pointer,
but the function can not update the value of the pointer itself.
So with calling
BufRxIndx=Filling(Buffer, BufRxIndx, BufWxIndx);
BufWxIndx will not be updated, even if you assign a new value to BufWxIndx.
Similarly you can not modify the value of BufRRxIndex by calling
BufWxIndx=Reading(Buffer, BufRxIndx, BufWxIndx);
If you definitely need to do this, you may be able to call the function
you need to pass the address of the pointer, which is called "handle"
please use some search engines, such as google about it.
Anyway, I modified you main.c like below
================================
#include "project.h"
#include<stdio.h>
#define SIZE 6
char str[128] ; /* print buffer */
int rx_flag = 0 ;
int tx_flag = 0 ;
char* Filling(char* Buffer, char* pTail, char* pHead);
char* Reading(char* Buffer, char* pTail, char* pHead);
CY_ISR(Rx_isr_handling) { //ISR to check if a char was collect
Rx_isr_ClearPending() ;
rx_flag = 1 ;
}
CY_ISR(Tx_isr_handling){ //ISR to check if a char was sent
Tx_isr_ClearPending() ;
tx_flag = 1 ;
}
int main(void){
char Buffer[SIZE];
char* BufRxIndx=Buffer;
char* BufWxIndx=Buffer;
CyGlobalIntEnable;
UART_Start();
sprintf(str, "Uart test program (%s %s)\n", __DATE__, __TIME__ ) ;
UART_PutString(str) ;
Rx_isr_StartEx(Rx_isr_handling);
// Tx_isr_StartEx(Rx_isr_handling); // ???
// Tx_isr_StartEx(Tx_isr_handling) ; // I did not use this
for(;;){
if (rx_flag) { /* received a char */
BufRxIndx=Filling(Buffer, BufRxIndx, BufWxIndx);
rx_flag = 0 ;
}
if (tx_flag) { /* tx buffer empty */
BufWxIndx=Reading(Buffer, BufRxIndx, BufWxIndx);
BufRxIndx = Buffer ;
BufWxIndx = Buffer ;
tx_flag = 0 ;
}
}
}
//Get char by char and and build a string
char* Filling(char* Buffer, char* pTail, char* pHead){
(void) Buffer ;
while(((pTail - pHead) < SIZE - 1)&&UART_GetRxBufferSize()){
UART_PutString("Print a char: ");
*pTail=UART_GetChar();
UART_PutChar(*pTail) ;
UART_PutString("\n") ;
pTail++;
}
if ((pTail - pHead) >= (SIZE-1)) {
*pTail = '\0' ;
tx_flag = 1 ; /* need to write out the buffer */
}
return(pTail); //Back to RxBufIndx the address of pTail
}
//Read the string
char* Reading(char* Buffer, char* pTail, char* pHead){
(void) Buffer ;
if ((pTail - pHead) > 0) { /* anything in the buffer? */
UART_PutString("Buffer reading procedure\n");
UART_PutString("Buffer keeps the following string: ");
UART_PutString(pHead);
UART_PutString("\n") ;
}
return(pHead); //Back to WxBufIndx the address of pHead
}
================================
The TeraTerm log was
moto
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
As It bothered me that I was not using tx_isr in my previous message I re-activated tx_isr.
======================================
#include "project.h"
#include<stdio.h>
#define SIZE 6
char str[128] ; /* print buffer */
int rx_flag = 0 ;
int tx_flag = 0 ;
int buffer_full = 0 ;
char* Filling(char* Buffer, char* pTail, char* pHead);
char* Reading(char* Buffer, char* pTail, char* pHead);
CY_ISR(Rx_isr_handling) { //ISR to check if a char was collect
Rx_isr_ClearPending() ;
rx_flag = 1 ;
}
CY_ISR(Tx_isr_handling){ //ISR to check if a char was sent
Tx_isr_ClearPending() ;
tx_flag = 1 ;
}
int main(void){
char Buffer[SIZE];
char* BufRxIndx=Buffer;
char* BufWxIndx=Buffer;
CyGlobalIntEnable;
UART_Start();
sprintf(str, "Uart test program (%s %s)\n", __DATE__, __TIME__ ) ;
UART_PutString(str) ;
Rx_isr_StartEx(Rx_isr_handling);
Tx_isr_StartEx(Tx_isr_handling) ;
for(;;){
if (rx_flag) { /* received a char */
if (buffer_full) {
UART_PutString("Buffer Over Run!\n") ;
} else {
BufRxIndx=Filling(Buffer, BufRxIndx, BufWxIndx);
rx_flag = 0 ;
}
}
if (tx_flag) { /* tx buffer empty */
if (buffer_full) { /* write only when the buffer is full */
BufWxIndx=Reading(Buffer, BufRxIndx, BufWxIndx);
BufRxIndx = Buffer ;
BufWxIndx = Buffer ;
buffer_full = 0 ;
tx_flag = 0 ;
}
}
}
}
//Get char by char and and build a string
char* Filling(char* Buffer, char* pTail, char* pHead){
(void) Buffer ;
while(((pTail - pHead) < SIZE - 1)&&UART_GetRxBufferSize()){
UART_PutString("Print a char: ");
*pTail=UART_GetChar();
UART_PutChar(*pTail) ;
UART_PutString("\n") ;
pTail++;
}
if ((pTail - pHead) >= (SIZE-1)) {
*pTail = '\0' ;
buffer_full = 1 ; /* need to write out the buffer */
}
return(pTail); //Back to RxBufIndx the address of pTail
}
//Read the string
char* Reading(char* Buffer, char* pTail, char* pHead){
(void) Buffer ;
if ((pTail - pHead) > 0) { /* anything in the buffer? */
UART_PutString("Buffer reading procedure\n");
UART_PutString("Buffer keeps the following string: ");
UART_PutString(pHead);
UART_PutString("\n") ;
}
return(pHead); //Back to WxBufIndx the address of pHead
}
======================================
moto
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you, you really help me.
First of all, please note that if you pass a pointer to a function
the function can modify the contents of the pointer,
but the function can not update the value of the pointer itself.
So with calling
BufRxIndx=Filling(Buffer, BufRxIndx, BufWxIndx);
BufWxIndx will not be updated, even if you assign a new value to BufWxIndx.
Similarly you can not modify the value of BufRRxIndex by calling
BufWxIndx=Reading(Buffer, BufRxIndx, BufWxIndx);
I don't understand if you mean this kind of mistake:
if ((pTail - pHead) > 0) { /* anything in the buffer? */
UART_PutString("Buffer reading procedure\n");
UART_PutString("Buffer keeps the following string: ");
UART_PutString(pHead);
UART_PutString("\n") ;
return(pHead); //Back to Wx
1.In the conditional expression I could use pTail-Buffer instead of pTail-pHead because it's the same
2.return(pHead) it shouldn't make no sense because pHead points to Buf and never change is address
3.In my previous code I wrote in the beggining of each function:
pTail=Buffer;
pHead= Buffer;
because this is already done by calling the function(handle)
Maybe I'm getting wrong.
In your code I dont understand why you use void Buffer; if It points always to char.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
So at first I thought you were trying to manage a ring buffer,
but it turned out that only pTail is moving and pTail is always at the Buffer,
then story is much simpler as you already aware of.
I wanted to warn you that your assigning value to pTail or pHead will not
affect the pointer out side the function.
So anyway it was not a problem.
The second thing about (void) Buffer, this is a hint for the compiler
that although I have the variable in the function argument,
it will not be used inside so please don't flag warning.
If you don't mind/care about waring "unused variable", you can safely remove that line.
moto