gh_adc.c 16.2 KB
Newer Older
G
gokemicro 已提交
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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
/******************************************************************************
**
** \file      gh_adc.c
**
** \brief     ADC.
**
**            Copyright:   2012 - 2016 (C) GoKe Microelectronics
**
** \attention THIS SAMPLE CODE IS PROVIDED AS IS. GOKE MICROELECTRONICS
**            ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR
**            OMMISSIONS.
**
** \note      Do not modify this file as it is generated automatically.
**
******************************************************************************/
#ifndef SRC_INLINE
#include "gh_adc.h"
#endif

/*----------------------------------------------------------------------------*/
/* registers                                                                  */
/*----------------------------------------------------------------------------*/
#define REG_ADC_AUX_ATOP_REAL                               FIO_ADDRESS(ADC,0x60020A00) /* read/write */
#define REG_ADC_GPIO_REAL                                   FIO_ADDRESS(ADC,0x60020A04) /* read/write */
#define REG_ADC_CONTROL_REAL                                FIO_ADDRESS(ADC,0x7000D000) /* read/write */
#define REG_ADC_READDATA_REAL                               FIO_ADDRESS(ADC,0x7000D004) /* read */
#define REG_ADC_ENABLE_REAL                                 FIO_ADDRESS(ADC,0x7000D018) /* read/write */
#define REG_ADC_INTCONTROL_REAL                             FIO_ADDRESS(ADC,0x7000D044) /* read/write */

/*----------------------------------------------------------------------------*/
/* bit group structures                                                       */
/*----------------------------------------------------------------------------*/
typedef union { /* ADC_AUX_ATOP */
    U32 all;
    struct {
        U32 sar_pd                      : 1;
        U32 sar_maxnsel                 : 3;
        U32 sar_maxsel                  : 4;
        U32 sar_oneshot                 : 1;
        U32 sar_freerun                 : 1;
        U32 sar_refnsel                 : 2;
        U32 sar_refsel                  : 3;
        U32                             : 17;
    } bitc;
} GH_ADC_AUX_ATOP_REAL_S;

typedef union { /* ADC_GPIO */
    U32 all;
    struct {
        U32 sar_test                    : 4;
        U32 sar_key_pge                 : 4;
        U32                             : 24;
    } bitc;
} GH_ADC_GPIO_REAL_S;

typedef union { /* ADC_Control */
    U32 all;
    struct {
        U32 status                      : 1;
        U32 start                       : 1;
        U32                             : 1;
        U32 channel                     : 3;
        U32                             : 26;
    } bitc;
} GH_ADC_CONTROL_REAL_S;

typedef union { /* ADC_IntControl */
    U32 all;
    struct {
        U32 val_lo                      : 10;
        U32                             : 5;
        U32 val_hi                      : 10;
        U32                             : 5;
        U32 en_lo                       : 1;
        U32 en_hi                       : 1;
    } bitc;
} GH_ADC_INTCONTROL_REAL_S;


