RKH
"bsp.c"
/* -------------------------- Development history -------------------------- */
/*
* 2016.12.06 LeFr v2.4.05 Initial version
*/
/* -------------------------------- Authors -------------------------------- */
/*
* LeFr Leandro Francucci lf@vortexmakes.com
*/
/* --------------------------------- Notes --------------------------------- */
/* ----------------------------- Include files ----------------------------- */
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "bsp.h"
#include "my.h"
#include "rkh.h"
/* ----------------------------- Local macros ------------------------------ */
#define BIN_TRACE 0
#define SOCKET_TRACE 1
#define ESC 0x1B
#define kbmap(c) ((c) - '0')
/* ------------------------------- Constants ------------------------------- */
#define SIZEOF_EP0STO 64
#define SIZEOF_EP0_BLOCK 4
#define SIZEOF_EP1STO 32
#define SIZEOF_EP1_BLOCK 8
/* ---------------------------- Local data types --------------------------- */
/* ---------------------------- Global variables --------------------------- */
rui8_t running;
/* ---------------------------- Local variables ---------------------------- */
static DWORD tick_msec; /* clock tick in msec */
static RKH_ROM_STATIC_EVENT(eterm, TERM);
static rui8_t ep0sto[SIZEOF_EP0STO],
ep1sto[SIZEOF_EP1STO];
#if BIN_TRACE == 1 /* For binary trace feature */
static FILE *ftbin;
#endif
#if SOCKET_TRACE == 1 /* For socket trace feature */
#include "tcptrc.h"
/* Trazer Tool IP Address */
#define TRC_IP_ADDR "127.0.0.1"
/* Trazer Tool TCP Port Address */
#define TRC_TCP_PORT 6602
/* Trace Socket */
static SOCKET tsock;
#define TCP_TRACE_OPEN() \
if (tcp_trace_open(TRC_TCP_PORT, \
TRC_IP_ADDR, &tsock) < 0) \
{ \
printf("Can't open socket %s:%u\n", \
TRC_IP_ADDR, TRC_TCP_PORT); \
exit(EXIT_FAILURE); \
}
#define TCP_TRACE_CLOSE() \
tcp_trace_close(tsock)
#define TCP_TRACE_SEND(d) \
tcp_trace_send(tsock, d)
#else
#define TCP_TRACE_OPEN() (void)0
#define TCP_TRACE_CLOSE() (void)0
#define TCP_TRACE_SEND(d) (void)0
#endif
#if BIN_TRACE == 1
#define FTBIN_FLUSH(d) \
fwrite (d, 1, 1, ftbin); \
fflush(ftbin)
#define FTBIN_CLOSE() \
fclose(ftbin)
#define FTBIN_OPEN() \
if ((ftbin = fopen("../ftbin", "w+b")) == NULL) \
{ \
perror("Can't open file\n"); \
exit(EXIT_FAILURE); \
}
#else
#define FTBIN_FLUSH(d) (void)0
#define FTBIN_CLOSE() (void)0
#define FTBIN_OPEN() (void)0
#endif
/* ----------------------- Local function prototypes ----------------------- */
/* ---------------------------- Local functions ---------------------------- */
static DWORD WINAPI
isr_tmr_thread(LPVOID par) /* Win32 thread to emulate timer ISR */
{
(void)par;
while (running)
{
Sleep(tick_msec);
}
return 0;
}
static DWORD WINAPI
isr_kbd_thread(LPVOID par) /* Win32 thread to emulate keyboard ISR */
{
int c;
MyEvt *mye;
(void)par;
while (running)
{
c = _getch();
if (c == ESC)
{
RKH_SMA_POST_FIFO(my, &eterm, 0);
}
else
{
mye = RKH_ALLOC_EVT(MyEvt, kbmap(c), 0);
mye->ts = (rui16_t)rand();
}
}
return 0;
}
static void
print_banner(void)
{
printf("Abstract Hierarchical State Machine (AHSM) example\n\n");
printf("RKH version = %s\n", RKH_RELEASE);
printf("Port version = %s\n", rkh_get_port_version());
printf("Port description = %s\n\n", rkh_get_port_desc());
printf("Description: \n\n"
"The goal of this demo application is to explain how to \n"
"represent a state machine using the RKH framework. To do \n"
"that is proposed a simple and abstract example, which is \n"
"shown in the documentation file Figure 1 section \n"
"\"Representing a State Machine\". \n\n\n");
printf("1.- Press <numbers> to send events to state machine. \n");
printf("2.- Press ESC to quit \n\n\n");
}
/* ---------------------------- Global functions --------------------------- */
void
{
DWORD thtmr_id, thkbd_id;
HANDLE hth_tmr, hth_kbd;
/* set the desired tick rate */
tick_msec = 1000UL / BSP_TICKS_PER_SEC;
running = (rui8_t)1;
/* create the ISR timer thread */
hth_tmr = CreateThread(NULL, 1024, &isr_tmr_thread, 0, 0, &thtmr_id);
RKH_ASSERT(hth_tmr != (HANDLE)0);
SetThreadPriority(hth_tmr, THREAD_PRIORITY_TIME_CRITICAL);
/* create the ISR keyboard thread */
hth_kbd = CreateThread(NULL, 1024, &isr_kbd_thread, 0, 0, &thkbd_id);
RKH_ASSERT(hth_kbd != (HANDLE)0);
SetThreadPriority(hth_kbd, THREAD_PRIORITY_NORMAL);
rkh_fwk_epool_register(ep0sto, SIZEOF_EP0STO, SIZEOF_EP0_BLOCK);
rkh_fwk_epool_register(ep1sto, SIZEOF_EP1STO, SIZEOF_EP1_BLOCK);
}
void
{
}
void
rkh_hook_idle(void) /* called within critical section */
{
RKH_WAIT_FOR_EVENTS(); /* yield the CPU until new event(s) arrive */
}
void
{
}
void
rkh_assert(RKHROM char * const file, int line)
{
fprintf(stderr, "RKH_ASSERT: [%d] line from %s "
"file\n", line, file);
RKH_TR_FWK_ASSERT((RKHROM char *)file, __LINE__);
__debugbreak();
}
#if RKH_CFG_TRC_EN == 1
void
{
FTBIN_OPEN();
TCP_TRACE_OPEN();
RKH_TRC_SEND_CFG(BSP_TS_RATE_HZ);
}
void
{
FTBIN_CLOSE();
TCP_TRACE_CLOSE();
}
{
return (RKH_TS_T)clock();
}
void
{
rui8_t *d;
while ((d = rkh_trc_get()) != (rui8_t*)0)
{
FTBIN_FLUSH(d);
TCP_TRACE_SEND(*d);
}
}
#endif
void
bsp_init(int argc, char *argv[])
{
(void)argc;
(void)argv;
srand((unsigned)time(NULL));
print_banner();
/* set trace filters */
}
/* ------------------------------ End of file ------------------------------ */
#define RKH_SMA_POST_FIFO(me_, e_, sender_)
Invoke the direct event posting facility rkh_sma_post_fifo(). If RKH_CFG_SMA_VFUNCT_EN is set RKH_ENA...
Definition: rkhsma.h:450
#define RKH_ASSERT(exp)
The RKH_ASSERT() macro is used to check expressions that ought to be true as long as the program is r...
Definition: rkhassert.h:129
#define RKH_THIS_MODULE
This macro appears at the top of each C/C++ source file defining a name for that file,...
void rkh_assert(const char *const file, int line)
Callback invoked in case the condition passed to RKH_ASSERT(), RKH_REQUIRE(), RKH_ENSURE(),...
#define RKH_TIM_TICK(_sender)
Invoke the system clock tick processing rkh_tmr_tick().
Definition: rkhtmr.h:89
void rkh_hook_start(void)
This hook function is called just before the RKH takes over control of the application.
void rkh_hook_exit(void)
This hook function is called just before the RKH returns to the underlying OS/RTOS....
void rkh_hook_timetick(void)
This function is called by rkh_tmr_tick(), which is assumed to be called from an ISR....
void rkh_hook_idle(void)
An idle hook function will only get executed (with interrupts LOCKED) when there are no SMAs of highe...
void rkh_trc_open(void)
Open the tracing session.
#define RKH_TRC_OPEN()
Open the tracing session.
Definition: rkhtrc_out.h:107
void rkh_trc_flush(void)
Platform-dependent macro flushing the trace stream.
void rkh_trc_close(void)
Close the tracing session.
#define RKH_TRC_SEND_CFG(ts_hz)
Send the trace facility configuration to host application software Trazer.
Definition: rkhtrc_out.h:221
#define RKH_TRC_FLUSH()
Platform-dependent macro flushing the trace stream.
Definition: rkhtrc_out.h:191
RKH_TS_T rkh_trc_getts(void)
Retrieves a timestamp to be placed in a trace event.
#define RKH_EVT_CAST(_e)
Perform cast to pointer to RKH event structure (RKH_EVT_T*).
Definition: rkhevt.h:77
#define RKH_ROM_STATIC_EVENT(ev_obj, ev_sig)
This macro declares and initializes the event structure ev_ob with ev_sig signal number and establish...
#define RKH_ALLOC_EVT(et, e, sender_)
This macro dynamically creates a new event of type et with its signal.
void rkh_fwk_exit(void)
Exit the RKH framework.
void rkh_fwk_init(void)
Initializes the RKH framework.
#define RKH_FILTER_OFF_GROUP_ALL_EVENTS(grp)
Emit (enable) all events in a specific group.
#define RKH_FILTER_ON_EVENT(evt)
Suppress (disable) one trace event. Use the RKH_TRC_ALL_EVENTS to disable all trace events.
Definition: rkhtrc_filter.h:92
#define RKH_FILTER_ON_GROUP(grp)
Suppress the enabled trace events from a specific group. Use the RKH_TRC_ALL_GROUPS to disable all gr...
Definition: rkhtrc_filter.h:72
#define RKH_FILTER_OFF_SMA(sma)
Emmit the enabled trace events related to a specified active object.
#define RKH_TR_FWK_ASSERT(mod_, ln_)
Assertion expression was evaluated to false.
RKH framwwork platform - independent interface.
#define RKH_EXIT_CRITICAL(dummy)
RKH need to disable interrupts in order to access critical sections of code, and re-enable interrupts...
Definition: rkhitl.h:2054
#define RKH_DIS_INTERRUPT()
RKH need to disable interrupts in order to access critical sections of code, and re-enable interrupts...
Definition: rkhitl.h:1998
#define RKH_RELEASE
This macro retrieves a pointer to string describing the RKH version. For example, "2....
Definition: rkhitl.h:100
rui32_t RKH_TS_T
Defines the size of trace timestamp.
#define RKH_TRC_ALL_EVENTS
Emit or suppress all trace events.
#define RKH_TG_SM
State Machine group (SM)
#define RKH_TRC_ALL_GROUPS
Emit or suppress tracing for all groups and events.
void rkh_trc_init(void)
Initializes the RKH's trace record service.
rui8_t * rkh_trc_get(void)
Retrieves a pointer to oldest stored byte in the trace stream. Frequently, this function is used by t...