diff --git a/examples/tos_meets_rust/.gitignore b/examples/tos_meets_rust/.gitignore new file mode 100644 index 00000000..f8c6f120 --- /dev/null +++ b/examples/tos_meets_rust/.gitignore @@ -0,0 +1,31 @@ + .svn +*.pyc +.settings +.project +.pydevproject +.leaderf +.DS_Store +*.log +*.out +*.aux +*.toc +*.fdb_latexmk +*.fls +*.synctex.gz +*.xdv +*.ipynb_checkpoints +*.pyc +build +draft +out +nohup.out +target +node_modules +npm-* +dist +tmp +__pycache__ +# Ignore user specific files in Xcode Project +xcuserdata +user_config.h +.ccls-cache diff --git a/examples/tos_meets_rust/BSP/.mxproject b/examples/tos_meets_rust/BSP/.mxproject new file mode 100644 index 00000000..58441f9d --- /dev/null +++ b/examples/tos_meets_rust/BSP/.mxproject @@ -0,0 +1,19 @@ +[PreviousGenFiles] +HeaderPath=/Users/mybayswang/develop/tencentos-tiny/TencentOS_tiny_EVB_G0-cmake/BSP/Inc +HeaderFiles=gpio.h;adc.h;i2c.h;spi.h;sys.h;usart.h;stm32g0xx_it.h;stm32g0xx_hal_conf.h;main.h; +SourcePath=/Users/mybayswang/develop/tencentos-tiny/TencentOS_tiny_EVB_G0-cmake/BSP/Src +SourceFiles=gpio.c;adc.c;i2c.c;spi.c;sys.c;usart.c;stm32g0xx_it.c;stm32g0xx_hal_msp.c;main.c; + +[PreviousLibFiles] +LibFiles=Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_gpio.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_gpio_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_adc.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_adc_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_adc.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_rcc.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_rcc_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_rcc.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_flash.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_flash_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_flash_ramfunc.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_dma.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_dma_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_pwr.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_pwr_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_cortex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_def.h;Drivers/STM32G0xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_exti.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_i2c.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_i2c_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_spi.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_spi_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_tim.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_tim_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_system.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_uart.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_uart_ex.h;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_gpio.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_adc.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_adc_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_ll_adc.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_ll_rcc.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_cortex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_exti.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_i2c.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_i2c_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_spi.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_spi_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_tim.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_tim_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart_ex.c;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_gpio.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_gpio_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_adc.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_adc_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_adc.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_rcc.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_rcc_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_rcc.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_flash.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_flash_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_flash_ramfunc.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_dma.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_dma_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_pwr.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_pwr_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_cortex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_def.h;Drivers/STM32G0xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_exti.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_i2c.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_i2c_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_spi.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_spi_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_tim.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_tim_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_system.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_uart.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_uart_ex.h;Drivers/CMSIS/Device/ST/STM32G0xx/Include/stm32g070xx.h;Drivers/CMSIS/Device/ST/STM32G0xx/Include/stm32g0xx.h;Drivers/CMSIS/Device/ST/STM32G0xx/Include/system_stm32g0xx.h;Drivers/CMSIS/Device/ST/STM32G0xx/Source/Templates/system_stm32g0xx.c;Drivers/CMSIS/Include/core_cm7.h;Drivers/CMSIS/Include/tz_context.h;Drivers/CMSIS/Include/core_cm3.h;Drivers/CMSIS/Include/cmsis_compiler.h;Drivers/CMSIS/Include/cmsis_armclang.h;Drivers/CMSIS/Include/mpu_armv7.h;Drivers/CMSIS/Include/cmsis_armcc.h;Drivers/CMSIS/Include/core_cm4.h;Drivers/CMSIS/Include/core_cm0.h;Drivers/CMSIS/Include/cmsis_iccarm.h;Drivers/CMSIS/Include/core_armv8mml.h;Drivers/CMSIS/Include/core_sc000.h;Drivers/CMSIS/Include/core_cm1.h;Drivers/CMSIS/Include/mpu_armv8.h;Drivers/CMSIS/Include/core_sc300.h;Drivers/CMSIS/Include/cmsis_gcc.h;Drivers/CMSIS/Include/cmsis_version.h;Drivers/CMSIS/Include/core_cm23.h;Drivers/CMSIS/Include/core_cm33.h;Drivers/CMSIS/Include/core_cm0plus.h;Drivers/CMSIS/Include/core_armv8mbl.h; + +[PreviousUsedKeilFiles] +SourceFiles=..\Src\main.c;..\Src\gpio.c;..\Src\adc.c;..\Src\i2c.c;..\Src\spi.c;..\Src\sys.c;..\Src\usart.c;..\Src\stm32g0xx_it.c;..\Src\stm32g0xx_hal_msp.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_gpio.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_adc.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_adc_ex.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_ll_adc.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc_ex.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_ll_rcc.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash_ex.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma_ex.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr_ex.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_cortex.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_exti.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_i2c.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_i2c_ex.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_spi.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_spi_ex.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_tim.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_tim_ex.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart_ex.c;..\\Src/system_stm32g0xx.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_gpio.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_adc.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_adc_ex.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_ll_adc.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc_ex.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_ll_rcc.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash_ex.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma_ex.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr_ex.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_cortex.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_exti.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_i2c.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_i2c_ex.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_spi.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_spi_ex.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_tim.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_tim_ex.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart.c;..\Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart_ex.c;..\\Src/system_stm32g0xx.c;..\Drivers/CMSIS/Device/ST/STM32G0xx/Source/Templates/system_stm32g0xx.c;; +HeaderPath=..\Drivers\STM32G0xx_HAL_Driver\Inc;..\Drivers\STM32G0xx_HAL_Driver\Inc\Legacy;..\Drivers\CMSIS\Device\ST\STM32G0xx\Include;..\Drivers\CMSIS\Include;..\Inc; +CDefines=USE_HAL_DRIVER;STM32G070xx;USE_HAL_DRIVER;USE_HAL_DRIVER; + +[PreviousUsedMakefileFiles] +SourceFiles=Src/main.c;Src/gpio.c;Src/adc.c;Src/i2c.c;Src/spi.c;Src/sys.c;Src/usart.c;Src/stm32g0xx_it.c;Src/stm32g0xx_hal_msp.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_gpio.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_adc.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_adc_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_ll_adc.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_ll_rcc.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_cortex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_exti.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_i2c.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_i2c_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_spi.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_spi_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_tim.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_tim_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart_ex.c;Src/system_stm32g0xx.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_gpio.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_adc.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_adc_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_ll_adc.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_ll_rcc.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_cortex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_exti.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_i2c.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_i2c_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_spi.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_spi_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_tim.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_tim_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart_ex.c;Src/system_stm32g0xx.c;Drivers/CMSIS/Device/ST/STM32G0xx/Source/Templates/system_stm32g0xx.c;; +HeaderPath=Drivers/STM32G0xx_HAL_Driver/Inc;Drivers/STM32G0xx_HAL_Driver/Inc/Legacy;Drivers/CMSIS/Device/ST/STM32G0xx/Include;Drivers/CMSIS/Include;Inc; +CDefines=USE_HAL_DRIVER;STM32G070xx;USE_HAL_DRIVER;USE_HAL_DRIVER; + diff --git a/examples/tos_meets_rust/BSP/Hardware/CH20/ch20_parser.c b/examples/tos_meets_rust/BSP/Hardware/CH20/ch20_parser.c new file mode 100644 index 00000000..97d2c693 --- /dev/null +++ b/examples/tos_meets_rust/BSP/Hardware/CH20/ch20_parser.c @@ -0,0 +1,202 @@ +/*---------------------------------------------------------------------------- + * Tencent is pleased to support the open source community by making TencentOS + * available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * If you have downloaded a copy of the TencentOS binary from Tencent, please + * note that the TencentOS binary is licensed under the BSD 3-Clause License. + * + * If you have downloaded a copy of the TencentOS source code from Tencent, + * please note that TencentOS source code is licensed under the BSD 3-Clause + * License, except for the third-party components listed below which are + * subject to different license terms. Your integration of TencentOS into your + * own projects may require compliance with the BSD 3-Clause License, as well + * as the other licenses applicable to the third-party components included + * within TencentOS. + *---------------------------------------------------------------------------*/ +#include "ch20_parser.h" + +static ch20_parser_ctrl_t ch20_parser_ctrl; + +static k_stack_t ch20_parser_task_stack[CH20_PARSER_TASK_STACK_SIZE]; + +static uint8_t ch20_parser_buffer[CH20_PARSER_BUFFER_SIZE]; + +/** + * @brief 向ch20解析器中送入一个字节数据 + * @param data 送入的数据 + * @retval none + * @note 需要用户在串口中断函数中手动调用 +*/ +void ch20_parser_input_byte(uint8_t data) +{ + if (tos_chr_fifo_push(&ch20_parser_ctrl.parser_rx_fifo, data) == K_ERR_NONE) { + /* 送入数据成功,释放信号量,计数 */ + tos_sem_post(&ch20_parser_ctrl.parser_rx_sem); + } +} + +/** + * @brief ch20解析器从chr fifo中取出一个字节数据 + * @param none + * @retval 正常返回读取数据,错误返回-1 +*/ +static int ch20_parser_getchar(void) +{ + uint8_t chr; + k_err_t err; + + /* 永久等待信号量,信号量为空表示chr fifo中无数据 */ + if (tos_sem_pend(&ch20_parser_ctrl.parser_rx_sem, TOS_TIME_FOREVER) != K_ERR_NONE) { + return -1; + } + + /* 从chr fifo中取出数据 */ + err = tos_chr_fifo_pop(&ch20_parser_ctrl.parser_rx_fifo, &chr); + + return err == K_ERR_NONE ? chr : -1; +} + +/** + * @brief ch20读取传感器原始数据并解析 + * @param void + * @retval 解析成功返回0,解析失败返回-1 +*/ +static int ch20_parser_read_raw_data(ch20_data_t *ch20_data) +{ + uint8_t data; + uint8_t data_h, data_l; + uint8_t check_sum_cal = 0x17; + + /* 读取气体浓度单位 */ + data = ch20_parser_getchar(); + if (data != 0x04) { + return -1; + } + CH20_DEBUG_LOG("--->[%#02x]\r\n", data); + check_sum_cal += data; + + /* 读取小数位数 */ + data = ch20_parser_getchar(); + if (data != 0x00) { + return -1; + } + CH20_DEBUG_LOG("--->[%#02x]\r\n", data); + check_sum_cal += data; + + /* 读取气体浓度高位 */ + data = ch20_parser_getchar(); + if (data == 0xFF) { + return -1; + } + CH20_DEBUG_LOG("--->[%#02x]\r\n", data); + data_h = data; + check_sum_cal += data; + + /* 读取气体浓度低位 */ + data = ch20_parser_getchar(); + if (data == 0xFF) { + return -1; + } + CH20_DEBUG_LOG("--->[%#02x]\r\n", data); + data_l = data; + check_sum_cal += data; + + /* 读取满量程高位 */ + data = ch20_parser_getchar(); + if (data != 0x07) { + return -1; + } + CH20_DEBUG_LOG("--->[%#02x]\r\n", data); + check_sum_cal += data; + + /* 读取满量程低位 */ + data = ch20_parser_getchar(); + if (data != 0xD0) { + return -1; + } + CH20_DEBUG_LOG("--->[%#02x]\r\n", data); + check_sum_cal += data; + + /* 和校验 */ + data = ch20_parser_getchar(); + CH20_DEBUG_LOG("--->[%#02x]\r\n", data); + check_sum_cal = ~(check_sum_cal) + 1; + CH20_DEBUG_LOG("check_sum_cal is 0x%02x \r\n", check_sum_cal); + if (check_sum_cal != data) { + return -1; + } + + /* 存储数据 */ + ch20_data->data = (data_h << 8) + data_l; + CH20_DEBUG_LOG("ch20_data->data is 0x%04x\r\n", ch20_data->data); + + return 0; +} + +extern k_mail_q_t mail_q; +ch20_data_t ch20_data; + +/** + * @brief ch20解析器任务 +*/ +static void ch20_parser_task_entry(void *arg) +{ + int chr, last_chr = 0; + + while (1) { + + chr = ch20_parser_getchar(); + if (chr < 0) { + printf("parser task get char fail!\r\n"); + continue; + } + + if (chr == 0x17 && last_chr == 0xFF) { + /* 解析到包头 */ + if (0 == ch20_parser_read_raw_data(&ch20_data)) { + /* 正常解析之后通过邮箱发送 */ + tos_mail_q_post(&mail_q, &ch20_data, sizeof(ch20_data_t)); + } + } + + last_chr = chr; + } +} + +/** + * @brief 初始化ch20解析器 + * @param none + * @retval 全部创建成功返回0,任何一个创建失败则返回-1 +*/ +int ch20_parser_init(void) +{ + k_err_t ret; + + memset((ch20_parser_ctrl_t*)&ch20_parser_ctrl, 0, sizeof(ch20_parser_ctrl)); + + /* 创建 chr fifo */ + ret = tos_chr_fifo_create(&ch20_parser_ctrl.parser_rx_fifo, ch20_parser_buffer, sizeof(ch20_parser_buffer)); + if (ret != K_ERR_NONE) { + printf("ch20 parser chr fifo create fail, ret = %d\r\n", ret); + return -1; + } + + /* 创建信号量 */ + ret = tos_sem_create(&ch20_parser_ctrl.parser_rx_sem, 0); + if (ret != K_ERR_NONE) { + printf("ch20 parser_rx_sem create fail, ret = %d\r\n", ret); + return -1; + } + + /* 创建线程 */ + ret = tos_task_create(&ch20_parser_ctrl.parser_task, "ch20_parser_task", + ch20_parser_task_entry, NULL, CH20_PARSER_TASK_PRIO, + ch20_parser_task_stack,CH20_PARSER_TASK_STACK_SIZE,0); + if (ret != K_ERR_NONE) { + printf("ch20 parser task create fail, ret = %d\r\n", ret); + return -1; + } + + return 0; +} diff --git a/examples/tos_meets_rust/BSP/Hardware/CH20/ch20_parser.h b/examples/tos_meets_rust/BSP/Hardware/CH20/ch20_parser.h new file mode 100644 index 00000000..79306d48 --- /dev/null +++ b/examples/tos_meets_rust/BSP/Hardware/CH20/ch20_parser.h @@ -0,0 +1,59 @@ +/*---------------------------------------------------------------------------- + * Tencent is pleased to support the open source community by making TencentOS + * available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * If you have downloaded a copy of the TencentOS binary from Tencent, please + * note that the TencentOS binary is licensed under the BSD 3-Clause License. + * + * If you have downloaded a copy of the TencentOS source code from Tencent, + * please note that TencentOS source code is licensed under the BSD 3-Clause + * License, except for the third-party components listed below which are + * subject to different license terms. Your integration of TencentOS into your + * own projects may require compliance with the BSD 3-Clause License, as well + * as the other licenses applicable to the third-party components included + * within TencentOS. + *---------------------------------------------------------------------------*/ +#ifndef _CH20_PARSER_H_ +#define _CH20_PARSER_H_ + +#include "tos_k.h" + +/* 使能是否开启调试日志 */ +#define CH20_DEBUG_LOG_EN 0 + +/* CH20 parser config */ +#define CH20_PARSER_TASK_STACK_SIZE 512 +#define CH20_PARSER_TASK_PRIO 5 +#define CH20_PARSER_BUFFER_SIZE 32 + +#if CH20_DEBUG_LOG_EN +#include +#define CH20_DEBUG_LOG printf +#else +#define CH20_DEBUG_LOG(fmt,...) +#endif + + +/* PM2.5 数据解析器控制块 */ +typedef struct CH20_parser_control_st { + k_task_t parser_task; //解析器任务控制块 + + k_sem_t parser_rx_sem; //表示解析器从串口接收到数据 + k_chr_fifo_t parser_rx_fifo; //存放解析器接收到的数据 +} ch20_parser_ctrl_t; + +/** + * @brief 解析出的CH20数据值 + * @note 可以作为邮件发送给其他任务进行进一步处理 + * @param + */ +typedef struct ch20_data_st { + uint16_t data; +} ch20_data_t; + +void ch20_parser_input_byte(uint8_t byte); + +int ch20_parser_init(void); + +#endif /* _CH20_PARSER_H_ */ diff --git a/examples/tos_meets_rust/BSP/Hardware/OLED/bmp.h b/examples/tos_meets_rust/BSP/Hardware/OLED/bmp.h new file mode 100644 index 00000000..9c852fa4 --- /dev/null +++ b/examples/tos_meets_rust/BSP/Hardware/OLED/bmp.h @@ -0,0 +1,80 @@ +////////////////////////////////////////////////////////////////////////////////// +// +// +//瀛樺偍鍥剧墖鏁版嵁锛屽浘鐗囧ぇ灏忎负64*32鍍忕礌 +// +///////////////////////////////////////////////////////////////////////////////// + +#ifndef __BMP_H +#define __BMP_H +unsigned char BMP1[] = +{0x00,0x06,0x0A,0xFE,0x0A,0xC6,0x00,0xE0,0x00,0xF0,0x00,0xF8,0x00,0x00,0x00,0x00, +0x00,0x00,0xFE,0x7D,0xBB,0xC7,0xEF,0xEF,0xEF,0xEF,0xEF,0xEF,0xEF,0xC7,0xBB,0x7D, +0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08, +0x0C,0xFE,0xFE,0x0C,0x08,0x20,0x60,0xFE,0xFE,0x60,0x20,0x00,0x00,0x00,0x78,0x48, +0xFE,0x82,0xBA,0xBA,0x82,0xBA,0xBA,0x82,0xBA,0xBA,0x82,0xBA,0xBA,0x82,0xFE,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0xFF, +0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0xFF,0xFF,0x00,0x00,0xFE,0xFF,0x03, +0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0xFF,0xFE,0x00,0x00,0x00,0x00,0xC0,0xC0, +0xC0,0x00,0x00,0x00,0x00,0xFE,0xFF,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03, +0xFF,0xFE,0x00,0x00,0xFE,0xFF,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0xFF, +0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x0C, +0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0xFF,0xFF,0x00,0x00,0x00,0x00,0xE1,0xE1, +0xE1,0x00,0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0xFF, +0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x1F, +0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x1F,0x0F,0x00,0x00,0x0F,0x1F,0x18, +0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x1F,0x0F,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x0F,0x1F,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18, +0x1F,0x0F,0x00,0x00,0x0F,0x1F,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x1F, +0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x8C,0x42,0x22,0x12,0x0C,0x00,0xFC,0x02,0x02,0x02,0xFC,0x00,0x00,0x04, +0xFE,0x00,0x00,0x00,0x20,0x58,0x44,0xFE,0x40,0x00,0x10,0x10,0x10,0x10,0x10,0x00, +0x00,0x04,0xFE,0x00,0x00,0x00,0xFC,0x02,0x02,0x02,0xFC,0x00,0x10,0x10,0x10,0x10, +0x10,0x00,0xFC,0x02,0x02,0x02,0xFC,0x00,0x00,0x04,0xFE,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x24,0xA4,0x2E,0x24,0xE4,0x24,0x2E,0xA4,0x24,0x00,0x00,0x00,0xF8,0x4A,0x4C, +0x48,0xF8,0x48,0x4C,0x4A,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x01,0x01,0x01,0x00,0x00,0x00,0x01, +0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x01,0x01,0x01,0x00,0x00,0x00,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x01,0x01,0x01,0x00,0x00,0x00,0x01,0x01,0x01,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x20,0x10,0x10, +0x10,0x10,0x20,0xC0,0x00,0x00,0xC0,0x20,0x10,0x10,0x10,0x10,0x20,0xC0,0x00,0x00, +0x00,0x12,0x0A,0x07,0x02,0x7F,0x02,0x07,0x0A,0x12,0x00,0x00,0x00,0x0B,0x0A,0x0A, +0x0A,0x7F,0x0A,0x0A,0x0A,0x0B,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x40,0x40, +0x40,0x50,0x20,0x5F,0x80,0x00,0x1F,0x20,0x40,0x40,0x40,0x50,0x20,0x5F,0x80,0x00, + +}; + +#endif + + diff --git a/examples/tos_meets_rust/BSP/Hardware/OLED/oled.c b/examples/tos_meets_rust/BSP/Hardware/OLED/oled.c new file mode 100644 index 00000000..b9fd9c17 --- /dev/null +++ b/examples/tos_meets_rust/BSP/Hardware/OLED/oled.c @@ -0,0 +1,320 @@ +#include "oled.h" +#include "stdlib.h" +#include "oledfont.h" +//OLED鐨勬樉瀛 +//瀛樻斁鏍煎紡濡備笅. +//----------------------------------- +//|x鈫抂0,127] | +//| OLED鏄剧ず鍧愭爣 | +//|y 鑼冨洿 | +//|鈫 | +//|[0,31] | +//----------------------------------- +/********************************************** +//IIC Start +**********************************************/ +void IIC_Start(void) +{ + OLED_SCLK_Set(); + OLED_SDIN_Set(); + OLED_SDIN_Clr(); + OLED_SCLK_Clr(); +} + +/********************************************** +//IIC Stop +**********************************************/ +void IIC_Stop(void) +{ + OLED_SCLK_Set(); + OLED_SDIN_Clr(); + OLED_SDIN_Set(); +} + +void IIC_Wait_Ack(void) +{ + OLED_SCLK_Set(); + OLED_SCLK_Clr(); +} +/********************************************** +// IIC Write byte +**********************************************/ + +void Write_IIC_Byte(unsigned char IIC_Byte) +{ + unsigned char i; + unsigned char m,da; + da=IIC_Byte; + OLED_SCLK_Clr(); + for(i=0;i<8;i++) + { + m=da; + m=m&0x80; + if(m==0x80) + { + OLED_SDIN_Set(); + } + else + OLED_SDIN_Clr(); + da=da<<1; + OLED_SCLK_Set(); + OLED_SCLK_Clr(); + } +} +/********************************************** +// IIC Write Command +**********************************************/ +void Write_IIC_Command(unsigned char IIC_Command) +{ + IIC_Start(); + Write_IIC_Byte(0x78); //Slave address,SA0=0 + IIC_Wait_Ack(); + Write_IIC_Byte(0x00); //write command + IIC_Wait_Ack(); + Write_IIC_Byte(IIC_Command); + IIC_Wait_Ack(); + IIC_Stop(); +} +/********************************************** +// IIC Write Data +**********************************************/ +void Write_IIC_Data(unsigned char IIC_Data) +{ + IIC_Start(); + Write_IIC_Byte(0x78); //D/C#=0; R/W#=0 + IIC_Wait_Ack(); + Write_IIC_Byte(0x40); //write data + IIC_Wait_Ack(); + Write_IIC_Byte(IIC_Data); + IIC_Wait_Ack(); + IIC_Stop(); +} +void OLED_WR_Byte(unsigned dat,unsigned cmd) +{ + if(cmd) + { + Write_IIC_Data(dat); + } + else + { + Write_IIC_Command(dat); + } +} + + +/******************************************** +// fill_Picture +********************************************/ +void fill_picture(unsigned char fill_Data) +{ + unsigned char m,n; + for(m=0;m<8;m++) + { + OLED_WR_Byte(0xb0+m,0); //page0-page1 + OLED_WR_Byte(0x00,0); //low column start address + OLED_WR_Byte(0x10,0); //high column start address + for(n=0;n<128;n++) + { + OLED_WR_Byte(fill_Data,1); + } + } +} + +//鍧愭爣璁剧疆 +void OLED_Set_Pos(unsigned char x, unsigned char y) +{ + OLED_WR_Byte(0xb0+y,OLED_CMD); + OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD); + OLED_WR_Byte((x&0x0f),OLED_CMD); +} +//寮鍚疧LED鏄剧ず +void OLED_Display_On(void) +{ + OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC鍛戒护 + OLED_WR_Byte(0X14,OLED_CMD); //DCDC ON + OLED_WR_Byte(0XAF,OLED_CMD); //DISPLAY ON +} +//鍏抽棴OLED鏄剧ず +void OLED_Display_Off(void) +{ + OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC鍛戒护 + OLED_WR_Byte(0X10,OLED_CMD); //DCDC OFF + OLED_WR_Byte(0XAE,OLED_CMD); //DISPLAY OFF +} +//娉ㄦ剰锛氭竻灞忓嚱鏁,娓呭畬灞,鏁翠釜灞忓箷鏄粦鑹茬殑!鍜屾病鐐逛寒涓鏍!!! +void OLED_Clear(void) +{ + uint8_t i,n; + for(i=0;i<8;i++) + { + OLED_WR_Byte (0xb0+i,OLED_CMD); //璁剧疆椤靛湴鍧锛0~7锛 + OLED_WR_Byte (0x00,OLED_CMD); //璁剧疆鏄剧ず浣嶇疆鈥斿垪浣庡湴鍧 + OLED_WR_Byte (0x10,OLED_CMD); //璁剧疆鏄剧ず浣嶇疆鈥斿垪楂樺湴鍧 + for(n=0;n<128;n++) + OLED_WR_Byte(0,OLED_DATA); + } //鏇存柊鏄剧ず +} +void OLED_On(void) +{ + uint8_t i,n; + for(i=0;i<8;i++) + { + OLED_WR_Byte (0xb0+i,OLED_CMD); //璁剧疆椤靛湴鍧锛0~7锛 + OLED_WR_Byte (0x00,OLED_CMD); //璁剧疆鏄剧ず浣嶇疆鈥斿垪浣庡湴鍧 + OLED_WR_Byte (0x10,OLED_CMD); //璁剧疆鏄剧ず浣嶇疆鈥斿垪楂樺湴鍧 + for(n=0;n<128;n++)OLED_WR_Byte(1,OLED_DATA); + } //鏇存柊鏄剧ず +} +//鍦ㄦ寚瀹氫綅缃樉绀轰竴涓瓧绗,鍖呮嫭閮ㄥ垎瀛楃 +//x:0~127 +//y:0~63 +// +//size:閫夋嫨瀛椾綋 16/12 +void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t Char_Size) +{ + unsigned char c=0,i=0; + c=chr-' ';//寰楀埌鍋忕Щ鍚庣殑鍊 + if(x>Max_Column-1){x=0;y=y+2;} + if(Char_Size ==16) + { + OLED_Set_Pos(x,y); + for(i=0;i<8;i++) + { + OLED_WR_Byte(F8X16[c*16+i],OLED_DATA); + } + OLED_Set_Pos(x,y+1); + for(i=0;i<8;i++){ + OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA); + } + } + else + { + OLED_Set_Pos(x,y); + for(i=0;i<6;i++) + OLED_WR_Byte(F6x8[c][i],OLED_DATA); + } +} +//m^n鍑芥暟 +uint32_t oled_pow(uint8_t m,uint8_t n) +{ + uint32_t result=1; + while(n--)result*=m; + return result; +} +//鏄剧ず2涓暟瀛 +//x,y :璧风偣鍧愭爣 +//len :鏁板瓧鐨勪綅鏁 +//size:瀛椾綋澶у皬 +// +//num:鏁板(0~4294967295); +void OLED_ShowNum(uint8_t x,uint8_t y,uint32_t num,uint8_t len,uint8_t size2) +{ + uint8_t t,temp; + uint8_t enshow=0; + for(t=0;t120) + { + x=0;y+=2; + } + j++; + } +} +//鏄剧ず姹夊瓧 +void OLED_ShowChinese(uint8_t x,uint8_t y,uint8_t no) +{ + uint8_t t,adder=0; + OLED_Set_Pos(x,y); + for(t=0;t<16;t++) + { + OLED_WR_Byte(Hzk[2*no][t],OLED_DATA); + adder+=1; + } + OLED_Set_Pos(x,y+1); + for(t=0;t<16;t++) + { + OLED_WR_Byte(Hzk[2*no+1][t],OLED_DATA); + adder+=1; + } +} +/***********鍔熻兘鎻忚堪锛氭樉绀烘樉绀築MP鍥剧墖128脳64璧峰鐐瑰潗鏍(x,y),x鐨勮寖鍥0銆127锛寉涓洪〉鐨勮寖鍥0銆7*****************/ +void OLED_DrawBMP(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1,unsigned char BMP[]) +{ + unsigned int j=0; + unsigned char x,y; + + if(y1%8==0) y=y1/8; + else y=y1/8+1; + for(y=y0;y +{0x00, 0x02, 0x01, 0x51, 0x09, 0x06},// ? +{0x00, 0x32, 0x49, 0x59, 0x51, 0x3E},// @ +{0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C},// A +{0x00, 0x7F, 0x49, 0x49, 0x49, 0x36},// B +{0x00, 0x3E, 0x41, 0x41, 0x41, 0x22},// C +{0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C},// D +{0x00, 0x7F, 0x49, 0x49, 0x49, 0x41},// E +{0x00, 0x7F, 0x09, 0x09, 0x09, 0x01},// F +{0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A},// G +{0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F},// H +{0x00, 0x00, 0x41, 0x7F, 0x41, 0x00},// I +{0x00, 0x20, 0x40, 0x41, 0x3F, 0x01},// J +{0x00, 0x7F, 0x08, 0x14, 0x22, 0x41},// K +{0x00, 0x7F, 0x40, 0x40, 0x40, 0x40},// L +{0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F},// M +{0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F},// N +{0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E},// O +{0x00, 0x7F, 0x09, 0x09, 0x09, 0x06},// P +{0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E},// Q +{0x00, 0x7F, 0x09, 0x19, 0x29, 0x46},// R +{0x00, 0x46, 0x49, 0x49, 0x49, 0x31},// S +{0x00, 0x01, 0x01, 0x7F, 0x01, 0x01},// T +{0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F},// U +{0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F},// V +{0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F},// W +{0x00, 0x63, 0x14, 0x08, 0x14, 0x63},// X +{0x00, 0x07, 0x08, 0x70, 0x08, 0x07},// Y +{0x00, 0x61, 0x51, 0x49, 0x45, 0x43},// Z +{0x00, 0x00, 0x7F, 0x41, 0x41, 0x00},// [ +{0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55},// 55 +{0x00, 0x00, 0x41, 0x41, 0x7F, 0x00},// ] +{0x00, 0x04, 0x02, 0x01, 0x02, 0x04},// ^ +{0x00, 0x40, 0x40, 0x40, 0x40, 0x40},// _ +{0x00, 0x00, 0x01, 0x02, 0x04, 0x00},// ' +{0x00, 0x20, 0x54, 0x54, 0x54, 0x78},// a +{0x00, 0x7F, 0x48, 0x44, 0x44, 0x38},// b +{0x00, 0x38, 0x44, 0x44, 0x44, 0x20},// c +{0x00, 0x38, 0x44, 0x44, 0x48, 0x7F},// d +{0x00, 0x38, 0x54, 0x54, 0x54, 0x18},// e +{0x00, 0x08, 0x7E, 0x09, 0x01, 0x02},// f +{0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C},// g +{0x00, 0x7F, 0x08, 0x04, 0x04, 0x78},// h +{0x00, 0x00, 0x44, 0x7D, 0x40, 0x00},// i +{0x00, 0x40, 0x80, 0x84, 0x7D, 0x00},// j +{0x00, 0x7F, 0x10, 0x28, 0x44, 0x00},// k +{0x00, 0x00, 0x41, 0x7F, 0x40, 0x00},// l +{0x00, 0x7C, 0x04, 0x18, 0x04, 0x78},// m +{0x00, 0x7C, 0x08, 0x04, 0x04, 0x78},// n +{0x00, 0x38, 0x44, 0x44, 0x44, 0x38},// o +{0x00, 0xFC, 0x24, 0x24, 0x24, 0x18},// p +{0x00, 0x18, 0x24, 0x24, 0x18, 0xFC},// q +{0x00, 0x7C, 0x08, 0x04, 0x04, 0x08},// r +{0x00, 0x48, 0x54, 0x54, 0x54, 0x20},// s +{0x00, 0x04, 0x3F, 0x44, 0x40, 0x20},// t +{0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C},// u +{0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C},// v +{0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C},// w +{0x00, 0x44, 0x28, 0x10, 0x28, 0x44},// x +{0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C},// y +{0x00, 0x44, 0x64, 0x54, 0x4C, 0x44},// z +{0x14, 0x14, 0x14, 0x14, 0x14, 0x14},// horiz lines +}; +/****************************************8*16鐨勭偣闃************************************/ +const unsigned char F8X16[]= +{ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0 + 0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1 + 0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2 + 0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3 + 0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4 + 0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5 + 0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6 + 0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7 + 0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8 + 0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9 + 0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10 + 0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14 + 0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15 + 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16 + 0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17 + 0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18 + 0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19 + 0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20 + 0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21 + 0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22 + 0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23 + 0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24 + 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25 + 0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26 + 0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27 + 0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28 + 0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29 + 0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30 + 0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31 + 0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32 + 0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33 + 0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34 + 0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35 + 0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36 + 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37 + 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38 + 0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39 + 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40 + 0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41 + 0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42 + 0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43 + 0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44 + 0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45 + 0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46 + 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47 + 0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48 + 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49 + 0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50 + 0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51 + 0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52 + 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53 + 0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54 + 0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55 + 0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56 + 0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57 + 0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58 + 0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59 + 0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60 + 0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61 + 0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63 + 0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64 + 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65 + 0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66 + 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67 + 0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68 + 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69 + 0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70 + 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71 + 0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72 + 0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73 + 0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74 + 0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75 + 0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76 + 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77 + 0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78 + 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79 + 0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80 + 0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81 + 0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82 + 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83 + 0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84 + 0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85 + 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86 + 0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87 + 0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88 + 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89 + 0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90 + 0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91 + 0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92 + 0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93 + 0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94 +}; +char Hzk[][16]={ + +{0x40,0x3C,0x10,0xFF,0x10,0x10,0x20,0x10,0x8F,0x78,0x08,0xF8,0x08,0xF8,0x00,0x00}, +{0x02,0x06,0x02,0xFF,0x01,0x01,0x04,0x42,0x21,0x18,0x46,0x81,0x40,0x3F,0x00,0x00},/*"鐗",0*/ +/* (16 X 16 , 瀹嬩綋 )*/ + +{0x02,0xFE,0x92,0x92,0xFE,0x02,0x00,0x10,0x11,0x16,0xF0,0x14,0x13,0x10,0x00,0x00}, +{0x10,0x1F,0x08,0x08,0xFF,0x04,0x81,0x41,0x31,0x0D,0x03,0x0D,0x31,0x41,0x81,0x00},/*"鑱",1*/ +/* (16 X 16 , 瀹嬩綋 )*/ + +{0x00,0xFE,0x02,0x22,0x42,0x82,0x72,0x02,0x22,0x42,0x82,0x72,0x02,0xFE,0x00,0x00}, +{0x00,0xFF,0x10,0x08,0x06,0x01,0x0E,0x10,0x08,0x06,0x01,0x4E,0x80,0x7F,0x00,0x00},/*"缃",2*/ +/* (16 X 16 , 瀹嬩綋 )*/ + +{0x00,0x80,0x60,0xF8,0x07,0x00,0xFE,0x52,0x52,0x52,0x52,0x52,0xFE,0x00,0x00,0x00}, +{0x01,0x00,0x00,0xFF,0x08,0x88,0x4F,0x29,0x09,0x09,0x09,0x29,0x4F,0x88,0x08,0x00},/*"淇",3*/ +/* (16 X 16 , 瀹嬩綋 )*/ + +{0x00,0x00,0xE0,0x9C,0x84,0x84,0x84,0xF4,0x82,0x82,0x83,0x82,0x80,0x80,0x00,0x00}, +{0x00,0x20,0x10,0x08,0x06,0x40,0x80,0x7F,0x00,0x00,0x02,0x04,0x08,0x30,0x00,0x00},/*"涔",4*/ +/* (16 X 16 , 瀹嬩綋 )*/ + +{0x40,0x44,0x54,0x65,0x46,0x44,0x64,0x54,0x44,0x40,0xFE,0x02,0x22,0xDA,0x06,0x00}, +{0x00,0x00,0x7E,0x22,0x22,0x22,0x22,0x7E,0x00,0x00,0xFF,0x08,0x10,0x08,0x07,0x00},/*"閮",5*/ +/* (16 X 16 , 瀹嬩綋 )*/ +}; + +#endif diff --git a/examples/tos_meets_rust/BSP/Hardware/PM2D5/pm2d5_parser.c b/examples/tos_meets_rust/BSP/Hardware/PM2D5/pm2d5_parser.c new file mode 100644 index 00000000..65248209 --- /dev/null +++ b/examples/tos_meets_rust/BSP/Hardware/PM2D5/pm2d5_parser.c @@ -0,0 +1,231 @@ +/*---------------------------------------------------------------------------- + * Tencent is pleased to support the open source community by making TencentOS + * available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * If you have downloaded a copy of the TencentOS binary from Tencent, please + * note that the TencentOS binary is licensed under the BSD 3-Clause License. + * + * If you have downloaded a copy of the TencentOS source code from Tencent, + * please note that TencentOS source code is licensed under the BSD 3-Clause + * License, except for the third-party components listed below which are + * subject to different license terms. Your integration of TencentOS into your + * own projects may require compliance with the BSD 3-Clause License, as well + * as the other licenses applicable to the third-party components included + * within TencentOS. + *---------------------------------------------------------------------------*/ +#include "pm2d5_parser.h" + +static pm2d5_parser_ctrl_t pm2d5_parser_ctrl; + +static k_stack_t pm2d5_parser_task_stack[PM2D5_PARSER_TASK_STACK_SIZE]; + +static uint8_t pm2d5_parser_buffer[PM2D5_PARSER_BUFFER_SIZE]; + +/** + * @brief PM2D5传感器原始数据 + * @note 传感器每次上报32字节,头部0x42和0x4d固定,用于解析包头,帧长度用于判断是否是正常数据包,所以只存储后面的28字节 + */ +typedef struct pm2d5_raw_data_st { + + uint8_t data1_h; + uint8_t data1_l; + + uint8_t data2_h; + uint8_t data2_l; + + uint8_t data3_h; + uint8_t data3_l; + + uint8_t data4_h; + uint8_t data4_l; + + uint8_t data5_h; + uint8_t data5_l; + + uint8_t data6_h; + uint8_t data6_l; + + uint8_t data7_h; + uint8_t data7_l; + + uint8_t data8_h; + uint8_t data8_l; + + uint8_t data9_h; + uint8_t data9_l; + + uint8_t data10_h; + uint8_t data10_l; + + uint8_t data11_h; + uint8_t data11_l; + + uint8_t data12_h; + uint8_t data12_l; + + uint8_t data13_h; //版本号 + uint8_t data13_l; //错误代码 + + uint8_t chk_sum_h; //和校验高8位 + uint8_t chk_sum_l; //和校验低8位 +} pm2d5_raw_data_t; + +typedef union pm2d5_raw_data_un { + uint8_t data[30]; + pm2d5_raw_data_t pm2d5_raw_data; +}pm2d5_raw_data_u; + +/** + * @brief 向PM2D5解析器中送入一个字节数据 + * @param data 送入的数据 + * @retval none + * @note 需要用户在串口中断函数中手动调用 +*/ +void pm2d5_parser_input_byte(uint8_t data) +{ + if (tos_chr_fifo_push(&pm2d5_parser_ctrl.parser_rx_fifo, data) == K_ERR_NONE) { + /* 送入数据成功,释放信号量,计数 */ + tos_sem_post(&pm2d5_parser_ctrl.parser_rx_sem); + } +} + +/** + * @brief PM2D5解析器从chr fifo中取出一个字节数据 + * @param none + * @retval 正常返回读取数据,错误返回-1 +*/ +static int pm2d5_parser_getchar(void) +{ + uint8_t chr; + k_err_t err; + + /* 永久等待信号量,信号量为空表示chr fifo中无数据 */ + if (tos_sem_pend(&pm2d5_parser_ctrl.parser_rx_sem, TOS_TIME_FOREVER) != K_ERR_NONE) { + return -1; + } + + /* 从chr fifo中取出数据 */ + err = tos_chr_fifo_pop(&pm2d5_parser_ctrl.parser_rx_fifo, &chr); + + return err == K_ERR_NONE ? chr : -1; +} + +/** + * @brief PM2D5读取传感器原始数据并解析 + * @param void + * @retval 解析成功返回0,解析失败返回-1 +*/ +static int pm2d5_parser_read_raw_data(pm2d5_raw_data_u *pm2d5_raw_data, pm2d5_data_u *pm2d5_data) +{ + int i; + uint8_t len_h,len_l; + uint16_t len; + uint16_t check_sum; + uint16_t check_sum_cal = 0x42 + 0x4d; + + /* 读取并计算帧长度 */ + len_h = pm2d5_parser_getchar(); + len_l = pm2d5_parser_getchar(); + len = (len_h << 8) | len_l; + + if ( len != 0x001C) { + //非传感器值数据,清空缓存 + for (i = 0; i < len; i++) { + pm2d5_parser_getchar(); + } + return -1; + } + + /* 读取传感器原始数据 */ + for (i = 0; i < len; i++) { + pm2d5_raw_data->data[i] = pm2d5_parser_getchar(); + } + + /* 和校验 */ + //通过数据计算和校验 + check_sum_cal = check_sum_cal + len_h + len_l; + for (i = 0; i < len -2; i++) { + check_sum_cal += pm2d5_raw_data->data[i]; + } + //协议中给出的和校验值 + check_sum = (pm2d5_raw_data->pm2d5_raw_data.chk_sum_h << 8) + pm2d5_raw_data->pm2d5_raw_data.chk_sum_l; + if (check_sum_cal != check_sum) { + return -1; + } + + /* 存储传感器值 */ + for (i = 0; i < sizeof(pm2d5_data_t); i++) { + pm2d5_data->data[i] = pm2d5_raw_data->data[i]; + } + + return 0; +} + +extern k_mail_q_t mail_q; +pm2d5_raw_data_u pm2d5_raw_data; +pm2d5_data_u pm2d5_data; + +/** + * @brief PM2D5解析器任务 +*/ +static void pm2d5_parser_task_entry(void *arg) +{ + int chr, last_chr = 0; + + while (1) { + + chr = pm2d5_parser_getchar(); + if (chr < 0) { + printf("parser task get char fail!\r\n"); + continue; + } + + if (chr == 0x4d && last_chr == 0x42) { + /* 解析到包头 */ + if (0 == pm2d5_parser_read_raw_data(&pm2d5_raw_data, &pm2d5_data)) { + /* 正常解析之后通过邮箱发送 */ + tos_mail_q_post(&mail_q, &pm2d5_data, sizeof(pm2d5_data_t)); + } + } + + last_chr = chr; + } +} + +/** + * @brief 初始化PM2D5解析器 + * @param none + * @retval 全部创建成功返回0,任何一个创建失败则返回-1 +*/ +int pm2d5_parser_init(void) +{ + k_err_t ret; + + memset((pm2d5_parser_ctrl_t*)&pm2d5_parser_ctrl, 0, sizeof(pm2d5_parser_ctrl)); + + /* 创建 chr fifo */ + ret = tos_chr_fifo_create(&pm2d5_parser_ctrl.parser_rx_fifo, pm2d5_parser_buffer, sizeof(pm2d5_parser_buffer)); + if (ret != K_ERR_NONE) { + printf("pm2d5 parser chr fifo create fail, ret = %d\r\n", ret); + return -1; + } + + /* 创建信号量 */ + ret = tos_sem_create(&pm2d5_parser_ctrl.parser_rx_sem, 0); + if (ret != K_ERR_NONE) { + printf("pm2d5 parser_rx_sem create fail, ret = %d\r\n", ret); + return -1; + } + + /* 创建线程 */ + ret = tos_task_create(&pm2d5_parser_ctrl.parser_task, "pm2d5_parser_task", + pm2d5_parser_task_entry, NULL, PM2D5_PARSER_TASK_PRIO, + pm2d5_parser_task_stack,PM2D5_PARSER_TASK_STACK_SIZE,0); + if (ret != K_ERR_NONE) { + printf("pm2d5 parser task create fail, ret = %d\r\n", ret); + return -1; + } + + return 0; +} diff --git a/examples/tos_meets_rust/BSP/Hardware/PM2D5/pm2d5_parser.h b/examples/tos_meets_rust/BSP/Hardware/PM2D5/pm2d5_parser.h new file mode 100644 index 00000000..5ba739f8 --- /dev/null +++ b/examples/tos_meets_rust/BSP/Hardware/PM2D5/pm2d5_parser.h @@ -0,0 +1,66 @@ +/*---------------------------------------------------------------------------- + * Tencent is pleased to support the open source community by making TencentOS + * available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * If you have downloaded a copy of the TencentOS binary from Tencent, please + * note that the TencentOS binary is licensed under the BSD 3-Clause License. + * + * If you have downloaded a copy of the TencentOS source code from Tencent, + * please note that TencentOS source code is licensed under the BSD 3-Clause + * License, except for the third-party components listed below which are + * subject to different license terms. Your integration of TencentOS into your + * own projects may require compliance with the BSD 3-Clause License, as well + * as the other licenses applicable to the third-party components included + * within TencentOS. + *---------------------------------------------------------------------------*/ +#ifndef _PM2D5_PARSER_H_ +#define _PM2D5_PARSER_H_ + +#include "tos_k.h" + +/* pm2d5 parser config */ +#define PM2D5_PARSER_TASK_STACK_SIZE 512 +#define PM2D5_PARSER_TASK_PRIO 5 +#define PM2D5_PARSER_BUFFER_SIZE 64 + +/* PM2.5 数据解析器控制块 */ +typedef struct pm2d5_parser_control_st { + k_task_t parser_task; //解析器任务控制块 + + k_sem_t parser_rx_sem; //表示解析器从串口接收到数据 + k_chr_fifo_t parser_rx_fifo; //存放解析器接收到的数 +} pm2d5_parser_ctrl_t; + +/** + * @brief 解析出的PM2D5数据值 + * @note 可以作为邮件发送给其他任务进行进一步处理 + */ +typedef struct pm2d5_data_st { + uint16_t data1; + uint16_t data2; + uint16_t data3; + uint16_t data4; + uint16_t data5; + uint16_t data6; + uint16_t data7; + uint16_t data8; + uint16_t data9; + uint16_t data10; + uint16_t data11; + uint16_t data12; + uint16_t data13; +}pm2d5_data_t; + +typedef union pm2d5_data_un { + uint16_t data[13]; + pm2d5_data_t pm2d5_data; +} pm2d5_data_u; + +void pm2d5_parser_input_byte(uint8_t byte); + +int pm2d5_send_read_cmd(void); + +int pm2d5_parser_init(void); + +#endif /* _PM2D5_PARSER_H_ */ diff --git a/examples/tos_meets_rust/BSP/Inc/adc.h b/examples/tos_meets_rust/BSP/Inc/adc.h new file mode 100644 index 00000000..f0e4dbee --- /dev/null +++ b/examples/tos_meets_rust/BSP/Inc/adc.h @@ -0,0 +1,58 @@ +/** + ****************************************************************************** + * File Name : ADC.h + * Description : This file provides code for the configuration + * of the ADC instances. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2020 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __adc_H +#define __adc_H +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +extern ADC_HandleTypeDef hadc1; + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_ADC1_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif +#endif /*__ adc_H */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/examples/tos_meets_rust/BSP/Inc/gpio.h b/examples/tos_meets_rust/BSP/Inc/gpio.h new file mode 100644 index 00000000..6a60e56f --- /dev/null +++ b/examples/tos_meets_rust/BSP/Inc/gpio.h @@ -0,0 +1,57 @@ +/** + ****************************************************************************** + * File Name : gpio.h + * Description : This file contains all the functions prototypes for + * the gpio + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2020 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __gpio_H +#define __gpio_H +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_GPIO_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif +#endif /*__ pinoutConfig_H */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/examples/tos_meets_rust/BSP/Inc/i2c.h b/examples/tos_meets_rust/BSP/Inc/i2c.h new file mode 100644 index 00000000..2cf02dac --- /dev/null +++ b/examples/tos_meets_rust/BSP/Inc/i2c.h @@ -0,0 +1,58 @@ +/** + ****************************************************************************** + * File Name : I2C.h + * Description : This file provides code for the configuration + * of the I2C instances. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2020 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __i2c_H +#define __i2c_H +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +extern I2C_HandleTypeDef hi2c2; + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_I2C2_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif +#endif /*__ i2c_H */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/examples/tos_meets_rust/BSP/Inc/main.h b/examples/tos_meets_rust/BSP/Inc/main.h new file mode 100644 index 00000000..50a1be87 --- /dev/null +++ b/examples/tos_meets_rust/BSP/Inc/main.h @@ -0,0 +1,72 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file : main.h + * @brief : Header for main.c file. + * This file contains the common defines of the application. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2020 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ +/* USER CODE END Header */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __MAIN_H +#define __MAIN_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "stm32g0xx_hal.h" +#include "stm32g0xx_ll_system.h" + +/* Private includes ----------------------------------------------------------*/ +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* Exported types ------------------------------------------------------------*/ +/* USER CODE BEGIN ET */ + +/* USER CODE END ET */ + +/* Exported constants --------------------------------------------------------*/ +/* USER CODE BEGIN EC */ + +/* USER CODE END EC */ + +/* Exported macro ------------------------------------------------------------*/ +/* USER CODE BEGIN EM */ + +/* USER CODE END EM */ + +/* Exported functions prototypes ---------------------------------------------*/ +void Error_Handler(void); + +/* USER CODE BEGIN EFP */ + +/* USER CODE END EFP */ + +/* Private defines -----------------------------------------------------------*/ +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +#ifdef __cplusplus +} +#endif + +#endif /* __MAIN_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/examples/tos_meets_rust/BSP/Inc/mcu_init.h b/examples/tos_meets_rust/BSP/Inc/mcu_init.h new file mode 100644 index 00000000..79ad1c53 --- /dev/null +++ b/examples/tos_meets_rust/BSP/Inc/mcu_init.h @@ -0,0 +1,22 @@ +#ifndef __MCU_INIT_H +#define __MCU_INIT_H +#ifdef __cplusplus + extern "C" { +#endif + +#include "main.h" +#include "stm32g0xx_hal.h" +#include "usart.h" +#include "gpio.h" +#include "adc.h" +#include "i2c.h" +#include "spi.h" +#include "tos_k.h" + +void board_init(void); +void SystemClock_Config(void); + +#ifdef __cplusplus +} +#endif +#endif /*__ __MCU_INIT_H */ diff --git a/examples/tos_meets_rust/BSP/Inc/spi.h b/examples/tos_meets_rust/BSP/Inc/spi.h new file mode 100644 index 00000000..d82dfc45 --- /dev/null +++ b/examples/tos_meets_rust/BSP/Inc/spi.h @@ -0,0 +1,60 @@ +/** + ****************************************************************************** + * File Name : SPI.h + * Description : This file provides code for the configuration + * of the SPI instances. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2020 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __spi_H +#define __spi_H +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +extern SPI_HandleTypeDef hspi1; +extern SPI_HandleTypeDef hspi2; + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_SPI1_Init(void); +void MX_SPI2_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif +#endif /*__ spi_H */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/examples/tos_meets_rust/BSP/Inc/stm32g0xx_hal_conf.h b/examples/tos_meets_rust/BSP/Inc/stm32g0xx_hal_conf.h new file mode 100644 index 00000000..55018a62 --- /dev/null +++ b/examples/tos_meets_rust/BSP/Inc/stm32g0xx_hal_conf.h @@ -0,0 +1,310 @@ +/** + ****************************************************************************** + * @file stm32g0xx_hal_conf.h + * @author MCD Application Team + * @brief HAL configuration file. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2018 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef STM32G0xx_HAL_CONF_H +#define STM32G0xx_HAL_CONF_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ + +/* ########################## Module Selection ############################## */ +/** + * @brief This is the list of modules to be used in the HAL driver + */ +#define HAL_MODULE_ENABLED + + #define HAL_ADC_MODULE_ENABLED +/* #define HAL_CEC_MODULE_ENABLED */ +/* #define HAL_COMP_MODULE_ENABLED */ +/* #define HAL_CRC_MODULE_ENABLED */ +/* #define HAL_CRYP_MODULE_ENABLED */ +/* #define HAL_DAC_MODULE_ENABLED */ +/* #define HAL_EXTI_MODULE_ENABLED */ +#define HAL_I2C_MODULE_ENABLED +/* #define HAL_I2S_MODULE_ENABLED */ +/* #define HAL_IWDG_MODULE_ENABLED */ +/* #define HAL_IRDA_MODULE_ENABLED */ +/* #define HAL_LPTIM_MODULE_ENABLED */ +/* #define HAL_RNG_MODULE_ENABLED */ +/* #define HAL_RTC_MODULE_ENABLED */ +/* #define HAL_SMARTCARD_MODULE_ENABLED */ +/* #define HAL_SMBUS_MODULE_ENABLED */ +#define HAL_SPI_MODULE_ENABLED +/* #define HAL_TIM_MODULE_ENABLED */ +#define HAL_UART_MODULE_ENABLED +/* #define HAL_USART_MODULE_ENABLED */ +/* #define HAL_WWDG_MODULE_ENABLED */ +#define HAL_GPIO_MODULE_ENABLED +#define HAL_EXTI_MODULE_ENABLED +#define HAL_DMA_MODULE_ENABLED +#define HAL_RCC_MODULE_ENABLED +#define HAL_FLASH_MODULE_ENABLED +#define HAL_PWR_MODULE_ENABLED +#define HAL_CORTEX_MODULE_ENABLED + +/* ########################## Register Callbacks selection ############################## */ +/** + * @brief This is the list of modules where register callback can be used + */ +#define USE_HAL_ADC_REGISTER_CALLBACKS 0u +#define USE_HAL_CEC_REGISTER_CALLBACKS 0u +#define USE_HAL_COMP_REGISTER_CALLBACKS 0u +#define USE_HAL_CRYP_REGISTER_CALLBACKS 0u +#define USE_HAL_DAC_REGISTER_CALLBACKS 0u +#define USE_HAL_I2C_REGISTER_CALLBACKS 0u +#define USE_HAL_I2S_REGISTER_CALLBACKS 0u +#define USE_HAL_IRDA_REGISTER_CALLBACKS 0u +#define USE_HAL_LPTIM_REGISTER_CALLBACKS 0u +#define USE_HAL_RNG_REGISTER_CALLBACKS 0u +#define USE_HAL_RTC_REGISTER_CALLBACKS 0u +#define USE_HAL_SMBUS_REGISTER_CALLBACKS 0u +#define USE_HAL_SPI_REGISTER_CALLBACKS 0u +#define USE_HAL_TIM_REGISTER_CALLBACKS 0u +#define USE_HAL_UART_REGISTER_CALLBACKS 0u +#define USE_HAL_USART_REGISTER_CALLBACKS 0u +#define USE_HAL_WWDG_REGISTER_CALLBACKS 0u + +/* ########################## Oscillator Values adaptation ####################*/ +/** + * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSE is used as system clock source, directly or through the PLL). + */ +#if !defined (HSE_VALUE) +#define HSE_VALUE 8000000U /*!< Value of the External oscillator in Hz */ +#endif /* HSE_VALUE */ + +#if !defined (HSE_STARTUP_TIMEOUT) +#define HSE_STARTUP_TIMEOUT 100U /*!< Time out for HSE start up, in ms */ +#endif /* HSE_STARTUP_TIMEOUT */ + +/** + * @brief Internal High Speed oscillator (HSI) value. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSI is used as system clock source, directly or through the PLL). + */ +#if !defined (HSI_VALUE) +#define HSI_VALUE 16000000U /*!< Value of the Internal oscillator in Hz*/ +#endif /* HSI_VALUE */ + +/** + * @brief Internal Low Speed oscillator (LSI) value. + */ +#if !defined (LSI_VALUE) +#define LSI_VALUE 32000U /*!< LSI Typical Value in Hz*/ +#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz +The real value may vary depending on the variations +in voltage and temperature.*/ +/** + * @brief External Low Speed oscillator (LSE) value. + * This value is used by the UART, RTC HAL module to compute the system frequency + */ +#if !defined (LSE_VALUE) +#define LSE_VALUE 32768U /*!< Value of the External oscillator in Hz*/ +#endif /* LSE_VALUE */ + +#if !defined (LSE_STARTUP_TIMEOUT) +#define LSE_STARTUP_TIMEOUT 5000U /*!< Time out for LSE start up, in ms */ +#endif /* LSE_STARTUP_TIMEOUT */ + +/** + * @brief External clock source for I2S1 peripheral + * This value is used by the RCC HAL module to compute the I2S1 clock source + * frequency. + */ +#if !defined (EXTERNAL_I2S1_CLOCK_VALUE) +#define EXTERNAL_I2S1_CLOCK_VALUE 48000U /*!< Value of the I2S1 External clock source in Hz*/ +#endif /* EXTERNAL_I2S1_CLOCK_VALUE */ + +/* Tip: To avoid modifying this file each time you need to use different HSE, + === you can define the HSE value in your toolchain compiler preprocessor. */ + +/* ########################### System Configuration ######################### */ +/** + * @brief This is the HAL system configuration section + */ +#define VDD_VALUE 3300U /*!< Value of VDD in mv */ +#define TICK_INT_PRIORITY 0U /*!< tick interrupt priority */ +#define USE_RTOS 0U +#define PREFETCH_ENABLE 1U +#define INSTRUCTION_CACHE_ENABLE 1U + +/* ################## SPI peripheral configuration ########################## */ + +/* CRC FEATURE: Use to activate CRC feature inside HAL SPI Driver +* Activated: CRC code is present inside driver +* Deactivated: CRC code cleaned from driver +*/ + +#define USE_SPI_CRC 0U + +/* ################## CRYP peripheral configuration ########################## */ + +#define USE_HAL_CRYP_SUSPEND_RESUME 1U + +/* ########################## Assert Selection ############################## */ +/** + * @brief Uncomment the line below to expanse the "assert_param" macro in the + * HAL drivers code + */ +/* #define USE_FULL_ASSERT 1U */ + +/* Includes ------------------------------------------------------------------*/ +/** + * @brief Include module's header file + */ + +#ifdef HAL_RCC_MODULE_ENABLED +#include "stm32g0xx_hal_rcc.h" +#endif /* HAL_RCC_MODULE_ENABLED */ + +#ifdef HAL_GPIO_MODULE_ENABLED +#include "stm32g0xx_hal_gpio.h" +#endif /* HAL_GPIO_MODULE_ENABLED */ + +#ifdef HAL_DMA_MODULE_ENABLED +#include "stm32g0xx_hal_dma.h" +#endif /* HAL_DMA_MODULE_ENABLED */ + +#ifdef HAL_CORTEX_MODULE_ENABLED +#include "stm32g0xx_hal_cortex.h" +#endif /* HAL_CORTEX_MODULE_ENABLED */ + +#ifdef HAL_ADC_MODULE_ENABLED +#include "stm32g0xx_hal_adc.h" +#include "stm32g0xx_hal_adc_ex.h" +#endif /* HAL_ADC_MODULE_ENABLED */ + +#ifdef HAL_CEC_MODULE_ENABLED +#include "stm32g0xx_hal_cec.h" +#endif /* HAL_CEC_MODULE_ENABLED */ + +#ifdef HAL_COMP_MODULE_ENABLED +#include "stm32g0xx_hal_comp.h" +#endif /* HAL_COMP_MODULE_ENABLED */ + +#ifdef HAL_CRC_MODULE_ENABLED +#include "stm32g0xx_hal_crc.h" +#endif /* HAL_CRC_MODULE_ENABLED */ + +#ifdef HAL_CRYP_MODULE_ENABLED +#include "stm32g0xx_hal_cryp.h" +#endif /* HAL_CRYP_MODULE_ENABLED */ + +#ifdef HAL_DAC_MODULE_ENABLED +#include "stm32g0xx_hal_dac.h" +#endif /* HAL_DAC_MODULE_ENABLED */ + +#ifdef HAL_EXTI_MODULE_ENABLED +#include "stm32g0xx_hal_exti.h" +#endif /* HAL_EXTI_MODULE_ENABLED */ + +#ifdef HAL_FLASH_MODULE_ENABLED +#include "stm32g0xx_hal_flash.h" +#endif /* HAL_FLASH_MODULE_ENABLED */ + +#ifdef HAL_I2C_MODULE_ENABLED +#include "stm32g0xx_hal_i2c.h" +#endif /* HAL_I2C_MODULE_ENABLED */ + +#ifdef HAL_I2S_MODULE_ENABLED +#include "stm32g0xx_hal_i2s.h" +#endif /* HAL_I2S_MODULE_ENABLED */ + +#ifdef HAL_IRDA_MODULE_ENABLED +#include "stm32g0xx_hal_irda.h" +#endif /* HAL_IRDA_MODULE_ENABLED */ + +#ifdef HAL_IWDG_MODULE_ENABLED +#include "stm32g0xx_hal_iwdg.h" +#endif /* HAL_IWDG_MODULE_ENABLED */ + +#ifdef HAL_LPTIM_MODULE_ENABLED +#include "stm32g0xx_hal_lptim.h" +#endif /* HAL_LPTIM_MODULE_ENABLED */ + +#ifdef HAL_PWR_MODULE_ENABLED +#include "stm32g0xx_hal_pwr.h" +#endif /* HAL_PWR_MODULE_ENABLED */ + +#ifdef HAL_RNG_MODULE_ENABLED +#include "stm32g0xx_hal_rng.h" +#endif /* HAL_RNG_MODULE_ENABLED */ + +#ifdef HAL_RTC_MODULE_ENABLED +#include "stm32g0xx_hal_rtc.h" +#endif /* HAL_RTC_MODULE_ENABLED */ + +#ifdef HAL_SMARTCARD_MODULE_ENABLED +#include "stm32g0xx_hal_smartcard.h" +#endif /* HAL_SMARTCARD_MODULE_ENABLED */ + +#ifdef HAL_SMBUS_MODULE_ENABLED +#include "stm32g0xx_hal_smbus.h" +#endif /* HAL_SMBUS_MODULE_ENABLED */ + +#ifdef HAL_SPI_MODULE_ENABLED +#include "stm32g0xx_hal_spi.h" +#endif /* HAL_SPI_MODULE_ENABLED */ + +#ifdef HAL_TIM_MODULE_ENABLED +#include "stm32g0xx_hal_tim.h" +#endif /* HAL_TIM_MODULE_ENABLED */ + +#ifdef HAL_UART_MODULE_ENABLED +#include "stm32g0xx_hal_uart.h" +#endif /* HAL_UART_MODULE_ENABLED */ + +#ifdef HAL_USART_MODULE_ENABLED +#include "stm32g0xx_hal_usart.h" +#endif /* HAL_USART_MODULE_ENABLED */ + +#ifdef HAL_WWDG_MODULE_ENABLED +#include "stm32g0xx_hal_wwdg.h" +#endif /* HAL_WWDG_MODULE_ENABLED */ + +/* Exported macro ------------------------------------------------------------*/ +#ifdef USE_FULL_ASSERT +/** + * @brief The assert_param macro is used for functions parameters check. + * @param expr If expr is false, it calls assert_failed function + * which reports the name of the source file and the source + * line number of the call that failed. + * If expr is true, it returns no value. + * @retval None + */ +#define assert_param(expr) ((expr) ? (void)0U : assert_failed((uint8_t *)__FILE__, __LINE__)) +/* Exported functions ------------------------------------------------------- */ +void assert_failed(uint8_t *file, uint32_t line); +#else +#define assert_param(expr) ((void)0U) +#endif /* USE_FULL_ASSERT */ + +#ifdef __cplusplus +} +#endif + +#endif /* STM32G0xx_HAL_CONF_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/examples/tos_meets_rust/BSP/Inc/stm32g0xx_it.h b/examples/tos_meets_rust/BSP/Inc/stm32g0xx_it.h new file mode 100644 index 00000000..1c76dfda --- /dev/null +++ b/examples/tos_meets_rust/BSP/Inc/stm32g0xx_it.h @@ -0,0 +1,68 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file stm32g0xx_it.h + * @brief This file contains the headers of the interrupt handlers. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2020 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ +/* USER CODE END Header */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32G0xx_IT_H +#define __STM32G0xx_IT_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Private includes ----------------------------------------------------------*/ +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* Exported types ------------------------------------------------------------*/ +/* USER CODE BEGIN ET */ + +/* USER CODE END ET */ + +/* Exported constants --------------------------------------------------------*/ +/* USER CODE BEGIN EC */ + +/* USER CODE END EC */ + +/* Exported macro ------------------------------------------------------------*/ +/* USER CODE BEGIN EM */ + +/* USER CODE END EM */ + +/* Exported functions prototypes ---------------------------------------------*/ +void NMI_Handler(void); +void HardFault_Handler(void); +void SVC_Handler(void); +void PendSV_Handler(void); +void SysTick_Handler(void); +void USART1_IRQHandler(void); +void USART2_IRQHandler(void); +void USART3_4_IRQHandler(void); +/* USER CODE BEGIN EFP */ + +/* USER CODE END EFP */ + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32G0xx_IT_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/examples/tos_meets_rust/BSP/Inc/sys.h b/examples/tos_meets_rust/BSP/Inc/sys.h new file mode 100644 index 00000000..0f51f9f6 --- /dev/null +++ b/examples/tos_meets_rust/BSP/Inc/sys.h @@ -0,0 +1,56 @@ +/** + ****************************************************************************** + * File Name : SYS.h + * Description : This file provides code for the configuration + * of the SYS instances. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2020 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __sys_H +#define __sys_H +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_SYS_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif +#endif /*__ sys_H */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/examples/tos_meets_rust/BSP/Inc/usart.h b/examples/tos_meets_rust/BSP/Inc/usart.h new file mode 100644 index 00000000..151898ab --- /dev/null +++ b/examples/tos_meets_rust/BSP/Inc/usart.h @@ -0,0 +1,64 @@ +/** + ****************************************************************************** + * File Name : USART.h + * Description : This file provides code for the configuration + * of the USART instances. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2020 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __usart_H +#define __usart_H +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +extern UART_HandleTypeDef huart1; +extern UART_HandleTypeDef huart2; +extern UART_HandleTypeDef huart3; +extern UART_HandleTypeDef huart4; + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_USART1_UART_Init(void); +void MX_USART2_UART_Init(void); +void MX_USART3_UART_Init(void); +void MX_USART4_UART_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif +#endif /*__ usart_H */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/examples/tos_meets_rust/BSP/Makefile b/examples/tos_meets_rust/BSP/Makefile new file mode 100644 index 00000000..2d942585 --- /dev/null +++ b/examples/tos_meets_rust/BSP/Makefile @@ -0,0 +1,206 @@ +########################################################################################################################## +# File automatically-generated by tool: [projectgenerator] version: [3.5.2] date: [Tue Sep 22 14:14:18 CST 2020] +########################################################################################################################## + +# ------------------------------------------------ +# Generic Makefile (based on gcc) +# +# ChangeLog : +# 2017-02-10 - Several enhancements + project update mode +# 2015-07-22 - first version +# ------------------------------------------------ + +###################################### +# target +###################################### +TARGET = TencentOS_tiny_EVB_G0 + + +###################################### +# building variables +###################################### +# debug build? +DEBUG = 1 +# optimization +OPT = -Og + + +####################################### +# paths +####################################### +# Build path +BUILD_DIR = build + +###################################### +# source +###################################### +# C sources +C_SOURCES = \ +Src/main.c \ +Src/gpio.c \ +Src/adc.c \ +Src/i2c.c \ +Src/spi.c \ +Src/sys.c \ +Src/usart.c \ +Src/stm32g0xx_it.c \ +Src/stm32g0xx_hal_msp.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_gpio.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_adc.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_adc_ex.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_ll_adc.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc_ex.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_ll_rcc.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash_ex.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma_ex.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr_ex.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_cortex.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_exti.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_i2c.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_i2c_ex.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_spi.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_spi_ex.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_tim.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_tim_ex.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart_ex.c \ +Src/system_stm32g0xx.c + +# ASM sources +ASM_SOURCES = \ +startup_stm32g070xx.s + + +####################################### +# binaries +####################################### +PREFIX = arm-none-eabi- +# The gcc compiler bin path can be either defined in make command via GCC_PATH variable (> make GCC_PATH=xxx) +# either it can be added to the PATH environment variable. +ifdef GCC_PATH +CC = $(GCC_PATH)/$(PREFIX)gcc +AS = $(GCC_PATH)/$(PREFIX)gcc -x assembler-with-cpp +CP = $(GCC_PATH)/$(PREFIX)objcopy +SZ = $(GCC_PATH)/$(PREFIX)size +else +CC = $(PREFIX)gcc +AS = $(PREFIX)gcc -x assembler-with-cpp +CP = $(PREFIX)objcopy +SZ = $(PREFIX)size +endif +HEX = $(CP) -O ihex +BIN = $(CP) -O binary -S + +####################################### +# CFLAGS +####################################### +# cpu +CPU = -mcpu=cortex-m0plus + +# fpu +# NONE for Cortex-M0/M0+/M3 + +# float-abi + + +# mcu +MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI) + +# macros for gcc +# AS defines +AS_DEFS = + +# C defines +C_DEFS = \ +-DUSE_HAL_DRIVER \ +-DSTM32G070xx + + +# AS includes +AS_INCLUDES = + +# C includes +C_INCLUDES = \ +-IInc \ +-IDrivers/STM32G0xx_HAL_Driver/Inc \ +-IDrivers/STM32G0xx_HAL_Driver/Inc/Legacy \ +-IDrivers/CMSIS/Device/ST/STM32G0xx/Include \ +-IDrivers/CMSIS/Include \ +-IDrivers/CMSIS/Include + + +# compile gcc flags +ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections + +CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections + +ifeq ($(DEBUG), 1) +CFLAGS += -g -gdwarf-2 +endif + + +# Generate dependency information +CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)" + + +####################################### +# LDFLAGS +####################################### +# link script +LDSCRIPT = STM32G070RBTx_FLASH.ld + +# libraries +LIBS = -lc -lm -lnosys +LIBDIR = +LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections + +# default action: build all +all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin + + +####################################### +# build the application +####################################### +# list of objects +OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o))) +vpath %.c $(sort $(dir $(C_SOURCES))) +# list of ASM program objects +OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o))) +vpath %.s $(sort $(dir $(ASM_SOURCES))) + +$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) + $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@ + +$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR) + $(AS) -c $(CFLAGS) $< -o $@ + +$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile + $(CC) $(OBJECTS) $(LDFLAGS) -o $@ + $(SZ) $@ + +$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR) + $(HEX) $< $@ + +$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR) + $(BIN) $< $@ + +$(BUILD_DIR): + mkdir $@ + +####################################### +# clean up +####################################### +clean: + -rm -fR $(BUILD_DIR) + +####################################### +# dependencies +####################################### +-include $(wildcard $(BUILD_DIR)/*.d) + +# *** EOF *** \ No newline at end of file diff --git a/examples/tos_meets_rust/BSP/STM32G070RBTx_FLASH.ld b/examples/tos_meets_rust/BSP/STM32G070RBTx_FLASH.ld new file mode 100644 index 00000000..319c9ba8 --- /dev/null +++ b/examples/tos_meets_rust/BSP/STM32G070RBTx_FLASH.ld @@ -0,0 +1,189 @@ +/* +****************************************************************************** +** + +** File : LinkerScript.ld +** +** Author : Auto-generated by System Workbench for STM32 +** +** Abstract : Linker script for STM32G070RBTx series +** 128Kbytes FLASH and 36Kbytes RAM +** +** Set heap size, stack size and stack location according +** to application requirements. +** +** Set memory bank area and size if external memory is used. +** +** Target : STMicroelectronics STM32 +** +** Distribution: The file is distributed 鈥渁s is,鈥 without any warranty +** of any kind. +** +***************************************************************************** +** @attention +** +**

