Mouse code for the MacroRat
mbed-dev/targets/TARGET_ARM_SSG/TARGET_BEETLE/analogin_api.c@18:6a4db94011d3, 2017-05-14 (annotated)
- Committer:
- sahilmgandhi
- Date:
- Sun May 14 23:18:57 2017 +0000
- Revision:
- 18:6a4db94011d3
Publishing again
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahilmgandhi | 18:6a4db94011d3 | 1 | /* |
sahilmgandhi | 18:6a4db94011d3 | 2 | * PackageLicenseDeclared: Apache-2.0 |
sahilmgandhi | 18:6a4db94011d3 | 3 | * Copyright (c) 2016 ARM Limited |
sahilmgandhi | 18:6a4db94011d3 | 4 | * |
sahilmgandhi | 18:6a4db94011d3 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sahilmgandhi | 18:6a4db94011d3 | 6 | * you may not use this file except in compliance with the License. |
sahilmgandhi | 18:6a4db94011d3 | 7 | * You may obtain a copy of the License at |
sahilmgandhi | 18:6a4db94011d3 | 8 | * |
sahilmgandhi | 18:6a4db94011d3 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
sahilmgandhi | 18:6a4db94011d3 | 10 | * |
sahilmgandhi | 18:6a4db94011d3 | 11 | * Unless required by applicable law or agreed to in writing, software |
sahilmgandhi | 18:6a4db94011d3 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
sahilmgandhi | 18:6a4db94011d3 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sahilmgandhi | 18:6a4db94011d3 | 14 | * See the License for the specific language governing permissions and |
sahilmgandhi | 18:6a4db94011d3 | 15 | * limitations under the License. |
sahilmgandhi | 18:6a4db94011d3 | 16 | */ |
sahilmgandhi | 18:6a4db94011d3 | 17 | #include "cmsis.h" |
sahilmgandhi | 18:6a4db94011d3 | 18 | #include "mbed_assert.h" |
sahilmgandhi | 18:6a4db94011d3 | 19 | #include "mbed_wait_api.h" |
sahilmgandhi | 18:6a4db94011d3 | 20 | #include "analogin_api.h" |
sahilmgandhi | 18:6a4db94011d3 | 21 | #include "gpio_api.h" |
sahilmgandhi | 18:6a4db94011d3 | 22 | #include "spi_api.h" |
sahilmgandhi | 18:6a4db94011d3 | 23 | #include "pinmap.h" |
sahilmgandhi | 18:6a4db94011d3 | 24 | |
sahilmgandhi | 18:6a4db94011d3 | 25 | #if DEVICE_ANALOGIN |
sahilmgandhi | 18:6a4db94011d3 | 26 | |
sahilmgandhi | 18:6a4db94011d3 | 27 | /* |
sahilmgandhi | 18:6a4db94011d3 | 28 | * Channel Address for the next acquisition: |
sahilmgandhi | 18:6a4db94011d3 | 29 | * XXAAAXXX XXXXXXXX |
sahilmgandhi | 18:6a4db94011d3 | 30 | */ |
sahilmgandhi | 18:6a4db94011d3 | 31 | #define ADC_SPI_ADDRESS 11 |
sahilmgandhi | 18:6a4db94011d3 | 32 | |
sahilmgandhi | 18:6a4db94011d3 | 33 | /* ADC Resolution */ |
sahilmgandhi | 18:6a4db94011d3 | 34 | #define ADC_RESOLUTION 0xFFF |
sahilmgandhi | 18:6a4db94011d3 | 35 | |
sahilmgandhi | 18:6a4db94011d3 | 36 | /* ADC Voltage Divider */ |
sahilmgandhi | 18:6a4db94011d3 | 37 | #define ADC_DIV 819.0 |
sahilmgandhi | 18:6a4db94011d3 | 38 | |
sahilmgandhi | 18:6a4db94011d3 | 39 | /* PinMap structure for ADC IN */ |
sahilmgandhi | 18:6a4db94011d3 | 40 | static const PinMap PinMap_ADC[] = { |
sahilmgandhi | 18:6a4db94011d3 | 41 | {A0, ADC0_0, 0}, |
sahilmgandhi | 18:6a4db94011d3 | 42 | {A1, ADC0_1, 0}, |
sahilmgandhi | 18:6a4db94011d3 | 43 | {A2, ADC0_2, 0}, |
sahilmgandhi | 18:6a4db94011d3 | 44 | {A3, ADC0_3, 0}, |
sahilmgandhi | 18:6a4db94011d3 | 45 | {A4, ADC0_4, 0}, |
sahilmgandhi | 18:6a4db94011d3 | 46 | {A5, ADC0_5, 0}, |
sahilmgandhi | 18:6a4db94011d3 | 47 | {NC, NC, 0} |
sahilmgandhi | 18:6a4db94011d3 | 48 | }; |
sahilmgandhi | 18:6a4db94011d3 | 49 | |
sahilmgandhi | 18:6a4db94011d3 | 50 | /* ADC SPI Private Data */ |
sahilmgandhi | 18:6a4db94011d3 | 51 | typedef struct { |
sahilmgandhi | 18:6a4db94011d3 | 52 | /* ADC SPI */ |
sahilmgandhi | 18:6a4db94011d3 | 53 | spi_t analogin_spi; |
sahilmgandhi | 18:6a4db94011d3 | 54 | /* ADC SPI CS */ |
sahilmgandhi | 18:6a4db94011d3 | 55 | gpio_t adc_spi_cs_gpio; |
sahilmgandhi | 18:6a4db94011d3 | 56 | /* ADC SPI State */ |
sahilmgandhi | 18:6a4db94011d3 | 57 | uint32_t analog_spi_inited; |
sahilmgandhi | 18:6a4db94011d3 | 58 | } analogin_spi_t; |
sahilmgandhi | 18:6a4db94011d3 | 59 | /* ADC SPI Device */ |
sahilmgandhi | 18:6a4db94011d3 | 60 | static analogin_spi_t analogin_spi_dev; |
sahilmgandhi | 18:6a4db94011d3 | 61 | |
sahilmgandhi | 18:6a4db94011d3 | 62 | /* |
sahilmgandhi | 18:6a4db94011d3 | 63 | * ADC SPI CS |
sahilmgandhi | 18:6a4db94011d3 | 64 | */ |
sahilmgandhi | 18:6a4db94011d3 | 65 | #define ADC_SPI_CS P1_2 |
sahilmgandhi | 18:6a4db94011d3 | 66 | |
sahilmgandhi | 18:6a4db94011d3 | 67 | /* |
sahilmgandhi | 18:6a4db94011d3 | 68 | * Initialize the analogin peripheral |
sahilmgandhi | 18:6a4db94011d3 | 69 | * Configures the pin used by analogin. |
sahilmgandhi | 18:6a4db94011d3 | 70 | * obj: The analogin object to initialize |
sahilmgandhi | 18:6a4db94011d3 | 71 | * pin: The analogin pin name |
sahilmgandhi | 18:6a4db94011d3 | 72 | */ |
sahilmgandhi | 18:6a4db94011d3 | 73 | void analogin_init(analogin_t *obj, PinName pin) |
sahilmgandhi | 18:6a4db94011d3 | 74 | { |
sahilmgandhi | 18:6a4db94011d3 | 75 | /* Initialize ADC Pin */ |
sahilmgandhi | 18:6a4db94011d3 | 76 | obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); |
sahilmgandhi | 18:6a4db94011d3 | 77 | /* Verify if pin is valid */ |
sahilmgandhi | 18:6a4db94011d3 | 78 | MBED_ASSERT(obj->adc != (ADCName)NC); |
sahilmgandhi | 18:6a4db94011d3 | 79 | |
sahilmgandhi | 18:6a4db94011d3 | 80 | /* Initialize the ADC SPI */ |
sahilmgandhi | 18:6a4db94011d3 | 81 | if(analogin_spi_dev.analog_spi_inited == 0){ |
sahilmgandhi | 18:6a4db94011d3 | 82 | /* Initialize SPI for ADC */ |
sahilmgandhi | 18:6a4db94011d3 | 83 | spi_init(&(analogin_spi_dev.analogin_spi), ADC_SPI_MOSI, |
sahilmgandhi | 18:6a4db94011d3 | 84 | ADC_SPI_MISO, ADC_SPI_SCK, ADC_SPI_nCS); |
sahilmgandhi | 18:6a4db94011d3 | 85 | spi_format(&(analogin_spi_dev.analogin_spi), 16, 3, 0); |
sahilmgandhi | 18:6a4db94011d3 | 86 | /* Set SPI to MAX Freq */ |
sahilmgandhi | 18:6a4db94011d3 | 87 | spi_frequency(&(analogin_spi_dev.analogin_spi), 0); |
sahilmgandhi | 18:6a4db94011d3 | 88 | |
sahilmgandhi | 18:6a4db94011d3 | 89 | /* Initialize CS GPIO */ |
sahilmgandhi | 18:6a4db94011d3 | 90 | gpio_init_out(&(analogin_spi_dev.adc_spi_cs_gpio), ADC_SPI_CS); |
sahilmgandhi | 18:6a4db94011d3 | 91 | |
sahilmgandhi | 18:6a4db94011d3 | 92 | analogin_spi_dev.analog_spi_inited = 1; |
sahilmgandhi | 18:6a4db94011d3 | 93 | } |
sahilmgandhi | 18:6a4db94011d3 | 94 | |
sahilmgandhi | 18:6a4db94011d3 | 95 | /* If pin is valid assign it to the ADC data structure */ |
sahilmgandhi | 18:6a4db94011d3 | 96 | obj->pin = pin; |
sahilmgandhi | 18:6a4db94011d3 | 97 | obj->pin_number = pin-600; |
sahilmgandhi | 18:6a4db94011d3 | 98 | obj->address = (0x0000 | (pin-600)); |
sahilmgandhi | 18:6a4db94011d3 | 99 | |
sahilmgandhi | 18:6a4db94011d3 | 100 | /* Configure the pinout */ |
sahilmgandhi | 18:6a4db94011d3 | 101 | pinmap_pinout(pin, PinMap_ADC); |
sahilmgandhi | 18:6a4db94011d3 | 102 | } |
sahilmgandhi | 18:6a4db94011d3 | 103 | |
sahilmgandhi | 18:6a4db94011d3 | 104 | /* |
sahilmgandhi | 18:6a4db94011d3 | 105 | * Read the value from analogin pin, represented as an unsigned 16bit value |
sahilmgandhi | 18:6a4db94011d3 | 106 | * obj: The analogin object |
sahilmgandhi | 18:6a4db94011d3 | 107 | * @return: An unsigned 16bit value representing the current input voltage |
sahilmgandhi | 18:6a4db94011d3 | 108 | */ |
sahilmgandhi | 18:6a4db94011d3 | 109 | uint16_t analogin_read_u16(analogin_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 110 | { |
sahilmgandhi | 18:6a4db94011d3 | 111 | uint16_t result = 0; |
sahilmgandhi | 18:6a4db94011d3 | 112 | |
sahilmgandhi | 18:6a4db94011d3 | 113 | /* |
sahilmgandhi | 18:6a4db94011d3 | 114 | * The ADC SPI hw is 8 bit format, 16 bit emulation is required |
sahilmgandhi | 18:6a4db94011d3 | 115 | * in the SPI driver. |
sahilmgandhi | 18:6a4db94011d3 | 116 | */ |
sahilmgandhi | 18:6a4db94011d3 | 117 | /* CS = 1 */ |
sahilmgandhi | 18:6a4db94011d3 | 118 | gpio_write(&(analogin_spi_dev.adc_spi_cs_gpio), 1); |
sahilmgandhi | 18:6a4db94011d3 | 119 | /* Do the first read */ |
sahilmgandhi | 18:6a4db94011d3 | 120 | (void)spi_master_write(&(analogin_spi_dev.analogin_spi), |
sahilmgandhi | 18:6a4db94011d3 | 121 | ((obj->pin_number) << ADC_SPI_ADDRESS)); |
sahilmgandhi | 18:6a4db94011d3 | 122 | /* CS = 0 */ |
sahilmgandhi | 18:6a4db94011d3 | 123 | gpio_write(&(analogin_spi_dev.adc_spi_cs_gpio), 0); |
sahilmgandhi | 18:6a4db94011d3 | 124 | /* Wait 50 us */ |
sahilmgandhi | 18:6a4db94011d3 | 125 | wait_us(50); |
sahilmgandhi | 18:6a4db94011d3 | 126 | /* CS = 1 */ |
sahilmgandhi | 18:6a4db94011d3 | 127 | gpio_write(&(analogin_spi_dev.adc_spi_cs_gpio), 1); |
sahilmgandhi | 18:6a4db94011d3 | 128 | /* The second read provides the result */ |
sahilmgandhi | 18:6a4db94011d3 | 129 | result = spi_master_write(&(analogin_spi_dev.analogin_spi), |
sahilmgandhi | 18:6a4db94011d3 | 130 | ((obj->pin_number) << ADC_SPI_ADDRESS)); |
sahilmgandhi | 18:6a4db94011d3 | 131 | /* CS = 0 */ |
sahilmgandhi | 18:6a4db94011d3 | 132 | gpio_write(&(analogin_spi_dev.adc_spi_cs_gpio), 0); |
sahilmgandhi | 18:6a4db94011d3 | 133 | |
sahilmgandhi | 18:6a4db94011d3 | 134 | return result; |
sahilmgandhi | 18:6a4db94011d3 | 135 | } |
sahilmgandhi | 18:6a4db94011d3 | 136 | |
sahilmgandhi | 18:6a4db94011d3 | 137 | /* |
sahilmgandhi | 18:6a4db94011d3 | 138 | * Read the input voltage, represented as a float in the range [0.0, 1.0] |
sahilmgandhi | 18:6a4db94011d3 | 139 | * obj: The analogin object |
sahilmgandhi | 18:6a4db94011d3 | 140 | * @return: A floating value representing the current input voltage |
sahilmgandhi | 18:6a4db94011d3 | 141 | */ |
sahilmgandhi | 18:6a4db94011d3 | 142 | float analogin_read(analogin_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 143 | { |
sahilmgandhi | 18:6a4db94011d3 | 144 | uint16_t result = analogin_read_u16(obj); |
sahilmgandhi | 18:6a4db94011d3 | 145 | return (float)((result & ADC_RESOLUTION) * 1.0f) / ADC_DIV; |
sahilmgandhi | 18:6a4db94011d3 | 146 | } |
sahilmgandhi | 18:6a4db94011d3 | 147 | |
sahilmgandhi | 18:6a4db94011d3 | 148 | #endif |