mbed library sources, include can_api for nucleo-f091rc
Dependents: CanNucleoF0_example
Fork of mbed-src by
vendor/Freescale/KL25Z/hal/analogin_api.c@10:3bc89ef62ce7, 2013-06-14 (annotated)
- Committer:
- emilmont
- Date:
- Fri Jun 14 17:49:17 2013 +0100
- Revision:
- 10:3bc89ef62ce7
Unify mbed library sources
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 10:3bc89ef62ce7 | 1 | /* mbed Microcontroller Library |
emilmont | 10:3bc89ef62ce7 | 2 | * Copyright (c) 2006-2013 ARM Limited |
emilmont | 10:3bc89ef62ce7 | 3 | * |
emilmont | 10:3bc89ef62ce7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
emilmont | 10:3bc89ef62ce7 | 5 | * you may not use this file except in compliance with the License. |
emilmont | 10:3bc89ef62ce7 | 6 | * You may obtain a copy of the License at |
emilmont | 10:3bc89ef62ce7 | 7 | * |
emilmont | 10:3bc89ef62ce7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
emilmont | 10:3bc89ef62ce7 | 9 | * |
emilmont | 10:3bc89ef62ce7 | 10 | * Unless required by applicable law or agreed to in writing, software |
emilmont | 10:3bc89ef62ce7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
emilmont | 10:3bc89ef62ce7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
emilmont | 10:3bc89ef62ce7 | 13 | * See the License for the specific language governing permissions and |
emilmont | 10:3bc89ef62ce7 | 14 | * limitations under the License. |
emilmont | 10:3bc89ef62ce7 | 15 | */ |
emilmont | 10:3bc89ef62ce7 | 16 | #include "analogin_api.h" |
emilmont | 10:3bc89ef62ce7 | 17 | |
emilmont | 10:3bc89ef62ce7 | 18 | #include "cmsis.h" |
emilmont | 10:3bc89ef62ce7 | 19 | #include "pinmap.h" |
emilmont | 10:3bc89ef62ce7 | 20 | #include "error.h" |
emilmont | 10:3bc89ef62ce7 | 21 | |
emilmont | 10:3bc89ef62ce7 | 22 | static const PinMap PinMap_ADC[] = { |
emilmont | 10:3bc89ef62ce7 | 23 | {PTE20, ADC0_SE0, 0}, |
emilmont | 10:3bc89ef62ce7 | 24 | {PTE22, ADC0_SE3, 0}, |
emilmont | 10:3bc89ef62ce7 | 25 | {PTE29, ADC0_SE4b, 0}, |
emilmont | 10:3bc89ef62ce7 | 26 | {PTE30, ADC0_SE23, 0}, |
emilmont | 10:3bc89ef62ce7 | 27 | {PTB0, ADC0_SE8, 0}, |
emilmont | 10:3bc89ef62ce7 | 28 | {PTB1, ADC0_SE9, 0}, |
emilmont | 10:3bc89ef62ce7 | 29 | {PTB2, ADC0_SE12, 0}, |
emilmont | 10:3bc89ef62ce7 | 30 | {PTB3, ADC0_SE13, 0}, |
emilmont | 10:3bc89ef62ce7 | 31 | {PTC0, ADC0_SE14, 0}, |
emilmont | 10:3bc89ef62ce7 | 32 | {PTC1, ADC0_SE15, 0}, |
emilmont | 10:3bc89ef62ce7 | 33 | {PTC2, ADC0_SE11, 0}, |
emilmont | 10:3bc89ef62ce7 | 34 | {PTD1, ADC0_SE5b, 0}, |
emilmont | 10:3bc89ef62ce7 | 35 | {PTD5, ADC0_SE6b, 0}, |
emilmont | 10:3bc89ef62ce7 | 36 | {PTD6, ADC0_SE7b, 0}, |
emilmont | 10:3bc89ef62ce7 | 37 | {NC, NC, 0} |
emilmont | 10:3bc89ef62ce7 | 38 | }; |
emilmont | 10:3bc89ef62ce7 | 39 | |
emilmont | 10:3bc89ef62ce7 | 40 | void analogin_init(analogin_t *obj, PinName pin) { |
emilmont | 10:3bc89ef62ce7 | 41 | obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); |
emilmont | 10:3bc89ef62ce7 | 42 | if (obj->adc == (uint32_t)NC) { |
emilmont | 10:3bc89ef62ce7 | 43 | error("ADC pin mapping failed"); |
emilmont | 10:3bc89ef62ce7 | 44 | } |
emilmont | 10:3bc89ef62ce7 | 45 | |
emilmont | 10:3bc89ef62ce7 | 46 | SIM->SCGC6 |= SIM_SCGC6_ADC0_MASK; |
emilmont | 10:3bc89ef62ce7 | 47 | |
emilmont | 10:3bc89ef62ce7 | 48 | uint32_t port = (uint32_t)pin >> PORT_SHIFT; |
emilmont | 10:3bc89ef62ce7 | 49 | SIM->SCGC5 |= 1 << (SIM_SCGC5_PORTA_SHIFT + port); |
emilmont | 10:3bc89ef62ce7 | 50 | |
emilmont | 10:3bc89ef62ce7 | 51 | ADC0->SC1[1] = ADC_SC1_ADCH(obj->adc); |
emilmont | 10:3bc89ef62ce7 | 52 | |
emilmont | 10:3bc89ef62ce7 | 53 | ADC0->CFG1 = ADC_CFG1_ADLPC_MASK // Low-Power Configuration |
emilmont | 10:3bc89ef62ce7 | 54 | | ADC_CFG1_ADIV(3) // Clock Divide Select: (Input Clock)/8 |
emilmont | 10:3bc89ef62ce7 | 55 | | ADC_CFG1_ADLSMP_MASK // Long Sample Time |
emilmont | 10:3bc89ef62ce7 | 56 | | ADC_CFG1_MODE(3) // (16)bits Resolution |
emilmont | 10:3bc89ef62ce7 | 57 | | ADC_CFG1_ADICLK(1); // Input Clock: (Bus Clock)/2 |
emilmont | 10:3bc89ef62ce7 | 58 | |
emilmont | 10:3bc89ef62ce7 | 59 | ADC0->CFG2 = ADC_CFG2_MUXSEL_MASK // ADxxb channels are selected |
emilmont | 10:3bc89ef62ce7 | 60 | | ADC_CFG2_ADACKEN_MASK // Asynchronous Clock Output Enable |
emilmont | 10:3bc89ef62ce7 | 61 | | ADC_CFG2_ADHSC_MASK // High-Speed Configuration |
emilmont | 10:3bc89ef62ce7 | 62 | | ADC_CFG2_ADLSTS(0); // Long Sample Time Select |
emilmont | 10:3bc89ef62ce7 | 63 | |
emilmont | 10:3bc89ef62ce7 | 64 | ADC0->SC2 = ADC_SC2_REFSEL(0); // Default Voltage Reference |
emilmont | 10:3bc89ef62ce7 | 65 | |
emilmont | 10:3bc89ef62ce7 | 66 | ADC0->SC3 = ADC_SC3_AVGE_MASK // Hardware Average Enable |
emilmont | 10:3bc89ef62ce7 | 67 | | ADC_SC3_AVGS(0); // 4 Samples Averaged |
emilmont | 10:3bc89ef62ce7 | 68 | |
emilmont | 10:3bc89ef62ce7 | 69 | pinmap_pinout(pin, PinMap_ADC); |
emilmont | 10:3bc89ef62ce7 | 70 | } |
emilmont | 10:3bc89ef62ce7 | 71 | |
emilmont | 10:3bc89ef62ce7 | 72 | uint16_t analogin_read_u16(analogin_t *obj) { |
emilmont | 10:3bc89ef62ce7 | 73 | // start conversion |
emilmont | 10:3bc89ef62ce7 | 74 | ADC0->SC1[0] = ADC_SC1_ADCH(obj->adc); |
emilmont | 10:3bc89ef62ce7 | 75 | |
emilmont | 10:3bc89ef62ce7 | 76 | // Wait Conversion Complete |
emilmont | 10:3bc89ef62ce7 | 77 | while ((ADC0->SC1[0] & ADC_SC1_COCO_MASK) != ADC_SC1_COCO_MASK); |
emilmont | 10:3bc89ef62ce7 | 78 | |
emilmont | 10:3bc89ef62ce7 | 79 | // Return value |
emilmont | 10:3bc89ef62ce7 | 80 | return (uint16_t)ADC0->R[0]; |
emilmont | 10:3bc89ef62ce7 | 81 | } |
emilmont | 10:3bc89ef62ce7 | 82 | |
emilmont | 10:3bc89ef62ce7 | 83 | float analogin_read(analogin_t *obj) { |
emilmont | 10:3bc89ef62ce7 | 84 | uint16_t value = analogin_read_u16(obj); |
emilmont | 10:3bc89ef62ce7 | 85 | return (float)value * (1.0f / (float)0xFFFF); |
emilmont | 10:3bc89ef62ce7 | 86 | } |
emilmont | 10:3bc89ef62ce7 | 87 |