Skip to main content
Visitor II
February 8, 2024
Solved

STM32F103C8 using SPI2

  • February 8, 2024
  • 30 replies
  • 14200 views

Hello,

i am trying to interface 1.8 inch SPI TFT Display 128 x 160 to the STM32F103C8 using SPI2.

https://www.az-delivery.de/products/1-8-zoll-spi-tft-display

SPI1 is used for LoRa, i thought to connect TFT too to SPI1 using addresses but i am affraid to influence the

LoRa.

The library which i am using is from this website:
https://controllerstech.com/st7735-1-8-tft-display-with-stm32/

A year earlier i tried to interface LoRa to STM32F103C8 using SPI2 but without success.

Is it a general problem in STM32F103C8 using SPI2?

Any help is appreciated.

    This topic has been closed for replies.
    Best answer by hamo

    The problem was with the hardware.
    Finally i could get the SPI2 work together with SPI1 on the same Board.
    Many Bluepills are just not good this days.

    Anyway thank you all for being helpful.

    Best regards

    Mohamad

    30 replies

    hamoAuthor
    Visitor II
    February 13, 2024

    @AScha.3  i have just tried this settings and compiled the code and uploaded, it stills not working for me.

    I used the logic analyzer -> all the pins (SPI2) are not toggling.

    SPI2_setting.JPG

    Super User
    February 13, 2024

    What you write, to send on SPI ?

     

    hamoAuthor
    Visitor II
    February 13, 2024

    Here is my main code:

     

    #include "main.h"
    #include "spi.h"
    #include "gpio.h"
    
    /* Private includes ----------------------------------------------------------*/
    /* USER CODE BEGIN Includes */
    
    #include <string.h>
    #include "st7735.h"
    #include "GFX_FUNCTIONS.h"
    #include "fonts.h"
    
    /* USER CODE END Includes */
    
    /* Private typedef -----------------------------------------------------------*/
    /* USER CODE BEGIN PTD */
    
    /* USER CODE END PTD */
    
    /* Private define ------------------------------------------------------------*/
    /* USER CODE BEGIN PD */
    /* USER CODE END PD */
    
    /* Private macro -------------------------------------------------------------*/
    /* USER CODE BEGIN PM */
    
    /* USER CODE END PM */
    
    /* Private variables ---------------------------------------------------------*/
    
    /* USER CODE BEGIN PV */
    
    /* USER CODE END PV */
    
    /* Private function prototypes -----------------------------------------------*/
    void SystemClock_Config(void);
    /* USER CODE BEGIN PFP */
    
    /* USER CODE END PFP */
    
    /* Private user code ---------------------------------------------------------*/
    /* USER CODE BEGIN 0 */
    
    /* 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_SPI2_Init();
     /* USER CODE BEGIN 2 */
    	
    	ST7735_Init();
    
     /* USER CODE END 2 */
    
     /* Infinite loop */
     /* USER CODE BEGIN WHILE */
     while (1)
     {
     /* USER CODE END WHILE */
    
     /* USER CODE BEGIN 3 */
    		
    		ST7735_FillScreen(ST7735_BLACK);
    		ST7735_WriteString(3*18, 10, "Hello", Font_7x10, ST7735_RED, ST7735_BLACK);
    		ST7735_WriteString(5, 3*10, "from", Font_11x18, ST7735_GREEN, ST7735_BLACK);
    		ST7735_WriteString(0, 3*10+3*10, "Kurdistan", Font_16x26, ST7735_YELLOW, ST7735_BLACK);
    		HAL_Delay(3000);
    		ST7735_FillScreen(ST7735_BLACK);
    		drawCircle(50, 55, 20, ST7735_GREEN);
    		drawCircle(50, 55, 21, ST7735_GREEN);
    		drawCircle(50, 55, 22, ST7735_GREEN);
    		drawCircle(50, 55, 23, ST7735_GREEN);
    		drawCircle(50, 55, 24, ST7735_GREEN);
    		drawCircle(50, 55, 25, ST7735_RED);
    		drawCircle(50, 55, 26, ST7735_RED);
    		drawCircle(50, 55, 27, ST7735_RED);
    		drawCircle(50, 55, 28, ST7735_RED);
    		HAL_Delay(4000);
     }
     /* USER CODE END 3 */
    }

     

    here is the header file:

     

    #ifndef __ST7735_H__
    #define __ST7735_H__
    
    #include "fonts.h"
    #include "stm32f1xx_hal.h"
    #include <stdbool.h>
    
    #define ST7735_MADCTL_MY 0x80
    #define ST7735_MADCTL_MX 0x40
    #define ST7735_MADCTL_MV 0x20
    #define ST7735_MADCTL_ML 0x10
    #define ST7735_MADCTL_RGB 0x00
    #define ST7735_MADCTL_BGR 0x08
    #define ST7735_MADCTL_MH 0x04
    
    /*** Redefine if necessary ***/
    #define ST7735_SPI_PORT hspi2
    extern SPI_HandleTypeDef ST7735_SPI_PORT;
    
    #define ST7735_RES_Pin GPIO_PIN_10
    #define ST7735_RES_GPIO_Port GPIOB
    #define ST7735_CS_Pin GPIO_PIN_1
    #define ST7735_CS_GPIO_Port GPIOB
    #define ST7735_DC_Pin GPIO_PIN_0
    #define ST7735_DC_GPIO_Port GPIOB
    
    //#define ST7735_SPI_PORT hspi2
    //extern SPI_HandleTypeDef ST7735_SPI_PORT;
    //
    //#define ST7735_RES_Pin GPIO_PIN_2
    //#define ST7735_RES_GPIO_Port GPIOA
    //#define ST7735_CS_Pin GPIO_PIN_4
    //#define ST7735_CS_GPIO_Port GPIOA
    //#define ST7735_DC_Pin GPIO_PIN_3
    //#define ST7735_DC_GPIO_Port GPIOA
    
    
    // AliExpress/eBay 1.8" display, default orientation
    
    #define ST7735_IS_160X128 1
    #define ST7735_WIDTH 128
    #define ST7735_HEIGHT 160
    #define ST7735_XSTART 0
    #define ST7735_YSTART 0
    #define ST7735_ROTATION (ST7735_MADCTL_MX | ST7735_MADCTL_MY)
    
    
    // AliExpress/eBay 1.8" display, rotate right
    /*
    #define ST7735_IS_160X128 1
    #define ST7735_WIDTH 160
    #define ST7735_HEIGHT 128
    #define ST7735_XSTART 0
    #define ST7735_YSTART 0
    #define ST7735_ROTATION (ST7735_MADCTL_MY | ST7735_MADCTL_MV)
    */
    
    // AliExpress/eBay 1.8" display, rotate left
    /*
    #define ST7735_IS_160X128 1
    #define ST7735_WIDTH 160
    #define ST7735_HEIGHT 128
    #define ST7735_XSTART 0
    #define ST7735_YSTART 0
    #define ST7735_ROTATION (ST7735_MADCTL_MX | ST7735_MADCTL_MV)
    */
    
    // AliExpress/eBay 1.8" display, upside down
    /*
    #define ST7735_IS_160X128 1
    #define ST7735_WIDTH 128
    #define ST7735_HEIGHT 160
    #define ST7735_XSTART 0
    #define ST7735_YSTART 0
    #define ST7735_ROTATION (0)
    */
    
    // WaveShare ST7735S-based 1.8" display, default orientation
    /*
    #define ST7735_IS_160X128 1
    #define ST7735_WIDTH 128
    #define ST7735_HEIGHT 160
    #define ST7735_XSTART 2
    #define ST7735_YSTART 1
    #define ST7735_ROTATION (ST7735_MADCTL_MX | ST7735_MADCTL_MY | ST7735_MADCTL_RGB)
    */
    
    // WaveShare ST7735S-based 1.8" display, rotate right
    /*
    #define ST7735_IS_160X128 1
    #define ST7735_WIDTH 160
    #define ST7735_HEIGHT 128
    #define ST7735_XSTART 1
    #define ST7735_YSTART 2
    #define ST7735_ROTATION (ST7735_MADCTL_MY | ST7735_MADCTL_MV | ST7735_MADCTL_RGB)
    */
    
    // WaveShare ST7735S-based 1.8" display, rotate left
    /*
    #define ST7735_IS_160X128 1
    #define ST7735_WIDTH 160
    #define ST7735_HEIGHT 128
    #define ST7735_XSTART 1
    #define ST7735_YSTART 2
    #define ST7735_ROTATION (ST7735_MADCTL_MX | ST7735_MADCTL_MV | ST7735_MADCTL_RGB)
    */
    
    // WaveShare ST7735S-based 1.8" display, upside down
    /*
    #define ST7735_IS_160X128 1
    #define ST7735_WIDTH 128
    #define ST7735_HEIGHT 160
    #define ST7735_XSTART 2
    #define ST7735_YSTART 1
    #define ST7735_ROTATION (ST7735_MADCTL_RGB)
    */
    
    // 1.44" display, default orientation
    //#define ST7735_IS_128X128 1
    //#define ST7735_WIDTH 128
    //#define ST7735_HEIGHT 128
    //#define ST7735_XSTART 2
    //#define ST7735_YSTART 3
    //#define ST7735_ROTATION (ST7735_MADCTL_MX | ST7735_MADCTL_MY | ST7735_MADCTL_BGR)
    
    // 1.44" display, rotate right
    /*
    #define ST7735_IS_128X128 1
    #define ST7735_WIDTH 128
    #define ST7735_HEIGHT 128
    #define ST7735_XSTART 3
    #define ST7735_YSTART 2
    #define ST7735_ROTATION (ST7735_MADCTL_MY | ST7735_MADCTL_MV | ST7735_MADCTL_BGR)
    */
    
    // 1.44" display, rotate left
    /*
    #define ST7735_IS_128X128 1
    #define ST7735_WIDTH 128
    #define ST7735_HEIGHT 128
    #define ST7735_XSTART 1
    #define ST7735_YSTART 2
    #define ST7735_ROTATION (ST7735_MADCTL_MX | ST7735_MADCTL_MV | ST7735_MADCTL_BGR)
    */
    
    // 1.44" display, upside down
    /*
    #define ST7735_IS_128X128 1
    #define ST7735_WIDTH 128
    #define ST7735_HEIGHT 128
    #define ST7735_XSTART 2
    #define ST7735_YSTART 1
    #define ST7735_ROTATION (ST7735_MADCTL_BGR)
    */
    
    // mini 160x80 display (it's unlikely you want the default orientation)
    /*
    #define ST7735_IS_160X80 1
    #define ST7735_XSTART 26
    #define ST7735_YSTART 1
    #define ST7735_WIDTH 80
    #define ST7735_HEIGHT 160 
    #define ST7735_ROTATION (ST7735_MADCTL_MX | ST7735_MADCTL_MY | ST7735_MADCTL_BGR)
    */
    
    // mini 160x80, rotate left
    /*
    #define ST7735_IS_160X80 1
    #define ST7735_XSTART 1
    #define ST7735_YSTART 26
    #define ST7735_WIDTH 160
    #define ST7735_HEIGHT 80
    #define ST7735_ROTATION (ST7735_MADCTL_MX | ST7735_MADCTL_MV | ST7735_MADCTL_BGR)
    */
    
    // mini 160x80, rotate right 
    /*
    #define ST7735_IS_160X80 1
    #define ST7735_XSTART 1
    #define ST7735_YSTART 26
    #define ST7735_WIDTH 160
    #define ST7735_HEIGHT 80
    #define ST7735_ROTATION (ST7735_MADCTL_MY | ST7735_MADCTL_MV | ST7735_MADCTL_BGR)
    */
    
    /****************************/
    
    #define ST7735_NOP 0x00
    #define ST7735_SWRESET 0x01
    #define ST7735_RDDID 0x04
    #define ST7735_RDDST 0x09
    
    #define ST7735_SLPIN 0x10
    #define ST7735_SLPOUT 0x11
    #define ST7735_PTLON 0x12
    #define ST7735_NORON 0x13
    
    #define ST7735_INVOFF 0x20
    #define ST7735_INVON 0x21
    #define ST7735_GAMSET 0x26
    #define ST7735_DISPOFF 0x28
    #define ST7735_DISPON 0x29
    #define ST7735_CASET 0x2A
    #define ST7735_RASET 0x2B
    #define ST7735_RAMWR 0x2C
    #define ST7735_RAMRD 0x2E
    
    #define ST7735_PTLAR 0x30
    #define ST7735_COLMOD 0x3A
    #define ST7735_MADCTL 0x36
    
    #define ST7735_FRMCTR1 0xB1
    #define ST7735_FRMCTR2 0xB2
    #define ST7735_FRMCTR3 0xB3
    #define ST7735_INVCTR 0xB4
    #define ST7735_DISSET5 0xB6
    
    #define ST7735_PWCTR1 0xC0
    #define ST7735_PWCTR2 0xC1
    #define ST7735_PWCTR3 0xC2
    #define ST7735_PWCTR4 0xC3
    #define ST7735_PWCTR5 0xC4
    #define ST7735_VMCTR1 0xC5
    
    #define ST7735_RDID1 0xDA
    #define ST7735_RDID2 0xDB
    #define ST7735_RDID3 0xDC
    #define ST7735_RDID4 0xDD
    
    #define ST7735_PWCTR6 0xFC
    
    #define ST7735_GMCTRP1 0xE0
    #define ST7735_GMCTRN1 0xE1
    
    // Color definitions
    #define	ST7735_BLACK 0x0000
    #define	ST7735_BLUE 0x001F
    #define	ST7735_RED 0xF800
    #define	ST7735_GREEN 0x07E0
    #define ST7735_CYAN 0x07FF
    #define ST7735_MAGENTA 0xF81F
    #define ST7735_YELLOW 0xFFE0
    #define ST7735_WHITE 0xFFFF
    #define ST7735_COLOR565(r, g, b) (((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((b & 0xF8) >> 3))
    
    typedef enum {
    	GAMMA_10 = 0x01,
    	GAMMA_25 = 0x02,
    	GAMMA_22 = 0x04,
    	GAMMA_18 = 0x08
    } GammaDef;
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    // call before initializing any SPI devices
    void ST7735_Unselect();
    
    void ST7735_Init(void);
    void ST7735_DrawPixel(uint16_t x, uint16_t y, uint16_t color);
    void ST7735_WriteString(uint16_t x, uint16_t y, const char* str, FontDef font, uint16_t color, uint16_t bgcolor);
    void ST7735_FillRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color);
    void ST7735_FillRectangleFast(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color);
    void ST7735_FillScreen(uint16_t color);
    void ST7735_FillScreenFast(uint16_t color);
    void ST7735_DrawImage(uint16_t x, uint16_t y, uint16_t w, uint16_t h, const uint16_t* data);
    void ST7735_InvertColors(bool invert);
    void ST7735_SetGamma(GammaDef gamma);
    
    #ifdef __cplusplus
    }
    #endif
    
    #endif // __ST7735_H__

     

     

     

    hamoAuthor
    Visitor II
    February 13, 2024

    The source file:

    #include "stm32f1xx_hal.h"
    #include "st7735.h"
    //#include "malloc.h"
    #include "string.h"
    
    #define DELAY 0x80
    
    // based on Adafruit ST7735 library for Arduino
    static const uint8_t
     init_cmds1[] = { // Init for 7735R, part 1 (red or green tab)
     15, // 15 commands in list:
     ST7735_SWRESET, DELAY, // 1: Software reset, 0 args, w/delay
     150, // 150 ms delay
     ST7735_SLPOUT , DELAY, // 2: Out of sleep mode, 0 args, w/delay
     255, // 500 ms delay
     ST7735_FRMCTR1, 3 , // 3: Frame rate ctrl - normal mode, 3 args:
     0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
     ST7735_FRMCTR2, 3 , // 4: Frame rate control - idle mode, 3 args:
     0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
     ST7735_FRMCTR3, 6 , // 5: Frame rate ctrl - partial mode, 6 args:
     0x01, 0x2C, 0x2D, // Dot inversion mode
     0x01, 0x2C, 0x2D, // Line inversion mode
     ST7735_INVCTR , 1 , // 6: Display inversion ctrl, 1 arg, no delay:
     0x07, // No inversion
     ST7735_PWCTR1 , 3 , // 7: Power control, 3 args, no delay:
     0xA2,
     0x02, // -4.6V
     0x84, // AUTO mode
     ST7735_PWCTR2 , 1 , // 8: Power control, 1 arg, no delay:
     0xC5, // VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD
     ST7735_PWCTR3 , 2 , // 9: Power control, 2 args, no delay:
     0x0A, // Opamp current small
     0x00, // Boost frequency
     ST7735_PWCTR4 , 2 , // 10: Power control, 2 args, no delay:
     0x8A, // BCLK/2, Opamp current small & Medium low
     0x2A, 
     ST7735_PWCTR5 , 2 , // 11: Power control, 2 args, no delay:
     0x8A, 0xEE,
     ST7735_VMCTR1 , 1 , // 12: Power control, 1 arg, no delay:
     0x0E,
     ST7735_INVOFF , 0 , // 13: Don't invert display, no args, no delay
     ST7735_MADCTL , 1 , // 14: Memory access control (directions), 1 arg:
     ST7735_ROTATION, // row addr/col addr, bottom to top refresh
     ST7735_COLMOD , 1 , // 15: set color mode, 1 arg, no delay:
     0x05 }, // 16-bit color
    
    #if (defined(ST7735_IS_128X128) || defined(ST7735_IS_160X128))
     init_cmds2[] = { // Init for 7735R, part 2 (1.44" display)
     2, // 2 commands in list:
     ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay:
     0x00, 0x00, // XSTART = 0
     0x00, 0x7F, // XEND = 127
     ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay:
     0x00, 0x00, // XSTART = 0
     0x00, 0x7F }, // XEND = 127
    #endif // ST7735_IS_128X128
    
    #ifdef ST7735_IS_160X80
     init_cmds2[] = { // Init for 7735S, part 2 (160x80 display)
     3, // 3 commands in list:
     ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay:
     0x00, 0x00, // XSTART = 0
     0x00, 0x4F, // XEND = 79
     ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay:
     0x00, 0x00, // XSTART = 0
     0x00, 0x9F , // XEND = 159
     ST7735_INVON, 0 }, // 3: Invert colors
    #endif
    
     init_cmds3[] = { // Init for 7735R, part 3 (red or green tab)
     4, // 4 commands in list:
     ST7735_GMCTRP1, 16 , // 1: Gamma Adjustments (pos. polarity), 16 args, no delay:
     0x02, 0x1c, 0x07, 0x12,
     0x37, 0x32, 0x29, 0x2d,
     0x29, 0x25, 0x2B, 0x39,
     0x00, 0x01, 0x03, 0x10,
     ST7735_GMCTRN1, 16 , // 2: Gamma Adjustments (neg. polarity), 16 args, no delay:
     0x03, 0x1d, 0x07, 0x06,
     0x2E, 0x2C, 0x29, 0x2D,
     0x2E, 0x2E, 0x37, 0x3F,
     0x00, 0x00, 0x02, 0x10,
     ST7735_NORON , DELAY, // 3: Normal display on, no args, w/delay
     10, // 10 ms delay
     ST7735_DISPON , DELAY, // 4: Main screen turn on, no args w/delay
     100 }; // 100 ms delay
    
    static void ST7735_Select() {
     HAL_GPIO_WritePin(ST7735_CS_GPIO_Port, ST7735_CS_Pin, GPIO_PIN_RESET);
    }
    
    void ST7735_Unselect() {
     HAL_GPIO_WritePin(ST7735_CS_GPIO_Port, ST7735_CS_Pin, GPIO_PIN_SET);
    }
    
    static void ST7735_Reset() {
     HAL_GPIO_WritePin(ST7735_RES_GPIO_Port, ST7735_RES_Pin, GPIO_PIN_RESET);
     HAL_Delay(5);
     HAL_GPIO_WritePin(ST7735_RES_GPIO_Port, ST7735_RES_Pin, GPIO_PIN_SET);
    }
    
    static void ST7735_WriteCommand(uint8_t cmd) {
     HAL_GPIO_WritePin(ST7735_DC_GPIO_Port, ST7735_DC_Pin, GPIO_PIN_RESET);
     HAL_SPI_Transmit(&ST7735_SPI_PORT, &cmd, sizeof(cmd), HAL_MAX_DELAY);
    }
    
    static void ST7735_WriteData(uint8_t* buff, size_t buff_size) {
     HAL_GPIO_WritePin(ST7735_DC_GPIO_Port, ST7735_DC_Pin, GPIO_PIN_SET);
     HAL_SPI_Transmit(&ST7735_SPI_PORT, buff, buff_size, HAL_MAX_DELAY);
    }
    
    static void ST7735_ExecuteCommandList(const uint8_t *addr) {
     uint8_t numCommands, numArgs;
     uint16_t ms;
    
     numCommands = *addr++;
     while(numCommands--) {
     uint8_t cmd = *addr++;
     ST7735_WriteCommand(cmd);
    
     numArgs = *addr++;
     // If high bit set, delay follows args
     ms = numArgs & DELAY;
     numArgs &= ~DELAY;
     if(numArgs) {
     ST7735_WriteData((uint8_t*)addr, numArgs);
     addr += numArgs;
     }
    
     if(ms) {
     ms = *addr++;
     if(ms == 255) ms = 500;
     HAL_Delay(ms);
     }
     }
    }
    
    static void ST7735_SetAddressWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) {
     // column address set
     ST7735_WriteCommand(ST7735_CASET);
     uint8_t data[] = { 0x00, x0 + ST7735_XSTART, 0x00, x1 + ST7735_XSTART };
     ST7735_WriteData(data, sizeof(data));
    
     // row address set
     ST7735_WriteCommand(ST7735_RASET);
     data[1] = y0 + ST7735_YSTART;
     data[3] = y1 + ST7735_YSTART;
     ST7735_WriteData(data, sizeof(data));
    
     // write to RAM
     ST7735_WriteCommand(ST7735_RAMWR);
    }
    
    void ST7735_Init() {
     ST7735_Select();
     HAL_Delay(100);
     ST7735_Reset();
     HAL_Delay(100);
     ST7735_ExecuteCommandList(init_cmds1);
     HAL_Delay(100);
     ST7735_ExecuteCommandList(init_cmds2);
     HAL_Delay(100);
     ST7735_ExecuteCommandList(init_cmds3);
     HAL_Delay(100);
     ST7735_Unselect();
    }
    
    void ST7735_DrawPixel(uint16_t x, uint16_t y, uint16_t color) {
     if((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT))
     return;
    
     ST7735_Select();
    
     ST7735_SetAddressWindow(x, y, x+1, y+1);
     uint8_t data[] = { color >> 8, color & 0xFF };
     ST7735_WriteData(data, sizeof(data));
    
     ST7735_Unselect();
    }
    
    static void ST7735_WriteChar(uint16_t x, uint16_t y, char ch, FontDef font, uint16_t color, uint16_t bgcolor) {
     uint32_t i, b, j;
    
     ST7735_SetAddressWindow(x, y, x+font.width-1, y+font.height-1);
    
     for(i = 0; i < font.height; i++) {
     b = font.data[(ch - 32) * font.height + i];
     for(j = 0; j < font.width; j++) {
     if((b << j) & 0x8000) {
     uint8_t data[] = { color >> 8, color & 0xFF };
     ST7735_WriteData(data, sizeof(data));
     } else {
     uint8_t data[] = { bgcolor >> 8, bgcolor & 0xFF };
     ST7735_WriteData(data, sizeof(data));
     }
     }
     }
    }
    
    /*
    Simpler (and probably slower) implementation:
    
    static void ST7735_WriteChar(uint16_t x, uint16_t y, char ch, FontDef font, uint16_t color) {
     uint32_t i, b, j;
    
     for(i = 0; i < font.height; i++) {
     b = font.data[(ch - 32) * font.height + i];
     for(j = 0; j < font.width; j++) {
     if((b << j) & 0x8000) {
     ST7735_DrawPixel(x + j, y + i, color);
     } 
     }
     }
    }
    */
    
    void ST7735_WriteString(uint16_t x, uint16_t y, const char* str, FontDef font, uint16_t color, uint16_t bgcolor) {
     ST7735_Select();
    
     while(*str) {
     if(x + font.width >= ST7735_WIDTH) {
     x = 0;
     y += font.height;
     if(y + font.height >= ST7735_HEIGHT) {
     break;
     }
    
     if(*str == ' ') {
     // skip spaces in the beginning of the new line
     str++;
     continue;
     }
     }
    
     ST7735_WriteChar(x, y, *str, font, color, bgcolor);
     x += font.width;
     str++;
     }
    
     ST7735_Unselect();
    }
    
    void ST7735_FillRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color) {
     // clipping
     if((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) return;
     if((x + w - 1) >= ST7735_WIDTH) w = ST7735_WIDTH - x;
     if((y + h - 1) >= ST7735_HEIGHT) h = ST7735_HEIGHT - y;
    
     ST7735_Select();
     ST7735_SetAddressWindow(x, y, x+w-1, y+h-1);
    
     uint8_t data[] = { color >> 8, color & 0xFF };
     HAL_GPIO_WritePin(ST7735_DC_GPIO_Port, ST7735_DC_Pin, GPIO_PIN_SET);
     for(y = h; y > 0; y--) {
     for(x = w; x > 0; x--) {
     HAL_SPI_Transmit(&ST7735_SPI_PORT, data, sizeof(data), HAL_MAX_DELAY);
     }
     }
    
     ST7735_Unselect();
    }
    
    //void ST7735_FillRectangleFast(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color) {
    // // clipping
    // if((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) return;
    // if((x + w - 1) >= ST7735_WIDTH) w = ST7735_WIDTH - x;
    // if((y + h - 1) >= ST7735_HEIGHT) h = ST7735_HEIGHT - y;
    
    // ST7735_Select();
    // ST7735_SetAddressWindow(x, y, x+w-1, y+h-1);
    
    // // Prepare whole line in a single buffer
    // uint8_t pixel[] = { color >> 8, color & 0xFF };
    // uint8_t *line = malloc(w * sizeof(pixel));
    // for(x = 0; x < w; ++x)
    // 	memcpy(line + x * sizeof(pixel), pixel, sizeof(pixel));
    
    // HAL_GPIO_WritePin(ST7735_DC_GPIO_Port, ST7735_DC_Pin, GPIO_PIN_SET);
    // for(y = h; y > 0; y--)
    // HAL_SPI_Transmit(&ST7735_SPI_PORT, line, w * sizeof(pixel), HAL_MAX_DELAY);
    
    // free(line);
    // ST7735_Unselect();
    //}
    
    void ST7735_FillScreen(uint16_t color) {
     ST7735_FillRectangle(0, 0, ST7735_WIDTH, ST7735_HEIGHT, color);
    }
    
    //void ST7735_FillScreenFast(uint16_t color) {
    // ST7735_FillRectangleFast(0, 0, ST7735_WIDTH, ST7735_HEIGHT, color);
    //}
    
    void ST7735_DrawImage(uint16_t x, uint16_t y, uint16_t w, uint16_t h, const uint16_t* data) {
     if((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) return;
     if((x + w - 1) >= ST7735_WIDTH) return;
     if((y + h - 1) >= ST7735_HEIGHT) return;
    
     ST7735_Select();
     ST7735_SetAddressWindow(x, y, x+w-1, y+h-1);
     ST7735_WriteData((uint8_t*)data, sizeof(uint16_t)*w*h);
     ST7735_Unselect();
    }
    
    void ST7735_InvertColors(bool invert) {
     ST7735_Select();
     ST7735_WriteCommand(invert ? ST7735_INVON : ST7735_INVOFF);
     ST7735_Unselect();
    }
    
    //void ST7735_SetGamma(uint8_t gamma)
    //{
    //	ST7735_Select();
    //	ST7735_WriteCommand(ST7735_GAMSET);
    //	ST7735_WriteData(&gamma, sizeof(gamma));
    //	ST7735_Unselect();
    //}

    The library is using HAL_SPI_Transmit().

    hamoAuthor
    Visitor II
    February 13, 2024

    @AScha.3 

    can you share your code?

    Super User
    February 13, 2024

    >can you share your code?

    Yes - but will not help you. Its for H743 cpu.

    You have to understand: howto get it working for you - and your actual cpu.

    The "driver" has 3 parts:

    - some graphic routines , to draw circles etc.

    - basic graphic : mainly "drew pixel" and "draw rectangle" -- to put anything in color to the TFT

    - hardware access : what/how to write to the tft-chip

    --------------------------

    what you have to do : modify/adapt the hardware access, to what you have !

    Then it should work...

     

    hamoAuthor
    Visitor II
    February 13, 2024

    The clock pin isn't toggling.

    Not only that, the CS pin ist changing his state too, neither do MOSI so i think it may be a setting problem.

    I have changed the bluepill ( tried 3 bluepills).

    Super User
    February 13, 2024

    Come on - CS  You have to set . in software. no NSS . not working on F103 !!!!

    Read the rm. 

    If you dont know, how to do ... look on git :

    https://github.com/search?q=st7735+stm32f103&type=repositories

     

     

    hamoAuthor
    Visitor II
    February 15, 2024

    It stucks in the initilizing function of the ST7735.

    I saw an example in the link you provided and is the same by me.

    CS is set:


     

     ST7735_Unselect();
    		HAL_Delay(100);
     ST7735_Select();
     HAL_Delay(100);
     ST7735_Reset();
     HAL_Delay(100);
     ST7735_ExecuteCommandList(init_cmds1);​

     

    Where:

     

    static void ST7735_Select() {
     HAL_GPIO_WritePin(ST7735_CS_GPIO_Port, ST7735_CS_Pin, GPIO_PIN_RESET);
    }
    
    void ST7735_Unselect() {
     HAL_GPIO_WritePin(ST7735_CS_GPIO_Port, ST7735_CS_Pin, GPIO_PIN_SET);
    }
    
    static void ST7735_Reset() {
     HAL_GPIO_WritePin(ST7735_RES_GPIO_Port, ST7735_RES_Pin, GPIO_PIN_SET);
     HAL_Delay(5);
     HAL_GPIO_WritePin(ST7735_RES_GPIO_Port, ST7735_RES_Pin, GPIO_PIN_RESET);
     HAL_Delay(5);
     HAL_GPIO_WritePin(ST7735_RES_GPIO_Port, ST7735_RES_Pin, GPIO_PIN_SET);
    }

     

    So CS is set before sending command or data

     

     

    Super User
    February 15, 2024

    Thats wrong.

    Try:

     
     ST7735_Reset();
     HAL_Delay(150);
     ST7735_Select();
     ST7735_ExecuteCommandList(init_cmds1);

    1. reset

    2. delay....(chip needs time to start, i have 150ms) 

    3. cs low + write init sequence...

    hamoAuthor
    Visitor II
    February 15, 2024

    I have tried that, no.

    Idk why but there is no clock and without clock it will not work.

    Super User
    February 15, 2024

    Where  is no clock ?

    hamoAuthor
    Visitor II
    February 15, 2024

    After changing the code and uploading it on the bluepill, i use logic analyzer to see if it works.
    There is no clock on the pin PB13 -> not toggling at all.

    Super User
    February 15, 2024

    Cannot be ... :flushed_face:

    Show what you set in Cube to SPI2 .  we looked already.

    Try debug - on  HAL_SPI_Transmit(...) : what it doing ???

    hamoAuthor
    Visitor II
    February 15, 2024

    I ahve done it.
    after initialiying it tries to send the data and waits for the TXE flag to be set so it can send data.

    But it keeps wait then goes to timeout.

    With the help of logic analzyer i see there is no clock.

    Super User
    February 15, 2024

    ok - just : you use cubeIDE - right ? nothing special doing .... ?

    Because i set it in CubeMX, then generate code, then write to spi (HAL_... ) -- and it works.

    So you must do something different...but i have no idea, what it could be.

    ed 

    maybe i seen..

    you set : half duplex master . 

    try: 

     

    AScha3_0-1708027359333.png

     

    Super User
    February 15, 2024

    Read out and check/post content of SPI and relevant GPIO/AFIO registers.

    JW

    hamoAuthor
    Visitor II
    February 16, 2024

    Ok, and how?

    Super User
    February 17, 2024
    >> Read out and check/post content of SPI and relevant GPIO/AFIO registers.

    > Ok, and how?

    Read out in debugger, in the same way as you've did it with RCC registers (there should also be some "peripheral view" window in your IDE somewhere, I don't use IDEs). Do that just before the moment you intend to start to transmit (i.e. place a breakpoint there).

    (Using debugger is the most convenient way, even if there are some caveats. You could also read them out in program and output them in any way e.g. using UART).

    Check by comparing registers content to the expected content, referring to respective chapters in Reference Manual.

    JW