mbed os with nrf51 internal bandgap enabled to read battery level
Dependents: BLE_file_test BLE_Blink ExternalEncoder
targets/TARGET_STM/TARGET_STM32F2/analogout_api.c@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* mbed Microcontroller Library |
elessair | 0:f269e3021894 | 2 | * Copyright (c) 2016, STMicroelectronics |
elessair | 0:f269e3021894 | 3 | * All rights reserved. |
elessair | 0:f269e3021894 | 4 | * |
elessair | 0:f269e3021894 | 5 | * Redistribution and use in source and binary forms, with or without |
elessair | 0:f269e3021894 | 6 | * modification, are permitted provided that the following conditions are met: |
elessair | 0:f269e3021894 | 7 | * |
elessair | 0:f269e3021894 | 8 | * 1. Redistributions of source code must retain the above copyright notice, |
elessair | 0:f269e3021894 | 9 | * this list of conditions and the following disclaimer. |
elessair | 0:f269e3021894 | 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
elessair | 0:f269e3021894 | 11 | * this list of conditions and the following disclaimer in the documentation |
elessair | 0:f269e3021894 | 12 | * and/or other materials provided with the distribution. |
elessair | 0:f269e3021894 | 13 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
elessair | 0:f269e3021894 | 14 | * may be used to endorse or promote products derived from this software |
elessair | 0:f269e3021894 | 15 | * without specific prior written permission. |
elessair | 0:f269e3021894 | 16 | * |
elessair | 0:f269e3021894 | 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
elessair | 0:f269e3021894 | 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
elessair | 0:f269e3021894 | 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
elessair | 0:f269e3021894 | 20 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
elessair | 0:f269e3021894 | 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
elessair | 0:f269e3021894 | 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
elessair | 0:f269e3021894 | 23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
elessair | 0:f269e3021894 | 24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
elessair | 0:f269e3021894 | 25 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
elessair | 0:f269e3021894 | 26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
elessair | 0:f269e3021894 | 27 | */ |
elessair | 0:f269e3021894 | 28 | #include "analogout_api.h" |
elessair | 0:f269e3021894 | 29 | |
elessair | 0:f269e3021894 | 30 | #if DEVICE_ANALOGOUT |
elessair | 0:f269e3021894 | 31 | |
elessair | 0:f269e3021894 | 32 | #include "cmsis.h" |
elessair | 0:f269e3021894 | 33 | #include "pinmap.h" |
elessair | 0:f269e3021894 | 34 | #include "mbed_error.h" |
elessair | 0:f269e3021894 | 35 | #include "stm32f2xx_hal.h" |
elessair | 0:f269e3021894 | 36 | #include "PeripheralPins.h" |
elessair | 0:f269e3021894 | 37 | |
elessair | 0:f269e3021894 | 38 | #define DAC_RANGE (0xFFF) // 12 bits |
elessair | 0:f269e3021894 | 39 | #define DAC_NB_BITS (12) |
elessair | 0:f269e3021894 | 40 | |
elessair | 0:f269e3021894 | 41 | DAC_HandleTypeDef DacHandle; |
elessair | 0:f269e3021894 | 42 | static DAC_ChannelConfTypeDef sConfig; |
elessair | 0:f269e3021894 | 43 | |
elessair | 0:f269e3021894 | 44 | void analogout_init(dac_t *obj, PinName pin) |
elessair | 0:f269e3021894 | 45 | { |
elessair | 0:f269e3021894 | 46 | uint32_t channel ; |
elessair | 0:f269e3021894 | 47 | HAL_StatusTypeDef status; |
elessair | 0:f269e3021894 | 48 | |
elessair | 0:f269e3021894 | 49 | // Get the peripheral name (DAC_1, ...) from the pin and assign it to the object |
elessair | 0:f269e3021894 | 50 | obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); |
elessair | 0:f269e3021894 | 51 | // Get the functions (dac channel) from the pin and assign it to the object |
elessair | 0:f269e3021894 | 52 | uint32_t function = pinmap_function(pin, PinMap_DAC); |
elessair | 0:f269e3021894 | 53 | MBED_ASSERT(function != (uint32_t)NC); |
elessair | 0:f269e3021894 | 54 | // Save the channel for the write and read functions |
elessair | 0:f269e3021894 | 55 | obj->channel = STM_PIN_CHANNEL(function); |
elessair | 0:f269e3021894 | 56 | |
elessair | 0:f269e3021894 | 57 | if (obj->dac == (DACName)NC) { |
elessair | 0:f269e3021894 | 58 | error("DAC pin mapping failed"); |
elessair | 0:f269e3021894 | 59 | } |
elessair | 0:f269e3021894 | 60 | |
elessair | 0:f269e3021894 | 61 | // Configure GPIO |
elessair | 0:f269e3021894 | 62 | pinmap_pinout(pin, PinMap_DAC); |
elessair | 0:f269e3021894 | 63 | |
elessair | 0:f269e3021894 | 64 | __GPIOA_CLK_ENABLE(); |
elessair | 0:f269e3021894 | 65 | |
elessair | 0:f269e3021894 | 66 | __DAC_CLK_ENABLE(); |
elessair | 0:f269e3021894 | 67 | |
elessair | 0:f269e3021894 | 68 | DacHandle.Instance = DAC; |
elessair | 0:f269e3021894 | 69 | |
elessair | 0:f269e3021894 | 70 | status = HAL_DAC_Init(&DacHandle); |
elessair | 0:f269e3021894 | 71 | if (status != HAL_OK) { |
elessair | 0:f269e3021894 | 72 | error("HAL_DAC_Init failed"); |
elessair | 0:f269e3021894 | 73 | } |
elessair | 0:f269e3021894 | 74 | |
elessair | 0:f269e3021894 | 75 | sConfig.DAC_Trigger = DAC_TRIGGER_NONE; |
elessair | 0:f269e3021894 | 76 | sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE; |
elessair | 0:f269e3021894 | 77 | |
elessair | 0:f269e3021894 | 78 | if (obj->channel == 1) { |
elessair | 0:f269e3021894 | 79 | channel = DAC_CHANNEL_1; |
elessair | 0:f269e3021894 | 80 | } else { |
elessair | 0:f269e3021894 | 81 | channel = DAC_CHANNEL_2; |
elessair | 0:f269e3021894 | 82 | } |
elessair | 0:f269e3021894 | 83 | |
elessair | 0:f269e3021894 | 84 | if (HAL_DAC_ConfigChannel(&DacHandle, &sConfig, channel) != HAL_OK) { |
elessair | 0:f269e3021894 | 85 | error("HAL_DAC_ConfigChannel failed"); |
elessair | 0:f269e3021894 | 86 | } |
elessair | 0:f269e3021894 | 87 | |
elessair | 0:f269e3021894 | 88 | if (HAL_DAC_Start(&DacHandle, channel) != HAL_OK) { |
elessair | 0:f269e3021894 | 89 | error("HAL_DAC_Start failed"); |
elessair | 0:f269e3021894 | 90 | } |
elessair | 0:f269e3021894 | 91 | |
elessair | 0:f269e3021894 | 92 | if (HAL_DAC_SetValue(&DacHandle, channel, DAC_ALIGN_12B_R, 0x000) != HAL_OK) { |
elessair | 0:f269e3021894 | 93 | error("HAL_DAC_SetValue failed"); |
elessair | 0:f269e3021894 | 94 | } |
elessair | 0:f269e3021894 | 95 | |
elessair | 0:f269e3021894 | 96 | } |
elessair | 0:f269e3021894 | 97 | |
elessair | 0:f269e3021894 | 98 | void analogout_free(dac_t *obj) |
elessair | 0:f269e3021894 | 99 | { |
elessair | 0:f269e3021894 | 100 | } |
elessair | 0:f269e3021894 | 101 | |
elessair | 0:f269e3021894 | 102 | static inline void dac_write(dac_t *obj, int value) |
elessair | 0:f269e3021894 | 103 | { |
elessair | 0:f269e3021894 | 104 | HAL_StatusTypeDef status = HAL_ERROR; |
elessair | 0:f269e3021894 | 105 | |
elessair | 0:f269e3021894 | 106 | if (obj->channel == 1) { |
elessair | 0:f269e3021894 | 107 | status = HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, (value & DAC_RANGE)); |
elessair | 0:f269e3021894 | 108 | } else if (obj->channel == 2) { |
elessair | 0:f269e3021894 | 109 | status = HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, (value & DAC_RANGE)); |
elessair | 0:f269e3021894 | 110 | } |
elessair | 0:f269e3021894 | 111 | |
elessair | 0:f269e3021894 | 112 | if (status != HAL_OK) { |
elessair | 0:f269e3021894 | 113 | error("DAC pin mapping failed"); |
elessair | 0:f269e3021894 | 114 | } |
elessair | 0:f269e3021894 | 115 | } |
elessair | 0:f269e3021894 | 116 | |
elessair | 0:f269e3021894 | 117 | static inline int dac_read(dac_t *obj) |
elessair | 0:f269e3021894 | 118 | { |
elessair | 0:f269e3021894 | 119 | if (obj->channel == 1) { |
elessair | 0:f269e3021894 | 120 | return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1); |
elessair | 0:f269e3021894 | 121 | } else if (obj->channel == 2) { |
elessair | 0:f269e3021894 | 122 | return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_2); |
elessair | 0:f269e3021894 | 123 | } |
elessair | 0:f269e3021894 | 124 | return 0; /* Just silented warning */ |
elessair | 0:f269e3021894 | 125 | } |
elessair | 0:f269e3021894 | 126 | |
elessair | 0:f269e3021894 | 127 | void analogout_write(dac_t *obj, float value) |
elessair | 0:f269e3021894 | 128 | { |
elessair | 0:f269e3021894 | 129 | if (value < 0.0f) { |
elessair | 0:f269e3021894 | 130 | dac_write(obj, 0); // Min value |
elessair | 0:f269e3021894 | 131 | } else if (value > 1.0f) { |
elessair | 0:f269e3021894 | 132 | dac_write(obj, (int)DAC_RANGE); // Max value |
elessair | 0:f269e3021894 | 133 | } else { |
elessair | 0:f269e3021894 | 134 | dac_write(obj, (int)(value * (float)DAC_RANGE)); |
elessair | 0:f269e3021894 | 135 | } |
elessair | 0:f269e3021894 | 136 | } |
elessair | 0:f269e3021894 | 137 | |
elessair | 0:f269e3021894 | 138 | void analogout_write_u16(dac_t *obj, uint16_t value) |
elessair | 0:f269e3021894 | 139 | { |
elessair | 0:f269e3021894 | 140 | dac_write(obj, value >> (16 - DAC_NB_BITS)); |
elessair | 0:f269e3021894 | 141 | } |
elessair | 0:f269e3021894 | 142 | |
elessair | 0:f269e3021894 | 143 | float analogout_read(dac_t *obj) |
elessair | 0:f269e3021894 | 144 | { |
elessair | 0:f269e3021894 | 145 | uint32_t value = dac_read(obj); |
elessair | 0:f269e3021894 | 146 | return (float)value * (1.0f / (float)DAC_RANGE); |
elessair | 0:f269e3021894 | 147 | } |
elessair | 0:f269e3021894 | 148 | |
elessair | 0:f269e3021894 | 149 | uint16_t analogout_read_u16(dac_t *obj) |
elessair | 0:f269e3021894 | 150 | { |
elessair | 0:f269e3021894 | 151 | uint32_t value = dac_read(obj); |
elessair | 0:f269e3021894 | 152 | return (value << 4) | ((value >> 8) & 0x000F); // Conversion from 12 to 16 bits |
elessair | 0:f269e3021894 | 153 | } |
elessair | 0:f269e3021894 | 154 | |
elessair | 0:f269e3021894 | 155 | #endif // DEVICE_ANALOGOUT |