<commit_before>#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>

#include <aery32/gpio.h>
#include <aery32/spi.h>
#include "board.h"
#include <aery32/delay.h>

// ----------------------------------------------------------------------
// HD44780 instruction set
// ----------------------------------------------------------------------
#define HD44780_CLEAR_DISPLAY       0x01
#define HD44780_RETURN_HOME         0x02

#define HD44780_EMODE_INCREMENT     0x06
#define HD44780_EMODE_DECREMENT     0x04
#define HD44780_EMODE_INCRNSHIFT    0x07
#define HD44780_EMODE_DECRNSHIFT    0x05

#define HD44780_DISPLAY_ON          0x0C
#define HD44780_DISPLAY_OFF         0x08
#define HD44780_CURSOR_ON           0x0E
#define HD44780_CURSOR_ONBLINK      0x0F

#define HD44780_LSHIFT_CURSOR       0x10
#define HD44780_RSHIFT_CURSOR       0x14
#define HD44780_LSHIFT_DISPLAY      0x18
#define HD44780_RSHIFT_DISPLAY      0x1C

#define HD44780_DL4BIT              0x28
#define HD44780_DL8BIT              0x38
#define HD44780_FONTBL_ENJA         0x28
#define HD44780_FONTBL_WE1          0x29
#define HD44780_FONTBL_ENRU         0x2A
#define HD44780_FONTBL_WE2          0x2B

#define HD44780_DDRAM_ADDR          0x80
#define HD44780_CGRAM_ADDR          0x40

#define HD44780_BUSYBIT_MASK        0x80


// ----------------------------------------------------------------------
// Additional board settings
// ----------------------------------------------------------------------
#define LED                 AVR32_PIN_PC04
#define SPI0_GPIO_MASK      ((1 << 10) | (1 << 11) | (1 << 12) | (1 << 13))

#define DISPLAY_SPI         spi0
#define DISPLAY_SPI_NPCS    0   
#define DISPLAY_SPI_MODE    SPI_MODE3


// ----------------------------------------------------------------------
// Display functions
// ----------------------------------------------------------------------
bool
display_isbusy(void)
{
	uint16_t rd; /* read data */

	aery_spi_transmit(DISPLAY_SPI, 0x100, DISPLAY_SPI_NPCS, false);
	rd = aery_spi_transmit(DISPLAY_SPI, 0x100, DISPLAY_SPI_NPCS, true);
	return (HD44780_BUSYBIT_MASK & (rd >> 2)) != 0;
}

void
display_instruct(uint16_t instruction)
{
	if (instruction == HD44780_CLEAR_DISPLAY) {
		aery_delay_ms(3);
	} else {
		aery_delay_ms(1);
	}
	aery_spi_transmit(DISPLAY_SPI, instruction, DISPLAY_SPI_NPCS, true);
}

void
display_wrbyte(uint8_t byte)
{
	aery_delay_ms(1);
	aery_spi_transmit(DISPLAY_SPI, 0x200|byte, DISPLAY_SPI_NPCS, true);
}

void
display_putc(char c)
{
	display_wrbyte((uint8_t) c);
}

int
display_puts(const char *buf)<commit_msg>Use display_isbusy() in hd44780 example<commit_after>#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>

#include <aery32/gpio.h>
#include <aery32/spi.h>
#include "board.h"
#include <aery32/delay.h>

// ----------------------------------------------------------------------
// HD44780 instruction set
// ----------------------------------------------------------------------
#define HD44780_CLEAR_DISPLAY       0x01
#define HD44780_RETURN_HOME         0x02

#define HD44780_EMODE_INCREMENT     0x06
#define HD44780_EMODE_DECREMENT     0x04
#define HD44780_EMODE_INCRNSHIFT    0x07
#define HD44780_EMODE_DECRNSHIFT    0x05

#define HD44780_DISPLAY_ON          0x0C
#define HD44780_DISPLAY_OFF         0x08
#define HD44780_CURSOR_ON           0x0E
#define HD44780_CURSOR_ONBLINK      0x0F

#define HD44780_LSHIFT_CURSOR       0x10
#define HD44780_RSHIFT_CURSOR       0x14
#define HD44780_LSHIFT_DISPLAY      0x18
#define HD44780_RSHIFT_DISPLAY      0x1C

#define HD44780_DL4BIT              0x28
#define HD44780_DL8BIT              0x38
#define HD44780_FONTBL_ENJA         0x28
#define HD44780_FONTBL_WE1          0x29
#define HD44780_FONTBL_ENRU         0x2A
#define HD44780_FONTBL_WE2          0x2B

#define HD44780_DDRAM_ADDR          0x80
#define HD44780_CGRAM_ADDR          0x40

#define HD44780_BUSYBIT_MASK        0x80


// ----------------------------------------------------------------------
// Additional board settings
// ----------------------------------------------------------------------
#define LED                 AVR32_PIN_PC04
#define SPI0_GPIO_MASK      ((1 << 10) | (1 << 11) | (1 << 12) | (1 << 13))

#define DISPLAY_SPI         spi0
#define DISPLAY_SPI_NPCS    0   
#define DISPLAY_SPI_MODE    SPI_MODE3


// ----------------------------------------------------------------------
// Display functions
// ----------------------------------------------------------------------
bool
display_isbusy(void)
{
	uint16_t rd; /* read data */

	aery_spi_transmit(DISPLAY_SPI, 0x100, DISPLAY_SPI_NPCS, false);
	rd = aery_spi_transmit(DISPLAY_SPI, 0x100, DISPLAY_SPI_NPCS, true);
	return ((HD44780_BUSYBIT_MASK << 2) & rd) != 0;
}

void
display_wait(void)
{
	while (display_isbusy()) {
		aery_delay_us(600);
	}
}

void
display_instruct(uint16_t instruction)
{
	display_wait();
	aery_spi_transmit(DISPLAY_SPI, instruction, DISPLAY_SPI_NPCS, true);
}

void
display_wrbyte(uint8_t byte)
{
	display_wait();
	aery_spi_transmit(DISPLAY_SPI, 0x200|byte, DISPLAY_SPI_NPCS, true);
}

void
display_putc(char c)
{
	display_wrbyte((uint8_t) c);
}

int
display_puts(const char *buf)
{'fraction_non_alphanumeric': 0.2345465049104564, 'fraction_numerical': 0.09474292316580012, 'mean_word_length': 1.9000558347292016, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6482383', 'n_tokens_mistral': 2083, 'n_tokens_neox': 1818, 'n_words': 333}