Programmable Clock with RTOS 0.0.1
Implementaiton of a programmable clock using RTOS with STM32GO microcontroller.
Loading...
Searching...
No Matches
app_serial.c File Reference

Message processing implementation More...

#include "app_serial.h"
#include "bsp.h"

Data Structures

struct  SerialStates
 Struct with pointer to function. More...
 

Functions

static void CanTp_SingleFrameTx (uint8_t *data, uint8_t size)
 Transmition in CAN-TP single frame format
 
static uint8_t CanTp_SingleFrameRx (uint8_t *data, uint8_t *size)
 CAN-TP single frame format
 
static uint8_t Validate_Time (uint8_t hour, uint8_t minutes, uint8_t seconds)
 Validate time
 
static uint8_t Validate_Alarm (uint8_t hour, uint8_t minutes)
 Validate alarm
 
static uint8_t Validate_Date (uint8_t days, uint8_t month, uint16_t year)
 Validate date
 
static uint8_t WeekDay (uint8_t days, uint8_t month, uint16_t year)
 Validate the day of the week
 
static MACHINE_SERIAL Serial_StMachine (NEW_MsgTypeDef *pdata)
 Main state machine
 
static MACHINE_SERIAL SerialTimeState (NEW_MsgTypeDef *pmsg)
 Serial time function
 
static MACHINE_SERIAL SerialDateState (NEW_MsgTypeDef *pmsg)
 Serial date function
 
static MACHINE_SERIAL SerialAlarmState (NEW_MsgTypeDef *pmsg)
 Serial alarm function
 
static MACHINE_SERIAL SerialOkState (NEW_MsgTypeDef *pmsg)
 Serial ok function
 
static MACHINE_SERIAL SerialErrorState (NEW_MsgTypeDef *pmsg)
 Serial error function
 
static uint8_t BCD_conver (uint8_t data)
 BCD converter Function to unpacked from CAN-TP single frame format to BCD.
 
void Serial_Init (void)
 Initialize of CAN port
 
void HAL_FDCAN_RxFifo0Callback (FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs)
 Reception of CAN messages
 
void Serial_Task (void)
 Call of the serial state machine
 

Variables

FDCAN_HandleTypeDef CANHandler
 Structure type variable for CAN handler.
 
static FDCAN_TxHeaderTypeDef CANTxHeader
 Structure type variable for CAN transmissin initialization.
 

Detailed Description

Message processing implementation

Contains the functions to initialize the CAN port with the apropiate parameters, the callback to the CAN interruption, also the main state machine to message processing were the CAN frame will be evaluate to aprove it or not and to be assigned to the struct variable by the fuction in charge of it.

Function Documentation

◆ BCD_conver()

static uint8_t BCD_conver ( uint8_t data)
static

BCD converter Function to unpacked from CAN-TP single frame format to BCD.

< Macro defined for testing purposes.

Parameters
data[in] value to convert
Return values
Returnsan unsigned integer

< Macro defined for testing purposes.

◆ CanTp_SingleFrameRx()

static uint8_t CanTp_SingleFrameRx ( uint8_t * data,
uint8_t * size )
static

CAN-TP single frame format

< Macro defined for testing purposes.

The function validate if the message received complies with CAN-TP single frame format.

Parameters
data[in] Pointer to data
size[in] Size of data
Return values
Thefunction returns 1 when a certain number of bytes were received, otherwise, no message was received

< Macro defined for testing purposes.

◆ CanTp_SingleFrameTx()

static void CanTp_SingleFrameTx ( uint8_t * data,
uint8_t size )
static

Transmition in CAN-TP single frame format

< Macro defined for testing purposes.

This function transmit the ok or error messages through the terminal when the data is already procesed, will be send 0x55 if the data was in range and the 0xAA if the data was not in range with CAN-ID of 0x122

Parameters
data[in] Pointer to data
size[in] Size of data

< Macro defined for testing purposes.

◆ HAL_FDCAN_RxFifo0Callback()

void HAL_FDCAN_RxFifo0Callback ( FDCAN_HandleTypeDef * hfdcan,
uint32_t RxFifo0ITs )

