Skip to content
This repository has been archived by the owner on Feb 2, 2020. It is now read-only.

Commit

Permalink
Merge pull request #248 from ghi-electronics/#247_Port_ResetPeriphera…
Browse files Browse the repository at this point in the history
…l_NotTouchTheirPins

#247 port reset peripheral not touch their pins
  • Loading branch information
John Brochue committed Apr 2, 2018
2 parents c03dde8 + 8a55c0b commit 2b27297
Show file tree
Hide file tree
Showing 36 changed files with 261 additions and 324 deletions.
6 changes: 3 additions & 3 deletions Targets/AT91SAM9Rx64/AT91_AD.cpp
Expand Up @@ -118,7 +118,8 @@ TinyCLR_Result AT91_Adc_AcquireChannel(const TinyCLR_Adc_Provider *self, int32_t
}

TinyCLR_Result AT91_Adc_ReleaseChannel(const TinyCLR_Adc_Provider *self, int32_t channel) {
AT91_Gpio_ClosePin(AT91_Adc_GetPin(channel));
if (g_at91_adc_isOpened[channel])
AT91_Gpio_ClosePin(AT91_Adc_GetPin(channel));

g_at91_adc_isOpened[channel] = false;

Expand Down Expand Up @@ -215,8 +216,7 @@ bool AT91_Adc_IsChannelModeSupported(const TinyCLR_Adc_Provider *self, TinyCLR_A

void AT91_Adc_Reset() {
for (auto ch = 0; ch < AT91_Adc_GetControllerCount(); ch++) {
if (g_at91_adc_isOpened[ch])
AT91_Adc_ReleaseChannel(&adcProvider, ch);
AT91_Adc_ReleaseChannel(&adcProvider, ch);

g_at91_adc_isOpened[ch] = false;
}
Expand Down
23 changes: 11 additions & 12 deletions Targets/AT91SAM9Rx64/AT91_I2C.cpp
Expand Up @@ -405,34 +405,33 @@ TinyCLR_Result AT91_I2c_Release(const TinyCLR_I2c_Provider* self) {
AT91_I2C& I2C = AT91::I2C(portId);

I2C.TWI_CR = AT91_I2C::TWI_CR_SWRST;
if (g_I2cConfiguration[portId].isOpened) {

AT91_Interrupt_Disable(portId == 0 ? AT91C_ID_TWI0 : AT91C_ID_TWI1);

AT91_Interrupt_Disable(portId == 0 ? AT91C_ID_TWI0 : AT91C_ID_TWI1);

AT91_PMC &pmc = AT91::PMC();
pmc.DisablePeriphClock(portId == 0 ? AT91C_ID_TWI0 : AT91C_ID_TWI1);
AT91_PMC &pmc = AT91::PMC();
pmc.DisablePeriphClock(portId == 0 ? AT91C_ID_TWI0 : AT91C_ID_TWI1);

// disable
I2C.TWI_CR = AT91_I2C::TWI_CR_MSDIS;
// disable
I2C.TWI_CR = AT91_I2C::TWI_CR_MSDIS;

// disable all the interrupt
I2C.TWI_IDR = AT91_I2C::TWI_IDR_NACK | AT91_I2C::TWI_IDR_RXRDY | AT91_I2C::TWI_IDR_TXCOMP | AT91_I2C::TWI_IDR_TXRDY;
// disable all the interrupt
I2C.TWI_IDR = AT91_I2C::TWI_IDR_NACK | AT91_I2C::TWI_IDR_RXRDY | AT91_I2C::TWI_IDR_TXCOMP | AT91_I2C::TWI_IDR_TXRDY;


g_I2cConfiguration[portId].isOpened = false;

if (g_I2cConfiguration[self->Index].isOpened) {
AT91_Gpio_ClosePin(g_i2c_sda_pins[self->Index].number);
AT91_Gpio_ClosePin(g_i2c_scl_pins[self->Index].number);
}

g_I2cConfiguration[portId].isOpened = false;

return TinyCLR_Result::Success;
}

void AT91_I2c_Reset() {
for (auto i = 0; i < SIZEOF_ARRAY(g_i2c_scl_pins); i++) {
if (g_I2cConfiguration[i].isOpened)
AT91_I2c_Release(i2cProviders[i]);
AT91_I2c_Release(i2cProviders[i]);

g_I2cConfiguration[i].address = 0;
g_I2cConfiguration[i].clockRate = 0;
Expand Down
29 changes: 14 additions & 15 deletions Targets/AT91SAM9Rx64/AT91_SPI.cpp
Expand Up @@ -386,23 +386,23 @@ TinyCLR_Result AT91_Spi_Release(const TinyCLR_Spi_Provider* self) {

break;
}
if (g_SpiController[controller].isOpened) {
uint32_t clkPin, misoPin, mosiPin;

uint32_t clkPin, misoPin, mosiPin;

clkPin = g_at91_spi_sclk_pins[controller].number;
misoPin = g_at91_spi_miso_pins[controller].number;
mosiPin = g_at91_spi_mosi_pins[controller].number;
clkPin = g_at91_spi_sclk_pins[controller].number;
misoPin = g_at91_spi_miso_pins[controller].number;
mosiPin = g_at91_spi_mosi_pins[controller].number;

AT91_Gpio_ClosePin(clkPin);
AT91_Gpio_ClosePin(misoPin);
AT91_Gpio_ClosePin(mosiPin);
AT91_Gpio_ClosePin(clkPin);
AT91_Gpio_ClosePin(misoPin);
AT91_Gpio_ClosePin(mosiPin);

if (g_SpiController[controller].chipSelectLine != PIN_NONE) {
AT91_Gpio_ClosePin(g_SpiController[controller].chipSelectLine);
if (g_SpiController[controller].chipSelectLine != PIN_NONE) {
AT91_Gpio_ClosePin(g_SpiController[controller].chipSelectLine);

g_SpiController[controller].chipSelectLine = PIN_NONE;
g_SpiController[controller].chipSelectLine = PIN_NONE;
}
}

g_SpiController[controller].clockFrequency = 0;
g_SpiController[controller].dataBitLength = 0;

Expand Down Expand Up @@ -437,9 +437,8 @@ TinyCLR_Result AT91_Spi_GetSupportedDataBitLengths(const TinyCLR_Spi_Provider* s

void AT91_Spi_Reset() {
for (auto i = 0; i < TOTAL_SPI_CONTROLLERS; i++) {
if (g_SpiController[i].isOpened) {
AT91_Spi_Release(spiProviders[i]);
}
AT91_Spi_Release(spiProviders[i]);


g_SpiController[i].isOpened = false;
}
Expand Down
8 changes: 4 additions & 4 deletions Targets/AT91SAM9Rx64/AT91_USART.cpp
Expand Up @@ -490,8 +490,9 @@ TinyCLR_Result AT91_Uart_Release(const TinyCLR_Uart_Provider* self) {
int32_t uartId = AT91_Uart_GetPeripheralId(portNum);

AT91_Interrupt_Disable(uartId);

AT91_Uart_PinConfiguration(portNum, false);
if (g_UartController[portNum].isOpened) {
AT91_Uart_PinConfiguration(portNum, false);
}

pmc.DisablePeriphClock(uartId);

Expand Down Expand Up @@ -688,8 +689,7 @@ TinyCLR_Result AT91_Uart_SetIsRequestToSendEnabled(const TinyCLR_Uart_Provider*

void AT91_Uart_Reset() {
for (auto i = 0; i < TOTAL_UART_CONTROLLERS; i++) {
if (g_UartController[i].isOpened)
AT91_Uart_Release(uartProviders[i]);
AT91_Uart_Release(uartProviders[i]);

g_UartController[i].txBufferSize = 0;
g_UartController[i].rxBufferSize = 0;
Expand Down
6 changes: 3 additions & 3 deletions Targets/AT91SAM9X35/AT91_AD.cpp
Expand Up @@ -119,7 +119,8 @@ TinyCLR_Result AT91_Adc_AcquireChannel(const TinyCLR_Adc_Provider *self, int32_t
}

TinyCLR_Result AT91_Adc_ReleaseChannel(const TinyCLR_Adc_Provider *self, int32_t channel) {
AT91_Gpio_ClosePin(AT91_Adc_GetPin(channel));
if (g_at91_adc_isOpened[channel])
AT91_Gpio_ClosePin(AT91_Adc_GetPin(channel));

g_at91_adc_isOpened[channel] = false;

Expand Down Expand Up @@ -217,8 +218,7 @@ bool AT91_Adc_IsChannelModeSupported(const TinyCLR_Adc_Provider *self, TinyCLR_A

void AT91_Adc_Reset() {
for (auto ch = 0; ch < AT91_Adc_GetControllerCount(); ch++) {
if (g_at91_adc_isOpened[ch])
AT91_Adc_ReleaseChannel(&adcProvider, ch);
AT91_Adc_ReleaseChannel(&adcProvider, ch);

g_at91_adc_isOpened[ch] = false;
}
Expand Down
20 changes: 5 additions & 15 deletions Targets/AT91SAM9X35/AT91_CAN.cpp
Expand Up @@ -1475,24 +1475,15 @@ TinyCLR_Result AT91_Can_Release(const TinyCLR_Can_Provider* self) {

auto memoryProvider = (const TinyCLR_Memory_Provider*)apiProvider->FindDefault(apiProvider, TinyCLR_Api_Type::MemoryProvider);

TinyCLR_Result releasePin = AT91_Gpio_ReleasePin(nullptr, g_AT91_Can_Tx_Pins[channel].number);

if (releasePin != TinyCLR_Result::Success)
return releasePin;

releasePin = AT91_Gpio_ReleasePin(nullptr, g_AT91_Can_Rx_Pins[channel].number);

if (releasePin != TinyCLR_Result::Success)
return releasePin;

CAN_DisableIt(canController[channel].cand.pHw, 0xFFFFFFFF);

AT91_PMC &pmc = AT91::PMC();
pmc.DisablePeriphClock((channel == 0) ? AT91C_ID_CAN0 : AT91C_ID_CAN1);

// free pin
AT91_Gpio_ClosePin(g_AT91_Can_Tx_Pins[channel].number);
AT91_Gpio_ClosePin(g_AT91_Can_Rx_Pins[channel].number);
if (canController[channel].isOpened) {
AT91_Gpio_ClosePin(g_AT91_Can_Tx_Pins[channel].number);
AT91_Gpio_ClosePin(g_AT91_Can_Rx_Pins[channel].number);
}

if (canController[channel].canRxMessagesFifo != nullptr) {
memoryProvider->Free(memoryProvider, canController[channel].canRxMessagesFifo);
Expand Down Expand Up @@ -1880,8 +1871,7 @@ TinyCLR_Result AT91_Can_SetWriteBufferSize(const TinyCLR_Can_Provider* self, siz

void AT91_Can_Reset() {
for (int i = 0; i < TOTAL_CAN_CONTROLLERS; i++) {
if (canController[i].isOpened)
AT91_Can_Release(canProvider[i]);
AT91_Can_Release(canProvider[i]);

canController[i].isOpened = false;
}
Expand Down
22 changes: 10 additions & 12 deletions Targets/AT91SAM9X35/AT91_I2C.cpp
Expand Up @@ -405,23 +405,22 @@ TinyCLR_Result AT91_I2c_Release(const TinyCLR_I2c_Provider* self) {
AT91_I2C& I2C = AT91::I2C(portId);

I2C.TWI_CR = AT91_I2C::TWI_CR_SWRST;
if (g_I2cConfiguration[portId].isOpened) {

AT91_Interrupt_Disable(portId == 0 ? AT91C_ID_TWI0 : (portId == 1 ? AT91C_ID_TWI1 : AT91C_ID_TWI2));

AT91_Interrupt_Disable(portId == 0 ? AT91C_ID_TWI0 : (portId == 1 ? AT91C_ID_TWI1 : AT91C_ID_TWI2));

AT91_PMC &pmc = AT91::PMC();
pmc.DisablePeriphClock(portId == 0 ? AT91C_ID_TWI0 : (portId == 1 ? AT91C_ID_TWI1 : AT91C_ID_TWI2));
AT91_PMC &pmc = AT91::PMC();
pmc.DisablePeriphClock(portId == 0 ? AT91C_ID_TWI0 : (portId == 1 ? AT91C_ID_TWI1 : AT91C_ID_TWI2));

// disable
I2C.TWI_CR = AT91_I2C::TWI_CR_MSDIS;
// disable
I2C.TWI_CR = AT91_I2C::TWI_CR_MSDIS;

// disable all the interrupt
I2C.TWI_IDR = AT91_I2C::TWI_IDR_NACK | AT91_I2C::TWI_IDR_RXRDY | AT91_I2C::TWI_IDR_TXCOMP | AT91_I2C::TWI_IDR_TXRDY;
// disable all the interrupt
I2C.TWI_IDR = AT91_I2C::TWI_IDR_NACK | AT91_I2C::TWI_IDR_RXRDY | AT91_I2C::TWI_IDR_TXCOMP | AT91_I2C::TWI_IDR_TXRDY;


g_I2cConfiguration[portId].isOpened = false;
g_I2cConfiguration[portId].isOpened = false;

if (g_I2cConfiguration[self->Index].isOpened) {
AT91_Gpio_ClosePin(g_i2c_sda_pins[self->Index].number);
AT91_Gpio_ClosePin(g_i2c_scl_pins[self->Index].number);
}
Expand All @@ -431,8 +430,7 @@ TinyCLR_Result AT91_I2c_Release(const TinyCLR_I2c_Provider* self) {

void AT91_I2c_Reset() {
for (auto i = 0; i < SIZEOF_ARRAY(g_i2c_scl_pins); i++) {
if (g_I2cConfiguration[i].isOpened)
AT91_I2c_Release(i2cProviders[i]);
AT91_I2c_Release(i2cProviders[i]);

g_I2cConfiguration[i].address = 0;
g_I2cConfiguration[i].clockRate = 0;
Expand Down
26 changes: 13 additions & 13 deletions Targets/AT91SAM9X35/AT91_SPI.cpp
Expand Up @@ -395,20 +395,22 @@ TinyCLR_Result AT91_Spi_Release(const TinyCLR_Spi_Provider* self) {

}

uint32_t clkPin, misoPin, mosiPin;
if (g_SpiController[controller].isOpened) {
uint32_t clkPin, misoPin, mosiPin;

clkPin = g_at91_spi_sclk_pins[controller].number;
misoPin = g_at91_spi_miso_pins[controller].number;
mosiPin = g_at91_spi_mosi_pins[controller].number;
clkPin = g_at91_spi_sclk_pins[controller].number;
misoPin = g_at91_spi_miso_pins[controller].number;
mosiPin = g_at91_spi_mosi_pins[controller].number;

AT91_Gpio_ClosePin(clkPin);
AT91_Gpio_ClosePin(misoPin);
AT91_Gpio_ClosePin(mosiPin);
AT91_Gpio_ClosePin(clkPin);
AT91_Gpio_ClosePin(misoPin);
AT91_Gpio_ClosePin(mosiPin);

if (g_SpiController[controller].chipSelectLine != PIN_NONE) {
AT91_Gpio_ClosePin(g_SpiController[controller].chipSelectLine);
if (g_SpiController[controller].chipSelectLine != PIN_NONE) {
AT91_Gpio_ClosePin(g_SpiController[controller].chipSelectLine);

g_SpiController[controller].chipSelectLine = PIN_NONE;
g_SpiController[controller].chipSelectLine = PIN_NONE;
}
}

g_SpiController[controller].clockFrequency = 0;
Expand Down Expand Up @@ -445,9 +447,7 @@ TinyCLR_Result AT91_Spi_GetSupportedDataBitLengths(const TinyCLR_Spi_Provider* s

void AT91_Spi_Reset() {
for (auto i = 0; i < TOTAL_SPI_CONTROLLERS; i++) {
if (g_SpiController[i].isOpened) {
AT91_Spi_Release(spiProviders[i]);
}
AT91_Spi_Release(spiProviders[i]);

g_SpiController[i].isOpened = false;
}
Expand Down
6 changes: 3 additions & 3 deletions Targets/AT91SAM9X35/AT91_USART.cpp
Expand Up @@ -492,7 +492,8 @@ TinyCLR_Result AT91_Uart_Release(const TinyCLR_Uart_Provider* self) {

AT91_Interrupt_Disable(uartId);

AT91_Uart_PinConfiguration(portNum, false);
if (g_UartController[portNum].isOpened)
AT91_Uart_PinConfiguration(portNum, false);

pmc.DisablePeriphClock(uartId);

Expand Down Expand Up @@ -689,8 +690,7 @@ TinyCLR_Result AT91_Uart_SetIsRequestToSendEnabled(const TinyCLR_Uart_Provider*

void AT91_Uart_Reset() {
for (auto i = 0; i < TOTAL_UART_CONTROLLERS; i++) {
if (g_UartController[i].isOpened)
AT91_Uart_Release(uartProviders[i]);
AT91_Uart_Release(uartProviders[i]);

g_UartController[i].txBufferSize = 0;
g_UartController[i].rxBufferSize = 0;
Expand Down
6 changes: 3 additions & 3 deletions Targets/LPC177x_LPC178x/LPC17_AD.cpp
Expand Up @@ -297,7 +297,8 @@ TinyCLR_Result LPC17_Adc_AcquireChannel(const TinyCLR_Adc_Provider* self, int32_
}

TinyCLR_Result LPC17_Adc_ReleaseChannel(const TinyCLR_Adc_Provider* self, int32_t channel) {
LPC17_Gpio_ClosePin(g_lpc17_adc_pins[channel].number);
if (g_lpc17_adc_isOpened[channel])
LPC17_Gpio_ClosePin(g_lpc17_adc_pins[channel].number);

g_lpc17_adc_isOpened[channel] = false;

Expand Down Expand Up @@ -359,8 +360,7 @@ void LPC17_Adc_Reset() {
LPC_SC->PCONP &= ~PCONP_PCAD;

for (auto ch = 0; ch < SIZEOF_ARRAY(g_lpc17_adc_pins); ch++) {
if (g_lpc17_adc_isOpened[ch])
LPC17_Adc_ReleaseChannel(&adcProvider, ch);
LPC17_Adc_ReleaseChannel(&adcProvider, ch);

g_lpc17_adc_isOpened[ch] = false;
}
Expand Down
22 changes: 6 additions & 16 deletions Targets/LPC177x_LPC178x/LPC17_CAN.cpp
Expand Up @@ -2451,20 +2451,6 @@ TinyCLR_Result LPC17_Can_Release(const TinyCLR_Can_Provider* self) {

auto memoryProvider = (const TinyCLR_Memory_Provider*)apiProvider->FindDefault(apiProvider, TinyCLR_Api_Type::MemoryProvider);

TinyCLR_Result releasePin = LPC17_Gpio_ReleasePin(nullptr, g_LPC17_Can_Tx_Pins[channel].number);

if (releasePin != TinyCLR_Result::Success)
return releasePin;

releasePin = LPC17_Gpio_ReleasePin(nullptr, g_LPC17_Can_Rx_Pins[channel].number);

if (releasePin != TinyCLR_Result::Success)
return releasePin;

// free pin
LPC17_Gpio_ClosePin(g_LPC17_Can_Tx_Pins[channel].number);
LPC17_Gpio_ClosePin(g_LPC17_Can_Rx_Pins[channel].number);

if (canController[channel].canRxMessagesFifo != nullptr) {
memoryProvider->Free(memoryProvider, canController[channel].canRxMessagesFifo);

Expand All @@ -2474,6 +2460,11 @@ TinyCLR_Result LPC17_Can_Release(const TinyCLR_Can_Provider* self) {
CAN_DisableExplicitFilters(channel);
CAN_DisableGroupFilters(channel);

if (canController[channel].isOpened) {
LPC17_Gpio_ClosePin(g_LPC17_Can_Tx_Pins[channel].number);
LPC17_Gpio_ClosePin(g_LPC17_Can_Rx_Pins[channel].number);
}

canController[channel].isOpened = false;

return TinyCLR_Result::Success;
Expand Down Expand Up @@ -2828,8 +2819,7 @@ TinyCLR_Result LPC17_Can_SetWriteBufferSize(const TinyCLR_Can_Provider* self, si

void LPC17_Can_Reset() {
for (int i = 0; i < TOTAL_CAN_CONTROLLERS; i++) {
if (canController[i].isOpened)
LPC17_Can_Release(canProvider[i]);
LPC17_Can_Release(canProvider[i]);

canController[i].isOpened = false;
}
Expand Down
9 changes: 5 additions & 4 deletions Targets/LPC177x_LPC178x/LPC17_DA.cpp
Expand Up @@ -94,9 +94,11 @@ TinyCLR_Result LPC17_Dac_ReleaseChannel(const TinyCLR_Dac_Provider* self, int32_
if (channel >= SIZEOF_ARRAY(g_lpc17_dac_pins))
return TinyCLR_Result::ArgumentOutOfRange;

DACR = (0 << 6); // This sets the initial starting voltage at 0
if (g_lpc17_dac_isOpened[channel]) {
DACR = (0 << 6); // This sets the initial starting voltage at 0

LPC17_Gpio_ClosePin(g_lpc17_dac_pins[channel].number);
LPC17_Gpio_ClosePin(g_lpc17_dac_pins[channel].number);
}

g_lpc17_dac_isOpened[channel] = false;

Expand Down Expand Up @@ -138,8 +140,7 @@ int32_t LPC17_Dac_GetMaxValue(const TinyCLR_Dac_Provider* self) {

void LPC17_Dac_Reset() {
for (auto ch = 0; ch < LPC17_Dac_GetChannelCount(&dacProvider); ch++) {
if (g_lpc17_dac_isOpened[ch])
LPC17_Dac_ReleaseChannel(&dacProvider, ch);
LPC17_Dac_ReleaseChannel(&dacProvider, ch);

g_lpc17_dac_isOpened[ch] = false;
}
Expand Down

0 comments on commit 2b27297

Please sign in to comment.