Working with Netduino Plus 2 - clock for ENC28J60

Post here first, or if you can't find a relevant section!
jarwil
Posts: 12
Joined: Wed Nov 23, 2022 2:06 pm

Working with Netduino Plus 2 - clock for ENC28J60

Post by jarwil »

Hello!

I want to enable ethernet on Netduino Plus 2 board but I do not know how to setup output port A8 as a clock 25MHz.
The problem is because on Netduino Plus 2 board ENC28J60 does not have own oscillator. Instead of the clock signal from MCO1 was used.

Another problem is MISO connected to PB4 and SS connected to PC8 but I think I can change it in ENC28J60Network.h

Is there anyone who can help me enable clock on PA8?

Thank you in advance.
by ozcar » Thu Nov 24, 2022 1:33 am
From a quick look there does not seem to be a board definition for that, so you might have to create one yourself.

Have a look here: https://github.com/stm32duino/wiki/wiki ... 28board%29.

The board appears to have a 25MHz crystal, so you want to set up the clock config for that. There might be some decisions to make there, or maybe you can find somewhere how it was set up for .NET (something I know nothing about).

For the MCO part, I think this should do what you want (but TBH, the only time I needed to setup MCO I just wrote directly to the registers).

Code: Select all

HAL_RCC_MCOConfig(RCC_MCO1,  RCC_MCO1SOURCE_HSE,  RCC_MCODIV_1);
Go to full post
ozcar
Posts: 143
Joined: Wed Apr 29, 2020 9:07 pm
Answers: 5

Re: Working with Netduino Plus 2 - clock for ENC28J60

Post by ozcar »

From a quick look there does not seem to be a board definition for that, so you might have to create one yourself.

Have a look here: https://github.com/stm32duino/wiki/wiki ... 28board%29.

The board appears to have a 25MHz crystal, so you want to set up the clock config for that. There might be some decisions to make there, or maybe you can find somewhere how it was set up for .NET (something I know nothing about).

For the MCO part, I think this should do what you want (but TBH, the only time I needed to setup MCO I just wrote directly to the registers).

Code: Select all

HAL_RCC_MCOConfig(RCC_MCO1,  RCC_MCO1SOURCE_HSE,  RCC_MCODIV_1);
jarwil
Posts: 12
Joined: Wed Nov 23, 2022 2:06 pm

Re: Working with Netduino Plus 2 - clock for ENC28J60

Post by jarwil »

Thank you.

Probably this is what I am looking for but it's not working right now

I read that the generic boards (I am using Generic STM32F4 series > Generic F405RGTx) have internal clock enabled so probably for this reason I have noisy wave on PA8.

When I change second parameter to RCC_MCO1SOURCE_HSI I can get 16MHz square waveform.

Now I am reading how to change the internal clock to external crystal oscillator and I am trying add a new variant.
jarwil
Posts: 12
Joined: Wed Nov 23, 2022 2:06 pm

Re: Working with Netduino Plus 2 - clock for ENC28J60

Post by jarwil »

I am stuck. I don't know how to use the STM32CubeMX tool. Anyone can support me configure the external crystal for board Netduino Plus 2?
ozcar
Posts: 143
Joined: Wed Apr 29, 2020 9:07 pm
Answers: 5

Re: Working with Netduino Plus 2 - clock for ENC28J60

Post by ozcar »

I see there is at least one existing F4 board with 25MHz HSE, called "Blue F407VET mini".

Perhaps you could try with the generic F405GTx board definition, with SystemClock_Config() copied into your sketch from https://github.com/stm32duino/Arduino_C ... E_MINI.cpp

Take the WEAK off that and replace it with extern "C" as mentioned at the bottom of this page: https://github.com/stm32duino/wiki/wiki ... ock_config
jarwil
Posts: 12
Joined: Wed Nov 23, 2022 2:06 pm

Re: Working with Netduino Plus 2 - clock for ENC28J60

Post by jarwil »

Taa daaaaa!!!
ping.png
ping.png (6.22 KiB) Viewed 1111 times
Thank you ozcar!
At PA8 now I have beautiful sinus 25MHz.

Success is but my satisfaction is about 95% :D

At this time I had to change MISO to PB4 and CS to PC8

I have tried to do it like this in my sketch:

Code: Select all

SPI = SPIClass(PA7, PB4, PA5, PC8);
SPI.begin(PC8);
and also like this:

Code: Select all

SPI.setMISO(PB4);
SPI.setMOSI(PA7);
SPI.setSCLK(PA5);
SPI.setSSEL(PC8);
without success :?

In the end, I did it in a very inelegant way.
I changed the MISO directly in SPI library (SPI.cpp file)

Code: Select all

SPIClass::SPIClass() : _CSPinConfig(NO_CONFIG)
{
  _spi.pin_miso = digitalPinToPinName(PB4); //MISO);
  _spi.pin_mosi = digitalPinToPinName(MOSI);
  _spi.pin_sclk = digitalPinToPinName(SCK);
  _spi.pin_ssel = NC;
}
and PC8 I setup in my *.ino file

Code: Select all

Ethernet.init(PC8);
It's not clear to me why the above two methods I tried don't work (with SPI=... and SPI.set...)
jarwil
Posts: 12
Joined: Wed Nov 23, 2022 2:06 pm

Re: Working with Netduino Plus 2 - clock for ENC28J60

Post by jarwil »

I read the SPI API manual and now everything is clear. https://github.com/stm32duino/wiki/wiki ... unctions-1 I shouldn't set SSEL pin.

To sum up, to run ethernet on Netduino Plus 2 it is necessary:
1. set the clock

Code: Select all

extern "C" void SystemClock_Config(void)
{

	RCC_OscInitTypeDef RCC_OscInitStruct = {};
	RCC_ClkInitTypeDef RCC_ClkInitStruct = {};

	/**Configure the main internal regulator output voltage
	*/
	__HAL_RCC_PWR_CLK_ENABLE();

	__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

	/**Initializes the CPU, AHB and APB busses clocks
	*/
	RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
	RCC_OscInitStruct.HSEState = RCC_HSE_ON;
	RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
	RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
	RCC_OscInitStruct.PLL.PLLM = 25;
	RCC_OscInitStruct.PLL.PLLN = 336;
	RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
	RCC_OscInitStruct.PLL.PLLQ = 7;
	if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
		Error_Handler();
	}

	/**Initializes the CPU, AHB and APB busses clocks
	*/
	RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
		| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
	RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
	RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
	RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
	RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

	if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) {
		Error_Handler();
	}

	/* Ensure CCM RAM clock is enabled */
	__HAL_RCC_CCMDATARAMEN_CLK_ENABLE();

}
2. set PA8 as clock for enc28J60
3. set the pins for SPI

Code: Select all

void setup()
{
	//clock setup
	//SystemClock_Config(); (extern)
	
	//PA8 as clock for enc28J60
	HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, RCC_MCODIV_1);
	
	//PB4 as MISO
	SPI.setMISO(PB4);
	
	//PC8 as chip sellect
	Ethernet.init(PC8);
	
	//start ethernet
	Ethernet.begin(mac, Eth_ModuleIP);
}
Last edited by jarwil on Sat Nov 26, 2022 10:25 pm, edited 1 time in total.
ozcar
Posts: 143
Joined: Wed Apr 29, 2020 9:07 pm
Answers: 5

Re: Working with Netduino Plus 2 - clock for ENC28J60

Post by ozcar »

Good that you got it working.

I would have thought that your SystemClock_Config() would get used anyway without you having to call it from setup() though.
jarwil
Posts: 12
Joined: Wed Nov 23, 2022 2:06 pm

Re: Working with Netduino Plus 2 - clock for ENC28J60

Post by jarwil »

I have never use the extern declaration.
Thank you - now I know how it works.
jarwil
Posts: 12
Joined: Wed Nov 23, 2022 2:06 pm

Re: Working with Netduino Plus 2 - clock for ENC28J60

Post by jarwil »

I'll have to dig into the clock setup.
Using the millis() function to count the time, it turns out that 1s is actually 327ms.
As a result, libraries that use the millis() and micros() functions (for sensor DHT22 and for RC5) do not work.
Above proportion similar to 25000000 and 8000000
Should I change 8000000 to 25000000 in any other file? Something tells me that somewhere I should change HSE_VALUE.
Post Reply

Return to “General discussion”