Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:44

0001 /*  ir.c
0002  *
0003  *  Milkymist RC5 IR driver for RTEMS
0004  *
0005  *  The license and distribution terms for this file may be
0006  *  found in the file LICENSE in this distribution or at
0007  *  http://www.rtems.org/license/LICENSE.
0008  *
0009  *  COPYRIGHT (c) 2010, 2011 Sebastien Bourdeauducq
0010  */
0011 
0012 #define RTEMS_STATUS_CHECKS_USE_PRINTK
0013 
0014 #include <stdlib.h>
0015 #include <sys/types.h>
0016 #include <rtems.h>
0017 #include <rtems/status-checks.h>
0018 #include <bsp.h>
0019 #include <bsp/irq-generic.h>
0020 #include <rtems/libio.h>
0021 #include "../include/system_conf.h"
0022 #include <bsp/milkymist_ir.h>
0023 
0024 #define DEVICE_NAME "/dev/ir"
0025 
0026 static rtems_id ir_q;
0027 
0028 static rtems_isr interrupt_handler(rtems_vector_number n)
0029 {
0030   unsigned short int msg;
0031 
0032   lm32_interrupt_ack(1 << MM_IRQ_IR);
0033   msg = MM_READ(MM_IR_RX);
0034   rtems_message_queue_send(ir_q, &msg, 2);
0035 }
0036 
0037 rtems_device_driver ir_initialize(
0038   rtems_device_major_number major,
0039   rtems_device_minor_number minor,
0040   void *arg
0041 )
0042 {
0043   rtems_status_code sc;
0044   rtems_isr_entry dummy;
0045 
0046   sc = rtems_io_register_name(DEVICE_NAME, major, 0);
0047   RTEMS_CHECK_SC(sc, "create IR input device");
0048 
0049  sc = rtems_message_queue_create(
0050     rtems_build_name('R', 'C', '5', 'Q'),
0051     64,
0052     2,
0053     0,
0054     &ir_q
0055   );
0056   RTEMS_CHECK_SC(sc, "create IR queue");
0057 
0058   rtems_interrupt_catch(interrupt_handler, MM_IRQ_IR, &dummy);
0059   bsp_interrupt_vector_enable(MM_IRQ_IR);
0060 
0061   return RTEMS_SUCCESSFUL;
0062 }
0063 
0064 rtems_device_driver ir_open(
0065   rtems_device_major_number major,
0066   rtems_device_minor_number minor,
0067   void *arg
0068 )
0069 {
0070   uint32_t count;
0071 
0072   rtems_message_queue_flush(ir_q, &count);
0073   return RTEMS_SUCCESSFUL;
0074 }
0075 
0076 rtems_device_driver ir_read(
0077   rtems_device_major_number major,
0078   rtems_device_minor_number minor,
0079   void *arg
0080 )
0081 {
0082   rtems_libio_rw_args_t *rw_args = (rtems_libio_rw_args_t *)arg;
0083   rtems_status_code sc;
0084 
0085   if (rw_args->count < 2) {
0086     rw_args->bytes_moved = 0;
0087     return RTEMS_UNSATISFIED;
0088   }
0089 
0090   sc = rtems_message_queue_receive(
0091     ir_q,
0092     rw_args->buffer,
0093     (size_t *)&rw_args->bytes_moved,
0094     RTEMS_WAIT,
0095     RTEMS_NO_TIMEOUT
0096   );
0097 
0098   if(sc == RTEMS_SUCCESSFUL)
0099     return RTEMS_SUCCESSFUL;
0100   else {
0101     rw_args->bytes_moved = 0;
0102     return RTEMS_UNSATISFIED;
0103   }
0104 }