Created
January 29, 2018 02:34
-
-
Save spirilis/3fec64b403f20327fa4e05a08423f29e to your computer and use it in GitHub Desktop.
MSP432 LaunchPad demo'ing TI logo on I2C OLED display
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2015-2017, 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 ======== | |
*/ | |
/* For usleep() */ | |
#include <unistd.h> | |
#include <stdint.h> | |
#include <stddef.h> | |
/* XDC module Headers */ | |
#include <xdc/std.h> | |
#include <xdc/runtime/System.h> | |
#include <xdc/runtime/Memory.h> | |
#include <xdc/runtime/Error.h> | |
/* TI-RTOS kernel */ | |
#include <ti/sysbios/BIOS.h> | |
#include <ti/sysbios/knl/Clock.h> | |
#include <ti/sysbios/knl/Task.h> | |
/* Driver Header files */ | |
#include <ti/drivers/GPIO.h> | |
#include <ti/drivers/I2C.h> | |
// #include <ti/drivers/SDSPI.h> | |
// #include <ti/drivers/SPI.h> | |
// #include <ti/drivers/UART.h> | |
// #include <ti/drivers/Watchdog.h> | |
/* Board Header file */ | |
#include "Board.h" | |
/* | |
* ======== mainThread ======== | |
*/ | |
#define SSD1306_OLED_SLAVE_ADDRESS 0x3C // 011110<0><R/W#> | |
Bool ssd1306_command(I2C_Handle i2c, UInt8 cmd); | |
Bool ssd1306_writedata(I2C_Handle i2c, Void * data, size_t count); | |
Void ssd1306_clearScreen(I2C_Handle i2c); | |
void *mainThread(void *arg0) | |
{ | |
/* Call driver init functions */ | |
GPIO_init(); | |
I2C_init(); | |
// SDSPI_init(); | |
// SPI_init(); | |
// UART_init(); | |
// Watchdog_init(); | |
I2C_Params iP; | |
I2C_Handle i2c; | |
I2C_Params_init(&iP); | |
iP.bitRate = I2C_400kHz; | |
iP.transferMode = I2C_MODE_BLOCKING; | |
i2c = I2C_open(Board_I2C1, &iP); | |
if (i2c == NULL) { | |
System_abort("I2C_open failed\n"); | |
} | |
// Wait 10ms | |
Task_sleep(10000 / Clock_tickPeriod); | |
System_printf("Beginning OLED init-\n"); System_flush(); | |
// Initialize SSD1306 | |
UInt8 ssd1306_init0_sequence[] = { | |
0xAE, | |
0xD5, | |
0x80, | |
0xA8, | |
63, | |
0xD3, | |
0x00, | |
0x40, | |
0x8D, | |
0x14, | |
0x20, | |
0x02, // PAGE ADDRESSING MODE | |
0xA1, | |
0xC8, | |
0xDA, | |
0x12, | |
0x81, | |
0xCF, | |
0xD9, | |
0xF1, | |
0xDB, | |
0x40, | |
0xA4, | |
0xA6 | |
}; | |
Int i; | |
for (i=0; i < sizeof(ssd1306_init0_sequence); i++) { | |
ssd1306_command(i2c, ssd1306_init0_sequence[i]); | |
} | |
ssd1306_clearScreen(i2c); | |
ssd1306_command(i2c, 0xAF); // DISPLAYON | |
System_printf("done\n"); System_flush(); | |
Task_sleep(250000 / Clock_tickPeriod); | |
// Set page address = 0 | |
System_printf("Writing logo-\n"); System_flush(); | |
UInt8 txn_logo[] = {0x40, 0x08, 0x18, 0x38, 0x3F, 0x1F, 0x3F, 0x44, 0xF6, 0x3C, 0x1C, 0x18, 0x00}; | |
for (i=0; i < 8; i++) { | |
ssd1306_command(i2c, 0xB0 | i); | |
ssd1306_command(i2c, 0x00); | |
ssd1306_command(i2c, 0x10); | |
ssd1306_writedata(i2c, txn_logo, 12); | |
} | |
System_printf("done\n"); System_flush(); | |
while (1) { | |
GPIO_write(Board_GPIO_LED1, !GPIO_read(Board_GPIO_LED1)); | |
Task_sleep(250000 / Clock_tickPeriod); | |
} | |
} | |
Bool ssd1306_command(I2C_Handle i2c, UInt8 cmd) | |
{ | |
UInt8 buf[2]; | |
buf[0] = 0; | |
buf[1] = cmd; | |
return ssd1306_writedata(i2c, &buf[0], 2); | |
} | |
Bool ssd1306_writedata(I2C_Handle i2c, Void * data, size_t count) // Needs to include a 0x40 at the very beginning | |
{ | |
I2C_Transaction txn; | |
txn.writeBuf = (void *)data; | |
txn.writeCount = count; | |
txn.readCount = 0; | |
txn.readBuf = NULL; | |
txn.slaveAddress = SSD1306_OLED_SLAVE_ADDRESS; | |
return I2C_transfer(i2c, &txn); | |
} | |
Void ssd1306_clearScreen(I2C_Handle i2c) | |
{ | |
UInt8 buf[129]; | |
Int i; | |
I2C_Transaction txn; | |
txn.writeBuf = (void *)buf; | |
txn.writeCount = 129; | |
txn.readCount = 0; | |
txn.readBuf = NULL; | |
txn.slaveAddress = SSD1306_OLED_SLAVE_ADDRESS; | |
buf[0] = 0x40; | |
for (i=1; i < 129; i++) { | |
buf[i] = 255; | |
} | |
for (i=0; i < 8; i++) { | |
ssd1306_command(i2c, 0xB0 | i); | |
ssd1306_command(i2c, 0x00); | |
ssd1306_command(i2c, 0x10); | |
I2C_transfer(i2c, &txn); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment