Skip to content

Commit

Permalink
Merge branch 'bracz-select-executor'
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsracz committed May 16, 2015
2 parents 4250d3d + 2eeaf74 commit b5b08e2
Show file tree
Hide file tree
Showing 91 changed files with 3,109 additions and 206 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include <string.h>

#define TARGET_IS_TM4C123_RB1

#include "../boards/ti-tm4c123-generic/BootloaderHal.hxx"
#include "bootloader_hal.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"
#include "driverlib/can.h"
#include "inc/hw_memmap.h"
#include "driverlib/pin_map.h"

#include "nmranet_config.h"
#include "nmranet/Defs.hxx"
#include "TivaGPIO.hxx"

extern "C" {

GPIO_PIN(GOLD_SW, GpioInputPU, C, 7);
GPIO_PIN(BLUE_SW, GpioInputPU, C, 6);

GPIO_PIN(GOLD_LED, LedPin, B, 6);
GPIO_PIN(BLUE_LED, LedPin, B, 7);

GPIO_PIN(RED, LedPin, D, 6);
GPIO_PIN(YELLOW, LedPin, B, 0);
GPIO_PIN(GREEN, LedPin, D, 5);
GPIO_PIN(BLUE, LedPin, G, 1);

typedef BLUE_SW_Pin SWITCH_Pin;

void pin_to_safe(unsigned periph, unsigned port, unsigned pin)
{
ROM_SysCtlPeripheralEnable(periph);
ROM_GPIOPinTypeGPIOOutput(port, pin);
ROM_GPIOPinWrite(port, pin, 0);
}

void bootloader_hw_set_to_safe(void)
{
pin_to_safe(SYSCTL_PERIPH_GPIOC, GPIO_PORTC_BASE, GPIO_PIN_4 | GPIO_PIN_5);
pin_to_safe(SYSCTL_PERIPH_GPIOG, GPIO_PORTG_BASE, GPIO_PIN_5);
pin_to_safe(SYSCTL_PERIPH_GPIOF, GPIO_PORTF_BASE, GPIO_PIN_3);

pin_to_safe(SYSCTL_PERIPH_GPIOE, GPIO_PORTE_BASE,
GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5);
pin_to_safe(SYSCTL_PERIPH_GPIOD, GPIO_PORTD_BASE,
GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);
SWITCH_Pin::hw_set_to_safe();
}

extern void bootloader_reset_segments(void);
extern unsigned long cm3_cpu_clock_hz;

void bootloader_hw_init()
{
bootloader_reset_segments();
ROM_SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_20MHZ);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0);
ROM_GPIOPinConfigure(GPIO_PB4_CAN0RX);
ROM_GPIOPinConfigure(GPIO_PB5_CAN0TX);
ROM_GPIOPinTypeCAN(GPIO_PORTB_BASE, GPIO_PIN_4 | GPIO_PIN_5);

ROM_CANInit(CAN0_BASE);
ROM_CANBitRateSet(CAN0_BASE, cm3_cpu_clock_hz,
config_nmranet_can_bitrate());

// Sets up CAN message receiving object.
tCANMsgObject can_message;
can_message.ui32MsgID = 0;
can_message.ui32MsgIDMask = 0;
can_message.ui32Flags = MSG_OBJ_RX_INT_ENABLE | MSG_OBJ_USE_ID_FILTER;
can_message.ui32MsgLen = 8;
ROM_CANMessageSet(CAN0_BASE, 1, &can_message, MSG_OBJ_TYPE_RX);

ROM_CANEnable(CAN0_BASE);

SWITCH_Pin::hw_init();
// Sets up LEDs.
GOLD_LED_Pin::hw_init();
BLUE_LED_Pin::hw_init();
GREEN_Pin::hw_init();
YELLOW_Pin::hw_init();
BLUE_Pin::hw_init();
}

void bootloader_led(enum BootloaderLed id, bool value) {
switch(id) {
case LED_ACTIVE: GOLD_LED_Pin::set(value); return;
case LED_WRITING: BLUE_LED_Pin::set(value); return;
case LED_CSUM_ERROR: BLUE_Pin::set(value); return;
case LED_REQUEST: YELLOW_Pin::set(value); return;
default: ; /* ignore */
}
}

bool request_bootloader()
{
extern uint32_t __bootloader_magic_ptr;
if (__bootloader_magic_ptr == REQUEST_BOOTLOADER) {
__bootloader_magic_ptr = 0;
GREEN_Pin::set(true);
return true;
}
GREEN_Pin::set(SWITCH_Pin::get());
return !SWITCH_Pin::get();
}

} // extern "C"
2 changes: 2 additions & 0 deletions applications/bootloader_client/main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include "nmranet/If.hxx"
#include "nmranet/AliasAllocator.hxx"
#include "nmranet/DefaultNode.hxx"
#include "nmranet/NodeInitializeFlow.hxx"
#include "utils/socket_listener.hxx"

#include "freertos/bootloader_hal.h"
Expand All @@ -66,6 +67,7 @@ CanHubFlow can_hub0(&g_service);
static const nmranet::NodeID NODE_ID = 0x05010101181FULL;

nmranet::IfCan g_if_can(&g_executor, &can_hub0, 3, 3, 2);
nmranet::InitializeFlow g_init_flow{&g_service};
nmranet::CanDatagramService g_datagram_can(&g_if_can, 10, 2);
static nmranet::AddAliasAllocator g_alias_allocator(NODE_ID, &g_if_can);
nmranet::DefaultNode g_node(&g_if_can, NODE_ID);
Expand Down
3 changes: 3 additions & 0 deletions applications/js_hub/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SUBDIRS = targets
-include openmrnpath.mk
include $(OPENMRNPATH)/etc/recurse.mk
1 change: 1 addition & 0 deletions applications/js_hub/config.mk
2 changes: 2 additions & 0 deletions applications/js_hub/subdirs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SUBDIRS = \

3 changes: 3 additions & 0 deletions applications/js_hub/targets/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SUBDIRS = js.emscripten

include $(OPENMRNPATH)/etc/recurse.mk
1 change: 1 addition & 0 deletions applications/js_hub/targets/js.emscripten/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
js_hub.js
3 changes: 3 additions & 0 deletions applications/js_hub/targets/js.emscripten/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-include ../../config.mk
include $(OPENMRNPATH)/etc/prog.mk
LDFLAGS += --bind
1 change: 1 addition & 0 deletions applications/js_hub/targets/js.emscripten/lib/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include $(OPENMRNPATH)/etc/app_target_lib.mk
215 changes: 215 additions & 0 deletions applications/js_hub/targets/js.emscripten/main.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
/** \copyright
* Copyright (c) 2013, Balazs Racz
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* \file main.cxx
*
* An application which acts as an openlcb hub with the GC protocol.
*
* @author Balazs Racz
* @date 3 Aug 2013
*/

#include <emscripten.h>
#include <emscripten/bind.h>
#include <emscripten/val.h>

#include <memory>

#include "os/os.h"
#include "utils/constants.hxx"
#include "utils/Hub.hxx"
#include "utils/GridConnectHub.hxx"
#include "utils/GcTcpHub.hxx"
#include "executor/Executor.hxx"
#include "executor/Service.hxx"

Executor<1> g_executor{NO_THREAD()};
Service g_service(&g_executor);
CanHubFlow can_hub0(&g_service);
GcPacketPrinter packet_printer(&can_hub0);

OVERRIDE_CONST(gc_generate_newlines, 1);

int port = 12021;
const char *device_path = nullptr;

void usage(const char *e)
{
fprintf(stderr, "Usage: %s [-p port] [-d device_path]\n\n", e);
fprintf(stderr, "GridConnect CAN HUB.\nListens to a specific TCP port, "
"reads CAN packets from the incoming connections using "
"the GridConnect protocol, and forwards all incoming "
"packets to all other participants.\n\nArguments:\n");
fprintf(stderr, "\t-p port specifies the port number to listen on, "
"default is 12021.\n");
fprintf(stderr, "\t-d device is a path to a physical device doing "
"serial-CAN or USB-CAN. If specified, opens device and "
"adds it to the hub.\n");
exit(1);
}

