新手上路
- 积分
- 20
- 金钱
- 20
- 注册时间
- 2021-5-17
- 在线时间
- 3 小时
|
2金钱
本帖最后由 Audience80 于 2021-5-30 17:25 编辑
普通阻塞MEM读写ok
mem dma就读写错误
代码:
main.c > main()
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- uint8_t wirte_data[10] = "222";
- uint8_t read_data[10];
- /* 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_DMA_Init();
- MX_SPI2_Init();
- MX_USART1_UART_Init();
- MX_I2C1_Init();
- MX_I2C3_Init();
- MX_ADC1_Init();
- /* USER CODE BEGIN 2 */
- gui_init();
-
- eeprom_write(0x0aa0, wirte_data, 10);
-
- HAL_Delay(100);
-
- eeprom_read(0x0aa0, read_data, 10);
-
- printf("wirte_data: %s\n", wirte_data);
- printf("read_data: %s\n", read_data);
-
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
-
- gui_start(); // 界面轮循
- key_start(); // 按键扫描
- }
- /* USER CODE END 3 */
- }
复制代码
eeprom.c
- #include "eeprom.h"
- #include "i2c.h"
- #define EEPROM_ADDR 0xa0
- // at24c256 需要14/15位地址才能表示
- void eeprom_write(uint16_t eeprom_mem_addr, uint8_t* data, uint16_t leng)
- {
- //if(HAL_I2C_Mem_Write_DMA(&hi2c3, EEPROM_ADDR, eeprom_mem_addr, I2C_MEMADD_SIZE_16BIT, data, leng) == HAL_OK)
- if(HAL_I2C_Mem_Write(&hi2c3, EEPROM_ADDR, eeprom_mem_addr, I2C_MEMADD_SIZE_16BIT, data, leng, 2) == HAL_OK)
- {
- printf("write ok\n");
- } else printf("write error\n");
- }
- void eeprom_read(uint16_t eeprom_mem_addr, uint8_t* data, uint16_t leng)
- {
- //if(HAL_I2C_Mem_Read_DMA(&hi2c3, EEPROM_ADDR|0x01, eeprom_mem_addr, I2C_MEMADD_SIZE_16BIT, data, leng) == HAL_OK)
- if(HAL_I2C_Mem_Read(&hi2c3, EEPROM_ADDR|0x01, eeprom_mem_addr, I2C_MEMADD_SIZE_16BIT, data, leng, 2) == HAL_OK)
- {
- printf("read ok\n");
- }
- else
- {
- printf("read error\n");
- switch(hi2c3.ErrorCode)
- {
- case HAL_I2C_ERROR_INVALID_PARAM:
- printf("HAL_I2C_ERROR_INVALID_PARAM\n");
- break;
- case HAL_I2C_ERROR_DMA_PARAM:
- printf("HAL_I2C_ERROR_DMA_PARAM\n");
- break;
- case HAL_I2C_ERROR_DMA:
- printf("HAL_I2C_ERROR_DMA\n");
- break;
- }
- printf("hi2c3.ErrorCod %d\n", hi2c3.ErrorCode);
- }
- }
复制代码 串口输出:
[17:08:16.862]收←◆write ok
[17:08:16.963]收←◆read error
hi2c3.ErrorCod 0
wirte_data: 222
read_data:
[17:08:41.060]收←◆write ok
[17:08:41.163]收←◆read ok
wirte_data: 222
read_data: 222
|
|