Separate i2c from jshardware.c - still arduino lib

This commit is contained in:
Rhys Williams 2017-01-05 19:09:59 +13:00
parent 75d1ddba01
commit e0d6802bf5
4 changed files with 120 additions and 72 deletions

View File

@ -1004,6 +1004,7 @@ ifdef USE_NET
targets/esp32/jswrap_esp32.c
INCLUDE += -I$(ROOT)/libs/network/esp32
SOURCES += libs/network/esp32/network_esp32.c \
targets/esp32/i2c.c \
targets/esp32/jshardwareUart.c \
targets/esp32/jshardwareAnalog.c
ifdef RTOS
@ -1784,7 +1785,8 @@ endif
CCPREFIX=xtensa-esp32-elf-
SOURCES += targets/esp32/main.c
LDFLAGS +=-L$(ESP_IDF_PATH)/lib -L$(ESP_IDF_PATH)/ld -L$(ESP_IDF_PATH)/components/bt/lib \
-L$(ESP_IDF_PATH)/components/esp32/lib \
#-L$(ESP_IDF_PATH)/components/esp32/lib \
-L$(ESP_APP_TEMPLATE_PATH)/components/esp32/lib \
-L$(ESP_APP_TEMPLATE_PATH)/build/bootloader \
-L$(ESP_APP_TEMPLATE_PATH)/build/bt \
-L$(ESP_APP_TEMPLATE_PATH)/build/driver \
@ -1811,6 +1813,7 @@ ESPTOOL?=
INCLUDE+=\
-I$(ESP_APP_TEMPLATE_PATH)/build/include \
-I$(ESP_IDF_PATH)/components \
-I$(ESP_IDF_PATH)/components \
-I$(ESP_IDF_PATH)/components/newlib/include \
-I$(ESP_IDF_PATH)/components/bt/include \
-I$(ESP_IDF_PATH)/components/driver/include \
@ -1829,7 +1832,9 @@ INCLUDE+=\
-I$(ESP_APP_TEMPLATE_PATH)/components/arduino-esp32/cores/esp32 \
-Itargets/esp32/include
LDFLAGS+=-nostdlib -u call_user_start_cpu0 -Wl,--gc-sections -Wl,-static -Wl,-EL
LIBS+=-T esp32_out.ld \
#LIBS+=-T$(ESP_IDF_PATH)/components/esp32/ld/esp32_out.ld \
#LIBS+=-T$(ESP_APP_TEMPLATE_PATH)/components/arduino-esp32/tools/sdk/ld/esp32_out.ld \
LIBS+=-T$(ESP_APP_TEMPLATE_PATH)/build/esp32/esp32_out.ld \
-T$(ESP_IDF_PATH)/components/esp32/ld/esp32.common.ld \
-T$(ESP_IDF_PATH)/components/esp32/ld/esp32.rom.ld \
-T$(ESP_IDF_PATH)/components/esp32/ld/esp32.peripherals.ld \

84
targets/esp32/i2c.c Normal file
View File

@ -0,0 +1,84 @@
/*
* This file is designed to support i2c functions in Espruino,
* a JavaScript interpreter for Microcontrollers designed by Gordon Williams
*
* Copyright (C) 2016 by Rhys Williams (wilberforce)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* ----------------------------------------------------------------------------
* This file is designed to be parsed during the build process
*
* Contains ESP32 board specific functions.
* ----------------------------------------------------------------------------
*/
#include "esp_log.h"
#include "i2c.h"
// Using arduino library
#include "esp32-hal-i2c.h"
// Let's get this working with only one device
i2c_t * i2c=NULL;
/** Set-up I2C master for ESP32, default pins are SCL:21, SDA:22. Only device I2C1 is supported
* and only master mode. */
void jshI2CSetup(IOEventFlags device, JshI2CInfo *info) {
if (device != EV_I2C1) {
jsError("Only I2C1 supported");
return;
}
Pin scl = info->pinSCL != PIN_UNDEFINED ? info->pinSCL : 21;
Pin sda = info->pinSDA != PIN_UNDEFINED ? info->pinSDA : 22;
//jshPinSetState(scl, JSHPINSTATE_I2C);
//jshPinSetState(sda, JSHPINSTATE_I2C);
int num=1; // Master mode only 0 is slave mode..
i2c_err_t err;
i2c = i2cInit(num, 0, false);
//jsError("jshI2CSetup: Frequency: %d", info->bitrate);
err=i2cSetFrequency(i2c, (uint32_t)info->bitrate);
if ( err != I2C_ERROR_OK ) {
jsError( "jshI2CSetup: i2cSetFrequency error: %d", err);
return;
}
err=i2cAttachSDA(i2c, pinToESP32Pin(sda));
if ( err != I2C_ERROR_OK ) {
jsError( "jshI2CSetup: i2cAttachSDA error: %d", err);
return;
}
err=i2cAttachSCL(i2c, pinToESP32Pin(scl));
if ( err != I2C_ERROR_OK ) {
jsError( "jshI2CSetup: i2cAttachSCL error: %d", err);
return;
}
}
void jshI2CWrite(IOEventFlags device,
unsigned char address,
int nBytes,
const unsigned char *data,
bool sendStop) {
// i2cWrite(i2c_t * i2c, uint16_t address, bool addr_10bit, uint8_t * data, uint8_t len, bool sendStop);
i2c_err_t err=i2cWrite(i2c,address,false,data,nBytes,sendStop);
if ( err != I2C_ERROR_OK ) {
jsError( "jshI2CSetup: i2cAttachSCL error: %d", err);
return;
}
}
void jshI2CRead(IOEventFlags device,
unsigned char address,
int nBytes,
unsigned char *data,
bool sendStop) {
i2c_err_t err=i2cRead(i2c,address,false,data,nBytes,sendStop);
if ( err != I2C_ERROR_OK ) {
jsError( "jshI2CSetup: i2cAttachSCL error: %d", err);
return;
}
}

27
targets/esp32/i2c.h Normal file
View File

@ -0,0 +1,27 @@
/*
* This file is designed to support i2c functions in Espruino for ESP32,
* a JavaScript interpreter for Microcontrollers designed by Gordon Williams
*
* Copyright (C) 2016 by Rhys Williams (wilberforce)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* ----------------------------------------------------------------------------
* This file is designed to be parsed during the build process
*
* Contains ESP32 board specific functions.
* ----------------------------------------------------------------------------
*/
#include "jspininfo.h"
#include "jshardware.h"
#include "driver/gpio.h"
// Convert an Espruino pin to an ESP32 pin number.
gpio_num_t pinToESP32Pin(Pin pin);
void jshI2CSetup(IOEventFlags device, JshI2CInfo *info);
void jshI2CWrite(IOEventFlags device, unsigned char address, int nBytes, const unsigned char *data, bool sendStop);
void jshI2CRead(IOEventFlags device, unsigned char address, int nBytes, unsigned char *data, bool sendStop);

View File

@ -43,8 +43,8 @@
#include "rom/uart.h"
#include "driver/gpio.h"
#include "i2c.h"
#include "esp32-hal-spi.h"
#include "esp32-hal-i2c.h"
#define FLASH_MAX (4*1024*1024) //4MB
#define FLASH_PAGE_SHIFT 12 // Shift is much faster than division by 4096 (size of page)
@ -59,8 +59,6 @@ static char *tagGPIO = "jshardware(GPIO)";
static spi_t * _spi[VSPI];
static uint32_t g_lastSPIRead = (uint32_t)-1;
// Convert an Espruino pin to an ESP32 pin number.
static gpio_num_t pinToESP32Pin(Pin pin);
/**
* Convert a pin id to the corresponding Pin Event id.
*/
@ -659,72 +657,6 @@ void jshSPISetReceive(IOEventFlags device, bool isReceive) {
ESP_LOGD(tag,"<< jshSPISetReceive");
}
//===== I2C =====
// Let's get this working with only one device
i2c_t * i2c=NULL;
/** Set-up I2C master for ESP32, default pins are SCL:21, SDA:22. Only device I2C1 is supported
* and only master mode. */
void jshI2CSetup(IOEventFlags device, JshI2CInfo *info) {
if (device != EV_I2C1) {
jsError("Only I2C1 supported");
return;
}
Pin scl = info->pinSCL != PIN_UNDEFINED ? info->pinSCL : 21;
Pin sda = info->pinSDA != PIN_UNDEFINED ? info->pinSDA : 22;
//jshPinSetState(scl, JSHPINSTATE_I2C);
//jshPinSetState(sda, JSHPINSTATE_I2C);
int num=1; // Master mode only 0 is slave mode..
i2c_err_t err;
i2c = i2cInit(num, 0, false);
//ESP_LOGE(tag, "jshI2CSetup: Frequency: %d", info->bitrate);
err=i2cSetFrequency(i2c, (uint32_t)info->bitrate);
if ( err != I2C_ERROR_OK ) {
ESP_LOGE(tag, "jshI2CSetup: i2cSetFrequency error: %d", err);
return;
}
err=i2cAttachSDA(i2c, pinToESP32Pin(sda));
if ( err != I2C_ERROR_OK ) {
ESP_LOGE(tag, "jshI2CSetup: i2cAttachSDA error: %d", err);
return;
}
err=i2cAttachSCL(i2c, pinToESP32Pin(scl));
if ( err != I2C_ERROR_OK ) {
ESP_LOGE(tag, "jshI2CSetup: i2cAttachSCL error: %d", err);
return;
}
}
void jshI2CWrite(IOEventFlags device,
unsigned char address,
int nBytes,
const unsigned char *data,
bool sendStop) {
// i2cWrite(i2c_t * i2c, uint16_t address, bool addr_10bit, uint8_t * data, uint8_t len, bool sendStop);
i2c_err_t err=i2cWrite(i2c,address,false,data,nBytes,sendStop);
if ( err != I2C_ERROR_OK ) {
ESP_LOGE(tag, "jshI2CSetup: i2cAttachSCL error: %d", err);
return;
}
}
void jshI2CRead(IOEventFlags device,
unsigned char address,
int nBytes,
unsigned char *data,
bool sendStop) {
i2c_err_t err=i2cRead(i2c,address,false,data,nBytes,sendStop);
if ( err != I2C_ERROR_OK ) {
ESP_LOGE(tag, "jshI2CSetup: i2cAttachSCL error: %d", err);
return;
}
}
//===== System time stuff =====
/**
@ -914,7 +846,7 @@ unsigned int jshSetSystemClock(JsVar *options) {
/**
* Convert an Espruino pin id to a native ESP32 pin id.
*/
static gpio_num_t pinToESP32Pin(Pin pin) {
gpio_num_t pinToESP32Pin(Pin pin) {
if ( pin < 40 )
return pin + GPIO_NUM_0;
ESP_LOGE(tag, "pinToESP32Pin: Unknown pin: %d", pin);