void parse_args(int argc, char *argv[])
{
int opt;
while ((opt = getopt(argc, argv, "hp:d:n:a:s:f:c:")) >= 0)
{
switch (opt)
{
case 'h':
usage(argv[0]);
break;
case 'd':
device_path = optarg;
break;
case 'p':
port = atoi(optarg);
break;
default:
fprintf(stderr, "Unknown option %c\n", opt);
usage(argv[0]);
}
}
}

class JSHubPort : public HubPortInterface
{
public:
JSHubPort(HubFlow *parent, emscripten::val send_fn)
: parent_(parent)
, sendFn_(send_fn)
{
HASSERT(sendFn_.typeof().as<std::string>() == "function");
parent_->register_port(this);
}

~JSHubPort()
{
parent_->unregister_port(this);
}

void send(HubPortInterface::message_type *buffer, unsigned priority = UINT_MAX) OVERRIDE
{
sendFn_((string &)*buffer->data());
buffer->unref();
}

void recv(string s)
{
auto *b = parent_->alloc();
b->data()->assign(s);
b->data()->skipMember_ = this;
parent_->send(b);
}

private:
HubFlow *parent_;
emscripten::val sendFn_;
};

class JSTcpHub
{
public:
JSTcpHub(CanHubFlow *hflow, int port)
: canHub_(hflow)
, gcHub_(canHub_->service())
, gcAdapter_(
GCAdapterBase::CreateGridConnectAdapter(&gcHub_, canHub_, false))
{
EM_ASM_(
{
var net = require('net');
var server = net.createServer(function(c)
{
console.log('client connected');
c.setEncoding('utf-8');
var client_port = new Module.JSHubPort($1, function(data)
{ c.write(data); });
c.on('close', function()
{
console.log('client disconnected');
client_port.delete ();
});
c.on('data', function(data)
{ client_port.recv(data); });
});
server.listen($0, function()
{ console.log('listening on port ' + $0); });
},
port, &gcHub_);
}

private:
CanHubFlow *canHub_;
HubFlow gcHub_;
std::unique_ptr<GCAdapterBase> gcAdapter_;
};

/** Entry point to application.
* @param argc number of command line arguments
* @param argv array of command line arguments
* @return 0, should never return
*/
int appl_main(int argc, char *argv[])
{
parse_args(argc, argv);
JSTcpHub hub(&can_hub0, port);
/* int dev_fd = 0;
while (1)
{
if (device_path && !dev_fd)
{
dev_fd = ::open(device_path, O_RDWR);
if (dev_fd > 0)
{
// Sets up the terminal in raw mode. Otherwise linux might echo
// characters coming in from the device and that will make
// packets go back to where they came from.
HASSERT(!tcflush(dev_fd, TCIOFLUSH));
struct termios settings;
HASSERT(!tcgetattr(dev_fd, &settings));
cfmakeraw(&settings);
HASSERT(!tcsetattr(dev_fd, TCSANOW, &settings));
LOG(INFO, "Opened device %s.\n", device_path);
create_gc_port_for_can_hub(&can_hub0, dev_fd);
}
else
{
LOG(ERROR, "Failed to open device %s: %s\n", device_path,
strerror(errno));
}
}
sleep(1);
}*/
g_executor.thread_body();
return 0;
}

EMSCRIPTEN_BINDINGS(js_hub_module)
{
emscripten::class_<JSHubPort>("JSHubPort")
.constructor<HubFlow *, emscripten::val>()
.function("recv", &JSHubPort::recv);
}
2 changes: 1 addition & 1 deletion bin/callgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def DebugString(self):
d += ("address = %x " % self.address)
d += ("objfile = %s " % self.objfile)
d += ("in edge count = %d " % len(self.indeps))
d += ("in sysle count = %d " % self.in_cycle_edge_count)
d += ("in cycle count = %d " % self.in_cycle_edge_count)
return d


Expand Down
Loading

0 comments on commit b5b08e2

Please sign in to comment.