Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nuvoton: Add implementations of HAL API i2c_free and analogin_free #11780

Merged
merged 3 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions targets/TARGET_NUVOTON/TARGET_M2351/analogin_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,40 @@ void analogin_init(analogin_t *obj, PinName pin)
eadc_modinit_mask |= 1 << chn;
}

void analogin_free(analogin_t *obj)
{
const struct nu_modinit_s *modinit = get_modinit(obj->adc, adc_modinit_tab);
MBED_ASSERT(modinit->modname == (int) obj->adc);

/* Module subindex (aka channel) */
uint32_t chn = NU_MODSUBINDEX(obj->adc);

EADC_T *eadc_base = (EADC_T *) NU_MODBASE(obj->adc);

/* Channel-level windup from here */

/* Mark channel free */
eadc_modinit_mask &= ~(1 << chn);

/* Module-level windup from here */

/* See analogin_init() for reason */
if (! eadc_modinit_mask) {
/* Disable EADC module */
EADC_Close(eadc_base);

/* Disable IP clock
*
* NOTE: We must call secure version (from non-secure domain) because SYS/CLK regions are secure.
*/
CLK_DisableModuleClock_S(modinit->clkidx);
}

/* Free up pins */
gpio_set(obj->pin);
obj->pin = NC;
}

uint16_t analogin_read_u16(analogin_t *obj)
{
EADC_T *eadc_base = (EADC_T *) NU_MODBASE(obj->adc);
Expand Down
31 changes: 31 additions & 0 deletions targets/TARGET_NUVOTON/TARGET_M2351/i2c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,37 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
i2c_modinit_mask |= 1 << i;
}

void i2c_free(i2c_t *obj)
{
const struct nu_modinit_s *modinit = get_modinit(obj->i2c.i2c, i2c_modinit_tab);
MBED_ASSERT(modinit != NULL);
MBED_ASSERT(modinit->modname == (int) obj->i2c.i2c);

/* Disable I2C interrupt */
NVIC_DisableIRQ(modinit->irq_n);

I2C_T *i2c_base = (I2C_T *) NU_MODBASE(obj->i2c.i2c);

/* Disable I2C module */
I2C_Close(i2c_base);

/* Disable IP clock
*
* NOTE: We must call secure version (from non-secure domain) because SYS/CLK regions are secure.
*/
CLK_DisableModuleClock_S(modinit->clkidx);

// Mark this module to be deinited.
int i = modinit - i2c_modinit_tab;
i2c_modinit_mask &= ~(1 << i);

/* Free up pins */
gpio_set(obj->i2c.pin_sda);
gpio_set(obj->i2c.pin_scl);
obj->i2c.pin_sda = NC;
obj->i2c.pin_scl = NC;
}

int i2c_start(i2c_t *obj)
{
return i2c_do_trsn(obj, I2C_CTL0_STA_Msk | I2C_CTL0_SI_Msk, 1);
Expand Down
31 changes: 31 additions & 0 deletions targets/TARGET_NUVOTON/TARGET_M251/analogin_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,37 @@ void analogin_init(analogin_t *obj, PinName pin)
eadc_modinit_mask |= 1 << chn;
}

void analogin_free(analogin_t *obj)
{
const struct nu_modinit_s *modinit = get_modinit(obj->adc, adc_modinit_tab);
MBED_ASSERT(modinit->modname == (int) obj->adc);

/* Module subindex (aka channel) */
uint32_t chn = NU_MODSUBINDEX(obj->adc);

EADC_T *eadc_base = (EADC_T *) NU_MODBASE(obj->adc);

/* Channel-level windup from here */

/* Mark channel free */
eadc_modinit_mask &= ~(1 << chn);

/* Module-level windup from here */

/* See analogin_init() for reason */
if (! eadc_modinit_mask) {
/* Disable EADC module */
EADC_Close(eadc_base);

/* Disable IP clock */
CLK_DisableModuleClock(modinit->clkidx);
}

/* Free up pins */
gpio_set(obj->pin);
obj->pin = NC;
}

uint16_t analogin_read_u16(analogin_t *obj)
{
EADC_T *eadc_base = (EADC_T *) NU_MODBASE(obj->adc);
Expand Down
28 changes: 28 additions & 0 deletions targets/TARGET_NUVOTON/TARGET_M251/i2c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,34 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
i2c_modinit_mask |= 1 << i;
}

void i2c_free(i2c_t *obj)
{
const struct nu_modinit_s *modinit = get_modinit(obj->i2c.i2c, i2c_modinit_tab);
MBED_ASSERT(modinit != NULL);
MBED_ASSERT(modinit->modname == (int) obj->i2c.i2c);

/* Disable I2C interrupt */
NVIC_DisableIRQ(modinit->irq_n);

I2C_T *i2c_base = (I2C_T *) NU_MODBASE(obj->i2c.i2c);

/* Disable I2C module */
I2C_Close(i2c_base);

/* Disable IP clock */
CLK_DisableModuleClock(modinit->clkidx);

// Mark this module to be deinited.
int i = modinit - i2c_modinit_tab;
i2c_modinit_mask &= ~(1 << i);

/* Free up pins */
gpio_set(obj->i2c.pin_sda);
gpio_set(obj->i2c.pin_scl);
obj->i2c.pin_sda = NC;
obj->i2c.pin_scl = NC;
}

int i2c_start(i2c_t *obj)
{
return i2c_do_trsn(obj, I2C_CTL0_STA_Msk | I2C_CTL0_SI_Msk, 1);
Expand Down
31 changes: 31 additions & 0 deletions targets/TARGET_NUVOTON/TARGET_M261/analogin_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,37 @@ void analogin_init(analogin_t *obj, PinName pin)
eadc_modinit_mask |= 1 << chn;
}

void analogin_free(analogin_t *obj)
{
const struct nu_modinit_s *modinit = get_modinit(obj->adc, adc_modinit_tab);
MBED_ASSERT(modinit->modname == (int) obj->adc);

/* Module subindex (aka channel) */
uint32_t chn = NU_MODSUBINDEX(obj->adc);

EADC_T *eadc_base = (EADC_T *) NU_MODBASE(obj->adc);

/* Channel-level windup from here */

/* Mark channel free */
eadc_modinit_mask &= ~(1 << chn);

/* Module-level windup from here */

/* See analogin_init() for reason */
if (! eadc_modinit_mask) {
/* Disable EADC module */
EADC_Close(eadc_base);

/* Disable IP clock */
CLK_DisableModuleClock(modinit->clkidx);
}

/* Free up pins */
gpio_set(obj->pin);
obj->pin = NC;
}

uint16_t analogin_read_u16(analogin_t *obj)
{
EADC_T *eadc_base = (EADC_T *) NU_MODBASE(obj->adc);
Expand Down
28 changes: 28 additions & 0 deletions targets/TARGET_NUVOTON/TARGET_M261/i2c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,34 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
i2c_modinit_mask |= 1 << i;
}

void i2c_free(i2c_t *obj)
{
const struct nu_modinit_s *modinit = get_modinit(obj->i2c.i2c, i2c_modinit_tab);
MBED_ASSERT(modinit != NULL);
MBED_ASSERT(modinit->modname == (int) obj->i2c.i2c);

/* Disable I2C interrupt */
NVIC_DisableIRQ(modinit->irq_n);

I2C_T *i2c_base = (I2C_T *) NU_MODBASE(obj->i2c.i2c);

/* Disable I2C module */
I2C_Close(i2c_base);

/* Disable IP clock */
CLK_DisableModuleClock(modinit->clkidx);

// Mark this module to be deinited.
int i = modinit - i2c_modinit_tab;
i2c_modinit_mask &= ~(1 << i);

/* Free up pins */
gpio_set(obj->i2c.pin_sda);
gpio_set(obj->i2c.pin_scl);
obj->i2c.pin_sda = NC;
obj->i2c.pin_scl = NC;
}

int i2c_start(i2c_t *obj)
{
return i2c_do_trsn(obj, I2C_CTL0_STA_Msk | I2C_CTL0_SI_Msk, 1);
Expand Down
31 changes: 31 additions & 0 deletions targets/TARGET_NUVOTON/TARGET_M451/analogin_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,37 @@ void analogin_init(analogin_t *obj, PinName pin)
eadc_modinit_mask |= 1 << chn;
}

void analogin_free(analogin_t *obj)
{
const struct nu_modinit_s *modinit = get_modinit(obj->adc, adc_modinit_tab);
MBED_ASSERT(modinit->modname == (int) obj->adc);

/* Module subindex (aka channel) */
uint32_t chn = NU_MODSUBINDEX(obj->adc);

EADC_T *eadc_base = (EADC_T *) NU_MODBASE(obj->adc);

/* Channel-level windup from here */

/* Mark channel free */
eadc_modinit_mask &= ~(1 << chn);

/* Module-level windup from here */

/* See analogin_init() for reason */
if (! eadc_modinit_mask) {
/* Disable EADC module */
EADC_Close(eadc_base);

/* Disable IP clock */
CLK_DisableModuleClock(modinit->clkidx);
}

/* Free up pins */
gpio_set(obj->pin);
obj->pin = NC;
}

uint16_t analogin_read_u16(analogin_t *obj)
{
EADC_T *eadc_base = (EADC_T *) NU_MODBASE(obj->adc);
Expand Down
28 changes: 28 additions & 0 deletions targets/TARGET_NUVOTON/TARGET_M451/i2c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,34 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
i2c_modinit_mask |= 1 << i;
}

void i2c_free(i2c_t *obj)
{
const struct nu_modinit_s *modinit = get_modinit(obj->i2c.i2c, i2c_modinit_tab);
MBED_ASSERT(modinit != NULL);
MBED_ASSERT(modinit->modname == (int) obj->i2c.i2c);

/* Disable I2C interrupt */
NVIC_DisableIRQ(modinit->irq_n);

I2C_T *i2c_base = (I2C_T *) NU_MODBASE(obj->i2c.i2c);

/* Disable I2C module */
I2C_Close(i2c_base);

/* Disable IP clock */
CLK_DisableModuleClock(modinit->clkidx);

// Mark this module to be deinited.
int i = modinit - i2c_modinit_tab;
i2c_modinit_mask &= ~(1 << i);

/* Free up pins */
gpio_set(obj->i2c.pin_sda);
gpio_set(obj->i2c.pin_scl);
obj->i2c.pin_sda = NC;
obj->i2c.pin_scl = NC;
}

int i2c_start(i2c_t *obj)
{
return i2c_do_trsn(obj, I2C_CTL_STA_Msk | I2C_CTL_SI_Msk, 1);
Expand Down
31 changes: 31 additions & 0 deletions targets/TARGET_NUVOTON/TARGET_M480/analogin_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,37 @@ void analogin_init(analogin_t *obj, PinName pin)
eadc_modinit_mask |= 1 << chn;
}

void analogin_free(analogin_t *obj)
{
const struct nu_modinit_s *modinit = get_modinit(obj->adc, adc_modinit_tab);
MBED_ASSERT(modinit->modname == (int) obj->adc);

/* Module subindex (aka channel) */
uint32_t chn = NU_MODSUBINDEX(obj->adc);

EADC_T *eadc_base = (EADC_T *) NU_MODBASE(obj->adc);

/* Channel-level windup from here */

/* Mark channel free */
eadc_modinit_mask &= ~(1 << chn);

/* Module-level windup from here */

/* See analogin_init() for reason */
if (! eadc_modinit_mask) {
/* Disable EADC module */
EADC_Close(eadc_base);

/* Disable IP clock */
CLK_DisableModuleClock(modinit->clkidx);
}

/* Free up pins */
gpio_set(obj->pin);
obj->pin = NC;
}

uint16_t analogin_read_u16(analogin_t *obj)
{
EADC_T *eadc_base = (EADC_T *) NU_MODBASE(obj->adc);
Expand Down
28 changes: 28 additions & 0 deletions targets/TARGET_NUVOTON/TARGET_M480/i2c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,34 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
i2c_modinit_mask |= 1 << i;
}

void i2c_free(i2c_t *obj)
{
const struct nu_modinit_s *modinit = get_modinit(obj->i2c.i2c, i2c_modinit_tab);
MBED_ASSERT(modinit != NULL);
MBED_ASSERT(modinit->modname == (int) obj->i2c.i2c);

/* Disable I2C interrupt */
NVIC_DisableIRQ(modinit->irq_n);

I2C_T *i2c_base = (I2C_T *) NU_MODBASE(obj->i2c.i2c);

/* Disable I2C module */
I2C_Close(i2c_base);

/* Disable IP clock */
CLK_DisableModuleClock(modinit->clkidx);

// Mark this module to be deinited.
int i = modinit - i2c_modinit_tab;
i2c_modinit_mask &= ~(1 << i);

/* Free up pins */
gpio_set(obj->i2c.pin_sda);
gpio_set(obj->i2c.pin_scl);
obj->i2c.pin_sda = NC;
obj->i2c.pin_scl = NC;
}

int i2c_start(i2c_t *obj)
{
return i2c_do_trsn(obj, I2C_CTL0_STA_Msk | I2C_CTL0_SI_Msk, 1);
Expand Down
Loading