[活动专区] 【AT-START-F425测评】+ 驱动EEPROM

[复制链接]
1004|1
 楼主| 比神乐 发表于 2022-3-13 12:34 | 显示全部楼层 |阅读模式
本帖最后由 比神乐 于 2022-3-13 12:36 编辑

我有一个AT24C04,今天搞了一下。
SCL->PB6,SDA->PB7
按下用户按键——>PA0
读写正确,LED3点亮。
  1. #include "at32f425_board.h"
  2. #include "at32f425_clock.h"
  3. #include "i2c_application.h"

  4. /** @addtogroup AT32F425_periph_examples
  5.   * @{
  6.   */
  7.   
  8. /** @addtogroup 425_I2C_eeprom I2C_eeprom
  9.   * @{
  10.   */

  11. #define I2C_TIMEOUT                      0xFFFFFFF
  12.                                          
  13. //#define I2Cx_CLKCTRL                   0x2170FAFA   //10K
  14. //#define I2Cx_CLKCTRL                   0x80E06565   //50K
  15. #define I2Cx_CLKCTRL                     0x80E03030   //100K
  16. //#define I2Cx_CLKCTRL                   0x20E0355F   //200K

  17. #define I2Cx_ADDRESS                     0xA0
  18.                                          
  19. #define I2Cx_PORT                        I2C1
  20. #define I2Cx_CLK                         CRM_I2C1_PERIPH_CLOCK
  21. #define I2Cx_DMA                         DMA1
  22. #define I2Cx_DMA_CLK                     CRM_DMA1_PERIPH_CLOCK
  23.                                          
  24. #define I2Cx_SCL_GPIO_CLK                CRM_GPIOB_PERIPH_CLOCK
  25. #define I2Cx_SCL_GPIO_PIN                GPIO_PINS_6
  26. #define I2Cx_SCL_GPIO_PinsSource         GPIO_PINS_SOURCE6  
  27. #define I2Cx_SCL_GPIO_PORT               GPIOB
  28. #define I2Cx_SCL_GPIO_MUX                GPIO_MUX_1     
  29.                                          
  30. #define I2Cx_SDA_GPIO_CLK                CRM_GPIOB_PERIPH_CLOCK
  31. #define I2Cx_SDA_GPIO_PIN                GPIO_PINS_7   
  32. #define I2Cx_SDA_GPIO_PinsSource         GPIO_PINS_SOURCE7      
  33. #define I2Cx_SDA_GPIO_PORT               GPIOB
  34. #define I2Cx_SDA_GPIO_MUX                GPIO_MUX_1
  35.                                          
  36. #define I2Cx_DMA_TX_Channel              DMA1_CHANNEL1
  37. #define I2Cx_DMA_TX_DMAMUX_Channel       FLEX_CHANNEL1
  38. #define I2Cx_DMA_TX_DMAREQ               DMA_FLEXIBLE_I2C1_TX
  39. #define I2Cx_DMA_TX_IRQn                 DMA1_Channel1_IRQn

  40. #define I2Cx_DMA_RX_Channel              DMA1_CHANNEL2
  41. #define I2Cx_DMA_RX_DMAMUX_Channel       FLEX_CHANNEL2
  42. #define I2Cx_DMA_RX_DMAREQ               DMA_FLEXIBLE_I2C1_RX
  43. #define I2Cx_DMA_RX_IRQn                 DMA1_Channel3_2_IRQn

  44. #define I2Cx_EVT_IRQn                    I2C1_EVT_IRQn
  45. #define I2Cx_ERR_IRQn                    I2C1_ERR_IRQn

  46. #define BUF_SIZE                         8

  47. uint8_t tx_buf1[BUF_SIZE] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
  48. uint8_t tx_buf2[BUF_SIZE] = {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80};
  49. uint8_t tx_buf3[BUF_SIZE] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
  50. uint8_t rx_buf1[BUF_SIZE] = {0};
  51. uint8_t rx_buf2[BUF_SIZE] = {0};
  52. uint8_t rx_buf3[BUF_SIZE] = {0};

  53. i2c_handle_type hi2cx;

  54. /**
  55.   * [url=home.php?mod=space&uid=247401]@brief[/url]  error handler program
  56.   * @param  i2c_status
  57.   * @retval none
  58.   */
  59. void error_handler(uint32_t error_code)
  60. {
  61.   while(1)
  62.   {
  63.     at32_led_toggle(LED2);
  64.     delay_ms(500);
  65.   }
  66. }

  67. /**
  68.   * [url=home.php?mod=space&uid=247401]@brief[/url]  compare whether the valus of buffer 1 and buffer 2 are equal.
  69.   * @param  buffer1: buffer 1 address.
  70.             buffer2: buffer 2 address.
  71.   * @retval 0: equal.
  72.   *         1: unequal.
  73.   */
  74. uint32_t buffer_compare(uint8_t* buffer1, uint8_t* buffer2, uint32_t len)
  75. {
  76.   uint32_t i;
  77.   
  78.   for(i = 0; i < len; i++)
  79.   {
  80.     if(buffer1[i] != buffer2[i])
  81.     {
  82.       return 1;
  83.     }
  84.   }

  85.   return 0;
  86. }

  87. /**
  88.   * @brief  main function.
  89.   * @param  none
  90.   * @retval none
  91.   */
  92. int main(void)
  93. {
  94.   i2c_status_type i2c_status;
  95.   
  96.   /* initial system clock */  
  97.   system_clock_config();  
  98.   
  99.   /* config nvic priority group */
  100.   nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
  101.   
  102.   /* at board initial */
  103.   at32_board_init();

  104.   hi2cx.i2cx = I2Cx_PORT;
  105.   
  106.   /* i2c config */
  107.   i2c_config(&hi2cx);
  108.   
  109.   while(1)
  110.   {
  111.     /* wait for key USER_BUTTON press before starting the communication */
  112.     while(at32_button_press() != USER_BUTTON)
  113.     {
  114.     }
  115.    
  116.     /* write data to memory device */  
  117.     if((i2c_status = i2c_memory_write(&hi2cx, I2Cx_ADDRESS, 0, tx_buf1, BUF_SIZE, I2C_TIMEOUT)) != I2C_OK)
  118.     {
  119.       error_handler(i2c_status);
  120.     }
  121.    
  122.     delay_ms(5);
  123.    
  124.     /* read data from memory device */  
  125.     if((i2c_status = i2c_memory_read(&hi2cx, I2Cx_ADDRESS, 0, rx_buf1, BUF_SIZE, I2C_TIMEOUT)) != I2C_OK)
  126.     {
  127.       error_handler(i2c_status);
  128.     }

  129.     /* write data to memory device */  
  130.     if((i2c_status = i2c_memory_write_int(&hi2cx, I2Cx_ADDRESS, 0, tx_buf2, BUF_SIZE, I2C_TIMEOUT)) != I2C_OK)
  131.     {
  132.       error_handler(i2c_status);
  133.     }      

  134.     /* wait for the communication to end */  
  135.     if(i2c_wait_end(&hi2cx, I2C_TIMEOUT) != I2C_OK)
  136.     {
  137.       error_handler(i2c_status);
  138.     }      

  139.     delay_ms(5);
  140.    
  141.     /* read data from memory device */  
  142.     if((i2c_status = i2c_memory_read_int(&hi2cx, I2Cx_ADDRESS, 0, rx_buf2, BUF_SIZE, I2C_TIMEOUT)) != I2C_OK)
  143.     {
  144.       error_handler(i2c_status);
  145.     }
  146.    
  147.     /* wait for the communication to end */  
  148.     if(i2c_wait_end(&hi2cx, I2C_TIMEOUT) != I2C_OK)
  149.     {
  150.       error_handler(i2c_status);
  151.     }      

  152.     /* write data to memory device */  
  153.     if((i2c_status = i2c_memory_write_dma(&hi2cx, I2Cx_ADDRESS, 0, tx_buf3, BUF_SIZE, I2C_TIMEOUT)) != I2C_OK)
  154.     {
  155.       error_handler(i2c_status);
  156.     }      

  157.     /* wait for the communication to end */  
  158.     if(i2c_wait_end(&hi2cx, I2C_TIMEOUT) != I2C_OK)
  159.     {
  160.       error_handler(i2c_status);
  161.     }      

  162.     delay_ms(5);
  163.    
  164.     /* read data from memory device */  
  165.     if((i2c_status = i2c_memory_read_dma(&hi2cx, I2Cx_ADDRESS, 0, rx_buf3, BUF_SIZE, I2C_TIMEOUT)) != I2C_OK)
  166.     {
  167.       error_handler(i2c_status);
  168.     }
  169.    
  170.     /* wait for the communication to end */  
  171.     if(i2c_wait_end(&hi2cx, I2C_TIMEOUT) != I2C_OK)
  172.     {
  173.       error_handler(i2c_status);
  174.     }      

  175.     if((buffer_compare(tx_buf1, rx_buf1, BUF_SIZE) == 0) &&
  176.        (buffer_compare(tx_buf2, rx_buf2, BUF_SIZE) == 0) &&
  177.        (buffer_compare(tx_buf3, rx_buf3, BUF_SIZE) == 0))
  178.     {
  179.       at32_led_on(LED3);
  180.     }
  181.     else
  182.     {
  183.       error_handler(i2c_status);
  184.     }
  185.    
  186.   }   
  187. }

  188. /**
  189.   * @brief  initializes peripherals used by the i2c.
  190.   * @param  none
  191.   * @retval none
  192.   */
  193. void i2c_lowlevel_init(i2c_handle_type* hi2c)
  194. {
  195.   gpio_init_type gpio_init_structure;
  196.   
  197.   if(hi2c->i2cx == I2Cx_PORT)
  198.   {
  199.     /* i2c periph clock enable */
  200.     crm_periph_clock_enable(I2Cx_CLK, TRUE);   
  201.     crm_periph_clock_enable(I2Cx_SCL_GPIO_CLK, TRUE);
  202.     crm_periph_clock_enable(I2Cx_SDA_GPIO_CLK, TRUE);
  203.    
  204.     /* gpio configuration */  
  205.     gpio_pin_mux_config(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_GPIO_PinsSource, I2Cx_SCL_GPIO_MUX);
  206.    
  207.     gpio_pin_mux_config(I2Cx_SDA_GPIO_PORT, I2Cx_SDA_GPIO_PinsSource, I2Cx_SDA_GPIO_MUX);
  208.    
  209.     /* configure i2c pins: scl */
  210.     gpio_init_structure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  211.     gpio_init_structure.gpio_mode           = GPIO_MODE_MUX;
  212.     gpio_init_structure.gpio_out_type       = GPIO_OUTPUT_OPEN_DRAIN;
  213.     gpio_init_structure.gpio_pull           = GPIO_PULL_UP;  
  214.                                             
  215.     gpio_init_structure.gpio_pins           = I2Cx_SCL_GPIO_PIN;
  216.     gpio_init(I2Cx_SCL_GPIO_PORT, &gpio_init_structure);

  217.     /* configure i2c pins: sda */
  218.     gpio_init_structure.gpio_pins           = I2Cx_SDA_GPIO_PIN;
  219.     gpio_init(I2Cx_SDA_GPIO_PORT, &gpio_init_structure);
  220.    
  221.     /* configure and enable i2c interrupt */     
  222.     nvic_irq_enable(I2Cx_EVT_IRQn, 0, 0);
  223.     nvic_irq_enable(I2Cx_ERR_IRQn, 0, 0);     
  224.    
  225.     /* configure and enable i2c dma channel interrupt */
  226.     nvic_irq_enable(I2Cx_DMA_TX_IRQn, 0, 0);
  227.     nvic_irq_enable(I2Cx_DMA_RX_IRQn, 0, 0);        

  228.     /* i2c dma tx and rx channels configuration */
  229.     /* enable the dma clock */
  230.     crm_periph_clock_enable(I2Cx_DMA_CLK, TRUE);
  231.    
  232.     /* i2c dma channel configuration */
  233.     dma_reset(hi2c->dma_tx_channel);
  234.     dma_reset(hi2c->dma_rx_channel);   
  235.    
  236.     hi2c->dma_tx_channel = I2Cx_DMA_TX_Channel;
  237.     hi2c->dma_rx_channel = I2Cx_DMA_RX_Channel;

  238.     hi2c->dma_init_struct.peripheral_base_addr    = (uint32_t)&hi2c->i2cx->txdt;
  239.     hi2c->dma_init_struct.memory_base_addr        = 0;
  240.     hi2c->dma_init_struct.direction               = DMA_DIR_MEMORY_TO_PERIPHERAL;
  241.     hi2c->dma_init_struct.buffer_size             = 0xFFFF;
  242.     hi2c->dma_init_struct.peripheral_inc_enable   = FALSE;
  243.     hi2c->dma_init_struct.memory_inc_enable       = TRUE;
  244.     hi2c->dma_init_struct.peripheral_data_width   = DMA_PERIPHERAL_DATA_WIDTH_BYTE;
  245.     hi2c->dma_init_struct.memory_data_width       = DMA_MEMORY_DATA_WIDTH_BYTE;
  246.     hi2c->dma_init_struct.loop_mode_enable        = FALSE;
  247.     hi2c->dma_init_struct.priority                = DMA_PRIORITY_LOW;

  248.     dma_init(hi2c->dma_tx_channel, &hi2c->dma_init_struct);  
  249.     dma_init(hi2c->dma_rx_channel, &hi2c->dma_init_struct);   
  250.    
  251.     dma_flexible_config(DMA1, I2Cx_DMA_TX_DMAMUX_Channel, I2Cx_DMA_TX_DMAREQ);
  252.     dma_flexible_config(DMA1, I2Cx_DMA_RX_DMAMUX_Channel, I2Cx_DMA_RX_DMAREQ);
  253.   
  254.     /* config i2c */
  255.     i2c_init(hi2c->i2cx, 0, I2Cx_CLKCTRL);
  256.    
  257.     i2c_own_address1_set(hi2c->i2cx, I2C_ADDRESS_MODE_7BIT, I2Cx_ADDRESS);
  258.   }
  259. }
效果图:

 楼主| 比神乐 发表于 2022-3-13 12:35 | 显示全部楼层
本帖最后由 比神乐 于 2022-3-13 12:39 编辑

1.jpg 提示无法上传文件。
关闭浏览器,重新打开就好了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

470

主题

3537

帖子

7

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