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 <ctype.h>
- #include <stdlib.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 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};
- /* 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 = 0x33C; /* viteza transfer si 8 biti date, master mode */
- static const U32 GPIOB_OTYPER = ~0x00000300; /* set as open drain */
- static const U32 GPIOB_PUPDR = ~0x00050000; /* setup SCL, SDA ca pull-up pentru I2C*/
- static const U16 I2C_CR1 = 0x8000; /* SW resetm ie;ire reset, enable I2C1*/
- static const U16 I2CC_CCR = 80; /* 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 send_str(char *str)
- {
- int i=0;
- while(str[i]!='\0')
- {
- LCD_data(str[i]);
- i++;
- }
- }
- /*
- * Scan keypad and write the keycode to LEDs.
- * The matrix keypad of the EduBase board is connected to
- * col 0 - PB12
- * col 1 - PB13
- * col 2 - PB14
- * col 3 - PB15
- * row 0 - PC8
- * row 1 - PC9
- * row 2 - PC10
- * row 3 - PC11
- *
- * The four LEDs are connected to
- * LED3 - PB7
- * LED2 - PB6
- * LED1 - PB5
- * LED0 - PB4
- */
- #include "STM32F4XX.h"
- void delay(void);
- void keypad_init(void);
- char keypad_getkey(void);
- void writeLEDs(char n);
- int readRows(void);
- void outputEnableCols(char n);
- void writeCols(char n);
- /* use bit 3-0 of parameter n to turn on/off LEDs
- 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 */
- }
- /*
- * This is a non-blocking function to read the keypad.
- * If a key is pressed, it returns a keycode. Otherwise, a zero
- * is returned.
- * The keypad is arranged as a 4x4 matrix. There are pull-down
- * resistors for all pins of the rows on the EduBase board.
- * When no keys are pressed, these pins are low.
- * The columns used as output that can be driven high.
- * First all columns are driven high and the row pins are read. If no
- * keys are pressed, it reads a zero. Otherwise, some key is pressed.
- * If any key is pressed, the program drives one column high at a time and
- * leave the rest of the columns inactive (float) then read the input (row) pins.
- * Knowing which column is active and which row is active, the program
- * can decide which key is pressed.
- */
- 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;}
- if (row == 0x02) { return 4 + col;}
- if (row == 0x04) { return 8 + col;}
- 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;
- }
- int main(void)
- {
- int b2[4]={2,3,7,1};
- int i=0,tries=3;
- int cont=0;
- U8 status = 0;
- PeriphInit();
- if(SPI_CR1 != 0) {
- LCD_init(SPI_CR1);
- }
- IRQInit();
- char key;
- /* initialize pins connected to keypad */
- keypad_init();
- /* initialize LEDs for display */
- RCC->AHB1ENR |= 2; /* enable GPIOB clock */
- GPIOB->MODER &= ~0x0000ff00; /* clear pin mode */
- GPIOB->MODER |= 0x00005500; /* set pins to output mode */
- printf("Lucrarea 6 interfete seriale \n");
- printf("please enter the packet in the following format ID Byte0 Byte1 ... Byte7 \n");
- while(1)
- {
- char st[100];
- char *s = "LOCKED";
- sprintf(st,"%s",s);
- send_str(st);
- //while(tries!=0)
- //{
- GPIOB->BSRR = 0x00F00000;
- while(i<4)
- {
- while((key = keypad_getkey()) == 0);
- if(key != b2[i])
- {
- }
- else
- {
- cont++;
- }
- i++;
- while(keypad_getkey() != 0);
- }
- if(cont == 4)
- {
- LCD_command(0x01);
- s = "UNLOCKED";
- sprintf(st,"%s",s);
- send_str(st);
- }
- else
- {
- GPIOB->BSRR = ~0x00F00000;
- }
- if (TimerCountDown_U16 == 0)
- {
- CyclicMessage_U8 = 0;
- TimerCountDown_U16 = (U16)2 * SECONDS_TO_COUNT_U8;
- StopPeriodicTimer();
- TIM3_Stop();
- TIM8_Stop();
- DAC_Stop();
- printf("please enter the packet in the following format ID Byte0 Byte1 ... Byte7 \n");
- }
- if (CyclicMessage_U8 != 1)
- {
- status = ReceiveMessage();
- }
- if (status)
- {
- if (CyclicMessage_U8 != 1)
- PrintReceivedMessage(msg);
- ProcessMessage(msg);
- if (CyclicMessage_U8 != 1)
- printf("please enter the packet in the following format ID Byte0 Byte1 ... Byte7 \n");
- delayMs(10);
- }
- else
- {
- printf("Wrong message format \n");
- }
- }
- }
- /* 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