GLCD Library
A C Library for Embedded Applications
 All Data Structures Files Functions Variables Enumerations Enumerator Macros Groups Pages
STM32F0xx.c
Go to the documentation of this file.
1 
7 /*
8  Copyright (c) 2012, Andy Gock
9 
10  All rights reserved.
11 
12  Redistribution and use in source and binary forms, with or without
13  modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright
15  notice, this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright
17  notice, this list of conditions and the following disclaimer in the
18  documentation and/or other materials provided with the distribution.
19  * Neither the name of Andy Gock nor the
20  names of its contributors may be used to endorse or promote products
21  derived from this software without specific prior written permission.
22 
23  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  DISCLAIMED. IN NO EVENT SHALL ANDY GOCK BE LIABLE FOR ANY
27  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34 
35 #if defined(GLCD_DEVICE_STM32F0XX)
36 
37 /* Includes from CMSIS and Peripheral Library */
38 #include "stm32f0xx.h"
39 #include "stm32f0xx_gpio.h"
40 #include "stm32f0xx_spi.h"
41 #include "stm32f0xx_rcc.h"
42 #include "stm32f0xx_misc.h"
43 
44 /* Includes from GLCD */
45 #include "../glcd.h"
46 #include "inc/STM32F0xx.h"
47 
48 void glcd_init(void)
49 {
50 
51 #if defined(GLCD_CONTROLLER_PCD8544)
52  /* Initialisation for PCD8544 controller */
53 
54  /* Declare GPIO and SPI init structures */
55  GPIO_InitTypeDef GPIO_InitStructure;
56  SPI_InitTypeDef SPI_InitStructure;
57  //NVIC_InitTypeDef NVIC_InitStructure;
58 
59  /* Initialise structures (which we will overide later) */
60  GPIO_StructInit(&GPIO_InitStructure);
61  SPI_StructInit(&SPI_InitStructure);
62 
63  /* Need to make start up the correct peripheral clocks */
64  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
65 
66  /* SS pin */
67  GPIO_InitStructure.GPIO_Pin = CONTROLLER_SPI_SS_PIN;
68  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
69  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
70  GPIO_Init(CONTROLLER_SPI_SS_PORT, &GPIO_InitStructure);
71 
72  /* DC pin */
73  GPIO_InitStructure.GPIO_Pin = CONTROLLER_SPI_DC_PIN;
74  GPIO_Init(CONTROLLER_SPI_DC_PORT, &GPIO_InitStructure);
75 
76  /* RESET pin */
77  GPIO_InitStructure.GPIO_Pin = CONTROLLER_SPI_RST_PIN;
78  GPIO_Init(CONTROLLER_SPI_RST_PORT, &GPIO_InitStructure);
79 
80  /* Make sure chip is de-selected by default */
81  GLCD_DESELECT();
82 
83  /* Set up GPIO for SPI pins */
84  GPIO_InitStructure.GPIO_Pin = CONTROLLER_SPI_SCK_PIN | CONTROLLER_SPI_MISO_PIN | CONTROLLER_SPI_MOSI_PIN;
85  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
86  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
87  GPIO_Init(CONTROLLER_SPI_PORT, &GPIO_InitStructure);
88 
89  /* Configure alternate function mode for SPI pins */
90  GPIO_PinAFConfig(GPIOA,CONTROLLER_SPI_SCK_PINSRC,GPIO_AF_0);
91  GPIO_PinAFConfig(GPIOA,CONTROLLER_SPI_MOSI_PINSRC,GPIO_AF_0);
92  GPIO_PinAFConfig(GPIOA,CONTROLLER_SPI_MISO_PINSRC,GPIO_AF_0);
93 
94  /* Initialise SPI */
95  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
96  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
97  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
98  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
99  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
100  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
101  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32; /* Set clock speed! */
102  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
103  SPI_InitStructure.SPI_CRCPolynomial = 7;
104  SPI_Init(CONTROLLER_SPI_NUMBER, &SPI_InitStructure);
105 
106  /* Enable SPI interupts */
107  /*
108  SPI_I2S_ITConfig(CONTROLLER_SPI_NUMBER, SPI_I2S_IT_TXE, ENABLE);
109  NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn;
110  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
111  NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00;
112  NVIC_Init(&NVIC_InitStructure);
113  */
114 
115  /* Enable SPI */
116  SPI_Cmd(CONTROLLER_SPI_NUMBER, ENABLE);
117 
118  /* Initialisation sequence of controller */
119  glcd_reset();
120 
121  /* Get into the EXTENDED mode! */
123 
124  /* LCD bias select (4 is optimal?) */
126 
127  /* Set VOP */
128  glcd_command(PCD8544_SET_VOP | 50); // Experimentally determined
129 
130  /* Back to standard instructions */
132 
133  /* Normal mode */
135 
137 
138  glcd_set_contrast(50);
139 
140  glcd_clear();
141 
142 
143 #else
144  #error "Controller not supported by STM32F0xx"
145 #endif
146 
147 }
148 
149 void glcd_spi_write(uint8_t c)
150 {
151 
152  GLCD_SELECT();
153  SPI_SendData8(CONTROLLER_SPI_NUMBER, (uint16_t) c);
154 
155  /* Wait until entire byte has been read (which we discard anyway) */
156  while(SPI_I2S_GetFlagStatus(CONTROLLER_SPI_NUMBER, SPI_I2S_FLAG_BSY) != RESET);
157 
158  GLCD_DESELECT();
159 }
160 
161 void glcd_reset(void)
162 {
163  /* Toggle RST low to reset. Minimum pulse 100ns on datasheet. */
164  GLCD_SELECT();
165  GLCD_RESET_LOW();
166  delay_ms(GLCD_RESET_TIME);
167  GLCD_RESET_HIGH();
168  GLCD_DESELECT();
169 }
170 
171 #endif
#define PCD8544_FUNCTION_SET
Definition: PCD8544.h:43
void glcd_spi_write(uint8_t c)
Write a byte to the connected SPI slave.
#define PCD8544_SET_VOP
Definition: PCD8544.h:75
void glcd_select_screen(uint8_t *buffer, glcd_BoundingBox_t *bbox)
Select screen buffer and bounding box structure.
Definition: glcd.c:133
void glcd_reset(void)
Reset the LCD.
#define PCD8544_EXTENDED_INSTRUCTION
Definition: PCD8544.h:47
#define PCD8544_DISPLAY_NORMAL
Definition: PCD8544.h:56
glcd_BoundingBox_t glcd_bbox
Keeps track of bounding box of area on LCD which need to be updated next reresh cycle.
Definition: glcd.c:55
#define PCD8544_DISPLAY_CONTROL
Definition: PCD8544.h:54
#define PCD8544_SET_BIAS
Definition: PCD8544.h:74
void glcd_command(uint8_t c)
Send command byte to LCD.
Functions specific to STM32 F0 ARM Cortex-M0 devices.
void glcd_clear(void)
Clear the display.
Definition: glcd.c:122
void glcd_set_contrast(uint8_t val)
Set contrast.
uint8_t glcd_buffer[GLCD_LCD_WIDTH *GLCD_LCD_HEIGHT/8]
Screen buffer.
Definition: glcd.c:49
#define GLCD_RESET_TIME
Reset duration by glcd_reset(), in milliseconds.
Definition: glcd.h:156
void glcd_init(void)
Initialise the LCD.