st7735s: signs of life
[cortex-from-scratch] / drivers / st7735s.c
1 /* (CC-BY-NC-SA) ROBIN KRENS - ROBIN @ ROBINKRENS.NL
2  * 
3  * $LOG$
4  * 2019/9/14 - ROBIN KRENS      
5  * Initial version 
6  * 
7  * $DESCRIPTION$
8  * 
9  * $USAGE$
10  *
11  * */
12
13 #include <stdbool.h>
14 #include <stddef.h>
15 #include <stdint.h>
16
17 #include <sys/mmap.h>
18 #include <sys/robsys.h>
19
20 #include <lib/regfunc.h>
21 #include <lib/string.h>
22 #include <lib/tinyprintf.h>
23
24 #include <drivers/st7735s.h>
25
26 void tft_command();
27 void tft_init() {
28
29         /* Peripherial init */
30         rsetbit(RCC_APB1ENR, 14); // enable SPI2
31         rsetbit(RCC_APB2ENR, 3); // enable GPIOB
32
33         /* The PINS used are respective PB12, PB13 and PB15 
34          * NSS (or CS): alternative function pusp-pull
35          * NSS Output is (always high) enabled with this setting
36          * SCK Master: alternate function push-pull
37          * MOSI (or DI): alternate function push-pull */ 
38         rwrite(GPIOB_CRH, 0xA4AA4444);
39
40         /* Chip select: software enabled */
41         rsetbit(SPI2_CR1, 9);
42         rsetbit(SPI2_CR1, 8);
43
44         // LOW and first edge: standard settings        
45
46         rsetbit(SPI2_CR1, 15); // one-wire mode
47         rsetbit(SPI2_CR1, 14); // start with transfer
48         rsetbit(SPI2_CR1, 4); // FPLCK div 8
49         rsetbit(SPI2_CR1, 2); // master selection
50         rsetbit(SPI2_CR1, 6); // enable SPI
51
52         tft_command();  
53
54 }
55
56
57 void tft_command() {
58
59         /* 9-bit hack */
60         uint8_t cmd = 0x0A;
61
62         rwrite(SPI2_DR, cmd);
63         while (!rchkbit(SPI2_SR, 1));
64         rclrbit(SPI2_CR1, 14); // receive
65 }