Commit d77666c0 authored by sergio's avatar sergio Committed by Terry Ellison
Browse files

trailing spaces cleanup (#2659)

parent d7583040
// //
// FILE: dht.cpp // FILE: dht.cpp
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// VERSION: 0.1.14 // VERSION: 0.1.14
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino // PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
// URL: http://arduino.cc/playground/Main/DHTLib // URL: http://arduino.cc/playground/Main/DHTLib
// //
// HISTORY: // HISTORY:
// 0.1.14 replace digital read with faster (~3x) code => more robust low MHz machines. // 0.1.14 replace digital read with faster (~3x) code => more robust low MHz machines.
// 0.1.13 fix negative dht_temperature // 0.1.13 fix negative dht_temperature
// 0.1.12 support DHT33 and DHT44 initial version // 0.1.12 support DHT33 and DHT44 initial version
// 0.1.11 renamed DHTLIB_TIMEOUT // 0.1.11 renamed DHTLIB_TIMEOUT
// 0.1.10 optimized faster WAKEUP + TIMEOUT // 0.1.10 optimized faster WAKEUP + TIMEOUT
// 0.1.09 optimize size: timeout check + use of mask // 0.1.09 optimize size: timeout check + use of mask
// 0.1.08 added formula for timeout based upon clockspeed // 0.1.08 added formula for timeout based upon clockspeed
// 0.1.07 added support for DHT21 // 0.1.07 added support for DHT21
// 0.1.06 minimize footprint (2012-12-27) // 0.1.06 minimize footprint (2012-12-27)
// 0.1.05 fixed negative dht_temperature bug (thanks to Roseman) // 0.1.05 fixed negative dht_temperature bug (thanks to Roseman)
// 0.1.04 improved readability of code using DHTLIB_OK in code // 0.1.04 improved readability of code using DHTLIB_OK in code
// 0.1.03 added error values for temp and dht_humidity when read failed // 0.1.03 added error values for temp and dht_humidity when read failed
// 0.1.02 added error codes // 0.1.02 added error codes
// 0.1.01 added support for Arduino 1.0, fixed typos (31/12/2011) // 0.1.01 added support for Arduino 1.0, fixed typos (31/12/2011)
// 0.1.00 by Rob Tillaart (01/04/2011) // 0.1.00 by Rob Tillaart (01/04/2011)
// //
// inspired by DHT11 library // inspired by DHT11 library
// //
// Released to the public domain // Released to the public domain
// //
#include "user_interface.h" #include "user_interface.h"
#include "platform.h" #include "platform.h"
#include "c_stdio.h" #include "c_stdio.h"
#include "dht.h" #include "dht.h"
#ifndef LOW #ifndef LOW
#define LOW 0 #define LOW 0
#endif /* ifndef LOW */ #endif /* ifndef LOW */
#ifndef HIGH #ifndef HIGH
#define HIGH 1 #define HIGH 1
#endif /* ifndef HIGH */ #endif /* ifndef HIGH */
#define COMBINE_HIGH_AND_LOW_BYTE(byte_high, byte_low) (((byte_high) << 8) | (byte_low)) #define COMBINE_HIGH_AND_LOW_BYTE(byte_high, byte_low) (((byte_high) << 8) | (byte_low))
static double dht_humidity; static double dht_humidity;
static double dht_temperature; static double dht_temperature;
static uint8_t dht_bytes[5]; // buffer to receive data static uint8_t dht_bytes[5]; // buffer to receive data
static int dht_readSensor(uint8_t pin, uint8_t wakeupDelay); static int dht_readSensor(uint8_t pin, uint8_t wakeupDelay);
///////////////////////////////////////////////////// /////////////////////////////////////////////////////
// //
// PUBLIC // PUBLIC
// //
// return values: // return values:
// Humidity // Humidity
double dht_getHumidity(void) double dht_getHumidity(void)
{ {
return dht_humidity; return dht_humidity;
} }
// return values: // return values:
// Temperature // Temperature
double dht_getTemperature(void) double dht_getTemperature(void)
{ {
return dht_temperature; return dht_temperature;
} }
// return values: // return values:
// DHTLIB_OK // DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM // DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT // DHTLIB_ERROR_TIMEOUT
int dht_read_universal(uint8_t pin) int dht_read_universal(uint8_t pin)
{ {
// READ VALUES // READ VALUES
int rv = dht_readSensor(pin, DHTLIB_DHT_UNI_WAKEUP); int rv = dht_readSensor(pin, DHTLIB_DHT_UNI_WAKEUP);
if (rv != DHTLIB_OK) if (rv != DHTLIB_OK)
{ {
dht_humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered? dht_humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
dht_temperature = DHTLIB_INVALID_VALUE; // invalid value dht_temperature = DHTLIB_INVALID_VALUE; // invalid value
return rv; // propagate error value return rv; // propagate error value
} }
#if defined(DHT_DEBUG_BYTES) #if defined(DHT_DEBUG_BYTES)
int i; int i;
for (i = 0; i < 5; i++) for (i = 0; i < 5; i++)
{ {
DHT_DEBUG("%02X\n", dht_bytes[i]); DHT_DEBUG("%02X\n", dht_bytes[i]);
} }
#endif // defined(DHT_DEBUG_BYTES) #endif // defined(DHT_DEBUG_BYTES)
// Assume it is DHT11 // Assume it is DHT11
// If it is DHT11, both bit[1] and bit[3] is 0 // If it is DHT11, both bit[1] and bit[3] is 0
if ((dht_bytes[1] == 0) && (dht_bytes[3] == 0)) if ((dht_bytes[1] == 0) && (dht_bytes[3] == 0))
{ {
// It may DHT11 // It may DHT11
// CONVERT AND STORE // CONVERT AND STORE
DHT_DEBUG("DHT11 method\n"); DHT_DEBUG("DHT11 method\n");
dht_humidity = dht_bytes[0]; // dht_bytes[1] == 0; dht_humidity = dht_bytes[0]; // dht_bytes[1] == 0;
dht_temperature = dht_bytes[2]; // dht_bytes[3] == 0; dht_temperature = dht_bytes[2]; // dht_bytes[3] == 0;
// TEST CHECKSUM // TEST CHECKSUM
// dht_bytes[1] && dht_bytes[3] both 0 // dht_bytes[1] && dht_bytes[3] both 0
uint8_t sum = dht_bytes[0] + dht_bytes[2]; uint8_t sum = dht_bytes[0] + dht_bytes[2];
if (dht_bytes[4] != sum) if (dht_bytes[4] != sum)
{ {
// It may not DHT11 // It may not DHT11
dht_humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered? dht_humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
dht_temperature = DHTLIB_INVALID_VALUE; // invalid value dht_temperature = DHTLIB_INVALID_VALUE; // invalid value
// Do nothing // Do nothing
} }
else else
{ {
return DHTLIB_OK; return DHTLIB_OK;
} }
} }
// Assume it is not DHT11 // Assume it is not DHT11
// CONVERT AND STORE // CONVERT AND STORE
DHT_DEBUG("DHTxx method\n"); DHT_DEBUG("DHTxx method\n");
dht_humidity = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bytes[0], dht_bytes[1]) * 0.1; dht_humidity = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bytes[0], dht_bytes[1]) * 0.1;
dht_temperature = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bytes[2] & 0x7F, dht_bytes[3]) * 0.1; dht_temperature = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bytes[2] & 0x7F, dht_bytes[3]) * 0.1;
if (dht_bytes[2] & 0x80) // negative dht_temperature if (dht_bytes[2] & 0x80) // negative dht_temperature
{ {
dht_temperature = -dht_temperature; dht_temperature = -dht_temperature;
} }
// TEST CHECKSUM // TEST CHECKSUM
uint8_t sum = dht_bytes[0] + dht_bytes[1] + dht_bytes[2] + dht_bytes[3]; uint8_t sum = dht_bytes[0] + dht_bytes[1] + dht_bytes[2] + dht_bytes[3];
if (dht_bytes[4] != sum) if (dht_bytes[4] != sum)
{ {
return DHTLIB_ERROR_CHECKSUM; return DHTLIB_ERROR_CHECKSUM;
} }
return DHTLIB_OK; return DHTLIB_OK;
} }
// return values: // return values:
// DHTLIB_OK // DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM // DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT // DHTLIB_ERROR_TIMEOUT
int dht_read11(uint8_t pin) int dht_read11(uint8_t pin)
{ {
// READ VALUES // READ VALUES
int rv = dht_readSensor(pin, DHTLIB_DHT11_WAKEUP); int rv = dht_readSensor(pin, DHTLIB_DHT11_WAKEUP);
if (rv != DHTLIB_OK) if (rv != DHTLIB_OK)
{ {
dht_humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered? dht_humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
dht_temperature = DHTLIB_INVALID_VALUE; // invalid value dht_temperature = DHTLIB_INVALID_VALUE; // invalid value
return rv; return rv;
} }
// CONVERT AND STORE // CONVERT AND STORE
dht_humidity = dht_bytes[0]; // dht_bytes[1] == 0; dht_humidity = dht_bytes[0]; // dht_bytes[1] == 0;
dht_temperature = dht_bytes[2]; // dht_bytes[3] == 0; dht_temperature = dht_bytes[2]; // dht_bytes[3] == 0;
// TEST CHECKSUM // TEST CHECKSUM
// dht_bytes[1] && dht_bytes[3] both 0 // dht_bytes[1] && dht_bytes[3] both 0
uint8_t sum = dht_bytes[0] + dht_bytes[2]; uint8_t sum = dht_bytes[0] + dht_bytes[2];
if (dht_bytes[4] != sum) return DHTLIB_ERROR_CHECKSUM; if (dht_bytes[4] != sum) return DHTLIB_ERROR_CHECKSUM;
return DHTLIB_OK; return DHTLIB_OK;
} }
// return values: // return values:
// DHTLIB_OK // DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM // DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT // DHTLIB_ERROR_TIMEOUT
int dht_read(uint8_t pin) int dht_read(uint8_t pin)
{ {
// READ VALUES // READ VALUES
int rv = dht_readSensor(pin, DHTLIB_DHT_WAKEUP); int rv = dht_readSensor(pin, DHTLIB_DHT_WAKEUP);
if (rv != DHTLIB_OK) if (rv != DHTLIB_OK)
{ {
dht_humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered? dht_humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
dht_temperature = DHTLIB_INVALID_VALUE; // invalid value dht_temperature = DHTLIB_INVALID_VALUE; // invalid value
return rv; // propagate error value return rv; // propagate error value
} }
// CONVERT AND STORE // CONVERT AND STORE
dht_humidity = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bytes[0], dht_bytes[1]) * 0.1; dht_humidity = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bytes[0], dht_bytes[1]) * 0.1;
dht_temperature = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bytes[2] & 0x7F, dht_bytes[3]) * 0.1; dht_temperature = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bytes[2] & 0x7F, dht_bytes[3]) * 0.1;
if (dht_bytes[2] & 0x80) // negative dht_temperature if (dht_bytes[2] & 0x80) // negative dht_temperature
{ {
dht_temperature = -dht_temperature; dht_temperature = -dht_temperature;
} }
// TEST CHECKSUM // TEST CHECKSUM
uint8_t sum = dht_bytes[0] + dht_bytes[1] + dht_bytes[2] + dht_bytes[3]; uint8_t sum = dht_bytes[0] + dht_bytes[1] + dht_bytes[2] + dht_bytes[3];
if (dht_bytes[4] != sum) if (dht_bytes[4] != sum)
{ {
return DHTLIB_ERROR_CHECKSUM; return DHTLIB_ERROR_CHECKSUM;
} }
return DHTLIB_OK; return DHTLIB_OK;
} }
// return values: // return values:
// DHTLIB_OK // DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM // DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT // DHTLIB_ERROR_TIMEOUT
int dht_read21(uint8_t pin) __attribute__((alias("dht_read"))); int dht_read21(uint8_t pin) __attribute__((alias("dht_read")));
// return values: // return values:
// DHTLIB_OK // DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM // DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT // DHTLIB_ERROR_TIMEOUT
int dht_read22(uint8_t pin) __attribute__((alias("dht_read"))); int dht_read22(uint8_t pin) __attribute__((alias("dht_read")));
// return values: // return values:
// DHTLIB_OK // DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM // DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT // DHTLIB_ERROR_TIMEOUT
int dht_read33(uint8_t pin) __attribute__((alias("dht_read"))); int dht_read33(uint8_t pin) __attribute__((alias("dht_read")));
// return values: // return values:
// DHTLIB_OK // DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM // DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT // DHTLIB_ERROR_TIMEOUT
int dht_read44(uint8_t pin) __attribute__((alias("dht_read"))); int dht_read44(uint8_t pin) __attribute__((alias("dht_read")));
///////////////////////////////////////////////////// /////////////////////////////////////////////////////
// //
// PRIVATE // PRIVATE
// //
// return values: // return values:
// DHTLIB_OK // DHTLIB_OK
// DHTLIB_ERROR_TIMEOUT // DHTLIB_ERROR_TIMEOUT
int dht_readSensor(uint8_t pin, uint8_t wakeupDelay) int dht_readSensor(uint8_t pin, uint8_t wakeupDelay)
{ {
// INIT BUFFERVAR TO RECEIVE DATA // INIT BUFFERVAR TO RECEIVE DATA
uint8_t mask = 128; uint8_t mask = 128;
uint8_t idx = 0; uint8_t idx = 0;
uint8_t i = 0; uint8_t i = 0;
// replace digitalRead() with Direct Port Reads. // replace digitalRead() with Direct Port Reads.
// reduces footprint ~100 bytes => portability issue? // reduces footprint ~100 bytes => portability issue?
// direct port read is about 3x faster // direct port read is about 3x faster
// uint8_t bit = digitalPinToBitMask(pin); // uint8_t bit = digitalPinToBitMask(pin);
// uint8_t port = digitalPinToPort(pin); // uint8_t port = digitalPinToPort(pin);
// volatile uint8_t *PIR = portInputRegister(port); // volatile uint8_t *PIR = portInputRegister(port);
// EMPTY BUFFER // EMPTY BUFFER
for (i = 0; i < 5; i++) dht_bytes[i] = 0; for (i = 0; i < 5; i++) dht_bytes[i] = 0;
// REQUEST SAMPLE // REQUEST SAMPLE
// pinMode(pin, OUTPUT); // pinMode(pin, OUTPUT);
platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_PULLUP); platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_PULLUP);
DIRECT_MODE_OUTPUT(pin); DIRECT_MODE_OUTPUT(pin);
// digitalWrite(pin, LOW); // T-be // digitalWrite(pin, LOW); // T-be
DIRECT_WRITE_LOW(pin); DIRECT_WRITE_LOW(pin);
// delay(wakeupDelay); // delay(wakeupDelay);
for (i = 0; i < wakeupDelay; i++) os_delay_us(1000); for (i = 0; i < wakeupDelay; i++) os_delay_us(1000);
// Disable interrupts // Disable interrupts
ets_intr_lock(); ets_intr_lock();
// digitalWrite(pin, HIGH); // T-go // digitalWrite(pin, HIGH); // T-go
DIRECT_WRITE_HIGH(pin); DIRECT_WRITE_HIGH(pin);
os_delay_us(40); os_delay_us(40);
// pinMode(pin, INPUT); // pinMode(pin, INPUT);
DIRECT_MODE_INPUT(pin); DIRECT_MODE_INPUT(pin);
// GET ACKNOWLEDGE or TIMEOUT // GET ACKNOWLEDGE or TIMEOUT
uint16_t loopCntLOW = DHTLIB_TIMEOUT; uint16_t loopCntLOW = DHTLIB_TIMEOUT;
while (DIRECT_READ(pin) == LOW ) // T-rel while (DIRECT_READ(pin) == LOW ) // T-rel
{ {
os_delay_us(1); os_delay_us(1);
if (--loopCntLOW == 0) return DHTLIB_ERROR_TIMEOUT; if (--loopCntLOW == 0) return DHTLIB_ERROR_TIMEOUT;
} }
uint16_t loopCntHIGH = DHTLIB_TIMEOUT; uint16_t loopCntHIGH = DHTLIB_TIMEOUT;
while (DIRECT_READ(pin) != LOW ) // T-reh while (DIRECT_READ(pin) != LOW ) // T-reh
{ {
os_delay_us(1); os_delay_us(1);
if (--loopCntHIGH == 0) return DHTLIB_ERROR_TIMEOUT; if (--loopCntHIGH == 0) return DHTLIB_ERROR_TIMEOUT;
} }
// READ THE OUTPUT - 40 BITS => 5 BYTES // READ THE OUTPUT - 40 BITS => 5 BYTES
for (i = 40; i != 0; i--) for (i = 40; i != 0; i--)
{ {
loopCntLOW = DHTLIB_TIMEOUT; loopCntLOW = DHTLIB_TIMEOUT;
while (DIRECT_READ(pin) == LOW ) while (DIRECT_READ(pin) == LOW )
{ {
os_delay_us(1); os_delay_us(1);
if (--loopCntLOW == 0) return DHTLIB_ERROR_TIMEOUT; if (--loopCntLOW == 0) return DHTLIB_ERROR_TIMEOUT;
} }
uint32_t t = system_get_time(); uint32_t t = system_get_time();
loopCntHIGH = DHTLIB_TIMEOUT; loopCntHIGH = DHTLIB_TIMEOUT;
while (DIRECT_READ(pin) != LOW ) while (DIRECT_READ(pin) != LOW )
{ {
os_delay_us(1); os_delay_us(1);
if (--loopCntHIGH == 0) return DHTLIB_ERROR_TIMEOUT; if (--loopCntHIGH == 0) return DHTLIB_ERROR_TIMEOUT;
} }
if ((system_get_time() - t) > 40) if ((system_get_time() - t) > 40)
{ {
dht_bytes[idx] |= mask; dht_bytes[idx] |= mask;
} }
mask >>= 1; mask >>= 1;
if (mask == 0) // next byte? if (mask == 0) // next byte?
{ {
mask = 128; mask = 128;
idx++; idx++;
} }
} }
// Enable interrupts // Enable interrupts
ets_intr_unlock(); ets_intr_unlock();
// pinMode(pin, OUTPUT); // pinMode(pin, OUTPUT);
DIRECT_MODE_OUTPUT(pin); DIRECT_MODE_OUTPUT(pin);
// digitalWrite(pin, HIGH); // digitalWrite(pin, HIGH);
DIRECT_WRITE_HIGH(pin); DIRECT_WRITE_HIGH(pin);
return DHTLIB_OK; return DHTLIB_OK;
} }
// //
// END OF FILE // END OF FILE
// //
// //
// FILE: dht.h // FILE: dht.h
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// VERSION: 0.1.14 // VERSION: 0.1.14
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino // PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
// URL: http://arduino.cc/playground/Main/DHTLib // URL: http://arduino.cc/playground/Main/DHTLib
// //
// HISTORY: // HISTORY:
// see dht.cpp file // see dht.cpp file
// //
#ifndef dht_h #ifndef dht_h
#define dht_h #define dht_h
// #if ARDUINO < 100 // #if ARDUINO < 100
// #include <WProgram.h> // #include <WProgram.h>
// #else // #else
// #include <Arduino.h> // #include <Arduino.h>
// #endif // #endif
#include "c_types.h" #include "c_types.h"
#define DHT_LIB_VERSION "0.1.14" #define DHT_LIB_VERSION "0.1.14"
#define DHTLIB_OK 0 #define DHTLIB_OK 0
#define DHTLIB_ERROR_CHECKSUM -1 #define DHTLIB_ERROR_CHECKSUM -1
#define DHTLIB_ERROR_TIMEOUT -2 #define DHTLIB_ERROR_TIMEOUT -2
#define DHTLIB_INVALID_VALUE -999 #define DHTLIB_INVALID_VALUE -999
#define DHTLIB_DHT11_WAKEUP 18 #define DHTLIB_DHT11_WAKEUP 18
#define DHTLIB_DHT_WAKEUP 1 #define DHTLIB_DHT_WAKEUP 1
#define DHTLIB_DHT_UNI_WAKEUP 18 #define DHTLIB_DHT_UNI_WAKEUP 18
#define DHT_DEBUG #define DHT_DEBUG
// max timeout is 100 usec. // max timeout is 100 usec.
// For a 16 Mhz proc 100 usec is 1600 clock cycles // For a 16 Mhz proc 100 usec is 1600 clock cycles
// loops using DHTLIB_TIMEOUT use at least 4 clock cycli // loops using DHTLIB_TIMEOUT use at least 4 clock cycli
// so 100 us takes max 400 loops // so 100 us takes max 400 loops
// so by dividing F_CPU by 40000 we "fail" as fast as possible // so by dividing F_CPU by 40000 we "fail" as fast as possible
// ESP8266 uses delay_us get 1us time // ESP8266 uses delay_us get 1us time
#define DHTLIB_TIMEOUT (100) #define DHTLIB_TIMEOUT (100)
// Platform specific I/O definitions // Platform specific I/O definitions
#define DIRECT_READ(pin) (0x1 & GPIO_INPUT_GET(GPIO_ID_PIN(pin_num[pin]))) #define DIRECT_READ(pin) (0x1 & GPIO_INPUT_GET(GPIO_ID_PIN(pin_num[pin])))
#define DIRECT_MODE_INPUT(pin) GPIO_DIS_OUTPUT(pin_num[pin]) #define DIRECT_MODE_INPUT(pin) GPIO_DIS_OUTPUT(pin_num[pin])
#define DIRECT_MODE_OUTPUT(pin) #define DIRECT_MODE_OUTPUT(pin)
#define DIRECT_WRITE_LOW(pin) (GPIO_OUTPUT_SET(GPIO_ID_PIN(pin_num[pin]), 0)) #define DIRECT_WRITE_LOW(pin) (GPIO_OUTPUT_SET(GPIO_ID_PIN(pin_num[pin]), 0))
#define DIRECT_WRITE_HIGH(pin) (GPIO_OUTPUT_SET(GPIO_ID_PIN(pin_num[pin]), 1)) #define DIRECT_WRITE_HIGH(pin) (GPIO_OUTPUT_SET(GPIO_ID_PIN(pin_num[pin]), 1))
// return values: // return values:
// DHTLIB_OK // DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM // DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT // DHTLIB_ERROR_TIMEOUT
int dht_read_universal(uint8_t pin); int dht_read_universal(uint8_t pin);
int dht_read11(uint8_t pin); int dht_read11(uint8_t pin);
int dht_read(uint8_t pin); int dht_read(uint8_t pin);
int dht_read21(uint8_t pin); int dht_read21(uint8_t pin);
int dht_read22(uint8_t pin); int dht_read22(uint8_t pin);
int dht_read33(uint8_t pin); int dht_read33(uint8_t pin);
int dht_read44(uint8_t pin); int dht_read44(uint8_t pin);
double dht_getHumidity(void); double dht_getHumidity(void);
double dht_getTemperature(void); double dht_getTemperature(void);
#endif #endif
// //
// END OF FILE // END OF FILE
// //
\ No newline at end of file
...@@ -24,7 +24,7 @@ STD_CFLAGS=-std=gnu11 -Wimplicit ...@@ -24,7 +24,7 @@ STD_CFLAGS=-std=gnu11 -Wimplicit
# makefile at its root level - these are then overridden # makefile at its root level - these are then overridden
# for a subtree within the makefile rooted therein # for a subtree within the makefile rooted therein
# #
#DEFINES += #DEFINES +=
############################################################# #############################################################
# Recursion Magic - Don't touch this!! # Recursion Magic - Don't touch this!!
......
...@@ -49,7 +49,7 @@ LOCAL uint8 *pwm_channel; ...@@ -49,7 +49,7 @@ LOCAL uint8 *pwm_channel;
// Toggle flips between 1 and 0 when we make updates so that the interrupt code // Toggle flips between 1 and 0 when we make updates so that the interrupt code
// cn switch cleanly between the two states. The cinterrupt handler uses either // cn switch cleanly between the two states. The cinterrupt handler uses either
// the pwm_single_toggle[0] or pwm_single_toggle[1] // the pwm_single_toggle[0] or pwm_single_toggle[1]
// pwm_toggle indicates which state should be used on the *next* timer interrupt // pwm_toggle indicates which state should be used on the *next* timer interrupt
// freq boundary. // freq boundary.
LOCAL uint8 pwm_toggle = 1; LOCAL uint8 pwm_toggle = 1;
LOCAL volatile uint8 pwm_current_toggle = 1; LOCAL volatile uint8 pwm_current_toggle = 1;
...@@ -326,7 +326,7 @@ pwm_tim1_intr_handler(os_param_t p) ...@@ -326,7 +326,7 @@ pwm_tim1_intr_handler(os_param_t p)
int offset = 0; int offset = 0;
while (1) { while (1) {
if (pwm_current_channel >= (*pwm_channel - 1)) { if (pwm_current_channel >= (*pwm_channel - 1)) {
pwm_single = pwm_single_toggle[pwm_toggle]; pwm_single = pwm_single_toggle[pwm_toggle];
pwm_channel = &pwm_channel_toggle[pwm_toggle]; pwm_channel = &pwm_channel_toggle[pwm_toggle];
pwm_current_toggle = pwm_toggle; pwm_current_toggle = pwm_toggle;
...@@ -388,7 +388,7 @@ pwm_init(uint16 freq, uint16 *duty) ...@@ -388,7 +388,7 @@ pwm_init(uint16 freq, uint16 *duty)
// GPIO_OUTPUT_SET(GPIO_ID_PIN(PWM_0_OUT_IO_NUM), 0); // GPIO_OUTPUT_SET(GPIO_ID_PIN(PWM_0_OUT_IO_NUM), 0);
// GPIO_OUTPUT_SET(GPIO_ID_PIN(PWM_1_OUT_IO_NUM), 0); // GPIO_OUTPUT_SET(GPIO_ID_PIN(PWM_1_OUT_IO_NUM), 0);
// GPIO_OUTPUT_SET(GPIO_ID_PIN(PWM_2_OUT_IO_NUM), 0); // GPIO_OUTPUT_SET(GPIO_ID_PIN(PWM_2_OUT_IO_NUM), 0);
for (i = 0; i < PWM_CHANNEL; i++) { for (i = 0; i < PWM_CHANNEL; i++) {
// pwm_gpio |= (1 << pwm_out_io_num[i]); // pwm_gpio |= (1 << pwm_out_io_num[i]);
pwm_gpio = 0; pwm_gpio = 0;
......
...@@ -20,7 +20,7 @@ bool uart_getc(char *c){ ...@@ -20,7 +20,7 @@ bool uart_getc(char *c){
ETS_INTR_LOCK(); ETS_INTR_LOCK();
*c = (char)*(pRxBuff->pReadPos); *c = (char)*(pRxBuff->pReadPos);
if (pRxBuff->pReadPos == (pRxBuff->pRcvMsgBuff + RX_BUFF_SIZE)) { if (pRxBuff->pReadPos == (pRxBuff->pRcvMsgBuff + RX_BUFF_SIZE)) {
pRxBuff->pReadPos = pRxBuff->pRcvMsgBuff ; pRxBuff->pReadPos = pRxBuff->pRcvMsgBuff ;
} else { } else {
pRxBuff->pReadPos++; pRxBuff->pReadPos++;
} }
...@@ -73,7 +73,7 @@ start: ...@@ -73,7 +73,7 @@ start:
else else
continue; continue;
} }
/* end of line */ /* end of line */
if (ch == '\r' || ch == '\n') if (ch == '\r' || ch == '\n')
{ {
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
#include "ets_sys.h" #include "ets_sys.h"
// //
// Queue is empty if read == write. // Queue is empty if read == write.
// However, we always want to keep the previous value // However, we always want to keep the previous value
// so writing is only allowed if write - read < QUEUE_SIZE - 1 // so writing is only allowed if write - read < QUEUE_SIZE - 1
...@@ -60,7 +60,7 @@ static uint8_t task_queued; ...@@ -60,7 +60,7 @@ static uint8_t task_queued;
static void set_gpio_bits(void); static void set_gpio_bits(void);
static void rotary_clear_pin(int pin) static void rotary_clear_pin(int pin)
{ {
if (pin >= 0) { if (pin >= 0) {
gpio_pin_intr_state_set(GPIO_ID_PIN(pin_num[pin]), GPIO_PIN_INTR_DISABLE); gpio_pin_intr_state_set(GPIO_ID_PIN(pin_num[pin]), GPIO_PIN_INTR_DISABLE);
...@@ -69,7 +69,7 @@ static void rotary_clear_pin(int pin) ...@@ -69,7 +69,7 @@ static void rotary_clear_pin(int pin)
} }
// Just takes the channel number. Cleans up the resources used. // Just takes the channel number. Cleans up the resources used.
int rotary_close(uint32_t channel) int rotary_close(uint32_t channel)
{ {
if (channel >= sizeof(data) / sizeof(data[0])) { if (channel >= sizeof(data) / sizeof(data[0])) {
return -1; return -1;
...@@ -94,7 +94,7 @@ int rotary_close(uint32_t channel) ...@@ -94,7 +94,7 @@ int rotary_close(uint32_t channel)
return 0; return 0;
} }
static uint32_t ICACHE_RAM_ATTR rotary_interrupt(uint32_t ret_gpio_status) static uint32_t ICACHE_RAM_ATTR rotary_interrupt(uint32_t ret_gpio_status)
{ {
// This function really is running at interrupt level with everything // This function really is running at interrupt level with everything
// else masked off. It should take as little time as necessary. // else masked off. It should take as little time as necessary.
...@@ -168,10 +168,10 @@ static uint32_t ICACHE_RAM_ATTR rotary_interrupt(uint32_t ret_gpio_status) ...@@ -168,10 +168,10 @@ static uint32_t ICACHE_RAM_ATTR rotary_interrupt(uint32_t ret_gpio_status)
} }
new_status |= rotary_pos & 0x7fffffff; new_status |= rotary_pos & 0x7fffffff;
if (last_status != new_status) { if (last_status != new_status) {
// Either we overwrite the status or we add a new one // Either we overwrite the status or we add a new one
if (!HAS_QUEUED_DATA(d) if (!HAS_QUEUED_DATA(d)
|| STATUS_IS_PRESSED(last_status ^ new_status) || STATUS_IS_PRESSED(last_status ^ new_status)
|| STATUS_IS_PRESSED(last_status ^ GET_PREV_STATUS(d).pos)) { || STATUS_IS_PRESSED(last_status ^ GET_PREV_STATUS(d).pos)) {
if (HAS_QUEUE_SPACE(d)) { if (HAS_QUEUE_SPACE(d)) {
...@@ -271,10 +271,10 @@ bool rotary_has_queued_event(uint32_t channel) ...@@ -271,10 +271,10 @@ bool rotary_has_queued_event(uint32_t channel)
} }
// Get the oldest event in the queue and remove it (if possible) // Get the oldest event in the queue and remove it (if possible)
bool rotary_getevent(uint32_t channel, rotary_event_t *resultp) bool rotary_getevent(uint32_t channel, rotary_event_t *resultp)
{ {
rotary_event_t result = { 0 }; rotary_event_t result = { 0 };
if (channel >= sizeof(data) / sizeof(data[0])) { if (channel >= sizeof(data) / sizeof(data[0])) {
return FALSE; return FALSE;
} }
......
...@@ -15,33 +15,33 @@ static uint32_t spi_clkdiv[2]; ...@@ -15,33 +15,33 @@ static uint32_t spi_clkdiv[2];
*******************************************************************************/ *******************************************************************************/
void spi_lcd_mode_init(uint8 spi_no) void spi_lcd_mode_init(uint8 spi_no)
{ {
uint32 regvalue; uint32 regvalue;
if(spi_no>1) return; //handle invalid input number if(spi_no>1) return; //handle invalid input number
//bit9 of PERIPHS_IO_MUX should be cleared when HSPI clock doesn't equal CPU clock //bit9 of PERIPHS_IO_MUX should be cleared when HSPI clock doesn't equal CPU clock
//bit8 of PERIPHS_IO_MUX should be cleared when SPI clock doesn't equal CPU clock //bit8 of PERIPHS_IO_MUX should be cleared when SPI clock doesn't equal CPU clock
if(spi_no==SPI_SPI){ if(spi_no==SPI_SPI){
WRITE_PERI_REG(PERIPHS_IO_MUX, 0x005); //clear bit9,and bit8 WRITE_PERI_REG(PERIPHS_IO_MUX, 0x005); //clear bit9,and bit8
PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CLK_U, 1);//configure io to spi mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CLK_U, 1);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CMD_U, 1);//configure io to spi mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CMD_U, 1);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA0_U, 1);//configure io to spi mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA0_U, 1);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA1_U, 1);//configure io to spi mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA1_U, 1);//configure io to spi mode
}else if(spi_no==SPI_HSPI){ }else if(spi_no==SPI_HSPI){
WRITE_PERI_REG(PERIPHS_IO_MUX, 0x105); //clear bit9 WRITE_PERI_REG(PERIPHS_IO_MUX, 0x105); //clear bit9
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, 2);//configure io to spi mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, 2);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, 2);//configure io to spi mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, 2);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, 2);//configure io to spi mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, 2);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, 2);//configure io to spi mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, 2);//configure io to spi mode
} }
SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_CS_SETUP|SPI_CS_HOLD|SPI_USR_COMMAND); SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_CS_SETUP|SPI_CS_HOLD|SPI_USR_COMMAND);
CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_FLASH_MODE); CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_FLASH_MODE);
// SPI clock=CPU clock/8 // SPI clock=CPU clock/8
WRITE_PERI_REG(SPI_CLOCK(spi_no), WRITE_PERI_REG(SPI_CLOCK(spi_no),
((1&SPI_CLKDIV_PRE)<<SPI_CLKDIV_PRE_S)| ((1&SPI_CLKDIV_PRE)<<SPI_CLKDIV_PRE_S)|
((3&SPI_CLKCNT_N)<<SPI_CLKCNT_N_S)| ((3&SPI_CLKCNT_N)<<SPI_CLKCNT_N_S)|
((1&SPI_CLKCNT_H)<<SPI_CLKCNT_H_S)| ((1&SPI_CLKCNT_H)<<SPI_CLKCNT_H_S)|
((3&SPI_CLKCNT_L)<<SPI_CLKCNT_L_S)); //clear bit 31,set SPI clock div ((3&SPI_CLKCNT_L)<<SPI_CLKCNT_L_S)); //clear bit 31,set SPI clock div
} }
/****************************************************************************** /******************************************************************************
* FunctionName : spi_lcd_9bit_write * FunctionName : spi_lcd_9bit_write
...@@ -55,11 +55,11 @@ void spi_lcd_9bit_write(uint8 spi_no,uint8 high_bit,uint8 low_8bit) ...@@ -55,11 +55,11 @@ void spi_lcd_9bit_write(uint8 spi_no,uint8 high_bit,uint8 low_8bit)
uint32 regvalue; uint32 regvalue;
uint8 bytetemp; uint8 bytetemp;
if(spi_no>1) return; //handle invalid input number if(spi_no>1) return; //handle invalid input number
if(high_bit) bytetemp=(low_8bit>>1)|0x80; if(high_bit) bytetemp=(low_8bit>>1)|0x80;
else bytetemp=(low_8bit>>1)&0x7f; else bytetemp=(low_8bit>>1)&0x7f;
regvalue= ((8&SPI_USR_COMMAND_BITLEN)<<SPI_USR_COMMAND_BITLEN_S)|((uint32)bytetemp); //configure transmission variable,9bit transmission length and first 8 command bit regvalue= ((8&SPI_USR_COMMAND_BITLEN)<<SPI_USR_COMMAND_BITLEN_S)|((uint32)bytetemp); //configure transmission variable,9bit transmission length and first 8 command bit
if(low_8bit&0x01) regvalue|=BIT15; //write the 9th bit if(low_8bit&0x01) regvalue|=BIT15; //write the 9th bit
while(READ_PERI_REG(SPI_CMD(spi_no))&SPI_USR); //waiting for spi module available while(READ_PERI_REG(SPI_CMD(spi_no))&SPI_USR); //waiting for spi module available
WRITE_PERI_REG(SPI_USER2(spi_no), regvalue); //write command and command length into spi reg WRITE_PERI_REG(SPI_USER2(spi_no), regvalue); //write command and command length into spi reg
...@@ -97,7 +97,7 @@ uint32_t spi_set_clkdiv(uint8 spi_no, uint32_t clock_div) ...@@ -97,7 +97,7 @@ uint32_t spi_set_clkdiv(uint8 spi_no, uint32_t clock_div)
WRITE_PERI_REG(PERIPHS_IO_MUX, 0x005 | (clock_div <= 1 ? 0x100 : 0)); WRITE_PERI_REG(PERIPHS_IO_MUX, 0x005 | (clock_div <= 1 ? 0x100 : 0));
} }
else if(spi_no==SPI_HSPI){ else if(spi_no==SPI_HSPI){
WRITE_PERI_REG(PERIPHS_IO_MUX, 0x105 | (clock_div <= 1 ? 0x200 : 0)); WRITE_PERI_REG(PERIPHS_IO_MUX, 0x105 | (clock_div <= 1 ? 0x200 : 0));
} }
spi_clkdiv[spi_no] = clock_div; spi_clkdiv[spi_no] = clock_div;
...@@ -112,7 +112,7 @@ uint32_t spi_set_clkdiv(uint8 spi_no, uint32_t clock_div) ...@@ -112,7 +112,7 @@ uint32_t spi_set_clkdiv(uint8 spi_no, uint32_t clock_div)
*******************************************************************************/ *******************************************************************************/
void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, uint32_t clock_div) void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, uint32_t clock_div)
{ {
uint32 regvalue; uint32 regvalue;
if(spi_no>1) return; //handle invalid input number if(spi_no>1) return; //handle invalid input number
...@@ -125,7 +125,7 @@ void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, uint32_t clock_ ...@@ -125,7 +125,7 @@ void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, uint32_t clock_
} else { } else {
CLEAR_PERI_REG_MASK(SPI_PIN(spi_no), SPI_IDLE_EDGE); CLEAR_PERI_REG_MASK(SPI_PIN(spi_no), SPI_IDLE_EDGE);
} }
//set clock phase //set clock phase
if (cpha == cpol) { if (cpha == cpol) {
// Mode 3: MOSI is set on falling edge of clock // Mode 3: MOSI is set on falling edge of clock
...@@ -133,8 +133,8 @@ void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, uint32_t clock_ ...@@ -133,8 +133,8 @@ void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, uint32_t clock_
CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_CK_OUT_EDGE); CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_CK_OUT_EDGE);
} else { } else {
// Mode 2: MOSI is set on rising edge of clock // Mode 2: MOSI is set on rising edge of clock
// Mode 1: MOSI is set on rising edge of clock // Mode 1: MOSI is set on rising edge of clock
SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_CK_OUT_EDGE); SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_CK_OUT_EDGE);
} }
CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_FLASH_MODE|SPI_USR_MISO|SPI_USR_ADDR|SPI_USR_COMMAND|SPI_USR_DUMMY); CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_FLASH_MODE|SPI_USR_MISO|SPI_USR_ADDR|SPI_USR_COMMAND|SPI_USR_DUMMY);
...@@ -146,15 +146,15 @@ void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, uint32_t clock_ ...@@ -146,15 +146,15 @@ void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, uint32_t clock_
if(spi_no==SPI_SPI){ if(spi_no==SPI_SPI){
PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CLK_U, 1);//configure io to spi mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CLK_U, 1);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CMD_U, 1);//configure io to spi mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CMD_U, 1);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA0_U, 1);//configure io to spi mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA0_U, 1);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA1_U, 1);//configure io to spi mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA1_U, 1);//configure io to spi mode
} }
else if(spi_no==SPI_HSPI){ else if(spi_no==SPI_HSPI){
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, 2);//configure io to spi mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, 2);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, 2);//configure io to spi mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, 2);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, 2);//configure io to spi mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, 2);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, 2);//configure io to spi mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, 2);//configure io to spi mode
} }
} }
...@@ -398,7 +398,7 @@ void spi_byte_write_espslave(uint8 spi_no,uint8 data) ...@@ -398,7 +398,7 @@ void spi_byte_write_espslave(uint8 spi_no,uint8 data)
//SPI_FLASH_USER2 bit28-31 is cmd length,cmd bit length is value(0-15)+1, //SPI_FLASH_USER2 bit28-31 is cmd length,cmd bit length is value(0-15)+1,
// bit15-0 is cmd value. // bit15-0 is cmd value.
//0x70000000 is for 8bits cmd, 0x04 is eps8266 slave write cmd value //0x70000000 is for 8bits cmd, 0x04 is eps8266 slave write cmd value
WRITE_PERI_REG(SPI_USER2(spi_no), WRITE_PERI_REG(SPI_USER2(spi_no),
((7&SPI_USR_COMMAND_BITLEN)<<SPI_USR_COMMAND_BITLEN_S)|4); ((7&SPI_USR_COMMAND_BITLEN)<<SPI_USR_COMMAND_BITLEN_S)|4);
WRITE_PERI_REG(SPI_W0(spi_no), (uint32)(data)); WRITE_PERI_REG(SPI_W0(spi_no), (uint32)(data));
SET_PERI_REG_MASK(SPI_CMD(spi_no), SPI_USR); SET_PERI_REG_MASK(SPI_CMD(spi_no), SPI_USR);
...@@ -424,10 +424,10 @@ void spi_byte_write_espslave(uint8 spi_no,uint8 data) ...@@ -424,10 +424,10 @@ void spi_byte_write_espslave(uint8 spi_no,uint8 data)
//SPI_FLASH_USER2 bit28-31 is cmd length,cmd bit length is value(0-15)+1, //SPI_FLASH_USER2 bit28-31 is cmd length,cmd bit length is value(0-15)+1,
// bit15-0 is cmd value. // bit15-0 is cmd value.
//0x70000000 is for 8bits cmd, 0x06 is eps8266 slave read cmd value //0x70000000 is for 8bits cmd, 0x06 is eps8266 slave read cmd value
WRITE_PERI_REG(SPI_USER2(spi_no), WRITE_PERI_REG(SPI_USER2(spi_no),
((7&SPI_USR_COMMAND_BITLEN)<<SPI_USR_COMMAND_BITLEN_S)|6); ((7&SPI_USR_COMMAND_BITLEN)<<SPI_USR_COMMAND_BITLEN_S)|6);
SET_PERI_REG_MASK(SPI_CMD(spi_no), SPI_USR); SET_PERI_REG_MASK(SPI_CMD(spi_no), SPI_USR);
while(READ_PERI_REG(SPI_CMD(spi_no))&SPI_USR); while(READ_PERI_REG(SPI_CMD(spi_no))&SPI_USR);
*data=(uint8)(READ_PERI_REG(SPI_W0(spi_no))&0xff); *data=(uint8)(READ_PERI_REG(SPI_W0(spi_no))&0xff);
} }
...@@ -440,7 +440,7 @@ void spi_byte_write_espslave(uint8 spi_no,uint8 data) ...@@ -440,7 +440,7 @@ void spi_byte_write_espslave(uint8 spi_no,uint8 data)
*******************************************************************************/ *******************************************************************************/
void spi_slave_init(uint8 spi_no) void spi_slave_init(uint8 spi_no)
{ {
uint32 regvalue; uint32 regvalue;
if(spi_no>1) if(spi_no>1)
return; //handle invalid input number return; //handle invalid input number
...@@ -450,29 +450,29 @@ void spi_slave_init(uint8 spi_no) ...@@ -450,29 +450,29 @@ void spi_slave_init(uint8 spi_no)
////WRITE_PERI_REG(PERIPHS_IO_MUX, 0x105); //clear bit9//TEST ////WRITE_PERI_REG(PERIPHS_IO_MUX, 0x105); //clear bit9//TEST
if(spi_no==SPI_SPI){ if(spi_no==SPI_SPI){
PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CLK_U, 1);//configure io to spi mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CLK_U, 1);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CMD_U, 1);//configure io to spi mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CMD_U, 1);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA0_U, 1);//configure io to spi mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA0_U, 1);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA1_U, 1);//configure io to spi mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA1_U, 1);//configure io to spi mode
}else if(spi_no==SPI_HSPI){ }else if(spi_no==SPI_HSPI){
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, 2);//configure io to spi mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, 2);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, 2);//configure io to spi mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, 2);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, 2);//configure io to spi mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, 2);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, 2);//configure io to spi mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, 2);//configure io to spi mode
} }
//regvalue=READ_PERI_REG(SPI_FLASH_SLAVE(spi_no)); //regvalue=READ_PERI_REG(SPI_FLASH_SLAVE(spi_no));
//slave mode,slave use buffers which are register "SPI_FLASH_C0~C15", enable trans done isr //slave mode,slave use buffers which are register "SPI_FLASH_C0~C15", enable trans done isr
//set bit 30 bit 29 bit9,bit9 is trans done isr mask //set bit 30 bit 29 bit9,bit9 is trans done isr mask
SET_PERI_REG_MASK( SPI_SLAVE(spi_no), SET_PERI_REG_MASK( SPI_SLAVE(spi_no),
SPI_SLAVE_MODE|SPI_SLV_WR_RD_BUF_EN| SPI_SLAVE_MODE|SPI_SLV_WR_RD_BUF_EN|
SPI_SLV_WR_BUF_DONE_EN|SPI_SLV_RD_BUF_DONE_EN| SPI_SLV_WR_BUF_DONE_EN|SPI_SLV_RD_BUF_DONE_EN|
SPI_SLV_WR_STA_DONE_EN|SPI_SLV_RD_STA_DONE_EN| SPI_SLV_WR_STA_DONE_EN|SPI_SLV_RD_STA_DONE_EN|
SPI_TRANS_DONE_EN); SPI_TRANS_DONE_EN);
//disable general trans intr //disable general trans intr
//CLEAR_PERI_REG_MASK(SPI_SLAVE(spi_no),SPI_TRANS_DONE_EN); //CLEAR_PERI_REG_MASK(SPI_SLAVE(spi_no),SPI_TRANS_DONE_EN);
CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_FLASH_MODE);//disable flash operation mode CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_FLASH_MODE);//disable flash operation mode
SET_PERI_REG_MASK(SPI_USER(spi_no),SPI_USR_MISO_HIGHPART);//SLAVE SEND DATA BUFFER IN C8-C15 SET_PERI_REG_MASK(SPI_USER(spi_no),SPI_USR_MISO_HIGHPART);//SLAVE SEND DATA BUFFER IN C8-C15
//////**************RUN WHEN SLAVE RECIEVE*******************/////// //////**************RUN WHEN SLAVE RECIEVE*******************///////
...@@ -482,12 +482,12 @@ void spi_slave_init(uint8 spi_no) ...@@ -482,12 +482,12 @@ void spi_slave_init(uint8 spi_no)
WRITE_PERI_REG(SPI_CLOCK(spi_no), 0); WRITE_PERI_REG(SPI_CLOCK(spi_no), 0);
/////***************************************************//////
//set 8 bit slave command length, because slave must have at least one bit addr, /////***************************************************//////
//8 bit slave+8bit addr, so master device first 2 bytes can be regarded as a command
//and the following bytes are datas, //set 8 bit slave command length, because slave must have at least one bit addr,
//8 bit slave+8bit addr, so master device first 2 bytes can be regarded as a command
//and the following bytes are datas,
//32 bytes input wil be stored in SPI_FLASH_C0-C7 //32 bytes input wil be stored in SPI_FLASH_C0-C7
//32 bytes output data should be set to SPI_FLASH_C8-C15 //32 bytes output data should be set to SPI_FLASH_C8-C15
WRITE_PERI_REG(SPI_USER2(spi_no), (0x7&SPI_USR_COMMAND_BITLEN)<<SPI_USR_COMMAND_BITLEN_S); //0x70000000 WRITE_PERI_REG(SPI_USER2(spi_no), (0x7&SPI_USR_COMMAND_BITLEN)<<SPI_USR_COMMAND_BITLEN_S); //0x70000000
...@@ -498,15 +498,15 @@ void spi_slave_init(uint8 spi_no) ...@@ -498,15 +498,15 @@ void spi_slave_init(uint8 spi_no)
((0x7&SPI_SLV_STATUS_BITLEN)<<SPI_SLV_STATUS_BITLEN_S)| ((0x7&SPI_SLV_STATUS_BITLEN)<<SPI_SLV_STATUS_BITLEN_S)|
((0x7&SPI_SLV_WR_ADDR_BITLEN)<<SPI_SLV_WR_ADDR_BITLEN_S)| ((0x7&SPI_SLV_WR_ADDR_BITLEN)<<SPI_SLV_WR_ADDR_BITLEN_S)|
((0x7&SPI_SLV_RD_ADDR_BITLEN)<<SPI_SLV_RD_ADDR_BITLEN_S)); ((0x7&SPI_SLV_RD_ADDR_BITLEN)<<SPI_SLV_RD_ADDR_BITLEN_S));
SET_PERI_REG_MASK(SPI_PIN(spi_no),BIT19);//BIT19
//maybe enable slave transmission liston SET_PERI_REG_MASK(SPI_PIN(spi_no),BIT19);//BIT19
//maybe enable slave transmission liston
SET_PERI_REG_MASK(SPI_CMD(spi_no),SPI_USR); SET_PERI_REG_MASK(SPI_CMD(spi_no),SPI_USR);
//register level2 isr function, which contains spi, hspi and i2s events //register level2 isr function, which contains spi, hspi and i2s events
ETS_SPI_INTR_ATTACH(spi_slave_isr_handler,NULL); ETS_SPI_INTR_ATTACH(spi_slave_isr_handler,NULL);
//enable level2 isr, which contains spi, hspi and i2s events //enable level2 isr, which contains spi, hspi and i2s events
ETS_SPI_INTR_ENABLE(); ETS_SPI_INTR_ENABLE();
} }
...@@ -531,7 +531,7 @@ void spi_slave_init(uint8 spi_no) ...@@ -531,7 +531,7 @@ void spi_slave_init(uint8 spi_no)
/****************************************************************************** /******************************************************************************
* FunctionName : hspi_master_readwrite_repeat * FunctionName : hspi_master_readwrite_repeat
* Description : SPI master test function for reading and writing esp8266 slave buffer, * Description : SPI master test function for reading and writing esp8266 slave buffer,
the function uses HSPI module the function uses HSPI module
*******************************************************************************/ *******************************************************************************/
os_timer_t timer2; os_timer_t timer2;
...@@ -556,7 +556,7 @@ void hspi_master_readwrite_repeat(void) ...@@ -556,7 +556,7 @@ void hspi_master_readwrite_repeat(void)
/****************************************************************************** /******************************************************************************
* FunctionName : spi_slave_isr_handler * FunctionName : spi_slave_isr_handler
* Description : SPI interrupt function, SPI HSPI and I2S interrupt can trig this function * Description : SPI interrupt function, SPI HSPI and I2S interrupt can trig this function
some basic operation like clear isr flag has been done, some basic operation like clear isr flag has been done,
and it is availible for adding user coder in the funtion and it is availible for adding user coder in the funtion
* Parameters : void *para- function parameter address, which has been registered in function spi_slave_init * Parameters : void *para- function parameter address, which has been registered in function spi_slave_init
*******************************************************************************/ *******************************************************************************/
...@@ -578,7 +578,7 @@ os_event_t * spiQueue; ...@@ -578,7 +578,7 @@ os_event_t * spiQueue;
#define DATA_ERROR 6 #define DATA_ERROR 6
#define STATUS_R_IN_RD 7 #define STATUS_R_IN_RD 7
//init the two intr line of slave //init the two intr line of slave
//gpio0: wr_ready ,and //gpio0: wr_ready ,and
//gpio2: rd_ready , controlled by slave //gpio2: rd_ready , controlled by slave
void ICACHE_FLASH_ATTR void ICACHE_FLASH_ATTR
gpio_init() gpio_init()
...@@ -600,32 +600,32 @@ void spi_slave_isr_handler(void *para) ...@@ -600,32 +600,32 @@ void spi_slave_isr_handler(void *para)
static uint8 state =0; static uint8 state =0;
uint32 recv_data,send_data; uint32 recv_data,send_data;
if(READ_PERI_REG(0x3ff00020)&BIT4){ if(READ_PERI_REG(0x3ff00020)&BIT4){
//following 3 lines is to clear isr signal //following 3 lines is to clear isr signal
CLEAR_PERI_REG_MASK(SPI_SLAVE(SPI_SPI), 0x3ff); CLEAR_PERI_REG_MASK(SPI_SLAVE(SPI_SPI), 0x3ff);
}else if(READ_PERI_REG(0x3ff00020)&BIT7){ //bit7 is for hspi isr, }else if(READ_PERI_REG(0x3ff00020)&BIT7){ //bit7 is for hspi isr,
regvalue=READ_PERI_REG(SPI_SLAVE(SPI_HSPI)); regvalue=READ_PERI_REG(SPI_SLAVE(SPI_HSPI));
CLEAR_PERI_REG_MASK(SPI_SLAVE(SPI_HSPI), CLEAR_PERI_REG_MASK(SPI_SLAVE(SPI_HSPI),
SPI_TRANS_DONE_EN| SPI_TRANS_DONE_EN|
SPI_SLV_WR_STA_DONE_EN| SPI_SLV_WR_STA_DONE_EN|
SPI_SLV_RD_STA_DONE_EN| SPI_SLV_RD_STA_DONE_EN|
SPI_SLV_WR_BUF_DONE_EN| SPI_SLV_WR_BUF_DONE_EN|
SPI_SLV_RD_BUF_DONE_EN); SPI_SLV_RD_BUF_DONE_EN);
SET_PERI_REG_MASK(SPI_SLAVE(SPI_HSPI), SPI_SYNC_RESET); SET_PERI_REG_MASK(SPI_SLAVE(SPI_HSPI), SPI_SYNC_RESET);
CLEAR_PERI_REG_MASK(SPI_SLAVE(SPI_HSPI), CLEAR_PERI_REG_MASK(SPI_SLAVE(SPI_HSPI),
SPI_TRANS_DONE| SPI_TRANS_DONE|
SPI_SLV_WR_STA_DONE| SPI_SLV_WR_STA_DONE|
SPI_SLV_RD_STA_DONE| SPI_SLV_RD_STA_DONE|
SPI_SLV_WR_BUF_DONE| SPI_SLV_WR_BUF_DONE|
SPI_SLV_RD_BUF_DONE); SPI_SLV_RD_BUF_DONE);
SET_PERI_REG_MASK(SPI_SLAVE(SPI_HSPI), SET_PERI_REG_MASK(SPI_SLAVE(SPI_HSPI),
SPI_TRANS_DONE_EN| SPI_TRANS_DONE_EN|
SPI_SLV_WR_STA_DONE_EN| SPI_SLV_WR_STA_DONE_EN|
SPI_SLV_RD_STA_DONE_EN| SPI_SLV_RD_STA_DONE_EN|
SPI_SLV_WR_BUF_DONE_EN| SPI_SLV_WR_BUF_DONE_EN|
SPI_SLV_RD_BUF_DONE_EN); SPI_SLV_RD_BUF_DONE_EN);
if(regvalue&SPI_SLV_WR_BUF_DONE){ if(regvalue&SPI_SLV_WR_BUF_DONE){
GPIO_OUTPUT_SET(0, 0); GPIO_OUTPUT_SET(0, 0);
idx=0; idx=0;
while(idx<8){ while(idx<8){
...@@ -646,7 +646,7 @@ void spi_slave_isr_handler(void *para) ...@@ -646,7 +646,7 @@ void spi_slave_isr_handler(void *para)
//system_os_post(USER_TASK_PRIO_1,WR_RD,regvalue); //system_os_post(USER_TASK_PRIO_1,WR_RD,regvalue);
} }
}else if(READ_PERI_REG(0x3ff00020)&BIT9){ //bit7 is for i2s isr, }else if(READ_PERI_REG(0x3ff00020)&BIT9){ //bit7 is for i2s isr,
} }
...@@ -698,19 +698,19 @@ void ICACHE_FLASH_ATTR ...@@ -698,19 +698,19 @@ void ICACHE_FLASH_ATTR
break; break;
case STATUS_W: case STATUS_W:
os_printf("SW ERR,Reg:%08x\n",e->par); os_printf("SW ERR,Reg:%08x\n",e->par);
break; break;
case TR_DONE_ALONE: case TR_DONE_ALONE:
os_printf("TD ALO ERR,Reg:%08x\n",e->par); os_printf("TD ALO ERR,Reg:%08x\n",e->par);
break; break;
case WR_RD: case WR_RD:
os_printf("WR&RD ERR,Reg:%08x\n",e->par); os_printf("WR&RD ERR,Reg:%08x\n",e->par);
break; break;
case DATA_ERROR: case DATA_ERROR:
os_printf("Data ERR,Reg:%08x\n",e->par); os_printf("Data ERR,Reg:%08x\n",e->par);
break; break;
case STATUS_R_IN_RD : case STATUS_R_IN_RD :
os_printf("SR ERR in RDPR,Reg:%08x\n",e->par); os_printf("SR ERR in RDPR,Reg:%08x\n",e->par);
break; break;
default: default:
break; break;
} }
...@@ -738,7 +738,7 @@ void ICACHE_FLASH_ATTR ...@@ -738,7 +738,7 @@ void ICACHE_FLASH_ATTR
os_printf("spi miso init\n\r"); os_printf("spi miso init\n\r");
set_miso_data(); set_miso_data();
#endif #endif
//os_timer_disarm(&spi_timer_test); //os_timer_disarm(&spi_timer_test);
//os_timer_setfn(&spi_timer_test, (os_timer_func_t *)set_miso_data, NULL);//wjl //os_timer_setfn(&spi_timer_test, (os_timer_func_t *)set_miso_data, NULL);//wjl
//os_timer_arm(&spi_timer_test,50,1); //os_timer_arm(&spi_timer_test,50,1);
......
/* /*
* Module for interfacing with Switec instrument steppers (and * Module for interfacing with Switec instrument steppers (and
* similar devices). These are the steppers that are used in automotive * similar devices). These are the steppers that are used in automotive
* instrument panels and the like. Run off 5 volts at low current. * instrument panels and the like. Run off 5 volts at low current.
* *
* Code inspired by: * Code inspired by:
...@@ -81,7 +81,7 @@ static void ICACHE_RAM_ATTR timer_interrupt(os_param_t); ...@@ -81,7 +81,7 @@ static void ICACHE_RAM_ATTR timer_interrupt(os_param_t);
// Just takes the channel number // Just takes the channel number
int switec_close(uint32_t channel) int switec_close(uint32_t channel)
{ {
if (channel >= sizeof(data) / sizeof(data[0])) { if (channel >= sizeof(data) / sizeof(data[0])) {
return -1; return -1;
...@@ -118,28 +118,28 @@ int switec_close(uint32_t channel) ...@@ -118,28 +118,28 @@ int switec_close(uint32_t channel)
return 0; return 0;
} }
static __attribute__((always_inline)) inline void write_io(DATA *d) static __attribute__((always_inline)) inline void write_io(DATA *d)
{ {
uint32_t pin_state = d->pinstate[d->current_state]; uint32_t pin_state = d->pinstate[d->current_state];
gpio_output_set(pin_state, d->mask & ~pin_state, 0, 0); gpio_output_set(pin_state, d->mask & ~pin_state, 0, 0);
} }
static __attribute__((always_inline)) inline void step_up(DATA *d) static __attribute__((always_inline)) inline void step_up(DATA *d)
{ {
d->current_step++; d->current_step++;
d->current_state = (d->current_state + 1) % N_STATES; d->current_state = (d->current_state + 1) % N_STATES;
write_io(d); write_io(d);
} }
static __attribute__((always_inline)) inline void step_down(DATA *d) static __attribute__((always_inline)) inline void step_down(DATA *d)
{ {
d->current_step--; d->current_step--;
d->current_state = (d->current_state + N_STATES - 1) % N_STATES; d->current_state = (d->current_state + N_STATES - 1) % N_STATES;
write_io(d); write_io(d);
} }
static void ICACHE_RAM_ATTR timer_interrupt(os_param_t p) static void ICACHE_RAM_ATTR timer_interrupt(os_param_t p)
{ {
// This function really is running at interrupt level with everything // This function really is running at interrupt level with everything
// else masked off. It should take as little time as necessary. // else masked off. It should take as little time as necessary.
...@@ -179,9 +179,9 @@ static void ICACHE_RAM_ATTR timer_interrupt(os_param_t p) ...@@ -179,9 +179,9 @@ static void ICACHE_RAM_ATTR timer_interrupt(os_param_t p)
if (d->vel == 0) { if (d->vel == 0) {
d->dir = d->current_step < d->target_step ? 1 : -1; d->dir = d->current_step < d->target_step ? 1 : -1;
// do not set to 0 or it could go negative in case 2 below // do not set to 0 or it could go negative in case 2 below
d->vel = 1; d->vel = 1;
} }
// Move the pointer by one step in the correct direction // Move the pointer by one step in the correct direction
if (d->dir > 0) { if (d->dir > 0) {
step_up(d); step_up(d);
...@@ -192,7 +192,7 @@ static void ICACHE_RAM_ATTR timer_interrupt(os_param_t p) ...@@ -192,7 +192,7 @@ static void ICACHE_RAM_ATTR timer_interrupt(os_param_t p)
// determine delta, number of steps in current direction to target. // determine delta, number of steps in current direction to target.
// may be negative if we are headed away from target // may be negative if we are headed away from target
int delta = d->dir > 0 ? d->target_step - d->current_step : d->current_step - d->target_step; int delta = d->dir > 0 ? d->target_step - d->current_step : d->current_step - d->target_step;
if (delta > 0) { if (delta > 0) {
// case 1 : moving towards target (maybe under accel or decel) // case 1 : moving towards target (maybe under accel or decel)
if (delta <= d->vel) { if (delta <= d->vel) {
...@@ -208,7 +208,7 @@ static void ICACHE_RAM_ATTR timer_interrupt(os_param_t p) ...@@ -208,7 +208,7 @@ static void ICACHE_RAM_ATTR timer_interrupt(os_param_t p)
// case 2 : at or moving away from target (slow down!) // case 2 : at or moving away from target (slow down!)
d->vel--; d->vel--;
} }
// vel now defines delay // vel now defines delay
uint8_t row = 0; uint8_t row = 0;
// this is why vel must not be greater than the last vel in the table. // this is why vel must not be greater than the last vel in the table.
...@@ -232,7 +232,7 @@ static void ICACHE_RAM_ATTR timer_interrupt(os_param_t p) ...@@ -232,7 +232,7 @@ static void ICACHE_RAM_ATTR timer_interrupt(os_param_t p)
if (need_to_wait < delay) { if (need_to_wait < delay) {
delay = need_to_wait; delay = need_to_wait;
} }
} }
if (delay < 1000000) { if (delay < 1000000) {
if (delay < 50) { if (delay < 50) {
...@@ -367,11 +367,11 @@ int switec_moveto(uint32_t channel, int pos) ...@@ -367,11 +367,11 @@ int switec_moveto(uint32_t channel, int pos)
} }
} }
return 0; return 0;
} }
// Get the current position, direction and target position // Get the current position, direction and target position
int switec_getpos(uint32_t channel, int32_t *pos, int32_t *dir, int32_t *target) int switec_getpos(uint32_t channel, int32_t *pos, int32_t *dir, int32_t *target)
{ {
if (channel >= sizeof(data) / sizeof(data[0])) { if (channel >= sizeof(data) / sizeof(data[0])) {
return -1; return -1;
......
...@@ -307,7 +307,7 @@ uart0_rx_intr_handler(void *para) ...@@ -307,7 +307,7 @@ uart0_rx_intr_handler(void *para)
} }
} }
static void static void
uart_autobaud_timeout(void *timer_arg) uart_autobaud_timeout(void *timer_arg)
{ {
uint32_t uart_no = (uint32_t) timer_arg; uint32_t uart_no = (uint32_t) timer_arg;
...@@ -325,7 +325,7 @@ uart_autobaud_timeout(void *timer_arg) ...@@ -325,7 +325,7 @@ uart_autobaud_timeout(void *timer_arg)
} }
#include "pm/swtimer.h" #include "pm/swtimer.h"
static void static void
uart_init_autobaud(uint32_t uart_no) uart_init_autobaud(uint32_t uart_no)
{ {
os_timer_setfn(&autobaud_timer, uart_autobaud_timeout, (void *) uart_no); os_timer_setfn(&autobaud_timer, uart_autobaud_timeout, (void *) uart_no);
...@@ -334,7 +334,7 @@ uart_init_autobaud(uint32_t uart_no) ...@@ -334,7 +334,7 @@ uart_init_autobaud(uint32_t uart_no)
os_timer_arm(&autobaud_timer, 100, TRUE); os_timer_arm(&autobaud_timer, 100, TRUE);
} }
static void static void
uart_stop_autobaud() uart_stop_autobaud()
{ {
os_timer_disarm(&autobaud_timer); os_timer_disarm(&autobaud_timer);
......
...@@ -23,7 +23,7 @@ endif ...@@ -23,7 +23,7 @@ endif
# makefile at its root level - these are then overridden # makefile at its root level - these are then overridden
# for a subtree within the makefile rooted therein # for a subtree within the makefile rooted therein
# #
#DEFINES += -DGDBSTUB_REDIRECT_CONSOLE_OUTPUT #DEFINES += -DGDBSTUB_REDIRECT_CONSOLE_OUTPUT
############################################################# #############################################################
# Recursion Magic - Don't touch this!! # Recursion Magic - Don't touch this!!
......
...@@ -10,9 +10,9 @@ and do some other magic to make everything work and compile under FreeRTOS. ...@@ -10,9 +10,9 @@ and do some other magic to make everything work and compile under FreeRTOS.
#endif #endif
/* /*
Enable this to make the exception and debugging handlers switch to a private stack. This will use Enable this to make the exception and debugging handlers switch to a private stack. This will use
up 1K of RAM, but may be useful if you're debugging stack or stack pointer corruption problems. It's up 1K of RAM, but may be useful if you're debugging stack or stack pointer corruption problems. It's
normally disabled because not many situations need it. If for some reason the GDB communication normally disabled because not many situations need it. If for some reason the GDB communication
stops when you run into an error in your code, try enabling this. stops when you run into an error in your code, try enabling this.
*/ */
#ifndef GDBSTUB_USE_OWN_STACK #ifndef GDBSTUB_USE_OWN_STACK
...@@ -30,7 +30,7 @@ the gdbstub_init call. ...@@ -30,7 +30,7 @@ the gdbstub_init call.
#endif #endif
/* /*
Enabling this will redirect console output to GDB. This basically means that printf/os_printf output Enabling this will redirect console output to GDB. This basically means that printf/os_printf output
will show up in your gdb session, which is useful if you use gdb to do stuff. It also means that if will show up in your gdb session, which is useful if you use gdb to do stuff. It also means that if
you use a normal terminal, you can't read the printfs anymore. you use a normal terminal, you can't read the printfs anymore.
*/ */
...@@ -52,7 +52,7 @@ Gdbstub functions are placed in flash or IRAM using attributes, as defined here. ...@@ -52,7 +52,7 @@ Gdbstub functions are placed in flash or IRAM using attributes, as defined here.
(and related) can always be in flash, because it's called in the normal code flow. The rest of the (and related) can always be in flash, because it's called in the normal code flow. The rest of the
gdbstub functions can be in flash too, but only if there's no chance of them being called when the gdbstub functions can be in flash too, but only if there's no chance of them being called when the
flash somehow is disabled (eg during SPI operations or flash write/erase operations). If the routines flash somehow is disabled (eg during SPI operations or flash write/erase operations). If the routines
are called when the flash is disabled (eg due to a Ctrl-C at the wrong time), the ESP8266 will most are called when the flash is disabled (eg due to a Ctrl-C at the wrong time), the ESP8266 will most
likely crash. likely crash.
*/ */
#define ATTR_GDBINIT ICACHE_FLASH_ATTR #define ATTR_GDBINIT ICACHE_FLASH_ATTR
......
...@@ -46,9 +46,9 @@ The savedRegs struct: ...@@ -46,9 +46,9 @@ The savedRegs struct:
*/ */
/* /*
This is the debugging exception routine; it's called by the debugging vector This is the debugging exception routine; it's called by the debugging vector
We arrive here with all regs intact except for a2. The old contents of A2 are saved We arrive here with all regs intact except for a2. The old contents of A2 are saved
into the DEBUG_EXCSAVE special function register. EPC is the original PC. into the DEBUG_EXCSAVE special function register. EPC is the original PC.
*/ */
gdbstub_debug_exception_entry: gdbstub_debug_exception_entry:
...@@ -170,7 +170,7 @@ DebugExceptionExit: ...@@ -170,7 +170,7 @@ DebugExceptionExit:
FreeRTOS exception handling code. For some reason or another, we can't just hook the main exception vector: it FreeRTOS exception handling code. For some reason or another, we can't just hook the main exception vector: it
seems FreeRTOS uses that for something else too (interrupts). FreeRTOS has its own fatal exception handler, and we seems FreeRTOS uses that for something else too (interrupts). FreeRTOS has its own fatal exception handler, and we
hook that. Unfortunately, that one is called from a few different places (eg directly in the DoubleExceptionVector) hook that. Unfortunately, that one is called from a few different places (eg directly in the DoubleExceptionVector)
so the precise location of the original register values are somewhat of a mystery when we arrive here... so the precise location of the original register values are somewhat of a mystery when we arrive here...
As a 'solution', we'll just decode the most common case of the user_fatal_exception_handler being called from As a 'solution', we'll just decode the most common case of the user_fatal_exception_handler being called from
the user exception handler vector: the user exception handler vector:
...@@ -350,14 +350,14 @@ gdbstub_set_hw_watchpoint: ...@@ -350,14 +350,14 @@ gdbstub_set_hw_watchpoint:
bany a6, a5, return_w_error bany a6, a5, return_w_error
//Set watchpoint //Set watchpoint
wsr a2, DBREAKA wsr a2, DBREAKA
//Combine type and mask //Combine type and mask
movi a6, 0x3F movi a6, 0x3F
and a3, a3, a6 and a3, a3, a6
slli a4, a4, 30 slli a4, a4, 30
or a3, a3, a4 or a3, a3, a4
wsr a3, DBREAKC wsr a3, DBREAKC
// movi a2, 1 // movi a2, 1
mov a2, a3 mov a2, a3
isync isync
......
/****************************************************************************** /******************************************************************************
* Copyright 2015 Espressif Systems * Copyright 2015 Espressif Systems
* *
* Description: A stub to make the ESP8266 debuggable by GDB over the serial * Description: A stub to make the ESP8266 debuggable by GDB over the serial
* port. * port.
* *
* License: ESPRESSIF MIT License * License: ESPRESSIF MIT License
...@@ -267,7 +267,7 @@ static int ATTR_GDBFN validWrAddr(int p) { ...@@ -267,7 +267,7 @@ static int ATTR_GDBFN validWrAddr(int p) {
return 0; return 0;
} }
/* /*
Register file in the format lx106 gdb port expects it. Register file in the format lx106 gdb port expects it.
Inspired by gdb/regformats/reg-xtensa.dat from Inspired by gdb/regformats/reg-xtensa.dat from
https://github.com/jcmvbkbc/crosstool-NG/blob/lx106-g%2B%2B/overlays/xtensa_lx106.tar https://github.com/jcmvbkbc/crosstool-NG/blob/lx106-g%2B%2B/overlays/xtensa_lx106.tar
...@@ -470,7 +470,7 @@ static int ATTR_GDBFN gdbHandleCommand(unsigned char *cmd, int len) { ...@@ -470,7 +470,7 @@ static int ATTR_GDBFN gdbHandleCommand(unsigned char *cmd, int len) {
//Lower layer: grab a command packet and check the checksum //Lower layer: grab a command packet and check the checksum
//Calls gdbHandleCommand on the packet if the checksum is OK //Calls gdbHandleCommand on the packet if the checksum is OK
//Returns ST_OK on success, ST_ERR when checksum fails, a //Returns ST_OK on success, ST_ERR when checksum fails, a
//character if it is received instead of the GDB packet //character if it is received instead of the GDB packet
//start char. //start char.
static int ATTR_GDBFN gdbReadCommand() { static int ATTR_GDBFN gdbReadCommand() {
...@@ -580,7 +580,7 @@ void ATTR_GDBFN gdbstub_handle_debug_exception() { ...@@ -580,7 +580,7 @@ void ATTR_GDBFN gdbstub_handle_debug_exception() {
while(gdbReadCommand()!=ST_CONT); while(gdbReadCommand()!=ST_CONT);
if ((gdbstub_savedRegs.reason&0x84)==0x4) { if ((gdbstub_savedRegs.reason&0x84)==0x4) {
//We stopped due to a watchpoint. We can't re-execute the current instruction //We stopped due to a watchpoint. We can't re-execute the current instruction
//because it will happily re-trigger the same watchpoint, so we emulate it //because it will happily re-trigger the same watchpoint, so we emulate it
//while we're still in debugger space. //while we're still in debugger space.
emulLdSt(); emulLdSt();
} else if ((gdbstub_savedRegs.reason&0x88)==0x8) { } else if ((gdbstub_savedRegs.reason&0x88)==0x8) {
...@@ -659,12 +659,12 @@ static void ATTR_GDBFN gdb_semihost_putchar1(char c) { ...@@ -659,12 +659,12 @@ static void ATTR_GDBFN gdb_semihost_putchar1(char c) {
} }
#if !GDBSTUB_FREERTOS #if !GDBSTUB_FREERTOS
//The OS-less SDK uses the Xtensa HAL to handle exceptions. We can use those functions to catch any //The OS-less SDK uses the Xtensa HAL to handle exceptions. We can use those functions to catch any
//fatal exceptions and invoke the debugger when this happens. //fatal exceptions and invoke the debugger when this happens.
static void ATTR_GDBINIT install_exceptions() { static void ATTR_GDBINIT install_exceptions() {
int i; int i;
int exno[]={EXCCAUSE_ILLEGAL, EXCCAUSE_SYSCALL, EXCCAUSE_INSTR_ERROR, EXCCAUSE_LOAD_STORE_ERROR, int exno[]={EXCCAUSE_ILLEGAL, EXCCAUSE_SYSCALL, EXCCAUSE_INSTR_ERROR, EXCCAUSE_LOAD_STORE_ERROR,
EXCCAUSE_DIVIDE_BY_ZERO, EXCCAUSE_UNALIGNED, EXCCAUSE_INSTR_DATA_ERROR, EXCCAUSE_LOAD_STORE_DATA_ERROR, EXCCAUSE_DIVIDE_BY_ZERO, EXCCAUSE_UNALIGNED, EXCCAUSE_INSTR_DATA_ERROR, EXCCAUSE_LOAD_STORE_DATA_ERROR,
EXCCAUSE_INSTR_ADDR_ERROR, EXCCAUSE_LOAD_STORE_ADDR_ERROR, EXCCAUSE_INSTR_PROHIBITED, EXCCAUSE_INSTR_ADDR_ERROR, EXCCAUSE_LOAD_STORE_ADDR_ERROR, EXCCAUSE_INSTR_PROHIBITED,
EXCCAUSE_LOAD_PROHIBITED, EXCCAUSE_STORE_PROHIBITED}; EXCCAUSE_LOAD_PROHIBITED, EXCCAUSE_STORE_PROHIBITED};
for (i=0; i<(sizeof(exno)/sizeof(exno[0])); i++) { for (i=0; i<(sizeof(exno)/sizeof(exno[0])); i++) {
...@@ -708,9 +708,9 @@ static void ATTR_GDBFN uart_hdlr(void *arg, void *frame) { ...@@ -708,9 +708,9 @@ static void ATTR_GDBFN uart_hdlr(void *arg, void *frame) {
//Copy registers the Xtensa HAL did save to gdbstub_savedRegs //Copy registers the Xtensa HAL did save to gdbstub_savedRegs
os_memcpy(&gdbstub_savedRegs, frame, 19*4); os_memcpy(&gdbstub_savedRegs, frame, 19*4);
gdbstub_savedRegs.a1=(uint32_t)frame+EXCEPTION_GDB_SP_OFFSET; gdbstub_savedRegs.a1=(uint32_t)frame+EXCEPTION_GDB_SP_OFFSET;
gdbstub_savedRegs.reason=0xff; //mark as user break reason gdbstub_savedRegs.reason=0xff; //mark as user break reason
ets_wdt_disable(); ets_wdt_disable();
sendReason(); sendReason();
xthal_set_intenable(0); xthal_set_intenable(0);
...@@ -749,9 +749,9 @@ void ATTR_GDBFN gdbstub_handle_uart_int(struct XTensa_rtos_int_frame_s *frame) { ...@@ -749,9 +749,9 @@ void ATTR_GDBFN gdbstub_handle_uart_int(struct XTensa_rtos_int_frame_s *frame) {
for (x=2; x<16; x++) gdbstub_savedRegs.a[x-2]=frame->a[x]; for (x=2; x<16; x++) gdbstub_savedRegs.a[x-2]=frame->a[x];
// gdbstub_savedRegs.a1=(uint32_t)frame+EXCEPTION_GDB_SP_OFFSET; // gdbstub_savedRegs.a1=(uint32_t)frame+EXCEPTION_GDB_SP_OFFSET;
gdbstub_savedRegs.reason=0xff; //mark as user break reason gdbstub_savedRegs.reason=0xff; //mark as user break reason
// ets_wdt_disable(); // ets_wdt_disable();
sendReason(); sendReason();
while(gdbReadCommand()!=ST_CONT); while(gdbReadCommand()!=ST_CONT);
......
############################################################# #############################################################
# Required variables for each makefile # Required variables for each makefile
# Discard this section from all parent makefiles # Discard this section from all parent makefiles
# Expected variables (with automatic defaults): # Expected variables (with automatic defaults):
# CSRCS (all "C" files in the dir) # CSRCS (all "C" files in the dir)
# SUBDIRS (all subdirs with a Makefile) # SUBDIRS (all subdirs with a Makefile)
# GEN_LIBS - list of libs to be generated () # GEN_LIBS - list of libs to be generated ()
# GEN_IMAGES - list of images to be generated () # GEN_IMAGES - list of images to be generated ()
# COMPONENTS_xxx - a list of libs/objs in the form # COMPONENTS_xxx - a list of libs/objs in the form
# subdir/lib to be extracted and rolled up into # subdir/lib to be extracted and rolled up into
# a generated lib/image xxx.a () # a generated lib/image xxx.a ()
# #
ifndef PDIR ifndef PDIR
GEN_LIBS = libfatfs.a GEN_LIBS = libfatfs.a
endif endif
ifndef FATFS_INC_DIR ifndef FATFS_INC_DIR
FATFS_INC_DIR = ./ FATFS_INC_DIR = ./
endif endif
STD_CFLAGS=-std=gnu11 -Wimplicit -imacros $(FATFS_INC_DIR)fatfs_prefix_lib.h STD_CFLAGS=-std=gnu11 -Wimplicit -imacros $(FATFS_INC_DIR)fatfs_prefix_lib.h
############################################################# #############################################################
# Configuration i.e. compile options etc. # Configuration i.e. compile options etc.
# Target specific stuff (defines etc.) goes in here! # Target specific stuff (defines etc.) goes in here!
# Generally values applying to a tree are captured in the # Generally values applying to a tree are captured in the
# makefile at its root level - these are then overridden # makefile at its root level - these are then overridden
# for a subtree within the makefile rooted therein # for a subtree within the makefile rooted therein
# #
#DEFINES += #DEFINES +=
############################################################# #############################################################
# Recursion Magic - Don't touch this!! # Recursion Magic - Don't touch this!!
# #
# Each subtree potentially has an include directory # Each subtree potentially has an include directory
# corresponding to the common APIs applicable to modules # corresponding to the common APIs applicable to modules
# rooted at that subtree. Accordingly, the INCLUDE PATH # rooted at that subtree. Accordingly, the INCLUDE PATH
# of a module can only contain the include directories up # of a module can only contain the include directories up
# its parent path, and not its siblings # its parent path, and not its siblings
# #
# Required for each makefile to inherit from the parent # Required for each makefile to inherit from the parent
# #
INCLUDES := $(INCLUDES) -I $(PDIR)include INCLUDES := $(INCLUDES) -I $(PDIR)include
INCLUDES += -I ./ INCLUDES += -I ./
INCLUDES += -I ../platform INCLUDES += -I ../platform
INCLUDES += -I ../libc INCLUDES += -I ../libc
INCLUDES += -I ../lua INCLUDES += -I ../lua
PDIR := ../$(PDIR) PDIR := ../$(PDIR)
sinclude $(PDIR)Makefile sinclude $(PDIR)Makefile
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
/* Low level disk I/O module skeleton for FatFs (C)ChaN, 2016 */ /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2016 */
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be */ /* If a working storage control module is available, it should be */
/* attached to the FatFs via a glue function rather than modifying it. */ /* attached to the FatFs via a glue function rather than modifying it. */
/* This is an example of glue functions to attach various exsisting */ /* This is an example of glue functions to attach various exsisting */
/* storage control modules to the FatFs module with a defined API. */ /* storage control modules to the FatFs module with a defined API. */
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
#include "ff.h" /* Obtains integer types */ #include "ff.h" /* Obtains integer types */
#include "diskio.h" /* FatFs lower layer API */ #include "diskio.h" /* FatFs lower layer API */
#include "sdcard.h" #include "sdcard.h"
static DSTATUS m_status = STA_NOINIT; static DSTATUS m_status = STA_NOINIT;
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
/* Get Drive Status */ /* Get Drive Status */
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
DSTATUS disk_status ( DSTATUS disk_status (
BYTE pdrv /* Physical drive nmuber to identify the drive */ BYTE pdrv /* Physical drive nmuber to identify the drive */
) )
{ {
return m_status; return m_status;
} }
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
/* Inidialize a Drive */ /* Inidialize a Drive */
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
DSTATUS disk_initialize ( DSTATUS disk_initialize (
BYTE pdrv /* Physical drive nmuber to identify the drive */ BYTE pdrv /* Physical drive nmuber to identify the drive */
) )
{ {
int result; int result;
if (platform_sdcard_init( 1, pdrv )) { if (platform_sdcard_init( 1, pdrv )) {
m_status &= ~STA_NOINIT; m_status &= ~STA_NOINIT;
} }
return m_status; return m_status;
} }
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
/* Read Sector(s) */ /* Read Sector(s) */
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
DRESULT disk_read ( DRESULT disk_read (
BYTE pdrv, /* Physical drive nmuber to identify the drive */ BYTE pdrv, /* Physical drive nmuber to identify the drive */
BYTE *buff, /* Data buffer to store read data */ BYTE *buff, /* Data buffer to store read data */
DWORD sector, /* Sector address in LBA */ DWORD sector, /* Sector address in LBA */
UINT count /* Number of sectors to read */ UINT count /* Number of sectors to read */
) )
{ {
if (count == 1) { if (count == 1) {
if (! platform_sdcard_read_block( pdrv, sector, buff )) { if (! platform_sdcard_read_block( pdrv, sector, buff )) {
return RES_ERROR; return RES_ERROR;
} }
} else { } else {
if (! platform_sdcard_read_blocks( pdrv, sector, count, buff )) { if (! platform_sdcard_read_blocks( pdrv, sector, count, buff )) {
return RES_ERROR; return RES_ERROR;
} }
} }
return RES_OK; return RES_OK;
} }
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
/* Write Sector(s) */ /* Write Sector(s) */
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
#if FF_FS_READONLY == 0 #if FF_FS_READONLY == 0
DRESULT disk_write ( DRESULT disk_write (
BYTE pdrv, /* Physical drive nmuber to identify the drive */ BYTE pdrv, /* Physical drive nmuber to identify the drive */
const BYTE *buff, /* Data to be written */ const BYTE *buff, /* Data to be written */
DWORD sector, /* Sector address in LBA */ DWORD sector, /* Sector address in LBA */
UINT count /* Number of sectors to write */ UINT count /* Number of sectors to write */
) )
{ {
if (count == 1) { if (count == 1) {
if (! platform_sdcard_write_block( pdrv, sector, buff )) { if (! platform_sdcard_write_block( pdrv, sector, buff )) {
return RES_ERROR; return RES_ERROR;
} }
} else { } else {
if (! platform_sdcard_write_blocks( pdrv, sector, count, buff )) { if (! platform_sdcard_write_blocks( pdrv, sector, count, buff )) {
return RES_ERROR; return RES_ERROR;
} }
} }
return RES_OK; return RES_OK;
} }
#endif #endif
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
/* Miscellaneous Functions */ /* Miscellaneous Functions */
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
DRESULT disk_ioctl ( DRESULT disk_ioctl (
BYTE pdrv, /* Physical drive nmuber (0..) */ BYTE pdrv, /* Physical drive nmuber (0..) */
BYTE cmd, /* Control code */ BYTE cmd, /* Control code */
void *buff /* Buffer to send/receive control data */ void *buff /* Buffer to send/receive control data */
) )
{ {
switch (cmd) { switch (cmd) {
case CTRL_TRIM: /* no-op */ case CTRL_TRIM: /* no-op */
case CTRL_SYNC: /* no-op */ case CTRL_SYNC: /* no-op */
return RES_OK; return RES_OK;
default: /* anything else throws parameter error */ default: /* anything else throws parameter error */
return RES_PARERR; return RES_PARERR;
} }
} }
/*-----------------------------------------------------------------------/ /*-----------------------------------------------------------------------/
/ Low level disk interface modlue include file (C)ChaN, 2014 / / Low level disk interface modlue include file (C)ChaN, 2014 /
/-----------------------------------------------------------------------*/ /-----------------------------------------------------------------------*/
#ifndef _DISKIO_DEFINED #ifndef _DISKIO_DEFINED
#define _DISKIO_DEFINED #define _DISKIO_DEFINED
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
/* Status of Disk Functions */ /* Status of Disk Functions */
typedef BYTE DSTATUS; typedef BYTE DSTATUS;
/* Results of Disk Functions */ /* Results of Disk Functions */
typedef enum { typedef enum {
RES_OK = 0, /* 0: Successful */ RES_OK = 0, /* 0: Successful */
RES_ERROR, /* 1: R/W Error */ RES_ERROR, /* 1: R/W Error */
RES_WRPRT, /* 2: Write Protected */ RES_WRPRT, /* 2: Write Protected */
RES_NOTRDY, /* 3: Not Ready */ RES_NOTRDY, /* 3: Not Ready */
RES_PARERR /* 4: Invalid Parameter */ RES_PARERR /* 4: Invalid Parameter */
} DRESULT; } DRESULT;
/*---------------------------------------*/ /*---------------------------------------*/
/* Prototypes for disk control functions */ /* Prototypes for disk control functions */
DSTATUS disk_initialize (BYTE pdrv); DSTATUS disk_initialize (BYTE pdrv);
DSTATUS disk_status (BYTE pdrv); DSTATUS disk_status (BYTE pdrv);
DRESULT disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count); DRESULT disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count);
DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count); DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count);
DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff); DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff);
/* Disk Status Bits (DSTATUS) */ /* Disk Status Bits (DSTATUS) */
#define STA_NOINIT 0x01 /* Drive not initialized */ #define STA_NOINIT 0x01 /* Drive not initialized */
#define STA_NODISK 0x02 /* No medium in the drive */ #define STA_NODISK 0x02 /* No medium in the drive */
#define STA_PROTECT 0x04 /* Write protected */ #define STA_PROTECT 0x04 /* Write protected */
/* Command code for disk_ioctrl fucntion */ /* Command code for disk_ioctrl fucntion */
/* Generic command (Used by FatFs) */ /* Generic command (Used by FatFs) */
#define CTRL_SYNC 0 /* Complete pending write process (needed at FF_FS_READONLY == 0) */ #define CTRL_SYNC 0 /* Complete pending write process (needed at FF_FS_READONLY == 0) */
#define GET_SECTOR_COUNT 1 /* Get media size (needed at FF_USE_MKFS == 1) */ #define GET_SECTOR_COUNT 1 /* Get media size (needed at FF_USE_MKFS == 1) */
#define GET_SECTOR_SIZE 2 /* Get sector size (needed at FF_MAX_SS != FF_MIN_SS) */ #define GET_SECTOR_SIZE 2 /* Get sector size (needed at FF_MAX_SS != FF_MIN_SS) */
#define GET_BLOCK_SIZE 3 /* Get erase block size (needed at FF_USE_MKFS == 1) */ #define GET_BLOCK_SIZE 3 /* Get erase block size (needed at FF_USE_MKFS == 1) */
#define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at FF_USE_TRIM == 1) */ #define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at FF_USE_TRIM == 1) */
/* Generic command (Not used by FatFs) */ /* Generic command (Not used by FatFs) */
#define CTRL_POWER 5 /* Get/Set power status */ #define CTRL_POWER 5 /* Get/Set power status */
#define CTRL_LOCK 6 /* Lock/Unlock media removal */ #define CTRL_LOCK 6 /* Lock/Unlock media removal */
#define CTRL_EJECT 7 /* Eject media */ #define CTRL_EJECT 7 /* Eject media */
#define CTRL_FORMAT 8 /* Create physical format on the media */ #define CTRL_FORMAT 8 /* Create physical format on the media */
/* MMC/SDC specific ioctl command */ /* MMC/SDC specific ioctl command */
#define MMC_GET_TYPE 10 /* Get card type */ #define MMC_GET_TYPE 10 /* Get card type */
#define MMC_GET_CSD 11 /* Get CSD */ #define MMC_GET_CSD 11 /* Get CSD */
#define MMC_GET_CID 12 /* Get CID */ #define MMC_GET_CID 12 /* Get CID */
#define MMC_GET_OCR 13 /* Get OCR */ #define MMC_GET_OCR 13 /* Get OCR */
#define MMC_GET_SDSTAT 14 /* Get SD status */ #define MMC_GET_SDSTAT 14 /* Get SD status */
#define ISDIO_READ 55 /* Read data form SD iSDIO register */ #define ISDIO_READ 55 /* Read data form SD iSDIO register */
#define ISDIO_WRITE 56 /* Write data to SD iSDIO register */ #define ISDIO_WRITE 56 /* Write data to SD iSDIO register */
#define ISDIO_MRITE 57 /* Masked write data to SD iSDIO register */ #define ISDIO_MRITE 57 /* Masked write data to SD iSDIO register */
/* ATA/CF specific ioctl command */ /* ATA/CF specific ioctl command */
#define ATA_GET_REV 20 /* Get F/W revision */ #define ATA_GET_REV 20 /* Get F/W revision */
#define ATA_GET_MODEL 21 /* Get model name */ #define ATA_GET_MODEL 21 /* Get model name */
#define ATA_GET_SN 22 /* Get serial number */ #define ATA_GET_SN 22 /* Get serial number */
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
/* Sample Code of OS Dependent Functions for FatFs */ /* Sample Code of OS Dependent Functions for FatFs */
/* (C)ChaN, 2018 */ /* (C)ChaN, 2018 */
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
#include <stdlib.h> #include <stdlib.h>
#include "ff.h" #include "ff.h"
#if FF_USE_LFN == 3 /* Dynamic memory allocation */ #if FF_USE_LFN == 3 /* Dynamic memory allocation */
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
/* Allocate a memory block */ /* Allocate a memory block */
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
void* ff_memalloc ( /* Returns pointer to the allocated memory block (null if not enough core) */ void* ff_memalloc ( /* Returns pointer to the allocated memory block (null if not enough core) */
UINT msize /* Number of bytes to allocate */ UINT msize /* Number of bytes to allocate */
) )
{ {
return malloc(msize); /* Allocate a new memory block with POSIX API */ return malloc(msize); /* Allocate a new memory block with POSIX API */
} }
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
/* Free a memory block */ /* Free a memory block */
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
void ff_memfree ( void ff_memfree (
void* mblock /* Pointer to the memory block to free (nothing to do if null) */ void* mblock /* Pointer to the memory block to free (nothing to do if null) */
) )
{ {
free(mblock); /* Free the memory block with POSIX API */ free(mblock); /* Free the memory block with POSIX API */
} }
#endif #endif
#if FF_FS_REENTRANT /* Mutal exclusion */ #if FF_FS_REENTRANT /* Mutal exclusion */
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
/* Create a Synchronization Object */ /* Create a Synchronization Object */
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
/* This function is called in f_mount() function to create a new /* This function is called in f_mount() function to create a new
/ synchronization object for the volume, such as semaphore and mutex. / synchronization object for the volume, such as semaphore and mutex.
/ When a 0 is returned, the f_mount() function fails with FR_INT_ERR. / When a 0 is returned, the f_mount() function fails with FR_INT_ERR.
*/ */
//const osMutexDef_t Mutex[FF_VOLUMES]; /* Table of CMSIS-RTOS mutex */ //const osMutexDef_t Mutex[FF_VOLUMES]; /* Table of CMSIS-RTOS mutex */
int ff_cre_syncobj ( /* 1:Function succeeded, 0:Could not create the sync object */ int ff_cre_syncobj ( /* 1:Function succeeded, 0:Could not create the sync object */
BYTE vol, /* Corresponding volume (logical drive number) */ BYTE vol, /* Corresponding volume (logical drive number) */
FF_SYNC_t* sobj /* Pointer to return the created sync object */ FF_SYNC_t* sobj /* Pointer to return the created sync object */
) )
{ {
/* Win32 */ /* Win32 */
*sobj = CreateMutex(NULL, FALSE, NULL); *sobj = CreateMutex(NULL, FALSE, NULL);
return (int)(*sobj != INVALID_HANDLE_VALUE); return (int)(*sobj != INVALID_HANDLE_VALUE);
/* uITRON */ /* uITRON */
// T_CSEM csem = {TA_TPRI,1,1}; // T_CSEM csem = {TA_TPRI,1,1};
// *sobj = acre_sem(&csem); // *sobj = acre_sem(&csem);
// return (int)(*sobj > 0); // return (int)(*sobj > 0);
/* uC/OS-II */ /* uC/OS-II */
// OS_ERR err; // OS_ERR err;
// *sobj = OSMutexCreate(0, &err); // *sobj = OSMutexCreate(0, &err);
// return (int)(err == OS_NO_ERR); // return (int)(err == OS_NO_ERR);
/* FreeRTOS */ /* FreeRTOS */
// *sobj = xSemaphoreCreateMutex(); // *sobj = xSemaphoreCreateMutex();
// return (int)(*sobj != NULL); // return (int)(*sobj != NULL);
/* CMSIS-RTOS */ /* CMSIS-RTOS */
// *sobj = osMutexCreate(&Mutex[vol]); // *sobj = osMutexCreate(&Mutex[vol]);
// return (int)(*sobj != NULL); // return (int)(*sobj != NULL);
} }
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
/* Delete a Synchronization Object */ /* Delete a Synchronization Object */
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
/* This function is called in f_mount() function to delete a synchronization /* This function is called in f_mount() function to delete a synchronization
/ object that created with ff_cre_syncobj() function. When a 0 is returned, / object that created with ff_cre_syncobj() function. When a 0 is returned,
/ the f_mount() function fails with FR_INT_ERR. / the f_mount() function fails with FR_INT_ERR.
*/ */
int ff_del_syncobj ( /* 1:Function succeeded, 0:Could not delete due to an error */ int ff_del_syncobj ( /* 1:Function succeeded, 0:Could not delete due to an error */
FF_SYNC_t sobj /* Sync object tied to the logical drive to be deleted */ FF_SYNC_t sobj /* Sync object tied to the logical drive to be deleted */
) )
{ {
/* Win32 */ /* Win32 */
return (int)CloseHandle(sobj); return (int)CloseHandle(sobj);
/* uITRON */ /* uITRON */
// return (int)(del_sem(sobj) == E_OK); // return (int)(del_sem(sobj) == E_OK);
/* uC/OS-II */ /* uC/OS-II */
// OS_ERR err; // OS_ERR err;
// OSMutexDel(sobj, OS_DEL_ALWAYS, &err); // OSMutexDel(sobj, OS_DEL_ALWAYS, &err);
// return (int)(err == OS_NO_ERR); // return (int)(err == OS_NO_ERR);
/* FreeRTOS */ /* FreeRTOS */
// vSemaphoreDelete(sobj); // vSemaphoreDelete(sobj);
// return 1; // return 1;
/* CMSIS-RTOS */ /* CMSIS-RTOS */
// return (int)(osMutexDelete(sobj) == osOK); // return (int)(osMutexDelete(sobj) == osOK);
} }
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
/* Request Grant to Access the Volume */ /* Request Grant to Access the Volume */
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
/* This function is called on entering file functions to lock the volume. /* This function is called on entering file functions to lock the volume.
/ When a 0 is returned, the file function fails with FR_TIMEOUT. / When a 0 is returned, the file function fails with FR_TIMEOUT.
*/ */
int ff_req_grant ( /* 1:Got a grant to access the volume, 0:Could not get a grant */ int ff_req_grant ( /* 1:Got a grant to access the volume, 0:Could not get a grant */
FF_SYNC_t sobj /* Sync object to wait */ FF_SYNC_t sobj /* Sync object to wait */
) )
{ {
/* Win32 */ /* Win32 */
return (int)(WaitForSingleObject(sobj, FF_FS_TIMEOUT) == WAIT_OBJECT_0); return (int)(WaitForSingleObject(sobj, FF_FS_TIMEOUT) == WAIT_OBJECT_0);
/* uITRON */ /* uITRON */
// return (int)(wai_sem(sobj) == E_OK); // return (int)(wai_sem(sobj) == E_OK);
/* uC/OS-II */ /* uC/OS-II */
// OS_ERR err; // OS_ERR err;
// OSMutexPend(sobj, FF_FS_TIMEOUT, &err)); // OSMutexPend(sobj, FF_FS_TIMEOUT, &err));
// return (int)(err == OS_NO_ERR); // return (int)(err == OS_NO_ERR);
/* FreeRTOS */ /* FreeRTOS */
// return (int)(xSemaphoreTake(sobj, FF_FS_TIMEOUT) == pdTRUE); // return (int)(xSemaphoreTake(sobj, FF_FS_TIMEOUT) == pdTRUE);
/* CMSIS-RTOS */ /* CMSIS-RTOS */
// return (int)(osMutexWait(sobj, FF_FS_TIMEOUT) == osOK); // return (int)(osMutexWait(sobj, FF_FS_TIMEOUT) == osOK);
} }
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
/* Release Grant to Access the Volume */ /* Release Grant to Access the Volume */
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
/* This function is called on leaving file functions to unlock the volume. /* This function is called on leaving file functions to unlock the volume.
*/ */
void ff_rel_grant ( void ff_rel_grant (
FF_SYNC_t sobj /* Sync object to be signaled */ FF_SYNC_t sobj /* Sync object to be signaled */
) )
{ {
/* Win32 */ /* Win32 */
ReleaseMutex(sobj); ReleaseMutex(sobj);
/* uITRON */ /* uITRON */
// sig_sem(sobj); // sig_sem(sobj);
/* uC/OS-II */ /* uC/OS-II */
// OSMutexPost(sobj); // OSMutexPost(sobj);
/* FreeRTOS */ /* FreeRTOS */
// xSemaphoreGive(sobj); // xSemaphoreGive(sobj);
/* CMSIS-RTOS */ /* CMSIS-RTOS */
// osMutexRelease(sobj); // osMutexRelease(sobj);
} }
#endif #endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment