source: rtems/bsps/lm32/shared/milkymist_gpio/gpio.c @ 762fa62

5
Last change on this file since 762fa62 was 21978523, checked in by Sebastian Huber <sebastian.huber@…>, on Apr 23, 2018 at 11:31:18 AM

bsps/lm32: Move shared drivers to bsps

This patch is a part of the BSP source reorganization.

Update #3285.

  • Property mode set to 100644
File size: 2.4 KB
Line 
1/*  gpio.c
2 *
3 *  GPIO driver for the Milkymist One board
4 *
5 *  The license and distribution terms for this file may be
6 *  found in the file LICENSE in this distribution or at
7 *  http://www.rtems.org/license/LICENSE.
8 *
9 *  COPYRIGHT (c) 2010, 2011 Sebastien Bourdeauducq
10 *  COPYRIGHT (c) Yann Sionneau <yann.sionneau@telecom-sudparis.eu> (GSoC 2010)
11 *  Telecom SudParis
12 */
13
14#define RTEMS_STATUS_CHECKS_USE_PRINTK
15
16#include <stdlib.h>
17#include <stdio.h>
18#include <errno.h>
19#include <sys/types.h>
20#include <rtems.h>
21#include <rtems/status-checks.h>
22#include <bsp.h>
23#include <rtems/libio.h>
24#include "../include/system_conf.h"
25#include <bsp/milkymist_gpio.h>
26
27struct milkymist_gpio {
28  char *name;
29  unsigned int mask;
30  bool readonly;
31};
32
33static const struct milkymist_gpio gpio[] = {
34  {
35    .name = "/dev/led1",
36    .mask = GPIO_LED1,
37    .readonly = false
38  },
39  {
40    .name = "/dev/led2",
41    .mask = GPIO_LED2,
42    .readonly = false
43  },
44};
45
46rtems_device_driver gpio_initialize(
47  rtems_device_major_number major,
48  rtems_device_minor_number minor,
49  void *arg
50)
51{
52  rtems_status_code sc;
53  int i;
54
55  for (i=0;i<sizeof(gpio)/sizeof(struct milkymist_gpio);i++) {
56    sc = rtems_io_register_name(gpio[i].name, major, i);
57    RTEMS_CHECK_SC(sc, "create GPIO device");
58  }
59
60  return RTEMS_SUCCESSFUL;
61}
62
63rtems_device_driver gpio_read(
64  rtems_device_major_number major,
65  rtems_device_minor_number minor,
66  void *arg
67)
68{
69  unsigned int data;
70
71  rtems_libio_rw_args_t *rw_args = (rtems_libio_rw_args_t *)arg;
72
73  if (rw_args->offset > 0) {
74    rw_args->bytes_moved = 0;
75    return RTEMS_SUCCESSFUL;
76  }
77
78  rw_args->bytes_moved = 1;
79
80  if (gpio[minor].readonly)
81    data = MM_READ(MM_GPIO_IN);
82  else
83    data = MM_READ(MM_GPIO_OUT);
84
85  if (data & gpio[minor].mask)
86    *(uint8_t *)rw_args->buffer = '1';
87  else
88    *(uint8_t *)rw_args->buffer = '0';
89
90  return RTEMS_SUCCESSFUL;
91}
92
93rtems_device_driver gpio_write(
94  rtems_device_major_number  major,
95  rtems_device_minor_number  minor,
96  void *arg
97)
98{
99  rtems_libio_rw_args_t *rw_args = (rtems_libio_rw_args_t *)arg;
100
101  if (gpio[minor].readonly) {
102    rw_args->bytes_moved = 0;
103    return RTEMS_UNSATISFIED;
104  }
105
106  if (rw_args->offset > 0) {
107    rw_args->bytes_moved = 0;
108    return RTEMS_SUCCESSFUL;
109  }
110
111  rw_args->bytes_moved = 1;
112
113  if (*(uint8_t *)rw_args->buffer == '1')
114    MM_WRITE(MM_GPIO_OUT, MM_READ(MM_GPIO_OUT)|gpio[minor].mask);
115  else
116    MM_WRITE(MM_GPIO_OUT, MM_READ(MM_GPIO_OUT) & ~gpio[minor].mask);
117
118  return RTEMS_SUCCESSFUL;
119}
Note: See TracBrowser for help on using the repository browser.