Reception of CAN messages

Callback to the CAN interrup, it is activated when a message is recieve then is store in the variable NewMessage

Parameters
hfdcan[in] To handle CAN
RxFifo0ITs[in] void Valid_Time( uint8_t *data );

< Number 0

< Single frame element recieved

< Single frame element recieved

◆ Serial_Init()

void Serial_Init ( void )

Initialize of CAN port

Is the function to initialize all the required to start working with the CAN port and the messages reception processing

Note
Declaration of the options to configure the FDCAN1 module to transmit to the CAN bus at 100Kbps and sample point of 75%, the frequency with which the CAN module is powered is fCAN = fHSI / CANHandler.Init.ClockDivider / CANHandler.Init.NominalPrescaler fCAN = 32MHz / 1 / 8 = 4MHz The number of time quantas required is Ntq = fCAN / CANbaudrate Ntq = 4MHz / 250Kbps = 16 The sample point percentage is Sp = ( CANHandler.Init.NominalTimeSeg1 + 1 / Ntq ) * 100 Sp = ( ( 11 + 1 ) / 16 ) * 100 = 75%

< Value for ExtFiltersNbr

< Value for StdFiltersNbr

< Value for NominalPrescaler

< Value for NominalSyncJumpWidth

< Value for NominalTimeSeg1

< Value for NominalTimeSeg2

< Value for Identifier

< Value for FilterIndex

< Value for FilterID1

< Value for FilterID2

< Value for BufferIndexes

◆ Serial_StMachine()

MACHINE_SERIAL Serial_StMachine ( NEW_MsgTypeDef * pdata)
static

Main state machine

< Macro defined for testing purposes.

Is going to implement the state machine in charge of calling the function for the new message, after the interruption of CAN is trigger, depending if it is a message of time, date and alarm.

Parameters
pdata[in] Pointer to data struct
Return values
Returnsthe next state that will be executed

< Number 5

< Number 0

< Number 0

< Number 0

< Number 1

◆ Serial_Task()

void Serial_Task ( void )

Call of the serial state machine

This function calls the serial state machine every 10ms with the help of queues, therefore it won't be execute all the time

< Single frame element recieved

< True in a comparison

◆ SerialAlarmState()

static MACHINE_SERIAL SerialAlarmState ( NEW_MsgTypeDef * pmsg)
static

Serial alarm function

< Macro defined for testing purposes.

Is going to evaluate if the data for alarm is valid and send the message to the queue.

Parameters
pmsg[in] Pointer to data struct
Return values
Returnsthe next state that will be executed

< Macro defined for testing purposes.

< Message location

< Alarm hour element recieved

< Alarm min element recieved

< True in a comparison

< The maximum amount of time

< Message location

< The maximum amount of time

◆ SerialDateState()

static MACHINE_SERIAL SerialDateState ( NEW_MsgTypeDef * pmsg)
static

Serial date function

< Macro defined for testing purposes.

Is going to evaluate if the data for date is valid and send the message to the queue.

Parameters
pmsg[in] Pointer to data struct
Return values
Returnsthe next state that will be executed

< Macro defined for testing purposes.

< Message location

< Date day element recieved

< Date month element recieved

< Date MSB year element recieved

< Number 100

< Date MSB year element recieved

< True in a comparison

< The maximum amount of time

< Message location

< The maximum amount of time

◆ SerialErrorState()

static MACHINE_SERIAL SerialErrorState ( NEW_MsgTypeDef * pmsg)
static

Serial error function

< Macro defined for testing purposes.

Is going to transmit a message through CAN to indicate the data is not valid.

Parameters
pmsg[in] Pointer to data struct
Return values
Returnsthe next state that will be executed

< Macro defined for testing purposes.

< Number 0

< Message for error in data in hexa

< Single frame element recieved

◆ SerialOkState()

static MACHINE_SERIAL SerialOkState ( NEW_MsgTypeDef * pmsg)
static

Serial ok function

< Macro defined for testing purposes.

