Revision 1678f270 core/src/aos_system.c

View differences:

core/src/aos_system.c
1 1
/*
2 2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2018  Thomas Schöpping et al.
3
Copyright (C) 2016..2019  Thomas Schöpping et al.
4 4

  
5 5
This program is free software: you can redistribute it and/or modify
6 6
it under the terms of the GNU General Public License as published by
......
79 79
 */
80 80
static systime_t _synctime;
81 81

  
82
#if (AMIROOS_CFG_SSSP_ENABLE == true)
83
#if (AMIROOS_CFG_SSSP_MASTER == true) || defined(__DOXYGEN__)
82
#if ((AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_MASTER == true)) || defined(__DOXYGEN__)
84 83
/**
85 84
 * @brief   Timer to drive the SYS_SYNC signal for system wide time synchronization according to SSSP.
86 85
 */
87 86
static virtual_timer_t _syssynctimer;
88 87

  
89
#endif
90

  
91 88
/**
92 89
 * @brief   Last uptime of system wide time synchronization.
93 90
 */
94 91
static aos_timestamp_t _syssynctime;
95 92
#endif
96 93

  
97
#if (AMIROOS_CFG_SSSP_ENABLE == true)
98
#if ((AMIROOS_CFG_SSSP_MASTER != true) && (AMIROOS_CFG_PROFILE == true)) || defined(__DOXYGEN__)
94
#if ((AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_MASTER != true) && (AMIROOS_CFG_PROFILE == true)) || defined(__DOXYGEN__)
99 95
/**
100 96
 * @brief   Offset between local clock and system wide synchronization signal.
101 97
 */
......
105 101
 * @brief   Weighting factor for the low-pass filter used for calculating the @p _syssyncskew value.
106 102
 */
107 103
#define SYSTEM_SYSSYNCSKEW_LPFACTOR   (0.1f / AOS_SYSTEM_TIME_RESOLUTION)
108
#endif
109
#endif
104
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_MASTER != true) && (AMIROOS_CFG_PROFILE == true) */
110 105

  
111 106
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__)
112 107
/**
......
146 141
 * @brief   Shell command to shutdown the system.
147 142
 */
148 143
static aos_shellcommand_t _shellcmd_shutdown = {
144
#if (AMIROOS_CFG_SSSP_ENABLE == true)
149 145
  /* name     */ "system:shutdown",
146
#else
147
  /* name     */ "module:shutdown",
148
#endif
150 149
  /* callback */ _shellcmd_shutdowncb,
151 150
  /* next     */ NULL,
152 151
};
......
242 241

  
243 242
  // print static information about module and operating system
244 243
  _printSystemInfoSeparator(stream, '=', SYSTEM_INFO_WIDTH);
244
#ifdef BOARD_VERSION
245 245
  _printSystemInfoLine(stream, "Module", SYSTEM_INFO_NAMEWIDTH, "%s (v%s)", BOARD_NAME, BOARD_VERSION);
246
#else
247
  _printSystemInfoLine(stream, "Module", SYSTEM_INFO_NAMEWIDTH, "%s", BOARD_NAME);
248
#endif
246 249
#ifdef PLATFORM_NAME
247 250
  _printSystemInfoLine(stream, "Platform", SYSTEM_INFO_NAMEWIDTH, "%s", PLATFORM_NAME);
248 251
#endif
......
253 256
  _printSystemInfoSeparator(stream, '-', SYSTEM_INFO_WIDTH);
254 257
#if (AMIROOS_CFG_SSSP_ENABLE == true)
255 258
  _printSystemInfoLine(stream, "AMiRo-OS" , SYSTEM_INFO_NAMEWIDTH, "%u.%u.%u %s (SSSP %u.%u)", AMIROOS_VERSION_MAJOR, AMIROOS_VERSION_MINOR, AMIROOS_VERSION_PATCH, AMIROOS_RELEASE_TYPE, AOS_SYSTEM_SSSP_VERSION_MAJOR, AOS_SYSTEM_SSSP_VERSION_MINOR);
