![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/targets/TARGET_Freescale/TARGET_K20XX/analogout_api.c@0:8fdf9a60065b, 2018-10-10 (annotated)
- Committer:
- kadonotakashi
- Date:
- Wed Oct 10 00:33:53 2018 +0000
- Revision:
- 0:8fdf9a60065b
how to make mbed librry
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kadonotakashi | 0:8fdf9a60065b | 1 | /* mbed Microcontroller Library |
kadonotakashi | 0:8fdf9a60065b | 2 | * Copyright (c) 2006-2015 ARM Limited |
kadonotakashi | 0:8fdf9a60065b | 3 | * |
kadonotakashi | 0:8fdf9a60065b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kadonotakashi | 0:8fdf9a60065b | 5 | * you may not use this file except in compliance with the License. |
kadonotakashi | 0:8fdf9a60065b | 6 | * You may obtain a copy of the License at |
kadonotakashi | 0:8fdf9a60065b | 7 | * |
kadonotakashi | 0:8fdf9a60065b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
kadonotakashi | 0:8fdf9a60065b | 9 | * |
kadonotakashi | 0:8fdf9a60065b | 10 | * Unless required by applicable law or agreed to in writing, software |
kadonotakashi | 0:8fdf9a60065b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
kadonotakashi | 0:8fdf9a60065b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kadonotakashi | 0:8fdf9a60065b | 13 | * See the License for the specific language governing permissions and |
kadonotakashi | 0:8fdf9a60065b | 14 | * limitations under the License. |
kadonotakashi | 0:8fdf9a60065b | 15 | */ |
kadonotakashi | 0:8fdf9a60065b | 16 | |
kadonotakashi | 0:8fdf9a60065b | 17 | #include "mbed_assert.h" |
kadonotakashi | 0:8fdf9a60065b | 18 | #include "analogout_api.h" |
kadonotakashi | 0:8fdf9a60065b | 19 | |
kadonotakashi | 0:8fdf9a60065b | 20 | #if DEVICE_ANALOGOUT |
kadonotakashi | 0:8fdf9a60065b | 21 | |
kadonotakashi | 0:8fdf9a60065b | 22 | #include "cmsis.h" |
kadonotakashi | 0:8fdf9a60065b | 23 | #include "pinmap.h" |
kadonotakashi | 0:8fdf9a60065b | 24 | #include "PeripheralPins.h" |
kadonotakashi | 0:8fdf9a60065b | 25 | |
kadonotakashi | 0:8fdf9a60065b | 26 | #define RANGE_12BIT 0xFFF |
kadonotakashi | 0:8fdf9a60065b | 27 | |
kadonotakashi | 0:8fdf9a60065b | 28 | void analogout_init(dac_t *obj, PinName pin) { |
kadonotakashi | 0:8fdf9a60065b | 29 | obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); |
kadonotakashi | 0:8fdf9a60065b | 30 | MBED_ASSERT(obj->dac != (DACName)NC); |
kadonotakashi | 0:8fdf9a60065b | 31 | |
kadonotakashi | 0:8fdf9a60065b | 32 | SIM->SCGC2 |= SIM_SCGC2_DAC0_MASK; |
kadonotakashi | 0:8fdf9a60065b | 33 | |
kadonotakashi | 0:8fdf9a60065b | 34 | uint32_t port = (uint32_t)pin >> PORT_SHIFT; |
kadonotakashi | 0:8fdf9a60065b | 35 | SIM->SCGC5 |= 1 << (SIM_SCGC5_PORTA_SHIFT + port); |
kadonotakashi | 0:8fdf9a60065b | 36 | |
kadonotakashi | 0:8fdf9a60065b | 37 | DAC0->DAT[obj->dac].DATH = 0; |
kadonotakashi | 0:8fdf9a60065b | 38 | DAC0->DAT[obj->dac].DATL = 0; |
kadonotakashi | 0:8fdf9a60065b | 39 | |
kadonotakashi | 0:8fdf9a60065b | 40 | DAC0->C1 = DAC_C1_DACBFMD_MASK; // One-Time Scan Mode |
kadonotakashi | 0:8fdf9a60065b | 41 | |
kadonotakashi | 0:8fdf9a60065b | 42 | DAC0->C0 = DAC_C0_DACEN_MASK // Enable |
kadonotakashi | 0:8fdf9a60065b | 43 | | DAC_C0_DACSWTRG_MASK // Software Trigger |
kadonotakashi | 0:8fdf9a60065b | 44 | | DAC_C0_DACRFS_MASK; // VDDA selected |
kadonotakashi | 0:8fdf9a60065b | 45 | |
kadonotakashi | 0:8fdf9a60065b | 46 | analogout_write_u16(obj, 0); |
kadonotakashi | 0:8fdf9a60065b | 47 | } |
kadonotakashi | 0:8fdf9a60065b | 48 | |
kadonotakashi | 0:8fdf9a60065b | 49 | void analogout_free(dac_t *obj) {} |
kadonotakashi | 0:8fdf9a60065b | 50 | |
kadonotakashi | 0:8fdf9a60065b | 51 | static inline void dac_write(dac_t *obj, int value) { |
kadonotakashi | 0:8fdf9a60065b | 52 | DAC0->DAT[obj->dac].DATL = (uint8_t)( value & 0xFF); |
kadonotakashi | 0:8fdf9a60065b | 53 | DAC0->DAT[obj->dac].DATH = (uint8_t)((value >> 8) & 0xFF); |
kadonotakashi | 0:8fdf9a60065b | 54 | } |
kadonotakashi | 0:8fdf9a60065b | 55 | |
kadonotakashi | 0:8fdf9a60065b | 56 | static inline int dac_read(dac_t *obj) { |
kadonotakashi | 0:8fdf9a60065b | 57 | return ((DAC0->DAT[obj->dac].DATH << 8) | DAC0->DAT[obj->dac].DATL); |
kadonotakashi | 0:8fdf9a60065b | 58 | } |
kadonotakashi | 0:8fdf9a60065b | 59 | |
kadonotakashi | 0:8fdf9a60065b | 60 | void analogout_write(dac_t *obj, float value) { |
kadonotakashi | 0:8fdf9a60065b | 61 | if (value < 0.0) { |
kadonotakashi | 0:8fdf9a60065b | 62 | dac_write(obj, 0); |
kadonotakashi | 0:8fdf9a60065b | 63 | } else if (value > 1.0) { |
kadonotakashi | 0:8fdf9a60065b | 64 | dac_write(obj, RANGE_12BIT); |
kadonotakashi | 0:8fdf9a60065b | 65 | } else { |
kadonotakashi | 0:8fdf9a60065b | 66 | dac_write(obj, value * (float)RANGE_12BIT); |
kadonotakashi | 0:8fdf9a60065b | 67 | } |
kadonotakashi | 0:8fdf9a60065b | 68 | } |
kadonotakashi | 0:8fdf9a60065b | 69 | |
kadonotakashi | 0:8fdf9a60065b | 70 | void analogout_write_u16(dac_t *obj, uint16_t value) { |
kadonotakashi | 0:8fdf9a60065b | 71 | dac_write(obj, value >> 4); // 12-bit |
kadonotakashi | 0:8fdf9a60065b | 72 | } |
kadonotakashi | 0:8fdf9a60065b | 73 | |
kadonotakashi | 0:8fdf9a60065b | 74 | float analogout_read(dac_t *obj) { |
kadonotakashi | 0:8fdf9a60065b | 75 | uint32_t value = dac_read(obj); |
kadonotakashi | 0:8fdf9a60065b | 76 | return (float)value * (1.0f / (float)RANGE_12BIT); |
kadonotakashi | 0:8fdf9a60065b | 77 | } |
kadonotakashi | 0:8fdf9a60065b | 78 | |
kadonotakashi | 0:8fdf9a60065b | 79 | uint16_t analogout_read_u16(dac_t *obj) { |
kadonotakashi | 0:8fdf9a60065b | 80 | uint32_t value = dac_read(obj); // 12-bit |
kadonotakashi | 0:8fdf9a60065b | 81 | return (value << 4) | ((value >> 8) & 0x003F); |
kadonotakashi | 0:8fdf9a60065b | 82 | } |
kadonotakashi | 0:8fdf9a60065b | 83 | |
kadonotakashi | 0:8fdf9a60065b | 84 | #endif |