Software APIs
dt_pwm.c
Go to the documentation of this file.
1// Copyright lowRISC contributors (OpenTitan project).
2// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3// SPDX-License-Identifier: Apache-2.0
4//
5// Device table API auto-generated by `dtgen`
6
7/**
8 * @file
9 * @brief Device Tables (DT) for IP pwm and top earlgrey.
10 */
11
12#include "dt/dt_pwm.h"
13
14
15
16/**
17 * Description of instances.
18 */
19typedef struct dt_desc_pwm {
20 dt_instance_id_t inst_id; /**< Instance ID */
21 uint32_t base_addr[kDtPwmRegBlockCount]; /**< Base address of each register block */
22 /**
23 * Alert ID of the first Alert of this instance.
24 *
25 * This value is undefined if the block is not connected to the Alert Handler.
26 */
28 dt_clock_t clock[kDtPwmClockCount]; /**< Clock signal connected to each clock port */
29 dt_reset_t reset[kDtPwmResetCount]; /**< Reset signal connected to each reset port */
30 dt_periph_io_t periph_io[kDtPwmPeriphIoCount]; /**< Description of each peripheral I/O */
32
33
34
35
36static const dt_desc_pwm_t pwm_desc[kDtPwmCount] = {
37 [kDtPwmAon] = {
38 .inst_id = kDtInstanceIdPwmAon,
39 .base_addr = {
40 [kDtPwmRegBlockCore] = 0x40450000,
41 },
43 .clock = {
46 },
47 .reset = {
50 },
51 .periph_io = {
52 [kDtPwmPeriphIoPwm0] = {
53 .__internal = {
54 .type = kDtPeriphIoTypeMio,
55 .dir = kDtPeriphIoDirOut,
56 .periph_input_or_direct_pad = 0,
57 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselPwmAonPwm0,
58 },
59 },
60 [kDtPwmPeriphIoPwm1] = {
61 .__internal = {
62 .type = kDtPeriphIoTypeMio,
63 .dir = kDtPeriphIoDirOut,
64 .periph_input_or_direct_pad = 0,
65 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselPwmAonPwm1,
66 },
67 },
68 [kDtPwmPeriphIoPwm2] = {
69 .__internal = {
70 .type = kDtPeriphIoTypeMio,
71 .dir = kDtPeriphIoDirOut,
72 .periph_input_or_direct_pad = 0,
73 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselPwmAonPwm2,
74 },
75 },
76 [kDtPwmPeriphIoPwm3] = {
77 .__internal = {
78 .type = kDtPeriphIoTypeMio,
79 .dir = kDtPeriphIoDirOut,
80 .periph_input_or_direct_pad = 0,
81 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselPwmAonPwm3,
82 },
83 },
84 [kDtPwmPeriphIoPwm4] = {
85 .__internal = {
86 .type = kDtPeriphIoTypeMio,
87 .dir = kDtPeriphIoDirOut,
88 .periph_input_or_direct_pad = 0,
89 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselPwmAonPwm4,
90 },
91 },
92 [kDtPwmPeriphIoPwm5] = {
93 .__internal = {
94 .type = kDtPeriphIoTypeMio,
95 .dir = kDtPeriphIoDirOut,
96 .periph_input_or_direct_pad = 0,
97 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselPwmAonPwm5,
98 },
99 },
100 },
101 },
102};
103
104/**
105 * Return a pointer to the `dt_pwm_desc_t` structure of the requested
106 * `dt` if it's a valid index. Otherwise, this macro will `return` (i.e. exit
107 * the function) with the provided default value.
108 */
109#define TRY_GET_DT(dt, default) ({ if ((dt) < (dt_pwm_t)0 || (dt) >= kDtPwmCount) return (default); &pwm_desc[dt]; })
110
112 if (inst_id >= kDtInstanceIdPwmAon && inst_id <= kDtInstanceIdPwmAon) {
113 return (dt_pwm_t)(inst_id - kDtInstanceIdPwmAon);
114 }
115 return (dt_pwm_t)0;
116}
117
122
124 dt_pwm_t dt,
125 dt_pwm_reg_block_t reg_block) {
126 // Return a recognizable address in case of wrong argument.
127 return TRY_GET_DT(dt, 0xdeadbeef)->base_addr[reg_block];
128}
129
130
132 dt_pwm_t dt,
133 dt_pwm_alert_t alert) {
134 return (dt_alert_id_t)((uint32_t)pwm_desc[dt].first_alert + (uint32_t)alert);
135}
136
138 dt_pwm_t dt,
139 dt_alert_id_t alert) {
140 dt_pwm_alert_t count = kDtPwmAlertCount;
141 if (alert < pwm_desc[dt].first_alert || alert >= pwm_desc[dt].first_alert + (dt_alert_id_t)count) {
142 return count;
143 }
144 return (dt_pwm_alert_t)(alert - pwm_desc[dt].first_alert);
145}
146
147
149 dt_pwm_t dt,
150 dt_pwm_periph_io_t sig) {
151 // Return a harmless value in case of wrong argument.
152 return TRY_GET_DT(dt, kDtPeriphIoConstantHighZ)->periph_io[sig];
153}
154
156 dt_pwm_t dt,
157 dt_pwm_clock_t clk) {
158 // Return the first clock in case of invalid argument.
159 return TRY_GET_DT(dt, (dt_clock_t)0)->clock[clk];
160}
161
163 dt_pwm_t dt,
164 dt_pwm_reset_t rst) {
165 const dt_pwm_reset_t count = kDtPwmResetCount;
166 if (rst >= count) {
167 return kDtResetUnknown;
168 }
169 return TRY_GET_DT(dt, kDtResetUnknown)->reset[rst];
170}
171
172