259
#else
260
  _printSystemInfoLine(stream, "AMiRo-OS" , SYSTEM_INFO_NAMEWIDTH, "%u.%u.%u %s", AMIROOS_VERSION_MAJOR, AMIROOS_VERSION_MINOR, AMIROOS_VERSION_PATCH, AMIROOS_RELEASE_TYPE);
256 261
#endif
257 262
  _printSystemInfoLine(stream, "AMiRo-LLD" , SYSTEM_INFO_NAMEWIDTH, "%u.%u.%u %s (periphAL %u.%u)", AMIRO_LLD_VERSION_MAJOR, AMIRO_LLD_VERSION_MINOR, AMIRO_LLD_VERSION_PATCH, AMIRO_LLD_RELEASE_TYPE, PERIPHAL_VERSION_MAJOR, PERIPHAL_VERSION_MINOR);
258 263
  _printSystemInfoLine(stream, "ChibiOS/RT" , SYSTEM_INFO_NAMEWIDTH, "%u.%u.%u %s", CH_KERNEL_MAJOR, CH_KERNEL_MINOR, CH_KERNEL_PATCH, (CH_KERNEL_STABLE == 1) ? "stable" : "non-stable");
......
264 269
  // print static information about the bootloader
265 270
  _printSystemInfoSeparator(stream, '-', SYSTEM_INFO_WIDTH);
