Funpack第11期-LPC55S69 Micropython移植
使用LPC55S69-EVK移植Micropython,并实现串口通信和IO控制
标签
嵌入式系统
DayDay
更新2021-11-02
1070

      本次Funpack选作题目4,在LPC55S69-EVK上移植Micropyhon并实现串口通信和IO控制。

      Micropython是Python解释器的一个简化版本,使Python可以运行在单片机中。

      移植Micropython到一个开发板上,在其网站上有简要的教程,http://docs.micropython.org/en/latest/develop/porting.html

      简单来说,就是开发板启动后,先对芯片进行基本初始化,比如串口,IO,然后运行mp提供的main函数流程,同时实现mp要求的终端(串口)收发函数,再创建自定义模块,将C语言函数包装,形成可以在Python中调用的对象、方法。

      在mp main函数一开始先进行lpc55s69的初始化:

     

void BOARD_InitPins(void)
{
    /* Enables the clock for the I/O controller.: Enable Clock. */
    CLOCK_EnableClock(kCLOCK_Iocon);

    const uint32_t port0_pin29_config = (/* Pin is configured as FC0_RXD_SDA_MOSI_DATA */
                                         IOCON_PIO_FUNC1 |
                                         /* No addition pin function */
                                         IOCON_PIO_MODE_INACT |
                                         /* Standard mode, output slew rate control is enabled */
                                         IOCON_PIO_SLEW_STANDARD |
                                         /* Input function is not inverted */
                                         IOCON_PIO_INV_DI |
                                         /* Enables digital function */
                                         IOCON_PIO_DIGITAL_EN |
                                         /* Open drain is disabled */
                                         IOCON_PIO_OPENDRAIN_DI);
    /* PORT0 PIN29 (coords: 92) is configured as FC0_RXD_SDA_MOSI_DATA */
    IOCON_PinMuxSet(IOCON, 0U, 29U, port0_pin29_config);

    const uint32_t port0_pin30_config = (/* Pin is configured as FC0_TXD_SCL_MISO_WS */
                                         IOCON_PIO_FUNC1 |
                                         /* No addition pin function */
                                         IOCON_PIO_MODE_INACT |
                                         /* Standard mode, output slew rate control is enabled */
                                         IOCON_PIO_SLEW_STANDARD |
                                         /* Input function is not inverted */
                                         IOCON_PIO_INV_DI |
                                         /* Enables digital function */
                                         IOCON_PIO_DIGITAL_EN |
                                         /* Open drain is disabled */
                                         IOCON_PIO_OPENDRAIN_DI);
    /* PORT0 PIN30 (coords: 94) is configured as FC0_TXD_SCL_MISO_WS */
    IOCON_PinMuxSet(IOCON, 0U, 30U, port0_pin30_config);

    /* Enables the clock for the GPIO1 module */
    CLOCK_EnableClock(kCLOCK_Gpio1);

    gpio_pin_config_t LED_BULE_config = {
        .pinDirection = kGPIO_DigitalOutput,
        .outputLogic = 0U
    };
    /* Initialize GPIO functionality on pin PIO1_4 (pin 1)  */
    GPIO_PinInit(BOARD_LED_BULE_GPIO, BOARD_LED_BULE_PORT, BOARD_LED_BULE_PIN, &LED_BULE_config);

    const uint32_t LED_BULE = (/* Pin is configured as PIO1_4 */
                               IOCON_PIO_FUNC0 |
                               /* Selects pull-up function */
                               IOCON_PIO_MODE_PULLUP |
                               /* Standard mode, output slew rate control is enabled */
                               IOCON_PIO_SLEW_STANDARD |
                               /* Input function is not inverted */
                               IOCON_PIO_INV_DI |
                               /* Enables digital function */
                               IOCON_PIO_DIGITAL_EN |
                               /* Open drain is disabled */
                               IOCON_PIO_OPENDRAIN_DI);
    /* PORT1 PIN4 (coords: 1) is configured as PIO1_4 */
    IOCON_PinMuxSet(IOCON, BOARD_LED_BULE_PORT, BOARD_LED_BULE_PIN, LED_BULE);
}

static void lpc55s69_init()
{
    usart_config_t config;

    /* set BOD VBAT level to 1.65V */
    POWER_SetBodVbatLevel(kPOWER_BodVbatLevel1650mv, kPOWER_BodHystLevel50mv, false);
    /* attach 12 MHz clock to FLEXCOMM0 (debug console) */
    CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);

    BOARD_InitBootPins();
    BOARD_InitBootClocks();
    BOARD_InitDebugConsole();
    /*
     * config.baudRate_Bps = 115200U;
     * config.parityMode = kUSART_ParityDisabled;
     * config.stopBitCount = kUSART_OneStopBit;
     * config.loopback = false;
     * config.enableTx = false;
     * config.enableRx = false;
     */
    USART_GetDefaultConfig(&config);
    config.baudRate_Bps = BOARD_DEBUG_UART_BAUDRATE;
    config.enableTx     = true;
    config.enableRx     = true;

    USART_Init(DEMO_USART, &config, DEMO_USART_CLK_FREQ);
}

      将串口收发函数对接到mp终端的stdin和stdout上:

     

// Receive single character
int mp_hal_stdin_rx_chr(void) {
    unsigned char c = 0;
    USART_ReadBlocking(DEMO_USART, &c, 1);
    return c;
}

// Send string of given length
void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
    USART_WriteBlocking(DEMO_USART, (const uint8_t *)str, len);

}

     实现自定义模块LED,控制板载LED开关:

// This is the function which will be called from Python as cexample.add_ints(a, b).
STATIC mp_obj_t  off() {
    GPIO_PortSet(GPIO, BOARD_LED_PORT, 1u << BOARD_LED_PIN);
    return mp_const_none;
}

STATIC mp_obj_t  on() {
    GPIO_PortClear(GPIO, BOARD_LED_PORT, 1u << BOARD_LED_PIN);
    return mp_const_none;
}

STATIC MP_DEFINE_CONST_FUN_OBJ_0(on_obj, on);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(off_obj, off);

// Define all properties of the module.
// Table entries are key/value pairs of the attribute name (a string)
// and the MicroPython object reference.
// All identifiers and strings are written as MP_QSTR_xxx and will be
// optimized to word-sized integers by the build system (interned strings).
STATIC const mp_rom_map_elem_t led_module_globals_table[] = {
    { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_led) },
    { MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&on_obj) },
    { MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&off_obj) },

};
STATIC MP_DEFINE_CONST_DICT(led_module_globals, led_module_globals_table);

// Define module object.
const mp_obj_module_t led_user_cmodule = {
    .base = { &mp_type_module },
    .globals = (mp_obj_dict_t *)&led_module_globals,
};

// Register the module to make it available in Python.
MP_REGISTER_MODULE(MP_QSTR_led, led_user_cmodule, 1);

 

     程序编译烧录后,打开一个串口中断,重启开发板,可以看到Micropython启动信息,导入LED模块,可以控制led开关:

Ft9BwX_zI2rj45ILQOIK4_40Mm_B

     灯亮:

Flz-xmcFEhLjmgJfSCrAkp6qINGM

     灯灭:

FrPaHA0ndD4juswEd5TR-0P0LSol

 

     借助这个任务,进一步学习了Micropython的移植。感谢Funpack活动,希望最后一期更精彩。

 

     附件是本次移植的完整代码,将其解压至mp源码目录ports文件夹下,进入其文件夹,编译,即可得到mp固件。

附件下载
lpcxpresso55s69_usart_polling.7z
团队介绍
张新天 芯片厂商的软件工程师
评论
0 / 100
查看更多
目录
硬禾服务号
关注最新动态
0512-67862536
info@eetree.cn
江苏省苏州市苏州工业园区新平街388号腾飞创新园A2幢815室
苏州硬禾信息科技有限公司
Copyright © 2024 苏州硬禾信息科技有限公司 All Rights Reserved 苏ICP备19040198号