Commit Graph

1765 Commits (136569cf4508adf58602735d4da0626c4e761409)

Author SHA1 Message Date
Adam Green 657e6df203 Updates to network code to improve performance/robustness
I started out looking at some UDP receive code that was only able to
handle 3 inbound 550 byte datagrams out of 16 when sent in quick
succession.  I stepped through the ethernet driver code and it
seemed to work as expected but it just couldn't queue up more than
3 PBUFs for each burst.  It was almost like it was being starved of
CPU cycles.  Based on that observation, I looked up the thread
priorities for the receive ethernet thread and found the following
close to the top of the lpc17_emac.c source file:
    #define RX_PRIORITY   (osPriorityNormal)
This got me to thinking, what is the priority of the tcp thead?  It
turns out that it gets its priority from the following line in
lwipopts.h:
    #define TCPIP_THREAD_PRIO           1
Interesting!  What priority is 1?  It turns out that it corresponds
to osPriorityAboveNormal.  This means that while the tcp thread is
handling one packet that has been posted to its mailbox from the
ethernet receive thread, the receive thread is starved from processing
any more inbound ethernet packets.

What happens if we set TCP_IP_THREAD_PRIO to osPriorityNormal?  Crash!
The ethernet driver ends up crashing in lpc_low_level_input() when
it tries to set p->len on a NULL p pointer.  The p pointer ended up
being NULL because an earlier call to pbuf_alloc() in lpc_rx_queue()
failed its allocation (I will have more to say about this failed
allocation later since that is caused by yet another bug).  I pulled a
fix from http://lpcware.com/content/bugtrackerissue/lpc17xx-mac-bugs to
remedy this issue.  When the pbuf allocation fails, it discards the
inbound packet in the pbuf and just puts it back into the rx queue.
This means we never end up with a NULL pointer in that queue to
dereference and crash on.

With that bug fixed, the application would just appear to hang after
receiving and processing a few datagrams.  I could place breakpoints in
the packet_rx() thread function and found that it was being signalled
by the ethernet ISR but it was always failing to allocate new PBUFs,
which is what led to our previous crash.  This means that the new
crash prevention code was just discarding every packet that arrived.

