From 75a3db6441b7c326d9bdd8191e40c7e1212ec31c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kruk?= Date: Mon, 22 Jan 2018 04:11:53 +0100 Subject: [PATCH] 22.01.2018 lysy Add: Uart interrupt Measurment --- inc/uart.h | 2 ++ src/main.c | 95 +++++++++++++++++++++++++++++++++++++++++++++--------- src/uart.c | 21 ++++++++++++ 3 files changed, 102 insertions(+), 16 deletions(-) diff --git a/inc/uart.h b/inc/uart.h index c8d556a..bd01a15 100644 --- a/inc/uart.h +++ b/inc/uart.h @@ -12,6 +12,8 @@ void sendStrig(char* s); void sendData(uint8_t* data, uint16_t size); +void sendLong(unsigned long long time); +uint16_t ReceiveData(void); #endif /* UART_H_ */ diff --git a/src/main.c b/src/main.c index 233ef0f..aa30bed 100644 --- a/src/main.c +++ b/src/main.c @@ -81,35 +81,98 @@ #include "sys/alt_stdio.h" #include "sys/alt_irq.h" #include +#include + +#include "system.h" +#include "altera_avalon_pio_regs.h" +#include "altera_up_avalon_rs232_regs.h" +#include "altera_up_avalon_rs232.h" #include "../inc/pio_driver.h" #include "../inc/delay.h" +#include "../inc/uart.h" + +// MACROS +#define TRIGGER PIN_0 +#define RESET PIN_1 +#define CONV_END PIN_2 + +// VARIABLES +char buff[30]; +uint8_t receivedData; +volatile uint8_t flag = 0; +volatile uint32_t set; + +// UART VARIABLES +volatile uint32_t uart_context; /* Kontekst do przerwa� z uartu */ +void* uart_ptr = (void*) &uart_context; + +//UART INTERRUPT FUNCTION +void handle_uart_interrupt(void* p, alt_u32 param) +{ + volatile uint32_t uart_ptr = (volatile uint32_t*) p;//pobierz kontekst + + sendStrig("Przerwanie\r\n"); -//void handle_timer_interrupt(void* p, alt_u32 param) -//{ -// // clear irq status in order to prevent retriggering -// //IOWR_ALTERA_AVALON_TIMER_STATUS(SYS_TIMER_BASE, 0); -// -// // your isr code here -// // .... -//} + receivedData = ReceiveData(); + flag = 0x01; +} int main() { - alt_putstr("KCC Project!\n"); + unsigned long long time; + + alt_putstr("KCC Project!\n"); + + //SET PIO + PIO_Direction(PIO_0_BASE , 0x0F); + + alt_irq_register(UART_0_IRQ, uart_ptr, handle_uart_interrupt); + + // SET IRQ ON + set = IORD_ALT_UP_RS232_CONTROL(UART_0_BASE); + set |= 0x01; + IOWR_ALT_UP_RS232_CONTROL(UART_0_BASE, set); + - // register the timer irq to be serviced by handle_timer_interrupt() function -// alt_irq_register(SYS_TIMER_IRQ, 0, handle_timer_interrupt); + delayMs(10); + + sendStrig("KCC PROJ\r\n"); while (1) { - PIO_SetBit(LED_PORT, LED_0); - delayMs(400); - PIO_ClearBit(LED_PORT, LED_0); - delayMs(400); - sendStrig("SIEMANO\n\r"); + if(flag){ + //CLEAR FLAG + flag = 0x0; + + //RECEIVE DATA + + sendStrig("Cos odebrano\r\n"); + + if(receivedData == 0x66){ //START CONDITION + + sendStrig("Rozpoczynam Pomiar\r\n"); + + PIO_SetBit(PIO_0_BASE , RESET); // RESET COUNTERS + delayMs(2); + PIO_ClearBit(PIO_0_BASE , RESET); + delayMs(2); + + PIO_SetBit(PIO_0_BASE , TRIGGER); // SET TRIGER + delayMs(55); + PIO_ClearBit(PIO_0_BASE , TRIGGER); + + delayMs(55); + while(!PIO_ReadBit(PIO_0_BASE, CONV_END)); // WHAIT FOR END OF MEASURE + + time = (PIO_Read(CONV_MSB_BASE) << 4) | PIO_Read(CONV_LSB_BASE); + + sendLong(time); //SEND MEASURMENT + } + } + } return 0; diff --git a/src/uart.c b/src/uart.c index ff9444c..5b3686b 100644 --- a/src/uart.c +++ b/src/uart.c @@ -15,6 +15,11 @@ static void sendByte(char byte) IOWR_ALT_UP_RS232_DATA(UART_0_BASE, byte); } +uint16_t ReceiveData(void){ + + return IORD_ALT_UP_RS232_DATA(UART_0_BASE); +} + void sendStrig(char* s) { while(*s) @@ -33,3 +38,19 @@ void sendData(uint8_t* data, uint16_t size) sendByte((char)*data++); } } +void sendOneByte(uint8_t data) +{ + sendByte((char) data); + +} +void sendLong(unsigned long long time){ + time=time|0x0000010000000000; + + sendOneByte((uint8_t)((time >> 40)&0xFF)); + sendOneByte((uint8_t)((time >> 32)&0xFF)); + sendOneByte((uint8_t)((time >> 24)&0xFF)); + sendOneByte((uint8_t)((time >> 16)&0xFF)); + sendOneByte((uint8_t)((time >> 8)&0xFF)); + sendOneByte((uint8_t)(time&0xFF)); + +}