Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.42 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4.  
  5. /*
  6.  *  intsimple.c
  7.  *
  8.  *  This module demonstrates will contain code for handling an
  9.  *  interrupt.
  10.  *
  11.  *  To test is, simply run it.  Note that you may have to do something
  12.  *  to cause the interrupts to be generated (e.g. press a key if
  13.  *  handling the keyboard interrupt).
  14.  *
  15.  *    intsimple
  16.  *
  17. */
  18.  
  19. #include <errno.h>
  20. #include <fcntl.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <sys/neutrino.h>
  26. #include <sys/syspage.h>
  27. #include <hw/inout.h>
  28.  
  29.  
  30.  
  31. #define PORT 0x2F8 //COM1 - 0x3F8, COM2 - 0x2F8
  32. #define INT_RCV 0x01
  33. #define INT_DISABLE 0x00
  34.  
  35. unsigned READ_RS232(unsigned char *dana);
  36. void SET_RS232();
  37. void SEND_COMMAND(unsigned char COMM_TRANS);
  38. void SET_INT_SRC(unsigned char INT_SOURCE);
  39. void CLR_INT_SRC(unsigned char INT_SOURCE);
  40. void send_int(unsigned char data);
  41.  
  42. char *progname = "COM_2";
  43.  
  44. struct sigevent int_event;  // the event to wake up the thread
  45.  
  46. volatile unsigned char buf;
  47. volatile char buf_in[16];
  48. volatile char* buf_out = "BCD";
  49. volatile int idx;
  50. volatile int idx_rx;
  51. volatile char event_type;
  52.  
  53. const struct sigevent *com2_hdlr( void *blah, int id )
  54. {
  55.     static int st = 0;
  56.     st = in8(PORT+2);
  57.     event_type = 0;
  58.     //sprawdzenie źródła przerwania
  59.     if ((st & 4) != 0)
  60.     {
  61.         //buf = in8(PORT);
  62.         event_type = 1;
  63.         buf_in[idx_rx++] = in8(PORT);
  64.         buf_in[idx_rx] = NULL;
  65.         return &int_event;
  66.     }
  67.     if((st & 2) != 0)
  68.     {
  69.  
  70.         out8(PORT, 'B');
  71.         idx++;
  72.         if(idx>2)
  73.         {
  74.             out8(PORT+1, 0x01);
  75.             //event_type = 2;
  76.         }
  77.  
  78.         return &int_event;
  79.     }
  80.  
  81. }
  82.  
  83. int main (int argc, char **argv){
  84.     int id,i;
  85.  
  86.     idx = 0;
  87.     idx_rx = 0;
  88.  
  89.     setvbuf (stdout, NULL, _IOLBF, 0);
  90.  
  91.     printf ("%s:  starting...\n", progname);
  92.  
  93.     // request I/O privity
  94.     ThreadCtl(_NTO_TCTL_IO, 0 );
  95.     // set up an event for the handler or the kernel to use to wake up
  96.     // this thread.  Use whatever type of event and event handling you want
  97.     SIGEV_INTR_INIT( &int_event );
  98.     // either register an interrupt handler or the event
  99.     SET_RS232();
  100.  
  101.     id = InterruptAttach(3, com2_hdlr, NULL, 0, _NTO_INTR_FLAGS_TRK_MSK );
  102.  
  103.     out8(PORT+1, 0x03);
  104.  
  105.     //SEND_COMMAND('A');
  106.  
  107.     while (1) {
  108.         // block here waiting for the event
  109.         InterruptWait(0, NULL );
  110.  
  111.         // if using a high frequency interrupt, don't print every interrupt
  112.         //printf ("%s:  we got an event after 1500 timer ticks and unblocked\n", progname);
  113.         if(event_type == 1)
  114.             printf("received: %s\n", buf_in);
  115.         else if(event_type == 2)
  116.             printf("sent: %s\n", buf_out);
  117.  
  118.     }
  119.  
  120. }
  121.  
  122.  
  123. unsigned READ_RS232(unsigned char *dana){
  124.  
  125.   unsigned c = in8(PORT+5);
  126.   if(c & 1){
  127.    *dana = in8(PORT);
  128.    return 1;
  129.   }
  130.   else{
  131.    return 0;
  132.   }
  133. }
  134.  
  135. void SET_RS232(){
  136.   unsigned char temp,licz;
  137.  
  138.   out8(PORT+3, 0x80); /* SET DLAB ON */
  139.   out8(PORT+0, 0x06); /* SET BAUND RATE - 19200 BPS */
  140.   out8(PORT+1,  0x00); /* DISABLE INTERRUPTS */
  141.   out8(PORT+3, 0x1b); /* PARITY, 1 STOP BIT, 8 BITS */
  142.   //out8(PORT+1, 1); /* */
  143.   out8(PORT+2, 0xc7); /* FIFO CONTROL */
  144.   out8(PORT+4, 0x18); /* TURN ON DTR, RTS and OUT2 */
  145.   licz=16;
  146.   do{
  147.    licz--;
  148.    if(!licz) break;
  149.   }while(READ_RS232((unsigned char*)&temp));
  150. }
  151.  
  152. void SEND_COMMAND(unsigned char COMM_TRANS){
  153.    out8(PORT, COMM_TRANS);
  154. }
  155.  
  156. void SET_INT_SRC(unsigned char INT_SOURCE){
  157.     out8(PORT+1, INT_SOURCE);
  158. }
  159.  
  160. void CLR_INT_SRC(unsigned char INT_SOURCE){
  161.     out8(PORT+1, INT_SOURCE);
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement