1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-21 10:26:06 +03:00

fix UART RX PullUps and TWI

This commit is contained in:
ficeto 2015-05-03 17:51:06 +03:00
parent fcbd7dbed0
commit 3d0dafcbc0
4 changed files with 36 additions and 26 deletions

View File

@ -37,7 +37,7 @@ extern "C" {
#include "binary.h" #include "binary.h"
#include "pgmspace.h" #include "pgmspace.h"
#include "esp8266_peri.h" #include "esp8266_peri.h"
#include "si2c.h" #include "twi.h"
void yield(void); void yield(void);

View File

@ -18,12 +18,12 @@
License along with this library; if not, write to the Free Software License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#include "si2c.h" #include "twi.h"
#include "pins_arduino.h" #include "pins_arduino.h"
#include "wiring_private.h" #include "wiring_private.h"
uint8_t twi_dcount = 18; unsigned char twi_dcount = 18;
static uint8_t twi_sda, twi_scl; static unsigned char twi_sda, twi_scl;
#define SDA_LOW() (GPES = (1 << twi_sda)) //Enable SDA (becomes output and since GPO is 0 for the pin, it will pull the line low) #define SDA_LOW() (GPES = (1 << twi_sda)) //Enable SDA (becomes output and since GPO is 0 for the pin, it will pull the line low)
#define SDA_HIGH() (GPEC = (1 << twi_sda)) //Disable SDA (becomes input and since it has pullup it will go high) #define SDA_HIGH() (GPEC = (1 << twi_sda)) //Disable SDA (becomes input and since it has pullup it will go high)
@ -42,7 +42,7 @@ static uint8_t twi_sda, twi_scl;
#define TWI_CLOCK_STRETCH 400 #define TWI_CLOCK_STRETCH 400
#endif #endif
void twi_setClock(uint32_t freq){ void twi_setClock(unsigned int freq){
#if F_CPU == FCPU80 #if F_CPU == FCPU80
if(freq <= 100000) twi_dcount = 18;//about 100KHz if(freq <= 100000) twi_dcount = 18;//about 100KHz
else if(freq <= 200000) twi_dcount = 8;//about 200KHz else if(freq <= 200000) twi_dcount = 8;//about 200KHz
@ -58,7 +58,7 @@ void twi_setClock(uint32_t freq){
#endif #endif
} }
void twi_init(uint8_t sda, uint8_t scl){ void twi_init(unsigned char sda, unsigned char scl){
twi_sda = sda; twi_sda = sda;
twi_scl = scl; twi_scl = scl;
pinMode(twi_sda, INPUT_PULLUP); pinMode(twi_sda, INPUT_PULLUP);
@ -71,9 +71,9 @@ void twi_stop(void){
pinMode(twi_scl, INPUT); pinMode(twi_scl, INPUT);
} }
static void twi_delay(uint8_t v){ static void twi_delay(unsigned char v){
uint8_t i; unsigned char i;
uint32_t reg; unsigned int reg;
for(i=0;i<v;i++) reg = GPI; for(i=0;i<v;i++) reg = GPI;
} }
@ -88,7 +88,7 @@ static bool twi_write_start(void) {
} }
static bool twi_write_stop(void){ static bool twi_write_stop(void){
uint8_t i = 0; unsigned char i = 0;
SCL_LOW(); SCL_LOW();
SDA_LOW(); SDA_LOW();
twi_delay(twi_dcount); twi_delay(twi_dcount);
@ -102,7 +102,7 @@ static bool twi_write_stop(void){
} }
static bool twi_write_bit(bool bit) { static bool twi_write_bit(bool bit) {
uint8_t i = 0; unsigned char i = 0;
SCL_LOW(); SCL_LOW();
if (bit) SDA_HIGH(); if (bit) SDA_HIGH();
else SDA_LOW(); else SDA_LOW();
@ -114,7 +114,7 @@ static bool twi_write_bit(bool bit) {
} }
static bool twi_read_bit(void) { static bool twi_read_bit(void) {
uint8_t i = 0; unsigned char i = 0;
SCL_LOW(); SCL_LOW();
SDA_HIGH(); SDA_HIGH();
twi_delay(twi_dcount+1); twi_delay(twi_dcount+1);
@ -125,8 +125,8 @@ static bool twi_read_bit(void) {
return bit; return bit;
} }
static bool twi_write_byte(uint8_t byte) { static bool twi_write_byte(unsigned char byte) {
uint8_t bit; unsigned char bit;
for (bit = 0; bit < 8; bit++) { for (bit = 0; bit < 8; bit++) {
twi_write_bit(byte & 0x80); twi_write_bit(byte & 0x80);
byte <<= 1; byte <<= 1;
@ -134,16 +134,16 @@ static bool twi_write_byte(uint8_t byte) {
return twi_read_bit();//NACK/ACK return twi_read_bit();//NACK/ACK
} }
static uint8_t twi_read_byte(bool nack) { static unsigned char twi_read_byte(bool nack) {
uint8_t byte = 0; unsigned char byte = 0;
uint8_t bit; unsigned char bit;
for (bit = 0; bit < 8; bit++) byte = (byte << 1) | twi_read_bit(); for (bit = 0; bit < 8; bit++) byte = (byte << 1) | twi_read_bit();
twi_write_bit(nack); twi_write_bit(nack);
return byte; return byte;
} }
uint8_t twi_writeTo(uint8_t address, uint8_t * buf, uint32_t len, uint8_t sendStop){ unsigned char twi_writeTo(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop){
uint32_t i; unsigned int i;
if(!twi_write_start()) return 4;//line busy if(!twi_write_start()) return 4;//line busy
if(!twi_write_byte(((address << 1) | 0) & 0xFF)) return 2;//received NACK on transmit of address if(!twi_write_byte(((address << 1) | 0) & 0xFF)) return 2;//received NACK on transmit of address
for(i=0; i<len; i++){ for(i=0; i<len; i++){
@ -153,8 +153,8 @@ uint8_t twi_writeTo(uint8_t address, uint8_t * buf, uint32_t len, uint8_t sendSt
return 0; return 0;
} }
uint8_t twi_readFrom(uint8_t address, uint8_t* buf, uint32_t len, uint8_t sendStop){ unsigned char twi_readFrom(unsigned char address, unsigned char* buf, unsigned int len, unsigned char sendStop){
uint32_t i; unsigned int i;
if(!twi_write_start()) return 4;//line busy if(!twi_write_start()) return 4;//line busy
if(!twi_write_byte(((address << 1) | 1) & 0xFF)) return 2;//received NACK on transmit of address if(!twi_write_byte(((address << 1) | 1) & 0xFF)) return 2;//received NACK on transmit of address
for(i=0; i<len; i++) buf[i] = twi_read_byte(false); for(i=0; i<len; i++) buf[i] = twi_read_byte(false);

View File

@ -31,10 +31,12 @@ extern void __pinMode(uint8_t pin, uint8_t mode) {
GPC(pin) = (GPC(pin) & (0xF << GPCI)); //SOURCE(GPIO) | DRIVER(NORMAL) | INT_TYPE(UNCHANGED) | WAKEUP_ENABLE(DISABLED) GPC(pin) = (GPC(pin) & (0xF << GPCI)); //SOURCE(GPIO) | DRIVER(NORMAL) | INT_TYPE(UNCHANGED) | WAKEUP_ENABLE(DISABLED)
GPEC = (1 << pin); //Disable GPEC = (1 << pin); //Disable
GPF(pin) = GPFFS(GPFFS_BUS(pin));//Set mode to BUS (RX0, TX0, TX1, SPI, HSPI or CLK depending in the pin) GPF(pin) = GPFFS(GPFFS_BUS(pin));//Set mode to BUS (RX0, TX0, TX1, SPI, HSPI or CLK depending in the pin)
if(pin == 3) GPF(pin) |= (1 << GPFPU);//enable pullup on RX
} else if(mode & FUNCTION_0){ } else if(mode & FUNCTION_0){
GPC(pin) = (GPC(pin) & (0xF << GPCI)); //SOURCE(GPIO) | DRIVER(NORMAL) | INT_TYPE(UNCHANGED) | WAKEUP_ENABLE(DISABLED) GPC(pin) = (GPC(pin) & (0xF << GPCI)); //SOURCE(GPIO) | DRIVER(NORMAL) | INT_TYPE(UNCHANGED) | WAKEUP_ENABLE(DISABLED)
GPEC = (1 << pin); //Disable GPEC = (1 << pin); //Disable
GPF(pin) = GPFFS((mode >> 4) & 0x07); GPF(pin) = GPFFS((mode >> 4) & 0x07);
if(pin == 13 && mode == FUNCTION_4) GPF(pin) |= (1 << GPFPU);//enable pullup on RX
} else if(mode == OUTPUT){ } else if(mode == OUTPUT){
GPF(pin) = GPFFS(GPFFS_GPIO(pin));//Set mode to GPIO GPF(pin) = GPFFS(GPFFS_GPIO(pin));//Set mode to GPIO
GPC(pin) = (GPC(pin) & (0xF << GPCI)); //SOURCE(GPIO) | DRIVER(NORMAL) | INT_TYPE(UNCHANGED) | WAKEUP_ENABLE(DISABLED) GPC(pin) = (GPC(pin) & (0xF << GPCI)); //SOURCE(GPIO) | DRIVER(NORMAL) | INT_TYPE(UNCHANGED) | WAKEUP_ENABLE(DISABLED)

View File

@ -1,5 +1,5 @@
/* /*
si2c.h - Software I2C library for esp8266 twi.h - Software I2C library for esp8266
Copyright (c) 2015 Hristo Gochkov. All rights reserved. Copyright (c) 2015 Hristo Gochkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment. This file is part of the esp8266 core for Arduino environment.
@ -22,10 +22,18 @@
#define SI2C_h #define SI2C_h
#include "Arduino.h" #include "Arduino.h"
void twi_init(uint8_t sda, uint8_t scl); #ifdef __cplusplus
extern "C" {
#endif
void twi_init(unsigned char sda, unsigned char scl);
void twi_stop(void); void twi_stop(void);
void twi_setClock(uint32_t freq); void twi_setClock(unsigned int freq);
uint8_t twi_writeTo(uint8_t address, uint8_t * buf, uint32_t len, uint8_t sendStop); uint8_t twi_writeTo(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop);
uint8_t twi_readFrom(uint8_t address, uint8_t * buf, uint32_t len, uint8_t sendStop); uint8_t twi_readFrom(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop);
#ifdef __cplusplus
}
#endif
#endif #endif