Why are these allocations failing?  In my opinion, this was the most
interesting bug to track down.  Is there a memory leak somewhere in
the code which maybe only triggers in low memory situations?  I
figured the easiest way to determine that would be to learn a bit
about the format of the lwIP heap from which the PBUF was failing to
be allocated.  I started by just stepping into the failing lwIP memory
allocator, mem_malloc().  The loop which search the free list starts
with this code:
    for (ptr = (mem_size_t)((u8_t *)lfree - ram);
This loop didn't even go through one iteration and when I looked at the
initial ptr value it contained a really large value.  It turns out that
lfree was actually lower than ram.  At this point I figured that lfree
had probably been corrupted during a free operation after one of the
heap allocations had been underflowed/overflowed to cause the metadata
for an allocation to be corrupted.  As I started thinking about how to
track that kind of bug down, I noticed that the ram variable might be
too large (0x20080a68).  I restarted the debugger and looked at the
initial value.  It was at a nice even address (0x2007c000) and
certainly nothing like what I saw when the allocations were failing.
This global variable shouldn't change at all during the execution of
the program.  I placed a memory access watchpoint on this ram variable
and it fired very quickly inside of the rt_mbx_send() function.  The
ram variable was being changed by this line in rt_mbx_send():
    p_MCB->msg[p_MCB->first] = p_msg;

What the what?  Why does writing to the mailbox queue overwrite the
ram global variable?  Let's start by looking at the data structure used
in the lwIP port to target RTX (defined in sys_arch.h):
// === MAIL BOX ===

typedef struct {
    osMessageQId    id;
    osMessageQDef_t def;
    uint32_t        queue[MB_SIZE];
} sys_mbox_t;

Compare that to the utility macro that RTX defines to help setup one of
these mailboxes with queue:
    #define osMessageQDef(name, queue_sz, type)   \
    uint32_t os_messageQ_q_##name[4+(queue_sz)]; \
    osMessageQDef_t os_messageQ_def_##name = \
    { (queue_sz), (os_messageQ_q_##name) }
Note the 4+(queue_sz) used in the definition of the message queue
array.  What a hack!  The RTX OS requires an extra 16 bytes to contain
its OS_MCB header and this is how it adds it in.  Obviously the
sys_mbox_t structure used in the lwIP OS targetting code doesn't have
this.  Without it, the RTX mailbox routines end up scribbling on
memory following the structure in memory.  Adding 4 in that structure
fixes the memory allocation failure that I was seeing and now the network
stack can handle between 7 and 10 datagrams within a burst.
2013-08-15 03:08:47 -07:00
Bogdan Marinescu 9d846d9902 Merge pull request #31 from adamgreen/gccWarnFixNetwork
Update LPC1768 linker script and silence GNU warnings in network stack
2013-08-15 02:55:51 -07:00
Bogdan Marinescu 092da0d425 Merge pull request #28 from 0xc0170/ticker_chaining_bug
Chaining in Ticker - switched arguments [fix]
2013-08-15 00:36:59 -07:00
Adam Green 282c354ba7 The value of 2 can't fit in a 1 bit wide field.
The phy_speed_100mbs, phy_full_duplex, and phy_link_active fields of
PHY_STATUS_TYPE are 1 bit wide but lpc_phy_init() attempted to
initialize them to a value of 2.  I switched the initializations to
be 0 instead and it still generated the same .bin image.
2013-08-14 20:17:54 -07:00
Adam Green 962dd8bd8f Silence GCC warnings in dhcp.c
The first was a potential out of range index read in dhcp_handle_ack().
The (n < DNS_MAX_SERVERS) check should occur first.  There is also a
documented lwIP bug for this issue here:
    http://savannah.nongnu.org/bugs/?36170

In dhcp_bind() there is no need to perform the NULL check in
ip_addr_isany() for &gw_addr.  Just check (gw_addr.addr == IPADDR_ANY)
instead.

I refactored the chaddr[] copy in dhcp_create_msg() to first copy all
of the valid bytes in hwaddr and then pad the rest of the bytes with 0.
Before it used to check on every destination byte if it should copy or
pad.  GCC originally complained about an index out of range read from
the hwaddr[] array even though it was protected by a conditional
operator.  The refactor makes the intent a bit clearer and saves the
extra comparison per loop iteration.  It also stops GCC from
complaining :)
2013-08-14 20:17:54 -07:00
Adam Green 1cf5243206 Don't call ip_addr_isany() on objects which can't be NULL.
GCC will issue a warning when the ip_addr_isany() macro is used on
a pointer which can never be NULL since the macros NULL check will
always be false:
    #define ip_addr_isany(addr1) ((addr1) == NULL || \
                                  (addr1)->addr == IPADDR_ANY)

In these cases, it is probably clearer to just perform the
x.addr == IPADDR_ANY check inline.
2013-08-14 20:17:54 -07:00
Adam Green 6e95a5ecde Removed extra ALIGNED macro define 2013-08-14 20:17:53 -07:00
Adam Green e014b41377 Silence signed/unsigned comparison warnings in GCC
The dn variable in lpc_low_level_output() was originally defined as a
u32_t but it is later compared to the s32_t return value from
lpc_tx_ready().  Since it is intialized to pbuf_clean() which returns
a u8_t, a s32_t type can safely hold the initial value and remains
consistent with the signed lpc_tx_ready() comparison.

I also modifed writtenLen in TCPSocketConnection::send_all() and
readLen in TCPSocketConnection::recieve_all() to be of type int instead
of size_t.  This is more consistent with their usage within these
methods (they accumulate int ret values and are compared to the int
length value) and their use as a signed integer return values.
2013-08-14 20:17:53 -07:00
Adam Green aa7a55b6dd Update LPC1768.ld linker script to work with net stack.
The original script assigned memory ranges to USB_RAM and ETH_RAM but
it never placed any section data in those regions.  I added clauses
towards the bottom of the script to place data that the programmer
has marked for the AHBSRAM0 and AHBSRAM1 sections into these regions
of RAM.  Previously the data destined for these sections was being
placed in the lower 32K RAM bank and overflowing it at link time.

I also added a few Image$$ linker symbols to mimic those used by the
online compiler.  I have had samples in the past which took advantage
of these to display static memory statistics for each SRAM region.

I also changed LENGTH=0x7F38 to LENGTH=(32K - 0xC8) to make it more
consistent with the sizing of the other regions in this script which
use human readable K sizing information.  The 0xC8 subtraction reflects
the starting offset of 0xC8 for this region.
2013-08-14 20:17:53 -07:00
pbrier ac078485ac Compile network and RTOS with GCC_ARM 2013-08-14 22:52:16 +02:00
pbrier 9011a5453a Compile network and RTOS with GCC_ARM 2013-08-14 22:45:55 +02:00
pbrier c0fdbede02 Compile network and RTOS with GCC_ARM 2013-08-14 22:34:33 +02:00
0xc0170 3d9be83e38 Chaining in Ticker - switched arguments [fix] 2013-08-14 18:22:30 +02:00
Emilio Monti de715aed95 Merge pull request #27 from ytsuboi/master
Fixed some problem around LPC1114 porting
2013-08-14 09:05:52 -07:00
Toyomasa Watarai 9f2a930f31 Fixed GPIO read operation 2013-08-14 22:43:03 +09:00
Toyomasa Watarai b632d8db46 Removed redundant code in serial_api 2013-08-14 22:29:31 +09:00
Toyomasa Watarai 83e065d1bc Fixed missing mask register setting for I2C 2013-08-14 22:28:30 +09:00
Emilio Monti e85d048b1a Merge pull request #26 from 0xc0170/eth_static_mac_address
Eth static mac address
2013-08-14 03:16:35 -07:00
0xc0170 4cbbf8f274 Update eth_static_mac_address from mbed master 2013-08-14 07:29:56 +02:00
0xc0170 74a34c842d default mbed interface for ethernet
- correction in macros, renamimg
2013-08-13 21:49:04 +02:00
Bogdan Marinescu 5ae0e913b1 Merge pull request #24 from adamgreen/gccWarnFixMbedSDK
Update mbed SDK sources to silence GCC warnings
2013-08-13 03:04:42 -07:00
Adam Green 692e666ced Silence signed/unsigned comparison warnings in GCC.
i2c_frequency() compares a uint32_t ref variable to the int hz
function parameter passed in by the caller.  I forced this to be an
uint32_t comparison.

i2c_slave_write() declared i and count variables to be of type uint32_t
but used them as int type throughout the code (in comparisons and
returns) so I switched them to be of signed int type.

spi_frequency() contains a change similar to that made in
i2c_frequency().
2013-08-13 01:47:20 -07:00
Adam Green 461a3dd0d2 Cast to matching enumeration type instead of uint32_t
This commit targets the KL25Z code, whereas previous ones targetted
similar issues in the LPC1768 and LPC11U24 mbed HAL code.

These changes were made to silence GCC warnings and fix potential bugs
where they would never be equal when the enumeration wasn't a 32-bit
type.

For example, pinmap.c used to contain this code:
    if (pin == (uint32_t)NC) return;
I switched it to:
    if (pin == (PinName)NC) return;

I wonder why this casting to uint32_t was done in the first place?
Maybe another supported compiler requires it?
2013-08-13 01:47:20 -07:00
Adam Green cc56997a70 Cast to matching enumeration type instead of uint32_t
This commit targets the LPC11U24 code, whereas a previous one
targetted similar issues in the LPC1768 mbed HAL code.

These changes were made to silence GCC warnings and fix potential bugs
where they would never be equal when the enumeration wasn't a 32-bit
type.

For example, pinmap.c used to contain this code:
    if (pin == (uint32_t)NC) return;
I switched it to:
    if (pin == (PinName)NC) return;

I wonder why this casting to uint32_t was done in the first place?
Maybe another supported compiler requires it?
2013-08-13 01:47:19 -07:00
Adam Green 8fe7276b98 Silence signed/unsigned comparison warnings in GCC.
Why do the wait APIs take a signed integer if they are going to be
compared to unsigned quantities?
2013-08-13 01:47:19 -07:00
Adam Green c411823656 Fix operator precedence warning in can_api.c
The original code was:
    if(LPC_CAN1->IER | LPC_CAN2->IER != 0) {

This would actually be interpreted as:
    if(LPC_CAN1->IER | (LPC_CAN2->IER != 0)) {
I simplified it to:
    if(LPC_CAN1->IER | LPC_CAN2->IER) {
With the comparison removed, the GCC warning no longer fires since the
user's intent is no longer unclear.  However, the end result should be
the same.
2013-08-13 01:47:19 -07:00
Adam Green 15f833bc1b Cast to matching enumeration type instead of uint32_t
These were done to silence GCC warnings and fix potential bugs where
they would never be equal when the enumeration wasn't a 32-bit type.

For example, common/pinmap_common.c used to contain this code:
    if (pin == (uint32_t)NC)
I switched it to:
    if (pin == (PinName)NC)

I wonder why this casting to uint32_t was done in the first place?
Maybe another supported compiler requires it?
2013-08-13 01:47:19 -07:00
Adam Green 3be88a4a6e Add .DS_Store to .gitignore
Instruct git to ignore these OS X files.
2013-08-13 01:47:19 -07:00
0xc0170 4ea25c9ebd Static mac address for ethernet interface
- enables to debug code even with semihosting enabled
2013-08-12 21:56:25 +02:00
Bogdan Marinescu 0e013cf5c8 Added script for building an official release of the mbed library 2013-08-12 13:45:35 +03:00
Bogdan Marinescu 1a47a218a4 [KL25Z] Fixed counter type for i2c_read operations. 2013-08-12 12:31:05 +03:00
Emilio Monti 0843613136 Add LPC11U24/301 TARGET 2013-08-09 17:21:03 +01:00
Emilio Monti d6c658e859 Fix targets.py formatting 2013-08-09 17:18:50 +01:00
Emilio Monti 27d07f4a1d Define all the required symbols in the generated project files 2013-08-08 16:58:37 +01:00
Emilio Monti 239a2f9adf Define all the required symbols in the generated Makefile 2013-08-08 16:39:51 +01:00
Emilio Monti ed2f350000 Merge pull request #23 from arebert/lpc4088
Updated pin mapping and CAN HAL impl for LPC4088 target
2013-08-08 06:53:34 -07:00
Andreas Rebert 75dba19438 Updated pin mapping and CAN HAL for LPC4088 target 2013-08-08 13:57:02 +02:00
Bogdan Marinescu 348c24e578 Added .gitattributes for automatic LF line ending conversion 2013-08-08 13:19:34 +03:00
Bogdan Marinescu 3b465de3aa Changed line endings to LF, removed non-ASCII chars from sources 2013-08-08 12:58:34 +03:00
Bogdan Marinescu 3f703f1bf0 [LPC11C24] Make code compatible with the interrupt chaining code 2013-08-07 15:24:30 +03:00
Bogdan Marinescu 7b9081b59c Add support for calling a function before main()
Also added a test for this feature (MBED_A22).
Fixes PRMBED-906.
2013-08-07 14:51:30 +03:00
Bogdan Marinescu ad5516f898 Add test groups.
Group tests with the same functionality into groups that can be used by autotest.py.
The groups can be overriden from private_settings.py.
2013-08-07 14:51:24 +03:00
Bogdan Marinescu 9ee1fc9f55 [KL25Z] Fix I2C issue related to the silicon errata.
Makes a difference when running the I2C EEPROM test at 400KHz, which has a 100%
success rate after this change.
2013-08-07 14:49:11 +03:00
Bogdan Marinescu 6c05438993 Interrupt chaining: added documentation, fixed a synchronization issue in CallChain. 2013-08-07 14:46:40 +03:00
Bogdan Marinescu d399e51bfd Interrupt chaining: now working on all targets.
Tested on LPC1768, LPC11U24, KL25Z, LPC2368.
2013-08-07 14:43:36 +03:00
Bogdan Marinescu 43d4445074 Interrupt chaining: preliminary version 2013-08-07 14:43:26 +03:00
Bogdan Marinescu 1e8e50996c [KL25Z] Restore full SPI pin mappings 2013-08-07 14:42:02 +03:00
Bogdan Marinescu 58fd85fc6f Add support for calling a function before main()
The name of the function is mbed_main.
Fixes PRMBED-906.
2013-08-07 14:39:13 +03:00
francois.berder@arm.com ffb2668411 Fixed some bugs in HTTPS library 2013-08-07 14:38:07 +03:00
francois.berder@arm.com bd9a31095a Add HTTPS library 2013-08-07 14:36:53 +03:00