Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * USART2 is connected to the ST-Link virtual COM port.
- * Use Tera Term to interract with STM board
- *
- * By default, the clock is running at 16 MHz.
- * The UART2 is configured for 115200 Baud.
- * PA2 - USART2 TX (AF7)
- * PA3 - USART2 RX (AF7)
- */
- #include "stm32f4xx.h"
- #include <stdio.h>
- #include <string.h>
- #include "Types.h"
- #include "Drivers.h"
- void delayMs(int);
- UART_MSG_T msg = {.data = {0}, .id = 0, .len = 0};
- U8 CyclicMessage_U8 = 0;
- U8 TimerCountDown_U16 = SECONDS_TO_COUNT_U8;
- U8 PrintInCycleMode_U8 = 0;
- /* Lucrarea 2 de completat */
- void PeriphInit(void)
- {
- __disable_irq();
- // Configure PC[11..8] as input
- RCC->AHB1ENR |= 4; /* Enable GPIOC clock */
- GPIOC->MODER &= ~0x0CFF0000; /* Reset GPIOC PC[13..9] for input mode */
- RCC->AHB1ENR |= 1; /* porneste semnalul de tact al portului A */
- GPIOA->MODER &= ~0x00000C00; /* sterge registrul de mod al portului A */
- GPIOA->MODER |= 0x00000400; /* seteaza modul de iesire al pinului PA5 */
- RCC->AHB1ENR |= 2; /* enable GPIOB clock */
- GPIOB->MODER &= ~0x0000ff00; /* clear pin mode */
- GPIOB->MODER |= 0x00005500; /* set pins to output mode */
- USART2_init();
- __enable_irq();
- }
- /* TODO: change the 0's to the correct value */
- const U8 Hex7Segment[16] = {0};
- /* end lucrarea 2*/
- /* Lucrarea 3 De completat*/
- static const U32 STRVR = 0;
- static const U16 STCTRL = 0;
- static const U16 TIM3_PSC = 0;
- static const U16 TIM3_ARR = 0;
- static const U16 TIM3_CH1_PSC = 0;
- static const U16 TIM3_CH1_ARR = 0;
- static const U16 TIM3_CH1_CCMR1 = 0;
- static const U16 TIM3_CH1_CCR1 = 0;
- static const U16 TIM8_CH3_PSC = 0;
- static const U16 TIM8_CH3_CCMR2 = 0;
- U32 timestamp = 0;
- U32 capture_time = 0;
- U8 nr_of_pushes = 0;
- U8 factor = 1;
- /* end lucrarea 3 */
- /* Lucrarea 4 */
- U16 dac_factor = 600;
- U16 dac_hold_value = 0;
- #define ADC_8_BIT 1 //dezactivati pentru adc 12 biti
- #if ADC_8_BIT
- static const U32 ADC_CR1 = 0; /* 8-bit resolution */
- static const U32 ADC_CR2 = 0; /* SW Start, Align right justified, start adc */
- #else
- static const U32 ADC_CR1 = 0; /* 12-bit resolution */
- static const U32 ADC_CR2 = 0; /* SW Start, Align left justified, start ADC */
- #endif
- static const U32 MODER = 0; /* PA0-PA4 ca intrari analogice*/
- /*End of Lucrarea 4*/
- /* lucrarea 5 */
- static const U32 MODER_IRQ_B = 0; /* PB15-PB12 ca intrari*/
- static const U32 MODER_IRQ_C = 0; /* PC11-PC8 ca intrari */
- static const U32 PUPDR_IRQ_B = 0; /* PB15-PB12 pull-up*/
- static const U16 EXTI_FTSR = 0; /* ISR on PB15-PB12 falling edge*/
- static const U16 EXTI_RTSR = 0; /* ISR on PC11-PC8 rising edge*/
- void IRQInit()
- {
- SetupIOInterrupt(MODER_IRQ_B, MODER_IRQ_C, PUPDR_IRQ_B, EXTI_FTSR, EXTI_RTSR);
- }
- /* end of lucrarea 5 */
- /* Lucrarea 6 */
- static const U16 SPI_CR1 = 0x33F; /* viteza transfer si 8 biti date, master mode */
- static const U32 GPIOB_OTYPER = 0; /* set as open drain */
- static const U32 GPIOB_PUPDR = 0; /* setup SCL, SDA ca pull-up pentru I2C*/
- static const U16 I2C_CR1 = 0; /* SW resetm ie;ire reset, enable I2C1*/
- static const U16 I2CC_CCR = 0; /* setup as standard mode*/
- /* end Lucrarea 6 */
- U8 ReceiveMessage(void)
- {
- char msg_str[MAX_PACKET_STR_SIZE];
- const char *pos = msg_str;
- memset(&msg, 0, sizeof(UART_MSG_T));
- fgets(msg_str, MAX_PACKET_STR_SIZE, stdin);
- if (strlen(msg_str) < 3)
- {
- msg.id = 0xFF;
- return 0;
- }
- if (sscanf(pos, "%2hhx", &msg.id) == 0)
- {
- msg.id = 0xFF;
- return 0;
- }
- if (strlen(msg_str) > 5)
- {
- pos += 3;
- for (int i = 1; i < strlen(msg_str) / 3; i++)
- {
- sscanf(pos, "%2hhx", &msg.data[i - 1]);
- pos += 3;
- msg.len++;
- }
- }
- return 1;
- }
- void PrintReceivedMessage(const UART_MSG_T msg)
- {
- printf("the received packet is: \n");
- printf("ID: %02x \n", msg.id);
- printf("Size in Bytes: %d\n", msg.len);
- printf("Data:");
- for (int i = 0; i < msg.len; i++)
- printf("%02X ", msg.data[i]);
- printf("\r\n");
- }
- void ProcessMessage(const UART_MSG_T msg)
- {
- U32 p;
- switch (msg.id)
- {
- case SW_VERSION:
- printf("SW Version: 01.03.24-SI\n");
- break;
- case PORT_INPUT:
- if (msg.len < 2)
- {
- printf("bad-len\n");
- }
- else if (msg.data[0] != 0x42 && msg.data[0] != 0x43)
- {
- printf("bad-port\n");
- }
- else if (((msg.data[0] == 0x42) && (msg.data[1] > 15 || msg.data[1] < 12)) ||
- ((msg.data[0] == 0x43) && (msg.data[1] > 11 || msg.data[1] < 8)))
- {
- printf("bad-pin\n");
- }
- else
- {
- U32 i = 1;
- p = msg.data[0] != 0x42 ? GPIOC->IDR : GPIOB->IDR;
- i <<= msg.data[1];
- if (CyclicMessage_U8 == 0 || PrintInCycleMode_U8 == 1)
- {
- printf("%ld \n", (p & i) >> msg.data[1]);
- PrintInCycleMode_U8 = 0;
- }
- if (CyclicMessage_U8 == 0 && msg.len >= 3)
- {
- if (msg.data[2] > 0)
- {
- CyclicMessage_U8 = 1;
- InitPeriodicTimer(PERIODIC_TIMER_VALUE_500US_U16);
- }
- }
- }
- break;
- case PORT_OUTPUT:
- if (msg.len < 3)
- {
- printf("bad-len\n");
- }
- else if (msg.data[0] != 0x42)
- {
- printf("bad-port\n");
- }
- else if ((msg.data[1] > 7) || (msg.data[1] < 4))
- {
- printf("bad-pin\n");
- }
- else
- {
- U32 i = 1;
- i <<= msg.data[1];
- GPIOB->BSRR = msg.data[2] ? (GPIOB->BSRR | i) : (GPIOB->BSRR | (i << 16));
- printf("%02lX\n", (GPIOB->ODR & 0x000000F0) >> 4);
- }
- break;
- case PORT_HEX:
- if (msg.len < 2)
- {
- printf("bad-len\n");
- }
- else if (msg.data[0] > 3)
- {
- printf("bad-display\n");
- }
- else if (msg.data[1] > 15)
- {
- printf("bad-number\n");
- }
- else
- {
- SPI1_init();
- SPI1_write(Hex7Segment[msg.data[1]]); /* write pattern to the seven segments */
- SPI1_write(1 << msg.data[0]); /* select digit */
- }
- break;
- case TIMERS:
- if (msg.len < 1)
- {
- printf("bad-len\n");
- }
- else
- {
- SystemTickTimerStop();
- switch (msg.data[0])
- {
- /*general timer */
- case GENERAL_TIM3:
- if (CyclicMessage_U8 == 0)
- {
- TIM3_Cfg(TIM3_PSC, TIM3_ARR);
- timestamp = 0;
- if (msg.len > 1)
- {
- TIM3->ARR = ((TIM3->ARR + 1) * msg.data[1]) - 1;
- factor = msg.data[1];
- }
- }
- if (CyclicMessage_U8 == 0 || PrintInCycleMode_U8 == 1)
- {
- printf("%ld \n", timestamp);
- PrintInCycleMode_U8 = 0;
- }
- if (CyclicMessage_U8 == 0 && msg.len >= 3)
- {
- if (msg.data[2] > 0)
- {
- CyclicMessage_U8 = 1;
- InitPeriodicTimer(PERIODIC_TIMER_VALUE_500US_U16);
- }
- else
- {
- TIM3_Stop();
- }
- }
- break;
- /* out compare */
- case TIM3_CH1_COMPARE:
- TIM3_CH1_Compare_Cfg(TIM3_CH1_PSC, TIM3_CH1_ARR, TIM3_CH1_CCMR1, TIM3_CH1_CCR1);
- if (msg.len > 1)
- {
- TIM3->ARR = TIM3->ARR * msg.data[1];
- }
- break;
- /* in capture */
- case TIM8_CH3_CAPTURE:
- if (CyclicMessage_U8 == 0)
- {
- TIM8_CH3_Capture_Cfg(TIM8_CH3_PSC, TIM8_CH3_CCMR2);
- capture_time = 0;
- }
- if (CyclicMessage_U8 == 0 || PrintInCycleMode_U8 == 1)
- {
- printf("%d %ld \n", nr_of_pushes, capture_time);
- PrintInCycleMode_U8 = 0;
- }
- if (CyclicMessage_U8 == 0 && msg.len >= 2)
- {
- if (msg.data[1] > 0)
- {
- CyclicMessage_U8 = 1;
- InitPeriodicTimer(PERIODIC_TIMER_VALUE_500US_U16);
- }
- else
- {
- TIM8_Stop();
- }
- }
- break;
- /* PWM */
- case TIM8_CH1_PWM:
- StopTIM8_CH1_PWM();
- InitTIM8_CH1_PWM();
- if (msg.len >= 2)
- {
- if (msg.data[1] <= 0x64)
- {
- SetTIM8_CH1_PWM_DutyCycle(msg.data[1]);
- }
- else
- {
- printf("bad-duty-cycle\n");
- StopTIM8_CH1_PWM();
- }
- }
- break;
- case SYS_TICK_TIMER:
- SystemTickTimerInit(STRVR, STCTRL);
- break;
- default:
- printf("Invalid CMD received \n");
- break;
- }
- }
- break;
- case ADC_DAC:
- if (msg.len < 2)
- {
- printf("bad-len\n");
- }
- else if (msg.data[0] == 0x00) /*ADC*/
- {
- if (msg.data[1] > MAX_NR_OF_CHANNELS)
- {
- printf("bad-channel\n");
- }
- else if (msg.data[1] < 2 || (msg.data[1] > 3 && msg.data[1] <5) || msg.data[1] > 16)
- {
- U16 adc_value = 0xFFFF;
- ADC_Configure(MODER, ADC_CR1, ADC_CR2);
- adc_value = ADC_SampleChannel(msg.data[1]);
- #if ADC_8_BIT
- adc_value &= 0x00FF;
- #else
- adc_value &= 0xFFF0;
- #endif
- printf("Channel %d value %d\n", msg.data[1], adc_value);
- ADC_Stop();
- }
- else
- {
- printf("Channel %d value %d\n", msg.data[1], 0xFFFF);
- }
- }
- else if (msg.data[0] == 0x01) /*DAC*/
- {
- if (CyclicMessage_U8 == 0)
- {
- dac_factor = dac_factor * msg.data[1];
- DAC_Configure(dac_hold_value + dac_factor);
- }
- if (CyclicMessage_U8 == 0 || PrintInCycleMode_U8 == 1)
- {
- dac_hold_value = dac_hold_value + dac_factor;
- DAC->DHR12R1 = dac_hold_value & 0x0FFF;
- printf("Hold Value: %ld \n", DAC->DHR12R1);
- PrintInCycleMode_U8 = 0;
- }
- if (CyclicMessage_U8 == 0)
- {
- CyclicMessage_U8 = 1;
- InitPeriodicTimer(PERIODIC_TIMER_VALUE_500US_U16);
- }
- }
- else
- {
- printf("Invalid module\n");
- }
- break;
- case IRQ:
- if (msg.len < 2)
- {
- printf("bad-len\n");
- }
- else if (msg.data[0] != 0x42 && msg.data[0] != 0x43)
- {
- printf("bad-port\n");
- }
- else if (((msg.data[0] == 0x42) && (msg.data[1] > 15 || msg.data[1] < 12)) ||
- ((msg.data[0] == 0x43) && (msg.data[1] > 11 || msg.data[1] < 8)))
- {
- printf("bad-pin\n");
- }
- else
- {
- EnableIRQForIO(msg.data[1]);
- printf("Press the button for port %c, pin %d\n", msg.data[0], msg.data[1]);
- delayMs(5000);
- DisableIRQForIO(msg.data[1]);
- }
- break;
- case SPI:
- if (msg.len < 1)
- {
- printf("bad-len\n");
- }
- else
- {
- LCD_data(msg.data[0]);
- }
- break;
- case I2C:
- if (msg.len < 1)
- {
- printf("bad-len\n");
- }
- else
- {
- I16 acc_g[6];
- I2C1_Init(GPIOB_OTYPER, GPIOB_PUPDR, I2C_CR1, I2CC_CCR);
- MPU6050_initialize();
- MPU6050_getMotion6(&acc_g[0], &acc_g[1], &acc_g[2], &acc_g[3], &acc_g[4], &acc_g[5]);
- if(msg.data[0] > 6)
- printf("sensor return values: %d, %d, %d, %d, %d, %d\n", acc_g[0], acc_g[1], acc_g[2], acc_g[3], acc_g[4], acc_g[5]);
- else
- printf("sensor return value: %d\n", acc_g[msg.data[0]]);
- }
- break;
- default:
- printf("Invalid ID received \n");
- break;
- }
- }
- void writeStringLCD(char *line) {
- for(unsigned int i=0; i < strlen(line); i++)
- {
- LCD_data(line[i]);
- }
- }
- void newLine(unsigned int size){
- for(unsigned int i=0; i < 40-size; i++)
- {
- LCD_data(' ');
- }
- }
- void stopProgram(){
- while(1){}
- }
- int main(void)
- {
- U8 status = 0;
- PeriphInit();
- if(SPI_CR1 != 0) {
- LCD_init(SPI_CR1);
- }
- printf("Lucrarea 6 interfete seriale \n");
- printf("please enter the packet in the following format ID Byte0 Byte1 ... Byte7 \n");
- //RCC->AHB1ENR |= 4; /* porneste semnalul de tact al portului C */
- //RCC->AHB1ENR |= 1; /* porneste semnalul de tact al portului A */
- //GPIOA->MODER &= ~0x00000C00; /* sterge registrul de mod al portului A */
- //GPIOA->MODER |= 0x00000400; /* seteaza modul de iesire al pinului PA5 */
- //GPIOC->MODER &= ~0x00030000; /* Seteaza modul de intrare al pinului PC13 */
- char startMessageLine1[100] = {'I', 'n', 't', 'r', 'o', 'd', 'u', 'c', 'e', 't', 'i', '\0'};
- char startMessageLine2[100] = {'p', 'a', 'r', 'o', 'l', 'a', ':', '\0'};
- char buttonMessageLine1[100] = {'1', '\0'};
- char buttonMessageLine2[100] = {'2', '\0'};
- char buttonMessageLine3[100] = {'3', '\0'};
- char buttonMessageLine4[100] = {'4', '\0'};
- char password[100] = {'1', '2', '3', '4'};
- char passwordTry[100];
- char successMessageLine2[100] = {'D', 'E', 'B', 'L', 'O', 'C', 'A', 'T','\0'};
- char wrongPasswordMessageLine1[100] = {'I', 'n', 'c', 'e', 'r', 'c', 'a', 'r', 'i', '\0'};
- char wrongPasswordMessageLine2[100] = {'r', 'a', 'm', 'a', 's', 'e', ':', '\0'};
- char noMoreTriesMessageLine1[100] = {'A', 'i', ' ', 'r', 'a', 'm', 'a', 's', ' ', 'f', 'a', 'r', 'a', '\0'};
- char noMoreTriesMessageLine2[100] = {'i', 'n', 'c', 'e', 'r', 'c', 'a', 'r', 'i', '!', '\0'};
- char unlockStateMessageLine[100] = {'M', 'e', 'r', 'g', 'e', '!', '\0'};
- int k=0;
- int tries=3;
- int unlocked = 0;
- while (1)
- {
- //writeStringLCD(startMessageLine1);
- //newLine(strlen(startMessageLine1));
- //writeStringLCD(startMessageLine2);
- if(strcmp(password, passwordTry) == 0 && !unlocked) {
- LCD_command(1);
- delayMs(10);
- GPIOB->ODR &= ~0x000000F0;
- writeStringLCD(successMessageLine2);
- unlocked = 1;
- delayMs(500);
- //LCD_command(1);
- } else if(k >= 4 && !unlocked){
- LCD_command(1);
- GPIOB->ODR |= 0x000000F0;
- tries--;
- writeStringLCD(wrongPasswordMessageLine1);
- newLine(strlen(wrongPasswordMessageLine1));
- writeStringLCD(wrongPasswordMessageLine2);
- LCD_data('0' + tries);
- delayMs(500);
- if(tries == 0) {
- LCD_command(1);
- writeStringLCD(noMoreTriesMessageLine1);
- newLine(strlen(noMoreTriesMessageLine1));
- writeStringLCD(noMoreTriesMessageLine2);
- stopProgram();
- } else {
- //GPIOB->ODR &= ~0x000000F0;
- k=0;
- LCD_command(1);
- }
- }
- //if (GPIOC->IDR & 0x2000 && unlocked)
- //{
- //writeStringLCD(unlockStateMessageLine);
- //delayMs(500);
- //LCD_command(1);
- //}
- if (GPIOC->IDR & 0x100 && !unlocked)
- {
- passwordTry[k] = '1';
- LCD_data('*');
- delayMs(125);
- k++;
- }
- //GPIOA->BSRR = 0x00000020; /* aprinde LED-ul verde */
- if (GPIOC->IDR & 0x200 && !unlocked){
- passwordTry[k] = '2';
- LCD_data('*');
- delayMs(125);
- k++;
- }
- if (GPIOC->IDR & 0x400 && !unlocked){
- passwordTry[k] = '3';
- LCD_data('*');
- delayMs(125);
- k++;
- }
- if (GPIOC->IDR & 0x800 && !unlocked){
- passwordTry[k] = '4';
- LCD_data('*');
- delayMs(125);
- k++;
- }
- }
- }
- /* ISRs */
- void TIM2_IRQHandler(void)
- {
- TIM2->SR = 0; /* clear UIF */
- TimerCountDown_U16--;
- PrintInCycleMode_U8 = 1; /* time to print */
- }
- /* Lucrarea 3*/
- void TIM3_IRQHandler(void)
- {
- if (TIM3->SR & 1)
- {
- TIM3->SR &= ~1;
- timestamp += 40 * factor;
- }
- }
- void TIM8_CC_IRQHandler(void)
- {
- if (TIM8->SR & 8)
- {
- TIM8->SR &= ~8;
- nr_of_pushes += 1;
- capture_time += TIM8->CCR3;
- }
- }
- void SysTick_Handler(void)
- {
- GPIOB->ODR ^= 0x00000020;
- }
- /* Lucrarea 5 */
- void EXTI15_10_IRQHandler(void)
- {
- if (EXTI->PR & 0x00000400)
- { /* isr on pin 10 */
- EXTI->PR = 0x00000400; /* clear interrupt pending flag */
- GPIOB->ODR ^= 0x00000040;
- }
- else if (EXTI->PR & 0x00000800)
- { /* isr on pin 11 */
- EXTI->PR = 0x00000800; /* clear interrupt pending flag */
- GPIOB->ODR ^= 0x00000080;
- }
- else if (EXTI->PR & 0x00001000)
- { /* isr on pin 12 */
- EXTI->PR = 0x00001000; /* clear interrupt pending flag */
- GPIOB->ODR ^= 0x00000010;
- }
- else if (EXTI->PR & 0x00002000)
- { /* isr on pin 13 */
- EXTI->PR = 0x00002000; /* clear interrupt pending flag */
- GPIOB->ODR ^= 0x00000020;
- }
- else if (EXTI->PR & 0x00004000)
- { /* isr on pin 14 */
- EXTI->PR = 0x00004000; /* clear interrupt pending flag */
- GPIOB->ODR ^= 0x00000040;
- }
- else if (EXTI->PR & 0x00008000)
- { /* isr on pin 15 */
- EXTI->PR = 0x00008000; /* clear interrupt pending flag */
- GPIOB->ODR ^= 0x00000080;
- }
- }
- void EXTI9_5_IRQHandler(void)
- {
- if (EXTI->PR & 0x000000100)
- { /* isr on pin 8 */
- EXTI->PR = 0x00000100; /* clear interrupt pending flag */
- GPIOB->ODR ^= 0x00000010;
- }
- else if (EXTI->PR & 0x00000200)
- { /* isr on pin 9 */
- EXTI->PR = 0x00000200; /* clear interrupt pending flag */
- GPIOB->ODR ^= 0x00000020;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement