Add Winbond W25Q512J support (#14036)
[betaflight.git] / src / main / io / displayport_max7456.c
blob3e937a14ef7e22d404ac4644089a6fa2ba6dd8dc
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #include <stdbool.h>
22 #include <stdint.h>
24 #include "platform.h"
26 #ifdef USE_MAX7456
28 #include "common/utils.h"
30 #include "drivers/display.h"
31 #include "drivers/max7456.h"
32 #include "drivers/osd.h"
34 #include "config/config.h"
36 #include "fc/runtime_config.h"
38 #include "io/displayport_max7456.h"
40 #include "osd/osd.h"
42 #include "pg/displayport_profiles.h"
43 #include "pg/max7456.h"
44 #include "pg/vcd.h"
46 static displayPort_t max7456DisplayPort;
47 static vcdProfile_t const *max7456VcdProfile;
49 static int grab(displayPort_t *displayPort)
51 UNUSED(displayPort);
53 return 0;
56 static int release(displayPort_t *displayPort)
58 UNUSED(displayPort);
60 return 0;
63 static int clearScreen(displayPort_t *displayPort, displayClearOption_e options)
65 UNUSED(displayPort);
66 UNUSED(options);
68 max7456Invert(displayPortProfileMax7456()->invert);
69 max7456Brightness(displayPortProfileMax7456()->blackBrightness, displayPortProfileMax7456()->whiteBrightness);
71 max7456ClearScreen();
73 return 0;
76 // Return true if screen still being transferred
77 static bool drawScreen(displayPort_t *displayPort)
79 UNUSED(displayPort);
80 return max7456DrawScreen();
83 static int screenSize(const displayPort_t *displayPort)
85 UNUSED(displayPort);
86 return maxScreenSize;
89 static int writeString(displayPort_t *displayPort, uint8_t x, uint8_t y, uint8_t attr, const char *text)
91 UNUSED(displayPort);
92 UNUSED(attr);
94 max7456Write(x, y, text);
96 return 0;
99 static int writeChar(displayPort_t *displayPort, uint8_t x, uint8_t y, uint8_t attr, uint8_t c)
101 UNUSED(displayPort);
102 UNUSED(attr);
104 max7456WriteChar(x, y, c);
106 return 0;
109 static bool isTransferInProgress(const displayPort_t *displayPort)
111 UNUSED(displayPort);
112 return max7456DmaInProgress();
115 static bool isSynced(const displayPort_t *displayPort)
117 UNUSED(displayPort);
118 return max7456BuffersSynced();
121 static void redraw(displayPort_t *displayPort)
123 UNUSED(displayPort);
125 if (!ARMING_FLAG(ARMED)) {
126 max7456RefreshAll();
130 static int heartbeat(displayPort_t *displayPort)
132 UNUSED(displayPort);
134 // (Re)Initialize MAX7456 at startup or stall is detected.
135 return max7456ReInitIfRequired(false);
138 static uint32_t txBytesFree(const displayPort_t *displayPort)
140 UNUSED(displayPort);
141 return UINT32_MAX;
144 static bool layerSupported(displayPort_t *displayPort, displayPortLayer_e layer)
146 UNUSED(displayPort);
147 return max7456LayerSupported(layer);
150 static bool layerSelect(displayPort_t *displayPort, displayPortLayer_e layer)
152 UNUSED(displayPort);
153 return max7456LayerSelect(layer);
156 static bool layerCopy(displayPort_t *displayPort, displayPortLayer_e destLayer, displayPortLayer_e sourceLayer)
158 UNUSED(displayPort);
159 return max7456LayerCopy(destLayer, sourceLayer);
162 static bool writeFontCharacter(displayPort_t *displayPort, uint16_t addr, const osdCharacter_t *chr)
164 UNUSED(displayPort);
166 return max7456WriteNvm(addr, (const uint8_t *)chr);
169 static bool checkReady(displayPort_t *displayPort, bool rescan)
171 UNUSED(displayPort);
172 if (!max7456IsDeviceDetected()) {
173 if (!rescan) {
174 return false;
175 } else {
176 // Try to initialize the device
177 if (max7456Init(max7456Config(), max7456VcdProfile, systemConfig()->cpu_overclock) != MAX7456_INIT_OK) {
178 return false;
180 // At this point the device has been initialized and detected
181 redraw(&max7456DisplayPort);
185 return true;
188 void setBackgroundType(displayPort_t *displayPort, displayPortBackground_e backgroundType)
190 UNUSED(displayPort);
191 max7456SetBackgroundType(backgroundType);
194 static const displayPortVTable_t max7456VTable = {
195 .grab = grab,
196 .release = release,
197 .clearScreen = clearScreen,
198 .drawScreen = drawScreen,
199 .screenSize = screenSize,
200 .writeString = writeString,
201 .writeChar = writeChar,
202 .isTransferInProgress = isTransferInProgress,
203 .heartbeat = heartbeat,
204 .redraw = redraw,
205 .isSynced = isSynced,
206 .txBytesFree = txBytesFree,
207 .layerSupported = layerSupported,
208 .layerSelect = layerSelect,
209 .layerCopy = layerCopy,
210 .writeFontCharacter = writeFontCharacter,
211 .checkReady = checkReady,
212 .setBackgroundType = setBackgroundType,
215 bool max7456DisplayPortInit(const vcdProfile_t *vcdProfile, displayPort_t **displayPort)
217 max7456VcdProfile = vcdProfile;
219 switch (max7456Init(max7456Config(), max7456VcdProfile, systemConfig()->cpu_overclock)) {
220 case MAX7456_INIT_NOT_CONFIGURED:
221 // MAX7456 IO pins are not defined. We either don't have
222 // it on board or either the configuration for it has
223 // not been set.
224 *displayPort = NULL;
226 return false;
228 break;
229 case MAX7456_INIT_NOT_FOUND:
230 // MAX7456 IO pins are defined, but we could not get a reply
231 // from it at this time. Delay full initialization to
232 // checkReady() with 'rescan' enabled
233 displayInit(&max7456DisplayPort, &max7456VTable, DISPLAYPORT_DEVICE_TYPE_MAX7456);
234 *displayPort = &max7456DisplayPort;
236 return false;
238 break;
239 case MAX7456_INIT_OK:
240 // MAX7456 configured and detected
241 displayInit(&max7456DisplayPort, &max7456VTable, DISPLAYPORT_DEVICE_TYPE_MAX7456);
242 *displayPort = &max7456DisplayPort;
244 break;
247 uint8_t displayRows;
249 switch(vcdProfile->video_system) {
250 default:
251 case VIDEO_SYSTEM_PAL:
252 displayRows = VIDEO_LINES_PAL;
253 break;
255 case VIDEO_SYSTEM_NTSC:
256 displayRows = VIDEO_LINES_NTSC;
257 break;
259 case VIDEO_SYSTEM_AUTO:
260 displayRows = max7456GetRowsCount();
261 break;
264 max7456DisplayPort.rows = displayRows + displayPortProfileMax7456()->rowAdjust;
265 max7456DisplayPort.cols = 30 + displayPortProfileMax7456()->colAdjust;
267 return true;
269 #endif // USE_MAX7456