本帖最后由 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、硬件配置
3、代码实现
串口初始化代码官方例程已经写好了直接参考就好了
- /* Defines for UART_0 */
- #define UART_0_INST UART0
- #define UART_0_INST_IRQHandler UART0_IRQHandler
- #define UART_0_INST_INT_IRQN UART0_INT_IRQn
- #define GPIO_UART_0_RX_PORT GPIOA
- #define GPIO_UART_0_TX_PORT GPIOA
- #define GPIO_UART_0_RX_PIN DL_GPIO_PIN_9
- #define GPIO_UART_0_TX_PIN DL_GPIO_PIN_8
- #define GPIO_UART_0_IOMUX_RX (IOMUX_PINCM10)
- #define GPIO_UART_0_IOMUX_TX (IOMUX_PINCM9)
- #define GPIO_UART_0_IOMUX_RX_FUNC IOMUX_PINCM10_PF_UART0_RX
- #define GPIO_UART_0_IOMUX_TX_FUNC IOMUX_PINCM9_PF_UART0_TX
- #define UART_0_BAUD_RATE (9600)
- #define UART_0_IBRD_33_kHZ_9600_BAUD (1)
- #define UART_0_FBRD_33_kHZ_9600_BAUD (9)
- static const DL_UART_Main_ClockConfig gUART_0ClockConfig = {
- .clockSel = DL_UART_MAIN_CLOCK_LFCLK,
- .divideRatio = DL_UART_MAIN_CLOCK_DIVIDE_RATIO_1
- };
- static const DL_UART_Main_Config gUART_0Config = {
- .mode = DL_UART_MAIN_MODE_NORMAL,
- .direction = DL_UART_MAIN_DIRECTION_TX_RX,
- .flowControl = DL_UART_MAIN_FLOW_CONTROL_NONE,
- .parity = DL_UART_MAIN_PARITY_NONE,
- .wordLength = DL_UART_MAIN_WORD_LENGTH_8_BITS,
- .stopBits = DL_UART_MAIN_STOP_BITS_ONE
- };
- SYSCONFIG_WEAK void SYSCFG_DL_UART_0_init(void)
- {
- DL_UART_Main_setClockConfig(UART_0_INST, (DL_UART_Main_ClockConfig *) &gUART_0ClockConfig);
- DL_UART_Main_init(UART_0_INST, (DL_UART_Main_Config *) &gUART_0Config);
- /*
- * Configure baud rate by setting oversampling and baud rate divisors.
- * Target baud rate: 9600
- * Actual baud rate: 9576.04
- */
- DL_UART_Main_setOversampling(UART_0_INST, DL_UART_OVERSAMPLING_RATE_3X);
- DL_UART_Main_setBaudRateDivisor(UART_0_INST, UART_0_IBRD_33_kHZ_9600_BAUD, UART_0_FBRD_33_kHZ_9600_BAUD);
- /* Configure Interrupts */
- DL_UART_Main_enableInterrupt(UART_0_INST,
- DL_UART_MAIN_INTERRUPT_RX);
- DL_UART_Main_enable(UART_0_INST);
- }
xprintf移植
源码 https://gitee.com/eegithub/xprintf_demo功能裁剪:
- #define XF_USE_OUTPUT 1 /* 1: Enable output functions */
- #define XF_CRLF 0 /* 1: Convert \n ==> \r\n in the output char */
- #define XF_USE_DUMP 0 /* 1: Enable put_dump function */
- #define XF_USE_LLI 0 /* 1: Enable long long integer in size prefix ll */
- #define XF_USE_FP 0 /* 1: Enable support for floating point in type e and f */
- #define XF_DPC '.' /* Decimal separator for floating point */
- #define XF_USE_INPUT 0 /* 1: Enable input functions */
- #define XF_INPUT_ECHO 0 /* 1: Echo back input chars in xgets function */
格式:
- xprintf("%d", 1234); /* "1234" */
- xprintf("%6d,%3d%%", -200, 5); /* " -200, 5%" */
- xprintf("%-6u", 100); /* "100 " */
- xprintf("%ld", 12345678); /* "12345678" */
- xprintf("%llu", 0x100000000); /* "4294967296" <XF_USE_LLI> */
- xprintf("%lld", -1LL); /* "-1" <XF_USE_LLI> */
- xprintf("%04x", 0xA3); /* "00a3" */
- xprintf("%08lX", 0x123ABC); /* "00123ABC" */
- xprintf("%016b", 0x550F); /* "0101010100001111" */
- xprintf("%*d", 6, 100); /* "100" */
- xprintf("%s", "abcdefg"); /* "abcdefg" */
- xprintf("%5s", "abc"); /* " abc" */
- xprintf("%-5s", "abc"); /* "abc " */
- xprintf("%.5s", "abcdefg"); /* "abcde" */
- xprintf("%-5.2s", "abcdefg"); /* "ab" */
- xprintf("%c", 'a'); /* "a" */
- xprintf("%12f", 10.0); /* " 10.000000" <XF_USE_FP> */
- 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发送的函数即可
- void UART_0_write_Byte(uint8_t data)//1 byte 发送
- {
- DL_UART_transmitDataBlocking(UART_0_INST,data);
- }
- xdev_out(UART_0_write_Byte);
主函数
- #include "ti_msp_dl_config.h"
- #include "xprintf.h"
- uint8_t data = 0;
- const uint8_t str[]="HELLO,TI MCU SERIAL MSPM0L1306\n";
- void UART_0_write_str(uint8_t *s);
- void UART_0_write_Byte(uint8_t data);
- int main(void)
- {
- uint8_t i=0;
- SYSCFG_DL_init();
- NVIC_ClearPendingIRQ(UART_0_INST_INT_IRQN);
- NVIC_EnableIRQ(UART_0_INST_INT_IRQN);
- DL_SYSCTL_disableSleepOnExit();
- xdev_out(UART_0_write_Byte);
- while (1)
- {
- delay_cycles((32000*1000));
- UART_0_write_str(str);
- UART_0_write_str("string send\n");
- xprintf("xprintf :%d\n\r", i);
- i++;
- }
- }
- void UART_0_INST_IRQHandler(void)
- {
- switch (DL_UART_Main_getPendingInterrupt(UART_0_INST))
- {
- case DL_UART_MAIN_IIDX_RX:
- data = DL_UART_Main_receiveData(UART_0_INST);
- DL_UART_Main_transmitData(UART_0_INST, data);
- break;
- default:
- break;
- }
- }
- void UART_0_write_str(uint8_t *s)//字符串发送函数
- {
- uint8_t i=0;
- while(s[i]!='\0')
- {
- if(s[i]=='\r') DL_UART_transmitDataBlocking(UART_0_INST, '\n');
- DL_UART_transmitDataBlocking(UART_0_INST, s[i]);
- i++;
- }
- }
- void UART_0_write_Byte(uint8_t data)//1 byte 发送
- {
- DL_UART_transmitDataBlocking(UART_0_INST,data);
- }
4、实验现象
|