Skip to content

Commit 696a713

Browse files
committed
[FIX] K64F - adc, sgtl driver updates
1 parent 5c3edcb commit 696a713

File tree

7 files changed

+149
-49
lines changed

7 files changed

+149
-49
lines changed

libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_K64F/analogin_api.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,27 @@
1919
#include "pinmap.h"
2020
#include "error.h"
2121
#include "fsl_adc_driver.h"
22-
22+
#include "PeripheralNames.h"
23+
2324
static const PinMap PinMap_ADC[] = {
2425
{PTC0, ADC0_SE14, 0},
2526
{NC, NC, 0}
2627
};
2728

2829
void analogin_init(analogin_t *obj, PinName pin) {
2930
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
30-
adc_config_t module_config;
31+
adc_user_config_t module_config;
3132
uint32_t instance = obj->adc >> ADC_SHIFT;
3233

3334
module_config.clockSourceMode = kAdcClockSourceBusClk;
3435
module_config.clockSourceDividerMode = kAdcClockDivider8;
3536
module_config.resolutionMode = kAdcSingleDiff16;
3637
module_config.referenceVoltageMode = kAdcVoltageVref;
37-
module_config.isContinuousEnabled = true;
38-
module_config.calibrationParam = NULL;
38+
module_config.isContinuousEnabled = false;
3939

4040
adc_init(instance, &module_config);
4141

42-
obj->channel_cfg.channelId =(adc_channel_mode_t)(obj->adc & 0x7F);
42+
obj->channel_cfg.channelId =(adc_channel_mode_t)(obj->adc & 0xF);
4343
obj->channel_cfg.isDifferentialEnabled = false;
4444
obj->channel_cfg.isInterruptEnabled = false;
4545
obj->channel_cfg.muxSelect = kAdcChannelMuxB;
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2013 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include "analogout_api.h"
17+
18+
#include "cmsis.h"
19+
#include "pinmap.h"
20+
#include "error.h"
21+
#include "fsl_gpio_driver.h"
22+
23+
#define RANGE_12BIT 0xFFF
24+
25+
const PinMap PinMap_DAC[] = {
26+
{PTE23, DAC_0, 0},
27+
{NC , NC , 0}
28+
};
29+
30+
void analogout_init(dac_t *obj, PinName pin) {
31+
obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
32+
if (obj->dac == (DACName)NC) {
33+
error("DAC pin mapping failed");
34+
}
35+
36+
SIM->SCGC6 |= SIM_SCGC6_DAC0_MASK;
37+
38+
uint32_t port = (uint32_t)pin >> GPIO_PORT_SHIFT;
39+
SIM->SCGC5 |= 1 << (SIM_SCGC5_PORTA_SHIFT + port);
40+
41+
DAC0->DAT[obj->dac].DATH = 0;
42+
DAC0->DAT[obj->dac].DATL = 0;
43+
44+
DAC0->C1 = DAC_C1_DACBFMD_MASK; // One-Time Scan Mode
45+
46+
DAC0->C0 = DAC_C0_DACEN_MASK // Enable
47+
| DAC_C0_DACSWTRG_MASK; // Software Trigger
48+
49+
pinmap_pinout(pin, PinMap_DAC);
50+
51+
analogout_write_u16(obj, 0);
52+
}
53+
54+
void analogout_free(dac_t *obj) {}
55+
56+
static inline void dac_write(dac_t *obj, int value) {
57+
DAC0->DAT[obj->dac].DATL = (uint8_t)( value & 0xFF);
58+
DAC0->DAT[obj->dac].DATH = (uint8_t)((value >> 8) & 0xFF);
59+
}
60+
61+
static inline int dac_read(dac_t *obj) {
62+
return ((DAC0->DAT[obj->dac].DATH << 8) | DAC0->DAT[obj->dac].DATL);
63+
}
64+
65+
void analogout_write(dac_t *obj, float value) {
66+
if (value < 0.0f) {
67+
dac_write(obj, 0);
68+
} else if (value > 1.0f) {
69+
dac_write(obj, RANGE_12BIT);
70+
} else {
71+
dac_write(obj, value * (float)RANGE_12BIT);
72+
}
73+
}
74+
75+
void analogout_write_u16(dac_t *obj, uint16_t value) {
76+
dac_write(obj, value >> 4); // 12-bit
77+
}
78+
79+
float analogout_read(dac_t *obj) {
80+
uint32_t value = dac_read(obj);
81+
return (float)value * (1.0f / (float)RANGE_12BIT);
82+
}
83+
84+
uint16_t analogout_read_u16(dac_t *obj) {
85+
uint32_t value = dac_read(obj); // 12-bit
86+
return (value << 4) | ((value >> 8) & 0x003F);
87+
}

libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_K64F/device.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#define DEVICE_INTERRUPTIN 1
2424

2525
#define DEVICE_ANALOGIN 1
26-
#define DEVICE_ANALOGOUT 0
26+
#define DEVICE_ANALOGOUT 1
2727

2828
#define DEVICE_SERIAL 1
2929

libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_K64F/objects.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ struct spi_s {
5858
uint32_t instance;
5959
};
6060

61+
struct dac_s {
62+
DACName dac;
63+
};
64+
6165
#include "gpio_object.h"
6266

6367
#ifdef __cplusplus

libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/boards/common/sgtl5000/fsl_sgtl5000_driver.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ extern "C" {
123123
/*!
124124
* @brief sgtl5000 initialize function.
125125
*
126-
* This function would call sgtl_i2c_init(), and in this fuction, some configurations
126+
* This function would call sgtl_i2c_init(), and in this function, some configurations
127127
* are fixed. The second parameter is NULL to sgtl5000 in this version. If users want
128128
* to change the settings, they have to use sgtl_write_reg() or sgtl_modify_reg()
129129
* to set the register value of sgtl5000.
@@ -147,7 +147,7 @@ sgtl_status_t sgtl_i2c_init(sgtl_handler_t *handler);
147147
sgtl_status_t sgtl_deinit(sgtl_handler_t *handler);
148148

149149
/*!
150-
* @brief Configure the data foramt of audio data.
150+
* @brief Configure the data format of audio data.
151151
*
152152
* This function would configure the registers about the sample rate, bit depths.
153153
* @param handler Sgtl5000 handler structure pointer.

libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/boards/common/sgtl5000/src/fsl_sgtl5000_driver.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
/*******************************************************************************
3333
*Code
3434
******************************************************************************/
35+
static volatile uint32_t i2c_state = 0;
3536

3637
/*FUNCTION**********************************************************************
3738
*
@@ -41,7 +42,11 @@
4142
*END**************************************************************************/
4243
sgtl_status_t sgtl_init(sgtl_handler_t *handler, void *codec_config)
4344
{
44-
sgtl_i2c_init(handler);
45+
if(i2c_state == 0)
46+
{
47+
sgtl_i2c_init(handler);
48+
}
49+
i2c_state ++;
4550
/*
4651
* Power Supply Configuration
4752
* NOTE: This next 2 Write calls is needed ONLY if VDDD is internally driven by the chip.
@@ -185,7 +190,11 @@ sgtl_status_t sgtl_i2c_init(sgtl_handler_t *handler)
185190
*END**************************************************************************/
186191
sgtl_status_t sgtl_deinit(sgtl_handler_t *handler)
187192
{
188-
i2c_master_shutdown(&handler->master);
193+
i2c_state --;
194+
if(i2c_state == 0)
195+
{
196+
i2c_master_shutdown(&handler->master);
197+
}
189198
return kStatus_SGTL_Success;
190199
}
191200

@@ -304,7 +313,7 @@ sgtl_status_t sgtl_write_reg(sgtl_handler_t *handler, uint16_t reg, uint16_t val
304313
buff[2] = (val & 0xFF00U) >> 8U;
305314
buff[3] = val & 0xFF;
306315

307-
i2c_master_transfer(master, device , kI2CWrite, 0, 0, buff, 4, &transferred, kSyncWaitForever);
316+
i2c_master_transfer(master, device , kI2CWrite, true, 0, 0, buff, 4, &transferred, kSyncWaitForever);
308317
return kStatus_SGTL_Success;
309318
}
310319

@@ -324,12 +333,12 @@ sgtl_status_t sgtl_read_reg(sgtl_handler_t *handler, uint16_t reg, uint16_t *val
324333
uint8_t retval = 0;
325334
buff[0] = (reg & 0xFF00U) >> 8U;
326335
buff[1] = reg & 0xFF;
327-
retval = i2c_master_transfer(master, device , kI2CWrite, 0, 0, buff, 2, &transferred, kSyncWaitForever);
336+
retval = i2c_master_transfer(master, device , kI2CWrite, true, 0, 0, buff, 2, &transferred, kSyncWaitForever);
328337
if(retval != kStatus_SGTL_Success)
329338
{
330339
return kStatus_SGTL_Fail;
331340
}
332-
retval = i2c_master_transfer(master, device , kI2CRead, 0, 0, data, 2, &transferred, kSyncWaitForever);
341+
retval = i2c_master_transfer(master, device , kI2CRead, true, 0, 0, data, 2, &transferred, kSyncWaitForever);
333342
*val = (uint16_t)(((uint32_t)data[0] << 8U) | (uint32_t)data[1]);
334343
return kStatus_SGTL_Success;
335344
}

0 commit comments

Comments
 (0)