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 0452906..87bac65 100644 --- a/src/main.c +++ b/src/main.c @@ -86,62 +86,97 @@ #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" -//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 -// // .... -//} +// 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"); + + receivedData = ReceiveData(); + flag = 0x01; +} int main() { - char buff[30]; - alt_putstr("KCC Project!\n"); unsigned long long time; - //time - 36 bits result + alt_putstr("KCC Project!\n"); + + //SET PIO + PIO_Direction(PIO_0_BASE , 0x0F); - uint16_t receivedData; + alt_irq_register(UART_0_IRQ, uart_ptr, handle_uart_interrupt); - alt_putstr("KCC Project!\n"); + // 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) { - receivedData=ReceiveData(); - if(receivedData==66){ - SendLong(time); - } - - PIO_SetBit(LED_PORT, LED_0); - delayMs(400); - PIO_ClearBit(LED_PORT, LED_0); - delayMs(400); - - sprintf(buff, "Measurement 1 ms impulse\n\r"); - alt_putstr(buff); - - sprintf(buff, "Result = %d impulses of clock\n\r", resultOfConversion); - alt_putstr(buff); - - 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 bcbcc82..bdb6636 100644 --- a/src/uart.c +++ b/src/uart.c @@ -17,7 +17,8 @@ static void sendByte(char byte) uint16_t ReceiveData(void){ - IORD_ALT_UP_RS232_DATA(UART_0_BASE); + return IORD_ALT_UP_RS232_DATA(UART_0_BASE); + } void sendStrig(char* s)