当前位置: 首页 > 知识库问答 >
问题:

如何在STM32上使用Eclipse/ gcc / ac6 / sw4stm32的自定义回调来重新定位printf?

融唯
2023-03-14

我正在使用Eclipse/gcc/ac6/sw4stm32对stm32g071rb核进行编程,并且我在使用打印到SPI连接的显示器的自定义回调重新定位printf时遇到了一些困难。

现在,我的main看起来像这样:

void main(void)
{
    /* Declare BOARD typedef. */
    BOARD_TypeDef_t board;

    /* Initialize BOARD typedef. */
    board_init(&board);

    /* Initialize display */
    display_init(&board.display[displayA]);

    /* Writes a "B" to the display */
    display_write(&board.display[displayA], 0x42);
}

我想做的是在重新定位的printf中使用我的display_write功能和显示目标。

我看到的选项有:

>

  • 使用__io_putchar函数:这可以从 syscalls.c 文件中导出。但是,它唯一需要的参数是要打印的字符,所以我不确定,也不认为我可以在那里获得BOARD_TypeDef_t。

    使用写函数:这在syscall. c文件中可用。但是,我宁愿通过__io_putchar以规范的方式使用它,而不是直接编辑写函数

    使用fopencookie:我想这是我喜欢的方式,但我有一些困难。我了解到我必须激活_GNU_SOURCE并包含stdio.h,所以我在main上添加了这个:

    #ifndef _GNU_SOURCE
    #define _GNU_SOURCE
    #include <stdio.h> // Needed for ssize_t, cookie_io_functions_t, etc
    #endif /* _GNU_SOURCE */
    

    但是,我仍然得到一个"未知类型名称'cookie_io_functions_t'"错误,尽管这个结构是在包含的stdio. h中定义的,并且对这个stdio. h中的类型的其他调用被引用得很好,例如,cookie_write_function_t、cookie_seek_function_t等。

    我不太清楚这是阿瑟gcc的问题还是Eclipse的问题,所以进展很慢。

    任何关于我可能做些什么来解决这个问题的建议都是值得赞赏的。

  • 共有2个答案

    索正豪
    2023-03-14

    我在USB CDC VCOM端口上得到了printf retargetted,将__io_putchar添加到main. c-以下代码

    /* USER CODE BEGIN Header */
    /**
      ******************************************************************************
      * @file           : main.c
      * @brief          : Main program body
      ******************************************************************************
      * @attention
      *
      * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
      * All rights reserved.</center></h2>
      *
      * This software component is licensed by ST under BSD 3-Clause license,
      * the "License"; You may not use this file except in compliance with the
      * License. You may obtain a copy of the License at:
      *                        opensource.org/licenses/BSD-3-Clause
      *
      ******************************************************************************
      */
    /* USER CODE END Header */
    
    /* Includes ------------------------------------------------------------------*/
    #include "main.h"
    #include "usb_device.h"
    
    /* Private includes ----------------------------------------------------------*/
    /* USER CODE BEGIN Includes */
    #include "usbd_cdc_if.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);
    static void MX_GPIO_Init(void);
    /* USER CODE BEGIN PFP */
    int __io_putchar(int ch);
    /* 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 */
        setvbuf(stdout, NULL, _IONBF, 0);
      /* 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 B                   EGIN SysInit */
    
      /* USER CODE END SysInit */
    
      /* Initialize all configured peripherals */
      MX_GPIO_Init();
      MX_USB_DEVICE_Init();
      /* USER CODE BEGIN 2 */
      HAL_Delay(5000);
    //  CDC_Transmit_HS("Hello World", strlen("Hello World"));
      printf("Hello World");
      /* USER CODE END 2 */
    
      /* Infinite loop */
      /* USER CODE BEGIN WHILE */
      while (1)
      {
        /* USER CODE END WHILE */
    
        /* USER CODE BEGIN 3 */
      }
      /* USER CODE END 3 */
    }
    
    /**
      * @brief System Clock Configuration
      * @retval None
      */
    void SystemClock_Config(void)
    {
      RCC_OscInitTypeDef RCC_OscInitStruct = {0};
      RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
    
      /** Configure the main internal regulator output voltage 
      */
      __HAL_RCC_PWR_CLK_ENABLE();
      __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
      /** Initializes the CPU, AHB and APB busses clocks 
      */
      RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
      RCC_OscInitStruct.HSEState = RCC_HSE_ON;
      RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
      RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
      RCC_OscInitStruct.PLL.PLLM = 4;
      RCC_OscInitStruct.PLL.PLLN = 120;
      RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
      RCC_OscInitStruct.PLL.PLLQ = 5;
      if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
      {
        Error_Handler();
      }
      /** Initializes the CPU, AHB and APB busses clocks 
      */
      RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                                  |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
      RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
      RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV4;
      RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV8;
      RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
    
      if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
      {
        Error_Handler();
      }
    }
    
    /**
      * @brief GPIO Initialization Function
      * @param None
      * @retval None
      */
    static void MX_GPIO_Init(void)
    {
      GPIO_InitTypeDef GPIO_InitStruct = {0};
    
      /* GPIO Ports Clock Enable */
      __HAL_RCC_GPIOC_CLK_ENABLE();
      __HAL_RCC_GPIOF_CLK_ENABLE();
      __HAL_RCC_GPIOH_CLK_ENABLE();
      __HAL_RCC_GPIOA_CLK_ENABLE();
      __HAL_RCC_GPIOB_CLK_ENABLE();
      __HAL_RCC_GPIOG_CLK_ENABLE();
      __HAL_RCC_GPIOE_CLK_ENABLE();
      __HAL_RCC_GPIOD_CLK_ENABLE();
    
      /*Configure GPIO pin Output Level */
      HAL_GPIO_WritePin(GPIOC, NCS_MEMS_SPI_Pin|CSX_Pin|OTG_FS_PSO_Pin, GPIO_PIN_RESET);
    
      /*Configure GPIO pin Output Level */
      HAL_GPIO_WritePin(ACP_RST_GPIO_Port, ACP_RST_Pin, GPIO_PIN_RESET);
    
      /*Configure GPIO pin Output Level */
      HAL_GPIO_WritePin(GPIOD, RDX_Pin|WRX_DCX_Pin, GPIO_PIN_RESET);
    
      /*Configure GPIO pin Output Level */
      HAL_GPIO_WritePin(GPIOG, LD3_Pin|LD4_Pin, GPIO_PIN_RESET);
    
      /*Configure GPIO pins : A0_Pin A1_Pin A2_Pin A3_Pin 
                               A4_Pin A5_Pin SDNRAS_Pin A6_Pin 
                               A7_Pin A8_Pin A9_Pin */
      GPIO_InitStruct.Pin = A0_Pin|A1_Pin|A2_Pin|A3_Pin 
                              |A4_Pin|A5_Pin|SDNRAS_Pin|A6_Pin 
                              |A7_Pin|A8_Pin|A9_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
      GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
      HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
    
      /*Configure GPIO pins : SPI5_SCK_Pin SPI5_MISO_Pin SPI5_MOSI_Pin */
      GPIO_InitStruct.Pin = SPI5_SCK_Pin|SPI5_MISO_Pin|SPI5_MOSI_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
      GPIO_InitStruct.Alternate = GPIO_AF5_SPI5;
      HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
    
      /*Configure GPIO pin : ENABLE_Pin */
      GPIO_InitStruct.Pin = ENABLE_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
      GPIO_InitStruct.Alternate = GPIO_AF14_LTDC;
      HAL_GPIO_Init(ENABLE_GPIO_Port, &GPIO_InitStruct);
    
      /*Configure GPIO pin : SDNWE_Pin */
      GPIO_InitStruct.Pin = SDNWE_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
      GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
      HAL_GPIO_Init(SDNWE_GPIO_Port, &GPIO_InitStruct);
    
      /*Configure GPIO pins : NCS_MEMS_SPI_Pin CSX_Pin OTG_FS_PSO_Pin */
      GPIO_InitStruct.Pin = NCS_MEMS_SPI_Pin|CSX_Pin|OTG_FS_PSO_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
      HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
    
      /*Configure GPIO pins : B1_Pin MEMS_INT1_Pin MEMS_INT2_Pin TP_INT1_Pin */
      GPIO_InitStruct.Pin = B1_Pin|MEMS_INT1_Pin|MEMS_INT2_Pin|TP_INT1_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    
      /*Configure GPIO pins : B5_Pin VSYNC_Pin G2_Pin R4_Pin 
                               R5_Pin */
      GPIO_InitStruct.Pin = B5_Pin|VSYNC_Pin|G2_Pin|R4_Pin 
                              |R5_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
      GPIO_InitStruct.Alternate = GPIO_AF14_LTDC;
      HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    
      /*Configure GPIO pin : ACP_RST_Pin */
      GPIO_InitStruct.Pin = ACP_RST_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
      HAL_GPIO_Init(ACP_RST_GPIO_Port, &GPIO_InitStruct);
    
      /*Configure GPIO pin : OTG_FS_OC_Pin */
      GPIO_InitStruct.Pin = OTG_FS_OC_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      HAL_GPIO_Init(OTG_FS_OC_GPIO_Port, &GPIO_InitStruct);
    
      /*Configure GPIO pins : R3_Pin R6_Pin */
      GPIO_InitStruct.Pin = R3_Pin|R6_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
      GPIO_InitStruct.Alternate = GPIO_AF9_LTDC;
      HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    
      /*Configure GPIO pin : BOOT1_Pin */
      GPIO_InitStruct.Pin = BOOT1_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      HAL_GPIO_Init(BOOT1_GPIO_Port, &GPIO_InitStruct);
    
      /*Configure GPIO pins : A10_Pin A11_Pin BA0_Pin BA1_Pin 
                               SDCLK_Pin SDNCAS_Pin */
      GPIO_InitStruct.Pin = A10_Pin|A11_Pin|BA0_Pin|BA1_Pin 
                              |SDCLK_Pin|SDNCAS_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
      GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
      HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
    
      /*Configure GPIO pins : D4_Pin D5_Pin D6_Pin D7_Pin 
                               D8_Pin D9_Pin D10_Pin D11_Pin 
                               D12_Pin NBL0_Pin NBL1_Pin */
      GPIO_InitStruct.Pin = D4_Pin|D5_Pin|D6_Pin|D7_Pin 
                              |D8_Pin|D9_Pin|D10_Pin|D11_Pin 
                              |D12_Pin|NBL0_Pin|NBL1_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
      GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
      HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
    
      /*Configure GPIO pins : G4_Pin G5_Pin B6_Pin B7_Pin */
      GPIO_InitStruct.Pin = G4_Pin|G5_Pin|B6_Pin|B7_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
      GPIO_InitStruct.Alternate = GPIO_AF14_LTDC;
      HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    
      /*Configure GPIO pins : D13_Pin D14_Pin D15_Pin D0_Pin 
                               D1_Pin D2_Pin D3_Pin */
      GPIO_InitStruct.Pin = D13_Pin|D14_Pin|D15_Pin|D0_Pin 
                              |D1_Pin|D2_Pin|D3_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
      GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
      HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
    
      /*Configure GPIO pin : TE_Pin */
      GPIO_InitStruct.Pin = TE_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      HAL_GPIO_Init(TE_GPIO_Port, &GPIO_InitStruct);
    
      /*Configure GPIO pins : RDX_Pin WRX_DCX_Pin */
      GPIO_InitStruct.Pin = RDX_Pin|WRX_DCX_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
      HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
    
      /*Configure GPIO pins : R7_Pin DOTCLK_Pin B3_Pin */
      GPIO_InitStruct.Pin = R7_Pin|DOTCLK_Pin|B3_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
      GPIO_InitStruct.Alternate = GPIO_AF14_LTDC;
      HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
    
      /*Configure GPIO pins : HSYNC_Pin G6_Pin R2_Pin */
      GPIO_InitStruct.Pin = HSYNC_Pin|G6_Pin|R2_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
      GPIO_InitStruct.Alternate = GPIO_AF14_LTDC;
      HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
    
      /*Configure GPIO pin : I2C3_SDA_Pin */
      GPIO_InitStruct.Pin = I2C3_SDA_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
      GPIO_InitStruct.Pull = GPIO_PULLUP;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
      GPIO_InitStruct.Alternate = GPIO_AF4_I2C3;
      HAL_GPIO_Init(I2C3_SDA_GPIO_Port, &GPIO_InitStruct);
    
      /*Configure GPIO pin : I2C3_SCL_Pin */
      GPIO_InitStruct.Pin = I2C3_SCL_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
      GPIO_InitStruct.Pull = GPIO_PULLUP;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
      GPIO_InitStruct.Alternate = GPIO_AF4_I2C3;
      HAL_GPIO_Init(I2C3_SCL_GPIO_Port, &GPIO_InitStruct);
    
      /*Configure GPIO pins : STLINK_RX_Pin STLINK_TX_Pin */
      GPIO_InitStruct.Pin = STLINK_RX_Pin|STLINK_TX_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
      GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
      HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    
      /*Configure GPIO pins : G7_Pin B2_Pin */
      GPIO_InitStruct.Pin = G7_Pin|B2_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
      GPIO_InitStruct.Alternate = GPIO_AF14_LTDC;
      HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
    
      /*Configure GPIO pins : G3_Pin B4_Pin */
      GPIO_InitStruct.Pin = G3_Pin|B4_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
      GPIO_InitStruct.Alternate = GPIO_AF9_LTDC;
      HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
    
      /*Configure GPIO pins : LD3_Pin LD4_Pin */
      GPIO_InitStruct.Pin = LD3_Pin|LD4_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
      HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
    
      /*Configure GPIO pins : SDCKE1_Pin SDNE1_Pin */
      GPIO_InitStruct.Pin = SDCKE1_Pin|SDNE1_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
      GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
      HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    
    }
    
    /* USER CODE BEGIN 4 */
    int __io_putchar(int ch) {
        CDC_Transmit_HS(&ch, 1);
        return ch;
    }
    /* USER CODE END 4 */
    
    /**
      * @brief  This function is executed in case of error occurrence.
      * @retval None
      */
    void Error_Handler(void)
    {
      /* USER CODE BEGIN Error_Handler_Debug */
      /* User can add his own implementation to report the HAL error return state */
    
      /* USER CODE END Error_Handler_Debug */
    }
    
    #ifdef  USE_FULL_ASSERT
    /**
      * @brief  Reports the name of the source file and the source line number
      *         where the assert_param error has occurred.
      * @param  file: pointer to the source file name
      * @param  line: assert_param error line source number
      * @retval None
      */
    void assert_failed(uint8_t *file, uint32_t line)
    { 
      /* USER CODE BEGIN 6 */
      /* User can add his own implementation to report the file name and line number,
         tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
      /* USER CODE END 6 */
    }
    #endif /* USE_FULL_ASSERT */
    
    /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
    
    
    许高峻
    2023-03-14

    经过一番调查,我找到了一个可行的解决方案。

    这里有完整的帖子,包括最终产品的图片:https://zfembedded.wordpress.com/2020/02/27/redirecting-printf-on-stm32-using-fopencookie/

    从在屏幕上打印“B”的基本程序开始,我执行了以下操作:

    1. fopencookie函数位于stdio中。h,所以这需要包括在内
    2. 为了激活它,必须在包含之前定义_GNU_SOURCE
    3. 我们为显示器创建一个流my_stream0。我们也可以为stdout创建一个
    4. fopencookie函数需要cookie_io_functions,所以我们定义了它们。这些似乎不参与行动,但没有它们,事情对我来说就不起作用。关于为什么会这样的提示很有帮助
    5. 我们为cookie_io_functions的一个组件编写函数,在本例中为.write。对我来说,这个函数不需要做任何事情
    6. 我们编写流写入函数
    7. 我们将流写入函数和板定义传递给定义的流
    8. 我们修改流的缓冲
    9. 我们测试了所创建流的fprintfs和printf

    '

    /* 2 */
    #define _GNU_SOURCE
    
    /* 1 */
    #include <stdio.h>
    
    /* 5 */
    static ssize_t dummy_cookie_write(void *c, const char *buf, size_t size)
    {
       return size;
    }
    
    /* 6 */
    int displayA_stream_write(struct _reent *q1, void *q2, const char *q3, int q4)
    {
        (void)q1;
        BOARD_TypeDef_t* q5 = (BOARD_TypeDef_t*)q2;
    
        int DataIdx;
    
        for (DataIdx = 0; DataIdx < q4; DataIdx++)
        {
            display_write(&q5->display[displayA], *q3++);
        }
    
        return q4;
    }
    
    void main(void)
    {
        /* Declare BOARD typedef. */
        BOARD_TypeDef_t board;
    
        /* Initialize BOARD typedef. */
        board_init(&board);
    
        /* Initialize display */
        display_init(&board.display[displayA]);
    
        /* Writes a "B" to the display */
        display_write(&board.display[displayA], 0x42);
    
        /* 4 */
        cookie_io_functions_t dummy_cookie_funcs = {
                .read  = 0,
                .write = dummy_cookie_write, /* Setting this to 0 breaks the solution. */
                .seek  = 0,
                .close = 0
            };
    
        /* 3 */
        FILE *my_stream0 = fopencookie(NULL,"w", dummy_cookie_funcs); /* Replacing dummy_cookie_funcs with NULL breaks the solution. */
        stdout = fopencookie(NULL,"w", dummy_cookie_funcs);
    
        /* 8 */
        setbuf(my_stream0, NULL);
        setbuf(stdout, NULL);
    
        /* 7 */
        my_stream0->_write = displayA_stream_write;
        my_stream0->_cookie = (void *)&board;
        stdout->_write = displayA_stream_write;
        stdout->_cookie = (void *)&board;
    
        /* 9 */
        fprintf(my_stream0, "\nNowhere");
        fprintf(stdout, "\nSomewhere");
        printf("\nHere");
    
    }
    

    '

    使这一切成为可能的关键发现是发现#7写入比#4写入更重要。

    谢谢你的建议。

     类似资料:
    • 问题内容: 我希望在下面的代码中调用newTarget.a()和newTarget.b()时应用调制后的ASM类,以便它看起来像这样 当调用应用了修改的ASM类的newTarget.a()和newTarget.b()时,如何获得以下结果? 码: 想要结果: 第一种方法 第二种方法 转换方法 第一种方法 转换方法 第二种方法 问题答案: 您正在寻找Java API 。它要求您通过参数附加Java代理

    • 这里是简单的Java问题。我制作了一个自定义的JSlider,它在用户移动滑块的最后一个位置绘制方块。我们重写paintcomponent并绘制相对于滑块大小的方块。当你正确地使用滑块时,一切工作都很好。但是,当你调整框架的大小时,问题就会发生。滑块变大,滑块调节器随之移动,但是我们放置的方块相对于滑块不移动。当我们调整框架大小时,我们应该从JSlider中使用哪个函数来重新绘制? 我试图将JSl

    • 问题内容: 在最近的Google IO中,有一个有关实现宁静的客户端应用程序的演示。不幸的是,这只是高层讨论,没有实现的源代码。 在此图中,在返回路径上,有各种不同的对其他方法的回调。 Google io演示幻灯片 如何声明这些方法是什么? 我了解回调的想法-发生某个事件后会调用一段代码,但是我不知道如何实现它。到目前为止,实现回调的唯一方法是重写各种方法(例如onActivityResult)。

    • 我想扩展eclipse cdt的调试器,使其能够在默认情况下跳过具有指定行号的行(步骤)。 我想知道有什么可以扩展到这样做,因为我不需要编写一个完整的新调试器,C/C++的功能是一样好的。

    • 问题内容: 大家好! 我有这样的电话: 是否可以将其包装在自定义函数中 但 保留回调? 就像是: 将是这样的: 问题答案: 编辑: 最近对此表示赞同,我不得不说我不再这样做。返回a,因此您可以直接使用promise以更一致和更可靠的方式执行我在这里所做的几乎所有工作。 然后用法如下: 当然,我一直都这样做。您可以在实际的成功Callack中执行回调,也可以将回调分配为成功回调: 用法如下所示:

    • 问题内容: 我试图将对象存储在redis中,redis是类的实例,因此具有功能,这是一个示例: 有没有一种方法可以将对象与函数一起存储在Redis中?我尝试过,但是只保留了属性。如何存储函数定义并能够执行以下操作: 打电话时如何获得? 先感谢您! 编辑 得到了修改MyClass.prototype并存储值的建议,但是类似这样的事情(setter / getter以外的功能)呢? 我正在尝试说明一个