[M33] STM32C542RCT6 开发板测试

[复制链接]
13|0
LDXSJiaojunh 发表于 2026-12-2 19:43 | 显示全部楼层 |阅读模式
由于工作原因,板载已经收到很长时间了,一直没有做测试,最近有闲时,发一个我的测试状态。本次测试的项目为SPI 80*160 tft显示屏一块,EC16编码器一块带按键,can调试工具一只,CAN调试软件使用我自己编写的, STM32C542RCT6 NUCLEO 开发板一块,

测试视频:

测试功能:

1、测试CANFD,因为经常接触到CAN通讯的环境,对这块新的开发板测试是我最想做的一件事
    在调试CAN的过程中出现了一个让人非常遗憾的使,板载的MC2564是坏了, 屏幕截图 2026-12-02 183554.png 就是这个芯片,测量都正常,就是在不通讯,使用逻辑分析仪 对“1”,“4”脚测量是发现有数据发送出来 ,没有接收到的数据,反过来在分析“6”,“7”脚时没有任何数据, 但CAN_H,L电压分别2.9,2.1,最后没办法了只能更换芯片,替换A1042/3后完美运行,这里还有一点说明,开发板上面的CAN接口实际上接的是UART4,某种意义并不是mcu实际的CAN线,感兴趣的可以多研究下 屏幕截图 2026-12-02 184649.png 。下面说一下在高速通讯下的状态,我的极限测试下5ms的速度下测试没有丢包,非常理想。 屏幕截图 2026-12-02 185050.png


2、测试高速IO,这里我使用了EC16的编码器带按键的,显示做了编码器的计数显示,实际转速,测试结果非常完美,手指旋转峰值速度达到170转不丢中间计数值,足以说明内核的计算能力大幅提升。 屏幕截图 2026-12-02 192036.png 因使用手机录像没有抓取到最高转速。
3、这套显示跑了LVGL套件,流畅度完美,不卡顿,与H750的运行速度相当。

