GLCD Library
A C Library for Embedded Applications
 All Data Structures Files Functions Variables Enumerations Enumerator Macros Groups Pages
STM32F10x.c
Go to the documentation of this file.
1 
9 /*
10  Copyright (c) 2012, Andy Gock
11 
12  All rights reserved.
13 
14  Redistribution and use in source and binary forms, with or without
15  modification, are permitted provided that the following conditions are met:
16  * Redistributions of source code must retain the above copyright
17  notice, this list of conditions and the following disclaimer.
18  * Redistributions in binary form must reproduce the above copyright
19  notice, this list of conditions and the following disclaimer in the
20  documentation and/or other materials provided with the distribution.
21  * Neither the name of Andy Gock nor the
22  names of its contributors may be used to endorse or promote products
23  derived from this software without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
26  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  DISCLAIMED. IN NO EVENT SHALL ANDY GOCK BE LIABLE FOR ANY
29  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36 #if defined(GLCD_DEVICE_STM32F10X)
37 
38 #include "STM32F10x.h"
39 
40 void glcd_init(void)
41 {
42 
43 #if defined(GLCD_CONTROLLER_PCD8544)
44  /* Initialisation for PCD8544 controller */
45 
46  /* Need to make start up the correct peripheral clocks */
47  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE);
48 
49  /* Set up GPIO pins */
50  GPIO_InitTypeDef GPIO_InitStructure;
51 
52  /* SS pin */
53  GPIO_InitStructure.GPIO_Pin = CONTROLLER_SPI_SS_PIN;
54  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
55  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
56  GPIO_Init(CONTROLLER_SPI_SS_PORT, &GPIO_InitStructure);
57 
58  /* DC pin */
59  GPIO_InitStructure.GPIO_Pin = CONTROLLER_SPI_DC_PIN;
60  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
61  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
62  GPIO_Init(CONTROLLER_SPI_DC_PORT, &GPIO_InitStructure);
63 
64  /* RESET pin */
65  GPIO_InitStructure.GPIO_Pin = CONTROLLER_SPI_RST_PIN;
66  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
67  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
68  GPIO_Init(CONTROLLER_SPI_RST_PORT, &GPIO_InitStructure);
69 
70  /* Make sure chip is de-selected by default */
71  GLCD_DESELECT();
72 
73  /* Set up GPIO for SPI pins */
74  GPIO_InitStructure.GPIO_Pin = CONTROLLER_SPI_SCK_PIN | CONTROLLER_SPI_MISO_PIN | CONTROLLER_SPI_MOSI_PIN;
75  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
76  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
77  GPIO_Init(CONTROLLER_SPI_PORT, &GPIO_InitStructure);
78 
79  /* Initialise SPI */
80  SPI_InitTypeDef SPI_InitStructure;
81  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
82  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
83  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
84  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
85  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
86  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
87  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32; /* Set clock speed! */
88  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
89  SPI_InitStructure.SPI_CRCPolynomial = 7;
90  SPI_Init(CONTROLLER_SPI_NUMBER, &SPI_InitStructure);
91 
92  SPI_I2S_ITConfig(CONTROLLER_SPI_NUMBER, SPI_I2S_IT_RXNE, ENABLE);
93 
94  /* Enable SPI */
95  SPI_Cmd(CONTROLLER_SPI_NUMBER, ENABLE);
96 
97  /* Initialisation sequence of controller */
100 #else
101  #error "Controller not supported by STM32F100x"
102 #endif
103 
104 }
105 
106 void glcd_spi_write(uint8_t c)
107 {
108 
109  GLCD_SELECT();
110  SPI_I2S_SendData(CONTROLLER_SPI_NUMBER, (uint16_t) data);
111 
112  /* Wait until entire byte has been read (which we discard anyway) */
113  while( SPI_I2S_GetFlagStatus(CONTROLLER_SPI_NUMBER,SPI_I2S_FLAG_RXNE) != SET );
114 
115  GLCD_SELECT();
116 }
117 
118 void glcd_reset(void)
119 {
120  GLCD_SELECT();
121  GLCD_RESET_LOW();
122  delay_ms(GLCD_RESET_TIME);
123  GLCD_RESET_HIGH();
124  GLCD_DESELECT();
125 }
126 
127 #endif
void glcd_spi_write(uint8_t c)
Write a byte to the connected SPI slave.
void glcd_reset(void)
Reset the LCD.
Device implementation for ST STM32F10x ARM Cortex-M3 MCUs Requires the use of ST's Standard Periphera...
#define GLCD_RESET_TIME
Reset duration by glcd_reset(), in milliseconds.
Definition: glcd.h:156
void glcd_init(void)
Initialise the LCD.