├── 01 - LIB ├── 01 - STD_TYPES │ └── STD_TYPES.h ├── 02 - BIT_MATH │ └── BIT_MATH.h └── 03 - DELAY │ ├── DELAY.c │ └── DELAY.h ├── 02 - MCAL ├── 01 - RCC │ ├── RCC_config.h │ ├── RCC_interface.h │ ├── RCC_private.h │ └── RCC_program.c ├── 02 - GPIO │ ├── GPIO_config.h │ ├── GPIO_interface.h │ ├── GPIO_private.h │ └── GPIO_program.c ├── 03 - NVIC │ ├── NVIC_config.h │ ├── NVIC_interface.h │ ├── NVIC_private.h │ └── NVIC_program.c ├── 04 - EXTI │ ├── EXTI_config.h │ ├── EXTI_interface.h │ ├── EXTI_private.h │ └── EXTI_program.c ├── 05 - AFIO │ ├── AFIO_config.h │ ├── AFIO_interface.h │ ├── AFIO_private.h │ └── AFIO_program.c ├── 06 - STK │ ├── STK_config.h │ ├── STK_interface.h │ ├── STK_private.h │ └── STK_program.c ├── 07 - TIMER │ ├── TIMER_config.c │ ├── TIMER_config.h │ ├── TIMER_interface.h │ ├── TIMER_private.h │ └── TIMER_program.c ├── 08 - DMA │ ├── DMA_config.h │ ├── DMA_interface.h │ ├── DMA_private.h │ └── DMA_program.c ├── 09 - SPI │ ├── SPI_config.h │ ├── SPI_interface.h │ ├── SPI_private.h │ └── SPI_program.c ├── 10 - UART │ ├── UART_config.c │ ├── UART_config.h │ ├── UART_interface.h │ ├── UART_private.h │ └── UART_program.c └── 11 - FPEC │ ├── FPEC_config.h │ ├── FPEC_interface.h │ ├── FPEC_private.h │ └── FPEC_program.c ├── 03 - HAL ├── 01 - LED │ ├── LED_config.h │ ├── LED_interface.h │ ├── LED_private.h │ └── LED_program.c ├── 02 - SSD │ ├── SSD_config.h │ ├── SSD_interface.h │ ├── SSD_private.h │ └── SSD_program.c ├── 03 - BUTTON │ ├── BUTTON_config.h │ ├── BUTTON_interface.h │ ├── BUTTON_private.h │ └── BUTTON_program.c ├── 04 - LEDMRX │ ├── LEDMRX_config.c │ ├── LEDMRX_config.h │ ├── LEDMRX_interface.h │ ├── LEDMRX_private.h │ └── LEDMRX_program.c ├── 04 - LEDMRX_OS │ ├── LEDMRX_config.c │ ├── LEDMRX_config.h │ ├── LEDMRX_interface.h │ ├── LEDMRX_private.h │ └── LEDMRX_program.c ├── 05 - REMOTE │ ├── REMOTE_config.h │ ├── REMOTE_interface.h │ ├── REMOTE_private.h │ └── REMOTE_program.c ├── 06 - AUDIO │ ├── AUDIO_config.h │ ├── AUDIO_interface.h │ ├── AUDIO_private.h │ └── AUDIO_program.c ├── 07 - STP │ ├── STP_config.h │ ├── STP_interface.h │ ├── STP_private.h │ └── STP_program.c ├── 08 - TFT │ ├── TFT_config.h │ ├── TFT_interface.h │ ├── TFT_private.h │ └── TFT_program.c └── 09 - ESP │ ├── ESP_config.h │ ├── ESP_interface.h │ ├── ESP_private.h │ └── ESP_program.c └── 04 - SERVICE └── 01 - OS ├── OS_config.h ├── OS_interface.h ├── OS_private.h └── OS_programe.c /01 - LIB/01 - STD_TYPES/STD_TYPES.h: -------------------------------------------------------------------------------- 1 | /************************************************************/ 2 | /* Author : A . MOSAD */ 3 | /* Date : 4 / 8 / 2020 */ 4 | /* version : V01 */ 5 | /************************************************************/ 6 | #ifndef STD_TYPES_H 7 | #define STD_TYPES_H 8 | 9 | typedef unsigned char u8 ; 10 | typedef unsigned short int u16 ; 11 | typedef unsigned long int u32 ; 12 | typedef unsigned long long int u64 ; 13 | typedef signed char s8 ; 14 | typedef signed short int s16 ; 15 | typedef signed long int s32 ; 16 | typedef signed long long int s64 ; 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /01 - LIB/02 - BIT_MATH/BIT_MATH.h: -------------------------------------------------------------------------------- 1 | /************************************************************/ 2 | /* Author : A . MOSAD */ 3 | /* Date : 4 / 8 / 2020 */ 4 | /* version : V01 */ 5 | /************************************************************/ 6 | 7 | #ifndef BIT_MATH_H 8 | #define BIT_MATH_H 9 | 10 | #define SET_BIT(REG,BIT) (REG |= (1 << BIT)) 11 | #define CLEAR_BIT(REG,BIT) (REG &= ~(1 << BIT)) 12 | #define TOGGLE_BIT(REG,BIT) (REG ^= ( 1 << BIT)) 13 | #define CLEAR_REG(REG) (REG &= ~(255 << 0)) 14 | #define SET_REG(REG) (REG |= ~(255 << 0)) 15 | #define IS_SET(REG,BIT)(REG & (1<>BIT) & 1) 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /01 - LIB/03 - DELAY/DELAY.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 14/8/2020 */ 5 | /*****************************************************/ 6 | #include "STD_TYPES.h" 7 | #include "delay.h" 8 | 9 | void delay_ms(u32 ms) 10 | { 11 | u32 i; 12 | 13 | for(i = 0; i < (SYSTEMCORECLOCK/(6000)) * ms; i++){ 14 | __asm ("NOP"); 15 | } 16 | } -------------------------------------------------------------------------------- /01 - LIB/03 - DELAY/DELAY.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 14/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef DELAY_H 7 | #define DELAY_H 8 | 9 | 10 | 11 | #ifndef SYSTEMCORECLOCK 12 | #define SYSTEMCORECLOCK 8000000 13 | #endif 14 | 15 | // 6000 for keil and 12000 for eclipce 16 | // must define the system clock first if not the default is 8 MHZ 17 | void delay_ms(u32 ms) ; 18 | 19 | 20 | #endif -------------------------------------------------------------------------------- /02 - MCAL/01 - RCC/RCC_config.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 8/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef RCC_CONFIG_H_ 7 | #define RCC_CONFIG_H_ 8 | 9 | 10 | /* Options : 11 | RCC_HSI 12 | RCC_HSE_CRYSTAL 13 | RCC_HSE_RC 14 | RCC_PLL 15 | */ 16 | #define SYS_CLOCK RCC_HSE_CRYSTAL 17 | 18 | /* 19 | Clock security system : (ENABLE - DISABLE) 20 | */ 21 | #define CSS_ON DISABLE 22 | 23 | /* 24 | Options : -16 : 15 25 | */ 26 | #define HSI_STEP_CORRECTION 0 27 | 28 | 29 | /* 30 | Options : 31 | RCC_PLL_HSI_BY2 32 | RCC_PLL_HSE 33 | RCC_PLL_HSE_BY2 34 | */ 35 | #define RCC_PLL_SOURCE HSI_CLOCK 36 | /* 37 | Options : 2 : 16 38 | */ 39 | #define RCC_PLL_MUL 2 40 | 41 | /* 42 | Options : 43 | NO_CLOCK 44 | HSI_CLOCK 45 | HSE_CLOCK 46 | PLL_CLOCK 47 | SYSTEM_CLOCK 48 | */ 49 | #define RCC_MCO_PIN NO_CLOCK 50 | 51 | /* 52 | Options : 53 | RCC_PRESCALLER_2 54 | RCC_PRESCALLER_4 55 | RCC_PRESCALLER_6 56 | RCC_PRESCALLER_8 57 | */ 58 | #define RCC_ADC_PRESCALLER RCC_PRESCALLER_2 59 | 60 | /* 61 | Options : 62 | RCC_PRESCALLER_0 63 | RCC_PRESCALLER_2 64 | RCC_PRESCALLER_4 65 | RCC_PRESCALLER_8 66 | RCC_PRESCALLER_16 67 | */ 68 | #define RCC_APB2_PRESCALLER RCC_PRESCALLER_0 69 | 70 | /* 71 | Options : 72 | RCC_PRESCALLER_0 73 | RCC_PRESCALLER_2 74 | RCC_PRESCALLER_4 75 | RCC_PRESCALLER_8 76 | RCC_PRESCALLER_16 77 | */ 78 | /* Note : this bus clock must not exceed 36 MHZ*/ 79 | #define RCC_APB1_PRESCALLER RCC_PRESCALLER_0 80 | 81 | /* 82 | Options : 83 | RCC_PRESCALLER_0 84 | RCC_PRESCALLER_2 85 | RCC_PRESCALLER_4 86 | RCC_PRESCALLER_8 87 | RCC_PRESCALLER_16 88 | RCC_PRESCALLER_64 89 | RCC_PRESCALLER_128 90 | RCC_PRESCALLER_256 91 | RCC_PRESCALLER_512 92 | */ 93 | #define RCC_AHP_PRESCALLER RCC_PRESCALLER_0 94 | 95 | 96 | 97 | 98 | #endif 99 | -------------------------------------------------------------------------------- /02 - MCAL/01 - RCC/RCC_interface.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 8/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef RCC_INTERFACE_H_ 7 | #define RCC_INTERFACE_H_ 8 | 9 | #define RCC_AHP 0 10 | #define RCC_APB1 1 11 | #define RCC_APB2 2 12 | 13 | 14 | //periphera id bits for AHP 15 | #define DMA1_PERIPHERAL 0 16 | #define DMA2_PERIPHERAL 1 17 | #define SRAM_PERIPHERAL 2 18 | #define FLITF_PERIPHERAL 4 19 | #define CRCE_PERIPHERAL 6 20 | #define FSMC_PERIPHERAL 8 21 | #define SDIO_PERIPHERAL 10 22 | 23 | //periphera id bits for APB2 24 | #define AFIO_PERIPHERAL 0 25 | #define IOPA_PERIPHERAL 2 26 | #define IOPB_PERIPHERAL 3 27 | #define IOPC_PERIPHERAL 4 28 | #define IOPD_PERIPHERAL 5 29 | #define IOPE_PERIPHERAL 6 30 | #define IOPF_PERIPHERAL 7 31 | #define IOPG_PERIPHERAL 8 32 | #define ADC1_PERIPHERAL 9 33 | #define ADC2_PERIPHERAL 10 34 | #define TIM1_PERIPHERAL 11 35 | #define SPI1_PERIPHERAL 12 36 | #define TIM8_PERIPHERAL 13 37 | #define USART1_PERIPHERAL 14 38 | #define ADC3_PERIPHERAL 15 39 | #define TIM9_PERIPHERAL 19 40 | #define TIM10_PERIPHERAL 20 41 | #define TIM11_PERIPHERAL 21 42 | 43 | //periphera id bits for APB1 44 | #define TIM2_PERIPHERAL 0 45 | #define TIM3_PERIPHERAL 1 46 | #define TIM4_PERIPHERAL 2 47 | #define TIM5_PERIPHERAL 3 48 | #define TIM6_PERIPHERAL 4 49 | #define TIM7_PERIPHERAL 5 50 | #define TIM12_PERIPHERAL 6 51 | #define TIM13_PERIPHERAL 7 52 | #define TIM14_PERIPHERAL 8 53 | #define WWD_PERIPHERAL 11 54 | #define SPI2_PERIPHERAL 14 55 | #define SPI3_PERIPHERAL 15 56 | #define USART2_PERIPHERAL 17 57 | #define USART3_PERIPHERAL 18 58 | #define USART4_PERIPHERAL 19 59 | #define USART5_PERIPHERAL 20 60 | #define I2C1_PERIPHERAL 21 61 | #define I2C2_PERIPHERAL 22 62 | #define USB_PERIPHERAL 23 63 | #define CAN_PERIPHERAL 25 64 | #define BKP_PERIPHERAL 27 65 | #define PWR_PERIPHERAL 28 66 | #define DAC_PERIPHERAL 29 67 | 68 | 69 | 70 | 71 | /* 72 | * RCC_voidInitSysClock - > Intialize the clock for the system 73 | */ 74 | void RCC_voidInitSysClock (void); 75 | 76 | /* 77 | * RCC_voidEnableClock - > Enable a periheral clock 78 | * i/p : Bus id (AHP - APB1 - APB2) / Peripheral id 0:31 79 | */ 80 | void RCC_voidEnableClock (u8 copy_u8BudId , u8 copy_u8PerId); 81 | 82 | /* 83 | * RCC_voidDisableClock - > Disable a periheral clock 84 | * i/p : Bus id (AHP - APB1 - APB2) / Peripheral id 0:31 85 | */ 86 | void RCC_voidDisableClock (u8 copy_u8BudId , u8 copy_u8PerId); 87 | 88 | 89 | #endif 90 | -------------------------------------------------------------------------------- /02 - MCAL/01 - RCC/RCC_private.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 8/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef RCC_PRIVATE_H_ 7 | #define RCC_PRIVATE_H_ 8 | 9 | // base address : 0x4002 1000 10 | /* Register Definitions */ 11 | #define RCC_CR *((volatile u32 *) 0x40021000) 12 | #define RCC_CFGR *((volatile u32 *) 0x40021004) 13 | #define RCC_CIR *((volatile u32 *) 0x40021008) 14 | #define RCC_APB2RSTR *((volatile u32 *) 0x4002100C) 15 | #define RCC_APB1RSTR *((volatile u32 *) 0x40021010) 16 | #define RCC_AHBENR *((volatile u32 *) 0x40021014) 17 | #define RCC_APB2ENR *((volatile u32 *) 0x40021018) 18 | #define RCC_APB1ENR *((volatile u32 *) 0x4002101C) 19 | #define RCC_BDCR *((volatile u32 *) 0x40021020) 20 | #define RCC_CSR *((volatile u32 *) 0x40021024) 21 | 22 | 23 | /* Configuratuin parameters */ 24 | #define ENABLE 1 25 | #define DISABLE 0 26 | 27 | /* Clock system options */ 28 | #define RCC_HSI 0 29 | #define RCC_HSE_CRYSTAL 1 30 | #define RCC_HSE_RC 2 31 | #define RCC_PLL 3 32 | 33 | /* PLL options */ 34 | #define RCC_PLL_HSI_BY2 0 35 | #define RCC_PLL_HSE 1 36 | #define RCC_PLL_HSE_BY2 2 37 | 38 | /* MCO pin options */ 39 | #define NO_CLOCK 0 40 | #define HSI_CLOCK 1 41 | #define HSE_CLOCK 2 42 | #define PLL_CLOCK 3 43 | #define SYSTEM_CLOCK 4 44 | 45 | /* Prescaller options for buses*/ 46 | #define RCC_PRESCALLER_0 0 47 | #define RCC_PRESCALLER_2 2 48 | #define RCC_PRESCALLER_4 3 49 | #define RCC_PRESCALLER_6 4 50 | #define RCC_PRESCALLER_16 5 51 | #define RCC_PRESCALLER_64 6 52 | #define RCC_PRESCALLER_128 7 53 | #define RCC_PRESCALLER_256 8 54 | #define RCC_PRESCALLER_512 9 55 | 56 | 57 | /* RCC_CR BITS */ 58 | #define RCC_HSI_ON 0 59 | #define RCC_HSI_RDY 1 60 | #define RCC_HSI_TRIM0 3 61 | #define RCC_HSE_ON 16 62 | #define RCC_HSE_RDY 17 63 | #define RCC_HSE_BYP 18 64 | #define RCC_CSS_ON 19 65 | #define RCC_PLL_ON 24 66 | #define RCC_PLL_RDY 25 67 | 68 | /* RCC_CFGR BITS */ 69 | #define RCC_SW0 0 70 | #define RCC_SW1 1 71 | #define RCC_HPRE0 4 72 | #define RCC_HPRE1 5 73 | #define RCC_HPRE2 6 74 | #define RCC_HPRE3 7 75 | #define RCC_PPRE1_0 8 76 | #define RCC_PPRE1_1 9 77 | #define RCC_PPRE1_2 10 78 | #define RCC_PPRE2_0 11 79 | #define RCC_PPRE2_1 12 80 | #define RCC_PPRE2_2 13 81 | #define RCC_ADCPRE0 14 82 | #define RCC_ADCPRE2 15 83 | #define RCC_PLL_SRC 16 84 | #define RCC_PLL_XTPRE 17 85 | #define RCC_PLL_MUL0 18 86 | #define RCC_MCO_0 24 87 | #define RCC_MCO_1 25 88 | #define RCC_MCO_2 26 89 | 90 | 91 | 92 | #endif 93 | -------------------------------------------------------------------------------- /02 - MCAL/01 - RCC/RCC_program.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 8/8/2020 */ 5 | /*****************************************************/ 6 | #include "STD_TYPES.h" 7 | #include "BIT_MATH.h" 8 | 9 | #include "RCC_interface.h" 10 | #include "RCC_private.h" 11 | #include "RCC_config.h" 12 | 13 | void RCC_voidInitSysClock (void){ 14 | 15 | // choose system clock from (HSI - HSE crystal - HSE RC- PLL) 16 | #if SYS_CLOCK == RCC_HSI 17 | SET_BIT (RCC_CR , RCC_HSI_ON); //HSI on 18 | while( !(RCC_CR & (1< set the mode of the pin 81 | * i/p : port id (PORTA - PORTB - PORTC) / pin number ( PIN0 -> PIN15) / mode 82 | */ 83 | void GPIO_voidSetPinMode (GPIO_portId_t copyPortId , GPIO_pinsNumbers_t copyPinNumber , GPIO_modes_t copyMode ); 84 | 85 | /* 86 | * GPIO_voidsetPinValue - > used to set a specific pin HIGH or LOW 87 | or enable PULL UP or DOWN 88 | * i/p : port id (PORTA - PORTB - PORTC) / pin number ( PIN0 -> PIN15) / value ( HIGH - LOW) (GPIO_PULL_UP - GPIO_PULL_DOWN) 89 | */ 90 | void GPIO_voidsetPinValue (GPIO_portId_t copyPortId , GPIO_pinsNumbers_t copyPinNumber , u8 copy_u8Value); 91 | 92 | /* 93 | * GPIO_voidTogglePinValue - > Toggle the pin value 94 | * i/p : port id (PORTA - PORTB - PORTC) / pin number ( PIN0 -> PIN15) 95 | */ 96 | void GPIO_voidTogglePinValue (GPIO_portId_t copyPortId , GPIO_pinsNumbers_t copyPinNumber ); 97 | 98 | /* 99 | * GPIO_u8GetPinValue - > set the pin value 1 or 0 100 | * i/p : port id (PORTA - PORTB - PORTC) / pin number ( PIN0 -> PIN15) 101 | * o/p : the pin value (u8) 102 | */ 103 | u8 GPIO_u8GetPinValue (GPIO_portId_t copyPortId , GPIO_pinsNumbers_t copyPinNumber); 104 | 105 | /* 106 | * GPIO_voidSetPortMode - > set the mode of the port 107 | * i/p : port id (PORTA - PORTB - PORTC) / mode 108 | */ 109 | void GPIO_voidSetPortMode (GPIO_portId_t copyPortId , GPIO_modes_t copyMode ); 110 | 111 | /* 112 | * GPIO_voidSetPortValue - > write a value to the whole port 113 | * i/p : port id (PORTA - PORTB - PORTC) / value (0x0000 - 0xffff) 114 | */ 115 | void GPIO_voidSetPortValue (GPIO_portId_t copyPortId , u16 copy_u16Value); 116 | 117 | /* 118 | * GPIO_voidWriteByte - > write a value to the 8 pins 119 | * i/p : port id (PORTA - PORTB - PORTC) / pin number ( PIN0 -> PIN15)/ number of pins to be written /value (0x00 - 0xff) 120 | */ 121 | void GPIO_voidWritePins(GPIO_portId_t copyPortId , GPIO_pinsNumbers_t copyStartPinNumber , GPIO_numberOfPins_t copyNumberOfPins , u8 copy_u8Value ) ; 122 | 123 | /* 124 | * GPIO_voidLockPin - > Lock the pin configuration 125 | * i/p : port id (PORTA - PORTB - PORTC) / pin number (PIN0 - PIN15) 126 | */ 127 | void GPIO_voidLockPin(GPIO_portId_t copyPortId , GPIO_pinsNumbers_t copyPinNumber); 128 | 129 | #endif -------------------------------------------------------------------------------- /02 - MCAL/02 - GPIO/GPIO_private.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v02 */ 4 | /* date : 12/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef GPIO_PRIVATE_H 7 | #define GPIO_PRIVATE_H 8 | 9 | /* 10 | base address 11 | 0x4001 1000 GPIO Port C 12 | 0x4001 0C00 GPIO Port B 13 | 0x4001 0800 GPIO Port A 14 | */ 15 | /* 16 | GPIOx_CRL 0X00 17 | GPIOx_CRH 0X04 18 | GPIOx_IDR 0X08 19 | GPIOx_ODR 0X0C 20 | GPIOx_BSRR 0X10 21 | GPIOx_BRR 0X14 22 | GPIOx_LCKR 0X18 23 | */ 24 | 25 | /* PORTA Registers defintions */ 26 | #define GPIOA_CRL *((volatile u32 *) 0x40010800 ) 27 | #define GPIOA_CRH *((volatile u32 *) 0x40010804 ) 28 | #define GPIOA_IDR *((volatile u32 *) 0x40010808 ) 29 | #define GPIOA_ODR *((volatile u32 *) 0x4001080C ) 30 | #define GPIOA_BSRR *((volatile u32 *) 0x40010810 ) 31 | #define GPIOA_BRR *((volatile u32 *) 0x40010814 ) 32 | #define GPIOA_LCKR *((volatile u32 *) 0x40010818 ) 33 | 34 | /* PORTB Registers defintions */ 35 | #define GPIOB_CRL *((volatile u32 *) 0x40010C00 ) 36 | #define GPIOB_CRH *((volatile u32 *) 0x40010C04 ) 37 | #define GPIOB_IDR *((volatile u32 *) 0x40010C08 ) 38 | #define GPIOB_ODR *((volatile u32 *) 0x40010C0C ) 39 | #define GPIOB_BSRR *((volatile u32 *) 0x40010C10 ) 40 | #define GPIOB_BRR *((volatile u32 *) 0x40010C14 ) 41 | #define GPIOB_LCKR *((volatile u32 *) 0x40010C18 ) 42 | 43 | /* PORTC Registers defintions */ 44 | #define GPIOC_CRL *((volatile u32 *) 0x40011000 ) 45 | #define GPIOC_CRH *((volatile u32 *) 0x40011004 ) 46 | #define GPIOC_IDR *((volatile u32 *) 0x40011008 ) 47 | #define GPIOC_ODR *((volatile u32 *) 0x4001100C ) 48 | #define GPIOC_BSRR *((volatile u32 *) 0x40011010 ) 49 | #define GPIOC_BRR *((volatile u32 *) 0x40011014 ) 50 | #define GPIOC_LCKR *((volatile u32 *) 0x40011018 ) 51 | 52 | #define CLEAR_VALUE 15 53 | 54 | #endif -------------------------------------------------------------------------------- /02 - MCAL/02 - GPIO/GPIO_program.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v02 */ 4 | /* date : 12/8/2020 */ 5 | /*****************************************************/ 6 | #include "BIT_MATH.h" 7 | #include "STD_TYPES.h" 8 | 9 | #include "GPIO_interface.h" 10 | #include "GPIO_private.h" 11 | #include "GPIO_config.h" 12 | 13 | void GPIO_voidSetPinMode (GPIO_portId_t copyPortId , GPIO_pinsNumbers_t copyPinNumber , GPIO_modes_t copyMode ) { 14 | switch (copyPortId){ 15 | case PORTA : 16 | if (copyPinNumber <= 7){ 17 | GPIOA_CRL &= ~ (CLEAR_VALUE << copyPinNumber * 4 ) ; // clear the four bits first 18 | GPIOA_CRL |= (copyMode << copyPinNumber * 4 ) ; // update with mode value 19 | } 20 | else if (copyPinNumber <= 15){ 21 | copyPinNumber = copyPinNumber - 8 ; 22 | GPIOA_CRH &= ~ (CLEAR_VALUE << copyPinNumber * 4 ) ; // clear the four bits first 23 | GPIOA_CRH |= (copyMode << copyPinNumber * 4 ) ; // update with mode value 24 | } 25 | else { 26 | 27 | } 28 | break; 29 | case PORTB : 30 | if (copyPinNumber <= 7){ 31 | GPIOB_CRL &= ~ (CLEAR_VALUE << copyPinNumber * 4 ) ; // clear the four bits first 32 | GPIOB_CRL |= (copyMode << copyPinNumber * 4 ) ; // update with mode value 33 | } 34 | else if (copyPinNumber <= 15){ 35 | copyPinNumber = copyPinNumber - 8 ; 36 | GPIOB_CRH &= ~ (CLEAR_VALUE << copyPinNumber * 4 ) ; // clear the four bits first 37 | GPIOB_CRH |= (copyMode << copyPinNumber * 4 ) ; // update with mode value 38 | } 39 | else { 40 | 41 | } 42 | break; 43 | case PORTC : 44 | if (copyPinNumber <= 7){ 45 | GPIOC_CRL &= ~ (CLEAR_VALUE << copyPinNumber * 4 ) ; // clear the four bits first 46 | GPIOC_CRL |= (copyMode << copyPinNumber * 4 ) ; // update with mode value 47 | } 48 | else if (copyPinNumber <= 15){ 49 | copyPinNumber = copyPinNumber - 8 ; 50 | GPIOC_CRH &= ~ (CLEAR_VALUE << copyPinNumber * 4 ) ; // clear the four bits first 51 | GPIOC_CRH |= (copyMode << copyPinNumber * 4 ) ; // update with mode value 52 | } 53 | else{ 54 | 55 | } 56 | break; 57 | default : /* Error code */ break ; 58 | 59 | } 60 | } 61 | 62 | void GPIO_voidsetPinValue (GPIO_portId_t copyPortId , GPIO_pinsNumbers_t copyPinNumber , u8 copy_u8Value){ 63 | switch (copyPortId) { 64 | case PORTA : 65 | if (copy_u8Value == HIGH){ 66 | GPIOA_BSRR = (1 << copyPinNumber); 67 | } 68 | else if (copy_u8Value == LOW ){ 69 | GPIOA_BRR = (1 << copyPinNumber); 70 | } 71 | else { 72 | /* should not be here */ 73 | } 74 | break ; 75 | case PORTB : 76 | if (copy_u8Value == HIGH){ 77 | GPIOB_BSRR = (1 << copyPinNumber); 78 | } 79 | else if (copy_u8Value == LOW ){ 80 | GPIOB_BRR = (1 << copyPinNumber); 81 | } 82 | else { 83 | /* should not be here */ 84 | } 85 | break ; 86 | case PORTC : 87 | if (copy_u8Value == HIGH){ 88 | GPIOC_BSRR = (1 << copyPinNumber); 89 | } 90 | else if (copy_u8Value == LOW ){ 91 | GPIOC_BRR = (1 << copyPinNumber); 92 | } 93 | else { 94 | /* should not be here */ 95 | } 96 | break ; 97 | default : /* should not be here */ break ; 98 | } 99 | } 100 | 101 | void GPIO_voidTogglePinValue (GPIO_portId_t copyPortId , GPIO_pinsNumbers_t copyPinNumber){ 102 | switch (copyPortId) { 103 | case PORTA : 104 | TOGGLE_BIT (GPIOA_ODR , copyPinNumber) ; 105 | break ; 106 | case PORTB : 107 | TOGGLE_BIT (GPIOB_ODR , copyPinNumber) ; 108 | break ; 109 | case PORTC : 110 | TOGGLE_BIT (GPIOC_ODR , copyPinNumber) ; 111 | break ; 112 | default : /* should not be here */ break ; 113 | } 114 | } 115 | 116 | u8 GPIO_u8GetPinValue (GPIO_portId_t copyPortId , GPIO_pinsNumbers_t copyPinNumber){ 117 | u8 bitValue = 0 ; 118 | switch (copyPortId) { 119 | case PORTA : 120 | bitValue = GIT_BIT (GPIOA_IDR , copyPinNumber ); 121 | break ; 122 | case PORTB : 123 | bitValue = GIT_BIT (GPIOB_IDR , copyPinNumber ); 124 | break ; 125 | case PORTC : 126 | bitValue = GIT_BIT (GPIOC_IDR , copyPinNumber ); 127 | break ; 128 | default : /* should not be here */ break ; 129 | } 130 | return bitValue ; 131 | } 132 | 133 | void GPIO_voidSetPortMode (GPIO_portId_t copyPortId , GPIO_modes_t copyMode ){ 134 | for (GPIO_pinsNumbers_t pins = PIN0 ; pins <= PIN15 ; pins++){ 135 | GPIO_voidSetPinMode(copyPortId , pins , copyMode); 136 | } 137 | } 138 | 139 | void GPIO_voidSetPortValue (GPIO_portId_t copyPortId , u16 copy_u16Value){ 140 | switch (copyPortId) { 141 | case PORTA : 142 | GPIOA_ODR = copy_u16Value ; 143 | break ; 144 | case PORTB : 145 | GPIOB_ODR = copy_u16Value ; 146 | break ; 147 | case PORTC : 148 | GPIOC_ODR = copy_u16Value ; 149 | break ; 150 | default : /* should not be here */ break ; 151 | } 152 | } 153 | 154 | void GPIO_voidWritePins(GPIO_portId_t copyPortId , GPIO_pinsNumbers_t copyStartPinNumber , GPIO_numberOfPins_t copyNumberOfPins , u8 copy_u8Value ){ 155 | switch (copyPortId) { 156 | case PORTA : 157 | GPIOA_ODR &= ~ (copyNumberOfPins << copyStartPinNumber ) ; 158 | GPIOA_ODR |= (copy_u8Value << copyStartPinNumber ) ; 159 | break ; 160 | case PORTB : 161 | GPIOB_ODR &= ~ (copyNumberOfPins << copyStartPinNumber ) ; 162 | GPIOB_ODR |= (copy_u8Value << copyStartPinNumber ) ; 163 | break ; 164 | case PORTC : 165 | GPIOC_ODR &= ~ (copyNumberOfPins << copyStartPinNumber ) ; 166 | GPIOC_ODR |= (copy_u8Value << copyStartPinNumber ) ; 167 | break ; 168 | default : /* should not be here */ break ; 169 | } 170 | } 171 | 172 | void GPIO_voidLockPin(GPIO_portId_t copyPortId , GPIO_pinsNumbers_t copyPinNumber){ 173 | u32 tmp = 0x00010000; 174 | switch (copyPortId) { 175 | case PORTA : 176 | tmp |= (1 << copyPinNumber); 177 | /* Set LCKK bit */ 178 | GPIOA_LCKR = tmp; 179 | /* Reset LCKK bit */ 180 | GPIOA_LCKR = (1 << copyPinNumber); 181 | /* Set LCKK bit */ 182 | GPIOA_LCKR = tmp; 183 | /* Read LCKK bit*/ 184 | tmp = GPIOA_LCKR; 185 | /* Read LCKK bit*/ 186 | tmp = GPIOA_LCKR; 187 | break ; 188 | case PORTB : 189 | tmp |= (1 << copyPinNumber) ; 190 | /* Set LCKK bit */ 191 | GPIOB_LCKR = tmp; 192 | /* Reset LCKK bit */ 193 | GPIOB_LCKR = (1 << copyPinNumber); 194 | /* Set LCKK bit */ 195 | GPIOB_LCKR = tmp; 196 | /* Read LCKK bit*/ 197 | tmp = GPIOB_LCKR; 198 | /* Read LCKK bit*/ 199 | tmp = GPIOB_LCKR; 200 | 201 | break ; 202 | case PORTC : 203 | tmp |= (1 << copyPinNumber); 204 | /* Set LCKK bit */ 205 | GPIOC_LCKR = tmp; 206 | /* Reset LCKK bit */ 207 | GPIOC_LCKR = (1 << copyPinNumber); 208 | /* Set LCKK bit */ 209 | GPIOC_LCKR = tmp; 210 | /* Read LCKK bit*/ 211 | tmp = GPIOC_LCKR; 212 | /* Read LCKK bit*/ 213 | tmp = GPIOC_LCKR; 214 | break ; 215 | default : /* should not be here */ break ; 216 | } 217 | 218 | } -------------------------------------------------------------------------------- /02 - MCAL/03 - NVIC/NVIC_config.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 22/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef NVIC_CONFIG_H 7 | #define NVIC_CONFIG_H 8 | 9 | /* 10 | Options : 11 | NVIC_GROUPING_16 * sub priority = 0 * 12 | NVIC_GROUPING_8 * sub priority = 2 * 13 | NVIC_GROUPING_4 * sub priority = 4 * 14 | NVIC_GROUPING_2 * sub priority = 8 * 15 | NVIC_GROUPING_0 * sub priority = 16 * 16 | 17 | To disable nesting choose ( NVIC_GROUPING_0 ) 18 | */ 19 | #define NVIC_GROUPING_PRIORITY_TYPE NVIC_GROUPING_0 20 | 21 | 22 | 23 | 24 | #endif -------------------------------------------------------------------------------- /02 - MCAL/03 - NVIC/NVIC_interface.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 22/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef NVIC_INTERFACE_H 7 | #define NVIC_INTERFACE_H 8 | 9 | /* enum for all external interrupt 0:59 */ 10 | typedef enum { 11 | WWDG_IRQ = 0 , 12 | PVD_IRQ , 13 | TAMPER_IRQ , 14 | RTC_IRQ , 15 | FLASH_IRQ , 16 | RCC_IRQ , 17 | EXTI0_IRQ , 18 | EXTI1_IRQ , 19 | EXTI2_IRQ , 20 | EXTI3_IRQ , 21 | EXTI4_IRQ , 22 | DMA1_Channel1_IRQ , 23 | DMA1_Channel2_IRQ , 24 | DMA1_Channel3_IRQ , 25 | DMA1_Channel4_IRQ , 26 | DMA1_Channel5_IRQ , 27 | DMA1_Channel6_IRQ , 28 | DMA1_Channel7_IRQ , 29 | ADC1_2_IRQ , 30 | USB_HP_CAN_IRQ , 31 | USB_LP_CAN_IRQ , 32 | CAN_RX1_IRQ , 33 | CAN_SCE_IRQ , 34 | EXTI9_5_IRQ , 35 | TIM1_BRK_IRQ , 36 | TIM1_UP_IRQ , 37 | TIM1_TRG_COM_IRQ , 38 | TIM1_CC_IRQ , 39 | TIM2_IRQ , 40 | TIM3_IRQ , 41 | TIM4_IRQ , 42 | I2C1_EV_IRQ , 43 | I2C1_ER_IRQ , 44 | I2C2_EV_IRQ , 45 | I2C2_ER_IRQ , 46 | SPI1_IRQ , 47 | SPI2_IRQ , 48 | USART1_IRQ , 49 | USART2_IRQ , 50 | USART3_IRQ , 51 | EXTI15_10_IRQ , 52 | RTCAlarm_IRQ , 53 | USBWakeup_IRQ , 54 | TIM8_BRK_IRQ , 55 | TIM8_UP_IRQ , 56 | TIM8_TRG_COM_IRQ , 57 | TIM8_CC_IRQ , 58 | ADC3_IRQ , 59 | FSMC_IRQ , 60 | SDIO_IRQ , 61 | TIM5_IRQ , 62 | SPI3_IRQ , 63 | UART4_IRQ , 64 | UART5_IRQ , 65 | TIM6_IRQ , 66 | TIM7_IRQ , 67 | DMA2_Channel1_IRQ , 68 | DMA2_Channel2_IRQ , 69 | DMA2_Channel3_IRQ , 70 | DMA2_Channel4_5_IRQ 71 | }NVIC_IRQn_t; 72 | 73 | 74 | 75 | /* 76 | * NVIC_voidInit - > initialize the periority system (detrmine number of group priorities and sub priorties) 77 | */ 78 | void NVIC_voidInit(void); 79 | 80 | /* 81 | * NVIC_voidEnableIRQ - > Enable a specific external interrupt 82 | * i/p : Interrupt number (NVIC_IRQn_t) 0:59 83 | */ 84 | void NVIC_voidEnableIRQ(NVIC_IRQn_t copyIRQn); 85 | 86 | /* 87 | * NVIC_voidDisableIRQ - > Disable a specific external interrupt 88 | * i/p : Interrupt number (NVIC_IRQn_t) 0:59 89 | */ 90 | void NVIC_voidDisableIRQ(NVIC_IRQn_t copyIRQn); 91 | 92 | /* 93 | * NVIC_u8GetPendingIRQ - > Get the pending flag state 94 | * i/p : Interrupt number (NVIC_IRQn_t) 0:59 95 | * o/p : (u8) 0 or 1 96 | */ 97 | u8 NVIC_u8GetPendingIRQ (NVIC_IRQn_t copyIRQn); 98 | 99 | /* 100 | * NVIC_voidSetPendingIRQ - > set pending flag for a specific external interrupt 101 | * i/p : Interrupt number (NVIC_IRQn_t) 0:59 102 | */ 103 | void NVIC_voidSetPendingIRQ (NVIC_IRQn_t copyIRQn); 104 | 105 | /* 106 | * NVIC_voidClearPendingIRQ - > clear pending flag for a specific external interrupt 107 | * i/p : Interrupt number (NVIC_IRQn_t) 0:59 108 | */ 109 | void NVIC_voidClearPendingIRQ (NVIC_IRQn_t copyIRQn); 110 | 111 | /* 112 | * NVIC_u8GetActive - > Get the active flag state 113 | * i/p : Interrupt number (NVIC_IRQn_t) 0:59 114 | * o/p : (u8) 0 or 1 115 | */ 116 | u8 NVIC_u8GetActive (NVIC_IRQn_t copyIRQn); 117 | 118 | /* 119 | * NVIC_voidSetPriority - > Set periority for a specific external interrupt 120 | * i/p : Interrupt number (NVIC_IRQn_t) 0:59 / Interrupt periority (u8) 0:15 121 | */ 122 | void NVIC_voidSetPriority (NVIC_IRQn_t copyIRQn, u8 copy_u8Priority); 123 | 124 | 125 | 126 | 127 | #endif -------------------------------------------------------------------------------- /02 - MCAL/03 - NVIC/NVIC_private.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 22/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef NVIC_PRIVATE_H 7 | #define NVIC_PRIVATE_H 8 | 9 | /* Struct for the NVIC regsiter mapping */ 10 | typedef struct { 11 | volatile u32 ISER[3] ; /* Interrupt set enable registers */ 12 | volatile u32 RESERVED0[29] ; 13 | volatile u32 ICER[3] ; /* Interrupt clear enable registers */ 14 | volatile u32 RESERVED1[29] ; 15 | volatile u32 ISPR[3] ; /* Interrupt set pinding registers */ 16 | volatile u32 RESERVED2[29] ; 17 | volatile u32 ICPR[3] ; /* Interrupt clear pinding registers */ 18 | volatile u32 RESERVED3[29] ; 19 | volatile u32 IABR[3] ; /* Interrupt active bit registers */ 20 | volatile u32 RESERVED4[61] ; 21 | volatile u8 IPR[80] ; /* Interrupt priority registers */ 22 | }NVIC_t; 23 | 24 | /* Register defintions */ 25 | #define NVIC_BASE_ADDRESS 0xE000E100 26 | #define NVIC ((volatile NVIC_t *) NVIC_BASE_ADDRESS ) 27 | #define NVIC_STIR *((volatile u32 *) NVIC_BASE_ADDRESS + 0xE00 ) 28 | #define SCB_AIRCR *((volatile u32 *) 0xE000ED0C ) 29 | 30 | /* constants */ 31 | #define VECTKEY 0x05FA0000 32 | #define GROUPING_BIT 8 33 | 34 | /* Group priority options */ 35 | #define NVIC_GROUPING_16 0b011 36 | #define NVIC_GROUPING_8 0b100 37 | #define NVIC_GROUPING_4 0b101 38 | #define NVIC_GROUPING_2 0b110 39 | #define NVIC_GROUPING_0 0b111 40 | 41 | 42 | 43 | #endif -------------------------------------------------------------------------------- /02 - MCAL/03 - NVIC/NVIC_program.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 22/8/2020 */ 5 | /*****************************************************/ 6 | /* Library includes */ 7 | #include "BIT_MATH.h" 8 | #include "STD_TYPES.h" 9 | 10 | /* Module includes */ 11 | #include "NVIC_interface.h" 12 | #include "NVIC_private.h" 13 | #include "NVIC_config.h" 14 | 15 | 16 | void NVIC_voidInit(void){ 17 | 18 | /* Choose configurations */ 19 | #if NVIC_GROUPING_PRIORITY_TYPE == NVIC_GROUPING_16 20 | SCB_AIRCR = ( VECTKEY | (NVIC_GROUPING_16 << GROUPING_BIT) ) ; 21 | 22 | #elif NVIC_GROUPING_PRIORITY_TYPE == NVIC_GROUPING_8 23 | SCB_AIRCR = ( VECTKEY | (NVIC_GROUPING_8 << GROUPING_BIT) ) ; 24 | 25 | #elif NVIC_GROUPING_PRIORITY_TYPE == NVIC_GROUPING_4 26 | SCB_AIRCR = ( VECTKEY | (NVIC_GROUPING_4 << GROUPING_BIT) ) ; 27 | 28 | #elif NVIC_GROUPING_PRIORITY_TYPE == NVIC_GROUPING_2 29 | SCB_AIRCR = ( VECTKEY | (NVIC_GROUPING_2 << GROUPING_BIT) ) ; 30 | 31 | #elif NVIC_GROUPING_PRIORITY_TYPE == NVIC_GROUPING_0 32 | SCB_AIRCR = ( VECTKEY | (NVIC_GROUPING_0 << GROUPING_BIT) ) ; 33 | 34 | #else 35 | #error (" Configuration error ") 36 | #endif 37 | 38 | } 39 | 40 | void NVIC_voidEnableIRQ(NVIC_IRQn_t copyIRQn){ 41 | /* Devided by 32 to write to the right regsiter index */ 42 | /* Anding (copyIRQn) with 31 so value will always be under 32 */ 43 | u8 reg_index = copyIRQn / 32 ; 44 | NVIC->ISER[ reg_index ] = (1 << (copyIRQn & 31 )) ; 45 | } 46 | 47 | void NVIC_voidDisableIRQ(NVIC_IRQn_t copyIRQn){ 48 | u8 reg_index = copyIRQn / 32 ; 49 | NVIC->ICER[ reg_index ] = (1 << (copyIRQn & 31 )) ; 50 | } 51 | 52 | u8 NVIC_u8GetPendingIRQ (NVIC_IRQn_t copyIRQn){ 53 | u8 reg_index = copyIRQn / 32 ; 54 | u8 bit = GIT_BIT(NVIC->ISPR[ reg_index ] , (copyIRQn & 31 ) ) ; 55 | return bit ; 56 | } 57 | 58 | void NVIC_voidSetPendingIRQ (NVIC_IRQn_t copyIRQn){ 59 | u8 reg_index = copyIRQn / 32 ; 60 | NVIC->ISPR[ reg_index ] = (1 << (copyIRQn & 31 )) ; 61 | } 62 | 63 | void NVIC_voidClearPendingIRQ (NVIC_IRQn_t copyIRQn){ 64 | u8 reg_index = copyIRQn / 32 ; 65 | NVIC->ICPR[ reg_index ] = (1 << (copyIRQn & 31 )) ; 66 | } 67 | 68 | u8 NVIC_u8GetActive (NVIC_IRQn_t copyIRQn){ 69 | u8 reg_index = copyIRQn / 32 ; 70 | u8 bit = GIT_BIT(NVIC->IABR[ reg_index ] , (copyIRQn & 31 ) ) ; 71 | return bit ; 72 | } 73 | 74 | void NVIC_voidSetPriority (NVIC_IRQn_t copyIRQn, u8 copy_u8Priority){ 75 | 76 | /* Writing into the four bit the periority */ 77 | NVIC->IPR[ copyIRQn ] = (copy_u8Priority << 4); 78 | 79 | } 80 | 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /02 - MCAL/04 - EXTI/EXTI_config.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 24/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef EXTI_CONFIG_H 7 | #define EXTI_CONFIG_H 8 | 9 | 10 | 11 | 12 | 13 | 14 | #endif -------------------------------------------------------------------------------- /02 - MCAL/04 - EXTI/EXTI_interface.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 24/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef EXTI_INTERFACE_H 7 | #define EXTI_INTERFACE_H 8 | 9 | /* enum to hold the trigger options */ 10 | typedef enum { 11 | EXTI_RISING_EDGE , 12 | EXTI_FALLING_EDGE, 13 | EXTI_ON_CHANGE 14 | }EXTI_triggerSelect_t; 15 | 16 | /* 17 | * EXTI_voidInitLineInterrupt - > Only set the trigger sensing 18 | * i/p : (u8) line number / (EXTI_triggerSelect_t) the trigger mode 19 | */ 20 | void EXTI_voidInitLineInterrupt(u8 copy_u8Line , EXTI_triggerSelect_t copy_triggerType); 21 | 22 | /* 23 | * EXTI_voidEnableLineInterrupt - > Enable a specific external interrupt line 24 | * i/p : (u8) line number 25 | */ 26 | void EXTI_voidEnableLineInterrupt(u8 copy_u8Line); 27 | 28 | /* 29 | * EXTI_voidDisableLineInterrupt - > Disable a specific external interrupt line 30 | * i/p : (u8) line number 31 | */ 32 | void EXTI_voidDisableLineInterrupt(u8 copy_u8Line); 33 | 34 | /* 35 | * EXTI_voidSoftwareInterrupt - > Trigger SWI for a given external interrupt line 36 | * i/p : (u8) line number 37 | */ 38 | void EXTI_voidSoftwareInterrupt(u8 copy_u8Line); 39 | 40 | /* 41 | * EXTI_voidClearPendingFlag - > clear the pending flag for a specific external interrupt line 42 | * i/p : (u8) line number 43 | */ 44 | void EXTI_voidClearPendingFlag(u8 copy_u8Line); 45 | 46 | 47 | #endif -------------------------------------------------------------------------------- /02 - MCAL/04 - EXTI/EXTI_private.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 24/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef EXTI_PRIVATE_H 7 | #define EXTI_PRIVATE_H 8 | 9 | /* Struct for the EXTI regsiter mapping */ 10 | typedef struct { 11 | volatile u32 IMR ; /* Interrupt mask register */ 12 | volatile u32 EMR ; /* Event mask register */ 13 | volatile u32 RTSR ; /* Rising trigger selection register */ 14 | volatile u32 FTSR ; /* Falling trigger selection register */ 15 | volatile u32 SWIER ; /* Software interrupt event register */ 16 | volatile u32 PR ; /* Pending register */ 17 | }EXTI_t; 18 | 19 | #define EXTI ((volatile EXTI_t *) 0x40010400) 20 | 21 | 22 | 23 | #endif -------------------------------------------------------------------------------- /02 - MCAL/04 - EXTI/EXTI_program.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 24/8/2020 */ 5 | /*****************************************************/ 6 | /* Library includes */ 7 | #include "BIT_MATH.h" 8 | #include "STD_TYPES.h" 9 | 10 | /* Module includes */ 11 | #include "EXTI_interface.h" 12 | #include "EXTI_private.h" 13 | #include "EXTI_config.h" 14 | 15 | 16 | void EXTI_voidInitLineInterrupt(u8 copy_u8Line , EXTI_triggerSelect_t copy_triggerType){ 17 | /* Disable interrupt on line */ 18 | CLEAR_BIT(EXTI->IMR , copy_u8Line ); 19 | 20 | /* select the edge */ 21 | switch(copy_triggerType){ 22 | case EXTI_RISING_EDGE : 23 | SET_BIT (EXTI->RTSR , copy_u8Line ); 24 | CLEAR_BIT(EXTI->FTSR , copy_u8Line ); 25 | break; 26 | 27 | case EXTI_FALLING_EDGE : 28 | SET_BIT (EXTI->FTSR , copy_u8Line ); 29 | CLEAR_BIT(EXTI->RTSR , copy_u8Line ); 30 | break; 31 | 32 | case EXTI_ON_CHANGE : 33 | SET_BIT(EXTI->RTSR , copy_u8Line ); 34 | SET_BIT(EXTI->FTSR , copy_u8Line ); 35 | break; 36 | 37 | default : 38 | /* should not be here */ 39 | break; 40 | } 41 | } 42 | 43 | void EXTI_voidEnableLineInterrupt(u8 copy_u8Line){ 44 | /* Enable interrupt on line */ 45 | SET_BIT(EXTI->IMR , copy_u8Line ); 46 | } 47 | 48 | void EXTI_voidDisableLineInterrupt(u8 copy_u8Line){ 49 | /* Disable interrupt on line */ 50 | CLEAR_BIT(EXTI->IMR , copy_u8Line ); 51 | } 52 | 53 | void EXTI_voidSoftwareInterrupt(u8 copy_u8Line){ 54 | SET_BIT(EXTI->SWIER , copy_u8Line ); 55 | } 56 | 57 | void EXTI_voidClearPendingFlag(u8 copy_u8Line){ 58 | SET_BIT(EXTI->PR , copy_u8Line ); 59 | } 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /02 - MCAL/05 - AFIO/AFIO_config.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 24/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef AFIO_CONFIG_H 7 | #define AFIO_CONFIG_H 8 | 9 | 10 | 11 | 12 | 13 | 14 | #endif -------------------------------------------------------------------------------- /02 - MCAL/05 - AFIO/AFIO_interface.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 24/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef AFIO_INTERFACE_H 7 | #define AFIO_INTERFACE_H 8 | 9 | 10 | /* 11 | * AFIO_voidSelectPortForLine - > Assign the line for specific port 12 | * i/p : (u8) line number 0:15 / (u8) Port id 0:6 13 | */ 14 | void AFIO_voidSelectPortForLine(u8 copy_u8Line , u8 copy_u8PortId); 15 | 16 | 17 | #endif -------------------------------------------------------------------------------- /02 - MCAL/05 - AFIO/AFIO_private.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 24/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef AFIO_PRIVATE_H 7 | #define AFIO_PRIVATE_H 8 | 9 | /* Struct for the AFIO regsiter mapping */ 10 | typedef struct { 11 | volatile u32 EVCR ; /* Event control register */ 12 | volatile u32 MAPR ; /* remap and debug I/O configuration register */ 13 | volatile u32 EXTICR[4] ; /* External interrupt configuration registers */ 14 | volatile u32 MAPR2 ; /* remap and debug I/O configuration register2 */ 15 | }AFIO_t; 16 | 17 | 18 | #define AFIO ((volatile AFIO_t *) 0x40010000) 19 | 20 | 21 | 22 | #endif -------------------------------------------------------------------------------- /02 - MCAL/05 - AFIO/AFIO_program.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 24/8/2020 */ 5 | /*****************************************************/ 6 | /* Library includes */ 7 | #include "BIT_MATH.h" 8 | #include "STD_TYPES.h" 9 | 10 | /* Module includes */ 11 | #include "AFIO_interface.h" 12 | #include "AFIO_private.h" 13 | #include "AFIO_config.h" 14 | 15 | 16 | void AFIO_voidSelectPortForLine(u8 copy_u8Line , u8 copy_u8PortId){ 17 | 18 | /* Get the register index */ 19 | u8 local_u8RegIndex = copy_u8Line / 4 ; 20 | 21 | /* Get the offest */ 22 | u8 local_u8Offset = copy_u8Line % 4 ; 23 | local_u8Offset *= 4 ; 24 | 25 | /* Clear the four bits first */ 26 | AFIO->EXTICR[local_u8RegIndex] &= ~(0xf << local_u8Offset); 27 | 28 | /* Write into the four bits */ 29 | AFIO->EXTICR[local_u8RegIndex] |= (copy_u8PortId << local_u8Offset); 30 | } 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /02 - MCAL/06 - STK/STK_config.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v02 */ 4 | /* date : 25/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef STK_CONFIG_H 7 | #define STK_CONFIG_H 8 | 9 | /* 10 | Options : 11 | AHP 12 | AHP_BY_8 13 | */ 14 | #define STK_CLK_SOURCE AHP_BY_8 15 | 16 | 17 | /* 18 | Determine the AHP clock so the calculations be correct 19 | */ 20 | #define STK_AHP_CLK 8000000 21 | 22 | 23 | 24 | 25 | 26 | 27 | #endif -------------------------------------------------------------------------------- /02 - MCAL/06 - STK/STK_interface.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v02 */ 4 | /* date : 25/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef STK_INTERFACE_H 7 | #define STK_INTERFACE_H 8 | 9 | /******************************************************/ 10 | /* (STK_voidSetBusyWait) function is to use the STK in 11 | synchronous mode the processor will be hold. 12 | (STK_voidSetIntervalPeriodic) function is to use the 13 | STK in asynchronous mode and a function will be 14 | passed to the ISR. 15 | you should use the STK in only one of the modes to 16 | guarantee to work correctly 17 | you can also use it to caclulate some elabsed time from 18 | specific point using (STK_voidStart) 19 | */ 20 | /*******************************************************/ 21 | 22 | /**** global types ****/ 23 | typedef enum { 24 | TIME_MS, 25 | TIME_US 26 | }STK_time_t; 27 | 28 | /*******************************************************/ 29 | 30 | /**** Function prototypes *****/ 31 | /* 32 | * STK_voidInit - > initialize the SysTick 33 | */ 34 | void STK_voidInit(void); 35 | 36 | /* 37 | * STK_voidSetBusyWait - > hold the processor for amount of time in ms or us 38 | * i/p : (u32) Time / (STK_time_t) unit : TIME_MS - TIME_US 39 | */ 40 | void STK_voidSetBusyWait( u32 Copy_u32Time , STK_time_t copy_unit ); 41 | 42 | /* 43 | * STK_voidSetIntervalPeriodic - > pass a periodic function to the SysTick ISR 44 | * i/p : (u32) Time / (STK_time_t) unit : TIME_MS - TIME_US / void function 45 | */ 46 | void STK_voidSetIntervalPeriodic( u32 Copy_u32Time , STK_time_t copy_unit, void (*Copy_func)(void)); 47 | 48 | /* 49 | * STK_voidSetIntervalSingle - > pass a one shot function to the SysTick ISR 50 | * i/p : (u32) Time / (STK_time_t) unit : TIME_MS - TIME_US / void function 51 | */ 52 | void STK_voidSetIntervalSingle( u32 Copy_u32Time , STK_time_t copy_unit, void (*Copy_func)(void)); 53 | 54 | /* 55 | * STK_voidStop - > stop timer and disable interrupt 56 | */ 57 | void STK_voidStop(void); 58 | 59 | /* 60 | * STK_voidResume - > start timer and enable interrupt 61 | */ 62 | void STK_voidResume(void); 63 | 64 | /* 65 | * STK_voidStart - > start timer to count from max value can be used to culcated time elapsed 66 | from calling this function 67 | */ 68 | void STK_voidStart(void); 69 | 70 | /* 71 | * STK_u32GetElapsedTime - > return elapsed time in ms or us 72 | * i/p : (STK_time_t) unit : TIME_MS - TIME_US 73 | */ 74 | u32 STK_u32GetElapsedTime(STK_time_t copy_unit); 75 | 76 | /* 77 | * STK_u32GetRemainingTime - > return remaining time in ms or us 78 | * i/p : (STK_time_t) unit : TIME_MS - TIME_US 79 | */ 80 | u32 STK_u32GetRemainingTime(STK_time_t copy_unit); 81 | 82 | 83 | #endif -------------------------------------------------------------------------------- /02 - MCAL/06 - STK/STK_private.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v02 */ 4 | /* date : 25/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef STK_PRIVATE_H 7 | #define STK_PRIVATE_H 8 | 9 | /* Struct for the AFIO regsiter mapping */ 10 | typedef struct { 11 | volatile u32 CTRL ; /* SysTick control and status register */ 12 | volatile u32 LOAD ; /* SysTick reload value register */ 13 | volatile u32 VAL ; /* SysTick reload value register */ 14 | volatile u32 CALIB ; /* SysTick calibration value register */ 15 | }STK_t; 16 | 17 | #define STK ((volatile STK_t *) 0xE000E010) 18 | 19 | /********** Bits **********/ 20 | #define STK_CTRL_ENABLE 0 21 | #define STK_CTRL_TICKINT 1 22 | #define STK_CTRL_CLKSOURCE 2 23 | #define STK_CTRL_COUNTFLAG 16 24 | 25 | /******** Clk options ********/ 26 | #define AHP 0 27 | #define AHP_BY_8 1 28 | 29 | /****** interrupt options *******/ 30 | #define ENABLE 1 31 | #define DISABLE 0 32 | 33 | 34 | 35 | #endif -------------------------------------------------------------------------------- /02 - MCAL/06 - STK/STK_program.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v02 */ 4 | /* date : 25/8/2020 */ 5 | /*****************************************************/ 6 | /* Library includes */ 7 | #include "BIT_MATH.h" 8 | #include "STD_TYPES.h" 9 | 10 | /* Module includes */ 11 | #include "STK_interface.h" 12 | #include "STK_private.h" 13 | #include "STK_config.h" 14 | 15 | /***** Global variable *****/ 16 | u32 global_u32Clk = 0 ; 17 | u8 Global_u8SingleFlag = 0 ; 18 | void (* functionCallBack)(); 19 | 20 | void STK_voidInit(void) 21 | { 22 | 23 | /* choosing clk for the SysTick */ 24 | #if STK_CLK_SOURCE == AHP 25 | SET_BIT (STK->CTRL , STK_CTRL_CLKSOURCE); 26 | global_u32Clk = STK_AHP_CLK ; 27 | 28 | #elif STK_CLK_SOURCE == AHP_BY_8 29 | CLEAR_BIT (STK->CTRL , STK_CTRL_CLKSOURCE); 30 | global_u32Clk = STK_AHP_CLK / 8 ; 31 | 32 | #else 33 | #error (" configuration error") 34 | #endif 35 | } 36 | 37 | void STK_voidSetBusyWait( u32 Copy_u32Time , STK_time_t copy_unit ) 38 | { 39 | u32 local_u32Load = 0 ; 40 | STK->VAL = 0 ; 41 | /* Calculate and load the load value */ 42 | switch (copy_unit){ 43 | case TIME_MS : 44 | local_u32Load = Copy_u32Time * (global_u32Clk / 1000) ; 45 | STK->LOAD = local_u32Load ; 46 | break; 47 | 48 | case TIME_US : 49 | local_u32Load = Copy_u32Time * (global_u32Clk / 1000000) ; 50 | STK->LOAD = local_u32Load ; 51 | break ; 52 | default : /* Should not be here */ break; 53 | } 54 | 55 | /* Start timer */ 56 | SET_BIT(STK->CTRL , STK_CTRL_ENABLE); 57 | 58 | /* Wait for the flag */ 59 | while ( !(GIT_BIT (STK->CTRL , STK_CTRL_COUNTFLAG))); 60 | 61 | /* Stop timer */ 62 | CLEAR_BIT(STK->CTRL , STK_CTRL_ENABLE); 63 | } 64 | 65 | void STK_voidSetIntervalPeriodic( u32 Copy_u32Time , STK_time_t copy_unit, void (*Copy_func)(void)){ 66 | u32 local_u32Load = 0 ; 67 | STK->VAL = 0 ; 68 | /* Calculate and load the load value */ 69 | switch (copy_unit){ 70 | case TIME_MS : 71 | local_u32Load = Copy_u32Time * (global_u32Clk / 1000) ; 72 | STK->LOAD = local_u32Load ; 73 | break; 74 | 75 | case TIME_US : 76 | local_u32Load = Copy_u32Time * (global_u32Clk / 1000000) ; 77 | STK->LOAD = local_u32Load ; 78 | break ; 79 | default : /* Should not be here */ break; 80 | } 81 | 82 | /* To pass the function to ISR */ 83 | functionCallBack = Copy_func ; 84 | 85 | /* Start timer */ 86 | SET_BIT(STK->CTRL , STK_CTRL_ENABLE); 87 | 88 | /* Enable interrupt */ 89 | SET_BIT(STK->CTRL , STK_CTRL_TICKINT); 90 | } 91 | 92 | void STK_voidSetIntervalSingle( u32 Copy_u32Time , STK_time_t copy_unit, void (*Copy_func)(void)){ 93 | u32 local_u32Load = 0 ; 94 | CLEAR_BIT(STK->CTRL , STK_CTRL_ENABLE); 95 | STK->VAL = 0 ; 96 | /* Calculate and load the load value */ 97 | switch (copy_unit){ 98 | case TIME_MS : 99 | local_u32Load = Copy_u32Time * (global_u32Clk / 1000) ; 100 | STK->LOAD = local_u32Load ; 101 | break; 102 | 103 | case TIME_US : 104 | local_u32Load = Copy_u32Time * (global_u32Clk / 1000000) ; 105 | STK->LOAD = local_u32Load ; 106 | break ; 107 | default : /* Should not be here */ break; 108 | } 109 | 110 | /* To pass the function to ISR */ 111 | functionCallBack = Copy_func ; 112 | 113 | /* Start timer */ 114 | SET_BIT(STK->CTRL , STK_CTRL_ENABLE); 115 | 116 | /* Enable interrupt */ 117 | SET_BIT(STK->CTRL , STK_CTRL_TICKINT); 118 | 119 | /* Set single flag */ 120 | Global_u8SingleFlag = 1 ; 121 | } 122 | 123 | void STK_voidStop(void) 124 | { 125 | /* Stop timer */ 126 | CLEAR_BIT(STK->CTRL , STK_CTRL_ENABLE); 127 | 128 | /* Disable interrupt */ 129 | CLEAR_BIT(STK->CTRL , STK_CTRL_TICKINT); 130 | } 131 | 132 | void STK_voidResume(void) 133 | { 134 | /* Start timer */ 135 | SET_BIT(STK->CTRL , STK_CTRL_ENABLE); 136 | 137 | /* Enable interrupt */ 138 | SET_BIT(STK->CTRL , STK_CTRL_TICKINT); 139 | } 140 | 141 | void STK_voidStart(void) 142 | { 143 | /* Zero the val rigester */ 144 | STK->VAL = 0 ; 145 | 146 | /* Load value with max value */ 147 | STK->LOAD = 0xFFFFFF; 148 | 149 | /* Start timer */ 150 | SET_BIT(STK->CTRL , STK_CTRL_ENABLE); 151 | } 152 | 153 | 154 | u32 STK_u32GetElapsedTime(STK_time_t copy_unit) 155 | { 156 | u32 local_u32Value = STK->LOAD - STK->VAL ; 157 | u32 local_u32ElapsedTime = 0 ; 158 | /* Calculate the elabsed time in ms or us */ 159 | switch (copy_unit){ 160 | case TIME_MS : 161 | local_u32ElapsedTime = local_u32Value / (global_u32Clk / 1000) ; 162 | break; 163 | 164 | case TIME_US : 165 | local_u32ElapsedTime = local_u32Value / (global_u32Clk / 1000000) ; 166 | break ; 167 | default : /* Should not be here */ break; 168 | } 169 | return local_u32ElapsedTime ; 170 | } 171 | 172 | 173 | u32 STK_u32GetRemainingTime(STK_time_t copy_unit) 174 | { 175 | u32 local_u32Value = STK->VAL ; 176 | u32 local_u32RemainingTime = 0 ; 177 | /* Calculate remaining time in ms or us */ 178 | switch (copy_unit){ 179 | case TIME_MS : 180 | local_u32RemainingTime = local_u32Value / (global_u32Clk / 1000) ; 181 | break; 182 | 183 | case TIME_US : 184 | local_u32RemainingTime = local_u32Value / (global_u32Clk / 1000000) ; 185 | break ; 186 | default : /* Should not be here */ break; 187 | } 188 | return local_u32RemainingTime ; 189 | } 190 | 191 | void SysTick_Handler(void) 192 | { 193 | /* In case of using single interval*/ 194 | if (Global_u8SingleFlag){ 195 | /* Stop timer */ 196 | CLEAR_BIT(STK->CTRL , STK_CTRL_ENABLE); 197 | /* Disable interrupt */ 198 | CLEAR_BIT(STK->CTRL , STK_CTRL_TICKINT); 199 | } 200 | functionCallBack(); 201 | } 202 | -------------------------------------------------------------------------------- /02 - MCAL/07 - TIMER/TIMER_config.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 10/9/2020 */ 5 | /*****************************************************/ 6 | 7 | #include "STD_TYPES.h" 8 | #include "TIMER_interface.h" 9 | #include "TIMER_config.h" 10 | 11 | 12 | /** Timer configurations table **/ 13 | const TIMER_config_t configTable[TIMER_NUMBER] = { 14 | // Timer Timer Timer Interrupt CLK Prescaller Interval 15 | // channel enable mode enable source in US 16 | {TIM1 , DISABLE , UP_COUNTER , DISABLE , APB2_CLK , 128 , 0 } , 17 | {TIM2 , DISABLE , UP_COUNTER , DISABLE , APB1_CLK , 8 , 0 } , 18 | {TIM3 , ENABLE , UP_COUNTER , DISABLE , APB1_CLK , 128 , 0 } , 19 | {TIM4 , DISABLE , UP_COUNTER , DISABLE , APB1_CLK , 128 , 0 } 20 | }; 21 | -------------------------------------------------------------------------------- /02 - MCAL/07 - TIMER/TIMER_config.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 10/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef TIMER_CONFIG_H 7 | #define TIMER_CONFIG_H 8 | 9 | 10 | /* 11 | Enter the APB2 and APB1 bus clock 12 | */ 13 | #define APB2_CLK 8000000 14 | #define APB1_CLK 8000000 15 | 16 | /* 17 | Number of timer in the MCU 18 | */ 19 | 20 | #define TIMER_NUMBER 4 21 | 22 | 23 | /******************* Private data for the config.c ******************/ 24 | 25 | /*** Timer configurations struct ***/ 26 | typedef struct { 27 | u8 u8Channel ; 28 | u8 u8Enable ; 29 | u8 u8Mode ; 30 | u8 u8InterruptEnable; 31 | u32 u32ClkSource ; 32 | u16 u16Prescaler; 33 | u32 u32Interval ; 34 | }TIMER_config_t; 35 | 36 | /* Macros */ 37 | #define ENABLE 1 38 | #define DISABLE 0 39 | 40 | /* Timer Modes */ 41 | #define UP_COUNTER 0 42 | #define DOWN_COUNTER 1 43 | 44 | 45 | 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /02 - MCAL/07 - TIMER/TIMER_interface.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 10/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef TIMER_INTERFACE_H 7 | #define TIMER_INTERFACE_H 8 | 9 | 10 | /**** Timer channels ****/ 11 | typedef enum { 12 | TIM1, 13 | TIM2, 14 | TIM3, 15 | TIM4, 16 | MAX_TIMERS 17 | }TIMER_channels_t; 18 | 19 | /***********************************************************/ 20 | /* Function Name : TIMER_voidInit 21 | * Function Type : Initialization 22 | * Parameters : void 23 | * Return : void 24 | * Discription : 25 | * Pre-conditions : RCC must be enabled for used timers 26 | if interrupt is enabled you must enable 27 | the correspending interrupt from NVIC 28 | */ 29 | /************************************************************/ 30 | void TIMER_voidInit (void); 31 | 32 | /***********************************************************/ 33 | /* Function Name : TIMER_voidSetBusyWait 34 | * Function Type : Delay function 35 | * Parameters : Timer channel / Time in us 36 | * Return : void 37 | * Discription : 38 | * Pre-conditions : Timer's interrupt must be disabled 39 | */ 40 | /************************************************************/ 41 | void TIMER_voidSetBusyWait (TIMER_channels_t copy_channel , u32 copy_u32TimeUS); 42 | 43 | 44 | /***********************************************************/ 45 | /* Function Name : TIMER_voidSetPeriodic 46 | * Function Type : 47 | * Parameters : Timer channel / Time in us / void function 48 | * Return : void 49 | * Discription : 50 | * Pre-conditions : 51 | */ 52 | /************************************************************/ 53 | void TIMER_voidSetPeriodic (TIMER_channels_t copy_channel , u32 copy_u32TimeUS , void (*func)(void)); 54 | 55 | /***********************************************************/ 56 | /* Function Name : TIMER_voidDisableOverFlowInterrupt 57 | * Function Type : 58 | * Parameters : Timer channel 59 | * Return : void 60 | * Discription : 61 | * Pre-conditions : 62 | */ 63 | /************************************************************/ 64 | void TIMER_voidDisableOverFlowInterrupt(TIMER_channels_t copy_channel ); 65 | 66 | 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /02 - MCAL/07 - TIMER/TIMER_private.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 10/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef TIMER_PRIVATE_H 7 | #define TIMER_PRIVATE_H 8 | 9 | 10 | /* Array of the base address of each timer */ 11 | volatile u32 *TIM [TIMER_NUMBER] = 12 | { (u32*)(0x40012C00) , 13 | (u32*)(0x40000000) , 14 | (u32*)(0x40000400) , 15 | (u32*)(0x40000800) 16 | }; 17 | 18 | /* Offset of each rigester */ 19 | #define CR1 0 20 | #define CR2 1 21 | #define SMCR 2 22 | #define DIER 3 23 | #define SR 4 24 | #define EGR 5 25 | #define CCMR1 6 26 | #define CCMR2 7 27 | #define CCER 8 28 | #define CNT 9 29 | #define PSC 10 30 | #define ARR 11 31 | #define RCR 12 32 | #define CCR1 13 33 | #define CCR2 14 34 | #define CCR3 15 35 | #define CCR4 16 36 | #define BDTR 17 37 | #define DCR 18 38 | #define DMAR 19 39 | 40 | 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /02 - MCAL/07 - TIMER/TIMER_program.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 10/9/2020 */ 5 | /*****************************************************/ 6 | 7 | /* Library includes */ 8 | #include "BIT_MATH.h" 9 | #include "STD_TYPES.h" 10 | 11 | /* module includes */ 12 | #include "TIMER_interface.h" 13 | #include "TIMER_config.h" 14 | #include "TIMER_private.h" 15 | 16 | /* Global variables */ 17 | extern const TIMER_config_t configTable[TIMER_NUMBER]; 18 | void (* callBackFunc[TIMER_NUMBER])(void); 19 | 20 | void TIMER_voidInit (void) 21 | { 22 | for (TIMER_channels_t i = 0 ; i < MAX_TIMERS ; i++){ 23 | /* Will only configure the time if enabled otherwise the rigseters 24 | will remain at the reset value */ 25 | if (configTable[i].u8Enable){ 26 | /* Disable timer */ 27 | CLEAR_BIT ( *(TIM[i]+CR1) , 0 ); 28 | 29 | /* choose mode */ 30 | switch (configTable[i].u8Mode) { 31 | case UP_COUNTER : 32 | CLEAR_BIT ( *(TIM[i]+CR1) , 4 ); 33 | break ; 34 | case DOWN_COUNTER : 35 | SET_BIT ( *(TIM[i]+CR1) , 4 ); 36 | break ; 37 | default : 38 | break; 39 | } 40 | 41 | /* Enable / disable interrupt for update */ 42 | /* Rest of interrups will be implemented later */ 43 | if (configTable[i].u8InterruptEnable){ 44 | SET_BIT ( (*(TIM[i]+DIER)) , 0 ); 45 | 46 | } 47 | else { 48 | CLEAR_BIT ( (*(TIM[i]+DIER)) , 0 ); 49 | } 50 | 51 | /* Configure prescaller */ 52 | (*(TIM[i]+PSC)) = (configTable[i].u16Prescaler - 1 ) ; 53 | 54 | /* Configure interval */ 55 | u32 loacl_u32Hold = (configTable[i].u32ClkSource / 1000000 ) * configTable[i].u32Interval ; 56 | loacl_u32Hold = loacl_u32Hold / configTable[i].u16Prescaler; 57 | if (loacl_u32Hold < 65536 ){ 58 | (*(TIM[i]+ARR)) = loacl_u32Hold ; 59 | } 60 | else { 61 | /* Error code */ 62 | } 63 | 64 | /* Start timer */ 65 | SET_BIT ( (*(TIM[i] +CR1)) , 0 ); 66 | } 67 | } 68 | } 69 | 70 | /****************** Timer basic functions **********************/ 71 | 72 | void TIMER_voidSetBusyWait (TIMER_channels_t copy_channel , u32 copy_u32TimeUS) 73 | { 74 | /* Stop timer */ 75 | CLEAR_BIT ( (*(TIM[copy_channel]+CR1)) , 0 ); 76 | 77 | /* Clear flag */ 78 | CLEAR_BIT ((*(TIM[copy_channel] +SR)) , 0); 79 | 80 | /* Configure interval */ 81 | u32 loacl_u32Hold = (configTable[copy_channel].u32ClkSource / 1000000 ) * copy_u32TimeUS ; 82 | loacl_u32Hold = loacl_u32Hold / configTable[copy_channel].u16Prescaler; 83 | if (loacl_u32Hold < 65536 ){ 84 | (*(TIM[copy_channel]+ARR)) = loacl_u32Hold ; 85 | (*(TIM[copy_channel]+CNT)) = 0 ; 86 | } 87 | else { 88 | /* Error code */ 89 | } 90 | 91 | /* Start timer */ 92 | SET_BIT ( (*(TIM[copy_channel] +CR1)) , 0 ); 93 | 94 | /* Wait for flag */ 95 | while (!(GIT_BIT ((*(TIM[copy_channel] +SR)) , 0))); 96 | 97 | /* Stop timer */ 98 | CLEAR_BIT ( (*(TIM[copy_channel]+CR1)) , 0 ); 99 | } 100 | 101 | 102 | void TIMER_voidSetPeriodic (TIMER_channels_t copy_channel , u32 copy_u32TimeUS , void (*func)(void)) 103 | { 104 | 105 | /* Stop timer */ 106 | CLEAR_BIT ( (*(TIM[copy_channel]+CR1)) , 0 ); 107 | 108 | /* Configure interval */ 109 | u32 loacl_u32Hold = (configTable[copy_channel].u32ClkSource / 1000000 ) * copy_u32TimeUS ; 110 | loacl_u32Hold = loacl_u32Hold / configTable[copy_channel].u16Prescaler; 111 | if (loacl_u32Hold < 65536 ){ 112 | (*(TIM[copy_channel]+ARR)) = loacl_u32Hold ; 113 | } 114 | else { 115 | /* Error code */ 116 | } 117 | 118 | /* Set call back function */ 119 | callBackFunc[copy_channel] = func ; 120 | 121 | /* Start timer */ 122 | SET_BIT ( (*(TIM[copy_channel] +CR1)) , 0 ); 123 | 124 | /* Enable interrupt */ 125 | SET_BIT ( (*(TIM[copy_channel]+DIER)) , 0 ); 126 | } 127 | 128 | 129 | void TIMER_voidDisableOverFlowInterrupt(TIMER_channels_t copy_channel ) 130 | { 131 | /* Disable interrupt */ 132 | CLEAR_BIT( (*(TIM[copy_channel]+DIER)) , 0 ); 133 | } 134 | 135 | 136 | 137 | /************** ISR handlers ******************/ 138 | 139 | void TIM2_IRQHandler (void){ 140 | /* Clear flag */ 141 | CLEAR_BIT ((*(TIM[1] +SR)) , 0); 142 | callBackFunc[1](); 143 | } 144 | void TIM3_IRQHandler (void){ 145 | /* Clear flag */ 146 | CLEAR_BIT ((*(TIM[2] +SR)) , 0); 147 | callBackFunc[2](); 148 | } 149 | void TIM4_IRQHandler (void){ 150 | /* Clear flag */ 151 | CLEAR_BIT ((*(TIM[3] +SR)) , 0); 152 | callBackFunc[3](); 153 | } 154 | -------------------------------------------------------------------------------- /02 - MCAL/08 - DMA/DMA_config.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v02 */ 4 | /* date : 14/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef DMA_CONFIG_H 7 | #define DMA_CONFIG_H 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | #endif -------------------------------------------------------------------------------- /02 - MCAL/08 - DMA/DMA_interface.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v02 */ 4 | /* date : 14/9/2020 */ 5 | /*****************************************************/ 6 | 7 | /************************************************************ 8 | Pre_conditions : 9 | 1 - Enable RCC for DMA module 10 | 2 - if interrupt used enabel correspending NVIC bit 11 | *************************************************************/ 12 | 13 | #ifndef DMA_INTERFACE_H 14 | #define DMA_INTERFACE_H 15 | 16 | /**** macros *****/ 17 | #define DMA_NT 2 18 | #define DMA_TC 1 19 | #define DMA_TE 0 20 | 21 | /************************ Public data types ***********************/ 22 | /* DMA channels */ 23 | typedef enum { 24 | DMA_CH1 , 25 | DMA_CH2 , 26 | DMA_CH3 , 27 | DMA_CH4 , 28 | DMA_CH5 , 29 | DMA_CH6 , 30 | DMA_CH7 31 | }DMA_Channels_t; 32 | 33 | /* DMA priority levels*/ 34 | typedef enum { 35 | LOW , 36 | MEDIUM , 37 | HIGH , 38 | VERY_HIGH 39 | }DMA_priorityLevels_t; 40 | 41 | /* DMA tranfer modes */ 42 | typedef enum { 43 | MEM2MEM , 44 | PER2MEM , 45 | MEM2PER , 46 | PER2PER 47 | }DMA_transferModes_t; 48 | 49 | /* DMA data size */ 50 | typedef enum { 51 | BIT_8 , 52 | BIT_16 , 53 | BIT_32 54 | }DMA_dataSize_t; 55 | 56 | /* DMA states */ 57 | typedef enum { 58 | DMA_DISABLE , 59 | DMA_ENABLE 60 | }DMA_states_t; 61 | 62 | 63 | /* Configuration parameters used with DMA_voidConfigChannel */ 64 | /* Note that : in all modes except for MEM2PER mode mem is considered distantion 65 | and per is considered source */ 66 | typedef struct { 67 | // Priority level for DMA channel refer to DMA_priorityLevels_t enum data type 68 | DMA_priorityLevels_t p_level ; 69 | 70 | // DMA transfer mode refer to DMA_transferModes_t enum data type 71 | DMA_transferModes_t tr_mode ; 72 | 73 | // Size of the memory elemnt considered distantion size in all modes except for 74 | // MEM2PER mode 75 | DMA_dataSize_t mem_size ; 76 | 77 | // Size of the peripheral elemnt considered source size in all modes except 78 | // for MEM2PER mode 79 | DMA_dataSize_t per_size ; 80 | 81 | // enable or disable incremnt for memory 82 | DMA_states_t mem_increment ; 83 | 84 | // enable or disable incremnt for peripheral 85 | DMA_states_t per_increment ; 86 | 87 | // enable or disabel circular mode 88 | DMA_states_t circular_mode ; 89 | 90 | // enable tranfer complete interrupt 91 | DMA_states_t TF_interrupt ; 92 | 93 | // enable half tranfer complete interrupt 94 | DMA_states_t HTF_interrupt; 95 | 96 | // enable error tranfer interrupt 97 | DMA_states_t ETF_interrupt; 98 | }DMA_configCh_t; 99 | 100 | /* Transfere parameters used with DMA_voidTransfer */ 101 | typedef struct{ 102 | // DMA transfer mode refer to DMA_transferModes_t enum data type 103 | DMA_transferModes_t tr_mode; 104 | 105 | // Source address 106 | u32 *u32SourceAdress; 107 | 108 | // Distenation address 109 | u32 *u32DistAddress; 110 | 111 | // Block size (if one element yoy must write 1 ) 112 | u16 u16BlockSize; 113 | }DMA_TransData_t; 114 | 115 | /******************** Prototypes ******************/ 116 | 117 | /* 118 | * DMA_voidConfigChannel - > Configure DMA channel 119 | * i/p : DMA_Channels_t / pinter to DMA_configCh_t 120 | */ 121 | void DMA_voidConfigChannel (DMA_Channels_t copy_ch , DMA_configCh_t *copy_config); 122 | 123 | /* 124 | * DMA_voidTransfer - > Sen data from onel location to another according 125 | to configuration 126 | * i/p : DMA_Channels_t / pinter to DMA_TransData_t 127 | */ 128 | void DMA_voidTransfer (DMA_Channels_t copy_ch , DMA_TransData_t *copy_trans); 129 | 130 | /* 131 | * DMA_voidSetHandler - > Set the ISR call back function 132 | * i/p : DMA_Channels_t / void function (void) 133 | */ 134 | void DMA_voidSetHandler (DMA_Channels_t copy_ch , void(*func)(void)); 135 | 136 | /* 137 | * DMA_u8GetStatus - > return status of DMA tranfere complete or error 138 | * i/p : DMA_Channels_t 139 | */ 140 | u8 DMA_u8GetStatus(DMA_Channels_t copy_ch); 141 | 142 | 143 | 144 | #endif 145 | -------------------------------------------------------------------------------- /02 - MCAL/08 - DMA/DMA_private.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v02 */ 4 | /* date : 14/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef DMA_PRIVATE_H 7 | #define DMA_PRIVATE_H 8 | 9 | /******** Rigester defintions *********/ 10 | typedef struct { 11 | volatile u32 CCR ; 12 | volatile u32 CNDTR ; 13 | volatile u32 CPAR ; 14 | volatile u32 CMAR ; 15 | volatile u32 RES ; 16 | }DMA_channel_t; 17 | 18 | typedef struct { 19 | volatile u32 ISR ; 20 | volatile u32 IFCR ; 21 | DMA_channel_t CH[7]; 22 | }DMA_t; 23 | 24 | #define DMA ((volatile DMA_t *) 0x40020000 ) 25 | 26 | /************ Rigester BITS ***********/ 27 | 28 | /***** ISR/IFCR *****/ 29 | #define DMA_GIF1 0 30 | #define DMA_TCIF1 1 31 | #define DMA_HTIF1 2 32 | #define DMA_TEIF1 3 33 | #define DMA_GIF2 4 34 | #define DMA_TCIF2 5 35 | #define DMA_HTIF2 6 36 | #define DMA_TEIF2 7 37 | 38 | #define DMA_GIF3 8 39 | #define DMA_TCIF3 9 40 | #define DMA_HTIF3 10 41 | #define DMA_TEIF3 11 42 | 43 | #define DMA_GIF4 12 44 | #define DMA_TCIF4 13 45 | #define DMA_HTIF4 14 46 | #define DMA_TEIF4 15 47 | 48 | #define DMA_GIF5 16 49 | #define DMA_TCIF5 17 50 | #define DMA_HTIF5 18 51 | #define DMA_TEIF5 19 52 | 53 | #define DMA_GIF6 20 54 | #define DMA_TCIF6 21 55 | #define DMA_HTIF6 22 56 | #define DMA_TEIF6 23 57 | 58 | #define DMA_GIF7 24 59 | #define DMA_TCIF7 25 60 | #define DMA_HTIF7 26 61 | #define DMA_TEIF7 27 62 | 63 | 64 | /***** CCR *****/ 65 | #define DMA_EN 0 66 | #define DMA_TCIE 1 67 | #define DMA_HTIE 2 68 | #define DMA_TEIE 3 69 | #define DMA_DIR 4 70 | #define DMA_CIRC 5 71 | #define DMA_PINC 6 72 | #define DMA_MINC 7 73 | #define DMA_PSIZE0 8 74 | #define DMA_PSIZE1 9 75 | #define DMA_MSIZE0 10 76 | #define DMA_MSIZE1 11 77 | #define DMA_PL0 12 78 | #define DMA_PL1 13 79 | #define DMA_MEM2MEM 14 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /02 - MCAL/08 - DMA/DMA_program.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v02 */ 4 | /* date : 14/9/2020 */ 5 | /*****************************************************/ 6 | /* Library includes */ 7 | #include "BIT_MATH.h" 8 | #include "STD_TYPES.h" 9 | 10 | /* Module includes */ 11 | #include "DMA_interface.h" 12 | #include "DMA_private.h" 13 | #include "DMA_config.h" 14 | 15 | /******** Global variables ********/ 16 | void (*callBackFunc[7])(void); 17 | 18 | void DMA_voidConfigChannel (DMA_Channels_t copy_ch , DMA_configCh_t *copy_config) 19 | { 20 | /* Disable channel and reset register */ 21 | DMA->CH[copy_ch].CCR = 0 ; 22 | 23 | /* Set channel priority */ 24 | DMA->CH[copy_ch].CCR |= ( copy_config -> p_level << DMA_PL0 ); 25 | 26 | /* Set timer mode */ 27 | switch (copy_config -> tr_mode){ 28 | case MEM2MEM : 29 | /* Enable MEM2MEM and make the peripheral rigster source */ 30 | SET_BIT (DMA->CH[copy_ch].CCR , DMA_MEM2MEM); 31 | CLEAR_BIT(DMA->CH[copy_ch].CCR , DMA_DIR); 32 | break; 33 | case PER2MEM : 34 | /* Peripheral rigster as source */ 35 | CLEAR_BIT(DMA->CH[copy_ch].CCR , DMA_DIR); 36 | break; 37 | case MEM2PER : 38 | /* Memory rigster as source */ 39 | SET_BIT (DMA->CH[copy_ch].CCR , DMA_DIR); 40 | break; 41 | case PER2PER : 42 | /* Peripheral rigster as source */ 43 | CLEAR_BIT(DMA->CH[copy_ch].CCR , DMA_DIR); 44 | break; 45 | default : /* Should not be here */ break; 46 | } 47 | 48 | /* Set memroy elemnt size */ 49 | DMA->CH[copy_ch].CCR |= ( copy_config -> mem_size << DMA_MSIZE0 ); 50 | 51 | /* Set peripheral elemnt size */ 52 | DMA->CH[copy_ch].CCR |= ( copy_config -> per_size << DMA_PSIZE0 ); 53 | 54 | /* Memory address increment */ 55 | DMA->CH[copy_ch].CCR |= ( copy_config -> mem_increment << DMA_MINC ); 56 | 57 | /* Peripheral address increment */ 58 | DMA->CH[copy_ch].CCR |= ( copy_config -> per_increment << DMA_PINC ); 59 | 60 | /* Circular mode */ 61 | DMA->CH[copy_ch].CCR |= ( copy_config -> circular_mode << DMA_CIRC ); 62 | 63 | /* Interrupst mangement */ 64 | DMA->CH[copy_ch].CCR |= ( copy_config -> TF_interrupt << DMA_TCIE ); 65 | DMA->CH[copy_ch].CCR |= ( copy_config -> HTF_interrupt << DMA_HTIE ); 66 | DMA->CH[copy_ch].CCR |= ( copy_config -> ETF_interrupt << DMA_TEIE ); 67 | } 68 | 69 | void DMA_voidTransfer (DMA_Channels_t copy_ch , DMA_TransData_t *copy_trans) 70 | { 71 | /* Disable channel */ 72 | CLEAR_BIT(DMA->CH[copy_ch].CCR , DMA_EN); 73 | 74 | /* Set source and distination */ 75 | if (copy_trans ->tr_mode == MEM2PER ){ 76 | /* Set MEM as source address */ 77 | DMA->CH[copy_ch].CMAR = copy_trans -> u32SourceAdress ; 78 | /* Set PER as distination address */ 79 | DMA->CH[copy_ch].CPAR = copy_trans -> u32DistAddress ; 80 | } 81 | else { 82 | /* Set MEM as source address */ 83 | DMA->CH[copy_ch].CPAR = copy_trans -> u32SourceAdress ; 84 | /* Set PER as distination address */ 85 | DMA->CH[copy_ch].CMAR = copy_trans -> u32DistAddress ; 86 | } 87 | 88 | /* Configure block size */ 89 | DMA->CH[copy_ch].CNDTR = copy_trans -> u16BlockSize ; 90 | 91 | /* Enable channel */ 92 | SET_BIT(DMA->CH[copy_ch].CCR , DMA_EN); 93 | } 94 | 95 | void DMA_voidSetHandler (DMA_Channels_t copy_ch , void(*func)(void)) 96 | { 97 | callBackFunc[copy_ch] = func ; 98 | } 99 | 100 | u8 DMA_u8GetStatus(DMA_Channels_t copy_ch) 101 | { 102 | u8 local_u8TC = DMA_TCIF1 + (copy_ch * 4 ); 103 | u8 local_u8TE = DMA_TEIF1 + (copy_ch * 4 ); 104 | u8 local_u8State ; 105 | if (GIT_BIT (DMA-> ISR , local_u8TC)){ 106 | local_u8State = DMA_TC ; 107 | } 108 | else if (GIT_BIT (DMA-> ISR , local_u8TE)){ 109 | local_u8State = DMA_TE ; 110 | } 111 | else { 112 | local_u8State = DMA_NT ; 113 | } 114 | return local_u8State ; 115 | } 116 | 117 | /****************** ISR handlers ************************/ 118 | 119 | void DMA1_Channel1_IRQHandler (void) 120 | { 121 | callBackFunc[0](); 122 | /* Clear flags */ 123 | DMA -> IFCR = ( 15 << DMA_GIF1); 124 | } 125 | 126 | void DMA1_Channel2_IRQHandler (void) 127 | { 128 | callBackFunc[1](); 129 | /* Clear flags */ 130 | DMA -> IFCR = ( 15 << DMA_GIF2); 131 | } 132 | 133 | void DMA1_Channel3_IRQHandler (void) 134 | { 135 | callBackFunc[2](); 136 | /* Clear flags */ 137 | DMA -> IFCR = ( 15 << DMA_GIF3); 138 | } 139 | 140 | void DMA1_Channel4_IRQHandler (void) 141 | { 142 | callBackFunc[3](); 143 | /* Clear flags */ 144 | DMA -> IFCR = ( 15 << DMA_GIF4); 145 | } 146 | 147 | void DMA1_Channel5_IRQHandler (void) 148 | { 149 | callBackFunc[4](); 150 | /* Clear flags */ 151 | DMA -> IFCR = ( 15 << DMA_GIF5); 152 | } 153 | 154 | void DMA1_Channel6_IRQHandler (void) 155 | { 156 | callBackFunc[5](); 157 | /* Clear flags */ 158 | DMA -> IFCR = ( 15 << DMA_GIF6); 159 | } 160 | 161 | void DMA1_Channel7_IRQHandler (void) 162 | { 163 | callBackFunc[6](); 164 | /* Clear flags */ 165 | DMA -> IFCR = ( 15 << DMA_GIF7); 166 | } 167 | 168 | 169 | 170 | 171 | 172 | 173 | -------------------------------------------------------------------------------- /02 - MCAL/09 - SPI/SPI_config.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 14/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef SPI_CONFIG_H 7 | #define SPI_CONFIG_H 8 | 9 | 10 | /* Max number of SPI channels */ 11 | #define SPI_MAX_CH 2 12 | 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /02 - MCAL/09 - SPI/SPI_interface.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 16/9/2020 */ 5 | /*****************************************************/ 6 | /* 7 | Pre conditions : 8 | 1 - Enable RCC for used GPIO pins 9 | 2 - Enable RCC for use SPI 10 | 3 - Configure pins of SPI as output and input 11 | 4 - Enable NVIC in case of using interrupts 12 | */ 13 | 14 | 15 | #ifndef SPI_INTERFACE_H 16 | #define SPI_INTERFACE_H 17 | 18 | /******************* Public data types *******************/ 19 | 20 | /**** Timer channels ****/ 21 | typedef enum { 22 | SPI1, 23 | SPI2, 24 | MAX_SPIS 25 | }SPI_Ch_t; 26 | 27 | 28 | /*** Configuration parameters ***/ 29 | typedef struct { 30 | /* 1 for master / 0 for slave */ 31 | u8 u8Mode; 32 | 33 | /* 1 Idle is high / 0 Idle is low */ 34 | u8 u8CPOL ; 35 | 36 | /* 1 Read first / 0 Write first */ 37 | u8 u8CPHA ; 38 | 39 | /* 1 for LSB first / 0 for MSB first */ 40 | u8 u8FrameFormat ; 41 | 42 | /* 1 for 16-bit / 0 for 8-bit */ 43 | u8 u8FrameSize ; 44 | 45 | /* 1 S.W mange slave / 0 H.W mange slave */ 46 | u8 u8SSM ; 47 | 48 | /* 1 for Rexieve only / 0 for Full duplex */ 49 | u8 u8TransmissonMode ; 50 | 51 | /* (F / 2 ) -> 0 */ 52 | /* (F / 4 ) -> 1 */ 53 | /* (F / 8 ) -> 2 */ 54 | /* (F / 16 ) -> 3 */ 55 | /* (F / 32 ) -> 4 */ 56 | /* (F / 64 ) -> 5 */ 57 | /* (F / 128 ) -> 6 */ 58 | /* (F / 256 ) -> 7 */ 59 | u8 u8Prescaler; 60 | 61 | /* 1 for Enable / 0 for Disable */ 62 | u8 u8InterruptEnable ; 63 | }SPI_config_t; 64 | 65 | 66 | /********************* Prototypes **********************/ 67 | 68 | /* 69 | * SPI_u8ConfigureCh - > Configure specific SPI module refere to SPI_config_t data type 70 | * I/P : SPI_Ch_t / pointer to SPI_config_t 71 | * O/P : u8 (Error state) 72 | */ 73 | u8 SPI_u8ConfigureCh(SPI_Ch_t copy_channel , SPI_config_t *copy_config); 74 | 75 | /* 76 | * SPI_voidSendRecSynch - > Send and receive data by specific SPI module (Synchronous function) 77 | * I/P : SPI_Ch_t / u16 / pointer to u16 78 | * Pre condition : Activate SS pin of slave 79 | */ 80 | u16 SPI_voidSendRecSynch (SPI_Ch_t copy_channel , u16 copy_u16SendData); 81 | 82 | /* 83 | * SPI_voidSendRecAsynch - > Send and receive data by specific SPI module (Asynchronous function) 84 | * I/P : SPI_Ch_t / u16 / pointer to function (u16) 85 | * Pre condition : Activate SS pin of slave 86 | */ 87 | void SPI_voidSendRecAsynch (SPI_Ch_t copy_channel , u16 copy_u16SendData , void (*func)(u16)); 88 | 89 | #endif 90 | -------------------------------------------------------------------------------- /02 - MCAL/09 - SPI/SPI_private.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 16/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef SPI_PRIVATE_H 7 | #define SPI_PRIVATE_H 8 | 9 | /*************** Regsiters defintions **************/ 10 | volatile u32 *SPI[SPI_MAX_CH] = { 11 | (u32 *) (0x40013000) , 12 | (u32 *) (0x40003800) 13 | }; 14 | 15 | /** Offsets **/ 16 | #define CR1 0 17 | #define CR2 1 18 | #define SR 2 19 | #define DR 3 20 | #define CRCPR 4 21 | #define RXCRCR 5 22 | #define TXCRCR 6 23 | #define I2SCFGR 7 24 | #define I2SPR 8 25 | 26 | /************* Bits difention **************/ 27 | 28 | /*** CR1 ****/ 29 | #define CPHA 0 30 | #define CPOL 1 31 | #define MSTR 2 32 | #define BR0 3 33 | #define SPE 6 34 | #define LSBFIRST 7 35 | #define SS1 8 36 | #define SSM 9 37 | #define RXONLY 10 38 | #define DFF 11 39 | 40 | /*** CR2 ***/ 41 | #define TXEIE 7 42 | #define RXNEIE 6 43 | #define ERRIE 5 44 | #define SSOE 2 45 | 46 | /*** SR ***/ 47 | #define RXNE 0 48 | #define TXE 1 49 | #define CHSIDE 2 50 | #define UDR 3 51 | #define CRCERR 4 52 | #define MODF 5 53 | #define OVR 6 54 | #define BSY 7 55 | 56 | 57 | 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /02 - MCAL/09 - SPI/SPI_program.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 14/9/2020 */ 5 | /*****************************************************/ 6 | /* Library includes */ 7 | #include "BIT_MATH.h" 8 | #include "STD_TYPES.h" 9 | 10 | /* Module includes */ 11 | #include "SPI_interface.h" 12 | #include "SPI_config.h" 13 | #include "SPI_private.h" 14 | 15 | /* Global variables */ 16 | void (*callBackFunc[SPI_MAX_CH])(u16); 17 | 18 | /****** Public fucntions *******/ 19 | 20 | u8 SPI_u8ConfigureCh(SPI_Ch_t copy_channel , SPI_config_t *copy_config) 21 | { 22 | u8 local_u8ErrorCode = 0 ; 23 | 24 | /* Intialize the register */ 25 | (*(SPI[copy_channel] + CR1 )) = 0; 26 | 27 | 28 | /* Configure polarity in in idle */ 29 | switch (copy_config -> u8CPOL){ 30 | case 1 : 31 | SET_BIT ((*(SPI[copy_channel] + CR1 )) , CPOL); 32 | break ; 33 | case 0 : 34 | CLEAR_BIT((*(SPI[copy_channel] + CR1 )) , CPOL); 35 | break; 36 | default : 37 | local_u8ErrorCode = 1 ; 38 | break ; 39 | } 40 | 41 | /* Configure phase */ 42 | switch (copy_config -> u8CPHA){ 43 | case 1 : 44 | SET_BIT ((*(SPI[copy_channel] + CR1 )) , CPHA); 45 | break ; 46 | case 0 : 47 | CLEAR_BIT((*(SPI[copy_channel] + CR1 )) , CPHA); 48 | break; 49 | default : 50 | local_u8ErrorCode = 1 ; 51 | break ; 52 | } 53 | 54 | /* Configure Frame format MSB or LSB first */ 55 | switch (copy_config -> u8FrameFormat){ 56 | case 1 : 57 | SET_BIT ((*(SPI[copy_channel] + CR1 )) , LSBFIRST); 58 | break ; 59 | case 0 : 60 | CLEAR_BIT((*(SPI[copy_channel] + CR1 )) , LSBFIRST); 61 | break; 62 | default : 63 | local_u8ErrorCode = 1 ; 64 | break ; 65 | } 66 | 67 | /* Configure Frame size 8 or 16 bit */ 68 | switch (copy_config -> u8FrameSize){ 69 | case 1 : 70 | SET_BIT ((*(SPI[copy_channel] + CR1 )) , DFF); 71 | break ; 72 | case 0 : 73 | CLEAR_BIT((*(SPI[copy_channel] + CR1 )) , DFF); 74 | break; 75 | default : 76 | local_u8ErrorCode = 1 ; 77 | break ; 78 | } 79 | 80 | /* Enable or disable S.W slave mangement */ 81 | switch (copy_config -> u8SSM){ 82 | case 1 : 83 | SET_BIT ((*(SPI[copy_channel] + CR1 )) , SSM); 84 | SET_BIT ((*(SPI[copy_channel] + CR1 )) , SS1); 85 | break ; 86 | case 0 : 87 | CLEAR_BIT((*(SPI[copy_channel] + CR1 )) , SSM); 88 | break; 89 | default : 90 | local_u8ErrorCode = 1 ; 91 | break ; 92 | } 93 | 94 | /* Full duplex or Receive only */ 95 | switch (copy_config -> u8TransmissonMode){ 96 | case 1 : 97 | SET_BIT ((*(SPI[copy_channel] + CR1 )) , RXONLY); 98 | break ; 99 | case 0 : 100 | CLEAR_BIT((*(SPI[copy_channel] + CR1 )) , RXONLY); 101 | break; 102 | default : 103 | local_u8ErrorCode = 1 ; 104 | break ; 105 | } 106 | 107 | /* Prescaller */ 108 | if (copy_config -> u8Prescaler < 8 ) 109 | { 110 | (*(SPI[copy_channel] + CR1 )) |= (copy_config -> u8Prescaler << BR0); 111 | } 112 | else 113 | { 114 | local_u8ErrorCode = 1 ; 115 | } 116 | 117 | /* Enable or Disable interrupt */ 118 | switch (copy_config -> u8InterruptEnable){ 119 | case 1 : 120 | SET_BIT ((*(SPI[copy_channel] + CR2 )) , RXNEIE); 121 | break ; 122 | case 0 : 123 | CLEAR_BIT((*(SPI[copy_channel] + CR2 )) , RXNEIE); 124 | break; 125 | default : 126 | local_u8ErrorCode = 1 ; 127 | break ; 128 | } 129 | 130 | /* Configure mode master or slave */ 131 | switch (copy_config -> u8Mode){ 132 | case 1 : 133 | SET_BIT ((*(SPI[copy_channel] + CR1 )) , MSTR); 134 | break ; 135 | case 0 : 136 | CLEAR_BIT((*(SPI[copy_channel] + CR1 )) , MSTR); 137 | break; 138 | default : 139 | local_u8ErrorCode = 1 ; 140 | break ; 141 | } 142 | 143 | /* Enable SPI */ 144 | SET_BIT ((*(SPI[copy_channel] + CR1 )) , SPE); 145 | 146 | /* return error state */ 147 | return local_u8ErrorCode ; 148 | } 149 | 150 | 151 | u16 SPI_voidSendRecSynch (SPI_Ch_t copy_channel , u16 copy_u16SendData) 152 | { 153 | /* Send data */ 154 | (*(SPI[copy_channel] + DR )) = copy_u16SendData ; 155 | 156 | /* Wait till finish transmision */ 157 | while (GIT_BIT ( (*(SPI[copy_channel] + SR )) , BSY) == 1) ; 158 | 159 | /* Return data data */ 160 | return ( (*(SPI[copy_channel] + DR )) ) ; 161 | } 162 | 163 | void SPI_voidSendRecAsynch (SPI_Ch_t copy_channel , u16 copy_u16SendData , void (*func)(u16)) 164 | { 165 | /* Assign call back function */ 166 | callBackFunc[copy_channel] = func ; 167 | 168 | /* Send data */ 169 | (*(SPI[copy_channel] + DR )) = copy_u16SendData ; 170 | } 171 | 172 | /***************** ISR handlers ******************/ 173 | void SPI1_IRQHandler (void){ 174 | /* clear falg */ 175 | CLEAR_BIT ((*(SPI[0] + SR )) , RXNE ); 176 | /* Receive data */ 177 | u16 local_u16RecData = (*(SPI[0] + DR )) ; 178 | /* call function*/ 179 | callBackFunc[0](local_u16RecData); 180 | } 181 | 182 | void SPI2_IRQHandler (void){ 183 | /* clear falg */ 184 | CLEAR_BIT ((*(SPI[1] + SR )) ,RXNE ); 185 | /* Receive data */ 186 | u16 local_u16RecData = (*(SPI[0] + DR )) ; 187 | /* call function*/ 188 | callBackFunc[1](local_u16RecData); 189 | } 190 | 191 | 192 | -------------------------------------------------------------------------------- /02 - MCAL/10 - UART/UART_config.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 20/9/2020 */ 5 | /*****************************************************/ 6 | 7 | #include "STD_TYPES.h" 8 | 9 | #include "UART_config.h" 10 | 11 | UART_config_t UART_configTable [UART_MAX_CH] = { 12 | // Module Date parity Rec / trans Bus baudrate 13 | // enable bits mode enable clk 14 | {UART_ENABLE , DATA_8BIT , NO_PARITY , ENABLE_BOTH , APB2_CLK , 9600}, 15 | {UART_DISABLE , DATA_8BIT , NO_PARITY , ENABLE_BOTH , APB1_CLK , 9600}, 16 | {UART_DISABLE , DATA_8BIT , NO_PARITY , ENABLE_BOTH , APB1_CLK , 9600} 17 | }; 18 | -------------------------------------------------------------------------------- /02 - MCAL/10 - UART/UART_config.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 20/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef UART_CONFIG_H 7 | #define UART_CONFIG_H 8 | 9 | 10 | /* Max number of UART channels */ 11 | #define UART_MAX_CH 3 12 | 13 | #define APB1_CLK 8000000 14 | #define APB2_CLK 8000000 15 | 16 | /********* Configuration data ********/ 17 | typedef enum { 18 | UART_ENABLE , 19 | UART_DISABLE 20 | }State_t; 21 | 22 | typedef enum { 23 | DATA_8BIT , 24 | DATA_9BIT 25 | }WordLeng_t; 26 | 27 | typedef enum { 28 | EVEN_PARITY , 29 | ODD_PARITY , 30 | NO_PARITY 31 | }ParityMode_t; 32 | 33 | typedef enum { 34 | ENABLE_RECEIVER , 35 | ENABLE_TRANSMITTER, 36 | ENABLE_BOTH 37 | }RTControl_t; 38 | 39 | /* Configuration table data type */ 40 | typedef struct { 41 | State_t moduleEnable ; 42 | WordLeng_t wordLenght; 43 | ParityMode_t parityMode ; 44 | RTControl_t RTControl ; 45 | u32 u32BusFreq ; 46 | u32 u32Baudrate ; 47 | }UART_config_t; 48 | 49 | 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /02 - MCAL/10 - UART/UART_interface.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 20/9/2020 */ 5 | /*****************************************************/ 6 | 7 | /*********************************************************** 8 | * Pre conitions : 9 | 1 - Enable RCC for the used UART module 10 | 2 - Configure RX and TX pins as GPIO pins 11 | RX -> input floating 12 | TX -> Alternative output 13 | 3 - populate configuration table in config.c 14 | ************************************************************/ 15 | 16 | #ifndef UART_INTERFACE_H 17 | #define UART_INTERFACE_H 18 | 19 | /******************* Public data types *******************/ 20 | 21 | /**** Timer channels ****/ 22 | typedef enum { 23 | UART1, 24 | UART2, 25 | UART3, 26 | MAX_TIMERS 27 | }UART_Ch_t; 28 | 29 | /********************* Prototypes **********************/ 30 | 31 | /* 32 | * UART_voidInit - > Intialize UART module 33 | */ 34 | void UART_voidInit(void); 35 | 36 | /* 37 | * UART_voidSendDataSynch - > send a character or string to uart 38 | * I/P : UART_Ch_t / pointer to (u8) 39 | */ 40 | void UART_voidSendDataSynch(UART_Ch_t copy_ch , u8 *copy_u8data); 41 | 42 | /* 43 | * UART_voidRecDataSynch - > receive a character or string to uart 44 | * I/P : UART_Ch_t / pointer to (u8) / (u8) length of data 45 | */ 46 | void UART_voidRecDataSynch (UART_Ch_t copy_ch , u8 *copy_u8data , u8 copy_u8DataLength) 47 | 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /02 - MCAL/10 - UART/UART_private.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 20/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef UART_PRIVATE_H 7 | #define UART_PRIVATE_H 8 | 9 | /*************** Regsiters defintions **************/ 10 | volatile u32 *UART[UART_MAX_CH] = { 11 | (u32 *) (0x40013800) , //APB2 12 | (u32 *) (0x40004400) , //APB1 13 | (u32 *) (0x40004800) //APB1 14 | }; 15 | 16 | /*** Registers offsets ***/ 17 | #define SR 0 18 | #define DR 1 19 | #define BBR 2 20 | #define CR1 3 21 | #define CR2 4 22 | #define CR3 5 23 | #define GTPR 6 24 | 25 | 26 | /************ Registers bits defintion ***************/ 27 | 28 | /** CR1 **/ 29 | #define SBK 0 30 | #define RWU 1 31 | #define RE 2 32 | #define TE 3 33 | #define IDLEIE 4 34 | #define RXNEIE 5 35 | #define TCIE 6 36 | #define TXEIE 7 37 | #define PEIE 8 38 | #define PS 9 39 | #define PCE 10 40 | #define WAKE 11 41 | #define M 12 42 | #define UE 13 43 | 44 | /** CR2 **/ 45 | #define ADD0 0 46 | #define LBDL 5 47 | #define LBCL 8 48 | #define CPHA 9 49 | #define CPOL 10 50 | #define CLKEN 11 51 | #define STOP 12 52 | #define LINEN 13 53 | 54 | /** CR3 **/ 55 | 56 | #define EIE 0 57 | #define IREN 1 58 | #define IRLP 2 59 | #define HDSEL 3 60 | #define NACK 4 61 | #define SCEN 5 62 | #define DMAR 6 63 | #define DMAT 7 64 | #define RTSE 8 65 | #define CTSE 9 66 | #define CTSIE 10 67 | 68 | 69 | /****/ 70 | extern UART_config_t UART_configTable [UART_MAX_CH]; 71 | 72 | 73 | 74 | 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /02 - MCAL/10 - UART/UART_program.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 20/9/2020 */ 5 | /*****************************************************/ 6 | /* Library includes */ 7 | #include "BIT_MATH.h" 8 | #include "STD_TYPES.h" 9 | 10 | /* Module includes */ 11 | #include "UART_interface.h" 12 | #include "UART_config.h" 13 | #include "UART_private.h" 14 | 15 | 16 | 17 | void UART_voidInit(void) 18 | { 19 | UART_Ch_t local_u8Counter = 0; 20 | // To initiaize all the channels 21 | for (local_u8Counter = 0 ; local_u8Counter < MAX_TIMERS ; local_u8Counter++){ 22 | // To only configure the enabled uart modules 23 | if (UART_configTable [local_u8Counter].moduleEnable == UART_ENABLE){ 24 | 25 | /* Configure word length */ 26 | switch (UART_configTable [local_u8Counter].wordLenght){ 27 | case DATA_8BIT : 28 | CLEAR_BIT ((*(UART[local_u8Counter]+CR1)) , M); 29 | break ; 30 | case DATA_9BIT : 31 | SET_BIT ((*(UART[local_u8Counter]+CR1)) , M); 32 | break ; 33 | default : /* Should not be here */ break; 34 | } 35 | 36 | /* Configure parity mode */ 37 | switch (UART_configTable [local_u8Counter].parityMode){ 38 | case EVEN_PARITY : 39 | SET_BIT ((*(UART[local_u8Counter]+CR1)) , PCE); // enable parity control 40 | CLEAR_BIT ((*(UART[local_u8Counter]+CR1)) , PS); // choose even parity 41 | break ; 42 | case ODD_PARITY : 43 | SET_BIT ((*(UART[local_u8Counter]+CR1)) , PCE); // enable parity control 44 | SET_BIT ((*(UART[local_u8Counter]+CR1)) , PS); // choose odd parity 45 | break ; 46 | case NO_PARITY : 47 | CLEAR_BIT ((*(UART[local_u8Counter]+CR1)) , PCE); // disable parity control 48 | break ; 49 | default : /* Should not be here */ break; 50 | } 51 | 52 | /* Configure R / T enable and disable */ 53 | switch (UART_configTable [local_u8Counter].parityMode){ 54 | case ENABLE_RECEIVER : 55 | SET_BIT ((*(UART[local_u8Counter]+CR1)) , RE); 56 | break ; 57 | case ENABLE_TRANSMITTER : 58 | SET_BIT ((*(UART[local_u8Counter]+CR1)) , TE); 59 | break ; 60 | case ENABLE_BOTH : 61 | SET_BIT ((*(UART[local_u8Counter]+CR1)) , RE); 62 | SET_BIT ((*(UART[local_u8Counter]+CR1)) , TE); 63 | break ; 64 | default : /* Should not be here */ break; 65 | } 66 | 67 | /* Configure baud rate */ 68 | u32 local_u32BRRValue = UART_configTable [local_u8Counter].u32BusFreq / (16 * UART_configTable [local_u8Counter].u32Baudrate); 69 | *(UART[local_u8Counter]+BBR) = (local_u32BRRValue << 4) ; 70 | 71 | /* Enable module */ 72 | SET_BIT((*(UART[local_u8Counter]+CR1)) , UE); 73 | 74 | /* Intialize status rigeter */ 75 | *(UART[local_u8Counter]+SR) = 0 ; 76 | } 77 | } 78 | } 79 | 80 | 81 | void UART_voidSendDataSynch(UART_Ch_t copy_ch , u8 *copy_u8data) 82 | { 83 | while (*copy_u8data){ 84 | *(UART[copy_ch]+DR) = *copy_u8data ; 85 | while (GIT_BIT ((*(UART[copy_ch]+SR)) , 6) == 0); 86 | copy_u8data++; 87 | } 88 | } 89 | 90 | void UART_voidRecDataSynch (UART_Ch_t copy_ch , u8 *copy_u8data , u8 copy_u8DataLength) 91 | { 92 | 93 | while (copy_u8DataLength--){ 94 | while (GIT_BIT ((*(UART[copy_ch]+SR)) , 5) == 0); 95 | *copy_u8data = *(UART[copy_ch]+DR); 96 | copy_u8data++; 97 | } 98 | } 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /02 - MCAL/11 - FPEC/FPEC_config.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 9/10/2020 */ 5 | /*****************************************************/ 6 | #ifndef FPEC_CONFIG_H 7 | #define FPEC_CONFIG_H 8 | 9 | 10 | 11 | 12 | 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /02 - MCAL/11 - FPEC/FPEC_interface.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 9/10/2020 */ 5 | /*****************************************************/ 6 | 7 | 8 | #ifndef FPEC_INTERFACE_H 9 | #define FPEC_INTERFACE_H 10 | 11 | #define WRITE_DONE 1 12 | #define WRITE_ERROR 0 13 | 14 | 15 | /* 16 | * FPEC_u8WriteHalfWord - > write half word at specific location 17 | * I/P : (u32) the address , (u16) data 18 | * O/P : u8 (Error state) 19 | */ 20 | u8 FPEC_u8WriteHalfWord( u32 copy_u32Address, u16 copy_u16Data); 21 | 22 | /* 23 | * FPEC_voidErasePage - > Erase a page 24 | * I/P : (u32) the address 25 | */ 26 | void FPEC_voidErasePage( u32 copy_u32Address ); 27 | 28 | /* 29 | * FPEC_voidErasePage - > Erase a the whole user pages 30 | */ 31 | void FPEC_voidMassErase(void); 32 | 33 | 34 | 35 | 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /02 - MCAL/11 - FPEC/FPEC_private.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 9/10/2020 */ 5 | /*****************************************************/ 6 | #ifndef FPEC_PRIVATE_H 7 | #define FPEC_PRIVATE_H 8 | 9 | /** Register defintions **/ 10 | typedef struct { 11 | volatile u32 FLASH_ACR ; 12 | volatile u32 FLASH_KEYR ; 13 | volatile u32 FLASH_OPTKEYR ; 14 | volatile u32 FLASH_SR ; 15 | volatile u32 FLASH_CR ; 16 | volatile u32 FLASH_AR ; 17 | volatile u32 RESERVED ; 18 | volatile u32 FLASH_OBR ; 19 | volatile u32 FLASH_WRPR ; 20 | }FPEC_t ; 21 | 22 | #define FPEC ((FPEC_t *)0x40022000) 23 | 24 | /** Register bits **/ 25 | 26 | // FLASH_SR 27 | #define BSY 0 28 | #define PGERR 2 29 | #define WRPRTERR 4 30 | #define EOP 5 31 | 32 | // FLASH_CR 33 | #define PG 0 34 | #define PER 1 35 | #define MER 2 36 | #define OPTPG 4 37 | #define OPTER 5 38 | #define STRT 6 39 | #define LOCK 7 40 | #define OPTWRE 9 41 | #define ERRIE 10 42 | #define EOPIE 12 43 | 44 | // Lock keys 45 | #define FPEC_KEY1 0x45670123 46 | #define FPEC_KEY2 0xCDEF89AB 47 | #define RDPRT_key 0x00A5 48 | 49 | 50 | 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /02 - MCAL/11 - FPEC/FPEC_program.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 9/10/2020 */ 5 | /*****************************************************/ 6 | /* Library includes */ 7 | #include "BIT_MATH.h" 8 | #include "STD_TYPES.h" 9 | 10 | /* Module includes */ 11 | #include "FPEC_interface.h" 12 | #include "FPEC_config.h" 13 | #include "FPEC_private.h" 14 | 15 | u8 FPEC_u8WriteHalfWord( u32 copy_u32Address, u16 copy_u16Data) 16 | { 17 | u8 local_u8Error ; 18 | /* Check on the lock bit */ 19 | if (GIT_BIT ((FPEC -> FLASH_CR ), LOCK )){ 20 | /* if FPEC locked perform a lock sequence */ 21 | FPEC -> FLASH_KEYR = FPEC_KEY1 ; 22 | FPEC -> FLASH_KEYR = FPEC_KEY2 ; 23 | } 24 | 25 | /* Enable program */ 26 | SET_BIT ((FPEC -> FLASH_CR ), PG ); 27 | 28 | /* Write byte at the desired place */ 29 | (*(u16 *)copy_u32Address) = copy_u16Data ; 30 | 31 | /* Wait till the wirte is done */ 32 | while (GIT_BIT ((FPEC -> FLASH_SR ), BSY )); 33 | 34 | /* Disable PG */ 35 | CLEAR_BIT ((FPEC -> FLASH_CR ), PG ); 36 | 37 | /* Check on Data */ 38 | if ((*(u16 *)copy_u32Address) == copy_u16Data){ 39 | local_u8Error = WRITE_DONE ; 40 | } 41 | else { 42 | local_u8Error = WRITE_ERROR ; 43 | } 44 | 45 | return local_u8Error ; 46 | } 47 | 48 | void FPEC_voidErasePage( u32 copy_u32Address ) 49 | { 50 | /* Check on the lock bit */ 51 | if (GIT_BIT ((FPEC -> FLASH_CR ), LOCK )){ 52 | /* if FPEC locked perform a lock sequence */ 53 | FPEC -> FLASH_KEYR = FPEC_KEY1 ; 54 | FPEC -> FLASH_KEYR = FPEC_KEY2 ; 55 | } 56 | 57 | /* Enable erase for page */ 58 | SET_BIT ((FPEC -> FLASH_CR ), PER ); 59 | 60 | /* Set Address for page */ 61 | FPEC -> FLASH_AR = copy_u32Address ; 62 | 63 | /* Start erase */ 64 | SET_BIT ((FPEC -> FLASH_CR ), STRT ); 65 | 66 | /* Wait till the erase is done */ 67 | while (GIT_BIT ((FPEC -> FLASH_SR ), BSY )); 68 | 69 | /* Disable erase for page */ 70 | CLEAR_BIT ((FPEC -> FLASH_CR ), PER ); 71 | 72 | } 73 | 74 | void FPEC_voidMassErase(void) 75 | { 76 | /* Check on the lock bit */ 77 | if (GIT_BIT ((FPEC -> FLASH_CR ), LOCK )){ 78 | /* if FPEC locked perform a lock sequence */ 79 | FPEC -> FLASH_KEYR = FPEC_KEY1 ; 80 | FPEC -> FLASH_KEYR = FPEC_KEY2 ; 81 | } 82 | 83 | /* Enable erase for page */ 84 | SET_BIT ((FPEC -> FLASH_CR ), MER ); 85 | 86 | /* Start erase */ 87 | SET_BIT ((FPEC -> FLASH_CR ), STRT ); 88 | 89 | /* Wait till the erase is done */ 90 | while (GIT_BIT ((FPEC -> FLASH_SR ), BSY )); 91 | 92 | } 93 | 94 | 95 | -------------------------------------------------------------------------------- /03 - HAL/01 - LED/LED_config.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 15/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef LED_CONFIG_H 7 | #define LED_CONFIG_H 8 | 9 | 10 | /* 11 | Options : 12 | ACTIVE_HIGH 13 | ACTIVE_LOW 14 | */ 15 | #define LED_OPERATION ACTIVE_HIGH 16 | 17 | /* 18 | Options : 19 | GPIO_OUTPUT_2MHZ_PP 20 | GPIO_OUTPUT_10MHZ_PP 21 | GPIO_OUTPUT_50MHZ_PP 22 | */ 23 | #define LED_OUTPUT_MODE GPIO_OUTPUT_10MHZ_PP 24 | 25 | #endif -------------------------------------------------------------------------------- /03 - HAL/01 - LED/LED_interface.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 15/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef LED_INTERFACE_H 7 | #define LED_INTERFACE_H 8 | 9 | /* A struct type to hold the LED port and pin 10 | The struck is like an id to the LED as you can make than one LED 11 | at differnt pins and ports by passing the struct to the function */ 12 | typedef struct { 13 | GPIO_portId_t LED_portId ; 14 | GPIO_pinsNumbers_t LED_pinNumber ; 15 | }LED_id_t; 16 | 17 | /* LED states */ 18 | typedef enum { 19 | ON , 20 | OFF 21 | }LED_states_t; 22 | 23 | /* 24 | * LED_voidInit - > initialize the led 25 | * i/p : struct have the port and the pin (SSD_selectID_t) 26 | */ 27 | void LED_voidInit(LED_id_t copyID); 28 | 29 | /* 30 | * LED_voidSetState - > Set led state on or off 31 | * i/p : struct have the port and the start pin (SSD_selectID_t) / (LED_states_t) ON , OFF 32 | */ 33 | void LED_voidSetState (LED_id_t copyID , LED_states_t copyState); 34 | 35 | /* 36 | * LED_voidToggle - > toggle led state 37 | * i/p : struct have the port and the start pin (SSD_selectID_t) / delay of toggle (u16) 38 | */ 39 | void LED_voidToggle (LED_id_t copyID); 40 | 41 | 42 | #endif -------------------------------------------------------------------------------- /03 - HAL/01 - LED/LED_private.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 15/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef LED_PRIVATE_H 7 | #define LED_PRIVATE_H 8 | 9 | #define ACTIVE_HIGH 1 10 | #define ACTIVE_LOW 0 11 | 12 | 13 | #endif -------------------------------------------------------------------------------- /03 - HAL/01 - LED/LED_program.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 15/8/2020 */ 5 | /*****************************************************/ 6 | /* Library includes */ 7 | #include "STD_TYPES.h" 8 | #include "BIT_MATH.h" 9 | 10 | /* MCAL includes */ 11 | #include "GPIO_interface.h" 12 | #include "RCC_interface.h" 13 | 14 | /* Specific module includes */ 15 | #include "LED_interface.h" 16 | #include "LED_private.h" 17 | #include "LED_config.h" 18 | 19 | #if LED_OPERATION == ACTIVE_HIGH 20 | #define LED_SET_ON 1 21 | #define LED_SET_OFF 0 22 | #elif LED_OPERATION == ACTIVE_LOW 23 | #define LED_SET_ON 0 24 | #define LED_SET_OFF 1 25 | #endif 26 | 27 | void LED_voidInit(LED_id_t copyID){ 28 | /* RCC enable clock for port */ 29 | RCC_voidEnableClock(RCC_APB2 , (copyID.LED_portId + IOPA_PERIPHERAL) ); 30 | /* Setting pin as output */ 31 | GPIO_voidSetPinMode(copyID.LED_portId , copyID.LED_pinNumber , LED_OUTPUT_MODE); 32 | /* Initialize the LED off */ 33 | GPIO_voidsetPinValue(copyID.LED_portId , copyID.LED_pinNumber , LED_SET_OFF ); 34 | 35 | } 36 | 37 | void LED_voidSetState (LED_id_t copyID , LED_states_t copyState){ 38 | switch (copyState){ 39 | case ON : 40 | GPIO_voidsetPinValue(copyID.LED_portId , copyID.LED_pinNumber , LED_SET_ON ); 41 | break ; 42 | case OFF : 43 | GPIO_voidsetPinValue(copyID.LED_portId , copyID.LED_pinNumber , LED_SET_OFF ); 44 | break ; 45 | default : /* Should not be here */ break ; 46 | } 47 | } 48 | 49 | void LED_voidToggle (LED_id_t copyID){ 50 | 51 | GPIO_voidTogglePinValue(copyID.LED_portId , copyID.LED_pinNumber); 52 | 53 | } -------------------------------------------------------------------------------- /03 - HAL/02 - SSD/SSD_config.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 14/8/2020 */ 5 | /*****************************************************/ 6 | /* 7 | * The SSD module will be in the HAL layer and dependant on RCC and GPIO modules in the MCAL 8 | * the module has three basic functions one to intialize the SSD you can intialize more than one 9 | * SSD each one has an ID which is a struct ( SSD_selectID_t ) a function to write to one SSD 10 | * and a function to write to a set of multiplexing SSD 11 | * these features can be controlled for the desired Application by the configuration paramteres in 12 | * this file . 13 | * how to configure : 14 | * - > first you can choose what output mode you need in ( SSD_OUTPUT_MODE ) 15 | * - > choose the the SSD type from ( SSD_TYPE ) 16 | * - > if you are not using multiplexing write 0 or 1 to ( SSD_MULTIPLEXING_NUMBER ) 17 | * - > in case of multiplexing write number of SSD in ( SSD_MULTIPLEXING_NUMBER ) up to 4 18 | * - > you can choose the port of selection pins and which pins and also the delay time between switching the SSD 19 | * - > if a pin is not used write ( NOT_USED ) 20 | 21 | */ 22 | 23 | #ifndef SSD_CONFIG_H 24 | #define SSD_CONFIG_H 25 | 26 | /* 27 | Options : 28 | GPIO_OUTPUT_10MHZ_PP 29 | GPIO_OUTPUT_2MHZ_PP 30 | GPIO_OUTPUT_50MHZ_PP 31 | */ 32 | #define SSD_OUTPUT_MODE GPIO_OUTPUT_10MHZ_PP 33 | 34 | 35 | /* 36 | Options : 37 | COMMON_CATHOD 38 | COMMON_ANODE 39 | */ 40 | #define SSD_TYPE COMMON_CATHOD 41 | 42 | /* 43 | Options : 44 | 0 : 4 45 | */ 46 | #define SSD_MULTIPLEXING_NUMBER 2 47 | 48 | /* SSD select configuration pins */ 49 | #if SSD_MULTIPLEXING_NUMBER > 1 50 | #define SSD_DELAY 10 51 | #define SSD_SELECT_PORT PORTA 52 | #define SSD_RIGHT_SELECT PIN9 53 | #define SSD_MID1_SELECT PIN10 54 | #define SSD_MID2_SELECT NOT_USED 55 | #define SSD_LEFT_SELECT NOT_USED 56 | #endif 57 | 58 | 59 | 60 | #endif -------------------------------------------------------------------------------- /03 - HAL/02 - SSD/SSD_interface.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 14/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef SSD_INTERFACE_H 7 | #define SSD_INTERFACE_H 8 | 9 | /* A struct to type to hold the ssd port and start pin 10 | The struck is like an id to the ssd as you can make than one ssd 11 | at differnt pins and ports by passing the struct to the function */ 12 | typedef struct { 13 | GPIO_portId_t SSD_portId ; 14 | GPIO_pinsNumbers_t SSD_startPin ; 15 | }SSD_selectID_t; 16 | 17 | 18 | 19 | /* 20 | * SSD_voidInit - > Initialize the SSD 21 | * i/p : struct have the port and the start pin (SSD_selectID_t) 22 | */ 23 | void SSD_voidInit(SSD_selectID_t copy_id); 24 | 25 | /* 26 | * SSD_voidWriteNumber - > Write a number to the ssd 27 | * i/p : struct have the port and the start pin (SSD_selectID_t) / Number 0:9 / 28 | */ 29 | void SSD_voidWriteNumber (SSD_selectID_t copy_id , u8 copy_u8Number); 30 | 31 | /* 32 | * SSD_voidUpdateMultiplexing - > Write a number to the number of SSD multiplexing up to 4 (check SSD_config.h) 33 | * i/p : struct have the port and the start pin (SSD_selectID_t) / Number up to 4 digits / 34 | */ 35 | void SSD_voidUpdateMultiplexing (SSD_selectID_t copy_id , u16 copy_u16Number) ; 36 | #endif -------------------------------------------------------------------------------- /03 - HAL/02 - SSD/SSD_private.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 14/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef SSD_PRIVATE_H 7 | #define SSD_PRIVATE_H 8 | 9 | /* Seven segment types option */ 10 | #define COMMON_CATHOD 0 11 | #define COMMON_ANODE 1 12 | 13 | /* Used to the unsed pins in multiplexing mode*/ 14 | #define NOT_USED 16 15 | 16 | 17 | 18 | 19 | 20 | #endif -------------------------------------------------------------------------------- /03 - HAL/02 - SSD/SSD_program.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 14/8/2020 */ 5 | /*****************************************************/ 6 | 7 | /* Library includes */ 8 | #include "STD_TYPES.h" 9 | #include "BIT_MATH.h" 10 | #include "DELAY.h" 11 | 12 | /* MCAL includes */ 13 | #include "GPIO_interface.h" 14 | #include "RCC_interface.h" 15 | 16 | /* Specific module includes */ 17 | #include "SSD_interface.h" 18 | #include "SSD_private.h" 19 | #include "SSD_config.h" 20 | 21 | /* Initialize some parameters for the multiplexing mode*/ 22 | #if SSD_MULTIPLEXING_NUMBER > 1 23 | #if SSD_TYPE == COMMON_CATHOD 24 | #define SSD_SELCET_INIT_STATE HIGH 25 | #define SSD_SELCET_STATE LOW 26 | 27 | #elif SSD_TYPE == COMMON_ANODE 28 | #define SSD_SELCET_INIT_STATE LOW 29 | #define SSD_SELCET_STATE HIGH 30 | #endif 31 | const u8 SSD_selectPins[4] = {SSD_RIGHT_SELECT , SSD_MID1_SELECT , SSD_MID2_SELECT , SSD_LEFT_SELECT }; 32 | #endif 33 | 34 | 35 | 36 | /* public variables */ 37 | const u8 SSD_values[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F}; 38 | 39 | 40 | void SSD_voidInit(SSD_selectID_t copy_id){ 41 | // Enable the port clock 42 | RCC_voidEnableClock(RCC_APB2 , (copy_id.SSD_portId + IOPA_PERIPHERAL) ); 43 | // Making the pins output 44 | for (u8 i = copy_id.SSD_startPin ; i <= (copy_id.SSD_startPin+7) ; i++ ){ 45 | GPIO_voidSetPinMode(copy_id.SSD_portId , i , SSD_OUTPUT_MODE ); 46 | } 47 | // To initialize the selection pins 48 | #if SSD_MULTIPLEXING_NUMBER > 1 49 | // Enable the port clock 50 | RCC_voidEnableClock(RCC_APB2 , (SSD_SELECT_PORT + IOPA_PERIPHERAL) ); 51 | // configure the select pins as output and setting them to the initialize state 52 | for (u8 i = 0 ; i < SSD_MULTIPLEXING_NUMBER ; i++){ 53 | GPIO_voidSetPinMode (SSD_SELECT_PORT , SSD_selectPins[i] , SSD_OUTPUT_MODE ); 54 | GPIO_voidsetPinValue(SSD_SELECT_PORT , SSD_selectPins[i] , SSD_SELCET_INIT_STATE ); 55 | } 56 | #endif 57 | } 58 | 59 | void SSD_voidWriteNumber (SSD_selectID_t copy_id , u8 copy_u8Number){ 60 | /* select the ssd type */ 61 | #if SSD_TYPE == COMMON_CATHOD 62 | GPIO_voidWritePins (copy_id.SSD_portId , copy_id.SSD_startPin , PINS_7 , SSD_values[copy_u8Number] ); 63 | 64 | #elif SSD_TYPE == COMMON_ANODE 65 | GPIO_voidWritePins (copy_id.SSD_portId , copy_id.SSD_startPin , PINS_7 , ~ SSD_values[copy_u8Number] ); 66 | 67 | #endif 68 | } 69 | 70 | #if SSD_MULTIPLEXING_NUMBER > 1 71 | void SSD_voidUpdateMultiplexing (SSD_selectID_t copy_id , u16 copy_u16Number){ 72 | u8 number[SSD_MULTIPLEXING_NUMBER]; // store the number devided to ones 73 | u8 i = 0 ; 74 | if (copy_u16Number == 0 ) { 75 | for(u8 i = 0 ; i < SSD_MULTIPLEXING_NUMBER ; i ++){ 76 | number[i] = 0 ; 77 | } 78 | } 79 | /* Devide number to onse */ 80 | while (copy_u16Number){ 81 | number[i] = copy_u16Number % 10 ; 82 | copy_u16Number = copy_u16Number / 10 ; 83 | i++; 84 | } 85 | for(u8 i = 0 ; i < SSD_MULTIPLEXING_NUMBER ; i ++){ 86 | // Select the right SSD and write to it 87 | GPIO_voidsetPinValue(SSD_SELECT_PORT , SSD_selectPins[i] , SSD_SELCET_STATE ); 88 | SSD_voidWriteNumber(copy_id , number[i]); 89 | delay_ms(SSD_DELAY); 90 | GPIO_voidsetPinValue(SSD_SELECT_PORT , SSD_selectPins[i] , SSD_SELCET_INIT_STATE ); 91 | } 92 | 93 | } 94 | #endif 95 | 96 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /03 - HAL/03 - BUTTON/BUTTON_config.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 15/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef BUTTON_CONFIG_H 7 | #define BUTTON_CONFIG_H 8 | 9 | /* 10 | Options : 11 | INTERNAL_PULL_UP 12 | EXTERNAL_PULL_UP 13 | INTERNAL_PULL_DOWN 14 | EXTERNAL_PULL_DOWN 15 | */ 16 | 17 | #define BUTTON_PULL_TYPE INTERNAL_PULL_UP 18 | 19 | /* 20 | Options : 21 | 1 : max number of pin in MC 22 | */ 23 | #define BUTTON_NUMBER 2 24 | 25 | 26 | #endif -------------------------------------------------------------------------------- /03 - HAL/03 - BUTTON/BUTTON_interface.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 15/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef BUTTON_INTERFACE_H 7 | #define BUTTON_INTERFACE_H 8 | 9 | /* button states */ 10 | typedef enum{ 11 | PRE_PRESSED, 12 | PRESSED, 13 | PRE_RELEASED, 14 | RELEASED 15 | }BUTTON_state_t; 16 | 17 | 18 | /* 19 | * BUTTON_voidInit - > Initialize the button and add it to the set must be 20 | be called for every button 21 | * i/p : port id (PORTA - PORTB - PORTC) / pin number ( PIN0 -> PIN15) 22 | * o/p : the button number in the set used to get state of a button 23 | */ 24 | u8 BUTTON_voidInit(GPIO_portId_t copyPortId , GPIO_pinsNumbers_t copyPinNumber); 25 | 26 | /* 27 | * BUTTON_voidGetState - > check on the button it is a periodic function should be called every 20 ms 28 | (the period can change ) and after calling it check on state 29 | */ 30 | void BUTTON_voidScane(void); 31 | 32 | /* 33 | * BUTTON_voidGetState - > return the state of a specific button 34 | * i/p : button number in the set refer to function (BUTTON_voidInit) 35 | * o/p : state of the button (PRE_PRESSED - PRESSED - PRE_RELEASED - RELEASED) 36 | */ 37 | BUTTON_state_t BUTTON_voidGetState(u8 copy_u8Number); 38 | 39 | 40 | 41 | 42 | #endif -------------------------------------------------------------------------------- /03 - HAL/03 - BUTTON/BUTTON_private.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 15/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef BUTTON_PRIVATE_H 7 | #define BUTTON_PRIVATE_H 8 | 9 | #define INTERNAL_PULL_UP 0 10 | #define EXTERNAL_PULL_UP 1 11 | #define INTERNAL_PULL_DOWN 2 12 | #define EXTERNAL_PULL_DOWN 3 13 | 14 | 15 | #endif -------------------------------------------------------------------------------- /03 - HAL/03 - BUTTON/BUTTON_program.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 15/8/2020 */ 5 | /*****************************************************/ 6 | /* Library includes */ 7 | #include "STD_TYPES.h" 8 | #include "BIT_MATH.h" 9 | 10 | /* MCAL includes */ 11 | #include "GPIO_interface.h" 12 | #include "RCC_interface.h" 13 | 14 | /* Specific module includes */ 15 | #include "BUTTON_interface.h" 16 | #include "BUTTON_private.h" 17 | #include "BUTTON_config.h" 18 | 19 | /* check to detrmine the voltage check on pins */ 20 | #if (BUTTON_PULL_TYPE == INTERNAL_PULL_UP ) || (BUTTON_PULL_TYPE == EXTERNAL_PULL_UP ) 21 | #define BUTTON_PRESSED_VOLTAGE 0 22 | #define BUTTON_RELEASED_VOLTAGE 1 23 | 24 | #elif (BUTTON_PULL_TYPE == INTERNAL_PULL_DOWN) || (BUTTON_PULL_TYPE == EXTERNAL_PULL_DOWN) 25 | #define BUTTON_PRESSED_VOLTAGE 1 26 | #define BUTTON_RELEASED_VOLTAGE 0 27 | 28 | #else 29 | #error("Configuration error") 30 | #endif 31 | 32 | 33 | typedef struct { 34 | BUTTON_state_t state ; 35 | u8 samples[2]; 36 | }BUTTON_data_t; 37 | 38 | BUTTON_data_t button_info [BUTTON_NUMBER] ; 39 | GPIO_portId_t buttons_port[BUTTON_NUMBER] ; 40 | GPIO_pinsNumbers_t buttons_pins[BUTTON_NUMBER] ; 41 | 42 | 43 | 44 | u8 BUTTON_voidInit(GPIO_portId_t copyPortId , GPIO_pinsNumbers_t copyPinNumber){ 45 | static u8 i ; 46 | /* Enable RCC for the port*/ 47 | RCC_voidEnableClock(RCC_APB2 , (copyPortId + IOPA_PERIPHERAL) ); 48 | 49 | /* Intialize the pins as input */ 50 | #if BUTTON_PULL_TYPE == INTERNAL_PULL_UP 51 | GPIO_voidSetPinMode (copyPortId , copyPinNumber , GPIO_INPUT_PULL_UP_DOWN ); 52 | GPIO_voidsetPinValue(copyPortId , copyPinNumber , GPIO_PULL_UP); 53 | 54 | #elif BUTTON_PULL_TYPE == INTERNAL_PULL_DOWN 55 | GPIO_voidSetPinMode(copyPortId , copyPinNumber , GPIO_INPUT_PULL_UP_DOWN ); 56 | GPIO_voidsetPinValue(copyPortId , copyPinNumber , GPIO_PULL_DOWN); 57 | 58 | #elif (BUTTON_PULL_TYPE == EXTERNAL_PULL_UP) || (BUTTON_PULL_TYPE == EXTERNAL_PULL_DOWN) 59 | GPIO_voidSetPinMode(copyPortId , copyPinNumber , GPIO_INPUT_FLOATING ); 60 | 61 | #else 62 | #error("Configuration error") 63 | #endif 64 | 65 | //initial state 66 | button_info [i].state = RELEASED ; 67 | buttons_port[i] = copyPortId ; 68 | buttons_pins[i] = copyPinNumber ; 69 | i++ ; 70 | return (i - 1) ; 71 | } 72 | 73 | void BUTTON_voidScane(void){ 74 | for ( u8 i = 0 ; i < BUTTON_NUMBER ; i++){ 75 | button_info[i].samples[0] = button_info[i].samples[1] ; 76 | 77 | button_info[i].samples[1] = GPIO_u8GetPinValue(buttons_port[i] , buttons_pins[i]); 78 | 79 | switch(button_info[i].state){ 80 | case RELEASED: 81 | if((button_info[i].samples[0] == BUTTON_PRESSED_VOLTAGE) && 82 | (button_info[i].samples[1] == BUTTON_PRESSED_VOLTAGE)) 83 | { 84 | button_info[i].state = PRE_PRESSED; 85 | } 86 | break; 87 | 88 | case PRE_PRESSED: 89 | if(button_info[i].samples[1] == BUTTON_PRESSED_VOLTAGE) 90 | { 91 | button_info[i].state = PRESSED; 92 | } 93 | break; 94 | 95 | case PRESSED: 96 | if((button_info[i].samples[0] == BUTTON_RELEASED_VOLTAGE) && 97 | (button_info[i].samples[1] == BUTTON_RELEASED_VOLTAGE)) 98 | { 99 | button_info[i].state = PRE_RELEASED; 100 | } 101 | break; 102 | case PRE_RELEASED: 103 | if(button_info[i].samples[1] == BUTTON_RELEASED_VOLTAGE) 104 | { 105 | button_info[i].state = RELEASED; 106 | } 107 | break; 108 | default: 109 | /* Should not be here */ 110 | break; 111 | } 112 | } 113 | } 114 | 115 | BUTTON_state_t BUTTON_voidGetState(u8 copy_u8Number){ 116 | 117 | return button_info[copy_u8Number].state ; 118 | 119 | } -------------------------------------------------------------------------------- /03 - HAL/04 - LEDMRX/LEDMRX_config.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 31/8/2020 */ 5 | /*****************************************************/ 6 | #include "STD_TYPES.h" 7 | #include "LEDMRX_config.h" 8 | 9 | /* Port id */ 10 | #define PORTA 0 11 | #define PORTB 1 12 | #define PORTC 2 13 | #define PORTD 3 14 | 15 | /* pins number */ 16 | #define PIN0 0 17 | #define PIN1 1 18 | #define PIN2 2 19 | #define PIN3 3 20 | #define PIN4 4 21 | #define PIN5 5 22 | #define PIN6 6 23 | #define PIN7 7 24 | #define PIN8 8 25 | #define PIN9 9 26 | #define PIN10 10 27 | #define PIN11 11 28 | #define PIN12 12 29 | #define PIN13 13 30 | #define PIN14 14 31 | #define PIN15 15 32 | 33 | /* Rows mapping */ 34 | u8 LEDMRX_globalRows[(LEDMRX_ROWS_NUMBER * 2 )] = { 35 | PORTA,PIN0, 36 | PORTA,PIN1, 37 | PORTA,PIN2, 38 | PORTA,PIN3, 39 | PORTA,PIN4, 40 | PORTA,PIN5, 41 | PORTA,PIN6, 42 | PORTA,PIN7 43 | }; 44 | 45 | /* Coloumns mapping */ 46 | u8 LEDMRX_globalColoumns[(LEDMRX_COLOUMNS_NUMBER *2)] = { 47 | PORTB,PIN0, 48 | PORTB,PIN1, 49 | PORTB,PIN5, 50 | PORTB,PIN6, 51 | PORTB,PIN7, 52 | PORTB,PIN8, 53 | PORTB,PIN9, 54 | PORTB,PIN10 55 | }; 56 | 57 | -------------------------------------------------------------------------------- /03 - HAL/04 - LEDMRX/LEDMRX_config.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 31/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef LEDMRX_CONFIG_H 7 | #define LEDMRX_CONFIG_H 8 | 9 | 10 | /* Choode the number of coloumns and rows */ 11 | #define LEDMRX_ROWS_NUMBER 8 12 | #define LEDMRX_COLOUMNS_NUMBER 8 13 | 14 | /* Options: 15 | GPIO_OUTPUT_10MHZ_PP 16 | GPIO_OUTPUT_50MHZ_PP 17 | GPIO_OUTPUT_2MHZ_PP 18 | 19 | */ 20 | #define LEDMRX_OUTPUT_MODE GPIO_OUTPUT_10MHZ_PP 21 | 22 | /* 23 | Options: 0 OR 1 24 | */ 25 | #define LEDMRX_ROW_VOLTAGE 1 26 | #define LEDMRX_COLOUMN_VOLTAGE 0 27 | 28 | /* Determine the Frame rate */ 29 | #define LEDMRX_Frame_RATE 50 30 | 31 | 32 | 33 | #endif -------------------------------------------------------------------------------- /03 - HAL/04 - LEDMRX/LEDMRX_interface.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 31/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef LEDMRX_INTERFACE_H 7 | #define LEDMRX_INTERFACE_H 8 | 9 | 10 | /* 11 | * LEDMRX_voidInit - > Initialize the pins as output 12 | */ 13 | void LEDMRX_voidInit(void); 14 | 15 | /* 16 | * LEDMRX_voidDisplay - > Dispaly a figure on the display 17 | * I/P : a pointer to the address of the array of the figure 18 | */ 19 | void LEDMRX_voidDisplay (u8 *copy_u8Data); 20 | 21 | /* 22 | * LEDMRX_voidDisplayShiftText - > Dispaly a text and shift it 23 | * I/P : a pointer to the address of the array of the figure , shift time (u16) , text size (u8) 24 | */ 25 | void LEDMRX_voidDisplayShiftText (u8 *copy_u8Data , u16 copy_u8Time , u8 copy_u8TextSize); 26 | 27 | 28 | 29 | 30 | #endif -------------------------------------------------------------------------------- /03 - HAL/04 - LEDMRX/LEDMRX_private.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 31/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef LEDMRX_PRIVATE_H 7 | #define LEDMRX_PRIVATE_H 8 | 9 | 10 | /* contants */ 11 | #define LEDMRX_DELAY (1000000 / (LEDMRX_Frame_RATE * LEDMRX_ROWS_NUMBER) ) 12 | 13 | /* Configuration pin mapping */ 14 | extern u8 LEDMRX_globalRows[(LEDMRX_ROWS_NUMBER * 2 )]; 15 | extern u8 LEDMRX_globalColoumns[(LEDMRX_COLOUMNS_NUMBER*2)]; 16 | 17 | /* private functions prototypes*/ 18 | static void SetRowValues (u8 copy_u8Value); 19 | 20 | 21 | 22 | 23 | #endif -------------------------------------------------------------------------------- /03 - HAL/04 - LEDMRX/LEDMRX_program.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 31/8/2020 */ 5 | /*****************************************************/ 6 | 7 | #include "BIT_MATH.h" 8 | #include "STD_TYPES.h" 9 | 10 | /* MCAL includes */ 11 | #include "GPIO_interface.h" 12 | #include "STK_interface.h" 13 | 14 | /* module includes */ 15 | #include "LEDMRX_interface.h" 16 | #include "LEDMRX_config.h" 17 | #include "LEDMRX_private.h" 18 | 19 | 20 | 21 | void LEDMRX_voidInit(void) 22 | { 23 | /* Initialize the systick */ 24 | STK_voidInit(); 25 | 26 | /* Making the i/o pins for row output */ 27 | for (u8 i = 0 ; i < (LEDMRX_ROWS_NUMBER*2) ; i += 2){ 28 | GPIO_voidSetPinMode(LEDMRX_globalRows[i] , LEDMRX_globalRows[i+1], LEDMRX_OUTPUT_MODE); 29 | } 30 | 31 | /* Making the i/o pins for coloumns output */ 32 | for (u8 i = 0 ; i < (LEDMRX_COLOUMNS_NUMBER*2) ; i += 2){ 33 | GPIO_voidSetPinMode(LEDMRX_globalColoumns[i] , LEDMRX_globalColoumns[i+1], LEDMRX_OUTPUT_MODE); 34 | GPIO_voidsetPinValue(LEDMRX_globalColoumns[i] , LEDMRX_globalColoumns[i+1], !(LEDMRX_COLOUMN_VOLTAGE)); 35 | } 36 | 37 | } 38 | 39 | 40 | void LEDMRX_voidDisplay (u8 *copy_u8Data) 41 | { 42 | for (u8 i = 0 ;i < (LEDMRX_COLOUMNS_NUMBER*2) ; i += 2){ 43 | /* Activate a coloumn */ 44 | GPIO_voidsetPinValue(LEDMRX_globalColoumns[i] , LEDMRX_globalColoumns[i+1], LEDMRX_COLOUMN_VOLTAGE); 45 | /* Write to the rows */ 46 | SetRowValues(copy_u8Data[(i/2)]); 47 | /* Delay */ 48 | STK_voidSetBusyWait(LEDMRX_DELAY , TIME_US); 49 | /* Diactivate a coloumn */ 50 | GPIO_voidsetPinValue(LEDMRX_globalColoumns[i] , LEDMRX_globalColoumns[i+1], !(LEDMRX_COLOUMN_VOLTAGE)); 51 | } 52 | } 53 | 54 | void LEDMRX_voidDisplayShiftText (u8 *copy_u8Data , u16 copy_u8Time , u8 copy_u8TextSize) 55 | { 56 | /* calculate how much to loop in while */ 57 | copy_u8Time = copy_u8Time / (LEDMRX_COLOUMNS_NUMBER * (LEDMRX_DELAY /1000 )); 58 | /* Hold the time to update it after every shift */ 59 | u16 Local_u16Hold =copy_u8Time ; 60 | 61 | /* Looping through the text */ 62 | while(copy_u8TextSize--){ 63 | /* making delay betweem shifts */ 64 | while (copy_u8Time--){ 65 | LEDMRX_voidDisplay(copy_u8Data); 66 | } 67 | copy_u8Data++; 68 | copy_u8Time = Local_u16Hold; 69 | } 70 | } 71 | 72 | 73 | /*********** Private functions *************/ 74 | static void SetRowValues (u8 copy_u8Value) 75 | { 76 | u8 local_u8Bit = 0 ; 77 | for (u8 i = 0 ; i < (LEDMRX_COLOUMNS_NUMBER*2) ; i += 2){ 78 | local_u8Bit = GIT_BIT(copy_u8Value , (i / 2) ) ; 79 | GPIO_voidsetPinValue(LEDMRX_globalRows[i] , LEDMRX_globalRows[i+1] , local_u8Bit ); 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /03 - HAL/04 - LEDMRX_OS/LEDMRX_config.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 7/9/2020 */ 5 | /*****************************************************/ 6 | 7 | 8 | #include "STD_TYPES.h" 9 | 10 | #include "LEDMRX_config.h" 11 | 12 | /* Port id */ 13 | #define PORTA 0 14 | #define PORTB 1 15 | #define PORTC 2 16 | #define PORTD 3 17 | 18 | /* pins number */ 19 | #define PIN0 0 20 | #define PIN1 1 21 | #define PIN2 2 22 | #define PIN3 3 23 | #define PIN4 4 24 | #define PIN5 5 25 | #define PIN6 6 26 | #define PIN7 7 27 | #define PIN8 8 28 | #define PIN9 9 29 | #define PIN10 10 30 | #define PIN11 11 31 | #define PIN12 12 32 | #define PIN13 13 33 | #define PIN14 14 34 | #define PIN15 15 35 | 36 | /* Enter each pin in the rows as PORTx and PINx */ 37 | u8 LEDMRX_globalRows[(LEDMRX_ROWS_NUMBER * 2 )] = { 38 | PORTA,PIN0, 39 | PORTA,PIN1, 40 | PORTA,PIN2, 41 | PORTA,PIN3, 42 | PORTA,PIN4, 43 | PORTA,PIN5, 44 | PORTA,PIN6, 45 | PORTA,PIN7 46 | }; 47 | 48 | /* Enter each pin in the colomns as PORTx and PINx */ 49 | u8 LEDMRX_globalColoumns[(LEDMRX_COLOUMNS_NUMBER *2)] = { 50 | PORTB,PIN0, 51 | PORTB,PIN1, 52 | PORTB,PIN5, 53 | PORTB,PIN6, 54 | PORTB,PIN7, 55 | PORTB,PIN8, 56 | PORTB,PIN9, 57 | PORTB,PIN10 58 | }; 59 | 60 | -------------------------------------------------------------------------------- /03 - HAL/04 - LEDMRX_OS/LEDMRX_config.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 7/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef LEDMRX_CONFIG_H 7 | #define LEDMRX_CONFIG_H 8 | 9 | 10 | /* Choode the number of coloumns and rows */ 11 | #define LEDMRX_ROWS_NUMBER 8 12 | #define LEDMRX_COLOUMNS_NUMBER 8 13 | 14 | /* Options: 15 | GPIO_OUTPUT_10MHZ_PP 16 | GPIO_OUTPUT_50MHZ_PP 17 | GPIO_OUTPUT_2MHZ_PP 18 | 19 | */ 20 | #define LEDMRX_OUTPUT_MODE GPIO_OUTPUT_10MHZ_PP 21 | 22 | /* 23 | Options: 0 OR 1 24 | */ 25 | #define LEDMRX_ROW_VOLTAGE 1 26 | #define LEDMRX_COLOUMN_VOLTAGE 0 27 | 28 | 29 | 30 | 31 | 32 | #endif -------------------------------------------------------------------------------- /03 - HAL/04 - LEDMRX_OS/LEDMRX_interface.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 7/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef LEDMRX_INTERFACE_H 7 | #define LEDMRX_INTERFACE_H 8 | 9 | /***********************************************************/ 10 | /* Function Name : LEDMRX_voidInit 11 | * Function Type : Initialization 12 | * Parameters : void 13 | * Return : void 14 | * Discription : configure LED matrix display pins as output 15 | * Pre-conditions : RCC must be enabled for the GPIO ports used 16 | */ 17 | /************************************************************/ 18 | void LEDMRX_voidInit(void); 19 | 20 | /***********************************************************/ 21 | /* Function Name : LEDMRX_voidDisplay 22 | * Function Type : Periodic Task 23 | * Parameters : void 24 | * Return : void 25 | * Discription : show a certain frame on the display , frame 26 | is passed as an array to setter function 27 | * Pre-conditions : OS must be initialized first 28 | * Notes : Period of the task will determine the frame 29 | rate per seconed 30 | */ 31 | /************************************************************/ 32 | void LEDMRX_voidDisplay (void); 33 | 34 | 35 | /***********************************************************/ 36 | /* Function Name : LEDMRX_voidDisplayShiftText 37 | * Function Type : Periodic Task 38 | * Parameters : void 39 | * Return : void 40 | * Discription : Shift string on the display , frame is passed as 41 | an array to setter function 42 | * Pre-conditions : OS must be initialized first 43 | LEDMRX_voidDisplay Task must be activated 44 | * Notes : Period of the task will determine the delay 45 | between each shift 46 | */ 47 | /************************************************************/ 48 | void LEDMRX_voidDisplayShiftText (void); 49 | 50 | 51 | /***********************************************************/ 52 | /* Function Name : LEDMRX_voidSetData 53 | * Function Type : Setter 54 | * Parameters : Pointer to u8 55 | * Return : void 56 | * Discription : To pass frame for the task 57 | */ 58 | /************************************************************/ 59 | void LEDMRX_voidSetData (u8 *copy_u8Data); 60 | 61 | 62 | /***********************************************************/ 63 | /* Function Name : LEDMRX_voidSetStringLenght 64 | * Function Type : Setter 65 | * Parameters : (u8) Lenght of the string 66 | * Return : void 67 | * Discription : To pass string lenght 68 | */ 69 | /************************************************************/ 70 | void LEDMRX_voidSetStringLenght(u8 copy_u8Data); 71 | 72 | 73 | 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /03 - HAL/04 - LEDMRX_OS/LEDMRX_private.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 31/8/2020 */ 5 | /*****************************************************/ 6 | #ifndef LEDMRX_PRIVATE_H 7 | #define LEDMRX_PRIVATE_H 8 | 9 | 10 | 11 | 12 | /* Configuration pin mapping */ 13 | extern u8 LEDMRX_globalRows[(LEDMRX_ROWS_NUMBER * 2 )]; 14 | extern u8 LEDMRX_globalColoumns[(LEDMRX_COLOUMNS_NUMBER*2)]; 15 | 16 | /* private functions prototypes*/ 17 | static void SetRowValues (u8 copy_u8Value); 18 | 19 | 20 | 21 | 22 | #endif -------------------------------------------------------------------------------- /03 - HAL/04 - LEDMRX_OS/LEDMRX_program.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 7/9/2020 */ 5 | /*****************************************************/ 6 | 7 | /* Library includes */ 8 | #include "BIT_MATH.h" 9 | #include "STD_TYPES.h" 10 | 11 | /* MCAL includes */ 12 | #include "GPIO_interface.h" 13 | 14 | /* module includes */ 15 | #include "LEDMRX_interface.h" 16 | #include "LEDMRX_config.h" 17 | #include "LEDMRX_private.h" 18 | 19 | /*************** Global variables *****************/ 20 | static volatile u8 global_u8CurrentColoumn = 2 ; 21 | static volatile u8 global_u8PreviosColoumn = 0 ; 22 | static volatile u8 *global_u8Data ; 23 | static volatile u8 *global_u8StartData ; 24 | static volatile u8 *global_u8StringEnd ; 25 | 26 | /**************** Intialization ******************/ 27 | void LEDMRX_voidInit(void) 28 | { 29 | /* Making the i/o pins for row output */ 30 | for (u8 i = 0 ; i < (LEDMRX_ROWS_NUMBER*2) ; i += 2){ 31 | GPIO_voidSetPinMode(LEDMRX_globalRows[i] , LEDMRX_globalRows[i+1], LEDMRX_OUTPUT_MODE); 32 | } 33 | 34 | /* Making the i/o pins for coloumns output */ 35 | for (u8 i = 0 ; i < (LEDMRX_COLOUMNS_NUMBER*2) ; i += 2){ 36 | GPIO_voidSetPinMode(LEDMRX_globalColoumns[i] , LEDMRX_globalColoumns[i+1], LEDMRX_OUTPUT_MODE); 37 | GPIO_voidsetPinValue(LEDMRX_globalColoumns[i] , LEDMRX_globalColoumns[i+1], !(LEDMRX_COLOUMN_VOLTAGE)); 38 | } 39 | } 40 | 41 | /********************* Tasks *********************/ 42 | void LEDMRX_voidDisplay (void) 43 | { 44 | /* Diactivate a previous column so data will not be written to it */ 45 | GPIO_voidsetPinValue(LEDMRX_globalColoumns[global_u8PreviosColoumn] , LEDMRX_globalColoumns[global_u8PreviosColoumn+1], !(LEDMRX_COLOUMN_VOLTAGE)); 46 | /* Write to the rows */ 47 | SetRowValues(global_u8Data[(global_u8CurrentColoumn/2)]); 48 | /* Activate the current column to write to a specific row */ 49 | GPIO_voidsetPinValue(LEDMRX_globalColoumns[global_u8CurrentColoumn] , LEDMRX_globalColoumns[global_u8CurrentColoumn+1], LEDMRX_COLOUMN_VOLTAGE); 50 | /* Shift to another column */ 51 | global_u8CurrentColoumn += 2 ; 52 | global_u8PreviosColoumn += 2 ; 53 | /* Index reached the last colomn so will begin from zero again */ 54 | if ( global_u8CurrentColoumn == (LEDMRX_COLOUMNS_NUMBER *2) ){ 55 | global_u8CurrentColoumn = 0; 56 | } 57 | else if (global_u8PreviosColoumn == (LEDMRX_COLOUMNS_NUMBER *2)){ 58 | global_u8PreviosColoumn=0; 59 | } 60 | } 61 | 62 | void LEDMRX_voidDisplayShiftText (void) 63 | { 64 | /* While the pointer to data less than the end of string will inceremnt data pointer*/ 65 | if ((global_u8Data) < global_u8StringEnd) { 66 | global_u8Data++; 67 | } 68 | else{ 69 | global_u8Data = global_u8StartData; 70 | } 71 | } 72 | 73 | /****************** Setter functions ***********************/ 74 | void LEDMRX_voidSetData (u8 *copy_u8Data){ 75 | global_u8Data = copy_u8Data ; 76 | global_u8StartData=global_u8Data; 77 | } 78 | 79 | void LEDMRX_voidSetStringLenght(u8 copy_u8Data){ 80 | global_u8StringEnd = global_u8StartData+copy_u8Data ; 81 | } 82 | 83 | 84 | /*********** Private functions *************/ 85 | static void SetRowValues (u8 copy_u8Value) 86 | { 87 | u8 local_u8Bit = 0 ; 88 | for (u8 i = 0 ; i < (LEDMRX_COLOUMNS_NUMBER*2) ; i += 2){ 89 | local_u8Bit = GIT_BIT(copy_u8Value , (i / 2) ) ; 90 | GPIO_voidsetPinValue(LEDMRX_globalRows[i] , LEDMRX_globalRows[i+1] , local_u8Bit ); 91 | } 92 | 93 | } 94 | -------------------------------------------------------------------------------- /03 - HAL/05 - REMOTE/REMOTE_config.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 4/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef REMOTE_CONFIG_H 7 | #define REMOTE_CONFIG_H 8 | 9 | 10 | 11 | /* 12 | Options : 13 | 0 : 15 14 | */ 15 | #define REMOTE_EXTI_LINE 8 16 | 17 | /* 18 | Options : 19 | PORTA 20 | PORTB 21 | PORTC 22 | */ 23 | #define REMOTE_EXTI_PORT PORTA 24 | 25 | 26 | 27 | #endif -------------------------------------------------------------------------------- /03 - HAL/05 - REMOTE/REMOTE_interface.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 4/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef REMOTE_INTERFACE_H 7 | #define REMOTE_INTERFACE_H 8 | 9 | /* 10 | Notes should be taken when using driver 11 | 1 - open RCC for the port you used as EXTI for the reciever 12 | 2 - open RCC for AFIO 13 | 3 - Don't use systick in other applications 14 | 4 - call (REMOTE_voidInit) before while 15 | 5 - check the comming data from (REMOTE_getButton) function 16 | 6 - if data is less than (REMOTE_NO_DATA) then data is valid 17 | 18 | */ 19 | 20 | 21 | 22 | /* The buttons IDs*/ 23 | typedef enum { 24 | REMOTE_ZERO , 25 | REMOTE_ONE , 26 | REMOTE_TWO , 27 | REMOTE_THREE , 28 | REMOTE_FOUR , 29 | REMOTE_FIVE , 30 | REMOTE_SIX , 31 | REMOTE_SEVEN , 32 | REMOTE_EIGHT , 33 | REMOTE_NINE , 34 | REMOTE_RPT , 35 | REMOTE_U_SD , 36 | REMOTE_EQ , 37 | REMOTE_VOL_DOWN , 38 | REMOTE_VOL_UP , 39 | REMOTE_P_RESUME , 40 | REMOTE_RIGHT , 41 | REMOTE_LEFT , 42 | REMOTE_POWER , 43 | REMOTE_MODE , 44 | REMOTE_MUTE , 45 | REMOTE_NO_DATA , 46 | REMOTE_ERROR , 47 | }REMOTE_buttons_t; 48 | 49 | 50 | 51 | /* 52 | * REMOTE_voidInit - > Intialize the exti for remote 53 | */ 54 | void REMOTE_voidInit(void); 55 | 56 | 57 | /* 58 | * REMOTE_getButton - > get the button pressed 59 | * O/P : (REMOTE_buttons_t) the pressed button if no data came return (REMOTE_NO_DATA) 60 | if data was not right (REMOTE_ERROR) 61 | */ 62 | REMOTE_buttons_t REMOTE_getButton (void); 63 | 64 | 65 | 66 | 67 | #endif -------------------------------------------------------------------------------- /03 - HAL/05 - REMOTE/REMOTE_private.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 4/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef REMOTE_PRIVATE_H 7 | #define REMOTE_PRIVATE_H 8 | 9 | 10 | /* Setting up ISR number for NVIC and handler of the isr */ 11 | #if REMOTE_EXTI_LINE == 0 12 | #define REMOTE_HANDLER EXTI0_IRQHandler 13 | #define REMOTE_NVIC_NUMBER EXTI0_IRQ 14 | 15 | #elif REMOTE_EXTI_LINE == 1 16 | #define REMOTE_HANDLER EXTI1_IRQHandler 17 | #define REMOTE_NVIC_NUMBER EXTI1_IRQ 18 | 19 | #elif REMOTE_EXTI_LINE == 2 20 | #define REMOTE_HANDLER EXTI2_IRQHandler 21 | #define REMOTE_NVIC_NUMBER EXTI2_IRQ 22 | 23 | #elif REMOTE_EXTI_LINE == 3 24 | #define REMOTE_HANDLER EXTI3_IRQHandler 25 | #define REMOTE_NVIC_NUMBER EXTI3_IRQ 26 | 27 | #elif REMOTE_EXTI_LINE == 4 28 | #define REMOTE_HANDLER EXTI4_IRQHandler 29 | #define REMOTE_NVIC_NUMBER EXTI4_IRQ 30 | 31 | #elif (REMOTE_EXTI_LINE > 4) && (REMOTE_EXTI_LINE < 10) 32 | #define REMOTE_HANDLER EXTI9_5_IRQHandler 33 | #define REMOTE_NVIC_NUMBER EXTI9_5_IRQ 34 | 35 | #elif (REMOTE_EXTI_LINE > 9) && (REMOTE_EXTI_LINE < 16) 36 | #define REMOTE_HANDLER EXTI15_10_IRQHandler 37 | #define REMOTE_NVIC_NUMBER EXTI15_10_IRQ 38 | 39 | #else 40 | #error (" Configuration error ") 41 | #endif 42 | 43 | 44 | /* Frame bits */ 45 | #define ADDRESS_BIT 0 46 | #define ADDRESS_INVERTED_BIT 8 47 | #define DATA_BIT 16 48 | #define DATA_INVERTED_BIT 24 49 | 50 | /* Errors */ 51 | #define FRAME_CORRECT 1 52 | #define FRAME_ERROR 0 53 | 54 | /* Buttons data ID */ 55 | #define POWER 69 56 | #define MODE 70 57 | #define MUTE 71 58 | #define PLAY_RESUME 68 59 | #define RIGHT 64 60 | #define LEFT 67 61 | #define EQ 7 62 | #define VOLUME_DOWN 21 63 | #define VOLUME_UP 9 64 | #define ZERO 22 65 | #define RPT 25 66 | #define U_USD 13 67 | #define ONE 12 68 | #define TWO 24 69 | #define THREE 94 70 | #define FOUR 8 71 | #define FIVE 28 72 | #define SIX 90 73 | #define SEVEN 60 74 | #define EIGHT 82 75 | #define NINE 74 76 | 77 | /******* Private functions prototypes ******/ 78 | static u8 REMOTE_u8CheckFrame(void); 79 | 80 | 81 | 82 | #endif -------------------------------------------------------------------------------- /03 - HAL/05 - REMOTE/REMOTE_program.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 4/9/2020 */ 5 | /*****************************************************/ 6 | 7 | /* Library includes */ 8 | #include "BIT_MATH.h" 9 | #include "STD_TYPES.h" 10 | 11 | /* MCAL includes */ 12 | #include "GPIO_interface.h" 13 | #include "STK_interface.h" 14 | #include "EXTI_interface.h" 15 | #include "AFIO_interface.h" 16 | #include "NVIC_interface.h" 17 | 18 | /* module includes */ 19 | #include "REMOTE_interface.h" 20 | #include "REMOTE_config.h" 21 | #include "REMOTE_private.h" 22 | 23 | /****** Global variabled *******/ 24 | volatile u8 global_u8Flag = 0 ; 25 | volatile u8 global_u8FinishFlag = 0 ; 26 | volatile u32 global_u32Frame = 0 ; 27 | volatile u8 global_u8Index = 0 ; 28 | 29 | u32 global_u32Data = 0 ; 30 | u32 global_u32DataInverted = 0 ; 31 | u32 global_u32Address = 0 ; 32 | u32 global_u32AddressInverted = 0 ; 33 | 34 | 35 | void REMOTE_voidInit(void) 36 | { 37 | /* Making the pin INPUT */ 38 | GPIO_voidSetPinMode(REMOTE_EXTI_PORT , REMOTE_EXTI_LINE , GPIO_INPUT_FLOATING); 39 | 40 | /* Intialize EXTI */ 41 | NVIC_voidEnableIRQ(REMOTE_NVIC_NUMBER); 42 | EXTI_voidInitLineInterrupt(REMOTE_EXTI_LINE , EXTI_FALLING_EDGE); 43 | EXTI_voidEnableLineInterrupt(REMOTE_EXTI_LINE); 44 | 45 | /* Configure the AFIO */ 46 | AFIO_voidSelectPortForLine(REMOTE_EXTI_LINE ,REMOTE_EXTI_PORT); 47 | 48 | /* Systick initialize */ 49 | STK_voidInit(); 50 | } 51 | 52 | 53 | REMOTE_buttons_t REMOTE_getButton (void) 54 | { 55 | REMOTE_buttons_t local_button = REMOTE_NO_DATA ; 56 | if (global_u8FinishFlag){ 57 | u8 local_u8Check = REMOTE_u8CheckFrame(); 58 | // determine which button was pressed 59 | if (local_u8Check){ 60 | switch (global_u32Data){ 61 | case POWER : local_button = REMOTE_POWER; break; 62 | case MODE : local_button = REMOTE_MODE; break; 63 | case MUTE : local_button = REMOTE_MUTE; break; 64 | case PLAY_RESUME : local_button = REMOTE_P_RESUME ; break; 65 | case RIGHT : local_button = REMOTE_RIGHT; break; 66 | case LEFT : local_button = REMOTE_LEFT; break; 67 | case EQ : local_button = REMOTE_EQ ; break; 68 | case VOLUME_DOWN : local_button = REMOTE_VOL_DOWN; break; 69 | case VOLUME_UP : local_button = REMOTE_VOL_UP; break; 70 | case ZERO : local_button = REMOTE_ZERO ; break; 71 | case RPT : local_button = REMOTE_RPT; break; 72 | case U_USD : local_button = REMOTE_U_SD; break; 73 | case ONE : local_button = REMOTE_ONE ; break; 74 | case TWO : local_button = REMOTE_TWO ; break; 75 | case THREE : local_button = REMOTE_THREE; break; 76 | case FOUR : local_button = REMOTE_FOUR ; break; 77 | case FIVE : local_button = REMOTE_FIVE ; break; 78 | case SIX : local_button = REMOTE_SIX ; break; 79 | case SEVEN : local_button = REMOTE_SEVEN; break; 80 | case EIGHT : local_button = REMOTE_EIGHT; break; 81 | case NINE : local_button = REMOTE_NINE ; break; 82 | default : 83 | /* If it is here data is not correct*/ 84 | local_button = REMOTE_ERROR ; break; 85 | } 86 | } 87 | global_u8FinishFlag = 0 ; 88 | } 89 | return local_button ; 90 | } 91 | 92 | 93 | void REMOTE_HANDLER(void) { 94 | /* Clear flag */ 95 | EXTI_voidClearPendingFlag(REMOTE_EXTI_LINE); 96 | /* Get time elapsed*/ 97 | u32 time = STK_u32GetElapsedTime(TIME_US); 98 | /* Start timer to count till the next isr */ 99 | STK_voidStart(); 100 | /* Wait for the start bit before recording frame*/ 101 | if ((global_u8Flag == 0) && (time > 10000) && (time < 15000)){ 102 | global_u8Flag = 1 ; 103 | global_u8Index = 0 ; 104 | } 105 | /* Start bit came and will start recording the frame */ 106 | else if (global_u8Flag == 1){ 107 | /* If bit == 1 */ 108 | if ((time > 2000 ) && (time < 2400 )){ 109 | SET_BIT(global_u32Frame , global_u8Index ); 110 | global_u8Index++; 111 | } 112 | /* If bit == 0 */ 113 | else if ( (time > 1000 ) && (time < 1200 ) ){ 114 | CLEAR_BIT(global_u32Frame , global_u8Index ); 115 | global_u8Index++; 116 | } 117 | /* the Wait before the next frame */ 118 | else if ( (time > 20000 )){ 119 | global_u8Flag = 0 ; 120 | global_u8FinishFlag = 1 ; 121 | } 122 | else { 123 | 124 | } 125 | } 126 | } 127 | 128 | 129 | /***** Private functions ******/ 130 | static u8 REMOTE_u8CheckFrame(void) 131 | { 132 | u8 local_u8Error = FRAME_ERROR ; 133 | /* Get data inverted from the frame*/ 134 | global_u32DataInverted = (global_u32Frame & 0xff000000); 135 | global_u32DataInverted = (global_u32DataInverted >> DATA_INVERTED_BIT); 136 | 137 | /* Get data from the frame */ 138 | global_u32Data = (global_u32Frame & 0x00ff0000); 139 | global_u32Data = (global_u32Data >> DATA_BIT); 140 | 141 | /* Get Address inverted from the frame */ 142 | global_u32AddressInverted = (global_u32Frame & 0x0000ff00); 143 | global_u32AddressInverted = (global_u32AddressInverted >> ADDRESS_INVERTED_BIT); 144 | 145 | /* Get Address from the frame */ 146 | global_u32Address = (global_u32Address & 0x000000ff); 147 | 148 | /* Check the data for errors */ 149 | if ( (global_u32Data & global_u32DataInverted) | (global_u32Address & global_u32AddressInverted) ){ 150 | local_u8Error = FRAME_ERROR ; 151 | } 152 | else { 153 | local_u8Error = FRAME_CORRECT ; 154 | } 155 | 156 | return local_u8Error ; 157 | 158 | } 159 | 160 | 161 | -------------------------------------------------------------------------------- /03 - HAL/06 - AUDIO/AUDIO_config.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 8/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef AUDIO_CONFIG_H 7 | #define AUDIO_CONFIG_H 8 | 9 | /** Choose pins of the DAC **/ 10 | #define AUDIO_DAC0 PORTA,PIN0 11 | #define AUDIO_DAC1 PORTA,PIN1 12 | #define AUDIO_DAC2 PORTA,PIN2 13 | #define AUDIO_DAC3 PORTA,PIN3 14 | #define AUDIO_DAC4 PORTA,PIN4 15 | #define AUDIO_DAC5 PORTA,PIN5 16 | #define AUDIO_DAC6 PORTA,PIN6 17 | #define AUDIO_DAC7 PORTA,PIN7 18 | 19 | /* 20 | OPTIONS : 21 | GPIO_OUTPUT_10MHZ_PP 22 | GPIO_OUTPUT_50MHZ_PP 23 | GPIO_OUTPUT_2MHZ_PP 24 | */ 25 | #define AUDIO_DAC_OUTPUT_MODE GPIO_OUTPUT_10MHZ_PP 26 | 27 | 28 | 29 | 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /03 - HAL/06 - AUDIO/AUDIO_interface.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 8/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef AUDIO_INTERFACE_H 7 | #define AUDIO_INTERFACE_H 8 | 9 | 10 | /***********************************************************/ 11 | /* Function Name : AUDIO_voidInit 12 | * Function Type : Initialization 13 | * Parameters : void 14 | * Return : void 15 | * Discription : configure DAC pins as OUTPUT 16 | * Pre-conditions : RCC must be enabled for the GPIO ports used 17 | */ 18 | /************************************************************/ 19 | void AUDIO_voidInit (void); 20 | 21 | /***********************************************************/ 22 | /* Function Name : AUDIO_voidSetDAC 23 | * Function Type : Periodic Task 24 | * Parameters : void 25 | * Return : void 26 | * Description : Write one byte of the audio to the DAC port 27 | * Pre-conditions : OS must be initialized first 28 | * Notes : Period of the task will determine the audio 29 | frequency 30 | for human at least period must be 125 us 31 | */ 32 | /************************************************************/ 33 | void AUDIO_voidSetDAC (void); 34 | 35 | 36 | /***********************************************************/ 37 | /* Function Name : AUDIO_voidSetSong 38 | * Function Type : Setter 39 | * Parameters : pointer to u8 , length of array 40 | * Return : void 41 | * Description : set the array of the audio 42 | */ 43 | /************************************************************/ 44 | void AUDIO_voidSetSong(u8 *copy_u8Arr , u16 copy_u16SongLen); 45 | 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /03 - HAL/06 - AUDIO/AUDIO_private.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 8/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef AUDIO_PRIVATE_H 7 | #define AUDIO_PRIVATE_H 8 | 9 | 10 | 11 | 12 | 13 | 14 | #endif -------------------------------------------------------------------------------- /03 - HAL/06 - AUDIO/AUDIO_program.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 8/9/2020 */ 5 | /*****************************************************/ 6 | 7 | /* Library includes */ 8 | #include "BIT_MATH.h" 9 | #include "STD_TYPES.h" 10 | 11 | /* MCAL includes */ 12 | #include "GPIO_interface.h" 13 | 14 | /* module includes */ 15 | #include "AUDIO_interface.h" 16 | #include "AUDIO_config.h" 17 | #include "AUDIO_private.h" 18 | 19 | 20 | 21 | /****** Global variables *******/ 22 | u8 global_u8PinsMap[16] = {AUDIO_DAC0 , AUDIO_DAC1 , AUDIO_DAC2 , AUDIO_DAC3 , 23 | AUDIO_DAC4 , AUDIO_DAC5 , AUDIO_DAC6 , AUDIO_DAC7 }; 24 | u16 global_u16Index ; 25 | u16 global_u16SongLen ; 26 | u8 global_u8Arr ; 27 | 28 | /********* Initialization ********/ 29 | void AUDIO_voidInit (void) 30 | { 31 | for (u8 i = 0 ; i < 16 ; i += 2) 32 | { 33 | GPIO_voidSetPinMode(global_u8PinsMap[i] , global_u8PinsMap[i+1] , AUDIO_DAC_OUTPUT_MODE); 34 | } 35 | } 36 | 37 | /*********** Tasks ************/ 38 | void AUDIO_voidSetDAC (void){ 39 | GPIO_voidWritePins(AUDIO_DAC0 , PINS_8 , global_u8Arr[global_u16Index]); 40 | global_u16Index++; 41 | if (global_u16Index == global_u16SongLen){ 42 | global_u16Index = 0 ; 43 | } 44 | } 45 | 46 | 47 | /* Setter function */ 48 | void AUDIO_voidSetSong(u8 *copy_u8Arr , u16 copy_u16SongLen){ 49 | global_u8Arr = copy_u8Arr ; 50 | global_u16SongLen = copy_u16SongLen ; 51 | } 52 | 53 | -------------------------------------------------------------------------------- /03 - HAL/07 - STP/STP_config.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 14/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef STP_CONFIG_H 7 | #define STP_CONFIG_H 8 | 9 | /* 10 | Options : 11 | 12 | */ 13 | #define STP_PINS_OUTPUT_MODE GPIO_OUTPUT_2MHZ_PP 14 | 15 | /* Configuration pins */ 16 | 17 | #define STP_SERIAL_DATA PORTA,PIN0 18 | #define STP_SHIFT_CLK PORTA,PIN1 19 | #define STP_STORE_CLK PORTA,PIN2 20 | 21 | /* MSB of the parellel data 22 | if using one it will be 7 23 | if using two it will be 15 ....etc 24 | */ 25 | #define STP_MSP 15 26 | 27 | 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /03 - HAL/07 - STP/STP_interface.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 14/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef STP_INTERFACE_H 7 | #define STP_INTERFACE_H 8 | 9 | /* 10 | * STP_voidInit - > Initialize STP pins 11 | * i/p : void 12 | */ 13 | void STP_voidInit(void); 14 | 15 | /* 16 | * STP_voidInit - > write data 17 | * i/p : void 18 | */ 19 | void STP_voidSendSynch(u16 copy_u16DataToSend); 20 | 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /03 - HAL/07 - STP/STP_private.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 14/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef STP_PRIVATE_H 7 | #define STP_PRIVATE_H 8 | 9 | 10 | 11 | 12 | 13 | 14 | #endif -------------------------------------------------------------------------------- /03 - HAL/07 - STP/STP_program.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 14/9/2020 */ 5 | /*****************************************************/ 6 | 7 | /* Library includes */ 8 | #include "BIT_MATH.h" 9 | #include "STD_TYPES.h" 10 | 11 | /* MCAL includes */ 12 | #include "GPIO_interface.h" 13 | #include "STK_interface.h" 14 | 15 | /* module includes */ 16 | #include "STP_interface.h" 17 | #include "STP_config.h" 18 | #include "STP_private.h" 19 | 20 | void STP_voidInit(void) 21 | { 22 | GPIO_voidSetPinMode(STP_SERIAL_DATA , GPIO_OUTPUT_2MHZ_PP); 23 | GPIO_voidSetPinMode(STP_SHIFT_CLK , GPIO_OUTPUT_2MHZ_PP); 24 | GPIO_voidSetPinMode(STP_STORE_CLK , GPIO_OUTPUT_2MHZ_PP); 25 | } 26 | 27 | void STP_voidSendSynch(u16 copy_u16DataToSend) 28 | { 29 | s8 local_u8Counter ; 30 | u8 local_u8Bit ; 31 | for (local_u8Counter = STP_MSP ; local_u8Counter >= 0 ; local_u8Counter--){ 32 | /* Send to serial pin */ 33 | local_u8Bit = GIT_BIT(copy_u16DataToSend ,local_u8Counter); 34 | GPIO_voidsetPinValue (STP_SERIAL_DATA ,local_u8Bit); 35 | 36 | /* Send pulse to shift clk */ 37 | GPIO_voidsetPinValue (STP_SHIFT_CLK ,HIGH); 38 | STK_voidSetBusyWait(2 , TIME_US); 39 | GPIO_voidsetPinValue (STP_SHIFT_CLK ,LOW); 40 | STK_voidSetBusyWait(2 , TIME_US); 41 | } 42 | 43 | /* Send pulse to store */ 44 | GPIO_voidsetPinValue (STP_STORE_CLK ,HIGH); 45 | STK_voidSetBusyWait(2 , TIME_US); 46 | GPIO_voidsetPinValue (STP_STORE_CLK ,LOW); 47 | STK_voidSetBusyWait(2 , TIME_US); 48 | } 49 | 50 | 51 | -------------------------------------------------------------------------------- /03 - HAL/08 - TFT/TFT_config.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 21/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef TFT_CONFIG_H 7 | #define TFT_CONFIG_H 8 | 9 | 10 | /* Write pin in pair port,pin */ 11 | #define TFT_A0_PIN PORTA,1 // output pin 12 | #define TFT_RST_PIN PORTA,2 // reset pin 13 | 14 | /* Write pin in pair port,pin */ 15 | #define TFT_SLAVE_PIN PORTA,3 // slave select pin 16 | 17 | /* Configure TFT size */ 18 | #define TFT_MAX_X 127 19 | #define TFT_MAX_Y 159 20 | 21 | 22 | 23 | 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /03 - HAL/08 - TFT/TFT_interface.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 21/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef TFT_INTERFACE_H 7 | #define TFT_INTERFACE_H 8 | 9 | 10 | /* Colours */ 11 | #define TFT_BLACK 0x0000 12 | #define TFT_WHITE 0xffff 13 | #define TFT_RED 0xf800 14 | #define TFT_BLUE 0x39df 15 | #define TFT_YELLOW 0xffc0 16 | #define TFT_ORANGE 0xfd20 17 | #define TFT_PURPLE 0xf1df 18 | #define TFT_GREEN 0x1427 19 | 20 | 21 | void TFT_voidInit (void); 22 | 23 | void TFT_voidDisplayImage(const u16* copy_u16Image); 24 | 25 | void TFT_voidDrawRectangle (u16 copy_u16X ,u16 copy_u16Y , u16 copy_u16Width , u16 copy_u16Hight , u16 copy_u16Color ); 26 | 27 | void TFT_voidFillDisplay (u16 copy_u16Colour); 28 | 29 | void TFT_voidPrintChar(s8 copy_s8Char , u16 copy_u16X , u16 copy_u16Y, u8 copy_u8Size , u16 copy_u16Color, u16 copy_u16BackColor); 30 | 31 | void TFT_voidPrintText(s8 *copy_s8Text , u16 copy_u16X , u16 copy_u16Y, u8 copy_u8Size , u16 copy_u16Color, u16 copy_u16BackColor); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /03 - HAL/08 - TFT/TFT_private.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 21/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef TFT_PRIVATE_H 7 | #define TFT_PRIVATE_H 8 | 9 | 10 | #define TFT_CHARACTER_WIDTH 6 11 | #define TFT_CHARACTER_HIGHT 8 12 | 13 | static const u8 TFT_font[96][6] = { 14 | {0x00,0x00,0x00,0x00,0x00,0x00}, // 15 | {0x5c,0x00,0x00,0x00,0x00,0x00}, // ! 16 | {0x06,0x00,0x06,0x00,0x00,0x00}, // " 17 | {0x28,0x7c,0x28,0x7c,0x28,0x00}, // # 18 | {0x5c,0x54,0xfe,0x54,0x74,0x00}, // $ 19 | {0x44,0x20,0x10,0x08,0x44,0x00}, // % 20 | {0x28,0x54,0x54,0x20,0x50,0x00}, // & 21 | {0x06,0x00,0x00,0x00,0x00,0x00}, // ' 22 | {0x38,0x44,0x00,0x00,0x00,0x00}, // ( 23 | {0x44,0x38,0x00,0x00,0x00,0x00}, // ) 24 | {0x02,0x07,0x02,0x00,0x00,0x00}, // * 25 | {0x10,0x10,0x7c,0x10,0x10,0x00}, // + 26 | {0xc0,0x00,0x00,0x00,0x00,0x00}, // , 27 | {0x10,0x10,0x10,0x10,0x10,0x00}, // - 28 | {0x40,0x00,0x00,0x00,0x00,0x00}, // . 29 | {0x60,0x10,0x0c,0x00,0x00,0x00}, // / 30 | {0x7c,0x64,0x54,0x4c,0x7c,0x00}, // 0 31 | {0x48,0x7c,0x40,0x00,0x00,0x00}, // 1 32 | {0x64,0x54,0x54,0x54,0x48,0x00}, // 2 33 | {0x44,0x54,0x54,0x54,0x6c,0x00}, // 3 34 | {0x3c,0x20,0x70,0x20,0x20,0x00}, // 4 35 | {0x5c,0x54,0x54,0x54,0x24,0x00}, // 5 36 | {0x7c,0x54,0x54,0x54,0x74,0x00}, // 6 37 | {0x04,0x04,0x64,0x14,0x0c,0x00}, // 7 38 | {0x7c,0x54,0x54,0x54,0x7c,0x00}, // 8 39 | {0x5c,0x54,0x54,0x54,0x7c,0x00}, // 9 40 | {0x44,0x00,0x00,0x00,0x00,0x00}, // : 41 | {0xc4,0x00,0x00,0x00,0x00,0x00}, // ; 42 | {0x10,0x28,0x44,0x00,0x00,0x00}, // < 43 | {0x28,0x28,0x28,0x28,0x28,0x00}, // = 44 | {0x44,0x28,0x10,0x00,0x00,0x00}, // > 45 | {0x08,0x04,0x54,0x08,0x00,0x00}, // ? 46 | {0x7c,0x44,0x54,0x54,0x5c,0x00}, // @ 47 | {0x7c,0x24,0x24,0x24,0x7c,0x00}, // A 48 | {0x7c,0x54,0x54,0x54,0x6c,0x00}, // B 49 | {0x7c,0x44,0x44,0x44,0x44,0x00}, // C 50 | {0x7c,0x44,0x44,0x44,0x38,0x00}, // D 51 | {0x7c,0x54,0x54,0x54,0x44,0x00}, // E 52 | {0x7c,0x14,0x14,0x14,0x04,0x00}, // F 53 | {0x7c,0x44,0x44,0x54,0x74,0x00}, // G 54 | {0x7c,0x10,0x10,0x10,0x7c,0x00}, // H 55 | {0x44,0x44,0x7c,0x44,0x44,0x00}, // I 56 | {0x60,0x40,0x40,0x44,0x7c,0x00}, // J 57 | {0x7c,0x10,0x10,0x28,0x44,0x00}, // K 58 | {0x7c,0x40,0x40,0x40,0x40,0x00}, // L 59 | {0x7c,0x08,0x10,0x08,0x7c,0x00}, // M 60 | {0x7c,0x08,0x10,0x20,0x7c,0x00}, // N 61 | {0x38,0x44,0x44,0x44,0x38,0x00}, // O 62 | {0x7c,0x14,0x14,0x14,0x08,0x00}, // P 63 | {0x3c,0x24,0x64,0x24,0x3c,0x00}, // Q 64 | {0x7c,0x14,0x14,0x14,0x68,0x00}, // R 65 | {0x5c,0x54,0x54,0x54,0x74,0x00}, // S 66 | {0x04,0x04,0x7c,0x04,0x04,0x00}, // T 67 | {0x7c,0x40,0x40,0x40,0x7c,0x00}, // U 68 | {0x0c,0x30,0x40,0x30,0x0c,0x00}, // V 69 | {0x3c,0x40,0x30,0x40,0x3c,0x00}, // W 70 | {0x44,0x28,0x10,0x28,0x44,0x00}, // X 71 | {0x0c,0x10,0x60,0x10,0x0c,0x00}, // Y 72 | {0x44,0x64,0x54,0x4c,0x44,0x00}, // Z 73 | {0x7c,0x44,0x00,0x00,0x00,0x00}, // [ 74 | {0x0c,0x10,0x60,0x00,0x00,0x00}, // "\" 75 | {0x44,0x7c,0x00,0x00,0x00,0x00}, // ] 76 | {0x00,0x01,0x00,0x01,0x00,0x00}, // ^ 77 | {0x40,0x40,0x40,0x40,0x40,0x40}, // _ 78 | {0x00,0x01,0x00,0x00,0x00,0x00}, // ` 79 | {0x7c,0x24,0x24,0x24,0x7c,0x00}, // a 80 | {0x7c,0x54,0x54,0x54,0x6c,0x00}, // b 81 | {0x7c,0x44,0x44,0x44,0x44,0x00}, // c 82 | {0x7c,0x44,0x44,0x44,0x38,0x00}, // d 83 | {0x7c,0x54,0x54,0x54,0x44,0x00}, // e 84 | {0x7c,0x14,0x14,0x14,0x04,0x00}, // f 85 | {0x7c,0x44,0x44,0x54,0x74,0x00}, // g 86 | {0x7c,0x10,0x10,0x10,0x7c,0x00}, // h 87 | {0x44,0x44,0x7c,0x44,0x44,0x00}, // i 88 | {0x60,0x40,0x40,0x44,0x7c,0x00}, // j 89 | {0x7c,0x10,0x10,0x28,0x44,0x00}, // k 90 | {0x7c,0x40,0x40,0x40,0x40,0x00}, // l 91 | {0x7c,0x08,0x10,0x08,0x7c,0x00}, // m 92 | {0x7c,0x08,0x10,0x20,0x7c,0x00}, // n 93 | {0x38,0x44,0x44,0x44,0x38,0x00}, // o 94 | {0x7c,0x14,0x14,0x14,0x08,0x00}, // p 95 | {0x3c,0x24,0x64,0x24,0x3c,0x00}, // q 96 | {0x7c,0x14,0x14,0x14,0x68,0x00}, // r 97 | {0x5c,0x54,0x54,0x54,0x74,0x00}, // s 98 | {0x04,0x04,0x7c,0x04,0x04,0x00}, // t 99 | {0x7c,0x40,0x40,0x40,0x7c,0x00}, // u 100 | {0x0c,0x30,0x40,0x30,0x0c,0x00}, // v 101 | {0x3c,0x40,0x30,0x40,0x3c,0x00}, // w 102 | {0x44,0x28,0x10,0x28,0x44,0x00}, // x 103 | {0x0c,0x10,0x60,0x10,0x0c,0x00}, // y 104 | {0x44,0x64,0x54,0x4c,0x44,0x00}, // z 105 | {0x10,0x7c,0x44,0x00,0x00,0x00}, // { 106 | {0x6c,0x00,0x00,0x00,0x00,0x00}, // | 107 | {0x44,0x7c,0x10,0x00,0x00,0x00}, // } 108 | {0x02,0x01,0x02,0x01,0x00,0x00}, // ~ 109 | {0x00,0x00,0x00,0x00,0x00,0x00} 110 | }; 111 | 112 | static void voidWriteData (u8 copy_u8Data); 113 | static void voidWriteCommand (u8 copy_u8Command); 114 | static void voidDrawPixel (u16 copy_u16X , u16 copy_u16Y , u16 copy_u16Colour); 115 | static void voidSetAddress (u16 copy_u16StartX ,u16 copy_u16EndX , u16 copy_u16StartY , u16 copy_u16EndY); 116 | static void voidSetColour(u16 copy_u16Colour); 117 | 118 | 119 | #endif 120 | -------------------------------------------------------------------------------- /03 - HAL/08 - TFT/TFT_program.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 21/9/2020 */ 5 | /*****************************************************/ 6 | 7 | /* Library includes */ 8 | #include "BIT_MATH.h" 9 | #include "STD_TYPES.h" 10 | 11 | /* MCAL includes */ 12 | #include "GPIO_interface.h" 13 | #include "SPI_interface.h" 14 | #include "STK_interface.h" 15 | 16 | /* module includes */ 17 | #include "TFT_interface.h" 18 | #include "TFT_config.h" 19 | #include "TFT_private.h" 20 | 21 | void TFT_voidInit (void){ 22 | /* GPIO slave pin init */ 23 | GPIO_voidSetPinMode(TFT_SLAVE_PIN , GPIO_OUTPUT_10MHZ_PP); 24 | /* Reset pulse */ 25 | GPIO_voidsetPinValue (TFT_RST_PIN , HIGH); 26 | STK_voidSetBusyWait(100 , TIME_US); 27 | GPIO_voidsetPinValue (TFT_RST_PIN , LOW); 28 | STK_voidSetBusyWait(2 , TIME_US); 29 | GPIO_voidsetPinValue (TFT_RST_PIN , HIGH); 30 | STK_voidSetBusyWait(100 , TIME_US); 31 | GPIO_voidsetPinValue (TFT_RST_PIN , LOW); 32 | STK_voidSetBusyWait(100 , TIME_US); 33 | GPIO_voidsetPinValue (TFT_RST_PIN , HIGH); 34 | STK_voidSetBusyWait(120 , TIME_MS); 35 | 36 | /* Sleep out */ 37 | voidWriteCommand(0x11); 38 | 39 | /* Wait 150 ms */ 40 | STK_voidSetBusyWait(150 , TIME_MS); 41 | 42 | /* Colour mode command */ 43 | voidWriteCommand(0x3A); 44 | voidWriteData(0x05); 45 | 46 | /* Display on */ 47 | voidWriteCommand(0x29); 48 | 49 | } 50 | 51 | void TFT_voidDisplayImage(const u16* copy_u16Image){ 52 | 53 | /* Set address range for the whole display */ 54 | voidSetAddress(0 , TFT_MAX_X , 0 , TFT_MAX_Y); 55 | 56 | u16 local_u16Counter ; 57 | for (local_u16Counter = 0 ; local_u16Counter < (TFT_MAX_X * TFT_MAX_Y); local_u16Counter++){ 58 | voidSetColour(copy_u16Image[local_u16Counter]); 59 | } 60 | } 61 | 62 | void TFT_voidDrawRectangle (u16 copy_u16X ,u16 copy_u16Y , u16 copy_u16Width , u16 copy_u16Hight , u16 copy_u16Color ){ 63 | u16 loacal_u16EndX = copy_u16X + copy_u16Width - 1; 64 | u16 loacal_u16EndY = copy_u16Y + copy_u16Hight - 1; 65 | 66 | /* Set area of addresses */ 67 | voidSetAddress(copy_u16X ,loacal_u16EndX,copy_u16Y ,loacal_u16EndY ); 68 | 69 | /* Draw the Rectangle*/ 70 | for (u16 i = 0 ; i < (copy_u16Width *copy_u16Hight ) ; i++){ 71 | voidSetColour(copy_u16Color); 72 | } 73 | } 74 | 75 | 76 | void TFT_voidFillDisplay (u16 copy_u16Colour){ 77 | TFT_voidDrawRectangle(0 , 0 ,TFT_MAX_X , TFT_MAX_Y , copy_u16Colour); 78 | } 79 | 80 | void TFT_voidPrintChar(s8 copy_s8Char , u16 copy_u16X , u16 copy_u16Y, u8 copy_u8Size , u16 copy_u16Color , u16 copy_u16BackColor) 81 | { 82 | /* Get array index */ 83 | u8 local_u8CharIndex = 0 ; 84 | if (( copy_s8Char >= ' ' )){ 85 | local_u8CharIndex = copy_s8Char - 32 ; 86 | } 87 | 88 | /* Background */ 89 | TFT_voidDrawRectangle( copy_u16X, copy_u16Y ,copy_u8Size*TFT_CHARACTER_WIDTH ,copy_u8Size*TFT_CHARACTER_HIGHT ,copy_u16BackColor); 90 | 91 | for (u8 i = 0; i > 8)); // MS byte 157 | voidWriteData(copy_u16StartX); 158 | 159 | /* Stop byte */ 160 | voidWriteData((copy_u16EndX >> 8)); 161 | voidWriteData(copy_u16EndX); 162 | 163 | /* Set x Address */ 164 | voidWriteCommand(0x2B); 165 | /* Start byte */ 166 | voidWriteData((copy_u16StartY >> 8)); // MS byte 167 | voidWriteData(copy_u16StartY); 168 | /* Stop byte */ 169 | voidWriteData((copy_u16EndY >> 8)); 170 | voidWriteData(copy_u16EndY); 171 | 172 | /* RAM write */ 173 | voidWriteCommand(0x2C); 174 | } 175 | } 176 | 177 | static void voidSetColour(u16 copy_u16Colour) 178 | { 179 | 180 | /* Write pixel */ 181 | u8 high_byte = (copy_u16Colour >> 8); 182 | u8 low_byte = (copy_u16Colour & 0xff); 183 | voidWriteData(high_byte); 184 | voidWriteData(low_byte); 185 | } 186 | 187 | 188 | -------------------------------------------------------------------------------- /03 - HAL/09 - ESP/ESP_config.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 30/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef ESP_CONFIG_H 7 | #define ESP_CONFIG_H 8 | 9 | #define ESP_UART_CH UART1 10 | 11 | 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /03 - HAL/09 - ESP/ESP_interface.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 30/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef ESP_INTERFACE_H 7 | #define ESP_INTERFACE_H 8 | 9 | /********************************************************* 10 | Pre conditions : 11 | 1 - UART initialized 12 | *********************************************************/ 13 | 14 | 15 | /* 16 | * ESP_voidInit - > Initialize the WIFI module 17 | */ 18 | void ESP_voidInit (void); 19 | 20 | /* 21 | * ESP_voidConnectToWifi - > connect to wifi network 22 | * i/p : the SSD of wifi and password as text 23 | */ 24 | void ESP_voidConnectToWifi(const u8 *copy_u8SSD , const u8 *copy_u8Pass); 25 | 26 | /* 27 | * ESP_voidConnectToServer - > connect to server 28 | * i/p : the IP of the server as text and port number 29 | */ 30 | void ESP_voidConnectToServer(const u8 *copy_u8IP ,const u8 copy_u8Port ); 31 | 32 | /* 33 | * ESP_voidRecData - > Receive data from a given URL 34 | * i/p : the URL as text 35 | * o/p : the received character 36 | */ 37 | u8 ESP_voidRecData (const u8 *copy_u8URL ); 38 | 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /03 - HAL/09 - ESP/ESP_private.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 30/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef ESP_PRIVATE_H 7 | #define ESP_PRIVATE_H 8 | 9 | 10 | 11 | 12 | 13 | 14 | #endif -------------------------------------------------------------------------------- /03 - HAL/09 - ESP/ESP_program.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 30/9/2020 */ 5 | /*****************************************************/ 6 | 7 | /* Library includes */ 8 | #include "BIT_MATH.h" 9 | #include "STD_TYPES.h" 10 | #include 11 | 12 | /* MCAL includes */ 13 | #include "UART_interface.h" 14 | 15 | /* module includes */ 16 | #include "ESP_interface.h" 17 | #include "ESP_config.h" 18 | #include "ESP_private.h" 19 | 20 | static void voidWaitForRes(u8 *copy_u8Rec , u8 time ){ 21 | while (time--){ 22 | UART_voidRecDataSynch (ESP_UART_CH , copy_u8Rec , 100); 23 | if (copy_u8Rec[0]){break;} 24 | } 25 | } 26 | 27 | void ESP_voidInit (void) 28 | { 29 | u8 local_RecBuffer[100] = {0} ; 30 | do { 31 | /* Turn off echo */ 32 | UART_voidSendDataSynch (ESP_UART_CH , "ATE0\r\n" ); 33 | voidWaitForRes(local_RecBuffer , 1); 34 | }while((local_RecBuffer[2] != 'O') &&(local_RecBuffer[3] != 'K')); 35 | 36 | /* Set the mode */ 37 | do { 38 | UART_voidSendDataSynch (ESP_UART_CH , "AT+CWMODE=1\r\n" ); 39 | voidWaitForRes(local_RecBuffer , 1); 40 | }while((local_RecBuffer[2] != 'O') &&(local_RecBuffer[3] != 'K')); 41 | 42 | } 43 | 44 | void ESP_voidConnectToWifi(const u8 *copy_u8SSD , const u8 *copy_u8Pass) 45 | { 46 | u8 local_buffer[100] = {0} ; 47 | sprintf (local_buffer , "AT+CWJAP_CUR=\"%s\",\"%s\"\r\n" ,copy_u8SSD , copy_u8Pass ); 48 | UART_voidSendDataSynch (ESP_UART_CH , local_buffer ); 49 | } 50 | 51 | void ESP_voidConnectToServer(const u8 *copy_u8IP ,const u8 copy_u8Port ) 52 | { 53 | u8 local_buffer[100] = {0} ; 54 | u8 local_RecBuffer[100] = {0} ; 55 | u8 flag1 , flag2 ; 56 | /* Try to connect to server till it is connceted */ 57 | do{ 58 | sprintf (local_buffer , "AT+CIPSTART=\"TCP\",\"%s\",%i\r\n" ,copy_u8IP , copy_u8Port ); 59 | UART_voidSendDataSynch (ESP_UART_CH , local_buffer ); 60 | voidWaitForRes(local_RecBuffer , 2); 61 | if ((local_RecBuffer[2] != 'b') &&(local_RecBuffer[3] != 'u')){ 62 | voidWaitForRes(local_RecBuffer , 4); 63 | } 64 | flag1 = ((local_RecBuffer[0] != 'C') &&(local_RecBuffer[1] != 'O')); 65 | flag2 = ((local_RecBuffer[0] != 'A') &&(local_RecBuffer[1] != 'L')); 66 | }while( flag1 && flag2); 67 | 68 | } 69 | 70 | 71 | u8 ESP_voidRecData (const u8 *copy_u8URL ) 72 | { 73 | u8 local_u8Lenght = 0; 74 | u8 local_ReceiveBuffer[100] = {0}; 75 | u8 local_buffer[100] = {0} ; 76 | u8 local_buffer2[20] = {0}; 77 | 78 | local_u8Lenght = strlen(copy_u8URL); 79 | local_u8Lenght += 6 ; 80 | 81 | sprintf (local_buffer , "GET %s\r\n" ,copy_u8URL); 82 | sprintf (local_buffer2 , "AT+CIPSEND=%i\r\n" ,local_u8Lenght); 83 | 84 | /* Send length */ 85 | u8 buf[100] = {0}; 86 | UART_voidSendDataSynch (ESP_UART_CH , local_buffer2 ); 87 | voidWaitForRes(buf , 1); 88 | /* Send request */ 89 | UART_voidSendDataSynch (ESP_UART_CH , local_buffer ); 90 | voidWaitForRes(buf , 1); 91 | /* Rec one char from text file */ 92 | UART_voidRecDataSynch (ESP_UART_CH , local_ReceiveBuffer , 22); 93 | return local_ReceiveBuffer[20]; 94 | } 95 | 96 | 97 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /04 - SERVICE/01 - OS/OS_config.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 5/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef OS_CONFIG_H 7 | #define OS_CONFIG_H 8 | 9 | /* 10 | Number of tasks 11 | each task take 10 byte in RAM 12 | */ 13 | #define OS_NUMBER_OF_TASKS 3 14 | 15 | /* 16 | Options 17 | TIME_MS 18 | TIME_US 19 | */ 20 | #define OS_TICK_UNIT TIME_MS 21 | 22 | /* 23 | The tick interval 24 | */ 25 | #define OS_TICK_PERIOD 1 26 | 27 | 28 | 29 | #endif -------------------------------------------------------------------------------- /04 - SERVICE/01 - OS/OS_interface.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 5/9/2020 */ 5 | /**************************************************************/ 6 | /** Pre conditions : 7 | 1 - No other module use STK driver 8 | 2 - STK drivers files must be included in the project 9 | 3 - Number of tasks must be configured ( OS_NUMBER_OF_TASKS ) 10 | 4 - Clock must be selected right in STK driver to ensure right time 11 | 5 - OS_voidStart must be called before while 12 | 6 - OS_voidDispatcher must be called in the while 13 | 7 - Global interrupt must be active 14 | **/ 15 | /****************************************************************/ 16 | 17 | #ifndef OS_INTERFACE_H 18 | #define OS_INTERFACE_H 19 | 20 | /** Errors **/ 21 | #define SUCCESSED 1 22 | #define ERROR 0 23 | 24 | /* Hold the task states */ 25 | typedef enum { 26 | OS_TASK_READY , 27 | OS_TASK_SUSPENDE , 28 | }OS_TaskStates_t; 29 | 30 | /*********************** Scheduler APIS *********************/ 31 | 32 | /* 33 | * OS_voidStart - > Initialize the OS and start the scheduling 34 | */ 35 | void OS_voidStart(void); 36 | 37 | /* 38 | * OS_voidStop - > Stop the scheduling by stoping the timer ISR 39 | */ 40 | void OS_voidStop(void); 41 | 42 | /* 43 | * OS_voidResume - > Resume the scheduling by re enabling the ISR 44 | */ 45 | void OS_voidResume(void); 46 | 47 | /* 48 | * OS_voidDispatcher - > Called in the while Loop and responsible of calling the 49 | the right task 50 | */ 51 | void OS_voidDispatcher (void); 52 | 53 | 54 | 55 | /*********************** Task APIS ****************************/ 56 | 57 | /* 58 | * OS_u8CreateTask - > create a new task note that maximum number of tasks is limited by 59 | the config parameter (OS_NUMBER_OF_TASKS) 60 | * I / P : task id (u8) / initial delay for the first invoke of the function (u16) 61 | Period of the task (u16) , void function aka the task 62 | * O / P : an error state if successfully completed will be(SUCCESSED) else will be (ERROR) 63 | */ 64 | u8 OS_u8CreateTask(u8 copy_u8ID , u16 copy_u16delay , u16 copy_u16priode , void (* copy_voidFunc) (void)); 65 | 66 | /* 67 | * OS_u8DeleteTask - > Delete a task permanently from the scheduler 68 | * I / P : task id (u8) 69 | * o / p : an error state if successfully completed will be(SUCCESSED) else will be (ERROR) 70 | */ 71 | u8 OS_u8DeleteTask (u8 copy_u8ID); 72 | 73 | /* 74 | * OS_voidSetTaskState - > Set task state to ready or suspended 75 | * I / P : task id (u8) / state (OS_TaskStates_t) 76 | * o / p : an error state if successfully completed will be(SUCCESSED) else will be (ERROR) 77 | */ 78 | u8 OS_voidSetTaskState(u8 copy_u8ID , OS_TaskStates_t copy_state); 79 | 80 | 81 | 82 | 83 | 84 | #endif 85 | -------------------------------------------------------------------------------- /04 - SERVICE/01 - OS/OS_private.h: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 5/9/2020 */ 5 | /*****************************************************/ 6 | #ifndef OS_PRIVATE_H 7 | #define OS_PRIVATE_H 8 | 9 | /* Task data type to hold task information */ 10 | typedef struct 11 | { 12 | OS_TaskStates_t taskState ; 13 | u8 RunMe; 14 | u16 priode; 15 | u16 initialDelay ; 16 | void (* func) (void); 17 | }OS_Task_t; 18 | 19 | #define NULL ((void *) 0) 20 | 21 | /******* Private functions *******/ 22 | static void voidScheduler (void); 23 | static void voidGoToSleep(void); 24 | 25 | 26 | 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /04 - SERVICE/01 - OS/OS_programe.c: -------------------------------------------------------------------------------- 1 | /*****************************************************/ 2 | /* Author : mosad */ 3 | /* Version : v01 */ 4 | /* date : 5/9/2020 */ 5 | /*****************************************************/ 6 | /* Library includes */ 7 | #include "BIT_MATH.h" 8 | #include "STD_TYPES.h" 9 | 10 | /* MCAL includes */ 11 | #include "STK_interface.h" 12 | 13 | /* module includes */ 14 | #include "OS_interface.h" 15 | #include "OS_private.h" 16 | #include "OS_config.h" 17 | 18 | /************* Global variables ************/ 19 | OS_Task_t globalTasks[OS_NUMBER_OF_TASKS] ; 20 | 21 | /************ Public functions **************/ 22 | 23 | /*********************** Scheduler APIS *********************/ 24 | void OS_voidStart(void) 25 | { 26 | /* To initialize the STK driver */ 27 | STK_voidInit(); 28 | /* To pass the scheduer function to the systick ISR */ 29 | STK_voidSetIntervalPeriodic(OS_TICK_PERIOD, OS_TICK_UNIT , voidScheduler ); 30 | } 31 | 32 | void OS_voidStop(void) 33 | { 34 | STK_voidStop(); 35 | } 36 | 37 | void OS_voidResume(void) 38 | { 39 | STK_voidResume(); 40 | } 41 | 42 | void OS_voidDispatcher (void) 43 | { 44 | /* Check on all the tasks and execute any task with RunMe flag above zero*/ 45 | for (u8 i = 0; i < OS_NUMBER_OF_TASKS ; i++) 46 | { 47 | if ( (globalTasks[i].func ) && (globalTasks[i].RunMe > 0) ) 48 | { 49 | globalTasks[i].func(); 50 | globalTasks[i].RunMe -= 1 ; 51 | 52 | } 53 | else 54 | { 55 | /* Should not be here */ 56 | } 57 | } 58 | /* To save the power between ticks when there is no tasks ready yet*/ 59 | voidGoToSleep(); 60 | } 61 | /***************************************************************/ 62 | 63 | /*********************** Task APIS ****************************/ 64 | u8 OS_u8CreateTask(u8 copy_u8ID , u16 copy_u16delay , u16 copy_u16priode , void (* copy_voidFunc) (void)) 65 | { 66 | u8 local_u8ErrorState = SUCCESSED ; 67 | /* assign task parameters to the right array index */ 68 | if (copy_u8ID < OS_NUMBER_OF_TASKS) 69 | { 70 | globalTasks[copy_u8ID].initialDelay = copy_u16delay ; 71 | globalTasks[copy_u8ID].priode = copy_u16priode ; 72 | globalTasks[copy_u8ID].func = copy_voidFunc ; 73 | globalTasks[copy_u8ID].taskState = OS_TASK_READY ; 74 | globalTasks[copy_u8ID].RunMe = 0 ; 75 | } 76 | else 77 | { 78 | local_u8ErrorState = ERROR ; 79 | } 80 | /* To make the user check if the task created successfully */ 81 | return local_u8ErrorState ; 82 | } 83 | 84 | u8 OS_u8DeleteTask (u8 copy_u8ID) 85 | { 86 | u8 local_u8ErrorCode ; 87 | if (globalTasks[copy_u8ID].func) 88 | { 89 | globalTasks[copy_u8ID].initialDelay = 0 ; 90 | globalTasks[copy_u8ID].priode = 0 ; 91 | globalTasks[copy_u8ID].func = NULL ; 92 | globalTasks[copy_u8ID].RunMe = 0 ; 93 | local_u8ErrorCode = SUCCESSED ; 94 | } 95 | else 96 | { 97 | local_u8ErrorCode = ERROR ; 98 | } 99 | /* To make the user check if the task deleted successfully */ 100 | return local_u8ErrorCode ; 101 | } 102 | 103 | u8 OS_voidSetTaskState(u8 copy_u8ID , OS_TaskStates_t copy_state) 104 | { 105 | u8 local_u8ErrorCode ; 106 | if (globalTasks[copy_u8ID].func) 107 | { 108 | globalTasks[copy_u8ID].taskState = copy_state ; 109 | local_u8ErrorCode = SUCCESSED ; 110 | } 111 | else 112 | { 113 | local_u8ErrorCode = ERROR ; 114 | } 115 | /* To make the user check if the user is changing the state of valid task */ 116 | return local_u8ErrorCode ; 117 | } 118 | 119 | 120 | /********** Private functions ***************/ 121 | static void voidScheduler (void) 122 | { 123 | /** Co operative scheduling **/ 124 | for (u8 i = 0; i < OS_NUMBER_OF_TASKS ; i++) 125 | { 126 | if ( (globalTasks[i].func) && (globalTasks[i].taskState == OS_TASK_READY) ) 127 | { 128 | 129 | /* Time of the task has come */ 130 | if ( globalTasks[i].initialDelay == 0) 131 | { 132 | /* Dispatcher will check this flag in the while */ 133 | globalTasks[i].RunMe++; 134 | 135 | /* Only if the task periodic will updated the initialDelay */ 136 | if (globalTasks[i].priode){ 137 | globalTasks[i].initialDelay = globalTasks[i].priode - OS_TICK_PERIOD ; 138 | } 139 | } 140 | else 141 | { 142 | globalTasks[i].initialDelay -= OS_TICK_PERIOD ; 143 | } 144 | } 145 | else 146 | { 147 | /* Should not be here */ 148 | } 149 | } 150 | 151 | } 152 | 153 | static void voidGoToSleep(void){ 154 | __asm ("WFI"); 155 | } 156 | --------------------------------------------------------------------------------