Files @ a48d9ffdc5db
Branch filter:

Location: therm/ssd1306.c

Ethan Zonca
Oled driver updates and pin assignment fixes
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*******************************************************************************
* File Name          : SSD1303.c
* Author             : lxyppc
* Version            : V1.0
* Date               : 10-01-21
* Description        : SSD1303 operations
*                      the SSH1101A is compatible with SSD1303
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "stm32l100c_discovery.h"
#include "bsp.h"
#include "ssd1306.h"
#include "DrawText.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define   SSD1303_PAGE_NUMBER           8
#define   SSD1303_COLUMN_NUMBER         128
#define   SSD1303_COLUMN_MARGIN_START   2
#define   SSD1303_COLUMN_MARGIN_END     2
#define   SSD1303_X_PIXEL   128
#define   SSD1303_Y_PIXEL   64

/* Private macro -------------------------------------------------------------*/
#define   SSD1303_Buffer    (_SSD1303_Buffer + SSD1303_COLUMN_MARGIN_START)

/* Private variables ---------------------------------------------------------*/
static  uint8_t  _SSD1303_Buffer[SSD1303_COLUMN_NUMBER*SSD1303_PAGE_NUMBER 
+ SSD1303_COLUMN_MARGIN_START + SSD1303_COLUMN_MARGIN_END] = {0};
static  uint8_t  pageIndex = 0;
static  uint8_t  iS_SSD_On = 0;
static  uint8_t  pre_on = 0;
static  uint8_t  curContrast = 0xCC;
static  uint8_t  lastContrast = 0xCC;

/* Private function prototypes -----------------------------------------------*/
void  WriteCommand(unsigned char command);
void  WriteData(unsigned char data);
void  OnPageTransferDone(void);
unsigned long SSD1303_OFF(void);
unsigned long SSD1303_ON(void);
unsigned char* SSD1303_GetBuffer()
{
  return SSD1303_Buffer;
}
/*******************************************************************************
* Function Name  : WriteCommand
* Description    : Write command to the SSD1303
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void WriteCommand(unsigned char command)
{
  SSD_A0_Low();
  SPI_SendByte(command);
  SPI_Wait();
}

/*******************************************************************************
* Function Name  : WriteData
* Description    : Write data to the SSD1303
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void WriteData(unsigned char data)
{
  SSD_A0_High();
  SPI_SendByte(data);
  SPI_Wait();
}

/*******************************************************************************
* Function Name  : SSD1303_Init
* Description    : Initialize the SSD1303
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SSD1303_Init(void)
{
  /* Generate a reset */
  SSD_Reset_Low();
  uint32_t i ;
  for(i=5000;--i;);
  SSD_Reset_High();
  
  /*************************************************
  // SSD1303 Initialization Command
  *************************************************/
  // Lower Column Address
  WriteCommand(0x00); /* Set Lower Column Address */
  // High Column Address
  WriteCommand(0x10); /* Set Higher Column Address*/
  // Display Start Line
  WriteCommand(0x40); /* Set Display Start Line */
#ifdef    DEBUG_BOARD
  curContrast = lastContrast = 0x30;
#else
  curContrast = lastContrast = 0xCF;
#endif
  // Contrast Control Register
  WriteCommand(0x81); /* Set Contrast Control */
  WriteCommand(lastContrast); /* 0 ~ 255 0x1f*/
  
  // Re-map
  WriteCommand(0xA1); /* [A0]:column address 0 is map 
  to SEG0 , [A1]: columnaddress 131 is map to SEG0*/ 
  // Entire Display ON/OFF
  WriteCommand(0xA4); /* A4=ON */
  // Normal or Inverse Display
  WriteCommand(0XA6); /* Normal Display*/
  // Multiplex Ratio
  WriteCommand(0xA8); /* Set Multiplex Ratio */
  WriteCommand(0x3f); /* Set to 36 Mux*/
  // Set DC-DC
  WriteCommand(0xAD); /* Set DC-DC */
  WriteCommand(0x8B); /* 8B=ON, 8A=Off */
  
  // Display ON/OFF
  WriteCommand(0xAE); /* AF=ON , AE=OFF*/
  // Display Offset
  WriteCommand(0xD3); /* Set Display Offset */
  WriteCommand(0x00); /* No offset */
  // Display Clock Divide
  WriteCommand(0xD5); /* Set Clock Divide */
  WriteCommand(0x20); /* Set to 80Hz */
  // Area Color Mode
  WriteCommand(0xD8); /* Set Area Color On or Off*/
  WriteCommand(0x00); /* Mono Mode */
  // COM Pins Hardware Configuration
  WriteCommand(0xDA); /* Set Pins HardwareConfiguration */
  WriteCommand(0x12);
  // VCOMH
  WriteCommand(0xDB); /* Set VCOMH */
  WriteCommand(0x00);
  // VP
  WriteCommand(0xD9); /* Set VP */
  WriteCommand(0x22); /* P1=2 , P2=2 */
  
  // Set Common output scan direction
  WriteCommand(0xc8);/* Set COM scan direction */
  
  // For SSD1306 Set the address mode
  WriteCommand(0x20);/* Set address mode */
  WriteCommand(0x00);/* Set address mode horizontal */
  
  // Set the page start address
  WriteCommand(0xb0);
  WriteCommand(0x00);
  WriteCommand(0x10);
  
  /* Turn on the controller */
  pre_on = SSD1303_ON();
//  // Set Charge pump
//  WriteCommand(0x8D); /* Set Charge pump */
//  WriteCommand(0x14); /* 0x14=ON, 0x10=Off */
//  
//  // Turn on the display
//  WriteCommand(0xaf);
}

/*******************************************************************************
* Function Name  : SSD1303_TurnOff
* Description    : Turn off the SSD1303 controller
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
unsigned long SSD1303_TurnOff(void)
{
  pre_on = 0;
  return iS_SSD_On;
}

/*******************************************************************************
* Function Name  : SSD1303_TurnOn
* Description    : Turn off the SSD1303 controller
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
unsigned long SSD1303_TurnOn(void)
{
  pre_on = 1;
  return iS_SSD_On;
}

/*******************************************************************************
* Function Name  : SSD1303_SetContrast
* Description    : Set the SSD1303 contrast
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
unsigned char SSD1303_SetContrast(unsigned char contrast)
{
  curContrast = contrast;
  return lastContrast;
}

/*******************************************************************************
* Function Name  : SSD1303_GetContrast
* Description    : Get the SSD1303 contrast
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
unsigned char SSD1303_GetContrast()
{
  return lastContrast;
}

/*******************************************************************************
* Function Name  : SSD1303_OFF
* Description    : Turn off the SSD1303 controller
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
unsigned long SSD1303_OFF(void)
{
  if(iS_SSD_On){
#ifdef  DEBUG_UI
    uint32_t i = 0;
    for(i=0;i<SSD1303_COLUMN_NUMBER*SSD1303_PAGE_NUMBER;i++){
      SSD1303_Buffer[i] = 0;
    }
#else
    // Turn off the display
    WriteCommand(0xae);
    
    // Set Charge pump
    WriteCommand(0x8D); /* Set Charge pump */
    WriteCommand(0x10); /* 0x14=ON, 0x10=Off */
#endif
    iS_SSD_On = 0;
  }
  return iS_SSD_On;
}


/*******************************************************************************
* Function Name  : SSD1303_ON
* Description    : Turn on the SSD1303 controller
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
unsigned long SSD1303_ON(void)
{
  if(!iS_SSD_On){
#ifdef  DEBUG_UI
#else
  #ifdef    DEBUG_BOARD
  #else
    // Set Charge pump
    WriteCommand(0x8D); /* Set Charge pump */
    WriteCommand(0x14); /* 0x14=ON, 0x10=Off */
  #endif
    // Turn on the display
    WriteCommand(0xaf);
#endif
    iS_SSD_On = 1;
  }
  return iS_SSD_On;
}


