Showing posts with label CC1352. Show all posts
Showing posts with label CC1352. Show all posts

Thursday, August 10, 2023

LPSTK-CC1352R/HDC2080 measurement

Use the following code relacing mainThread in i2copt3001_cpp.cpp of i2copt3001_cpp example to read LPSTK-CC1352R/HDC2080 temperature/humidity measurement

/*
 *  ======== mainThread ========
 */
uint8_t reg_addr, high_id, low_id, drdy, high_temp, low_temp, high_humi, low_humi;
uint16_t temp16, humi16;
float temperature, humidity;
uint8_t drdy_int_conf[2];

void *mainThread(void *arg0)
{
    uint16_t sample;
    int32_t luxValue;
    uint8_t data;
    uint8_t addr;

    /* I2C structures*/
    I2C_Handle      i2c;
    I2C_Params      i2cParams;
    I2C_Transaction i2cTransaction;

    /* Call driver init functions */
    Display_init();
    GPIO_init();
    I2C_init();

    /* Open the UART display for output */
    display = Display_open(Display_Type_UART, NULL);
    if (display == NULL) {
        while (1);
    }

    /* Create I2C for usage */
    I2C_Params_init(&i2cParams);
    i2cParams.bitRate = I2C_400kHz;
    i2c = I2C_open(CONFIG_I2C_OPT3001, &i2cParams);
    if (i2c == NULL) {
        Display_printf(display, 0, 0, (char *)"Error Initializing I2C!\n");
        while (1);
    }
    else {
        Display_printf(display, 0, 0, (char *)"I2C Initialized!");
    }

    i2cTransaction.targetAddress=0x41; //HDC2080 device address

    /* Read Device ID High byte */
    reg_addr=0xFF;
    i2cTransaction.writeBuf = &reg_addr;
    i2cTransaction.writeCount = 1;
    i2cTransaction.readBuf = &high_id;
    i2cTransaction.readCount = 1;
    if (I2C_transfer(i2c, &i2cTransaction)){
        Display_printf(display, 0, 0,
                        (char *)"I2C device High ID 0x%x!", high_id);
    }

    /* Read Device ID Low byte */
    reg_addr=0xFE;
    i2cTransaction.writeBuf = &reg_addr;
    i2cTransaction.writeCount = 1;
    i2cTransaction.readBuf = &low_id;
    i2cTransaction.readCount = 1;
    if (I2C_transfer(i2c, &i2cTransaction)){
        Display_printf(display, 0, 0,
                        (char *)"I2C device Low ID 0x%x!", low_id);
    }

    while(1)
    {
        //Write drdy_int_conf to 5 samples per second
        drdy_int_conf[0]=0x0F;
        drdy_int_conf[1]=0x01;
        i2cTransaction.writeBuf = drdy_int_conf;
        i2cTransaction.writeCount = 2;
        i2cTransaction.readBuf = &drdy;
        i2cTransaction.readCount = 0;
        if (I2C_transfer(i2c, &i2cTransaction)){
            Display_printf(display, 0, 0,
                            (char *)"Data ready byte? 0x%x", drdy);
        }

        //read if data is ready
        reg_addr=0x04;
        i2cTransaction.writeBuf = &reg_addr;
        i2cTransaction.writeCount = 1;
        i2cTransaction.readBuf = &drdy;
        i2cTransaction.readCount = 1;
        if (I2C_transfer(i2c, &i2cTransaction)){
            Display_printf(display, 0, 0,
                            (char *)"Data ready byte? 0x%x", drdy);
        }

        if(drdy&0x80)
        {
            //Data is ready and start to read temperature and temperature data
            /* Read Temperature Low byte */
            reg_addr=0x00;
            i2cTransaction.writeBuf = &reg_addr;
            i2cTransaction.writeCount = 1;
            i2cTransaction.readBuf = &low_temp;
            i2cTransaction.readCount = 1;
            if (I2C_transfer(i2c, &i2cTransaction)){
                Display_printf(display, 0, 0,
                                (char *)"Temperature Low byte 0x%x!", low_temp);
            }

            /* Read Temperature High byte */
            reg_addr=0x01;
            i2cTransaction.writeBuf = &reg_addr;
            i2cTransaction.writeCount = 1;
            i2cTransaction.readBuf = &high_temp;
            i2cTransaction.readCount = 1;
            if (I2C_transfer(i2c, &i2cTransaction)){
                Display_printf(display, 0, 0,
                                (char *)"Temperature high byte 0x%x!", high_temp);
            }

            temp16=(uint16_t)high_temp;
            temp16=(temp16<<8)|((uint16_t)low_temp);
            temperature = (((float)temp16)/65535)*165-40.5;
            Display_printf(display, 0, 0,(char *)"Temperature: %.1f", temperature);

            /* Read Humidity Low byte */
            reg_addr=0x02;
            i2cTransaction.writeBuf = &reg_addr;
            i2cTransaction.writeCount = 1;
            i2cTransaction.readBuf = &low_humi;
            i2cTransaction.readCount = 1;
            if (I2C_transfer(i2c, &i2cTransaction)){
                Display_printf(display, 0, 0,
                                (char *)"Temperature Low byte 0x%x.1!", low_humi);
            }

            /* Read Humidity High byte */
            reg_addr=0x03;
            i2cTransaction.writeBuf = &reg_addr;
            i2cTransaction.writeCount = 1;
            i2cTransaction.readBuf = &high_humi;
            i2cTransaction.readCount = 1;
            if (I2C_transfer(i2c, &i2cTransaction)){
                Display_printf(display, 0, 0,
                                (char *)"Temperature high byte 0x%x!", high_humi);
            }

            humi16=(uint16_t)high_humi;
            humi16=(humi16<<8)|((uint16_t)low_humi);
            humidity = (((float)humi16)/65535)*100;
            Display_printf(display, 0, 0,(char *)"Humidity: %f", humidity);
        }
    }
    return (NULL);
}
 

