Skip to content

Commit 72ea613

Browse files
committed
Nuvoton: Add i2c_free
1. Disable interrupt 2. Disable IP clock 3. Free up pins Support targets: - NUMAKER_PFM_NANO130 - NUMAKER_PFM_NUC472 - NUMAKER_PFM_M453 - NUMAKER_PFM_M487/NUMAKER_IOT_M487 - NU_PFM_M2351* - NUMAKER_IOT_M263A - NUMAKER_M252KG
1 parent 3abd026 commit 72ea613

File tree

7 files changed

+199
-0
lines changed

7 files changed

+199
-0
lines changed

targets/TARGET_NUVOTON/TARGET_M2351/i2c_api.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,37 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
147147
i2c_modinit_mask |= 1 << i;
148148
}
149149

150+
void i2c_free(i2c_t *obj)
151+
{
152+
const struct nu_modinit_s *modinit = get_modinit(obj->i2c.i2c, i2c_modinit_tab);
153+
MBED_ASSERT(modinit != NULL);
154+
MBED_ASSERT(modinit->modname == (int) obj->i2c.i2c);
155+
156+
/* Disable I2C interrupt */
157+
NVIC_DisableIRQ(modinit->irq_n);
158+
159+
I2C_T *i2c_base = (I2C_T *) NU_MODBASE(obj->i2c.i2c);
160+
161+
/* Disable I2C module */
162+
I2C_Close(i2c_base);
163+
164+
/* Disable IP clock
165+
*
166+
* NOTE: We must call secure version (from non-secure domain) because SYS/CLK regions are secure.
167+
*/
168+
CLK_DisableModuleClock_S(modinit->clkidx);
169+
170+
// Mark this module to be deinited.
171+
int i = modinit - i2c_modinit_tab;
172+
i2c_modinit_mask &= ~(1 << i);
173+
174+
/* Free up pins */
175+
gpio_set(obj->i2c.pin_sda);
176+
gpio_set(obj->i2c.pin_scl);
177+
obj->i2c.pin_sda = NC;
178+
obj->i2c.pin_scl = NC;
179+
}
180+
150181
int i2c_start(i2c_t *obj)
151182
{
152183
return i2c_do_trsn(obj, I2C_CTL0_STA_Msk | I2C_CTL0_SI_Msk, 1);

targets/TARGET_NUVOTON/TARGET_M251/i2c_api.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,34 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
137137
i2c_modinit_mask |= 1 << i;
138138
}
139139

140+
void i2c_free(i2c_t *obj)
141+
{
142+
const struct nu_modinit_s *modinit = get_modinit(obj->i2c.i2c, i2c_modinit_tab);
143+
MBED_ASSERT(modinit != NULL);
144+
MBED_ASSERT(modinit->modname == (int) obj->i2c.i2c);
145+
146+
/* Disable I2C interrupt */
147+
NVIC_DisableIRQ(modinit->irq_n);
148+
149+
I2C_T *i2c_base = (I2C_T *) NU_MODBASE(obj->i2c.i2c);
150+
151+
/* Disable I2C module */
152+
I2C_Close(i2c_base);
153+
154+
/* Disable IP clock */
155+
CLK_DisableModuleClock(modinit->clkidx);
156+
157+
// Mark this module to be deinited.
158+
int i = modinit - i2c_modinit_tab;
159+
i2c_modinit_mask &= ~(1 << i);
160+
161+
/* Free up pins */
162+
gpio_set(obj->i2c.pin_sda);
163+
gpio_set(obj->i2c.pin_scl);
164+
obj->i2c.pin_sda = NC;
165+
obj->i2c.pin_scl = NC;
166+
}
167+
140168
int i2c_start(i2c_t *obj)
141169
{
142170
return i2c_do_trsn(obj, I2C_CTL0_STA_Msk | I2C_CTL0_SI_Msk, 1);

targets/TARGET_NUVOTON/TARGET_M261/i2c_api.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,34 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
142142
i2c_modinit_mask |= 1 << i;
143143
}
144144

145+
void i2c_free(i2c_t *obj)
146+
{
147+
const struct nu_modinit_s *modinit = get_modinit(obj->i2c.i2c, i2c_modinit_tab);
148+
MBED_ASSERT(modinit != NULL);
149+
MBED_ASSERT(modinit->modname == (int) obj->i2c.i2c);
150+
151+
/* Disable I2C interrupt */
152+
NVIC_DisableIRQ(modinit->irq_n);
153+
154+
I2C_T *i2c_base = (I2C_T *) NU_MODBASE(obj->i2c.i2c);
155+
156+
/* Disable I2C module */
157+
I2C_Close(i2c_base);
158+
159+
/* Disable IP clock */
160+
CLK_DisableModuleClock(modinit->clkidx);
161+
162+
// Mark this module to be deinited.
163+
int i = modinit - i2c_modinit_tab;
164+
i2c_modinit_mask &= ~(1 << i);
165+
166+
/* Free up pins */
167+
gpio_set(obj->i2c.pin_sda);
168+
gpio_set(obj->i2c.pin_scl);
169+
obj->i2c.pin_sda = NC;
170+
obj->i2c.pin_scl = NC;
171+
}
172+
145173
int i2c_start(i2c_t *obj)
146174
{
147175
return i2c_do_trsn(obj, I2C_CTL0_STA_Msk | I2C_CTL0_SI_Msk, 1);

targets/TARGET_NUVOTON/TARGET_M451/i2c_api.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,34 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
150150
i2c_modinit_mask |= 1 << i;
151151
}
152152

153+
void i2c_free(i2c_t *obj)
154+
{
155+
const struct nu_modinit_s *modinit = get_modinit(obj->i2c.i2c, i2c_modinit_tab);
156+
MBED_ASSERT(modinit != NULL);
157+
MBED_ASSERT(modinit->modname == (int) obj->i2c.i2c);
158+
159+
/* Disable I2C interrupt */
160+
NVIC_DisableIRQ(modinit->irq_n);
161+
162+
I2C_T *i2c_base = (I2C_T *) NU_MODBASE(obj->i2c.i2c);
163+
164+
/* Disable I2C module */
165+
I2C_Close(i2c_base);
166+
167+
/* Disable IP clock */
168+
CLK_DisableModuleClock(modinit->clkidx);
169+
170+
// Mark this module to be deinited.
171+
int i = modinit - i2c_modinit_tab;
172+
i2c_modinit_mask &= ~(1 << i);
173+
174+
/* Free up pins */
175+
gpio_set(obj->i2c.pin_sda);
176+
gpio_set(obj->i2c.pin_scl);
177+
obj->i2c.pin_sda = NC;
178+
obj->i2c.pin_scl = NC;
179+
}
180+
153181
int i2c_start(i2c_t *obj)
154182
{
155183
return i2c_do_trsn(obj, I2C_CTL_STA_Msk | I2C_CTL_SI_Msk, 1);

targets/TARGET_NUVOTON/TARGET_M480/i2c_api.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,34 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
141141
i2c_modinit_mask |= 1 << i;
142142
}
143143

