aboutsummaryrefslogtreecommitdiffstats
path: root/demos/Win32-MinGW/chcore.c
blob: 047fe66da1225c949face6caff5fc2ef30199e53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
    ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio.

    This file is part of ChibiOS/RT.

    ChibiOS/RT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    ChibiOS/RT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * Core file for MingGW32 demo project.
 */

#include <windows.h>
#include <stdio.h>

#undef CDECL

#include <ch.h>

static LARGE_INTEGER nextcnt;
static LARGE_INTEGER slice;

void InitSimCom1(void);
void InitSimCom2(void);
BOOL Com1ConnInterruptSimCom(void);
BOOL Com2ConnInterruptSimCom(void);
BOOL Com1InInterruptSimCom(void);
BOOL Com2InInterruptSimCom(void);
BOOL Com1OutInterruptSimCom(void);
BOOL Com2OutInterruptSimCom(void);

/*
 * Simulated HW initialization.
 */
void InitCore(void) {
  WSADATA wsaData;

  // Initialization.
  if (WSAStartup(2, &wsaData) != 0) {
    printf("Unable to locate a winsock DLL\n");
    exit(1);
  }

  printf("Win32 ChibiOS/RT simulator\n\n");
  printf("Thread structure %d bytes\n", sizeof(Thread));
  if (!QueryPerformanceFrequency(&slice)) {
    printf("QueryPerformanceFrequency() error");
    exit(1);
  }
  printf("Core Frequency   %u Hz\n", (int)slice.LowPart);
  slice.QuadPart /= CH_FREQUENCY;
  QueryPerformanceCounter(&nextcnt);
  nextcnt.QuadPart += slice.QuadPart;

  InitSimCom1();
  InitSimCom2();
}

/*
 * Interrupt simulation.
 */
void ChkIntSources(void) {
  LARGE_INTEGER n;

  if (Com1InInterruptSimCom()   || Com2InInterruptSimCom()  ||
      Com1OutInterruptSimCom()  || Com2OutInterruptSimCom() ||
      Com1ConnInterruptSimCom() || Com2ConnInterruptSimCom()) {
    if (chSchRescRequiredI())
      chSchDoRescheduleI();
    return;
  }

  // Interrupt Timer simulation (10ms interval).
  QueryPerformanceCounter(&n);
  if (n.QuadPart > nextcnt.QuadPart) {
    nextcnt.QuadPart += slice.QuadPart;
    chSysTimerHandlerI();
    if (chSchRescRequiredI())
      chSchDoRescheduleI();
  }
}

msg_t _IdleThread(void *p) {

  while (TRUE) {

    ChkIntSources();
    Sleep(0);
  }
}

__attribute__((fastcall)) void chSysHalt(void) {

  exit(2);
}
n>); #else /* Writing in buffer mode.*/ (void) obqGetEmptyBufferTimeout(&SDU1.obqueue, TIME_INFINITE); memcpy(SDU1.obqueue.ptr, buf, SERIAL_USB_BUFFERS_SIZE); obqPostFullBuffer(&SDU1.obqueue, SERIAL_USB_BUFFERS_SIZE); #endif } chprintf(chp, "\r\n\nstopped\r\n"); } static const ShellCommand commands[] = { {"write", cmd_write}, {NULL, NULL} }; static const ShellConfig shell_cfg1 = { (BaseSequentialStream *)&SDU1, commands }; static const ShellConfig shell_cfg2 = { (BaseSequentialStream *)&SDU2, commands }; /*===========================================================================*/ /* Generic code. */ /*===========================================================================*/ /* * Red LED blinker thread, times are in milliseconds. */ static THD_WORKING_AREA(waThread1, 128); static THD_FUNCTION(Thread1, arg) { (void)arg; chRegSetThreadName("blinker"); while (true) { systime_t time = serusbcfg1.usbp->state == USB_ACTIVE ? 250 : 500; palClearPad(GPIOE, GPIOE_LED3_RED); chThdSleepMilliseconds(time); palSetPad(GPIOE, GPIOE_LED3_RED); chThdSleepMilliseconds(time); } } /* * Application entry point. */ int main(void) { thread_t *shelltp1 = NULL; thread_t *shelltp2 = NULL; event_listener_t shell_el; /* * System initializations. * - HAL initialization, this also initializes the configured device drivers * and performs the board-specific initializations. * - Kernel initialization, the main() function becomes a thread and the * RTOS is active. */ halInit(); chSysInit(); /* * Initializes two serial-over-USB CDC drivers. */ sduObjectInit(&SDU1); sduStart(&SDU1, &serusbcfg1); sduObjectInit(&SDU2); sduStart(&SDU2, &serusbcfg2); /* * Activates the USB driver and then the USB bus pull-up on D+. * Note, a delay is inserted in order to not have to disconnect the cable * after a reset. */ usbDisconnectBus(serusbcfg1.usbp); chThdSleepMilliseconds(1500); usbStart(serusbcfg1.usbp, &usbcfg); usbConnectBus(serusbcfg1.usbp); /* * Shell manager initialization. * Event zero is shell exit. */ shellInit(); chEvtRegister(&shell_terminated, &shell_el, 0); /* * Creates the blinker thread. */ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); /* * Normal main() thread activity, managing two shells. */ while (true) { if (SDU1.config->usbp->state == USB_ACTIVE) { /* Starting shells.*/ if (shelltp1 == NULL) { shelltp1 = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, "shell1", NORMALPRIO + 1, shellThread, (void *)&shell_cfg1); } if (shelltp2 == NULL) { shelltp2 = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, "shell2", NORMALPRIO + 1, shellThread, (void *)&shell_cfg2); } /* Waiting for an exit event then freeing terminated shells.*/ chEvtWaitAny(EVENT_MASK(0)); if (chThdTerminatedX(shelltp1)) { chThdRelease(shelltp1); shelltp1 = NULL; } if (chThdTerminatedX(shelltp2)) { chThdRelease(shelltp2); shelltp2 = NULL; } } else { chThdSleepMilliseconds(1000); } } }