ADD info, time

This commit is contained in:
Thomas Kuschel 2022-07-16 00:30:23 +02:00
parent cf43818890
commit c3d3f229a6
11 changed files with 258 additions and 65 deletions

View File

@ -68,7 +68,7 @@
#define configTICK_RATE_HZ ((TickType_t)1000)
#define configMAX_PRIORITIES ( 56 )
#define configMINIMAL_STACK_SIZE ((uint16_t)128)
#define configTOTAL_HEAP_SIZE ((size_t)30000)
#define configTOTAL_HEAP_SIZE ((size_t)50000)
#define configMAX_TASK_NAME_LEN ( 16 )
#define configUSE_TRACE_FACILITY 1
#define configUSE_16_BIT_TICKS 0
@ -76,6 +76,7 @@
#define configQUEUE_REGISTRY_SIZE 8
#define configUSE_RECURSIVE_MUTEXES 1
#define configUSE_COUNTING_SEMAPHORES 1
#define configENABLE_BACKWARD_COMPATIBILITY 0
#define configUSE_PORT_OPTIMISED_TASK_SELECTION 0
/* USER CODE BEGIN MESSAGE_BUFFER_LENGTH_TYPE */
/* Defaults to size_t for backward compatibility, but can be changed

View File

@ -7,8 +7,8 @@
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef AT1_DEFINES_H
#define AT1_DEFINES_H
#ifndef _AT1_DEFINES_H_
#define _AT1_DEFINES_H_
#ifdef __cplusplus
extern "C" {
@ -26,14 +26,33 @@ extern "C" {
#define STR(x) STR_HELPER(x)
#endif
#define VERSION_MAJOR 0
#define VERSION_MINOR 2
#define VERSION_MINOR 3
#define PROGRAM_ID "f0x.at1 Version " STR(VERSION_MAJOR) "." STR(VERSION_MINOR)
#define VERSION_STRING STR(VERSION_MAJOR) "." STR(VERION_MINOR)
//#define DATE __DATE__
/*#define DATE __DATE__ */
#define AUTHOR_STRING "Tom Kuschel KW4NZ"
// From SI5351 datasheet:
/* From SI5351 datasheet: */
#define SI5351_I2C_ADDR (0x60 << 1)
/* #define COMPILE_YEAR (__DATE__ + 7) */
#define DATE_COMPILE_CENTURY (20)
#define DATE_COMPILE_YEAR (22)
#if !defined _SYS_ERRNO_H_ && !defined __ERRNO_H__ && !defined __AT1_ERROR_NUMBERS__
#define __AT1_ERROR_NUMBERS__
typedef enum {
EPERM = 1, /*!< Operation not permitted */
EIO = 5, /*!< I/O error */
ENOMEM = 12, /*!< Out of memory */
EFAULT = 14, /*!< Bad address */
EBUSY = 16, /*!< Device or resource busy */
ENODEV = 19, /*!< No such device */
EINVAL = 22, /*!< Invalid argument */
EADDRINUSE = 98,/*!< Address already in use */
ETIMEDOUT = 116,/*!< Connection timed out */
} at1_errno_t;
#endif
/* Exported variables --------------------------------------------------------*/
/* Exported macros -----------------------------------------------------------*/
@ -44,4 +63,4 @@ extern "C" {
} /* extern "C" */
#endif
#endif /* AT1_DEFINES_H */
#endif /* _AT1_DEFINES_H_ */

View File

@ -22,6 +22,7 @@ extern RTC_HandleTypeDef hrtc;
/* function prototypes */
int do_help(char *args);
int do_devid(char *args);
int do_info(char *args);
int do_test(/*command_inst_t inst,*/ char *args);
int do_time(char *args);

View File

@ -8,8 +8,21 @@
#ifndef INC_HELPER_H_
#define INC_HELPER_H_
struct _tm_ { /* compare to ctime(3) */
uint16_t year;
uint16_t mon;
uint16_t day;
uint16_t hour;
uint16_t min;
uint16_t sec;
};
char *ltrim(char *s);
char *rtrim(char *s);
char *trim(char *s);
int strtotime (char *s, struct _tm_ *tp);
// uint8_t dayofweek(uint8_t century, uint8_t y, uint8_t m, uint8_t d);
#endif /* INC_HELPER_H_ */

View File

@ -53,6 +53,7 @@ extern "C" {
void Error_Handler(void);
/* USER CODE BEGIN EFP */
void print_system_info(void);
/* USER CODE END EFP */

View File

@ -10,14 +10,14 @@
#define RING_STATISTICS_ENABLED 1
#define RINGBUF_MAX_READ_LEN 20
#define RINGBUF_MAX_READ_LEN 80
typedef enum {
RINGBUF_PARAM_NONE = 0x00,
RINGBUF_ALLOWOVERWRITE= 0x01,
} ringbuf_param_t;
#if !defined _SYS_ERRNO_H_ && !defined __ERRNO_H__ && !defined _STM32_SI5351_H_
#if !defined _SYS_ERRNO_H_ && !defined __ERRNO_H__ && !defined __AT1_ERROR_NUMBERS__
typedef enum {
EPERM = 1, /*!< Operation not permitted */
EIO = 5, /*!< I/O error */

View File

@ -92,9 +92,9 @@ extern "C" {
typedef struct __SI5351_HandleTypeDef *si5351_inst_t;
/* Exported constants --------------------------------------------------------*/
/** @enum errno_t Error Number Constants, @TODO could also errno.h included!!
/** @enum errno_t Error Number Constants, @TODO you can also include <errno.h>
*/
#if !defined _SYS_ERRNO_H_ && !defined __ERRNO_H__
#if !defined _SYS_ERRNO_H_ && !defined __ERRNO_H__ && !defined __AT1_ERROR_NUMBERS__
typedef enum {
EPERM = 1, /*!< Operation not permitted */
EIO = 5, /*!< I/O error */

View File

@ -8,6 +8,7 @@
*
******************************************************************************/
#include "main.h"
#include "at1_defines.h"
#include "cmsis_os.h"
#include <stdint.h>
#include <ctype.h>
@ -45,6 +46,7 @@ typedef struct {
int (*func)(/*command_ctx_t *ctx,*/ char *args);
} cmd_table_t;
// the entries must be in alphabetical order due to the binary search
static const cmd_table_t cmd_table[] = {
{"admin", 2, CMD_ADMIN, do_test },
{"altitude", 1, CMD_NO_FLAG, do_test },
@ -52,6 +54,7 @@ static const cmd_table_t cmd_table[] = {
{"devid", 3, CMD_HIDDEN, do_devid },
{"help", 1, CMD_NO_FLAG, do_help },
{"hidden", 2, CMD_HIDDEN, do_test },
{"info", 1, CMD_NO_FLAG, do_info },
{"secret", 1, CMD_SECRET, do_test },
{"time", 1, CMD_NO_FLAG, do_time },
};
@ -114,11 +117,11 @@ int cmd_interpreter(char * cmdline) {
}
entry = bsearch(cmdline, cmd_table, sizeof(cmd_table)/sizeof(cmd_table[0]), sizeof(cmd_table[0]), compar);
if (entry == NULL) {
printf("command \"%s\" not found.\n", cmdline);
printf("Command \"%s\" not found.\n", cmdline);
} else {
printf("command: \"%s\" ...\n", entry->name );
printf("Command \"%s\":\n", entry->name);
if (*ptr != '\0')
printf("with parameter: %s\n", ptr );
printf("called with parameter: \"%s\"\n", ptr);
rv = entry->func(ptr);
}
return rv;
@ -145,21 +148,54 @@ int do_devid(char *args) {
return 0;
}
int do_info(char *args) {
(void)args;
print_system_info();
return 0;
}
int do_time(char *args) {
HAL_StatusTypeDef status;
RTC_TimeTypeDef time;
RTC_DateTypeDef date;
RTC_TimeTypeDef stime = {0};
RTC_DateTypeDef sdate = {0};
int rv = 0;
if (!args) {
status = HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN);
if (status != HAL_OK)
puts("HAL_RTC_GetTime problem...");
if (args) {
status = HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN);
printf("%02d%02d-%02d-%02d %02d:%02d:%02d\n", (date.Year < 22) ? 21 : 20, date.Year, date.Month, date.Date, time.Hours, time.Minutes, time.Seconds);
struct _tm_ tp;
rv = strtotime (args, &tp);
if (rv > 0) {
printf("%02d%02d-%02d-%02d %02d:%02d:%02d\n", (tp.year < DATE_COMPILE_YEAR) ? DATE_COMPILE_CENTURY + 1 : DATE_COMPILE_CENTURY, \
tp.year, tp.mon, tp.day, tp.hour, tp.min, tp.sec);
/** Initialize RTC and set the Time and Date */
stime.Hours = (uint8_t)tp.hour;
stime.Minutes = (uint8_t)tp.min;
stime.Seconds = (uint8_t)tp.sec;
stime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
stime.StoreOperation = RTC_STOREOPERATION_RESET;
if (HAL_RTC_SetTime(&hrtc, &stime, RTC_FORMAT_BIN) != HAL_OK) {
Error_Handler();
}
sdate.WeekDay = RTC_WEEKDAY_MONDAY;
sdate.Month = (uint8_t)tp.mon;
sdate.Date = (uint8_t)tp.day;
sdate.Year = (uint8_t)tp.year;
//uint8_t weekday;
//weekday = dayofweek((tp.year < DATE_COMPILE_YEAR) ? DATE_COMPILE_CENTURY + 1 : DATE_COMPILE_CENTURY, sdate.Year, sdate.Month, sdate.Date);
//printf("Wochentag: %d\n", weekday);
if (HAL_RTC_SetDate(&hrtc, &sdate, RTC_FORMAT_BIN) != HAL_OK) {
Error_Handler();
}
}
}
return 0;
status = HAL_RTC_GetTime(&hrtc, &stime, RTC_FORMAT_BIN);
if (status != HAL_OK)
puts("HAL_RTC_GetTime problem...");
status = HAL_RTC_GetDate(&hrtc, &sdate, RTC_FORMAT_BIN);
printf("%02d%02d-%02d-%02d %02d:%02d:%02d\n", (sdate.Year < DATE_COMPILE_YEAR) ? DATE_COMPILE_CENTURY + 1 : DATE_COMPILE_CENTURY, \
sdate.Year, sdate.Month, sdate.Date, stime.Hours, stime.Minutes, stime.Seconds);
return rv;
}
int do_help(char *args) {

View File

@ -6,6 +6,21 @@
*/
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h> /* sscanf */
#include <at1_defines.h> /* include my defines from a global view, else */
#ifndef DATE_COMPILE_CENTURY
#define DATE_COMPILE_CENTURY (20)
#define DATE_COMPILE_YEAR (22)
#endif
#if !defined _SYS_ERRNO_H_ && !defined __ERRNO_H__ && !defined __AT1_ERROR_NUMBERS__
#include <errno.h>
#endif
/* do not use the #include <time.h> library */
#include "helper.h"
char *ltrim(char *s)
{
@ -25,3 +40,111 @@ char *trim(char *s)
{
return rtrim(ltrim(s));
}
/* this is ISO 8601 2018-12-31 with separator == '-' */
int strtotime (char *s, struct _tm_ *tp) {
int rv = 0;
uint32_t tmp;
struct _tm_ tm = {0};
do {
/* Check for the day and/or year */
tmp = strtoul(s, &s, 10); /* base 10 */
/* Check of plausibility: Year must be within compilation year and max 10 centuries from now on, i.e. 3000 */
if (tmp >= (DATE_COMPILE_CENTURY * 100 + DATE_COMPILE_YEAR) && tmp < ((DATE_COMPILE_CENTURY + 10) * 100)) {
tm.year = (uint8_t)(tmp % 100UL);
} else {
rv = -EINVAL;
break;
}
rv = sscanf(s, "-%hu-%hu %hu:%hu:%hu", &tm.mon, &tm.day, &tm.hour, &tm.min, &tm.sec);
if (rv >= 3)
*tp = tm;
else
rv = -EINVAL;
} while(0);
return rv;
}
#if 0
while (*str == ' ') {
str++;
};
*args = str;
return CMD_OK;
} else {
separator = '/';
if (tmp > 0 && tmp < 32)
timeinfo.tm_mday = tmp;
else
goto error;
}
str++;
/* Read the month */
tmp = strtoul(str, &str, 10);
if (*str != separator || tmp == 0 || tmp > 12)
goto error;
timeinfo.tm_mon = tmp - 1; /* the month is interpreted from 0..11 */
str++;
/* Check either the day or the four-digit year */
tmp = strtoul(str, &str, 10);
/* there should either spaces or a T between date and time info */
if ((toupper(*str) != 'T' && *str != ' ') || tmp == 0 || tmp >= 2106)
goto error;
if (separator == '-') {
timeinfo.tm_mday = tmp;
} else if (tmp >= FW_VERSION_YEAR) {
timeinfo.tm_year = tmp - 1900;
} else {
goto error;
}
do {
str++;
} while (*str == ' ');
/* the hour have to be 0..23 */
tmp = strtoul(str, &str, 10);
if (*str != ':' || tmp > 23)
goto error;
timeinfo.tm_hour = tmp;
str++;
/* the minute have to be 0..59 */
tmp = strtoul(str, &str, 10);
if (tmp > 59)
goto error;
timeinfo.tm_min = tmp;
if (*str == ':') {
str++;
tmp = strtoul(str, &str, 10);
} else {
tmp = 0;
}
if (tmp > 59)
goto error;
timeinfo.tm_sec = tmp;
while (*str == ' ')
str++;
if (*str != '\0' && (flags & ARG_LAST))
goto error;
/* Try to create the time stamp */
{
time_t tmp_unixtime = mktime(&timeinfo);
if (tmp_unixtime == (unsigned)-1) {
goto error;
}
*args = str;
*unixtime = tmp_unixtime;
}
return CMD_OK;
error:
*args = str;
return CMD_ERROR_PARAMETERS;
}
#endif

View File

@ -112,7 +112,7 @@ void start_morse_task(void *argument);
void start_clk2_task(void *argument);
/* USER CODE BEGIN PFP */
void print_system_info(void);
void ringbuffer_callback(uint16_t delimiterfound, void * cb_data);
/* USER CODE END PFP */
@ -184,13 +184,13 @@ int main(void)
/* USER CODE BEGIN 2 */
si5351_inst_t si5351_inst = 0;
//printf("Date Compile Century: %d\n", DATE_COMPILE_CENTURY);
//printf("Date Compile Year: %d\n", DATE_COMPILE_YEAR);
print_system_info();
// do some tests for ringbuf.h library
//struct ringbuf * ring;
ring = ringbuf_create(mainbuf_size, RINGBUF_ALLOWOVERWRITE);
if (ring == NULL)
puts("Can't create memory for ringbuffer");
@ -382,12 +382,8 @@ int main(void)
/* We should never get here as control is now taken by the scheduler */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
@ -578,8 +574,8 @@ static void MX_RTC_Init(void)
/** Initialize RTC and set the Time and Date
*/
sTime.Hours = 0x12;
sTime.Minutes = 0x30;
sTime.Hours = 0x0;
sTime.Minutes = 0x0;
sTime.Seconds = 0x0;
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
@ -588,8 +584,8 @@ static void MX_RTC_Init(void)
Error_Handler();
}
sDate.WeekDay = RTC_WEEKDAY_MONDAY;
sDate.Month = RTC_MONTH_MAY;
sDate.Date = 0x23;
sDate.Month = RTC_MONTH_AUGUST;
sDate.Date = 0x1;
sDate.Year = 0x22;
if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
@ -829,15 +825,24 @@ int _write(int fd, unsigned char *p, int len)
return len;
}
//int _write_r(void *reent, int fd, char *p, size_t len)
//{
// return _write(fd, p, len);
//}
void print_system_info(void) {
puts("\n**************************************");
puts("*************************************");
puts(PROGRAM_ID " " AUTHOR_STRING);
puts("Compiled: " __DATE__ " " __TIME__);
puts(osKernelSystemId);
printf("CMSIS V%u.%u\n", __CM_CMSIS_VERSION_MAIN, __CM_CMSIS_VERSION_SUB);
printf("STM32L4 CMSIS Device V%u.%u.%u.%u\n", __STM32L4_CMSIS_VERSION_MAIN,
__STM32L4_CMSIS_VERSION_SUB1,__STM32L4_CMSIS_VERSION_SUB2,__STM32L4_CMSIS_VERSION_RC);
printf("STM32L4xx HAL Driver V%u.%u.%u.%u\n", STM32L4XX_HAL_VERSION_MAIN,
STM32L4XX_HAL_VERSION_SUB1, STM32L4XX_HAL_VERSION_SUB2, STM32L4XX_HAL_VERSION_RC);
printf("Si5351 STM32-driver V%u.%u.%u\n", __SI5351__, __SI5351_MINOR__, __SI5351_PATCHLEVEL__);
printf("GNU Compiler Collection V%u.%u.%u\n", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
puts("Byte Order: little-endian");
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
puts("Byte Order: big-endian");
#endif
puts("Newlib V" _NEWLIB_VERSION);
}
void make_di_dah(si5351_inst_t inst, unsigned int dah, uint32_t delay, uint8_t clk) {
@ -912,7 +917,6 @@ void morse(si5351_inst_t inst, char * s, uint32_t delay, uint8_t clk) {
break;
case '$': ch = 0x04; /* error 0b00000 100 */
break;
case ' ': /* make a word break */
osDelay(7*delay); /* fall through */
default:
@ -980,11 +984,11 @@ void StartDefaultTask(void *argument)
/* Infinite loop */
for(;;) {
HAL_GPIO_TogglePin(LD1_GPIO_Port, LD1_Pin);
//HAL_GPIO_WritePin(LD1_GPIO_Port, LD1_Pin, GPIO_PIN_SET);
//HAL_GPIO_TogglePin(LD1_GPIO_Port, LD1_Pin);
HAL_GPIO_WritePin(LD1_GPIO_Port, LD1_Pin, GPIO_PIN_SET);
osDelay(100);
HAL_GPIO_TogglePin(LD1_GPIO_Port, LD1_Pin);
//HAL_GPIO_WritePin(LD1_GPIO_Port, LD1_Pin, GPIO_PIN_RESET);
//HAL_GPIO_TogglePin(LD1_GPIO_Port, LD1_Pin);
HAL_GPIO_WritePin(LD1_GPIO_Port, LD1_Pin, GPIO_PIN_RESET);
sw = HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin);
if (sw_last == GPIO_PIN_RESET && sw == GPIO_PIN_SET) {
leds_on = !leds_on;
@ -1019,8 +1023,7 @@ void start_terminal_task(void *argument)
/* USER CODE BEGIN start_terminal_task */
(void)argument;
char data[128];
char data[RINGBUF_MAX_READ_LEN + 1];
char *tdata = data;
int bytes;
int rv;
@ -1030,28 +1033,22 @@ void start_terminal_task(void *argument)
//HAL_UART_Receive_IT(&hlpuart1, UART1_rxBuffer, 1);
// osSemaphoreRelease is in the callback function when a command is entered
osSemaphoreAcquire(commandHandle, osWaitForever);
// status = HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN);
// if (status != HAL_OK)
// puts("HAL_RTC_GetTime problem...");
// status = HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN);
// printf("%02d%02d-%02d-%02d %02d:%02d:%02d\n", (date.Year < 22) ? 21 : 20, date.Year, date.Month, date.Date, time.Hours, time.Minutes, time.Seconds);
//ringbuf_dump(ring);
do {
bytes = ringbuf_read(ring, data);
printf("READ (%d): %s\n",bytes, data);
// printf("READ (%d): %s\n",bytes, data);
if (bytes) {
ringbuf_dump(ring);
//ringbuf_dump(ring);
tdata = trim(data);
printf("TRIMMED (%d): %s\n",strlen(tdata), tdata);
// printf("TRIMMED (%d): %s\n",strlen(tdata), tdata);
rv = cmd_interpreter(tdata);
if (rv) {
if (rv < 0) {
printf("ERROR: %d\n", -rv);
}
}
if (ringbuf_is_empty(ring))
break;
} while (bytes);
//osDelay(9999);
}
/* USER CODE END start_terminal_task */
}

View File

@ -12,10 +12,12 @@ Dma.Request0=LPUART_RX
Dma.RequestsNb=1
FREERTOS.BinarySemaphores01=si5351,Dynamic,NULL;command,Dynamic,NULL
FREERTOS.FootprintOK=true
FREERTOS.IPParameters=Tasks01,configUSE_NEWLIB_REENTRANT,FootprintOK,configTOTAL_HEAP_SIZE,BinarySemaphores01
FREERTOS.IPParameters=Tasks01,configUSE_NEWLIB_REENTRANT,FootprintOK,configTOTAL_HEAP_SIZE,BinarySemaphores01,configENABLE_BACKWARD_COMPATIBILITY,configUSE_POSIX_ERRNO
FREERTOS.Tasks01=defaultTask,24,128,StartDefaultTask,Default,NULL,Dynamic,NULL,NULL;terminalTask,24,256,start_terminal_task,Default,NULL,Dynamic,NULL,NULL;morseTask,24,128,start_morse_task,Default,si5351_inst,Dynamic,NULL,NULL;clk2Task,40,128,start_clk2_task,Default,si5351_inst,Dynamic,NULL,NULL
FREERTOS.configTOTAL_HEAP_SIZE=30000
FREERTOS.configENABLE_BACKWARD_COMPATIBILITY=0
FREERTOS.configTOTAL_HEAP_SIZE=50000
FREERTOS.configUSE_NEWLIB_REENTRANT=1
FREERTOS.configUSE_POSIX_ERRNO=0
File.Version=6
GPIO.groupedBy=Group By Peripherals
I2C1.I2C_Fall_Time=10
@ -493,11 +495,11 @@ RCC.VCOInputFreq_Value=4000000
RCC.VCOOutputFreq_Value=284000000
RCC.VCOSAI1OutputFreq_Value=96000000
RCC.VCOSAI2OutputFreq_Value=32000000
RTC.Date=23
RTC.Hours=12
RTC.Date=1
RTC.Hours=0
RTC.IPParameters=Hours,Minutes,Month,Date,Year,StoreOperation
RTC.Minutes=30
RTC.Month=RTC_MONTH_MAY
RTC.Minutes=0
RTC.Month=RTC_MONTH_AUGUST
RTC.StoreOperation=RTC_STOREOPERATION_RESET
RTC.Year=22
VP_FREERTOS_VS_CMSIS_V2.Mode=CMSIS_V2