Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- /*
- * intsimple.c
- *
- * This module demonstrates will contain code for handling an
- * interrupt.
- *
- * To test is, simply run it. Note that you may have to do something
- * to cause the interrupts to be generated (e.g. press a key if
- * handling the keyboard interrupt).
- *
- * intsimple
- *
- */
- #include <errno.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/neutrino.h>
- #include <sys/syspage.h>
- #include <hw/inout.h>
- #define PORT 0x2F8 //COM1 - 0x3F8, COM2 - 0x2F8
- #define INT_RCV 0x01
- #define INT_DISABLE 0x00
- unsigned READ_RS232(unsigned char *dana);
- void SET_RS232();
- void SEND_COMMAND(unsigned char COMM_TRANS);
- void SET_INT_SRC(unsigned char INT_SOURCE);
- void CLR_INT_SRC(unsigned char INT_SOURCE);
- void send_int(unsigned char data);
- char *progname = "COM_2";
- struct sigevent int_event; // the event to wake up the thread
- volatile unsigned char buf;
- volatile char buf_in[16];
- volatile char* buf_out = "BCD";
- volatile int idx;
- volatile int idx_rx;
- volatile char event_type;
- const struct sigevent *com2_hdlr( void *blah, int id )
- {
- static int st = 0;
- st = in8(PORT+2);
- event_type = 0;
- //sprawdzenie źródła przerwania
- if ((st & 4) != 0)
- {
- //buf = in8(PORT);
- event_type = 1;
- buf_in[idx_rx++] = in8(PORT);
- buf_in[idx_rx] = NULL;
- return &int_event;
- }
- if((st & 2) != 0)
- {
- out8(PORT, 'B');
- idx++;
- if(idx>2)
- {
- out8(PORT+1, 0x01);
- //event_type = 2;
- }
- return &int_event;
- }
- }
- int main (int argc, char **argv){
- int id,i;
- idx = 0;
- idx_rx = 0;
- setvbuf (stdout, NULL, _IOLBF, 0);
- printf ("%s: starting...\n", progname);
- // request I/O privity
- ThreadCtl(_NTO_TCTL_IO, 0 );
- // set up an event for the handler or the kernel to use to wake up
- // this thread. Use whatever type of event and event handling you want
- SIGEV_INTR_INIT( &int_event );
- // either register an interrupt handler or the event
- SET_RS232();
- id = InterruptAttach(3, com2_hdlr, NULL, 0, _NTO_INTR_FLAGS_TRK_MSK );
- out8(PORT+1, 0x03);
- //SEND_COMMAND('A');
- while (1) {
- // block here waiting for the event
- InterruptWait(0, NULL );
- // if using a high frequency interrupt, don't print every interrupt
- //printf ("%s: we got an event after 1500 timer ticks and unblocked\n", progname);
- if(event_type == 1)
- printf("received: %s\n", buf_in);
- else if(event_type == 2)
- printf("sent: %s\n", buf_out);
- }
- }
- unsigned READ_RS232(unsigned char *dana){
- unsigned c = in8(PORT+5);
- if(c & 1){
- *dana = in8(PORT);
- return 1;
- }
- else{
- return 0;
- }
- }
- void SET_RS232(){
- unsigned char temp,licz;
- out8(PORT+3, 0x80); /* SET DLAB ON */
- out8(PORT+0, 0x06); /* SET BAUND RATE - 19200 BPS */
- out8(PORT+1, 0x00); /* DISABLE INTERRUPTS */
- out8(PORT+3, 0x1b); /* PARITY, 1 STOP BIT, 8 BITS */
- //out8(PORT+1, 1); /* */
- out8(PORT+2, 0xc7); /* FIFO CONTROL */
- out8(PORT+4, 0x18); /* TURN ON DTR, RTS and OUT2 */
- licz=16;
- do{
- licz--;
- if(!licz) break;
- }while(READ_RS232((unsigned char*)&temp));
- }
- void SEND_COMMAND(unsigned char COMM_TRANS){
- out8(PORT, COMM_TRANS);
- }
- void SET_INT_SRC(unsigned char INT_SOURCE){
- out8(PORT+1, INT_SOURCE);
- }
- void CLR_INT_SRC(unsigned char INT_SOURCE){
- out8(PORT+1, INT_SOURCE);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement