|
23 | 23 | #include "mbed_error.h"
|
24 | 24 | #include "PeripheralPins.h"
|
25 | 25 |
|
26 |
| -DAC_HandleTypeDef hdac; |
| 26 | +#if STATIC_PINMAP_READY |
| 27 | +#define ANALOGOUT_INIT_DIRECT analogout_init_direct |
| 28 | +void analogout_init_direct(dac_t *obj, const PinMap *pinmap) |
| 29 | +#else |
| 30 | +#define ANALOGOUT_INIT_DIRECT _analogout_init_direct |
| 31 | +static void _analogout_init_direct(dac_t *obj, const PinMap *pinmap) |
| 32 | +#endif |
| 33 | +{ |
| 34 | + DAC_ChannelConfTypeDef sConfig = {0}; |
| 35 | + |
| 36 | + // Get the peripheral name from the pin and assign it to the object |
| 37 | + obj->dac = (DACName)pinmap->peripheral; |
| 38 | + MBED_ASSERT(obj->dac != (DACName)NC); |
| 39 | + |
| 40 | + // Get the pin function and assign the used channel to the object |
| 41 | + uint32_t function = (uint32_t)pinmap->function; |
| 42 | + MBED_ASSERT(function != (uint32_t)NC); |
| 43 | + |
| 44 | + switch (STM_PIN_CHANNEL(function)) { |
| 45 | + case 1: |
| 46 | + obj->channel = DAC_CHANNEL_1; |
| 47 | + break; |
| 48 | + default: |
| 49 | + error("Unknown DAC channel"); |
| 50 | + break; |
| 51 | + } |
| 52 | + |
| 53 | + // Configure GPIO |
| 54 | + pin_function(pinmap->pin, pinmap->function); |
| 55 | + pin_mode(pinmap->pin, PullNone); |
| 56 | + |
| 57 | + // Save the pin for future use |
| 58 | + obj->pin = pinmap->pin; |
| 59 | + |
| 60 | + // Enable DAC clock |
| 61 | + __HAL_RCC_DAC_CLK_ENABLE(); |
| 62 | + |
| 63 | + // Configure DAC |
| 64 | + obj->handle.Instance = DAC; |
| 65 | + obj->handle.State = HAL_DAC_STATE_RESET; |
| 66 | + |
| 67 | + if (HAL_DAC_Init(&obj->handle) != HAL_OK) { |
| 68 | + error("HAL_DAC_Init failed"); |
| 69 | + } |
| 70 | + |
| 71 | + sConfig.DAC_Trigger = DAC_TRIGGER_NONE; |
| 72 | + sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE; |
| 73 | + sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_DISABLE; |
| 74 | + sConfig.DAC_UserTrimming = DAC_TRIMMING_FACTORY; |
| 75 | + |
| 76 | + sConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_DISABLE; |
| 77 | + |
| 78 | + if (HAL_DAC_ConfigChannel(&obj->handle, &sConfig, obj->channel) != HAL_OK) { |
| 79 | + error("Cannot configure DAC channel\n"); |
| 80 | + } |
| 81 | + |
| 82 | + analogout_write_u16(obj, 0); |
| 83 | +} |
27 | 84 |
|
28 | 85 | void analogout_init(dac_t *obj, PinName pin)
|
29 | 86 | {
|
| 87 | + int peripheral = (int)pinmap_peripheral(pin, PinMap_DAC); |
| 88 | + int function = (int)pinmap_find_function(pin, PinMap_DAC); |
| 89 | + |
| 90 | + const PinMap static_pinmap = {pin, peripheral, function}; |
30 | 91 |
|
31 |
| - DAC_ChannelConfTypeDef sConfig = {0}; |
32 |
| - |
33 |
| - /* obj Initialization */ |
34 |
| - obj->dac = (DACName)NC; |
35 |
| - |
36 |
| - obj->pin = pin; |
37 |
| - |
38 |
| - obj->channel = DAC_CHANNEL_1; |
39 |
| - |
40 |
| - /* DAC Initialization */ |
41 |
| - hdac.Instance = DAC; |
42 |
| - if (HAL_DAC_Init(&hdac) != HAL_OK) |
43 |
| - { |
44 |
| - //Error_Handler(); |
45 |
| - } |
46 |
| - /** DAC channel OUT1 config |
47 |
| - */ |
48 |
| - sConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_DISABLE; |
49 |
| - sConfig.DAC_Trigger = DAC_TRIGGER_T1_TRGO; |
50 |
| - sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE; |
51 |
| - sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_DISABLE; |
52 |
| - sConfig.DAC_UserTrimming = DAC_TRIMMING_FACTORY; |
53 |
| - |
54 |
| - if (HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_1) != HAL_OK) |
55 |
| - { |
56 |
| - //Error_Handler(); |
57 |
| - } |
58 |
| - |
| 92 | + ANALOGOUT_INIT_DIRECT(obj, &static_pinmap); |
| 93 | +} |
| 94 | + |
| 95 | +void analogout_free(dac_t *obj) |
| 96 | +{ |
| 97 | + __HAL_RCC_DAC_FORCE_RESET(); |
| 98 | + __HAL_RCC_DAC_RELEASE_RESET(); |
| 99 | + __HAL_RCC_DAC_CLK_DISABLE(); |
| 100 | + |
| 101 | + // Configure GPIO |
| 102 | + pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
59 | 103 | }
|
60 | 104 |
|
61 | 105 | const PinMap *analogout_pinmap()
|
62 | 106 | {
|
63 | 107 | return PinMap_DAC;
|
64 | 108 | }
|
65 |
| -#endif |
| 109 | + |
| 110 | +#endif // DEVICE_ANALOGOUT |
0 commit comments