<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CY8CKIT-044 &#8211; IoT Expert</title>
	<atom:link href="https://iotexpert.com/category/devkits/cy8ckit-044/feed/" rel="self" type="application/rss+xml" />
	<link>https://iotexpert.com</link>
	<description>Engineering for the Internet of Things</description>
	<lastBuildDate>Tue, 22 May 2018 00:17:40 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://iotexpert.com/wp-content/uploads/2017/01/cropped-Avatar-32x32.jpg</url>
	<title>CY8CKIT-044 &#8211; IoT Expert</title>
	<link>https://iotexpert.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Matrix Orbital GTT43A: PSoC 4 Interface</title>
		<link>https://iotexpert.com/matrix-orbital-gtt43a-psoc-4-interface/</link>
					<comments>https://iotexpert.com/matrix-orbital-gtt43a-psoc-4-interface/#respond</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Thu, 24 May 2018 12:00:10 +0000</pubDate>
				<category><![CDATA[CY8CKIT-044]]></category>
		<category><![CDATA[Matrix Orbital]]></category>
		<category><![CDATA[PSoC 4200]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=5143</guid>

					<description><![CDATA[Summary In the last several articles I have written about how to use and talk to the Matrix Orbital GTT43A.  Now it is time to write some code.  The PSoC4 program that I am going to show you has evolved over time as I added stuff to it.  For instance, while I was working on [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>In the last several articles I have written about how to use and talk to the <a href="https://www.matrixorbital.com/gtt43a" target="_blank" rel="noopener">Matrix Orbital GTT43A</a>.  Now it is time to write some code.  The PSoC4 program that I am going to show you has evolved over time as I added stuff to it.  For instance, while I was working on this program I ran into a problem where the I2C Bus would hang (the subject of the next article).  As such, some of the code that is in this program was written to help me debug that problem.  With that said, I wanted a program that:</p>
<ol>
<li>Is command line driven i.e. I can interact with my program via serial commands through the PC COM Port</li>
<li>Can read 1 byte at a time (like I do on the bridge control panel)</li>
<li>Can test the &#8220;read whole packet&#8221; code</li>
<li>Can selectively send commands via a I2C or the UART</li>
<li>Send test commands to the display e.g. reset, clear</li>
<li>Test the display generated messages (like button presses)</li>
</ol>
<p>All of this code was built to run on the <a href="http://www.cypress.com/documentation/development-kitsboards/cy8ckit-044-psoc-4-m-series-pioneer-kit" target="_blank" rel="noopener">CY8CKIT-044</a> which has a PSoC 4200M MCU</p>
<h1>Schematic &amp; Pin Assignment</h1>
<p>All PSoC 4 projects start with a schematic.  In my schematic I have a UART setup to talk to the KitProg (called UART) and serve as the command processor, an I2C which directly attaches to the I2C bus that drives the display, and a UART that is also attached to the display which I called SCRUART.</p>
<p>The I2CFAIL pin in the schematic I used to help me debug the I2C problem (the subject of the next article)</p>
<p><a href="https://iotexpert.com/?attachment_id=5254" rel="attachment wp-att-5254"><img fetchpriority="high" decoding="async" src="https://iotexpert.com/wp-content/uploads/2018/05/Screen-Shot-2018-05-21-at-12.29.07-PM.png" alt="PSoC 4200m Schematic" width="347" height="349" class="alignnone wp-image-5254 size-full" srcset="https://iotexpert.com/wp-content/uploads/2018/05/Screen-Shot-2018-05-21-at-12.29.07-PM.png 347w, https://iotexpert.com/wp-content/uploads/2018/05/Screen-Shot-2018-05-21-at-12.29.07-PM-100x100.png 100w, https://iotexpert.com/wp-content/uploads/2018/05/Screen-Shot-2018-05-21-at-12.29.07-PM-150x150.png 150w, https://iotexpert.com/wp-content/uploads/2018/05/Screen-Shot-2018-05-21-at-12.29.07-PM-298x300.png 298w" sizes="(max-width: 347px) 100vw, 347px" /></a></p>
<p>The pin assignment has the UART attached to the KitProg UART, the I2C attached to KitProg and the Display.  The SCRUART is attached to UART on the Display.</p>
<p><a href="https://iotexpert.com/?attachment_id=5253" rel="attachment wp-att-5253"><img decoding="async" src="https://iotexpert.com/wp-content/uploads/2018/05/Screen-Shot-2018-05-21-at-12.29.18-PM.png" alt="PSoC 4200M Pin Assignment" width="456" height="204" class="alignnone wp-image-5253 size-full" srcset="https://iotexpert.com/wp-content/uploads/2018/05/Screen-Shot-2018-05-21-at-12.29.18-PM.png 456w, https://iotexpert.com/wp-content/uploads/2018/05/Screen-Shot-2018-05-21-at-12.29.18-PM-300x134.png 300w" sizes="(max-width: 456px) 100vw, 456px" /></a></p>
<p>&nbsp;</p>
<h1>Main Event Loop</h1>
<p>The main event loop has the following parts</p>
<ol>
<li>Read a character from the keyboard</li>
<li>Process the keyboard command character</li>
<li>Read data from the screen
<ol>
<li>If you are in packet mode read a whole packet</li>
<li>If you are in streaming mode read one byte</li>
</ol>
</li>
</ol>
<p>There are three system modes.</p>
<ol>
<li>IDLE = Dont read from the screen</li>
<li>PACKET = Read whole packets (poll for complete packets)</li>
<li>STREAMING = read bytes (polling)</li>
</ol>
<p>I also have setup the program to read/write bytes to the UART and I2C.  This is called the &#8220;comInterface&#8221;.</p>
<p>The enumerated type systemMode is used to setup the polling mode (each time through the main loop, what does it do?).</p>
<pre class="start-line:18 lang:c decode:true">typedef enum {
    MODE_IDLE,
    MODE_PACKET,
    MODE_STREAMING
} systemMode_t;

systemMode_t systemMode=MODE_IDLE;

typedef enum {
    INTERFACE_I2C,
    INTERFACE_UART
} cominterface_t;

cominterface_t comInterface=INTERFACE_I2C;
</pre>
<p>The actual main section of the code</p>
<ol>
<li>Starts by initializing the UART, SCRUART and I2C.</li>
<li>On line 299 it reads a character, then switches on the character to figure out which command the user has type.</li>
</ol>
<p>You can see that &#8216;u&#8217; and &#8216;U&#8217; change the communication interface from UART to I2C and back.</p>
<pre class="start-line:285 lang:c decode:true">int main(void)
{
    CyGlobalIntEnable; /* Enable global interrupts. */
    uint32 returncode;
    
    UART_Start();
    UART_UartPutString("Started\r\n");
    I2C_Start();
    SCRUART_Start();
    
    char c;
     
    for(;;)
    {
        c = UART_UartGetChar();
        switch(c)
        {
            case 0:
            break;
            
            case 'u':
                UART_UartPutString("I2C Mode\r\n");
                comInterface = INTERFACE_I2C;
            break;
            
            case 'U':
                UART_UartPutString("UART Mode\r\n");
                comInterface = INTERFACE_UART;
            break;
</pre>
<p>The end of the loop handles the &#8220;?&#8221; case&#8230; in other words print out all of the commands.  Then based on the system mode, it either reads a whole message packet from the display or it reads only byte.</p>
<pre class="start-line:398 lang:c decode:true">           case '?':
                UART_UartPutString("------Communication Mode------\r\n");
                UART_UartPutString("u\tI2C Mode\r\n");
                UART_UartPutString("U\tUART Mode\r\n");
                
                UART_UartPutString("------GTT43A Commands------\r\n");
                UART_UartPutString("N\tDefault Comm None\r\n");
                UART_UartPutString("I\tDefault Comm I2C\r\n");
                UART_UartPutString("S\tDefault Comm Serial\r\n");
                
                UART_UartPutString("e\tEcho abc\r\n");
                UART_UartPutString("R\tReset\r\n");
                UART_UartPutString("c\tSend Clear Screen\r\n");
                
                UART_UartPutString("------Communcation Commands------\r\n");
                UART_UartPutString("r\tRead one byte if IDLE\r\n");
                UART_UartPutString("p\tRead Packet if IDLE\r\n");
                
                UART_UartPutString("------System Mode------\r\n");
                UART_UartPutString("0\tTurn I2C polling off \r\n");
                UART_UartPutString("1\tTurn on I2C packet polling\r\n");
                UART_UartPutString("2\tRead i2c bytes \r\n");
                
                UART_UartPutString("------I2C Debugging------\r\n");
                UART_UartPutString("s\tPrint SCB Status\r\n");
                UART_UartPutString("z\tSend I2C Reset Sequence\r\n");
                UART_UartPutString("x\tPrint I2C SCL and SDA value\r\n");
            break;
        }
        
        switch(systemMode)
        {
            case MODE_IDLE:
            break;
            
            case MODE_PACKET:
                readPacket();
            break;
                
            case MODE_STREAMING:
                readByte();
            break;
        }</pre>
<p>I created three keys (0,1,2) to change the system mode, from IDLE, to reading whole packets to reading bytes.</p>
<pre class="start-line:361 lang:c decode:true ">            // System Modes
            case '0':
                    I2CFAIL_Write(0);
                    UART_UartPutString("Packet Poling Off\r\n");
                    systemMode = MODE_IDLE;
                break;
               
            case '1':
                    I2CFAIL_Write(0);
                    UART_UartPutString("Packet Poling On\r\n");
                    systemMode = MODE_PACKET;
            break;
            case '2':
                    I2CFAIL_Write(0);
                    UART_UartPutString("Read continuous\r\n");
                    systemMode = MODE_STREAMING;
                    break;
</pre>
<p>While I was trying to figure out how things worked I wanted to be able to do one thing at a time. So I create &#8216;r&#8217; to read one byte (like Bridge Control Panel) and &#8216;p&#8217; to read a whole packet.  Notice that you really really only want to do this why you are not polling the display.</p>
<pre class="start-line:347 EnlighterJSRAW" data-enlighter-language="c" ">            // If you are IDLE you can read 1 byte with 'r' or read a whole packet with 'p'
            case 'r':  // Read byte
                if(systemMode != MODE_IDLE)
                    break;
                readByte(&amp;data);
                
            break;
            case 'p': // read packet
                if(systemMode == MODE_IDLE)
                    readPacketI2C();
            break;
</pre>
<p>The last section of commands send various GTT2.0 commands to the display.  Notice that the writePacket function knows which system interface to use (either I2C or UART).</p>
<p>First, I declare some commands, just an array of bytes.</p>
<pre class="start-line:6 lang:c decode:true ">// These commands come the GTT 2.0 and GTT2.5 Protocol Manuals
uint8 clearCMD[] = { 0x58 };
uint8 resetCMD[] = { 0x01};
uint8 comI2CCMD[] = { 0x05, 0x02};
uint8 comNONECMD[] = { 0x05, 0x00};
uint8 comSERIALCMD[] = { 0x05, 0x01};
uint8 comECHOCMD[] = {0xFF,'a','b','c', 0};
</pre>
<p>Then I use them:</p>
<pre class="start-line:318 EnlighterJSRAW" data-enlighter-language="c" ">            case 'e':
                UART_UartPutString("Send Echo Command\r\n");
                writePacket(sizeof(comECHOCMD) , comECHOCMD);
            break;
                
            case 'c':
                UART_UartPutString("Sent Clear String\r\n");
                writePacket(sizeof(clearCMD),clearCMD);
            break;
            case 'R':
                UART_UartPutString("Sent Reset String\r\n");
                writePacket(sizeof(resetCMD),resetCMD);
            break;
            case 'I':
                UART_UartPutString("I2C Communcation Channel\r\n");
                writePacket(sizeof(comI2CCMD),comI2CCMD);
            break;
            case 'N':
                UART_UartPutString("NONE Communcation Channel\r\n");
                writePacket(sizeof(comNONECMD),comNONECMD);
            break;</pre>
<h1>Read Byte</h1>
<p>In order to read one byte from the display I first determine which mode Im in, then call the appropriate sub-function.</p>
<pre class="start-line:269 lang:c decode:true ">uint32_t readByte(uint8_t *data)
{
    uint32_t returncode=0;
    switch(comInterface)
    {
        case INTERFACE_I2C:
            returncode = readByteI2C(data);
        break;
        case INTERFACE_UART:
            returncode = readByteUART(data);
        break;
    }
    sprintf(buff,"Returncode = %X Data=%d\r\n",(unsigned int)returncode,*data);
    UART_UartPutString(buff);

    return returncode;
}</pre>
<p>The first sub function to read bytes via I2C.  To read a byte with no error checking you have to</p>
<ol>
<li>Send a Start</li>
<li>Send the address</li>
<li>Send the Read bit</li>
<li>Clock it 8 times (this is exactly what the I2CMasterReadByte function does)</li>
<li>Send an NAK</li>
<li>Send a Stop</li>
</ol>
<p>I ran into an I2C issue which I will talk about in the next article, however, if I see an error from any of these commands Ill put the system into MODE_IDLE and throw an error.  In addition I write a 1 to the I2CFAIL pin, which I am using to trigger the Oscilliscope (so I can see what is happening)</p>
<pre class="start-line:221 lang:c decode:true ">uint32_t readByteI2C(uint8_t *data)
{
    uint32 returncode;
    returncode = I2C_I2CMasterSendStart(I2CADDR,I2C_I2C_READ_XFER_MODE , I2CTIMEOUT);
    if(returncode)
    {
        sprintf(buff,"send start error %lX status %lX\r\n",returncode,I2C_I2CMasterStatus());
        UART_UartPutString(buff);
        I2CFAIL_Write(1);
        systemMode = MODE_IDLE;
        goto cnt;
    }
            
    returncode = I2C_I2CMasterReadByte(I2C_I2C_ACK_DATA,data,I2CTIMEOUT);
    if(returncode)
    {
        sprintf(buff,"read byte error %lX status %lX sda=%d scl =%d\r\n",returncode,I2C_I2CMasterStatus(),I2C_sda_Read(),I2C_scl_Read());
        UART_UartPutString(buff);
        I2CFAIL_Write(1);
        systemMode = MODE_IDLE;
        goto cnt;
    }
            
    returncode = I2C_I2CMasterSendStop(I2CTIMEOUT);
    if(returncode)
    {
        sprintf(buff,"send stop error %lX status %lX\r\n",returncode,I2C_I2CMasterStatus());
        UART_UartPutString(buff);
        I2CFAIL_Write(1);
        systemMode = MODE_IDLE;
        goto cnt;
    }
    
    cnt:
    return returncode;
}
</pre>
<h1>Read Packet</h1>
<p>For the packet read code I did the same thing as the byte read code.  Specifically I wrote an overall get packet, then called the correct read packet based on the</p>
<p>If you read the source code that Matrix Orbital gives you for drivers, you will find that it reads one byte at a time.  The problem with doing this is that you</p>
<ol>
<li>Send a start</li>
<li>Send an I2C address</li>
<li>Send a read bit</li>
<li>Read the ACK</li>
<li>Read a byte</li>
<li>Send a NAK</li>
<li>Send a stop</li>
</ol>
<p>The problem with this approach is that it uses 11 bit-times extra per byte of overhead (steps 1-4) which kinda sucks.  So I wanted to write a complete packet reader.  My packet reader will</p>
<ol>
<li>Send a start</li>
<li>Send an I2C address</li>
<li>Send a read bit</li>
<li>Read the ACK</li>
<li>Read a byte  [This is the 254 that marks the start of the packet]</li>
<li>ACK</li>
<li>Read a byte [This is the command which identifies the packet]</li>
<li>ACK</li>
<li>Read a byte [The MSB of the Length]</li>
<li>ACK</li>
<li>Read a byte [The LSB of the Length]</li>
<li>NAK</li>
<li>If there is a length then:</li>
<li>Send the start</li>
<li>Send an I2C address</li>
<li>Send a read bit</li>
<li>Read the ACK</li>
<li>read length -1 bytes</li>
<li>ACK</li>
<li>Read the last byte</li>
<li>Send a NAK</li>
<li>Send a stop</li>
</ol>
<p>By spec you are supposed to NAK your last read byte to indicate that your read transaction is over&#8230; that means you have to NAK the last Length byte because there could be 0 bytes to read, in which case you would need to stop.  It would have been nice if the protocol let you send only one start, but Im pretty sure it was designed for UART, which doesn&#8217;t suffer from this problem.  Also as a side note, Im pretty sure that the MCU they are using doesn&#8217;t really care, but Im not willing to implement it incorrectly.</p>
<p>Here is the code:</p>
<pre class="start-line:93 lang:c decode:true ">void readPacketI2C()
{
    int length;
    int command;
    uint8_t data;
    uint32_t returncode;
    
    returncode = I2C_I2CMasterSendStart(I2CADDR,I2C_I2C_READ_XFER_MODE , I2CTIMEOUT);
    returncode |= I2C_I2CMasterReadByte(I2C_I2C_NAK_DATA,&amp;data,I2CTIMEOUT);
    returncode |= I2C_I2CMasterSendStop(I2CTIMEOUT);
    
    // Something bad happened on the I2C Bus ....
    if(returncode)
    {
        systemMode = MODE_IDLE; 
        sprintf(buff,"I2C Return Code %X\r\n",(unsigned int)returncode);
        UART_UartPutString(buff);
    }
 
    // The screen returns a 0 when there is nothing in the buffer.
    if(data == 0)
    {
        return;
    }

    // This is bad because there was something other than a packet start byte
    if(data != 252)
    {
        sprintf(buff,"bad data = %d\r\n",data);
        UART_UartPutString(buff);
        systemMode = MODE_IDLE; // put it into nothing mode...
        return;
    }
    
    // We know that we have a command
    returncode = I2C_I2CMasterSendStart(I2CADDR,I2C_I2C_READ_XFER_MODE , I2CTIMEOUT);
    returncode |= I2C_I2CMasterReadByte(I2C_I2C_ACK_DATA,&amp;data,I2CTIMEOUT); // command
    command = data;
    
    returncode |= I2C_I2CMasterReadByte(I2C_I2C_ACK_DATA,&amp;data,I2CTIMEOUT); // length
    length = data&lt;&lt;8;
    returncode |= I2C_I2CMasterReadByte(I2C_I2C_NAK_DATA,&amp;data,I2CTIMEOUT); // length
    length = length + data;
    returncode |= I2C_I2CMasterSendStop(I2CTIMEOUT);
    
    // If the packet has any data... then read it.
    if(length != 0)
    {
        returncode |= I2C_I2CMasterSendStart(I2CADDR,I2C_I2C_READ_XFER_MODE , I2CTIMEOUT);
    
        for(int i=0;i&lt;length-1; i++)
        {
            I2C_I2CMasterReadByte(I2C_I2C_ACK_DATA,&amp;data,I2CTIMEOUT); // length
            inbuff[i] = data;
        }

        // Read the last byte
        I2C_I2CMasterReadByte(I2C_I2C_NAK_DATA,&amp;data,I2CTIMEOUT); // length
        inbuff[length-1] = data;
        returncode |= I2C_I2CMasterSendStop(I2CTIMEOUT);
        
        I2C_I2CMasterSendStop(I2CTIMEOUT);
    }
      
    sprintf(buff,"command = %d length = %d bytes= ",command,length);
    UART_UartPutString(buff);
    for(int i=0;i&lt;length;i++)
    {
        sprintf(buff,"%d ",inbuff[i]);
        UART_UartPutString(buff);
    }
    UART_UartPutString("\r\n");
    
}
</pre>
<p><span><p>You can "git" these projects from</p>
<p>https://github.com/iotexpert/GTT43A</p>
<p>And the driver library from </p>
<p>https://github.com/iotexpert/GTT-Client-Library</p>
<p><div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >Title</th>
</tr>
</thead>
<tbody>
<tr><td ><a href="https://iotexpert.com/2018/05/15/matrix-orbital-gtt43a-a-cool-display/">Matrix Orbital GTT43: A Cool Display</a></td>
</tr>

<tr><td ><a href="https://iotexpert.com/2018/05/17/matrix-orbital-gtt43a-serial-interface/">Matrix Orbital GTT43A: Serial Interface</a></td>
</tr>

<tr><td ><a href="https://iotexpert.com/2018/05/22/matrix-orbital-gtt43a-gtt-scripts/">Matrix Orbital GTT43A: GTT Scripts</a></td>
</tr>

<tr><td ><a href="https://iotexpert.com/2018/05/24/matrix-orbital-gtt43a-psoc-4-interface/" target="_blank" rel="noopener">Matrix Orbital GTT43A: A PSoC 4 Interface</a></td>
</tr>

<tr><td >Matrix Orbital GTT43A: Debugging the I2C</td>
</tr>

<tr><td >Matrix Orbital GTT43A: GTT Driver Library - Part 1</td>
</tr>

<tr><td >Matrix Orbital GTT43A: GTT Driver Library - Part 1</td>
</tr>

<tr><td >Matrix Orbital GTT43A: PSoC 6 using RTOS and the GTT Driver Library</td>
</tr>
</tbody></table></div></p></span></p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/matrix-orbital-gtt43a-psoc-4-interface/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>The Lost Art of Assembly Language Programming</title>
		<link>https://iotexpert.com/lost-art-assembly-language-programming/</link>
					<comments>https://iotexpert.com/lost-art-assembly-language-programming/#comments</comments>
		
		<dc:creator><![CDATA[Darrin Vallis]]></dc:creator>
		<pubDate>Fri, 09 Feb 2018 19:53:37 +0000</pubDate>
				<category><![CDATA[CY8CKIT-042]]></category>
		<category><![CDATA[CY8CKIT-044]]></category>
		<category><![CDATA[Cy8CKIT-145]]></category>
		<category><![CDATA[Embedded Design]]></category>
		<category><![CDATA[PSoC 4000S]]></category>
		<category><![CDATA[PSoC 4200]]></category>
		<category><![CDATA[PSoC Creator]]></category>
		<category><![CDATA[ARM]]></category>
		<category><![CDATA[Assembly. Machine Code]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=4992</guid>

					<description><![CDATA[Cypress introduced it&#8217;s first mass market microcontroller in 2001. It used a Cypress designed 8 bit CISC processor running at 24 MHz, with as little as 4 KB Flash and 256 bytes RAM. Wrapped around that was a neat array of programmable analog and digital blocks. This may not sound like much, but with a [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Cypress introduced it&#8217;s first mass market microcontroller in 2001. It used a Cypress designed 8 bit CISC processor running at 24 MHz, with as little as 4 KB Flash and 256 bytes RAM. Wrapped around that was a neat array of programmable analog and digital blocks. This may not sound like much, but with a creative mindset you could get these parts to do amazing things. For instance, I once implemented a complete ultrasonic ranging sensor with full wave analog demodulation in a single PSOC1 as shown below.</p>
<figure id="attachment_4993" aria-describedby="caption-attachment-4993" style="width: 1024px" class="wp-caption aligncenter"><a href="https://iotexpert.com/wp-content/uploads/2018/02/UltrasonicRangingPSOC1.jpg"><img decoding="async" width="1024" height="430" class="size-large wp-image-4993" alt="" src="https://iotexpert.com/wp-content/uploads/2018/02/UltrasonicRangingPSOC1-1024x430.jpg" srcset="https://iotexpert.com/wp-content/uploads/2018/02/UltrasonicRangingPSOC1-1024x430.jpg 1024w, https://iotexpert.com/wp-content/uploads/2018/02/UltrasonicRangingPSOC1-600x252.jpg 600w, https://iotexpert.com/wp-content/uploads/2018/02/UltrasonicRangingPSOC1-300x126.jpg 300w, https://iotexpert.com/wp-content/uploads/2018/02/UltrasonicRangingPSOC1-768x323.jpg 768w, https://iotexpert.com/wp-content/uploads/2018/02/UltrasonicRangingPSOC1.jpg 1040w" sizes="(max-width: 1024px) 100vw, 1024px" /></a><figcaption id="caption-attachment-4993" class="wp-caption-text">PSOC1 Ultrasonic Ranging</figcaption></figure>
<p>With CPU resources at a premium, you had to write tight, efficient code to get the most out of PSOC1. A single C library could consume the entire Flash. Consequently, I wrote a <em>lot</em> of assembly code. That&#8217;s not so bad, since I actually enjoy it more than C. There&#8217;s a certain elegance to well written, fully commented machine code. In the case of PSOC1, here&#8217;s what you had to work with: 5 registers, some RAM and Flash. That&#8217;s it. Real Men Write In Assembly.</p>
<figure id="attachment_4995" aria-describedby="caption-attachment-4995" style="width: 518px" class="wp-caption aligncenter"><a href="https://iotexpert.com/wp-content/uploads/2018/02/M8Carch.jpg"><img loading="lazy" decoding="async" width="518" height="341" class="size-full wp-image-4995" alt="" src="https://iotexpert.com/wp-content/uploads/2018/02/M8Carch.jpg" srcset="https://iotexpert.com/wp-content/uploads/2018/02/M8Carch.jpg 518w, https://iotexpert.com/wp-content/uploads/2018/02/M8Carch-300x197.jpg 300w" sizes="(max-width: 518px) 100vw, 518px" /></a><figcaption id="caption-attachment-4995" class="wp-caption-text">M8C Architecture</figcaption></figure>
<p>&nbsp;</p>
<p>We&#8217;ll start with simple machine code instruction to make the CPU do something. You can reference the M8C assembly language user guide <a href="http://www.cypress.com/file/72341/download">here</a> for more details. To get the M8C to execute 2+3=5 we write:</p>
<p><span style="font-family: Courier New;color: black">mov A,2       ;Load A with 2<br />
add A,3       ;Add 3 to A. Result=5 is in A<br />
</span></p>
<p>We can get fancy by using variables. Let&#8217;s add R=P+Q. Assume P is at RAM location 0x20 and Q is at location 0x21, and R is at 0x22</p>
<p><span style="font-family: Courier New;color: black">;Initialize variables<br />
mov [0x20],2  ;Load P with 2<br />
mov [0x21],3  ;Load Q with 3</span></p>
<p><span style="font-family: Courier New;color: black">;Add variables<br />
mov X,[0x20]  ;X &lt;- P<br />
mov A,[0x21]  ;A &lt;- Q<br />
adc [X],A     ;X &lt;- P + Q<br />
mov [0x22],X  ;R &lt;- X<br />
</span><br />
The fun thing about assembly is you can always dream up cool ways of doing things in less operations based on the machine&#8217;s instruction set. For example, we can simplify the above code as follows:</p>
<p><span style="font-family: Courier New;color: black">;Add variables<br />
mov [0x20],[0x22]   ;R &lt;- P<br />
adc [0x22],[0x21]   ;R &lt;- P + Q</span></p>
<p>In my experience, a good programmer with expert knowledge of the instruction set and CPU resources can always write better code than a compiler. There&#8217;s a certain human creativity that algorithms can&#8217;t match.</p>
<p>All that being said, I had not seen a good &#8220;machine code 101&#8221; tutorial for writing assembly in PSOC Creator on modern ARM M0 processors. So let&#8217;s walk through one now. We&#8217;ll use a CY8CKIT-145 and blink the LED. It&#8217;s just what happens to be laying around on the lab bench. Any PSOC4 kit will do.</p>
<p><img loading="lazy" decoding="async" width="518" height="341" class="size-full wp-image-4995 aligncenter" alt="CY8CKIT-145-40XX" src="http://www.cypress.com/sites/default/files/media-embed/263821/CY8CKIT-145_image.png" /></p>
<p>We&#8217;ll start by creating a standard project in PSOC Creator, drop a Digital Output pin on the schematic and call it &#8220;LED&#8221;</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2018/02/1.jpg"><img loading="lazy" decoding="async" width="1023" height="585" class="alignnone wp-image-5014 size-full" alt="" src="https://iotexpert.com/wp-content/uploads/2018/02/1.jpg" srcset="https://iotexpert.com/wp-content/uploads/2018/02/1.jpg 1023w, https://iotexpert.com/wp-content/uploads/2018/02/1-600x343.jpg 600w, https://iotexpert.com/wp-content/uploads/2018/02/1-300x172.jpg 300w, https://iotexpert.com/wp-content/uploads/2018/02/1-768x439.jpg 768w" sizes="(max-width: 1023px) 100vw, 1023px" /></a></p>
<p>Then open the .CYDWR file and drag pin LED to P2[5], since that&#8217;s where it is on the LED board. Yours may be in a different place on whatever board you are using.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2018/02/2.jpg"><img loading="lazy" decoding="async" width="871" height="579" class="alignnone size-full wp-image-5015" alt="" src="https://iotexpert.com/wp-content/uploads/2018/02/2.jpg" srcset="https://iotexpert.com/wp-content/uploads/2018/02/2.jpg 871w, https://iotexpert.com/wp-content/uploads/2018/02/2-600x399.jpg 600w, https://iotexpert.com/wp-content/uploads/2018/02/2-300x199.jpg 300w, https://iotexpert.com/wp-content/uploads/2018/02/2-768x511.jpg 768w" sizes="(max-width: 871px) 100vw, 871px" /></a></p>
<p>Now under &#8220;Source Files&#8221; in the workspace directory you will delete main.c and replace with main.s</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2018/02/3JPG.jpg"><img loading="lazy" decoding="async" width="343" height="567" class="alignnone size-full wp-image-5016" alt="" src="https://iotexpert.com/wp-content/uploads/2018/02/3JPG.jpg" srcset="https://iotexpert.com/wp-content/uploads/2018/02/3JPG.jpg 343w, https://iotexpert.com/wp-content/uploads/2018/02/3JPG-181x300.jpg 181w" sizes="(max-width: 343px) 100vw, 343px" /></a></p>
<p>Now right clock on &#8220;Source Files&#8221;, select &#8220;Add New File&#8221; and select &#8220;GNU ARM Assembly File&#8221; in the dialog. Rename the file from GNUArmAssembly01.s to main.s</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2018/02/4.jpg"><img loading="lazy" decoding="async" width="524" height="402" class="alignnone size-full wp-image-5017" alt="" src="https://iotexpert.com/wp-content/uploads/2018/02/4.jpg" srcset="https://iotexpert.com/wp-content/uploads/2018/02/4.jpg 524w, https://iotexpert.com/wp-content/uploads/2018/02/4-300x230.jpg 300w" sizes="(max-width: 524px) 100vw, 524px" /></a></p>
<p>Your workspace ends up looking like this:</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2018/02/5.jpg"><img loading="lazy" decoding="async" width="319" height="339" class="alignnone size-full wp-image-5018" alt="" src="https://iotexpert.com/wp-content/uploads/2018/02/5.jpg" srcset="https://iotexpert.com/wp-content/uploads/2018/02/5.jpg 319w, https://iotexpert.com/wp-content/uploads/2018/02/5-282x300.jpg 282w" sizes="(max-width: 319px) 100vw, 319px" /></a></p>
<p>So far, so good. Now open main.s, delete everything if it&#8217;s not empty and add the following code. This sets up the IDE for M0 assembly architecture</p>
<p><span style="font-family: Courier New;color: black">// ==============================================<br />
// ARM M0 Assembly Tutorial<br />
//<br />
// 01 &#8211; Blink LED<br />
// ==============================================<br />
.syntax unified<br />
.text<br />
.thumb<br />
</span><br />
Next we need to include register definitions for the chip we are using. These are all from the PSOC4 Technical Reference Manual (TRM)</p>
<p><span style="font-family: Courier New;color: black">// ==============================================<br />
// Includes<br />
// ==============================================<br />
.include &#8220;cydevicegnu_trm.inc&#8221;<br />
</span></p>
<p>Then we are going to do some .equ statements, same as #define in C. This identifies the Port 2 GPIO data register plus bits for the LED pin in on and off state</p>
<p><span style="font-family: Courier New;color: black">// ==============================================<br />
// Defines<br />
// ==============================================<br />
.equ LED_DR,CYREG_GPIO_PRT2_DR          // LED data reg address<br />
.equ LED_PIN,5                          // P2.5<br />
.equ LED_OFF,1&lt;&lt;led_pin                 // 0010 0000<br />
.equ LED_ON,~LED_OFF                    // 1101 1111<br />
</span></p>
<p>Now you add the right syntax to set up main()</p>
<p><span style="font-family: Courier New;color: black">// ==============================================<br />
// main<br />
// ==============================================<br />
.global main<br />
.func main, main<br />
.type main, %function<br />
.thumb_func<br />
</span></p>
<p>Finally we add the code for main, which is pretty simple:</p>
<p><span style="font-family: Courier New;color: black">main:<br />
ldr r5,=LED_DR      // Load GPIO port addr to r5</span></p>
<p><span style="font-family: Courier New;color: black">loop0:<br />
ldr r6,=LED_ON      // Move led data to r6<br />
str r6,[r5]         // Write r6 data to r5 addr</span></p>
<p><span style="font-family: Courier New;color: black">ldr r0,=0xFFFFFF    // Argument passed in r0<br />
bl CyDelayCycles    // Delay for N cycles</span></p>
<p><span style="font-family: Courier New;color: black">ldr r6,=LED_OFF     // Move led data to r6<br />
str r6,[r5]         // Write r6 data to r5 addr</span></p>
<p><span style="font-family: Courier New;color: black">ldr r0,=0xFFFFFF    // Argument passed in r0<br />
bl CyDelayCycles    // Delay for N cycles</span></p>
<p><span style="font-family: Courier New;color: black">b loop0             // Branch loop0</span></p>
<p><span style="font-family: Courier New;color: black">.endfunc            // End of main<br />
.end                // End of code<br />
</span></p>
<p>One thing to note: The function CyDelayCycles is defined CyBootAsmGnu.s. Any function in assembly gets its arguments passed by the first 4 registers r0,r1,r2 and r3. Before calling the function you simply load r0 with the argument then do a bl (branch with link). This is also why I avoided the first 4 registers when messing with LED data. If you&#8217;re interested in doing more with ARM assembly, definitely read the <a href="https://iotexpert.com/wp-content/uploads/2018/02/m0p_trm.pdf">Cortex M0+ Technical Reference Manual</a>. It&#8217;s a great primer for the M0+ instruction set.</p>
<p>That&#8217;s it. End result is a blinking LED. Cool thing is you can use PSOC Creator with all it&#8217;s nice features, but sill access the power of machine code.</p>
<p>You can get the project ZIP file <a href="https://iotexpert.com/wp-content/uploads/2018/02/M0_ASM_v2.zip">here</a>.</p>
<p>Regards<br />
Darrin Vallis</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2018/02/20180209_133339.jpg"><img loading="lazy" decoding="async" width="1024" height="768" class="alignnone size-large wp-image-5022" alt="" src="https://iotexpert.com/wp-content/uploads/2018/02/20180209_133339-1024x768.jpg" srcset="https://iotexpert.com/wp-content/uploads/2018/02/20180209_133339-1024x768.jpg 1024w, https://iotexpert.com/wp-content/uploads/2018/02/20180209_133339-600x450.jpg 600w, https://iotexpert.com/wp-content/uploads/2018/02/20180209_133339-300x225.jpg 300w, https://iotexpert.com/wp-content/uploads/2018/02/20180209_133339-768x576.jpg 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></a><a href="https://iotexpert.com/wp-content/uploads/2018/02/M0_ASM_v2.zip"></a><a href="https://iotexpert.com/wp-content/uploads/2018/02/20180209_133339.jpg"></a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/lost-art-assembly-language-programming/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
		<item>
		<title>PSoC 4200M WDT Long Deep Sleep</title>
		<link>https://iotexpert.com/psoc-4200m-wdt-long-deep-sleep/</link>
					<comments>https://iotexpert.com/psoc-4200m-wdt-long-deep-sleep/#comments</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Tue, 21 Nov 2017 11:26:03 +0000</pubDate>
				<category><![CDATA[CY8CKIT-044]]></category>
		<category><![CDATA[PSoC 4200]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=4694</guid>

					<description><![CDATA[Summary Earlier this year I wrote an article about using the PSoC 4200M WDT Watch Dog Timers.  In it I described a bunch of things that you could do with the PSoC 4200M WDT to help you time events, reset the part etc.  Recently a user named JAGIR asked if I could generate interrupts slower [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>Earlier this year I wrote an article about using the <a href="https://iotexpert.com/2017/04/11/psoc4-watch-dog-timer/">PSoC 4200M WDT Watch Dog Timers</a>.  In it I described a bunch of things that you could do with the PSoC 4200M WDT to help you time events, reset the part etc.  Recently a user named JAGIR asked if I could generate interrupts slower than 2 seconds and have the PSoC 4200M in deep sleep.  The answer to both of those questions is yes.</p>
<p>In order to make this work you need to &#8220;cascade&#8221; two WDTs together, something you can only do with firmware.  I have updated my previous workspace with a new project called &#8220;LongDeepSleep&#8221; which you can &#8220;Git&#8221; on my <span>GitHub </span><a href="https://github.com/iotexpert/PSoC4-WDTExamples" target="_blank" rel="noopener">website</a><span> or you can “git@github.com:iotexpert/PSoC4-WDTExamples.git”</span></p>
<h1>Configure the PSoC 4200M WDT Design</h1>
<p>First add a digital output pin called &#8220;RED&#8221; to the schematic</p>
<p><a href="https://iotexpert.com/?attachment_id=4697" rel="attachment wp-att-4697"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-1.48.44-PM.png" alt="PSoC 4200M WDT Schematic" width="449" height="325" class="alignnone wp-image-4697 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-1.48.44-PM.png 449w, https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-1.48.44-PM-300x217.png 300w" sizes="(max-width: 449px) 100vw, 449px" /></a></p>
<p>Then assign it to P0[6]</p>
<p><a href="https://iotexpert.com/?attachment_id=4696" rel="attachment wp-att-4696"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-1.49.05-PM.png" alt="PSoC 4200M WDT - DWR Pin Assignment" width="1011" height="845" class="alignnone wp-image-4696 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-1.49.05-PM.png 1011w, https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-1.49.05-PM-600x501.png 600w, https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-1.49.05-PM-300x251.png 300w, https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-1.49.05-PM-768x642.png 768w" sizes="(max-width: 1011px) 100vw, 1011px" /></a></p>
<p>Go to the clocks tab of the design wide resources.  Then click on &#8220;Edit Clock&#8221;.</p>
<p><a href="https://iotexpert.com/?attachment_id=4704" rel="attachment wp-att-4704"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-2.28.29-PM.png" alt="PSoC 4200M WDT Clocks" width="1011" height="526" class="alignnone size-full wp-image-4704" srcset="https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-2.28.29-PM.png 1011w, https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-2.28.29-PM-600x312.png 600w, https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-2.28.29-PM-300x156.png 300w, https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-2.28.29-PM-768x400.png 768w" sizes="(max-width: 1011px) 100vw, 1011px" /></a></p>
<p>On the Low Frequency Clocks configuration page of the design wide resources turn on the two WDTs and setup the dividers.</p>
<p><a href="https://iotexpert.com/?attachment_id=4695" rel="attachment wp-att-4695"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-1.50.10-PM.png" alt="PSoC 4200M WDT" width="785" height="594" class="alignnone wp-image-4695 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-1.50.10-PM.png 785w, https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-1.50.10-PM-600x454.png 600w, https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-1.50.10-PM-300x227.png 300w, https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-1.50.10-PM-768x581.png 768w" sizes="(max-width: 785px) 100vw, 785px" /></a></p>
<h1>System Reference Guide &#8211; PSoC 4200M WDT</h1>
<p>All of the documentation for the PSoC 4200M WDT is in the &#8220;Low Frequency Clock aka cy_lfclk&#8221; of the system resources documentations.</p>
<p><a href="https://iotexpert.com/?attachment_id=4699" rel="attachment wp-att-4699"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-2.01.45-PM-1024x943.png" alt="PSoC 4200M WDT System Reference Guide" width="1024" height="943" class="alignnone wp-image-4699 size-large" srcset="https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-2.01.45-PM-1024x943.png 1024w, https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-2.01.45-PM-600x553.png 600w, https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-2.01.45-PM-300x276.png 300w, https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-2.01.45-PM-768x708.png 768w, https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-2.01.45-PM.png 1054w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>When you read a little bit down in the PSoC 4 Low Frequency clock documentation you will find &#8220;Note The EDT cascade options are not configurable using these panels but the APIs can be used to perform cascading of WDTs&#8221;</p>
<p><a href="https://iotexpert.com/?attachment_id=4700" rel="attachment wp-att-4700"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-2.03.00-PM.png" alt="PSoC 4200M WDT" width="702" height="293" class="alignnone wp-image-4700 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-2.03.00-PM.png 702w, https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-2.03.00-PM-600x250.png 600w, https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-2.03.00-PM-300x125.png 300w" sizes="(max-width: 702px) 100vw, 702px" /></a></p>
<p>Then you search a little bit further down in the document and you will find the API CySysWDTSetCascade which will allow you to hook multiple 16-bit counters together to get 32 or more bits.</p>
<p><a href="https://iotexpert.com/?attachment_id=4698" rel="attachment wp-att-4698"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-1.53.26-PM.png" alt="PSoC 4200M WDT - CySysWdtSetCascade" width="760" height="318" class="alignnone wp-image-4698 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-1.53.26-PM.png 760w, https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-1.53.26-PM-600x251.png 600w, https://iotexpert.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-17-at-1.53.26-PM-300x126.png 300w" sizes="(max-width: 760px) 100vw, 760px" /></a></p>
<h1>Write the PSoC 4200M WDT Firmware</h1>
<p>The first example will blink the LED every 4 seconds.  Remember from the configuration above it has a 32KHz input clock with a divider of 1024 on WDT0 and a divider of 128 on WDT1.  That means you will get 32KHz / (1024 *128) = 0.25 Hz aka every 4 seconds.</p>
<pre class="lang:c decode:true">#include "project.h"

void wdtCallback()
{
    RED_Write(~RED_Read());
    
}

int main(void)
{
    CyGlobalIntEnable; /* Enable global interrupts. */
    CySysWdtSetInterruptCallback(CY_SYS_WDT_COUNTER1,wdtCallback);
    CySysWdtSetCascade(CY_SYS_WDT_CASCADE_01);
    
    for(;;)
    {
        CySysPmDeepSleep();
    }
}</pre>
<p>You can also get different delays by changing the dividers using firmware.  In the example below it is 1Hz output.</p>
<pre class="lang:c decode:true">#include "project.h"

void wdtCallback()
{
    RED_Write(~RED_Read());
}

int main(void)
{
    CyGlobalIntEnable; /* Enable global interrupts. */
    CySysWdtSetInterruptCallback(CY_SYS_WDT_COUNTER1,wdtCallback);
    CySysWdtSetCascade(CY_SYS_WDT_CASCADE_01);
    
    CySysWdtSetMatch(0,512);
    CySysWdtSetMatch(1,64);
    // Total divide = 512*64 = 32768 
    
    for(;;)
    {
        CySysPmDeepSleep();
    }
}
</pre>
<p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/psoc-4200m-wdt-long-deep-sleep/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Serial Wire View with PSOC4</title>
		<link>https://iotexpert.com/serial-wire-view-with-psoc4/</link>
					<comments>https://iotexpert.com/serial-wire-view-with-psoc4/#respond</comments>
		
		<dc:creator><![CDATA[Darrin Vallis]]></dc:creator>
		<pubDate>Sat, 18 Nov 2017 01:17:08 +0000</pubDate>
				<category><![CDATA[CY8CKIT-042]]></category>
		<category><![CDATA[CY8CKIT-044]]></category>
		<category><![CDATA[Devices]]></category>
		<category><![CDATA[Embedded Design]]></category>
		<category><![CDATA[PSoC 4000S]]></category>
		<category><![CDATA[PSoC 4200]]></category>
		<category><![CDATA[PSoC Creator]]></category>
		<category><![CDATA[PSoC4 BLE]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=4707</guid>

					<description><![CDATA[I use PSOC4 to invent all kinds of unique solutions for customers. Usually, they want them field upgradeable to deploy new features or fix bugs. Fortunately Cypress has a great I2C boot loader to meet this need, so I use the heck out of it. Cypress has a great debugger built into PSOC Creator which [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>I use PSOC4 to invent all kinds of unique solutions for customers. Usually, they want them field upgradeable to deploy new features or fix bugs. Fortunately Cypress has a great <a href="http://www.cypress.com/documentation/application-notes/an86526-psocr-4-and-psoc-analog-coprocessor-i2c-bootloader-zh">I2C boot loader</a> to meet this need, so I use the heck out of it.</p>
<p>Cypress has a great debugger built into <a href="http://www.cypress.com/products/psoc-creator-integrated-design-environment-ide">PSOC Creator</a> which fully supports all the ARM Serial Wire Debug protocols such as breakpoints, single step, memory, register viewing etc. However, when you are running a boot loader <em>the debugger does not work!</em> Why not? Because with a boot loader there are two applications resident in PSOC4: The boot loader and application. This is not supported by Cypress implementation of SWD.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/11/bootloader.jpg"><img loading="lazy" decoding="async" width="300" height="155" class="alignnone wp-image-4710 size-medium" alt="" src="https://iotexpert.com/wp-content/uploads/2017/11/bootloader-300x155.jpg" srcset="https://iotexpert.com/wp-content/uploads/2017/11/bootloader-300x155.jpg 300w, https://iotexpert.com/wp-content/uploads/2017/11/bootloader-600x311.jpg 600w, https://iotexpert.com/wp-content/uploads/2017/11/bootloader.jpg 653w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>Where does this leave you, the intrepid code developer, when debugging a boot loader project? Personally, I have used all kinds of methods: debug UART interface, debug I2C interface, bang out states on pins, debug Bluetooth interface &#8230; and on and on. You get the idea. All these methods burn a communications interface and require extra pins on the chip. Sometimes that&#8217;s not possible.</p>
<p>The issue recently came to a head when a customer very nearly in production experienced a boot loader failure. One system out of a few thousand was &#8220;bricked&#8221; when they tried to field  update in the lab. Their pinout is frozen, they can&#8217;t add new hardware so how do we look inside PSOC4 and see what&#8217;s going on?</p>
<p>I woke up at 2 AM and thought &#8220;Ah Ha! SWV!&#8221; (Yes, I Am A Geek) Serial Wire View is an ARM native debug protocol that let&#8217;s you XRAY the insides of any ARM MCU <em>with the right interface</em>. SWV is a protocol which runs on the SWD pins (clock and data) but also needs the Serial Wire Output (SWO) pin. Cypress left the SWO pin and associated IP off of PSOC4 to save die cost, foiling my great idea. Brief interlude to drink and bang head on desk.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/11/3272886-coresight-debug.png"><img loading="lazy" decoding="async" width="300" height="222" class="aligncenter wp-image-4712 size-medium" alt="" src="https://iotexpert.com/wp-content/uploads/2017/11/3272886-coresight-debug-300x222.png" srcset="https://iotexpert.com/wp-content/uploads/2017/11/3272886-coresight-debug-300x222.png 300w, https://iotexpert.com/wp-content/uploads/2017/11/3272886-coresight-debug-600x443.png 600w, https://iotexpert.com/wp-content/uploads/2017/11/3272886-coresight-debug-768x567.png 768w, https://iotexpert.com/wp-content/uploads/2017/11/3272886-coresight-debug-1024x756.png 1024w, https://iotexpert.com/wp-content/uploads/2017/11/3272886-coresight-debug.png 1190w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>Fortunately, I don&#8217;t give up easily. At least my subconscious does not. Woke up the next night thinking &#8220;Ah Ha!&#8221; again. Wife was mildly annoyed, but tolerates my idiosyncrasies.</p>
<p>Cypress has a nice software UART transmitter implementation. I shamelessly stole it, modified for my purposes and created a custom component. (It&#8217;s pretty easy to do this by the way) Baud rate was modified to 230 KBps and the output pin forced to a specific pin with a control file.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/11/dtview2.jpg"><img loading="lazy" decoding="async" width="767" height="301" class="alignnone wp-image-4717 size-full" alt="" src="https://iotexpert.com/wp-content/uploads/2017/11/dtview2.jpg" srcset="https://iotexpert.com/wp-content/uploads/2017/11/dtview2.jpg 767w, https://iotexpert.com/wp-content/uploads/2017/11/dtview2-600x235.jpg 600w, https://iotexpert.com/wp-content/uploads/2017/11/dtview2-300x118.jpg 300w" sizes="(max-width: 767px) 100vw, 767px" /></a></p>
<p>Once the component is in place, you can use its _DView_Printf( ) API call to display any debug data. Here is an example:</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/11/code.jpg"><img loading="lazy" decoding="async" width="816" height="227" class="alignnone size-full wp-image-4723" alt="" src="https://iotexpert.com/wp-content/uploads/2017/11/code.jpg" srcset="https://iotexpert.com/wp-content/uploads/2017/11/code.jpg 816w, https://iotexpert.com/wp-content/uploads/2017/11/code-600x167.jpg 600w, https://iotexpert.com/wp-content/uploads/2017/11/code-300x83.jpg 300w, https://iotexpert.com/wp-content/uploads/2017/11/code-768x214.jpg 768w" sizes="(max-width: 816px) 100vw, 816px" /></a></p>
<p>More about that output pin. Cypress sells a tool for programming and debugging PSOC called <a href="http://www.cypress.com/documentation/development-kitsboards/cy8ckit-002-psoc-miniprog3-program-and-debug-kit">CY8CKIT-002</a>, aka MiniProg3. The programming connector consists of VDD, GND, reset, SWD clock and SWD data as shown below.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/11/Header.jpg"><img loading="lazy" decoding="async" width="300" height="192" class="alignnone size-medium wp-image-4719" alt="" src="https://iotexpert.com/wp-content/uploads/2017/11/Header-300x192.jpg" srcset="https://iotexpert.com/wp-content/uploads/2017/11/Header-300x192.jpg 300w, https://iotexpert.com/wp-content/uploads/2017/11/Header-600x384.jpg 600w, https://iotexpert.com/wp-content/uploads/2017/11/Header-768x492.jpg 768w, https://iotexpert.com/wp-content/uploads/2017/11/Header.jpg 814w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>Since we can&#8217;t use SWD protocol for debugging anyway, we can change the pins from SWD to normal GPIO. <em>The pins still function for programming</em>. By default they are in SWD mode as shown.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/11/SWD_Pins.jpg"><img loading="lazy" decoding="async" width="300" height="194" class="alignnone size-medium wp-image-4720" alt="" src="https://iotexpert.com/wp-content/uploads/2017/11/SWD_Pins-300x194.jpg" srcset="https://iotexpert.com/wp-content/uploads/2017/11/SWD_Pins-300x194.jpg 300w, https://iotexpert.com/wp-content/uploads/2017/11/SWD_Pins.jpg 587w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>Going to the system tab of the .CYDWR file, we can change them to GPIO.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/11/ChangeToGPIO.jpg"><img loading="lazy" decoding="async" width="634" height="258" class="alignnone wp-image-4721 size-full" alt="" src="https://iotexpert.com/wp-content/uploads/2017/11/ChangeToGPIO.jpg" srcset="https://iotexpert.com/wp-content/uploads/2017/11/ChangeToGPIO.jpg 634w, https://iotexpert.com/wp-content/uploads/2017/11/ChangeToGPIO-600x244.jpg 600w, https://iotexpert.com/wp-content/uploads/2017/11/ChangeToGPIO-300x122.jpg 300w" sizes="(max-width: 634px) 100vw, 634px" /></a></p>
<p>Once we do that, the pins look like this. Here&#8217;s the trick. We now assign the TX output of our DTView component to pin 3[2], which is available  on the SWD programming header, pin 5.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/11/TX-UART.jpg"><img loading="lazy" decoding="async" width="300" height="204" class="alignnone size-medium wp-image-4722" alt="" src="https://iotexpert.com/wp-content/uploads/2017/11/TX-UART-300x204.jpg" srcset="https://iotexpert.com/wp-content/uploads/2017/11/TX-UART-300x204.jpg 300w, https://iotexpert.com/wp-content/uploads/2017/11/TX-UART.jpg 533w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>Can you see where we are going with this? Printf( ) data is now coming out of PSOC4 on pin 3[2], easily accessible on our debug header. This is where MiniProg3 comes in. It can actually receive data as a 230 KBps RX UART on its XRES pin. Weird, right? By building a simple interface cable we can get the data from your debug header into MiniProg3.</p>
<p>MiniProg3 XRES &#8212;&#8212; SWD HEADER pin 5</p>
<p>MiniProg3   GND &#8212;&#8212; SWD HEADER pin 2</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/11/20171114_102729.jpg"><img loading="lazy" decoding="async" width="653" height="490" class="alignnone wp-image-4726 size-full" alt="" src="https://iotexpert.com/wp-content/uploads/2017/11/20171114_102729.jpg" srcset="https://iotexpert.com/wp-content/uploads/2017/11/20171114_102729.jpg 653w, https://iotexpert.com/wp-content/uploads/2017/11/20171114_102729-600x450.jpg 600w, https://iotexpert.com/wp-content/uploads/2017/11/20171114_102729-300x225.jpg 300w" sizes="(max-width: 653px) 100vw, 653px" /></a></p>
<p>However, MiniProg3 does not show up as a COM port on your PC, so how do we the data? It needs to be accessed by a host application running the PP_COM API. This is documented under <em>PSOC Programmer Component Object Model COM Interface Guide</em>, Cypress specification <span style="font-family: Arial;font-size: xx-small"></span>001-45209. If you installed PSOC Creator or Programmer, this document is actually on your PC under C:\Program Files (x86)\Cypress\Programmer\Documents. Engineers don&#8217;t like to read instructions. Amazing what you can find when you do.</p>
<p>I wrote a simple  console application which opens MiniProg3 using PP_COM, retrieves data from the serial RX pin via USB and displays it like a simple terminal program. Voila! You now have a serial debugger that works for any PSOC4 project using MiniProg3 as your USB to serial dongle.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/11/DTViewOutput.jpg"><img loading="lazy" decoding="async" width="1024" height="388" class="alignnone size-large wp-image-4728" alt="" src="https://iotexpert.com/wp-content/uploads/2017/11/DTViewOutput-1024x388.jpg" srcset="https://iotexpert.com/wp-content/uploads/2017/11/DTViewOutput-1024x388.jpg 1024w, https://iotexpert.com/wp-content/uploads/2017/11/DTViewOutput-600x227.jpg 600w, https://iotexpert.com/wp-content/uploads/2017/11/DTViewOutput-300x114.jpg 300w, https://iotexpert.com/wp-content/uploads/2017/11/DTViewOutput-768x291.jpg 768w, https://iotexpert.com/wp-content/uploads/2017/11/DTViewOutput.jpg 1111w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Customer was really happy with this. We were able to immediately see his problem and fixed it in about 5 minutes.</p>
<p>Finally, here are all the source files</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/11/DTView-Firmware.zip">DTView Firmware</a> : PSOC Creator example project and DTView component</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/11/DTViewer-Setup.zip">DTViewer Binary</a> : Installer for DTViewer console</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/11/ViewerSource.zip">ViewerSource</a> : Complete source code for DTViewer console (Requires Visual Studio 2015)</p>
<p>That&#8217;s all. Have fun with the new debugging tool.</p>
<p>DTV</p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/serial-wire-view-with-psoc4/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>CY8CKIT-044 PSoC4200M Real Time Clock</title>
		<link>https://iotexpert.com/cy8ckit-044-psoc4200m-real-time-clock/</link>
					<comments>https://iotexpert.com/cy8ckit-044-psoc4200m-real-time-clock/#respond</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Thu, 02 Nov 2017 12:00:00 +0000</pubDate>
				<category><![CDATA[CY8CKIT-044]]></category>
		<category><![CDATA[FreeRTOS]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=4309</guid>

					<description><![CDATA[Summary In my article entitled &#8220;FreeRTOS FAT SL &#8211; Musing on my Implementation&#8220;, I lamented that I was supposed to implement a Real Time Clock so that the reads and writes of files could be timestamped, but didnt.  I knew that the PSoC4200M Real Time Clock is actually really easy to use.  But, I didn&#8217;t [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>In my article entitled &#8220;<a href="https://iotexpert.com/2017/10/25/freertos-fal-sl-musing-on-my-implementation/">FreeRTOS FAT SL &#8211; Musing on my Implementation</a>&#8220;, I lamented that I was supposed to implement a Real Time Clock so that the reads and writes of files could be timestamped, but didnt.  I knew that the <a href="http://www.cypress.com/file/274641/download" target="_blank" rel="noopener">PSoC4200M Real Time Clock</a> is actually really easy to use.  But, I didn&#8217;t because I didn&#8217;t want to fix the command line interface that I was using.  In this article I will show you how to implement a <a href="http://www.cypress.com/documentation/datasheets/psocr-4-psoc-4200m-family-datasheet-programmable-system-chip-psocr-ja" target="_blank" rel="noopener">PSoC4200M</a> Real Time Clock in FreeRTOS.  Then in the next article Ill show you how to implement the FreeRTOS Command Line Interface to set the time and turn on the FreeRTOS FAT SL Clock Driver.</p>
<h1>PSoC4200M Real Time Clock Schematic</h1>
<p>The first thing to do is copy the <a href="https://iotexpert.com/2017/08/31/freertos-psoc-template-project/" target="_blank" rel="noopener">FreeRTOS Template project</a>.  Then add the UART and RTC components to the project schematic from the PSoC Creator Component Catalog.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-28-at-2.09.18-PM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-28-at-2.09.18-PM.png" alt="PSoC4200M Real Time Clock Schematic" width="569" height="310" class="alignnone wp-image-4315 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-28-at-2.09.18-PM.png 569w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-28-at-2.09.18-PM-300x163.png 300w" sizes="(max-width: 569px) 100vw, 569px" /></a></p>
<p>The PSoC4200M Real Time Clock will have the default settings for the component.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-28-at-2.09.38-PM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-28-at-2.09.38-PM.png" alt="PSoC4200M Real Time Clock Configuration" width="630" height="479" class="alignnone wp-image-4314 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-28-at-2.09.38-PM.png 630w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-28-at-2.09.38-PM-600x456.png 600w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-28-at-2.09.38-PM-300x228.png 300w" sizes="(max-width: 630px) 100vw, 630px" /></a></p>
<p>To make an accurate clock, I want to turn on the Watch Crystal Oscillator which drives the <a href="https://iotexpert.com/2017/04/11/psoc4-watch-dog-timer/" target="_blank" rel="noopener">Watch Dog Timers</a>.  To do this, click on the &#8220;Clocks&#8221; tab of the Design Wide Resources.  Then press edit clock.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-28-at-2.11.39-PM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-28-at-2.11.39-PM-1024x460.png" alt="PSoC4200M Real Time Clock Configuration DWR" width="1024" height="460" class="alignnone wp-image-4310 size-large" srcset="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-28-at-2.11.39-PM-1024x460.png 1024w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-28-at-2.11.39-PM-600x269.png 600w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-28-at-2.11.39-PM-300x135.png 300w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-28-at-2.11.39-PM-768x345.png 768w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-28-at-2.11.39-PM.png 1316w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Once you are on the clock editor page, turn on the WCO (by pressing the checkmark in the upper left hand corner).  Then turn on the WDT Timer 0 in the Periodic Timer Mode.  Set the divider to 32768 so that the RTC will get 1 interrupt per second.  You also need to select the source of the RTC to be Timer 0.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-28-at-2.10.13-PM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-28-at-2.10.13-PM.png" alt="PSoC4200M Real Time Clock Low Frequency Clock Configuration" width="784" height="594" class="alignnone wp-image-4313 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-28-at-2.10.13-PM.png 784w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-28-at-2.10.13-PM-600x455.png 600w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-28-at-2.10.13-PM-300x227.png 300w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-28-at-2.10.13-PM-768x582.png 768w" sizes="(max-width: 784px) 100vw, 784px" /></a></p>
<h1>Firmware</h1>
<p>In the firmware I create a new task called &#8220;uartTask&#8221; which just starts the RTC, then prints out the time when the user presses &#8216;t&#8217;.  Obviously I didnt set the clock, so it starts from 12:00:00.  Ill fix that in the next post after I get the FreeRTOS Command Line Interpreter working.</p>
<pre class="start-line:65 lang:c decode:true ">void uartTask(void *arg)
{
    (void)arg;
    UART_Start();
    clearScreen();
    UART_UartPutString("Start Real Time Clock Demo\n");
    UART_SetCustomInterruptHandler(uartISR);
    uint32 timeBCD;
    char buff[32];
    
    RTC_Start();

    
    while(1)
    {
        ulTaskNotifyTake(pdTRUE,portMAX_DELAY);
        
        while(UART_SpiUartGetRxBufferSize()) // if there is data then read and process
        {
            char c;
            
            c= UART_UartGetChar();
			switch(c)
			{
                case 't':
                    
                    timeBCD = RTC_GetTime();
                    sprintf(buff,"%d:%d:%d\n",(int)RTC_GetHours(timeBCD),(int)RTC_GetMinutes(timeBCD),(int)RTC_GetSecond(timeBCD));
                    UART_UartPutString(buff);
                    
                break;</pre>
<p>You can find this project called RTC-Example in the workspace PSoC-Filesystem on the <a href="https://github.com/iotexpert/PSoC-FileSystem" target="_blank" rel="noopener">IoT Expert GitHub</a> site or git@github.com:iotexpert/PSoC-FileSystem.git</p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/cy8ckit-044-psoc4200m-real-time-clock/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>FreeRTOS FAT SL &#8211; Musings on my Implementation</title>
		<link>https://iotexpert.com/freertos-fal-sl-musing-on-my-implementation/</link>
					<comments>https://iotexpert.com/freertos-fal-sl-musing-on-my-implementation/#comments</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Wed, 25 Oct 2017 12:00:52 +0000</pubDate>
				<category><![CDATA[CY8CKIT-044]]></category>
		<category><![CDATA[FreeRTOS]]></category>
		<category><![CDATA[FreeRTOS FAT SL]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=4240</guid>

					<description><![CDATA[Summary I just finished writing the &#8220;last&#8221; article about building a FreeRTOS FAT SL filesystem into the Cypress FM24V10 FRAM using a CY8CKIT-044.  My implementation works pretty well&#8230;. but I am not really that happy with it.  As I sit here and write this article I am not totally sure what I should do next. [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>I just finished writing the &#8220;last&#8221; article about building a <a href="http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT_SL/FreeRTOS_Plus_FAT_SL.shtml" target="_blank" rel="noopener">FreeRTOS FAT SL</a> filesystem into the Cypress <a href="http://www.cypress.com/file/41666/download" target="_blank" rel="noopener">FM24V10 FRAM</a> using a <a href="http://www.cypress.com/documentation/development-kitsboards/cy8ckit-044-psoc-4-m-series-pioneer-kit" target="_blank" rel="noopener">CY8CKIT-044</a>.  My implementation works pretty well&#8230;. but I am not really that happy with it.  As I sit here and write this article I am not totally sure what I should do next.</p>
<p>I suppose the first thing to do is talk about the things that I don&#8217;t like in what I did.</p>
<ol>
<li>Real Time Clock</li>
<li>Command Line Interpreter</li>
<li>DMA Media Driver</li>
<li>Include files</li>
<li>F_FS_THREAD_AWARE 0</li>
<li>Template project</li>
<li>Performance Metrics</li>
<li>Wear Leveling</li>
<li>Performance Metrics</li>
<li>Discussion of FAT Filesystems</li>
</ol>
<h1>Real Time Clock</h1>
<p>As part of the port, you are supposed to provide psp_rtc.c which has one function, psp_getcurrentimedate.  This function is used to get the time to use as a timestamp on files.  I left it default, which means every transaction is timestamped the same, probably not good.  Moreover, there is an RTC in the PSoC4200M that is on the CY8CKIT-044.  The board also has the watch crystal which drives the RTC populated so there is really no good reason not to turn it on.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-22-at-10.05.00-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-22-at-10.05.00-AM.png" alt="FreeRTOS FAT SL PSoC RTC" width="628" height="479" class="alignnone wp-image-4242 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-22-at-10.05.00-AM.png 628w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-22-at-10.05.00-AM-600x458.png 600w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-22-at-10.05.00-AM-300x229.png 300w" sizes="(max-width: 628px) 100vw, 628px" /></a></p>
<p>However, when I wrote the original example the command line interpreter that I build only took one character at a time, so there was no good way to set the clock.   Which brings me to the next problem.</p>
<h1>Command Line Interpreter (CLI)</h1>
<p>When I originally build the example project my command interpreter just had an infinite loop that waited for a character from the keyboard, then did one command based on that character.  It looks like this:</p>
<pre class="lang:c decode:true ">   while(1)
    {
        ulTaskNotifyTake(pdTRUE,portMAX_DELAY);
        
        while(UART_SpiUartGetRxBufferSize()) // if there is data then read and process
        {
            char c;
            
            c= UART_UartGetChar();
			switch(c)
			{
				case 'i': 
				break;
				
			
                case '?': // Print out the list of commands
                    
                break;
                    
                default:
                break;
			}
        }
        // Turn the interrupts back on
        UART_ClearRxInterruptSource(UART_INTR_RX_NOT_EMPTY); 
        UART_SetRxInterruptMode(UART_INTR_RX_NOT_EMPTY);
    }</pre>
<p>What I did was cheap and easy&#8230; but, FreeRTOS has a CLI built in, so I suppose that I should have used.</p>
<h1>DMA Media Driver</h1>
<p>When I read and write from the FRAM I put in code that is blocking.  Meaning that it essentially hangs the entire system until they return.  Not really good given that this is an RTOS.  This is what all of the I2C_ functions below do.</p>
<pre class="lang:c decode:true ">  status = I2C_I2CMasterSendStart( calcI2CAddress(sector),I2C_I2C_WRITE_XFER_MODE);
    if(status != I2C_I2C_MSTR_NO_ERROR)
    {
        UART_UartPutString("I2C Error\n");
        return MDRIVER_RAM_ERR_SECTOR;
    }
    int i;
    I2C_I2CMasterWriteByte((address&gt;&gt;8)&amp;0xFF);
    I2C_I2CMasterWriteByte(address &amp; 0xFF); //</pre>
<p>There is no reason that I shouldn&#8217;t have used the DMA engine to read and write the FRAM which would have freed up the processor.</p>
<h1>Include files</h1>
<p>I absolutely hate the scheme that I used to name and use the includes in the FreeRTOS FAT SL port.  I should fix this for sure.</p>
<h1>F_FS_THREAD_AWARE 0</h1>
<p>When I originally tried to compile this project I marked this #define from &#8220;config_fat_sl.h&#8221; as &#8220;1&#8221;.  However when I do that, I end up with this error which I should for sure fix as the code is not reentrant and this #define protects you.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-22-at-10.20.03-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-22-at-10.20.03-AM.png" alt="An RTOS bug in the FreeRTOS FAT SL Implementation" width="847" height="132" class="alignnone wp-image-4245 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-22-at-10.20.03-AM.png 847w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-22-at-10.20.03-AM-600x94.png 600w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-22-at-10.20.03-AM-300x47.png 300w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-22-at-10.20.03-AM-768x120.png 768w" sizes="(max-width: 847px) 100vw, 847px" /></a></p>
<h1>Template project</h1>
<p>My good friend Mark Saunders pointed out PSoC Creator has a new feature which you can use to make template projects.. which I didnt know about until he told me.  Obviously this would be better than what I am doing.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-22-at-10.21.35-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-22-at-10.21.35-AM-1024x142.png" alt="" width="1024" height="142" class="alignnone size-large wp-image-4246" srcset="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-22-at-10.21.35-AM-1024x142.png 1024w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-22-at-10.21.35-AM-600x83.png 600w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-22-at-10.21.35-AM-300x42.png 300w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-22-at-10.21.35-AM-768x106.png 768w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-22-at-10.21.35-AM.png 1076w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Performance Metrics</h1>
<p>I did not collect any performance metrics when I build this project.  How much RAM? Flash?  How long does it take to read and write files?  I don&#8217;t know.  Moreover, I put in debugging information into the media driver which was counter productive to good memory usage.  For instance in this snip from the readsector function I define a big ass buffer of 128 bytes on line 134, then I printout a message to the uart each time this function is called.</p>
<pre class="start-line:132 EnlighterJSRAW" data-enlighter-language="c"">static int fram_readsector ( F_DRIVER * driver, void * data, unsigned long sector )
{
    char buff[128]; // A scratch buffer for UART Printing
    (void)driver;
    uint16 address;
    uint32_t status;
    
    sprintf(buff,"Read sector %d\n",(int)sector);
    UART_UartPutString(buff);
</pre>
<h1>Error Checking</h1>
<p>There are a bunch of places where I could have put in much better error checking, and I didnt.  For instance in this section of the readsector function if the I2C_I2CMasterWriteByte function fails, it probably hangs the I2C bus until the chip is reset&#8230; this is bad.  Even when an error occurs, printing a message probably isn&#8217;t a good idea (line 146).</p>
<pre class="start-line:144 lang:c decode:true">    if(status != I2C_I2C_MSTR_NO_ERROR)
    {
        UART_UartPutString("I2C Error\n");
        return MDRIVER_RAM_ERR_SECTOR;
    }
    int i;
    I2C_I2CMasterWriteByte((address&gt;&gt;8)&amp;0xFF);
    I2C_I2CMasterWriteByte(address &amp; 0xFF); // 
    
    
    I2C_I2CMasterSendRestart(calcI2CAddress(sector),I2C_I2C_READ_XFER_MODE);</pre>
<h1>Wear Leveling</h1>
<p>Many nonvolatile memory chip will wear out if you write them too many time.  Even 100K cycles can easily happen on a key sector of the filesystem for instance sector 0.  One convient thing about the FRAM is that it doesnt wear out.  But, when I started this journey I was originally going to use the PSoC6 development kit which uses a NOR Flash.  The NOR Flash will for sure wear out.  To combat this problem, people have developed wear leveling schemes.  But I don&#8217;t address this issue at all with my media driver.</p>
<h1>Discussion of FAT Filesystems</h1>
<p>As I wrote about the FreeRTOS FAT SL Filesystem I was originally planning a tutorial on file systems.  But as I dug a little bit a whole bunch of issues came up which felt a little bit overwhelming to address.  The issues that were left unaddressed are:</p>
<ol>
<li>Copywrite of the FAT File System</li>
<li>Efficiency of FAT File Systems</li>
<li>The licensing of the FreeRTOS FAT SL</li>
<li>Other FAT implementations</li>
</ol>
<p>I suppose that at some point I should come back and look at those issues.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/freertos-fal-sl-musing-on-my-implementation/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
		<item>
		<title>FreeRTOS FAT SL – PSoC Example Project (Part 2)</title>
		<link>https://iotexpert.com/freertos-fat-sl-psoc-example-project-part-2/</link>
					<comments>https://iotexpert.com/freertos-fat-sl-psoc-example-project-part-2/#respond</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Tue, 24 Oct 2017 12:00:21 +0000</pubDate>
				<category><![CDATA[CY8CKIT-044]]></category>
		<category><![CDATA[FreeRTOS]]></category>
		<category><![CDATA[FreeRTOS FAT SL]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=4237</guid>

					<description><![CDATA[Summary &#8211; Examples using FreeRTOS FAT SL In the previous several articles I have written about the FreeRTOS FAT SL Filesystem.  This included using the Cypress FM24V10 FRAM, creating an FRAM media driver and building an example project.  This article will show you the C-functions in &#8220;extestfs.c&#8221; that are used to actually test the filesystem. [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary &#8211; Examples using FreeRTOS FAT SL</h1>
<p>In the previous several articles I have written about the FreeRTOS FAT SL Filesystem.  This included using the <a href="https://iotexpert.com/2017/10/11/freertos-fat-sl-filesystem-part-1/" target="_blank" rel="noopener">Cypress FM24V10 FRAM</a>, creating an <a href="https://iotexpert.com/2017/10/17/freertos-fat-sl-filesystem-porting-to-psoc4m/" target="_blank" rel="noopener">FRAM media driver</a> and building an <a href="https://iotexpert.com/2017/10/19/freertos-fat-sl-psoc-example-project-part-1/" target="_blank" rel="noopener">example project</a>.  This article will show you the C-functions in &#8220;extestfs.c&#8221; that are used to actually test the filesystem.  Notice that I named all of the functions starting with &#8220;ex&#8221; which I adopted from the FreeRTOS FAT SL examples.  The functions in this article are all called by the command line interpreter that I built in the previous post.  They include</p>
<ul>
<li>exInit &#8211; Initializing the Filesystem</li>
<li>exCreateFile &#8211; Creating a file in the FileSystem</li>
<li>exReadFile &#8211; Reading the data from a file</li>
<li>exFormat &#8211; Formatting the disk</li>
<li>exDriveInfo &#8211; Printing the drive information</li>
<li>exDirectory &#8211; Print an &#8220;ls&#8221; or &#8220;dir&#8221;</li>
</ul>
<h1>exInit</h1>
<p>Before the FreeRTOS FAT SL Filesystem can do anything, you must initialize it.  All this function does is call the function that I created to turn on the file system in the media port.  One thing that is interesting is that it returns an error code that tells you if the file system is formatted or  not.   It figured this out by looking at the data in the first sector of the FRAM.</p>
<pre class="start-line:14 lang:c decode:true">// This function initalize the filesystem
void exInit(void)
{
    unsigned char ucStatus;
    
	/* First create the volume. */
	ucStatus = f_initvolume( fram_initfunc );
    UART_UartPutString("Initialized sucessfully\n");
	/* It is expected that the volume is not formatted. */
	if( ucStatus == F_ERR_NOTFORMATTED )
	{
        UART_UartPutString("Filesystem Unformatted\n");
	}
    
    else
    {
        UART_UartPutString("Filesystem Formatted\n");
    }
}</pre>
<h1>exCreateFile</h1>
<p>This functiom creates a file called &#8220;afile.txt&#8221; in the FreeRTOS FAT SL filesystem with the ASCII characters for 0-9.  I did something that was probably not helpful in that I made a loop (on line 43) that started a &#8216;0&#8217; which is also known as 49 (go look at the ASCII table).</p>
<p>This function calls <a href="http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT_SL/API/f_open.shtml" target="_blank" rel="noopener">f_open</a> which will create a new file when it is passed the argument &#8220;w&#8221;.  I also use the function <a href="http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT_SL/API/f_putc.shtml" target="_blank" rel="noopener">f_putc</a> to output characters to the file.</p>
<p>As I am looking at the code to write this article I realized that I didnt f_close the file (which I have now fixed).</p>
<pre class="start-line:33 lang:c decode:true">#define FILENAME "afile.txt"
// exCreateFile - this function creates a file called "afile.txt" an
void exCreateFile(void)
{
    F_FILE *pxFile;
    UART_UartPutString("Attempting Create &amp; Write File\n");
    pxFile = f_open(FILENAME, "w" );
  
    if(pxFile)
    {
        for(int i='0' ; i&lt; '9' ; i++)
        {
            f_putc(i,pxFile);
        }
        f_close(pxFile);
    }
    else
    {
        UART_UartPutString("Failed File Create\n");
    }
}
</pre>
<h1>exReadFile</h1>
<p>The exReadFile functions reads all of the characters out of the &#8220;afile.txt&#8221; and prints them to the screen.  It uses the function <a href="http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT_SL/API/f_read.shtml" target="_blank" rel="noopener">f_getc</a> which reads one character from the file.  Notice that I open the file in read mode by using the &#8220;r&#8221; option.</p>
<pre class="start-line:54 lang:c decode:true">void exReadFile(void)
{
    UART_UartPutString("Attempting Read\n");
    F_FILE *pxFile;
    pxFile = f_open( FILENAME, "r" );
    if(pxFile)
    {
        while(!f_eof(pxFile))
        {
            UART_UartPutChar(f_getc(pxFile));
        }
        f_close(pxFile);
    }
    else
    {
        UART_UartPutString("File not found\n");
    }
}

</pre>
<h1>exFormat</h1>
<p>The exFormat function calles <a href="http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT_SL/API/f_format.shtml" target="_blank" rel="noopener">f_format</a> with the FAT12 option.  It is impossible to use FAT16 and FAT32 which require a much larger media to use.  FAT12 was originally created for use on smallish floppy disk.</p>
<pre class="start-line:75 lang:c decode:true">void exFormat(void)
{
    char buff[128];
	/* Format the created volume. */
    unsigned char ucStatus;
	ucStatus = f_format( F_FAT12_MEDIA );
	if( ucStatus == F_NO_ERROR )
	{
        UART_UartPutString("Formatted sucessfully\n");
    }
    else
    {
        sprintf(buff,"Error = %d\n",ucStatus);
        UART_UartPutString(buff);   
        UART_UartPutString("Format Error\n");
    }   
}</pre>
<h1>exDriveInfo</h1>
<p>The exDriveInfo function calls the <a href="http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT_SL/API/f_getfreespace.shtml" target="_blank" rel="noopener">f_getfreespace</a> function to find out how much space is free for use on the FRAM FreeRTOS FAT SL Filesystem.</p>
<pre class="start-line:93 EnlighterJSRAW" data-enlighter-language="c"">void exDriveInfo( void )
{
    char buff[128];
    F_SPACE xSpace;
    unsigned char ucReturned;

    /* Get space information on current embedded FAT file system drive. */
    ucReturned = f_getfreespace( &amp;xSpace );
    if( ucReturned == F_NO_ERROR )
    {
        /* xSpace.total holds the total drive size, xSpace.free holds the
        free space on the drive, xSpace.used holds the size of the used space
        on the drive, xSpace.bad holds the size of unusable space on the
        drive. */
        sprintf(buff,"Free Space = %lu\nUsed Space = %lu\nTotal = %lu\n",xSpace.free,xSpace.used,xSpace.total);
        UART_UartPutString(buff);
    }
    else
    {
        /* xSpace could not be completed.  ucReturned holds the error code. */
        UART_UartPutString("Free space failed\n");
    }
}</pre>
<h1>exDirectory</h1>
<p>The exDirectory function calls the <a href="http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT_SL/API/f_findfirst.shtml" target="_blank" rel="noopener">f_findfirst</a> and <a href="http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT_SL/API/f_findnext.shtml" target="_blank" rel="noopener">f_findnext</a> functions to iterate all of the files and directories on the top level of the file system.  The f_findfirst function uses a wildcard regular expression to match filenames. When it finds a file it prints information about that file.</p>
<pre class="start-line:117 lang:c decode:true">void exDirectory( void )
{
    char buff[128];
    F_FIND xFindStruct;

    /* Print out information on every file in the subdirectory "subdir". */
    if( f_findfirst( "*.*", &amp;xFindStruct ) == F_NO_ERROR )
    {
        do
        {
            sprintf(buff,"filename:%s ", xFindStruct.filename );
            UART_UartPutString(buff);

            if( ( xFindStruct.attr &amp; F_ATTR_DIR ) != 0 )
            {
                UART_UartPutString ( "is a directory directory\n" );
            }
            else
            {
                sprintf ( buff, "is a file of size %lu\n", xFindStruct.filesize );
                UART_UartPutString(buff);
            }

        } while( f_findnext( &amp;xFindStruct ) == F_NO_ERROR );
    }
}</pre>
<p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/freertos-fat-sl-psoc-example-project-part-2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>FreeRTOS FAT SL FileSystem Porting to PSoC4M</title>
		<link>https://iotexpert.com/freertos-fat-sl-filesystem-porting-to-psoc4m/</link>
					<comments>https://iotexpert.com/freertos-fat-sl-filesystem-porting-to-psoc4m/#respond</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Tue, 17 Oct 2017 10:00:16 +0000</pubDate>
				<category><![CDATA[CY8CKIT-044]]></category>
		<category><![CDATA[FreeRTOS]]></category>
		<category><![CDATA[PSoC 4200]]></category>
		<category><![CDATA[FreeRTOS FAT SL]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=4211</guid>

					<description><![CDATA[Summary of FreeRTOS FAT SL FileSystem Port In the previous article I discussed the Cypress 24V10 FRAM which I am going to use to store nonvolatile data for the FreeRTOS FAT SL FileSystem.  The &#8220;SL&#8221; in the name stands for &#8220;super light&#8221; and was built by HCC Embedded for, get this, embedded applications. In this article I [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary of FreeRTOS FAT SL FileSystem Port</h1>
<p>In the previous <a href="https://iotexpert.com/2017/10/11/freertos-fat-sl-filesystem-part-1/" target="_blank" rel="noopener">article</a> I discussed the Cypress 24V10 FRAM which I am going to use to store nonvolatile data for the <a href="http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT_SL/FreeRTOS_Plus_FAT_SL.shtml" target="_blank" rel="noopener">FreeRTOS FAT SL FileSystem</a>.  The &#8220;SL&#8221; in the name stands for &#8220;super light&#8221; and was built by <a href="https://www.hcc-embedded.com" target="_blank" rel="noopener">HCC Embedded</a> for, get this, embedded applications.</p>
<p>In this article I am going to show you how to build a media driver to make the FreeRTOS FAT SL FileSystem work.  In the next article I will talk about how to actually use the FreeRTOS FAT SL FileSystem on the CY8CKIT-044 using the Cypress FMV24V10 FRAM.</p>
<p>The media driver is pretty simple,  you just need to provide the following functions:</p>
<ul>
<li><a href="http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT_SL/Media_Driver_API/F_DRIVERINIT.shtml" target="_blank" rel="noopener">F_DRVERINIT</a>() &#8211; Initialize everything</li>
<li><a href="http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT_SL/Media_Driver_API/F_GETPHY.shtml" target="_blank" rel="noopener">F_GETPHY</a>() &#8211; Return information about the FRAM</li>
<li><a href="http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT_SL/Media_Driver_API/F_READSECTOR.shtml" target="_blank" rel="noopener">F_READSECTOR</a>() &#8211; Read 512 bytes from the FRAM</li>
<li><a href="http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT_SL/Media_Driver_API/F_WRITESECTOR.shtml" target="_blank" rel="noopener">F_WRITESECTOR</a>() &#8211; Write 512 bytes to the FRAM</li>
<li><a href="http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT_SL/Media_Driver_API/F_GETSTATUS.shtml" target="_blank" rel="noopener">F_GETSTATUS</a>() &#8211; Return the state of the system</li>
<li><a href="http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT_SL/Media_Driver_API/F_RELEASE.shtml" target="_blank" rel="noopener">F_RELEASE</a>() &#8211; Close down</li>
</ul>
<p>And two structures</p>
<ul>
<li><a href="http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT_SL/Media_Driver_API/structures.shtml" target="_blank" rel="noopener">F_DRIVER</a> &#8211; Function pointers to the media driver functions</li>
<li><a href="http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT_SL/Media_Driver_API/structures.shtml" target="_blank" rel="noopener">F_PHY</a> &#8211; Information about the FRAM</li>
</ul>
<p>To make all of this work I will start by copying the &#8220;ram&#8221; driver that was provided as an example (ill call mine framdrv_f.c).  The file that I started with resides in <span>FreeRTOS-Plus/Source/FreeRTOS-Plus-FAT-SL/media-drv/ram/ramdrv_f.c</span></p>
<h1>F_DRIVER Structure</h1>
<p>The F_DRIVER structure mostly contains function pointers to the driver functions (which you create).  All throughout the FreeRTOS FAT SL FileSystem code it will do things like :</p>
<pre class="lang:c decode:true ">status = mdrv-&gt;getstatus( mdrv );</pre>
<p>which calls the function in the media driver structure named &#8220;getstatus&#8221;.</p>
<p>Here is the exact definition of the F_DRIVER STRUCTURE (found in <span>FreeRTOS-Plus-FAT-SL/api/a</span>pi_mdriver.h) :</p>
<pre class="start-line:78 lang:c decode:true">typedef struct F_DRIVER  F_DRIVER;

typedef int           ( *F_WRITESECTOR )( F_DRIVER * driver, void * data, unsigned long sector );
typedef int           ( *F_READSECTOR )( F_DRIVER * driver, void * data, unsigned long sector );
typedef int           ( *F_GETPHY )( F_DRIVER * driver, F_PHY * phy );
typedef long          ( *F_GETSTATUS )( F_DRIVER * driver );
typedef void          ( *F_RELEASE )( F_DRIVER * driver );

typedef struct F_DRIVER
{
  unsigned long  user_data;     /* user defined data */
  void         * user_ptr;      /* user define pointer */

  /* driver functions */
  F_WRITESECTOR          writesector;
  F_READSECTOR           readsector;
  F_GETPHY               getphy;
  F_GETSTATUS            getstatus;
  F_RELEASE              release;
} _F_DRIVER;

typedef F_DRIVER *( *F_DRIVERINIT )( unsigned long driver_param );</pre>
<p>The user_data and user_ptr do not appear to be used anywhere in the FreeRTOS FAT SL FileSystem code, so I am not sure what they had in mind for those variables.</p>
<h1>The F_PHY Structure</h1>
<p>The other structure that is needed (sort of) is F_PHY.  This structure contains parameters that were originally meant for disk drives and are no longer used.  For the FRAM I will divide up the 128k array into &#8220;sectors&#8221; of &#8220;512&#8221; bytes.  For some reason which I don&#8217;t understand the sector size must be fixed to 512 bytes.  The <a href="https://en.wikipedia.org/wiki/Design_of_the_FAT_file_system" target="_blank" rel="noopener">history</a> of FAT filesystems is incredibly messy, and I started to dig through it so that I understood the number, but I am not sure that there are enough hours in my life.</p>
<pre class="start-line:54 lang:c decode:true" title="api_mdriver.h">typedef struct
{
  unsigned short  number_of_cylinders;
  unsigned short  sector_per_track;
  unsigned short  number_of_heads;
  unsigned long   number_of_sectors;
  unsigned char   media_descriptor;

  unsigned short  bytes_per_sector;
} F_PHY;</pre>
<p>As I wrote this article I didnt remember setting up the F_PHY structure&#8230; but things still worked (ill address this later in the article).</p>
<h1>F_DRVERINIT()</h1>
<p>This function sets up the function pointers and marks the in_use variable.</p>
<pre class="start-line:294 lang:c decode:true">/****************************************************************************
 *
 * fram_initfunc
 *
 * this init function has to be passed for highlevel to initiate the
 * driver functions
 *
 * INPUTS
 *
 * driver_param - driver parameter
 *
 * RETURNS
 *
 * driver structure pointer
 *
 ***************************************************************************/
F_DRIVER * fram_initfunc ( unsigned long driver_param )
{
  ( void ) driver_param;

    UART_UartPutString("Calling init\n");

  if( in_use )
    return NULL;

  (void)psp_memset( &amp;t_driver, 0, sizeof( F_DRIVER ) );

  t_driver.readsector = fram_readsector;
  t_driver.writesector = fram_writesector;
  t_driver.getphy = fram_getphy;
  t_driver.release = fram_release;

  in_use = 1;

  return &amp;t_driver;
} /* fram_initfunc */
</pre>
<h1>F_GETPHY()</h1>
<p>This function returns information about the FRAM including the size (in sectors) and the size of the sectors (in bytes)</p>
<pre class="start-line:221 lang:c decode:true">/****************************************************************************
 *
 * ram_getphy
 *
 * determinate ramdrive physicals
 *
 * INPUTS
 *
 * driver - driver structure
 * phy - this structure has to be filled with physical information
 *
 * RETURNS
 *
 * error code or zero if successful
 *
 ***************************************************************************/
static int fram_getphy ( F_DRIVER * driver, F_PHY * phy )
{
  /* Not used. */
  ( void ) driver;

  phy-&gt;number_of_sectors = maxsector;
  phy-&gt;bytes_per_sector = F_SECTOR_SIZE;

  return MDRIVER_RAM_NO_ERROR;
}</pre>
<h1>FRAM Helper Functions</h1>
<p>In order to map sectors to the correct I2C address (remember the FRAM has two banks of memory in two different I2C addresses) and to the correct bank address, I created two function which map &#8220;sector&#8221; into address and I2C address.</p>
<pre class="start-line:63 lang:c decode:true">static const unsigned long maxsector = FDRIVER_VOLUME0_SIZE / F_SECTOR_SIZE;
static const unsigned long halfsector = FDRIVER_VOLUME0_SIZE / F_SECTOR_SIZE / 2;

/****************************************************************************
 *
 * calcAddress
 *
 * This function takes a sector and returns the address in the bank for the
 * start of the secor
 *
 * INPUTS
 *
 * unsigned long sector - which logical sector in the FRAM
 * 
 * RETURNS
 *
 * The FRAM Address of the sector
 *
 ***************************************************************************/
static inline uint32_t calcAddress(unsigned long sector)
{
    if(sector &lt; halfsector)
        return sector * F_SECTOR_SIZE;
    else
        return (sector-halfsector) * F_SECTOR_SIZE;
}

/****************************************************************************
 *
 * calcI2CAddress
 *
 * This function takes a sector from 0 --&gt; maxsector and figures out which bank
 * the address exists.
 *
 * INPUTS
 *
 * unsigned long sector - which logical sector in the FRAM to write to
 * 
 * RETURNS
 *
 * The I2C Address of Bank 0 or Bank 1
 *
 ***************************************************************************/
static inline uint32_t calcI2CAddress(unsigned long sector)
{
    if(sector &lt; halfsector)
        return 0x50; // I2C Bank 0 Address from the datasheet
    else
        return 0x51; // I2C Bank 0 Address - From the datasheet  
}
</pre>
<h1>F_READSECTOR()</h1>
<p>This function reads 512 bytes that are located in the sector into the RAM buffer pointed to by data.  Notice on lines 26 &amp; 27 I put in debugging information.  In order to read you need to</p>
<ul>
<li>Send a start (line 30)</li>
<li>Send the I2C address and the write bit</li>
<li>Set the address you want to read from (line 37-38)</li>
<li>Send a restart (line 41)</li>
<li>read the 512 bytes (lines 44-54)</li>
<li>On the last byte NAK (line 47-50)</li>
</ul>
<pre class="start-line:114 lang:c decode:true">/****************************************************************************
 *
 * fram_readsector
 *
 * This function reads 512 bytes into the SRAM at the pointer data
 *
 * INPUTS
 *
 * driver - driver structure
 * void *data - a pointer to the SRAM where the 512 bytes of data will be written
 * unsigned long sector - which logical sector in the FRAM to read from
 * 
 * RETURNS
 *
 * error code or MDRIVER_RAM_NO_ERROR if successful
 *
 ***************************************************************************/

static int fram_readsector ( F_DRIVER * driver, void * data, unsigned long sector )
{
    char buff[128]; // A scratch buffer for UART Printing
    (void)driver;
    uint16 address;
    uint32_t status;
    
    sprintf(buff,"Read sector %d\n",(int)sector);
    UART_UartPutString(buff);
    
    address = calcAddress(sector);
    status = I2C_I2CMasterSendStart( calcI2CAddress(sector),I2C_I2C_WRITE_XFER_MODE);
    if(status != I2C_I2C_MSTR_NO_ERROR)
    {
        UART_UartPutString("I2C Error\n");
        return MDRIVER_RAM_ERR_SECTOR;
    }
    int i;
    I2C_I2CMasterWriteByte((address&gt;&gt;8)&amp;0xFF);
    I2C_I2CMasterWriteByte(address &amp; 0xFF); // 
    
    
    I2C_I2CMasterSendRestart(calcI2CAddress(sector),I2C_I2C_READ_XFER_MODE);
    
    uint8_t d;
    for(i=0;i&lt;F_SECTOR_SIZE;i++)
    {
        
        if(i != F_SECTOR_SIZE - 1)
            d = I2C_I2CMasterReadByte(I2C_I2C_ACK_DATA);
        else
            d = I2C_I2CMasterReadByte(I2C_I2C_NAK_DATA);
         
        *((uint8_t *)data + i) = d;
        
    }
    
    I2C_I2CMasterSendStop();
    
  return MDRIVER_RAM_NO_ERROR;
}

</pre>
<h1>F_WRITESECTOR()</h1>
<p>The write sector is very similar to the read.  To write the data you need to</p>
<ul>
<li>Send a start (line 204)</li>
<li>Write the address you want to write to (line 205-206)</li>
<li>Write the 512 bytes (lines 208-214)</li>
<li>Send a stop (line 215)</li>
</ul>
<pre class="start-line:175 lang:c decode:true ">/****************************************************************************
 *
 * fram_writesector
 *
 * This function takes 512 bytes of user input and writes to the FRAM in the
 *
 * INPUTS
 *
 * driver - driver structure
 * void *data - a pointer to the SRAM where the 512 bytes of data exists
 * unsigned long sector - which logical sector in the FRAM to write to
 * 
 * RETURNS
 *
 * error code or MDRIVER_RAM_NO_ERROR if successful
 *
 ***************************************************************************/

static int fram_writesector ( F_DRIVER * driver, void * data, unsigned long sector )
{
    (void)driver;
    char buff[128]; // A scratch buffer for UART Printing
    uint16 address;
    int i;
        
    sprintf(buff,"Wrote sector %d\n",(int)sector);
    UART_UartPutString(buff);
        
    address = calcAddress(sector);
    I2C_I2CMasterSendStart(calcI2CAddress(sector),I2C_I2C_WRITE_XFER_MODE);
    I2C_I2CMasterWriteByte((address&gt;&gt;8)&amp;0xFF);
    I2C_I2CMasterWriteByte(address &amp; 0xFF); // 
    
    for(i=0;i&lt;F_SECTOR_SIZE;i++)
    {
        uint8_t d = *((uint8_t *)data +i);
       
        I2C_I2CMasterWriteByte(d); 
        
    }
    I2C_I2CMasterSendStop();
    
  return MDRIVER_RAM_NO_ERROR;
}
</pre>
<h1>F_GETSTATUS()</h1>
<p>As I wrote the article I noticed in the documentation that I needed to provide the &#8220;F_GETSTATUS&#8221; function.  The reason that I had not noticed before was that it was not in the media driver provided in the distribution.  That being said, my implementation always returns a 0 indicating OK.</p>
<pre class="start-line:269 lang:c decode:true">/****************************************************************************
 *
 * fram_getstatus
 *
 * This function must return the status of the drive... F_ST_MISSING, F_ST_CHANGED
 * or F_ST_WRPROECT or 0 (for OK)
 * INPUTS
 *
 * driver_param - driver parameter
 *
 *
 * For the FRAM I dont support any of these other status's
 ***************************************************************************/

static long fram_getstatus(F_DRIVER *driver)
{
    (void) driver;
    // F_ST_MISSING	The media is not present (it has been removed or was never inserted).
    //F_ST_CHANGED	Since F_GETSTATUS() was last called the media has either been removed and re-inserted, or a different media has been inserted.
    //F_ST_WRPROTECT	The media is write protected.
    
    return 0;
}</pre>
<h1>F_RELEASE()</h1>
<p>This function simply set the in_use to 0;</p>
<pre class="start-line:249 lang:c decode:true">/****************************************************************************
 *
 * fram_release
 *
 * Releases a drive
 *
 * INPUTS
 *
 * driver_param - driver parameter
 *
 ***************************************************************************/
static void fram_release ( F_DRIVER * driver )
{
  /* Not used. */
  ( void ) driver;

  /* Disk no longer in use. */
  in_use = 0;
}</pre>
<p>In the next article I will show you the source code for an example project using the FreeRTOS FAT SL Filesystem.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/freertos-fat-sl-filesystem-porting-to-psoc4m/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>FreeRTOS FAT SL FileSystem &#8211; Using the FM24V10 FRAM</title>
		<link>https://iotexpert.com/freertos-fat-sl-filesystem-part-1/</link>
					<comments>https://iotexpert.com/freertos-fat-sl-filesystem-part-1/#respond</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Wed, 11 Oct 2017 13:41:41 +0000</pubDate>
				<category><![CDATA[CY8CKIT-044]]></category>
		<category><![CDATA[FreeRTOS]]></category>
		<category><![CDATA[FreeRTOS FAT SL]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=4110</guid>

					<description><![CDATA[Summary Last week while working on a PSoC FreeRTOS Project, I looked at the CY8CKIT-044 Development kit that I was using and remembered that right under the Ambient Light Sensor there is a Cypress FM24V10 FRAM.  Way back, I started pushing our Development Kit team to put other Cypress parts onto the dev kits&#8230;. and [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>Last week while working on a PSoC FreeRTOS Project, I looked at the CY8CKIT-044 Development kit that I was using and remembered that right under the Ambient Light Sensor there is a <a href="http://www.cypress.com/file/41666/download" target="_blank" rel="noopener">Cypress FM24V10</a> FRAM.  Way back, I started pushing our Development Kit team to put other Cypress parts onto the dev kits&#8230;. and because they are awesome, they aggressively started doing that.  One of those chips is the FM24V10 FRAM.  As I looked at the chip, I realized again that I had never really written anything into the FM24V10 FRAM on any of our dev kits, which seems silly after I pushed the dev kit team for them.  I realized that the reason I had never used the FM24V10 FRAM was I didn&#8217;t have a nifty way to write files.  But, while working on the other FreeRTOS projects, I noticed the <a href="http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT_SL/FreeRTOS_Plus_FAT_SL.shtml" target="_blank" rel="noopener">FreeRTOS FAT SL</a> Filesystem is built into FreeRTOS.  The next couple (3?) articles I will talk about making the FreeRTOS FAT SL Filesystem work on a PSoC4200M with a FM24V10 I2C FRAM.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/07/IMG_4575.jpg"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/07/IMG_4575-1024x768.jpg" alt="CY8CKIT-044 with FM24V10 FRAM" width="1024" height="768" class="alignnone wp-image-3789 size-large" srcset="https://iotexpert.com/wp-content/uploads/2017/07/IMG_4575-1024x768.jpg 1024w, https://iotexpert.com/wp-content/uploads/2017/07/IMG_4575-600x450.jpg 600w, https://iotexpert.com/wp-content/uploads/2017/07/IMG_4575-300x225.jpg 300w, https://iotexpert.com/wp-content/uploads/2017/07/IMG_4575-768x576.jpg 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>The Cypress FM24V10 FRAM</h1>
<p>The &#8220;F&#8221; in FRAM stands for ferroelectric.  What that means is each of the memory cells is a little magnet.  So what you say?  Well the &#8220;what&#8221; is that FRAMs don&#8217;t wear out (unlike Flash), you can write individual cells (unlike Flash), and it is super low power, it is plenty fast (I2C = 3.4MBS), and it retains data for a long long time (151 years).</p>
<p>That list sounds great.  Actually really great.  But, what is the but?  There are two answers to that question.  The first answer is there is a density penalty, the biggest I2C FRAM is 1Mb where you can buy a 1Gb NOR Flash or even bigger NAND Flash.  The next issue is price.  The digikey pricing for a 128Kb FRAM is $12.76 or 9.9c/kb and a 1GB NOR Flash is $14.97 or <span>0.0014c/kb.  That means an FRAM is 6724 x as expensive per bit.</span></p>
<p>How do I use it?  The FRAM is attached to the I2C pins on the CY8CKIT-044.  That means that I can talk to it with either the <a href="https://iotexpert.com/?s=bridge+control+panel" target="_blank" rel="noopener">Bridge Control Panel</a>, or with the PSoC.  To start with I will show you the Bridge Control Panel.  First, I probe the I2C bus, and I see that there are three I2C devices.</p>
<ul>
<li>0x0F = The I2C Accelerometer (which I wrote about <a href="https://iotexpert.com/2017/06/29/psoc-freertos-kionix-kxtj2-accelerometer/" target="_blank" rel="noopener">here</a>)</li>
<li>0x50= The first 64K of FRAM</li>
<li>0x51 = The second 64K of FRAM</li>
</ul>
<p>The 128K FRAM is divided into two banks which are accessed on separate I2C addresses.  By splitting 128K into two blocks of 64K the chip designers optimized the addressing of the memory.  In order to access 64K you need 16-bits, which means to access 128K you need 17-bits.  This would have required sending a whole extra byte of address when you really only needed 1 of those 8 bits.  They avoid this problem by having two addresses.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-03-at-8.24.44-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-03-at-8.24.44-AM-1024x797.png" alt="" width="1024" height="797" class="alignnone size-large wp-image-4198" srcset="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-03-at-8.24.44-AM-1024x797.png 1024w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-03-at-8.24.44-AM-600x467.png 600w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-03-at-8.24.44-AM-300x234.png 300w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-03-at-8.24.44-AM-768x598.png 768w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-03-at-8.24.44-AM.png 1736w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>The FM24V10 FRAM is easy to use as it implements an EzI2C (like) protocol.  EZI2C keeps an internal address pointer.  The pointer is automatically incremented each time you do an I2C transaction.  When you do a write transaction, the first two bytes you write are placed into the address pointer.  I say &#8220;EZI2C like&#8221; because the pointer is not retained between transactions.  Some examples:</p>
<p>If I want to write 0x56 into location 0x0123 I would issue</p>
<ul>
<li>I2C Start</li>
<li>I2C Write to I2C address 0x50</li>
<li>Send 0x01, 0x02 (which write the address pointer)</li>
<li>Send 0x56</li>
<li>I2C Stop</li>
</ul>
<p>I can do that sequence with the Bridge Control Panel command &#8220;W 50 01 23 56 P;&#8221;  The &#8220;W&#8221; stands for &#8220;send a start and send the 7-bit address 0x50 (left shifted 1) and send a &#8216;0&#8217; for write&#8221;.  For some reason the engineers at Philips decided that the I2C protocol would have 7-bit addressing and then 1 bit to represent write or read.  When you talk about I2C there is always confusion about &#8220;8-bit address&#8221; versus &#8220;7-bit address&#8221;.  When they talk about 8-bit address they are talking about the 7-bit address shifted left 1 bit and then or-ed with the read or write bit.  Here is a picture from the bridge control panel:</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-11-at-8.32.38-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-11-at-8.32.38-AM.png" alt="" width="839" height="680" class="alignnone size-full wp-image-4204" srcset="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-11-at-8.32.38-AM.png 839w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-11-at-8.32.38-AM-600x486.png 600w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-11-at-8.32.38-AM-300x243.png 300w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-11-at-8.32.38-AM-768x622.png 768w" sizes="(max-width: 839px) 100vw, 839px" /></a></p>
<p>And the same thing from the logic analyzer.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-11-at-8.33.49-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-11-at-8.33.49-AM-1024x372.png" alt="" width="1024" height="372" class="alignnone size-large wp-image-4205" srcset="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-11-at-8.33.49-AM-1024x372.png 1024w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-11-at-8.33.49-AM-600x218.png 600w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-11-at-8.33.49-AM-300x109.png 300w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-11-at-8.33.49-AM-768x279.png 768w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-11-at-8.33.49-AM.png 1108w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Once I have written 0x56 into address 0x0123, I can read it back by a &#8220;w 50 01 23 r 50 x p;&#8221; which sends:</p>
<ul>
<li>I2C Start</li>
<li>I2C Write to I2C address 0x50</li>
<li>Send 0x01, 0x02 (which write the address pointer)</li>
<li>I2C Restart</li>
<li>Sends 0x50 (shifted left) or-ed with &#8220;read&#8221;</li>
<li>Reads the byte</li>
<li>I2C Stop</li>
</ul>
<p>In the transaction output window below, you can see that the &#8220;x&#8221; is replaced with &#8220;56&#8221; meaning that it read the previously written value back.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-11-at-8.39.40-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-11-at-8.39.40-AM.png" alt="" width="836" height="672" class="alignnone size-full wp-image-4206" srcset="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-11-at-8.39.40-AM.png 836w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-11-at-8.39.40-AM-600x482.png 600w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-11-at-8.39.40-AM-300x241.png 300w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-11-at-8.39.40-AM-768x617.png 768w" sizes="(max-width: 836px) 100vw, 836px" /></a></p>
<p>The only other interesting thing about the protocol is that once you have sent and address, you can keep reading, or writing without sending the address again.  For instance to write 01, 02, 03, 04 to address 23, 23,25 and 26 you can send &#8220;W 50 00 23 01 02 03 04 p&#8221;, then you can read it back with &#8220;W 50 00 23 r 50 x x x x p;&#8221;</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-11-at-8.43.24-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-11-at-8.43.24-AM.png" alt="" width="838" height="674" class="alignnone size-full wp-image-4207" srcset="https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-11-at-8.43.24-AM.png 838w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-11-at-8.43.24-AM-600x483.png 600w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-11-at-8.43.24-AM-300x241.png 300w, https://iotexpert.com/wp-content/uploads/2017/10/Screen-Shot-2017-10-11-at-8.43.24-AM-768x618.png 768w" sizes="(max-width: 838px) 100vw, 838px" /></a></p>
<p>In the next Article I will start the process of using the Free RTOS File System to write and read from the I2C FRAM.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/freertos-fat-sl-filesystem-part-1/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>FreeRTOS PSoC Template Project</title>
		<link>https://iotexpert.com/freertos-psoc-template-project/</link>
					<comments>https://iotexpert.com/freertos-psoc-template-project/#comments</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Thu, 31 Aug 2017 12:24:28 +0000</pubDate>
				<category><![CDATA[CY8CKIT-044]]></category>
		<category><![CDATA[FreeRTOS]]></category>
		<category><![CDATA[PSoC 4200]]></category>
		<category><![CDATA[PSoC Creator]]></category>
		<category><![CDATA[Tracealyzer]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=4097</guid>

					<description><![CDATA[Summary In the last article I showed you clever FreeRTOS PSoC Component&#8230; and the talked about some of the issues that I had with it.  In this article I will talk about a self-contained FreeRTOS PSoC Template project that includes everything for FreeRTOS and Tracealyzer all in one project.  My idea was that when I [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>In the last <a href="https://iotexpert.com/2017/08/29/freertos-psoc-component/">article</a> I showed you clever FreeRTOS PSoC Component&#8230; and the talked about some of the issues that I had with it.  In this article I will talk about a self-contained FreeRTOS PSoC Template project that includes everything for FreeRTOS and Tracealyzer all in one project.  My idea was that when I start a new project I will just make a copy of the template project, rename it, then move on.</p>
<p>My FreeRTOS PSoC Template has</p>
<ul>
<li>FreeRTOS with the files in in the same directory (i.e. not referenced externally)</li>
<li>Tracealyzer with the files in the same directory &amp; all of the streamports including RTT and the UART DMA</li>
<li>All of the files organized into PSoC Creator folders</li>
<li>A template main.c</li>
<li>All of the build settings configured</li>
</ul>
<p>This project is checked into <a href="https://github.com/iotexpert/PSoC-FreeRTOS-Template" target="_blank" rel="noopener">GitHub</a> and you can find it git@<span>github.com:iotexpert/PSoC-FreeRTOS-Template.git</span></p>
<h1>Building the FreeRTOS PSoC Template</h1>
<p>I started the process by creating a blank PSoC project called &#8220;FreeRTOS_Template&#8221;.  I then went into the Windows Explorer and copied the entire FreeRTOS source directory into the FreeRTOS PSoC Template Project directory.  Then I copied the Percepio TraceRecorder library into the FreeRTOS PSoC Template project.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.23.00-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.23.00-AM.png" alt="FreeRTOS PSoC Template Directory Structure" width="660" height="613" class="alignnone wp-image-4099 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.23.00-AM.png 660w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.23.00-AM-600x557.png 600w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.23.00-AM-300x279.png 300w" sizes="(max-width: 660px) 100vw, 660px" /></a></p>
<p>By having both of the source directories in my project it isolated this project from changes in the FreeRTOS or the TraceRecorder.  Obviously that is a double edged sword.  The next thing that I did was use the Windows Explorer to copy FreeRTOSConfig.h, trcConfig.h, trcSnapshotConfig.h and trcStreamingConfig.h into the project.   Once all of the files were in my project directory is was time to fix up the PSoC Creator Project.</p>
<p>To do this I created a folder called &#8220;FreeRTOS_Source&#8221; in the &#8220;Header Files&#8221; and &#8220;Source Files&#8221; folders in my template project.  You can do this by right clicking and selecting &#8220;Add-&gt;New Folder&#8221;.  Then I added all of the appropriate .h and .c files from FreeRTOS source directory.  This gives me the following project view:</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.29.53-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.29.53-AM.png" alt="FreeRTOS PSoC Template PSoC Creator Header Files" width="243" height="296" class="alignnone wp-image-4101 size-full" /></a></p>
<p>And Source Files (notice that I also added heap_4.c which I generally use for memory management)</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.30.04-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.30.04-AM.png" alt="FreeRTOS PSoC Template PSoC Creator Source Files" width="238" height="219" class="alignnone wp-image-4100 size-full" /></a></p>
<p>Then I add the configuration files FreeRTOSConfig.h, trcConfig.h, trcSnapshotConfig.h, trcStreamingConfig.h and trcStreamingport.h.  Next I do the same thing for the TraceRecorder library which makes my project look like this:</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.33.16-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.33.16-AM.png" alt="" width="328" height="526" class="alignnone size-full wp-image-4102" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.33.16-AM.png 328w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.33.16-AM-187x300.png 187w" sizes="(max-width: 328px) 100vw, 328px" /></a></p>
<p>Then I modify the build settings to add the include directories:</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.34.39-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.34.39-AM.png" alt="FreeRTOS PSoC Template Build Settings" width="754" height="493" class="alignnone wp-image-4104 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.34.39-AM.png 754w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.34.39-AM-600x392.png 600w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.34.39-AM-300x196.png 300w" sizes="(max-width: 754px) 100vw, 754px" /></a></p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.34.49-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.34.49-AM.png" alt="FreeRTOS PSoC Template PSoC Creator Include Path" width="352" height="311" class="alignnone wp-image-4103 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.34.49-AM.png 352w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.34.49-AM-300x265.png 300w" sizes="(max-width: 352px) 100vw, 352px" /></a></p>
<p>Now you can modify the FreeRTOSConfig.h to include all of the Tracealyzer stuff:</p>
<pre class="start-line:89 lang:c decode:true ">/* A header file that defines trace macro can be included here. */
#if ( configUSE_TRACE_FACILITY == 1 )
#include "trcRecorder.h"
#endif

#endif /* FREERTOS_CONFIG_H */</pre>
<p>Then I setup a new tab in the schematic to contain the DMA UART Streamport.  You can read all about the UART DMA Streamport in this <a href="https://iotexpert.com/2017/08/08/percepio-tracealyzer-a-psoc-dma-streamport/" target="_blank" rel="noopener">article</a>.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.37.44-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.37.44-AM.png" alt="FreeRTOS PSoC Template PSoC Creator Streamport Schematic" width="983" height="1004" class="alignnone wp-image-4105 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.37.44-AM.png 983w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.37.44-AM-600x613.png 600w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.37.44-AM-294x300.png 294w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.37.44-AM-768x784.png 768w" sizes="(max-width: 983px) 100vw, 983px" /></a></p>
<p>By putting that part of the stream port on a separate schematic page I can now do a right click and disable the page when I am not using the Tracealyzer streamport.  Disabling a page of the schematic completely removes everything from the build.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.40.39-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.40.39-AM.png" alt="PSoC Creator Schematic Disable Settings" width="329" height="160" class="alignnone wp-image-4106 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.40.39-AM.png 329w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-31-at-7.40.39-AM-300x146.png 300w" sizes="(max-width: 329px) 100vw, 329px" /></a></p>
<p>Next I create files called FreeRTOS_Start.h/.c to put in the startup code:</p>
<pre class="lang:c decode:true ">#include &lt;project.h&gt;
#include "FreeRTOS.h"

extern void xPortPendSVHandler(void);
extern void xPortSysTickHandler(void);
extern void vPortSVCHandler(void);

#define CORTEX_INTERRUPT_BASE          (16)
void FreeRTOS_Start()
{
    /* Handler for Cortex Supervisor Call (SVC, formerly SWI) - address 11 */
    CyIntSetSysVector( CORTEX_INTERRUPT_BASE + SVCall_IRQn,
        (cyisraddress)vPortSVCHandler );
    
    /* Handler for Cortex PendSV Call - address 14 */
	CyIntSetSysVector( CORTEX_INTERRUPT_BASE + PendSV_IRQn,
        (cyisraddress)xPortPendSVHandler );    
    
    /* Handler for Cortex SYSTICK - address 15 */
	CyIntSetSysVector( CORTEX_INTERRUPT_BASE + SysTick_IRQn,
        (cyisraddress)xPortSysTickHandler );
}
</pre>
<p>Finally I make a template main.c that starts everything and has a simple ledTask and instructions for changing Memory Management scheme and the TraceRecorder.</p>
<pre class="lang:c decode:true ">// This template has heap_4.c included in the project.  If you want a 
// different heap scheme then remove heap_4 from the project and add 
// the other scheme from FreeRTOS/Source/portable/memmag

// TraceRecorder
// There are methods
// 1. Snapshot mode
// 2. Streaming UART/DMA
// 3. Streaming JLINK RTT
//
// To use method 1: 
// - in FreeRTOSConfig.h make this line be 1 (currently line 45)
//#define configUSE_TRACE_FACILITY                1
// - in trcConfig.h configure the TRC_CFG_RECORDER 
// #define TRC_CFG_RECORDER_MODE TRC_RECORDER_MODE_SNAPSHOT
//
// To use method 2:
// - in FreeRTOSConfig.h make this line be 1 (currently line 45)
//#define configUSE_TRACE_FACILITY                1
// - in trcConfig.h configure the TRC_CFG_RECORDER
//#define TRC_CFG_RECORDER_MODE TRC_RECORDER_MODE_STREAMING
//
// This project currently has the PSoC UART Streaming Port
// add the TraceRecorder/streamports/PSoC_Serial/trcStreamingPort.c to the project
// add the TraceRecorder/streamports/PSoC_Serial/include/trcStreamingPort.h
// add the TraceRecorder/streamports/PSoC_Serial/include to the compiler include directories
// Enable the Trace_DMA schematic sheet and make sure UART pins are assigned correctly
// This port depends on the UART being named UART and the DMA being named "DMA"
//
// To use method 3: JLINK RTT
// Remove the previous streamport files from the project
// Remove the Streamport include path
// add the TraceRecorder/streamports/JLink_RTT/Segger_RTT.c to the project
// add the TraceRecorder/streamports/JLink_RTT/Segger_RTT_Printf.c to the project
// add the TraceRecorder/streamports/JLink_RTT/include/trcStreamingPort.h
// add the TraceRecorder/streamports/JLink_RTT/include/Segger_RTT.h
// add the TraceRecorder/streamports/JLink_RTT/include/Segger_RTT_Conf.h
// add the TraceRecorder/streamports/JLink_RTT/include to the compiler include directories


#include "project.h"
#include "FreeRTOS.h"
#include "timers.h"

// An example Task
void ledTask(void *arg)
{
    (void)arg;
    while(1)
    {
        RED_Write(~RED_Read());
        vTaskDelay(500);  
    }
}

int main(void)
{
    CyGlobalIntEnable;
    FreeRTOS_Start();
    
    #if ( configUSE_TRACE_FACILITY == 1 )
    vTraceEnable(TRC_START);
    #endif
    
    xTaskCreate(ledTask,"LED Task",configMINIMAL_STACK_SIZE,0,1,0);
    vTaskStartScheduler();  // Will never return
    while(1);               // Eliminate compiler warning
}
</pre>
<p>&nbsp;</p>
<p><span><p><div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >Topic</th>
<th >Description</th>
</tr>
</thead>
<tbody>
<tr><td ><a href="https://iotexpert.com/2017/02/23/psoc4-freertos-port/" target="_blank" rel="noopener noreferrer">FreeRTOS: A PSoC4 FreeRTOS Port</a></td>
<td >An introduction to making FreeRTOS work on PSoC 4</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/05/23/freertos-psoc-examples/" target="_blank" rel="noopener noreferrer">FreeRTOS PSoC Examples</a></td>
<td >Using multiple tasks in FreeRTOS</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/05/25/psoc-freertos-queue-example/" target="_blank" rel="noopener noreferrer">FreeRTOS Queue Example</a></td>
<td >Using a queue to communicate between tasks</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/05/30/psoc-6-freertos-first-example/" target="_blank" rel="noopener noreferrer">PSoC 6 FreeRTOS - The First Example</a></td>
<td >Booting FreeRTOS on PSoC 6</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/06/01/psoc-freertos-binary-semaphore/" target="_blank" rel="noopener noreferrer">FreeRTOS Binary Semaphore</a></td>
<td >An first example of a binary semaphore</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/06/08/psoc-freertos-binary-semaphore-part-2/">FreeRTOS Binary Semaphore (Part 2)</td>
<td >Removing polling in the UART Task</a></td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/06/20/psoc-freertos-counting-semaphore/" target="_blank" rel="noopener">FreeRTOS Counting Semaphore</a></td>
<td >An example using a counting semaphore</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/06/22/psoc-freertos-sharing-the-i2c-bus/" target="_blank" rel="noopener">PSoC FreeRTOS</a></td>
<td >Reading I2C Sensors with a shared I2C Bus</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/08/22/psoc-freertos-task-notify/" target="_blank" rel="noopener">PSoC FreeRTOS Task Notify</a></td>
<td >A light weight scheme to replace Semaphores</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/08/24/psoc-freertos-task-notification-value/" target="_blank" rel="noopener">PSoC FreeRTOS Task Notification Values</a></td>
<td >A very light weight method to transfer one word of information into a task</td>
</tr>
</tbody></table></div></p></span></p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/freertos-psoc-template-project/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
		<item>
		<title>FreeRTOS PSoC Component</title>
		<link>https://iotexpert.com/freertos-psoc-component/</link>
					<comments>https://iotexpert.com/freertos-psoc-component/#respond</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Tue, 29 Aug 2017 10:00:22 +0000</pubDate>
				<category><![CDATA[CY8CKIT-044]]></category>
		<category><![CDATA[FreeRTOS]]></category>
		<category><![CDATA[PSoC 4200]]></category>
		<category><![CDATA[Tracealyzer]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=4068</guid>

					<description><![CDATA[Summary This weekend I found myself down a rabbit hole, beating my head on the wall trying to make a USB driver work properly inside of Parallels &#38; Windows 10 on my Mac (which I still don&#8217;t have quite right).  While going through that excruciating process I ended up creating 3-4-5 different FreeRTOS PSoC projects, [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>This weekend I found myself down a rabbit hole, beating my head on the wall trying to make a USB driver work properly inside of Parallels &amp; Windows 10 on my Mac (which I still don&#8217;t have quite right).  While going through that excruciating process I ended up creating 3-4-5 different FreeRTOS PSoC projects, which although not difficult, is still a bit of a pain as it requires 1/2 dozen step.  After reflecting on it a while I decided that I needed something easier.  The obvious thing to do was to build a FreeRTOS PSoC Component, which I did, but eventually abandoned (Ill talk about that process in the next section).  After the component idea was abandoned I decided to just make a template project that could be used to start a FreeRTOS PSoC Project (the next Article).</p>
<p>In this article I will show you:</p>
<ul>
<li>An example project using a clever FreeRTOS PSoC Component that I found on GitHub built by <a href="http://www.e2forlife.com" target="_blank" rel="noopener">E2ForLife</a></li>
<li>A discussion of the implementation details of that FreeRTOS PSoC Component</li>
<li>A discussion of problems with that implementation which lead me to build a template project.</li>
</ul>
<h1>FreeRTOS PSoC Component Example Project</h1>
<p>When I first started looking at FreeRTOS I wasn&#8217;t totally sure where to start.  When I googled FreeRTOS PSoC, one of the first hits was this <a href="https://github.com/e2forlife/PSoC-FreeRTOS" target="_blank" rel="noopener">repo</a> which contains a PSoC Component that was built by &#8220;E2ForLife&#8221;.  I really liked the idea of a component based FreeRTOS implementation, as all you would need to do is place the components&#8230; and then away you go.  The more that I looked at the component the more that I liked what E2ForLife had done.  When I cloned his repo, there was &#8220;nothing&#8221; in it, but it turns out there is another branch called &#8220;Implement-PSoC5&#8221; which makes me think that he was partially done (he hasn&#8217;t been changed since August 2015)</p>
<p>First, his example project.  When you look at the schematic you see a nice looking FreeRTOS PSoC Symbol (obviously E2ForLife draws way better than me).</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-8.20.07-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-8.20.07-AM.png" alt="FreeRTOS PSoC Component" width="696" height="572" class="alignnone wp-image-4072 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-8.20.07-AM.png 696w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-8.20.07-AM-600x493.png 600w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-8.20.07-AM-300x247.png 300w" sizes="(max-width: 696px) 100vw, 696px" /></a></p>
<p>When you double click the component you see all of the stuff that is normally in FreeRTOSConfig.h which you can set using the GUI instead of the FreeRTOSConfig.h</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-8.20.23-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-8.20.23-AM-1024x975.png" alt="FreeRTOS PSoC Component Configuration" width="1024" height="975" class="alignnone wp-image-4071 size-large" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-8.20.23-AM-1024x975.png 1024w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-8.20.23-AM-600x571.png 600w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-8.20.23-AM-300x286.png 300w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-8.20.23-AM-768x731.png 768w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-8.20.23-AM.png 1294w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>When you &#8220;Generate Application&#8221; you can see that he setup all of the FreeRTOS files to come into the Generated Source automatically (though with different names)</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-9.43.13-AM.png"></a><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-9.43.13-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-9.43.13-AM.png" alt="FreeRTOS PSoC Component API" width="283" height="672" class="alignnone wp-image-4075 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-9.43.13-AM.png 283w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-9.43.13-AM-126x300.png 126w" sizes="(max-width: 283px) 100vw, 283px" /></a></p>
<p>And his FreeRTOS PSoC Component example project is straight forward</p>
<pre class="lang:c decode:true ">#include &lt;project.h&gt;

xTaskHandle redTask;
xTaskHandle blueTask;
xTaskHandle greenTask;

void vRedTask( void* pvParameters);
void vGreenTask( void* pvParameters);
void vBlueTask( void* pvParameters);

int main()
{
    CyGlobalIntEnable; /* Enable global interrupts. */

	xTaskCreate(vRedTask,"red",80,NULL,3,&amp;redTask);
	xTaskCreate(vGreenTask,"green",80,NULL,3,&amp;greenTask);

	FreeRTOS_Start();
	
    for(;;);
}

void vRedTask( void* pvParameters)
{
	for(;;) {
		vTaskDelay( 125/portTICK_RATE_MS );
		RED_Write( ~RED_Read() );
	}
}

void vGreenTask( void* pvParameters)
{
	for(;;) {
		vTaskDelay( 219/portTICK_RATE_MS );
		GREEN_Write( ~GREEN_Read() );
	}
}
</pre>
<p>He built a function called &#8220;FreeRTOS_Start()&#8221; which installs the interrupt vectors then starts up the scheduler.</p>
<pre class="lang:c decode:true ">uint8 FreeRTOS_initVar = 0;

/* ------------------------------------------------------------------------ */
void FreeRTOS_Init( void )
{
    /* Handler for Cortex Supervisor Call (SVC, formerly SWI) - address 11 */
    CyIntSetSysVector( CORTEX_INTERRUPT_BASE + SVCall_IRQn,
        (cyisraddress)vPortSVCHandler );
    
    /* Handler for Cortex PendSV Call - address 14 */
	CyIntSetSysVector( CORTEX_INTERRUPT_BASE + PendSV_IRQn,
        (cyisraddress)xPortPendSVHandler );    
    
    /* Handler for Cortex SYSTICK - address 15 */
	CyIntSetSysVector( CORTEX_INTERRUPT_BASE + SysTick_IRQn,
        (cyisraddress)xPortSysTickHandler );
	
	FreeRTOS_initVar = 1;
}
/* ------------------------------------------------------------------------ */
void FreeRTOS_Enable( void )
{
	/* start the scheduler so the tasks will start executing */
	vTaskStartScheduler();	
}
/* ------------------------------------------------------------------------ */
void FreeRTOS_Start( void )
{
	if (FreeRTOS_initVar == 0) {
		FreeRTOS_Init();
	}
	FreeRTOS_Enable();
	
	/*
	 * After the scheduler starts in Enable(), the code should never get to
	 * this location.
	 */
	for (;;);
}
</pre>
<p>And when you run the FreeRTOS PSoC Component project you get the nice blinking LED (Red and Green)</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/IMG_4852.jpg"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/IMG_4852-1024x768.jpg" alt="CY8CKIT-042" width="1024" height="768" class="alignnone wp-image-4076 size-large" srcset="https://iotexpert.com/wp-content/uploads/2017/08/IMG_4852-1024x768.jpg 1024w, https://iotexpert.com/wp-content/uploads/2017/08/IMG_4852-300x225.jpg 300w, https://iotexpert.com/wp-content/uploads/2017/08/IMG_4852-768x576.jpg 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>FreeRTOS PSoC Component</h1>
<p>After downloading the project, then opening it I first wanted to look at the components.  You can do this by clicking the &#8220;Components&#8221; tab in the workspace explorer.  It looks like he created (or was planning to create) several other components in addition to the &#8220;FreeRTOS&#8221;.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.04.59-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.04.59-AM.png" alt="FreeRTOS PSoC Component" width="367" height="369" class="alignnone wp-image-4082 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.04.59-AM.png 367w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.04.59-AM-100x100.png 100w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.04.59-AM-150x150.png 150w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.04.59-AM-298x300.png 298w" sizes="(max-width: 367px) 100vw, 367px" /></a></p>
<p>When you double click on the &#8220;FreeRTOS_v8_2.cysym&#8221; you will get an editable view of the symbol.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.10.04-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.10.04-AM.png" alt="FreeRTOS PSoC Component Symbol" width="351" height="328" class="alignnone wp-image-4083 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.10.04-AM.png 351w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.10.04-AM-300x280.png 300w" sizes="(max-width: 351px) 100vw, 351px" /></a></p>
<p>When you right click on the blank part of the canvas, PSoC Creator will bring up this menu.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.11.54-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.11.54-AM.png" alt="PSoC Creator Configuration " width="191" height="173" class="alignnone wp-image-4084 size-full" /></a></p>
<p>On the properties menu you can configure which tab the component belongs to in the Component Catalog.  In this case he created a tab called &#8220;Community&#8221; which looks like this in the actual Component Catalog.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.14.31-AM.png"></a><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.14.31-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.14.31-AM.png" alt="PSoC Creator Component Catalog" width="276" height="201" class="alignnone wp-image-4086 size-full" /></a></p>
<p>To make that happen he setup the &#8220;Doc.CatalogPlacement&#8221; to be &#8220;Community/Operating System/FreeRTOS&#8221; (on the properties menu)</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.11.09-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.11.09-AM-1024x687.png" alt="FreeRTOS PSoC Component Parameter Configuration" width="1024" height="687" class="alignnone wp-image-4085 size-large" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.11.09-AM-1024x687.png 1024w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.11.09-AM-600x402.png 600w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.11.09-AM-300x201.png 300w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.11.09-AM-768x515.png 768w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.11.09-AM.png 1045w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>The next thing that he did was add a whole bunch of Symbol Parameters which will let the user change the setup of FreeRTOS. Each  of these parameters show up as an editable field on the component customers (in the schematic).</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.03.29-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.03.29-AM.png" alt="FreeRTOS PSoC Parameter" width="988" height="656" class="alignnone wp-image-4079 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.03.29-AM.png 988w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.03.29-AM-600x398.png 600w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.03.29-AM-300x199.png 300w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.03.29-AM-768x510.png 768w" sizes="(max-width: 988px) 100vw, 988px" /></a></p>
<p>In the picture above you can see that he created two new &#8220;enumerated&#8221; datatypes called &#8220;FreeRTOS_CheckStackOverFlowType&#8221; &#8220;FreeRTOS_MemMangType&#8221;.  In the picture below you can see the legal values for that type.  This lets him restrict the things that the user of the component can select when he places it in his schematic.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.03.05-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.03.05-AM.png" alt="FreeRTOS PSoC Component Configuration" width="927" height="590" class="alignnone wp-image-4080 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.03.05-AM.png 927w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.03.05-AM-600x382.png 600w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.03.05-AM-300x191.png 300w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.03.05-AM-768x489.png 768w" sizes="(max-width: 927px) 100vw, 927px" /></a></p>
<p>Here is what the component looks like when the user actually uses it.  You can see the legal fields (from above) and the legal values for those fields (from above)</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.21.36-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.21.36-AM.png" alt="FreeRTOS PSoC Component Configuration" width="781" height="690" class="alignnone wp-image-4087 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.21.36-AM.png 781w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.21.36-AM-600x530.png 600w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.21.36-AM-300x265.png 300w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-11.21.36-AM-768x679.png 768w" sizes="(max-width: 781px) 100vw, 781px" /></a></p>
<p>The next thing that he did was copy all of the FreeRTOS files into component API.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-12.32.09-PM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-12.32.09-PM.png" alt="FreeRTOS PSoC Component API" width="304" height="574" class="alignnone wp-image-4090 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-12.32.09-PM.png 304w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-28-at-12.32.09-PM-159x300.png 159w" sizes="(max-width: 304px) 100vw, 304px" /></a></p>
<p>If you remember each FreeRTOS project needs to have a file called &#8220;FreeRTOSConfig.h&#8221;.  But, that file doesn&#8217;t appear to exist in the list above.  How is that?  Remember that when PSoC Creator builds a project it copies the API into your generated source directory, but it renames each of the files to be INSTANCE_NAME_filename.  In this example, &#8220;Config.h&#8221; will become &#8220;FreeRTOS_Config.h&#8221; (FreeRTOS is the instance name in the schematic)</p>
<p>In FreeRTOS each of the features of the RTOS turn into a #define that is 0 or 1.  When the component was created he modified &#8220;Config.h&#8221; so that each of the parameters on the component set values for the #defines in the &#8220;Config.h&#8221; file.  And there is a bunch of them so that must have taken a good bit of work.  Here is an example of a section of his &#8220;Config.h&#8221;.  Each place you see the back-tick $parameter back-tick PSoC Creator will substitute the value of the parameter (I am typing back-tick because WordPress interprets the actual symbol to mean something special and I don&#8217;t know how to get it into the text)</p>
<pre class="lang:c decode:true ">#if CY_PSOC4
    
    /* Port-dependent settings - not user-editable */
    #define configCPU_CLOCK_HZ          ( ( unsigned long ) CYDEV_BCLK__SYSCLK__HZ )
        
    /* Application settings - user editable */
    #define configMAX_PRIORITIES		( `$MAX_PRIORITIES` )
    #define configTOTAL_HEAP_SIZE		( `$TOTAL_HEAP_SIZE` )
    
#elif CY_PSOC5
    
    /* Device settings - not user-editable */
    #define configCPU_CLOCK_HZ			( ( unsigned long ) BCLK__BUS_CLK__HZ )
        
    /* Application settings - user editable */
    #define configMAX_PRIORITIES        ( `$MAX_PRIORITIES` )
    #define configTOTAL_HEAP_SIZE		( `$TOTAL_HEAP_SIZE` ) 
    
#else
    
    #error "This FreeRTOSConfig.h file is for PSoC 4 and PSoC 5LP devices only"
    
#endif

#define configUSE_PREEMPTION			`$USE_PREEMPTION`
#define configUSE_IDLE_HOOK				`$USE_IDLE_HOOK`
#define configUSE_TICK_HOOK				`$USE_TICK_HOOK`
#define configTICK_RATE_HZ				( ( portTickType ) `$TICK_RATE_HZ` )</pre>
<p>Because each filename changes name, he had to go fix all of the #includes to look like this example from croutine.c (this was a bunch of work)</p>
<pre class="lang:c decode:true ">/* PSoC Component Customizations */

#include "`$INSTANCE_NAME`.h"
#include "`$INSTANCE_NAME`_task.h"
#include "`$INSTANCE_NAME`_croutine.h"</pre>
<p>He also wanted to be able to have all of the <a href="http://www.freertos.org/a00111.html" target="_blank" rel="noopener">FreeRTOS memory management schemes</a> included in the project at one time.  To solve this problem he put an #if around all of the code in heap_1, heap_2 &#8230; that adds and removes the appropriate memory manager.</p>
<pre class="lang:c decode:true ">#if (`$MemManager` == 2)

.
.
.
.
.

#endif</pre>
<h1>A FreeRTOS PSoC Template Project</h1>
<p>When I started working on FreeRTOS I knew that I wanted to use V9.0.  But the component was setup for V8.2.  So I launched into the process of converting the component.  This turned out to be a colossal pain in the ass because of the massive number of &#8220;small&#8221; changes.  In addition I didn&#8217;t really like having to change the names of key files (even the stupidly named semphr.h).  Moreover, the source code as a component debate was raging wildly inside of Cypress and in general is on the way out.  So I decided to implement this as a template project instead of a component.</p>
<p>All that being said, what <a href="http://www.e2forlife.com" target="_blank" rel="noopener">E2ForLife</a> did I continue to think was really clever.</p>
<p><span><p><div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >Topic</th>
<th >Description</th>
</tr>
</thead>
<tbody>
<tr><td ><a href="https://iotexpert.com/2017/02/23/psoc4-freertos-port/" target="_blank" rel="noopener noreferrer">FreeRTOS: A PSoC4 FreeRTOS Port</a></td>
<td >An introduction to making FreeRTOS work on PSoC 4</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/05/23/freertos-psoc-examples/" target="_blank" rel="noopener noreferrer">FreeRTOS PSoC Examples</a></td>
<td >Using multiple tasks in FreeRTOS</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/05/25/psoc-freertos-queue-example/" target="_blank" rel="noopener noreferrer">FreeRTOS Queue Example</a></td>
<td >Using a queue to communicate between tasks</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/05/30/psoc-6-freertos-first-example/" target="_blank" rel="noopener noreferrer">PSoC 6 FreeRTOS - The First Example</a></td>
<td >Booting FreeRTOS on PSoC 6</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/06/01/psoc-freertos-binary-semaphore/" target="_blank" rel="noopener noreferrer">FreeRTOS Binary Semaphore</a></td>
<td >An first example of a binary semaphore</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/06/08/psoc-freertos-binary-semaphore-part-2/">FreeRTOS Binary Semaphore (Part 2)</td>
<td >Removing polling in the UART Task</a></td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/06/20/psoc-freertos-counting-semaphore/" target="_blank" rel="noopener">FreeRTOS Counting Semaphore</a></td>
<td >An example using a counting semaphore</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/06/22/psoc-freertos-sharing-the-i2c-bus/" target="_blank" rel="noopener">PSoC FreeRTOS</a></td>
<td >Reading I2C Sensors with a shared I2C Bus</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/08/22/psoc-freertos-task-notify/" target="_blank" rel="noopener">PSoC FreeRTOS Task Notify</a></td>
<td >A light weight scheme to replace Semaphores</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/08/24/psoc-freertos-task-notification-value/" target="_blank" rel="noopener">PSoC FreeRTOS Task Notification Values</a></td>
<td >A very light weight method to transfer one word of information into a task</td>
</tr>
</tbody></table></div></p></span></p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/freertos-psoc-component/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>PSoC FreeRTOS Task Notification Value</title>
		<link>https://iotexpert.com/psoc-freertos-task-notification-value/</link>
					<comments>https://iotexpert.com/psoc-freertos-task-notification-value/#respond</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Thu, 24 Aug 2017 09:00:28 +0000</pubDate>
				<category><![CDATA[CY8CKIT-044]]></category>
		<category><![CDATA[FreeRTOS]]></category>
		<category><![CDATA[PSoC 4200]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=4042</guid>

					<description><![CDATA[Summary In the previous article I showed you how to use the FreeRTOS task notification mechanism to replace a binary semaphore.  In this article I will build PSoC FreeRTOS Task Notification Value firmware.  Every FreeRTOS task has one built-in &#8220;uint32_t&#8221; which you can use to pass information between the task that sends the notification and [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>In the previous article I showed you how to use the FreeRTOS task notification mechanism to replace a binary semaphore.  In this article I will build PSoC FreeRTOS Task Notification Value firmware.  Every FreeRTOS task has one built-in &#8220;uint32_t&#8221; which you can use to pass information between the task that sends the notification and the task that receives the notification.  In this article, instead of using the task notification as a binary semaphore, I will use it as a Queue of depth 1 that can hold one 32-bit word.  I will also show you a lesson that I learned about the interrupt registers in the SCB UART.</p>
<h1>PSoC FreeRTOS Task Notification Value</h1>
<p>I start by copying the project 9-TaskNotify and calling it 10-TaskNotifyValue.  I thought that it would be interesting to pass the cause of the UART interrupt to the UART Task, so that it could be told to the user.  To do this I call &#8220;xTaskNotifyFromISR&#8221; instead of &#8220;vTaskGiveFromISR&#8221;.   This function lets me set the value of the FreeRTOS Task notification value, is case to the bit mask of the cause of the UART interrupt.  The function also lets you specific if you want to</p>
<ul>
<li>eNoAction &#8211; don&#8217;t do anything (this is what the xTaskNotifyFromISR does)</li>
<li>eSetBits &#8211; or the current notification value with the value you send</li>
<li>eIncrement &#8211; increment the notification value by 1 (in which case it ignore the value you send)</li>
<li>eSetValueWithOverwrite &#8211; replace the current notification value with the value passed in this function</li>
<li>eSetValueWithoutOverwrite &#8211; if there is no pending write, then write the value from this function into the notification value</li>
</ul>
<p>In the UART_Task I take the value and then clear all of the bit (aka set it to 0) when I exit.</p>
<pre class="lang:c decode:true " title="Task Notification - Pass Value">CY_ISR(uartHandler)
{
    uint32_t intrBits = UART_GetRxInterruptSourceMasked();
    UART_SetRxInterruptMode(0); // Turn off the Rx interrupt
    BaseType_t xHigherPriorityTaskWoken;
    xTaskNotifyFromISR( uartTaskHandle,intrBits,eSetValueWithOverwrite,&amp;xHigherPriorityTaskWoken);
    portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
}
void UART_Task( void *arg)
{
    (void)arg;
    char c;
    UART_Start();
    UART_SetCustomInterruptHandler(uartHandler);
    while(1)
    {
        uint32_t intrBits;
              
        xTaskNotifyWait( 0x00,         /* Don't clear any bits on entry. */
                         ULONG_MAX,          /* Clear all bits on exit. */
                         &amp;intrBits, 
                         portMAX_DELAY );    /* Block indefinitely. */
      
        switch(intrBits)
        {
            case UART_INTR_RX_NOT_EMPTY:
                UART_UartPutString("Interrupt: FIFO Not Empty\n");
            break;
            
            case UART_INTR_RX_ERR:
                UART_UartPutString("Interrupt: Error\n");
            break;
                
            case UART_INTR_RX_FULL:
                UART_UartPutString("Interrupt: FIFO Full\n");
            break;
            
            default:
                UART_UartPutString("Interrupt: Unknown\n");
            break;
        }
        
        while(UART_SpiUartGetRxBufferSize())
        {
            c = UART_UartGetChar();
            UART_UartPutString("Char = ");
            UART_UartPutChar(c);
            UART_UartPutString("\n");
        }
        // re-enable the interrupt
        UART_ClearRxInterruptSource(UART_INTR_RX_NOT_EMPTY);
        UART_SetRxInterruptMode(UART_INTR_RX_NOT_EMPTY);
    }
}
</pre>
<h1>New Learning</h1>
<p>When I first wrote the program I had something &#8220;weird&#8221; happening.  Specifically it looked like I was getting the interrupt service routine called twice:</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-19-at-12.05.11-PM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-19-at-12.05.11-PM.png" alt="" width="664" height="418" class="alignnone size-full wp-image-4045" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-19-at-12.05.11-PM.png 664w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-19-at-12.05.11-PM-600x378.png 600w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-19-at-12.05.11-PM-300x189.png 300w" sizes="(max-width: 664px) 100vw, 664px" /></a></p>
<p>I originally wrote the code like this:</p>
<pre class="lang:c decode:true">CY_ISR(uartHandler)
{
    uint32_t intrBits = UART_GetRxInterruptSourceMasked();
    UART_SetRxInterruptMode(0); // Turn off the Rx interrupt
    UART_ClearRxInterruptSource(UART_INTR_RX_NOT_EMPTY);
    BaseType_t xHigherPriorityTaskWoken;
    xTaskNotifyFromISR( uartTaskHandle,intrBits,eSetValueWithOverwrite,&amp;xHigherPriorityTaskWoken);
    portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
}</pre>
<p>But, what happens is:</p>
<ol>
<li>Turn off interrupts</li>
<li>Clear the interrupt source (meaning turn off the flag in the SCB that says there is a character in the Rx Buffer)</li>
<li>One or so UART clock cycles later the flag is reset (because there is still something in the UART Rx Buffer)</li>
<li>Send the notification to the UART Task</li>
<li>The UART Task wakes up and processes the UART Rx Buffer</li>
<li>The UART Task turns back on the interrupts</li>
<li>The interrupt is called because the RX_NOT_EMPTY flag is still set (it is set until it is clear)</li>
<li>The interrupt handler clears the flag (which isn&#8217;t reset this time because there is nothing in the Rx buffer)</li>
<li>The interrupt handler sends the notification</li>
<li>The UART Task wakes up again&#8230; prints out the Flag..</li>
<li>The UART Task tries to read out of the Rx Buffer&#8230; but there isn&#8217;t anything in it.</li>
</ol>
<p>Each time I start thinking that I know what I am doing, I find something else to learn.  I suppose that is what makes this whole thing fun though.</p>
<p><span><p><div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >Topic</th>
<th >Description</th>
</tr>
</thead>
<tbody>
<tr><td ><a href="https://iotexpert.com/2017/02/23/psoc4-freertos-port/" target="_blank" rel="noopener noreferrer">FreeRTOS: A PSoC4 FreeRTOS Port</a></td>
<td >An introduction to making FreeRTOS work on PSoC 4</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/05/23/freertos-psoc-examples/" target="_blank" rel="noopener noreferrer">FreeRTOS PSoC Examples</a></td>
<td >Using multiple tasks in FreeRTOS</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/05/25/psoc-freertos-queue-example/" target="_blank" rel="noopener noreferrer">FreeRTOS Queue Example</a></td>
<td >Using a queue to communicate between tasks</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/05/30/psoc-6-freertos-first-example/" target="_blank" rel="noopener noreferrer">PSoC 6 FreeRTOS - The First Example</a></td>
<td >Booting FreeRTOS on PSoC 6</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/06/01/psoc-freertos-binary-semaphore/" target="_blank" rel="noopener noreferrer">FreeRTOS Binary Semaphore</a></td>
<td >An first example of a binary semaphore</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/06/08/psoc-freertos-binary-semaphore-part-2/">FreeRTOS Binary Semaphore (Part 2)</td>
<td >Removing polling in the UART Task</a></td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/06/20/psoc-freertos-counting-semaphore/" target="_blank" rel="noopener">FreeRTOS Counting Semaphore</a></td>
<td >An example using a counting semaphore</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/06/22/psoc-freertos-sharing-the-i2c-bus/" target="_blank" rel="noopener">PSoC FreeRTOS</a></td>
<td >Reading I2C Sensors with a shared I2C Bus</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/08/22/psoc-freertos-task-notify/" target="_blank" rel="noopener">PSoC FreeRTOS Task Notify</a></td>
<td >A light weight scheme to replace Semaphores</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/08/24/psoc-freertos-task-notification-value/" target="_blank" rel="noopener">PSoC FreeRTOS Task Notification Values</a></td>
<td >A very light weight method to transfer one word of information into a task</td>
</tr>
</tbody></table></div></p></span></p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/psoc-freertos-task-notification-value/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>PSoC FreeRTOS Task Notify</title>
		<link>https://iotexpert.com/psoc-freertos-task-notify/</link>
					<comments>https://iotexpert.com/psoc-freertos-task-notify/#respond</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Tue, 22 Aug 2017 09:00:11 +0000</pubDate>
				<category><![CDATA[CY8CKIT-044]]></category>
		<category><![CDATA[FreeRTOS]]></category>
		<category><![CDATA[PSoC 4200]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=4032</guid>

					<description><![CDATA[Summary I have been writing a bunch of articles about implementing PSoC FreeRTOS so, this morning I was reading the FreeRTOS manual (yes I am one of those&#8230;) and I noticed a section in the API guide that I hadn&#8217;t see before&#8230; Task Notifications.  Every task in the FreeRTOS has a built in 32-bit integer [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>I have been writing a bunch of articles about implementing PSoC FreeRTOS so, this morning I was reading the FreeRTOS manual (yes I am one of those&#8230;) and I noticed a section in the API guide that I hadn&#8217;t see before&#8230; <a href="http://www.freertos.org/RTOS-task-notification-API.html" target="_blank" rel="noopener">Task Notifications</a>.  Every task in the FreeRTOS has a built in 32-bit integer notification value.  This value is super light weight and can be used like a task specific counting semaphore, or a signaling bit mask, or binary semaphore.  The API includes:</p>
<ul>
<li><a class="el" href="http://www.freertos.org/xTaskNotifyGive.html">xTaskNotifyGive()</a></li>
<li><a class="el" href="http://www.freertos.org/vTaskNotifyGiveFromISR.html">vTaskNotifyGiveFromISR()</a></li>
<li><a class="el" href="http://www.freertos.org/ulTaskNotifyTake.html">ulTaskNotifyTake()</a></li>
<li><a class="el" href="http://www.freertos.org/xTaskNotify.html">xTaskNotify()</a></li>
<li><a class="el" href="http://www.freertos.org/xTaskNotifyAndQuery.html">xTaskNotifyAndQuery()</a></li>
<li><a class="el" href="http://www.freertos.org/xTaskNotifyAndQueryFromISR.html">xTaskNotifyAndQueryFromISR()</a></li>
<li><a class="el" href="http://www.freertos.org/xTaskNotifyFromISR.html">xTaskNotifyFromISR()</a></li>
<li><a class="el" href="http://www.freertos.org/xTaskNotifyWait.html">xTaskNotifyWait()</a></li>
<li><a class="el" href="http://www.freertos.org/xTaskNotifyStateClear.html">xTaskNotifyStateClear()</a></li>
</ul>
<p>It seems like this API is good for the situations when your Semaphore has a specific task target in mind.  I thought that this would be a perfect scheme to have a PSoC FreeRTOS UART ISR signal the UART Handling task that there is data available to do something with.</p>
<h1>Setup the PSoC FreeRTOS Project</h1>
<p>I start this process by making a copy of &#8220;1-BlinkingLED&#8221; (which already has all of the FreeRTOS stuff in it) and naming it &#8220;9-TaskNotify&#8221;.  Then I add a UART to the schematic and name it &#8220;UART&#8221;</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-18-at-5.29.28-PM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-18-at-5.29.28-PM.png" alt="PSoC FreeRTOS Schematic" width="349" height="324" class="alignnone wp-image-4035 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-18-at-5.29.28-PM.png 349w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-18-at-5.29.28-PM-300x279.png 300w" sizes="(max-width: 349px) 100vw, 349px" /></a></p>
<p>I attach the UART to the right pins on the CY8CKIT-044 kit.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-18-at-5.29.42-PM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-18-at-5.29.42-PM.png" alt="PSoC Creator Pin Assignment" width="639" height="110" class="alignnone wp-image-4034 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-18-at-5.29.42-PM.png 639w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-18-at-5.29.42-PM-600x103.png 600w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-18-at-5.29.42-PM-300x52.png 300w" sizes="(max-width: 639px) 100vw, 639px" /></a><br />
Next I turn on the interrupt which will be called when there is data in the receive FIFO.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-18-at-5.22.15-PM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-18-at-5.22.15-PM.png" alt="PSoC UART Configuration" width="775" height="764" class="alignnone wp-image-4033 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-18-at-5.22.15-PM.png 775w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-18-at-5.22.15-PM-100x100.png 100w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-18-at-5.22.15-PM-600x591.png 600w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-18-at-5.22.15-PM-300x296.png 300w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-18-at-5.22.15-PM-768x757.png 768w" sizes="(max-width: 775px) 100vw, 775px" /></a></p>
<h1>PSoC FreeRTOS UART Code</h1>
<p>Now that the schematic is all configured I update my firmware.  The function &#8220;uartHandler&#8221; is called when there is data in the UART RX FIFO.  It turns of the interrupts for the UART (which I will turn back on after I have cleared the data in the input buffer), clears the interrupt  (so that it will stop pending) and then sends the notification to the UART_Task.</p>
<p>The UART Task just registers the handler&#8230; then while(1)&#8217;s until the end of time.  It waits for a notification, then reads data out of the RX fifo and puts out,  then re-enables the interrupts.</p>
<pre class="lang:c decode:true">CY_ISR(uartHandler)
{
    BaseType_t xHigherPriorityTaskWoken;

    // disable the interrupt
    UART_SetRxInterruptMode(0);
    vTaskNotifyGiveFromISR(uartTaskHandle,&amp;xHigherPriorityTaskWoken);
    portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
}

void UART_Task( void *arg)
{
    (void)arg;
    char c;
    UART_Start();
    UART_SetCustomInterruptHandler(uartHandler);
    while(1)
    {
        ulTaskNotifyTake(pdTRUE,portMAX_DELAY);
        while(UART_SpiUartGetRxBufferSize())
        {
            c = UART_UartGetChar();
            UART_UartPutChar(c);
        }
        // clear &amp; re-enable the interrupt
        UART_ClearRxInterruptSource(UART_INTR_RX_NOT_EMPTY);
        UART_SetRxInterruptMode(UART_INTR_RX_NOT_EMPTY);
    }
}
</pre>
<p><span><p><div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >Topic</th>
<th >Description</th>
</tr>
</thead>
<tbody>
<tr><td ><a href="https://iotexpert.com/2017/02/23/psoc4-freertos-port/" target="_blank" rel="noopener noreferrer">FreeRTOS: A PSoC4 FreeRTOS Port</a></td>
<td >An introduction to making FreeRTOS work on PSoC 4</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/05/23/freertos-psoc-examples/" target="_blank" rel="noopener noreferrer">FreeRTOS PSoC Examples</a></td>
<td >Using multiple tasks in FreeRTOS</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/05/25/psoc-freertos-queue-example/" target="_blank" rel="noopener noreferrer">FreeRTOS Queue Example</a></td>
<td >Using a queue to communicate between tasks</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/05/30/psoc-6-freertos-first-example/" target="_blank" rel="noopener noreferrer">PSoC 6 FreeRTOS - The First Example</a></td>
<td >Booting FreeRTOS on PSoC 6</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/06/01/psoc-freertos-binary-semaphore/" target="_blank" rel="noopener noreferrer">FreeRTOS Binary Semaphore</a></td>
<td >An first example of a binary semaphore</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/06/08/psoc-freertos-binary-semaphore-part-2/">FreeRTOS Binary Semaphore (Part 2)</td>
<td >Removing polling in the UART Task</a></td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/06/20/psoc-freertos-counting-semaphore/" target="_blank" rel="noopener">FreeRTOS Counting Semaphore</a></td>
<td >An example using a counting semaphore</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/06/22/psoc-freertos-sharing-the-i2c-bus/" target="_blank" rel="noopener">PSoC FreeRTOS</a></td>
<td >Reading I2C Sensors with a shared I2C Bus</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/08/22/psoc-freertos-task-notify/" target="_blank" rel="noopener">PSoC FreeRTOS Task Notify</a></td>
<td >A light weight scheme to replace Semaphores</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/08/24/psoc-freertos-task-notification-value/" target="_blank" rel="noopener">PSoC FreeRTOS Task Notification Values</a></td>
<td >A very light weight method to transfer one word of information into a task</td>
</tr>
</tbody></table></div></p></span></p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/psoc-freertos-task-notify/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Percepio Tracealyzer: A PSoC DMA Streamport</title>
		<link>https://iotexpert.com/percepio-tracealyzer-a-psoc-dma-streamport/</link>
					<comments>https://iotexpert.com/percepio-tracealyzer-a-psoc-dma-streamport/#respond</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Tue, 08 Aug 2017 10:00:04 +0000</pubDate>
				<category><![CDATA[CY8CKIT-044]]></category>
		<category><![CDATA[PSoC 4200]]></category>
		<category><![CDATA[Tracealyzer]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=3961</guid>

					<description><![CDATA[Summary In the last Article I analyzed the performance problems of my firmware based PSoC Tracealyzer Streamport &#8230; which was terrible.  Although it has been a long time since I used the PSoC4M DMA engine, I knew that it would solve my problem.  In this article Ill show how to use the PSoC DMA, then [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>In the last <a href="https://iotexpert.com/2017/08/03/psoc-tracelyzer-streampoint/" target="_blank" rel="noopener">Article</a> I analyzed the performance problems of my firmware based PSoC <a href="https://percepio.com/tz/freertostrace/" target="_blank" rel="noopener">Tracealyzer</a> Streamport &#8230; which was terrible.  Although it has been a long time since I used the <a href="http://www.cypress.com/documentation/datasheets/psocr-4-psoc-4200m-family-datasheet-programmable-system-chip-psocr-ja" target="_blank" rel="noopener">PSoC4M</a> DMA engine, I knew that it would solve my problem.  In this article Ill show how to use the PSoC <a href="http://www.cypress.com/documentation/component-datasheets/direct-memory-access-dma" target="_blank" rel="noopener">DMA</a>, then Ill build and analyze a PSoC DMA Streamport for Tracealyzer.</p>
<h1>PSoC4 UART DMA</h1>
<p>The DMA block that shows up in the PSoC4200M, PSoC 4200L and PSoC4BLE is pretty amazing.  It can do a bunch of stuff.  Here is a snapshot that I took out of the <a href="http://www.cypress.com/file/159196/download" target="_blank" rel="noopener">TRM</a>.  This block sits on the main AHB bus inside of the PSoC.  It can act as a master (see the block that says Master I/F) and read and write any of things in the ARM address space including the Flash, SRAM, and all of the peripherals.  It has an incoming trigger which can get the transfers going and when it is done it can trigger an interrupt or another DMA channel.  The Slave I/F allows the CPU to program the block.  The device has 8-channels with each channel having 2 descriptors (so it can ping pong).</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.06.25-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.06.25-AM-1024x574.png" alt="PSoC4 DMA" width="1024" height="574" class="alignnone wp-image-3972 size-large" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.06.25-AM-1024x574.png 1024w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.06.25-AM-600x336.png 600w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.06.25-AM-300x168.png 300w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.06.25-AM-768x430.png 768w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.06.25-AM.png 1342w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Before I tried to make the PSoC DMA Streamport I started by looking at the example projects by pressing &#8220;File-&gt;Code Example&#8221;</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.38.52-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.38.52-AM.png" alt="PSoC Creator Example" width="201" height="398" class="alignnone wp-image-3974 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.38.52-AM.png 201w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.38.52-AM-152x300.png 152w" sizes="(max-width: 201px) 100vw, 201px" /></a></p>
<p>Then filtering for DMA</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.39.17-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.39.17-AM.png" alt="PSoC Creator Example Project - DMA" width="635" height="476" class="alignnone wp-image-3973 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.39.17-AM.png 635w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.39.17-AM-600x450.png 600w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.39.17-AM-300x225.png 300w" sizes="(max-width: 635px) 100vw, 635px" /></a></p>
<p>Finally creating project.  This project uses two DMA channels, one for the UART receive and one for the UART Transfer.  It lets you type characters into the UART, it saves them in one of the RAM buffers, then when you have typed 8, it DMAs them back into the Transmit channel of the UART.  This example ping-pings back and forth between two RAM buffers.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.41.07-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.41.07-AM.png" alt="PSoC Creator DMA Example" width="883" height="671" class="alignnone wp-image-3975 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.41.07-AM.png 883w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.41.07-AM-600x456.png 600w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.41.07-AM-300x228.png 300w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.41.07-AM-768x584.png 768w" sizes="(max-width: 883px) 100vw, 883px" /></a></p>
<p>I decided that it would be best to build a bare metal DMA project called &#8220;test-uart&#8221; to prove that I understood.  This project will DMA transfer an array of characters to the UART when the user presses the switch on the board or a &#8220;s&#8221; on the keyboard.  The first thing to do is build the schematic with a UART, a DMA block,  two output pins, and input pin and an interrupt.</p>
<h1><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-8.48.55-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-8.48.55-AM.png" alt="PSoC DMA Streamport" width="579" height="434" class="alignnone wp-image-3969 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-8.48.55-AM.png 579w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-8.48.55-AM-300x225.png 300w" sizes="(max-width: 579px) 100vw, 579px" /></a></h1>
<p>Place an SCB UART. then configure it (change the name, but accept all of the defaults)</p>
<h1><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-8.49.10-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-8.49.10-AM.png" alt="PSoC Creator UART" width="776" height="761" class="alignnone wp-image-3968 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-8.49.10-AM.png 776w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-8.49.10-AM-600x588.png 600w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-8.49.10-AM-300x294.png 300w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-8.49.10-AM-768x753.png 768w" sizes="(max-width: 776px) 100vw, 776px" /></a></h1>
<p>Then click on the advanced tab.  Turn on the DMA for Transfer (TX Output) and set the &#8220;FIFO Level&#8221; to 7.  This will cause the UART to assert DMA signal anytime the transmit FIFO has less than 7 bytes.  In  other words &#8230; FEED ME!!!</p>
<h1><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-8.49.30-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-8.49.30-AM.png" alt="" width="778" height="770" class="alignnone size-large wp-image-3967" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-8.49.30-AM.png 778w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-8.49.30-AM-100x100.png 100w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-8.49.30-AM-600x594.png 600w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-8.49.30-AM-300x297.png 300w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-8.49.30-AM-768x760.png 768w" sizes="(max-width: 778px) 100vw, 778px" /></a></h1>
<p>After configuring the UART, Set the PINs</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.02.09-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.02.09-AM.png" alt="PSoC DMA Streamport Pin Assignment" width="952" height="615" class="alignnone wp-image-3971 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.02.09-AM.png 952w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.02.09-AM-600x388.png 600w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.02.09-AM-300x194.png 300w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.02.09-AM-768x496.png 768w" sizes="(max-width: 952px) 100vw, 952px" /></a></p>
<p>Next configure the DMA.  The memory array that I have will be &#8220;uint8_t&#8221; aka &#8220;char&#8221;.  So the input needs to be &#8220;bytes&#8221;.  The UART FIFO hold Words&#8230; aka 4 bytes.  So I need to configure the transfers to do &#8220;Byte to Word&#8221;.  After the DMA transfer is done, I setup the DMA to create an interrupt (so that I can reset everything in the CPU) and to invalidate the descriptor.</p>
<h1><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-8.51.43-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-8.51.43-AM.png" alt="PSoC DMA Streamport" width="633" height="473" class="alignnone wp-image-3966 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-8.51.43-AM.png 633w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-8.51.43-AM-600x448.png 600w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-8.51.43-AM-300x224.png 300w" sizes="(max-width: 633px) 100vw, 633px" /></a></h1>
<p>In the firmware works by</p>
<ul>
<li>Turning on the UART</li>
<li>Enabling the DMA</li>
<li>Setting up the channel with the address of the buffer that I am going to write to (aka the TX FIFO) and asking for an interrupt</li>
</ul>
<p>Then looping:</p>
<ul>
<li>When the User presses &#8220;s&#8221; or the switch, I set myFlag to be 1.</li>
<li>If it is 1 and the channel is inactive, then initialize the &#8220;source&#8221; address, setup the number of transfer elements to be the number in my array, validate the descriptor, and turn on the channel.</li>
</ul>
<p>When the channel turns on, the Tx fifo will be empty so it will assert the Tx Out, which will make the DMA keep triggering and copying 1 byte at a time into the FIFO.  While this is happening, the UART will try to empty the fifo by sending the bytes.  Finally when the DMA reaches the end of the RAM buffer, it will stop, and at some point the TX FIFO will finish emptying.  The DMA will trigger the interrupt to toggle the BLUE LED.  And the whole process can start again.</p>
<pre class="lang:c decode:true ">#include "project.h"
#include &lt;stdio.h&gt;

// A flag to trigger the DMA
volatile int myFlag=0;
  
static const char myArray[]="asdf1234asdfadsfasdfadsfqwerasdfqwerqwer9\n";

CY_ISR(sw_handler)
{
    myFlag = 1;
    SW_ClearInterrupt();
}

// This is called TWICE at the end of the DMA transaction
CY_ISR(myDMA)
{
    BLUE_Write(~BLUE_Read());
}
int main(void)
{
    CyIntEnable(CYDMA_INTR_NUMBER);
    CyGlobalIntEnable; /* Enable global interrupts. */

    char c;
    
    UART_Start();
    UART_UartPutString("Started\n");
  
    isr_1_StartEx(sw_handler);
  
    CyDmaEnable();
    
    DMA_Init();
    DMA_SetDstAddress(0, (void *)UART_TX_FIFO_WR_PTR);
    DMA_SetInterruptCallback(myDMA);

    while(1)
    {
        c = UART_UartGetChar();
        switch(c)
        {       
            case 's':
                myFlag = 1;
            break;
        }
        
        // This turns on the DMA so that the string will go to the UART.
        if(myFlag &amp;&amp; CyDmaGetActiveChannels() == 0)
        {
            DMA_SetSrcAddress(0, (void *)myArray);
            DMA_SetNumDataElements(0,strlen(myArray)-1);
            DMA_ValidateDescriptor(0);
            DMA_ChEnable();
            myFlag=0;
        }
    }
}
</pre>
<h1>PSoC DMA Streamport</h1>
<p>Now that I understand how to use the DMA, I can create the PSoC DMA Streamport by copying the project &#8220;1-BlionkingLED_UART_TRACE&#8221; project and calling it &#8220;1-BlinkingLED_UART_TRCE_DMA&#8221;.  Next, add the DMA and modify the UART.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.58.55-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.58.55-AM.png" alt="PSoC DMA Streamport" width="383" height="270" class="alignnone wp-image-3980 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.58.55-AM.png 383w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.58.55-AM-300x211.png 300w" sizes="(max-width: 383px) 100vw, 383px" /></a></p>
<p>Configure the UART</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.59.06-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.59.06-AM.png" alt="PSoC DMA Streamport - UART Configuration" width="777" height="764" class="alignnone wp-image-3979 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.59.06-AM.png 777w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.59.06-AM-600x590.png 600w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.59.06-AM-300x295.png 300w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.59.06-AM-768x755.png 768w" sizes="(max-width: 777px) 100vw, 777px" /></a></p>
<p>Turn on the UART DMA and set the level to 7</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.59.15-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.59.15-AM.png" alt="" width="779" height="771" class="alignnone size-large wp-image-3978" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.59.15-AM.png 779w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.59.15-AM-100x100.png 100w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.59.15-AM-600x594.png 600w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.59.15-AM-300x297.png 300w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.59.15-AM-768x760.png 768w" sizes="(max-width: 779px) 100vw, 779px" /></a></p>
<p>Configure the DMA Block</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.59.26-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.59.26-AM.png" alt="PSoC DMA Streamport Configuration" width="629" height="472" class="alignnone wp-image-3977 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.59.26-AM.png 629w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.59.26-AM-600x450.png 600w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.59.26-AM-300x225.png 300w" sizes="(max-width: 629px) 100vw, 629px" /></a></p>
<p>Make byte transfers and byte &#8211;&gt; word.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.59.34-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.59.34-AM.png" alt="PSoC DMA Streamport - DMA Configuration" width="628" height="472" class="alignnone wp-image-3976 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.59.34-AM.png 628w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.59.34-AM-600x451.png 600w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-9.59.34-AM-300x225.png 300w" sizes="(max-width: 628px) 100vw, 628px" /></a></p>
<p>Finally modify the trcStreamingPort.c &#8211; AKA the PSoC DMA Streamport file.  Specifically you need to fix up the PSoC_Transmit to send out the data when the TraceRecorder buffer is full.</p>
<ul>
<li>If the DMA is busy&#8230; then wait until the previous transaction is done.</li>
<li>Then setup the DMA and let it rip.</li>
</ul>
<pre class="start-line:23 lang:c decode:true">int32_t PSoC_Transmit(void* data, uint32_t size, int32_t *numOfBytesSent )
{
    
    while( CyDmaGetActiveChannels()&amp; DMA_CHANNEL_MASK);
    DMA_SetSrcAddress(0, (void *)data);
    DMA_SetNumDataElements(0,size);
    DMA_ValidateDescriptor(0);
    DMA_ChEnable();

    *numOfBytesSent=size;
    
    return 0; // Doesnt matter what you return... i dont think that it is checked
}
</pre>
<p>The only other thing that needs to happen is configure the DMA in main.c</p>
<pre class="start-line:44 lang:c decode:true " title="main.c">    CyDmaEnable();
    DMA_Init();
    DMA_SetDstAddress(0, (void *)UART_TX_FIFO_WR_PTR);
</pre>
<h1>Testing the PSoC DMA Streamport</h1>
<p>Now when I startup the Tracealyzer, here is what I get:</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-11.17.40-AM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-11.17.40-AM.png" alt="PSoC DMA Streamport - New CPU Load" width="691" height="321" class="alignnone wp-image-3983 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-11.17.40-AM.png 691w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-11.17.40-AM-600x279.png 600w, https://iotexpert.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-05-at-11.17.40-AM-300x139.png 300w" sizes="(max-width: 691px) 100vw, 691px" /></a></p>
<p>It looks like I solved my problem because that is way way better than:</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-3.23.31-PM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-3.23.31-PM.png" alt="PSoC DMA Streamport - Terrible Performance" width="596" height="359" class="alignnone wp-image-3925 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-3.23.31-PM.png 596w, https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-3.23.31-PM-300x181.png 300w" sizes="(max-width: 596px) 100vw, 596px" /></a></p>
<p><span><p>As always you can find all of these projects on the <a href="https://github.com/iotexpert/PSoC-Tracelyzer" target="_blank" rel="noopener">IotExpert GitHub</a> site or git@github.com:iotexpert/PSoC-Tracelyzer.git</p>
<p><div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >Article</th>
<th >Description</th>
</tr>
</thead>
<tbody>
<tr><td ><a href="https://iotexpert.com/2017/07/18/percepio-tracelyzer-psoc-4200m/">Percepio Tracealyzer &amp; PSoC</a></td>
<td >An Introduction to Percepio Tracealyzer on the Cypress PSoC</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/07/25/perception-tracelyzer-rtt-streamport-psoc4200m/">Percepio Tracealyzer RTT Streamport - PSoC4200M</a></td>
<td >Make the JLINK RTT Library work with Tracealyzer</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/08/01/percepio-tracelyzer-psoc-uart-streamport/">Percepio Tracealyzer PSoC UART Streamport</a></td>
<td >Creating a UART Streamport</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/08/03/psoc-tracelyzer-streampoint/">Percepio Tracealyzer - Analyzing the PSoC Tracealyzer Streamport</a></td>
<td >Figure out what is broken in the UART Streamport</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/08/08/percepio-tracelyzer-a-psoc-dma-streamport/">Percepio Tracealyzer - Using PSoC DMA to Fix the UART Streamport</a></td>
<td >Implementing PSoC DMA to improve the CPU Utilization</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/08/15/percepio-tracealyzer-on-psoc6/">Percepio Tracealyzer - Running on PSoC6</a></td>
<td >Porting the Percepio Tracealyzer to PSoC6</td>
</tr>
</tbody></table></div></p></span></p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/percepio-tracealyzer-a-psoc-dma-streamport/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Percepio Tracealyzer PSoC UART Streamport</title>
		<link>https://iotexpert.com/percepio-tracealyzer-psoc-uart-streamport/</link>
					<comments>https://iotexpert.com/percepio-tracealyzer-psoc-uart-streamport/#comments</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Tue, 01 Aug 2017 10:20:35 +0000</pubDate>
				<category><![CDATA[CY8CKIT-044]]></category>
		<category><![CDATA[Tracealyzer]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=3920</guid>

					<description><![CDATA[Summary In the last Article I showed you how to use the JLINK RTT Streamport to make Tracealyzer work.  When I did that port I didn&#8217;t really like having to use a JLink, it seemed like a pain to have another box.  I knew that it was possible to make a UART based Streamport.  So, [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>In the last <a href="https://iotexpert.com/2017/07/25/perception-tracelyzer-rtt-streamport-psoc4200m/" target="_blank" rel="noopener">Article</a> I showed you how to use the JLINK RTT Streamport to make <a href="https://percepio.com/tz/" target="_blank" rel="noopener">Tracealyzer</a> work.  When I did that port I didn&#8217;t really like having to use a JLink, it seemed like a pain to have another box.  I knew that it was possible to make a UART based Streamport.  So, that is what I am going to do, create a Tracealyzer PSoC Streamport.  This Article is going to be broken up into two parts.  In Part 1 I will show you a software based port and the problems that it creates.  In Part 2 I will show you how to use DMA to hopefully (as I haven&#8217;t done the work yet) make it work much better.  I was hoping this morning when I started working on this Article that it would only take me a couple of hours, but that did not turn out to be the case as I ran into a PSoC Creator bug which made it hard to figure out what was going on.</p>
<h1>Cypress USB Serial Bridge &amp; CY8CKIT-049</h1>
<p>If you remember from the last article, the Tracealyzer needs to stream about 20KBs.  That is a big B as in Bytes.  Which means that a 115200 Kbs (aka 14.4 KBs) UART probably won&#8217;t do the trick.  That is OK as the PSoC SCB UART can run at up to 921600 aka 115.2 KBs.  The problem is the Kitprog bridge on my development kit is limited to 115200 (I&#8217;m not sure why)  I didn&#8217;t realize that limitation this morning when I started working on this problem.  But it turns out that the <a href="http://www.cypress.com/documentation/development-kitsboards/psoc-4-cy8ckit-049-4xxx-prototyping-kits" target="_blank" rel="noopener">CY8CKIT-049</a> has a Cypress <a href="http://www.cypress.com/products/usb-serial-bridge-controller" target="_blank" rel="noopener">USB Serial Bridge.</a>which is used to boatload the PSoC4200.  And&#8230; it is designed to break off the kit.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/07/CY8CKit-049_full_img.jpg"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/07/CY8CKit-049_full_img.jpg" alt="CY8CKIT-049" width="942" height="250" class="alignnone wp-image-3928 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/07/CY8CKit-049_full_img.jpg 942w, https://iotexpert.com/wp-content/uploads/2017/07/CY8CKit-049_full_img-600x159.jpg 600w, https://iotexpert.com/wp-content/uploads/2017/07/CY8CKit-049_full_img-300x80.jpg 300w, https://iotexpert.com/wp-content/uploads/2017/07/CY8CKit-049_full_img-768x204.jpg 768w" sizes="(max-width: 942px) 100vw, 942px" /></a></p>
<p>So I broke it off and gave it to my lab assistant to solder (he isn&#8217;t normally that grumpy, but he stayed up all night at a birthday party)</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/07/IMG_4689-2.jpg"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/07/IMG_4689-2-768x1024.jpg" alt="Nicholas the Lab Tech" width="768" height="1024" class="alignnone wp-image-3929 size-large" srcset="https://iotexpert.com/wp-content/uploads/2017/07/IMG_4689-2-768x1024.jpg 768w, https://iotexpert.com/wp-content/uploads/2017/07/IMG_4689-2-600x800.jpg 600w, https://iotexpert.com/wp-content/uploads/2017/07/IMG_4689-2-225x300.jpg 225w, https://iotexpert.com/wp-content/uploads/2017/07/IMG_4689-2-scaled.jpg 1920w" sizes="(max-width: 768px) 100vw, 768px" /></a></p>
<p>Now I have two wires which I can use to talk to the CY8CKIT-044 UART.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/07/IMG_4686.jpg"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/07/IMG_4686-e1501358940465-1024x768.jpg" alt="Cypress USB Serial Bridge" width="1024" height="768" class="alignnone wp-image-3926 size-large" srcset="https://iotexpert.com/wp-content/uploads/2017/07/IMG_4686-e1501358940465-1024x768.jpg 1024w, https://iotexpert.com/wp-content/uploads/2017/07/IMG_4686-e1501358940465-600x450.jpg 600w, https://iotexpert.com/wp-content/uploads/2017/07/IMG_4686-e1501358940465-300x225.jpg 300w, https://iotexpert.com/wp-content/uploads/2017/07/IMG_4686-e1501358940465-768x576.jpg 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>After I got the wires on the bridge, I installed the USB Serial Configuration Utility and set the chip up to be 921600 baud.  To do this, run the utility.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-4.10.39-PM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-4.10.39-PM.png" alt="USB Serial Configuration Utility" width="524" height="526" class="alignnone wp-image-3933 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-4.10.39-PM.png 524w, https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-4.10.39-PM-300x300.png 300w, https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-4.10.39-PM-100x100.png 100w, https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-4.10.39-PM-150x150.png 150w" sizes="(max-width: 524px) 100vw, 524px" /></a></p>
<p>Connect to your bridge, in this case the CY7C65211-24LTXI</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-4.10.49-PM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-4.10.49-PM.png" alt="USB Serial Configuration Utility" width="518" height="496" class="alignnone wp-image-3932 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-4.10.49-PM.png 518w, https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-4.10.49-PM-300x287.png 300w" sizes="(max-width: 518px) 100vw, 518px" /></a></p>
<p>Then pick the SCB (which stands for Serial Communication Block)</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-4.11.12-PM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-4.11.12-PM.png" alt="USB Serial Configuration Utility" width="523" height="525" class="alignnone wp-image-3931 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-4.11.12-PM.png 523w, https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-4.11.12-PM-300x300.png 300w, https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-4.11.12-PM-100x100.png 100w, https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-4.11.12-PM-150x150.png 150w" sizes="(max-width: 523px) 100vw, 523px" /></a></p>
<p>Click the configure, then set the baud to 921600.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-4.11.32-PM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-4.11.32-PM.png" alt="USB Serial Configuration Utility" width="448" height="348" class="alignnone wp-image-3930 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-4.11.32-PM.png 448w, https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-4.11.32-PM-300x233.png 300w" sizes="(max-width: 448px) 100vw, 448px" /></a></p>
<p>Now, I wire the two kits together (the Jlink is just sitting there)</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/07/IMG_4685.jpg"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/07/IMG_4685-1024x768.jpg" alt="Percepio Tracelyzer PSoC Setup" width="1024" height="768" class="alignnone wp-image-3927 size-large" srcset="https://iotexpert.com/wp-content/uploads/2017/07/IMG_4685-1024x768.jpg 1024w, https://iotexpert.com/wp-content/uploads/2017/07/IMG_4685-600x450.jpg 600w, https://iotexpert.com/wp-content/uploads/2017/07/IMG_4685-300x225.jpg 300w, https://iotexpert.com/wp-content/uploads/2017/07/IMG_4685-768x576.jpg 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Creating a Tracealyzer PSoC UART Streamport</h1>
<p>The first thing to do is modify the previous project to also have a UART.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-5.51.36-PM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-5.51.36-PM.png" alt="PSoC Creator" width="986" height="763" class="alignnone wp-image-3935 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-5.51.36-PM.png 986w, https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-5.51.36-PM-600x464.png 600w, https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-5.51.36-PM-300x232.png 300w, https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-5.51.36-PM-768x594.png 768w" sizes="(max-width: 986px) 100vw, 986px" /></a></p>
<p>Then connect the UART to Pins P3[0] &amp; P3[1]</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-5.52.58-PM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-5.52.58-PM.png" alt="PSoC Creator" width="433" height="130" class="alignnone wp-image-3936 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-5.52.58-PM.png 433w, https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-5.52.58-PM-300x90.png 300w" sizes="(max-width: 433px) 100vw, 433px" /></a></p>
<p>To make the Tracealyzer PSoC Streamport work I make a copy of the TraceRecorder/streamports/USB_CDC and called it PSoC_UART.  After I cut all all of the ST USB crap, I realized that all I need to provide is a</p>
<ul>
<li>A function to receive bytes &#8230; called PSoC_Receive</li>
<li>A function to transmit bytes&#8230; called PSoC_Transmit</li>
</ul>
<p>Next you need to modify these 10 CPP Macros to support the Tracealyzer PSoC Streamport.   (I only had to modify INIT, READ and SEND)</p>
<ul>
<li>TRC_STREAM_PORT_ALLOCATE_FIELDS</li>
<li>TRC_STREAM_PORT_MALLOC</li>
<li>TRC_STREAM_PORT_INIT</li>
<li>TRC_STREAM_PORT_ALLOCATE_EVENT</li>
<li>TRC_STREAM_PORT_ALLOCATE_DYNAMIC_EVENT</li>
<li>TRC_STREAM_PORT_COMMIT_EVENT</li>
<li>TRC_STREAM_PORT_READ_DATA</li>
<li>TRC_STREAM_PORT_PERIODIC_SEND_DATA</li>
<li>TRC_STREAM_PORT_ON_TRACE_BEGIN</li>
<li>TRC_STREAM_PORT_ON_TRACE_END</li>
</ul>
<p>In addition I provide function prototypes for PSoC_Receive and PSoC_Transmit which are called by TRC_STREAM_PORT_READ_DATA and TRC_STREAM_PORT_PERIODIC_SEND_DATA</p>
<pre class="start-line:47 lang:c decode:true">// An Adaption of the Percepio CDC Library to PSoC SCB UART
#include &lt;project.h&gt;

#ifndef TRC_STREAMING_PORT_H
#define TRC_STREAMING_PORT_H

#ifdef __cplusplus
extern "C" {
#endif

/*******************************************************************************
 * Implement the below macros to define your own stream port. If your transfer 
 * method uses RTOS functions, you should not send the data directly but use 
 * the recorder's internal buffer to store the trace data, for later transfer by
 * the TzCtrl task. Check the predefined stream ports for examples on how to use 
 * the internal buffer (e.g., TCP/IP, UART or USB CDC).
 *
 * Read more at http://percepio.com/2016/10/05/rtos-tracing/  
 ******************************************************************************/

int32_t PSoC_Receive(void *data, uint32_t size, int32_t *numOfBytesReceived);
int32_t PSoC_Transmit(void *data, uint32_t size, int32_t *numOfBytesSent );

#if TRC_RECORDER_BUFFER_ALLOCATION == TRC_RECORDER_BUFFER_ALLOCATION_STATIC
#define TRC_STREAM_PORT_ALLOCATE_FIELDS() static char _TzTraceData[TRC_CFG_PAGED_EVENT_BUFFER_PAGE_COUNT * TRC_CFG_PAGED_EVENT_BUFFER_PAGE_SIZE];       /* Static allocation. */
#define TRC_STREAM_PORT_MALLOC() /* Static allocation. Not used. */
#else
#define TRC_STREAM_PORT_ALLOCATE_FIELDS() static char* _TzTraceData = NULL;     /* Dynamic allocation. */
#define TRC_STREAM_PORT_MALLOC() _TzTraceData = TRC_PORT_MALLOC(TRC_PAGED_EVENT_BUFFER_PAGE_COUNT * TRC_PAGED_EVENT_BUFFER_PAGE_SIZE);
#endif

#define TRC_STREAM_PORT_INIT() \
        UART_Start(); \
        TRC_STREAM_PORT_MALLOC(); /*Dynamic allocation or empty if static */

#define TRC_STREAM_PORT_ALLOCATE_EVENT(_type, _ptrData, _size) _type* _ptrData; _ptrData = (_type*)prvPagedEventBufferGetWritePointer(_size);
#define TRC_STREAM_PORT_ALLOCATE_DYNAMIC_EVENT(_type, _ptrData, _size) TRC_STREAM_PORT_ALLOCATE_EVENT(_type, _ptrData, _size) /* We do the same thing as for non-dynamic event sizes */
#define TRC_STREAM_PORT_COMMIT_EVENT(_ptrData, _size) /* Not needed since we write immediately into the buffer received above by TRC_STREAM_PORT_ALLOCATE_EVENT, and the TRC_STREAM_PORT_PERIODIC_SEND_DATA defined below will take care of the actual trace transfer. */
#define TRC_STREAM_PORT_READ_DATA(_ptrData, _size, _ptrBytesRead) PSoC_Receive(_ptrData, _size, _ptrBytesRead);
#define TRC_STREAM_PORT_PERIODIC_SEND_DATA(_ptrBytesSent) prvPagedEventBufferTransfer(PSoC_Transmit, _ptrBytesSent);
#define TRC_STREAM_PORT_ON_TRACE_BEGIN() { prvPagedEventBufferInit(_TzTraceData); }
#define TRC_STREAM_PORT_ON_TRACE_END() /* Do nothing */

#ifdef __cplusplus
}
#endif

#endif /* TRC_STREAMING_PORT_H */
</pre>
<p>The last thing to make this work is provide the C function to read and write the UART by modifying trcStreamingPort.c</p>
<pre class="lang:c decode:true">// An adataption of the Percepio USB_CDC Port to PSoC Serial
#include "trcRecorder.h"

#if (TRC_USE_TRACEALYZER_RECORDER == 1)
#if(TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)

#include "stdint.h"

int32_t PSoC_Receive(void *data, uint32_t size, int32_t *numOfBytesReceived)
{
  
    uint8_t *myData= (uint8_t *)data;
    
    *numOfBytesReceived = 0;
    while( (*numOfBytesReceived &lt; (int32_t)size) &amp;&amp; UART_SpiUartGetRxBufferSize())
    {
            myData[*numOfBytesReceived] = UART_SpiUartReadRxData();
            *numOfBytesReceived += 1;
    }
	return 0; // Doesnt matter what you return... i dont think that it is checked
}

int32_t PSoC_Transmit(void* data, uint32_t size, int32_t *numOfBytesSent )
{
    UART_SpiUartPutArray((uint8_t *)data,size);
    *numOfBytesSent=size;
    
    return 0; // Doesnt matter what you return... i dont think that it is checked
}

#endif	/*(TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)*/
#endif  /*(TRC_USE_TRACEALYZER_RECORDER == 1)*/
</pre>
<p>There at two things that are a bit goofy.</p>
<ul>
<li>The return values for both of these function is not used by the calling functions&#8230; and is not defined what it means.</li>
<li>The 3rd parameter of both functions is defined as int32_t, but in the TraceRecorder library it is defined as int.  This makes a warning, which I got rid of by fixing the bug in the Percepio code.</li>
</ul>
<h1>Testing</h1>
<p>To test this you need to change the Tracealyzer settings from JLink to Serial and fix the COM port and baud rate.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-6.17.13-PM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-6.17.13-PM.png" alt="Percepio Tracelyzer PSoC Setup" width="557" height="442" class="alignnone wp-image-3938 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-6.17.13-PM.png 557w, https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-6.17.13-PM-300x238.png 300w" sizes="(max-width: 557px) 100vw, 557px" /></a></p>
<p>When I built this code originally, I tried 115200 baud.  But it didn&#8217;t work at all (it locked up FreeRTOS).  Then I tried 921600 which locked up the Tracealyzer.  I am running Tracealyzer and PSoC Creator on a Mac in a VMWare Fusion Windows 7 box and I am pretty sure that there is a USB bridging problem (I am going to try it on a Windows box).  After grinding for a while I found out that both 230400 and 460800 both worked but both showed something very funny, look at the data:</p>
<p>230400 Baud</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-3.20.49-PM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-3.20.49-PM.png" alt="Percepio Tracelyzer PSoC - Streaming Data" width="594" height="326" class="alignnone wp-image-3921 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-3.20.49-PM.png 594w, https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-3.20.49-PM-300x165.png 300w" sizes="(max-width: 594px) 100vw, 594px" /></a> <a href="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-3.20.29-PM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-3.20.29-PM.png" alt="Percepio Tracelyzer PSoC - Streaming Data" width="778" height="849" class="alignnone wp-image-3922 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-3.20.29-PM.png 778w, https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-3.20.29-PM-600x655.png 600w, https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-3.20.29-PM-275x300.png 275w, https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-3.20.29-PM-768x838.png 768w" sizes="(max-width: 778px) 100vw, 778px" /></a></p>
<p>460800 Baud</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-3.23.31-PM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-3.23.31-PM.png" alt="Percepio Tracelyzer PSoC - Streaming Data" width="596" height="359" class="alignnone wp-image-3925 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-3.23.31-PM.png 596w, https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-3.23.31-PM-300x181.png 300w" sizes="(max-width: 596px) 100vw, 596px" /></a></p>
<p><a href="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-3.24.02-PM.png"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-3.24.02-PM.png" alt="Percepio Tracelyzer PSoC - Streaming Data" width="776" height="848" class="alignnone wp-image-3924 size-full" srcset="https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-3.24.02-PM.png 776w, https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-3.24.02-PM-600x656.png 600w, https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-3.24.02-PM-275x300.png 275w, https://iotexpert.com/wp-content/uploads/2017/07/Screen-Shot-2017-07-29-at-3.24.02-PM-768x839.png 768w" sizes="(max-width: 776px) 100vw, 776px" /></a></p>
<p>I knew that my implementation of the Percepio Tracealyzer PSoC Streamport would be demanding on the CPU, but I did not predict that it would take 30-50% of the CPU.  This is definitely a problem as the JLink RTT Streamport only takes 0.4% of the CPU.  To fix this I am going to try using the PSoC4200M DMA to automatically transfer the buffers to the UART.  Hopefully that will make my Tracealyzer PSoC streamport better.  But that is for the next Article.</p>
<p><span><p>As always you can find all of these projects on the <a href="https://github.com/iotexpert/PSoC-Tracelyzer" target="_blank" rel="noopener">IotExpert GitHub</a> site or git@github.com:iotexpert/PSoC-Tracelyzer.git</p>
<p><div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >Article</th>
<th >Description</th>
</tr>
</thead>
<tbody>
<tr><td ><a href="https://iotexpert.com/2017/07/18/percepio-tracelyzer-psoc-4200m/">Percepio Tracealyzer &amp; PSoC</a></td>
<td >An Introduction to Percepio Tracealyzer on the Cypress PSoC</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/07/25/perception-tracelyzer-rtt-streamport-psoc4200m/">Percepio Tracealyzer RTT Streamport - PSoC4200M</a></td>
<td >Make the JLINK RTT Library work with Tracealyzer</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/08/01/percepio-tracelyzer-psoc-uart-streamport/">Percepio Tracealyzer PSoC UART Streamport</a></td>
<td >Creating a UART Streamport</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/08/03/psoc-tracelyzer-streampoint/">Percepio Tracealyzer - Analyzing the PSoC Tracealyzer Streamport</a></td>
<td >Figure out what is broken in the UART Streamport</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/08/08/percepio-tracelyzer-a-psoc-dma-streamport/">Percepio Tracealyzer - Using PSoC DMA to Fix the UART Streamport</a></td>
<td >Implementing PSoC DMA to improve the CPU Utilization</td>
</tr>

<tr><td ><a href="https://iotexpert.com/2017/08/15/percepio-tracealyzer-on-psoc6/">Percepio Tracealyzer - Running on PSoC6</a></td>
<td >Porting the Percepio Tracealyzer to PSoC6</td>
</tr>
</tbody></table></div></p></span></p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/percepio-tracealyzer-psoc-uart-streamport/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
