tigp tests
[soft/lpc122x/tigp] / tests / main.c
1 /****************************************************************************
2  *   oled/main.c
3  *
4  * Test of Oled display on tigp board
5  *
6  * Copyright 2017 Nathael Pajani <nathael.pajani@ed3l.fr>
7  *
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  *************************************************************************** */
26 #include "core/system.h"
27 #include "core/systick.h"
28 #include "core/pio.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"
37 #include "lib/font.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..
55  */
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 },
67         /* Buttons */
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 */
76         /* GPIO */
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 */
85         /* Debug */
86         /* FIXME : SWDIO and SWCLK on PIO0_25 and PIO0_26 */
87         /* ADC */
88         { LPC_ADC_AD0_PIO_0_30, LPC_IO_ANALOG },  /* ADC0 */
89         { LPC_ADC_AD1_PIO_0_31, LPC_IO_ANALOG },  /* ADC1 */
90         ARRAY_LAST_PIO,
91 };
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 */
102 void system_init()
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);
109         gpio_on();
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
112          * functions */
113         systick_timer_on(1); /* 1ms */
114         systick_start();
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.
121  */
122 void fault_info(const char* name, uint32_t len)
124         uprintf(UART0, name);
125         while (1);
129 /***************************************************************************** */
130 /* Oled Display */
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,
137         .contrast = 128,
138         .scan_dir = SSD130x_SCAN_BOTTOM_TOP,
139         .read_dir = SSD130x_RIGHT_TO_LEFT,
140         .display_offset_dir = SSD130x_MOVE_TOP,
141         .display_offset = 4,
142         .gddram = gddram,
143         .gpio_cs = LPC_GPIO_1_2,
144         .gpio_dc = LPC_GPIO_1_1,
145         .gpio_rst = LPC_GPIO_1_3,
146 };
148 #define ROW(x)   VERTICAL_REV(x)
149 DECLARE_FONT(font);
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);
160         int i = 0;
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)) {
167                         col = 0;
168                         line++;
169                         if (line >= SSD130x_NB_PAGES) {
170                                 return i;
171                         }
172                 }
173         }
174         return len;
177 /***************************************************************************** */
178 /* Communication over USB */
179 uint8_t text_received = 0;
180 #define NB_CHARS  15
181 char inbuff[NB_CHARS + 1];
182 void data_rx(uint8_t c)
184         static int idx = 0;
185         if ((c != 0) && (c != '\n') && (c != '\r')) {
186                 inbuff[idx++] = c;
187                 if (idx >= NB_CHARS) {
188                         inbuff[NB_CHARS] = 0;
189                         idx = 0;
190                         text_received = 1;
191                 }
192         } else {
193                 if (idx != 0) {
194                         inbuff[idx] = 0;
195                         text_received = 1;
196                 }
197                 idx = 0;
198         }
202 const struct pio sd_cs = LPC_GPIO_1_0;
203 /***************************************************************************** */
204 int main(void)
206         int ret = 0;
207         system_init();
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);
227         while (1) {
228                 chenillard(500);
229                 if (text_received != 0) {
230                         display_line(2, 1, inbuff);
231                         ret = ssd130x_display_full_screen(&display);
232                         if (ret != 0) {
233                                 uprintf(UART0, "Display update error: %d\n", ret);
234                         } else {
235                                 uprintf(UART0, "Display updated.\n");
236                         }
237                         text_received = 0;
238                 }
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);
244         }
245         return 0;