FlexBook / Mbed 2 deprecated FlexBook171204a

Dependencies:   SDFileSystem app epson mbed msp430 pl tests

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers epdpsu.c Source File

epdpsu.c

00001 /*
00002   Plastic Logic EPD project on MSP430
00003 
00004   Copyright (C) 2014 Plastic Logic Limited
00005 
00006   This program is free software: you can redistribute it and/or modify
00007   it under the terms of the GNU General Public License as published by
00008   the Free Software Foundation, either version 3 of the License, or
00009   (at your option) any later version.
00010 
00011   This program is distributed in the hope that it will be useful,
00012   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014   GNU General Public License for more details.
00015 
00016   You should have received a copy of the GNU General Public License
00017   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 /*
00020  * epdpsu.c -- EPD PSU generic implementation
00021  *
00022  * Authors:
00023  *   Guillaume Tucker <guillaume.tucker@plasticlogic.com>
00024  *
00025  */
00026 
00027 #include <pl/epdpsu.h>
00028 #include <pl/gpio.h>
00029 #include <pl/epdc.h>
00030 #include "assert.h"
00031 
00032 #define LOG_TAG "epdpsu"
00033 #include "utils.h"
00034 
00035 /* Set to 1 to enable verbose logging */
00036 #define LOG_VERBOSE 0
00037 
00038 /* --- GPIO --- */
00039 
00040 static int pl_epdpsu_gpio_on(struct pl_epdpsu *psu)
00041 {
00042     struct pl_epdpsu_gpio *p = psu->data;
00043     unsigned timeout;
00044 
00045     if (psu->state)
00046         return 0;
00047 
00048 #if LOG_VERBOSE
00049     LOG("on");
00050 #endif
00051 
00052     pl_gpio_set(p->gpio, p->hv_en, 1);
00053 
00054     for (timeout = p->timeout_ms; timeout; timeout--) {
00055         if (pl_gpio_get(p->gpio, p->pok))
00056             break;
00057         mdelay(1);
00058     }
00059 
00060     if (!timeout) {
00061         LOG("POK timeout");
00062         pl_gpio_set(p->gpio, p->hv_en, 0);
00063         return -1;
00064     }
00065 
00066     pl_gpio_set(p->gpio, p->com_close, 1);
00067     msleep(p->on_delay_ms);
00068     psu->state = 1;
00069 
00070     return 0;
00071 }
00072 
00073 static int pl_epdpsu_gpio_off(struct pl_epdpsu *psu)
00074 {
00075     struct pl_epdpsu_gpio *p = psu->data;
00076 
00077 #if LOG_VERBOSE
00078     LOG("off");
00079 #endif
00080 
00081     pl_gpio_set(p->gpio, p->com_close, 0);
00082     pl_gpio_set(p->gpio, p->hv_en, 0);
00083     msleep(p->off_delay_ms);
00084     psu->state = 0;
00085 
00086     return 0;
00087 }
00088 
00089 int pl_epdpsu_gpio_init(struct pl_epdpsu *psu, struct pl_epdpsu_gpio *p)
00090 {
00091     assert(psu != NULL);
00092     assert(p != NULL);
00093     assert(p->gpio != NULL);
00094     assert(p->timeout_ms != 0);
00095     assert(p->hv_en != PL_GPIO_NONE);
00096     assert(p->com_close != PL_GPIO_NONE);
00097     assert(p->pok != PL_GPIO_NONE);
00098     assert(p->flt != PL_GPIO_NONE);
00099 
00100     psu->on = pl_epdpsu_gpio_on;
00101     psu->off = pl_epdpsu_gpio_off;
00102     psu->state = 0;
00103     psu->data = p;
00104 
00105     return 0;
00106 }
00107 
00108 /* --- EPDC --- */
00109 
00110 static int pl_epdpsu_epdc_on(struct pl_epdpsu *psu)
00111 {
00112     struct pl_epdc *epdc = psu->data;
00113 
00114     if (!psu->state) {
00115         if (epdc->set_epd_power(epdc, 1))
00116             return -1;
00117 
00118         psu->state = 1;
00119     }
00120 
00121     return 0;
00122 }
00123 
00124 static int pl_epdpsu_epdc_off(struct pl_epdpsu *psu)
00125 {
00126     struct pl_epdc *epdc = psu->data;
00127 
00128     if (psu->state) {
00129         if (epdc->set_epd_power(epdc, 0))
00130             return -1;
00131 
00132         psu->state = 0;
00133     }
00134 
00135     return 0;
00136 }
00137 
00138 int pl_epdpsu_epdc_init(struct pl_epdpsu *psu, struct pl_epdc *epdc)
00139 {
00140     psu->on = pl_epdpsu_epdc_on;
00141     psu->off = pl_epdpsu_epdc_off;
00142     psu->state = 0;
00143     psu->data = epdc;
00144 
00145     return 0;
00146 }
00147 
00148 #if PL_EPDPSU_STUB
00149 
00150 /* --- Stub --- */
00151 
00152 static int pl_epdpsu_stub_on(struct pl_epdpsu *psu)
00153 {
00154 #if LOG_VERBOSE
00155     LOG("stub on");
00156 #endif
00157 
00158     psu->state = 1;
00159 
00160     return 0;
00161 }
00162 
00163 static int pl_epdpsu_stub_off(struct pl_epdpsu *psu)
00164 {
00165 #if LOG_VERBOSE
00166     LOG("stub off");
00167 #endif
00168 
00169     psu->state = 0;
00170 
00171     return 0;
00172 }
00173 
00174 int pl_epdpsu_stub_init(struct pl_epdpsu *psu)
00175 {
00176     assert(psu != NULL);
00177 
00178     psu->on = pl_epdpsu_stub_on;
00179     psu->off = pl_epdpsu_stub_off;
00180     psu->state = 0;
00181     psu->data = NULL;
00182 
00183     return 0;
00184 }
00185 
00186 #endif /* PL_EPDPSU_STUB */