© COPYRIGHT(c) 2019 STMicroelectronics

+** +** Redistribution and use in source and binary forms, with or without modification, +** are permitted provided that the following conditions are met: +** 1. Redistributions of source code must retain the above copyright notice, +** this list of conditions and the following disclaimer. +** 2. Redistributions in binary form must reproduce the above copyright notice, +** this list of conditions and the following disclaimer in the documentation +** and/or other materials provided with the distribution. +** 3. Neither the name of STMicroelectronics nor the names of its contributors +** may be used to endorse or promote products derived from this software +** without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +** DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +** +***************************************************************************** +*/ + +/* Entry Point */ +ENTRY(Reset_Handler) + +/* Highest address of the user mode stack */ +_estack = 0x20009000; /* end of RAM */ +/* Generate a link error if heap and stack don't fit into RAM */ +_Min_Heap_Size = 0x200; /* required amount of heap */ +_Min_Stack_Size = 0x400; /* required amount of stack */ + +/* Specify the memory areas */ +MEMORY +{ +RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 36K +FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 128K +} + +/* Define output sections */ +SECTIONS +{ + /* The startup code goes first into FLASH */ + .isr_vector : + { + . = ALIGN(4); + KEEP(*(.isr_vector)) /* Startup code */ + . = ALIGN(4); + } >FLASH + + /* The program code and other data goes into FLASH */ + .text : + { + . = ALIGN(4); + *(.text) /* .text sections (code) */ + *(.text*) /* .text* sections (code) */ + *(.glue_7) /* glue arm to thumb code */ + *(.glue_7t) /* glue thumb to arm code */ + *(.eh_frame) + + KEEP (*(.init)) + KEEP (*(.fini)) + + . = ALIGN(4); + _etext = .; /* define a global symbols at end of code */ + } >FLASH + + /* Constant data goes into FLASH */ + .rodata : + { + . = ALIGN(4); + *(.rodata) /* .rodata sections (constants, strings, etc.) */ + *(.rodata*) /* .rodata* sections (constants, strings, etc.) */ + . = ALIGN(4); + } >FLASH + + .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH + .ARM : { + __exidx_start = .; + *(.ARM.exidx*) + __exidx_end = .; + } >FLASH + + .preinit_array : + { + PROVIDE_HIDDEN (__preinit_array_start = .); + KEEP (*(.preinit_array*)) + PROVIDE_HIDDEN (__preinit_array_end = .); + } >FLASH + .init_array : + { + PROVIDE_HIDDEN (__init_array_start = .); + KEEP (*(SORT(.init_array.*))) + KEEP (*(.init_array*)) + PROVIDE_HIDDEN (__init_array_end = .); + } >FLASH + .fini_array : + { + PROVIDE_HIDDEN (__fini_array_start = .); + KEEP (*(SORT(.fini_array.*))) + KEEP (*(.fini_array*)) + PROVIDE_HIDDEN (__fini_array_end = .); + } >FLASH + + /* used by the startup to initialize data */ + _sidata = LOADADDR(.data); + + /* Initialized data sections goes into RAM, load LMA copy after code */ + .data : + { + . = ALIGN(4); + _sdata = .; /* create a global symbol at data start */ + *(.data) /* .data sections */ + *(.data*) /* .data* sections */ + + . = ALIGN(4); + _edata = .; /* define a global symbol at data end */ + } >RAM AT> FLASH + + + /* Uninitialized data section */ + . = ALIGN(4); + .bss : + { + /* This is used by the startup in order to initialize the .bss secion */ + _sbss = .; /* define a global symbol at bss start */ + __bss_start__ = _sbss; + *(.bss) + *(.bss*) + *(COMMON) + + . = ALIGN(4); + _ebss = .; /* define a global symbol at bss end */ + __bss_end__ = _ebss; + } >RAM + + /* User_heap_stack section, used to check that there is enough RAM left */ + ._user_heap_stack : + { + . = ALIGN(8); + PROVIDE ( end = . ); + PROVIDE ( _end = . ); + . = . + _Min_Heap_Size; + . = . + _Min_Stack_Size; + . = ALIGN(8); + } >RAM + + + + /* Remove information from the standard libraries */ + /DISCARD/ : + { + libc.a ( * ) + libm.a ( * ) + libgcc.a ( * ) + } + + .ARM.attributes 0 : { *(.ARM.attributes) } +} + + diff --git a/examples/tos_meets_rust/BSP/Src/adc.c b/examples/tos_meets_rust/BSP/Src/adc.c new file mode 100644 index 00000000..5fd01959 --- /dev/null +++ b/examples/tos_meets_rust/BSP/Src/adc.c @@ -0,0 +1,126 @@ +/** + ****************************************************************************** + * File Name : ADC.c + * Description : This file provides code for the configuration + * of the ADC instances. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2020 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "adc.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +ADC_HandleTypeDef hadc1; + +/* ADC1 init function */ +void MX_ADC1_Init(void) +{ + ADC_ChannelConfTypeDef sConfig = {0}; + + /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) + */ + hadc1.Instance = ADC1; + hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2; + hadc1.Init.Resolution = ADC_RESOLUTION_12B; + hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; + hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE; + hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + hadc1.Init.LowPowerAutoWait = DISABLE; + hadc1.Init.LowPowerAutoPowerOff = DISABLE; + hadc1.Init.ContinuousConvMode = DISABLE; + hadc1.Init.NbrOfConversion = 1; + hadc1.Init.DiscontinuousConvMode = DISABLE; + hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; + hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; + hadc1.Init.DMAContinuousRequests = DISABLE; + hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED; + hadc1.Init.SamplingTimeCommon1 = ADC_SAMPLETIME_1CYCLE_5; + hadc1.Init.SamplingTimeCommon2 = ADC_SAMPLETIME_1CYCLE_5; + hadc1.Init.OversamplingMode = DISABLE; + hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH; + if (HAL_ADC_Init(&hadc1) != HAL_OK) + { + Error_Handler(); + } + /** Configure Regular Channel + */ + sConfig.Channel = ADC_CHANNEL_0; + sConfig.Rank = ADC_REGULAR_RANK_1; + sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1; + if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) + { + Error_Handler(); + } + +} + +void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle) +{ + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if(adcHandle->Instance==ADC1) + { + /* USER CODE BEGIN ADC1_MspInit 0 */ + + /* USER CODE END ADC1_MspInit 0 */ + /* ADC1 clock enable */ + __HAL_RCC_ADC_CLK_ENABLE(); + + __HAL_RCC_GPIOA_CLK_ENABLE(); + /**ADC1 GPIO Configuration + PA0 ------> ADC1_IN0 + PA1 ------> ADC1_IN1 + */ + GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /* USER CODE BEGIN ADC1_MspInit 1 */ + + /* USER CODE END ADC1_MspInit 1 */ + } +} + +void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle) +{ + + if(adcHandle->Instance==ADC1) + { + /* USER CODE BEGIN ADC1_MspDeInit 0 */ + + /* USER CODE END ADC1_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_ADC_CLK_DISABLE(); + + /**ADC1 GPIO Configuration + PA0 ------> ADC1_IN0 + PA1 ------> ADC1_IN1 + */ + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0|GPIO_PIN_1); + + /* USER CODE BEGIN ADC1_MspDeInit 1 */ + + /* USER CODE END ADC1_MspDeInit 1 */ + } +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/examples/tos_meets_rust/BSP/Src/gpio.c b/examples/tos_meets_rust/BSP/Src/gpio.c new file mode 100644 index 00000000..289a9024 --- /dev/null +++ b/examples/tos_meets_rust/BSP/Src/gpio.c @@ -0,0 +1,71 @@ +/** + ****************************************************************************** + * File Name : gpio.c + * Description : This file provides code for the configuration + * of all used GPIO pins. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2020 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "gpio.h" +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +/*----------------------------------------------------------------------------*/ +/* Configure GPIO */ +/*----------------------------------------------------------------------------*/ +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ + +/** Configure pins +*/ +void MX_GPIO_Init(void) +{ + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + + /* GPIO Ports Clock Enable */ + __HAL_RCC_GPIOC_CLK_ENABLE(); + __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4|GPIO_PIN_9|GPIO_PIN_10, GPIO_PIN_RESET); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1|GPIO_PIN_12, GPIO_PIN_RESET); + + /*Configure GPIO pins : PA4 PA9 PA10 */ + GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_9|GPIO_PIN_10; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /*Configure GPIO pins : PB1 PB12 */ + GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_12; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + +} + +/* USER CODE BEGIN 2 */ + +/* USER CODE END 2 */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/examples/tos_meets_rust/BSP/Src/i2c.c b/examples/tos_meets_rust/BSP/Src/i2c.c new file mode 100644 index 00000000..ceb5b3df --- /dev/null +++ b/examples/tos_meets_rust/BSP/Src/i2c.c @@ -0,0 +1,118 @@ +/** + ****************************************************************************** + * File Name : I2C.c + * Description : This file provides code for the configuration + * of the I2C instances. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2020 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "i2c.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +I2C_HandleTypeDef hi2c2; + +/* I2C2 init function */ +void MX_I2C2_Init(void) +{ + + hi2c2.Instance = I2C2; + hi2c2.Init.Timing = 0x10707DBC; + hi2c2.Init.OwnAddress1 = 0; + hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; + hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; + hi2c2.Init.OwnAddress2 = 0; + hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK; + hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; + hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; + if (HAL_I2C_Init(&hi2c2) != HAL_OK) + { + Error_Handler(); + } + /** Configure Analogue filter + */ + if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK) + { + Error_Handler(); + } + /** Configure Digital filter + */ + if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK) + { + Error_Handler(); + } + +} + +void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle) +{ + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if(i2cHandle->Instance==I2C2) + { + /* USER CODE BEGIN I2C2_MspInit 0 */ + + /* USER CODE END I2C2_MspInit 0 */ + + __HAL_RCC_GPIOB_CLK_ENABLE(); + /**I2C2 GPIO Configuration + PB10 ------> I2C2_SCL + PB11 ------> I2C2_SDA + */ + GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11; + GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; + GPIO_InitStruct.Pull = GPIO_PULLUP; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF6_I2C2; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /* I2C2 clock enable */ + __HAL_RCC_I2C2_CLK_ENABLE(); + /* USER CODE BEGIN I2C2_MspInit 1 */ + + /* USER CODE END I2C2_MspInit 1 */ + } +} + +void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle) +{ + + if(i2cHandle->Instance==I2C2) + { + /* USER CODE BEGIN I2C2_MspDeInit 0 */ + + /* USER CODE END I2C2_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_I2C2_CLK_DISABLE(); + + /**I2C2 GPIO Configuration + PB10 ------> I2C2_SCL + PB11 ------> I2C2_SDA + */ + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10|GPIO_PIN_11); + + /* USER CODE BEGIN I2C2_MspDeInit 1 */ + + /* USER CODE END I2C2_MspDeInit 1 */ + } +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/examples/tos_meets_rust/BSP/Src/main.c b/examples/tos_meets_rust/BSP/Src/main.c new file mode 100644 index 00000000..f80a3d17 --- /dev/null +++ b/examples/tos_meets_rust/BSP/Src/main.c @@ -0,0 +1,32 @@ +/* + + 鎻愪緵 board_init 鍑芥暟, 鏈缁堣皟鐢ㄧ殑鏄 + + tos/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal.h + + 涓彁渚涚殑 HAL_Init() 鍑芥暟 + +*/ +#include "mcu_init.h" + +/* + + 浣嶄簬 tos/platform/vendor_bsp/st/CMSIS/RTOS2/Template/cmsis_os.h + + 鎻愪緵 osKernelXXX 鍑芥暟 + +*/ +#include "cmsis_os.h" + +#define APPLICATION_TASK_STK_SIZE 1024 +extern void application_entry_rust(); +osThreadDef(application_entry_rust, osPriorityNormal, 1, APPLICATION_TASK_STK_SIZE); + +int main(void) +{ + board_init(); + printf("Welcome to TencentOS tiny\r\n"); + osKernelInitialize(); + osThreadCreate(osThread(application_entry_rust), NULL); + osKernelStart(); +} diff --git a/examples/tos_meets_rust/BSP/Src/mcu_init.c b/examples/tos_meets_rust/BSP/Src/mcu_init.c new file mode 100644 index 00000000..95423d98 --- /dev/null +++ b/examples/tos_meets_rust/BSP/Src/mcu_init.c @@ -0,0 +1,130 @@ +#include "mcu_init.h" + +int fputc(int ch, FILE *f) +{ + if (ch == '\n') { + HAL_UART_Transmit(&huart1, (void *)"\r", 1,30000); + } + HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF); + return ch; +} + +int _write(int fd, char *ptr, int len) +{ + (void)HAL_UART_Transmit(&huart1, (uint8_t *)ptr, len, 0xFFFF); + return len; +} + +int fgetc(FILE *f) +{ + /* Place your implementation of fgetc here */ + /* e.g. readwrite a character to the USART2 and Loop until the end of transmission */ + uint8_t ch = 0; + //uint32_t recv_size; + HAL_UART_Receive(&huart1, &ch, 1,30000); + return ch; +} + +void board_init(void) +{ + HAL_Init(); + SystemClock_Config(); + MX_GPIO_Init(); + MX_ADC1_Init(); + MX_I2C2_Init(); + MX_SPI1_Init(); + MX_SPI2_Init(); + MX_USART1_UART_Init(); + //MX_USART2_UART_Init(); + MX_USART4_UART_Init(); + MX_USART3_UART_Init(); +} + +/** + * @brief System Clock Configuration + * @retval None + */ +void SystemClock_Config(void) +{ + RCC_OscInitTypeDef RCC_OscInitStruct = {0}; + RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; + RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; + + /** Configure the main internal regulator output voltage + */ + HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1); + /** Initializes the CPU, AHB and APB busses clocks + */ + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; + RCC_OscInitStruct.HSIState = RCC_HSI_ON; + RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1; + RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; + RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV1; + RCC_OscInitStruct.PLL.PLLN = 8; + RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; + RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2; + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) + { + Error_Handler(); + } + /** Initializes the CPU, AHB and APB busses clocks + */ + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK + |RCC_CLOCKTYPE_PCLK1; + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; + + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) + { + Error_Handler(); + } + /** Initializes the peripherals clocks + */ + PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USART2 + |RCC_PERIPHCLK_ADC; + PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1; + PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1; + PeriphClkInit.AdcClockSelection = RCC_ADCCLKSOURCE_SYSCLK; + if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) + { + Error_Handler(); + } +} + +/* USER CODE BEGIN 4 */ + +/* USER CODE END 4 */ + +/** + * @brief This function is executed in case of error occurrence. + * @retval None + */ +void Error_Handler(void) +{ + /* USER CODE BEGIN Error_Handler_Debug */ + /* User can add his own implementation to report the HAL error return state */ + + /* USER CODE END Error_Handler_Debug */ +} + +#ifdef USE_FULL_ASSERT +/** + * @brief Reports the name of the source file and the source line number + * where the assert_param error has occurred. + * @param file: pointer to the source file name + * @param line: assert_param error line source number + * @retval None + */ +void assert_failed(uint8_t *file, uint32_t line) +{ + /* USER CODE BEGIN 6 */ + /* User can add his own implementation to report the file name and line number, + tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ + /* USER CODE END 6 */ +} +#endif /* USE_FULL_ASSERT */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/examples/tos_meets_rust/BSP/Src/spi.c b/examples/tos_meets_rust/BSP/Src/spi.c new file mode 100644 index 00000000..8edf4b7d --- /dev/null +++ b/examples/tos_meets_rust/BSP/Src/spi.c @@ -0,0 +1,192 @@ +/** + ****************************************************************************** + * File Name : SPI.c + * Description : This file provides code for the configuration + * of the SPI instances. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2020 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "spi.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +SPI_HandleTypeDef hspi1; +SPI_HandleTypeDef hspi2; + +/* SPI1 init function */ +void MX_SPI1_Init(void) +{ + + hspi1.Instance = SPI1; + hspi1.Init.Mode = SPI_MODE_MASTER; + hspi1.Init.Direction = SPI_DIRECTION_2LINES; + hspi1.Init.DataSize = SPI_DATASIZE_8BIT; + hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; + hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; + hspi1.Init.NSS = SPI_NSS_SOFT; + hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4; + hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; + hspi1.Init.TIMode = SPI_TIMODE_DISABLE; + hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; + hspi1.Init.CRCPolynomial = 7; + hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE; + hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE; + if (HAL_SPI_Init(&hspi1) != HAL_OK) + { + Error_Handler(); + } + +} +/* SPI2 init function */ +void MX_SPI2_Init(void) +{ + + hspi2.Instance = SPI2; + hspi2.Init.Mode = SPI_MODE_MASTER; + hspi2.Init.Direction = SPI_DIRECTION_2LINES; + hspi2.Init.DataSize = SPI_DATASIZE_8BIT; + hspi2.Init.CLKPolarity = SPI_POLARITY_LOW; + hspi2.Init.CLKPhase = SPI_PHASE_1EDGE; + hspi2.Init.NSS = SPI_NSS_SOFT; + hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4; + hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB; + hspi2.Init.TIMode = SPI_TIMODE_DISABLE; + hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; + hspi2.Init.CRCPolynomial = 7; + hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE; + hspi2.Init.NSSPMode = SPI_NSS_PULSE_ENABLE; + if (HAL_SPI_Init(&hspi2) != HAL_OK) + { + Error_Handler(); + } + +} + +void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle) +{ + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if(spiHandle->Instance==SPI1) + { + /* USER CODE BEGIN SPI1_MspInit 0 */ + + /* USER CODE END SPI1_MspInit 0 */ + /* SPI1 clock enable */ + __HAL_RCC_SPI1_CLK_ENABLE(); + + __HAL_RCC_GPIOA_CLK_ENABLE(); + /**SPI1 GPIO Configuration + PA5 ------> SPI1_SCK + PA6 ------> SPI1_MISO + PA7 ------> SPI1_MOSI + */ + GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF0_SPI1; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /* USER CODE BEGIN SPI1_MspInit 1 */ + + /* USER CODE END SPI1_MspInit 1 */ + } + else if(spiHandle->Instance==SPI2) + { + /* USER CODE BEGIN SPI2_MspInit 0 */ + + /* USER CODE END SPI2_MspInit 0 */ + /* SPI2 clock enable */ + __HAL_RCC_SPI2_CLK_ENABLE(); + + __HAL_RCC_GPIOC_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + /**SPI2 GPIO Configuration + PC2 ------> SPI2_MISO + PC3 ------> SPI2_MOSI + PB13 ------> SPI2_SCK + */ + GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF1_SPI2; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + + GPIO_InitStruct.Pin = GPIO_PIN_13; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF0_SPI2; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /* USER CODE BEGIN SPI2_MspInit 1 */ + + /* USER CODE END SPI2_MspInit 1 */ + } +} + +void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle) +{ + + if(spiHandle->Instance==SPI1) + { + /* USER CODE BEGIN SPI1_MspDeInit 0 */ + + /* USER CODE END SPI1_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_SPI1_CLK_DISABLE(); + + /**SPI1 GPIO Configuration + PA5 ------> SPI1_SCK + PA6 ------> SPI1_MISO + PA7 ------> SPI1_MOSI + */ + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7); + + /* USER CODE BEGIN SPI1_MspDeInit 1 */ + + /* USER CODE END SPI1_MspDeInit 1 */ + } + else if(spiHandle->Instance==SPI2) + { + /* USER CODE BEGIN SPI2_MspDeInit 0 */ + + /* USER CODE END SPI2_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_SPI2_CLK_DISABLE(); + + /**SPI2 GPIO Configuration + PC2 ------> SPI2_MISO + PC3 ------> SPI2_MOSI + PB13 ------> SPI2_SCK + */ + HAL_GPIO_DeInit(GPIOC, GPIO_PIN_2|GPIO_PIN_3); + + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_13); + + /* USER CODE BEGIN SPI2_MspDeInit 1 */ + + /* USER CODE END SPI2_MspDeInit 1 */ + } +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/examples/tos_meets_rust/BSP/Src/stm32g0xx_hal_msp.c b/examples/tos_meets_rust/BSP/Src/stm32g0xx_hal_msp.c new file mode 100644 index 00000000..344c7e92 --- /dev/null +++ b/examples/tos_meets_rust/BSP/Src/stm32g0xx_hal_msp.c @@ -0,0 +1,84 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * File Name : stm32g0xx_hal_msp.c + * Description : This file provides code for the MSP Initialization + * and de-Initialization codes. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2020 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ +/* USER CODE END Header */ + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* Private typedef -----------------------------------------------------------*/ +/* USER CODE BEGIN TD */ + +/* USER CODE END TD */ + +/* Private define ------------------------------------------------------------*/ +/* USER CODE BEGIN Define */ + +/* USER CODE END Define */ + +/* Private macro -------------------------------------------------------------*/ +/* USER CODE BEGIN Macro */ + +/* USER CODE END Macro */ + +/* Private variables ---------------------------------------------------------*/ +/* USER CODE BEGIN PV */ + +/* USER CODE END PV */ + +/* Private function prototypes -----------------------------------------------*/ +/* USER CODE BEGIN PFP */ + +/* USER CODE END PFP */ + +/* External functions --------------------------------------------------------*/ +/* USER CODE BEGIN ExternalFunctions */ + +/* USER CODE END ExternalFunctions */ + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ +/** + * Initializes the Global MSP. + */ +void HAL_MspInit(void) +{ + /* USER CODE BEGIN MspInit 0 */ + + /* USER CODE END MspInit 0 */ + + __HAL_RCC_SYSCFG_CLK_ENABLE(); + __HAL_RCC_PWR_CLK_ENABLE(); + + /* System interrupt init*/ + + /* USER CODE BEGIN MspInit 1 */ + + /* USER CODE END MspInit 1 */ +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/examples/tos_meets_rust/BSP/Src/stm32g0xx_it.c b/examples/tos_meets_rust/BSP/Src/stm32g0xx_it.c new file mode 100644 index 00000000..2f1a8325 --- /dev/null +++ b/examples/tos_meets_rust/BSP/Src/stm32g0xx_it.c @@ -0,0 +1,213 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file stm32g0xx_it.c + * @brief Interrupt Service Routines. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2020 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ +/* USER CODE END Header */ + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" +#include "stm32g0xx_it.h" +/* Private includes ----------------------------------------------------------*/ +/* USER CODE BEGIN Includes */ +#include "tos_k.h" +#include "tos_at.h" +#include "ch20_parser.h" +/* USER CODE END Includes */ + +/* Private typedef -----------------------------------------------------------*/ +/* USER CODE BEGIN TD */ + +/* USER CODE END TD */ + +/* Private define ------------------------------------------------------------*/ +/* USER CODE BEGIN PD */ + +/* USER CODE END PD */ + +/* Private macro -------------------------------------------------------------*/ +/* USER CODE BEGIN PM */ + +/* USER CODE END PM */ + +/* Private variables ---------------------------------------------------------*/ +/* USER CODE BEGIN PV */ + +/* USER CODE END PV */ + +/* Private function prototypes -----------------------------------------------*/ +/* USER CODE BEGIN PFP */ + +/* USER CODE END PFP */ + +/* Private user code ---------------------------------------------------------*/ +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +/* External variables --------------------------------------------------------*/ +extern UART_HandleTypeDef huart1; +extern UART_HandleTypeDef huart2; +extern UART_HandleTypeDef huart3; +extern UART_HandleTypeDef huart4; +/* USER CODE BEGIN EV */ + +/* USER CODE END EV */ + +/******************************************************************************/ +/* Cortex-M0+ Processor Interruption and Exception Handlers */ +/******************************************************************************/ +/** + * @brief This function handles Non maskable interrupt. + */ +void NMI_Handler(void) +{ + /* USER CODE BEGIN NonMaskableInt_IRQn 0 */ + + /* USER CODE END NonMaskableInt_IRQn 0 */ + /* USER CODE BEGIN NonMaskableInt_IRQn 1 */ + + /* USER CODE END NonMaskableInt_IRQn 1 */ +} + +/** + * @brief This function handles Hard fault interrupt. + */ +void HardFault_Handler(void) +{ + /* USER CODE BEGIN HardFault_IRQn 0 */ + + /* USER CODE END HardFault_IRQn 0 */ + while (1) + { + /* USER CODE BEGIN W1_HardFault_IRQn 0 */ + /* USER CODE END W1_HardFault_IRQn 0 */ + } +} + +/** + * @brief This function handles System service call via SWI instruction. + */ +void SVC_Handler(void) +{ + /* USER CODE BEGIN SVC_IRQn 0 */ + + /* USER CODE END SVC_IRQn 0 */ + /* USER CODE BEGIN SVC_IRQn 1 */ + + /* USER CODE END SVC_IRQn 1 */ +} + +/** + * @brief This function handles Pendable request for system service. + */ +__weak void PendSV_Handler(void) +{ + /* USER CODE BEGIN PendSV_IRQn 0 */ + + /* USER CODE END PendSV_IRQn 0 */ + /* USER CODE BEGIN PendSV_IRQn 1 */ + + /* USER CODE END PendSV_IRQn 1 */ +} + +/** + * @brief This function handles System tick timer. + */ +void SysTick_Handler(void) +{ + /* USER CODE BEGIN SysTick_IRQn 0 */ + + /* USER CODE END SysTick_IRQn 0 */ + HAL_IncTick(); + if(tos_knl_is_running()) + { + tos_knl_irq_enter(); + tos_tick_handler(); + tos_knl_irq_leave(); + } + /* USER CODE BEGIN SysTick_IRQn 1 */ + + /* USER CODE END SysTick_IRQn 1 */ +} + +/******************************************************************************/ +/* STM32G0xx Peripheral Interrupt Handlers */ +/* Add here the Interrupt Handlers for the used peripherals. */ +/* For the available peripheral interrupt handler names, */ +/* please refer to the startup file (startup_stm32g0xx.s). */ +/******************************************************************************/ + +/** + * @brief This function handles USART1 global interrupt / USART1 wake-up interrupt through EXTI line 25. + */ +void USART1_IRQHandler(void) +{ + /* USER CODE BEGIN USART1_IRQn 0 */ + + /* USER CODE END USART1_IRQn 0 */ + HAL_UART_IRQHandler(&huart1); + /* USER CODE BEGIN USART1_IRQn 1 */ + + /* USER CODE END USART1_IRQn 1 */ +} + +/** + * @brief This function handles USART2 global interrupt / USART2 wake-up interrupt through EXTI line 26. + */ +void USART2_IRQHandler(void) +{ + /* USER CODE BEGIN USART2_IRQn 0 */ + + /* USER CODE END USART2_IRQn 0 */ + HAL_UART_IRQHandler(&huart2); + /* USER CODE BEGIN USART2_IRQn 1 */ + + /* USER CODE END USART2_IRQn 1 */ +} + +/** + * @brief This function handles USART3 and USART4 interrupts. + */ +void USART3_4_IRQHandler(void) +{ + /* USER CODE BEGIN USART3_4_IRQn 0 */ + + /* USER CODE END USART3_4_IRQn 0 */ + HAL_UART_IRQHandler(&huart3); + HAL_UART_IRQHandler(&huart4); + /* USER CODE BEGIN USART3_4_IRQn 1 */ + + /* USER CODE END USART3_4_IRQn 1 */ +} + +/* USER CODE BEGIN 1 */ +void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) +{ + /* data is defined in usart.c */ + extern uint8_t data; + extern uint8_t ch20_msg; + + if (huart->Instance == USART2) { + HAL_UART_Receive_IT(&huart2, &data, 1); + tos_at_uart_input_byte(data); + } else if (huart->Instance == USART3) { + HAL_UART_Receive_IT(&huart3, &ch20_msg, 1); + ch20_parser_input_byte(ch20_msg); + } +} +/* USER CODE END 1 */ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/examples/tos_meets_rust/BSP/Src/stm32g0xx_it.c.back b/examples/tos_meets_rust/BSP/Src/stm32g0xx_it.c.back new file mode 100644 index 00000000..9cdf4b93 --- /dev/null +++ b/examples/tos_meets_rust/BSP/Src/stm32g0xx_it.c.back @@ -0,0 +1,192 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file stm32g0xx_it.c + * @brief Interrupt Service Routines. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2020 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ +/* USER CODE END Header */ + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" +#include "stm32g0xx_it.h" +/* Private includes ----------------------------------------------------------*/ +/* USER CODE BEGIN Includes */ +#include "tos_k.h" +/* USER CODE END Includes */ + +/* Private typedef -----------------------------------------------------------*/ +/* USER CODE BEGIN TD */ + +/* USER CODE END TD */ + +/* Private define ------------------------------------------------------------*/ +/* USER CODE BEGIN PD */ + +/* USER CODE END PD */ + +/* Private macro -------------------------------------------------------------*/ +/* USER CODE BEGIN PM */ + +/* USER CODE END PM */ + +/* Private variables ---------------------------------------------------------*/ +/* USER CODE BEGIN PV */ + +/* USER CODE END PV */ + +/* Private function prototypes -----------------------------------------------*/ +/* USER CODE BEGIN PFP */ + +/* USER CODE END PFP */ + +/* Private user code ---------------------------------------------------------*/ +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +/* External variables --------------------------------------------------------*/ +extern UART_HandleTypeDef huart1; +extern UART_HandleTypeDef huart2; +extern UART_HandleTypeDef huart3; +extern UART_HandleTypeDef huart4; +/* USER CODE BEGIN EV */ + +/* USER CODE END EV */ + +/******************************************************************************/ +/* Cortex-M0+ Processor Interruption and Exception Handlers */ +/******************************************************************************/ +/** + * @brief This function handles Non maskable interrupt. + */ +void NMI_Handler(void) +{ + /* USER CODE BEGIN NonMaskableInt_IRQn 0 */ + + /* USER CODE END NonMaskableInt_IRQn 0 */ + /* USER CODE BEGIN NonMaskableInt_IRQn 1 */ + + /* USER CODE END NonMaskableInt_IRQn 1 */ +} + +/** + * @brief This function handles Hard fault interrupt. + */ +void HardFault_Handler(void) +{ + /* USER CODE BEGIN HardFault_IRQn 0 */ + + /* USER CODE END HardFault_IRQn 0 */ + while (1) + { + /* USER CODE BEGIN W1_HardFault_IRQn 0 */ + /* USER CODE END W1_HardFault_IRQn 0 */ + } +} + +/** + * @brief This function handles System service call via SWI instruction. + */ +void SVC_Handler(void) +{ + /* USER CODE BEGIN SVC_IRQn 0 */ + + /* USER CODE END SVC_IRQn 0 */ + /* USER CODE BEGIN SVC_IRQn 1 */ + + /* USER CODE END SVC_IRQn 1 */ +} + +/** + * @brief This function handles Pendable request for system service. + */ +void PendSV_Handler(void) +{ + /* USER CODE BEGIN PendSV_IRQn 0 */ + + /* USER CODE END PendSV_IRQn 0 */ + /* USER CODE BEGIN PendSV_IRQn 1 */ + + /* USER CODE END PendSV_IRQn 1 */ +} + +/** + * @brief This function handles System tick timer. + */ +void SysTick_Handler(void) +{ + /* USER CODE BEGIN SysTick_IRQn 0 */ + + /* USER CODE END SysTick_IRQn 0 */ + HAL_IncTick(); + /* USER CODE BEGIN SysTick_IRQn 1 */ + + /* USER CODE END SysTick_IRQn 1 */ +} + +/******************************************************************************/ +/* STM32G0xx Peripheral Interrupt Handlers */ +/* Add here the Interrupt Handlers for the used peripherals. */ +/* For the available peripheral interrupt handler names, */ +/* please refer to the startup file (startup_stm32g0xx.s). */ +/******************************************************************************/ + +/** + * @brief This function handles USART1 global interrupt / USART1 wake-up interrupt through EXTI line 25. + */ +void USART1_IRQHandler(void) +{ + /* USER CODE BEGIN USART1_IRQn 0 */ + + /* USER CODE END USART1_IRQn 0 */ + HAL_UART_IRQHandler(&huart1); + /* USER CODE BEGIN USART1_IRQn 1 */ + + /* USER CODE END USART1_IRQn 1 */ +} + +/** + * @brief This function handles USART2 global interrupt / USART2 wake-up interrupt through EXTI line 26. + */ +void USART2_IRQHandler(void) +{ + /* USER CODE BEGIN USART2_IRQn 0 */ + + /* USER CODE END USART2_IRQn 0 */ + HAL_UART_IRQHandler(&huart2); + /* USER CODE BEGIN USART2_IRQn 1 */ + + /* USER CODE END USART2_IRQn 1 */ +} + +/** + * @brief This function handles USART3 and USART4 interrupts. + */ +void USART3_4_IRQHandler(void) +{ + /* USER CODE BEGIN USART3_4_IRQn 0 */ + + /* USER CODE END USART3_4_IRQn 0 */ + HAL_UART_IRQHandler(&huart3); + HAL_UART_IRQHandler(&huart4); + /* USER CODE BEGIN USART3_4_IRQn 1 */ + + /* USER CODE END USART3_4_IRQn 1 */ +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/examples/tos_meets_rust/BSP/Src/stm32g0xx_it_module.c.back b/examples/tos_meets_rust/BSP/Src/stm32g0xx_it_module.c.back new file mode 100644 index 00000000..859c5733 --- /dev/null +++ b/examples/tos_meets_rust/BSP/Src/stm32g0xx_it_module.c.back @@ -0,0 +1,208 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file stm32g0xx_it.c + * @brief Interrupt Service Routines. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2020 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ +/* USER CODE END Header */ + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" +#include "stm32g0xx_it.h" +/* Private includes ----------------------------------------------------------*/ +/* USER CODE BEGIN Includes */ +#include "tos_k.h" +#include "tos_at.h" +/* USER CODE END Includes */ + +/* Private typedef -----------------------------------------------------------*/ +/* USER CODE BEGIN TD */ + +/* USER CODE END TD */ + +/* Private define ------------------------------------------------------------*/ +/* USER CODE BEGIN PD */ + +/* USER CODE END PD */ + +/* Private macro -------------------------------------------------------------*/ +/* USER CODE BEGIN PM */ + +/* USER CODE END PM */ + +/* Private variables ---------------------------------------------------------*/ +/* USER CODE BEGIN PV */ + +/* USER CODE END PV */ + +/* Private function prototypes -----------------------------------------------*/ +/* USER CODE BEGIN PFP */ + +/* USER CODE END PFP */ + +/* Private user code ---------------------------------------------------------*/ +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +/* External variables --------------------------------------------------------*/ +extern UART_HandleTypeDef huart1; +extern UART_HandleTypeDef huart2; +extern UART_HandleTypeDef huart3; +extern UART_HandleTypeDef huart4; +/* USER CODE BEGIN EV */ + +/* USER CODE END EV */ + +/******************************************************************************/ +/* Cortex-M0+ Processor Interruption and Exception Handlers */ +/******************************************************************************/ +/** + * @brief This function handles Non maskable interrupt. + */ +void NMI_Handler(void) +{ + /* USER CODE BEGIN NonMaskableInt_IRQn 0 */ + + /* USER CODE END NonMaskableInt_IRQn 0 */ + /* USER CODE BEGIN NonMaskableInt_IRQn 1 */ + + /* USER CODE END NonMaskableInt_IRQn 1 */ +} + +/** + * @brief This function handles Hard fault interrupt. + */ +void HardFault_Handler(void) +{ + /* USER CODE BEGIN HardFault_IRQn 0 */ + + /* USER CODE END HardFault_IRQn 0 */ + while (1) + { + /* USER CODE BEGIN W1_HardFault_IRQn 0 */ + /* USER CODE END W1_HardFault_IRQn 0 */ + } +} + +/** + * @brief This function handles System service call via SWI instruction. + */ +void SVC_Handler(void) +{ + /* USER CODE BEGIN SVC_IRQn 0 */ + + /* USER CODE END SVC_IRQn 0 */ + /* USER CODE BEGIN SVC_IRQn 1 */ + + /* USER CODE END SVC_IRQn 1 */ +} + +/** + * @brief This function handles Pendable request for system service. + */ +__weak void PendSV_Handler(void) +{ + /* USER CODE BEGIN PendSV_IRQn 0 */ + + /* USER CODE END PendSV_IRQn 0 */ + /* USER CODE BEGIN PendSV_IRQn 1 */ + + /* USER CODE END PendSV_IRQn 1 */ +} + +/** + * @brief This function handles System tick timer. + */ +void SysTick_Handler(void) +{ + /* USER CODE BEGIN SysTick_IRQn 0 */ + + /* USER CODE END SysTick_IRQn 0 */ + HAL_IncTick(); + if(tos_knl_is_running()) + { + tos_knl_irq_enter(); + tos_tick_handler(); + tos_knl_irq_leave(); + } + /* USER CODE BEGIN SysTick_IRQn 1 */ + + /* USER CODE END SysTick_IRQn 1 */ +} + +/******************************************************************************/ +/* STM32G0xx Peripheral Interrupt Handlers */ +/* Add here the Interrupt Handlers for the used peripherals. */ +/* For the available peripheral interrupt handler names, */ +/* please refer to the startup file (startup_stm32g0xx.s). */ +/******************************************************************************/ + +/** + * @brief This function handles USART1 global interrupt / USART1 wake-up interrupt through EXTI line 25. + */ +void USART1_IRQHandler(void) +{ + /* USER CODE BEGIN USART1_IRQn 0 */ + + /* USER CODE END USART1_IRQn 0 */ + HAL_UART_IRQHandler(&huart1); + /* USER CODE BEGIN USART1_IRQn 1 */ + + /* USER CODE END USART1_IRQn 1 */ +} + +/** + * @brief This function handles USART2 global interrupt / USART2 wake-up interrupt through EXTI line 26. + */ +void USART2_IRQHandler(void) +{ + /* USER CODE BEGIN USART2_IRQn 0 */ + + /* USER CODE END USART2_IRQn 0 */ + HAL_UART_IRQHandler(&huart2); + /* USER CODE BEGIN USART2_IRQn 1 */ + + /* USER CODE END USART2_IRQn 1 */ +} + +/** + * @brief This function handles USART3 and USART4 interrupts. + */ +void USART3_4_IRQHandler(void) +{ + /* USER CODE BEGIN USART3_4_IRQn 0 */ + + /* USER CODE END USART3_4_IRQn 0 */ + HAL_UART_IRQHandler(&huart3); + HAL_UART_IRQHandler(&huart4); + /* USER CODE BEGIN USART3_4_IRQn 1 */ + + /* USER CODE END USART3_4_IRQn 1 */ +} + +/* USER CODE BEGIN 1 */ +void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) +{ + /* data is defined in usart.c */ + extern uint8_t data; + + if (huart->Instance == USART2) { + HAL_UART_Receive_IT(&huart2, &data, 1); + tos_at_uart_input_byte(data); + } +} +/* USER CODE END 1 */ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/examples/tos_meets_rust/BSP/Src/sys.c b/examples/tos_meets_rust/BSP/Src/sys.c new file mode 100644 index 00000000..1cc132ea --- /dev/null +++ b/examples/tos_meets_rust/BSP/Src/sys.c @@ -0,0 +1,37 @@ +/** + ****************************************************************************** + * File Name : SYS.c + * Description : This file provides code for the configuration + * of the SYS instances. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2020 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "sys.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +/* SYS init function */ +void MX_SYS_Init(void) +{ + +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/examples/tos_meets_rust/BSP/Src/system_stm32g0xx.c b/examples/tos_meets_rust/BSP/Src/system_stm32g0xx.c new file mode 100644 index 00000000..233ab30a --- /dev/null +++ b/examples/tos_meets_rust/BSP/Src/system_stm32g0xx.c @@ -0,0 +1,290 @@ +/** + ****************************************************************************** + * @file system_stm32g0xx.c + * @author MCD Application Team + * @brief CMSIS Cortex-M0+ Device Peripheral Access Layer System Source File + * + * This file provides two functions and one global variable to be called from + * user application: + * - SystemInit(): This function is called at startup just after reset and + * before branch to main program. This call is made inside + * the "startup_stm32g0xx.s" file. + * + * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used + * by the user application to setup the SysTick + * timer or configure other parameters. + * + * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must + * be called whenever the core clock is changed + * during program execution. + * + * After each device reset the HSI (8 MHz then 16 MHz) is used as system clock source. + * Then SystemInit() function is called, in "startup_stm32g0xx.s" file, to + * configure the system clock before to branch to main program. + * + * This file configures the system clock as follows: + *============================================================================= + *----------------------------------------------------------------------------- + * System Clock source | HSI + *----------------------------------------------------------------------------- + * SYSCLK(Hz) | 16000000 + *----------------------------------------------------------------------------- + * HCLK(Hz) | 16000000 + *----------------------------------------------------------------------------- + * AHB Prescaler | 1 + *----------------------------------------------------------------------------- + * APB Prescaler | 1 + *----------------------------------------------------------------------------- + * HSI Division factor | 1 + *----------------------------------------------------------------------------- + * PLL_M | 1 + *----------------------------------------------------------------------------- + * PLL_N | 8 + *----------------------------------------------------------------------------- + * PLL_P | 7 + *----------------------------------------------------------------------------- + * PLL_Q | 2 + *----------------------------------------------------------------------------- + * PLL_R | 2 + *----------------------------------------------------------------------------- + * Require 48MHz for RNG | Disabled + *----------------------------------------------------------------------------- + *============================================================================= + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2018 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ + +/** @addtogroup CMSIS + * @{ + */ + +/** @addtogroup stm32g0xx_system + * @{ + */ + +/** @addtogroup STM32G0xx_System_Private_Includes + * @{ + */ + +#include "stm32g0xx.h" + +#if !defined (HSE_VALUE) +#define HSE_VALUE (8000000UL) /*!< Value of the External oscillator in Hz */ +#endif /* HSE_VALUE */ + +#if !defined (HSI_VALUE) + #define HSI_VALUE (16000000UL) /*!< Value of the Internal oscillator in Hz*/ +#endif /* HSI_VALUE */ + +#if !defined (LSI_VALUE) + #define LSI_VALUE (32000UL) /*!< Value of LSI in Hz*/ +#endif /* LSI_VALUE */ + +#if !defined (LSE_VALUE) + #define LSE_VALUE (32768UL) /*!< Value of LSE in Hz*/ +#endif /* LSE_VALUE */ + +/** + * @} + */ + +/** @addtogroup STM32G0xx_System_Private_TypesDefinitions + * @{ + */ + +/** + * @} + */ + +/** @addtogroup STM32G0xx_System_Private_Defines + * @{ + */ + +/************************* Miscellaneous Configuration ************************/ +/*!< Uncomment the following line if you need to relocate your vector Table in + Internal SRAM. */ +/* #define VECT_TAB_SRAM */ +#define VECT_TAB_OFFSET 0x0U /*!< Vector Table base offset field. + This value must be a multiple of 0x100. */ +/******************************************************************************/ +/** + * @} + */ + +/** @addtogroup STM32G0xx_System_Private_Macros + * @{ + */ + +/** + * @} + */ + +/** @addtogroup STM32G0xx_System_Private_Variables + * @{ + */ + /* The SystemCoreClock variable is updated in three ways: + 1) by calling CMSIS function SystemCoreClockUpdate() + 2) by calling HAL API function HAL_RCC_GetHCLKFreq() + 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency + Note: If you use this function to configure the system clock; then there + is no need to call the 2 first functions listed above, since SystemCoreClock + variable is updated automatically. + */ + uint32_t SystemCoreClock = 16000000UL; + + const uint32_t AHBPrescTable[16UL] = {0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 1UL, 2UL, 3UL, 4UL, 6UL, 7UL, 8UL, 9UL}; + const uint32_t APBPrescTable[8UL] = {0UL, 0UL, 0UL, 0UL, 1UL, 2UL, 3UL, 4UL}; + +/** + * @} + */ + +/** @addtogroup STM32G0xx_System_Private_FunctionPrototypes + * @{ + */ + +/** + * @} + */ + +/** @addtogroup STM32G0xx_System_Private_Functions + * @{ + */ + +/** + * @brief Setup the microcontroller system. + * @param None + * @retval None + */ +void SystemInit(void) +{ + /* Configure the Vector Table location add offset address ------------------*/ +#ifdef VECT_TAB_SRAM + SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */ +#else + SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */ +#endif +} + +/** + * @brief Update SystemCoreClock variable according to Clock Register Values. + * The SystemCoreClock variable contains the core clock (HCLK), it can + * be used by the user application to setup the SysTick timer or configure + * other parameters. + * + * @note Each time the core clock (HCLK) changes, this function must be called + * to update SystemCoreClock variable value. Otherwise, any configuration + * based on this variable will be incorrect. + * + * @note - The system frequency computed by this function is not the real + * frequency in the chip. It is calculated based on the predefined + * constant and the selected clock source: + * + * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**) / HSI division factor + * + * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***) + * + * - If SYSCLK source is LSI, SystemCoreClock will contain the LSI_VALUE + * + * - If SYSCLK source is LSE, SystemCoreClock will contain the LSE_VALUE + * + * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***) + * or HSI_VALUE(*) multiplied/divided by the PLL factors. + * + * (**) HSI_VALUE is a constant defined in stm32g0xx_hal_conf.h file (default value + * 16 MHz) but the real value may vary depending on the variations + * in voltage and temperature. + * + * (***) HSE_VALUE is a constant defined in stm32g0xx_hal_conf.h file (default value + * 8 MHz), user has to ensure that HSE_VALUE is same as the real + * frequency of the crystal used. Otherwise, this function may + * have wrong result. + * + * - The result of this function could be not correct when using fractional + * value for HSE crystal. + * + * @param None + * @retval None + */ +void SystemCoreClockUpdate(void) +{ + uint32_t tmp; + uint32_t pllvco; + uint32_t pllr; + uint32_t pllsource; + uint32_t pllm; + uint32_t hsidiv; + + /* Get SYSCLK source -------------------------------------------------------*/ + switch (RCC->CFGR & RCC_CFGR_SWS) + { + case RCC_CFGR_SWS_HSE: /* HSE used as system clock */ + SystemCoreClock = HSE_VALUE; + break; + + case RCC_CFGR_SWS_LSI: /* LSI used as system clock */ + SystemCoreClock = LSI_VALUE; + break; + + case RCC_CFGR_SWS_LSE: /* LSE used as system clock */ + SystemCoreClock = LSE_VALUE; + break; + + case RCC_CFGR_SWS_PLL: /* PLL used as system clock */ + /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN + SYSCLK = PLL_VCO / PLLR + */ + pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC); + pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1UL; + + if(pllsource == 0x03UL) /* HSE used as PLL clock source */ + { + pllvco = (HSE_VALUE / pllm); + } + else /* HSI used as PLL clock source */ + { + pllvco = (HSI_VALUE / pllm); + } + pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos); + pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos) + 1UL); + + SystemCoreClock = pllvco/pllr; + break; + + case RCC_CFGR_SWS_HSI: /* HSI used as system clock */ + default: /* HSI used as system clock */ + hsidiv = (1UL << ((READ_BIT(RCC->CR, RCC_CR_HSIDIV))>> RCC_CR_HSIDIV_Pos)); + SystemCoreClock = (HSI_VALUE/hsidiv); + break; + } + /* Compute HCLK clock frequency --------------------------------------------*/ + /* Get HCLK prescaler */ + tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos)]; + /* HCLK clock frequency */ + SystemCoreClock >>= tmp; +} + + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/examples/tos_meets_rust/BSP/Src/usart.c b/examples/tos_meets_rust/BSP/Src/usart.c new file mode 100644 index 00000000..08d4652c --- /dev/null +++ b/examples/tos_meets_rust/BSP/Src/usart.c @@ -0,0 +1,365 @@ +/** + ****************************************************************************** + * File Name : USART.c + * Description : This file provides code for the configuration + * of the USART instances. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2020 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "usart.h" + +/* USER CODE BEGIN 0 */ +uint8_t data; +uint8_t ch20_msg; +/* USER CODE END 0 */ + +UART_HandleTypeDef huart1; +UART_HandleTypeDef huart2; +UART_HandleTypeDef huart3; +UART_HandleTypeDef huart4; + +/* USART1 init function */ + +void MX_USART1_UART_Init(void) +{ + + huart1.Instance = USART1; + huart1.Init.BaudRate = 115200; + huart1.Init.WordLength = UART_WORDLENGTH_8B; + huart1.Init.StopBits = UART_STOPBITS_1; + huart1.Init.Parity = UART_PARITY_NONE; + huart1.Init.Mode = UART_MODE_TX_RX; + huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; + huart1.Init.OverSampling = UART_OVERSAMPLING_16; + huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; + huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1; + huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; + if (HAL_UART_Init(&huart1) != HAL_OK) + { + Error_Handler(); + } + if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK) + { + Error_Handler(); + } + if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK) + { + Error_Handler(); + } + if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK) + { + Error_Handler(); + } + +} +/* USART2 init function */ + +void MX_USART2_UART_Init(void) +{ + + huart2.Instance = USART2; + huart2.Init.BaudRate = 115200; + huart2.Init.WordLength = UART_WORDLENGTH_8B; + huart2.Init.StopBits = UART_STOPBITS_1; + huart2.Init.Parity = UART_PARITY_NONE; + huart2.Init.Mode = UART_MODE_TX_RX; + huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; + huart2.Init.OverSampling = UART_OVERSAMPLING_16; + huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; + huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1; + huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; + if (HAL_UART_Init(&huart2) != HAL_OK) + { + Error_Handler(); + } + if (HAL_UARTEx_SetTxFifoThreshold(&huart2, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK) + { + Error_Handler(); + } + if (HAL_UARTEx_SetRxFifoThreshold(&huart2, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK) + { + Error_Handler(); + } + if (HAL_UARTEx_DisableFifoMode(&huart2) != HAL_OK) + { + Error_Handler(); + } + HAL_UART_Receive_IT(&huart2, &data, 1); + +} +/* USART3 init function */ + +void MX_USART3_UART_Init(void) +{ + + huart3.Instance = USART3; + huart3.Init.BaudRate = 9600; + huart3.Init.WordLength = UART_WORDLENGTH_8B; + huart3.Init.StopBits = UART_STOPBITS_1; + huart3.Init.Parity = UART_PARITY_NONE; + huart3.Init.Mode = UART_MODE_TX_RX; + huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE; + huart3.Init.OverSampling = UART_OVERSAMPLING_16; + huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; + huart3.Init.ClockPrescaler = UART_PRESCALER_DIV1; + huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; + if (HAL_UART_Init(&huart3) != HAL_OK) + { + Error_Handler(); + } + HAL_UART_Receive_IT(&huart3, &ch20_msg, 1); + +} +/* USART4 init function */ + +void MX_USART4_UART_Init(void) +{ + + huart4.Instance = USART4; + huart4.Init.BaudRate = 115200; + huart4.Init.WordLength = UART_WORDLENGTH_8B; + huart4.Init.StopBits = UART_STOPBITS_1; + huart4.Init.Parity = UART_PARITY_NONE; + huart4.Init.Mode = UART_MODE_TX_RX; + huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE; + huart4.Init.OverSampling = UART_OVERSAMPLING_16; + huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; + huart4.Init.ClockPrescaler = UART_PRESCALER_DIV1; + huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; + if (HAL_UART_Init(&huart4) != HAL_OK) + { + Error_Handler(); + } + +} + +void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) +{ + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if(uartHandle->Instance==USART1) + { + /* USER CODE BEGIN USART1_MspInit 0 */ + + /* USER CODE END USART1_MspInit 0 */ + /* USART1 clock enable */ + __HAL_RCC_USART1_CLK_ENABLE(); + + __HAL_RCC_GPIOC_CLK_ENABLE(); + /**USART1 GPIO Configuration + PC4 ------> USART1_TX + PC5 ------> USART1_RX + */ + GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF1_USART1; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + + /* USART1 interrupt Init */ + HAL_NVIC_SetPriority(USART1_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(USART1_IRQn); + /* USER CODE BEGIN USART1_MspInit 1 */ + + /* USER CODE END USART1_MspInit 1 */ + } + else if(uartHandle->Instance==USART2) + { + /* USER CODE BEGIN USART2_MspInit 0 */ + + /* USER CODE END USART2_MspInit 0 */ + /* USART2 clock enable */ + __HAL_RCC_USART2_CLK_ENABLE(); + + __HAL_RCC_GPIOA_CLK_ENABLE(); + /**USART2 GPIO Configuration + PA2 ------> USART2_TX + PA3 ------> USART2_RX + */ + GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF1_USART2; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /* USART2 interrupt Init */ + HAL_NVIC_SetPriority(USART2_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(USART2_IRQn); + /* USER CODE BEGIN USART2_MspInit 1 */ + + /* USER CODE END USART2_MspInit 1 */ + } + else if(uartHandle->Instance==USART3) + { + /* USER CODE BEGIN USART3_MspInit 0 */ + + /* USER CODE END USART3_MspInit 0 */ + /* USART3 clock enable */ + __HAL_RCC_USART3_CLK_ENABLE(); + + __HAL_RCC_GPIOB_CLK_ENABLE(); + /**USART3 GPIO Configuration + PB8 ------> USART3_TX + PB9 ------> USART3_RX + */ + GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF4_USART3; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /* USART3 interrupt Init */ + HAL_NVIC_SetPriority(USART3_4_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(USART3_4_IRQn); + /* USER CODE BEGIN USART3_MspInit 1 */ + + /* USER CODE END USART3_MspInit 1 */ + } + else if(uartHandle->Instance==USART4) + { + /* USER CODE BEGIN USART4_MspInit 0 */ + + /* USER CODE END USART4_MspInit 0 */ + /* USART4 clock enable */ + __HAL_RCC_USART4_CLK_ENABLE(); + + __HAL_RCC_GPIOC_CLK_ENABLE(); + /**USART4 GPIO Configuration + PC11 ------> USART4_RX + PC10 ------> USART4_TX + */ + GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_10; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF1_USART4; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + + /* USART4 interrupt Init */ + HAL_NVIC_SetPriority(USART3_4_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(USART3_4_IRQn); + /* USER CODE BEGIN USART4_MspInit 1 */ + + /* USER CODE END USART4_MspInit 1 */ + } +} + +void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) +{ + + if(uartHandle->Instance==USART1) + { + /* USER CODE BEGIN USART1_MspDeInit 0 */ + + /* USER CODE END USART1_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_USART1_CLK_DISABLE(); + + /**USART1 GPIO Configuration + PC4 ------> USART1_TX + PC5 ------> USART1_RX + */ + HAL_GPIO_DeInit(GPIOC, GPIO_PIN_4|GPIO_PIN_5); + + /* USART1 interrupt Deinit */ + HAL_NVIC_DisableIRQ(USART1_IRQn); + /* USER CODE BEGIN USART1_MspDeInit 1 */ + + /* USER CODE END USART1_MspDeInit 1 */ + } + else if(uartHandle->Instance==USART2) + { + /* USER CODE BEGIN USART2_MspDeInit 0 */ + + /* USER CODE END USART2_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_USART2_CLK_DISABLE(); + + /**USART2 GPIO Configuration + PA2 ------> USART2_TX + PA3 ------> USART2_RX + */ + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3); + + /* USART2 interrupt Deinit */ + HAL_NVIC_DisableIRQ(USART2_IRQn); + /* USER CODE BEGIN USART2_MspDeInit 1 */ + + /* USER CODE END USART2_MspDeInit 1 */ + } + else if(uartHandle->Instance==USART3) + { + /* USER CODE BEGIN USART3_MspDeInit 0 */ + + /* USER CODE END USART3_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_USART3_CLK_DISABLE(); + + /**USART3 GPIO Configuration + PB8 ------> USART3_TX + PB9 ------> USART3_RX + */ + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8|GPIO_PIN_9); + + /* USART3 interrupt Deinit */ + /* USER CODE BEGIN USART3:USART3_4_IRQn disable */ + /** + * Uncomment the line below to disable the "USART3_4_IRQn" interrupt + * Be aware, disabling shared interrupt may affect other IPs + */ + /* HAL_NVIC_DisableIRQ(USART3_4_IRQn); */ + /* USER CODE END USART3:USART3_4_IRQn disable */ + + /* USER CODE BEGIN USART3_MspDeInit 1 */ + + /* USER CODE END USART3_MspDeInit 1 */ + } + else if(uartHandle->Instance==USART4) + { + /* USER CODE BEGIN USART4_MspDeInit 0 */ + + /* USER CODE END USART4_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_USART4_CLK_DISABLE(); + + /**USART4 GPIO Configuration + PC11 ------> USART4_RX + PC10 ------> USART4_TX + */ + HAL_GPIO_DeInit(GPIOC, GPIO_PIN_11|GPIO_PIN_10); + + /* USART4 interrupt Deinit */ + /* USER CODE BEGIN USART4:USART3_4_IRQn disable */ + /** + * Uncomment the line below to disable the "USART3_4_IRQn" interrupt + * Be aware, disabling shared interrupt may affect other IPs + */ + /* HAL_NVIC_DisableIRQ(USART3_4_IRQn); */ + /* USER CODE END USART4:USART3_4_IRQn disable */ + + /* USER CODE BEGIN USART4_MspDeInit 1 */ + + /* USER CODE END USART4_MspDeInit 1 */ + } +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/examples/tos_meets_rust/BSP/TencentOS_tiny_EVB_G0.ioc b/examples/tos_meets_rust/BSP/TencentOS_tiny_EVB_G0.ioc new file mode 100644 index 00000000..99f1fbf6 --- /dev/null +++ b/examples/tos_meets_rust/BSP/TencentOS_tiny_EVB_G0.ioc @@ -0,0 +1,207 @@ +#MicroXplorer Configuration settings - do not modify +ADC1.Channel-0\#ChannelRegularConversion=ADC_CHANNEL_0 +ADC1.IPParameters=Rank-0\#ChannelRegularConversion,Channel-0\#ChannelRegularConversion,SamplingTime-0\#ChannelRegularConversion,NbrOfConversionFlag,master +ADC1.NbrOfConversionFlag=1 +ADC1.Rank-0\#ChannelRegularConversion=1 +ADC1.SamplingTime-0\#ChannelRegularConversion=ADC_SAMPLINGTIME_COMMON_1 +ADC1.master=1 +File.Version=6 +I2C2.IPParameters=Timing +I2C2.Timing=0x10707DBC +KeepUserPlacement=false +Mcu.Family=STM32G0 +Mcu.IP0=ADC1 +Mcu.IP1=I2C2 +Mcu.IP10=USART4 +Mcu.IP2=NVIC +Mcu.IP3=RCC +Mcu.IP4=SPI1 +Mcu.IP5=SPI2 +Mcu.IP6=SYS +Mcu.IP7=USART1 +Mcu.IP8=USART2 +Mcu.IP9=USART3 +Mcu.IPNb=11 +Mcu.Name=STM32G070RBTx +Mcu.Package=LQFP64 +Mcu.Pin0=PC11 +Mcu.Pin1=PC2 +Mcu.Pin10=PA7 +Mcu.Pin11=PC4 +Mcu.Pin12=PC5 +Mcu.Pin13=PB1 +Mcu.Pin14=PB10 +Mcu.Pin15=PB11 +Mcu.Pin16=PB12 +Mcu.Pin17=PB13 +Mcu.Pin18=PA9 +Mcu.Pin19=PA10 +Mcu.Pin2=PC3 +Mcu.Pin20=PB8 +Mcu.Pin21=PB9 +Mcu.Pin22=PC10 +Mcu.Pin23=VP_SYS_VS_Systick +Mcu.Pin24=VP_SYS_VS_DBSignals +Mcu.Pin3=PA0 +Mcu.Pin4=PA1 +Mcu.Pin5=PA2 +Mcu.Pin6=PA3 +Mcu.Pin7=PA4 +Mcu.Pin8=PA5 +Mcu.Pin9=PA6 +Mcu.PinsNb=25 +Mcu.ThirdPartyNb=0 +Mcu.UserConstants= +Mcu.UserName=STM32G070RBTx +MxCube.Version=5.4.0 +MxDb.Version=DB.5.0.40 +NVIC.ForceEnableDMAVector=true +NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false +NVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:true\:false\:false +NVIC.PendSV_IRQn=true\:0\:0\:false\:false\:true\:false\:false +NVIC.SVC_IRQn=true\:0\:0\:false\:false\:true\:false\:false +NVIC.SysTick_IRQn=true\:0\:0\:false\:false\:true\:false\:true +NVIC.USART1_IRQn=true\:0\:0\:false\:false\:true\:true\:true +NVIC.USART2_IRQn=true\:0\:0\:false\:false\:true\:true\:true +NVIC.USART3_4_IRQn=true\:0\:0\:false\:false\:true\:true\:true +PA0.Mode=IN0 +PA0.Signal=ADC1_IN0 +PA1.Mode=IN1 +PA1.Signal=ADC1_IN1 +PA10.Locked=true +PA10.Signal=GPIO_Output +PA2.Mode=Asynchronous +PA2.Signal=USART2_TX +PA3.Mode=Asynchronous +PA3.Signal=USART2_RX +PA4.Locked=true +PA4.Signal=GPIO_Output +PA5.Locked=true +PA5.Mode=Full_Duplex_Master +PA5.Signal=SPI1_SCK +PA6.Mode=Full_Duplex_Master +PA6.Signal=SPI1_MISO +PA7.Mode=Full_Duplex_Master +PA7.Signal=SPI1_MOSI +PA9.Locked=true +PA9.Signal=GPIO_Output +PB1.Locked=true +PB1.Signal=GPIO_Output +PB10.Mode=I2C +PB10.Signal=I2C2_SCL +PB11.Mode=I2C +PB11.Signal=I2C2_SDA +PB12.Locked=true +PB12.Signal=GPIO_Output +PB13.Locked=true +PB13.Mode=Full_Duplex_Master +PB13.Signal=SPI2_SCK +PB8.Locked=true +PB8.Mode=Asynchronous +PB8.Signal=USART3_TX +PB9.Locked=true +PB9.Mode=Asynchronous +PB9.Signal=USART3_RX +PC10.Mode=Asynchronous +PC10.Signal=USART4_TX +PC11.Mode=Asynchronous +PC11.Signal=USART4_RX +PC2.Mode=Full_Duplex_Master +PC2.Signal=SPI2_MISO +PC3.Mode=Full_Duplex_Master +PC3.Signal=SPI2_MOSI +PC4.Mode=Asynchronous +PC4.Signal=USART1_TX +PC5.Mode=Asynchronous +PC5.Signal=USART1_RX +PCC.Checker=false +PCC.Line=STM32G0x0 Value line +PCC.MCU=STM32G070RBTx +PCC.PartNumber=STM32G070RBTx +PCC.Seq0=0 +PCC.Series=STM32G0 +PCC.Temperature=25 +PCC.Vdd=3.0 +PinOutPanel.RotationAngle=0 +ProjectManager.AskForMigrate=true +ProjectManager.BackupPrevious=false +ProjectManager.CompilerOptimize=6 +ProjectManager.ComputerToolchain=false +ProjectManager.CoupleFile=true +ProjectManager.CustomerFirmwarePackage= +ProjectManager.DefaultFWLocation=true +ProjectManager.DeletePrevious=true +ProjectManager.DeviceId=STM32G070RBTx +ProjectManager.FirmwarePackage=STM32Cube FW_G0 V1.3.0 +ProjectManager.FreePins=false +ProjectManager.HalAssertFull=false +ProjectManager.HeapSize=0x200 +ProjectManager.KeepUserCode=true +ProjectManager.LastFirmware=true +ProjectManager.LibraryCopy=0 +ProjectManager.MainLocation=Src +ProjectManager.NoMain=false +ProjectManager.PreviousToolchain= +ProjectManager.ProjectBuild=false +ProjectManager.ProjectFileName=TencentOS_tiny_EVB_G0.ioc +ProjectManager.ProjectName=TencentOS_tiny_EVB_G0 +ProjectManager.StackSize=0x400 +ProjectManager.TargetToolchain=Makefile +ProjectManager.ToolChainLocation= +ProjectManager.UnderRoot=false +ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-SystemClock_Config-RCC-false-HAL-false,3-MX_ADC1_Init-ADC1-false-HAL-true,4-MX_I2C2_Init-I2C2-false-HAL-true,5-MX_SPI1_Init-SPI1-false-HAL-true,6-MX_SPI2_Init-SPI2-false-HAL-true,7-MX_USART1_UART_Init-USART1-false-HAL-true,8-MX_USART2_UART_Init-USART2-false-HAL-true,9-MX_USART4_UART_Init-USART4-false-HAL-true,10-MX_USART3_UART_Init-USART3-false-HAL-true +RCC.ADCFreq_Value=64000000 +RCC.AHBFreq_Value=64000000 +RCC.APBFreq_Value=64000000 +RCC.APBTimFreq_Value=64000000 +RCC.CortexFreq_Value=64000000 +RCC.EXTERNAL_CLOCK_VALUE=48000 +RCC.FCLKCortexFreq_Value=64000000 +RCC.FamilyName=M +RCC.HCLKFreq_Value=64000000 +RCC.HSE_VALUE=8000000 +RCC.HSI_VALUE=16000000 +RCC.I2C1Freq_Value=64000000 +RCC.I2S1Freq_Value=64000000 +RCC.IPParameters=ADCFreq_Value,AHBFreq_Value,APBFreq_Value,APBTimFreq_Value,CortexFreq_Value,EXTERNAL_CLOCK_VALUE,FCLKCortexFreq_Value,FamilyName,HCLKFreq_Value,HSE_VALUE,HSI_VALUE,I2C1Freq_Value,I2S1Freq_Value,LSCOPinFreq_Value,LSE_VALUE,LSI_VALUE,MCO1PinFreq_Value,PLLPoutputFreq_Value,PLLRCLKFreq_Value,PWRFreq_Value,SYSCLKFreq_VALUE,SYSCLKSource,USART1Freq_Value,USART2Freq_Value,VCOInputFreq_Value,VCOOutputFreq_Value +RCC.LSCOPinFreq_Value=32000 +RCC.LSE_VALUE=32768 +RCC.LSI_VALUE=32000 +RCC.MCO1PinFreq_Value=64000000 +RCC.PLLPoutputFreq_Value=64000000 +RCC.PLLRCLKFreq_Value=64000000 +RCC.PWRFreq_Value=64000000 +RCC.SYSCLKFreq_VALUE=64000000 +RCC.SYSCLKSource=RCC_SYSCLKSOURCE_PLLCLK +RCC.USART1Freq_Value=64000000 +RCC.USART2Freq_Value=64000000 +RCC.VCOInputFreq_Value=16000000 +RCC.VCOOutputFreq_Value=128000000 +SPI1.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_4 +SPI1.CalculateBaudRate=16.0 MBits/s +SPI1.DataSize=SPI_DATASIZE_8BIT +SPI1.Direction=SPI_DIRECTION_2LINES +SPI1.IPParameters=VirtualType,Mode,Direction,CalculateBaudRate,DataSize,BaudRatePrescaler +SPI1.Mode=SPI_MODE_MASTER +SPI1.VirtualType=VM_MASTER +SPI2.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_4 +SPI2.CalculateBaudRate=16.0 MBits/s +SPI2.DataSize=SPI_DATASIZE_8BIT +SPI2.Direction=SPI_DIRECTION_2LINES +SPI2.IPParameters=VirtualType,Mode,Direction,CalculateBaudRate,DataSize,BaudRatePrescaler +SPI2.Mode=SPI_MODE_MASTER +SPI2.VirtualType=VM_MASTER +USART1.IPParameters=VirtualMode-Asynchronous +USART1.VirtualMode-Asynchronous=VM_ASYNC +USART2.IPParameters=VirtualMode-Asynchronous +USART2.VirtualMode-Asynchronous=VM_ASYNC +USART3.BaudRate=9600 +USART3.IPParameters=VirtualMode-Asynchronous,BaudRate +USART3.VirtualMode-Asynchronous=VM_ASYNC +USART4.IPParameters=VirtualMode-Asynchronous +USART4.VirtualMode-Asynchronous=VM_ASYNC +VP_SYS_VS_DBSignals.Mode=DisableDeadBatterySignals +VP_SYS_VS_DBSignals.Signal=SYS_VS_DBSignals +VP_SYS_VS_Systick.Mode=SysTick +VP_SYS_VS_Systick.Signal=SYS_VS_Systick +board=custom diff --git a/examples/tos_meets_rust/BSP/startup_stm32g070xx.s b/examples/tos_meets_rust/BSP/startup_stm32g070xx.s new file mode 100644 index 00000000..f4a53e2b --- /dev/null +++ b/examples/tos_meets_rust/BSP/startup_stm32g070xx.s @@ -0,0 +1,275 @@ +/** + ****************************************************************************** + * @file startup_stm32g070xx.s + * @author MCD Application Team + * @brief STM32G070xx devices vector table for SW4STM32 toolchain. + * This module performs: + * - Set the initial SP + * - Set the initial PC == Reset_Handler, + * - Set the vector table entries with the exceptions ISR address + * - Branches to main in the C library (which eventually + * calls main()). + * After Reset the Cortex-M0+ processor is in Thread mode, + * priority is Privileged, and the Stack is set to Main. + ****************************************************************************** + * @attention + * + * Copyright (c) 2018 STMicroelectronics. All rights reserved. + * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ + + .syntax unified + .cpu cortex-m0 + .fpu softvfp + .thumb + +.global g_pfnVectors +.global Default_Handler + +/* start address for the initialization values of the .data section. +defined in linker script */ +.word _sidata +/* start address for the .data section. defined in linker script */ +.word _sdata +/* end address for the .data section. defined in linker script */ +.word _edata +/* start address for the .bss section. defined in linker script */ +.word _sbss +/* end address for the .bss section. defined in linker script */ +.word _ebss + + .section .text.Reset_Handler + .weak Reset_Handler + .type Reset_Handler, %function +Reset_Handler: + ldr r0, =_estack + mov sp, r0 /* set stack pointer */ + +/* Copy the data segment initializers from flash to SRAM */ + movs r1, #0 + b LoopCopyDataInit + +CopyDataInit: + ldr r3, =_sidata + ldr r3, [r3, r1] + str r3, [r0, r1] + adds r1, r1, #4 + +LoopCopyDataInit: + ldr r0, =_sdata + ldr r3, =_edata + adds r2, r0, r1 + cmp r2, r3 + bcc CopyDataInit + ldr r2, =_sbss + b LoopFillZerobss +/* Zero fill the bss segment. */ +FillZerobss: + movs r3, #0 + str r3, [r2] + adds r2, r2, #4 + + +LoopFillZerobss: + ldr r3, = _ebss + cmp r2, r3 + bcc FillZerobss + +/* Call the clock system intitialization function.*/ + bl SystemInit +/* Call static constructors */ + bl __libc_init_array +/* Call the application's entry point.*/ + bl main + +LoopForever: + b LoopForever + + +.size Reset_Handler, .-Reset_Handler + +/** + * @brief This is the code that gets called when the processor receives an + * unexpected interrupt. This simply enters an infinite loop, preserving + * the system state for examination by a debugger. + * + * @param None + * @retval : None +*/ + .section .text.Default_Handler,"ax",%progbits +Default_Handler: +Infinite_Loop: + b Infinite_Loop + .size Default_Handler, .-Default_Handler +/****************************************************************************** +* +* The minimal vector table for a Cortex M0. Note that the proper constructs +* must be placed on this to ensure that it ends up at physical address +* 0x0000.0000. +* +******************************************************************************/ + .section .isr_vector,"a",%progbits + .type g_pfnVectors, %object + .size g_pfnVectors, .-g_pfnVectors + + +g_pfnVectors: + .word _estack + .word Reset_Handler + .word NMI_Handler + .word HardFault_Handler + .word 0 + .word 0 + .word 0 + .word 0 + .word 0 + .word 0 + .word 0 + .word SVC_Handler + .word 0 + .word 0 + .word PendSV_Handler + .word SysTick_Handler + .word WWDG_IRQHandler /* Window WatchDog */ + .word 0 /* reserved */ + .word RTC_TAMP_IRQHandler /* RTC through the EXTI line */ + .word FLASH_IRQHandler /* FLASH */ + .word RCC_IRQHandler /* RCC */ + .word EXTI0_1_IRQHandler /* EXTI Line 0 and 1 */ + .word EXTI2_3_IRQHandler /* EXTI Line 2 and 3 */ + .word EXTI4_15_IRQHandler /* EXTI Line 4 to 15 */ + .word 0 /* reserved */ + .word DMA1_Channel1_IRQHandler /* DMA1 Channel 1 */ + .word DMA1_Channel2_3_IRQHandler /* DMA1 Channel 2 and Channel 3 */ + .word DMA1_Ch4_7_DMAMUX1_OVR_IRQHandler /* DMA1 Channel 4 to Channel 7, DMAMUX1 overrun */ + .word ADC1_IRQHandler /* ADC1 */ + .word TIM1_BRK_UP_TRG_COM_IRQHandler /* TIM1 Break, Update, Trigger and Commutation */ + .word TIM1_CC_IRQHandler /* TIM1 Capture Compare */ + .word 0 /* reserved */ + .word TIM3_IRQHandler /* TIM3 */ + .word TIM6_IRQHandler /* TIM6 */ + .word TIM7_IRQHandler /* TIM7 */ + .word TIM14_IRQHandler /* TIM14 */ + .word TIM15_IRQHandler /* TIM15 */ + .word TIM16_IRQHandler /* TIM16 */ + .word TIM17_IRQHandler /* TIM17 */ + .word I2C1_IRQHandler /* I2C1 */ + .word I2C2_IRQHandler /* I2C2 */ + .word SPI1_IRQHandler /* SPI1 */ + .word SPI2_IRQHandler /* SPI2 */ + .word USART1_IRQHandler /* USART1 */ + .word USART2_IRQHandler /* USART2 */ + .word USART3_4_IRQHandler /* USART3, USART4 */ + +/******************************************************************************* +* +* Provide weak aliases for each Exception handler to the Default_Handler. +* As they are weak aliases, any function with the same name will override +* this definition. +* +*******************************************************************************/ + + .weak NMI_Handler + .thumb_set NMI_Handler,Default_Handler + + .weak HardFault_Handler + .thumb_set HardFault_Handler,Default_Handler + + .weak SVC_Handler + .thumb_set SVC_Handler,Default_Handler + + .weak PendSV_Handler + .thumb_set PendSV_Handler,Default_Handler + + .weak SysTick_Handler + .thumb_set SysTick_Handler,Default_Handler + + .weak WWDG_IRQHandler + .thumb_set WWDG_IRQHandler,Default_Handler + + .weak RTC_TAMP_IRQHandler + .thumb_set RTC_TAMP_IRQHandler,Default_Handler + + .weak FLASH_IRQHandler + .thumb_set FLASH_IRQHandler,Default_Handler + + .weak RCC_IRQHandler + .thumb_set RCC_IRQHandler,Default_Handler + + .weak EXTI0_1_IRQHandler + .thumb_set EXTI0_1_IRQHandler,Default_Handler + + .weak EXTI2_3_IRQHandler + .thumb_set EXTI2_3_IRQHandler,Default_Handler + + .weak EXTI4_15_IRQHandler + .thumb_set EXTI4_15_IRQHandler,Default_Handler + + .weak DMA1_Channel1_IRQHandler + .thumb_set DMA1_Channel1_IRQHandler,Default_Handler + + .weak DMA1_Channel2_3_IRQHandler + .thumb_set DMA1_Channel2_3_IRQHandler,Default_Handler + + .weak DMA1_Ch4_7_DMAMUX1_OVR_IRQHandler + .thumb_set DMA1_Ch4_7_DMAMUX1_OVR_IRQHandler,Default_Handler + + .weak ADC1_IRQHandler + .thumb_set ADC1_IRQHandler,Default_Handler + + .weak TIM1_BRK_UP_TRG_COM_IRQHandler + .thumb_set TIM1_BRK_UP_TRG_COM_IRQHandler,Default_Handler + + .weak TIM1_CC_IRQHandler + .thumb_set TIM1_CC_IRQHandler,Default_Handler + + .weak TIM3_IRQHandler + .thumb_set TIM3_IRQHandler,Default_Handler + + .weak TIM6_IRQHandler + .thumb_set TIM6_IRQHandler,Default_Handler + + .weak TIM7_IRQHandler + .thumb_set TIM7_IRQHandler,Default_Handler + + .weak TIM14_IRQHandler + .thumb_set TIM14_IRQHandler,Default_Handler + + .weak TIM15_IRQHandler + .thumb_set TIM15_IRQHandler,Default_Handler + + .weak TIM16_IRQHandler + .thumb_set TIM16_IRQHandler,Default_Handler + + .weak TIM17_IRQHandler + .thumb_set TIM17_IRQHandler,Default_Handler + + .weak I2C1_IRQHandler + .thumb_set I2C1_IRQHandler,Default_Handler + + .weak I2C2_IRQHandler + .thumb_set I2C2_IRQHandler,Default_Handler + + .weak SPI1_IRQHandler + .thumb_set SPI1_IRQHandler,Default_Handler + + .weak SPI2_IRQHandler + .thumb_set SPI2_IRQHandler,Default_Handler + + .weak USART1_IRQHandler + .thumb_set USART1_IRQHandler,Default_Handler + + .weak USART2_IRQHandler + .thumb_set USART2_IRQHandler,Default_Handler + + .weak USART3_4_IRQHandler + .thumb_set USART3_4_IRQHandler,Default_Handler + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + diff --git a/examples/tos_meets_rust/CMakeLists.txt b/examples/tos_meets_rust/CMakeLists.txt new file mode 100644 index 00000000..5fff0553 --- /dev/null +++ b/examples/tos_meets_rust/CMakeLists.txt @@ -0,0 +1,163 @@ +# Set minimum CMake version +cmake_minimum_required(VERSION 2.8.4) + +if (NOT DEFINED ENV{TOS_SRC_ROOT}) + message(FATAL_ERROR "You must set TOS_SRC_ROOT environment variable!") +endif() + +# Optional verbose mode, uncomment or pass in -DCMAKE_VERBOSE_MAKEFILE=ON +set(CMAKE_VERBOSE_MAKEFILE ON) +set(TOS_SRC_ROOT $ENV{TOS_SRC_ROOT}) +message(STATUS "Tencent OS source root: ${TOS_SRC_ROOT}.") +set(BOARD TencentOS_Tiny_EVB_G0) +set(CMAKE_C_COMPILER arm-none-eabi-gcc) +set(CMAKE_AR arm-none-eabi-ar) +# Set universal poject root directory +set(ROOTDIR ${CMAKE_SOURCE_DIR}) + +# Set the compiler (must be prior to project setup) +include(${ROOTDIR}/toolchain/arm-gcc.cmake) + +# Configure project and languages +project(mqtt_iot_explorer_tc_ch20_oled C CXX ASM) + +# Set device +if (NOT DEVICE) + set(DEVICE STM32G070xx) +endif () + +# Set build +if (NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE DEBUG) +endif () + +##### Modules ##### + +# Libraries can be added to the LIBS variable +# or manually included here. + +# Add base libs (emlib, CMSIS, device files) +include(${ROOTDIR}/toolchain/stm32-base.cmake) + + +set(LINKER_SCRIPT ${ROOTDIR}/BSP/STM32G070RBTx_FLASH.ld) + + +# This is normally set in stm32-base.cmake, but libraries may modify it so set +# it after libraries/subdirectories have been added +# Enable float printing in stm32, see https://stackoverflow.com/questions/28334435/stm32-printf-float-variable +set(CMAKE_EXE_LINKER_FLAGS "${COMMON_DEFINITIONS} -u _printf_float -Xlinker -T${LINKER_SCRIPT} -Wl,-Map=${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}.map -Wl,--gc-sections -Wl,-v ") + +##### Files ##### + +# Add project headers +include_directories(${TOS_SRC_ROOT}/kernel/core/include) +include_directories(${TOS_SRC_ROOT}/kernel/pm/include) +include_directories(${TOS_SRC_ROOT}/kernel/hal/include) +include_directories(${TOS_SRC_ROOT}/arch/arm/arm-v7m/common/include) +include_directories(${TOS_SRC_ROOT}/arch/arm/arm-v7m/cortex-m0+/gcc) +include_directories(${TOS_SRC_ROOT}/osal/cmsis_os) + + +include_directories(${TOS_SRC_ROOT}/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Inc) +include_directories(${TOS_SRC_ROOT}/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Inc/Legacy) +include_directories(${TOS_SRC_ROOT}/platform/vendor_bsp/st/CMSIS/Device/ST/STM32G0xx/Include) +include_directories(${TOS_SRC_ROOT}/platform/vendor_bsp/st/CMSIS/Include) + + +include_directories(${ROOTDIR}/inc) +include_directories(${ROOTDIR}/BSP/Inc) +include_directories(${ROOTDIR}/TOS_CONFIG) + + +include_directories(${TOS_SRC_ROOT}/net/at/include) +include_directories(${TOS_SRC_ROOT}/devices/esp8266_tencent_firmware) +include_directories(${TOS_SRC_ROOT}/net/tencent_firmware_module_wrapper) + + +include_directories(${ROOTDIR}/BSP/Hardware/CH20) +include_directories(${ROOTDIR}/BSP/Hardware/OLED) +include_directories(${ROOTDIR}/BSP/Hardware/PM2D5) + + + +#AUX_SOURCE_DIRECTORY(${TOS_SRC_ROOT}/platform/vendor_bsp/st/STM32Fg0xx_HAL_Driver/Src/ SOURCE_FILES) + +AUX_SOURCE_DIRECTORY(${TOS_SRC_ROOT}/platform/hal/st/stm32g0xx/src/ SOURCE_FILES) +AUX_SOURCE_DIRECTORY(${TOS_SRC_ROOT}/kernel/core/ SOURCE_FILES) +AUX_SOURCE_DIRECTORY(${TOS_SRC_ROOT}/kernel/pm/ SOURCE_FILES) +AUX_SOURCE_DIRECTORY(${TOS_SRC_ROOT}/arch/arm/arm-v7m/cortex-m0+/gcc/ SOURCE_FILES) +AUX_SOURCE_DIRECTORY(${TOS_SRC_ROOT}/arch/arm/arm-v7m/common/ SOURCE_FILES) +#AUX_SOURCE_DIRECTORY(${TOS_SRC_ROOT}/osal/cmsis_os/ SOURCE_FILES) + +AUX_SOURCE_DIRECTORY(${ROOTDIR}/BSP/Src SOURCE_FILES) + +AUX_SOURCE_DIRECTORY(${TOS_SRC_ROOT}/net/at/src SOURCE_FILES) +AUX_SOURCE_DIRECTORY(${TOS_SRC_ROOT}/devices/esp8266_tencent_firmware SOURCE_FILES) +AUX_SOURCE_DIRECTORY(${TOS_SRC_ROOT}/net/tencent_firmware_module_wrapper SOURCE_FILES) + + +AUX_SOURCE_DIRECTORY(${ROOTDIR}/BSP/Hardware/CH20 SOURCE_FILES) +AUX_SOURCE_DIRECTORY(${ROOTDIR}/BSP/Hardware/OLED SOURCE_FILES) +AUX_SOURCE_DIRECTORY(${ROOTDIR}/BSP/Hardware/PM2D5 SOURCE_FILES) + + +# Generate executable and link +add_executable(${PROJECT_NAME} + ${ROOTDIR}/BSP/startup_stm32g070xx.s + ${TOS_SRC_ROOT}/arch/arm/arm-v7m/cortex-m0+/gcc/port_s.S + ${TOS_SRC_ROOT}/osal/cmsis_os/cmsis_os.c + ${TOS_SRC_ROOT}/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_cortex.c + ${TOS_SRC_ROOT}/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_tim.c + ${TOS_SRC_ROOT}/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_tim_ex.c + ${TOS_SRC_ROOT}/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart.c + ${TOS_SRC_ROOT}/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart_ex.c + ${TOS_SRC_ROOT}/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc.c + ${TOS_SRC_ROOT}/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc_ex.c + ${TOS_SRC_ROOT}/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rtc.c + ${TOS_SRC_ROOT}/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rtc_ex.c + ${TOS_SRC_ROOT}/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash.c + ${TOS_SRC_ROOT}/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash_ex.c + ${TOS_SRC_ROOT}/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_gpio.c + ${TOS_SRC_ROOT}/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma.c + ${TOS_SRC_ROOT}/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma_ex.c + ${TOS_SRC_ROOT}/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr.c + ${TOS_SRC_ROOT}/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr_ex.c + ${TOS_SRC_ROOT}/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal.c + ${TOS_SRC_ROOT}/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_i2c.c + ${TOS_SRC_ROOT}/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_i2c_ex.c + ${TOS_SRC_ROOT}/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_spi.c + ${TOS_SRC_ROOT}/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_spi_ex.c + ${TOS_SRC_ROOT}/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_adc.c + ${SOURCE_FILES} + ) + +stm32_configure_linker_addresses(${PROJECT_NAME}) + +add_library(rustcore STATIC ${ROOTDIR}/libs/rustcore/stub.c) +add_library(rustapp STATIC ${ROOTDIR}/libs/rustapp/stub.c) +add_library(tosglue STATIC ${ROOTDIR}/tosglue.c) + +target_link_libraries(${PROJECT_NAME} ${LIBS} c nosys rustcore rustapp) + +# Link optional libraries if available +if (${HAS_HARDWARE}) + target_link_libraries(${PROJECT_NAME} hardware) +endif () + +if (${HAS_PROTOCOL}) + target_link_libraries(${PROJECT_NAME} protocol) +endif () + +##### Post build ##### + +# Add post build commands +include(${ROOTDIR}/toolchain/post-build.cmake) + +# Add stlink commands +include(${ROOTDIR}/toolchain/stlink.cmake) + +##### CMake debug prints ##### +if (CMAKE_VERBOSE_MAKEFILE) + print_debug_info() +endif() diff --git a/examples/tos_meets_rust/README.md b/examples/tos_meets_rust/README.md new file mode 100644 index 00000000..a1c895fd --- /dev/null +++ b/examples/tos_meets_rust/README.md @@ -0,0 +1,276 @@ +# TencentOS Tiny meets Rust + +## 缂栬瘧杩愯 + +缂栬瘧鍓嶅畬鎴愬涓嬪嚑姝 + +1. 閰嶇疆淇℃伅 + + 灏 `TOS_CONFIG/_user_config.h` 澶嶅埗涓浠戒负 `TOS_CONFIG/user_config.h`, + 鍐欏叆璁惧淇℃伅鍜 WiFI 淇℃伅. + +2. 鐜鍙橀噺 + + 鏂板缓绯荤粺鐜鍙橀噺 TOS_SRC_ROOT, 鍏跺间负 TencentOS Tiny 婧愮爜鐨勭粷瀵硅矾寰. + +3. STLINK 杩炴帴鏉垮瓙鍜岀數鑴 + +杩涘叆鏈」鐩牴鐩綍, 鎵ц濡備笅鍛戒护鍒峰叆绯荤粺 + + bash build.sh + +## 鐩綍浠嬬粛 + +椤圭洰鐩綍鍜屽悇鑷殑浣滅敤濡備笅鎵绀 + + +-- app/: Rust 搴旂敤绋嬪簭 + +-- BSP/: borad support package, 宓屽叆寮忔澘瀛愭敮鎸佸伐鍏峰寘 + +-- Hardware/ + +-- CH20/ + +-- OLED/ + +-- PM2D5/ + +-- Inc/: BSP 澶存枃浠 + +-- Src/ + +-- main.c: 鏉垮瓙鍚姩鍚, 浠庤繖閲岀殑 main 鍑芥暟寮濮嬫墽琛 + +-- docs/: 鐩稿叧鏂囨。 + +-- libs/: Rust 鎻掓々鏂囦欢 + +-- toolchain/: 宸ュ叿閾剧紪璇戦厤缃 + +-- TOS_CONFIG/ + +-- tos_config.h: tos 閰嶇疆 + +-- _user_config: 鐢ㄦ埛淇℃伅閰嶇疆 + +-- CMakeLists.txt: 鏁翠釜椤圭洰鐨 cmake 鏂囦欢 + +-- README.md: 椤圭洰姒傝鏂囨。 + +-- tosglue.c: Rust 鍜 TencentOS 鐨勮兌姘存枃浠 + +鏉垮瓙鍚姩鍚庝細鑷姩鎵ц BSP/Src/main.c 涓殑 main 鍑芥暟, +鍦 main 鍑芥暟涓細璋冪敤鐢ㄦ埛瀹氫箟鐨勫涓嬪嚱鏁 + + void application_entry_rust(); + +杩欎釜鍑芥暟鍘熸潵鏄 `application_entry`, 闆嗘垚 Rust 鍚庝慨鏀逛负 `application_entry_rust` 浜. + +闄や簡鍏ュ彛鍑芥暟, 闇瑕佹湁搴曞眰鐨勪腑鏂湇鍔″疄鐜, 浣嶄簬 `BSP/Src/stm32g0xx_it.c` 涓. + +## Rust 闆嗘垚 + +鐩墠鍗佸垎绮楃硻鐨勮璁℃灦鏋勫涓嬫墍绀 + + ------------------------------------ + Rust Application + ------------------------------------ + Rust Wrapper + ------------------------------------ + TencentOS API | Third C Libraries API + ------------------------------------ + TencentOS + ------------------------------------ + +鍗 TencentOS 瀵圭‖浠跺眰杩涜鎶借薄, 瀵逛笂灞傛彁渚涚郴缁 API 浠ュ強鍙兘鐨勭涓夋柟 C 搴 API, +鐒跺悗 Rust 瀵硅繖浜 API 閫氳繃鑳舵按鏂囦欢 (Wrapper) 杩涜灏佽, 鎻愪緵缁 Rust 搴旂敤绋嬪簭浣跨敤. + +杩欓噷鐨勪竴涓叧閿棶棰樻槸濡備綍灏 Rust 搴旂敤绋嬪簭缂栬瘧鍚庡拰绯荤粺婧愮爜閾炬帴璧锋潵鐢熸垚涓涓浐浠. + +鍏跺熀鏈殑瑙e喅鎬濇兂鏄鍏堣幏鍙 Rust 搴旂敤绋嬪簭缂栬瘧鍚庣殑瀵硅薄鏂囦欢(.obj), 鍏舵鑾峰彇绯荤粺婧愮爜缂栬瘧鍚庣殑瀵硅薄鏂囦欢, +鏈鍚庡皢鎵鏈夌殑瀵硅薄鏂囦欢閾炬帴璧锋潵鐢熸垚绯荤粺鍥轰欢. + +鍏蜂綋瀹炶返杩囩▼涓亣鍒扮殑缁嗚妭, 浼氬湪涓嬮潰鎻愬埌. + +- 瀹夎宸ュ叿閾 + + 杩欓噷榛樿浣犲凡缁忓畨瑁呬簡 rust 宸ュ叿閾, 濡傛灉浣犵殑 rustc 鐗堟湰浣庝簬 1.47.0 鍒欏垏鎹㈠埌 nightly 鐗堟湰, + 鍒囨崲鍛戒护濡備笅 + + rustup default nightly + + 濡傛灉鐗堟湰澶т簬绛変簬 1.47.0, 鍒欎笉鐢ㄥ垏鎹. + + 鎺ョ潃瀹夎鏀寔 tos_evb_g0 鏉垮瓙鐨勫伐鍏烽摼 + + rustup target add thumbv6m-none-eabi + sudo apt-get install -y gcc-arm-none-eabi + + 鍏朵粬鍙夊伐鍏 + + sudo apt-get install gdb-arm-none-eabi + sudo apt-get install OpenOCD + +- 璁剧疆 Rust 鎻掓々鏂囦欢 + + 鍦 `鐩綍浠嬬粛` 涓殑 libs 鐩綍涓殑鏂囦欢鍗充负鎻掓々鏂囦欢, 鍏剁粨鏋勫涓 + + +-- libs/ + +-- rustapp/ + +-- stub.c + +-- rustcore/ + +-- stub.c + + stub.c 涓殑鍐呭骞朵笉閲嶈, 鎻掓々鐨勬剰鎬濆氨鏄崰涓潙, 瀹冧滑鐨勭湡瀹炴剰鍥炬槸鐢ㄦ潵鐢熸垚涓や釜搴撴枃浠 + librustcore.a 鍜 librustapp.a 鏂囦欢, 鍥犳闇瑕佸湪 CMakeLists.txt 涓坊鍔犲涓嬩袱琛 + + add_library(rustcore STATIC ${ROOTDIR}/libs/rustcore/stub.c) + add_library(rustapp STATIC ${ROOTDIR}/libs/rustapp/stub.c) + + librustapp.a 灏嗕細琚湡瀹炵殑 rust 搴旂敤绋嬪簭鎵鏇夸唬, 閭 librustcore.a 鏄共浠涔堢殑? + 鎵鏈夌殑 rust 绋嬪簭閮戒緷璧栦簬 Rust 鐨勬牳蹇冨簱鍗 rust core, 鍥犳杩欎釜鏄繀椤绘彁渚, + Rust Core 鐨勪粙缁嶅弬瑙 [The Rust Core Library](https://doc.rust-lang.org/core/). + + rust core 搴撳拰 rust 搴旂敤绋嬪簭搴撶殑鐢熸垚灏嗕細鍦ㄦ帴涓嬫潵鑺備粙缁. + +- rust core 搴撶殑鐢熸垚 + + 褰撴坊鍔犲畬 rust 缂栬瘧宓屽叆寮忕殑宸ュ叿閾炬椂, rust core 搴撲細鑷姩琚斁鍒扮郴缁熶腑鍥哄畾鐨勭洰褰曚笅, + 鍏惰矾寰勭殑鑾峰彇鏂瑰紡濡備笅 + + RUST_THUMBV6M_SYSROOT=$(rustc --print sysroot --target thumbv6m-none-eabi) + RUST_LIBCORE_SRC=$(ls -1 $RUST_THUMBV6M_SYSROOT/lib/rustlib/thumbv6m-none-eabi/lib/libcore-*.rlib) + + rlib 鏂囦欢瀹為檯涓婂氨鏄潤鎬佸簱鏂囦欢, 鍙互鐢ㄥ涓嬪懡浠ゆ煡鐪 rlib 鍐呭 + + arm-none-eabi-ar t $RUST_LIBCORE_SRC + + core-1ba29f225cca71e5.core.1ml6ett9-cgu.0.rcgu.o + lib.rmeta + + 鍥犳鎴戜滑鐩存帴灏嗚鏂囦欢澶嶅埗涓 librustcore.a 鍗冲彲. + +- rust app 搴撶殑鐢熸垚 + + 鏂板缓鐨 rust 搴旂敤绋嬪簭鐩綍涓 app, Rust 鎻愪緵浜 cargo 宸ュ叿, 鍙互蹇熷垱寤洪」鐩, + 杩涘叆椤圭洰鏍圭洰褰曟墽琛屽涓嬪懡浠ゅ嵆鍙敓鎴 app 鐩綍(涔熷彲浠ヤ娇鐢 --lib 鐩存帴鐢熸垚搴撴枃浠剁洰褰曠粨鏋) + + cargo new app + + - 杩涘叆 app/src 鐩綍, 鍒犻櫎 main.rs, 鐒跺悗鏂板缓 lib.rs + - 閰嶇疆 app/Cargo.toml + + 閰嶇疆鍏跺唴瀹瑰涓嬫墍绀 + + [package] + name = "app" + version = "0.1.0" + authors = ["ikey4u "] + edition = "2018" + + # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + + [dependencies] + cty = "0.2.0" # String utilities from cty library: https://crates.io/crates/cty + cstr_core = "0.1.2" # String utilities from cstr_core library: https://crates.io/crates/cstr_core + cortex-m = "0.5.8" # Arm Cortex-M utilities: https://crates.io/crates/cortex-m + + [lib] + name = "tosrs" + test = false + bench = false + + # Options for `cargo build` + [profile.dev] + panic = "abort" # Disable stack unwinding on panic + + # Options for `cargo build --release` + [profile.release] + panic = "abort" # Disable stack unwinding on panic + codegen-units = 1 # Better optimizations + debug = true # Symbols are nice and they don't increase the size on Flash lto = true # Better optimizations + + + - 閰嶇疆 app/.cargo/config 鏂囦欢 + + 閰嶇疆鍏跺唴瀹瑰涓嬫墍绀 + + [target.thumbv6m-none-eabi] + + [target.'cfg(all(target_arch = "arm", target_os = "none"))'] + + rustflags = [ + # LLD (shipped with the Rust toolchain) is used as the default linker + "-C", "link-arg=-Tlink.x", + ] + + [build] + target = "thumbv6m-none-eabi" + + 娉ㄦ剰 tos_evb_g0 瀵瑰簲鐨 target 鏄 thumbv6m-none-eabi. + + 椤圭洰涓渶缁堢殑鐩綍濡備笅鎵绀 + + +-- app/ + +-- src/ + + bridge.rs + + lib.rs + +-- Cargo.toml + +-- .cargo/ + +-- config + + 閰嶇疆瀹屾垚鍚, 渚垮彲鍦 app 鐩綍鎵ц濡備笅鍛戒护缂栬瘧 + + cargo build + + 缂栬瘧鐨勪腑闂翠骇鐗╀綅浜 `app/target/thumbv6m-none-eabi/debug/deps` 涓, + 杩欓噷闈㈡槸涓绯诲垪鐨 .rlib 鏂囦欢(闈欐佸簱鏂囦欢), 杩欎簺搴撴枃浠跺寘鍚簡搴旂敤绋嬪簭浠g爜鍙婂叾渚濊禆鐨勫簱浠g爜. + + 鎴戜滑灏嗚繖浜涘簱鍖呭惈鐨勬墍鏈夊璞℃枃浠舵彁鍙栧嚭鏉ラ噸鏂版墦鍖呮垚涓涓柊鐨勫簱鏂囦欢, 鍗充负 librustapp.a 鏂囦欢. + + 鍏朵腑 bridge.rs 澹版槑浜嗚兌姘存枃浠朵腑鐨 API 鎺ュ彛, 姣斿 + + use cty::*; + + /// These glue functions are from tosglue.c + extern { + pub fn rust_print(msg: *const u8); + } + + lib.rs 鍒欐槸搴旂敤绋嬪簭浠g爜, 绀轰緥濡備笅 + + #![no_std] + + extern crate cortex_m; + + mod bridge; + + use crate::bridge::*; + use cty::*; + + #[no_mangle] + pub extern "C" fn application_entry_rust() -> c_void { + unsafe { + rust_mqtt_daemon(); + } + + loop { + unsafe { + rust_print(b"[+] Welcome to the RUST-WORLD in TencentOS :)".as_ptr()); + } + } + } + +- 鑳舵按鏂囦欢 + + Rust 璋冪敤绯荤粺 API 鎴栬呯涓夋柟 C 搴撶殑 API 閫氳繃鑳舵按鏂囦欢 tosglue.c 瀹炵幇, + 杩欓噷鐨勫仛娉曟槸灏 tosglue.c 缂栬瘧涓轰竴涓簱鏂囦欢, 浣嗘槸涓嶅弬涓庨摼鎺, 鑰屾槸鎻愬彇鍏跺璞℃枃浠, + 鍚堝苟鍒 librustapp.a 涓, 鍦 CMakeLists.txt 涓坊鍔犲涓嬭 + + add_library(tosglue STATIC ${ROOTDIR}/tosglue.c) + + tosglue.c 涓殑涓涓 API 绀轰緥濡備笅 + + void rust_print(const char *msg) { + printf("%s\r\n", msg); + } + +- 涓閿紪璇戠敓鎴愬浐浠 + + 鍦ㄧ紪璇戜箣鍓嶈繕闇瑕佹敞鎰忎慨鏀 CMakeLists.txt, 淇濊瘉鍥轰欢閾炬帴浜 librustcore.a 鍜 librustapp.a + + target_link_libraries(${PROJECT_NAME} ${LIBS} c nosys rustcore rustapp) + + 杩欐牱璁剧疆涔嬪悗, 鎴戜滑鍐嶆浛鎹㈠畬 librustcore.a 鍜 librustapp.a 涔嬪悗, + 灏卞彲浠ュ己鍒堕噸鏂扮敓鎴愭柊鐨勫浐浠. + + 鏈缁堢殑缂栬瘧鑴氭湰鍙傝 build.sh. + +## 鍙傝 + +- [Hosting Embedded Rust apps on Apache Mynewt with STM32 Blue Pill](https://medium.com/@ly.lee/hosting-embedded-rust-apps-on-apache-mynewt-with-stm32-blue-pill-c86b119fe5f) +- [STM32L0 Rust Part 1 - Getting Started](https://craigjb.com/2019/12/31/stm32l0-rust/) +- [FreeRTOS meets Rust](http://www.hashmismatch.net/freertos-meets-rust/) diff --git a/examples/tos_meets_rust/TOS_CONFIG/_user_config.h b/examples/tos_meets_rust/TOS_CONFIG/_user_config.h new file mode 100644 index 00000000..3e018658 --- /dev/null +++ b/examples/tos_meets_rust/TOS_CONFIG/_user_config.h @@ -0,0 +1,11 @@ +#ifndef _USER_CONFIG +#define _USER_CONFIG + +#define PRODUCT_ID "BDDSF87WEA" +#define DEVICE_NAME "dev001" +#define DEVICE_KEY "2/sOZRAJ6B+vMNNXS41w5g==" + +#define WIFI_NAME "1234" +#define WIFI_PASSWORD "12345678" + +#endif diff --git a/examples/tos_meets_rust/TOS_CONFIG/tos_config.h b/examples/tos_meets_rust/TOS_CONFIG/tos_config.h new file mode 100644 index 00000000..f5a1b2f0 --- /dev/null +++ b/examples/tos_meets_rust/TOS_CONFIG/tos_config.h @@ -0,0 +1,39 @@ +#ifndef _TOS_CONFIG_H_ +#define _TOS_CONFIG_H_ + +#include "stm32g0xx.h" + +#define TOS_CFG_TASK_PRIO_MAX 10u + +#define TOS_CFG_ROUND_ROBIN_EN 1u + +#define TOS_CFG_OBJECT_VERIFY_EN 0u + +#define TOS_CFG_TASK_DYNAMIC_CREATE_EN 0u + +#define TOS_CFG_EVENT_EN 1u + +#define TOS_CFG_MMBLK_EN 1u + +#define TOS_CFG_MMHEAP_EN 1u + +#define TOS_CFG_MMHEAP_DEFAULT_POOL_SIZE 0x2000 + +#define TOS_CFG_MUTEX_EN 1u + +#define TOS_CFG_TIMER_EN 1u + +#define TOS_CFG_SEM_EN 1u + +#define TOS_CFG_IDLE_TASK_STK_SIZE 128u + +#define TOS_CFG_CPU_TICK_PER_SECOND 1000u + +#define TOS_CFG_CPU_CLOCK (SystemCoreClock) + +#define TOS_CFG_TIMER_AS_PROC 1u + +#define TOS_CFG_MAIL_QUEUE_EN 1u + +#endif + diff --git a/examples/tos_meets_rust/app/.cargo/config b/examples/tos_meets_rust/app/.cargo/config new file mode 100644 index 00000000..2b6fc340 --- /dev/null +++ b/examples/tos_meets_rust/app/.cargo/config @@ -0,0 +1,16 @@ +[target.thumbv6m-none-eabi] + +[target.'cfg(all(target_arch = "arm", target_os = "none"))'] + +rustflags = [ + # LLD (shipped with the Rust toolchain) is used as the default linker + "-C", "link-arg=-Tlink.x", +] + +[build] +# Pick ONE of these compilation targets +# Select according to https://www.st.com/en/microcontrollers-microprocessors/stm32g0-series.html +target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+ +# target = "thumbv7m-none-eabi" # Cortex-M3 +# target = "thumbv7em-none-eabi" # Cortex-M4 and Cortex-M7 (no FPU) +# target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU) diff --git a/examples/tos_meets_rust/app/Cargo.lock b/examples/tos_meets_rust/app/Cargo.lock new file mode 100644 index 00000000..452009d6 --- /dev/null +++ b/examples/tos_meets_rust/app/Cargo.lock @@ -0,0 +1,189 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "aligned" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d39da9b88ae1a81c03c9c082b8db83f1d0e93914126041962af61034ab44c4a5" + +[[package]] +name = "aligned" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c19796bd8d477f1a9d4ac2465b464a8b1359474f06a96bb3cda650b4fca309bf" +dependencies = [ + "as-slice", +] + +[[package]] +name = "app" +version = "0.1.0" +dependencies = [ + "cortex-m 0.5.10", + "cstr_core", + "cty 0.2.1", +] + +[[package]] +name = "as-slice" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb4d1c23475b74e3672afa8c2be22040b8b7783ad9b461021144ed10a46bb0e6" +dependencies = [ + "generic-array 0.12.3", + "generic-array 0.13.2", + "generic-array 0.14.4", + "stable_deref_trait", +] + +[[package]] +name = "bare-metal" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5deb64efa5bd81e31fcd1938615a6d98c82eafcbcd787162b6f63b91d6bac5b3" +dependencies = [ + "rustc_version", +] + +[[package]] +name = "bitfield" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46afbd2983a5d5a7bd740ccb198caf5b82f45c40c09c0eed36052d91cb92e719" + +[[package]] +name = "cortex-m" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c0b159a1e8306949579de3698c841dba58058197b65c60807194e4fa1e7a554" +dependencies = [ + "aligned 0.2.0", + "bare-metal", + "cortex-m 0.6.3", + "volatile-register", +] + +[[package]] +name = "cortex-m" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2be99930c99669a74d986f7fd2162085498b322e6daae8ef63a97cc9ac1dc73c" +dependencies = [ + "aligned 0.3.4", + "bare-metal", + "bitfield", + "volatile-register", +] + +[[package]] +name = "cstr_core" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ebe7158ee57e848621d24d0ed87910edb97639cb94ad9977edf440e31226035" +dependencies = [ + "cty 0.1.5", + "memchr", +] + +[[package]] +name = "cty" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4e1d41c471573612df00397113557693b5bf5909666a8acb253930612b93312" + +[[package]] +name = "cty" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7313c0d620d0cb4dbd9d019e461a4beb501071ff46ec0ab933efb4daa76d73e3" + +[[package]] +name = "generic-array" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c68f0274ae0e023facc3c97b2e00f076be70e254bc851d972503b328db79b2ec" +dependencies = [ + "typenum", +] + +[[package]] +name = "generic-array" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ed1e761351b56f54eb9dcd0cfaca9fd0daecf93918e1cfc01c8a3d26ee7adcd" +dependencies = [ + "typenum", +] + +[[package]] +name = "generic-array" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "memchr" +version = "2.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" + +[[package]] +name = "rustc_version" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" +dependencies = [ + "semver", +] + +[[package]] +name = "semver" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" +dependencies = [ + "semver-parser", +] + +[[package]] +name = "semver-parser" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" + +[[package]] +name = "stable_deref_trait" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" + +[[package]] +name = "typenum" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33" + +[[package]] +name = "vcell" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "876e32dcadfe563a4289e994f7cb391197f362b6315dc45e8ba4aa6f564a4b3c" + +[[package]] +name = "version_check" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" + +[[package]] +name = "volatile-register" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d67cb4616d99b940db1d6bd28844ff97108b498a6ca850e5b6191a532063286" +dependencies = [ + "vcell", +] diff --git a/examples/tos_meets_rust/app/Cargo.toml b/examples/tos_meets_rust/app/Cargo.toml new file mode 100644 index 00000000..289a1c8f --- /dev/null +++ b/examples/tos_meets_rust/app/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "app" +version = "0.1.0" +authors = ["ikey4u "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +cty = "0.2.0" # String utilities from cty library: https://crates.io/crates/cty +cstr_core = "0.1.2" # String utilities from cstr_core library: https://crates.io/crates/cstr_core +cortex-m = "0.5.8" # Arm Cortex-M utilities: https://crates.io/crates/cortex-m + +[lib] +name = "tosrs" +test = false +bench = false + +# Options for `cargo build` +[profile.dev] +panic = "abort" # Disable stack unwinding on panic + +# Options for `cargo build --release` +[profile.release] +panic = "abort" # Disable stack unwinding on panic +codegen-units = 1 # Better optimizations +debug = true # Symbols are nice and they don't increase the size on Flash lto = true # Better optimizations diff --git a/examples/tos_meets_rust/app/src/bridge.rs b/examples/tos_meets_rust/app/src/bridge.rs new file mode 100644 index 00000000..71aa742a --- /dev/null +++ b/examples/tos_meets_rust/app/src/bridge.rs @@ -0,0 +1,11 @@ +use cty::*; + +/// These glue functions are from tosglue.c +extern { + pub fn rust_print(msg: *const u8); + pub fn rust_oled_print(msg: *const u8); + pub fn rust_wifi_init() -> i32; + pub fn rust_wifi_connect(ssid: *const u8, passwd: *const u8); + pub fn rust_sleep(ms: u32); + pub fn rust_mqtt_daemon() -> c_void; +} diff --git a/examples/tos_meets_rust/app/src/lib.rs b/examples/tos_meets_rust/app/src/lib.rs new file mode 100644 index 00000000..5c12a804 --- /dev/null +++ b/examples/tos_meets_rust/app/src/lib.rs @@ -0,0 +1,23 @@ +#![no_std] + +extern crate cortex_m; + +mod bridge; + +use crate::bridge::*; +use cty::*; + +#[no_mangle] +pub extern "C" fn application_entry_rust() -> c_void { + unsafe { + rust_print(b"[+] Welcome to the RUST-WORLD in TencentOS :)".as_ptr()); + rust_mqtt_daemon(); + } + + loop { + unsafe { + rust_print(b"[+] This is a mqtt demo!".as_ptr()); + rust_sleep(5000u32); + } + } +} diff --git a/examples/tos_meets_rust/build.sh b/examples/tos_meets_rust/build.sh new file mode 100644 index 00000000..0d599a80 --- /dev/null +++ b/examples/tos_meets_rust/build.sh @@ -0,0 +1,64 @@ +#! /usr/bin/env bash +# Author: ikey4u + +set -e + +function cook_rust() { + RETDIR=$(pwd) + + ROOTDIR=$(git rev-parse --show-toplevel)/examples/tos_meets_rust + [[ ! -e $ROOTDIR ]] && echo "[x] Cannot ROOTDIR" && return 42 + + BUILD_DIR=$ROOTDIR/build + [[ -e $BUILD_DIR ]] && rm -rf $BUILD_DIR + mkdir -p $BUILD_DIR && cd $BUILD_DIR + cmake .. && make + cd $ROOTDIR + + ROM=$ROOTDIR/build/mqtt_iot_explorer_tc_ch20_oled + [[ -e $ROM ]] && rm $ROM + + RUST_APP_DEST=$ROOTDIR/build/librustapp.a + [[ -e $RUST_APP_DEST ]] && rm $RUST_APP_DEST + + RUST_CORE_DEST=$ROOTDIR/build/librustcore.a + [[ -e $RUST_CORE_DEST ]] && rm $RUST_CORE_DEST + + cd $ROOTDIR/app + RUSTFLAGS="-L $BUILD_DIR" cargo build -v + cd $ROOTDIR + + RUST_OBJ_DIR=$ROOTDIR/app/target/thumbv6m-none-eabi/debug/deps + [[ ! -e $RUST_OBJ_DIR ]] && echo "[x] Cannot find RUST_OBJ_DIR" && return 42 + + RUST_OBJ_TMPDIR=$ROOTDIR/build/rustobj + [[ -e $RUST_OBJ_TMPDIR ]] && rm -rf $RUST_OBJ_TMPDIR + mkdir -p $RUST_OBJ_TMPDIR && cd $RUST_OBJ_TMPDIR + for rustobj in $RUST_OBJ_DIR/*.rlib + do + arm-none-eabi-ar x $rustobj + done + arm-none-eabi-ar x $BUILD_DIR/libtosglue.a + mv tosglue.c.obj tosglue.o + if [[ $(uname -s) == "Darwin" ]]; then + RUST_APP_SRC=$RUST_OBJ_TMPDIR/$(basename ${RUST_APP_DEST}) + else + RUST_APP_SRC=$RUST_OBJ_TMPDIR/$(basename -z ${RUST_APP_DEST}) + fi + arm-none-eabi-ar r $(basename ${RUST_APP_SRC}) *.o + cp $RUST_APP_SRC $RUST_APP_DEST + touch $RUST_APP_DEST + cd $ROOTDIR + + RUST_THUMBV6M_SYSROOT=$(rustc --print sysroot --target thumbv6m-none-eabi) + RUST_CORE_SRC=$(ls -1 $RUST_THUMBV6M_SYSROOT/lib/rustlib/thumbv6m-none-eabi/lib/libcore-*.rlib) + cp $RUST_CORE_SRC $RUST_CORE_DEST + + cd $BUILD_DIR && make + + make flash + + cd $RETDIR +} + +cook_rust diff --git a/examples/tos_meets_rust/docs/setup.linux.ubuntu.md b/examples/tos_meets_rust/docs/setup.linux.ubuntu.md new file mode 100644 index 00000000..7564d2d9 --- /dev/null +++ b/examples/tos_meets_rust/docs/setup.linux.ubuntu.md @@ -0,0 +1,490 @@ +# STM32 鐢查啗妫娴嬪櫒瀹夎鏁欑▼ + +鏈枃鎵鐢ㄧ郴缁熶负 Ubuntu 20.04.1, 浠ヤ笅鏁欑▼鎵鐢ㄥ埌鐨勭郴缁熷浐浠舵簮鐮佷互鏈粨搴 template 涓殑妯℃澘浠g爜涓哄噯, +鏈変换浣曠枒闂杩庢彁 issues. + +## 鏁欑▼ + +- 杩炴帴 ST-LINK (STv2) 鍜屽紑鍙戞澘 + + STv2 涓婃湁涓涓ず鎰忓浘鏍囨湁鍚勪釜 Pin 鐨勫惈涔, Pin 鍛ㄥ洿鏈変竴涓己鍙, + 缂哄彛瀵瑰簲鐨勪竴鎺 Pin 瀵瑰簲涓庣ず鎰忓浘澶栦晶鐨勪竴鍒, STv2 鍜屽紑鍙戞澘鎺ュ彛杩炴帴瀵瑰簲鍏崇郴濡備笅琛ㄦ墍绀. + + |STv2 鎺ュ彛 |寮鍙戞澘鎺ュ彛 | + | ------------- |-------------| + | 3.3v | 3v3 | + | SWDIO | DIO | + | SWCLK | CLK | + | GND | GND | + +- 瀹夎 STv2 椹卞姩 + + ST-LINK 鐨勯┍鍔ㄦ簮鐮佸湴鍧涓 https://github.com/stlink-org/stlink. + + 瀹夎缂栬瘧渚濊禆 + + sudo apt install clang build-essential cmake libusb-1.0-0 libusb-1.0-0-dev + + 涓嬭浇婧愮爜骞剁紪璇 + + git clone https://github.com/stlink-org/stlink + cd stlink + make release + + 瀹夎鍒 $HOME/.usr/stlink 鐩綍涓 + + cd build/Release && make install DESTDIR=$HOME/.usr/stlink + + 瀹夎鍚 $HOME/.usr/stlink 缁撴瀯濡備笅 + + 鉃 Release git:(develop) tree ~/.usr/stlink/ + /home/m9/.usr/stlink/ + 鈹溾攢鈹 etc + 鈹偮犅 鈹斺攢鈹 modprobe.d + 鈹偮犅 鈹斺攢鈹 stlink_v1.conf + 鈹溾攢鈹 lib + 鈹偮犅 鈹斺攢鈹 udev + 鈹偮犅 鈹斺攢鈹 rules.d + 鈹偮犅 鈹溾攢鈹 49-stlinkv1.rules + 鈹偮犅 鈹溾攢鈹 49-stlinkv2-1.rules + 鈹偮犅 鈹溾攢鈹 49-stlinkv2.rules + 鈹偮犅 鈹斺攢鈹 49-stlinkv3.rules + 鈹斺攢鈹 usr + 鈹斺攢鈹 local + 鈹溾攢鈹 bin + 鈹偮犅 鈹溾攢鈹 st-flash + 鈹偮犅 鈹溾攢鈹 st-info + 鈹偮犅 鈹斺攢鈹 st-util + 鈹溾攢鈹 include + 鈹偮犅 鈹斺攢鈹 stlink + 鈹偮犅 鈹溾攢鈹 backend.h + 鈹偮犅 鈹溾攢鈹 chipid.h + 鈹偮犅 鈹溾攢鈹 commands.h + 鈹偮犅 鈹溾攢鈹 flash_loader.h + 鈹偮犅 鈹溾攢鈹 libusb_settings.h + 鈹偮犅 鈹溾攢鈹 logging.h + 鈹偮犅 鈹溾攢鈹 md5.h + 鈹偮犅 鈹溾攢鈹 reg.h + 鈹偮犅 鈹溾攢鈹 sg.h + 鈹偮犅 鈹溾攢鈹 stlink.h + 鈹偮犅 鈹溾攢鈹 stm32.h + 鈹偮犅 鈹溾攢鈹 usb.h + 鈹偮犅 鈹斺攢鈹 version.h + 鈹溾攢鈹 lib + 鈹偮犅 鈹溾攢鈹 libstlink.a + 鈹偮犅 鈹溾攢鈹 libstlink.so -> libstlink.so.1 + 鈹偮犅 鈹溾攢鈹 libstlink.so.1 -> libstlink.so.1.6.1 + 鈹偮犅 鈹斺攢鈹 libstlink.so.1.6.1 + 鈹斺攢鈹 share + 鈹斺攢鈹 man + 鈹斺攢鈹 man1 + 鈹溾攢鈹 st-flash.1 + 鈹溾攢鈹 st-info.1 + 鈹斺攢鈹 st-util.1 + + 鐜板湪闇瑕侀氳繃 udev 璁剧疆 USB 璁块棶鏉冮檺, 杩涘叆鍒 stlink 婧愮爜鏍圭洰褰曟墽琛屽涓嬫搷浣 + + sudo cp -a config/udev/rules.d/* /etc/udev/rules.d/ + sudo udevadm control --reload-rules + sudo udevadm trigger + + 鐜板湪缁欑數鑴戞彃涓婄數璺澘, 浣跨敤 lsusb 鏌ョ湅 USB 璁惧, 鑳界湅鍒板涓嬭緭鍑鸿鏄庡畨瑁呮垚鍔 + + + 鉃 stlink git:(develop) lsusb + ... + Bus 002 Device 013: ID 0483:3748 STMicroelectronics ST-LINK/V2 + ... + + + 鍦 shell 閰嶇疆鏂囦欢濡 $HOME/.zshrc 涓坊鍔犲涓嬬幆澧冨彉閲 + + STLINK=$HOME/.usr/stlink + export PATH=${STLINK}/usr/local/bin:${PATH} + export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${STLINK}/usr/local/lib + + 鏇存柊搴撶紦瀛 + + sudo ldconfig + + 鍒锋柊鐜鍙橀噺 + + exec $SHELL + + 鐒跺悗鏌ョ湅鏉垮瓙淇℃伅 + + 鉃 ~ st-info --probe + Found 1 stlink programmers + serial: 2c5a15000b14324d434d4e00 + hla-serial: "\x2c\x5a\x15\x00\x0b\x14\x32\x4d\x43\x4d\x4e\x00" + flash: 131072 (pagesize: 2048) + sram: 36864 + chipid: 0x0460 + descr: G070/G071/G081 + + + 娴嬭瘯鏂囦欢璇诲彇 + + 鉃 ~ st-flash --debug read dummy.file 0 256 + st-flash 1.6.1-96-gbf41f14 + 2020-09-22T23:56:45 DEBUG common.c: *** looking up stlink version + 2020-09-22T23:56:45 DEBUG common.c: st vid = 0x0483 (expect 0x0483) + 2020-09-22T23:56:45 DEBUG common.c: stlink pid = 0x3748 + 2020-09-22T23:56:45 DEBUG common.c: stlink version = 0x2 + 2020-09-22T23:56:45 DEBUG common.c: jtag version = 0x23 + 2020-09-22T23:56:45 DEBUG common.c: swim version = 0x7 + 2020-09-22T23:56:45 DEBUG common.c: stlink current mode: debug (jtag or swd) + 2020-09-22T23:56:45 DEBUG usb.c: JTAG/SWD freq set to 0 + 2020-09-22T23:56:45 DEBUG common.c: *** set_swdclk *** + 2020-09-22T23:56:45 DEBUG common.c: stlink current mode: debug (jtag or swd) + 2020-09-22T23:56:45 DEBUG common.c: *** stlink_jtag_reset *** + 2020-09-22T23:56:45 DEBUG common.c: *** stlink_reset *** + 2020-09-22T23:56:45 DEBUG common.c: *** stlink_write_debug32 0x05fa0004 to 0xe000ed0c + 2020-09-22T23:56:45 DEBUG common.c: Loading device parameters.... + 2020-09-22T23:56:45 DEBUG common.c: *** stlink_core_id *** + 2020-09-22T23:56:45 DEBUG common.c: core_id = 0x0bc11477 + 2020-09-22T23:56:45 DEBUG common.c: *** stlink_read_debug32 0000000000 at 0xe0042000 + 2020-09-22T23:56:45 DEBUG common.c: *** stlink_read_debug32 0x20006460 at 0x40015800 + 2020-09-22T23:56:45 DEBUG common.c: *** stlink_read_debug32 0xffff0080 at 0x1fff75e0 + 2020-09-22T23:56:45 INFO common.c: G070/G071/G081: 36 KiB SRAM, 128 KiB flash in at least 2 KiB pages. + 2020-09-22T23:56:45 DEBUG common.c: stlink current mode: debug (jtag or swd) + 2020-09-22T23:56:45 DEBUG common.c: stlink current mode: debug (jtag or swd) + 2020-09-22T23:56:45 DEBUG common.c: *** stlink_force_debug_mode *** + 2020-09-22T23:56:45 DEBUG common.c: *** stlink_status *** + 2020-09-22T23:56:45 DEBUG usb.c: core status: 00030003 + 2020-09-22T23:56:45 DEBUG common.c: core status: halted + 2020-09-22T23:56:45 INFO common.c: read from address 0000000000 size 256 + 2020-09-22T23:56:45 DEBUG common.c: *** stlink_read_mem32 *** + 2020-09-22T23:56:45 DEBUG common.c: data_len = 256 0x100 + 68 5c 00 20 cd 00 00 08 c1 2d 00 08 c7 29 00 08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 b7 2f 00 08 00 00 00 00 00 00 00 00 25 01 00 08 b9 2f 00 08 df 00 00 08 00 00 00 00 df 00 00 08 df 00 00 08 df 00 00 08 df 00 00 08 df 00 00 08 df 00 00 08 00 00 00 00 df 00 00 08 df 00 00 08 df 00 00 08 df 00 00 08 df 00 00 08 df 00 00 08 00 00 00 00 df 00 00 08 df 00 00 08 df 00 00 08 df 00 00 08 df 00 00 08 df 00 00 08 df 00 00 08 df 00 00 08 df 00 00 08 df 00 00 08 df 00 00 08 9d 37 00 08 ad 37 00 08 bd 37 00 08 03 48 85 46 00 f0 56 fd 00 48 00 47 39 58 00 08 68 5c 00 20 04 48 80 47 04 48 00 47 fe e7 fe e7 fe e7 fe e7 fe e7 fe e7 4d 30 00 08 b9 00 00 08 72 b6 70 47 62 b6 70 47 ef f3 10 80 72 b6 70 47 80 f3 10 88 70 47 1b 48 + 2020-09-22T23:56:45 DEBUG common.c: *** stlink_exit_debug_mode *** + 2020-09-22T23:56:45 DEBUG common.c: *** stlink_write_debug32 0xa05f0000 to 0xe000edf0 + 2020-09-22T23:56:45 DEBUG common.c: *** stlink_close *** + +- CH340 涓插彛椹卞姩 + + 鍦ㄦ澘瀛愪笂鏈変竴涓啓鏈 CH340C 鐨勭數瀛愬厓浠, 杩欎釜灏辨槸鐢靛瓙鍏冧欢鐨勪綔鐢ㄥ氨鏄皢 USB 鍗忚杞崲涓轰覆鍙e崗璁, + 瑕佽杩欎釜杩欎釜鐢靛瓙鍏冧欢姝e父宸ヤ綔, 闇瑕佸畨瑁 CH340 椹卞姩. + + Ubuntu 20.04 绯荤粺鐨 Linux 鍐呮牳宸茬粡鑷甫浜嗚繖涓┍鍔, 鍙互浣跨敤濡備笅鍛戒护鏌ョ湅 + + 鉃 lsmod | grep ch34 + ch341 20480 0 + usbserial 53248 1 ch341 + + 濡傛灉涓嶆垚鍔, 瀵归┍鍔ㄦ墦琛ヤ竵鍙傝冨涓嬮摼鎺: + + https://learn.sparkfun.com/tutorials/how-to-install-ch340-drivers/all#linux + + 褰撲覆鍙i┍鍔ㄦ垚鍔熷畨瑁呭悗, 鏁版嵁鏄浣曚粠鐢佃剳浼犺緭鍒版澘瀛愪笂鍛? + + 鍦 USB 鎺ュ彛閲屾湁 4 鏍圭嚎, 澶栦晶鐨勪袱鏍归暱閲戞墜鎸囪礋璐d緵鐢, 鍗崇數婧愮嚎, 鍐呬晶鐨勪袱鏍归噾鎵嬫寚姣旇緝鐭, + 鐢ㄤ簬鏁版嵁浼犺緭, 鍗虫暟鎹嚎. + + 褰撴垜浠啀鐢佃剳涓婇氳繃涓插彛鍚戞澘瀛愪紶杈撴暟鎹椂, 鐢佃剳绔噰鐢 USB 鏁版嵁浼犺緭鍗忚閫氳繃鏁版嵁绾夸紶鍒版澘瀛愪笂, + 鐒跺悗 CH340 椹卞姩瑙f瀽 USB 鏁版嵁灏嗗叾杞崲涓轰覆鍙f暟鎹紶杈撶粰鏉垮瓙涓婄殑鍏朵粬鍏冨櫒浠. + + 涓插彛闄や簡浼犺緭鏁版嵁鐨勫姛鑳戒箣澶, 杩樻湁璋冭瘯鍔熻兘, 鏉垮瓙涓婄▼搴忕殑杈撳嚭涓鑸兘鏄閲嶅畾鍚戝埌涓插彛涓, + 鐒跺悗鎵撳嵃鍑烘暟鎹緵璋冭瘯鐢. + +- 涓插彛宸ュ叿 + + 閫氳繃涓插彛宸ュ叿, 鎴戜滑鍙互瑙傚療鍒扮獥鍙d笂浼犺緭鐨勬暟鎹, 杩欓噷浣跨敤鐨勪覆鍙e伐鍏锋槸 kermit, + 鍏堕椤典负 http://www.kermitproject.org/ck90.html + + 缂栬瘧瀹夎 c-kermit 鐨勫懡浠ゅ涓 + + wget http://www.kermitproject.org/ftp/kermit/test/tar/x.zip + unzip x.zip -d c-kermit && cd c-kermit + make linux + + 浼氱敓鎴愪竴涓 wermit 浜岃繘鍒舵枃浠, 灏嗗叾淇濆瓨鍒版煇涓矾寰勬瘮濡 $HOME/.usr/bin, + 寤鸿灏嗗叾閲嶅懡鍚嶄负 kermit, 鐒跺悗灏嗚璺緞鍔犲叆鐜鍙橀噺, 鐒跺悗杩愯鍗冲彲. + +- 杩炴帴涓插彛璁惧 + + 棣栧厛鏌ョ湅涓插彛璁惧, Linux 涓娇鐢 ttyS 鏉ヨ〃绀轰覆鍙h澶囧悕, 鍏朵腑 鏄彲鍙樼殑, + 姣斿 ttyS0, ttyS1, 瀵瑰簲鍒 Windows 涓氨鍒嗗埆琛ㄧず COM1, COM2. 鐗瑰埆鍦, + 鍩轰簬 USB 鐨勪覆鍙h澶囧悕绉颁竴鑸被浼间簬 ttyUSB0, ttyUSB1 绛, 杩欎簺璁惧瀵瑰簲鐨勬枃浠惰矾寰勪綅浜 /dev 涓嬮潰, + 姣斿 ttyUSB0 鐨勪綅缃氨鏄 /dev/ttyUSB0. + + 浣跨敤濡備笅鍛戒护鏌ョ湅杩炴帴鐨勪覆鍙h澶 + + 鉃 dmesg | grep tty + [ 0.352821] printk: console [tty0] enabled + [ 1.179333] tty tty50: hash matches + [193518.041877] usb 3-2: ch341-uart converter now attached to ttyUSB0 + + 鏌ョ湅涓插彛鍙傛暟 + + sudo stty -F /dev/ttyUSB0 -a + + 璁剧疆涓插彛鍙傛暟(娉㈢壒鐜囪缃负 115200, 8 浣嶆暟鎹ā寮, 鍏跺疄杩欐鏄笉蹇呰鐨, 鍙互鍊兼帴鍦 kermit 涓缃) + + sudo stty -F /dev/ttyUSB0 ispeed 115200 ospeed 115200 cs8 + + 鏌ョ湅璁剧疆鍚庣殑涓插彛灞炴т俊鎭 + + 鉃 sudo stty -F /dev/ttyUSB0 -a + speed 115200 baud; rows 0; columns 0; line = 0; + intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = ; eol2 = ; swtch = ; + start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 0; + -parenb -parodd -cmspar cs8 hupcl -cstopb cread clocal -crtscts + -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel + -iutf8 + opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 + isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke -flusho + -extproc + + 鐜板湪鎵ц wermit 杩涘叆涓插彛宸ュ叿, 鐒跺悗鎵ц濡備笅鍛戒护 + + set line /dev/ttyUSB0 + set carrier-watch off + connect + + 鍑虹幇濡備笅杈撳嚭琛ㄧず杩炴帴鎴愬姛 + + Connecting to /dev/ttyUSB0, speed 115200 + Escape character: Ctrl-\ (ASCII 28, FS): enabled + Type the escape character followed by C to get back, + or followed by ? to see other options. + ---------------------------------------------------- + + 濡傛灉鎯冲洖鍒 kermit, 鎸変笅 Ctrl-\, 鍐嶆寜涓 C 鍗冲彲, 濡傛灉鎯冲啀杩炴帴鍒颁覆鍙, 杈撳叆 c 鍗冲彲. + +- 鍒峰叆 WiFi 鍥轰欢 + + 鐢变簬鏄氳繃涓插彛鍒 WiFi 鍥轰欢, 鍥犳闇瑕佹柇寮鍏朵粬鐨勪覆鍙h繛鎺. + + - 璁╀覆鍙i摼璺繛鎺ュ埌 WiFi 鐢靛瓙鍏冧欢涓, 鍏跺叿浣撳仛娉曚负 + + 1. 瀹氫綅鍒 `涓插彛鍒囨崲` + + 涓插彛鍒囨崲涓鍏变袱鎺, 姣忔帓鍥涗釜 Pin, 绀烘剰鍥惧涓 + + * * * * + * * * * + + 灏嗙煭璺附(榛勮壊濉戞枡濂)鎸夊涓嬭繛鎺 + + * + + * + * - - * + + 鍔犲彿鐨勮繛鎺ュ埌涓璧, 鍑忓彿鐨勮繛鎺ュ埌涓璧. + + + 2. 瀹氫綅鍒 `ESP 鍥轰欢涓嬭浇閰嶇疆` + + 杩欎釜閰嶇疆涓鍏辨湁 3 涓, 渚濇濡備笅 + + 3v3 IO0 GND + + 鐢ㄧ煭璺附鎶 IO0 鍜 GND 杩炴帴. + + 3. 瀹氫綅鍒 `BOOT 閰嶇疆` + + 杩欎釜閰嶇疆涓鍏辨湁 3 涓, 渚濇濡備笅 + + 0 BOOT 1 + + 灏嗙煭璺附鎶 0 鍜 BOOT 杩炴帴. + + - 瀹夎 esptool 宸ュ叿 + + 闇瑕佺敤鍒板伐鍏 esptool, 鍦板潃涓 https://github.com/espressif/esptool, 瀹夎鍛戒护濡備笅 + + pip3 install esptool + + 瀹夎瀹屾垚鍚庡埛鏂扮郴缁熺幆澧冨彉閲, 浼氬緱鍒颁竴涓懡浠 esptool.py, 鎴戜滑灏辨槸閫氳繃杩欎釜鍛戒护灏 WiFi 鍥轰欢鍒峰叆鍒版澘瀛愪腑. + 浠ヤ笂鍑嗗瀹屾垚鍚, 娴嬭瘯涓涓 esp 鏄惁姝e父宸ヤ綔, 鍛戒护濡備笅 + + sudo -E env "PATH=$PATH" esptool.py --port /dev/ttyUSB0 read_mac + + 杈撳嚭绫讳技濡備笅 + + esptool.py v2.8 + Serial port /dev/ttyUSB0 + Connecting.... + Detecting chip type... ESP8266 + Chip is ESP8266EX + Features: WiFi + Crystal is 26MHz + MAC: 40:f5:20:08:6f:b1 + Uploading stub... + Running stub... + Stub running... + MAC: 40:f5:20:08:6f:b1 + Hard resetting via RTS pin... + + + 濡傛灉杩炴帴涓嶄笂, 鑰冭檻濡備笅鍑犵鎯呭舰 + + 1. reset 鏉垮瓙 + + 濡傛灉涔嬪墠浣犳墽琛岃繃 esptool.py, 浣犲繀椤绘墜鍔ㄦ寜涓涓嬫澘瀛愪笂鐨 reset 鎸夐敭, + 濡傛灉浣犱笉鎸 reset 缁х画鎵ц esptool.py 鍛戒护, 閭d箞杩欎釜鍛戒护浼氫竴鐩存寕璧, 杈撳嚭绫讳技浜庝笅闈㈣繖绉 + + esptool.py v2.8 + Serial port /dev/ttyUSB0 + Connecting........_____.... + + 2. 涓插彛琚崰鐢 + + 濡傛灉鎻愮ず涓嬮潰鐨勯敊璇〃绀轰綘鐨勪覆鍙h鍗犵敤浜, 鐪嬬湅浣犵殑涓插彛宸ュ叿鏄笉鏄繛涓婁簡. + + serial.serialutil.SerialException: read failed: device reports readiness to read but returned no data (device disconnected or multiple access on port?) + + 3. STLINK 鍜屼覆鍙e悓鏃惰繛涓婁簡 + + 鎶 STLINK 鍜屼覆鍙g嚎閮芥嫈涓嬫潵, 鍙彃涓插彛绾. + + - 鍒峰叆鍥轰欢 + + 纭畾 esp 姝e父宸ヤ綔鍚, 涓嬭浇鍥轰欢 + + git clone https://github.com/tencentyun/qcloud-iot-esp-wifi.git + + 鐒跺悗瑙e帇鍥轰欢 + + cd qcloud-iot-esp-wifi + unzip qcloud-iot-at-esp8266/QCloud_IoT_AT_ESP8266_FW/QCloud_IoT_AT_ESP8266_v2.1.1_20200903.zip + + 鑵捐浜 IoT 瀹氬埗 AT 涓插彛浣跨敤 UART0, Tx 涓 GPIO1, Rx 涓 GPIO3, UART_1_3 鐨勫浐浠 + 閲囩敤鐨 Tx 鍜 Rx 鍜 AT 鐨勪竴鑷, 鍥犳鎴戜滑浣跨敤 UART_1_3 鍥轰欢, 鍏惰矾寰勪负 + + QCloud_IoT_AT_ESP8266_v2.1.1_20200903/QCloud_IoT_AT_ESP8266_v2.1.1_20200903_UART_1_3.bin + + 鎵ц濡備笅鍛戒护鍒峰叆鍥轰欢 + + sudo -E env "PATH=$PATH" esptool.py --port /dev/ttyUSB0 --chip esp8266 --baud 1500000 --after hard_reset write_flash 0x0 QCloud_IoT_AT_ESP8266_v2.1.1_20200903/QCloud_IoT_AT_ESP8266_v2.1.1_20200903_UART_1_3.bin + + 鎴愬姛鎵ц鍚庣殑杈撳嚭绀轰緥濡備笅 + + esptool.py v2.8 + Serial port /dev/ttyUSB0 + Connecting... + Chip is ESP8266EX + Features: WiFi + Crystal is 26MHz + MAC: 40:f5:20:08:6f:b1 + Uploading stub... + Running stub... + Stub running... + Changing baud rate to 1500000 + Changed. + Configuring flash size... + Auto-detected Flash size: 4MB + Flash params set to 0x0040 + Compressed 2039808 bytes to 485108... + Wrote 2039808 bytes (485108 compressed) at 0x00000000 in 7.0 seconds (effective 2326.6 kbit/s)... + Hash of data verified. + + Leaving... + Hard resetting via RTS pin... + + - 鎭㈠涓插彛杩炴帴 + + 1. `涓插彛鍒囨崲` + + 杩炴帴绀烘剰鍥 + + 1 1 2 2 + 3 3 4 4 + + 鏁板肩浉鍚岀殑杩炴帴鍒颁竴璧 + + 2. `ESP 鍥轰欢涓嬭浇閰嶇疆` + + 杩炴帴 3v3 鍜 IO0 + + 3. `BOOT 閰嶇疆` + + 淇濇寔涓嶅彉 + +- 鑵捐浜 + + 鍒拌吘璁簯鐗╄仈缃戝紑鍙戝钩鍙 https://cloud.tencent.com/product/iotexplorer 娉ㄥ唽涓涓柊浜у搧. + + 鏂板缓浜у搧鍚庡鍏ユā鏉挎暟鎹涓 + + { + "version": "1.0", + "profile": { + "ProductId": "BDDSF87WEA", + "CategoryId": "1" + }, + "properties": [ + { + "id": "ch20_ppm_value", + "name": "鐢查啗娴撳害鍊", + "desc": "", + "mode": "r", + "define": { + "type": "float", + "min": "0", + "max": "2", + "start": "0", + "step": "0.001", + "unit": "ppm(mg/m3)" + }, + "required": false + } + ], + "events": [], + "actions": [] + } + + 鐒跺悗鏂板缓璁惧, 鏂板缓涔嬪悗鑳藉寰楀埌涓変釜淇℃伅: 璁惧鍚嶇О, 璁惧瀵嗛挜, 浜у搧 ID, + 杩欎笁涓俊鎭渶瑕佸啓鍏ュ埌 TencentOS 鍥轰欢涓. + +- 鐢查啗浼犳劅鍣ㄥ簳鏉胯繛鎺 + + 鏍规嵁鏉垮瓙鍜屽簳鏉跨殑 5v 鍜 GND 鎺ュ彛瀵瑰簲鍏崇郴, 灏嗗簳鏉胯繛鎺ュ埌鏉垮瓙涓, + 鐒跺悗鏍规嵁浼犳劅鍣ㄧ殑 Rx 鍜 Tx 鍙e皢浼犳劅鍣ㄥ拰鏉垮瓙搴曟澘杩炴帴璧锋潵. + +- 鍒峰叆 TencentOS 绯荤粺 + + + - 涓嬭浇婧愮爜 + + 涓嬭浇 TencentOS 婧愮爜, 杩欓噷鍋囧畾淇濆瓨璺緞涓 ``. + + git clone https://github.com/Tencent/TencentOS-tiny.git + + 鍒囧嚭 tos_evb_g0 鍒嗘敮婧愮爜骞朵繚瀛樺埌 ``. + + git clone -b tos_evb_g0 + + 杩涘叆 `` 鐩綍, 灏 TOS_CONFIG/_user_config.h 澶嶅埗涓浠戒繚瀛樹负 TOS_CONFIG/user_config.h, + 鐒跺悗淇敼 user_config.h 涓殑瀹忓畾涔. + + - 缂栬瘧 + + 浣跨敤濡備笅鍛戒护瀹夎宸ュ叿閾 + + sudo apt-get install -y gcc-arm-none-eabi + + 杩涘叆 tos_evb 鐩綍涓嬫墽琛屽涓嬪懡浠ょ紪璇戝浐浠 + + export TOS_SRC_ROOT= + mkdir build && cmake .. && make + + 鎵ц鎴愬姛鍚庝細鐢熸垚鏂囦欢 mqtt_iot_explorer_tc_ch20_oled.bin + + - 杩炴帴 STLINK 骞跺埛鍏ュ浐浠 + + 灏 STLINK 杩炴帴鍒版澘瀛愪笂, 鐒跺悗鎵ц濡備笅鍛戒护鍒峰叆 + + make flash + +- 娴嬭瘯 + + 涓插彛宸ュ叿杩炴帴鏉垮瓙 + + set line /dev/ttyUSB0 + set speed 115200 + set carrier-watch off + connect + + 鎸変笅鏉垮瓙涓婄殑 reset 鎸夐敭, 绋嬪簭灏辫繍琛屼簡, 涓插彛浼氳緭鍑 WIFI CONNECTED 涔嬬被鐨, + 鑵捐浜戜笂鏄剧ず璁惧涓婄嚎. diff --git a/examples/tos_meets_rust/docs/setup.mac.md b/examples/tos_meets_rust/docs/setup.mac.md new file mode 100644 index 00000000..1d190985 --- /dev/null +++ b/examples/tos_meets_rust/docs/setup.mac.md @@ -0,0 +1,103 @@ +# 鐢查啗妫娴嬩华 - mac 鐜涓嬫惌寤 + +鐢变簬涔嬪墠鍦 Linux 涓婂鐜杩涜浜嗛厤缃, 鏉垮瓙鍩烘湰涓婂凡缁忓垵濮嬪寲濂戒簡, +鐩墠鍦 mac 涓婇厤缃渶瑕佸仛鐨勫伐浣滀笉鏄お澶, 涓嬮潰鏈変簺姝ラ鍙兘涓嶅お璇︾粏, +鍙互鍙傝 [Linux](./setup.linux.ubuntu.md). + +## 宸ュ叿鍑嗗 + +- stlink 瀹夎 + + - 渚濊禆椤 + + - [cmake](https://cmake.org/download/): 涓嬭浇 tar.gz 鐨勫帇缂╁寘鑷瀹夎骞舵坊鍔犲埌鐜鍙橀噺涓 + - [llvm](https://releases.llvm.org/download.html): 鍙傝 cmake 澶勭悊 + - libusb: brew install libusb + + - 瀹夎 + + git clone https://github.com/stlink-org/stlink + cd stlink + make release + + 瀹夎鍒 $HOME/.usr/stlink 鐩綍涓 + + cd build/Release && make install DESTDIR=$HOME/.usr/stlink + + 浣跨敤 ioreg 鏌ョ湅 USB 璁惧: + + 鉃 ioreg -p IOUSB + +-o Root + +-o ... + | +-o USB2.1 Hub@14100000 + | +-o ... + | +-o STM32 STLink@14110000 + | +-o ... + +-o ... + + 鍙互鐪嬪埌鎴戜滑鐨 STM32 璁惧宸茬粡杩炴帴涓婁簡, 鐒跺悗浣跨敤 st-info --probe 鐪嬬湅鏄惁鑳芥娴嬪埌 + stlink 缂栫▼鍣, 濡傛灉鎵惧埌鍒欐湁绫讳技涓嬮潰鐨勮緭鍑 + + Found 1 stlink programmers + ... + +- CH340 椹卞姩 + + - macOS Mojava 10.4 鎴栬呬箣鍚庣殑鐗堟湰: 鏃犻渶瀹夎 + - macOS Mojava 10.4 涔嬪悗鐨勭増鏈: 鍒癧杩欓噷](https://github.com/adrianmihalko/ch340g-ch34g-ch34x-mac-os-x-driver)涓嬭浇瀹夎 + + 瀹夎瀹屾垚鍚庨氳繃 `ls -1 /dev/cu*` 鑳藉鐪嬪埌涓涓 `/dev/cu.wchusbserial****` 涔嬬被鐨勮澶. + +- 涓插彛宸ュ叿 + + wget http://www.kermitproject.org/ftp/kermit/test/tar/x.zip + unzip x.zip -d c-kermit && cd c-kermit + make KFLAGS='-Wno-error=implicit-function-declaration' macosx + make DESTDIR=$HOME/.usr/kermit BINDIR=$DESTDIR MANDIR=$DESTDIR install + + 浼氬畨瑁呭埌 $HOME/.usr/kermit 鐩綍, 灏嗚鐩綍娣诲姞鍒扮幆澧冨彉閲 PATH 涓. + +- 杩炴帴涓插彛璁惧 + + 鏄剧ず鍙敤鐨勪覆鍙 + + ls -1 /dev/tty.* /dev/cu.* + + 杈撳嚭鏍蜂緥 + + /dev/cu.Bluetooth-Incoming-Port + /dev/cu.usbserial-14120 + /dev/tty.Bluetooth-Incoming-Port + /dev/tty.usbserial-14120 + + 鏍规嵁 https://pbxbook.com/other/mac-tty.html, 鎴戜滑鐭ラ亾鎴戜滑搴旇閫夋嫨 cu 寮澶寸殑璁惧, + 鍥犳杩欓噷鎴戜滑浣跨敤 /dev/cu.usbserial-14120. + + 鎵撳紑 ~/.kermrc, 杈撳叆濡備笅鍛戒护 + + set line /dev/cu.usbserial-14120 + set speed 115200 + set carrier-watch off + connect + + 鐒跺悗姣忔鎵ц kermit 鍛戒护灏变細鑷姩杩炰笂涓插彛, 鑳藉鐪嬪埌璋冭瘯淇℃伅杈撳嚭, 瑕侀鍑虹殑璇濇寜涓 Ctrl-\ 鐒跺悗鎸変笅 C 鍗冲彲. + +## WiFi 鍥轰欢瀹夎 + +鍙傝 [Linux](./setup.linux.ubuntu.md). + +## 鑵捐浜戝笎鍙 + +鍙傝 [Linux](./setup.linux.ubuntu.md). + +## TencentOS 鍥轰欢瀹夎 + +缂栬瘧鎵闇瑕佺殑宸ュ叿閾惧畨瑁呮柟娉曞涓 + + brew install armmbed/formulae/arm-none-eabi-gcc + +瀹夎鍚庣殑璺緞涓 + + /usr/local/bin/arm-none-eabi-* + +鍏朵粬鍙傝 [Linux](./setup.linux.ubuntu.md). diff --git a/examples/tos_meets_rust/libs/rustapp/stub.c b/examples/tos_meets_rust/libs/rustapp/stub.c new file mode 100644 index 00000000..7061844c --- /dev/null +++ b/examples/tos_meets_rust/libs/rustapp/stub.c @@ -0,0 +1,3 @@ +void application_entry_rust() { + printf("Stub in rustapp library!\r\n"); +} diff --git a/examples/tos_meets_rust/libs/rustcore/stub.c b/examples/tos_meets_rust/libs/rustcore/stub.c new file mode 100644 index 00000000..b823998e --- /dev/null +++ b/examples/tos_meets_rust/libs/rustcore/stub.c @@ -0,0 +1,3 @@ +void rust_libcore_stub() { + printf("Stub in rust libcore library!\r\n"); +} diff --git a/examples/tos_meets_rust/toolchain/arm-gcc.cmake b/examples/tos_meets_rust/toolchain/arm-gcc.cmake new file mode 100644 index 00000000..b6a7c28d --- /dev/null +++ b/examples/tos_meets_rust/toolchain/arm-gcc.cmake @@ -0,0 +1,33 @@ +# ARM none eabi gcc toolchain configuration +# Copyright (c) 2016 Ryan Kurte +# This file is covered under the MIT license available at: https://opensource.org/licenses/MIT + +# We are cross compiling so we don't want compiler tests to run, as they will fail +set(CMAKE_C_COMPILER_WORKS TRUE) +set(CMAKE_CXX_COMPILER_WORKS TRUE) + +# Indicate we aren't compiling for an OS +set(CMAKE_SYSTEM_NAME Generic) + +# Set processor type +set(CMAKE_SYSTEM_PROCESSOR arm) + +# Set compiler paths +set(CMAKE_C_COMPILER ${COMPILER_PREFIX}arm-none-eabi-gcc) +set(CMAKE_CXX_COMPILER ${COMPILER_PREFIX}arm-none-eabi-g++) +set(CMAKE_ASM_COMPILER ${COMPILER_PREFIX}arm-none-eabi-g++) + +# Set other tools +set(OBJSIZE ${COMPILER_PREFIX}arm-none-eabi-size) +set(OBJCOPY ${COMPILER_PREFIX}arm-none-eabi-objcopy) +set(OBJDUMP ${COMPILER_PREFIX}arm-none-eabi-objdump) +set(DEBUGGER ${COMPILER_PREFIX}arm-none-eabi-gdb) + +# Remove preset linker flags +set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") +set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "") +set(CMAKE_SHARED_LIBRARY_LINK_ASM_FLAGS "") + +# Set library options +set(SHARED_LIBS OFF) +set(STATIC_LIBS ON) diff --git a/examples/tos_meets_rust/toolchain/erase.jlink b/examples/tos_meets_rust/toolchain/erase.jlink new file mode 100644 index 00000000..a244ff25 --- /dev/null +++ b/examples/tos_meets_rust/toolchain/erase.jlink @@ -0,0 +1,4 @@ +h +r +erase +qc \ No newline at end of file diff --git a/examples/tos_meets_rust/toolchain/flash.in b/examples/tos_meets_rust/toolchain/flash.in new file mode 100644 index 00000000..4dae7851 --- /dev/null +++ b/examples/tos_meets_rust/toolchain/flash.in @@ -0,0 +1,7 @@ +device stm32f401 +h +loadbin ${BINARY}, ${FLASH_ORIGIN} +verifybin ${BINARY}, ${FLASH_ORIGIN} +r +g +qc diff --git a/examples/tos_meets_rust/toolchain/jlink.cmake b/examples/tos_meets_rust/toolchain/jlink.cmake new file mode 100644 index 00000000..3806590a --- /dev/null +++ b/examples/tos_meets_rust/toolchain/jlink.cmake @@ -0,0 +1,56 @@ +# JLink functions +# Adds targets for JLink programmers and emulators +# Copyright (c) 2016 Ryan Kurte +# This file is covered under the MIT license available at: https://opensource.org/licenses/MIT + +# Configure flasher script for the project +set(BINARY ${PROJECT_NAME}.bin) +configure_file(${CMAKE_CURRENT_LIST_DIR}/flash.in ${CMAKE_CURRENT_BINARY_DIR}/flash.jlink) + +#Add JLink commands +add_custom_target(debug + COMMAND ${DEBUGGER} -tui -command ${CMAKE_CURRENT_LIST_DIR}/remote.gdbconf ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME} + DEPENDS ${PROJECT_NAME} + ) + +add_custom_target(debug-server + COMMAND JLinkGDBServer -device ${DEVICE} -speed 4000 -if SWD + DEPENDS ${PROJECT_NAME} + ) + +add_custom_target(d + COMMAND ${DEBUGGER} -command ${CMAKE_CURRENT_LIST_DIR}/remote.gdbconf ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME} + DEPENDS ${PROJECT_NAME} + ) + +add_custom_target(ds + COMMAND JLinkGDBServer -device ${DEVICE} -speed 4000 -if SWD + DEPENDS ${PROJECT_NAME} + ) + +add_custom_target(flash + COMMAND JLinkExe -device ${DEVICE} -speed 4000 -if SWD -CommanderScript ${CMAKE_CURRENT_BINARY_DIR}/flash.jlink + DEPENDS ${PROJECT_NAME} + ) + +add_custom_target(f + COMMAND JLinkExe -device ${DEVICE} -speed 4000 -if SWD -CommanderScript ${CMAKE_CURRENT_BINARY_DIR}/flash.jlink + DEPENDS ${PROJECT_NAME} + ) + + +add_custom_target(erase + COMMAND JLinkExe -device ${DEVICE} -speed 4000 -if SWD -CommanderScript ${CMAKE_CURRENT_LIST_DIR}/erase.jlink + ) + +add_custom_target(e + COMMAND JLinkExe -device ${DEVICE} -speed 4000 -if SWD -CommanderScript ${CMAKE_CURRENT_LIST_DIR}/erase.jlink + ) + +add_custom_target(reset + COMMAND JLinkExe -device ${DEVICE} -speed 4000 -if SWD -CommanderScript ${CMAKE_CURRENT_LIST_DIR}/reset.jlink + ) + +add_custom_target(r + COMMAND JLinkExe -device ${DEVICE} -speed 4000 -if SWD -CommanderScript ${CMAKE_CURRENT_LIST_DIR}/reset.jlink + ) diff --git a/examples/tos_meets_rust/toolchain/post-build.cmake b/examples/tos_meets_rust/toolchain/post-build.cmake new file mode 100644 index 00000000..912acca4 --- /dev/null +++ b/examples/tos_meets_rust/toolchain/post-build.cmake @@ -0,0 +1,12 @@ +# ARM post build commands +# Copyright (c) 2016 Ryan Kurte +# This file is covered under the MIT license available at: https://opensource.org/licenses/MIT + +# Create binary file +add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${OBJCOPY} -O binary ${PROJECT_NAME} ${PROJECT_NAME}.bin) + +add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${OBJCOPY} -O ihex ${PROJECT_NAME} ${PROJECT_NAME}.hex) + +add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${OBJDUMP} -d -S ${PROJECT_NAME} > ${PROJECT_NAME}.dmp) + +add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${OBJSIZE} ${PROJECT_NAME}) diff --git a/examples/tos_meets_rust/toolchain/remote.gdbconf b/examples/tos_meets_rust/toolchain/remote.gdbconf new file mode 100644 index 00000000..7c0306d3 --- /dev/null +++ b/examples/tos_meets_rust/toolchain/remote.gdbconf @@ -0,0 +1,2 @@ +b main +target remote localhost:2331 diff --git a/examples/tos_meets_rust/toolchain/reset.jlink b/examples/tos_meets_rust/toolchain/reset.jlink new file mode 100644 index 00000000..2e6e4a52 --- /dev/null +++ b/examples/tos_meets_rust/toolchain/reset.jlink @@ -0,0 +1,4 @@ +device stm32f401 +h +r +qc \ No newline at end of file diff --git a/examples/tos_meets_rust/toolchain/stlink.cmake b/examples/tos_meets_rust/toolchain/stlink.cmake new file mode 100644 index 00000000..966b9dd3 --- /dev/null +++ b/examples/tos_meets_rust/toolchain/stlink.cmake @@ -0,0 +1,20 @@ +# JLink functions +# Adds targets for stlink programmers and emulators + + +add_custom_target(flash + COMMAND st-flash --reset write ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.bin 0x8000000 + DEPENDS ${PROJECT_NAME} + ) + + +add_custom_target(erase + COMMAND st-flash erase + DEPENDS ${PROJECT_NAME} + ) + + +add_custom_target(reset + COMMAND st-flash reset + DEPENDS ${PROJECT_NAME} + ) diff --git a/examples/tos_meets_rust/toolchain/stm32-base.cmake b/examples/tos_meets_rust/toolchain/stm32-base.cmake new file mode 100644 index 00000000..0d41bd13 --- /dev/null +++ b/examples/tos_meets_rust/toolchain/stm32-base.cmake @@ -0,0 +1,104 @@ +function(stm32_configure_linker_addresses target) + if(FLASH_ORIGIN) + target_link_options(${target} + PRIVATE "LINKER:--defsym=flash_origin=${FLASH_ORIGIN}" + ) + else() + # We always need to set FLASH_ORIGIN so that the script + # toolchain/flash.in gets configured correctly. + set(FLASH_ORIGIN 0x00000000 PARENT_SCOPE) + endif() + + if(FLASH_LENGTH) + target_link_options(${target} + PRIVATE "LINKER:--defsym=flash_length=${FLASH_LENGTH}" + ) + endif() + + if(RAM_ORIGIN) + target_link_options(${target} + PRIVATE "LINKER:--defsym=ram_origin=${RAM_ORIGIN}" + ) + endif() + + if(RAM_LENGTH) + target_link_options(${target} + PRIVATE "LINKER:--defsym=ram_length=${RAM_LENGTH}" + ) + endif() +endfunction(stm32_configure_linker_addresses) + +if (NOT DEFINED DEVICE) + message(FATAL_ERROR "No processor defined") +endif () +message("Device: ${DEVICE}") + +# Convert to upper case +string(TOUPPER ${DEVICE} DEVICE_U) +message("Processor: ${DEVICE_U}") + +set(CPU_TYPE "m0plus") + + +# Set compiler flags +# Common arguments +add_definitions("-D${DEVICE}") +add_definitions("-DUSE_HAL_DRIVER") +set(COMMON_DEFINITIONS "-Wextra -Wall -Wno-unused-parameter -mcpu=cortex-${CPU_TYPE} -mthumb -fno-builtin -ffunction-sections -fdata-sections -fomit-frame-pointer -mfpu=fpv4-sp-d16 -mfloat-abi=soft ${OPTIONAL_DEBUG_SYMBOLS}") +set(DEPFLAGS "-MMD -MP") + +# Enable FLTO optimization if required +if (USE_FLTO) + set(OPTFLAGS "-Os -flto") +else () + set(OPTFLAGS "-Os") +endif () + +# Build flags +set(CMAKE_C_FLAGS "-std=gnu99 ${COMMON_DEFINITIONS} ${CPU_FIX} --specs=nano.specs ${DEPFLAGS}") +set(CMAKE_CXX_FLAGS "${COMMON_DEFINITIONS} ${CPU_FIX} --specs=nano.specs ${DEPFLAGS}") +set(CMAKE_ASM_FLAGS "${COMMON_DEFINITIONS} -x assembler-with-cpp -DLOOP_ADDR=0x8000") + +# Set default inclusions +set(LIBS ${LIBS} -lgcc -lc -lnosys -lgcc -lc -lnosys) + +# Debug Flags +set(COMMON_DEBUG_FLAGS "-O0 -g -gdwarf-2") +set(CMAKE_C_FLAGS_DEBUG "${COMMON_DEBUG_FLAGS}") +set(CMAKE_CXX_FLAGS_DEBUG "${COMMON_DEBUG_FLAGS}") +set(CMAKE_ASM_FLAGS_DEBUG "${COMMON_DEBUG_FLAGS}") + +# Release Flags +set(COMMON_RELEASE_FLAGS "${OPTFLAGS} -DNDEBUG=1 -DRELEASE=1") +set(CMAKE_C_FLAGS_RELEASE "${COMMON_RELEASE_FLAGS}") +set(CMAKE_CXX_FLAGS_RELEASE "${COMMON_RELEASE_FLAGS}") +set(CMAKE_ASM_FLAGS_RELEASE "${COMMON_RELEASE_FLAGS}") + +# Print debug info helper function +function(print_debug_info) + message("COMPILER_PREFIX =${COMPILER_PREFIX}") + message("CMAKE_SOURCE_DIR =${CMAKE_SOURCE_DIR}") + message("CMAKE_C_COMPILER =${CMAKE_C_COMPILER}") + message("CMAKE_C_FLAGS =${CMAKE_C_FLAGS}") + message("CMAKE_C_LINK_EXECUTABLE =${CMAKE_C_LINK_EXECUTABLE}") + message("CMAKE_EXE_LINKER_FLAGS =${CMAKE_EXE_LINKER_FLAGS}") + message("CMAKE_AR =${CMAKE_AR}") + + message("Definitions: ") + get_directory_property(defs DIRECTORY ${CMAKE_SOURCE_DIR} COMPILE_DEFINITIONS) + foreach (def ${defs}) + message(STATUS "-D${def}") + endforeach () + + get_property(dirs TARGET ${PROJECT_NAME} PROPERTY INCLUDE_DIRECTORIES) + message("Includes: ") + foreach (dir ${dirs}) + message(STATUS "${dir}") + endforeach () + + get_property(libs TARGET ${PROJECT_NAME} PROPERTY LINK_LIBRARIES) + message("Libraries:") + foreach (libs ${libs}) + message(STATUS "${libs}") + endforeach () +endfunction() diff --git a/examples/tos_meets_rust/tosglue.c b/examples/tos_meets_rust/tosglue.c new file mode 100644 index 00000000..6a97e88e --- /dev/null +++ b/examples/tos_meets_rust/tosglue.c @@ -0,0 +1,195 @@ +/* + + TencentOS API wrapper for rust world + +*/ + +#include "tos_k.h" +#include "esp8266_tencent_firmware.h" +#include "tencent_firmware_module_wrapper.h" +#include "ch20_parser.h" +#include "oled.h" +#include "user_config.h" + +#include "tos_at.h" + +#define REPORT_DATA_TEMPLATE "{\\\"method\\\":\\\"report\\\"\\,\\\"clientToken\\\":\\\"00000001\\\"\\,\\\"params\\\":{\\\"ch20_ppm_value\\\":%.3f}}" + +#define RECV_LEN 1024 +uint8_t recv_data[RECV_LEN]; + +void default_message_handler(mqtt_message_t* msg) +{ + printf("callback:\r\n"); + printf("---------------------------------------------------------\r\n"); + printf("\ttopic:%s\r\n", msg->topic); + printf("\tpayload:%s\r\n", msg->payload); + printf("---------------------------------------------------------\r\n"); +} + +char payload[256] = {0}; +static char report_topic_name[TOPIC_NAME_MAX_SIZE] = {0}; +static char report_reply_topic_name[TOPIC_NAME_MAX_SIZE] = {0}; + +k_mail_q_t mail_q; +ch20_data_t ch20_value; +uint8_t ch20_value_pool[5 * sizeof(ch20_data_t)]; + +void rust_mqtt_daemon() +{ + char *str = "TencentOS XXXX"; + + OLED_Init(); + OLED_Clear(); + OLED_ShowString(0, 0, (uint8_t*)str, 16); + + + int ret = 0; + int size = 0; + int lightness = 0; + mqtt_state_t state; + + char *product_id = PRODUCT_ID; + char *device_name = DEVICE_NAME; + char *key = DEVICE_KEY; + + device_info_t dev_info; + memset(&dev_info, 0, sizeof(device_info_t)); + + size_t mail_size; + float ch20_ppm_value; + char ch20_ppm_str[20]; + + + /* OLED鏄剧ず鏃ュ織 */ + OLED_ShowString(0, 2, (uint8_t*)"connecting...", 16); + + /* + + Please Choose your AT Port first, default is HAL_UART_2(USART2) + + 缃戠粶灞傚垵濮嬪寲, 杩欎釜鍑芥暟浣嶄簬 + + tos/devices/esp8266_tencent_firmware/esp8266_tencent_firmware.c + + */ + + ret = esp8266_tencent_firmware_sal_init(HAL_UART_PORT_2); + + if (ret < 0) { + printf("esp8266 tencent firmware sal init fail, ret is %d\r\n", ret); + } + + esp8266_tencent_firmware_join_ap(WIFI_NAME, WIFI_PASSWORD); + + strncpy(dev_info.product_id, product_id, PRODUCT_ID_MAX_SIZE); + strncpy(dev_info.device_name, device_name, DEVICE_NAME_MAX_SIZE); + strncpy(dev_info.device_serc, key, DEVICE_SERC_MAX_SIZE); + tos_tf_module_info_set(&dev_info, TLS_MODE_PSK); + + /* + + DEFAULT_MQTT_PARAMS 瀹氫箟鍦 tos/net/tencent_firmware_module_wrapper/tencent_firmware_module_wrapper.h + + typedef struct mqtt_param_st { + tls_mode_t tls_mode; + uint32_t command_timeout; + uint32_t keep_alive_interval_ms; + uint8_t clean_session; + uint8_t auto_connect_enable; + } mqtt_param_t; + #define DEFAULT_MQTT_PARAMS { TLS_MODE_PSK, MQTT_COMMAND_TIMEOUT, 240, 1, 1 } + + */ + mqtt_param_t init_params = DEFAULT_MQTT_PARAMS; + if (tos_tf_module_mqtt_conn(init_params) != 0) { + printf("module mqtt conn fail\r\n"); + } else { + printf("module mqtt conn success\r\n"); + } + + if (tos_tf_module_mqtt_state_get(&state) != -1) { + printf("MQTT: %s\n", state == MQTT_STATE_CONNECTED ? "CONNECTED" : "DISCONNECTED"); + } + + /* 寮濮嬭闃卼opic */ + size = snprintf(report_reply_topic_name, TOPIC_NAME_MAX_SIZE, "$thing/down/property/%s/%s", product_id, device_name); + + if (size < 0 || size > sizeof(report_reply_topic_name) - 1) { + printf("sub topic content length not enough! content size:%d buf size:%d", size, (int)sizeof(report_reply_topic_name)); + } + if (tos_tf_module_mqtt_sub(report_reply_topic_name, QOS0, default_message_handler) != 0) { + printf("module mqtt sub fail\n"); + } else { + printf("module mqtt sub success\n"); + } + + memset(report_topic_name, sizeof(report_topic_name), 0); + size = snprintf(report_topic_name, TOPIC_NAME_MAX_SIZE, "$thing/up/property/%s/%s", product_id, device_name); + + if (size < 0 || size > sizeof(report_topic_name) - 1) { + printf("pub topic content length not enough! content size:%d buf size:%d", size, (int)sizeof(report_topic_name)); + } + + /* 鍒涘缓閭 */ + tos_mail_q_create(&mail_q, ch20_value_pool, 3, sizeof(ch20_data_t)); + + HAL_NVIC_DisableIRQ(USART3_4_IRQn); + + if (ch20_parser_init() == -1) { + printf("ch20 parser init fail\r\n"); + return; + } + + while (1) { + /* 閫氳繃鎺ユ敹閭欢鏉ヨ鍙栨暟鎹 */ + HAL_NVIC_EnableIRQ(USART3_4_IRQn); + tos_mail_q_pend(&mail_q, (uint8_t*)&ch20_value, &mail_size, TOS_TIME_FOREVER); + HAL_NVIC_DisableIRQ(USART3_4_IRQn); + + /* 鎺ユ敹鍒颁箣鍚庢墦鍗颁俊鎭 */ + ch20_ppm_value = ch20_value.data / 1000.0; + printf("ch20 value: %.3f\r\n", ch20_ppm_value); + + /* OLED鏄剧ず鍊 */ + sprintf(ch20_ppm_str, "%.3f ppm(mg/m3)", ch20_ppm_value); + OLED_ShowString(0, 2, (uint8_t*)ch20_ppm_str, 16); + + /* 涓婃姤鍊 */ + memset(payload, 0, sizeof(payload)); + snprintf(payload, sizeof(payload), REPORT_DATA_TEMPLATE, ch20_ppm_value); + + if (lightness > 100) { + lightness = 0; + } + + if (tos_tf_module_mqtt_pub(report_topic_name, QOS0, payload) != 0) { + printf("module mqtt pub fail\n"); + break; + } else { + printf("module mqtt pub success\n"); + } + + tos_sleep_ms(5000); + } +} + +void rust_print(const char *msg) { + printf("%s\r\n", msg); +} + +void rust_oled_print(unsigned int x, unsigned int y, char *msg) { + OLED_ShowString(0, 2, (uint8_t*)msg, 16); +} + +int rust_wifi_init() { + return esp8266_tencent_firmware_sal_init(HAL_UART_PORT_2); +} + +void rust_wifi_connect(const char *ssid, const char *psd) { + esp8266_tencent_firmware_join_ap(ssid, psd); +} + +void rust_sleep(unsigned int ms) { + tos_sleep_ms(ms); +}