/*******************************************************************************
* Function Name  : SSD1303_IsOn
* Description    : Check whether the SSD1303 is on or off
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
unsigned long SSD1303_IsOn(void)
{
  return iS_SSD_On;
}

/*******************************************************************************
* Function Name  : OnPageTransferDone
* Description    : Called when each page transfer done
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void  OnPageTransferDone(void)
{
  if(pageIndex == SSD1303_PAGE_NUMBER){
    pageIndex = 0;
    return;
  }
#ifdef    DEBUG_BOARD
  WriteCommand(0xb0 + pageIndex);
  if(pageIndex == 0){
    WriteCommand(0x00);
    WriteCommand(0x10);
  }
  SSD_A0_High();
  DMA1_Channel5->CCR &= ((uint32_t)0xFFFFFFFE);
  DMA1_Channel5->CNDTR = SSD1303_COLUMN_NUMBER+SSD1303_COLUMN_MARGIN_START + SSD1303_COLUMN_MARGIN_END;
  DMA1_Channel5->CMAR = (uint32_t)(SSD1303_Buffer+SSD1303_COLUMN_NUMBER*pageIndex - SSD1303_COLUMN_MARGIN_START);
  DMA_SSD_1303->CCR |= ((uint32_t)0x00000001);
  pageIndex++;
#else
  SSD_A0_High();
  DMA_SSD_1303->CCR &= ((uint32_t)0xFFFFFFFE);
  DMA_SSD_1303->CNDTR = SSD1303_COLUMN_NUMBER*SSD1303_PAGE_NUMBER;//+SSD1303_COLUMN_MARGIN_START + SSD1303_COLUMN_MARGIN_END;
  DMA_SSD_1303->CMAR = (uint32_t)(SSD1303_Buffer);//+SSD1303_COLUMN_NUMBER*pageIndex);
  //DMA_Cmd(DMA_SSD_1303, ENABLE);
  DMA_SSD_1303->CCR |= ((uint32_t)0x00000001);
//  pageIndex++;
  pageIndex = SSD1303_PAGE_NUMBER;
#endif
}

/*******************************************************************************
* Function Name  : DMA1_Channel5_IRQHandler
* Description    : This function handles DMA1 Channel 5 interrupt request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void DMA_Handler_SSD_1303(void) //DMA1_Channel5_IRQHandler(void)
{
  if(DMA_GetITStatus(DMA1_IT_TC5)){
    DMA_ClearITPendingBit(DMA1_IT_GL5);
    OnPageTransferDone();
  }
}

/*******************************************************************************
* Function Name  : StartPageTransfer
* Description    : Start a new page transfer
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void StartPageTransfer(void)
{
  pageIndex = 0;
  if(pre_on){
    if(iS_SSD_On == 0){
      pre_on = SSD1303_ON();
    }
  }else{
    if(iS_SSD_On){
      pre_on = SSD1303_OFF();
    }
  }
  if( curContrast != lastContrast ){
    lastContrast = curContrast;
    WriteCommand(0x81); /* Set Contrast Control */
    WriteCommand(lastContrast); /* 0 ~ 255 0x1f*/
  }
  
  if(iS_SSD_On){
    OnPageTransferDone();
  }
}

/*******************************************************************************
  Display related functions
 ******************************************************************************/
