Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
04/18/13 20:56
Modified:
  04/18/13 21:01

Read: times


 
#189683 - I find code reuse in embedded, at best, cumbersome
Responding to: ???'s previous message
but for repeated tasks, I have things like this
////////////////////////////////////////////////
//
// circular buffer code template
//

//////////////////////////////////////////////
//


// dummies to make sample code compile
unsigned char YouStoreWhereItmustGo_ForIndependence = 0;
unsigned char ThereAreCharaftersToBeTransmittesd_ForIndependence = 0;
unsigned char TheCharafterToBeTransmittesd_ForIndependence = 0;
unsigned char WhereverTheHardwareStoresFetchedCharacter_ForIndependence = 0;
unsigned char WhereverTheHardwareRequiresCharacterToBeTransmitted_ForIndependence = 0;
// testing aids
unsigned char Test1 = 0;
unsigned char Test2 = 0;
//unsigned char Test3 = 0;
//unsigned char Test4 = 0;
void RxISR (void);
void TxISR (void);
// constants and variables
//
#define FALSE 0
#define TRUE 1
#define CIRCULAR_BUFFER_SIZE 3 // if changed to >255 RxBufFetch and RxBufStore must be changed to int
unsigned char RxBuffer[CIRCULAR_BUFFER_SIZE];
unsigned char TxBuffer[CIRCULAR_BUFFER_SIZE];
unsigned char RxBufFetch ;
unsigned char RxBufStore ;
unsigned char TxBufFetch ;
unsigned char TxBufStore ;
unsigned char RxBufferFull;
unsigned char TxBufferFull;
unsigned char TransmitInProgress;

void main(void)
{
// some initialization  
// initialize the circular buffers
RxBufFetch = 0;
RxBufStore = 0;  
TxBufFetch = 0;
TxBufStore = 0;
RxBufferFull = FALSE ;
TxBufferFull = FALSE ;
  
// in main loop
	while(1)
  {
  // ........
  // process fetched characters
    while ( (RxBufFetch |= RxBufStore) | RxBufferFull)
    {
      YouStoreWhereItmustGo_ForIndependence =  RxBuffer[RxBufFetch];
      RxBufFetch++;
      RxBufferFull = FALSE;
    }
  //...........
  // process charafters to be transmittesd
    if (ThereAreCharaftersToBeTransmittesd_ForIndependence)
    {
      if(TxBufferFull)
      { // a character may have been transmitted sice last 'visit here'
        if (TxBufStore == TxBufFetch)
        {
        // take apropiate action
        }
      }
      else
      {
        // here there is a slight chance that a fast transmit and a main holdup will cause problems
        // thus here disable transmit interrupt
        TxBuffer[RxBufStore] = TheCharafterToBeTransmittesd_ForIndependence;
        TxBufStore++;
        TxBufStore %= CIRCULAR_BUFFER_SIZE; // if size is a power of two an 'and' is more efficient here 
        if (TxBufStore == TxBufFetch)
        {
           TxBufferFull = TRUE;
        }
      // here reenable the transmit interrupt  
        if (!TransmitInProgress)
        {
          WhereverTheHardwareRequiresCharacterToBeTransmitted_ForIndependence = RxBuffer[RxBufFetch];
          TxBufFetch++;
          TxBufFetch %= CIRCULAR_BUFFER_SIZE; // if size is a power of two an 'and' is more efficient here 
          TransmitInProgress = TRUE;
        }
      }
    }
    
    if (Test1) RxISR();
    if (Test2) TxISR();
  } // end workloop 
}


//////////////////////////////////////////
//
// Code in Receive ISR OR part of UART ISR conditioned by Char available

void RxISR (void)
{
  if (RxBufferFull)
  {
    //You are hosed
  }
  RxBuffer[RxBufStore] = WhereverTheHardwareStoresFetchedCharacter_ForIndependence;
  RxBufStore++;
  RxBufStore %= CIRCULAR_BUFFER_SIZE; // if size is a power of two an 'and' is more efficient here 
  if (RxBufStore  == RxBufFetch)
  {
     RxBufferFull = TRUE;
  }
}


//////////////////////////////////////////
//
// Code in Transmit ISR OR part of UART ISR conditioned by transmit buffer empty

void TxISR (void)
{
  if ((TxBufFetch != TxBufStore) | TxBufferFull)
  {
    WhereverTheHardwareRequiresCharacterToBeTransmitted_ForIndependence = TxBuffer[TxBufFetch];
    TxBufFetch++;
    TxBufFetch %= CIRCULAR_BUFFER_SIZE; // if size is a power of two an 'and' is more efficient here 
    TxBufferFull = FALSE;
  }
  else  
  {
    TransmitInProgress = FALSE;
  }
}


note that - with the "dummies to make sample code compile" and the test flags, this code is compiled and teasted, not just "written and let what fails fail"

Erik

List of 36 messages in thread
TopicAuthorDate
8/32 bit musings            01/01/70 00:00      
   Have you looked at the TI M4 Cortex versions?            01/01/70 00:00      
      re:TI            01/01/70 00:00      
         One man's "Flexible" is another man's "Cumbersome"            01/01/70 00:00      
            flexibility ....            01/01/70 00:00      
               several menus to get a cuppa             01/01/70 00:00      
            re: experience            01/01/70 00:00      
            'Unwanted' flexibility            01/01/70 00:00      
               well, there is some help            01/01/70 00:00      
                  SiLabs AppBuilder            01/01/70 00:00      
                     sam e with freescale            01/01/70 00:00      
                     Same with Cypress PSoC Creator            01/01/70 00:00      
                        same ...            01/01/70 00:00      
         Certainly a wry smile is needed             01/01/70 00:00      
            It gets worse            01/01/70 00:00      
   develop all, even small, new apps in 32 bits            01/01/70 00:00      
      how ??            01/01/70 00:00      
         cost            01/01/70 00:00      
            re: cost            01/01/70 00:00      
               cost analysis            01/01/70 00:00      
         Money matters more to some than others            01/01/70 00:00      
            RE: ARMs following Stellaris into early retirement            01/01/70 00:00      
               as will we all (eventually)            01/01/70 00:00      
            ARM in small packages            01/01/70 00:00      
      Scope of "all"            01/01/70 00:00      
         Scope of "all"            01/01/70 00:00      
            RE: a hypothesis that will withstand scrutiny            01/01/70 00:00      
         RE: Superlatives are greatly over-used these days            01/01/70 00:00      
      Software costs            01/01/70 00:00      
         code reuse            01/01/70 00:00      
            I find code reuse in embedded, at best, cumbersome            01/01/70 00:00      
               Maybe it's because I don't use HLL, but ...             01/01/70 00:00      
            Devils triangle            01/01/70 00:00      
               I choose            01/01/70 00:00      
               except ...            01/01/70 00:00      
            Including code that wasn't absolutely necessary ...            01/01/70 00:00      

Back to Subject List