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"
- //----------------------------------------
- #define RS 1 /* BIT0 mask for reg select */
- #define EN 2 /* BIT1 mask for E */
- #define CORRECT_PASSWORD 1234
- //-------------------------------------------
- int corect[4]={2,2,2,2};
- int tries=3;
- int key=0;
- int count=0;
- int k=0;
- void delay(void);
- void delayMs(int);
- void LCD_nibble_write(char data, unsigned char control);
- void LCD_command(unsigned char command);
- void LCD_data(char data);
- void LCD_init(void);
- void SPI1_write(unsigned char data);
- int readRows(void);
- int getPasswordFromKeypad(void);
- void outputEnableCols(char n);
- void writeCols(char n);
- void writeLEDs(char n);
- void keypad_init(void);
- char keypad_getkey(void);
- void writeStringLCD(char *line);
- void newLine(unsigned int size);
- void stopProgram();
- int len =4;
- 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;
- static const U16 SPI_CR1 = 0x33F;
- int parola=6;
- /* Lucrarea 2 de completat */
- void PeriphInit(void)
- {
- __disable_irq();
- // Configure PB[7..4] as output
- RCC->AHB1ENR |= 0; /* Enable GPIOB clock */
- GPIOB->MODER &= 0; /* Reset GPIOB PB[7..4] */
- GPIOB->MODER |= 0; /* Set GPIOB PB[7..4] as ouput */
- // Configure PC[11..8] as input
- RCC->AHB1ENR |= 0; /* Enable GPIOC clock */
- GPIOC->MODER &= 0; /* Reset GPIOC PC[11..8] for input mode */
- // Configure PB[15..12] port as input and enables pull-ups
- GPIOB->MODER &= 0; /* Reset GPIOB PB[15..12] */
- GPIOB->PUPDR |= 0; /* Enable pull-ups on GPIOB PB[15..12] */
- USART2_init();
- __enable_irq();
- }
- /* TODO: change the 0's to the correct value */
- const U8 Hex7Segment[16] = {
- 0,
- 0xF9,
- 0xA4,
- 0xB0,
- 0x99,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 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 = 0x000003FF; /* PA0-PA4 ca intrari analogice*/
- /*End of Lucrarea 4*/
- void stopProgram(){
- while(1){}
- }
- U8 ReceiveMessage(void)
- {
- //U8 error = 0;
- 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);
- // printf("Unformated string: %s, size %d\n", msg_str, strlen(msg_str));
- 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("Current program: Lucrarea 5 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("%d \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("%02X\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("%d \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 %d \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: %d \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;
- default:
- printf("Invalid ID received \n");
- break;
- }
- }
- void citire()
- {
- while(k<4)
- {
- while((key = keypad_getkey()) == 0);
- if(key != corect[k])
- {
- }
- else
- {
- ++count;
- }
- ++k;
- while(keypad_getkey() != 0);
- }
- }
- int main(void)
- {
- U8 status = 0;
- PeriphInit();
- keypad_init();
- LCD_init();
- RCC->AHB1ENR |= 2; /* enable GPIOB clock */
- GPIOB->MODER &= ~0x0000ff00; /* clear pin mode */
- GPIOB->MODER |= 0x00005500; /* set pins to output mode */
- while(1)
- {
- LCD_data('P');
- LCD_data('A');
- LCD_data('S');
- LCD_data('S');
- LCD_data('W');
- LCD_data('O');
- LCD_data('R');
- LCD_data('D');
- LCD_data(':');
- delayMs(1000);
- if(tries>0)
- {
- citire();
- if(count==4)
- {
- /* clear LCD display */
- LCD_command(1);
- delayMs(1000);
- //writeLEDs(0x0);
- LCD_data('U');
- LCD_data('N');
- LCD_data('L');
- LCD_data('O');
- LCD_data('C');
- LCD_data('K');
- LCD_data('E');
- LCD_data('D');
- LCD_data('!');
- k=0;
- count=0;
- //while(1);
- }
- else
- {
- /* clear LCD display */
- LCD_command(1);
- delayMs(1000);
- tries--;
- LCD_data('W');
- LCD_data('R');
- LCD_data('O');
- LCD_data('N');
- LCD_data('G');
- writeLEDs(0xF);
- k=0;
- count=0;
- delayMs(1000);
- LCD_command(1);
- citire();
- }
- }
- else
- {
- LCD_data('L');
- LCD_data('O');
- LCD_data('C');
- LCD_data('K');
- LCD_data('E');
- LCD_data('D');
- LCD_data('!');
- delayMs(1000);
- LCD_command(1);
- }
- }
- }
- // incercare 2.0
- //-----------------------------------------------------------------------------
- /* configure SPI1 and the associated GPIO pins */
- void LCD_init(void) {
- RCC->AHB1ENR |= 1; /* enable GPIOA clock */
- RCC->AHB1ENR |= 4; /* enable GPIOC clock */
- RCC->APB2ENR |= 0x1000; /* enable SPI1 clock */
- /* PORTA 5, 7 for SPI1 MOSI and SCLK */
- GPIOA->MODER &= ~0x0000CC00; /* clear pin mode */
- GPIOA->MODER |= 0x00008800; /* set pin alternate mode */
- GPIOA->AFR[0] &= ~0xF0F00000; /* clear alt mode */
- GPIOA->AFR[0] |= 0x50500000; /* set alt mode SPI1 */
- /* PA12 as GPIO output for SPI slave select */
- GPIOA->MODER &= ~0x03000000; /* clear pin mode */
- GPIOA->MODER |= 0x01000000; /* set pin output mode */
- /* initialize SPI1 module */
- SPI1->CR1 = 0x31F;
- SPI1->CR2 = 0;
- SPI1->CR1 |= 0x40; /* enable SPI1 module */
- /* LCD controller reset sequence */
- delayMs(20);
- LCD_nibble_write(0x30, 0);
- delayMs(5);
- LCD_nibble_write(0x30, 0);
- delayMs(1);
- LCD_nibble_write(0x30, 0);
- delayMs(1);
- LCD_nibble_write(0x20, 0); /* use 4-bit data mode */
- delayMs(1);
- LCD_command(0x28); /* set 4-bit data, 2-line, 5x7 font */
- LCD_command(0x06); /* move cursor right */
- LCD_command(0x01); /* clear screen, move cursor to home */
- LCD_command(0x0F); /* turn on display, cursor blinking */
- }
- void LCD_nibble_write(char data, unsigned char control) {
- data &= 0xF0; /* clear lower nibble for control */
- control &= 0x0F; /* clear upper nibble for data */
- SPI1_write (data | control); /* RS = 0, R/W = 0 */
- SPI1_write (data | control | EN); /* pulse E */
- delayMs(0);
- SPI1_write (data);
- }
- void LCD_command(unsigned char command) {
- LCD_nibble_write(command & 0xF0, 0); /* upper nibble first */
- LCD_nibble_write(command << 4, 0); /* then lower nibble */
- if (command < 4)
- delayMs(2); /* command 1 and 2 needs up to 1.64ms */
- else
- delayMs(1); /* all others 40 us */
- }
- void LCD_data(char data) {
- LCD_nibble_write(data & 0xF0, RS); /* upper nibble first */
- LCD_nibble_write(data << 4, RS); /* then lower nibble */
- delayMs(1);
- }
- /* This function enables slave select, writes one byte to SPI1, */
- /* wait for transmit complete and deassert slave select. */
- void SPI1_write(unsigned char data) {
- while (!(SPI1->SR & 2)) {} /* wait until Transfer buffer Empty */
- GPIOA->BSRR = 0x10000000; /* assert slave select */
- SPI1->DR = data; /* write data */
- while (SPI1->SR & 0x80) {} /* wait for transmission done */
- GPIOA->BSRR = 0x00001000; /* deassert slave select */
- }
- /* 16 MHz SYSCLK */
- void delayMs(int n) {
- int i;
- for (; n > 0; n--)
- for (i = 0; i < 3195; i++) ;
- }
- 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(' ');
- }
- }
- int getPasswordFromKeypad(void) {
- int password = 0;
- char key;
- for (int i = 0; i < 4; i++) {
- do {
- key = keypad_getkey();
- } while (key == 0); /* wait for a key press */
- password = password * 10 + (key - '0'); /* assuming single digit keys */
- // btn 1 -> 0 * 10 + ( 0 - 0) = 0
- // btn 2 -> 0 * 10 + ( 1 - 0 ) = 0 + 1 = 1
- //btn 3 -> 1 * 10 + (2 - 0 ) = 10 + 2 = 12
- // btn 4 ->
- delay(); /* debounce delay */
- }
- return password;
- }
- char keypad_getkey(void)
- {
- int row, col;
- /* check to see any key is pressed first */
- outputEnableCols(0xF); /* enable all columns */
- writeCols(0xF); /* and drive them high */
- delay(); /* wait for signal to settle */
- row = readRows(); /* read all rows */
- writeCols(0x0); /* discharge all columns */
- outputEnableCols(0x0); /* disable all columns */
- if (row == 0) return 0; /* if no key pressed, return a zero */
- /* If a key is pressed, it gets here to find out which key.
- * It activates one column at a time and read the rows to see
- * which is active.
- */
- for (col = 0; col < 4; col++) {
- outputEnableCols(1 << col); /* enable one column */
- writeCols(1 << col); /* turn the active row high */
- delay(); /* wait for signal to settle */
- row = readRows(); /* read all rows */
- writeCols(0x0); /* discharge all columns */
- if (row != 0) break; /* if one of the row is low, some key is pressed. */
- }
- outputEnableCols(0x0); /* disable all columns */
- if (col == 4)
- return 0; /* if we get here, no key is pressed */
- /* gets here when one of the rows has key pressed.
- * generate a unique key code and return it.
- */
- if (row == 0x01) {return 0 + col;} // key in row 0
- if (row == 0x02) {return 4 + col; } // key in row 1
- if (row == 0x04) {return 8 + col; } // key in row 2
- if (row == 0x08) {return 12 + col; }// key in row 3
- return 0; /* just to be safe */
- }
- /* enable columns according to bit 3-0 of the parameter n */
- void outputEnableCols(char n) {
- GPIOB->MODER &= ~0xFF000000; /* clear pin mode */
- /* make the pin output according to n */
- if (n & 1)
- GPIOB->MODER |= 0x01000000;
- if (n & 2)
- GPIOB->MODER |= 0x04000000;
- if (n & 4)
- GPIOB->MODER |= 0x10000000;
- if (n & 1 << 3)
- GPIOB->MODER |= 0x40000000;
- }
- /* write columns high or low according to bit 3-0 of the parameter n */
- void writeCols(char n) {
- GPIOB->BSRR = 0xF0000000; // turn off all column pins
- GPIOB->BSRR = n << 12; // turn on column pins
- }
- /* read rows and return them in bit 3-0 */
- int readRows(void) {
- return (GPIOC->IDR & 0x0F00) >> 8;
- }
- void writeLEDs(char n) {
- GPIOB->BSRR = 0x00F00000; // turn off all LEDs
- GPIOB->BSRR = n << 4; // turn on LEDs
- }
- /* system clock at 16 MHz delay about 100 us */
- void delay(void) {
- int j;
- for (j = 0; j < 300; j++)
- ; /* do nothing */
- }
- /* This function intializes the pins connected to the keypad. */
- void keypad_init(void) {
- /* make rows input first */
- RCC->AHB1ENR |= 4; /* enable GPIOC clock */
- GPIOC->MODER &= ~0x00FF0000; /* clear pin mode */
- /* make columns input */
- RCC->AHB1ENR |= 2; /* enable GPIOB clock */
- GPIOB->MODER &= ~0xFF000000; /* clear pin mode */
- }
- //-----------------------------------------------------------------------------
- //void delayMs(int n)
- //{
- // int i;
- // for (; n > 0; n--)
- // for (i = 0; i < 3195; i++)
- // {
- // __NOP();
- // }
- //}
- /* 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement