Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/available write #830

Merged
merged 3 commits into from
Jan 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions hal/inc/hal_dynalib_usart.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ DYNALIB_FN(hal_usart,HAL_USART_Peek_Data)
DYNALIB_FN(hal_usart,HAL_USART_Flush_Data)
DYNALIB_FN(hal_usart,HAL_USART_Is_Enabled)
DYNALIB_FN(hal_usart,HAL_USART_Half_Duplex)
DYNALIB_FN(hal_usart,HAL_USART_Available_Data_For_Write)

DYNALIB_END(hal_usart)

Expand Down
1 change: 1 addition & 0 deletions hal/inc/usart_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ void HAL_USART_Init(HAL_USART_Serial serial, Ring_Buffer *rx_buffer, Ring_Buffer
void HAL_USART_Begin(HAL_USART_Serial serial, uint32_t baud);
void HAL_USART_End(HAL_USART_Serial serial);
uint32_t HAL_USART_Write_Data(HAL_USART_Serial serial, uint8_t data);
int32_t HAL_USART_Available_Data_For_Write(HAL_USART_Serial serial);
int32_t HAL_USART_Available_Data(HAL_USART_Serial serial);
int32_t HAL_USART_Read_Data(HAL_USART_Serial serial);
int32_t HAL_USART_Peek_Data(HAL_USART_Serial serial);
Expand Down
5 changes: 5 additions & 0 deletions hal/src/core/usart_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ void HAL_USART_End(HAL_USART_Serial serial)
usartMap[serial]->usart_transmitting = false;
}

int32_t HAL_USART_Available_Data_For_Write(HAL_USART_Serial serial)
{
return (unsigned int)(SERIAL_BUFFER_SIZE + usartMap[serial]->usart_tx_buffer->head - usartMap[serial]->usart_tx_buffer->tail) % SERIAL_BUFFER_SIZE;
}

uint32_t HAL_USART_Write_Data(HAL_USART_Serial serial, uint8_t data)
{
// interrupts are off and data in queue;
Expand Down
9 changes: 9 additions & 0 deletions hal/src/gcc/usart_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ struct Usart {
virtual void begin(uint32_t baud)=0;
virtual void end()=0;
virtual int32_t available()=0;
virtual int32_t availableForWrite()=0;
virtual int32_t read()=0;
virtual int32_t peek()=0;
virtual uint32_t write(uint8_t byte)=0;
Expand Down Expand Up @@ -84,6 +85,9 @@ class SocketUsartBase : public Usart
fillFromSocketIfNeeded();
return (rx->head-rx->tail) % SERIAL_BUFFER_SIZE;
}
virtual int32_t availableForWrite() override {
return (SERIAL_BUFFER_SIZE + tx->head - tx->tail) % SERIAL_BUFFER_SIZE;
}
virtual int32_t read() override {
fillFromSocketIfNeeded();
return read_char();
Expand Down Expand Up @@ -189,6 +193,11 @@ void HAL_USART_End(HAL_USART_Serial serial)
//usartMap(serial).end();
}

int32_t HAL_USART_Available_Data_For_Write(HAL_USART_Serial serial)
{
return usartMap(serial).availableForWrite();
}

uint32_t HAL_USART_Write_Data(HAL_USART_Serial serial, uint8_t data)
{
return usartMap(serial).write(data);
Expand Down
6 changes: 6 additions & 0 deletions hal/src/stm32f2xx/usart_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,12 @@ int32_t HAL_USART_Available_Data(HAL_USART_Serial serial)
return (unsigned int)(SERIAL_BUFFER_SIZE + usartMap[serial]->usart_rx_buffer->head - usartMap[serial]->usart_rx_buffer->tail) % SERIAL_BUFFER_SIZE;
}

int32_t HAL_USART_Available_Data_For_Write(HAL_USART_Serial serial)
{
return (unsigned int)(SERIAL_BUFFER_SIZE + usartMap[serial]->usart_tx_buffer->head - usartMap[serial]->usart_tx_buffer->tail) % SERIAL_BUFFER_SIZE;
}


int32_t HAL_USART_Read_Data(HAL_USART_Serial serial)
{
// if the head isn't ahead of the tail, we don't have any characters
Expand Down
4 changes: 2 additions & 2 deletions modules/shared/stm32f2xx/user_build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ LINKER_DEPS += $(SYSTEM_PART1_MODULE_PATH)/module_system_part1_export.ld

NANO_SUFFIX ?= _nano

LDFLAGS += -lnosys
LDFLAGS += -lnosys --specs=nano.specs
LDFLAGS += -L$(SYSTEM_PART2_MODULE_PATH)
LDFLAGS += -L$(SYSTEM_PART1_MODULE_PATH)
LDFLAGS += -L$(USER_PART_MODULE_PATH)
Expand All @@ -66,7 +66,7 @@ LDFLAGS += -Wl,--defsym,USER_FIRMWARE_IMAGE_LOCATION=$(USER_FIRMWARE_IMAGE_LOCAT
LDFLAGS += -Wl,-Map,$(TARGET_BASE).map

# used the -v flag to get gcc to output the commands it passes to the linker when --specs=nano.specs is provided
# LDFLAGS += -lstdc++_nano -lm -Wl,--start-group -lgcc -Wl,--end-group -Wl,--start-group -lgcc $(LIBG_TWEAK) -Wl,--end-group
# LDFLAGS += -lstdc++$(NANO_SUFFIX) -lm -Wl,--start-group -lgcc -Wl,--end-group -Wl,--start-group -lgcc $(LIBG_TWEAK) -Wl,--end-group

BUILTINS_EXCLUDE = malloc free realloc
CFLAGS += $(addprefix -fno-builtin-,$(BUILTINS_EXCLUDE))
Expand Down
5 changes: 5 additions & 0 deletions user/tests/wiring/api/wiring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ test(api_wiring_usartserial) {
API_COMPILE(Serial1.halfduplex(true));
API_COMPILE(Serial1.halfduplex(false));

API_COMPILE(Serial1.blockOnOverrun(false));
API_COMPILE(Serial1.blockOnOverrun(true));

API_COMPILE(Serial1.availableForWrite());

}

void TIM3_callback()
Expand Down
4 changes: 4 additions & 0 deletions wiring/inc/spark_wiring_usartserial.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class USARTSerial : public Stream
{
private:
HAL_USART_Serial _serial;
bool _blocking;
public:
USARTSerial(HAL_USART_Serial serial, Ring_Buffer *rx_buffer, Ring_Buffer *tx_buffer);
virtual ~USARTSerial() {};
Expand All @@ -43,6 +44,9 @@ class USARTSerial : public Stream
void halfduplex(bool);
void end();

virtual void blockOnOverrun(bool);

virtual int availableForWrite(void);
virtual int available(void);
virtual int peek(void);
virtual int read(void);
Expand Down
22 changes: 20 additions & 2 deletions wiring/src/spark_wiring_usartserial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
USARTSerial::USARTSerial(HAL_USART_Serial serial, Ring_Buffer *rx_buffer, Ring_Buffer *tx_buffer)
{
_serial = serial;
// Default is blocking mode
_blocking = true;
HAL_USART_Init(serial, rx_buffer, tx_buffer);
}
// Public Methods //////////////////////////////////////////////////////////////
Expand All @@ -57,6 +59,17 @@ void USARTSerial::halfduplex(bool Enable)
HAL_USART_Half_Duplex(_serial, Enable);
}

void USARTSerial::blockOnOverrun(bool block)
{
_blocking = block;
}


int USARTSerial::availableForWrite(void)
{
return HAL_USART_Available_Data(_serial);
}

int USARTSerial::available(void)
{
return HAL_USART_Available_Data(_serial);
Expand All @@ -79,7 +92,12 @@ void USARTSerial::flush()

size_t USARTSerial::write(uint8_t c)
{
return HAL_USART_Write_Data(_serial, c);
// attempt a write if blocking, or for non-blocking if there is room.
if (_blocking || HAL_USART_Available_Data_For_Write(_serial) > 0) {
// the HAL always blocks.
return HAL_USART_Write_Data(_serial, c);
}
return 0;
}

USARTSerial::operator bool() {
Expand All @@ -98,4 +116,4 @@ static Ring_Buffer serial1_tx_buffer;

USARTSerial Serial1(HAL_USART_SERIAL1, &serial1_rx_buffer, &serial1_tx_buffer);
// optional Serial2 is instantiated from libraries/Serial2/Serial2.h
#endif
#endif