From 94101a2cd9baed60360f68096033dc75cbb1dcb3 Mon Sep 17 00:00:00 2001 From: android <android@lingyun.com> Date: Wed, 10 Jul 2024 19:27:36 +0800 Subject: [PATCH] Add STM8S003F3 eDP LCD drvier --- mcu_sdk/stm8s003f3_edp/Libraries/STM8S_StdPeriph_Driver/src/gpio_i2c.c | 260 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 260 insertions(+), 0 deletions(-) diff --git a/mcu_sdk/stm8s003f3_edp/Libraries/STM8S_StdPeriph_Driver/src/gpio_i2c.c b/mcu_sdk/stm8s003f3_edp/Libraries/STM8S_StdPeriph_Driver/src/gpio_i2c.c new file mode 100644 index 0000000..fcfd58c --- /dev/null +++ b/mcu_sdk/stm8s003f3_edp/Libraries/STM8S_StdPeriph_Driver/src/gpio_i2c.c @@ -0,0 +1,260 @@ +/********************************************************************************* + * Copyright: (C) 2014 Guo Wenxue<guowenxue@gmail.com> + * All rights reserved. + * + * Filename: gpio_i2c.c + * Description: STM8 GPIOģ��I2C���� + * + * Version: 1.0.0(09/28/2014) + * Author: Guo Wenxue <guowenxue@gmail.com> + * ChangeLog: 1, Release initial version on "09/28/2014 10:58:17 PM" + * + ********************************************************************************/ + +#include "gpio_i2c.h" +#include "timing_delay.h" + +/* ��ʼ�źţ�SCLΪ�ߵ�ƽʱ��SDA�ɸߵ�ƽ��͵�ƽ���䣬��ʼ��������. */ +void gpio_i2c_start(void) +{ + SDA_MODE_OUT; + + SCL_LOW; + TIMDelay_N10us(1); + SDA_HIGH; + TIMDelay_N10us(1); + SCL_HIGH; + TIMDelay_N10us(1); + + SDA_LOW; + TIMDelay_N10us(1); + + SCL_LOW; /* Start֮��BUS����һ�㱻��Ϊ����ռ��״̬ */ + TIMDelay_N10us(1); +} + +/* �����źţ�SCLΪ�ߵ�ƽʱ��SDA�ɵ͵�ƽ��ߵ�ƽ���䣬������������. */ +void gpio_i2c_stop(void) +{ + SDA_MODE_OUT; + + SCL_LOW; + TIMDelay_N10us(1); + SDA_LOW; + TIMDelay_N10us(1); + + SCL_HIGH; + TIMDelay_N10us(1); + + SDA_HIGH; + TIMDelay_N10us(1); +} + +/* ���ڷ�����ЧӦ��λACK��Ҫ���ǣ��������ڵ�9��ʱ������֮ǰ�� + *�͵�ƽ�ڼ佫SDA�����ͣ�����ȷ���ڸ�ʱ�ӵĸߵ�ƽ�ڼ�Ϊ�ȶ��ĵ͵�ƽ + */ +void gpio_i2c_send_ack(void) +{ + SDA_MODE_OUT; + + /* Ĭ��SCL�ǵ͵�ƽ�����ڸı����ݣ��͵�ƽ��ʾACK */ + SDA_LOW; + TIMDelay_N10us(1); + + /* ʱ�������߲�ά��SDA״̬ */ + SCL_HIGH; + TIMDelay_N10us(1); + + /* �ָ�ʱ����Ϊ�͵�ƽ������æ */ + SCL_LOW; + TIMDelay_N10us(1); +} + +/* NACK(���������ߵĵ�ƽ����)����ʾ���ݳɹ�ʧ�� */ +void gpio_i2c_send_nak(void) +{ + SDA_MODE_OUT; + + /* Ĭ��SCL�ǵ͵�ƽ�����ڸı����ݣ��ߵ�ƽ��ʾNAK */ + SDA_HIGH; + TIMDelay_N10us(1); + + /* ʱ�������߲�ά��SDA״̬ */ + SCL_HIGH; + TIMDelay_N10us(1); + + /* �ָ�ʱ����Ϊ�͵�ƽ������æ */ + SCL_LOW; + TIMDelay_N10us(1); +} + +/* I2C ���߶�һ��ACK�ź� + * ����ֵ:0 �յ�ACK 1: �յ�NAK + */ +uint8_t gpio_i2c_wait_ack(void) +{ + uint8_t rv; + uint8_t times = 200; + + SDA_MODE_IN; + + /* ʱ�������� */ + SCL_HIGH; + TIMDelay_N10us(1); + + do + { + times --; + } while ( (SDA_DAT==1) && (times!=0) ); + + if( !times ) + { + /* �ȴ� ACK ��ʱ�����յ�NAK */ + rv = I2C_NAK; + goto OUT; + } + + /* �ȵ�ACK */ + SCL_LOW; + TIMDelay_N10us(1); + + rv = I2C_ACK; + +OUT: + return rv; +} + +/* I2C���߷���һ���ֽ�����: + * ����ֵ:0 ���ͳɹ����ȵ�ACK 1: ����ʧ��,�յ�NAK + */ +uint8_t gpio_i2c_write_byte(uint8_t byte) +{ + int i; + uint8_t rv = I2C_ACK; + + SDA_MODE_OUT; + + /* SCLΪ�͵�ƽʱ,SDA�߲��ܸı䴫���bit; SCLΪ�ߵ�ƽʱ,SDA�߱����ȶ� */ + for(i=7; i>=0; i--) /* MSB */ + { + /*MSB: �����Ӧλ�ĵ�ƽ�ź� */ + if( byte&(1<<i) ) + SDA_HIGH; + else + SDA_LOW; + TIMDelay_N10us(1); + + SCL_HIGH; + TIMDelay_N10us(1); + + SCL_LOW; + TIMDelay_N10us(1); + } + + rv = gpio_i2c_wait_ack(); + return rv; +} + +/* I2C���߽���һ���ֽ����� */ +uint8_t gpio_i2c_read_byte(void) +{ + uint8_t bit; + uint8_t byte = 0; + + SDA_MODE_IN; + + for(bit=0; bit<8; bit++) + { + byte <<= 1; + SCL_HIGH; + TIMDelay_N10us(2); + + if(SDA_DAT) + byte |= 1; + + SCL_LOW; + TIMDelay_N10us(2); + } + + gpio_i2c_send_ack(); + return byte; +} + +uint8_t gpio_i2c_read(uint8_t addr, uint8_t reg_addr) +{ + int rv; + uint8_t data = 0; + + // Start condition + gpio_i2c_start(); + + // Send the slave address with write bit and check for ACK + if (I2C_ACK != gpio_i2c_write_byte(addr | I2C_DIR_WRITE)) + { + rv = I2C_ERROR; + goto OUT; + } + + // Send the register address and check for ACK + if (I2C_ACK != gpio_i2c_write_byte(reg_addr)) + { + rv = I2C_ERROR; + goto OUT; + } + + // Repeated start condition + gpio_i2c_start(); + + // Send the slave address with read bit and check for ACK + if (I2C_ACK != gpio_i2c_write_byte(addr | I2C_DIR_READ)) + { + rv = I2C_ERROR; + goto OUT; + } + + // Read the data byte + data = gpio_i2c_read_byte(); + + rv = data; + +OUT: + gpio_i2c_stop(); + if (rv == I2C_ERROR) + { + return -1; + } + return rv; +} + + + +int gpio_i2c_write(uint8_t addr, uint8_t reg_addr, uint8_t data) +{ + int rv = I2C_OK; + + gpio_i2c_start(); + + // Send the slave device address with the write bit and check for ACK + if (I2C_ACK != gpio_i2c_write_byte(addr | I2C_DIR_WRITE)) + { + rv = I2C_ERROR; + goto OUT; + } + + // Send the register address and check for ACK + if (I2C_ACK != gpio_i2c_write_byte(reg_addr)) + { + rv = I2C_ERROR; + goto OUT; + } + + // Send the data byte and check for ACK + if (I2C_ACK != gpio_i2c_write_byte(data)) + { + rv = I2C_ERROR; + } + +OUT: + gpio_i2c_stop(); + return rv; +} -- Gitblit v1.9.1