※The complete code is available in here.
In my previous post, I used nRF24LE1, which is an 8051 microcontroller(MCU) integrated nRF24L01 function, to operate Enhanced Shockburst™ (ESB) transceiving. If your MCU would tackle only some simple tack with wireless function, nRF24LE1 is a good selection. However, in some case, nRF24LE1 may be not a choice. For example, nRF24LE1 computing power is limited and does not support CAN bus, pins of nRF24LE1 would be conflicted for your application, multi-channels ADC/I2C/SPI are required, or just existing product must not be totally reforged..etc. Hence, In here, I would to show how to use STM8L to manipulate nRF24L01.
The STM8L I use here is STM8L101F3P3 features its low price (STM8L10X series is lowest product line of STM8 series.). Because the STM101F1 flash size is not adequate to containing the firmware operating nRF24L01, I use the chip postfix being -F3 rather than -F1.
To detail explain how to deal with the porting, I start from my another post code of folder nRF24LE1, which has been completely implemented ESB send and receive functions.
零.
Download ST Visual develop IDE, STM8 Cosmic compiler, and STM8L10x standard peripheral library. you need to provide your email to STMicroelectronics.
Set ST Visual develop using the cosmic compiler : Tools → Option :
一. Download my the code for nRF24LE1 :
Download the code from here, you should extract those :
esb_app.c
esb_app.c
esb_app.h
hal\nordic_common.h
hal\nordic_common.h
hal\nrf24l01p\hal_nrf.c
hal\nrf24l01p\hal_nrf.h
hal\nrf24l01p\hal_nrf_reg.h
hal\nrf24le1\hal_nrf_hw.c
hal\nrf24le1\hal_nrf_hw.h
compiler\c51\stdin.h
And create a folder to contain those code, the folder structure is :
esb_app.c
esb_app.h
nRF24L01_hal\nordic_common.h
nRF24L01_hal\nordic_common.h
nRF24L01_hal\nrf24l01p\hal_nrf.c
nRF24L01_hal\nrf24l01p\hal_nrf.h
nRF24L01_hal\nrf24l01p\hal_nrf_reg.h
nRF24L01_hal\stm8L10x\stdin.h
nRF24L01_hal\stm8L10x\hal_nrf_hw.c
nRF24L01_hal\stm8L10x\hal_nrf_hw.h
二 . Modify the necessary code:
The firmware architecture diagram is as below, The blocks framed purple is the code you need to modify/implement.
The firmware architecture diagram is as below, The blocks framed purple is the code you need to modify/implement.
esb_app.h :
export esb_irq function :
: void esb_receiving_event_has_been_handled(void); /*nRF24L01 IRQ*/ #if !defined(NRF24LE1_H_) && !defined(NRF24LU1P_H) void esb_irq(void); #endif
esb_app.c :
comment out the line to set register RFCKEN and RF, and remove the keyword "static" of function esb_irq.
comment out the line to set register RFCKEN and RF, and remove the keyword "static" of function esb_irq.
void esb_init(void) : #if defined(NRF24LE1_H_) || defined(NRF24LU1P_H) RFCKEN = 1; //enable RF timer #endif : #if defined(NRF24LE1_H_) || defined(NRF24LU1P_H) RF = 1; //enable rf interrupt #endif : }/*enhanced_shockburst_init*/ : /*static */ void esb_irq(void) { :
As the diagram shows, the hal_nrf,c and hal_nrf.h would be intact, but hal_nrf_hw.c and hal_nrf_hw.h need to be completely implemented. Is is because the SPI operations are high MCU-dependency, and pin definition is high board-dependency.
The schematic of my board is :
keypoints:
PC0 pin is for nRF24L01 IRQ.
PB4 pin is for nRF24L01 CSN.
PB3 pin is for nRF24L01 CE.
In this manner, hal_nrf_hw.h as :
Those lines to define data, pdata and xdata are for avoiding compilation error.
hal_nrf_hw.c:
三. Create the project.
Create a workspace of ST Visual Develop with what the chip you use, and create a project inside it. the project folder in this structure:
stm8_interrupt_vector.c and stm8l10x_conf.h are auto-generated files. The STM8L10x_StdPeriph_Driver folder contains the driver files of of STM8L10x standard peripheral library, in directory STM8L10x_StdPeriph_Lib\Libraries\STM8L10x_StdPeriph_Driver, for inc and src folder respectively.
The next stuff I need to do, is to complete the nRF24L01 initialization. Pay attention :: it is not ESB initializtion function, which has been implemented in esb_app.c. The nRF24L01 initialization is for initializing the interface resources used in communicated with nRF24L01.
There are two part: initialize the Serial Peripheral Interface Bus(SPI), and the General-purpose input/output(GPIO).
The pins:
PB7 pin is SPI_MISO, input, high = 1.
PB6 pin is SPI_MOSI, output, high = 1.
PB5 pin is SPI_SCK, output, high = 1.
PB4 pin is for nRF24L01 CSN. output, set = 1.
PB3 pin is for nRF24L01 CE, output, set =1.
PC0 pin is for nRF24L01 IRQ, input, external interrupt, active-low.
According to above definition, the the data sheet of nRF24L01 for SPI timing explanation(CPOL= low, CPHA = rising edge, more detail explanation about CPOL and CPHA you could refer to here) , the code for nRF24L01 initialization is :
The last stuff to do is to register an external interrupt function for nRF24L01's IRQ in file stm8_interrupt_vector.c.
In my board, pin PC0 is the pin for nRF24L01 IRQ. According to the STM8L101xx datasheet( in section "interrupt vector mapping", p.32), the PX0 interrupt number is 8.
Therefore, add the interrupt function in stm8_interrupt_vector.c :
The interrupt function definition is:
This very clear that once the interrupt is triggered, function esb_irq, which is as the same as nRF24LE1's, will be called to deal with the callback events of send/receive/max re-try.
The schematic of my board is :
keypoints:
PC0 pin is for nRF24L01 IRQ.
PB4 pin is for nRF24L01 CSN.
PB3 pin is for nRF24L01 CE.
In this manner, hal_nrf_hw.h as :
#ifndef HAL_NRF_STM8L101XX_H__ #define HAL_NRF_STM8L101XX_H__ #include "STM8l10x_conf.h" #define data #define pdata #define xdata /** Macro that set radio's CSN line LOW. * */ #define CSN_LOW() do { GPIO_ResetBits( GPIOB, GPIO_Pin_4);} while(false) /** Macro that set radio's CSN line HIGH. * */ #define CSN_HIGH() do { GPIO_SetBits( GPIOB, GPIO_Pin_4);} while(false) /** Macro that set radio's CE line LOW. * */ #define CE_LOW() do {GPIO_ResetBits(GPIOB, GPIO_Pin_3);} while(false) /** Macro that set radio's CE line HIGH. * */ #define CE_HIGH() do {GPIO_SetBits(GPIOB, GPIO_Pin_3);} while(false) /** Macro for writing the radio SPI data register. * */ #define HAL_NRF_HW_SPI_WRITE(d) do{\ while (SPI_GetFlagStatus(SPI_FLAG_TXE) == RESET);\ SPI_SendData(d);\ } while(false) /** Macro for reading the radio SPI data register. * */ #define HAL_NRF_HW_SPI_READ() SPI_ReceiveData() /** Macro specifyng the radio SPI busy flag. * */ #define HAL_NRF_HW_SPI_BUSY (RESET == SPI_GetFlagStatus(SPI_FLAG_RXNE)) /** * Pulses the CE to nRF24L01 for at least 10 us */ #define CE_PULSE() do { \ uint8_t count; \ count = 20; \ CE_HIGH(); \ while(count--){} \ CE_LOW(); \ } while(false) #endif // HAL_NRF_STM8L101XX_H__ /** @} */
Those lines to define data, pdata and xdata are for avoiding compilation error.
hal_nrf_hw.c:
#include <stdint.h> #include "hal_nrf.h" #include "STM8l10x_conf.h" uint8_t hal_nrf_rw(uint8_t value) { while (RESET == SPI_GetFlagStatus(SPI_FLAG_TXE)); SPI_SendData(value); //wait for receiving a byte while(RESET == SPI_GetFlagStatus(SPI_FLAG_RXNE)); return SPI_ReceiveData(); }
三. Create the project.
Create a workspace of ST Visual Develop with what the chip you use, and create a project inside it. the project folder in this structure:
stm8_interrupt_vector.c and stm8l10x_conf.h are auto-generated files. The STM8L10x_StdPeriph_Driver folder contains the driver files of of STM8L10x standard peripheral library, in directory STM8L10x_StdPeriph_Lib\Libraries\STM8L10x_StdPeriph_Driver, for inc and src folder respectively.
The next stuff I need to do, is to complete the nRF24L01 initialization. Pay attention :: it is not ESB initializtion function, which has been implemented in esb_app.c. The nRF24L01 initialization is for initializing the interface resources used in communicated with nRF24L01.
There are two part: initialize the Serial Peripheral Interface Bus(SPI), and the General-purpose input/output(GPIO).
The pins:
PB7 pin is SPI_MISO, input, high = 1.
PB6 pin is SPI_MOSI, output, high = 1.
PB5 pin is SPI_SCK, output, high = 1.
PB4 pin is for nRF24L01 CSN. output, set = 1.
PB3 pin is for nRF24L01 CE, output, set =1.
PC0 pin is for nRF24L01 IRQ, input, external interrupt, active-low.
According to above definition, the the data sheet of nRF24L01 for SPI timing explanation(CPOL= low, CPHA = rising edge, more detail explanation about CPOL and CPHA you could refer to here) , the code for nRF24L01 initialization is :
void spi_init(void) { //Config the GPIOs for SPI bus /* GPIOB::GPIO_Pin_7 -> SPI_MISO GPIOB::GPIO_Pin_6 -> SPI_MOSI GPIOB::GPIO_Pin_5 -> SPI_SCK */ GPIO_Init( GPIOB, GPIO_Pin_7, GPIO_Mode_In_PU_No_IT); GPIO_Init( GPIOB, GPIO_Pin_5 | GPIO_Pin_6, GPIO_Mode_Out_PP_High_Slow); SPI_DeInit(); //enable clock for SPI bus CLK_PeripheralClockConfig(CLK_Peripheral_SPI, ENABLE); /*SPI BAUDRATE should not be over 5MHz*/ #define SPI_BAUDRATEPRESCALER (SPI_BaudRatePrescaler_4) //Set the priority of the SPI SPI_Init( SPI_FirstBit_MSB, SPI_BAUDRATEPRESCALER, SPI_Mode_Master, SPI_CPOL_Low, SPI_CPHA_1Edge, SPI_Direction_2Lines_FullDuplex, SPI_NSS_Soft); //Enable SPi SPI_Cmd(ENABLE); }/*spi_init*/ void nrf24l01_init(void) { spi_init(); /*GPIOB::GPIO_Pin_4 -> SPI::NSS -> NRF24l01::CSN*/ GPIO_Init( GPIOB, GPIO_Pin_4, GPIO_Mode_Out_PP_High_Fast); /*GPIOB::GPIO_Pin_3 -> NRF24l01::CE*/ GPIO_Init( GPIOB, GPIO_Pin_3, GPIO_Mode_Out_PP_High_Fast); /*GPIOC::GPIO_Pin_0 -> NRF24l01::IRQ*/ GPIO_Init(GPIOC, GPIO_Pin_0, GPIO_Mode_In_PU_IT); EXTI_SetPinSensitivity(GPIO_Pin_0, EXTI_Trigger_Falling_Low); EXTI_ClearITPendingBit(GPIO_Pin_0); }/*spi_init*/
The last stuff to do is to register an external interrupt function for nRF24L01's IRQ in file stm8_interrupt_vector.c.
In my board, pin PC0 is the pin for nRF24L01 IRQ. According to the STM8L101xx datasheet( in section "interrupt vector mapping", p.32), the PX0 interrupt number is 8.
Therefore, add the interrupt function in stm8_interrupt_vector.c :
struct interrupt_vector const _vectab[] = { {0x82, (interrupt_handler_t)_stext}, : : {0x82, NonHandledInterrupt}, /* irq6 */ {0x82, NonHandledInterrupt}, /* irq7 */ {0x82, external_interrupt0_handler}, /* irq8 */ {0x82, NonHandledInterrupt}, /* irq9 */
The interrupt function definition is:
@far @interrupt void external_interrupt0_handler(void) { EXTI_ClearITPendingBit(GPIO_Pin_0); esb_irq(); }/*external_interrupt0_handler*/
This very clear that once the interrupt is triggered, function esb_irq, which is as the same as nRF24LE1's, will be called to deal with the callback events of send/receive/max re-try.
It is all.
below is the uart output of this STM8L101F3PU + nRF24L01 while this device is communicating with nRF24LE (the original one, before this porting).
If I turn off the nRF24LE1, the uart output is :
It is compatible with the previous works totoally. Of course, this porting is able to communicate with my previous work on nRF5X.
沒有留言:
張貼留言