CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Second instance of printf function?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
notbad



Joined: 10 Jan 2013
Posts: 68

View user's profile Send private message

Second instance of printf function?
PostPosted: Sun Jun 01, 2014 1:16 am     Reply with quote

Hi
As you know, there is a solution for stopping the compiler from disabling interrupts for delay functions(here) but I couldn't find one for printf.

I tried:
Code:

#use rs232(BAUD=9600,UART1,ERRORS)
#int_xxx
void xxx_ISR(void)
{
  printf("a");
}

#use rs232(BAUD=9600,UART1,ERRORS)
void main()
{
  printf("b");
}


and:
Code:

#use rs232(stream=isr,BAUD=9600,UART1,ERRORS)
#use rs232(stream=maincode,BAUD=9600,UART1,ERRORS)
#int_xxx
void xxx_ISR(void)
{
  fprintf(isr,"a");
}

#use rs232(BAUD=9600,UART1,ERRORS)
void main()
{
  fprintf(maincode,"b");
}


My CCS version is 4.130.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sun Jun 01, 2014 1:56 am     Reply with quote

1) Why do you even want to printf in an ISR?
2) Exactly what do you want to happen?

Mike
notbad



Joined: 10 Jan 2013
Posts: 68

View user's profile Send private message

PostPosted: Sun Jun 01, 2014 2:41 am     Reply with quote

Thanks Mike
The Interrupt is simply an INT_TIMER2 and counts a timeout variable and if code is stuck somewhere (in an external eeprom routine) it prints something and halts(while(true);) right there in the isr. I know this is not good practice but I was hoping to find a way to do it this way.
Ttelmah



Joined: 11 Mar 2010
Posts: 19553

View user's profile Send private message

PostPosted: Sun Jun 01, 2014 2:53 am     Reply with quote

If you must print in an ISR, then don't!....

Use interrupt driven buffered routines instead.
This lets the characters actually transmit when there is space in the UART to take them, and allows the interrupt to just update data to the buffer.
Code:

#define T_BUFFER_SIZE 64
byte t_buffer[T_BUFFER_SIZE];
byte t_next_in = 0;
byte t_next_out = 0;
#define INC_PTR(x) if (++x == T_BUFFER_SIZE) x=0

#int_tbe
void serial_isr() {
   if(t_next_in!=t_next_out)
   {
      putc(t_buffer[t_next_out]);
      INC_PTR(t_next_out);
   }
   else
      disable_interrupts(int_tbe);
}

#bit GIE=getenv("BIT:GIE")

void bputc(char c) {
   //buffered putc to call in the main code.
   int1 GINT;
   GINT=GIE;
   disable_interrupts(GLOBAL);
   t_buffer[t_next_in]=c;
   INC_PTR(t_next_in);
   if (t_next_in==t_next_out);
      INC_PTR(t_next_out);
   if (GINT)
      enable_interrupts(GLOBAL); //interrupts will be re-enabled
   //if they were enabled when the routine was called - not otherwise.
   enable_interrupts(int_tbe);
}

void int_bputc(char c)
{
   //Buffered putc to call inside the interrupt
   t_buffer[t_next_in]=c;
   INC_PTR(t_next_in);
   if (t_next_in==t_next_out);
      INC_PTR(t_next_out);
   enable_interrupts(int_tbe);
}


Then call int_bputc for your printing in the interrupt, and bputc for the prints outside.

Key differences here is that unlike the CCS bputc, these will throw away characters if the buffer gets full, rather than waiting. If the buffer is larger than any message you might send, this is not a problem. They will also work efficiently for non binary buffer sizes. Because interrupts are disabled in the bputc version, they won't interfere with one another, except insofar as data could be inserted by the interrupt routine in the middle of a message being generated in the main code. If you don't want this to happen, then just disable_interrupts(GLOBAL) before calling the bputc, and re-enable it afterwards. This way the message from 'main' will always be unbroken. Since it only takes uSec to add a message to the buffer, this is generally OK.
notbad



Joined: 10 Jan 2013
Posts: 68

View user's profile Send private message

PostPosted: Sun Jun 01, 2014 6:33 am     Reply with quote

Hi Ttelmah. Thanks.
I tested your code and it works goodly. I disabled GIE after buffer is all out.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group