266 271
  if (BL_CALLBACK_TABLE_ADDRESS->magicNumber == BL_MAGIC_NUMBER) {
267
#if (AMIROOS_CFG_SSSP_ENABLE == true)
268 272
    _printSystemInfoLine(stream, "AMiRo-BLT", SYSTEM_INFO_NAMEWIDTH, "%u.%u.%u %s (SSSP %u.%u)", BL_CALLBACK_TABLE_ADDRESS->vBootloader.major, BL_CALLBACK_TABLE_ADDRESS->vBootloader.minor, BL_CALLBACK_TABLE_ADDRESS->vBootloader.patch,
269 273
                         (BL_CALLBACK_TABLE_ADDRESS->vBootloader.identifier == BL_VERSION_ID_AMiRoBLT_Release) ? "stable" :
270 274
                         (BL_CALLBACK_TABLE_ADDRESS->vBootloader.identifier == BL_VERSION_ID_AMiRoBLT_ReleaseCandidate) ? "release candidate" :
......
273 277
                         (BL_CALLBACK_TABLE_ADDRESS->vBootloader.identifier == BL_VERSION_ID_AMiRoBLT_PreAlpha) ? "pre-alpha" :
274 278
                         "<release type unknown>",
275 279
                         BL_CALLBACK_TABLE_ADDRESS->vSSSP.major, BL_CALLBACK_TABLE_ADDRESS->vSSSP.minor);
276

  
280
#if (AMIROOS_CFG_SSSP_ENABLE == true)
277 281
    if (BL_CALLBACK_TABLE_ADDRESS->vSSSP.major != AOS_SYSTEM_SSSP_VERSION_MAJOR) {
278 282
      if (stream) {
279 283
        chprintf(stream, "WARNING: Bootloader and AMiRo-OS implement incompatible SSSP versions!\n");
......
293 297

  
294 298
  // print dynamic information about the module
295 299
  _printSystemInfoSeparator(stream, '-', SYSTEM_INFO_WIDTH);
296
  #if (AMIROOS_CFG_SSSP_ENABLE == true)
300
#if (AMIROOS_CFG_SSSP_ENABLE == true)
297 301
  if (aos.sssp.moduleId != 0) {
298 302
    _printSystemInfoLine(stream, "Module ID", SYSTEM_INFO_NAMEWIDTH, "%u", aos.sssp.moduleId);
299 303
  } else {
......
508 512
  chprintf(stream, "%10u seconds\n", (uint8_t)(uptime % MICROSECONDS_PER_MINUTE / MICROSECONDS_PER_SECOND));
509 513
  chprintf(stream, "%10u milliseconds\n", (uint16_t)(uptime % MICROSECONDS_PER_SECOND / MICROSECONDS_PER_MILLISECOND));
510 514
  chprintf(stream, "%10u microseconds\n", (uint16_t)(uptime % MICROSECONDS_PER_MILLISECOND / MICROSECONDS_PER_MICROSECOND));
511
#if (AMIROOS_CFG_SSSP_ENABLE == true)
512
#if (AMIROOS_CFG_SSSP_MASTER != true) && (AMIROOS_CFG_PROFILE == true)
515
#if (AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_MASTER != true) && (AMIROOS_CFG_PROFILE == true)
513 516
  chprintf(stream, "SSSP synchronization offset: %.3fus per %uus\n", _syssyncskew, AMIROOS_CFG_SSSP_SYSSYNCPERIOD);
514 517
#endif /* AMIROOS_CFG_SSSP_MASTER != true && AMIROOS_CFG_PROFILE == true */
515 518
  _printSystemInfoSeparator(stream, '=', SYSTEM_INFO_WIDTH);
516
#endif
519

  
517 520
#if (AMIROOS_CFG_SHELL_ENABLE == true)
518 521
  // print shell info
519 522
  chprintf(stream, "System shell information:\n");
520 523
  chprintf(stream, "\tnumber of commands:      %u\n", aosShellCountCommands(&aos.shell));
521 524
  chprintf(stream, "\tmaximum line width:      %u characters\n", aos.shell.linesize);
522 525
  chprintf(stream, "\tmaximum #arguments:      %u\n", aos.shell.arglistsize);
526
#if (AMIROOS_CFG_DBG == true)
523 527
  chprintf(stream, "\tshell thread stack size: %u bytes\n", aosThdGetStacksize(aos.shell.thread));
524 528
#if (CH_DBG_FILL_THREADS == TRUE)
525 529
  chprintf(stream, "\tstack peak utilization:  %u bytes (%.2f%%)\n", aosThdGetStackPeakUtilization(aos.shell.thread), (float)aosThdGetStackPeakUtilization(aos.shell.thread) / (float)aosThdGetStacksize(aos.shell.thread) * 100.0f);
526 530
#endif /* CH_DBG_FILL_THREADS == TRUE */
531
#endif /* AMIROOS_CFG_DBG == true */
527 532
  _printSystemInfoSeparator(stream, '=', SYSTEM_INFO_WIDTH);
528 533
#endif /* AMIROOS_CFG_SHELL_ENABLE == true */
529 534

  
......
545 550
{
546 551
  aosDbgCheck(stream != NULL);
547 552

  
553
#if (AMIROOS_CFG_SSSP_ENABLE == true)
548 554
  // print help text
549 555
  if (argc != 2 || strcmp(argv[1], "--help") == 0) {
550 556
    chprintf(stream, "Usage: %s OPTION\n", argv[0]);
......
592 598
      return AOS_INVALID_ARGUMENTS;
593 599
    }
594 600
  }
601
#else /* AMIROOS_CFG_SSSP_ENABLE == false */
602
  (void)argv;
603
  (void)argc;
604

  
605
  chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_SHUTDOWN);
606
  chThdTerminate(chThdGetSelfX());
607
  return AOS_OK;
608
#endif /* AMIROOS_CFG_SSSP_ENABLE */
595 609
}
596 610

  
597 611
#if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
......
618 632
#endif /* AMIROOS_CFG_TESTS_ENABLE == true */
619 633
#endif /* AMIROOS_CFG_SHELL_ENABLE == true */
620 634

  
635
// suppress warning in case no interrupt GPIOs are defined
636
#pragma GCC diagnostic push
637
#pragma GCC diagnostic ignored "-Wunused-function"
621 638
/**
622 639
 * @brief   Generic callback function for GPIO interrupts.
623 640
 *
......
628 645
  aosDbgCheck((args != NULL) && (*((iopadid_t*)args) < sizeof(eventflags_t) * 8));
629 646

  
630 647
  chSysLockFromISR();
631
  chEvtBroadcastFlagsI(&aos.events.io, (eventflags_t)1 << *((iopadid_t*)args));
648
  chEvtBroadcastFlagsI(&aos.events.io, AOS_IOEVENT_FLAG(*((iopadid_t*)args)));
632 649
  chSysUnlockFromISR();
633 650

  
634 651
  return;
635 652
}
653
#pragma GCC diagnostic pop
636 654

  
637
#if (AMIROOS_CFG_SSSP_ENABLE == true)
638
#if (AMIROOS_CFG_SSSP_MASTER != true) || defined(__DOXYGEN__)
655
#if ((AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_MASTER != true)) || defined(__DOXYGEN__)
639 656
/**
640 657
 * @brief   Callback function for the Sync signal interrupt.
641 658
 *
......
672 689
    }
673 690
  }
674 691
  // broadcast event
675
  chEvtBroadcastFlagsI(&aos.events.io, (eventflags_t)1 << *((iopadid_t*)args));
692
  chEvtBroadcastFlagsI(&aos.events.io, AOS_IOEVENT_FLAG(*((iopadid_t*)args)));
676 693
  chSysUnlockFromISR();
677 694

  
678 695
  return;
679 696
}
680
#endif
681
#endif
697
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_MASTER != true) */
682 698

  
683 699
/**
684 700
 * @brief   Callback function for the uptime accumulation timer.
......
701 717

  
702 718
  return;
703 719
}
704
#if (AMIROOS_CFG_SSSP_ENABLE == true)
705
#if (AMIROOS_CFG_SSSP_MASTER == true) || defined (__DOXYGEN__)
720

  
721
#if ((AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_MASTER == true)) || defined (__DOXYGEN__)
706 722
/**
707 723
 * @brief   Periodic system synchronization callback function.
708 724
 * @details Toggles the SYS_SYNC signal and reconfigures the system synchronization timer.
......
736 752

  
737 753
  return;
738 754
}
739
#endif
740
#endif
755
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_MASTER == true) */
741 756

  
742 757
/**
743 758
 * @brief   AMiRo-OS system initialization.
......
756 771

  
757 772
  /* set local variables */
758 773
  chVTObjectInit(&_systimer);
774
#if (AMIROOS_CFG_SSSP_ENABLE == true)
759 775
  _synctime = 0;
760 776
  _uptime = 0;
761
  #if (AMIROOS_CFG_SSSP_ENABLE == true)
762 777
#if (AMIROOS_CFG_SSSP_MASTER == true)
763 778
  chVTObjectInit(&_syssynctimer);
764 779
  _syssynctime = 0;
......
766 781
#if (AMIROOS_CFG_SSSP_MASTER != true) && (AMIROOS_CFG_PROFILE == true)
767 782
  _syssyncskew = 0.0f;
768 783
#endif
769
#endif
784
#else /* AMIROOS_CFG_SSSP_ENABLE == false */
785
  // start the uptime counter
786
  chSysLock();
787
  _synctime = chVTGetSystemTimeX();
788
  _uptime = 0;
789
  chVTSetI(&_systimer, SYSTIMER_PERIOD, &_uptimeCallback, NULL);
790
  chSysUnlock();
791
#endif /* AMIROOS_CFG_SSSP_ENABLE */
770 792

  
771
  #if (AMIROOS_CFG_SSSP_ENABLE == true)
772 793
  /* initialize aos configuration */
794
#if (AMIROOS_CFG_SSSP_ENABLE == true)
773 795
  aos.sssp.stage = AOS_SSSP_STARTUP_2_1;
774 796
  aos.sssp.moduleId = 0;
775 797
#endif
......
778 800
  chEvtObjectInit(&aos.events.os);
779 801

  
780 802
  /* interrupt setup */
803
#if (AMIROOS_CFG_SSSP_ENABLE == true)
781 804
  // PD signal
782
  #if (AMIROOS_CFG_SSSP_ENABLE == true)
783 805
  palSetPadCallback(moduleSsspGpioPd.gpio->port, moduleSsspGpioPd.gpio->pad, _intCallback, &moduleSsspGpioPd.gpio->pad);
784 806
  palEnablePadEvent(moduleSsspGpioPd.gpio->port, moduleSsspGpioPd.gpio->pad, APAL2CH_EDGE(moduleSsspGpioPd.meta.edge));
785 807
  // SYNC signal
......
799 821
  palSetPadCallback(moduleSsspGpioUp.gpio->port, moduleSsspGpioUp.gpio->pad, _intCallback, &moduleSsspGpioUp.gpio->pad);
800 822
  palEnablePadEvent(moduleSsspGpioUp.gpio->port, moduleSsspGpioUp.gpio->pad, APAL2CH_EDGE(moduleSsspGpioUp.meta.edge));
801 823
#endif
802
#endif
824
#endif /* AMIROOS_CFG_SSSP_ENABLE == true */
803 825
#ifdef MODULE_INIT_INTERRUPTS
804 826
  // further interrupt signals
805 827
  MODULE_INIT_INTERRUPTS();
......
821 843
#if (AMIROOS_CFG_TESTS_ENABLE == true)
822 844
  aosShellAddCommand(&aos.shell, &_shellcmd_kerneltest);
823 845
#endif
824
#endif
846
#endif /* AMIROOS_CFG_SHELL_ENABLE == true */
825 847

  
826 848
  return;
827 849
}
......
847 869
    chSysUnlock();
848 870
  }
