BBG 2018 first day

BBG2018Participants
We had two workshops in Gorey Library (Wexford, Ireland) on Friday 10th of August. In total there were 35 attendees spread across the morning and afternoon. Happily no components were forgotten and apart from one or two malfunctioning STM32 “Blue pill” boards, all went well. We got great feedback from the library staff, parents and children.
The library’s Facebook post regarding the event is here

Nearly ready for Dublin Maker 2018

Putting the (hopefully) final touches to the paper based programming demos for Dublin Maker 2018 (July 21st, Merrion Square).
There are two demos here. The first controls a robot arm that can retrieve a sweet for the user. The user “programs” the arm by colouring in bits on the paper roll. Each column of the paper roll corresponds to an 8 bit opcode. The two MSB’s select which of the servo motors is to be controlled, the remaining 6 bits determine the angle.
The second demo is a music box similar in design to the first except that the opcodes are 15 bits long. 12 of these select the note, 2 select the octave and the 15th bit is a clock bit that allows the microcontroller control the motor speed. Fingers crossed these will work on the day 🙂

Breaboard Games 2018

Following on from last years successful day in Bunclody there will be 5 days of BBG in August this year scattered across various libraries in Co. Wexford. This year the hardware is the same as last – subject to random changes in the display sent by the supplier :). The software has moved on a bit however and the system can now use the touch screen to allow users edit sprites.
bbg2018edit
Apart from the addition of the edit function, the software has been refactored internally to (hopefully) allow for greater community involvement and to ease the transition between different screen types.
Full parts lists, schematics and source code will follow in July after a little more polishing.

Using Flash memory on the STM32F103

Some microcontrollers have a dedicated Non-Volatile-Memory (NVM) bank for storing calibration data, program settings and so on. The STM32F103C8T6 does not have NVM like this but it’s Flash program memory can be used with care for the same purpose. The Flash memory in this chip is divided into 1kiB sectors and there are 64 if them (0 to 63). The code to erase, write and read a sector is shown below:

int  writeSector(uint32_t Address,void * values, uint16_t size)
{              
    uint16_t *AddressPtr;
    uint16_t *valuePtr;
    AddressPtr = (uint16_t *)Address;
    valuePtr=(uint16_t *)values;
    size = size / 2;  // incoming value is expressed in bytes, not 16 bit words
    while(size) {        
        // unlock the flash 
        // Key 1 : 0x45670123
        // Key 2 : 0xCDEF89AB
        FLASH->KEYR = 0x45670123;
        FLASH->KEYR = 0xCDEF89AB;
        FLASH->CR &= ~BIT1; // ensure PER is low
        FLASH->CR |= BIT0;  // set the PG bit        
        *(AddressPtr) = *(valuePtr);
        while(FLASH->SR & BIT0); // wait while busy
        if (FLASH->SR & BIT2)
            return -1; // flash not erased to begin with
        if (FLASH->SR & BIT4)
            return -2; // write protect error
        AddressPtr++;
        valuePtr++;
        size--;
    }    
    return 0;    
}
void eraseSector(uint32_t SectorStartAddress)
{
    FLASH->KEYR = 0x45670123;
    FLASH->KEYR = 0xCDEF89AB;
    FLASH->CR &= ~BIT0;  // Ensure PG bit is low
    FLASH->CR |= BIT1; // set the PER bit
    FLASH->AR = SectorStartAddress;
    FLASH->CR |= BIT6; // set the start bit 
    while(FLASH->SR & BIT0); // wait while busy
}
void readSector(uint32_t SectorStartAddress, void * values, uint16_t size)
{
    uint16_t *AddressPtr;
    uint16_t *valuePtr;
    AddressPtr = (uint16_t *)SectorStartAddress;
    valuePtr=(uint16_t *)values;
    size = size/2; // incoming value is expressed in bytes, not 16 bit words
    while(size)
    {
        *((uint16_t *)valuePtr)=*((uint16_t *)AddressPtr);
        valuePtr++;
        AddressPtr++;
        size--;
    }
}

Writes and reads must be performed in 16 bit (half word) chunks so a little bit of type casting is necessary. The data type for the values to be read or written is void *. This allows a pointer to any type be passed without generating warnings. The sector start address must be on a 1k boundary. In this chip, Flash memory starts at 0x8000000 so sector start addresses can be 0x8000000, 0x8000400, 0x8000800, 0x8000c00, etc.
The data sheet states that the flash can withstand at least 10000 write cycles (does this include erases?). This seems like a pretty large amount but you would be surprised how quickly it can get used up. If your program requires to write to Flash memory regularly consider some wear leveling scheme.
Full code is available over here on GitHub