Skip to content

Commit

Permalink
22.01.2018 lysy
Browse files Browse the repository at this point in the history
Add:
Uart interrupt
Measurment
  • Loading branch information
krukm94 committed Jan 22, 2018
1 parent ac20939 commit 75a3db6
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 16 deletions.
2 changes: 2 additions & 0 deletions inc/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */
95 changes: 79 additions & 16 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,35 +81,98 @@
#include "sys/alt_stdio.h"
#include "sys/alt_irq.h"
#include <stdint.h>
#include <stdio.h>

#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;
Expand Down
21 changes: 21 additions & 0 deletions src/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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));

}

0 comments on commit 75a3db6

Please sign in to comment.