ME11C Sample Code in Maxim Integrated Team

Dependencies:   BMI160 max32630hsp3 MemoryLCD USBDevice

Fork of Host_Software_MAX32664GWEC_SpO2_HR-_EXTE by Seyhmus Cacina

Committer:
seyhmus.cacina
Date:
Mon Mar 18 14:09:48 2019 +0300
Revision:
0:b259fd1a88f5
ME11C Sample Code First Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
seyhmus.cacina 0:b259fd1a88f5 1 /***************************************************************************//**
seyhmus.cacina 0:b259fd1a88f5 2 * @file LS013B7DH03.cpp
seyhmus.cacina 0:b259fd1a88f5 3 * @brief Driver class for the Sharp LS013B7DH03 memory LCD on some kits.
seyhmus.cacina 0:b259fd1a88f5 4 *******************************************************************************
seyhmus.cacina 0:b259fd1a88f5 5 * @section License
seyhmus.cacina 0:b259fd1a88f5 6 * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b>
seyhmus.cacina 0:b259fd1a88f5 7 *******************************************************************************
seyhmus.cacina 0:b259fd1a88f5 8 *
seyhmus.cacina 0:b259fd1a88f5 9 * Permission is granted to anyone to use this software for any purpose,
seyhmus.cacina 0:b259fd1a88f5 10 * including commercial applications, and to alter it and redistribute it
seyhmus.cacina 0:b259fd1a88f5 11 * freely, subject to the following restrictions:
seyhmus.cacina 0:b259fd1a88f5 12 *
seyhmus.cacina 0:b259fd1a88f5 13 * 1. The origin of this software must not be misrepresented; you must not
seyhmus.cacina 0:b259fd1a88f5 14 * claim that you wrote the original software.
seyhmus.cacina 0:b259fd1a88f5 15 * 2. Altered source versions must be plainly marked as such, and must not be
seyhmus.cacina 0:b259fd1a88f5 16 * misrepresented as being the original software.
seyhmus.cacina 0:b259fd1a88f5 17 * 3. This notice may not be removed or altered from any source distribution.
seyhmus.cacina 0:b259fd1a88f5 18 *
seyhmus.cacina 0:b259fd1a88f5 19 * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no
seyhmus.cacina 0:b259fd1a88f5 20 * obligation to support this Software. Silicon Labs is providing the
seyhmus.cacina 0:b259fd1a88f5 21 * Software "AS IS", with no express or implied warranties of any kind,
seyhmus.cacina 0:b259fd1a88f5 22 * including, but not limited to, any implied warranties of merchantability
seyhmus.cacina 0:b259fd1a88f5 23 * or fitness for any particular purpose or warranties against infringement
seyhmus.cacina 0:b259fd1a88f5 24 * of any proprietary rights of a third party.
seyhmus.cacina 0:b259fd1a88f5 25 *
seyhmus.cacina 0:b259fd1a88f5 26 * Silicon Labs will not be liable for any consequential, incidental, or
seyhmus.cacina 0:b259fd1a88f5 27 * special damages, or any other relief, or for any claim by any third party,
seyhmus.cacina 0:b259fd1a88f5 28 * arising from your use of this Software.
seyhmus.cacina 0:b259fd1a88f5 29 *
seyhmus.cacina 0:b259fd1a88f5 30 ******************************************************************************/
seyhmus.cacina 0:b259fd1a88f5 31
seyhmus.cacina 0:b259fd1a88f5 32 #include "../screen/LS013B7DH03.h"
seyhmus.cacina 0:b259fd1a88f5 33
seyhmus.cacina 0:b259fd1a88f5 34 #include <mbed.h>
seyhmus.cacina 0:b259fd1a88f5 35 #include "SPI.h"
seyhmus.cacina 0:b259fd1a88f5 36 //#include "Peripherals.h"
seyhmus.cacina 0:b259fd1a88f5 37
seyhmus.cacina 0:b259fd1a88f5 38 /* LS013B7DH03 SPI commands */
seyhmus.cacina 0:b259fd1a88f5 39 #define LS013B7DH03_CMD_UPDATE (0x01)
seyhmus.cacina 0:b259fd1a88f5 40 #define LS013B7DH03_CMD_ALL_CLEAR (0x04)
seyhmus.cacina 0:b259fd1a88f5 41
seyhmus.cacina 0:b259fd1a88f5 42 /* Macro to switch endianness on char value */
seyhmus.cacina 0:b259fd1a88f5 43 #define SWAP8(a) ((((a) & 0x80) >> 7) | (((a) & 0x40) >> 5) | (((a) & 0x20) >> 3) | (((a) & 0x10) >> 1) | (((a) & 0x08) << 1) | (((a) & 0x04) << 3) | (((a) & 0x02) << 5) | (((a) & 0x01) << 7))
seyhmus.cacina 0:b259fd1a88f5 44
seyhmus.cacina 0:b259fd1a88f5 45 namespace silabs {
seyhmus.cacina 0:b259fd1a88f5 46
seyhmus.cacina 0:b259fd1a88f5 47
seyhmus.cacina 0:b259fd1a88f5 48 LS013B7DH03::LS013B7DH03(mbed::SPI * spi, DigitalOut * CS, const char *name) : BufferedDisplay( name ) {
seyhmus.cacina 0:b259fd1a88f5 49 //Save pointer to ChipSelect pin
seyhmus.cacina 0:b259fd1a88f5 50 _CS = CS;
seyhmus.cacina 0:b259fd1a88f5 51 _CS->write(0);
seyhmus.cacina 0:b259fd1a88f5 52 DigitalOut DISP(P6_6);
seyhmus.cacina 0:b259fd1a88f5 53
seyhmus.cacina 0:b259fd1a88f5 54 //Save pointer to ExtCom pin
seyhmus.cacina 0:b259fd1a88f5 55 /// _EXTCOM = ExtCom;
seyhmus.cacina 0:b259fd1a88f5 56 /// _EXTCOM->write(0);
seyhmus.cacina 0:b259fd1a88f5 57
seyhmus.cacina 0:b259fd1a88f5 58 DISP = 0;
seyhmus.cacina 0:b259fd1a88f5 59 wait_ms(1);
seyhmus.cacina 0:b259fd1a88f5 60 DISP = 1;
seyhmus.cacina 0:b259fd1a88f5 61
seyhmus.cacina 0:b259fd1a88f5 62 //Save pointer to spi peripheral
seyhmus.cacina 0:b259fd1a88f5 63 _spi = spi;
seyhmus.cacina 0:b259fd1a88f5 64 //_spi->frequency(600000);
seyhmus.cacina 0:b259fd1a88f5 65 _spi->format( 8, 0 );
seyhmus.cacina 0:b259fd1a88f5 66
seyhmus.cacina 0:b259fd1a88f5 67 _internalEventCallback.attach(this, &LS013B7DH03::_cbHandler);
seyhmus.cacina 0:b259fd1a88f5 68
seyhmus.cacina 0:b259fd1a88f5 69 //Initialize
seyhmus.cacina 0:b259fd1a88f5 70 //_spi->set_dma_usage((DMAUsage)DMA_USAGE_NEVER);
seyhmus.cacina 0:b259fd1a88f5 71 _refreshCount = 0;
seyhmus.cacina 0:b259fd1a88f5 72 _lcdPolarity = 0;
seyhmus.cacina 0:b259fd1a88f5 73 _state = IDLE;
seyhmus.cacina 0:b259fd1a88f5 74 _completionCallbackPtr = NULL;
seyhmus.cacina 0:b259fd1a88f5 75 _rowCount = 0;
seyhmus.cacina 0:b259fd1a88f5 76
seyhmus.cacina 0:b259fd1a88f5 77 //Start toggling the EXTCOM pin
seyhmus.cacina 0:b259fd1a88f5 78 //_displayToggler.attach(this, &LS013B7DH03::toggle, 0.008f);
seyhmus.cacina 0:b259fd1a88f5 79 }
seyhmus.cacina 0:b259fd1a88f5 80
seyhmus.cacina 0:b259fd1a88f5 81 /**
seyhmus.cacina 0:b259fd1a88f5 82 * Call this function at 55 ~ 65 Hz to keep the display up-to-date.
seyhmus.cacina 0:b259fd1a88f5 83 */
seyhmus.cacina 0:b259fd1a88f5 84 void LS013B7DH03::toggle() {
seyhmus.cacina 0:b259fd1a88f5 85 // _EXTCOM->write(!_EXTCOM->read());
seyhmus.cacina 0:b259fd1a88f5 86 // _refreshCount++;
seyhmus.cacina 0:b259fd1a88f5 87 }
seyhmus.cacina 0:b259fd1a88f5 88
seyhmus.cacina 0:b259fd1a88f5 89 /**
seyhmus.cacina 0:b259fd1a88f5 90 * Function to get internal refresh counter
seyhmus.cacina 0:b259fd1a88f5 91 */
seyhmus.cacina 0:b259fd1a88f5 92 uint32_t LS013B7DH03::getRefreshTicks() {
seyhmus.cacina 0:b259fd1a88f5 93 return _refreshCount;
seyhmus.cacina 0:b259fd1a88f5 94 }
seyhmus.cacina 0:b259fd1a88f5 95
seyhmus.cacina 0:b259fd1a88f5 96 /**
seyhmus.cacina 0:b259fd1a88f5 97 * Call this function to push all changes to the display
seyhmus.cacina 0:b259fd1a88f5 98 */
seyhmus.cacina 0:b259fd1a88f5 99 int LS013B7DH03::update( cbptr_t callback ) {
seyhmus.cacina 0:b259fd1a88f5 100 uint32_t rowCount = 0;
seyhmus.cacina 0:b259fd1a88f5 101 bool update = false;
seyhmus.cacina 0:b259fd1a88f5 102
seyhmus.cacina 0:b259fd1a88f5 103 // Check if something actually changed in the pixelbuffer
seyhmus.cacina 0:b259fd1a88f5 104 for(rowCount = 0; rowCount < DISPLAY_HEIGHT/DISPLAY_BUFFER_TYPE_SIZE; rowCount++) {
seyhmus.cacina 0:b259fd1a88f5 105 if(_dirtyRows[rowCount] != 0) update = true;
seyhmus.cacina 0:b259fd1a88f5 106 }
seyhmus.cacina 0:b259fd1a88f5 107
seyhmus.cacina 0:b259fd1a88f5 108 if(update == false) return LS013B7DH03_NO_ACTION;
seyhmus.cacina 0:b259fd1a88f5 109
seyhmus.cacina 0:b259fd1a88f5 110 // Watch out to not mess up a transfer
seyhmus.cacina 0:b259fd1a88f5 111 if(_state != IDLE) return LS013B7DH03_ERROR_BUSY;
seyhmus.cacina 0:b259fd1a88f5 112
seyhmus.cacina 0:b259fd1a88f5 113 _completionCallbackPtr = callback;
seyhmus.cacina 0:b259fd1a88f5 114
seyhmus.cacina 0:b259fd1a88f5 115 // Take control
seyhmus.cacina 0:b259fd1a88f5 116 _state = WAIT_WRITE;
seyhmus.cacina 0:b259fd1a88f5 117 _rowCount = 0;
seyhmus.cacina 0:b259fd1a88f5 118
seyhmus.cacina 0:b259fd1a88f5 119 //Initialize the command vector
seyhmus.cacina 0:b259fd1a88f5 120 _cmd[0] = (uint8_t)SWAP8(LS013B7DH03_CMD_UPDATE);
seyhmus.cacina 0:b259fd1a88f5 121 _cmd[1] = SWAP8(1);
seyhmus.cacina 0:b259fd1a88f5 122
seyhmus.cacina 0:b259fd1a88f5 123 // Activate LCD
seyhmus.cacina 0:b259fd1a88f5 124 _CS->write(1);
seyhmus.cacina 0:b259fd1a88f5 125 _csTimeout.attach(this, &LS013B7DH03::_cbHandlerTimeout, 0.01f);
seyhmus.cacina 0:b259fd1a88f5 126
seyhmus.cacina 0:b259fd1a88f5 127 return LS013B7DH03_OK;
seyhmus.cacina 0:b259fd1a88f5 128 }
seyhmus.cacina 0:b259fd1a88f5 129
seyhmus.cacina 0:b259fd1a88f5 130 /**
seyhmus.cacina 0:b259fd1a88f5 131 * Function to test display buffer
seyhmus.cacina 0:b259fd1a88f5 132 */
seyhmus.cacina 0:b259fd1a88f5 133 int LS013B7DH03::showDemo() {
seyhmus.cacina 0:b259fd1a88f5 134 for(uint32_t i = 0; i < DISPLAY_BUFFER_ELEMENTS; i+=2) {
seyhmus.cacina 0:b259fd1a88f5 135 _pixelBuffer[i] = 0x00555345;
seyhmus.cacina 0:b259fd1a88f5 136 }
seyhmus.cacina 0:b259fd1a88f5 137 memset((void*)_dirtyRows, 0x33, sizeof(_dirtyRows));
seyhmus.cacina 0:b259fd1a88f5 138
seyhmus.cacina 0:b259fd1a88f5 139 return LS013B7DH03_OK;
seyhmus.cacina 0:b259fd1a88f5 140 }
seyhmus.cacina 0:b259fd1a88f5 141
seyhmus.cacina 0:b259fd1a88f5 142 /**
seyhmus.cacina 0:b259fd1a88f5 143 * Call this function to immediately clear the display
seyhmus.cacina 0:b259fd1a88f5 144 */
seyhmus.cacina 0:b259fd1a88f5 145 int LS013B7DH03::clearImmediate( cbptr_t callback ) {
seyhmus.cacina 0:b259fd1a88f5 146 // Watch out to not mess up a transfer
seyhmus.cacina 0:b259fd1a88f5 147 if(_state != IDLE) return LS013B7DH03_ERROR_BUSY;
seyhmus.cacina 0:b259fd1a88f5 148
seyhmus.cacina 0:b259fd1a88f5 149 _state = WAIT_CLEAR;
seyhmus.cacina 0:b259fd1a88f5 150 _completionCallbackPtr = callback;
seyhmus.cacina 0:b259fd1a88f5 151
seyhmus.cacina 0:b259fd1a88f5 152 // Clear out the pixel buffer
seyhmus.cacina 0:b259fd1a88f5 153 memset((void*)_pixelBuffer, White, sizeof(_pixelBuffer));
seyhmus.cacina 0:b259fd1a88f5 154 memset((void*)_dirtyRows, 0, sizeof(_dirtyRows));
seyhmus.cacina 0:b259fd1a88f5 155
seyhmus.cacina 0:b259fd1a88f5 156 _cmd[0] = (uint8_t)(SWAP8(LS013B7DH03_CMD_ALL_CLEAR | _lcdPolarity));
seyhmus.cacina 0:b259fd1a88f5 157 _cmd[1] = 0;
seyhmus.cacina 0:b259fd1a88f5 158
seyhmus.cacina 0:b259fd1a88f5 159 // Wait for the ChipSelect line
seyhmus.cacina 0:b259fd1a88f5 160 _CS->write(1);
seyhmus.cacina 0:b259fd1a88f5 161 _csTimeout.attach(this, &LS013B7DH03::_cbHandlerTimeout, 0.01f);
seyhmus.cacina 0:b259fd1a88f5 162
seyhmus.cacina 0:b259fd1a88f5 163 return LS013B7DH03_OK;
seyhmus.cacina 0:b259fd1a88f5 164 }
seyhmus.cacina 0:b259fd1a88f5 165
seyhmus.cacina 0:b259fd1a88f5 166 void LS013B7DH03::_cbHandlerTimeout( void ) {
seyhmus.cacina 0:b259fd1a88f5 167 this->_cbHandler(0);
seyhmus.cacina 0:b259fd1a88f5 168 }
seyhmus.cacina 0:b259fd1a88f5 169
seyhmus.cacina 0:b259fd1a88f5 170 void LS013B7DH03::_cbHandler( int event ) {
seyhmus.cacina 0:b259fd1a88f5 171 if((_state == WAIT_WRITE) || (_state == WRITING))
seyhmus.cacina 0:b259fd1a88f5 172 {
seyhmus.cacina 0:b259fd1a88f5 173 _state = WRITING;
seyhmus.cacina 0:b259fd1a88f5 174 while(_rowCount < DISPLAY_HEIGHT) {
seyhmus.cacina 0:b259fd1a88f5 175 // Determine the next line to send
seyhmus.cacina 0:b259fd1a88f5 176 if((_dirtyRows[_rowCount / DISPLAY_BUFFER_TYPE_SIZE] & (1 << (_rowCount % DISPLAY_BUFFER_TYPE_SIZE))) != 0) {
seyhmus.cacina 0:b259fd1a88f5 177
seyhmus.cacina 0:b259fd1a88f5 178 // Row is dirty, send an update to the display
seyhmus.cacina 0:b259fd1a88f5 179 _cmd[1] = (uint8_t)SWAP8(_rowCount + 1);
seyhmus.cacina 0:b259fd1a88f5 180 memcpy((void*)&(_cmd[2]), (const void*)&(_pixelBuffer[_rowCount*(DISPLAY_WIDTH/DISPLAY_BUFFER_TYPE_SIZE)]), DISPLAY_WIDTH / DISPLAY_BUFFER_TYPE_SIZE * sizeof(DISPLAY_BUFFER_TYPE));
seyhmus.cacina 0:b259fd1a88f5 181
seyhmus.cacina 0:b259fd1a88f5 182 if(_spi->write((const char*)_cmd, (2 + (DISPLAY_WIDTH / DISPLAY_BUFFER_TYPE_SIZE * sizeof(DISPLAY_BUFFER_TYPE))) , (char*)NULL, 0/*, _internalEventCallback, SPI_EVENT_COMPLETE*/) != (2 + (DISPLAY_WIDTH / DISPLAY_BUFFER_TYPE_SIZE * sizeof(DISPLAY_BUFFER_TYPE)))) {
seyhmus.cacina 0:b259fd1a88f5 183 // SPI is busy, with another transaction. This means the data to the LCD has been corrupted, so fail here.
seyhmus.cacina 0:b259fd1a88f5 184 _state = DONE;
seyhmus.cacina 0:b259fd1a88f5 185 //printf("Failed at _cbHandler\n");
seyhmus.cacina 0:b259fd1a88f5 186 // Make sure the handler is called again
seyhmus.cacina 0:b259fd1a88f5 187 _csTimeout.attach(this, &LS013B7DH03::_cbHandlerTimeout, 0.1f);
seyhmus.cacina 0:b259fd1a88f5 188 }else{ //sc...
seyhmus.cacina 0:b259fd1a88f5 189 _csTimeout.attach(this, &LS013B7DH03::_cbHandlerTimeout, 0.001f);
seyhmus.cacina 0:b259fd1a88f5 190 }
seyhmus.cacina 0:b259fd1a88f5 191
seyhmus.cacina 0:b259fd1a88f5 192 // Transaction is in progress, so update row state
seyhmus.cacina 0:b259fd1a88f5 193 _dirtyRows[_rowCount / DISPLAY_BUFFER_TYPE_SIZE] &= ~(1 << (_rowCount % DISPLAY_BUFFER_TYPE_SIZE));
seyhmus.cacina 0:b259fd1a88f5 194 _rowCount++;
seyhmus.cacina 0:b259fd1a88f5 195 return;
seyhmus.cacina 0:b259fd1a88f5 196 }
seyhmus.cacina 0:b259fd1a88f5 197
seyhmus.cacina 0:b259fd1a88f5 198 // Row wasn't touched, so check the next row
seyhmus.cacina 0:b259fd1a88f5 199 _rowCount++;
seyhmus.cacina 0:b259fd1a88f5 200 }
seyhmus.cacina 0:b259fd1a88f5 201
seyhmus.cacina 0:b259fd1a88f5 202 // Done sending!
seyhmus.cacina 0:b259fd1a88f5 203 _cmd[1] = 0xFF;
seyhmus.cacina 0:b259fd1a88f5 204 _state = TRANSFERS_DONE;
seyhmus.cacina 0:b259fd1a88f5 205 if(_spi->write((const char*)_cmd, 2, (char*)NULL, 0/*, _internalEventCallback, SPI_EVENT_COMPLETE*/) != 2) {
seyhmus.cacina 0:b259fd1a88f5 206 // SPI is busy, with another transaction. This means the data to the LCD has been corrupted, so fail here.
seyhmus.cacina 0:b259fd1a88f5 207 _state = DONE;
seyhmus.cacina 0:b259fd1a88f5 208
seyhmus.cacina 0:b259fd1a88f5 209 // Make sure the handler is called again
seyhmus.cacina 0:b259fd1a88f5 210 _csTimeout.attach(this, &LS013B7DH03::_cbHandlerTimeout, 0.1f);
seyhmus.cacina 0:b259fd1a88f5 211 }else{ //sc...
seyhmus.cacina 0:b259fd1a88f5 212 _csTimeout.attach(this, &LS013B7DH03::_cbHandlerTimeout, 0.001f);
seyhmus.cacina 0:b259fd1a88f5 213 }
seyhmus.cacina 0:b259fd1a88f5 214 return;
seyhmus.cacina 0:b259fd1a88f5 215 }
seyhmus.cacina 0:b259fd1a88f5 216 else if (_state == WAIT_CLEAR)
seyhmus.cacina 0:b259fd1a88f5 217 {
seyhmus.cacina 0:b259fd1a88f5 218 _state = TRANSFERS_DONE;
seyhmus.cacina 0:b259fd1a88f5 219 if(_spi->write((const char*)_cmd, 2, (char*)NULL, 0/*, _internalEventCallback, SPI_EVENT_COMPLETE*/) != 2) {
seyhmus.cacina 0:b259fd1a88f5 220 // SPI is busy, with another transaction. This means the data to the LCD has been corrupted, so fail here.
seyhmus.cacina 0:b259fd1a88f5 221 _state = DONE;
seyhmus.cacina 0:b259fd1a88f5 222
seyhmus.cacina 0:b259fd1a88f5 223 // Make sure the handler is called again
seyhmus.cacina 0:b259fd1a88f5 224 _csTimeout.attach(this, &LS013B7DH03::_cbHandlerTimeout, 0.1f);
seyhmus.cacina 0:b259fd1a88f5 225 }else{ //sc...
seyhmus.cacina 0:b259fd1a88f5 226 _csTimeout.attach(this, &LS013B7DH03::_cbHandlerTimeout, 0.001f);
seyhmus.cacina 0:b259fd1a88f5 227 }
seyhmus.cacina 0:b259fd1a88f5 228 return;
seyhmus.cacina 0:b259fd1a88f5 229 }
seyhmus.cacina 0:b259fd1a88f5 230 else if (_state == TRANSFERS_DONE)
seyhmus.cacina 0:b259fd1a88f5 231 {
seyhmus.cacina 0:b259fd1a88f5 232 _state = DONE;
seyhmus.cacina 0:b259fd1a88f5 233 _csTimeout.attach(this, &LS013B7DH03::_cbHandlerTimeout, 0.01f);
seyhmus.cacina 0:b259fd1a88f5 234 return;
seyhmus.cacina 0:b259fd1a88f5 235 }
seyhmus.cacina 0:b259fd1a88f5 236 else if (_state == DONE)
seyhmus.cacina 0:b259fd1a88f5 237 {
seyhmus.cacina 0:b259fd1a88f5 238 _CS->write(0);
seyhmus.cacina 0:b259fd1a88f5 239 _state = IDLE;
seyhmus.cacina 0:b259fd1a88f5 240 if(_completionCallbackPtr != 0) _completionCallbackPtr();
seyhmus.cacina 0:b259fd1a88f5 241 return;
seyhmus.cacina 0:b259fd1a88f5 242 }
seyhmus.cacina 0:b259fd1a88f5 243 }
seyhmus.cacina 0:b259fd1a88f5 244
seyhmus.cacina 0:b259fd1a88f5 245 } // namespace silabs
seyhmus.cacina 0:b259fd1a88f5 246