From 5cfbacc060ba924346327a29f78f12fd388d9910 Mon Sep 17 00:00:00 2001 From: key Date: Sun, 6 Mar 2022 15:50:30 +0100 Subject: [PATCH] Timer.c Check for prescaler overflow and wrote some documentation with new todo s --- README.md | 5 ++-- bsl/csl/interfaces/timer.h | 23 +++++++++++--- .../stm32f042/Device/hardwareDescription.h | 30 +++++++++++++++---- bsl/csl/stm32f042/Src/timer.c | 12 +++++--- main.cpp | 30 +++---------------- 5 files changed, 58 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index eab7b71..d8aed46 100644 --- a/README.md +++ b/README.md @@ -20,14 +20,15 @@ INTERFACES - pin - pin.h Is now only missing interrupt functions - USART - - usart.h Is wirking Great But there are many more functionalities to be added. + - usart.h Is working Great But there are many more functionalities to be added. - usart.h Interrupt and DMA are the next steps - deviceSetup - deviceSetup.h it's a copy of what ST has made but wit direct register acsess - deviceSetup.h we need to test it with other configurations to see if it works as supposed or if the MCU is on it's defaul 8MHz congif. - - setupInit(); funtion is called from bsl/csl/stm32f042/startup/startup_stm32f042x6.s and is called before the main(); + - setupInit(); funtion is called from bsl/csl/stm32f042/startup/startup_stm32f042x6.s + and is called before the main(); PROGRAMMING diff --git a/bsl/csl/interfaces/timer.h b/bsl/csl/interfaces/timer.h index fe4dfd3..69a7e2f 100644 --- a/bsl/csl/interfaces/timer.h +++ b/bsl/csl/interfaces/timer.h @@ -10,8 +10,11 @@ * **Detailed Description :** * This the timer interface and belongs to the interface layer * -* @todo -* - 19.12.2021 : Should we check for a 32 to 16 bit overflow situation. +* @todo Following functions are to be implmented : +* timerSetHz +* timerSetMs +* timerSetNs +* timerSetPs ************************************************************************************************** */ @@ -24,9 +27,11 @@ extern "C" { #include "hardwareDescription.h" + +/*! Emun of possible Timer Errors */ typedef enum { - functionNotSupported, - prescalerOutOfRange + functionNotSupported, /*!< This Funtion is not awailable on this **HARDWARE** */ + prescalerOutOfRange /*!< Set prescaler value exeeds the Register's **HARDWARE** size */ } timerError_t; typedef enum { @@ -41,12 +46,21 @@ typedef enum { onePulse } timerMode_t; +/*! + * Reset the timer to its default state. + */ void timerReset(timerNo_t timer); void timerActivateBus(timerNo_t timer); void timerEnable(timerNo_t timer); void timerDisable(timerNo_t timer); void timerSetMode(timerNo_t timer, timerMode_t mode); void timerSetCountDirection(timerNo_t timer, timerCountDirection_t direction); + +/*! + * Sets the given timer's prescaler value. + * This fucntion will also check if the entered prascaler value exeeds the register size. + * If violated the prescalerOutOfRange Error will be generated. + */ void timerSetPrescaler(timerNo_t timer, uint32_t prescaler); void timerSetPostscaler(timerNo_t timer, uint32_t postscaler); void timerSetAutoReload(timerNo_t timer, uint32_t reload); @@ -56,6 +70,7 @@ void timerClearCounter(timerNo_t timer); /* Second stage configuration */ void timerInit(timerNo_t timer, uint32_t prescaler, uint32_t period, timerCountDirection_t direction, timerMode_t mode); + void timerSetHz(timerNo_t timer, uint16_t hz); void timerSetMs(timerNo_t timer, uint16_t ms); void timerSetNs(timerNo_t timer, uint16_t ns); diff --git a/bsl/csl/stm32f042/Device/hardwareDescription.h b/bsl/csl/stm32f042/Device/hardwareDescription.h index 7e18dd6..530a311 100644 --- a/bsl/csl/stm32f042/Device/hardwareDescription.h +++ b/bsl/csl/stm32f042/Device/hardwareDescription.h @@ -194,6 +194,9 @@ static const uint8_t timerBus_En_bitPos[MAX_TIMER_CHANNEL_COUNT] = { RCC_APB2ENR_TIM17EN_Pos }; +/*! + * RCC timer Reset Bit Position list + * */ static const uint8_t timerBus_Rst_bitPos[MAX_TIMER_CHANNEL_COUNT] = { RCC_APB2RSTR_TIM1RST_Pos, RCC_APB1RSTR_TIM2RST_Pos, @@ -203,13 +206,28 @@ static const uint8_t timerBus_Rst_bitPos[MAX_TIMER_CHANNEL_COUNT] = { RCC_APB2RSTR_TIM17RST_Pos }; +/*! + * RCC Bus number index list connected to the timer + * */ static const uint8_t timerBus_No[MAX_TIMER_CHANNEL_COUNT] = { - 2, - 1, - 1, - 1, - 2, - 2 + 2, /*!< timer 1 is connected to bus 2 */ + 1, /*!< timer 2 is connected to bus 1 */ + 1, /*!< timer 3 is connected to bus 1 */ + 1, /*!< timer 14 is connected to bus 1 */ + 2, /*!< timer 16 is connected to bus 2 */ + 2 /*!< timer 17 is connected to bus 2 */ +}; + +/*! + * Timer Prescaler resolution list TO BE DELETED IF NOT NEEDED + * */ +static const uint32_t timerRes_Prescaler[MAX_TIMER_CHANNEL_COUNT] = { + 0xFFFF, /*!< Timer 1 Prescaler Max Value */ + 0xFFFF, /*!< Timer 2 Prescaler Max Value */ + 0xFFFF, /*!< Timer 3 Prescaler Max Value */ + 0xFFFF, /*!< Timer 14 Prescaler Max Value */ + 0xFFFF, /*!< Timer 16 Prescaler Max Value */ + 0xFFFF, /*!< Timer 17 Prescaler Max Value */ }; #ifdef __cplusplus diff --git a/bsl/csl/stm32f042/Src/timer.c b/bsl/csl/stm32f042/Src/timer.c index f7a0379..b2fd084 100644 --- a/bsl/csl/stm32f042/Src/timer.c +++ b/bsl/csl/stm32f042/Src/timer.c @@ -54,12 +54,17 @@ void timerSetCountDirection(timerNo_t timer, timerCountDirection_t direction) void timerSetPrescaler(timerNo_t timer, uint32_t prescaler) { - BASE->PSC = prescaler; + if(prescaler > timerRes_Prescaler[timer]) + { + timerThrowError(prescalerOutOfRange); + return; // To prevent writing wrong value in to the Register + } + BASE->PSC = prescaler; } void timerSetPostscaler(timerNo_t timer, uint32_t postscaler) { - + timerThrowError(functionNotSupported); } void timerSetAutoReload(timerNo_t timer, uint32_t reload) @@ -138,7 +143,6 @@ void timerSetPs(timerNo_t timer, uint16_t ps) } - void timerSart(timerNo_t timer) { timerEnable(timer); @@ -163,6 +167,6 @@ uint32_t timerGetCount(timerNo_t timer) void timerThrowError(timerError_t error) { - + while(1); } diff --git a/main.cpp b/main.cpp index 22773b6..6831fb3 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,3 @@ -#warning "Generated from: " __FILE__ - - #include "main.hpp" #include "delay.h" #include "deviceSetup.h" @@ -11,14 +8,11 @@ int main(int argc, char *argv[]) { uint8_t i = 0; - uint8_t a = '0'; - - delayInitMs(8000000, 1000); - //timerSetHz(timer_2,3); + delayInitMs(8000000, 1000); // Clock Freq and Divider for ARM library - timerInit(timer_2, 7999, 999, downCounting, counter); - timerSart(timer_2); + timerInit(timer_1, 7999, 999, downCounting, counter); + timerSart(timer_1); pinConfig(pinB3, output, pushPull, def_res, def_speed); pinConfig(pinA0, input, def_stage, pullDown, def_speed); @@ -33,7 +27,7 @@ int main(int argc, char *argv[]) noFlowControl); //clears screen and send the wellcome messgae -// print_Usart(usart2, ASCII_clear); + print_Usart(usart2, ASCII_clear); print_Usart(usart2, const_cast("Wellcome to our KED project\n\r")); @@ -54,22 +48,6 @@ int main(int argc, char *argv[]) pinToggle(pinB3); } - /* - while(1) - { - //USART will rteturn what wou type and trun led on if you press "1" - a = usartGetChar(usart2); - usartSendChar(usart2, a); - - if(a == '1') { - pinWrite(pinB3,1); - } else if(a == '0'){ - pinWrite(pinB3,0); - - delayMs(100); - } - */ - return 1; }