RKH
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
"bsp.c"
/* -------------------------- Development history -------------------------- */
/*
* 2016.12.06 LeFr v2.4.05 Initial version
*/
/* -------------------------------- Authors -------------------------------- */
/*
* LeFr Leandro Francucci francuccilea@gmail.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_EXIT_CRITICAL(dummy);
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 ------------------------------ */