How to control endless loop's cycle duration in PSOC 5LP ?

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
HaHa_285681
Level 3
Level 3
First like given

 Please help me to find some solution.

   
    I have this loop:     
   
     for(;;)   
   
        {                 
   
        
   
            HM_step();   
   
            val=HM_Y.ECG;        
   
        
   
            uint8 val2;        
   
     val2=(int)(val)*1+35;   
   
        
   
     while(USBUART_1_CDCIsReady() == 0u);   
   
                            USBUART_1_PutData(&val2,1);        
   
        
   
        }    
   
    and it works with 1 kHrz frquency (cycle duration is 1miliSec. ). Can I decrease this cycle duration until 10 microSec. ?    
   
    If yes how?   
   
        
   
    Thanks for help.   
0 Likes
20 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

To shorten the time you spend you'll have to analyze every statement.

   

I do not know what hides behind your function HM_step() nor what the declaration of val looks like.

   

Most of the time will be spent in the while waiting for the USBUART getting ready for a new transmit. This is a serial device, and even when sending one byte only that will take a remarkable time span to accomplish that.

   

So you'll have to think over your communication part to speed up the main-loop.

   

 

   

Bob

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

One approach might be to move the USBUART handling outside the loop

   

using DMA and a circular buffer. That way it takes care of itself.

   

 

   

So one approach wirte data to circular buffer using pointer, inc pointer, generate

   

DMA request with a status register,and leave the rest to the DMA process. Or use

   

a combination of verlilog and DMA to automate the serial comm channel using

   

Verilog to generate the DMA request.

   

 

   

    

   

          

   

http://www.cypress.com/?rID=37793     AN52705

   

 

   

http://www.cypress.com/?docID=44514     AN84810

   

 

   

 

   

Regards, Dana.

0 Likes
HaHa_285681
Level 3
Level 3
First like given

 Yes exactly, I have some counting functions inside HM_Step() function, but duration for all of them less than 0.0001 sec, so I will delete communication part (USBUART) and I will organize it by another way.  And also please which possibilities are for decreasing counting step duration?  ( is it possible with PWM ?)

   

Regards,

   

Hakob

0 Likes
HaHa_285681
Level 3
Level 3
First like given

 Thanks for DMA, it is very cool tool but it is not suitable for my case (I have only one function generated by another program and I need to use it).

   

Regards,

   

Hakob

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Yes, you can always count with HW to speed things up. In fact

   

you can use the DFB Assembler and the DFB to create a very

   

fast math solution. Take a look at -

   

 

   

    

   

          http://www.cypress.com/?rID=60720

   

 

   
            uint8 val2;        
   
     val2=(int)(val)*1+35;   
   
        
   

What is the 2ond statement doing other than assigning a constant to val2, or am I misreading

   

the typing ?

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

I think another important point that you should think about is the fact that you are using a USB_UART device, so though it runs through USB (which has speeds faster than the internal PSoC clock) it is using a UART protocol to send data, which has a lot more limitations on its speed/baud rate.  Be wary of the fact that as you load more and more data into the transmit buffer of the USB_UART, that data may not be able to be sent as quickly as your computations are completing.  The fastest data rate possible will be your baud rate divided by 10, assuming 1 start bit 8 data bits and 1 stop bit, since baud rate is rated by 'bit' not by 'byte'.

   

 

   

Perspective: According to the datasheet the default baud rate of the USB_UART_CDC is 115200 baud, which after doing the math will allow you to transfer your data about every 87 microseconds.  This means you can only have the USB_UART transfer data every 87 microseconds if you don't want to overrun your buffer.  To put this in perspective, assuming you are using the default clock of 24MHz on the PSoC, 24MHz = 24000000 cycles/second 115200 baud = 115200 cycles/second (here I use cycles just for convenience of comparison).  24000000/115200 = 208, which means your PSoC has completed 208 cycles before the UART has completed 1, this is why you get the buffer overflow.  I hope I didn't bore you haha.

0 Likes
HaHa_285681
Level 3
Level 3
First like given

 Dear Danna in this case val is double for sending it by USB Uart I transform it to integer so val2 is transformed val and then again I retransform val2 to double in PC.

   

For DFB I understand it is alternative functions and can be programmed very easy right?.

   

 

   

regards,

   

Hakob 

0 Likes
HaHa_285681
Level 3
Level 3
First like given

 Dear barney3113 anyway I will delete USB UART from my project. Does the For(;;) cycle's speed depend from steps number inside too or only from clock value?

   

 

   

regards,

   

Hakob

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

The time spent inside the main-loop depends on

   

1st. The CPU usage. Number of instructions executed within the loop, not counted in C-lines but in assembly instructions (which can take different number of clock cycles to complete)

   

2nd. The main clock frequency: this is adjustable in the clocks tab of the cydwr-file and usually ranges between 24 .. to 66MHz, some devices up to 80MHz.

   

