LP-MSPM0L1306开发板试用体验---轻松实现xprintf串口打印

[复制链接]
1211|1
 楼主| xiaoqi976633690 发表于 2023-10-6 16:15 | 显示全部楼层 |阅读模式
本帖最后由 xiaoqi976633690 于 2023-10-6 16:28 编辑

1、开发环境
开发板:LP-MSPM0L1306 LaunchPad™ 开发套件
IDE:Code Composer Studio 12.4.0
仿真器:板载
例程参考自官方
SDK\mspm0_sdk_1_10_01_05\examples\nortos\LP_MSPM0L1306\driverlib\uart_echo_interrupts_standby

2、硬件配置


1.png


0.png


3、代码实现
串口初始化代码官方例程已经写好了直接参考就好了
  1. /* Defines for UART_0 */
  2. #define UART_0_INST                                                        UART0
  3. #define UART_0_INST_IRQHandler                                  UART0_IRQHandler
  4. #define UART_0_INST_INT_IRQN                                      UART0_INT_IRQn
  5. #define GPIO_UART_0_RX_PORT                                                GPIOA
  6. #define GPIO_UART_0_TX_PORT                                                GPIOA
  7. #define GPIO_UART_0_RX_PIN                                         DL_GPIO_PIN_9
  8. #define GPIO_UART_0_TX_PIN                                         DL_GPIO_PIN_8
  9. #define GPIO_UART_0_IOMUX_RX                                     (IOMUX_PINCM10)
  10. #define GPIO_UART_0_IOMUX_TX                                      (IOMUX_PINCM9)
  11. #define GPIO_UART_0_IOMUX_RX_FUNC                      IOMUX_PINCM10_PF_UART0_RX
  12. #define GPIO_UART_0_IOMUX_TX_FUNC                       IOMUX_PINCM9_PF_UART0_TX
  13. #define UART_0_BAUD_RATE                                                  (9600)
  14. #define UART_0_IBRD_33_kHZ_9600_BAUD                                         (1)
  15. #define UART_0_FBRD_33_kHZ_9600_BAUD                                         (9)



  1. static const DL_UART_Main_ClockConfig gUART_0ClockConfig = {
  2.     .clockSel    = DL_UART_MAIN_CLOCK_LFCLK,
  3.     .divideRatio = DL_UART_MAIN_CLOCK_DIVIDE_RATIO_1
  4. };

  5. static const DL_UART_Main_Config gUART_0Config = {
  6.     .mode        = DL_UART_MAIN_MODE_NORMAL,
  7.     .direction   = DL_UART_MAIN_DIRECTION_TX_RX,
  8.     .flowControl = DL_UART_MAIN_FLOW_CONTROL_NONE,
  9.     .parity      = DL_UART_MAIN_PARITY_NONE,
  10.     .wordLength  = DL_UART_MAIN_WORD_LENGTH_8_BITS,
  11.     .stopBits    = DL_UART_MAIN_STOP_BITS_ONE
  12. };

  13. SYSCONFIG_WEAK void SYSCFG_DL_UART_0_init(void)
  14. {
  15.     DL_UART_Main_setClockConfig(UART_0_INST, (DL_UART_Main_ClockConfig *) &gUART_0ClockConfig);

  16.     DL_UART_Main_init(UART_0_INST, (DL_UART_Main_Config *) &gUART_0Config);
  17.     /*
  18.      * Configure baud rate by setting oversampling and baud rate divisors.
  19.      *  Target baud rate: 9600
  20.      *  Actual baud rate: 9576.04
  21.      */
  22.     DL_UART_Main_setOversampling(UART_0_INST, DL_UART_OVERSAMPLING_RATE_3X);
  23.     DL_UART_Main_setBaudRateDivisor(UART_0_INST, UART_0_IBRD_33_kHZ_9600_BAUD, UART_0_FBRD_33_kHZ_9600_BAUD);


  24.     /* Configure Interrupts */
  25.     DL_UART_Main_enableInterrupt(UART_0_INST,
  26.                                  DL_UART_MAIN_INTERRUPT_RX);


  27.     DL_UART_Main_enable(UART_0_INST);
  28. }



xprintf移植
源码 https://gitee.com/eegithub/xprintf_demo功能裁剪:
  1. #define XF_USE_OUTPUT        1        /* 1: Enable output functions */
  2. #define        XF_CRLF                        0        /* 1: Convert \n ==> \r\n in the output char */
  3. #define        XF_USE_DUMP                0        /* 1: Enable put_dump function */
  4. #define        XF_USE_LLI                0        /* 1: Enable long long integer in size prefix ll */
  5. #define        XF_USE_FP                0        /* 1: Enable support for floating point in type e and f */
  6. #define XF_DPC                        '.'        /* Decimal separator for floating point */
  7. #define XF_USE_INPUT        0        /* 1: Enable input functions */
  8. #define        XF_INPUT_ECHO        0        /* 1: Echo back input chars in xgets function */

