[产品应用] 【CW32L031CxTx StartKit评估板测评】02-软件模拟I2C读写板载EEPROM

[复制链接]
 楼主| 怀揣少年梦 发表于 2023-11-10 11:44 | 显示全部楼层 |阅读模式
本帖最后由 怀揣少年梦 于 2023-11-10 11:44 编辑

一、开发板上硬件设计
CW32L031CxTx StartKit评估板上板载一颗型号为CW24C02AD的EEPROM,EEPROM适用于保存数据量比较小的场景,并且可靠性比较高。
先看一下CW24C02AD的数据手册,可以看到该EEPROM只有2Kbit,也就是256字节(这是EEPROM中容量最小的芯片),每页8个字节,一共32页。工作电压1.7-5.5V;允许页面局部写入;I2C 时钟频率为 1MHz (5V,3V),400 KHz (1.7V);擦写寿命:100 万次;数据保持时间: 20年;
DATASHEET5.png
从评估板的原理图来看,EEPROM的A2\A1\A0三个引脚接地,说明读写EEPROM的地址为A0;
EEPROM.png
二、软件设计
使用GPIO口模拟I2C读写EEPROM。
具体代码如下:

1).c代码
  1. #include "eeprom.h"

  2. #define I2C_DIR_TRANSMIT                 0x00
  3. #define I2C_DIR_RECEIVE                  0x01

  4. #define I2C_ACK_TIMEOUT                  64

  5. #define I2C_SCL_HIGH()                   GPIO_WritePin(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN, GPIO_Pin_SET)
  6. #define I2C_SCL_LOW()                    GPIO_WritePin(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN, GPIO_Pin_RESET)

  7. #define I2C_SDA_HIGH()                   GPIO_WritePin(I2Cx_SDA_GPIO_PORT, I2Cx_SDA_PIN, GPIO_Pin_SET)
  8. #define I2C_SDA_LOW()                    GPIO_WritePin(I2Cx_SDA_GPIO_PORT, I2Cx_SDA_PIN, GPIO_Pin_RESET)

  9. #define I2C_SDA_READ                     GPIO_ReadPin(I2Cx_SDA_GPIO_PORT, I2Cx_SDA_PIN)
  10. #define I2C_DALEY_US         5

  11. /**
  12.   * [url=home.php?mod=space&uid=247401]@brief[/url] i2c address direction
  13.   */

  14. void i2c_config(void)
  15. {
  16.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  17.   
  18.   /* i2c gpio clock enable */
  19.         __RCC_GPIOB_CLK_ENABLE();
  20.   
  21.   /* gpio configuration */  
  22.         GPIO_InitStruct.IT = GPIO_IT_NONE;
  23.         GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;

  24.   /* configure i2c pins: scl */   
  25.   GPIO_InitStruct.Pins = I2Cx_SCL_PIN;
  26.   GPIO_Init(I2Cx_SCL_GPIO_PORT, &GPIO_InitStruct);

  27.   /* configure i2c pins: sda */     
  28.   GPIO_InitStruct.Pins = I2Cx_SDA_PIN;
  29.   GPIO_Init(I2Cx_SDA_GPIO_PORT, &GPIO_InitStruct);

  30.   I2C_SDA_HIGH();
  31.   I2C_SCL_HIGH();
  32. }

  33. /**
  34.   * @brief  used to set the i2c clock frequency.
  35.   * @param  none.
  36.   * @retval none.
  37.   */
  38. void i2c_delay(void)
  39. {
  40.         uint8_t i = 0;
  41.         uint8_t j = 48;
  42.         for (i = 0; i< I2C_DALEY_US; i++) {
  43.                 while(j--);
  44.         }
  45. }


  46. /**
  47. * @brief 循环延时
  48. *
  49. * @param nCount
  50. */
  51. void Delay(__IO uint16_t nCount)
  52. {
  53.     /* Decrement nCount value */
  54.     while (nCount != 0)
  55.     {
  56.         nCount--;
  57.     }
  58. }

  59. /**
  60.   * @brief  used to generate start conditions.
  61.   * @param  none.
  62.   * @retval none.
  63.   */
  64. void i2c_start(void)
  65. {
  66.   i2c_delay();
  67.   
  68.   I2C_SDA_HIGH();
  69.   I2C_SCL_HIGH();
  70.   i2c_delay();
  71.   
  72.   I2C_SDA_LOW();
  73.   i2c_delay();
  74.   
  75.   I2C_SCL_LOW();
  76.   i2c_delay();
  77. }

  78. /**
  79.   * @brief  used to generate stop conditions.
  80.   * @param  none.
  81.   * @retval none.
  82.   */
  83. void i2c_stop(void)
  84. {
  85.   I2C_SCL_LOW();
  86.   I2C_SDA_LOW();  
  87.   i2c_delay();
  88.   
  89.   I2C_SCL_HIGH();
  90.   i2c_delay();
  91.   
  92.   I2C_SDA_HIGH();
  93.   i2c_delay();
  94. }

  95. /**
  96.   * @brief  used to generate ack conditions.
  97.   * @param  none.
  98.   * @retval none.
  99.   */
  100. void i2c_ack(void)
  101. {
  102.   I2C_SCL_LOW();
  103.   I2C_SDA_LOW();
  104.   i2c_delay();
  105.   
  106.   I2C_SCL_HIGH();
  107.   i2c_delay();
  108.   
  109.   I2C_SCL_LOW();
  110.   i2c_delay();
  111. }

  112. /**
  113.   * @brief  used to generate nack conditions.
  114.   * @param  none.
  115.   * @retval none.
  116.   */
  117. void i2c_no_ack(void)
  118. {
  119.   I2C_SCL_LOW();
  120.   I2C_SDA_HIGH();
  121.   i2c_delay();
  122.   
  123.   I2C_SCL_HIGH();
  124.   i2c_delay();
  125.   
  126.   I2C_SCL_LOW();
  127.   i2c_delay();
  128. }

  129. /**
  130.   * @brief  used to wait ack conditions.
  131.   * @param  none.
  132.   * @retval ack receive status.
  133.   *         - 1: no ack received.
  134.   *         - 0: ack received.
  135.   */
  136. uint8_t i2c_wait_ack(uint8_t timeout)
  137. {
  138.   I2C_SCL_LOW();
  139.   I2C_SDA_HIGH();  
  140.   
  141.   i2c_delay();
  142.   
  143.   while(timeout)
  144.   {
  145.     if (I2C_SDA_READ == 0)
  146.     {
  147.       I2C_SCL_HIGH();
  148.       
  149.       i2c_delay();
  150.       
  151.       I2C_SCL_LOW();
  152.       
  153.       return 0;
  154.     }
  155.    
  156.     i2c_delay();
  157.    
  158.     timeout--;
  159.   }  
  160.   
  161.   return 1;
  162. }

  163. /**
  164.   * @brief  send a byte.
  165.   * @param  data: byte to be transmitted.
  166.   * @retval none.
  167.   */
  168. void i2c_send_byte(uint8_t data)
  169. {
  170.   uint8_t i = 8;
  171.        
  172.   I2C_SCL_LOW(); //
  173.   while (i--)
  174.   {
  175.     if (data & 0x80)
  176.     {
  177.       I2C_SDA_HIGH();   
  178.     }
  179.     else
  180.     {
  181.       I2C_SDA_LOW();   
  182.     }   
  183.    
  184.     i2c_delay();
  185.    
  186.     I2C_SCL_HIGH();
  187.     i2c_delay();
  188.                 I2C_SCL_LOW();
  189.     i2c_delay();
  190.        
  191.         data <<= 1;
  192.   }   
  193. }

  194. /**
  195.   * @brief  receive a byte.
  196.   * @param  data: byte to be received.
  197.   * @retval none.
  198.   */
  199. uint8_t i2c_receive_byte(void)
  200. {
  201.   uint8_t i = 8;
  202.   uint8_t byte = 0;

  203.   I2C_SDA_HIGH();
  204.   I2C_SCL_LOW();
  205.        
  206.   while (i--)
  207.   {
  208.     byte <<= 1;
  209.     I2C_SCL_HIGH();
  210.     i2c_delay();
  211.   
  212.     byte |= I2C_SDA_READ;
  213.           I2C_SCL_LOW();
  214.     i2c_delay();

  215.   }

  216.   return byte;
  217. }

  218. uint8_t i2c_WriteByte(uint8_t I2C_Addr,uint8_t addr,uint8_t data)
  219. {
  220.         i2c_start();
  221.         i2c_send_byte(I2C_Addr | I2C_DIR_TRANSMIT);; //
  222.         if (i2c_wait_ack(I2C_ACK_TIMEOUT) == 1)
  223.         {
  224.       i2c_stop();
  225.                
  226.           return 1;
  227.         }
  228.         i2c_send_byte(addr); //
  229.         if (i2c_wait_ack(I2C_ACK_TIMEOUT) == 1)
  230.         {
  231.       i2c_stop();
  232.       printf("EEPROM %x \r\n",addr);       
  233.           return 1;
  234.         }          
  235.         i2c_send_byte(data);
  236.         if (i2c_wait_ack(I2C_ACK_TIMEOUT) == 1)
  237.         {
  238.       i2c_stop();
  239.       printf("EEPROM %x \r\n",data);               
  240.           return 1;
  241.         }
  242.        
  243.         i2c_stop(); //

  244.         Delay(0xffff);
  245.         Delay(0xffff);
  246.         Delay(0xffff);
  247.         printf("write ok\r\n");
  248.         return 0;
  249. }


  250. uint8_t i2c_ReadCurrentAddrByte(uint8_t I2C_Addr)
  251. {
  252.         uint8_t data;
  253.         i2c_start();
  254.         i2c_send_byte(I2C_Addr | I2C_DIR_RECEIVE);; //锟斤拷锟酵从伙拷锟斤拷址
  255.         if (i2c_wait_ack(I2C_ACK_TIMEOUT) == 1)
  256.         {
  257.       i2c_stop();
  258.                
  259.           return 1;
  260.         }
  261.         data = i2c_receive_byte();
  262.         i2c_no_ack();
  263.         i2c_stop(); //
  264.         printf("receive1 ok\r\n");
  265.         return data;
  266. }

  267. uint8_t i2c_ReadByte(uint8_t I2C_Addr,uint8_t addr)
  268. {
  269.         uint8_t data;
  270.         i2c_start();
  271.         i2c_send_byte(I2C_Addr | I2C_DIR_TRANSMIT);; //
  272.         if (i2c_wait_ack(I2C_ACK_TIMEOUT) == 1)
  273.         {
  274.       i2c_stop();
  275.                
  276.           return 1;
  277.         }
  278.         i2c_send_byte((uint8_t)addr); //
  279.         if (i2c_wait_ack(I2C_ACK_TIMEOUT) == 1)
  280.         {
  281.       i2c_stop();
  282.       printf("EEPROM %x \r\n",addr);       
  283.           return 1;
  284.         }          
  285.        
  286.         i2c_start();
  287.         i2c_send_byte(I2C_Addr | I2C_DIR_RECEIVE);; //
  288.         if (i2c_wait_ack(I2C_ACK_TIMEOUT) == 1)
  289.         {
  290.       i2c_stop();
  291.       printf("EEPROM %x \r\n",I2C_Addr);               
  292.           return 1;
  293.         }
  294.         data = i2c_receive_byte();
  295.         i2c_no_ack();
  296.         i2c_stop(); //
  297.         printf("receive ok\r\n");
  298.         return data;
  299. }


  300. uint8_t i2c_ReadMultByte(uint8_t I2C_Addr,uint16_t addr,uint8_t length, uint8_t *pdata)
  301. {
  302.         uint8_t i = 0;
  303.        
  304.         i2c_start();
  305.         i2c_send_byte(I2C_Addr | I2C_DIR_TRANSMIT);; //
  306.         if (i2c_wait_ack(I2C_ACK_TIMEOUT) == 1)
  307.         {
  308.       i2c_stop();
  309.                
  310.           return 1;
  311.         }

  312.         i2c_send_byte((uint8_t)addr); //
  313.         if (i2c_wait_ack(I2C_ACK_TIMEOUT) == 1)
  314.         {
  315.       i2c_stop();
  316.       printf("EEPROM %x \r\n",addr);       
  317.           return 1;
  318.         }          
  319.        
  320.         i2c_start();
  321.         i2c_send_byte(I2C_Addr | I2C_DIR_RECEIVE);; //
  322.         if (i2c_wait_ack(I2C_ACK_TIMEOUT) == 1)
  323.         {
  324.       i2c_stop();
  325.       printf("EEPROM %x \r\n",I2C_Addr);               
  326.           return 1;
  327.         }
  328.        
  329.         for(i = 0; i< length; i++) {
  330.        
  331.       pdata[i] = i2c_receive_byte();
  332.    
  333.       if (i < (length - 1))
  334.       {
  335.         i2c_ack();
  336.       }
  337.       else
  338.       {
  339.         i2c_no_ack();
  340.       }
  341.         }

  342.         i2c_stop(); //
  343.         printf("mul receive ok\r\n");
  344.         return 0;
  345. }

  346. uint8_t i2c_WriteMultByte(uint8_t I2C_Addr,uint16_t addr,uint8_t length, uint8_t *pdata)
  347. {
  348.         uint8_t i = 0;
  349.        
  350.         i2c_start();
  351.         i2c_send_byte(I2C_Addr | I2C_DIR_TRANSMIT);; //
  352.         if (i2c_wait_ack(I2C_ACK_TIMEOUT) == 1)
  353.         {
  354.       i2c_stop();
  355.                
  356.           return 1;
  357.         }
  358.        
  359.         i2c_send_byte((uint8_t)addr); //
  360.   printf("addr high %x",(uint8_t)(addr));
  361.         if (i2c_wait_ack(I2C_ACK_TIMEOUT) == 1)
  362.         {
  363.       i2c_stop();
  364.       printf("EEPROM %x\r\n",addr);       
  365.           return 1;
  366.         }          
  367.        
  368.         for(i = 0; i< length; i++) {
  369.       i2c_send_byte(pdata[i]);
  370.       if (i2c_wait_ack(I2C_ACK_TIMEOUT) == 1)
  371.       {
  372.         i2c_stop();
  373.         printf("EEPROM %d %x \r\n",i,addr);       
  374.         return 1;
  375.       }
  376.         }

  377.         i2c_stop(); //
  378.   printf("write ok\r\n");
  379.         Delay(0xffff);

  380.         return 0;
  381. }