What is your intention to ask for the main-loop speed? Can you tell us a bit more about your project?

   

 

   

Bob

0 Likes
HaHa_285681
Level 3
Level 3
First like given

 Dear Bob I think your answers enough for me to continue my work.

   

I want to develop some human heart model for testing pacemakers.

   

The problem is I am a new in this area also in C programming. I have possibility to make my model in Simulink Stateflow and then generate C code for ARM Cortex processors so I need real time model in PSOC 5 LP  but with this generated code.

   

Generated code has number of counting process and variables changing by situation.

   

I can call from mine loop only one function in this case HM_Step() and get outputs I need.

   

So I think if I will have this function with 0.000001 step and also my loop will have the same step I can put CyDelay( x ) deleay and by changing x variables value calibrate my loop duration untill 0.0001. In this way my loop duration generaly will depeand from x.

   

 I hope I can have 0.01 ACCURACY. 

   

With USB UART comunication I couldn't have 0.000001 step (it was only 0.001).

   

 

   

regards,

   

Hakob

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

A PSoC is a bit more than just an ARM processor. Since a lot of the required signal processing can be done with the built-in hardware-components that a PSoC5 contains, it might be possible to have a slow main-loop that runs at a constant frequency of, say, 100Hz. Your project will consist of analog processing parts, digital signal processing, communication with (some) different devices and (probably) a human interface. Many of this tasks can be burdened to the hardware, so it does not need any main-loop time. Even the "creation" of new hardware by programming in HDL (Hardware-Description Language) is a choice. Additionally there are 24 "DataPath" objects which contain a small programmable ALU (Arithmetic and Logical Unit) which all run independ from the CPU and from each other.Transfer of data may be done with DMA which does not use the CPU, too.

   

 

   

But all this will not get created by any code-generating system.

   

 

   

So you ought to start "Thinking PSoC" by understanding the availlable resources that are already packed into your PSoC5

   

 

   

Bob

0 Likes
HaHa_285681
Level 3
Level 3
First like given

 Step by step  )))))))))

   
        
   
    regards,   
   
    Hakob   
0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

You could also think in terms of using an RTOS -

   

 

   

    

   

          

   

http://www.cypress.com/?id=4&rID=57144

   

 http://en.wikipedia.org/wiki/Real-time_operating_system

   

 

   

 

   

In so far as using delays, just a suggestion, minimize them as they throw away CPU

   

horsepower. Consider instead using interrupt driven demand, that way you maximize

   

CPU resources.

   

 

   

Regards, Dana.

0 Likes
HaHa_285681
Level 3
Level 3
First like given

 Dear Dana, yes I will try to minimize my delays as possible.

   

Regards,

   

Hakob

0 Likes
lock attach
Attachments are accessible only for community members.
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Just as an example how a controlled loop can be made for a PSoC.

   

 

   

Bob

0 Likes
HaHa_285681
Level 3
Level 3
First like given

 I want to hold for loop duration 0.0001 sec (+, - 0.00001 sec). If I will have a delay in loop witch will 100 multiply bigger than my function's duration and "delay duration" + " function duration" will equal = 0.0001 sec  than I hope I will have it.

   

 

   

regards,

   

Hakob

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

If your requirement is to have such accurate timing, why not use a clock (running with 10kHz) which triggers an interrupt. In that ISR you then can do whatever you want, as long as it doesn't need less than 100 micro seconds. If you want to send something about an UART it needs to be at least 115200 baud, probably faster. I'm not sure whether you can be fast enough when using USB-UART - it might impose some additional overhead.

   

To test that: on the start of the ISR set a pin to high, at the end set it to low. With a scope (or a logic analyzer) you can than see how fast the code is.

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Keep in mind, unless the loop was built entirely of HW you have the

   

non-deterministic component of SW and async processes, like other

   

interrupts, to undo your precise timing loop. You can use a DSO, setup

   

on trigger conditions, to see if ever the timing loop fails repeatability in

   

time. Or plot a histogram of timing loop.

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

the best way to get a consistent sampling rate is as suggested by hli, do your reading/sampleing inside the interrupt and put that into a circular buffer.

   

transmitting of the data can be handled in the main loop, out side the interrupt, ( I would just transmite the raw data to PC, and let the PC handle the adustiment or scaling)

   

For the circular buffer, you need to make sure it will not overflow.

   

If you send data constinously non stop, your transmission must be at least equal to the sampleing rate( I woul make it 2-3 times faster at least), otherwise it would overflow.

   

If you only need to sample a few seconds, then your transmission speed can be slower then your sampling speed. But you must make sure that you have enough buffer to hold the data before sending.

0 Likes
HaHa_285681
Level 3
Level 3
First like given

 Thanks to all of you for comments, now I am waiting for DSO to continue the project.

   
    Regards,   
   
    Hakob   
0 Likes