yd-pyboard/README.md

497 lines
14 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

## YD-pyBoard核心板简介
YD-pyBoard核心板由源地工作室研发受到官方PYB启发改进而来但是与官方的固件并不兼容造成不兼容的原因由以下情况造成
1. 官方的加速度传感器MMA7660已经停产我们源地更换为KXTJ3-1057所以源代码要修改。
2. 由于官方的核心板SD卡座经过评估和实际测试容易发生失效检查卡座插入的机械结构容易损坏造成SD插入检查失效源地YD-pyBoard新更换的SD卡座性能更加可靠检查更换为高电平检查官方为低电平检查所以代码要修改。
3. 源地YD-pyBoard新添加WS2812灯代码要修改下。
YD-pyBoard核心板与官方的Pyboard V1.1差别详细对比:
| | 官方Pyboard V1.1 | 源地®YD-Pyboard V3.0 |
| ------------ | -------------------- | --------------------------- |
| TF卡座 | SD卡插入识别为低电平 | SD卡插入识别为高电平 |
| RGB灯 | 无 | 有WS2812(MCU的B5) |
| BOOT按键 | 无 | 有方便进入Bootloard模式 |
| USB接口 | MicroUSB老款 | Type-c新款 |
| 加速度传感器 | MMA7660FC已停产 | KXTJ3-1057性能更佳 |
| SWD接口 | 无无法SWD调试 | 有可以SWD调试 |
| 自恢复保险丝 | 无 | 有 |
| 电源指示灯 | 无 | 有 |
## 源地YD-pyBoard核心板mpconfigboard.h
主要修改加速度传感器和SD卡座识别方式等代码mpconfigboard.h代码修改如下
```bash
#define MICROPY_HW_BOARD_NAME "YD-Pyboard V3.0-MPY-1.21.1-2"
#define MICROPY_HW_MCU_NAME "STM32F405RG"
#define MICROPY_HW_HAS_SWITCH (1)
#define MICROPY_HW_HAS_FLASH (1)
#define MICROPY_HW_HAS_KXTJ3 (1)
#define MICROPY_HW_HAS_LCD (1)
#define MICROPY_HW_ENABLE_RNG (1)
#define MICROPY_HW_ENABLE_RTC (1)
#define MICROPY_HW_ENABLE_SERVO (1)
#define MICROPY_HW_ENABLE_DAC (1)
#define MICROPY_HW_ENABLE_USB (1)
#define MICROPY_HW_ENABLE_SDCARD (1)
#define MICROPY_PY_THREAD (1)
// HSE is 12MHz
#define MICROPY_HW_CLK_PLLM (12)
#define MICROPY_HW_CLK_PLLN (336)
#define MICROPY_HW_CLK_PLLP (RCC_PLLP_DIV2)
#define MICROPY_HW_CLK_PLLQ (7)
#define MICROPY_HW_CLK_LAST_FREQ (1)
// The pyboard has a 32kHz crystal for the RTC
#define MICROPY_HW_RTC_USE_LSE (1)
#define MICROPY_HW_RTC_USE_US (0)
#define MICROPY_HW_RTC_USE_CALOUT (1)
// UART config
#define MICROPY_HW_UART1_NAME "XB"
#define MICROPY_HW_UART1_TX (pin_B6)
#define MICROPY_HW_UART1_RX (pin_B7)
#define MICROPY_HW_UART2_TX (pin_A2)
#define MICROPY_HW_UART2_RX (pin_A3)
#define MICROPY_HW_UART2_RTS (pin_A1)
#define MICROPY_HW_UART2_CTS (pin_A0)
#define MICROPY_HW_UART3_NAME "YB"
#define MICROPY_HW_UART3_TX (pin_B10)
#define MICROPY_HW_UART3_RX (pin_B11)
#define MICROPY_HW_UART3_RTS (pin_B14)
#define MICROPY_HW_UART3_CTS (pin_B13)
#define MICROPY_HW_UART4_NAME "XA"
#define MICROPY_HW_UART4_TX (pin_A0)
#define MICROPY_HW_UART4_RX (pin_A1)
#define MICROPY_HW_UART6_NAME "YA"
#define MICROPY_HW_UART6_TX (pin_C6)
#define MICROPY_HW_UART6_RX (pin_C7)
// I2C buses
#define MICROPY_HW_I2C1_NAME "X"
#define MICROPY_HW_I2C1_SCL (pin_B6)
#define MICROPY_HW_I2C1_SDA (pin_B7)
#define MICROPY_HW_I2C2_NAME "Y"
#define MICROPY_HW_I2C2_SCL (pin_B10)
#define MICROPY_HW_I2C2_SDA (pin_B11)
// SPI buses
#define MICROPY_HW_SPI1_NAME "X"
#define MICROPY_HW_SPI1_NSS (pin_A4) // X5
#define MICROPY_HW_SPI1_SCK (pin_A5) // X6
#define MICROPY_HW_SPI1_MISO (pin_A6) // X7
#define MICROPY_HW_SPI1_MOSI (pin_A7) // X8
#define MICROPY_HW_SPI2_NAME "Y"
#define MICROPY_HW_SPI2_NSS (pin_B12) // Y5
#define MICROPY_HW_SPI2_SCK (pin_B13) // Y6
#define MICROPY_HW_SPI2_MISO (pin_B14) // Y7
#define MICROPY_HW_SPI2_MOSI (pin_B15) // Y8
// I2S buses
#define MICROPY_HW_I2S2 (1)
// CAN buses
#define MICROPY_HW_CAN1_NAME "YA"
#define MICROPY_HW_CAN1_TX (pin_B9) // Y4
#define MICROPY_HW_CAN1_RX (pin_B8) // Y3
#define MICROPY_HW_CAN2_NAME "YB"
#define MICROPY_HW_CAN2_TX (pin_B13) // Y6
#define MICROPY_HW_CAN2_RX (pin_B12) // Y5
// USRSW has no pullup or pulldown, and pressing the switch makes the input go low
#define MICROPY_HW_USRSW_PIN (pin_B3)
#define MICROPY_HW_USRSW_PULL (GPIO_PULLUP)
#define MICROPY_HW_USRSW_EXTI_MODE (GPIO_MODE_IT_FALLING)
#define MICROPY_HW_USRSW_PRESSED (0)
// The pyboard has 4 LEDs
#define MICROPY_HW_LED1 (pin_A13) // red
#define MICROPY_HW_LED2 (pin_A14) // green
#define MICROPY_HW_LED3 (pin_A15) // yellow
#define MICROPY_HW_LED4 (pin_B4) // blue
#define MICROPY_HW_LED3_PWM { TIM2, 2, TIM_CHANNEL_1, GPIO_AF1_TIM2 }
#define MICROPY_HW_LED4_PWM { TIM3, 3, TIM_CHANNEL_1, GPIO_AF2_TIM3 }
#define MICROPY_HW_LED_ON(pin) (mp_hal_pin_high(pin))
#define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_low(pin))
// SD card detect switch
#define MICROPY_HW_SDCARD_DETECT_PIN (pin_A8)
#define MICROPY_HW_SDCARD_DETECT_PULL (GPIO_PULLDOWN)
#define MICROPY_HW_SDCARD_DETECT_PRESENT (GPIO_PIN_SET)
// USB config
#define MICROPY_HW_USB_FS (1)
#define MICROPY_HW_USB_VBUS_DETECT_PIN (pin_A9)
// MMA accelerometer config-
// Bootloader configuration (only needed if Mboot is used)
#define MBOOT_I2C_PERIPH_ID 1
#define MBOOT_I2C_SCL (pin_B8)
#define MBOOT_I2C_SDA (pin_B9)
#define MBOOT_I2C_ALTFUNC (4)
```
## 源地YD-pyBoard的pins.csv
修改后的别名表RGB控制以引脚为B5。
| X1 | PA0 |
| ---------- | ----- |
| X2 | PA1 |
| X3 | PA2 |
| X4 | PA3 |
| X5 | PA4 |
| X6 | PA5 |
| X7 | PA6 |
| X8 | PA7 |
| X9 | PB6 |
| X10 | PB7 |
| X11 | PC4 |
| X12 | PC5 |
| X13 | Reset |
| X14 | GND |
| X15 | 3.3V |
| X16 | VIN |
| X17 | PB3 |
| X18 | PC13 |
| X19 | PC0 |
| X20 | PC1 |
| X21 | PC2 |
| X22 | PC3 |
| X23 | A3.3V |
| X24 | AGND |
| Y1 | PC6 |
| Y2 | PC7 |
| Y3 | PB8 |
| Y4 | PB9 |
| Y5 | PB12 |
| Y6 | PB13 |
| Y7 | PB14 |
| Y8 | PB15 |
| Y9 | PB10 |
| Y10 | PB11 |
| Y11 | PB0 |
| Y12 | PB1 |
| Y13 | Reset |
| Y14 | GND |
| Y15 | 3.3V |
| Y16 | VIN |
| SW | PB3 |
| LED_RED | PA13 |
| LED_GREEN | PA14 |
| LED_YELLOW | PA15 |
| LED_BLUE | PB4 |
| MMA_INT | PB2 |
| RGB | PB5 |
| SD_D0 | PC8 |
| SD_D1 | PC9 |
| SD_D2 | PC10 |
| SD_D3 | PC11 |
| SD_CMD | PD2 |
| SD_CK | PC12 |
| SD | PA8 |
| SD_SW | PA8 |
| USB_VBUS | PA9 |
| USB_ID | PA10 |
| USB_DM | PA11 |
| USB_DP | PA12 |
| A0 | PA0 |
| A1 | PA1 |
| A2 | PA2 |
| A3 | PA3 |
| A4 | PA4 |
| A5 | PA5 |
| A6 | PA6 |
| A7 | PA7 |
| B6 | PB6 |
| B7 | PB7 |
| C4 | PC4 |
| C5 | PC5 |
| B3 | PB3 |
| C13 | PC13 |
| C0 | PC0 |
| C1 | PC1 |
| C2 | PC2 |
| C3 | PC3 |
| C6 | PC6 |
| C7 | PC7 |
| B8 | PB8 |
| B9 | PB9 |
| B12 | PB12 |
| B13 | PB13 |
| B14 | PB14 |
| B15 | PB15 |
| B10 | PB10 |
| B11 | PB11 |
| B0 | PB0 |
| B1 | PB1 |
| B3 | PB3 |
| A13 | PA13 |
| A14 | PA14 |
| A15 | PA15 |
| B4 | PB4 |
| B2 | PB2 |
| B5 | PB5 |
| C8 | PC8 |
| C9 | PC9 |
| C10 | PC10 |
| C11 | PC11 |
| D2 | PD2 |
| C12 | PC12 |
| A8 | PA8 |
## 出厂测试代码
这是py测试脚本用户可以放入TF卡或下载到mcu内测试
```
import time
import _thread
from pyb import LED
from pyb import Accel
import neopixel
accel = Accel()
p = machine.Pin.board.RGB
n = neopixel.NeoPixel(p, 1)
def funcA(sec):
time.sleep(sec)
while 1:
LED(1).toggle()
time.sleep(0.5)
LED(2).toggle()
time.sleep(0.5)
LED(3).toggle()
time.sleep(0.5)
LED(4).toggle()
time.sleep(0.5)
def funcC(sec):
time.sleep(sec)
while 1:
if accel.x()>=10:
n[0] = (18, 0, 0)
n.write()
elif accel.x()<=-10:
n[0] = (0, 0, 20)
n.write()
elif accel.x()<10 and accel.y()>-10:
n[0] = (0, 20, 0)
n.write()
time.sleep(0.2)
if __name__ == '__main__':
_thread.start_new_thread(funcA, (0.5,))
_thread.start_new_thread(funcC, (1,))
```
## 下载固件
YD-pyBoard核心板在第一次使用前出厂已经烧录好MPY固件如果遇到某些情况需要烧写固件建议通过usb口烧录这是最简单的下载方式步骤如下
执行按键操作让核心板进入bootloard模式按住boot按键不放在按一下RST按键核心板便进入bootloard模式在bootloaed模式下mcu可以接受USB口下载这时候推荐使用STM32CubeProgrammer下载。
## 基本例程
摘录与micropython快速入门手册详细内容看登录官方网站micropython.org
基本操作
```
import pyb
pyb.repl_uart(pyb.UART(1, 9600)) # duplicate REPL on UART(1)
pyb.wfi() # pause CPU, waiting for interrupt
pyb.freq() # get CPU and bus frequencies
pyb.freq(60000000) # set CPU freq to 60MHz
pyb.stop() # stop CPU, waiting for external interrupt
```
延时和定时器
```
import time
time.sleep(1) # sleep for 1 second
time.sleep_ms(500) # sleep for 500 milliseconds
time.sleep_us(10) # sleep for 10 microseconds
start = time.ticks_ms() # get value of millisecond counter
delta = time.ticks_diff(time.ticks_ms(), start) # compute time difference
```
板载LED
```
from pyb import LED
led = LED(1) # 1=red, 2=green, 3=yellow, 4=blue
led.toggle()
led.on()
led.off()
# LEDs 3 and 4 support PWM intensity (0-255)
LED(4).intensity() # get intensity
LED(4).intensity(128) # set intensity to half
```
板载按键
```
from pyb import Switch
sw = Switch()
sw.value() # returns True or False
sw.callback(lambda: pyb.LED(1).toggle())
```
引脚和GPIO
```
from pyb import Pin
p_out = Pin('X1', Pin.OUT_PP)
p_out.high()
p_out.low()
p_in = Pin('X2', Pin.IN, Pin.PULL_UP)
p_in.value() # get value, 0 or 1
```
舵机控制
from pyb import Servo
```
s1 = Servo(1) # servo on position 1 (X1, VIN, GND)
s1.angle(45) # move to 45 degrees
s1.angle(-60, 1500) # move to -60 degrees in 1500ms
s1.speed(50) # for continuous rotation servos
```
定时器
```
from pyb import Timer
tim = Timer(1, freq=1000)
tim.counter() # get counter value
tim.freq(0.5) # 0.5 Hz
tim.callback(lambda t: pyb.LED(1).toggle())
```
RTC (实时时钟)
```
from pyb import RTC
rtc = RTC()
rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time
rtc.datetime() # get date and time
```
PWM
```
from pyb import Pin, Timer
p = Pin('X1') # X1 has TIM2, CH1
tim = Timer(2, freq=1000)
ch = tim.channel(1, Timer.PWM, pin=p)
ch.pulse_width_percent(50)
```
ADC (模数转换)
```
from pyb import Pin, ADC
adc = ADC(Pin('X19'))
adc.read() # read value, 0-4095
```
DAC(数模转换)
from pyb import Pin, DAC
dac = DAC(Pin('X5'))
dac.write(120) # output between 0 and 255
串口
```
from pyb import UART
uart = UART(1, 9600)
uart.write('hello')
uart.read(5) # read up to 5 bytes
```
SPI总线
```
from pyb import SPI
spi = SPI(1, SPI.CONTROLLER, baudrate=200000, polarity=1, phase=0)
spi.send('hello')
spi.recv(5) # receive 5 bytes on the bus
spi.send_recv('hello') # send and receive 5 bytes
```
I2C 软件通信
```
from machine import I2C
i2c = I2C('X', freq=400000) # create hardware I2c object
i2c = I2C(scl='X1', sda='X2', freq=100000) # create software I2C object
i2c.scan() # returns list of peripheral addresses
i2c.writeto(0x42, 'hello') # write 5 bytes to peripheral with address 0x42
i2c.readfrom(0x42, 5) # read 5 bytes from peripheral
i2c.readfrom_mem(0x42, 0x10, 2) # read 2 bytes from peripheral 0x42, peripheral memory 0x10
i2c.writeto_mem(0x42, 0x10, 'xy') # write 2 bytes to peripheral 0x42, peripheral memory 0x10
```
I2C硬件通信
```
from machine import I2S, Pin
i2s = I2S(2, sck=Pin('Y6'), ws=Pin('Y5'), sd=Pin('Y8'), mode=I2S.TX, bits=16, format=I2S.STEREO, rate=44100, ibuf=40000) # create I2S object
i2s.write(buf) # write buffer of audio samples to I2S device
i2s = I2S(1, sck=Pin('X5'), ws=Pin('X6'), sd=Pin('Y4'), mode=I2S.RX, bits=16, format=I2S.MONO, rate=22050, ibuf=40000) # create I2S object
i2s.readinto(buf) # fill buffer with audio samples from I2S device
```
CAN总线控制器局域网
```
from pyb import CAN
can = CAN(1, CAN.LOOPBACK)
can.setfilter(0, CAN.LIST16, 0, (123, 124, 125, 126))
can.send('message!', 123) # send a message with id 123
can.recv(0) # receive message on FIFO 0
```
板载加速器
```
from pyb import Accel
accel = Accel()
print(accel.x(), accel.y(), accel.z())
```