2)、.h代码
  1. #ifndef __EEPROM_H__
  2. #define __EEPROM_H__

  3. #include "..\inc\main.h"

  4. #define I2Cx_SCL_PIN                     GPIO_PIN_6
  5. #define I2Cx_SCL_GPIO_PORT               CW_GPIOB

  6. #define I2Cx_SDA_PIN                     GPIO_PIN_7
  7. #define I2Cx_SDA_GPIO_PORT               CW_GPIOB



  8. /** @defgroup I2C_library_exported_functions
  9.   * @{
  10.   */

  11. void i2c_config(void);
  12. void delay_us(uint32_t us);
  13. void Delay(__IO uint16_t nCount);
  14. uint8_t i2c_WriteByte(uint8_t I2C_Addr,uint8_t addr,uint8_t data);
  15. uint8_t i2c_ReadCurrentAddrByte(uint8_t I2C_Addr);
  16. uint8_t i2c_ReadByte(uint8_t I2C_Addr,uint8_t addr);
  17. uint8_t i2c_ReadMultByte(uint8_t I2C_Addr,uint16_t addr,uint8_t length, uint8_t *pdata);
  18. uint8_t i2c_WriteMultByte(uint8_t I2C_Addr,uint16_t addr,uint8_t length, uint8_t *pdata);
  19. #endif