/*----------------------------------------------------------------------------*/
/* mirror variables                                                           */
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/* register ADC_AUX_ATOP (read/write)                                         */
/*----------------------------------------------------------------------------*/
GH_INLINE void GH_ADC_set_AUX_ATOP(U32 data)
{
    GH_ADC_AUX_ATOP_REAL_S real;
    GH_ADC_AUX_ATOP_S dummy;
    dummy.all = data ;
    real.bitc.sar_pd = dummy.bitc.sar_pd;
    real.bitc.sar_maxnsel = dummy.bitc.sar_maxnsel;
    real.bitc.sar_maxsel = dummy.bitc.sar_maxsel;
    real.bitc.sar_oneshot = dummy.bitc.sar_oneshot;
    real.bitc.sar_freerun = dummy.bitc.sar_freerun;
    real.bitc.sar_refnsel = dummy.bitc.sar_refnsel;
    real.bitc.sar_refsel = dummy.bitc.sar_refsel;
    *(volatile U32 *)REG_ADC_AUX_ATOP_REAL = real.all;
}
GH_INLINE U32  GH_ADC_get_AUX_ATOP(void)
{
    GH_ADC_AUX_ATOP_REAL_S real;
    GH_ADC_AUX_ATOP_S dummy;
     dummy.all =  0;
     real.all = (*(volatile U32 *)REG_ADC_AUX_ATOP_REAL);

    dummy.bitc.sar_pd = real.bitc.sar_pd;
    dummy.bitc.sar_maxnsel = real.bitc.sar_maxnsel;
    dummy.bitc.sar_maxsel = real.bitc.sar_maxsel;
    dummy.bitc.sar_oneshot = real.bitc.sar_oneshot;
    dummy.bitc.sar_freerun = real.bitc.sar_freerun;
    dummy.bitc.sar_refnsel = real.bitc.sar_refnsel;
    dummy.bitc.sar_refsel = real.bitc.sar_refsel;
    return dummy.all;
}
GH_INLINE void GH_ADC_set_AUX_ATOP_sar_pd(U8 data)
{
    GH_ADC_AUX_ATOP_REAL_S d;
    d.all = *(volatile U32 *)REG_ADC_AUX_ATOP_REAL;
    d.bitc.sar_pd = data;
    *(volatile U32 *)REG_ADC_AUX_ATOP_REAL = d.all;
}
GH_INLINE U8   GH_ADC_get_AUX_ATOP_sar_pd(void)
{
    GH_ADC_AUX_ATOP_REAL_S tmp_value;
    U32 value = (*(volatile U32 *)REG_ADC_AUX_ATOP_REAL);

    tmp_value.all = value;
    return tmp_value.bitc.sar_pd;
}
GH_INLINE void GH_ADC_set_AUX_ATOP_sar_maxnsel(U8 data)
{
    GH_ADC_AUX_ATOP_REAL_S d;
    d.all = *(volatile U32 *)REG_ADC_AUX_ATOP_REAL;
    d.bitc.sar_maxnsel = data;
    *(volatile U32 *)REG_ADC_AUX_ATOP_REAL = d.all;
}
GH_INLINE U8   GH_ADC_get_AUX_ATOP_sar_maxnsel(void)
{
    GH_ADC_AUX_ATOP_REAL_S tmp_value;
    U32 value = (*(volatile U32 *)REG_ADC_AUX_ATOP_REAL);

    tmp_value.all = value;
    return tmp_value.bitc.sar_maxnsel;
}
GH_INLINE void GH_ADC_set_AUX_ATOP_sar_maxsel(U8 data)
{
    GH_ADC_AUX_ATOP_REAL_S d;
    d.all = *(volatile U32 *)REG_ADC_AUX_ATOP_REAL;
    d.bitc.sar_maxsel = data;
    *(volatile U32 *)REG_ADC_AUX_ATOP_REAL = d.all;
}
GH_INLINE U8   GH_ADC_get_AUX_ATOP_sar_maxsel(void)
{
    GH_ADC_AUX_ATOP_REAL_S tmp_value;
    U32 value = (*(volatile U32 *)REG_ADC_AUX_ATOP_REAL);

    tmp_value.all = value;
    return tmp_value.bitc.sar_maxsel;
}
GH_INLINE void GH_ADC_set_AUX_ATOP_sar_oneshot(U8 data)
{
    GH_ADC_AUX_ATOP_REAL_S d;
    d.all = *(volatile U32 *)REG_ADC_AUX_ATOP_REAL;
    d.bitc.sar_oneshot = data;
    *(volatile U32 *)REG_ADC_AUX_ATOP_REAL = d.all;
}
GH_INLINE U8   GH_ADC_get_AUX_ATOP_sar_oneshot(void)
{
    GH_ADC_AUX_ATOP_REAL_S tmp_value;
    U32 value = (*(volatile U32 *)REG_ADC_AUX_ATOP_REAL);

    tmp_value.all = value;
    return tmp_value.bitc.sar_oneshot;
}
GH_INLINE void GH_ADC_set_AUX_ATOP_sar_freerun(U8 data)
{
    GH_ADC_AUX_ATOP_REAL_S d;
    d.all = *(volatile U32 *)REG_ADC_AUX_ATOP_REAL;
    d.bitc.sar_freerun = data;
    *(volatile U32 *)REG_ADC_AUX_ATOP_REAL = d.all;
}
GH_INLINE U8   GH_ADC_get_AUX_ATOP_sar_freerun(void)
{
    GH_ADC_AUX_ATOP_REAL_S tmp_value;
    U32 value = (*(volatile U32 *)REG_ADC_AUX_ATOP_REAL);

    tmp_value.all = value;
    return tmp_value.bitc.sar_freerun;
}
GH_INLINE void GH_ADC_set_AUX_ATOP_sar_refnsel(U8 data)
{
    GH_ADC_AUX_ATOP_REAL_S d;
    d.all = *(volatile U32 *)REG_ADC_AUX_ATOP_REAL;
    d.bitc.sar_refnsel = data;
    *(volatile U32 *)REG_ADC_AUX_ATOP_REAL = d.all;
}
GH_INLINE U8   GH_ADC_get_AUX_ATOP_sar_refnsel(void)
{
    GH_ADC_AUX_ATOP_REAL_S tmp_value;
    U32 value = (*(volatile U32 *)REG_ADC_AUX_ATOP_REAL);

    tmp_value.all = value;
    return tmp_value.bitc.sar_refnsel;
}
GH_INLINE void GH_ADC_set_AUX_ATOP_sar_refsel(U8 data)
{
    GH_ADC_AUX_ATOP_REAL_S d;
    d.all = *(volatile U32 *)REG_ADC_AUX_ATOP_REAL;
    d.bitc.sar_refsel = data;
    *(volatile U32 *)REG_ADC_AUX_ATOP_REAL = d.all;
}
GH_INLINE U8   GH_ADC_get_AUX_ATOP_sar_refsel(void)
{
    GH_ADC_AUX_ATOP_REAL_S tmp_value;
    U32 value = (*(volatile U32 *)REG_ADC_AUX_ATOP_REAL);

    tmp_value.all = value;
    return tmp_value.bitc.sar_refsel;
}