Is going to assign the data to the variable clockMsg and send it to the queue of the clock, then it will transmit a message through CAN to indicate the data is valid.

Parameters
pmsg[in] Pointer to data struct
Return values
Returnsthe next state that will be executed

< Macro defined for testing purposes.

< Number 0

< Message for ok data in hexa

< Single frame element recieved

◆ SerialTimeState()

static MACHINE_SERIAL SerialTimeState ( NEW_MsgTypeDef * pmsg)
static

Serial time function

< Macro defined for testing purposes.

Is going to evaluate if the data for time is valid and send the message to the queue.

Parameters
pmsg[in] Pointer to data struct
Return values
Returnsthe next state that will be executed

< Macro defined for testing purposes.

< Message location

< Time hour element recieved

< Time min element recieved

< Time sec element recieved

< True in a comparison

< The maximum amount of time

< Message location

< The maximum amount of time

◆ Validate_Alarm()

static uint8_t Validate_Alarm ( uint8_t hour,
uint8_t minutes )
static

Validate alarm

< Macro defined for testing purposes.

This function validates if the Alarm message is in range, from 0 to 23 for hours and from 0 to 59 to minutes, no seconds nedded, and then returns the result.

Parameters
hour[in] Hour value
minutes[in] Minutes value
Return values
Thefunction returns 1 if the alarm is correct and 0 if not

< Macro defined for testing purposes.

< Number 0

< Max hour 23 in hexa

< Max seconds 59 in hexa

< Number 1

◆ Validate_Date()

static uint8_t Validate_Date ( uint8_t days,
uint8_t month,
uint16_t year )
static

Validate date

< Macro defined for testing purposes.

This function validates if the date message is in range, from 1 to 31 for days, from 1 to 12 for months and from 1901 to 2099 for years and returns the result. In addition it also vaidates if the date is in a leap-year and adjust the month of February to manage 29 days.

Parameters
days[in] Day value
month[in] Month value
year[in] Year value
Return values
Thefunction returns 1 if the date is correct and 0 if not

< Macro defined for testing purposes.

< Min day, 1 in hexa

< Max days 31 in hexa

< January in hexa

< December in hexa

< Min year, 1901 in hexa

< Max year, 2099 in hexa

< Number 4

< Number 0

< February in hexa

< Days in leap year in february, 29 in hexa

< Number 1

< Number 0

< February in hexa

< Days in february, 28 in hexa

< Number 1

< April in hexa

< June in hexa

< September in hexa

< November in hexa

< Max days 30 in hexa

< Number 1

< January in hexa

< March in hexa

< May in hexa

< July in hexa

< August in hexa

< October in hexa

< December in hexa

< Number 1

< Number 0

< Number 0

◆ Validate_Time()

static uint8_t Validate_Time ( uint8_t hour,
uint8_t minutes,
uint8_t seconds )
static

Validate time

< Macro defined for testing purposes.

This function validates if the time message is in range, from 0 to 23 for hours, from 0 to 59 for minutes and from 0 to 59 for seconds and returns the result.

Parameters
hour[in] Hour value
minutes[in] Minutes value
seconds[in] Seconds value
Return values
Thefunction returns 1 if time is correct and 0 if not

< Macro defined for testing purposes.

< Number 0

< Max hour 23 in hexa

< Max minutes 59 in hexa

< Max seconds 59 in hexa

< Number 1

◆ WeekDay()

static uint8_t WeekDay ( uint8_t days,
uint8_t month,
uint16_t year )
static

Validate the day of the week

< Macro defined for testing purposes.

This function calculates the day of the week according to the date, to do this, we will use Zeller's congruence. Taking the day, month and year to calculate the year of the century and the century. In this algorithm January and February are counted as months 13 and 14 of the previous year. With this data we use the formula for the Gregorian calendar.

Parameters
days[in] Day value
month[in] Month value
year[in] Year value
Return values
Thefunction returns the values 0-Saturday, 1-Sunday, 2-Monday, 3-Tuesday, 4-Wednesday, 5-Thursday, 6-Friday

< Macro defined for testing purposes.