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

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
Erik Malund
04/18/13 14:56
Modified:
  04/18/13 15:01

Read: 996 times
Mt Airy, Nc
USA


 
#189683 - I find code reuse in embedded, at best, cumbersome
Responding to: Joseph Hebert'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      Erik Malund      03/07/13 08:56      
   Have you looked at the TI M4 Cortex versions?      Richard Erlacher      03/07/13 11:13      
      re:TI      Jacob Boyce      03/07/13 11:20      
         One man's "Flexible" is another man's "Cumbersome"      Andy Neil      03/07/13 14:17      
            flexibility ....      Erik Malund      03/07/13 14:30      
               several menus to get a cuppa       Andy Neil      03/08/13 00:13      
            re: experience      Jacob Boyce      03/08/13 07:42      
            'Unwanted' flexibility      Oliver Sedlacek      03/18/13 06:31      
               well, there is some help      Erik Malund      03/18/13 11:04      
                  SiLabs AppBuilder      Andy Peters      04/16/13 15:29      
                     sam e with freescale      Erik Malund      04/16/13 15:40      
                     Same with Cypress PSoC Creator      Andy Neil      04/17/13 01:48      
                        same ...      Andy Peters      04/17/13 11:12      
         Certainly a wry smile is needed       Jim Granville      03/07/13 20:59      
            It gets worse      Andy Neil      04/17/13 01:52      
   develop all, even small, new apps in 32 bits      Joseph Hebert      04/17/13 06:48      
      how ??      Andy Peters      04/17/13 11:21      
         cost      Joseph Hebert      04/17/13 11:28      
            re: cost      Andy Peters      04/17/13 12:05      
               cost analysis      Joseph Hebert      04/17/13 16:00      
         Money matters more to some than others      Jim Granville      04/17/13 14:47      
            RE: ARMs following Stellaris into early retirement      Andy Neil      04/18/13 03:21      
               as will we all (eventually)      Jim Granville      04/18/13 06:11      
            ARM in small packages      Maarten Brock      04/23/13 05:23      
      Scope of "all"      Andy Neil      04/18/13 03:25      
         Scope of "all"      Joseph Hebert      04/18/13 12:48      
            RE: a hypothesis that will withstand scrutiny      Andy Neil      04/18/13 12:50      
         RE: Superlatives are greatly over-used these days      Andy Neil      04/18/13 12:51      
      Software costs      Oliver Sedlacek      04/18/13 07:52      
         code reuse      Joseph Hebert      04/18/13 12:51      
            I find code reuse in embedded, at best, cumbersome      Erik Malund      04/18/13 14:56      
               Maybe it's because I don't use HLL, but ...       Richard Erlacher      04/18/13 23:12      
            Devils triangle      Oliver Sedlacek      04/23/13 09:06      
               I choose      Mahmood Elnasser      04/23/13 09:33      
               except ...        Joseph Hebert      04/23/13 13:03      
            Including code that wasn't absolutely necessary ...      Richard Erlacher      04/23/13 15:35      

Back to Subject List