McuStudioMcuStudio
指南
开发者
组件
API
  • 版本发布
指南
开发者
组件
API
  • 版本发布
  • 概述
  • 快速开始
  • 添加厂商的芯片到平台
  • 芯片模板教程

    • 芯片模板开发教程
    • 创建引脚封装
    • 配置时钟树
    • 配置 GPIO 外设
    • 配置 ADC 外设
    • 配置通信外设
    • 配置 TIM 外设
  • 开发参考

    • 框架与规则
    • 芯片包结构
    • 芯片描述
    • 芯片包配置
    • 引脚配置详解
    • 外设配置详解
    • 外设元素参考
    • GPIO 开发参考
    • ADC 开发参考
    • TIM 开发参考
    • 固件库开发指南
    • 版本管理机制
    • Define 映射指南
    • 多语言(i18n)

配置通信外设

本教程将引导您完成 McuStudio 芯片模板中最常用的三种通信外设配置:USART(串口)、SPI(串行外设接口)和 I2C(集成电路总线)。

USART 配置

USART(Universal Synchronous/Asynchronous Receiver/Transmitter)是最常用的通信外设,用于芯片与外部设备之间的异步串行通信。

参数配置

// USART 参数配置示例
USART: {
  // 波特率
  BaudRate: {
    label: 'Baud Rate',
    type: 'input',
    value: '115200',
    placeholder: 'e.g. 9600, 115200'
  },

  // 数据位
  WordLength: {
    label: 'Word Length',
    type: 'select',
    value: '8',
    options: [
      { label: '8 Bits', value: '8' },
      { label: '9 Bits', value: '9' }
    ]
  },

  // 停止位
  StopBits: {
    label: 'Stop Bits',
    type: 'select',
    value: '1',
    options: [
      { label: '1 Stop Bit', value: '1' },
      { label: '0.5 Stop Bit', value: '0.5' },
      { label: '2 Stop Bits', value: '2' },
      { label: '1.5 Stop Bits', value: '1.5' }
    ]
  },

  // 校验位
  Parity: {
    label: 'Parity',
    type: 'select',
    value: 'none',
    options: [
      { label: 'None', value: 'none' },
      { label: 'Even', value: 'even' },
      { label: 'Odd', value: 'odd' }
    ]
  },

  // 硬件流控制
  HWFlowControl: {
    label: 'Hardware Flow Control',
    type: 'select',
    value: 'none',
    options: [
      { label: 'None', value: 'none' },
      { label: 'RTS', value: 'rts' },
      { label: 'CTS', value: 'cts' },
      { label: 'RTS/CTS', value: 'rts_cts' }
    ]
  },

  // 工作模式
  Mode: {
    label: 'Mode',
    type: 'select',
    value: 'tx_rx',
    options: [
      { label: 'TX Only', value: 'tx' },
      { label: 'RX Only', value: 'rx' },
      { label: 'TX + RX', value: 'tx_rx' }
    ]
  }
}

代码生成示例

基于上述配置,生成的初始化代码如下:

// USART 初始化代码
USART_InitStructure.BaudRate = 115200;
USART_InitStructure.WordLength = USART_WL_8BIT;
USART_InitStructure.StopBits = USART_STB_1BIT;
USART_InitStructure.Parity = USART_PARITY_NONE;
USART_InitStructure.HWFlowControl = USART_HWFLOW_NONE;
USART_InitStructure.Mode = USART_MODE_TX_RX;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);

SPI 配置

SPI(Serial Peripheral Interface)是一种高速全双工同步串行通信总线。

参数配置

// SPI 参数配置示例
SPI: {
  // 主从模式
  Mode: {
    label: 'SPI Mode',
    type: 'select',
    value: 'master',
    options: [
      { label: 'Master', value: 'master' },
      { label: 'Slave', value: 'slave' }
    ]
  },

  // 数据大小
  DataSize: {
    label: 'Data Size',
    type: 'select',
    value: '8',
    options: [
      { label: '8 Bits', value: '8' },
      { label: '16 Bits', value: '16' }
    ]
  },

  // 时钟极性 (CPOL)
  CPOL: {
    label: 'Clock Polarity (CPOL)',
    type: 'select',
    value: 'low',
    options: [
      { label: 'Low (CPOL=0)', value: 'low' },
      { label: 'High (CPOL=1)', value: 'high' }
    ]
  },

  // 时钟相位 (CPHA)
  CPHA: {
    label: 'Clock Phase (CPHA)',
    type: 'select',
    value: '1edge',
    options: [
      { label: '1st Edge (CPHA=0)', value: '1edge' },
      { label: '2nd Edge (CPHA=1)', value: '2edge' }
    ]
  },

  // 波特率预分频
  BaudRatePrescaler: {
    label: 'Baud Rate Prescaler',
    type: 'select',
    value: '2',
    options: [
      { label: 'fPCLK/2', value: '2' },
      { label: 'fPCLK/4', value: '4' },
      { label: 'fPCLK/8', value: '8' },
      { label: 'fPCLK/16', value: '16' },
      { label: 'fPCLK/32', value: '32' },
      { label: 'fPCLK/64', value: '64' },
      { label: 'fPCLK/128', value: '128' },
      { label: 'fPCLK/256', value: '256' }
    ]
  },

  // NSS 管理
  NSS: {
    label: 'NSS Mode',
    type: 'select',
    value: 'software',
    options: [
      { label: 'Software', value: 'software' },
      { label: 'Hardware', value: 'hardware' }
    ]
  },

  // 数据帧格式
  FirstBit: {
    label: 'Frame Format',
    type: 'select',
    value: 'msb',
    options: [
      { label: 'MSB First', value: 'msb' },
      { label: 'LSB First', value: 'lsb' }
    ]
  }
}