849 871
#endif
850
#endif
872
#endif /* AMIROOS_CFG_SSSP_ENABLE == true */
851 873

  
852 874
  // print system information;
853 875
  _printSystemInfo((BaseSequentialStream*)&aos.iostream);
......
864 886

  
865 887
  return;
866 888
}
867
#if (AMIROOS_CFG_SSSP_ENABLE == true)
889

  
890
#if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__)
868 891
/**
869 892
 * @brief   Implements the SSSP startup synchronization step.
870 893
 *
......
915 938
    return m;
916 939
  }
917 940
}
918
#endif
941
#endif /* AMIROOS_CFG_SSSP_ENABLE == true */
919 942

  
920 943
/**
921 944
 * @brief   Retrieves the system uptime.
......
983 1006
  // update the system SSSP stage
984 1007
  aos.sssp.stage = AOS_SSSP_SHUTDOWN_1_1;
985 1008

  
986
  // activate the SYS_PD control signal only, if this module initiated the shutdown
987 1009
  chSysLock();
1010
  // activate the SYS_PD control signal only, if this module initiated the shutdown
988 1011
  if (shutdown != AOS_SHUTDOWN_PASSIVE) {
989 1012
    apalControlGpioSet(&moduleSsspGpioPd, APAL_GPIO_ON);
990 1013
  }
991 1014
  // activate the SYS_SYNC signal
992 1015
  apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_ON);
993 1016
  chSysUnlock();
994
#endif
1017
#endif /* AMIROOS_CFG_SSSP_ENABLE == true */
995 1018

  
996 1019
  switch (shutdown) {
997 1020
    case AOS_SHUTDOWN_PASSIVE:
......
1014 1037
      chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_RESTART);
