1 /****************************************************************************
4 * Test of Oled display on tigp board
6 * Copyright 2017 Nathael Pajani <nathael.pajani@ed3l.fr>
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 *************************************************************************** */
26 #include "core/system.h"
27 #include "core/systick.h"
29 #include "lib/stdio.h"
30 #include "drivers/serial.h"
31 #include "drivers/gpio.h"
32 #include "drivers/ssp.h"
34 #include "extdrv/status_led.h"
35 #include "extdrv/ssd130x_oled_driver.h"
36 #include "extdrv/ssd130x_oled_buffer.h"
39 #include "extdrv/ws2812.h"
42 #define MODULE_VERSION 0x01
43 #define MODULE_NAME "TIGP"
45 #define TEST_VERSION "03"
47 #define SELECTED_FREQ FREQ_SEL_48MHz
50 /***************************************************************************** */
51 /* Pins configuration */
52 /* pins blocks are passed to set_pins() for pins configuration.
53 * Unused pin blocks can be removed safely with the corresponding set_pins() call
54 * All pins blocks may be safelly merged in a single block for single set_pins() call..
56 const struct pio_config common_pins[] = {
57 /* UART 0 : Config / Debug / USB */
58 { LPC_UART0_RX_PIO_0_1, LPC_IO_DIGITAL },
59 { LPC_UART0_TX_PIO_0_2, LPC_IO_DIGITAL },
60 /* I2C : RTC, Temp sensor, Accelerometer */
61 { LPC_I2C0_SCL_PIO_0_10, (LPC_IO_DIGITAL | LPC_IO_OPEN_DRAIN_ENABLE) },
62 { LPC_I2C0_SDA_PIO_0_11, (LPC_IO_DIGITAL | LPC_IO_OPEN_DRAIN_ENABLE) },
63 /* SPI : Display and uSD card */
64 { LPC_SSP0_SCLK_PIO_0_14, LPC_IO_DIGITAL },
65 { LPC_SSP0_MISO_PIO_0_16, LPC_IO_DIGITAL },
66 { LPC_SSP0_MOSI_PIO_0_17, LPC_IO_DIGITAL },
68 { LPC_GPIO_0_3, LPC_IO_DIGITAL }, /* B1 */
69 { LPC_GPIO_0_4, LPC_IO_DIGITAL }, /* B2 */
70 { LPC_GPIO_0_5, LPC_IO_DIGITAL }, /* B3 */
71 { LPC_GPIO_0_6, LPC_IO_DIGITAL }, /* B4 */
72 { LPC_GPIO_0_7, LPC_IO_DIGITAL }, /* B6 */
73 { LPC_GPIO_0_8, LPC_IO_DIGITAL }, /* B7 */
74 { LPC_GPIO_0_9, LPC_IO_DIGITAL }, /* B8 */
75 { LPC_GPIO_0_12, LPC_IO_DIGITAL }, /* ISP / B5 */
77 { LPC_GPIO_0_0, LPC_IO_DIGITAL }, /* Clkout / interrupt from RTC */
78 { LPC_GPIO_0_15, LPC_IO_DIGITAL }, /* Buzer */
79 { LPC_GPIO_0_18, LPC_IO_DIGITAL }, /* WS2812B Leds */
80 { LPC_GPIO_0_29, LPC_IO_DIGITAL }, /* Charging state */
81 { LPC_GPIO_1_0, LPC_IO_DIGITAL }, /* SD Card Chip select */
82 { LPC_GPIO_1_1, LPC_IO_DIGITAL }, /* Oled Ctrl */
83 { LPC_GPIO_1_2, LPC_IO_DIGITAL }, /* Oled Chip select */
84 { LPC_GPIO_1_3, LPC_IO_DIGITAL }, /* Oled Reset */
86 /* FIXME : SWDIO and SWCLK on PIO0_25 and PIO0_26 */
88 { LPC_ADC_AD0_PIO_0_30, LPC_IO_ANALOG }, /* ADC0 */
89 { LPC_ADC_AD1_PIO_0_31, LPC_IO_ANALOG }, /* ADC1 */
93 const struct pio status_led_green = LPC_GPIO_0_27;
94 const struct pio status_led_red = LPC_GPIO_0_28;
96 const struct pio ws2812_data_out_pin = LPC_GPIO_0_18; /* Led control data pin */
99 /***************************************************************************** */
100 /* Basic system init and configuration */
104 /* Stop the Watchdog */
105 startup_watchdog_disable(); /* Do it right now, before it gets a chance to break in */
106 system_set_default_power_state();
107 clock_config(SELECTED_FREQ);
108 set_pins(common_pins);
110 status_led_config(&status_led_green, &status_led_red);
111 /* System tick timer MUST be configured and running in order to use the sleeping
113 systick_timer_on(1); /* 1ms */
117 /* Define our fault handler. This one is not mandatory, the dummy fault handler
118 * will be used when it's not overridden here.
119 * Note : The default one does a simple infinite loop. If the watchdog is deactivated
120 * the system will hang.
122 void fault_info(const char* name, uint32_t len)
124 uprintf(UART0, name);
129 /***************************************************************************** */
131 static uint8_t gddram[ 4 + GDDRAM_SIZE ];
132 struct oled_display display = {
133 .bus_type = SSD130x_BUS_SPI,
134 .bus_num = SSP_BUS_0,
135 .charge_pump = SSD130x_INTERNAL_PUMP,
136 .video_mode = SSD130x_DISP_NORMAL,
138 .scan_dir = SSD130x_SCAN_BOTTOM_TOP,
139 .read_dir = SSD130x_RIGHT_TO_LEFT,
140 .display_offset_dir = SSD130x_MOVE_TOP,
143 .gpio_cs = LPC_GPIO_1_2,
144 .gpio_dc = LPC_GPIO_1_1,
145 .gpio_rst = LPC_GPIO_1_3,
148 #define ROW(x) VERTICAL_REV(x)
151 void display_char(uint8_t line, uint8_t col, uint8_t c)
153 uint8_t tile = (c > FIRST_FONT_CHAR) ? (c - FIRST_FONT_CHAR) : 0;
154 uint8_t* tile_data = (uint8_t*)(&font[tile]);
155 ssd130x_buffer_set_tile(gddram, col, line, tile_data);
157 int display_line(uint8_t line, uint8_t col, char* text)
159 int len = strlen((char*)text);
162 for (i = 0; i < len; i++) {
163 uint8_t tile = (text[i] > FIRST_FONT_CHAR) ? (text[i] - FIRST_FONT_CHAR) : 0;
164 uint8_t* tile_data = (uint8_t*)(&font[tile]);
165 ssd130x_buffer_set_tile(gddram, col++, line, tile_data);
166 if (col >= (SSD130x_NB_COL / 8)) {
169 if (line >= SSD130x_NB_PAGES) {
177 /***************************************************************************** */
178 /* Communication over USB */
179 uint8_t text_received = 0;
181 char inbuff[NB_CHARS + 1];
182 void data_rx(uint8_t c)
185 if ((c != 0) && (c != '\n') && (c != '\r')) {
187 if (idx >= NB_CHARS) {
188 inbuff[NB_CHARS] = 0;
202 const struct pio sd_cs = LPC_GPIO_1_0;
203 /***************************************************************************** */
209 uart_on(UART0, 115200, data_rx);
210 ssp_master_on(SSP_BUS_0, LPC_SSP_FRAME_SPI, 8, 4*1000*1000);
212 status_led(green_only);
214 config_gpio(&sd_cs, 0, GPIO_DIR_OUT, 1);
216 /* Configure and start display */
217 ret = ssd130x_display_on(&display);
218 uprintf(UART0, "display on : %d\n", ret);
219 /* Erase screen with lines, makes it easier to know things are going right */
220 ssd130x_buffer_set(gddram, 0x10);
221 ret = ssd130x_display_full_screen(&display);
222 uprintf(UART0, "full screen : %d\n", ret);
224 /* Led strip configuration */
225 ws2812_config(&ws2812_data_out_pin);
229 if (text_received != 0) {
230 display_line(2, 1, inbuff);
231 ret = ssd130x_display_full_screen(&display);
233 uprintf(UART0, "Display update error: %d\n", ret);
235 uprintf(UART0, "Display updated.\n");
239 ws2812_set_pixel(0, 10, 0, 10);
240 ws2812_set_pixel(1, 15, 5, 5);
241 ws2812_send_frame(0);
242 display_line(1, 0, TEST_VERSION);
243 ret = ssd130x_display_full_screen(&display);