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 source diff could not be displayed because it is too large. You can view the blob instead.
/*----------------------------------------------------------------------------/ /*----------------------------------------------------------------------------/
/ FatFs - Generic FAT Filesystem module R0.13c / / FatFs - Generic FAT Filesystem module R0.13c /
/-----------------------------------------------------------------------------/ /-----------------------------------------------------------------------------/
/ /
/ Copyright (C) 2018, ChaN, all right reserved. / Copyright (C) 2018, ChaN, all right reserved.
/ /
/ FatFs module is an open source software. Redistribution and use of FatFs in / FatFs module is an open source software. Redistribution and use of FatFs in
/ source and binary forms, with or without modification, are permitted provided / source and binary forms, with or without modification, are permitted provided
/ that the following condition is met: / that the following condition is met:
/ 1. Redistributions of source code must retain the above copyright notice, / 1. Redistributions of source code must retain the above copyright notice,
/ this condition and the following disclaimer. / this condition and the following disclaimer.
/ /
/ This software is provided by the copyright holder and contributors "AS IS" / This software is provided by the copyright holder and contributors "AS IS"
/ and any warranties related to this software are DISCLAIMED. / and any warranties related to this software are DISCLAIMED.
/ The copyright owner or contributors be NOT LIABLE for any damages caused / The copyright owner or contributors be NOT LIABLE for any damages caused
/ by use of this software. / by use of this software.
/ /
/----------------------------------------------------------------------------*/ /----------------------------------------------------------------------------*/
#ifndef FF_DEFINED #ifndef FF_DEFINED
#define FF_DEFINED 86604 /* Revision ID */ #define FF_DEFINED 86604 /* Revision ID */
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#include "ffconf.h" /* FatFs configuration options */ #include "ffconf.h" /* FatFs configuration options */
#if FF_DEFINED != FFCONF_DEF #if FF_DEFINED != FFCONF_DEF
#error Wrong configuration file (ffconf.h). #error Wrong configuration file (ffconf.h).
#endif #endif
/* Integer types used for FatFs API */ /* Integer types used for FatFs API */
#if defined(_WIN32) /* Main development platform */ #if defined(_WIN32) /* Main development platform */
#define FF_INTDEF 2 #define FF_INTDEF 2
#include <windows.h> #include <windows.h>
typedef unsigned __int64 QWORD; typedef unsigned __int64 QWORD;
#elif (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__cplusplus) /* C99 or later */ #elif (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__cplusplus) /* C99 or later */
#define FF_INTDEF 2 #define FF_INTDEF 2
#include <stdint.h> #include <stdint.h>
typedef unsigned int UINT; /* int must be 16-bit or 32-bit */ typedef unsigned int UINT; /* int must be 16-bit or 32-bit */
typedef unsigned char BYTE; /* char must be 8-bit */ typedef unsigned char BYTE; /* char must be 8-bit */
typedef uint16_t WORD; /* 16-bit unsigned integer */ typedef uint16_t WORD; /* 16-bit unsigned integer */
typedef uint16_t WCHAR; /* 16-bit unsigned integer */ typedef uint16_t WCHAR; /* 16-bit unsigned integer */
typedef uint32_t DWORD; /* 32-bit unsigned integer */ typedef uint32_t DWORD; /* 32-bit unsigned integer */
typedef uint64_t QWORD; /* 64-bit unsigned integer */ typedef uint64_t QWORD; /* 64-bit unsigned integer */
#else /* Earlier than C99 */ #else /* Earlier than C99 */
#define FF_INTDEF 1 #define FF_INTDEF 1
typedef unsigned int UINT; /* int must be 16-bit or 32-bit */ typedef unsigned int UINT; /* int must be 16-bit or 32-bit */
typedef unsigned char BYTE; /* char must be 8-bit */ typedef unsigned char BYTE; /* char must be 8-bit */
typedef unsigned short WORD; /* 16-bit unsigned integer */ typedef unsigned short WORD; /* 16-bit unsigned integer */
typedef unsigned short WCHAR; /* 16-bit unsigned integer */ typedef unsigned short WCHAR; /* 16-bit unsigned integer */
typedef unsigned long DWORD; /* 32-bit unsigned integer */ typedef unsigned long DWORD; /* 32-bit unsigned integer */
#endif #endif
/* Definitions of volume management */ /* Definitions of volume management */
#if FF_MULTI_PARTITION /* Multiple partition configuration */ #if FF_MULTI_PARTITION /* Multiple partition configuration */
typedef struct { typedef struct {
BYTE pd; /* Physical drive number */ BYTE pd; /* Physical drive number */
BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */ BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */
} PARTITION; } PARTITION;
extern PARTITION VolToPart[]; /* Volume - Partition resolution table */ extern PARTITION VolToPart[]; /* Volume - Partition resolution table */
#endif #endif
#if FF_STR_VOLUME_ID #if FF_STR_VOLUME_ID
#ifndef FF_VOLUME_STRS #ifndef FF_VOLUME_STRS
extern const char* VolumeStr[FF_VOLUMES]; /* User defied volume ID */ extern const char* VolumeStr[FF_VOLUMES]; /* User defied volume ID */
#endif #endif
#endif #endif
/* Type of path name strings on FatFs API */ /* Type of path name strings on FatFs API */
#ifndef _INC_TCHAR #ifndef _INC_TCHAR
#define _INC_TCHAR #define _INC_TCHAR
#if FF_USE_LFN && FF_LFN_UNICODE == 1 /* Unicode in UTF-16 encoding */ #if FF_USE_LFN && FF_LFN_UNICODE == 1 /* Unicode in UTF-16 encoding */
typedef WCHAR TCHAR; typedef WCHAR TCHAR;
#define _T(x) L ## x #define _T(x) L ## x
#define _TEXT(x) L ## x #define _TEXT(x) L ## x
#elif FF_USE_LFN && FF_LFN_UNICODE == 2 /* Unicode in UTF-8 encoding */ #elif FF_USE_LFN && FF_LFN_UNICODE == 2 /* Unicode in UTF-8 encoding */
typedef char TCHAR; typedef char TCHAR;
#define _T(x) u8 ## x #define _T(x) u8 ## x
#define _TEXT(x) u8 ## x #define _TEXT(x) u8 ## x
#elif FF_USE_LFN && FF_LFN_UNICODE == 3 /* Unicode in UTF-32 encoding */ #elif FF_USE_LFN && FF_LFN_UNICODE == 3 /* Unicode in UTF-32 encoding */
typedef DWORD TCHAR; typedef DWORD TCHAR;
#define _T(x) U ## x #define _T(x) U ## x
#define _TEXT(x) U ## x #define _TEXT(x) U ## x
#elif FF_USE_LFN && (FF_LFN_UNICODE < 0 || FF_LFN_UNICODE > 3) #elif FF_USE_LFN && (FF_LFN_UNICODE < 0 || FF_LFN_UNICODE > 3)
#error Wrong FF_LFN_UNICODE setting #error Wrong FF_LFN_UNICODE setting
#else /* ANSI/OEM code in SBCS/DBCS */ #else /* ANSI/OEM code in SBCS/DBCS */
typedef char TCHAR; typedef char TCHAR;
#define _T(x) x #define _T(x) x
#define _TEXT(x) x #define _TEXT(x) x
#endif #endif
#endif #endif
/* Type of file size variables */ /* Type of file size variables */
#if FF_FS_EXFAT #if FF_FS_EXFAT
#if FF_INTDEF != 2 #if FF_INTDEF != 2
#error exFAT feature wants C99 or later #error exFAT feature wants C99 or later
#endif #endif
typedef QWORD FSIZE_t; typedef QWORD FSIZE_t;
#else #else
typedef DWORD FSIZE_t; typedef DWORD FSIZE_t;
#endif #endif
/* Filesystem object structure (FATFS) */ /* Filesystem object structure (FATFS) */
typedef struct { typedef struct {
BYTE fs_type; /* Filesystem type (0:not mounted) */ BYTE fs_type; /* Filesystem type (0:not mounted) */
BYTE pdrv; /* Associated physical drive */ BYTE pdrv; /* Associated physical drive */
BYTE n_fats; /* Number of FATs (1 or 2) */ BYTE n_fats; /* Number of FATs (1 or 2) */
BYTE wflag; /* win[] flag (b0:dirty) */ BYTE wflag; /* win[] flag (b0:dirty) */
BYTE fsi_flag; /* FSINFO flags (b7:disabled, b0:dirty) */ BYTE fsi_flag; /* FSINFO flags (b7:disabled, b0:dirty) */
WORD id; /* Volume mount ID */ WORD id; /* Volume mount ID */
WORD n_rootdir; /* Number of root directory entries (FAT12/16) */ WORD n_rootdir; /* Number of root directory entries (FAT12/16) */
WORD csize; /* Cluster size [sectors] */ WORD csize; /* Cluster size [sectors] */
#if FF_MAX_SS != FF_MIN_SS #if FF_MAX_SS != FF_MIN_SS
WORD ssize; /* Sector size (512, 1024, 2048 or 4096) */ WORD ssize; /* Sector size (512, 1024, 2048 or 4096) */
#endif #endif
#if FF_USE_LFN #if FF_USE_LFN
WCHAR* lfnbuf; /* LFN working buffer */ WCHAR* lfnbuf; /* LFN working buffer */
#endif #endif
#if FF_FS_EXFAT #if FF_FS_EXFAT
BYTE* dirbuf; /* Directory entry block scratchpad buffer for exFAT */ BYTE* dirbuf; /* Directory entry block scratchpad buffer for exFAT */
#endif #endif
#if FF_FS_REENTRANT #if FF_FS_REENTRANT
FF_SYNC_t sobj; /* Identifier of sync object */ FF_SYNC_t sobj; /* Identifier of sync object */
#endif #endif
#if !FF_FS_READONLY #if !FF_FS_READONLY
DWORD last_clst; /* Last allocated cluster */ DWORD last_clst; /* Last allocated cluster */
DWORD free_clst; /* Number of free clusters */ DWORD free_clst; /* Number of free clusters */
#endif #endif
#if FF_FS_RPATH #if FF_FS_RPATH
DWORD cdir; /* Current directory start cluster (0:root) */ DWORD cdir; /* Current directory start cluster (0:root) */
#if FF_FS_EXFAT #if FF_FS_EXFAT
DWORD cdc_scl; /* Containing directory start cluster (invalid when cdir is 0) */ DWORD cdc_scl; /* Containing directory start cluster (invalid when cdir is 0) */
DWORD cdc_size; /* b31-b8:Size of containing directory, b7-b0: Chain status */ DWORD cdc_size; /* b31-b8:Size of containing directory, b7-b0: Chain status */
DWORD cdc_ofs; /* Offset in the containing directory (invalid when cdir is 0) */ DWORD cdc_ofs; /* Offset in the containing directory (invalid when cdir is 0) */
#endif #endif
#endif #endif
DWORD n_fatent; /* Number of FAT entries (number of clusters + 2) */ DWORD n_fatent; /* Number of FAT entries (number of clusters + 2) */
DWORD fsize; /* Size of an FAT [sectors] */ DWORD fsize; /* Size of an FAT [sectors] */
DWORD volbase; /* Volume base sector */ DWORD volbase; /* Volume base sector */
DWORD fatbase; /* FAT base sector */ DWORD fatbase; /* FAT base sector */
DWORD dirbase; /* Root directory base sector/cluster */ DWORD dirbase; /* Root directory base sector/cluster */
DWORD database; /* Data base sector */ DWORD database; /* Data base sector */
#if FF_FS_EXFAT #if FF_FS_EXFAT
DWORD bitbase; /* Allocation bitmap base sector */ DWORD bitbase; /* Allocation bitmap base sector */
#endif #endif
DWORD winsect; /* Current sector appearing in the win[] */ DWORD winsect; /* Current sector appearing in the win[] */
BYTE win[FF_MAX_SS]; /* Disk access window for Directory, FAT (and file data at tiny cfg) */ BYTE win[FF_MAX_SS]; /* Disk access window for Directory, FAT (and file data at tiny cfg) */
} FATFS; } FATFS;
/* Object ID and allocation information (FFOBJID) */ /* Object ID and allocation information (FFOBJID) */
typedef struct { typedef struct {
FATFS* fs; /* Pointer to the hosting volume of this object */ FATFS* fs; /* Pointer to the hosting volume of this object */
WORD id; /* Hosting volume mount ID */ WORD id; /* Hosting volume mount ID */
BYTE attr; /* Object attribute */ BYTE attr; /* Object attribute */
BYTE stat; /* Object chain status (b1-0: =0:not contiguous, =2:contiguous, =3:fragmented in this session, b2:sub-directory stretched) */ BYTE stat; /* Object chain status (b1-0: =0:not contiguous, =2:contiguous, =3:fragmented in this session, b2:sub-directory stretched) */
DWORD sclust; /* Object data start cluster (0:no cluster or root directory) */ DWORD sclust; /* Object data start cluster (0:no cluster or root directory) */
FSIZE_t objsize; /* Object size (valid when sclust != 0) */ FSIZE_t objsize; /* Object size (valid when sclust != 0) */
#if FF_FS_EXFAT #if FF_FS_EXFAT
DWORD n_cont; /* Size of first fragment - 1 (valid when stat == 3) */ DWORD n_cont; /* Size of first fragment - 1 (valid when stat == 3) */
DWORD n_frag; /* Size of last fragment needs to be written to FAT (valid when not zero) */ DWORD n_frag; /* Size of last fragment needs to be written to FAT (valid when not zero) */
DWORD c_scl; /* Containing directory start cluster (valid when sclust != 0) */ DWORD c_scl; /* Containing directory start cluster (valid when sclust != 0) */
DWORD c_size; /* b31-b8:Size of containing directory, b7-b0: Chain status (valid when c_scl != 0) */ DWORD c_size; /* b31-b8:Size of containing directory, b7-b0: Chain status (valid when c_scl != 0) */
DWORD c_ofs; /* Offset in the containing directory (valid when file object and sclust != 0) */ DWORD c_ofs; /* Offset in the containing directory (valid when file object and sclust != 0) */
#endif #endif
#if FF_FS_LOCK #if FF_FS_LOCK
UINT lockid; /* File lock ID origin from 1 (index of file semaphore table Files[]) */ UINT lockid; /* File lock ID origin from 1 (index of file semaphore table Files[]) */
#endif #endif
} FFOBJID; } FFOBJID;
/* File object structure (FIL) */ /* File object structure (FIL) */
typedef struct { typedef struct {
FFOBJID obj; /* Object identifier (must be the 1st member to detect invalid object pointer) */ FFOBJID obj; /* Object identifier (must be the 1st member to detect invalid object pointer) */
BYTE flag; /* File status flags */ BYTE flag; /* File status flags */
BYTE err; /* Abort flag (error code) */ BYTE err; /* Abort flag (error code) */
FSIZE_t fptr; /* File read/write pointer (Zeroed on file open) */ FSIZE_t fptr; /* File read/write pointer (Zeroed on file open) */
DWORD clust; /* Current cluster of fpter (invalid when fptr is 0) */ DWORD clust; /* Current cluster of fpter (invalid when fptr is 0) */
DWORD sect; /* Sector number appearing in buf[] (0:invalid) */ DWORD sect; /* Sector number appearing in buf[] (0:invalid) */
#if !FF_FS_READONLY #if !FF_FS_READONLY
DWORD dir_sect; /* Sector number containing the directory entry (not used at exFAT) */ DWORD dir_sect; /* Sector number containing the directory entry (not used at exFAT) */
BYTE* dir_ptr; /* Pointer to the directory entry in the win[] (not used at exFAT) */ BYTE* dir_ptr; /* Pointer to the directory entry in the win[] (not used at exFAT) */
#endif #endif
#if FF_USE_FASTSEEK #if FF_USE_FASTSEEK
DWORD* cltbl; /* Pointer to the cluster link map table (nulled on open, set by application) */ DWORD* cltbl; /* Pointer to the cluster link map table (nulled on open, set by application) */
#endif #endif
#if !FF_FS_TINY #if !FF_FS_TINY
BYTE buf[FF_MAX_SS]; /* File private data read/write window */ BYTE buf[FF_MAX_SS]; /* File private data read/write window */
#endif #endif
} FIL; } FIL;
/* Directory object structure (DIR) */ /* Directory object structure (DIR) */
typedef struct { typedef struct {
FFOBJID obj; /* Object identifier */ FFOBJID obj; /* Object identifier */
DWORD dptr; /* Current read/write offset */ DWORD dptr; /* Current read/write offset */
DWORD clust; /* Current cluster */ DWORD clust; /* Current cluster */
DWORD sect; /* Current sector (0:Read operation has terminated) */ DWORD sect; /* Current sector (0:Read operation has terminated) */
BYTE* dir; /* Pointer to the directory item in the win[] */ BYTE* dir; /* Pointer to the directory item in the win[] */
BYTE fn[12]; /* SFN (in/out) {body[8],ext[3],status[1]} */ BYTE fn[12]; /* SFN (in/out) {body[8],ext[3],status[1]} */
#if FF_USE_LFN #if FF_USE_LFN
DWORD blk_ofs; /* Offset of current entry block being processed (0xFFFFFFFF:Invalid) */ DWORD blk_ofs; /* Offset of current entry block being processed (0xFFFFFFFF:Invalid) */
#endif #endif
#if FF_USE_FIND #if FF_USE_FIND
const TCHAR* pat; /* Pointer to the name matching pattern */ const TCHAR* pat; /* Pointer to the name matching pattern */
#endif #endif
} DIR; } DIR;
/* File information structure (FILINFO) */ /* File information structure (FILINFO) */
typedef struct { typedef struct {
FSIZE_t fsize; /* File size */ FSIZE_t fsize; /* File size */
WORD fdate; /* Modified date */ WORD fdate; /* Modified date */
WORD ftime; /* Modified time */ WORD ftime; /* Modified time */
BYTE fattrib; /* File attribute */ BYTE fattrib; /* File attribute */
#if FF_USE_LFN #if FF_USE_LFN
TCHAR altname[FF_SFN_BUF + 1];/* Altenative file name */ TCHAR altname[FF_SFN_BUF + 1];/* Altenative file name */
TCHAR fname[FF_LFN_BUF + 1]; /* Primary file name */ TCHAR fname[FF_LFN_BUF + 1]; /* Primary file name */
#else #else
TCHAR fname[12 + 1]; /* File name */ TCHAR fname[12 + 1]; /* File name */
#endif #endif
} FILINFO; } FILINFO;
/* File function return code (FRESULT) */ /* File function return code (FRESULT) */
typedef enum { typedef enum {
FR_OK = 0, /* (0) Succeeded */ FR_OK = 0, /* (0) Succeeded */
FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */ FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */
FR_INT_ERR, /* (2) Assertion failed */ FR_INT_ERR, /* (2) Assertion failed */
FR_NOT_READY, /* (3) The physical drive cannot work */ FR_NOT_READY, /* (3) The physical drive cannot work */
FR_NO_FILE, /* (4) Could not find the file */ FR_NO_FILE, /* (4) Could not find the file */
FR_NO_PATH, /* (5) Could not find the path */ FR_NO_PATH, /* (5) Could not find the path */
FR_INVALID_NAME, /* (6) The path name format is invalid */ FR_INVALID_NAME, /* (6) The path name format is invalid */
FR_DENIED, /* (7) Access denied due to prohibited access or directory full */ FR_DENIED, /* (7) Access denied due to prohibited access or directory full */
FR_EXIST, /* (8) Access denied due to prohibited access */ FR_EXIST, /* (8) Access denied due to prohibited access */
FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */ FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */
FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */ FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */
FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */ FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */
FR_NOT_ENABLED, /* (12) The volume has no work area */ FR_NOT_ENABLED, /* (12) The volume has no work area */
FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */ FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */
FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any problem */ FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any problem */
FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */ FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */
FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */ FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */
FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */ FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */
FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > FF_FS_LOCK */ FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > FF_FS_LOCK */
FR_INVALID_PARAMETER /* (19) Given parameter is invalid */ FR_INVALID_PARAMETER /* (19) Given parameter is invalid */
} FRESULT; } FRESULT;
/*--------------------------------------------------------------*/ /*--------------------------------------------------------------*/
/* FatFs module application interface */ /* FatFs module application interface */
FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode); /* Open or create a file */ FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode); /* Open or create a file */
FRESULT f_close (FIL* fp); /* Close an open file object */ FRESULT f_close (FIL* fp); /* Close an open file object */
FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br); /* Read data from the file */ FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br); /* Read data from the file */
FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw); /* Write data to the file */ FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw); /* Write data to the file */
FRESULT f_lseek (FIL* fp, FSIZE_t ofs); /* Move file pointer of the file object */ FRESULT f_lseek (FIL* fp, FSIZE_t ofs); /* Move file pointer of the file object */
FRESULT f_truncate (FIL* fp); /* Truncate the file */ FRESULT f_truncate (FIL* fp); /* Truncate the file */
FRESULT f_sync (FIL* fp); /* Flush cached data of the writing file */ FRESULT f_sync (FIL* fp); /* Flush cached data of the writing file */
FRESULT f_opendir (DIR* dp, const TCHAR* path); /* Open a directory */ FRESULT f_opendir (DIR* dp, const TCHAR* path); /* Open a directory */
FRESULT f_closedir (DIR* dp); /* Close an open directory */ FRESULT f_closedir (DIR* dp); /* Close an open directory */
FRESULT f_readdir (DIR* dp, FILINFO* fno); /* Read a directory item */ FRESULT f_readdir (DIR* dp, FILINFO* fno); /* Read a directory item */
FRESULT f_findfirst (DIR* dp, FILINFO* fno, const TCHAR* path, const TCHAR* pattern); /* Find first file */ FRESULT f_findfirst (DIR* dp, FILINFO* fno, const TCHAR* path, const TCHAR* pattern); /* Find first file */
FRESULT f_findnext (DIR* dp, FILINFO* fno); /* Find next file */ FRESULT f_findnext (DIR* dp, FILINFO* fno); /* Find next file */
FRESULT f_mkdir (const TCHAR* path); /* Create a sub directory */ FRESULT f_mkdir (const TCHAR* path); /* Create a sub directory */
FRESULT f_unlink (const TCHAR* path); /* Delete an existing file or directory */ FRESULT f_unlink (const TCHAR* path); /* Delete an existing file or directory */
FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new); /* Rename/Move a file or directory */ FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new); /* Rename/Move a file or directory */
FRESULT f_stat (const TCHAR* path, FILINFO* fno); /* Get file status */ FRESULT f_stat (const TCHAR* path, FILINFO* fno); /* Get file status */
FRESULT f_chmod (const TCHAR* path, BYTE attr, BYTE mask); /* Change attribute of a file/dir */ FRESULT f_chmod (const TCHAR* path, BYTE attr, BYTE mask); /* Change attribute of a file/dir */
FRESULT f_utime (const TCHAR* path, const FILINFO* fno); /* Change timestamp of a file/dir */ FRESULT f_utime (const TCHAR* path, const FILINFO* fno); /* Change timestamp of a file/dir */
FRESULT f_chdir (const TCHAR* path); /* Change current directory */ FRESULT f_chdir (const TCHAR* path); /* Change current directory */
FRESULT f_chdrive (const TCHAR* path); /* Change current drive */ FRESULT f_chdrive (const TCHAR* path); /* Change current drive */
FRESULT f_getcwd (TCHAR* buff, UINT len); /* Get current directory */ FRESULT f_getcwd (TCHAR* buff, UINT len); /* Get current directory */
FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get number of free clusters on the drive */ FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get number of free clusters on the drive */
FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn); /* Get volume label */ FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn); /* Get volume label */
FRESULT f_setlabel (const TCHAR* label); /* Set volume label */ FRESULT f_setlabel (const TCHAR* label); /* Set volume label */
FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */ FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */
FRESULT f_expand (FIL* fp, FSIZE_t szf, BYTE opt); /* Allocate a contiguous block to the file */ FRESULT f_expand (FIL* fp, FSIZE_t szf, BYTE opt); /* Allocate a contiguous block to the file */
FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt); /* Mount/Unmount a logical drive */ FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt); /* Mount/Unmount a logical drive */
FRESULT f_mkfs (const TCHAR* path, BYTE opt, DWORD au, void* work, UINT len); /* Create a FAT volume */ FRESULT f_mkfs (const TCHAR* path, BYTE opt, DWORD au, void* work, UINT len); /* Create a FAT volume */
FRESULT f_fdisk (BYTE pdrv, const DWORD* szt, void* work); /* Divide a physical drive into some partitions */ FRESULT f_fdisk (BYTE pdrv, const DWORD* szt, void* work); /* Divide a physical drive into some partitions */
FRESULT f_setcp (WORD cp); /* Set current code page */ FRESULT f_setcp (WORD cp); /* Set current code page */
int f_putc (TCHAR c, FIL* fp); /* Put a character to the file */ int f_putc (TCHAR c, FIL* fp); /* Put a character to the file */
int f_puts (const TCHAR* str, FIL* cp); /* Put a string to the file */ int f_puts (const TCHAR* str, FIL* cp); /* Put a string to the file */
int f_printf (FIL* fp, const TCHAR* str, ...); /* Put a formatted string to the file */ int f_printf (FIL* fp, const TCHAR* str, ...); /* Put a formatted string to the file */
TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the file */ TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the file */
#define f_eof(fp) ((int)((fp)->fptr == (fp)->obj.objsize)) #define f_eof(fp) ((int)((fp)->fptr == (fp)->obj.objsize))
#define f_error(fp) ((fp)->err) #define f_error(fp) ((fp)->err)
#define f_tell(fp) ((fp)->fptr) #define f_tell(fp) ((fp)->fptr)
#define f_size(fp) ((fp)->obj.objsize) #define f_size(fp) ((fp)->obj.objsize)
#define f_rewind(fp) f_lseek((fp), 0) #define f_rewind(fp) f_lseek((fp), 0)
#define f_rewinddir(dp) f_readdir((dp), 0) #define f_rewinddir(dp) f_readdir((dp), 0)
#define f_rmdir(path) f_unlink(path) #define f_rmdir(path) f_unlink(path)
#define f_unmount(path) f_mount(0, path, 0) #define f_unmount(path) f_mount(0, path, 0)
#ifndef EOF #ifndef EOF
#define EOF (-1) #define EOF (-1)
#endif #endif
/*--------------------------------------------------------------*/ /*--------------------------------------------------------------*/
/* Additional user defined functions */ /* Additional user defined functions */
/* RTC function */ /* RTC function */
#if !FF_FS_READONLY && !FF_FS_NORTC #if !FF_FS_READONLY && !FF_FS_NORTC
DWORD get_fattime (void); DWORD get_fattime (void);
#endif #endif
/* LFN support functions */ /* LFN support functions */
#if FF_USE_LFN >= 1 /* Code conversion (defined in unicode.c) */ #if FF_USE_LFN >= 1 /* Code conversion (defined in unicode.c) */
WCHAR ff_oem2uni (WCHAR oem, WORD cp); /* OEM code to Unicode conversion */ WCHAR ff_oem2uni (WCHAR oem, WORD cp); /* OEM code to Unicode conversion */
WCHAR ff_uni2oem (DWORD uni, WORD cp); /* Unicode to OEM code conversion */ WCHAR ff_uni2oem (DWORD uni, WORD cp); /* Unicode to OEM code conversion */
DWORD ff_wtoupper (DWORD uni); /* Unicode upper-case conversion */ DWORD ff_wtoupper (DWORD uni); /* Unicode upper-case conversion */
#endif #endif
#if FF_USE_LFN == 3 /* Dynamic memory allocation */ #if FF_USE_LFN == 3 /* Dynamic memory allocation */
void* ff_memalloc (UINT msize); /* Allocate memory block */ void* ff_memalloc (UINT msize); /* Allocate memory block */
void ff_memfree (void* mblock); /* Free memory block */ void ff_memfree (void* mblock); /* Free memory block */
#endif #endif
/* Sync functions */ /* Sync functions */
#if FF_FS_REENTRANT #if FF_FS_REENTRANT
int ff_cre_syncobj (BYTE vol, FF_SYNC_t* sobj); /* Create a sync object */ int ff_cre_syncobj (BYTE vol, FF_SYNC_t* sobj); /* Create a sync object */
int ff_req_grant (FF_SYNC_t sobj); /* Lock sync object */ int ff_req_grant (FF_SYNC_t sobj); /* Lock sync object */
void ff_rel_grant (FF_SYNC_t sobj); /* Unlock sync object */ void ff_rel_grant (FF_SYNC_t sobj); /* Unlock sync object */
int ff_del_syncobj (FF_SYNC_t sobj); /* Delete a sync object */ int ff_del_syncobj (FF_SYNC_t sobj); /* Delete a sync object */
#endif #endif
/*--------------------------------------------------------------*/ /*--------------------------------------------------------------*/
/* Flags and offset address */ /* Flags and offset address */
/* File access mode and open method flags (3rd argument of f_open) */ /* File access mode and open method flags (3rd argument of f_open) */
#define FA_READ 0x01 #define FA_READ 0x01
#define FA_WRITE 0x02 #define FA_WRITE 0x02
#define FA_OPEN_EXISTING 0x00 #define FA_OPEN_EXISTING 0x00
#define FA_CREATE_NEW 0x04 #define FA_CREATE_NEW 0x04
#define FA_CREATE_ALWAYS 0x08 #define FA_CREATE_ALWAYS 0x08
#define FA_OPEN_ALWAYS 0x10 #define FA_OPEN_ALWAYS 0x10
#define FA_OPEN_APPEND 0x30 #define FA_OPEN_APPEND 0x30
/* Fast seek controls (2nd argument of f_lseek) */ /* Fast seek controls (2nd argument of f_lseek) */
#define CREATE_LINKMAP ((FSIZE_t)0 - 1) #define CREATE_LINKMAP ((FSIZE_t)0 - 1)
/* Format options (2nd argument of f_mkfs) */ /* Format options (2nd argument of f_mkfs) */
#define FM_FAT 0x01 #define FM_FAT 0x01
#define FM_FAT32 0x02 #define FM_FAT32 0x02
#define FM_EXFAT 0x04 #define FM_EXFAT 0x04
#define FM_ANY 0x07 #define FM_ANY 0x07
#define FM_SFD 0x08 #define FM_SFD 0x08
/* Filesystem type (FATFS.fs_type) */ /* Filesystem type (FATFS.fs_type) */
#define FS_FAT12 1 #define FS_FAT12 1
#define FS_FAT16 2 #define FS_FAT16 2
#define FS_FAT32 3 #define FS_FAT32 3
#define FS_EXFAT 4 #define FS_EXFAT 4
/* File attribute bits for directory entry (FILINFO.fattrib) */ /* File attribute bits for directory entry (FILINFO.fattrib) */
#define AM_RDO 0x01 /* Read only */ #define AM_RDO 0x01 /* Read only */
#define AM_HID 0x02 /* Hidden */ #define AM_HID 0x02 /* Hidden */
#define AM_SYS 0x04 /* System */ #define AM_SYS 0x04 /* System */
#define AM_DIR 0x10 /* Directory */ #define AM_DIR 0x10 /* Directory */
#define AM_ARC 0x20 /* Archive */ #define AM_ARC 0x20 /* Archive */
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* FF_DEFINED */ #endif /* FF_DEFINED */
/*---------------------------------------------------------------------------/ /*---------------------------------------------------------------------------/
/ FatFs Functional Configurations / FatFs Functional Configurations
/---------------------------------------------------------------------------*/ /---------------------------------------------------------------------------*/
#define FFCONF_DEF 86604 /* Revision ID */ #define FFCONF_DEF 86604 /* Revision ID */
#include "user_config.h" #include "user_config.h"
/*---------------------------------------------------------------------------/ /*---------------------------------------------------------------------------/
/ Function Configurations / Function Configurations
/---------------------------------------------------------------------------*/ /---------------------------------------------------------------------------*/
#define FF_FS_READONLY 0 #define FF_FS_READONLY 0
/* This option switches read-only configuration. (0:Read/Write or 1:Read-only) /* This option switches read-only configuration. (0:Read/Write or 1:Read-only)
/ Read-only configuration removes writing API functions, f_write(), f_sync(), / Read-only configuration removes writing API functions, f_write(), f_sync(),
/ f_unlink(), f_mkdir(), f_chmod(), f_rename(), f_truncate(), f_getfree() / f_unlink(), f_mkdir(), f_chmod(), f_rename(), f_truncate(), f_getfree()
/ and optional writing functions as well. */ / and optional writing functions as well. */
#define FF_FS_MINIMIZE 0 #define FF_FS_MINIMIZE 0
/* This option defines minimization level to remove some basic API functions. /* This option defines minimization level to remove some basic API functions.
/ /
/ 0: Basic functions are fully enabled. / 0: Basic functions are fully enabled.
/ 1: f_stat(), f_getfree(), f_unlink(), f_mkdir(), f_truncate() and f_rename() / 1: f_stat(), f_getfree(), f_unlink(), f_mkdir(), f_truncate() and f_rename()
/ are removed. / are removed.
/ 2: f_opendir(), f_readdir() and f_closedir() are removed in addition to 1. / 2: f_opendir(), f_readdir() and f_closedir() are removed in addition to 1.
/ 3: f_lseek() function is removed in addition to 2. */ / 3: f_lseek() function is removed in addition to 2. */
#define FF_USE_STRFUNC 0 #define FF_USE_STRFUNC 0
/* This option switches string functions, f_gets(), f_putc(), f_puts() and f_printf(). /* This option switches string functions, f_gets(), f_putc(), f_puts() and f_printf().
/ /
/ 0: Disable string functions. / 0: Disable string functions.
/ 1: Enable without LF-CRLF conversion. / 1: Enable without LF-CRLF conversion.
/ 2: Enable with LF-CRLF conversion. */ / 2: Enable with LF-CRLF conversion. */
#define FF_USE_FIND 0 #define FF_USE_FIND 0
/* This option switches filtered directory read functions, f_findfirst() and /* This option switches filtered directory read functions, f_findfirst() and
/ f_findnext(). (0:Disable, 1:Enable 2:Enable with matching altname[] too) */ / f_findnext(). (0:Disable, 1:Enable 2:Enable with matching altname[] too) */
#define FF_USE_MKFS 0 #define FF_USE_MKFS 0
/* This option switches f_mkfs() function. (0:Disable or 1:Enable) */ /* This option switches f_mkfs() function. (0:Disable or 1:Enable) */
#define FF_USE_FASTSEEK 0 #define FF_USE_FASTSEEK 0
/* This option switches fast seek function. (0:Disable or 1:Enable) */ /* This option switches fast seek function. (0:Disable or 1:Enable) */
#define FF_USE_EXPAND 0 #define FF_USE_EXPAND 0
/* This option switches f_expand function. (0:Disable or 1:Enable) */ /* This option switches f_expand function. (0:Disable or 1:Enable) */
#define FF_USE_CHMOD 1 #define FF_USE_CHMOD 1
/* This option switches attribute manipulation functions, f_chmod() and f_utime(). /* This option switches attribute manipulation functions, f_chmod() and f_utime().
/ (0:Disable or 1:Enable) Also FF_FS_READONLY needs to be 0 to enable this option. */ / (0:Disable or 1:Enable) Also FF_FS_READONLY needs to be 0 to enable this option. */
#define FF_USE_LABEL 1 #define FF_USE_LABEL 1
/* This option switches volume label functions, f_getlabel() and f_setlabel(). /* This option switches volume label functions, f_getlabel() and f_setlabel().
/ (0:Disable or 1:Enable) */ / (0:Disable or 1:Enable) */
#define FF_USE_FORWARD 0 #define FF_USE_FORWARD 0
/* This option switches f_forward() function. (0:Disable or 1:Enable) */ /* This option switches f_forward() function. (0:Disable or 1:Enable) */
/*---------------------------------------------------------------------------/ /*---------------------------------------------------------------------------/
/ Locale and Namespace Configurations / Locale and Namespace Configurations
/---------------------------------------------------------------------------*/ /---------------------------------------------------------------------------*/
#define FF_CODE_PAGE 932 #define FF_CODE_PAGE 932
/* This option specifies the OEM code page to be used on the target system. /* This option specifies the OEM code page to be used on the target system.
/ Incorrect code page setting can cause a file open failure. / Incorrect code page setting can cause a file open failure.
/ /
/ 437 - U.S. / 437 - U.S.
/ 720 - Arabic / 720 - Arabic
/ 737 - Greek / 737 - Greek
/ 771 - KBL / 771 - KBL
/ 775 - Baltic / 775 - Baltic
/ 850 - Latin 1 / 850 - Latin 1
/ 852 - Latin 2 / 852 - Latin 2
/ 855 - Cyrillic / 855 - Cyrillic
/ 857 - Turkish / 857 - Turkish
/ 860 - Portuguese / 860 - Portuguese
/ 861 - Icelandic / 861 - Icelandic
/ 862 - Hebrew / 862 - Hebrew
/ 863 - Canadian French / 863 - Canadian French
/ 864 - Arabic / 864 - Arabic
/ 865 - Nordic / 865 - Nordic
/ 866 - Russian / 866 - Russian
/ 869 - Greek 2 / 869 - Greek 2
/ 932 - Japanese (DBCS) / 932 - Japanese (DBCS)
/ 936 - Simplified Chinese (DBCS) / 936 - Simplified Chinese (DBCS)
/ 949 - Korean (DBCS) / 949 - Korean (DBCS)
/ 950 - Traditional Chinese (DBCS) / 950 - Traditional Chinese (DBCS)
/ 0 - Include all code pages above and configured by f_setcp() / 0 - Include all code pages above and configured by f_setcp()
*/ */
#define FF_USE_LFN 3 #define FF_USE_LFN 3
#define FF_MAX_LFN (FS_OBJ_NAME_LEN+1+1) #define FF_MAX_LFN (FS_OBJ_NAME_LEN+1+1)
/* The FF_USE_LFN switches the support for LFN (long file name). /* The FF_USE_LFN switches the support for LFN (long file name).
/ /
/ 0: Disable LFN. FF_MAX_LFN has no effect. / 0: Disable LFN. FF_MAX_LFN has no effect.
/ 1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe. / 1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe.
/ 2: Enable LFN with dynamic working buffer on the STACK. / 2: Enable LFN with dynamic working buffer on the STACK.
/ 3: Enable LFN with dynamic working buffer on the HEAP. / 3: Enable LFN with dynamic working buffer on the HEAP.
/ /
/ To enable the LFN, ffunicode.c needs to be added to the project. The LFN function / To enable the LFN, ffunicode.c needs to be added to the project. The LFN function
/ requiers certain internal working buffer occupies (FF_MAX_LFN + 1) * 2 bytes and / requiers certain internal working buffer occupies (FF_MAX_LFN + 1) * 2 bytes and
/ additional (FF_MAX_LFN + 44) / 15 * 32 bytes when exFAT is enabled. / additional (FF_MAX_LFN + 44) / 15 * 32 bytes when exFAT is enabled.
/ The FF_MAX_LFN defines size of the working buffer in UTF-16 code unit and it can / The FF_MAX_LFN defines size of the working buffer in UTF-16 code unit and it can
/ be in range of 12 to 255. It is recommended to be set 255 to fully support LFN / be in range of 12 to 255. It is recommended to be set 255 to fully support LFN
/ specification. / specification.
/ When use stack for the working buffer, take care on stack overflow. When use heap / When use stack for the working buffer, take care on stack overflow. When use heap
/ memory for the working buffer, memory management functions, ff_memalloc() and / memory for the working buffer, memory management functions, ff_memalloc() and
/ ff_memfree() in ffsystem.c, need to be added to the project. */ / ff_memfree() in ffsystem.c, need to be added to the project. */
#define FF_LFN_UNICODE 0 #define FF_LFN_UNICODE 0
/* This option switches the character encoding on the API when LFN is enabled. /* This option switches the character encoding on the API when LFN is enabled.
/ /
/ 0: ANSI/OEM in current CP (TCHAR = char) / 0: ANSI/OEM in current CP (TCHAR = char)
/ 1: Unicode in UTF-16 (TCHAR = WCHAR) / 1: Unicode in UTF-16 (TCHAR = WCHAR)
/ 2: Unicode in UTF-8 (TCHAR = char) / 2: Unicode in UTF-8 (TCHAR = char)
/ 3: Unicode in UTF-32 (TCHAR = DWORD) / 3: Unicode in UTF-32 (TCHAR = DWORD)
/ /
/ Also behavior of string I/O functions will be affected by this option. / Also behavior of string I/O functions will be affected by this option.
/ When LFN is not enabled, this option has no effect. */ / When LFN is not enabled, this option has no effect. */
#define FF_LFN_BUF 255 #define FF_LFN_BUF 255
#define FF_SFN_BUF 12 #define FF_SFN_BUF 12
/* This set of options defines size of file name members in the FILINFO structure /* This set of options defines size of file name members in the FILINFO structure
/ which is used to read out directory items. These values should be suffcient for / which is used to read out directory items. These values should be suffcient for
/ the file names to read. The maximum possible length of the read file name depends / the file names to read. The maximum possible length of the read file name depends
/ on character encoding. When LFN is not enabled, these options have no effect. */ / on character encoding. When LFN is not enabled, these options have no effect. */
#define FF_STRF_ENCODE 3 #define FF_STRF_ENCODE 3
/* When FF_LFN_UNICODE >= 1 with LFN enabled, string I/O functions, f_gets(), /* When FF_LFN_UNICODE >= 1 with LFN enabled, string I/O functions, f_gets(),
/ f_putc(), f_puts and f_printf() convert the character encoding in it. / f_putc(), f_puts and f_printf() convert the character encoding in it.
/ This option selects assumption of character encoding ON THE FILE to be / This option selects assumption of character encoding ON THE FILE to be
/ read/written via those functions. / read/written via those functions.
/ /
/ 0: ANSI/OEM in current CP / 0: ANSI/OEM in current CP
/ 1: Unicode in UTF-16LE / 1: Unicode in UTF-16LE
/ 2: Unicode in UTF-16BE / 2: Unicode in UTF-16BE
/ 3: Unicode in UTF-8 / 3: Unicode in UTF-8
*/ */
#define FF_FS_RPATH 2 #define FF_FS_RPATH 2
/* This option configures support for relative path. /* This option configures support for relative path.
/ /
/ 0: Disable relative path and remove related functions. / 0: Disable relative path and remove related functions.
/ 1: Enable relative path. f_chdir() and f_chdrive() are available. / 1: Enable relative path. f_chdir() and f_chdrive() are available.
/ 2: f_getcwd() function is available in addition to 1. / 2: f_getcwd() function is available in addition to 1.
*/ */
/*---------------------------------------------------------------------------/ /*---------------------------------------------------------------------------/
/ Drive/Volume Configurations / Drive/Volume Configurations
/---------------------------------------------------------------------------*/ /---------------------------------------------------------------------------*/
#define FF_VOLUMES 4 #define FF_VOLUMES 4
/* Number of volumes (logical drives) to be used. (1-10) */ /* Number of volumes (logical drives) to be used. (1-10) */
#define FF_STR_VOLUME_ID 1 #define FF_STR_VOLUME_ID 1
#define FF_VOLUME_STRS "SD0","SD1","SD2","SD3" #define FF_VOLUME_STRS "SD0","SD1","SD2","SD3"
/* FF_STR_VOLUME_ID switches support for volume ID in arbitrary strings. /* FF_STR_VOLUME_ID switches support for volume ID in arbitrary strings.
/ When FF_STR_VOLUME_ID is set to 1 or 2, arbitrary strings can be used as drive / When FF_STR_VOLUME_ID is set to 1 or 2, arbitrary strings can be used as drive
/ number in the path name. FF_VOLUME_STRS defines the volume ID strings for each / number in the path name. FF_VOLUME_STRS defines the volume ID strings for each
/ logical drives. Number of items must not be less than FF_VOLUMES. Valid / logical drives. Number of items must not be less than FF_VOLUMES. Valid
/ characters for the volume ID strings are A-Z, a-z and 0-9, however, they are / characters for the volume ID strings are A-Z, a-z and 0-9, however, they are
/ compared in case-insensitive. If FF_STR_VOLUME_ID >= 1 and FF_VOLUME_STRS is / compared in case-insensitive. If FF_STR_VOLUME_ID >= 1 and FF_VOLUME_STRS is
/ not defined, a user defined volume string table needs to be defined as: / not defined, a user defined volume string table needs to be defined as:
/ /
/ const char* VolumeStr[FF_VOLUMES] = {"ram","flash","sd","usb",... / const char* VolumeStr[FF_VOLUMES] = {"ram","flash","sd","usb",...
*/ */
#define FF_MULTI_PARTITION 1 #define FF_MULTI_PARTITION 1
/* This option switches support for multiple volumes on the physical drive. /* This option switches support for multiple volumes on the physical drive.
/ By default (0), each logical drive number is bound to the same physical drive / By default (0), each logical drive number is bound to the same physical drive
/ number and only an FAT volume found on the physical drive will be mounted. / number and only an FAT volume found on the physical drive will be mounted.
/ When this function is enabled (1), each logical drive number can be bound to / When this function is enabled (1), each logical drive number can be bound to
/ arbitrary physical drive and partition listed in the VolToPart[]. Also f_fdisk() / arbitrary physical drive and partition listed in the VolToPart[]. Also f_fdisk()
/ funciton will be available. */ / funciton will be available. */
#define FF_MIN_SS 512 #define FF_MIN_SS 512
#define FF_MAX_SS 512 #define FF_MAX_SS 512
/* This set of options configures the range of sector size to be supported. (512, /* This set of options configures the range of sector size to be supported. (512,
/ 1024, 2048 or 4096) Always set both 512 for most systems, generic memory card and / 1024, 2048 or 4096) Always set both 512 for most systems, generic memory card and
/ harddisk. But a larger value may be required for on-board flash memory and some / harddisk. But a larger value may be required for on-board flash memory and some
/ type of optical media. When FF_MAX_SS is larger than FF_MIN_SS, FatFs is configured / type of optical media. When FF_MAX_SS is larger than FF_MIN_SS, FatFs is configured
/ for variable sector size mode and disk_ioctl() function needs to implement / for variable sector size mode and disk_ioctl() function needs to implement
/ GET_SECTOR_SIZE command. */ / GET_SECTOR_SIZE command. */
#define FF_USE_TRIM 0 #define FF_USE_TRIM 0
/* This option switches support for ATA-TRIM. (0:Disable or 1:Enable) /* This option switches support for ATA-TRIM. (0:Disable or 1:Enable)
/ To enable Trim function, also CTRL_TRIM command should be implemented to the / To enable Trim function, also CTRL_TRIM command should be implemented to the
/ disk_ioctl() function. */ / disk_ioctl() function. */
#define FF_FS_NOFSINFO 0 #define FF_FS_NOFSINFO 0
/* If you need to know correct free space on the FAT32 volume, set bit 0 of this /* If you need to know correct free space on the FAT32 volume, set bit 0 of this
/ option, and f_getfree() function at first time after volume mount will force / option, and f_getfree() function at first time after volume mount will force
/ a full FAT scan. Bit 1 controls the use of last allocated cluster number. / a full FAT scan. Bit 1 controls the use of last allocated cluster number.
/ /
/ bit0=0: Use free cluster count in the FSINFO if available. / bit0=0: Use free cluster count in the FSINFO if available.
/ bit0=1: Do not trust free cluster count in the FSINFO. / bit0=1: Do not trust free cluster count in the FSINFO.
/ bit1=0: Use last allocated cluster number in the FSINFO if available. / bit1=0: Use last allocated cluster number in the FSINFO if available.
/ bit1=1: Do not trust last allocated cluster number in the FSINFO. / bit1=1: Do not trust last allocated cluster number in the FSINFO.
*/ */
/*---------------------------------------------------------------------------/ /*---------------------------------------------------------------------------/
/ System Configurations / System Configurations
/---------------------------------------------------------------------------*/ /---------------------------------------------------------------------------*/
#define FF_FS_TINY 0 #define FF_FS_TINY 0
/* This option switches tiny buffer configuration. (0:Normal or 1:Tiny) /* This option switches tiny buffer configuration. (0:Normal or 1:Tiny)
/ At the tiny configuration, size of file object (FIL) is shrinked FF_MAX_SS bytes. / At the tiny configuration, size of file object (FIL) is shrinked FF_MAX_SS bytes.
/ Instead of private sector buffer eliminated from the file object, common sector / Instead of private sector buffer eliminated from the file object, common sector
/ buffer in the filesystem object (FATFS) is used for the file data transfer. */ / buffer in the filesystem object (FATFS) is used for the file data transfer. */
#define FF_FS_EXFAT 0 #define FF_FS_EXFAT 0
/* This option switches support for exFAT filesystem. (0:Disable or 1:Enable) /* This option switches support for exFAT filesystem. (0:Disable or 1:Enable)
/ To enable exFAT, also LFN needs to be enabled. (FF_USE_LFN >= 1) / To enable exFAT, also LFN needs to be enabled. (FF_USE_LFN >= 1)
/ Note that enabling exFAT discards ANSI C (C89) compatibility. */ / Note that enabling exFAT discards ANSI C (C89) compatibility. */
#define FF_FS_NORTC 0 #define FF_FS_NORTC 0
#define FF_NORTC_MON 1 #define FF_NORTC_MON 1
#define FF_NORTC_MDAY 1 #define FF_NORTC_MDAY 1
#define FF_NORTC_YEAR 2018 #define FF_NORTC_YEAR 2018
/* The option FF_FS_NORTC switches timestamp functiton. If the system does not have /* The option FF_FS_NORTC switches timestamp functiton. If the system does not have
/ any RTC function or valid timestamp is not needed, set FF_FS_NORTC = 1 to disable / any RTC function or valid timestamp is not needed, set FF_FS_NORTC = 1 to disable
/ the timestamp function. Every object modified by FatFs will have a fixed timestamp / the timestamp function. Every object modified by FatFs will have a fixed timestamp
/ defined by FF_NORTC_MON, FF_NORTC_MDAY and FF_NORTC_YEAR in local time. / defined by FF_NORTC_MON, FF_NORTC_MDAY and FF_NORTC_YEAR in local time.
/ To enable timestamp function (FF_FS_NORTC = 0), get_fattime() function need to be / To enable timestamp function (FF_FS_NORTC = 0), get_fattime() function need to be
/ added to the project to read current time form real-time clock. FF_NORTC_MON, / added to the project to read current time form real-time clock. FF_NORTC_MON,
/ FF_NORTC_MDAY and FF_NORTC_YEAR have no effect. / FF_NORTC_MDAY and FF_NORTC_YEAR have no effect.
/ These options have no effect at read-only configuration (FF_FS_READONLY = 1). */ / These options have no effect at read-only configuration (FF_FS_READONLY = 1). */
#define FF_FS_LOCK 0 #define FF_FS_LOCK 0
/* The option FF_FS_LOCK switches file lock function to control duplicated file open /* The option FF_FS_LOCK switches file lock function to control duplicated file open
/ and illegal operation to open objects. This option must be 0 when FF_FS_READONLY / and illegal operation to open objects. This option must be 0 when FF_FS_READONLY
/ is 1. / is 1.
/ /
/ 0: Disable file lock function. To avoid volume corruption, application program / 0: Disable file lock function. To avoid volume corruption, application program
/ should avoid illegal open, remove and rename to the open objects. / should avoid illegal open, remove and rename to the open objects.
/ >0: Enable file lock function. The value defines how many files/sub-directories / >0: Enable file lock function. The value defines how many files/sub-directories
/ can be opened simultaneously under file lock control. Note that the file / can be opened simultaneously under file lock control. Note that the file
/ lock control is independent of re-entrancy. */ / lock control is independent of re-entrancy. */
/* #include <somertos.h> // O/S definitions */ /* #include <somertos.h> // O/S definitions */
#define FF_FS_REENTRANT 0 #define FF_FS_REENTRANT 0
#define FF_FS_TIMEOUT 1000 #define FF_FS_TIMEOUT 1000
#define FF_SYNC_t HANDLE #define FF_SYNC_t HANDLE
/* The option FF_FS_REENTRANT switches the re-entrancy (thread safe) of the FatFs /* The option FF_FS_REENTRANT switches the re-entrancy (thread safe) of the FatFs
/ module itself. Note that regardless of this option, file access to different / module itself. Note that regardless of this option, file access to different
/ volume is always re-entrant and volume control functions, f_mount(), f_mkfs() / volume is always re-entrant and volume control functions, f_mount(), f_mkfs()
/ and f_fdisk() function, are always not re-entrant. Only file/directory access / and f_fdisk() function, are always not re-entrant. Only file/directory access
/ to the same volume is under control of this function. / to the same volume is under control of this function.
/ /
/ 0: Disable re-entrancy. FF_FS_TIMEOUT and FF_SYNC_t have no effect. / 0: Disable re-entrancy. FF_FS_TIMEOUT and FF_SYNC_t have no effect.
/ 1: Enable re-entrancy. Also user provided synchronization handlers, / 1: Enable re-entrancy. Also user provided synchronization handlers,
/ ff_req_grant(), ff_rel_grant(), ff_del_syncobj() and ff_cre_syncobj() / ff_req_grant(), ff_rel_grant(), ff_del_syncobj() and ff_cre_syncobj()
/ function, must be added to the project. Samples are available in / function, must be added to the project. Samples are available in
/ option/syscall.c. / option/syscall.c.
/ /
/ The FF_FS_TIMEOUT defines timeout period in unit of time tick. / The FF_FS_TIMEOUT defines timeout period in unit of time tick.
/ The FF_SYNC_t defines O/S dependent sync object type. e.g. HANDLE, ID, OS_EVENT*, / The FF_SYNC_t defines O/S dependent sync object type. e.g. HANDLE, ID, OS_EVENT*,
/ SemaphoreHandle_t and etc. A header file for O/S definitions needs to be / SemaphoreHandle_t and etc. A header file for O/S definitions needs to be
/ included somewhere in the scope of ff.h. */ / included somewhere in the scope of ff.h. */
/*--- End of configuration options ---*/ /*--- End of configuration options ---*/
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
/* 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