From the schematics, we see that there are three pins associated with the USB port. 1. PA11 2. PA12 3. PD2
The Pins PA11 and PA12 are PA11 – USB D- PA12 – USB D+
These two pins will be configured by the stm32cube ide when you enable the USB device.
PD2 should be configured as GPIO pin. Because the USB FS implementation says to pull up the D+ line to 3.3V. The pull up is performed by the S8550 PNP transistor. So by making the PD2 pin LOW, we enable the USB FS, since it makes the D+ pull up.
We also need to select the Communication Device Class as Class For FS IP.
After configuring the cube mx project. we can proceed to generate code.
The code needs to add the following header file
/* USER CODE BEGIN Includes */
#include "usbd_cdc_if.h"
#include "string.h"
/* USER CODE END Includes */
and then in the main() function. you can write this code in while loop
/* USER CODE BEGIN 2 */
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
/* USER CODE END 2 */
/* USER CODE BEGIN WHILE */
uint8_t *data = "Hello World from USB CDC\r\n";
while (1)
{
CDC_Transmit_FS(data, strlen(data));
HAL_Delay (1000);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
After uploading the code to your microcontroller. It will be displayed in your windows device manager as “USB Serial Device”.
You can Connect to the Com port using the serial terminal software such as YAT, Putty or CoolTerm etc. Note: Since it is a virtual com port, you dont have to set a specific baud rate.
Copy that folder and place it into the driver folder of your stm32cubeide project.
Insert the following header files into your main.c file
/* USER CODE BEGIN Includes */
#include "stm32f103xb.h"
#include "stdio.h"
#include "stdlib.h"
#include "../../Drivers/LCD28/ili932x.h"
/* USER CODE END Includes */
Add the following code to initialize the LCD
/* USER CODE BEGIN 2 */
HAL_GPIO_WritePin(LCD_BL_EN_GPIO_Port, LCD_BL_EN_Pin, GPIO_PIN_SET);
LCD_Init();
LCD_Clear(BLACK);
/* USER CODE END 2 */
Here are the list of functions you can use to make graphic on the 2.8 inch display
/* USER CODE BEGIN WHILE */
HAL_GPIO_WritePin(LCD_BL_EN_GPIO_Port, LCD_BL_EN_Pin, GPIO_PIN_SET);
HAL_Delay(1000);
LCD_Init();
LCD_Clear(BLACK);
uint8_t *textPtr;
// Example usage of the provided LCD functions
LCD_Clear(0xFFFF); // Clear the LCD screen with white color
// Draw a blue line from (10, 10) to (100, 50)
LCD_DrawLine(10, 10, 100, 50);
// Draw a filled rectangle from (120, 50) to (180, 150) with red color
LCD_Fill(120, 50, 180, 150, 0xF800);
// Draw a green circle with center at (200, 200) and radius 20
Draw_Circle(200, 200, 20);
// Write a string at (50, 250) with black color
textPtr = ((uint8_t *)"Hello www.EXASUB.com");
WriteString(50,(250),textPtr,RED);
// Color array in the desired order
uint16_t colors[] = {BLACK, NAVY, DGREEN, DCYAN, MAROON, PURPLE, OLIVE, LGRAY,
DGRAY, BLUE, GREEN, CYAN, RED, MAGENTA, YELLOW, WHITE};
uint8_t numColors = sizeof(colors) / sizeof(colors[0]);
// Index variable for cycling through colors
uint8_t colorIndex = 0;
while (1)
{
/* USER CODE END WHILE */
textPtr = ((uint8_t *)"Hello www.EXASUB.com");
WriteString(50,(250),textPtr,colors[colorIndex]);
// Increment the color index and wrap around if necessary
colorIndex = (colorIndex + 1) % numColors;
textPtr = ((uint8_t *)"ScIeNcE TeCh EnG MaTh ");
WriteString(10,(270),textPtr,colors[colorIndex]);
HAL_Delay(250);
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
In the Project Explorer pane, expand the “Src” folder and open the “main.c” file. Scroll down to the main() function and add the following code to configure the GPIO pins:
This code configures pin 2 on GPIOA as a push-pull output pin with no pull-up or pull-down resistors and low output speed.
Blink the LED using HAL_GPIO_WritePin
Add the following code inside the while(1) loop to blink the LED:
/* Blink LED */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_SET); // Turn LED on
HAL_Delay(1000); // Wait for 1 second
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_RESET); // Turn LED off
HAL_Delay(1000); // Wait for 1 second
This code turns the LED on by setting the state of pin 2 on GPIOA to high, waits for 1 second, turns the LED off by setting the state of pin 2 on GPIOA to low, and then waits for 1 second again. This creates a blinking effect.
Blink the LED using HAL_GPIO_TogglePin
/* Blink LED */
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_2); // Toggle the state of the LED
HAL_Delay(1000); // Wait for 1 second
This code toggles the state of pin 2 on GPIOA (which is connected to an LED on the Mini STM32 board) and then waits for 1 second before toggling it again. This creates a blinking effect.
The first line toggles the state of an LED connected to the LED1_GPIO_Port and LED1_Pin. The second line waits for 1000 milliseconds (1 second) using the HAL_Delay() function. The third line toggles the state of another LED connected to the LED2_GPIO_Port and LED2_Pin.
I used two timers, the TIMER 2 generate the horizontal sync and then the second timer TIMER 3 is configured in gated slave mode which generates the vertical sync.
Then I used HAL_SPI_TRANSMIT_DMA function inside the timer 2 interrupt handler.
I in a hurried manner connected the video signal to the monitor directly. And that has burned the image onto the monitor.
I connected the green video signal directly from the stm32f103 to the VGA connector.
This development board has an STM32F103RB microcontroller. In this development board,
2.8 inch Resistive Touch TFT LCDSTM32F103RB based Development BoardBack side of TFT-LCDBack side of STM32F103RB board
8 Mhz Crystal
32.768 Khz Crystal
2.8 inch TFT-LCD Touch screen. The screen is a TFT-LCD Panel driven by the ili9325 driver.
PL2303 USB to Serial IC
User Programmable USB port
battery holder
Potentiometer
JTAG Port for debug and programming
2 User programmable LED (both Red Colour)
2 User configurable Push button
Boot 0 Button
Reset Button
AMS117 3.3 V LDO
Mini STM32 Schematic Diagram
2.8 inch TFT LCD Schematic
You can develop a program for this board using Keil, IAR or STM32 Cube IDE
You can program the microcontroller by holding the BOOT0 button and then while holding the boot button pressing the RESET button. Which enable you to program the microcontroller using UART.
Printf() function can be redirected to USART and also towards SWO.
Here you will see how to redirect printf() to USART in STM32f103RB
You need to rewrite this code in your main.c file
/*
* Function Name: _write
* Function Description: Redirect the printf() statement towards the UART using the HAL_UART_Transmit
Author: Abhay
*/
int _write(int fd, char* ptr, int len) {
HAL_UART_Transmit(&huart1, (uint8_t *) ptr, len, HAL_MAX_DELAY);
return len;
}
If you have created your project using STM32CubeMX or STM32 Cube IDE, then you can rewrite it in between USER CODE BEGIN 0 as shown
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int _write(int fd, char* ptr, int len) {
HAL_UART_Transmit(&huart1, (uint8_t *) ptr, len, HAL_MAX_DELAY);
return len;
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
After adding the code, connect the uart to your laptop or computer using serial to USB adapter and open serial Terminal application like YAT. Then you will be able to see printf message over there.
To use the ST-LINK/V2-1 to program the STM32 on an external application board (out of the STM32L476VGT6 onboard), remove the two jumpers from CN3 as shown in the above figure in red, and connect the board to the CN4 software debug connector according to Table. Make sure the jumpers JP6.3V3, and JP5.OFF are set. JP3, must be ON if CN4 pin 5 (NRST) is used in the external application board.
Pin
CN4
Function
JTAG PIN Number
JTAG Name
Note
1
Vapp
VDD from Application
Do not Connect Vapp to JTAG.
2
SWCLK
SWD Clock
9
TCK
3
GND
Ground
1 to 9
GND
4
SWDIO
SWD data input/output
7
TMS
5
NRST
RESET of target MCU
15
nSRST
6
SWO
RESERVED
Note: Do not connect Vapp to JTAG on the external board unless you know about the power domain of the external board. Power the board separately.
You can use STM32CubeProgrammer to read the memory and also write the hex file into the microcontroller.
Here I am using mini stm32 v3.0 as an example.
The mini stm32 v3.0 has an STM32F103RB microcontroller along with a JTAG interface for programming and debugging.