RTOS: OLED I2C TI-RTOS SSD1306 library

 

TI-RTOS SSD1306 library
In the previous article, we described all the necessary functions in one file, but now we have combined them into a library.

tirtos_ssd1306.zip

This library, originally created by Mr. Dmytro Hutsulenko, has been modified to work with TI-RTOS by myself.

Usage: 
Place the downloaded file in the same hierarchy as the C file.



/*SSD1306*/
#include "ssd1306_lib.h"
header should be read at the beginning.

If you want to update the display periodically in a thread, by opening I2C in the usual way and using the following example, we can display text on the OLED.

void *mainThread5(void *arg0)
{

    I2C_init();

    I2C_Params iP;
    I2C_Handle i2c;

    I2C_Params_init(&iP);
    iP.bitRate = I2C_400kHz;
    iP.transferMode = I2C_MODE_BLOCKING;
    i2c = I2C_open(CONFIG_I2C_0, &iP);

        // Initialize SSD1306
        ssd1306Init(i2c);
        fillDisplay (i2c,0x00);

        char OLED_buffer[32];
        while (1) {
            draw6x8Str(i2c,0, 0, "TI-RTOS", 1,0);
            draw6x8Str(i2c,0, 1, "/* MSP432E401Y */",1,0);
            draw6x8Str(i2c,0, 2, "/* Hello World*/",1,0);

            memset(OLED_buffer, 0x00, sizeof(OLED_buffer));
            sprintf(OLED_buffer,"V=%f",adcValue0Volt[1]);
            draw6x8Str(i2c,0, 3,OLED_buffer,1,0);
            Task_sleep(500 * (1000 / Clock_tickPeriod));
        }
}

In the example above, the adcValue0Volt[1] is updated twice a second to update the display. Although not visible in the picture, the value of V in the last line changes with time and is displayed.

As a precaution, when using sprintf, make sure that the task stack size is large. Otherwise, it will stop.


*************************************************
/*
 * Copyright (c) 2015-2019, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  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.
 *
 * *  Neither the name of Texas Instruments Incorporated 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 OWNER 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.
 */

/*
 *  ======== empty.c ========
 */

#include <unistd.h> /* For usleep() */
#include <stdint.h>
#include <stddef.h>
#include <string.h>/* to use memset and strcmp*/
#include <stdio.h> /* to use sprintf */
#include <stdlib.h>/* to use sprintf */

/*SSD1306*/
#include "ssd1306_lib.h"

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

// #include <ti/drivers/SPI.h>
#include <ti/drivers/UART.h>
 extern   UART_Handle uart;
 extern   UART_Params uartParams;
#include <ti/drivers/ADC.h>
// #include <ti/drivers/Watchdog.h>

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

#include <ti/sysbios/knl/Task.h> //to use task sleep Task.h is needed.
#include <ti/sysbios/knl/Clock.h> //of caurase, clock.h also.

#include <ti/sysbios/BIOS.h>

/* declaration of original functions*/
void gpioButtonFxn0(uint_least8_t index);
float getADC14(int channel);



/* ADC conversion result variables */
float adcValue0Volt[2];


/*
 *  ======== mainThread ========
 */
void *mainThread0(uint16_t arg0, UArg arg1)
{
    /* 1 second delay */
   // uint32_t time = 1000;

    /* Call driver init functions */
    GPIO_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);

    while (1) {
        GPIO_toggle(CONFIG_GPIO_LED_0);
        Task_sleep(((uint16_t)arg0)  * (1000 / Clock_tickPeriod));
    }
}

void *mainThread1(UArg arg0, UArg arg1)
{
    /* 1 second delay */
   // uint32_t time = 100; //msec

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

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

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

    while (1) {

        GPIO_toggle(CONFIG_GPIO_LED_1);
        Task_sleep(((UInt)arg0) * (1000 / Clock_tickPeriod));
    }
}

void *mainThread2(UArg arg0, UArg arg1)
{

    GPIO_init();

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

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

 //   GPIO_setCallback(CONFIG_GPIO_BTN_0, gpioButtonFxn0);
    GPIO_enableInt(CONFIG_GPIO_BTN_0);
    return 0;

}


void gpioButtonFxn0(uint_least8_t index)
{
    // Toggle the LED
    GPIO_toggle(CONFIG_GPIO_LED_2);
}

