This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[参考译文] LAUNCHXL-CC1312R1:UART PRINT ON 按钮按压中断

Guru**** 2595800 points
Other Parts Discussed in Thread: CC1312R

请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/wireless-connectivity/sub-1-ghz-group/sub-1-ghz/f/sub-1-ghz-forum/1108186/launchxl-cc1312r1-uart-print-on-button-press-interrupt

器件型号:LAUNCHXL-CC1312R1
主题中讨论的其他器件:CC1312R

您好!

按下 launchpad 上的用户按钮时、我正在尝试在终端上打印字符串。 但代码不起作用。  

请帮助了解我在这里遗漏的内容。

#include <stdint.h>
#include <stddef.h>
#include <unistd.h>
#include <string.h>

/* Driver Header files */
#include <ti/drivers/GPIO.h>
#include <ti/drivers/UART2.h>

/* Driver configuration */
#include "ti_drivers_config.h"

UART2_Handle uart;
UART2_Params uartParams;

const char b[] = "Positive\r\n";
const char c[] = "Negative\r\n";

size_t bytes_written;

void gpioButtonIsr(uint_least8_t index)
{
    GPIO_disableInt(CONFIG_GPIO_BUTTON_0);
    GPIO_toggle(CONFIG_GPIO_LED_0);
    UART2_write(uart,b,sizeof(b),&bytes_written);
    GPIO_enableInt(CONFIG_GPIO_BUTTON_0);
}

/*
 *  ======== mainThread ========
 */

void *mainThread(void *arg0)
{
    const char welcome[] = "Welcome....\r\n";
    /* Call driver init functions */
    GPIO_init();
    UART2_Params_init(&uartParams);
    uartParams.readMode = UART2_Mode_CALLBACK;
    uartParams.writeMode = UART2_Mode_CALLBACK;
    uartParams.baudRate = 115200;

    uart = UART2_open(CONFIG_UART2_0,&uartParams);

    if(uart==NULL){while(1);}

    /* Configure the LED and button pins */
    GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
    GPIO_setConfig(CONFIG_GPIO_BUTTON_0, GPIO_CFG_IN_PU | GPIO_CFG_IN_INT_FALLING);
    GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);

    /* Turn on user LED */
    GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);

    /* Install Button callback */
    GPIO_setCallback(CONFIG_GPIO_BUTTON_0, gpioButtonIsr);
    GPIO_enableInt(CONFIG_GPIO_BUTTON_0);

    UART2_write(uart,welcome,sizeof(welcome),&bytes_written);

    return(NULL);
}

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    我认为 UART2没有打开。 不过、我们非常感谢您的帮助。 谢谢

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    我从6_10 SDK 中获取了 uart2echo 示例、并对 uart2echo.c 文件进行了以下修改

    #include <stdint.h>
    #include <stddef.h>
    
    /* Driver Header files */
    #include <ti/drivers/GPIO.h>
    #include <ti/drivers/UART2.h>
    
    /* Driver configuration */
    #include "ti_drivers_config.h"
    
    #include <ti/devices/DeviceFamily.h>
    #include DeviceFamily_constructPath(driverlib/cpu.h)
    
    #include <semaphore.h>
    
    static sem_t buttonSemaphore;
    
    void buttonCallbackFunction(uint_least8_t index) {
    
        /* Simple debounce logic, only toggle if the button is still pushed (low) */
        CPUdelay((uint32_t)((48000000/3)*0.050f));
        if (!GPIO_read(index)) {
            sem_post(&buttonSemaphore);
        }
    }
    
    /*
     *  ======== mainThread ========
     */
    void *mainThread(void *arg0)
    {
        const char   uartMessage[] = "Writing on UART :-)\r\n";
        UART2_Handle uart;
        UART2_Params uartParams;
        size_t       bytesWritten = 0;
        int retc;
    
        /* Call driver init functions */
        GPIO_init();
    
        /* Configure the LED pin and button pin */
        GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
        GPIO_setConfig(CONFIG_GPIO_BTN1, GPIO_CFG_IN_PU | GPIO_CFG_IN_INT_FALLING);
    
        /* Install Button callback */
         GPIO_setCallback(CONFIG_GPIO_BTN1, buttonCallbackFunction);
    
         /* Enable interrupts */
         GPIO_enableInt(CONFIG_GPIO_BTN1);
    
         retc = sem_init(&buttonSemaphore, 0, 0);
         if (retc != 0) {
             while (1);
         }
    
        /* Create a UART where the default read and write mode is BLOCKING */
        UART2_Params_init(&uartParams);
        uartParams.baudRate = 115200;
    
        uart = UART2_open(CONFIG_UART2_0, &uartParams);
    
        if (uart == NULL) {
            /* UART2_open() failed */
            while (1);
        }
    
        /* Turn on user LED to indicate successful initialization */
        GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);
    
        while(1)
        {
            /* Wait for a button press */
            retc = sem_wait(&buttonSemaphore);
            if (retc == -1) {
                while (1);
            }
    
            UART2_write(uart, uartMessage, sizeof(uartMessage), &bytesWritten);
    
        }
    }
    

    在 CC1312R LP 上测试了代码并按预期工作。

    BR

    Siri