/*******************************************************************************
* Function Name  : SSD1303_DrawBlock
* Description    : Draw a block with specify data to SSD1303
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
unsigned long SSD1303_DrawBlock(
  Pos_t x,
  Pos_t y,
  Pos_t cx,
  Pos_t cy,
  const unsigned char* data)
{
  if(x >= SSD1303_X_PIXEL)return 0;
  if(y >= SSD1303_Y_PIXEL)return 0;
  if(x+cx > SSD1303_X_PIXEL ) cx = SSD1303_X_PIXEL - x ;
  if(y+cy > SSD1303_Y_PIXEL ) cy = SSD1303_Y_PIXEL - y ;
  unsigned char* pStart = SSD1303_Buffer + (y/8)*SSD1303_COLUMN_NUMBER + x;
  unsigned char offset1 = y%8;
  unsigned char offset2 = (cy+y)%8;
  if(data){
    unsigned char mask1 = (1<<offset1)-1;
    unsigned char mask2 = ~((1<<offset2)-1);
    cy = (cy+offset1)/8;
   
    Pos_t i = 0;
    for(i=0;i<cx;i++){
      Pos_t j = 0;
      unsigned short tmp = *pStart & mask1;
      for(;j<cy;j++){
        tmp |= (((unsigned short)data[j*cx])<<offset1);
        *(pStart + j*SSD1303_COLUMN_NUMBER) = tmp;
        tmp>>=8;
      }
      if(offset2){
        tmp |= ((((unsigned short)data[j*cx])<<offset1) & (~mask2))
            | *(pStart + j*SSD1303_COLUMN_NUMBER) & mask2;
        *(pStart + j*SSD1303_COLUMN_NUMBER) = tmp;
      }
      data++;
      pStart++;
    }
  }else{
    unsigned char mask1 = ~((1<<offset1)-1);
    unsigned char mask2 = ((1<<offset2)-1);
    cy = (cy+offset1)/8;

    Pos_t i = 0;
    for(i=0;i<cx;i++){
      Pos_t j = 1;
      *pStart ^= mask1;
      for(;j<cy;j++){
        *(pStart + j*SSD1303_COLUMN_NUMBER) ^= 0xFF;
      }
      if(offset2){
        *(pStart + j*SSD1303_COLUMN_NUMBER) ^= mask2;
      }
      pStart++;
    }
  }
  
  
//  if(!offset){
//    for(Pos_t i=0;i<cx;i++){
//      unsigned char tmp;
//      Pos_t j = 0;
//      for(;j<cy/8;j++){
//        tmp = data[i+j*cx];
//        *(pStart + j*SSD1303_COLUMN_NUMBER + i + x) = tmp;
//      }
//      if(cy&0x7){
//        unsigned char mask1 = (1<<(cy&7)) - 1;
//        unsigned char mask2 = 0xFF - mask1;
//        tmp = (data[i+j*cx] & mask1)
//            | (*(pStart + j*SSD1303_COLUMN_NUMBER + i + x) & mask2);
//        *(pStart + j*SSD1303_COLUMN_NUMBER + i + x) = tmp;
//      }
//    }
//  }else{
//    for(Pos_t i=0;i<cx;i++){
//      unsigned short tmp;
//      for(Pos_t j=0;j<cy/8;j++){
//        if(j == 0){
//          // First line
//          tmp = *(pStart + j*SSD1303_COLUMN_NUMBER + i + x) & mask;
//        }
//        tmp |= (((unsigned short)data[i+j*cx])<<offset);
//        *(pStart + j*SSD1303_COLUMN_NUMBER + i + x) = tmp;
//        tmp>>=8;
//        if(j == (cy/8)-1){
//          // Last line
//          *(pStart + (j+1)*SSD1303_COLUMN_NUMBER + i + x) &= ~mask;
//          *(pStart + (j+1)*SSD1303_COLUMN_NUMBER + i + x) |= tmp;
//        }
//      }
//    }
//  }
  
  return 0;
}

/*******************************************************************************
* Function Name  : SSD1303_DrawPoint
* Description    : Draw a point with specify data to SSD1303
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
unsigned long SSD1303_DrawPoint(
  Pos_t x,
  Pos_t y,
  Color_t color)
{
  if(x >= SSD1303_X_PIXEL)return 0;
  if(y >= SSD1303_Y_PIXEL)return 0;
  unsigned char* pStart = SSD1303_Buffer + (y/8)*SSD1303_COLUMN_NUMBER + x;
  unsigned char mask = 1<<(y%8);
  if(color){
    *pStart |= mask;
  }else{
    *pStart &= ~mask;
  }
  return 0;
}

/*******************************************************************************
* Function Name  : SSD1303_ReadPoint
* Description    : Read a point from SSD1303
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
unsigned long SSD1303_ReadPoint(
  Pos_t x,
  Pos_t y)
{
  unsigned char* pStart = SSD1303_Buffer + (y/8)*SSD1303_COLUMN_NUMBER + x;
  unsigned char mask = 1<<(y%8);
  return *pStart&mask ? 1 : 0;
}

/*******************************************************************************
* Function Name  : SSD1303_ClearScreen
* Description    : Clear the screen contents
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void  SSD1303_FillScreen(Color_t color)
{
  unsigned char mask = color ? 0xFF : 0;
  unsigned long i = 0;
  for(i=0;i<SSD1303_COLUMN_NUMBER*SSD1303_PAGE_NUMBER;i++){
    SSD1303_Buffer[i] = mask;
  }
}

const DeviceProp  SSD1303_Prop =
{
  .pfnDrawBlok = SSD1303_DrawBlock,
  .pfnDrawPoint = SSD1303_DrawPoint,
  .xPixel = 128,
  .yPixel = 64,
};