void *mainThread3(void *arg0)
{
    const char  echoPrompt[] = "Thread3:";
    char  UART_TX_buffer[32];

    while (1) {

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

        memset(UART_TX_buffer, 0x00, sizeof(UART_TX_buffer));
        sprintf(UART_TX_buffer,"V=%f\n",adcValue0Volt[1]);
        UART_write(uart, UART_TX_buffer, sizeof(UART_TX_buffer));
        Task_sleep(500 * (1000 / Clock_tickPeriod));
    }//end while
}


void *mainThread4(void *arg0)
{

    const char  echoPrompt[] = "Thread4:\n";

    while (1) {
        UART_write(uart, echoPrompt, sizeof(echoPrompt));

        adcValue0Volt[1]=getADC14(1);

        Task_sleep(500 * (1000 / Clock_tickPeriod));
    }
}

float getADC14(int channel)
{

    /* ADC conversion result variables */
    uint16_t adcValue;
    float adcValueVolt;
    ADC_init();
       ADC_Handle   adc; //adc handle
       ADC_Params   params; //adc parameters

       ADC_Params_init(&params); //initialization of parameters

       switch(channel){
       case 0:
           adc = ADC_open(CONFIG_ADC_0, &params); //open adc
           break;
       case 1:
           adc = ADC_open(CONFIG_ADC_1, &params); //open adc
           break;
       default:
           adc = ADC_open(CONFIG_ADC_0, &params); //open adc
       }
       int_fast16_t res; //for checking adc converting
       res=ADC_convert(adc, &adcValue); //second argument should set address.
                if (res == ADC_STATUS_SUCCESS) {
                adcValueVolt = (float)ADC_convertRawToMicroVolts(adc, adcValue)/1000000.0;
                }
       ADC_close(adc);
       return adcValueVolt;

}

void *mainThread5(void *arg0)
{

    I2C_init();

    I2C_Params iP;
    I2C_Handle i2c;

    I2C_Params_init(&iP);
    iP.bitRate = I2C_400kHz;
    iP.transferMode = I2C_MODE_BLOCKING;
    i2c = I2C_open(CONFIG_I2C_0, &iP);

        // Initialize SSD1306
        ssd1306Init(i2c);
        fillDisplay (i2c,0x00);

        char OLED_buffer[32];
        while (1) {
            draw6x8Str(i2c,0, 0, "TI-RTOS", 1,0);
            draw6x8Str(i2c,0, 1, "/* MSP432E401Y */",1,0);
            draw6x8Str(i2c,0, 2, "/* Hello World*/",1,0);

            memset(OLED_buffer, 0x00, sizeof(OLED_buffer));
            sprintf(OLED_buffer,"V=%f",adcValue0Volt[1]);
            draw6x8Str(i2c,0, 3,OLED_buffer,1,0);
            Task_sleep(500 * (1000 / Clock_tickPeriod));
        }
}



************************************
/*
 * Copyright (c) 2016-2020, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  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.
 *
 * *  Neither the name of Texas Instruments Incorporated 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 OWNER 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.
 */

/*
 *  ======== main_tirtos.c ========
 */
#include <stdint.h>

/* POSIX Header files */
#include <pthread.h>

/* RTOS header files */
#include <ti/sysbios/BIOS.h>

#include <ti/drivers/Board.h>
#include <ti/drivers/UART.h>
    UART_Handle uart;
    UART_Params uartParams;

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

extern void *mainThread0(uint16_t arg0, UArg arg1);
extern void *mainThread1(UArg arg0, UArg arg1);

/* Stack size in bytes */
#define THREADSTACKSIZE    1024

/*
 * The following (weak) function definition is needed in applications
 * that do *not* use the NDK TCP/IP stack:
 */
void __attribute__((weak)) NDK_hookInit(int32_t id) {}

/*
 *  ======== main ========
 */
int main(void)
{

    Board_init();

    UART_init();
    UART_Params_init(&uartParams);
    uartParams.writeDataMode = UART_DATA_BINARY;
    uartParams.readDataMode = UART_DATA_BINARY;
    uartParams.readReturnMode =UART_RETURN_NEWLINE; //UART_RETURN_NEWLINE;//UART_RETURN_FULL;
    uartParams.baudRate = 115200;
    //uartParams.writeMode=UART_MODE_BLOCKING;

    /* OPEN UART */
    uart = UART_open(CONFIG_UART_0, &uartParams);

    BIOS_start();

    return (0);
}

コメント

このブログの人気の投稿

Attiny85とAQM0802A(LCD)のI2C接続

ILI9341 240X320 Arduino

Attiny85 FuseRest