- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi again,
I already posted a topic about nios ii altera ans esp8266, I fixed all my problems with the esp8266, now i still struggling with my uart C code, my transmit function works well, but with my receive function its always a mess, its sometimes only if I don't use it, I just tried to return from my receive function two values: the received message and its size, I tried the following code but it does not work at all:
void uart_write(char* comm)
{
message* mess ;
int count=0;
strcpy(mess->send,comm);//command
mess->lenS=strlen(mess->send);//command length
for (count=0;count<mess->lenS;count++)
{
if(ALTERA_AVALON_UART_STATUS_TRDY_MSK==TRDY)// check if transmit ready
{
IOWR_ALTERA_AVALON_UART_TXDATA(UART_BASE,mess->send);//send character
Delay(600);//wait the end of transmission
}
}
if (ALTERA_AVALON_UART_STATUS_TRDY_MSK == TRDY)// check if transmit ready
IOWR_ALTERA_AVALON_UART_TXDATA(UART_BASE,0X0D);// \r
if (ALTERA_AVALON_UART_STATUS_TRDY_MSK == TRDY)// check if transmit ready
IOWR_ALTERA_AVALON_UART_TXDATA(UART_BASE,0x0A);// \n
}
void receive(char *buffer, int size){
alt_u8 status;
status= IORD_ALTERA_AVALON_UART_STATUS(UART_BASE);
while((status & RRDY)==RRDY ){ // check if reception ready{
buffer=IORD_ALTERA_AVALON_UART_RXDATA(UART_BASE);//receive character
status= IORD_ALTERA_AVALON_UART_STATUS(UART_BASE); //update status register
size++;
}
}
int main()
{
uart_init();
int size=0;
char *buffer;
uart_write("AT+CWLAP");
receive(&buffer,size);
printf("%d \n %s",size,buffer);
return 0; }
please guys I need help I'm lost Thanks a lot
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Why don't you use IRQ for flagging reception ? I presume that the printf("%d \n %s",size,buffer) command is preventing the program to correctly receive incoming new data. Anyway, where is the infinite loop, supposed while(1), in your code ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi, I changed my receive function into the following one, but when I use those receive/transmit function too many times in the same main, I keep receive strange things in my nios II console.
int receive(char *buffer){
int size=0,j=10000000;
alt_u8 status;
while(j){
status= IORD_ALTERA_AVALON_UART_STATUS(UART_BASE);
while(status & RRDY){ // check if reception ready{
buffer=IORD_ALTERA_AVALON_UART_RXDATA(UART_BASE);//receive character
status= IORD_ALTERA_AVALON_UART_STATUS(UART_BASE); //update status register
size++;
}
j--;
}
return size;
}
int main () {
char *buffer=malloc(200);
int size=0;
size=receive(buffer);
printf("%d\n%s",size,buffer);
return 0;
}
usually I use those functions in another C file. I don't need an infinite loop in my function, because I have to use my receive/transmit over 10 times in the main function.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
you are not writing to the buffer correctly. You need to increment the buffer index as well as check that you are not attempting to write outside the bounds of the allocated memory
if(size < 200)
buffer[size] = IORD_ALTERA_AVALON_UART_RXDATA(UART_BASE);//receive character
else
//buffer full
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello Ayelha,
I do not completely understand what you are trying to do. But, assuming you will be running the main function multiple times, as you say it, you might have to do with a memory leak: every time you circle through the main function, you allocate 200 bytes from the heap. It is good practice to release these bytes (malloc - free). If you do not to this, you could run out of memory after a while. Then you will get unstable program behaviour. Some other thing you might want to take a look at: in Your send function, there could be an error: if you want to test a flag in a register use ((value & mask) = mask) i do have the impression that you have written something else. This would explain while you need a delay in your function where as, under normal circumstances, you do need wait functions if you are using the status flags of the uart. Wait functions are not good for optimal performance and baudrate independency. Best Regards, Johi.- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page