hd44780: port library to STM32F4

This commit is contained in:
King Kévin 2022-05-10 10:49:03 +02:00
parent dba7f71154
commit d40972200d
2 changed files with 68 additions and 44 deletions

View File

@ -2,7 +2,7 @@
* @file
* @author King Kévin <kingkevin@cuvoodoo.info>
* @copyright SPDX-License-Identifier: GPL-3.0-or-later
* @date 2019-2020
* @date 2019-2022
* @note peripherals used: GPIO @ref lcd_hd44780_gpio
*/
/* standard libraries */
@ -121,27 +121,27 @@ static uint8_t lcd_hd44780_i2c_output = 0xff;
* @note this enables read back data, but more importantly read the busy flag to know when the controller finished processing the command. Tying R/nW to ground instead of a GPIO saves a pin, but no data can be read back. Also every command needs to wait a minimum time before being able to send the next one, even if the controlled might actually already have processed it and is not busy anymore.
* @note is pulled up by HD44780
*/
#define LCD_HD44780_GPIO_RnW PB10
#define LCD_HD44780_GPIO_RnW PB8
/** enable pin
* @warning needs an external 10k pull-up resistor
*/
#define LCD_HD44780_GPIO_E PB11
#define LCD_HD44780_GPIO_E PB7
/** data bit 7
* @note pulled up by HD44780
*/
#define LCD_HD44780_GPIO_DB7 PB12
#define LCD_HD44780_GPIO_DB7 PB3
/** data bit 6
* @note pulled up by HD44780
*/
#define LCD_HD44780_GPIO_DB6 PB13
#define LCD_HD44780_GPIO_DB6 PB4
/** data bit 5
* @note pulled up by HD44780
*/
#define LCD_HD44780_GPIO_DB5 PB14
#define LCD_HD44780_GPIO_DB5 PB5
/** data bit 4
* @note pulled up by HD44780
*/
#define LCD_HD44780_GPIO_DB4 PB15
#define LCD_HD44780_GPIO_DB4 PB6
/** data bit 3
* @note pulled up by HD44780
* @note leave undefined when only 4-bit mode is used
@ -448,6 +448,7 @@ static uint8_t lcd_hd44780_read(bool data)
return input;
#else // LCD_HD44780_GPIO_RnW || LCD_HD44780_I2C
(void)data; // not used
return 0; // read is not supported
#endif
}
@ -619,43 +620,67 @@ bool lcd_hd44780_setup(bool n_2lines, bool f_5x10)
return false;
}
#else // LCD_HD44780_I2C
// enable all GPIO
rcc_periph_clock_enable(GPIO_RCC(LCD_HD44780_GPIO_E)); // enable clock for GPIO port
gpio_clear(GPIO_PORT(LCD_HD44780_GPIO_E), GPIO_PIN(LCD_HD44780_GPIO_E)); // start idle low
gpio_set_mode(GPIO_PORT(LCD_HD44780_GPIO_E), GPIO_MODE_OUTPUT_10_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO_PIN(LCD_HD44780_GPIO_E)); // set GPIO as output
rcc_periph_clock_enable(GPIO_RCC(LCD_HD44780_GPIO_DB7)); // enable clock for GPIO port
// enable all GPIO
rcc_periph_clock_enable(GPIO_RCC(LCD_HD44780_GPIO_E)); // enable clock for GPIO
gpio_clear(GPIO_PORT(LCD_HD44780_GPIO_E), GPIO_PIN(LCD_HD44780_GPIO_E)); // idle low
gpio_mode_setup(GPIO_PORT(LCD_HD44780_GPIO_E), GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN(LCD_HD44780_GPIO_E)); // set pin as output
gpio_set_output_options(GPIO_PORT(LCD_HD44780_GPIO_E), GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN(LCD_HD44780_GPIO_E)); // set output as open-drain
rcc_periph_clock_enable(GPIO_RCC(LCD_HD44780_GPIO_DB7)); // enable clock for GPIO
gpio_set(GPIO_PORT(LCD_HD44780_GPIO_DB7), GPIO_PIN(LCD_HD44780_GPIO_DB7)); // idle high
gpio_set_mode(GPIO_PORT(LCD_HD44780_GPIO_DB7), GPIO_MODE_OUTPUT_10_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO_PIN(LCD_HD44780_GPIO_DB7)); // open drain allows to read and write
rcc_periph_clock_enable(GPIO_RCC(LCD_HD44780_GPIO_DB6)); // enable clock for GPIO port
gpio_mode_setup(GPIO_PORT(LCD_HD44780_GPIO_DB7), GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN(LCD_HD44780_GPIO_DB7)); // set pin as output
gpio_set_output_options(GPIO_PORT(LCD_HD44780_GPIO_DB7), GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN(LCD_HD44780_GPIO_DB7)); // set output as open-drain
rcc_periph_clock_enable(GPIO_RCC(LCD_HD44780_GPIO_DB6)); // enable clock for GPIO
gpio_set(GPIO_PORT(LCD_HD44780_GPIO_DB6), GPIO_PIN(LCD_HD44780_GPIO_DB6)); // idle high
gpio_set_mode(GPIO_PORT(LCD_HD44780_GPIO_DB6), GPIO_MODE_OUTPUT_10_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO_PIN(LCD_HD44780_GPIO_DB6)); // open drain allows to read and write
rcc_periph_clock_enable(GPIO_RCC(LCD_HD44780_GPIO_DB5)); // enable clock for GPIO port
gpio_mode_setup(GPIO_PORT(LCD_HD44780_GPIO_DB6), GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN(LCD_HD44780_GPIO_DB6)); // set pin as output
gpio_set_output_options(GPIO_PORT(LCD_HD44780_GPIO_DB6), GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN(LCD_HD44780_GPIO_DB6)); // set output as open-drain
rcc_periph_clock_enable(GPIO_RCC(LCD_HD44780_GPIO_DB5)); // enable clock for GPIO
gpio_set(GPIO_PORT(LCD_HD44780_GPIO_DB5), GPIO_PIN(LCD_HD44780_GPIO_DB5)); // idle high
gpio_set_mode(GPIO_PORT(LCD_HD44780_GPIO_DB5), GPIO_MODE_OUTPUT_10_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO_PIN(LCD_HD44780_GPIO_DB5)); // open drain allows to read and write
rcc_periph_clock_enable(GPIO_RCC(LCD_HD44780_GPIO_DB4)); // enable clock for GPIO port
gpio_mode_setup(GPIO_PORT(LCD_HD44780_GPIO_DB5), GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN(LCD_HD44780_GPIO_DB5)); // set pin as output
gpio_set_output_options(GPIO_PORT(LCD_HD44780_GPIO_DB5), GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN(LCD_HD44780_GPIO_DB5)); // set output as open-drain
rcc_periph_clock_enable(GPIO_RCC(LCD_HD44780_GPIO_DB4)); // enable clock for GPIO
gpio_set(GPIO_PORT(LCD_HD44780_GPIO_DB4), GPIO_PIN(LCD_HD44780_GPIO_DB4)); // idle high
gpio_set_mode(GPIO_PORT(LCD_HD44780_GPIO_DB4), GPIO_MODE_OUTPUT_10_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO_PIN(LCD_HD44780_GPIO_DB4)); // open drain allows to read and write
gpio_mode_setup(GPIO_PORT(LCD_HD44780_GPIO_DB4), GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN(LCD_HD44780_GPIO_DB4)); // set pin as output
gpio_set_output_options(GPIO_PORT(LCD_HD44780_GPIO_DB4), GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN(LCD_HD44780_GPIO_DB4)); // set output as open-drain
#if defined(LCD_HD44780_INTERFACE_DL) && LCD_HD44780_INTERFACE_DL
rcc_periph_clock_enable(GPIO_RCC(LCD_HD44780_GPIO_DB3)); // enable clock for GPIO port
rcc_periph_clock_enable(GPIO_RCC(LCD_HD44780_GPIO_DB3)); // enable clock for GPIO
gpio_set(GPIO_PORT(LCD_HD44780_GPIO_DB3), GPIO_PIN(LCD_HD44780_GPIO_DB3)); // idle high
gpio_set_mode(GPIO_PORT(LCD_HD44780_GPIO_DB3), GPIO_MODE_OUTPUT_10_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO_PIN(LCD_HD44780_GPIO_DB3)); // open drain allows to read and write
rcc_periph_clock_enable(GPIO_RCC(LCD_HD44780_GPIO_DB2)); // enable clock for GPIO port
gpio_mode_setup(GPIO_PORT(LCD_HD44780_GPIO_DB3), GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN(LCD_HD44780_GPIO_DB3)); // set pin as output
gpio_set_output_options(GPIO_PORT(LCD_HD44780_GPIO_DB3), GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN(LCD_HD44780_GPIO_DB3)); // set output as open-drain
rcc_periph_clock_enable(GPIO_RCC(LCD_HD44780_GPIO_DB2)); // enable clock for GPIO
gpio_set(GPIO_PORT(LCD_HD44780_GPIO_DB2), GPIO_PIN(LCD_HD44780_GPIO_DB2)); // idle high
gpio_set_mode(GPIO_PORT(LCD_HD44780_GPIO_DB2), GPIO_MODE_OUTPUT_10_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO_PIN(LCD_HD44780_GPIO_DB2)); // open drain allows to read and write
rcc_periph_clock_enable(GPIO_RCC(LCD_HD44780_GPIO_DB1)); // enable clock for GPIO port
gpio_mode_setup(GPIO_PORT(LCD_HD44780_GPIO_DB2), GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN(LCD_HD44780_GPIO_DB2)); // set pin as output
gpio_set_output_options(GPIO_PORT(LCD_HD44780_GPIO_DB2), GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN(LCD_HD44780_GPIO_DB2)); // set output as open-drain
rcc_periph_clock_enable(GPIO_RCC(LCD_HD44780_GPIO_DB1)); // enable clock for GPIO
gpio_set(GPIO_PORT(LCD_HD44780_GPIO_DB1), GPIO_PIN(LCD_HD44780_GPIO_DB1)); // idle high
gpio_set_mode(GPIO_PORT(LCD_HD44780_GPIO_DB1), GPIO_MODE_OUTPUT_10_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO_PIN(LCD_HD44780_GPIO_DB1)); // open drain allows to read and write
rcc_periph_clock_enable(GPIO_RCC(LCD_HD44780_GPIO_DB0)); // enable clock for GPIO port
gpio_mode_setup(GPIO_PORT(LCD_HD44780_GPIO_DB1), GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN(LCD_HD44780_GPIO_DB1)); // set pin as output
gpio_set_output_options(GPIO_PORT(LCD_HD44780_GPIO_DB1), GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN(LCD_HD44780_GPIO_DB1)); // set output as open-drain
rcc_periph_clock_enable(GPIO_RCC(LCD_HD44780_GPIO_DB0)); // enable clock for GPIO
gpio_set(GPIO_PORT(LCD_HD44780_GPIO_DB0), GPIO_PIN(LCD_HD44780_GPIO_DB0)); // idle high
gpio_set_mode(GPIO_PORT(LCD_HD44780_GPIO_DB0), GPIO_MODE_OUTPUT_10_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO_PIN(LCD_HD44780_GPIO_DB0)); // open drain allows to read and write
gpio_mode_setup(GPIO_PORT(LCD_HD44780_GPIO_DB0), GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN(LCD_HD44780_GPIO_DB0)); // set pin as output
gpio_set_output_options(GPIO_PORT(LCD_HD44780_GPIO_DB0), GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN(LCD_HD44780_GPIO_DB0)); // set output as open-drain
#endif // LCD_HD44780_INTERFACE_DL
rcc_periph_clock_enable(GPIO_RCC(LCD_HD44780_GPIO_RS)); // enable clock for GPIO port
rcc_periph_clock_enable(GPIO_RCC(LCD_HD44780_GPIO_RS)); // enable clock for GPIO
gpio_clear(GPIO_PORT(LCD_HD44780_GPIO_RS), GPIO_PIN(LCD_HD44780_GPIO_RS)); // set low to read busy flag
gpio_set_mode(GPIO_PORT(LCD_HD44780_GPIO_RS), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO_PIN(LCD_HD44780_GPIO_RS)); // set GPIO as output
gpio_mode_setup(GPIO_PORT(LCD_HD44780_GPIO_RS), GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN(LCD_HD44780_GPIO_RS)); // set pin as output
gpio_set_output_options(GPIO_PORT(LCD_HD44780_GPIO_RS), GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ, GPIO_PIN(LCD_HD44780_GPIO_RS)); // set output as open-drain
#ifdef LCD_HD44780_GPIO_RnW
rcc_periph_clock_enable(GPIO_RCC(LCD_HD44780_GPIO_RnW)); // enable clock for GPIO port
rcc_periph_clock_enable(GPIO_RCC(LCD_HD44780_GPIO_RnW)); // enable clock for GPIO
gpio_set(GPIO_PORT(LCD_HD44780_GPIO_RnW), GPIO_PIN(LCD_HD44780_GPIO_RnW)); // set high to read
gpio_set_mode(GPIO_PORT(LCD_HD44780_GPIO_RnW), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO_PIN(LCD_HD44780_GPIO_RnW)); // set GPIO as output
gpio_mode_setup(GPIO_PORT(LCD_HD44780_GPIO_RnW), GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN(LCD_HD44780_GPIO_RnW)); // set pin as output
gpio_set_output_options(GPIO_PORT(LCD_HD44780_GPIO_RnW), GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ, GPIO_PIN(LCD_HD44780_GPIO_RnW)); // set output as open-drain
#endif // LCD_HD44780_GPIO_RnW
#endif // LCD_HD44780_I2C
// initialize device
@ -690,21 +715,21 @@ void lcd_hd44780_release(void)
i2c_master_release(LCD_HD44780_I2C_PERIPH); // release I²C peripheral
#else // LCD_HD44780_I2C
// switch back GPIO back to input
gpio_set_mode(GPIO_PORT(LCD_HD44780_GPIO_DB7), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_PIN(LCD_HD44780_GPIO_DB7));
gpio_set_mode(GPIO_PORT(LCD_HD44780_GPIO_DB6), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_PIN(LCD_HD44780_GPIO_DB6));
gpio_set_mode(GPIO_PORT(LCD_HD44780_GPIO_DB5), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_PIN(LCD_HD44780_GPIO_DB5));
gpio_set_mode(GPIO_PORT(LCD_HD44780_GPIO_DB4), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_PIN(LCD_HD44780_GPIO_DB4));
gpio_mode_setup(GPIO_PORT(LCD_HD44780_GPIO_DB7), GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN(LCD_HD44780_GPIO_DB7));
gpio_mode_setup(GPIO_PORT(LCD_HD44780_GPIO_DB6), GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN(LCD_HD44780_GPIO_DB6));
gpio_mode_setup(GPIO_PORT(LCD_HD44780_GPIO_DB5), GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN(LCD_HD44780_GPIO_DB5));
gpio_mode_setup(GPIO_PORT(LCD_HD44780_GPIO_DB4), GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN(LCD_HD44780_GPIO_DB4));
#if defined(LCD_HD44780_INTERFACE_DL) && LCD_HD44780_INTERFACE_DL
gpio_set_mode(GPIO_PORT(LCD_HD44780_GPIO_DB3), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_PIN(LCD_HD44780_GPIO_DB3));
gpio_set_mode(GPIO_PORT(LCD_HD44780_GPIO_DB2), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_PIN(LCD_HD44780_GPIO_DB2));
gpio_set_mode(GPIO_PORT(LCD_HD44780_GPIO_DB1), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_PIN(LCD_HD44780_GPIO_DB1));
gpio_set_mode(GPIO_PORT(LCD_HD44780_GPIO_DB0), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_PIN(LCD_HD44780_GPIO_DB0));
gpio_mode_setup(GPIO_PORT(LCD_HD44780_GPIO_DB3), GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN(LCD_HD44780_GPIO_DB3));
gpio_mode_setup(GPIO_PORT(LCD_HD44780_GPIO_DB2), GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN(LCD_HD44780_GPIO_DB2));
gpio_mode_setup(GPIO_PORT(LCD_HD44780_GPIO_DB1), GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN(LCD_HD44780_GPIO_DB1));
gpio_mode_setup(GPIO_PORT(LCD_HD44780_GPIO_DB0), GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN(LCD_HD44780_GPIO_DB0));
#endif // LCD_HD44780_INTERFACE_DL
gpio_set_mode(GPIO_PORT(LCD_HD44780_GPIO_RS), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_PIN(LCD_HD44780_GPIO_RS));
gpio_mode_setup(GPIO_PORT(LCD_HD44780_GPIO_RS), GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN(LCD_HD44780_GPIO_RS));
#ifdef LCD_HD44780_GPIO_RnW
gpio_set_mode(GPIO_PORT(LCD_HD44780_GPIO_RnW), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_PIN(LCD_HD44780_GPIO_RnW));
gpio_mode_setup(GPIO_PORT(LCD_HD44780_GPIO_RnW), GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN(LCD_HD44780_GPIO_RnW));
#endif // LCD_HD44780_GPIO_RnW
gpio_set_mode(GPIO_PORT(LCD_HD44780_GPIO_E), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_PIN(LCD_HD44780_GPIO_E));
gpio_mode_setup(GPIO_PORT(LCD_HD44780_GPIO_E), GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN(LCD_HD44780_GPIO_E));
#endif // LCD_HD44780_I2C:
}

View File

@ -2,11 +2,10 @@
* @file
* @author King Kévin <kingkevin@cuvoodoo.info>
* @copyright SPDX-License-Identifier: GPL-3.0-or-later
* @date 2019-2020
* @date 2019-2022
* @note peripherals used: GPIO @ref lcd_hd44780_gpio, I²C @ref lcd_hd44780_i2c
*/
#pragma once
#error not converted for STM32F4
#include <libopencm3/stm32/i2c.h> // I²C definitions
/** @defgroup lcd_hd44780_i2c I²C peripheral used to control backpack adapter for the HD44780