/*----------------------------------------------------------------------------*/
/* register ADC_GPIO (read/write)                                             */
/*----------------------------------------------------------------------------*/
GH_INLINE void GH_ADC_set_GPIO(U32 data)
{
    GH_ADC_GPIO_REAL_S real;
    GH_ADC_GPIO_S dummy;
    dummy.all = data ;
    real.bitc.sar_test = dummy.bitc.sar_test;
    real.bitc.sar_key_pge = dummy.bitc.sar_key_pge;
    *(volatile U32 *)REG_ADC_GPIO_REAL = real.all;
}
GH_INLINE U32  GH_ADC_get_GPIO(void)
{
    GH_ADC_GPIO_REAL_S real;
    GH_ADC_GPIO_S dummy;
     dummy.all =  0;
     real.all = (*(volatile U32 *)REG_ADC_GPIO_REAL);

    dummy.bitc.sar_test = real.bitc.sar_test;
    dummy.bitc.sar_key_pge = real.bitc.sar_key_pge;
    return dummy.all;
}
GH_INLINE void GH_ADC_set_GPIO_sar_test(U8 data)
{
    GH_ADC_GPIO_REAL_S d;
    d.all = *(volatile U32 *)REG_ADC_GPIO_REAL;
    d.bitc.sar_test = data;
    *(volatile U32 *)REG_ADC_GPIO_REAL = d.all;
}
GH_INLINE U8   GH_ADC_get_GPIO_sar_test(void)
{
    GH_ADC_GPIO_REAL_S tmp_value;
    U32 value = (*(volatile U32 *)REG_ADC_GPIO_REAL);

    tmp_value.all = value;
    return tmp_value.bitc.sar_test;
}
GH_INLINE void GH_ADC_set_GPIO_sar_key_pge(U8 data)
{
    GH_ADC_GPIO_REAL_S d;
    d.all = *(volatile U32 *)REG_ADC_GPIO_REAL;
    d.bitc.sar_key_pge = data;
    *(volatile U32 *)REG_ADC_GPIO_REAL = d.all;
}
GH_INLINE U8   GH_ADC_get_GPIO_sar_key_pge(void)
{
    GH_ADC_GPIO_REAL_S tmp_value;
    U32 value = (*(volatile U32 *)REG_ADC_GPIO_REAL);

    tmp_value.all = value;
    return tmp_value.bitc.sar_key_pge;
}

