Micropython之开篇–基于F407VE Black F407VE的移植
开篇先写点什么,网上比较热的几个项目:Arduino,Micropython,树莓派,OpenMV,等等,每个项目都有其亮点,
网上也很多Micropython或基于树莓派python的教程,接触python有段时间了,一直想写点什么,就目前来看MDK及IAR更适应开发冷门模块(有很多比较经典的模块供参考),而Micropython参考模块相对较少,所以没有STM32之类开发经验及python语言基础做Micropython底层开发比较吃力,当然,优点很明显做顶层应用小学生都可以入门。
为什么搞Micropython?
1.顶层应用简单,不用很多专业知识就可以
2.Ai 嵌入式人脸识别,颜色识别,颜色追踪经典例程都是python,Micropython
3.Micropython操作方便,只用一根数据线就可以写程序,
4.调好的程序,可以在“编译”过程中添加属于自己的代码,用户可以调用,但无法查看到源码,对科技含量比较高的代码(比如算法等)提供保护。
5.通用性强,对各种模块应用有较强的兼容性(比如SPI,IIC,UART,Can等)
6.方便兼容IOT—ESP8266,ESP32都有Micropython版,会用一个平台,其它操作起来都比较简单
Micropython的缺点:
1.除了Rt thread,没有合适的RTOS与之兼容;
2.有些模块(功能)尚未添加(比如:FSMC Driver 以及DMA,DSP应用等 ),当然Github上会经常爆出国外大牛的模块驱动,多多收集,代码Demo多了自己做驱动才更得心应手。
好了,先描述下项目背景:
无意中在淘宝翻到一块不到30大洋的Micropython开发板–STM32F411CEU6如下图背面支持Flash,刚好把自己手里边剩余的一片25Q128 (16M)焊上,烧写上店家给的外部Flash支持Micropython固件,Type-C数据线插上电脑:赫然出现了一个16M U盘,各种调试ILI9341显示图片是真香。
当然,笔者喜欢收集各种开发板,除了PYB(STM32F405RG)手里单F407系列就有好几块:普中的(STM32F407ZG) 双核开发板,正点原子的(STM32F407ZG)探索者,还有就是今天的主角:Black F407VE,直接上图:
为什么叫选这个呢?1.ROM 512K : 够用 Micropython一般需要ROM 大于290K左右的MCU上才可以跑起来(详见RT-Thread官网关于Micropython移植)2.方便移植 Micropython: 官方库里有F4Discovery例程,只需要修改比较少的参数 3.良心-便宜外设齐全: F407VE据ST官网报价大约在人民币15-20元(步包含关税),带RTC电池,,25Q16 ,内存卡槽,40大洋还包邮,难怪老外都说便宜。
先上个电路原理图,方便待会儿说明micropython的配置:
板子25Q16 用的是SPI1:PB3–SCK PB4–MISO PB5–MOSI PB0–CS Mark一下
LED灯 PA6–LED1 PA7–LED2 这里吐槽下设计(好好的ADC 输入及PWM输出)少了2路 。
然后SD卡没有特别的特别地方:
代码配置如下:此配置可以选择Flash芯片(为了存更多东西,我把板载Flash换成25Q128 16M,当然不换也可以)作为启动还是像PYB一样从内存卡(最大支持32G)启动:
#define MICROPY_HW_BOARD_NAME "Alisa"
#define MICROPY_HW_MCU_NAME "STM32F407"
#define MICROPY_HW_FLASH_FS_LABEL "Alisa"
// 1 = use internal flash (512 KByte)
// 0 = use onboard SPI flash (2 MByte) Winbond W25Q16/128
#define MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE (0)
#define MICROPY_HW_HAS_SWITCH (1)
#define MICROPY_HW_HAS_FLASH (1)
#define MICROPY_HW_HAS_SDCARD (1)
#define MICROPY_HW_ENABLE_RNG (1)
#define MICROPY_HW_ENABLE_RTC (1)
#define MICROPY_HW_ENABLE_DAC (1)
#define MICROPY_HW_ENABLE_USB (1)
// HSE is 8MHz
#define MICROPY_HW_CLK_PLLM (8)
#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 board 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_TX (pin_A9)
#define MICROPY_HW_UART1_RX (pin_A10)
#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_TX (pin_B10)
#define MICROPY_HW_UART3_RX (pin_B11)
#if MICROPY_HW_HAS_SWITCH == 0
// NOTE: A0 also connects to the user switch. To use UART4 you should
// set MICROPY_HW_HAS_SWITCH to 0, and also remove SB20 (on the back
// of the board near the USER switch).
#define MICROPY_HW_UART4_TX (pin_A0)
#define MICROPY_HW_UART4_RX (pin_A1)
#endif
// NOTE: PC7 is connected to MCLK on the Audio chip. This is an input signal
// so I think as long as you're not using the audio chip then it should
// be fine to use as a UART pin.
#define MICROPY_HW_UART6_TX (pin_C6)
#define MICROPY_HW_UART6_RX (pin_C7)
// I2C busses
#define MICROPY_HW_I2C1_SCL (pin_B6)
#define MICROPY_HW_I2C1_SDA (pin_B7)
#define MICROPY_HW_I2C2_SCL (pin_B10)
#define MICROPY_HW_I2C2_SDA (pin_B11)
// SPI busses
#define MICROPY_HW_SPI1_NSS (pin_A4) // PA4
#define MICROPY_HW_SPI1_SCK (pin_A5) // PA5,PB3
#define MICROPY_HW_SPI1_MISO (pin_A6) // PA6,PB4
#define MICROPY_HW_SPI1_MOSI (pin_A7) // PA7,PB5
#define MICROPY_HW_SPI2_NSS (pin_B12) // PB12
#define MICROPY_HW_SPI2_SCK (pin_B13) // PB13
#define MICROPY_HW_SPI2_MISO (pin_B14) // PB14
#define MICROPY_HW_SPI2_MOSI (pin_B15) // PB15
#define MICROPY_HW_SPI3_NSS (pin_A15) // PA15
#define MICROPY_HW_SPI3_SCK (pin_B3) // PB3
#define MICROPY_HW_SPI3_MISO (pin_B4) // PB4
#define MICROPY_HW_SPI3_MOSI (pin_B5) // PB5
// CAN busses
#define MICROPY_HW_CAN1_TX (pin_B9) // PB9,PD1,PA12
#define MICROPY_HW_CAN1_RX (pin_B8) // PB8,PD0,PA11
#define MICROPY_HW_CAN2_TX (pin_B13)
#define MICROPY_HW_CAN2_RX (pin_B12)
// USRSW is pulled low. Pressing the button makes the input go high.
#define MICROPY_HW_USRSW_PIN (pin_A0)
#define MICROPY_HW_USRSW_PULL (GPIO_NOPULL)
#define MICROPY_HW_USRSW_EXTI_MODE (GPIO_MODE_IT_RISING)
#define MICROPY_HW_USRSW_PRESSED (1)
// LEDs
#define MICROPY_HW_LED1 (pin_A6) // red
#define MICROPY_HW_LED2 (pin_A7) // green
#define MICROPY_HW_LED_ON(pin) (mp_hal_pin_low(pin))
#define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_high(pin))
// If using onboard SPI flash
#if !MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE
// Winbond W25Q128 SPI Flash = 16 Mbit (2 MByte)
//如果你用的主板自带的25Q16,要这么设置:
//#define MICROPY_HW_SPIFLASH_SIZE_BITS (16 * 1024 * 1024)
#define MICROPY_HW_SPIFLASH_SIZE_BITS (128 * 1024 * 1024)
#define MICROPY_HW_SPIFLASH_CS (pin_B0)
#define MICROPY_HW_SPIFLASH_SCK (pin_B3)
#define MICROPY_HW_SPIFLASH_MISO (pin_B4)
#define MICROPY_HW_SPIFLASH_MOSI (pin_B5)
#define MICROPY_BOARD_EARLY_INIT Black_F407VE_board_early_init
void Black_F407VE_board_early_init(void);
extern const struct _mp_spiflash_config_t spiflash_config;
extern struct _spi_bdev_t spi_bdev;
#define MICROPY_HW_BDEV_IOCTL(op, arg) ( \
(op) == BDEV_IOCTL_NUM_BLOCKS ? (MICROPY_HW_SPIFLASH_SIZE_BITS / 8 / FLASH_BLOCK_SIZE) : \
(op) == BDEV_IOCTL_INIT ? spi_bdev_ioctl(&spi_bdev, (op), (uint32_t)&spiflash_config) : \
spi_bdev_ioctl(&spi_bdev, (op), (arg)) \
)
#define MICROPY_HW_BDEV_READBLOCKS(dest, bl, n) spi_bdev_readblocks(&spi_bdev, (dest), (bl), (n))
#define MICROPY_HW_BDEV_WRITEBLOCKS(src, bl, n) spi_bdev_writeblocks(&spi_bdev, (src), (bl), (n))
#endif
// SD card detect switch
#define MICROPY_HW_SDCARD_DETECT_PIN (pin_E2)
#define MICROPY_HW_SDCARD_DETECT_PULL (GPIO_NOPULL)
#define MICROPY_HW_SDCARD_DETECT_PRESENT (GPIO_PIN_RESET)
// USB config
#define MICROPY_HW_USB_FS (1)
#define MICROPY_HW_USB_VBUS_DETECT_PIN (pin_A9)
#define MICROPY_HW_USB_OTG_ID_PIN (pin_A10)
文件(包含生成的.dfu,.hex)已上传CSDN平台:
https://download.csdn.net/download/leotian966/12439502
跳线帽短接E2与GND 可以更改为从内存卡启动需要用。
如果觉得帖子好,请给个关注,谢谢!
后续帖子会演示此主板的使用Demo,敬请关注。