Tuesday, June 16, 2020

How to use two UARTs in CC26x2 and CC13x2.

The following steps show you how to enable second UART in CC26x2 and CC13x2 using uartecho example.

1. Add second UART CONFIG_UART_1 from syscfg with the following steps



2. Add the following red codes to use CONFIG_UART_1.

void *mainThread(void *arg0)
{
    char        input;
    const char  echoPrompt[] = "Echoing characters:\r\n";
    const char  echo1Prompt[] = "UART 2 Echoing characters:\r\n";
    UART_Handle uart;
    UART_Params uartParams;
    UART_Handle uart1;
    UART_Params uart1Params;


    /* Call driver init functions */
    GPIO_init();
    UART_init();

    /* Configure the LED pin */
    GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);

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

    /* Create a UART with data processing off. */
    UART_Params_init(&uartParams);
    uartParams.writeDataMode = UART_DATA_BINARY;
    uartParams.readDataMode = UART_DATA_BINARY;
    uartParams.readReturnMode = UART_RETURN_FULL;
    uartParams.baudRate = 115200;

    uart = UART_open(CONFIG_UART_0, &uartParams);

    if (uart == NULL) {
        /* UART_open() failed */
        while (1);
    }

    /* Create second UART with data processing off. */
    UART_Params_init(&uart1Params);
    uart1Params.writeDataMode = UART_DATA_BINARY;
    uart1Params.readDataMode = UART_DATA_BINARY;
    uart1Params.readReturnMode = UART_RETURN_FULL;
    uart1Params.baudRate = 115200;

    uart1 = UART_open(CONFIG_UART_1, &uart1Params);

    if (uart1 == NULL) {
        /* UART_open() failed */
        while (1);
    }

    UART_write(uart, echoPrompt, sizeof(echoPrompt));
    UART_write(uart1, echo1Prompt, sizeof(echo1Prompt));

    /* Loop forever echoing */
    while (1) {
        if(UART_read(uart, &input, 1))
            UART_write(uart, &input, 1);
    }

3. Build and run uartecho and now you can see "UART 2 Echoing characters:" output from the seconds UART TX pin.