/*----------------------------------------------------------------------------*/
/* register ADC_Control (read/write)                                          */
/*----------------------------------------------------------------------------*/
GH_INLINE void GH_ADC_set_Control(U32 data)
{
    GH_ADC_CONTROL_REAL_S real;
    GH_ADC_CONTROL_S dummy;
    dummy.all = data ;
    real.bitc.status = dummy.bitc.status;
    real.bitc.start = dummy.bitc.start;
    real.bitc.channel = dummy.bitc.channel;
    *(volatile U32 *)REG_ADC_CONTROL_REAL = real.all;
}
GH_INLINE U32  GH_ADC_get_Control(void)
{
    GH_ADC_CONTROL_REAL_S real;
    GH_ADC_CONTROL_S dummy;
     dummy.all =  0;
     real.all = (*(volatile U32 *)REG_ADC_CONTROL_REAL);

    dummy.bitc.status = real.bitc.status;
    dummy.bitc.start = real.bitc.start;
    dummy.bitc.channel = real.bitc.channel;
    return dummy.all;
}
GH_INLINE void GH_ADC_set_Control_status(U8 data)
{
    GH_ADC_CONTROL_REAL_S d;
    d.all = *(volatile U32 *)REG_ADC_CONTROL_REAL;
    d.bitc.status = data;
    *(volatile U32 *)REG_ADC_CONTROL_REAL = d.all;
}
GH_INLINE U8   GH_ADC_get_Control_status(void)
{
    GH_ADC_CONTROL_REAL_S tmp_value;
    U32 value = (*(volatile U32 *)REG_ADC_CONTROL_REAL);

    tmp_value.all = value;
    return tmp_value.bitc.status;
}
GH_INLINE void GH_ADC_set_Control_start(U8 data)
{
    GH_ADC_CONTROL_REAL_S d;
    d.all = *(volatile U32 *)REG_ADC_CONTROL_REAL;
    d.bitc.start = data;
    *(volatile U32 *)REG_ADC_CONTROL_REAL = d.all;
}
GH_INLINE U8   GH_ADC_get_Control_start(void)
{
    GH_ADC_CONTROL_REAL_S tmp_value;
    U32 value = (*(volatile U32 *)REG_ADC_CONTROL_REAL);

    tmp_value.all = value;
    return tmp_value.bitc.start;
}
GH_INLINE void GH_ADC_set_Control_channel(U8 data)
{
    GH_ADC_CONTROL_REAL_S d;
    d.all = *(volatile U32 *)REG_ADC_CONTROL_REAL;
    d.bitc.channel = data;
    *(volatile U32 *)REG_ADC_CONTROL_REAL = d.all;
}
GH_INLINE U8   GH_ADC_get_Control_channel(void)
{
    GH_ADC_CONTROL_REAL_S tmp_value;
    U32 value = (*(volatile U32 *)REG_ADC_CONTROL_REAL);

    tmp_value.all = value;
    return tmp_value.bitc.channel;
}

/*----------------------------------------------------------------------------*/
/* register ADC_ReadData (read)                                               */
/*----------------------------------------------------------------------------*/
GH_INLINE U32  GH_ADC_get_ReadData(U8 index)
{
    U32 value = (*(volatile U32 *)(REG_ADC_READDATA_REAL + index * FIO_MOFFSET(ADC,0x00000004)));

    return value;
}

/*----------------------------------------------------------------------------*/
/* register ADC_Enable (read/write)                                           */
/*----------------------------------------------------------------------------*/
GH_INLINE void GH_ADC_set_Enable(U32 data)
{
    *(volatile U32 *)REG_ADC_ENABLE_REAL = data;
}
GH_INLINE U32  GH_ADC_get_Enable(void)
{
    U32 value = (*(volatile U32 *)REG_ADC_ENABLE_REAL);

    return value;
}

