Simulating SPI Memory with Javascript - Part 1

by Crossware 4. March 2015 12:05

A program may store persistent data in on-chip flash and in a previous blog I showed you how to initialized the simulating on-chip flash from Javascript.

Programs that also or instead store data in SPI memory may want to simulate this off-chip memory too.  So in the next few blogs I will show you how to simulate SPI memory with Javascript.

My example program is using bit-bashing to control the SPI memory and it is writing the BSRR register of port B to do this (STM32Fxxx chip).  So my first step it to create an event handler which will be called whenever my simulating program writes to g_pGPIOB->BSRR. I can get the Embedded Development Studio to create the appropriate Javascript code for me.  To do this, I right mouse click on my Javascript source file and from the context menu that opens select jState Code Creation Wizards->GPIOB.

 

 

When I click on GPIOB, the code creation wizards opens:

This allows me to see all of the events and properties available for jState.gpiob.

I am interested in the first item and when I select it I see the source code (with comments) that the wizard will generate for me.  I click on Insert and this Javascript code it is placed into my Javascript file.

var writeGpiobBSRR = function(event) {
    // the Bit Set-Reset Register (BSRR) is being written
    // event.data contains the value being written to it
    // jState.gpiob.m_nODR has not yet been updated
    // add your javascript code here:
}
jState.gpiob.addEventListener("writeBSRR", writeGpiobBSRR, false);

This gives me both an event handler and the code that will register that event handler.

Now, whenever my simulating program writes to the BSRR register of port B, my event handler will be called.

You might want to put something like console.log("Event handler called\n") into this event handler for test purposes.

I am going to create a Javascript Object to encapsulate my SPI Memory simulation code:

var SPIMemory = function() {
}

var spiMemory = new SPIMemory();

 and into this object I can put a suitable function such as handlePortPin that I can call from the event handler:

var SPIMemory = function() {
    this.handlePortPin = function(portb, bsrr) {
    }
};

This function has two parameters. portb will contain the value of ODR (output data register) and the other is the value being written to BSRR.

Here is the modified event hander containing a call to this function:

var writeGpiobBSRR = function(event) {
    // the Bit Set-Reset Register (BSRR) is being written
    // event.data contains the value being written to it
    // jState.gpiob.m_nODR has not yet been updated
    // add your javascript code here:
    var nODR = jState.gpiob.ODR;
    spiMemory.handlePortPin(nODR, event.data);
}

With these two arguments I can determine the value of the output data register both before and after the write to BSRR. I do this in function updatePort below.  Then I will know the levels of each port B pin and whether or not they are rising or falling.

var SPIMemory = function() {
    this.updatePort = function(port, bsrr) {
        // portb has not yet been updated by the BSRR value
        // return the value that portb will become
        var newport = port;
        newport |= bsrr & 0XFFFF;
        newport &= ~(bsrr >> 16) & 0XFFFF;
        return newport;
    }
    this.handlePortPin = function(portb, bsrr) {
    }
};

In the next post I will add some code to send the chip Id to the simulating program in response to the 0X9F (ReadId) command.

Tags: , , , ,

Blog

Accessing on-chip memory from Javascript

by Crossware 27. February 2015 09:59

Today we updated the jState support within our Cortex-M3 and Cortex-M4 extensions to allow access to on-chip memory from Javascript.

The jState code creation wizard for the on-chip memory (accessible by right clicking on the Javascript file) will tell you what functions are available.

The image below shows code that it has created for us to set the on-chip memory read and write addresses to 0XE0000 and the increment on both read and write to 2.  (To increase the read or write address by 2 after each read or write.)

 

 

Clicking on Insert inserts this code into our Javascript file:

jState.onchipmemory.readaddress = 0X000E0000;
jState.onchipmemory.writeaddress = 0X000E0000;
jState.onchipmemory.readincrement = 2;
jState.onchipmemory.writeincrement = 2;


Our intension is to write a table of values into the on-chip flash memory sector at 0XE0000 - our simulating firmware is expecting this table to be in place at startup. So with a bit of manual editing we have the following Javascipt code:

console.clear();

PopulateTable()


function PopulateTable() {
    var TableData = [
        33, 38, 39, 44, 53, 55, 64, 69, 70, 75,
        156, 161, 162, 167, 176, 178, 187, 192, 193, 198,
        283, 288, 289, 294, 303, 305, 314, 319, 320, 325,
        411, 416, 417, 422, 431, 433, 442, 447, 448, 453,
        546, 551, 552, 557, 566, 568, 577, 582, 583, 589,
        661, 666, 667, 672, 681, 683, 692, 697, 698, 703]
    jState.onchipmemory.readaddress = 0X000E0000;
    jState.onchipmemory.writeaddress = 0X000E0000;
    jState.onchipmemory.readincrement = 2;
    jState.onchipmemory.writeincrement = 2;
    for (var i = 0; i < 60; i++) {
        jState.onchipmemory.value16 = TableData[i];
    }
    console.log("TableData written to flash:");
    for (var i = 0; i < 60; i++) {
        if ((i % 10) == 0) {
            console.log("\n");
        }
        console.log(jState.onchipmemory.value16 + ",");
    }
}

When step into the simulator we can see that the data has been written into the correct memory locations:

 

By the way, to create the initial Javascript file select File->New and in the Files tab select Javascript Source File.  The Embedded Development Studio will populate the file name field for you so just click OK.

Tags: , , , , ,

Blog

Development Suite for ARM Enhancements

by Crossware 20. February 2015 09:32

The GUI parts of our Development Suite for ARM have been rebuilt using Microsoft's Visual Studio 2013.  That's over 80 DLLs as well as the Embedded Development Studio executable itself and so it was not an insignificant task.

For the embedded developer, this brings two major benefits:

1. The simulator can be graphically extended using Microsoft's latest Visual Studio 2013, including the free Community edition

2. The Cortex-M3 and Cortex-M4 simulations can also be extended using state-of-the art Javascript (see jState)

Unforunately it does mean leaving behind operating systems prior to Windows XP - the minimum requirement for these latest editions of the Development Suite for ARM is Windows XP SP3.

Tags: , , , , , , ,

Blog

First jState supporting Development Suite released

by Crossware 3. May 2012 15:01

jState is our new Javascript simulator extension interface.  With the V8 Javascript engine integrated into the environment, extending the Crossware Cortex-M3 simulator is fast and simple.

The STM32 Development Suite is the first package to incorporate jState and a new evaluation version is available: STM32 Development Suite Download.

For an introduction to jState see: jState Overview

For more details see:  jState Documentation

Tags: , , , , ,

Blog

About this blog

This is where you will find topical information that we think might be useful to you.

Month List