格式:
  1. xprintf("%d", 1234);             /* "1234" */
  2.     xprintf("%6d,%3d%%", -200, 5);   /* " -200, 5%" */
  3.     xprintf("%-6u", 100);            /* "100 " */
  4.     xprintf("%ld", 12345678);        /* "12345678" */
  5.     xprintf("%llu", 0x100000000);    /* "4294967296" <XF_USE_LLI> */
  6.     xprintf("%lld", -1LL);           /* "-1" <XF_USE_LLI> */
  7.     xprintf("%04x", 0xA3);           /* "00a3" */
  8.     xprintf("%08lX", 0x123ABC);      /* "00123ABC" */
  9.     xprintf("%016b",        0x550F); /* "0101010100001111" */
  10.     xprintf("%*d", 6, 100);          /* "100" */
  11.     xprintf("%s", "abcdefg");        /* "abcdefg" */
  12.     xprintf("%5s", "abc");           /* " abc" */
  13.     xprintf("%-5s", "abc");          /* "abc " */
  14.     xprintf("%.5s", "abcdefg");      /* "abcde" */
  15.     xprintf("%-5.2s", "abcdefg");    /* "ab" */
  16.     xprintf("%c", 'a');              /* "a" */
  17.     xprintf("%12f", 10.0);           /* " 10.000000" <XF_USE_FP> */
  18.     xprintf("%.4E", 123.45678);      /* "1.2346E+02" <XF_USE_FP> */

设备输入/输出功能
  • 输出函数是用户提供的回调函数,用于将字节写入输出设备。其地址应设置为模块中的函数指针xfunc_output,默认输出设备。通常,此函数将字节放入 UART、LCD 或某些输出设备。输出函数从 xputc() 回调。有一个宏可以轻松设置。
  • 例如,当将 void uart1_putc (uint8_t chr) 附加到模块时, xdev_out(uart1_putc) 就可以了。如果输出函数有多个参数或没有简单的输出函数,则需要另一个封装函数。 xfputc()、xfputs()、xfprintf() 和 xsprintf() 使用其参数覆盖默认输出设备。
  • 输入函数是用户提供的回调函数,用于从输入设备读取字节。它的地址应该设置为函数指针 xfunc_input,默认输入设备。有一个宏 xdev_in() 可以轻松设置它。
  • 例如xdev_in(uart1_getc)。xfgets() 函数使用其参数覆盖默认输入设备。输入函数从 xgets() 函数回调。通常,输入函数从输入设备或文件中读取一个字节。当输入设备报告流结束时,输入函数应返回-1。xgets() 函数以零中止,应用程序将能够检测到它。




这里我们只需要实现一个byte发送的函数即可
  1. void UART_0_write_Byte(uint8_t data)//1 byte 发送
  2. {
  3.         DL_UART_transmitDataBlocking(UART_0_INST,data);
  4. }
  1. xdev_out(UART_0_write_Byte);


主函数
  1. #include "ti_msp_dl_config.h"
  2. #include "xprintf.h"

  3. uint8_t data = 0;
  4. const uint8_t str[]="HELLO,TI MCU SERIAL MSPM0L1306\n";
  5. void UART_0_write_str(uint8_t *s);
  6. void UART_0_write_Byte(uint8_t data);


  7. int main(void)
  8. {
  9.         uint8_t i=0;
  10.         SYSCFG_DL_init();

  11.         NVIC_ClearPendingIRQ(UART_0_INST_INT_IRQN);
  12.         NVIC_EnableIRQ(UART_0_INST_INT_IRQN);
  13.         DL_SYSCTL_disableSleepOnExit();
  14.         xdev_out(UART_0_write_Byte);
  15.         while (1)
  16.         {

  17.                 delay_cycles((32000*1000));
  18.                 UART_0_write_str(str);
  19.                 UART_0_write_str("string send\n");
  20.             xprintf("xprintf :%d\n\r", i);
  21.             i++;
  22.         }
  23. }

  24. void UART_0_INST_IRQHandler(void)
  25. {
  26.         switch (DL_UART_Main_getPendingInterrupt(UART_0_INST))
  27.         {
  28.         case DL_UART_MAIN_IIDX_RX:
  29.                 data = DL_UART_Main_receiveData(UART_0_INST);
  30.                 DL_UART_Main_transmitData(UART_0_INST, data);
  31.                 break;
  32.         default:
  33.                 break;
  34.         }
  35. }

  36. void UART_0_write_str(uint8_t *s)//字符串发送函数
  37. {
  38.         uint8_t i=0;
  39.         while(s[i]!='\0')
  40.         {
  41.                 if(s[i]=='\r') DL_UART_transmitDataBlocking(UART_0_INST, '\n');
  42.                 DL_UART_transmitDataBlocking(UART_0_INST, s[i]);
  43.                 i++;
  44.         }
  45. }

  46. void UART_0_write_Byte(uint8_t data)//1 byte 发送
  47. {
  48.         DL_UART_transmitDataBlocking(UART_0_INST,data);
  49. }




4、实验现象


3.png







































评分

参与人数 1威望 +10 收起 理由
xyz549040622 + 10 很给力!

查看全部评分

xyz549040622 发表于 2023-10-16 10:05 | 显示全部楼层
果断收藏了,这个可以移植到所有的MCU中,稍后就安排移植,太方便了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

35

主题

205

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部