144+
void i2c_free(i2c_t *obj)
145+
{
146+
const struct nu_modinit_s *modinit = get_modinit(obj->i2c.i2c, i2c_modinit_tab);
147+
MBED_ASSERT(modinit != NULL);
148+
MBED_ASSERT(modinit->modname == (int) obj->i2c.i2c);
149+
150+
/* Disable I2C interrupt */
151+
NVIC_DisableIRQ(modinit->irq_n);
152+
153+
I2C_T *i2c_base = (I2C_T *) NU_MODBASE(obj->i2c.i2c);
154+
155+
/* Disable I2C module */
156+
I2C_Close(i2c_base);
157+
158+
/* Disable IP clock */
159+
CLK_DisableModuleClock(modinit->clkidx);
160+
161+
// Mark this module to be deinited.
162+
int i = modinit - i2c_modinit_tab;
163+
i2c_modinit_mask &= ~(1 << i);
164+
165+
/* Free up pins */
166+
gpio_set(obj->i2c.pin_sda);
167+
gpio_set(obj->i2c.pin_scl);
168+
obj->i2c.pin_sda = NC;
169+
obj->i2c.pin_scl = NC;
170+
}
171+
144172
int i2c_start(i2c_t *obj)
145173
{
146174
return i2c_do_trsn(obj, I2C_CTL0_STA_Msk | I2C_CTL0_SI_Msk, 1);

targets/TARGET_NUVOTON/TARGET_NANO100/i2c_api.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,34 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
166166
i2c_modinit_mask |= 1 << i;
167167
}
168168

169+
void i2c_free(i2c_t *obj)
170+
{
171+
const struct nu_modinit_s *modinit = get_modinit(obj->i2c.i2c, i2c_modinit_tab);
172+
MBED_ASSERT(modinit != NULL);
173+
MBED_ASSERT(modinit->modname == (int) obj->i2c.i2c);
174+
175+
/* Disable I2C interrupt */
176+
NVIC_DisableIRQ(modinit->irq_n);
177+
178+
I2C_T *i2c_base = (I2C_T *) NU_MODBASE(obj->i2c.i2c);
179+
180+
/* Disable I2C module */
181+
I2C_Close(i2c_base);
182+
183+
/* Disable IP clock */
184+
CLK_DisableModuleClock(modinit->clkidx);
185+
186+
// Mark this module to be deinited.
187+
int i = modinit - i2c_modinit_tab;
188+
i2c_modinit_mask &= ~(1 << i);
189+
190+
/* Free up pins */
191+
gpio_set(obj->i2c.pin_sda);
192+
gpio_set(obj->i2c.pin_scl);
193+
obj->i2c.pin_sda = NC;
194+
obj->i2c.pin_scl = NC;
195+
}
196+
169197
int i2c_start(i2c_t *obj)
170198
{
171199
return i2c_do_trsn(obj, I2C_CON_START_Msk | I2C_CON_I2C_STS_Msk, 1);

targets/TARGET_NUVOTON/TARGET_NUC472/i2c_api.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,34 @@ int i2c_start(i2c_t *obj)
172172
return i2c_do_trsn(obj, I2C_CTL_STA_Msk | I2C_CTL_SI_Msk, 1);
173173
}
174174

175+
void i2c_free(i2c_t *obj)
176+
{
177+
const struct nu_modinit_s *modinit = get_modinit(obj->i2c.i2c, i2c_modinit_tab);
178+
MBED_ASSERT(modinit != NULL);
179+
MBED_ASSERT(modinit->modname == (int) obj->i2c.i2c);
180+
181+
/* Disable I2C interrupt */
182+
NVIC_DisableIRQ(modinit->irq_n);
183+
184+
I2C_T *i2c_base = (I2C_T *) NU_MODBASE(obj->i2c.i2c);
185+
186+
/* Disable I2C module */
187+
I2C_Close(i2c_base);
188+
189+
/* Disable IP clock */
190+
CLK_DisableModuleClock(modinit->clkidx);
191+
192+
// Mark this module to be deinited.
193+
int i = modinit - i2c_modinit_tab;
194+
i2c_modinit_mask &= ~(1 << i);
195+
196+
/* Free up pins */
197+
gpio_set(obj->i2c.pin_sda);
198+
gpio_set(obj->i2c.pin_scl);
199+
obj->i2c.pin_sda = NC;
200+
obj->i2c.pin_scl = NC;
201+
}
202+
175203
int i2c_stop(i2c_t *obj)
176204
{
177205
return i2c_do_trsn(obj, I2C_CTL_STO_Msk | I2C_CTL_SI_Msk, 1);

0 commit comments

Comments
 (0)