i2c write works

sipo
Ondřej Hruška 7 years ago
parent 9e7ae008b6
commit f917427be4
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 2
      framework/unit.c
  2. 3
      framework/unit.h
  3. 6
      gex.mk
  4. 10
      units/digital_in/unit_din.c
  5. 6
      units/digital_out/unit_dout.c
  6. 210
      units/i2c/unit_i2c.c
  7. 2
      utils/payload_parser.h
  8. 10
      vfs/file_stream.c
  9. 78
      vfs/vfs_manager.c
  10. 6
      vfs/virtual_fs.c
  11. 4
      vfs/virtual_fs.h

@ -6,7 +6,7 @@
#include "unit.h" #include "unit.h"
#include "resources.h" #include "resources.h"
char unit_tmp64[64]; char unit_tmp512[512];
// Abort partly inited unit // Abort partly inited unit
void clean_failed_unit(Unit *unit) void clean_failed_unit(Unit *unit)

@ -11,7 +11,8 @@
#include "utils/payload_builder.h" #include "utils/payload_builder.h"
#include "utils/payload_parser.h" #include "utils/payload_parser.h"
extern char unit_tmp64[64]; // temporary static buffer extern char unit_tmp512[512]; // temporary static buffer - not expected to be accessed asynchronously
// TODO add mutex?
typedef struct unit Unit; typedef struct unit Unit;
typedef struct unit_driver UnitDriver; typedef struct unit_driver UnitDriver;

@ -81,10 +81,10 @@ else
GEX_CDEFS = $(GEX_CDEFS_BASE) \ GEX_CDEFS = $(GEX_CDEFS_BASE) \
-DUSE_FULL_ASSERT=1 \ -DUSE_FULL_ASSERT=1 \
-DVERBOSE_ASSERT=1 \ -DVERBOSE_ASSERT=1 \
-DDEBUG_VFS=1 \ -DDEBUG_VFS=0 \
-DDEBUG_FLASH_WRITE=1 \ -DDEBUG_FLASH_WRITE=0 \
-DVERBOSE_HARDFAULT=1 \ -DVERBOSE_HARDFAULT=1 \
-DUSE_STACK_MONITOR=1 \ -DUSE_STACK_MONITOR=0 \
-DUSE_DEBUG_UART=1 -DUSE_DEBUG_UART=1
endif endif

@ -85,13 +85,13 @@ static void DI_writeIni(Unit *unit, IniWriter *iw)
iw_entry(iw, "port", "%c", priv->port_name); iw_entry(iw, "port", "%c", priv->port_name);
iw_comment(iw, "Pins (comma separated, supports ranges)"); iw_comment(iw, "Pins (comma separated, supports ranges)");
iw_entry(iw, "pins", "%s", str_pinmask(priv->pins, unit_tmp64)); iw_entry(iw, "pins", "%s", str_pinmask(priv->pins, unit_tmp512));
iw_comment(iw, "Pins with pull-up"); iw_comment(iw, "Pins with pull-up");
iw_entry(iw, "pull-up", "%s", str_pinmask(priv->pullup, unit_tmp64)); iw_entry(iw, "pull-up", "%s", str_pinmask(priv->pullup, unit_tmp512));
iw_comment(iw, "Pins with pull-down"); iw_comment(iw, "Pins with pull-down");
iw_entry(iw, "pull-down", "%s", str_pinmask(priv->pulldown, unit_tmp64)); iw_entry(iw, "pull-down", "%s", str_pinmask(priv->pulldown, unit_tmp512));
#if PLAT_NO_FLOATING_INPUTS #if PLAT_NO_FLOATING_INPUTS
iw_comment(iw, "NOTE: Pins use pull-up by default.\r\n"); iw_comment(iw, "NOTE: Pins use pull-up by default.\r\n");
@ -206,9 +206,9 @@ static bool DI_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, Payloa
switch (command) { switch (command) {
case CMD_READ:; case CMD_READ:;
PayloadBuilder pb = pb_start((uint8_t*)unit_tmp64, 64, NULL); PayloadBuilder pb = pb_start((uint8_t*)unit_tmp512, 64, NULL);
pb_u16(&pb, packed); pb_u16(&pb, packed);
com_respond_buf(frame_id, MSG_SUCCESS, (uint8_t *) unit_tmp64, pb_length(&pb)); com_respond_buf(frame_id, MSG_SUCCESS, (uint8_t *) unit_tmp512, pb_length(&pb));
break; break;
default: default:

@ -81,13 +81,13 @@ static void DO_writeIni(Unit *unit, IniWriter *iw)
iw_entry(iw, "port", "%c", priv->port_name); iw_entry(iw, "port", "%c", priv->port_name);
iw_comment(iw, "Pins (comma separated, supports ranges)"); iw_comment(iw, "Pins (comma separated, supports ranges)");
iw_entry(iw, "pins", "%s", str_pinmask(priv->pins, unit_tmp64)); iw_entry(iw, "pins", "%s", str_pinmask(priv->pins, unit_tmp512));
iw_comment(iw, "Initially high pins"); iw_comment(iw, "Initially high pins");
iw_entry(iw, "initial", "%s", str_pinmask(priv->initial, unit_tmp64)); iw_entry(iw, "initial", "%s", str_pinmask(priv->initial, unit_tmp512));
iw_comment(iw, "Open-drain pins"); iw_comment(iw, "Open-drain pins");
iw_entry(iw, "opendrain", "%s", str_pinmask(priv->open_drain, unit_tmp64)); iw_entry(iw, "opendrain", "%s", str_pinmask(priv->open_drain, unit_tmp512));
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------

@ -17,6 +17,10 @@ struct priv {
uint8_t speed; //!< 0 - Standard, 1 - Fast, 2 - Fast+ uint8_t speed; //!< 0 - Standard, 1 - Fast, 2 - Fast+
I2C_TypeDef *periph; I2C_TypeDef *periph;
GPIO_TypeDef *port;
uint32_t ll_pin_scl;
uint32_t ll_pin_sda;
}; };
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@ -146,8 +150,92 @@ static bool UI2C_init(Unit *unit)
priv->periph = I2C2; priv->periph = I2C2;
} }
// TODO claim pins (config option to remap?) // This is written for F072, other platforms will need adjustments
char portname;
uint8_t pin_scl;
uint8_t pin_sda;
uint32_t af_i2c;
uint32_t timing; // magic constant from CubeMX
#if GEX_PLAT_F072_DISCOVERY
// scl - 6 or 8 for I2C1, 10 for I2C2
// sda - 7 or 9 for I2C1, 11 for I2C2
portname = 'B';
if (priv->periph_num == 1) {
pin_scl = 8;
pin_sda = 9;
} else {
pin_scl = 10;
pin_sda = 12;
}
af_i2c = LL_GPIO_AF_1;
if (priv->speed == 1)
timing = 0x00301D2B; // Standard
else if (priv->speed == 2)
timing = 0x0000020B; // Fast
else
timing = 0x00000001; // Fast+
#elif GEX_PLAT_F103_BLUEPILL
#error "NO IMPL"
#elif GEX_PLAT_F303_DISCOVERY
#error "NO IMPL"
#elif GEX_PLAT_F407_DISCOVERY
#error "NO IMPL"
#else
#error "BAD PLATFORM!"
#endif
// first, we have to claim the pins
Resource r_sda = pin2resource(portname, pin_sda, &suc);
Resource r_scl = pin2resource(portname, pin_scl, &suc);
CHECK_SUC();
rsc_claim(unit, r_sda);
rsc_claim(unit, r_scl);
CHECK_SUC();
GPIO_TypeDef *port = port2periph(portname, &suc);
uint32_t ll_pin_scl = pin2ll(pin_scl, &suc);
uint32_t ll_pin_sda = pin2ll(pin_sda, &suc);
CHECK_SUC();
// configure AF
if (pin_scl < 8) LL_GPIO_SetAFPin_0_7(port, ll_pin_scl, af_i2c);
else LL_GPIO_SetAFPin_8_15(port, ll_pin_scl, af_i2c);
if (pin_sda < 8) LL_GPIO_SetAFPin_0_7(port, ll_pin_sda, af_i2c);
else LL_GPIO_SetAFPin_8_15(port, ll_pin_sda, af_i2c);
LL_GPIO_SetPinMode(port, ll_pin_scl, LL_GPIO_MODE_ALTERNATE);
LL_GPIO_SetPinMode(port, ll_pin_sda, LL_GPIO_MODE_ALTERNATE);
// set as OpenDrain (this may not be needed - TODO check)
LL_GPIO_SetPinOutputType(port, ll_pin_scl, LL_GPIO_OUTPUT_OPENDRAIN);
LL_GPIO_SetPinOutputType(port, ll_pin_sda, LL_GPIO_OUTPUT_OPENDRAIN);
if (priv->periph_num == 1) {
__HAL_RCC_I2C1_CLK_ENABLE();
} else {
__HAL_RCC_I2C2_CLK_ENABLE();
}
/* Disable the selected I2Cx Peripheral */
LL_I2C_Disable(priv->periph);
LL_I2C_ConfigFilters(priv->periph,
priv->anf ? LL_I2C_ANALOGFILTER_ENABLE
: LL_I2C_ANALOGFILTER_DISABLE,
priv->dnf);
LL_I2C_SetTiming(priv->periph, timing);
LL_I2C_DisableClockStretching(priv->periph);
LL_I2C_Enable(priv->periph);
LL_I2C_DisableOwnAddress1(priv->periph); // OA not used
LL_I2C_SetMode(priv->periph, LL_I2C_MODE_I2C); // not using SMBus
return true; return true;
} }
@ -159,7 +247,16 @@ static void UI2C_deInit(Unit *unit)
// de-init the pins & peripheral only if inited correctly // de-init the pins & peripheral only if inited correctly
if (unit->status == E_SUCCESS) { if (unit->status == E_SUCCESS) {
// TODO LL_I2C_DeInit(priv->periph);
if (priv->periph_num == 1) {
__HAL_RCC_I2C1_CLK_DISABLE();
} else {
__HAL_RCC_I2C2_CLK_DISABLE();
}
LL_GPIO_SetPinMode(priv->port, priv->ll_pin_sda, LL_GPIO_MODE_ANALOG);
LL_GPIO_SetPinMode(priv->port, priv->ll_pin_scl, LL_GPIO_MODE_ANALOG);
} }
// Release all resources // Release all resources
@ -177,18 +274,121 @@ enum PinCmd_ {
CMD_READ = 1, CMD_READ = 1,
}; };
static void i2c_reset(struct priv *priv)
{
LL_I2C_Disable(priv->periph);
HAL_Delay(1);
LL_I2C_Enable(priv->periph);
}
static bool i2c_wait_until_flag(struct priv *priv, TF_ID frame_id, uint32_t flag,
bool stop_state, const char *msg)
{
uint32_t t_start = HAL_GetTick();
while (((priv->periph->ISR & flag)!=0) != stop_state) {
if (HAL_GetTick() - t_start > 10) {
com_respond_err(frame_id, msg);
i2c_reset(priv);
return false;
}
}
}
/** Handle a request message */ /** Handle a request message */
static bool UI2C_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, PayloadParser *pp) static bool UI2C_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, PayloadParser *pp)
{ {
struct priv *priv = unit->data; struct priv *priv = unit->data;
uint8_t addrsize; // 7 or 10
uint16_t addr;
uint32_t len;
uint32_t t_start;
switch (command) { switch (command) {
case CMD_WRITE: case CMD_WRITE:;
// addrsize = pp_u8(pp); // 7 or 10
addr = pp_u16(pp);
t_start = HAL_GetTick();
while (LL_I2C_IsActiveFlag_BUSY(priv->periph)) {
if (HAL_GetTick() - t_start > 10) {
com_respond_err(frame_id, "BUSY TIMEOUT");
i2c_reset(priv);
return false;
}
}
// if (!i2c_wait_until_flag(priv, frame_id, I2C_ISR_BUSY, 0, "BUSY TIMEOUT")) return false;
uint32_t chunk_remain;
while (pp_length(pp) > 0) {
len = pp_length(pp);
chunk_remain = (uint8_t) ((len > 255) ? 255 : len), // if more than 255, first chunk is 255
LL_I2C_HandleTransfer(priv->periph,
(addrsize == 7) ? addr << 1 : addr, // Address must be shifted to left by one if 7-bit mode is used. Bit 0 serves for R/W flag
(addrsize == 7) ? LL_I2C_ADDRSLAVE_7BIT : LL_I2C_ADDRSLAVE_10BIT, // translate to LL bitfields
(uint32_t) chunk_remain,
(len > 255) ? LL_I2C_MODE_RELOAD : LL_I2C_MODE_AUTOEND, // Autoend if this is the last chunk
LL_I2C_GENERATE_START_WRITE);
for (; chunk_remain > 0; chunk_remain--) {
t_start = HAL_GetTick();
while (!LL_I2C_IsActiveFlag_TXIS(priv->periph)) {
if (HAL_GetTick() - t_start > 10) {
com_respond_err(frame_id, "TXIS TIMEOUT");
i2c_reset(priv);
return false;
}
}
uint8_t byte = pp_u8(pp);
LL_I2C_TransmitData8(priv->periph, byte);
}
}
t_start = HAL_GetTick();
while (!LL_I2C_IsActiveFlag_STOP(priv->periph)) {
if (HAL_GetTick() - t_start > 10) {
com_respond_err(frame_id, "STOPF TIMEOUT");
i2c_reset(priv);
return false;
}
}
break; break;
case CMD_READ: case CMD_READ:
// addrsize = pp_u8(pp); // 7 or 10
addr = pp_u16(pp);
len = pp_u16(pp);
// TODO support > 255?
if (len > 256) {
com_respond_err(frame_id, "TOO LONG");
return false;
}
LL_I2C_HandleTransfer(priv->periph,
(addrsize == 7) ? addr << 1 : addr,
addrsize == 7 ? LL_I2C_ADDRSLAVE_7BIT : LL_I2C_ADDRSLAVE_10BIT,
(uint32_t) pp_length(pp),
LL_I2C_MODE_AUTOEND, // ?
LL_I2C_GENERATE_START_READ);
for (uint32_t i = 0; i < len; i++) {
t_start = HAL_GetTick();
while (!LL_I2C_IsActiveFlag_RXNE(priv->periph)) {
// wait for RXNE
if (HAL_GetTick() - t_start > 10) {
com_respond_err(frame_id, "RX TIMEOUT");
return false;
}
}
uint8_t b = LL_I2C_ReceiveData8(priv->periph);
unit_tmp512[i] = b;
}
com_respond_buf(frame_id, MSG_SUCCESS, (uint8_t *) unit_tmp512, len);
break; break;
default: default:

@ -66,7 +66,7 @@ struct PayloadParser_ {
// --- utilities --- // --- utilities ---
/** Get remaining length */ /** Get remaining length */
#define pp_length(pp) ((pp)->end - (pp)->current) #define pp_length(pp) (uint32_t)((pp)->end - (pp)->current)
/** Reset the current pointer to start */ /** Reset the current pointer to start */
#define pp_rewind(pp) do { pp->current = pp->start; } while (0) #define pp_rewind(pp) do { pp->current = pp->start; } while (0)

@ -219,10 +219,10 @@ static error_t write_conf(void *state, const uint8_t *data, uint32_t size)
conf_state_t *conf = state; conf_state_t *conf = state;
conf->file_pos += size; conf->file_pos += size;
vfs_printf("Writing INI - RX %d bytes", size); vfs_printf("Writing INI - RX %d bytes", (int)size);
vfs_printf_nonl("\033[92m", 5); vfs_puts("\033[92m");
vfs_printf_nonl((const char *) data, size); vfs_putsn((const char *) data, size);
vfs_printf_nonl("\033[0m\r\n", 6); vfs_puts("\033[0m\r\n");
ini_parse((const char *) data, size); ini_parse((const char *) data, size);
@ -233,7 +233,7 @@ static error_t write_conf(void *state, const uint8_t *data, uint32_t size)
static error_t close_conf(void *state) static error_t close_conf(void *state)
{ {
conf_state_t *conf = state; conf_state_t *conf = state;
vfs_printf("Close INI, total bytes = %d", conf->file_pos); vfs_printf("Close INI, total bytes = %d", (int)conf->file_pos);
ini_parse_end(); ini_parse_end();
settings_load_ini_end(); settings_load_ini_end();

@ -134,7 +134,7 @@ static void transfer_update_state(error_t status);
void vfs_mngr_fs_enable(bool enable) void vfs_mngr_fs_enable(bool enable)
{ {
sync_lock(); sync_lock();
vfs_printf("Enable = %d", enable); vfs_printf("Enable = %d", (int)enable);
if (enable) { if (enable) {
if (VFS_MNGR_STATE_DISCONNECTED == vfs_state_next) { if (VFS_MNGR_STATE_DISCONNECTED == vfs_state_next) {
@ -208,8 +208,8 @@ void vfs_mngr_periodic(uint32_t elapsed_ms)
} }
vfs_printf("vfs_mngr_periodic()\r\n"); vfs_printf("vfs_mngr_periodic()\r\n");
vfs_printf(" time_usb_idle=%i\r\n", time_usb_idle); vfs_printf(" time_usb_idle=%i\r\n", (int)time_usb_idle);
vfs_printf(" transfer_state=%i\r\n", file_transfer_state.transfer_state); vfs_printf(" transfer_state=%i\r\n", (int)file_transfer_state.transfer_state);
// Transition to new state // Transition to new state
vfs_state_local_prev = vfs_state; vfs_state_local_prev = vfs_state;
vfs_state = vfs_state_next; vfs_state = vfs_state_next;
@ -229,7 +229,7 @@ void vfs_mngr_periodic(uint32_t elapsed_ms)
time_usb_idle = 0; time_usb_idle = 0;
sync_unlock(); sync_unlock();
// Processing when leaving a state // Processing when leaving a state
vfs_printf(" state %i->%i\r\n", vfs_state_local_prev, vfs_state_local); vfs_printf(" state %i->%i\r\n", (int)vfs_state_local_prev, (int)vfs_state_local);
bool want_notify_only = false; // Use this if the transfer timed out and we dont need full reconnect bool want_notify_only = false; // Use this if the transfer timed out and we dont need full reconnect
switch (vfs_state_local_prev) { switch (vfs_state_local_prev) {
@ -331,7 +331,7 @@ void vfs_if_usbd_msc_read_sect(uint32_t sector, uint8_t *buf, uint32_t num_of_se
void vfs_if_usbd_msc_write_sect(uint32_t sector, uint8_t *buf, uint32_t num_of_sectors) void vfs_if_usbd_msc_write_sect(uint32_t sector, uint8_t *buf, uint32_t num_of_sectors)
{ {
sync_assert_usb_thread(); sync_assert_usb_thread();
vfs_printf("\033[32mWRITE @ %d, len %d\033[0m", sector, num_of_sectors); vfs_printf("\033[32mWRITE @ %d, len %d\033[0m", (int)sector, (int)num_of_sectors);
if (buf[0] == 0xF8 && buf[1] == 0xFF && buf[2] == 0xFF && buf[3] == 0xFF) { if (buf[0] == 0xF8 && buf[1] == 0xFF && buf[2] == 0xFF && buf[3] == 0xFF) {
vfs_printf("Discard write of F8,FF,FF,FF"); vfs_printf("Discard write of F8,FF,FF,FF");
return; return;
@ -428,7 +428,7 @@ static void switch_to_new_file(stream_type_t stream, uint32_t start_sector, bool
// Callback to handle changes to the root directory. Should be used with vfs_set_file_change_callback // Callback to handle changes to the root directory. Should be used with vfs_set_file_change_callback
static void file_change_handler(const vfs_filename_t filename, vfs_file_change_t change, vfs_file_t file, vfs_file_t new_file_data) static void file_change_handler(const vfs_filename_t filename, vfs_file_change_t change, vfs_file_t file, vfs_file_t new_file_data)
{ {
vfs_printf("\033[33m@file_change_handler\033[0m (name=%*s, file=%p, ftp=%p, change=%i)\r\n", 11, filename, file, change); vfs_printf("\033[33m@file_change_handler\033[0m (name=%*s, file=%p, change=%i)\r\n", 11, filename, file, (int)change);
vfs_user_file_change_handler(filename, change, file, new_file_data); vfs_user_file_change_handler(filename, change, file, new_file_data);
if (TRASNFER_FINISHED == file_transfer_state.transfer_state) { if (TRASNFER_FINISHED == file_transfer_state.transfer_state) {
@ -490,10 +490,10 @@ static void file_data_handler(uint32_t sector, const uint8_t *buf, uint32_t num_
{ {
stream_type_t stream; stream_type_t stream;
uint32_t size; uint32_t size;
vfs_printf("\033[33m@file_data_handler\033[0m (sec=%d, num=%d)", sector, num_of_sectors); vfs_printf("\033[33m@file_data_handler\033[0m (sec=%d, num=%d)", (int)sector, (int)num_of_sectors);
if (sector <= 1) { if (sector <= 1) {
vfs_printf("Discard write to sector %d", sector); vfs_printf("Discard write to sector %d", (int)sector);
return; return;
} }
@ -521,7 +521,7 @@ static void file_data_handler(uint32_t sector, const uint8_t *buf, uint32_t num_
// sectors must be in order // sectors must be in order
if (sector != file_transfer_state.file_next_sector) { if (sector != file_transfer_state.file_next_sector) {
vfs_printf("file_data_handler BAD sector=%i\r\n", sector); vfs_printf("file_data_handler BAD sector=%i\r\n", (int)sector);
// Try to find what file this belongs to, if any // Try to find what file this belongs to, if any
// OS sometimes first writes the FAT and then the individual files, // OS sometimes first writes the FAT and then the individual files,
@ -544,7 +544,7 @@ static void file_data_handler(uint32_t sector, const uint8_t *buf, uint32_t num_
if (sector >= file_transfer_state.start_sector && sector < file_transfer_state.file_next_sector) { if (sector >= file_transfer_state.start_sector && sector < file_transfer_state.file_next_sector) {
vfs_printf(" sector out of order! lowest ooo = %i\r\n", vfs_printf(" sector out of order! lowest ooo = %i\r\n",
file_transfer_state.last_ooo_sector); (int)file_transfer_state.last_ooo_sector);
if (VFS_INVALID_SECTOR == file_transfer_state.last_ooo_sector) { if (VFS_INVALID_SECTOR == file_transfer_state.last_ooo_sector) {
file_transfer_state.last_ooo_sector = sector; file_transfer_state.last_ooo_sector = sector;
@ -572,12 +572,12 @@ static void file_data_handler(uint32_t sector, const uint8_t *buf, uint32_t num_
} }
vfs_printf(" discarding data - size transferred=0x%x\r\n", vfs_printf(" discarding data - size transferred=0x%x\r\n",
file_transfer_state.size_transferred); (int)file_transfer_state.size_transferred);
vfs_printf_nonl("\033[31m", 5); vfs_puts("\033[31m");
// FIXME this seems wrong // FIXME this seems wrong
vfs_printf_nonl((const char *) buf, VFS_SECTOR_SIZE * num_of_sectors); vfs_putsn((const char *) buf, VFS_SECTOR_SIZE * num_of_sectors);
vfs_printf_nonl("\033[0m\r\n", 6); vfs_puts("\033[0m\r\n");
return; return;
} else { } else {
@ -591,13 +591,13 @@ static void file_data_handler(uint32_t sector, const uint8_t *buf, uint32_t num_
// If stream processing is done then discard the data // If stream processing is done then discard the data
if (file_transfer_state.stream_finished) { if (file_transfer_state.stream_finished) {
vfs_printf("vfs_manager file_data_handler\r\n sector=%i, size=%i\r\n", sector, size); vfs_printf("vfs_manager file_data_handler\r\n sector=%i, size=%i\r\n", (int)sector, (int)size);
vfs_printf(" discarding data - size transferred=0x%x\r\n", vfs_printf(" discarding data - size transferred=0x%x\r\n",
file_transfer_state.size_transferred); (int)file_transfer_state.size_transferred);
vfs_printf_nonl("\033[31m", 5); vfs_puts("\033[31m");
vfs_printf_nonl((const char *) buf, VFS_SECTOR_SIZE * num_of_sectors); vfs_putsn((const char *) buf, VFS_SECTOR_SIZE * num_of_sectors);
vfs_printf_nonl("\033[0m\r\n", 6); vfs_puts("\033[0m\r\n");
transfer_update_state(E_SUCCESS); transfer_update_state(E_SUCCESS);
return; return;
@ -671,7 +671,7 @@ void abort_remount(void)
// Update the tranfer state with file information // Update the tranfer state with file information
static void transfer_update_file_info(vfs_file_t file, uint32_t start_sector, uint32_t size, stream_type_t stream) static void transfer_update_file_info(vfs_file_t file, uint32_t start_sector, uint32_t size, stream_type_t stream)
{ {
vfs_printf("\033[33m@transfer_update_file_info\033[0m (file=%p, start_sector=%i, size=%i)\r\n", file, start_sector, size); vfs_printf("\033[33m@transfer_update_file_info\033[0m (file=%p, start_sector=%i, size=%i)\r\n", file, (int)start_sector, (int)size);
if (TRASNFER_FINISHED == file_transfer_state.transfer_state) { if (TRASNFER_FINISHED == file_transfer_state.transfer_state) {
trap("xfer already finished"); trap("xfer already finished");
@ -692,7 +692,7 @@ static void transfer_update_file_info(vfs_file_t file, uint32_t start_sector, ui
file_transfer_state.start_sector = start_sector; file_transfer_state.start_sector = start_sector;
if (start_sector != VFS_INVALID_SECTOR) { if (start_sector != VFS_INVALID_SECTOR) {
vfs_printf(" start_sector=%i\r\n", start_sector); vfs_printf(" start_sector=%i\r\n", (int)start_sector);
} }
} }
@ -701,13 +701,13 @@ static void transfer_update_file_info(vfs_file_t file, uint32_t start_sector, ui
file_transfer_state.stream = stream; file_transfer_state.stream = stream;
if (stream != STREAM_TYPE_NONE) { if (stream != STREAM_TYPE_NONE) {
vfs_printf(" stream=%i\r\n", stream); vfs_printf(" stream=%i\r\n", (int)stream);
} }
} }
// Check - File size must either grow or be smaller than the size already transferred // Check - File size must either grow or be smaller than the size already transferred
if ((size < file_transfer_state.file_size) && (size < file_transfer_state.size_transferred)) { if ((size < file_transfer_state.file_size) && (size < file_transfer_state.size_transferred)) {
vfs_printf(" error: file size changed from %i to %i\r\n", file_transfer_state.file_size, size); vfs_printf(" error: file size changed from %i to %i\r\n", (int)file_transfer_state.file_size, (int)size);
// this is probably a new file // this is probably a new file
trap("File shrinks");//XXX trap("File shrinks");//XXX
switch_to_new_file(stream, start_sector, true); switch_to_new_file(stream, start_sector, true);
@ -715,7 +715,7 @@ static void transfer_update_file_info(vfs_file_t file, uint32_t start_sector, ui
// Check - Starting sector must be the same - this is optional for file info since it may not be present initially // Check - Starting sector must be the same - this is optional for file info since it may not be present initially
if ((VFS_INVALID_SECTOR != start_sector) && (start_sector != file_transfer_state.start_sector)) { if ((VFS_INVALID_SECTOR != start_sector) && (start_sector != file_transfer_state.start_sector)) {
vfs_printf(" error: starting sector changed from %i to %i\r\n", file_transfer_state.start_sector, start_sector); vfs_printf(" error: starting sector changed from %i to %i\r\n", (int)file_transfer_state.start_sector, (int)start_sector);
// this is probably a new file // this is probably a new file
dbg("WARN! Changed start offset"); dbg("WARN! Changed start offset");
@ -733,7 +733,7 @@ static void transfer_update_file_info(vfs_file_t file, uint32_t start_sector, ui
// Update values - Size is the only value that can change // Update values - Size is the only value that can change
file_transfer_state.file_size = size; file_transfer_state.file_size = size;
vfs_printf(" updated size=%i\r\n", size); vfs_printf(" updated size=%i\r\n", (int)size);
transfer_update_state(E_SUCCESS); transfer_update_state(E_SUCCESS);
} }
@ -757,11 +757,11 @@ static void transfer_stream_open(stream_type_t stream, uint32_t start_sector)
assert_param(!file_transfer_state.stream_open); assert_param(!file_transfer_state.stream_open);
assert_param(start_sector != VFS_INVALID_SECTOR); assert_param(start_sector != VFS_INVALID_SECTOR);
vfs_printf("\033[33m@transfer_stream_open\033[0m (stream=%i, start_sector=%i)\r\n", vfs_printf("\033[33m@transfer_stream_open\033[0m (stream=%i, start_sector=%i)\r\n",
stream, start_sector); (int)stream, (int)start_sector);
// Check - Starting sector must be the same // Check - Starting sector must be the same
if (start_sector != file_transfer_state.start_sector && file_transfer_state.start_sector != VFS_INVALID_SECTOR) { if (start_sector != file_transfer_state.start_sector && file_transfer_state.start_sector != VFS_INVALID_SECTOR) {
vfs_printf(" error: starting sector changed from %i to %i\r\n", file_transfer_state.start_sector, start_sector); vfs_printf(" error: starting sector changed from %i to %i\r\n", (int)file_transfer_state.start_sector, (int)start_sector);
// this is probably a new file // this is probably a new file
switch_to_new_file(stream, start_sector, false); switch_to_new_file(stream, start_sector, false);
file_transfer_state.start_sector = VFS_INVALID_SECTOR; file_transfer_state.start_sector = VFS_INVALID_SECTOR;
@ -769,7 +769,7 @@ static void transfer_stream_open(stream_type_t stream, uint32_t start_sector)
// Check - stream must be the same // Check - stream must be the same
if (stream != file_transfer_state.stream && file_transfer_state.stream != STREAM_TYPE_NONE) { if (stream != file_transfer_state.stream && file_transfer_state.stream != STREAM_TYPE_NONE) {
vfs_printf(" error: changed types during tranfer from %i to %i\r\n", stream, file_transfer_state.stream); vfs_printf(" error: changed types during tranfer from %i to %i\r\n", (int)stream, (int)file_transfer_state.stream);
// this is probably a new file // this is probably a new file
switch_to_new_file(stream, start_sector, false); switch_to_new_file(stream, start_sector, false);
file_transfer_state.start_sector = VFS_INVALID_SECTOR; file_transfer_state.start_sector = VFS_INVALID_SECTOR;
@ -780,7 +780,7 @@ static void transfer_stream_open(stream_type_t stream, uint32_t start_sector)
file_transfer_state.start_sector = start_sector; file_transfer_state.start_sector = start_sector;
if (start_sector != VFS_INVALID_SECTOR) { if (start_sector != VFS_INVALID_SECTOR) {
vfs_printf(" start_sector=%i\r\n", start_sector); vfs_printf(" start_sector=%i\r\n", (int)start_sector);
} }
} }
@ -789,13 +789,13 @@ static void transfer_stream_open(stream_type_t stream, uint32_t start_sector)
file_transfer_state.stream = stream; file_transfer_state.stream = stream;
if (stream != STREAM_TYPE_NONE) { if (stream != STREAM_TYPE_NONE) {
vfs_printf(" stream=%i\r\n", stream); vfs_printf(" stream=%i\r\n", (int)stream);
} }
} }
// Open stream // Open stream
status = stream_open(stream); status = stream_open(stream);
vfs_printf(" stream_open stream=%i ret %i\r\n", stream, status); vfs_printf(" stream_open stream=%i ret %i\r\n", (int)stream, (int)status);
if (E_SUCCESS == status) { if (E_SUCCESS == status) {
file_transfer_state.file_next_sector = start_sector; file_transfer_state.file_next_sector = start_sector;
@ -810,9 +810,9 @@ static void transfer_stream_open(stream_type_t stream, uint32_t start_sector)
static void transfer_stream_data(uint32_t sector, const uint8_t *data, uint32_t size) static void transfer_stream_data(uint32_t sector, const uint8_t *data, uint32_t size)
{ {
error_t status; error_t status;
vfs_printf("\033[33m@transfer_stream_data\033[0m (sector=%i, size=%i)\r\n", sector, size); vfs_printf("\033[33m@transfer_stream_data\033[0m (sector=%i, size=%i)\r\n", (int)sector, (int)size);
vfs_printf(" size processed=0x%x, data=%x,%x,%x,%x,...\r\n", vfs_printf(" size processed=0x%x, data=%x,%x,%x,%x,...\r\n",
file_transfer_state.size_processed, data[0], data[1], data[2], data[3]); (int)file_transfer_state.size_processed, data[0], data[1], data[2], data[3]);
if (file_transfer_state.stream_finished) { if (file_transfer_state.stream_finished) {
trap("Stream already closed"); trap("Stream already closed");
@ -822,13 +822,13 @@ static void transfer_stream_data(uint32_t sector, const uint8_t *data, uint32_t
assert_param(size % VFS_SECTOR_SIZE == 0); assert_param(size % VFS_SECTOR_SIZE == 0);
assert_param(file_transfer_state.stream_open); assert_param(file_transfer_state.stream_open);
status = stream_write((uint8_t *) data, size); status = stream_write((uint8_t *) data, size);
vfs_printf(" stream_write ret=%i\r\n", status); vfs_printf(" stream_write ret=%i\r\n", (int)status);
if (E_SUCCESS_DONE == status) { if (E_SUCCESS_DONE == status) {
// Override status so E_SUCCESS_DONE // Override status so E_SUCCESS_DONE
// does not get passed into transfer_update_state // does not get passed into transfer_update_state
status = stream_close(); status = stream_close();
vfs_printf(" stream_close ret=%i\r\n", status); vfs_printf(" stream_close ret=%i\r\n", (int)status);
file_transfer_state.stream_open = false; file_transfer_state.stream_open = false;
file_transfer_state.stream_finished = true; file_transfer_state.stream_finished = true;
file_transfer_state.stream_optional_finish = true; file_transfer_state.stream_optional_finish = true;
@ -921,11 +921,11 @@ static void transfer_update_state(error_t status)
if (TRASNFER_FINISHED == file_transfer_state.transfer_state) { if (TRASNFER_FINISHED == file_transfer_state.transfer_state) {
vfs_printf("vfs_manager transfer_update_state(status=%i)\r\n", status); vfs_printf("vfs_manager transfer_update_state(status=%i)\r\n", status);
vfs_printf(" file=%p, start_sect= %i, size=%i\r\n", vfs_printf(" file=%p, start_sect= %i, size=%i\r\n",
file_transfer_state.file_to_program, file_transfer_state.start_sector, file_transfer_state.file_to_program, (int)file_transfer_state.start_sector,
file_transfer_state.file_size); (int)file_transfer_state.file_size);
vfs_printf(" stream=%i, size_processed=%i, opt_finish=%i, timeout=%i\r\n", vfs_printf(" stream=%i, size_processed=%i, opt_finish=%i, timeout=%i\r\n",
file_transfer_state.stream, file_transfer_state.size_processed, (int)file_transfer_state.stream, (int)file_transfer_state.size_processed,
file_transfer_state.file_info_optional_finish, transfer_timeout); (int)file_transfer_state.file_info_optional_finish, (int)transfer_timeout);
// Close the file stream if it is open // Close the file stream if it is open
if (file_transfer_state.stream_open) { if (file_transfer_state.stream_open) {

@ -555,7 +555,7 @@ void vfs_write(uint32_t requested_sector, const uint8_t *buf, uint32_t num_secto
set_init_done(); set_init_done();
vfs_printf("vfs_write - at sector %d, count %d", requested_sector, num_sectors); vfs_printf("vfs_write - at sector %d, count %d", (int)requested_sector, (int)num_sectors);
for (i = 0; i < virtual_media_idx; i++) { for (i = 0; i < virtual_media_idx; i++) {
uint32_t vm_sectors = virtual_media[i].length / VFS_SECTOR_SIZE; uint32_t vm_sectors = virtual_media[i].length / VFS_SECTOR_SIZE;
uint32_t vm_start = current_sector; uint32_t vm_start = current_sector;
@ -582,7 +582,7 @@ void vfs_write(uint32_t requested_sector, const uint8_t *buf, uint32_t num_secto
// Move to the next virtual media entry // Move to the next virtual media entry
current_sector += vm_sectors; current_sector += vm_sectors;
} }
if (num_sectors > 0) vfs_printf("Failed to find place for writing, remain %d secs to write.", num_sectors); if (num_sectors > 0) vfs_printf("Failed to find place for writing, remain %d secs to write.", (int)num_sectors);
} }
static uint32_t read_zero(uint32_t sector_offset, uint8_t *data, uint32_t num_sectors) static uint32_t read_zero(uint32_t sector_offset, uint8_t *data, uint32_t num_sectors)
@ -788,7 +788,7 @@ static void set_init_done(void)
bool vfs_find_file(uint32_t start_sector, vfs_filename_t *destFilename, vfs_file_t **destFile) bool vfs_find_file(uint32_t start_sector, vfs_filename_t *destFilename, vfs_file_t **destFile)
{ {
vfs_printf("Looking for file at %d", start_sector); vfs_printf("Looking for file at %d", (int)start_sector);
for (int i = 0; i < 32; i++) { for (int i = 0; i < 32; i++) {
FatDirectoryEntry_t *f = &dir_current.f[i]; FatDirectoryEntry_t *f = &dir_current.f[i];
if (f->attributes == VFS_FILE_ATTR_LFN) continue; if (f->attributes == VFS_FILE_ATTR_LFN) continue;

@ -31,9 +31,13 @@ extern "C" {
#if DEBUG_VFS #if DEBUG_VFS
#define vfs_printf(...) do { dbg(__VA_ARGS__); } while(0) #define vfs_printf(...) do { dbg(__VA_ARGS__); } while(0)
#define vfs_printf_nonl(...) do { PRINTF(__VA_ARGS__); } while(0) #define vfs_printf_nonl(...) do { PRINTF(__VA_ARGS__); } while(0)
#define vfs_puts(buf) do { PUTS(buf); } while(0)
#define vfs_putsn(buf, n) do { PUTSN(buf, n); } while(0)
#else #else
#define vfs_printf(...) do { } while(0) #define vfs_printf(...) do { } while(0)
#define vfs_printf_nonl(...) do { } while(0) #define vfs_printf_nonl(...) do { } while(0)
#define vfs_puts(buf) do { } while(0)
#define vfs_putsn(buf, n) do { } while(0)
#endif #endif
#define VFS_CLUSTER_SIZE 0x1000 #define VFS_CLUSTER_SIZE 0x1000

Loading…
Cancel
Save