summaryrefslogtreecommitdiff
path: root/nuttx/configs/vsn/src/muxbus.c
blob: 1a1ba02c287a4bfab52b0e5f39ee68c9da6e4559 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/****************************************************************************
 * config/vsn/src/muxbus.c
 * arch/arm/src/board/muxbus.c
 *
 *   Copyright (C) 2011 Uros Platise. All rights reserved.
 *
 *   Authors: Uros Platise <uros.platise@isotel.eu>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/** \file
 *  \author Uros Platise
 *  \brief VSN Multiplexed Bus, SDIO Interface and the Analog Front-End
 */

#include <nuttx/config.h>
#include <semaphore.h>
#include <errno.h>

#include "vsn.h"
#include "stm32_gpio.h"


/****************************************************************************
 * Private data
 ****************************************************************************/

/** Set true when the bus has been locked by the SDIO interface */
bool vsn_muxbus_ownedbysdio;

/** Semaphore to resolve conflicts between muxbus locks */
sem_t vsn_muxbus_sem;


/****************************************************************************
 * Private functions
 ****************************************************************************/

void vsn_muxbus_takeownership(void)
{
    while (sem_wait(&vsn_muxbus_sem) != 0) {

        /* The only case that an error should occr here is if the wait was
         * awakened by a signal.
         */

        ASSERT(errno == EINTR);
    }
}


void vsn_muxbus_sdio_release(void)
{
    stm32_unconfiggpio(GPIO_SDIO_D0);
    stm32_unconfiggpio(GPIO_SDIO_CK);
    stm32_unconfiggpio(GPIO_SDIO_CMD);

    vsn_muxbus_ownedbysdio = FALSE;
}


void vsn_muxbus_sdio_access(void)
{
    stm32_configgpio(GPIO_SDIO_D0);
    stm32_configgpio(GPIO_SDIO_CK);
    stm32_configgpio(GPIO_SDIO_CMD);

    vsn_muxbus_ownedbysdio = TRUE;
}


/****************************************************************************
 * Public functions
 ****************************************************************************/


void vsn_muxbus_init(void)
{
    /* Put the PGA in default shut-down state */

    stm32_configgpio(GPIO_PGIA_AEN);

    /* setup semaphore in non-locked condition */

    sem_init(&vsn_muxbus_sem, 0, 1);

    /* by default give the bus to the SDIO */

    vsn_muxbus_sdio_access();
}


/**
 * We could do extra checks: who is the owner to prevent
 * unlocking from SDIO side eventhough it was not locked
 * by him, but temporarily by the PGA
 */
void stm32_muxbus_sdio_lock(bool lock)
{
    if ( lock )
        vsn_muxbus_takeownership();
    else
        sem_post(&vsn_muxbus_sem);
}


/**
 * The following rules apply for the SDcard:
 *
 *  - CMD serial line always starts with 0 (start-bit) and ends with 1 (stop-bit)
 *    The total length is always 48 bits protected by CRCs. When changing the
 *    gain, CMD must be seen as 1 on CK changes.
 *
 *  - An alternative mechanism would be to use suspend/resume commands
 *
 *  - If SDcard internal shift-register is 8-bit oriented there might be a need
 *    to shift 7 dummy bits to properly detect invalid start of packet
 *    (with start bit set as 1) to invalidate bus transitions (in case CK
 *    is changing).
 *
 * SDIO returns the bus in HiZ states, where CLK = 0, D = CMD = external pull-up
 */
int vsn_muxbus_setpgagain(int gain)
{
    /* Shutdown the Analog Devices AD8231 and exit if gain is invalid */

    stm32_gpiowrite(GPIO_PGIA_AEN, FALSE);

    if (gain < 0 || gain > 7)
        return -1;

    vsn_muxbus_takeownership();
    vsn_muxbus_sdio_release();

    /* If we have to set CLK = 1, made that first as D, CMD are 1 by pull-ups */

    if (gain & 2)
        stm32_configgpio(GPIO_PGIA_A1_H);
    else stm32_configgpio(GPIO_PGIA_A1_L);

    /* Set the D and CMD bits */

    if (gain & 1)
        stm32_configgpio(GPIO_PGIA_A0_H);
    else stm32_configgpio(GPIO_PGIA_A0_L);

    if (gain & 4)
        stm32_configgpio(GPIO_PGIA_A2_H);
    else stm32_configgpio(GPIO_PGIA_A2_L);

    /* Sample GAIN on rising edge */

    stm32_gpiowrite(GPIO_PGIA_AEN, TRUE);

    /* Release D and CMD pins to 1; however shorten rising edge actively */

    stm32_gpiowrite(GPIO_PGIA_A0_H, TRUE);
    stm32_gpiowrite(GPIO_PGIA_A2_H, TRUE);

    stm32_unconfiggpio(GPIO_PGIA_A0_H);
    stm32_unconfiggpio(GPIO_PGIA_A2_H);

    /* Release CLK by going down and return the bus */

    stm32_unconfiggpio(GPIO_PGIA_A1_L);

    vsn_muxbus_sdio_access();
    sem_post(&vsn_muxbus_sem);

    return gain;
}