STM32F030F4P6 USART1 init with STM32 Standard Peripheral Libraries

I could not find USART1 example code using the STM32 Standard Peripheral Libraries  for a STM32F0 device. Therefore I’ll post mine…

  usart.c

#include "usart.h"


/**
**===========================================================================
**
**  Abstract: Initialize USART1
**
**===========================================================================
*/
void InitUSART1(void)
{

	// USART peripheral initialization settings    
	USART_InitTypeDef USART_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_ClockInitTypeDef USART_ClockInitStructure;

	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	
	//Configure USART1 pins: Rx (PA2), Tx(PA3)
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	// configure GPIO pins with GPIO_Mode_AF before setting the AF config!
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
	
	//Configure USART1 setting: ----------------------------
	USART_StructInit(&USART_InitStructure);			// default 8bit, 9600 baud, stopbit=1, parity=none, full duplex, no hardware flowcontrol
	USART_InitStructure.USART_BaudRate = 115200;	// set baudrate to 115k2
	
	USART_OverSampling8Cmd(USART1, ENABLE); // standard oversampling = 16

	USART_Init(USART1, &USART_InitStructure); // USART is disabled after calling the USART_Init function
	
	USART_Cmd(USART1, ENABLE);
	
	Serial_PutString(USART1, "USART1 Init done...\r\n");
}
/**
**===========================================================================
**
**  Abstract: Get_Byte (blocking mode)
**
**===========================================================================
*/

uint8_t Serial_GetByte(USART_TypeDef *USARTx)
{
	while (USART_GetFlagStatus(USARTx, USART_FLAG_RXNE) == RESET)
		;
	return USART_ReceiveData(USARTx);
}
/**
**===========================================================================
**
**  Abstract: Send_Byte (blocking mode)
**
**===========================================================================
*/
void Serial_PutByte(USART_TypeDef *USARTx, uint8_t byte)
{
	while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET)
		;
	USART_SendData(USARTx, byte);
}
/**
**===========================================================================
**
**  Abstract: SendPacket
**
**===========================================================================
*/
void Serial_SendPacket(USART_TypeDef *USARTx, uint8_t *data, uint16_t length)
{
	uint16_t i;
	i = 0;
	while (i < length)
	{
		Serial_PutByte(USARTx, data[i]);
		i++;
	}
}
/**
**===========================================================================
**
**  Abstract: SendString
**
**===========================================================================
*/
void Serial_PutString(USART_TypeDef *USARTx, char *p_string)
{
	uint16_t length = 0;

	while (p_string[length] != '\0')
	{
		Serial_PutByte(USARTx, p_string[length]);
		length++;
	}
}

  usart.h

#pragma once
#include 
#include 
#include 
#include 

void InitUSART1(void);
void Serial_PutByte(USART_TypeDef *USARTx, uint8_t byte);
uint8_t Serial_GetByte(USART_TypeDef *USARTx);
void Serial_SendPacket(USART_TypeDef *USARTx, uint8_t *data, uint16_t length);
void Serial_PutString(USART_TypeDef *USARTx, char *p_string);
Posted in ARM, STM32 | Leave a comment

Prolific PL2303HX USB serial

I while ago I bought a couple of Prolific USB to Serial adapters not knowing that Prolific has ended support for “previous generation” chips on Windows 8 and up. Luckily a driver has been compiled to support all prolific IC’s
4-wire PL2303HX USB serial

Black cable—–GND
Green cable—-TXD
White cable—-RXD
Red cable —–5V

driver: Prolific USB To Serial Driver Fix!

 

Posted in Uncategorized | Leave a comment

Building a custom external loader for ST-LINK

Project is currently on hold…

I noticed the “External Loader” entry in the ST-LINK utility and wondered how to write a custom external loader plug-in for ST-LINK.  The “STM32 ST-LINK Utility software description user Manual” UM0892 has a tiny section 3.9 dedicated to

“Developing custom loaders for external memory”

I have found out by searching the internet that a custom loader is just a library in the ELF format.

Currently I’m collecting info about how to write and build a custom loader, I’ll use Eclips + GCC a STM32F030 and a W25Q32 SPI FLASH memory.

STM32F030Fx_SPI_loader

notes:

ST Forum – custom loader

The loader applet is an ELF object, with specific function/variable names (symbols) which ST’s loader is expecting. It payloads your code into RAM, and then calls you to initialize your hardware (processor clocks, PLL, pins, SPI, etc), and then with data buffers it provides expects you to Read or Write blocks of data to your device, or Erase/Mass Erase, etc. The ST tools are responsible to get your code into memory, and the pattern buffers in/out, and your code is responsible to deal with your hardware as it presents on the device.

LINKER:

Running programs from SRAM
==========================

You can run your firmware directly from SRAM if you want to. Just link it at 0x20000000 looking at the Keil linker configuration: the FlashLoader starts at address 0x20000004


FLASH_LOADER 0x20000004 PI   ; FlashLoader Functions
{
  PrgCode +0           ; Code
  {
    * (+RO)
  }
  PrgData +0           ; Data
  {
    * (+RW,+ZI)
  }
}

DEVICE_INFO +0               ; Device Info
{
  DevInfo +0           ; Info structure
  {
    dev_inf.o
  }
}

I would like to target a Cortex M0 with 4K SRAM so there is not much room for the loader application code + data buffers.

assumptions:

  • databuffer size = pagesize = 256 bytes?

API:

operations:

  • Init
  • Read
  • Write
  • MassErase
  • SectorErase
  • Verify

Dev_inf.h


#define 	MCU_FLASH   1
#define 	NAND_FLASH  2
#define 	NOR_FLASH   3
#define 	SRAM        4
#define 	PSRAM       5
#define 	PC_CARD     6
#define 	SPI_FLASH   7
#define 	I2C_FLASH   8
#define 	SDRAM       9
#define 	I2C_EEPROM  10

#define SECTOR_NUM 10				// Max Number of Sector types

struct DeviceSectors  
{
  unsigned long		SectorNum;     // Number of Sectors
  unsigned long		SectorSize;    // Sector Size in Bytes
};

struct StorageInfo  
{
   char           DeviceName[100];			// Device Name and Description
   unsigned short DeviceType;				// Device Type: ONCHIP, EXT8BIT, EXT16BIT, ...
   unsigned long  DeviceStartAddress;		        // Default Device Start Address
   unsigned long  DeviceSize;				// Total Size of Device
   unsigned long  PageSize;				// Programming Page Size
   unsigned char  EraseValue;				// Content of Erased Memory
   struct 	  DeviceSectors	 sectors[SECTOR_NUM];
};

Dev_inf.c


#include "Dev_Inf.h"

/* This structure containes information used by ST-LINK Utility to program and erase the device */
struct StorageInfo const StorageInfo  =  {
   "W25Q32_STM32F0_SPI1",   // Device Name + version number
   SPI_FLASH,               // Device Type
   0x00000000,              // Device Start Address
   0x00400000,              // Device Size in Bytes 4MBytes/32Mbits)
   0x00000100,              // Programming Page Size 256 Bytes
   0xFF,                    // Initial Content of Erased Memory
// Specify Size and Address of Sectors
   0x00000040,              // Sector Num : 64  
   0x00010000,  	    // Sector Size: 64KBytes 
   0x00000000,              // ?
   0x00000000,              // ?
}; 

Loader_Src.c


#include "stm32f03x.h"
#include "stm32_spi_flash.h"
#include "board_layout.h"

/********************************************************************************************
* Description :																			
* Initilize the MCU Clock, the GPIO Pins corresponding to the 		
* device and initilize the FSMC with the chosen configuration 		
* Inputs 	:																		
* 				 None 																
* outputs 	:																		
* 				"1" 			: Operation succeeded								
* 				"0" 			: Operation failure										
* Note: Mandatory for all types of device 									
*********************************************************************************************/
int Init (void)
{  
        SystemInit();
	sFLASH_Init();
	return 1;
}


/********************************************************************************************
* Description :																			
* Read data from the device 													    
* Inputs :																			
* 				Address 	: Read location  										
* 				Size 		: Length in bytes 										
* 				buffer 		: Address where to write readed data			
* outputs :																			
* 				"1" 			: Operation succeeded								
* 				"0" 			: Operation failure										
* Info :																			
* Alignment and Data size (32/16/8 bits) should be handled in this function 	
* Note : Not Mandatory for SRAM PSRAM and NOR_FLASH 				
*********************************************************************************************/
int Read (uint32_t Address, uint32_t Size, uint8_t* buffer)
{ 
	sFLASH_ReadBuffer(buffer, Address, Size);
	return 1;
} 


/********************************************************************************************
* Description :																			
* Write data to the device	 														
* Inputs :																			
* 				Address 	: Write location  										
* 				Size 		: Length in bytes 										
* 				buffer 		: Address where to get the data to write		
* outputs :																			
* 				"1" 			: Operation succeeded								
* 				"0" 			: Operation failure									
* Info :																			
* Alignment and Data size (32/16/8 bits) should be handled in this function 	
* Note : Mandatory for all types except SRAM and PSRAM			
*********************************************************************************************/
int Write (uint32_t Address, uint32_t Size, uint8_t* buffer)
{
	sFLASH_WriteBuffer(buffer, Address, Size);
	return 1;
}