贴一点代码,显示驱动,其他的就补贴了,个人水平有限,仅供参考
  1. /*
  2. * bsp_display.c - ST7735S 80x160 驱动
  3. *
  4. * 采用 4 线 SPI(SCK/MOSI/CS/DC + RST/BL),RGB565,像素大端发送。
  5. * SPI 句柄来自 CubeMX2 生成的 mx_spi2_gethandle(),引脚来自 board_config.h。
  6. *
  7. * 初始化命令序列来自一块实测可点亮的 0.96" 80x160 IPS(ST7735S);
  8. * 若你的模组是 ST7735R 或偏移不同,只需改 board_config.h 的 OFFSET/INVERSION。
  9. */

  10. #include "bsp_display.h"

  11. #include <string.h>

  12. #include "stm32c5xx_hal.h"
  13. #include "stm32c5xx_hal_gpio.h"
  14. #include "stm32c5xx_hal_spi.h"

  15. /* -------------------------------------------------------------------------- */
  16. /* 面板命令                                                                   */
  17. /* -------------------------------------------------------------------------- */

  18. #define ST_SWRESET      0x01u
  19. #define ST_SLPOUT       0x11u
  20. #define ST_NORON        0x13u
  21. #define ST_INVOFF       0x20u
  22. #define ST_INVON        0x21u
  23. #define ST_DISPOFF      0x28u
  24. #define ST_DISPON       0x29u
  25. #define ST_CASET        0x2Au
  26. #define ST_RASET        0x2Bu
  27. #define ST_RAMWR        0x2Cu
  28. #define ST_MADCTL       0x36u
  29. #define ST_COLMOD       0x3Au

  30. /* -------------------------------------------------------------------------- */
  31. /* 内部状态                                                                   */
  32. /* -------------------------------------------------------------------------- */

  33. static hal_spi_handle_t *s_spi;
  34. static uint16_t s_w = 80u;
  35. static uint16_t s_h = 160u;
  36. static uint16_t s_x_off = BSP_LCD_X_OFFSET;
  37. static uint16_t s_y_off = BSP_LCD_Y_OFFSET;
  38. static uint8_t  s_rotation = BSP_LCD_ROTATION;
  39. static uint8_t  s_tx_buf[BSP_LCD_CHUNK_PIXELS * 2u];

  40. static volatile uint32_t s_spi_ok;
  41. static volatile uint32_t s_spi_fail;
  42. static volatile uint32_t s_bytes;

  43. /* -------------------------------------------------------------------------- */
  44. /* 引脚操作(HAL2 GPIO)                                                       */
  45. /* -------------------------------------------------------------------------- */

  46. #define LCD_CS_LOW()    HAL_GPIO_WritePin(BSP_LCD_CS_PORT,  BSP_LCD_CS_PIN,  HAL_GPIO_PIN_RESET)
  47. #define LCD_CS_HIGH()   HAL_GPIO_WritePin(BSP_LCD_CS_PORT,  BSP_LCD_CS_PIN,  HAL_GPIO_PIN_SET)
  48. #define LCD_DC_LOW()    HAL_GPIO_WritePin(BSP_LCD_DC_PORT,  BSP_LCD_DC_PIN,  HAL_GPIO_PIN_RESET)
  49. #define LCD_DC_HIGH()   HAL_GPIO_WritePin(BSP_LCD_DC_PORT,  BSP_LCD_DC_PIN,  HAL_GPIO_PIN_SET)
  50. #define LCD_RST_LOW()   HAL_GPIO_WritePin(BSP_LCD_RST_PORT, BSP_LCD_RST_PIN, HAL_GPIO_PIN_RESET)
  51. #define LCD_RST_HIGH()  HAL_GPIO_WritePin(BSP_LCD_RST_PORT, BSP_LCD_RST_PIN, HAL_GPIO_PIN_SET)
  52. #define LCD_BL_LOW()    HAL_GPIO_WritePin(BSP_LCD_BL_PORT,  BSP_LCD_BL_PIN,  HAL_GPIO_PIN_RESET)
  53. #define LCD_BL_HIGH()   HAL_GPIO_WritePin(BSP_LCD_BL_PORT,  BSP_LCD_BL_PIN,  HAL_GPIO_PIN_SET)

  54. /* -------------------------------------------------------------------------- */
  55. /* 底层 SPI                                                                   */
  56. /* -------------------------------------------------------------------------- */

  57. static bool lcd_spi_tx(const uint8_t *buf, uint32_t len)
  58. {
  59.     if ((s_spi == NULL) || (len == 0u))
  60.     {
  61.         return false;
  62.     }

  63.     /* HAL2 的 count_packet 对 8 bit 数据宽度就是字节数 */
  64.     if (HAL_SPI_Transmit(s_spi, buf, len, 100u) == HAL_OK)
  65.     {
  66.         s_spi_ok++;
  67.         s_bytes += len;
  68.         return true;
  69.     }

  70.     s_spi_fail++;
  71.     return false;
  72. }

  73. static bool lcd_spi_byte(uint8_t b)
  74. {
  75.     return lcd_spi_tx(&b, 1u);
  76. }

  77. static void lcd_cmd_data(uint8_t cmd, const uint8_t *data, uint16_t len)
  78. {
  79.     LCD_CS_LOW();
  80.     LCD_DC_LOW();
  81.     (void)lcd_spi_byte(cmd);

  82.     if ((len != 0u) && (data != NULL))
  83.     {
  84.         LCD_DC_HIGH();
  85.         (void)lcd_spi_tx(data, len);
  86.     }
  87.     LCD_CS_HIGH();
  88. }

  89. static void lcd_cmd(uint8_t cmd)
  90. {
  91.     lcd_cmd_data(cmd, NULL, 0u);
  92. }

  93. /*
  94. * 连续命令+数据:调用者负责 CS 已经拉低,函数内不再动 CS。
  95. * ST7735S 要求 CASET/RASET/RAMWR 到像素数据全程 CS 保持低,
  96. * 否则 RAMWR 会被 CS 上升沿提前结束,后面的像素被当成新命令 -> 雪花。
  97. */
  98. static void lcd_cmd_data_cont(uint8_t cmd, const uint8_t *data, uint16_t len)
  99. {
  100.     LCD_DC_LOW();
  101.     (void)lcd_spi_byte(cmd);

  102.     if ((len != 0u) && (data != NULL))
  103.     {
  104.         LCD_DC_HIGH();
  105.         (void)lcd_spi_tx(data, len);
  106.     }
  107. }

  108. static void lcd_window(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  109. {
  110.     uint8_t buf[4];
  111.     uint16_t x0 = (uint16_t)(x + s_x_off);
  112.     uint16_t x1 = (uint16_t)(x + w - 1u + s_x_off);
  113.     uint16_t y0 = (uint16_t)(y + s_y_off);
  114.     uint16_t y1 = (uint16_t)(y + h - 1u + s_y_off);

  115.     LCD_CS_LOW();

  116.     buf[0] = (uint8_t)(x0 >> 8);
  117.     buf[1] = (uint8_t)(x0 & 0xFFu);
  118.     buf[2] = (uint8_t)(x1 >> 8);
  119.     buf[3] = (uint8_t)(x1 & 0xFFu);
  120.     lcd_cmd_data_cont(ST_CASET, buf, 4u);

  121.     buf[0] = (uint8_t)(y0 >> 8);
  122.     buf[1] = (uint8_t)(y0 & 0xFFu);
  123.     buf[2] = (uint8_t)(y1 >> 8);
  124.     buf[3] = (uint8_t)(y1 & 0xFFu);
  125.     lcd_cmd_data_cont(ST_RASET, buf, 4u);

  126.     LCD_DC_LOW();
  127.     (void)lcd_spi_byte(ST_RAMWR);
  128.     LCD_DC_HIGH();
  129.     /* CS 保持低,紧接着由 lcd_stream_xxx() 发像素 */
  130. }

  131. static void lcd_stream_color(uint16_t color, uint32_t n)
  132. {
  133.     uint32_t i;
  134.     uint32_t chunk;
  135.     uint8_t hi = (uint8_t)(color >> 8);
  136.     uint8_t lo = (uint8_t)(color & 0xFFu);

  137.     for (i = 0u; i < BSP_LCD_CHUNK_PIXELS; i++)
  138.     {
  139.         s_tx_buf[2u * i] = hi;
  140.         s_tx_buf[2u * i + 1u] = lo;
  141.     }

  142.     while (n != 0u)
  143.     {
  144.         chunk = (n > BSP_LCD_CHUNK_PIXELS) ? BSP_LCD_CHUNK_PIXELS : n;
  145.         (void)lcd_spi_tx(s_tx_buf, chunk * 2u);
  146.         n -= chunk;
  147.     }
  148. }

  149. static void lcd_stream_pixels(const uint16_t *pixels, uint32_t n)
  150. {
  151.     uint32_t i;
  152.     uint32_t chunk;

  153.     while (n != 0u)
  154.     {
  155.         chunk = (n > BSP_LCD_CHUNK_PIXELS) ? BSP_LCD_CHUNK_PIXELS : n;
  156.         for (i = 0u; i < chunk; i++)
  157.         {
  158.             s_tx_buf[2u * i] = (uint8_t)(pixels[i] >> 8);
  159.             s_tx_buf[2u * i + 1u] = (uint8_t)(pixels[i] & 0xFFu);
  160.         }
  161.         (void)lcd_spi_tx(s_tx_buf, chunk * 2u);
  162.         pixels += chunk;
  163.         n -= chunk;
  164.     }
  165. }

  166. /* -------------------------------------------------------------------------- */
  167. /* 初始化                                                                     */
  168. /* -------------------------------------------------------------------------- */

  169. struct lcd_init_cmd
  170. {
  171.     uint8_t  cmd;
  172.     uint8_t  len;
  173.     uint8_t  data[16];
  174.     uint16_t delay_ms;
  175. };

  176. static const struct lcd_init_cmd s_init_cmds[] =
  177. {
  178.     {ST_SLPOUT, 0u, {0}, 120u},
  179.     {0xB1u, 3u, {0x05, 0x3C, 0x3Cu}, 0u},                          /* 帧率 */
  180.     {0xB2u, 3u, {0x05, 0x3C, 0x3Cu}, 0u},
  181.     {0xB3u, 6u, {0x05, 0x3C, 0x3Cu, 0x05, 0x3C, 0x3Cu}, 0u},
  182.     {0xB4u, 1u, {0x03u}, 0u},                                      /* 点反转 */
  183.     {0xC0u, 3u, {0xA2u, 0x02u, 0x84u}, 0u},                        /* 电源 */
  184.     {0xC1u, 1u, {0xC5u}, 0u},
  185.     {0xC2u, 2u, {0x0Au, 0x00u}, 0u},
  186.     {0xC3u, 2u, {0x8Au, 0x2Au}, 0u},
  187.     {0xC4u, 2u, {0x8Au, 0xEEu}, 0u},
  188.     {0xC5u, 1u, {0x0Eu}, 0u},                                      /* VCOM */
  189.     {ST_COLMOD, 1u, {0x05u}, 0u},                                  /* RGB565 */
  190.     {0xE0u, 16u, {0x02u, 0x1Cu, 0x07u, 0x12u, 0x37u, 0x32u, 0x29u, 0x2Du,
  191.                   0x29u, 0x25u, 0x2Bu, 0x39u, 0x00u, 0x01u, 0x03u, 0x10u}, 0u},
  192.     {0xE1u, 16u, {0x03u, 0x1Du, 0x07u, 0x06u, 0x2Eu, 0x2Cu, 0x29u, 0x2Du,
  193.                   0x2Eu, 0x2Eu, 0x37u, 0x3Fu, 0x00u, 0x00u, 0x02u, 0x10u}, 0u},
  194. };

  195. static const uint8_t s_madctl[4] =
  196. {
  197.     0xC8u,   /* 0 竖屏      MY|MX|BGR */
  198.     0xA8u,   /* 1 横屏      MY|MV|BGR */
  199.     0x08u,   /* 2 竖屏翻转  BGR        */
  200.     0x68u,   /* 3 横屏翻转  MX|MV|BGR  */
  201. };

  202. static void lcd_apply_rotation(void)
  203. {
  204.     uint8_t madctl = s_madctl[s_rotation & 0x03u];

  205.     lcd_cmd_data(ST_MADCTL, &madctl, 1u);

  206.     if ((s_rotation & 0x01u) != 0u)
  207.     {
  208.         s_w = 160u;
  209.         s_h = 80u;
  210.         s_x_off = BSP_LCD_Y_OFFSET;
  211.         s_y_off = BSP_LCD_X_OFFSET;
  212.     }
  213.     else
  214.     {
  215.         s_w = 80u;
  216.         s_h = 160u;
  217.         s_x_off = BSP_LCD_X_OFFSET;
  218.         s_y_off = BSP_LCD_Y_OFFSET;
  219.     }
  220. }

  221. bool bsp_display_init(void)
  222. {
  223.     uint32_t i;

  224.     s_spi = BSP_LCD_SPI_HANDLE();
  225.     if (s_spi == NULL)
  226.     {
  227.         return false;
  228.     }

  229.     /* 先把控制脚拉到安全电平(引脚复用已在 CubeMX2 里配好) */
  230.     LCD_CS_HIGH();
  231.     LCD_DC_HIGH();

  232. #if BSP_LCD_USE_RESET
  233.     LCD_RST_HIGH();
  234.     HAL_Delay(10u);
  235.     LCD_RST_LOW();
  236.     HAL_Delay(20u);
  237.     LCD_RST_HIGH();
  238.     HAL_Delay(120u);
  239. #endif

  240.     lcd_cmd(ST_SWRESET);
  241.     HAL_Delay(120u);

  242.     for (i = 0u; i < (sizeof(s_init_cmds) / sizeof(s_init_cmds[0])); i++)
  243.     {
  244.         const struct lcd_init_cmd *c = &s_init_cmds[i];

  245.         lcd_cmd_data(c->cmd, (c->len != 0u) ? c->data : NULL, c->len);
  246.         if (c->delay_ms != 0u)
  247.         {
  248.             HAL_Delay(c->delay_ms);
  249.         }
  250.     }

  251.     lcd_apply_rotation();

  252. #if BSP_LCD_INVERSION
  253.     lcd_cmd(ST_INVON);
  254. #else
  255.     lcd_cmd(ST_INVOFF);
  256. #endif

  257.     lcd_cmd(ST_NORON);
  258.     HAL_Delay(10u);
  259.     lcd_cmd(ST_DISPON);
  260.     HAL_Delay(20u);

  261.     bsp_display_clear(0x0000u);
  262.     bsp_display_backlight(true);

  263.     return true;
  264. }

  265. /* -------------------------------------------------------------------------- */
  266. /* 绘图                                                                       */
  267. /* -------------------------------------------------------------------------- */

  268. void bsp_display_fill(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color)
  269. {
  270.     if ((w == 0u) || (h == 0u) || (x >= s_w) || (y >= s_h))
  271.     {
  272.         return;
  273.     }
  274.     if ((uint32_t)x + w > s_w)
  275.     {
  276.         w = (uint16_t)(s_w - x);
  277.     }
  278.     if ((uint32_t)y + h > s_h)
  279.     {
  280.         h = (uint16_t)(s_h - y);
  281.     }

  282.     lcd_window(x, y, w, h);
  283.     lcd_stream_color(color, (uint32_t)w * h);
  284.     LCD_CS_HIGH();
  285. }

  286. void bsp_display_blit(uint16_t x, uint16_t y, uint16_t w, uint16_t h, const uint16_t *pixels)
  287. {
  288.     if ((pixels == NULL) || (w == 0u) || (h == 0u))
  289.     {
  290.         return;
  291.     }
  292.     if (((uint32_t)x + w > s_w) || ((uint32_t)y + h > s_h))
  293.     {
  294.         return;
  295.     }

  296.     lcd_window(x, y, w, h);
  297.     lcd_stream_pixels(pixels, (uint32_t)w * h);
  298.     LCD_CS_HIGH();
  299. }

  300. void bsp_display_clear(uint16_t color)
  301. {
  302.     bsp_display_fill(0u, 0u, s_w, s_h, color);
  303. }

  304. void bsp_display_backlight(bool on)
  305. {
  306. #if BSP_LCD_USE_BL
  307.     if (on)
  308.     {
  309.         LCD_BL_HIGH();
  310.     }
  311.     else
  312.     {
  313.         LCD_BL_LOW();
  314.     }
  315. #else
  316.     (void)on;
  317. #endif
  318. }

  319. uint16_t bsp_display_width(void)
  320. {
  321.     return s_w;
  322. }

  323. uint16_t bsp_display_height(void)
  324. {
  325.     return s_h;
  326. }

  327. void bsp_display_get_stats(bsp_display_stats_t *st)
  328. {
  329.     if (st == NULL)
  330.     {
  331.         return;
  332.     }

  333.     st->spi_ok = s_spi_ok;
  334.     st->spi_fail = s_spi_fail;
  335.     st->bytes = s_bytes;
  336. }

  337. void bsp_display_reset_stats(void)
  338. {
  339.     s_spi_ok = 0u;
  340.     s_spi_fail = 0u;
  341.     s_bytes = 0u;
  342. }


微信图片_20261202132405_14_5.jpg
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

1

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部