SPI 模式组合

CPOL 和 CPHA 的组合定义了 SPI 的四种工作模式:

模式CPOLCPHA说明
Mode 000空闲低,第一边沿采样
Mode 101空闲低,第二边沿采样
Mode 210空闲高,第一边沿采样
Mode 311空闲高,第二边沿采样

I2C 配置

I2C(Inter-Integrated Circuit)是一种双线同步串行总线,广泛用于连接传感器和外围设备。

参数配置

// I2C 参数配置示例
I2C: {
  // 速度模式
  I2CSpeedMode: {
    label: 'I2C Speed Mode',
    type: 'select',
    value: 'StandardMode',
    options: [
      { label: 'Standard Mode (100kHz)', value: 'StandardMode' },
      { label: 'Fast Mode (400kHz)', value: 'FastMode' }
    ]
  },

  // 时钟频率
  ClockSpeed: {
    label: 'I2C Speed Frequency (Hz)',
    type: 'input',
    value: '100000',
    min: 0,
    max: 400000
  },

  // 寻址模式
  AddressingMode: {
    label: 'Addressing Mode',
    type: 'select',
    value: '7bit',
    options: [
      { label: '7-bit', value: '7bit' },
      { label: '10-bit', value: '10bit' }
    ]
  },

  // 广播呼叫
  GeneralCall: {
    label: 'General Call',
    type: 'select',
    value: 'disable',
    options: [
      { label: 'Enable', value: 'enable' },
      { label: 'Disable', value: 'disable' }
    ]
  },

  // 时钟拉长
  ClockStretching: {
    label: 'Clock Stretching',
    type: 'select',
    value: 'enable',
    options: [
      { label: 'Enable', value: 'enable' },
      { label: 'Disable', value: 'disable' }
    ]
  }
}

I2C 地址计算

在配置 I2C 从设备地址时,需要注意:

地址格式

  • 7 位地址模式:从设备地址为 7 位,最高位为读写位
  • 10 位地址模式:使用两个字节传输地址
  • 部分芯片的 SDK 中地址需要左移 1 位(如 0x50 → 0xA0)

NVIC 中断配置

每个通信外设通常都需要配置中断:

// NVIC 中断配置
NVIC: {
  USART1_IRQn: {
    label: 'USART1 Interrupt',
    type: 'checkbox',
    value: true,
    // 中断优先级
    PreemptionPriority: {
      label: 'Preemption Priority',
      type: 'select',
      value: '0',
      options: [
        { label: '0 (Highest)', value: '0' },
        { label: '1', value: '1' },
        { label: '2', value: '2' },
        { label: '3 (Lowest)', value: '3' }
      ]
    }
  }
}

DMA 集成

通信外设通常需要 DMA 来减少 CPU 负担:

// DMA 配置(以 USART 为例)
DMA: {
  // DMA TX 通道
  TX: {
    label: 'DMA TX Channel',
    type: 'select',
    value: 'none',
    options: [
      { label: 'None', value: 'none' },
      { label: 'DMA1 Channel 4', value: 'DMA1_CH4' },
      { label: 'DMA1 Channel 7', value: 'DMA1_CH7' }
    ]
  },

  // DMA RX 通道
  RX: {
    label: 'DMA RX Channel',
    type: 'select',
    value: 'none',
    options: [
      { label: 'None', value: 'none' },
      { label: 'DMA1 Channel 5', value: 'DMA1_CH5' }
    ]
  }
}

外设配置最佳实践

  1. 参数验证:使用 min/max 属性限制输入范围
  2. 默认值设置:为常用配置提供合理的默认值
  3. 模式互斥:使用 disabled 条件防止矛盾配置
  4. 条件可见性:使用 visible 条件按需显示相关参数
  5. 固件适配:确保配置值与 SDK 宏定义一致

下一步

  • 查阅 外设配置详解 — 了解外设面板和元素的详细配置
  • 查阅 外设元素参考 — 查看所有可用 UI 元素类型和语法
  • 查阅 固件库开发指南 — 了解 HAL/LL 模板开发
Prev
配置 ADC 外设
Next
配置 TIM 外设