/********************************************************************************************
* Description :																		
* Full erase of the device 															
* Inputs :																			
* 				None 																
* outputs :																			
* 				"1" : Operation succeeded											
* 				"0" : Operation failure													
* Info :																			
* Note : Not Mandatory for SRAM PSRAM and NOR_FLASH		
*********************************************************************************************/
int MassErase (void)
{  
	sFLASH_EraseBulk();
	return 1;	
}

/********************************************************************************************
* Description :							
* Erase a full sector in the device 												
* Inputs :																			
* 				SectrorAddress	: Start of sector 								
* outputs :																			
* 				"1" : Operation succeeded											
* 				"0" : Operation failure													
* Note : Not Mandatory for SRAM PSRAM and NOR_FLASH		
*********************************************************************************************/
int SectorErase (uint32_t EraseStartAddress ,uint32_t EraseEndAddress)
{      
	EraseStartAddress = EraseStartAddress -  EraseStartAddress%0x10000;
	while (EraseEndAddress>=EraseStartAddress)
	{
	 sFLASH_EraseSector(EraseStartAddress);
	 EraseStartAddress += 0x10000;
	}
 	return 1;	
}

/********************************************************************************************
* Description :                                                                                                                                
* Initilize the MCU Clock, the GPIO Pins corresponding to the           
* device and initilize the FSMC with the chosen configuration           
* Inputs    :                                                                                                                                 
*      FlashAddr   : Flash address                               
*      RAMBufferAddr : RAM buffer address                           
*      Size         : Size (in WORD)                      
* outputs   :                                                                                                                                 
*     "0"          : Operation succeeded                          
*     address of failure : Operation failed (address of failure)  
* Note: Optional for all types of device                          
********************************************************************************************/
int Verify (uint32_t MemoryAddr, uint32_t RAMBufferAddr, uint32_t Size)
{ 
        uint32_t VerifiedData = 0;
        uint8_t TmpBuffer = 0x00;
        Size*=4;
        
        while (Size>VerifiedData)
        {
          sFLASH_ReadBuffer(&TmpBuffer, MemoryAddr+VerifiedData, 1);
          
          if (TmpBuffer != *((uint8_t*)RAMBufferAddr+VerifiedData))
            return MemoryAddr+VerifiedData;
          
          VerifiedData++;  
        }
        
        return 0;
}

Posted in STM32 | 5 Comments

CMSIS-DAP on a cheap ST-LINK V2 mini adapter

I while ago I purchased several ST-LINK V2 mini adapters on Aliexpress. It would be great if those adapters could be converted to CMSIS-DAP debuggers.

Fortunately the CMSIS-DAP firmware was ported to STM32 and the ST-LINK V2 hardware.

See the following post for details: http://akb77.com/g/stm32/cmsis-dap/

I have two ST-LINK V2 mini adapter versions, an USB dongle like device and a plain PCB version. I have used the dongle to program the PCB version.

The dongle version:

ST-LINK V2 dongle

The PCB version

ST-LINK V2 pcb

Connecting the wires

Connection the wires

I have used the STM32 ST-Link utility to program the following HEX file into the device:

https://github.com/x893/CMSIS-DAP/blob/master/Firmware/STM32/hex/CMSIS-DAP-STLINK21.hex

Then I used the CooCox CoFlash programmer to test the CMSIS-DAP debugger which could connect to a STM32F030 device!

Later I’ll setup an Eclips toolchain on Windows 10 to test the CMSIS-DAP debugger.

Posted in ARM, STM32 | 8 Comments

eCyberspaces 4.0 inch 8bit 39P HD TFT LCD Screen R61509V 400 x 240 with Touch Panel (FTM39733A)

link: http://www.ecyberspaces.com/productsview.asp?id=3420

Pinout:

4.0 39P TFT LCD Screen(169) R61509V 400240 TP (ecyberspaces.com)

Posted in Uncategorized | Leave a comment

eCyberspaces 3.2 inch 44PIN HD TFT LCD Screen ILI9481 Drive IC 320*480

I have ordered a few displays via aliexpress

The 3.2 Inch screen is a WistronOpt P/N:R14158250C (without touch screen)

pin connections, o.8mm pitch solder-on flex

ILI9481 44pin pinout ecyberspaces.com

Posted in Uncategorized | 1 Comment

mcufriend 3.5 inch for uno TFT Touch shield

Shield schematic : MCUfriend 3.5 inch for uno shield schematic

Controller Datasheet : ILI9327DS V0.06

Touchscreen resistance : 300 Ohm

The LCD resolution is 400×240

3.5 inch for uno bottom 3.5 inch for uno top

Posted in Uncategorized | 1 Comment