3)、测试主程序
  1. #include "..\inc\main.h"
  2. /******************************************************************************
  3. * Local pre-processor symbols/macros ('#define')
  4. ******************************************************************************/
  5. //UARTx
  6. #define  DEBUG_USARTx                   CW_UART1   
  7. #define  DEBUG_USART_CLK                RCC_APB2_PERIPH_UART1
  8. #define  DEBUG_USART_APBClkENx          RCC_APBPeriphClk_Enable2
  9. #define  DEBUG_USART_BaudRate           9600
  10. #define  DEBUG_USART_UclkFreq           8000000
  11. #define  LED_GPIO_PORT CW_GPIOB
  12. #define  LED_GPIO_PINS GPIO_PIN_8 | GPIO_PIN_9
  13. //UARTx GPIO
  14. #define  DEBUG_USART_GPIO_CLK           RCC_AHB_PERIPH_GPIOA
  15. #define  DEBUG_USART_TX_GPIO_PORT       CW_GPIOA
  16. #define  DEBUG_USART_TX_GPIO_PIN        GPIO_PIN_8
  17. #define  DEBUG_USART_RX_GPIO_PORT       CW_GPIOA
  18. #define  DEBUG_USART_RX_GPIO_PIN        GPIO_PIN_9

  19. //GPIO AF
  20. #define  DEBUG_USART_AFTX               PA08_AFx_UART1TXD()
  21. #define  DEBUG_USART_AFRX               PA09_AFx_UART1RXD()
  22. /******************************************************************************
  23. * Global variable definitions (declared in header file with 'extern')
  24. ******************************************************************************/
  25. uint8_t test_data[] = {0x01,0x02,0x03};
  26. uint8_t test_readdata[3] = {0x00};
  27. /******************************************************************************
  28. * Local type definitions ('typedef')
  29. ******************************************************************************/

  30. /******************************************************************************
  31. * Local function prototypes ('static')
  32. ******************************************************************************/
  33. void RCC_Configuration(void);
  34. void GPIO_Configuration(void);
  35. void UART_Configuration(void);

  36. #ifdef __GNUC__
  37.     /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  38.     set to 'Yes') calls __io_putchar() */
  39.     #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  40. #else
  41.     #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  42. #endif /* __GNUC__ */

  43. /******************************************************************************
  44. * Local variable definitions ('static')                                      *
  45. ******************************************************************************/

  46. /******************************************************************************
  47. * Local pre-processor symbols/macros ('#define')
  48. ******************************************************************************/

  49. /*****************************************************************************
  50. * Function implementation - global ('extern') and local ('static')
  51. ******************************************************************************/

  52. /**
  53. ******************************************************************************
  54. ** \brief  Main function of project
  55. **
  56. ** \return uint32_t return value, if needed
  57. **
  58. ******************************************************************************/
  59. int32_t main(void)
  60. {
  61.     //配置RCC
  62.     RCC_Configuration();

  63.     //配置GPIO
  64.     GPIO_Configuration();
  65.                 //i2c_config();
  66.     //配置UART
  67.     UART_Configuration();
  68.                
  69.     printf("\r\n use CW32L031 simulate I2C READ EEPROM Example\r\n");
  70.                
  71.     while(1)
  72.     {
  73.                         if (GPIO_ReadPin(CW_GPIOA, GPIO_PIN_1) == 0) {
  74.                                 i2c_WriteByte(0xA0,0x01,0x5A);
  75.                                 i2c_WriteMultByte(0xA0,0x05,3,test_data);
  76.                         }
  77.                        
  78.                         if (GPIO_ReadPin(CW_GPIOA, GPIO_PIN_2) == 0) {
  79.                                 printf("data:%x \r\n",i2c_ReadByte(0xA0,0x01));
  80.                                 i2c_ReadMultByte(0xA0,0x05,3,test_readdata);
  81.                                 for(int i = 0;i < 3; i++) {
  82.                                         printf("test_readdata[%d] = %x \r\n",i,test_readdata[i]);
  83.                                 }
  84.                                
  85.                         }
  86.                        
  87.                         GPIO_TogglePin(LED_GPIO_PORT, LED_GPIO_PINS);
  88.       Delay(0xFFFF);
  89.     }
  90. }


  91. /**
  92. * @brief 配置RCC
  93. *
  94. */
  95. void RCC_Configuration(void)
  96. {
  97.     //SYSCLK = HSI = 8MHz = HCLK = PCLK
  98.     RCC_HSI_Enable(RCC_HSIOSC_DIV6);

  99.     //外设时钟使能
  100.     RCC_AHBPeriphClk_Enable(DEBUG_USART_GPIO_CLK, ENABLE);
  101.     DEBUG_USART_APBClkENx(DEBUG_USART_CLK, ENABLE);
  102.                 RCC_AHBPeriphClk_Enable(RCC_AHB_PERIPH_GPIOB,ENABLE);
  103.                 __RCC_GPIOA_CLK_ENABLE();
  104. }

  105. /**
  106. * @brief 配置GPIO
  107. *
  108. */
  109. void GPIO_Configuration(void)
  110. {
  111.     GPIO_InitTypeDef GPIO_InitStructure = {0};

  112.                
  113.     //UART TX RX 复用
  114.     DEBUG_USART_AFTX;
  115.     DEBUG_USART_AFRX;

  116.     GPIO_InitStructure.Pins = DEBUG_USART_TX_GPIO_PIN;
  117.     GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
  118.     GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &GPIO_InitStructure);

  119.                 GPIO_InitStructure.Pins = GPIO_PIN_1 | GPIO_PIN_2;
  120.     GPIO_InitStructure.Mode = GPIO_MODE_INPUT_PULLUP;
  121.     GPIO_Init(CW_GPIOA, &GPIO_InitStructure);
  122.                
  123.     GPIO_InitStructure.Pins = DEBUG_USART_RX_GPIO_PIN;
  124.     GPIO_InitStructure.Mode = GPIO_MODE_INPUT_PULLUP;
  125.     GPIO_Init(DEBUG_USART_RX_GPIO_PORT, &GPIO_InitStructure);  

  126.     GPIO_InitStructure.IT = GPIO_IT_NONE;
  127.     GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
  128.     GPIO_InitStructure.Pins = LED_GPIO_PINS;               
  129.                
  130.                 GPIO_Init(LED_GPIO_PORT, &GPIO_InitStructure);
  131.                
  132.                
  133.                   /* gpio configuration */  
  134.                 GPIO_InitStructure.IT = GPIO_IT_NONE;
  135.                 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_OD;

  136.                 /* configure i2c pins: scl */   
  137.                 GPIO_InitStructure.Pins = I2Cx_SCL_PIN;
  138.                 GPIO_Init(I2Cx_SCL_GPIO_PORT, &GPIO_InitStructure);

  139.                 /* configure i2c pins: sda */     
  140.                 GPIO_InitStructure.Pins = I2Cx_SDA_PIN;
  141.                 GPIO_Init(I2Cx_SDA_GPIO_PORT, &GPIO_InitStructure);

  142.                 GPIO_WritePin(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN, GPIO_Pin_SET);
  143.                 GPIO_WritePin(I2Cx_SDA_GPIO_PORT, I2Cx_SDA_PIN, GPIO_Pin_SET);
  144. }

  145. /**
  146. * @brief 配置UART
  147. *
  148. */
  149. void UART_Configuration(void)
  150. {
  151.     USART_InitTypeDef USART_InitStructure = {0};

  152.     USART_InitStructure.USART_BaudRate = DEBUG_USART_BaudRate;
  153.     USART_InitStructure.USART_Over = USART_Over_16;
  154.     USART_InitStructure.USART_Source = USART_Source_PCLK;
  155.     USART_InitStructure.USART_UclkFreq = DEBUG_USART_UclkFreq;
  156.     USART_InitStructure.USART_StartBit = USART_StartBit_FE;
  157.     USART_InitStructure.USART_StopBits = USART_StopBits_1;
  158.     USART_InitStructure.USART_Parity = USART_Parity_No ;
  159.     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  160.     USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  161.     USART_Init(DEBUG_USARTx, &USART_InitStructure);
  162. }

  163. // KEIL
  164. /**
  165. * @brief Retargets the C library printf function to the USART.
  166. *
  167. */
  168. PUTCHAR_PROTOTYPE
  169. {
  170.     USART_SendData_8bit(DEBUG_USARTx, (uint8_t)ch);

  171.     while (USART_GetFlagStatus(DEBUG_USARTx, USART_FLAG_TXE) == RESET);

  172.     return ch;
  173. }


  174. // IAR
  175. size_t __write(int handle, const unsigned char * buffer, size_t size)
  176. {
  177.     size_t nChars = 0;

  178.     if (buffer == 0)
  179.     {
  180.         /*
  181.          * This means that we should flush internal buffers.  Since we
  182.          * don't we just return.  (Remember, "handle" == -1 means that all
  183.          * handles should be flushed.)
  184.          */
  185.         return 0;
  186.     }


  187.     for (/* Empty */; size != 0; --size)
  188.     {
  189.         USART_SendData_8bit(DEBUG_USARTx, *buffer++);
  190.         while (USART_GetFlagStatus(DEBUG_USARTx, USART_FLAG_TXE) == RESET);
  191.         ++nChars;
  192.     }

  193.     return nChars;
  194. }
三、实际测试串口输出

单字节写入5a,单字节读出来也是5a;
多字节写入01 02 03;多字节读出来也是01 02 03


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

本版积分规则

个人签名:一切皆有可能

45

主题

473

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部
个人签名:一切皆有可能

45

主题

473

帖子

3

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