/*----------------------------------------------------------------------------*/
/* register ADC_IntControl (read/write)                                       */
/*----------------------------------------------------------------------------*/
GH_INLINE void GH_ADC_set_IntControl(U8 index, U32 data)
{
    GH_ADC_INTCONTROL_REAL_S real;
    GH_ADC_INTCONTROL_S dummy;
    dummy.all = data ;
    real.bitc.val_lo = dummy.bitc.val_lo;
    real.bitc.val_hi = dummy.bitc.val_hi;
    real.bitc.en_lo = dummy.bitc.en_lo;
    real.bitc.en_hi = dummy.bitc.en_hi;
    *(volatile U32 *)(REG_ADC_INTCONTROL_REAL + index * FIO_MOFFSET(ADC,0x00000004)) = real.all;
}
GH_INLINE U32  GH_ADC_get_IntControl(U8 index)
{
    GH_ADC_INTCONTROL_REAL_S real;
    GH_ADC_INTCONTROL_S dummy;
     dummy.all =  0;
     real.all = (*(volatile U32 *)(REG_ADC_INTCONTROL_REAL + index * FIO_MOFFSET(ADC,0x00000004)));

    dummy.bitc.val_lo = real.bitc.val_lo;
    dummy.bitc.val_hi = real.bitc.val_hi;
    dummy.bitc.en_lo = real.bitc.en_lo;
    dummy.bitc.en_hi = real.bitc.en_hi;
    return dummy.all;
}
GH_INLINE void GH_ADC_set_IntControl_val_lo(U8 index, U16 data)
{
    GH_ADC_INTCONTROL_REAL_S d;
    d.all = *(volatile U32 *)(REG_ADC_INTCONTROL_REAL + index * FIO_MOFFSET(ADC,0x00000004));
    d.bitc.val_lo = data;
    *(volatile U32 *)(REG_ADC_INTCONTROL_REAL + index * FIO_MOFFSET(ADC,0x00000004)) = d.all;
}
GH_INLINE U16  GH_ADC_get_IntControl_val_lo(U8 index)
{
    GH_ADC_INTCONTROL_REAL_S tmp_value;
    U32 value = (*(volatile U32 *)(REG_ADC_INTCONTROL_REAL + index * FIO_MOFFSET(ADC,0x00000004)));

    tmp_value.all = value;
    return tmp_value.bitc.val_lo;
}
GH_INLINE void GH_ADC_set_IntControl_val_hi(U8 index, U16 data)
{
    GH_ADC_INTCONTROL_REAL_S d;
    d.all = *(volatile U32 *)(REG_ADC_INTCONTROL_REAL + index * FIO_MOFFSET(ADC,0x00000004));
    d.bitc.val_hi = data;
    *(volatile U32 *)(REG_ADC_INTCONTROL_REAL + index * FIO_MOFFSET(ADC,0x00000004)) = d.all;
}
GH_INLINE U16  GH_ADC_get_IntControl_val_hi(U8 index)
{
    GH_ADC_INTCONTROL_REAL_S tmp_value;
    U32 value = (*(volatile U32 *)(REG_ADC_INTCONTROL_REAL + index * FIO_MOFFSET(ADC,0x00000004)));

    tmp_value.all = value;
    return tmp_value.bitc.val_hi;
}
GH_INLINE void GH_ADC_set_IntControl_en_lo(U8 index, U8 data)
{
    GH_ADC_INTCONTROL_REAL_S d;
    d.all = *(volatile U32 *)(REG_ADC_INTCONTROL_REAL + index * FIO_MOFFSET(ADC,0x00000004));
    d.bitc.en_lo = data;
    *(volatile U32 *)(REG_ADC_INTCONTROL_REAL + index * FIO_MOFFSET(ADC,0x00000004)) = d.all;
}
GH_INLINE U8   GH_ADC_get_IntControl_en_lo(U8 index)
{
    GH_ADC_INTCONTROL_REAL_S tmp_value;
    U32 value = (*(volatile U32 *)(REG_ADC_INTCONTROL_REAL + index * FIO_MOFFSET(ADC,0x00000004)));

    tmp_value.all = value;
    return tmp_value.bitc.en_lo;
}
GH_INLINE void GH_ADC_set_IntControl_en_hi(U8 index, U8 data)
{
    GH_ADC_INTCONTROL_REAL_S d;
    d.all = *(volatile U32 *)(REG_ADC_INTCONTROL_REAL + index * FIO_MOFFSET(ADC,0x00000004));
    d.bitc.en_hi = data;
    *(volatile U32 *)(REG_ADC_INTCONTROL_REAL + index * FIO_MOFFSET(ADC,0x00000004)) = d.all;
}
GH_INLINE U8   GH_ADC_get_IntControl_en_hi(U8 index)
{
    GH_ADC_INTCONTROL_REAL_S tmp_value;
    U32 value = (*(volatile U32 *)(REG_ADC_INTCONTROL_REAL + index * FIO_MOFFSET(ADC,0x00000004)));

    tmp_value.all = value;
    return tmp_value.bitc.en_hi;
}

/*----------------------------------------------------------------------------*/
/* init function                                                              */
/*----------------------------------------------------------------------------*/
GH_INLINE void GH_ADC_init(void)
{
    int i;

    GH_ADC_set_AUX_ATOP((U32)0x00000000);
    GH_ADC_set_GPIO((U32)0x000000f0);
    GH_ADC_set_Control((U32)0x00000000);
    GH_ADC_set_Enable((U32)0x00000000);
    for (i=0; i<2; i++)
    {
        GH_ADC_set_IntControl(i, (U32)0x00000000);
    }
    /* read read-clear registers in order to set mirror variables */
}

/*----------------------------------------------------------------------------*/
/* end of file                                                                */
/*----------------------------------------------------------------------------*/