Subdirectory provided by Embedded Artists

Dependencies:   DM_FATFileSystem DM_HttpServer DM_USBHost EthernetInterface USBDevice mbed-rpc mbed-rtos mbed-src

Dependents:   lpc4088_displaymodule_hello_world_Sept_2018

Fork of DMSupport by Embedded Artists

Committer:
embeddedartists
Date:
Fri Dec 19 09:03:25 2014 +0100
Revision:
10:1ac4b213f0f7
Parent:
9:a33326afd686
Child:
13:2c60e28503f8
- Added support for executing code in SDRAM.
- Restructured the Display/Touch interfaces.
- Added loading of Display/Touch BIOS.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 0:6b68dac0d986 1 /*
embeddedartists 0:6b68dac0d986 2 * Copyright 2014 Embedded Artists AB
embeddedartists 0:6b68dac0d986 3 *
embeddedartists 0:6b68dac0d986 4 * Licensed under the Apache License, Version 2.0 (the "License");
embeddedartists 0:6b68dac0d986 5 * you may not use this file except in compliance with the License.
embeddedartists 0:6b68dac0d986 6 * You may obtain a copy of the License at
embeddedartists 0:6b68dac0d986 7 *
embeddedartists 0:6b68dac0d986 8 * http://www.apache.org/licenses/LICENSE-2.0
embeddedartists 0:6b68dac0d986 9 *
embeddedartists 0:6b68dac0d986 10 * Unless required by applicable law or agreed to in writing, software
embeddedartists 0:6b68dac0d986 11 * distributed under the License is distributed on an "AS IS" BASIS,
embeddedartists 0:6b68dac0d986 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
embeddedartists 0:6b68dac0d986 13 * See the License for the specific language governing permissions and
embeddedartists 0:6b68dac0d986 14 * limitations under the License.
embeddedartists 0:6b68dac0d986 15 */
embeddedartists 0:6b68dac0d986 16
embeddedartists 0:6b68dac0d986 17 #include "mbed.h"
embeddedartists 0:6b68dac0d986 18 #include "DMBoard.h"
embeddedartists 0:6b68dac0d986 19 #include "InternalEEPROM.h"
embeddedartists 10:1ac4b213f0f7 20 #include "bios.h"
embeddedartists 10:1ac4b213f0f7 21 #include "crc.h"
embeddedartists 0:6b68dac0d986 22
embeddedartists 10:1ac4b213f0f7 23 #if defined(DM_BOARD_USE_DISPLAY)
embeddedartists 10:1ac4b213f0f7 24 #include "BiosDisplayAndTouch.h"
embeddedartists 0:6b68dac0d986 25 #endif
embeddedartists 0:6b68dac0d986 26
embeddedartists 4:6fdcdf7aff8d 27 #if defined(DM_BOARD_ENABLE_MEASSURING_PINS)
embeddedartists 4:6fdcdf7aff8d 28 #include "meas.h"
embeddedartists 4:6fdcdf7aff8d 29 #endif
embeddedartists 4:6fdcdf7aff8d 30
embeddedartists 5:c77fdb6e3438 31
embeddedartists 5:c77fdb6e3438 32 /******************************************************************************
embeddedartists 5:c77fdb6e3438 33 * Configuration Compatibility Control
embeddedartists 5:c77fdb6e3438 34 *****************************************************************************/
embeddedartists 5:c77fdb6e3438 35
embeddedartists 5:c77fdb6e3438 36 #if defined(DM_BOARD_USE_USB_DEVICE) && defined(DM_BOARD_USE_USB_HOST)
embeddedartists 5:c77fdb6e3438 37 #error The hardware supports either USB Device or USB Host - not both at the same time
embeddedartists 5:c77fdb6e3438 38 #endif
embeddedartists 5:c77fdb6e3438 39
embeddedartists 10:1ac4b213f0f7 40 #if defined(DM_BOARD_USE_TOUCH) && !defined(DM_BOARD_USE_DISPLAY)
embeddedartists 10:1ac4b213f0f7 41 #error Cannot have touch controller without a display!
embeddedartists 10:1ac4b213f0f7 42 #endif
embeddedartists 10:1ac4b213f0f7 43
embeddedartists 0:6b68dac0d986 44 /******************************************************************************
embeddedartists 0:6b68dac0d986 45 * Defines and typedefs
embeddedartists 0:6b68dac0d986 46 *****************************************************************************/
embeddedartists 0:6b68dac0d986 47
embeddedartists 2:887c6b45e7fa 48 #if defined(DM_BOARD_DISABLE_STANDARD_PRINTF)
embeddedartists 2:887c6b45e7fa 49 class DevNull : public Stream {
embeddedartists 2:887c6b45e7fa 50
embeddedartists 2:887c6b45e7fa 51 public:
embeddedartists 2:887c6b45e7fa 52 DevNull(const char *name=NULL) : Stream(name) {}
embeddedartists 2:887c6b45e7fa 53
embeddedartists 2:887c6b45e7fa 54 protected:
embeddedartists 2:887c6b45e7fa 55 virtual int _getc() {return 0;}
embeddedartists 2:887c6b45e7fa 56 virtual int _putc(int c) {return c;}
embeddedartists 2:887c6b45e7fa 57 };
embeddedartists 2:887c6b45e7fa 58
embeddedartists 2:887c6b45e7fa 59 static DevNull null("null");
embeddedartists 2:887c6b45e7fa 60 #endif
embeddedartists 2:887c6b45e7fa 61
embeddedartists 0:6b68dac0d986 62 /******************************************************************************
embeddedartists 0:6b68dac0d986 63 * Local variables
embeddedartists 0:6b68dac0d986 64 *****************************************************************************/
embeddedartists 0:6b68dac0d986 65
embeddedartists 0:6b68dac0d986 66 /******************************************************************************
embeddedartists 0:6b68dac0d986 67 * Private Functions
embeddedartists 0:6b68dac0d986 68 *****************************************************************************/
embeddedartists 0:6b68dac0d986 69
embeddedartists 0:6b68dac0d986 70 DMBoard::DMBoard() :
embeddedartists 0:6b68dac0d986 71 _initialized(false),
embeddedartists 10:1ac4b213f0f7 72 _conf(NULL), _confSize(0),
embeddedartists 0:6b68dac0d986 73 #if defined(DM_BOARD_USE_MCI_FS)
embeddedartists 0:6b68dac0d986 74 _mcifs("mci", P4_16),
embeddedartists 0:6b68dac0d986 75 #endif
embeddedartists 0:6b68dac0d986 76 #if defined(DM_BOARD_USE_QSPI_FS)
embeddedartists 0:6b68dac0d986 77 _qspifs("qspi"),
embeddedartists 0:6b68dac0d986 78 #endif
embeddedartists 0:6b68dac0d986 79 _buzzer(P1_5),
embeddedartists 0:6b68dac0d986 80 _button(p23),
embeddedartists 0:6b68dac0d986 81 _led1(LED1),
embeddedartists 0:6b68dac0d986 82 _led2(LED2),
embeddedartists 0:6b68dac0d986 83 _led3(LED3),
embeddedartists 0:6b68dac0d986 84 _led4(LED4)
embeddedartists 0:6b68dac0d986 85 {
embeddedartists 0:6b68dac0d986 86 }
embeddedartists 0:6b68dac0d986 87
embeddedartists 0:6b68dac0d986 88 DMBoard::~DMBoard()
embeddedartists 0:6b68dac0d986 89 {
embeddedartists 10:1ac4b213f0f7 90 if (_conf != NULL) {
embeddedartists 10:1ac4b213f0f7 91 free(_conf);
embeddedartists 10:1ac4b213f0f7 92 _conf = NULL;
embeddedartists 10:1ac4b213f0f7 93 _confSize = 0;
embeddedartists 0:6b68dac0d986 94 }
embeddedartists 0:6b68dac0d986 95 }
embeddedartists 0:6b68dac0d986 96
embeddedartists 0:6b68dac0d986 97 DMBoard::BoardError DMBoard::readConfiguration()
embeddedartists 0:6b68dac0d986 98 {
embeddedartists 10:1ac4b213f0f7 99 if (_conf == NULL) {
embeddedartists 10:1ac4b213f0f7 100 InternalEEPROM mem;
embeddedartists 10:1ac4b213f0f7 101 mem.init();
embeddedartists 10:1ac4b213f0f7 102
embeddedartists 10:1ac4b213f0f7 103 _confSize = mem.memorySize();
embeddedartists 10:1ac4b213f0f7 104 _conf = (uint8_t*)malloc(_confSize);
embeddedartists 10:1ac4b213f0f7 105 if (_conf == NULL) {
embeddedartists 10:1ac4b213f0f7 106 _confSize = 0;
embeddedartists 10:1ac4b213f0f7 107 return MemoryError;
embeddedartists 10:1ac4b213f0f7 108 }
embeddedartists 10:1ac4b213f0f7 109 mem.read(0, 0, _conf, _confSize);
embeddedartists 10:1ac4b213f0f7 110 mem.powerDown();
embeddedartists 10:1ac4b213f0f7 111 }
embeddedartists 0:6b68dac0d986 112 #if 0
embeddedartists 0:6b68dac0d986 113 uint8_t* p = buff;
embeddedartists 2:887c6b45e7fa 114 _logger.printf("\n-------\nBEFORE:\n-------\n");
embeddedartists 0:6b68dac0d986 115 for (int i = 0; i < 63; i++) {
embeddedartists 0:6b68dac0d986 116 for (int j = 0; j < 4; j++) {
embeddedartists 2:887c6b45e7fa 117 _logger.printf("0x%04x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
embeddedartists 0:6b68dac0d986 118 i,
embeddedartists 0:6b68dac0d986 119 p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7],
embeddedartists 0:6b68dac0d986 120 p[8],p[9],p[10],p[11],p[12],p[13],p[14],p[15]);
embeddedartists 0:6b68dac0d986 121 p += 16;
embeddedartists 0:6b68dac0d986 122 }
embeddedartists 2:887c6b45e7fa 123 _logger.printf("\n");
embeddedartists 0:6b68dac0d986 124 }
embeddedartists 0:6b68dac0d986 125
embeddedartists 0:6b68dac0d986 126 // find first non-zero page and write to that
embeddedartists 0:6b68dac0d986 127 for (int page = 0; page < mem.numPages(); page++) {
embeddedartists 0:6b68dac0d986 128 bool zeroPage = true;
embeddedartists 0:6b68dac0d986 129 for (int i = 0; i < mem.pageSize(); i++) {
embeddedartists 0:6b68dac0d986 130 if (buff[i + page*mem.pageSize()] != 0) {
embeddedartists 0:6b68dac0d986 131 zeroPage = false;
embeddedartists 0:6b68dac0d986 132 break;
embeddedartists 0:6b68dac0d986 133 }
embeddedartists 0:6b68dac0d986 134 }
embeddedartists 0:6b68dac0d986 135 if (zeroPage) {
embeddedartists 2:887c6b45e7fa 136 _logger.printf("Will fill page 0x%04x (%d) with 0x00..0x3f\n", page, page);
embeddedartists 0:6b68dac0d986 137 p = buff;
embeddedartists 0:6b68dac0d986 138 for (int i = 0; i < mem.pageSize(); i++) {
embeddedartists 0:6b68dac0d986 139 *p++ = i;
embeddedartists 0:6b68dac0d986 140 }
embeddedartists 0:6b68dac0d986 141 mem.write(page, 0, buff, mem.pageSize());
embeddedartists 0:6b68dac0d986 142 memset(buff, 0, mem.memorySize());
embeddedartists 0:6b68dac0d986 143 mem.read(0, 0, buff, mem.memorySize());
embeddedartists 0:6b68dac0d986 144 p = buff;
embeddedartists 2:887c6b45e7fa 145 _logger.printf("\n-------\nAFTER:\n-------\n");
embeddedartists 0:6b68dac0d986 146 for (int i = 0; i < 63; i++) {
embeddedartists 0:6b68dac0d986 147 for (int j = 0; j < 4; j++) {
embeddedartists 2:887c6b45e7fa 148 _logger.printf("0x%04x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
embeddedartists 0:6b68dac0d986 149 i,
embeddedartists 0:6b68dac0d986 150 p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7],
embeddedartists 0:6b68dac0d986 151 p[8],p[9],p[10],p[11],p[12],p[13],p[14],p[15]);
embeddedartists 0:6b68dac0d986 152 p += 16;
embeddedartists 0:6b68dac0d986 153 }
embeddedartists 2:887c6b45e7fa 154 _logger.printf("\n");
embeddedartists 0:6b68dac0d986 155 }
embeddedartists 0:6b68dac0d986 156 break;
embeddedartists 0:6b68dac0d986 157 }
embeddedartists 0:6b68dac0d986 158 }
embeddedartists 0:6b68dac0d986 159 #endif
embeddedartists 0:6b68dac0d986 160 return Ok;
embeddedartists 0:6b68dac0d986 161 }
embeddedartists 0:6b68dac0d986 162
embeddedartists 0:6b68dac0d986 163 #if defined(DM_BOARD_USE_DISPLAY)
embeddedartists 0:6b68dac0d986 164 DMBoard::BoardError DMBoard::readDisplayConfiguration(uint8_t** data, uint32_t* size)
embeddedartists 0:6b68dac0d986 165 {
embeddedartists 10:1ac4b213f0f7 166 BoardError err = readConfiguration();
embeddedartists 10:1ac4b213f0f7 167 do {
embeddedartists 10:1ac4b213f0f7 168 if (err != Ok) {
embeddedartists 10:1ac4b213f0f7 169 break;
embeddedartists 10:1ac4b213f0f7 170 }
embeddedartists 10:1ac4b213f0f7 171
embeddedartists 10:1ac4b213f0f7 172 file_header_t* fh = (file_header_t*)_conf;
embeddedartists 10:1ac4b213f0f7 173 if (fh->magic != BIOS_MAGIC) {
embeddedartists 10:1ac4b213f0f7 174 err = BiosInvalidError;
embeddedartists 10:1ac4b213f0f7 175 break;
embeddedartists 10:1ac4b213f0f7 176 }
embeddedartists 10:1ac4b213f0f7 177
embeddedartists 10:1ac4b213f0f7 178 if (fh->version != BIOS_VER) {
embeddedartists 10:1ac4b213f0f7 179 err = BiosVersionError;
embeddedartists 10:1ac4b213f0f7 180 break;
embeddedartists 10:1ac4b213f0f7 181 }
embeddedartists 10:1ac4b213f0f7 182
embeddedartists 10:1ac4b213f0f7 183 if ((fh->headerSize + fh->size) > _confSize) {
embeddedartists 10:1ac4b213f0f7 184 err = BiosInvalidError;
embeddedartists 10:1ac4b213f0f7 185 break;
embeddedartists 10:1ac4b213f0f7 186 }
embeddedartists 10:1ac4b213f0f7 187
embeddedartists 10:1ac4b213f0f7 188 uint32_t crc = crc_Buffer((uint32_t*)(&_conf[fh->headerSize]), fh->size/4);
embeddedartists 10:1ac4b213f0f7 189 if (crc != fh->crc) {
embeddedartists 10:1ac4b213f0f7 190 err = BiosInvalidError;
embeddedartists 10:1ac4b213f0f7 191 break;
embeddedartists 10:1ac4b213f0f7 192 }
embeddedartists 10:1ac4b213f0f7 193
embeddedartists 10:1ac4b213f0f7 194 // Bios header has been verified and seems ok
embeddedartists 10:1ac4b213f0f7 195 *data = _conf;
embeddedartists 10:1ac4b213f0f7 196 *size = fh->headerSize + fh->size;
embeddedartists 10:1ac4b213f0f7 197 err = Ok;
embeddedartists 10:1ac4b213f0f7 198 } while (false);
embeddedartists 0:6b68dac0d986 199
embeddedartists 10:1ac4b213f0f7 200 return err;
embeddedartists 0:6b68dac0d986 201 }
embeddedartists 0:6b68dac0d986 202 #endif
embeddedartists 0:6b68dac0d986 203
embeddedartists 0:6b68dac0d986 204 /******************************************************************************
embeddedartists 0:6b68dac0d986 205 * Public Functions
embeddedartists 0:6b68dac0d986 206 *****************************************************************************/
embeddedartists 0:6b68dac0d986 207
embeddedartists 0:6b68dac0d986 208 DMBoard::BoardError DMBoard::init()
embeddedartists 0:6b68dac0d986 209 {
embeddedartists 0:6b68dac0d986 210 BoardError err = Ok;
embeddedartists 0:6b68dac0d986 211 if (!_initialized) {
embeddedartists 0:6b68dac0d986 212 do {
embeddedartists 0:6b68dac0d986 213 // Turn off the buzzer
embeddedartists 0:6b68dac0d986 214 _buzzer.period_ms(1);
embeddedartists 0:6b68dac0d986 215 _buzzer = 0;
embeddedartists 0:6b68dac0d986 216
embeddedartists 0:6b68dac0d986 217 // Make sure the button is configured correctly
embeddedartists 0:6b68dac0d986 218 _button.mode(PullUp);
embeddedartists 0:6b68dac0d986 219
embeddedartists 0:6b68dac0d986 220 // Turn off all LEDs
embeddedartists 0:6b68dac0d986 221 _led1 = 1;
embeddedartists 0:6b68dac0d986 222 _led2 = 1;
embeddedartists 0:6b68dac0d986 223 _led3 = 0;
embeddedartists 0:6b68dac0d986 224 _led4 = 0;
embeddedartists 10:1ac4b213f0f7 225
embeddedartists 2:887c6b45e7fa 226 // Make sure that the logger is always initialized even if
embeddedartists 2:887c6b45e7fa 227 // other initialization tasks fail
embeddedartists 2:887c6b45e7fa 228 _logger.init();
embeddedartists 4:6fdcdf7aff8d 229
embeddedartists 4:6fdcdf7aff8d 230 #if defined(DM_BOARD_ENABLE_MEASSURING_PINS)
embeddedartists 4:6fdcdf7aff8d 231 _INTERNAL_INIT_MEAS();
embeddedartists 4:6fdcdf7aff8d 232 #endif
embeddedartists 2:887c6b45e7fa 233
embeddedartists 2:887c6b45e7fa 234 #if defined(DM_BOARD_DISABLE_STANDARD_PRINTF)
embeddedartists 2:887c6b45e7fa 235 // Kill all ouput of calls to printf() so that there is no
embeddedartists 2:887c6b45e7fa 236 // simultaneous calls into the non-thread-safe standard libraries.
embeddedartists 2:887c6b45e7fa 237 // User code should use the RtosLogger anyway.
embeddedartists 2:887c6b45e7fa 238 freopen("/null", "w", stdout);
embeddedartists 2:887c6b45e7fa 239 #endif
embeddedartists 2:887c6b45e7fa 240
embeddedartists 3:2fa7755f2cef 241 #if defined(DM_BOARD_USE_QSPI) || defined(DM_BOARD_USE_QSPI_FS)
embeddedartists 0:6b68dac0d986 242 if (SPIFI::instance().init() != SPIFI::Ok) {
embeddedartists 0:6b68dac0d986 243 err = SpifiError;
embeddedartists 0:6b68dac0d986 244 break;
embeddedartists 0:6b68dac0d986 245 }
embeddedartists 0:6b68dac0d986 246 #endif
embeddedartists 0:6b68dac0d986 247
embeddedartists 0:6b68dac0d986 248 readConfiguration();
embeddedartists 0:6b68dac0d986 249
embeddedartists 0:6b68dac0d986 250 #if defined(DM_BOARD_USE_DISPLAY)
embeddedartists 10:1ac4b213f0f7 251 if (BiosDisplayAndTouch::instance().initDisplay() != Display::DisplayError_Ok) {
embeddedartists 0:6b68dac0d986 252 err = DisplayError;
embeddedartists 0:6b68dac0d986 253 break;
embeddedartists 0:6b68dac0d986 254 }
embeddedartists 0:6b68dac0d986 255 #endif
embeddedartists 0:6b68dac0d986 256
embeddedartists 0:6b68dac0d986 257 #if defined(DM_BOARD_USE_TOUCH)
embeddedartists 10:1ac4b213f0f7 258 if (BiosDisplayAndTouch::instance().initTouchController() != TouchPanel::TouchError_Ok) {
embeddedartists 10:1ac4b213f0f7 259 err = TouchError;
embeddedartists 10:1ac4b213f0f7 260 break;
embeddedartists 10:1ac4b213f0f7 261 }
embeddedartists 0:6b68dac0d986 262 #endif
embeddedartists 0:6b68dac0d986 263
embeddedartists 0:6b68dac0d986 264 _initialized = true;
embeddedartists 0:6b68dac0d986 265 } while(0);
embeddedartists 0:6b68dac0d986 266 }
embeddedartists 0:6b68dac0d986 267 return err;
embeddedartists 0:6b68dac0d986 268 }
embeddedartists 0:6b68dac0d986 269
embeddedartists 2:887c6b45e7fa 270 void DMBoard::setLED(Leds led, bool on)
embeddedartists 0:6b68dac0d986 271 {
embeddedartists 0:6b68dac0d986 272 switch(led) {
embeddedartists 2:887c6b45e7fa 273 case Led1:
embeddedartists 0:6b68dac0d986 274 _led1 = (on ? 0 : 1);
embeddedartists 0:6b68dac0d986 275 break;
embeddedartists 2:887c6b45e7fa 276 case Led2:
embeddedartists 0:6b68dac0d986 277 _led2 = (on ? 0 : 1);
embeddedartists 0:6b68dac0d986 278 break;
embeddedartists 2:887c6b45e7fa 279 case Led3:
embeddedartists 0:6b68dac0d986 280 _led3 = (on ? 1 : 0);
embeddedartists 0:6b68dac0d986 281 break;
embeddedartists 2:887c6b45e7fa 282 case Led4:
embeddedartists 0:6b68dac0d986 283 _led4 = (on ? 1 : 0);
embeddedartists 0:6b68dac0d986 284 break;
embeddedartists 0:6b68dac0d986 285 }
embeddedartists 0:6b68dac0d986 286 }
embeddedartists 0:6b68dac0d986 287
embeddedartists 5:c77fdb6e3438 288 void DMBoard::buzzer(int frequency, int duration_ms)
embeddedartists 0:6b68dac0d986 289 {
embeddedartists 5:c77fdb6e3438 290 if (frequency <= 0) {
embeddedartists 5:c77fdb6e3438 291 _buzzer = 0;
embeddedartists 5:c77fdb6e3438 292 } else {
embeddedartists 5:c77fdb6e3438 293 _buzzer.period_us(1000000/frequency);
embeddedartists 5:c77fdb6e3438 294 _buzzer = 0.5;
embeddedartists 0:6b68dac0d986 295 }
embeddedartists 5:c77fdb6e3438 296 if (duration_ms > 0) {
embeddedartists 5:c77fdb6e3438 297 Thread::wait(duration_ms);
embeddedartists 5:c77fdb6e3438 298 _buzzer = 0;
embeddedartists 5:c77fdb6e3438 299 }
embeddedartists 0:6b68dac0d986 300 }
embeddedartists 0:6b68dac0d986 301
embeddedartists 0:6b68dac0d986 302 bool DMBoard::buttonPressed()
embeddedartists 0:6b68dac0d986 303 {
embeddedartists 0:6b68dac0d986 304 return _button.read() == 0;
embeddedartists 0:6b68dac0d986 305 }
embeddedartists 10:1ac4b213f0f7 306
embeddedartists 10:1ac4b213f0f7 307 TouchPanel* DMBoard::touchPanel()
embeddedartists 10:1ac4b213f0f7 308 {
embeddedartists 10:1ac4b213f0f7 309 if (BiosDisplayAndTouch::instance().isTouchSupported()) {
embeddedartists 10:1ac4b213f0f7 310 return &BiosDisplayAndTouch::instance();
embeddedartists 10:1ac4b213f0f7 311 }
embeddedartists 10:1ac4b213f0f7 312 return NULL;
embeddedartists 10:1ac4b213f0f7 313 }
embeddedartists 10:1ac4b213f0f7 314
embeddedartists 10:1ac4b213f0f7 315 Display* DMBoard::display()
embeddedartists 10:1ac4b213f0f7 316 {
embeddedartists 10:1ac4b213f0f7 317 return &BiosDisplayAndTouch::instance();
embeddedartists 10:1ac4b213f0f7 318 }