1 /*
2   ******************************************************************************
3   * @file    HAL_LCD.c
4   * @version V1.0.0
5   * @date    2020
6   * @brief   LCD HAL module driver.
7   *          This file provides firmware functions to manage the following
8   *          functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (LCD).
9   *           @ Initialization and de-initialization functions
10   *           @ IO operation functions
11   *           @ Peripheral Control functions
12   ******************************************************************************
13 */
14 #include "ACM32Fxx_HAL.h"
15 
16 /*********************************************************************************
17 * Function    : HAL_LCD_MspInit
18 * Description : Initialize the LCD MSP.
19 * Input       : hcan : pointer to a LCD structure that contains
20 *                      the configuration information for LCD module
21 * Output      :
22 * Author      : CWT                         Data : 2020年
23 **********************************************************************************/
HAL_LCD_MspInit(LCD_HandleTypeDef * hlcd)24 __weak void HAL_LCD_MspInit(LCD_HandleTypeDef *hlcd)
25 {
26 
27     /* NOTE : This function only enable lcd clk and config NVIC
28     Because lcd's SEG and COM is different,so the gpio of lcd need user config self.*/
29 
30     /* Enable LCD clock */
31     System_Module_Enable(EN_LCD);
32     /* Enable the LCD  Frame interrupt */
33     hlcd->Instance->CR1 |= LCD_CR1_IE;
34     NVIC_ClearPendingIRQ(LCD_IRQn);
35     NVIC_SetPriority(LCD_IRQn, 5);
36     NVIC_EnableIRQ(LCD_IRQn);
37 }
38 
39 /*********************************************************************************
40 * Function    : HAL_LCD_MspDeInit
41 * Description : DeInitialize the LCD MSP.
42 * Input       : hcan : pointer to a LCD structure that contains
43 *                      the configuration information for LCD module
44 * Output      :
45 * Author      : CWT                         Data : 2020年
46 **********************************************************************************/
HAL_LCD_MspDeInit(LCD_HandleTypeDef * hlcd)47 void HAL_LCD_MspDeInit(LCD_HandleTypeDef *hlcd)
48 {
49     /* Disable LCD clock */
50     System_Module_Disable(EN_LCD);
51 }
52 /*********************************************************************************
53 * Function    : HAL_LCD_Init
54 * Description : Initialize the LCD.
55 * Input       : hcan : pointer to a LCD structure that contains
56 *                      the configuration information for LCD module
57 * Output      :
58 * Author      : CWT                         Data : 2020年
59 **********************************************************************************/
HAL_LCD_Init(LCD_HandleTypeDef * hlcd)60 HAL_StatusTypeDef HAL_LCD_Init(LCD_HandleTypeDef *hlcd)
61 {
62     /* Check the parameters */
63     if(!IS_LCD_PERIPH(hlcd->Instance)) return HAL_ERROR;
64     if(!IS_LCD_DUTY(hlcd->Init.Duty)) return HAL_ERROR;
65     if(!IS_LCD_BIAS(hlcd->Init.Bias)) return HAL_ERROR;
66     if(!IS_LCD_Driving_Waveform(hlcd->Init.Driving_Waveform)) return HAL_ERROR;
67     if(!IS_LCD_BiasSrc(hlcd->Init.BiasSrc)) return HAL_ERROR;
68     if(!IS_LCD_DisplayMode(hlcd->Init.DisplayMode)) return HAL_ERROR;
69 
70     if(!IS_LCD_LCDFrequency(hlcd->Init.LCDFrequency)) return HAL_ERROR;
71     if(!IS_LCD_BlinkEN(hlcd->Init.BlinkEN)) return HAL_ERROR;
72     if(!IS_LCD_BlinkFrequency(hlcd->Init.BlinkFrequency)) return HAL_ERROR;
73 
74     /* Reset the RST_LCD */
75     System_Module_Reset(RST_LCD);
76     HAL_LCD_MspInit(hlcd);
77     hlcd->Instance->CR0|=(LCD_CR0_LCDEN|hlcd->Init.LCDFrequency|hlcd->Init.Bias|hlcd->Init.Duty|hlcd->Init.BiasSrc|hlcd->Init.Driving_Waveform);
78     hlcd->Instance->CR1|=(hlcd->Init.BlinkEN|hlcd->Init.BlinkFrequency|hlcd->Init.DisplayMode);
79     if(hlcd->Init.Driving_Waveform==LCD_Driving_Waveform_A)
80     {
81         hlcd->Instance->CR0&=~(LCD_CR0_WSEL);
82     }
83 
84     if(hlcd->Init.Duty==LCD_DUTY_STATIC)
85     {
86         if(!IS_LCD_StaticPower(hlcd->Init.StaticPower)) return HAL_ERROR;
87         hlcd->Instance->CR0|=hlcd->Init.StaticPower;
88     }
89     else
90     {
91         hlcd->Instance->CR0&=~(LCD_CR0_STATIC);//当DUTY选择非静态时,该位必须设置为0
92     }
93     return HAL_OK;
94 }
95 
96 /*********************************************************************************
97 * Function    : HAL_LCD_DeInit
98 * Description : DeInitialize the LCD.
99 * Input       : hcan : pointer to a LCD structure that contains
100 *                      the configuration information for LCD module
101 * Output      :
102 * Author      : CWT                         Data : 2020年
103 **********************************************************************************/
HAL_LCD_DeInit(LCD_HandleTypeDef * hlcd)104 HAL_StatusTypeDef HAL_LCD_DeInit(LCD_HandleTypeDef *hlcd)
105 {
106     /* Check the parameters */
107     if(!IS_LCD_PERIPH(hlcd->Instance)) return HAL_ERROR;
108     /* Reset the CAN peripheral */
109     CLEAR_BIT(hlcd->Instance->CR0, LCD_CR0_LCDEN);
110 
111     HAL_LCD_MspDeInit(hlcd);
112     /* Return function status */
113     return HAL_OK;
114 }
115 /*********************************************************************************
116 * Function    : HAL_LCD_InResConfig
117 * Description : Initialize the LCD When LCD BiasSrc is LCD_BiasSrc_InRes_Seg31_35_Normal or LCD_BiasSrc_InRes_Seg31_35_Cap.
118 * Input       : hlcd : pointer to a LCD structure that contains
119 *                      the configuration information for LCD module
120 *               LCD_InResInitStruct:LCD_InResInitTypeDef
121 * Output      :
122 * Author      : CWT                         Data : 2020年
123 **********************************************************************************/
HAL_LCD_InResConfig(LCD_HandleTypeDef * hlcd,LCD_InResInitTypeDef * LCD_InResInitStruct)124 HAL_StatusTypeDef HAL_LCD_InResConfig(LCD_HandleTypeDef *hlcd,LCD_InResInitTypeDef* LCD_InResInitStruct)
125 {
126     /* Check the parameters */
127     if(!IS_LCD_PERIPH(hlcd->Instance)) return HAL_ERROR;
128     /* Config when BiasSrc is Inside Resistance Mod  */
129     if(hlcd->Init.BiasSrc!=LCD_BiasSrc_ExRes_Seg31_35_Cap)
130     {
131     /* Check the parameters */
132     if(!IS_LCD_BiasRes(LCD_InResInitStruct->BiasRes)) return HAL_ERROR;
133     if(!IS_LCD_DriveMod(LCD_InResInitStruct->DriveMod)) return HAL_ERROR;
134     if(!IS_LCD_FastCharge(LCD_InResInitStruct->FastCharge)) return HAL_ERROR;
135     if(!IS_LCD_Contrast(LCD_InResInitStruct->Contrast)) return HAL_ERROR;
136     /* Config LCD Contrast and Bias Resistance and DriveMod */
137     hlcd->Instance->CR0|=(LCD_InResInitStruct->Contrast);
138     hlcd->Instance->CR1|=(LCD_InResInitStruct->BiasRes|LCD_InResInitStruct->DriveMod);
139 
140     /* Config LCD PONTime when DriveMod is Fast Charge and Fast Charge(FCC) is Enable. */
141     if(LCD_InResInitStruct->FastCharge==LCD_FastCharge_Enable && LCD_InResInitStruct->DriveMod==LCD_DriveMod_FC)
142     {
143         if(!IS_LCD_PONTime(LCD_InResInitStruct->PONTime)) return HAL_ERROR;
144         hlcd->Instance->CR1|=(LCD_InResInitStruct->PONTime<<18);
145     }
146     }
147     return HAL_OK;
148 }
149 
150 /*********************************************************************************
151 * Function    : HAL_LCD_SegComConfig
152 * Description : Config the LCD SEG and COM enable/disable.
153 * Input       : hlcd : pointer to a LCD structure that contains
154 *                      the configuration information for LCD module
155 *               SegCom:LCD_SegComInitTypeDef
156 * Output      :
157 * Author      : CWT                         Data : 2020年
158 **********************************************************************************/
HAL_LCD_SegComConfig(LCD_HandleTypeDef * hlcd,LCD_SegComInitTypeDef * SegCom)159 HAL_StatusTypeDef HAL_LCD_SegComConfig(LCD_HandleTypeDef *hlcd,LCD_SegComInitTypeDef *SegCom)
160 {
161     /* Check the parameters */
162     if(!IS_LCD_PERIPH(hlcd->Instance)) return HAL_ERROR;
163     hlcd->Instance->LCD_POEN0=SegCom->SEG0_31;
164     hlcd->Instance->LCD_POEN1=SegCom->Stc_SEG32_39_COM0_8.SEG32_39_COM0_8;
165     return HAL_OK;
166 }
167 
168 /*********************************************************************************
169 * Function    : HAL_LCD_Write
170 * Description : Write LCD RAMx.
171 * Input       : hlcd : pointer to a LCD structure that contains
172 *                      the configuration information for LCD module
173 *               LCDRAMIndex:LCD RAM index
174 *               Data:The data you want to write
175 * Output      :
176 * Author      : CWT                         Data : 2020年
177 **********************************************************************************/
HAL_LCD_Write(LCD_HandleTypeDef * hlcd,uint32_t LCDRAMIndex,uint32_t Data)178 HAL_StatusTypeDef HAL_LCD_Write(LCD_HandleTypeDef *hlcd, uint32_t LCDRAMIndex, uint32_t Data)
179 {
180     /* Check the parameters */
181     if(!IS_LCD_PERIPH(hlcd->Instance)) return HAL_ERROR;
182     if(LCDRAMIndex>15) return HAL_ERROR;
183     /* Wrete Data bytes to LCD RAM register */
184     hlcd->Instance->LCD_RAM[LCDRAMIndex]=Data;
185     return HAL_OK;
186 }
187 
188 /*********************************************************************************
189 * Function    : HAL_LCD_Clear
190 * Description : Clear LCD RAMx.
191 * Input       : hlcd : pointer to a LCD structure that contains
192 *                      the configuration information for LCD module
193 * Output      :
194 * Author      : CWT                         Data : 2020年
195 **********************************************************************************/
HAL_LCD_Clear(LCD_HandleTypeDef * hlcd)196 HAL_StatusTypeDef HAL_LCD_Clear(LCD_HandleTypeDef *hlcd)
197 {
198     uint8_t LCDRAMIndex=0;
199     /* Check the parameters */
200     if(!IS_LCD_PERIPH(hlcd->Instance)) return HAL_ERROR;
201     /* Clear the LCD_RAM registers */
202     for(LCDRAMIndex = 0; LCDRAMIndex <= 15; LCDRAMIndex++)
203     {
204         hlcd->Instance->LCD_RAM[LCDRAMIndex] = 0U;
205     }
206     return HAL_OK;
207 }
208 
209 /*********************************************************************************
210 * Function    : HAL_LCD_Start_DMA
211 * Description : Start lcd dma transfer
212 * Input       : hlcd : pointer to a LCD structure that contains
213 *                      the configuration information for LCD module
214 *               pData:The data want to transfer
215 *               Length:transfer Size
216 * Output      :
217 * Author      : CWT                         Data : 2020年
218 **********************************************************************************/
HAL_LCD_Start_DMA(LCD_HandleTypeDef * hlcd,uint32_t * pData,uint32_t Length)219 HAL_StatusTypeDef HAL_LCD_Start_DMA(LCD_HandleTypeDef *hlcd, uint32_t *pData, uint32_t Length)
220 {
221 
222     /* Check the parameters */
223     if(!IS_LCD_PERIPH(hlcd->Instance)) return HAL_ERROR;
224 
225     hlcd->Instance->CR1 |= LCD_CR1_DMAEN;
226 
227     if (HAL_DMA_Start_IT(hlcd->DMA_Handle,(uint32_t)pData,(uint32_t)(&hlcd->Instance->LCD_RAM[0]), Length))
228     {
229         return HAL_ERROR;
230     }
231 
232     return HAL_OK;
233 }
234 
235 /*********************************************************************************
236 * Function    : HAL_LCD_Stop_DMA
237 * Description : Stop lcd dma transfer
238 * Input       : hlcd : pointer to a LCD structure that contains
239 *                      the configuration information for LCD module
240 *               pData:The data want to transfer
241 *               Length:transfer Size
242 * Output      :
243 * Author      : CWT                         Data : 2020年
244 **********************************************************************************/
HAL_LCD_Stop_DMA(LCD_HandleTypeDef * hlcd)245 HAL_StatusTypeDef HAL_LCD_Stop_DMA(LCD_HandleTypeDef *hlcd)
246 {
247     HAL_StatusTypeDef status = HAL_OK;
248     /* Check the parameters */
249     if(!IS_LCD_PERIPH(hlcd->Instance)) return HAL_ERROR;
250 
251     hlcd->Instance->CR1 &=~ LCD_CR1_DMAEN;
252 
253     status = HAL_DMA_Abort(hlcd->DMA_Handle);
254 
255     return status;
256 }
257 
258 /*********************************************************************************
259 * Function    : HAL_LCD_IRQHandler
260 * Description : HAL_LCD_IRQHandler
261 * Input       : hlcd : pointer to a LCD structure that contains
262 *                      the configuration information for LCD module
263 * Output      :
264 * Author      : CWT                         Data : 2020年
265 **********************************************************************************/
HAL_LCD_IRQHandler(LCD_HandleTypeDef * hlcd)266 void HAL_LCD_IRQHandler(LCD_HandleTypeDef *hlcd)
267 {
268     hlcd->Instance->INTCLR &=~ (LCD_INTCLR_INTFT);
269 }
270