|
| 1 | +;------------------------------------------------------------------------------- |
| 2 | +include '../include/library.inc' |
| 3 | +;------------------------------------------------------------------------------- |
| 4 | + |
| 5 | +library LCDDRVCE, 0 |
| 6 | + |
| 7 | +;------------------------------------------------------------------------------- |
| 8 | +; no dependencies |
| 9 | +;------------------------------------------------------------------------------- |
| 10 | + |
| 11 | +;------------------------------------------------------------------------------- |
| 12 | +; v1 functions |
| 13 | +;------------------------------------------------------------------------------- |
| 14 | + export lcd_Init |
| 15 | + export lcd_Cleanup |
| 16 | + export lcd_Wait |
| 17 | + export lcd_SendCommandRaw |
| 18 | + export lcd_SendParamsRaw |
| 19 | + export lcd_SendCommand |
| 20 | + export lcd_SendCommand1 |
| 21 | + export lcd_SendCommand2 |
| 22 | + export lcd_SendCommandBytes |
| 23 | + export lcd_SendCommandWords |
| 24 | + export lcd_SetUniformGamma |
| 25 | + export lcd_SetDefaultGamma |
| 26 | + |
| 27 | +macro checked_param param, limit |
| 28 | + assert (param) >= 0 & (param) <= (limit) |
| 29 | + db param |
| 30 | +end macro |
| 31 | + |
| 32 | +macro checked_param2 param1, limit1, param2, limit2 |
| 33 | + assert (param1) >= 0 & (param1) <= (limit1) |
| 34 | + assert (param2) >= 0 & (param2) <= (limit2) |
| 35 | + db ((param2) shl 4) or (param1) |
| 36 | +end macro |
| 37 | + |
| 38 | +; Gamma voltage levels are specified here. |
| 39 | +; V0-V2, V20, V43, V61-V63 are the main points, ranging from 129 to 0. |
| 40 | +; V4, V6, V13 are interpolated between V2 and V20, ranging from 60 to 0. |
| 41 | +; V27, V36 are interpolated between V20 and V43, ranging from 25 to 0. |
| 42 | +; V50, V57, V59 are interpolated between V43 and V61, ranging from 60 to 0. |
| 43 | +; J0 and J1 are values between 0 and 3 affecting interpolations for remaining voltages. |
| 44 | +macro gamma_params V0, V1, V2, V20, V43, V61, V62, V63, V4, V6, V13, V27, V36, V50, V57, V59, J0, J1 |
| 45 | + checked_param2 129-(V0), $0F, 23-(V63), $0F |
| 46 | + checked_param 128-(V1), $3F |
| 47 | + checked_param 128-(V2), $3F |
| 48 | + checked_param 57-(V4), $1F |
| 49 | + checked_param 47-(V6), $1F |
| 50 | + checked_param2 21-(V13), $0F, J0, $03 |
| 51 | + checked_param 128-(V20), $7F |
| 52 | + checked_param2 20-(V27), $07, 11-(V36), $07 |
| 53 | + checked_param 128-(V43), $7F |
| 54 | + checked_param2 54-(V50), $0F, J1, $03 |
| 55 | + checked_param 44-(V57), $1F |
| 56 | + checked_param 34-(V59), $1F |
| 57 | + checked_param 64-(V61), $3F |
| 58 | + checked_param 64-(V62), $3F |
| 59 | +end macro |
| 60 | + |
| 61 | +lcd_Init: |
| 62 | + ; Increase the refcount |
| 63 | + scf |
| 64 | + sbc hl,hl |
| 65 | + ld de, 0 |
| 66 | +.refcount := $-3 |
| 67 | + add hl, de |
| 68 | + ld (.refcount), hl |
| 69 | + ret c |
| 70 | + ; Initialize if the old refcount was 0 |
| 71 | + ld hl, ((9-1) shl 16) or (2-1) |
| 72 | + ld (ti.mpSpiCtrl1), hl |
| 73 | + ; Only fully initialize once per program invocation |
| 74 | + ld hl, .fullinit |
| 75 | + srl (hl) |
| 76 | + jr nc, .fastinit |
| 77 | + ; Magic initialization sequence to work on Python models |
| 78 | + ld de, ti.spiSpiFrFmt or ti.bmSpiFlash or ti.bmSpiFsPolarity or ti.bmSpiMasterMono |
| 79 | +.loop: |
| 80 | + ld (ti.mpSpiCtrl0), de |
| 81 | + ld hl, ti.bmSpiTxClr or ti.bmSpiRxClr |
| 82 | + ld (ti.mpSpiCtrl2), hl |
| 83 | +.fullinit: |
| 84 | + ; Becomes a nop after being shifted |
| 85 | + db 1 |
| 86 | + ld hl, ti.bmSpiChipReset |
| 87 | + ld (ti.mpSpiCtrl2), hl |
| 88 | + call ti.Delay10ms |
| 89 | + bit ti.bSpiClkPolarity, e |
| 90 | + ld e, ti.bmSpiFsPolarity or ti.bmSpiMasterMono or ti.bmSpiClkPhase or ti.bmSpiClkPolarity |
| 91 | + jr z, .loop |
| 92 | +.fastinit: |
| 93 | + ld hl, $21 |
| 94 | + ld (ti.mpSpiIntCtrl), hl |
| 95 | + ld l, ti.bmSpiChipEn |
| 96 | + assert ti.bmSpiTxEn = (ti.bmSpiChipEn shl 8) |
| 97 | + ld h, l |
| 98 | + ld (ti.mpSpiCtrl2), hl |
| 99 | + ret |
| 100 | + |
| 101 | +lcd_Cleanup: |
| 102 | + ; Decrease the refcount |
| 103 | + ld hl, (lcd_Init.refcount) |
| 104 | + ld de, 1 |
| 105 | + add hl, de |
| 106 | + ld (lcd_Init.refcount), hl |
| 107 | + ret nc |
| 108 | + ; Deinitialize if the new refcount is 0 |
| 109 | + call lcd_Wait |
| 110 | + ld hl, ((3-1) shl 16) or (12-1) |
| 111 | + ld (ti.mpSpiCtrl1), hl |
| 112 | + ld hl, ti.bmSpiTxEn or ti.bmSpiTxClr or ti.bmSpiRxClr |
| 113 | + ld (ti.mpSpiCtrl2), hl |
| 114 | + ret |
| 115 | + |
| 116 | +lcd_Wait: |
| 117 | + ld hl, ti.mpSpiStatus + 1 |
| 118 | + ld a, (1 shl (ti.bSpiTxFifoBytes - 8)) - 1 |
| 119 | +.waitEmpty: |
| 120 | + cp a, (hl) |
| 121 | + jr c, .waitEmpty |
| 122 | + dec hl |
| 123 | +.waitBusy: |
| 124 | + bit ti.bSpiChipBusy, (hl) |
| 125 | + jr nz, .waitBusy |
| 126 | + ret |
| 127 | + |
| 128 | +lcd_SetUniformGamma: |
| 129 | + ld de, uniformGammaParams |
| 130 | + jr _Gamma |
| 131 | +lcd_SetDefaultGamma: |
| 132 | + ld de, defaultGammaParams |
| 133 | +_Gamma: |
| 134 | + ; Set positive gamma |
| 135 | + ld bc, (14 shl 8) or $E0 |
| 136 | + push bc |
| 137 | + call lcd_SendCommandRaw.entry |
| 138 | + ; Set negative gamma |
| 139 | + ex de, hl |
| 140 | + pop bc |
| 141 | + inc c |
| 142 | + jr lcd_SendCommandRaw.entry |
| 143 | + |
| 144 | +lcd_SendCommandRaw: |
| 145 | + pop hl |
| 146 | + pop bc |
| 147 | + pop de |
| 148 | + push de |
| 149 | + push bc |
| 150 | + push hl |
| 151 | + |
| 152 | +.entry: |
| 153 | + ld hl, ti.mpSpiStatus + 1 |
| 154 | + ld a, ((ti.bmSpiTxFifoBytes shr 8) and $FF) - 1 |
| 155 | +.waitNotFull: |
| 156 | + cp a, (hl) |
| 157 | + jr c, .waitNotFull |
| 158 | + ld l, ti.spiData + 1 |
| 159 | + ld (hl), h |
| 160 | + dec hl |
| 161 | + ld (hl), c |
| 162 | + ld c, 1 |
| 163 | + mlt bc |
| 164 | + ex de, hl |
| 165 | + xor a, a |
| 166 | + sub a, c |
| 167 | + jr nz, lcd_SendParamsRaw.entry |
| 168 | + ret |
| 169 | + |
| 170 | +lcd_SendParamsRaw: |
| 171 | + pop hl |
| 172 | + pop bc |
| 173 | + pop de |
| 174 | + push de |
| 175 | + push bc |
| 176 | + push hl |
| 177 | + scf |
| 178 | + sbc hl, hl |
| 179 | + add hl, bc |
| 180 | + ex de, hl |
| 181 | + ret nc |
| 182 | + |
| 183 | + ld de, ti.mpSpiRange |
| 184 | + xor a, a |
| 185 | + sub a, c |
| 186 | +.entry: |
| 187 | + and a, 7 |
| 188 | + add a, a |
| 189 | + add a, a |
| 190 | + ld (.sendParamsOffsetSMC), a |
| 191 | +.sendParamsLoop: |
| 192 | + ld e, ti.spiStatus + 1 |
| 193 | + scf |
| 194 | +.waitForEight: |
| 195 | + ld a, (de) |
| 196 | + rla |
| 197 | + jr c, .waitForEight |
| 198 | + ld e, ti.spiData + 1 |
| 199 | + jr nz, $ |
| 200 | +.sendParamsOffsetSMC = $-1 |
| 201 | + repeat 8 |
| 202 | + ld (de), a |
| 203 | + dec de |
| 204 | + ldi |
| 205 | + end repeat |
| 206 | + ret po |
| 207 | + cp a, a |
| 208 | + jr .sendParamsLoop |
| 209 | + |
| 210 | +_sendSingleByte: |
| 211 | + ld l, ti.spiStatus + 1 |
| 212 | + ld a, ((ti.bmSpiTxFifoBytes shr 8) and $FF) - 1 |
| 213 | +.waitNotFull: |
| 214 | + cp a, (hl) |
| 215 | + jr c, .waitNotFull |
| 216 | + ld l, ti.spiData |
| 217 | + ld a, (de) |
| 218 | + ld (hl), a |
| 219 | + inc hl |
| 220 | + ld (hl), l |
| 221 | + ret |
| 222 | + |
| 223 | +lcd_SendCommand: |
| 224 | + ld b, 0+1 |
| 225 | + jr lcd_SendCommandBytes.entry |
| 226 | + |
| 227 | +lcd_SendCommand1: |
| 228 | + ld b, 1+1 |
| 229 | + jr lcd_SendCommandBytes.entry |
| 230 | + |
| 231 | +lcd_SendCommand2: |
| 232 | + ld b, 2+1 |
| 233 | + jr lcd_SendCommandBytes.entry |
| 234 | + |
| 235 | +lcd_SendCommandBytes: |
| 236 | + pop hl |
| 237 | + pop bc |
| 238 | + push bc |
| 239 | + push hl |
| 240 | + inc b |
| 241 | +.entry: |
| 242 | + ld hl, 3 |
| 243 | + add hl, sp |
| 244 | + ex de, hl |
| 245 | + ld hl, ti.mpSpiData + 1 |
| 246 | + ld (hl), h |
| 247 | +.loop: |
| 248 | + call _sendSingleByte |
| 249 | + inc de |
| 250 | + inc de |
| 251 | + inc de |
| 252 | + djnz .loop |
| 253 | + ret |
| 254 | + |
| 255 | +lcd_SendCommandWords: |
| 256 | + ld hl, 4 |
| 257 | + add hl, sp |
| 258 | + ld b, (hl) |
| 259 | + inc b |
| 260 | + ex de, hl |
| 261 | + ld hl, ti.mpSpiData + 1 |
| 262 | + ld (hl), h |
| 263 | +.loop: |
| 264 | + dec de |
| 265 | + call _sendSingleByte |
| 266 | + dec b |
| 267 | + ret z |
| 268 | + inc de |
| 269 | + inc de |
| 270 | + inc de |
| 271 | + inc de |
| 272 | + call _sendSingleByte |
| 273 | + jr .loop |
| 274 | + |
| 275 | +defaultGammaParams: |
| 276 | + ; Positive gamma |
| 277 | + gamma_params 129, 128, 128, 83, 65, 45, 41, 10, 41, 32, 11, 16, 6, 43, 20, 11, 1, 3 |
| 278 | + ; Negative gamma |
| 279 | + gamma_params 129, 128, 128, 85, 64, 45, 41, 10, 41, 32, 12, 17, 7, 43, 20, 11, 0, 3 |
| 280 | + |
| 281 | +uniformGammaParams: |
| 282 | + ; Positive gamma |
| 283 | + gamma_params 129, 125, 121, 83, 65, 45, 41, 10, 49, 38, 13, 16, 6, 43, 20, 11, 1, 3 |
| 284 | + ; Negative gamma |
| 285 | + gamma_params 129, 125, 121, 85, 64, 45, 41, 10, 49, 38, 14, 17, 7, 43, 20, 11, 0, 3 |
0 commit comments