1015 1038
      aosprintf("restarting system...\n");
1016 1039
      break;
1017
   // must never occur
1018
   case AOS_SHUTDOWN_NONE:
1019
   default:
1040
    // must never occur
1041
    case AOS_SHUTDOWN_NONE:
1042
    default:
1020 1043
      break;
1021 1044
  }
1045

  
1022 1046
#if (AMIROOS_CFG_SSSP_ENABLE == true)
1023 1047
  // update the system SSSP stage
1024 1048
  aos.sssp.stage = AOS_SSSP_SHUTDOWN_1_2;
1025 1049
#endif
1050

  
1026 1051
  return;
1027 1052
}
1028 1053

  
......
1060 1085
  // disable all interrupts
1061 1086
  irqDeinit();
1062 1087

  
1063
  #if (AMIROOS_CFG_SSSP_ENABLE == true)
1088
#if (AMIROOS_CFG_SSSP_ENABLE == true)
1064 1089
  // update the system SSSP stage
1065 1090
  aos.sssp.stage = AOS_SSSP_SHUTDOWN_1_3;
1066 1091
#endif
1067 1092

  
1068
  // call bootloader callback depending on arguments
1069
  switch (shutdown) {
1070
    case AOS_SHUTDOWN_PASSIVE:
1071
      BL_CALLBACK_TABLE_ADDRESS->cbHandleShutdownRequest();
1072
      break;
1073
    case AOS_SHUTDOWN_HIBERNATE:
1074
      BL_CALLBACK_TABLE_ADDRESS->cbShutdownHibernate();
1075
      break;
1076
    case AOS_SHUTDOWN_DEEPSLEEP:
1077
      BL_CALLBACK_TABLE_ADDRESS->cbShutdownDeepsleep();
1078
      break;
1079
    case AOS_SHUTDOWN_TRANSPORTATION:
1080
      BL_CALLBACK_TABLE_ADDRESS->cbShutdownTransportation();
1081
      break;
1082
    case AOS_SHUTDOWN_RESTART:
1083
      BL_CALLBACK_TABLE_ADDRESS->cbShutdownRestart();
1084
      break;
1085
    // must never occur
1086
    case AOS_SHUTDOWN_NONE:
1087
    default:
1088
      break;
1093
  // validate bootloader
1094
  if ((BL_CALLBACK_TABLE_ADDRESS->magicNumber == BL_MAGIC_NUMBER) &&
1095
      (BL_CALLBACK_TABLE_ADDRESS->vBootloader.major == BL_VERSION_MAJOR) &&
1096
      (BL_CALLBACK_TABLE_ADDRESS->vBootloader.minor >= BL_VERSION_MINOR)) {
1097
    // call bootloader callback depending on arguments
1098
    switch (shutdown) {
1099
      case AOS_SHUTDOWN_PASSIVE:
1100
        BL_CALLBACK_TABLE_ADDRESS->cbHandleShutdownRequest();
1101
        break;
1102
      case AOS_SHUTDOWN_HIBERNATE:
1103
        BL_CALLBACK_TABLE_ADDRESS->cbShutdownHibernate();
1104
        break;
1105
      case AOS_SHUTDOWN_DEEPSLEEP:
1106
        BL_CALLBACK_TABLE_ADDRESS->cbShutdownDeepsleep();
1107
        break;
1108
      case AOS_SHUTDOWN_TRANSPORTATION:
1109
        BL_CALLBACK_TABLE_ADDRESS->cbShutdownTransportation();
1110
        break;
1111
      case AOS_SHUTDOWN_RESTART:
1112
        BL_CALLBACK_TABLE_ADDRESS->cbShutdownRestart();
1113
        break;
1114
      // must never occur
1115
      case AOS_SHUTDOWN_NONE:
1116
      default:
1117
        break;
1118
    }
1119
  } else {
1120
    // fallback if bootloader was found to be invalid
1121
    aosprintf("Bootloader incompatible or not available!\n");
1122
    chThdSleep(TIME_INFINITE);
1089 1123
  }
1090 1124

  
1091 1125
  return;

Also available in: Unified diff