RTCv2. Fixed testhal application.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@7454 35acf78f-673a-0410-8e92-d51de3d6d3f4
This commit is contained in:
barthess 2014-11-01 18:51:45 +00:00
parent 6d74608aff
commit ca14dc68fa
4 changed files with 122 additions and 72 deletions

View File

@ -30,7 +30,7 @@ endif
# Enable this if you want link time optimizations (LTO)
ifeq ($(USE_LTO),)
USE_LTO = yes
USE_LTO = no
endif
# If enabled, this option allows to compile the application in THUMB mode.

View File

@ -259,7 +259,7 @@
* @note The default is @p TRUE.
* @note Requires @p CH_CFG_USE_SEMAPHORES.
*/
#define CH_CFG_USE_MAILBOXES TRUE
#define CH_CFG_USE_MAILBOXES FALSE
/**
* @brief I/O Queues APIs.
@ -276,7 +276,7 @@
*
* @note The default is @p TRUE.
*/
#define CH_CFG_USE_MEMCORE TRUE
#define CH_CFG_USE_MEMCORE FALSE
/**
* @brief Heap Allocator APIs.
@ -288,7 +288,7 @@
* @p CH_CFG_USE_SEMAPHORES.
* @note Mutexes are recommended.
*/
#define CH_CFG_USE_HEAP TRUE
#define CH_CFG_USE_HEAP FALSE
/**
* @brief Memory Pools Allocator APIs.
@ -297,7 +297,7 @@
*
* @note The default is @p TRUE.
*/
#define CH_CFG_USE_MEMPOOLS TRUE
#define CH_CFG_USE_MEMPOOLS FALSE
/**
* @brief Dynamic Threads APIs.
@ -308,7 +308,7 @@
* @note Requires @p CH_CFG_USE_WAITEXIT.
* @note Requires @p CH_CFG_USE_HEAP and/or @p CH_CFG_USE_MEMPOOLS.
*/
#define CH_CFG_USE_DYNAMIC TRUE
#define CH_CFG_USE_DYNAMIC FALSE
/** @} */

View File

@ -14,10 +14,6 @@
limitations under the License.
*/
#include "ch.h"
#include "hal.h"
#if 0
/*
This structure is used to hold the values representing a calendar time.
It contains the following members, with the meanings as shown.
@ -32,44 +28,29 @@ int tm_wday days since Sunday [0-6]
int tm_yday days since January 1st [0-365]
int tm_isdst daylight savings indicator (1 = yes, 0 = no, -1 = unknown)
*/
#define WAKEUP_TEST FALSE
#define WAKEUP_TEST FALSE
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "ch.h"
#include "hal.h"
#include "shell.h"
#include "chprintf.h"
#include "chrtclib.h"
#if WAKEUP_TEST
static RTCWakeup wakeupspec;
#endif
static RTCAlarm alarmspec;
static RTCDateTime timespec;
static time_t unix_time;
/* libc stub */
int _getpid(void) {return 1;}
/* libc stub */
void _exit(int i) {(void)i;}
/* libc stub */
#include <errno.h>
#undef errno
extern int errno;
int _kill(int pid, int sig) {
(void)pid;
(void)sig;
errno = EINVAL;
return -1;
}
/* sleep indicator thread */
static WORKING_AREA(blinkWA, 128);
static msg_t blink_thd(void *arg){
/*
* Awake state indicator thread
*/
static THD_WORKING_AREA(blinkWA, 128);
static THD_FUNCTION(blink_thd, arg){
(void)arg;
while (TRUE) {
chThdSleepMilliseconds(100);
@ -78,7 +59,10 @@ static msg_t blink_thd(void *arg){
return 0;
}
static void func_sleep(void){
/*
* Helper functions putting MCU in sleep state
*/
static void anabiosis(void) {
chSysLock();
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
PWR->CR |= (PWR_CR_PDDS | PWR_CR_LPDS | PWR_CR_CSBF | PWR_CR_CWUF);
@ -87,24 +71,26 @@ static void func_sleep(void){
__WFI();
}
static void cmd_sleep(BaseSequentialStream *chp, int argc, char *argv[]){
/*
* Console applet for sleep testing
*/
static void cmd_sleep(BaseSequentialStream *chp, int argc, char *argv[]) {
(void)argv;
if (argc > 0) {
chprintf(chp, "Usage: sleep\r\n");
return;
}
chprintf(chp, "Going to sleep.\r\n");
chprintf(chp, "Going sleep...\r\n");
chThdSleepMilliseconds(200);
/* going to anabiosis */
func_sleep();
anabiosis();
}
/*
*
* Console applet for periodic alaram testing
*/
static void cmd_alarm(BaseSequentialStream *chp, int argc, char *argv[]){
static void cmd_alarm(BaseSequentialStream *chp, int argc, char *argv[]) {
int i = 0;
(void)argv;
@ -112,50 +98,101 @@ static void cmd_alarm(BaseSequentialStream *chp, int argc, char *argv[]){
goto ERROR;
}
if ((argc == 1) && (strcmp(argv[0], "get") == 0)){
if ((argc == 1) && (strcmp(argv[0], "get") == 0)) {
rtcGetAlarm(&RTCD1, 0, &alarmspec);
chprintf(chp, "%D%s",alarmspec," - alarm in STM internal format\r\n");
i = (alarmspec.alrmr & 0b1111) + ((alarmspec.alrmr >> 4) & 0b111) * 10;
chprintf(chp, "%U%s", i," - alarm in seconds\r\n");
return;
}
if ((argc == 2) && (strcmp(argv[0], "set") == 0)){
if ((argc == 2) && (strcmp(argv[0], "set") == 0)) {
i = atol(argv[1]);
alarmspec.tv_datetime = ((i / 10) & 7 << 4) | (i % 10) | RTC_ALRMAR_MSK4 |
RTC_ALRMAR_MSK3 | RTC_ALRMAR_MSK2;
if (i > 59)
goto ERROR;
/* first disable all alrams if any */
rtcSetAlarm(&RTCD1, 0, NULL);
rtcSetAlarm(&RTCD1, 1, NULL);
/* now set alarm only A */
alarmspec.alrmr = ((i / 10) << 4) | (i % 10) |
RTC_ALRMAR_MSK4 | RTC_ALRMAR_MSK3 | RTC_ALRMAR_MSK2;
rtcSetAlarm(&RTCD1, 0, &alarmspec);
return;
}
else{
else {
goto ERROR;
}
ERROR:
chprintf(chp, "Usage: alarm get\r\n");
chprintf(chp, " alarm set N\r\n");
chprintf(chp, "where N is alarm time in seconds\r\n");
chprintf(chp, "where N is alarm second on every minute\r\n");
chprintf(chp, "To test alarm functionality perform following steps:\r\n");
chprintf(chp, "1) set alarm second using this command\r\n");
chprintf(chp, "2) execute 'sleep' command\r\n");
chprintf(chp, "3) wait until the red led starts blinking\r\n");
chprintf(chp, "4) immediately execute 'date get' command\r\n");
chprintf(chp, "5) check seconds's field in returned date.\r\n");
chprintf(chp, " It must be close to programmed alarm second\r\n");
}
/*
*
* helper function
*/
static void cmd_date(BaseSequentialStream *chp, int argc, char *argv[]){
static time_t GetTimeUnixSec(void) {
struct tm tim;
rtcGetTime(&RTCD1, &timespec);
rtcConvertDateTimeToStructTm(&timespec, &tim);
return mktime(&tim);
}
/*
* helper function
*/
static void GetTimeTm(struct tm *timp) {
rtcGetTime(&RTCD1, &timespec);
rtcConvertDateTimeToStructTm(&timespec, timp);
}
/*
* helper function
*/
static void SetTimeUnixSec(time_t unix_time) {
struct tm tim;
struct tm *canary;
/* If the conversion is successful the function returns a pointer
to the object the result was written into.*/
canary = localtime_r(&unix_time, &tim);
osalDbgCheck(&tim == canary);
rtcConvertStructTmToDateTime(&tim, 0, &timespec);
rtcSetTime(&RTCD1, &timespec);
}
/*
* Console applet for date set and get
*/
static void cmd_date(BaseSequentialStream *chp, int argc, char *argv[]) {
(void)argv;
struct tm timp;
struct tm timp = {0};
if (argc == 0) {
goto ERROR;
}
if ((argc == 1) && (strcmp(argv[0], "get") == 0)){
unix_time = rtcGetTimeUnixSec(&RTCD1);
unix_time = GetTimeUnixSec();
if (unix_time == -1){
chprintf(chp, "incorrect time in RTC cell\r\n");
}
else{
chprintf(chp, "%D%s",unix_time," - unix time\r\n");
rtcGetTimeTm(&RTCD1, &timp);
chprintf(chp, "%s%s",asctime(&timp)," - formatted time string\r\n");
chprintf(chp, "%D%s", unix_time, "\r\n");
GetTimeTm(&timp);
chprintf(chp, "%s", asctime(&timp));
}
return;
}
@ -163,7 +200,7 @@ static void cmd_date(BaseSequentialStream *chp, int argc, char *argv[]){
if ((argc == 2) && (strcmp(argv[0], "set") == 0)){
unix_time = atol(argv[1]);
if (unix_time > 0){
rtcSetTimeUnixSec(&RTCD1, unix_time);
SetTimeUnixSec(unix_time);
return;
}
else{
@ -183,6 +220,9 @@ ERROR:
return;
}
/*
*
*/
static SerialConfig ser_cfg = {
115200,
0,
@ -190,6 +230,9 @@ static SerialConfig ser_cfg = {
0,
};
/*
*
*/
static const ShellCommand commands[] = {
{"alarm", cmd_alarm},
{"date", cmd_date},
@ -197,11 +240,18 @@ static const ShellCommand commands[] = {
{NULL, NULL}
};
/*
*
*/
static const ShellConfig shell_cfg1 = {
(BaseSequentialStream *)&SD2,
commands
};
#endif
/*
* working area for shell thread
*/
static THD_WORKING_AREA(waShell, 1024);
/**
* Main function.
@ -210,34 +260,32 @@ int main(void){
halInit();
chSysInit();
// chThdCreateStatic(blinkWA, sizeof(blinkWA), NORMALPRIO, blink_thd, NULL);
chThdCreateStatic(blinkWA, sizeof(blinkWA), NORMALPRIO, blink_thd, NULL);
#if WAKEUP_TEST
/* set wakeup */
wakeupspec.wutr = ((uint32_t)4) << 16; /* select 1 Hz clock source */
wakeupspec.wutr |= 9; /* set counter value to 9. Period will be 9+1 seconds. */
rtcSTM32SetPeriodicWakeup(&RTCD1, &wakeupspec);
osalThreadSleepSeconds(3);
anabiosis();
#else
#if 0
#if !WAKEUP_TEST
/* switch off wakeup */
rtcSetPeriodicWakeup_v2(&RTCD1, NULL);
rtcSTM32SetPeriodicWakeup(&RTCD1, NULL);
/* Shell initialization.*/
sdStart(&SD2, &ser_cfg);
shellInit();
static WORKING_AREA(waShell, 1024);
shellCreateStatic(&shell_cfg1, waShell, sizeof(waShell), NORMALPRIO);
/* wait until user do not want to test wakeup */
while (TRUE){
chThdSleepMilliseconds(200);
osalThreadSleepMilliseconds(200);
}
#else
/* set wakeup */
wakeupspec.wakeup = ((uint32_t)4) << 16; /* select 1 Hz clock source */
wakeupspec.wakeup |= 9; /* set counter value to 9. Period will be 9+1 seconds. */
rtcSetPeriodicWakeup_v2(&RTCD1, &wakeupspec);
chThdSleepSeconds(3);
func_sleep();
#endif /* !WAKEUP_TEST */
#endif
#endif /* WAKEUP_TEST */
return 0;
}

View File

@ -28,6 +28,8 @@
* 0...3 Lowest...Highest.
*/
#define STM32_USE_REVISION_A_FIX
#define STM32F4xx_MCUCONF
/*