Начинаю мучать в связке STM8S105.
Если кто с ними имел дело - прошу отписаться.
Потихоньку буду в первом посте выкладывать код
- Код: Выделить всё • Развернуть
- /*
 ********************************************************************************
 ** File : nrf24l01+.h
 ** Date : 07.07.2015
 ** Autors :
 ** Notes :
 ********************************************************************************
 */
 #ifndef __NRF24L01_PLUS_H__
 #define __NRF24L01_PLUS_H__
 #include "stm8s.h"
 #include "gpio.h"
 #include "spi.h"
 #include "delay.h"
 #include "led.h"
 /*
 Распиновка модуля
 1 - 3.3V
 2 - gnd
 3 - CE
 4 - CSn
 5 - SCK
 6 - MOSI
 7 - MISO
 8 - IRQ
 CN2->4 PC3 3
 CN2->5 PC4 4
 CN2->6 PC5->SPI_CLK 5
 CN2->7 gnd 2
 CN2->8 +3.3V 1
 CN2->9 PC6->SPI_MOSI 6
 CN2->10 PC7->SPI_MISO 7
 CN4->8 PD3 8
 */
 /*
 ********************************************************************************
 ** Definitions
 ********************************************************************************
 */
 /* PC4 - Chip Select */
 #define CSN_GPIO GPIOC
 #define CSN_PIN GPIO_PIN_4
 #define RF_CSN_PIN CSN_GPIO, CSN_PIN
 /* PC3 - CE */
 #define CE_GPIO GPIOC
 #define CE_PIN GPIO_PIN_3
 #define RF_CE_PIN CE_GPIO, CE_PIN
 /* SPI */
 /* PC5 - SPI_SCK */
 /* PC6 - SPI_MOSI */
 /* PC7 - SPI_MISO */
 /* PD3 - IRQ */
 #define IRQ_GPIO GPIOD
 #define IRQ_PIN GPIO_PIN_3
 #define RF_IRQ_PIN IRQ_GPIO, IRQ_PIN
 #define Config_Reg_Reset_Value 0x0E
 #define Max_Data_Len 32 //maximum Bytes in Payload, typ. 32
 #define Max_Adress_Len 5 //maximum Bytes in Data Adress, typ. 5
 #define Init_Delay_us 4000 //useconds wait for init chip delay, typ. 4000us
 #define Payload_send_delay 40000 //maximum delay in cycles for data send ready in case of error
 /* nRF24L01 адреса регистров */
 #define CONFIG_REG 0x00
 #define EN_AA_REG 0x01
 #define EN_RXADDR_REG 0x02
 #define SETUP_AW_REG 0x03
 #define SETUP_RETR_REG 0x04
 #define RF_CH_REG 0x05
 #define RF_SETUP_REG 0x06
 #define STATUS_REG 0x07
 #define OBSERVE_TX_REG 0x08
 #define RPD_REG 0x09
 #define RX_ADDR_P0_REG 0x0A
 #define RX_ADDR_P1_REG 0x0B
 #define RX_ADDR_P2_REG 0x0C
 #define RX_ADDR_P3_REG 0x0D
 #define RX_ADDR_P4_REG 0x0E
 #define RX_ADDR_P5_REG 0x0F
 #define TX_ADDR_REG 0x10
 #define RX_PW_P0_REG 0x11
 #define RX_PW_P1_REG 0x12
 #define RX_PW_P2_REG 0x13
 #define RX_PW_P3_REG 0x14
 #define RX_PW_P4_REG 0x15
 #define RX_PW_P5_REG 0x16
 #define FIFO_STATUS_REG 0x17
 #define DYNPD_REG 0x1C
 /* nRF24L01 команды */
 #define CMD_R_REGISTER 0x00 // читаем регистр
 #define CMD_W_REGISTER 0x20 // пишем в регистр
 #define CMD_R_RX_PAYLOAD 0x61 // считывание из буфера принятых данных
 #define CMD_W_TX_PAYLOAD 0xA0 // запись данных в буфер для отправки в космос
 #define CMD_FLUSH_TX 0xE1 //
 #define CMD_FLUSH_RX 0xE2 //
 #define CMD_NOP 0xFF // команда заглушка, ничего не делает.
 /* Биты регистра CONFIG */
 #define CONFIG_MASK_RX_DR 6 //вкл/откл прерывание от бита RX_DR в рег. STATUS. 0-вкл, 1-выкл.
 #define CONFIG_MASK_TX_DS 5 //вкл/откл прерывание от бита TX_DS в рег. STATUS. 0-вкл, 1-выкл.
 #define CONFIG_MASK_MAX_RT 4 //вкл/откл прерывание от бита MAX_RT в рег. STATUS. 0-вкл, 1-выкл.
 #define CONFIG_EN_CRC 3 //включение CRC. По умолчанию вкл. если один из битов регистра EN_AA включен.
 #define CONFIG_CRCO 2 //режим CRC. 0-1 байт, 1-2 байта.
 #define CONFIG_PWR_UP 1 //1-POWER UP, 0-POWER DOWN, по умолчанию 0.
 #define CONFIG_PRIM_RX 0 //0-режим передачи, 1-режим приема.
 /* Биты регистра STATUS */
 #define STATUS_RX_DR 6 /*прерывание: данные получены. Для сброса записать 1.*/
 #define STATUS_TX_DS 5 /*прерывание: данные переданы. Для сброса записать 1.*/
 #define STATUS_MAX_RT 4 /*прерывание: данные не переданы. Для сброса записать 1.*/
 #define STATUS_RX_P_NO2 3
 #define STATUS_RX_P_NO1 2
 #define STATUS_RX_P_NO0 1
 #define STATUS_TX_FULL0 0 /*флаг переполнения TX FIFO буфера передачи. */
 /* 1-переполнен, 0-есть еще место.*/
 /*
 ********************************************************************************
 ** Constants
 ********************************************************************************
 */
 /*
 ********************************************************************************
 ** Typedefs
 ********************************************************************************
 */
 //#pragma bitfields = reversed
 //
 //typedef struct _nrf_reg_CONFIG_content {
 // uint8_t reserved : 1;
 // uint8_t MASK_RX_DX : 1;
 //} nrf_reg_CONFIG_content;
 typedef struct _nrf_reg_ {
 uint8_t Config;
 uint8_t EN_AA;
 uint8_t En_RxAddr;
 uint8_t Setup_AW;
 uint8_t Setup_RETR;
 uint8_t RF_Chahell;
 uint8_t RF_Setup;
 uint8_t Status;
 uint8_t observe_Tx;
 uint8_t RPD;
 uint8_t Rx_Addr_Pipe0[5];
 uint8_t Rx_Addr_Pipe1[5];
 uint8_t Rx_Addr_Pipe2[5];
 uint8_t Rx_Addr_Pipe3[5];
 uint8_t Rx_Addr_Pipe4[5];
 uint8_t Rx_Addr_Pipe5[5];
 uint8_t Tx_Addr[5];
 uint8_t Rx_Pw_Pipe0;
 uint8_t Rx_Pw_Pipe1;
 uint8_t Rx_Pw_Pipe2;
 uint8_t Rx_Pw_Pipe3;
 uint8_t Rx_Pw_Pipe4;
 uint8_t Rx_Pw_Pipe5;
 uint8_t FIFO_Status;
 } _nrf_reg;
 /*
 ********************************************************************************
 ** Variables
 ********************************************************************************
 */
 extern _nrf_reg nrf_reg;
 /*
 ********************************************************************************
 ** Function prototypes
 ********************************************************************************
 */
 void nrf24l01_SendCommand ( uint8_t cmd );
 void nrf24l01_WriteRegister ( uint8_t address, uint8_t *value, uint8_t length );
 uint8_t nrf24l01_ReadRegister ( uint8_t address );
 void nrf24l01_SendAddress ( uint8_t address, uint8_t cmd[Max_Adress_Len] );
 void nrf24l01_Init ( void );
 void nrf24l01_Setup ( void );
 void nrf24l01_ReadAllRegister ( void );
 void nrf24l01_WriteAllRegister ( void );
 void nrf24l01_prx ( void );
 void nrf24l01_ptx ( void );
 void nrf24l01_standby ( void );
 void nrf24l01_PowerDown ( void );
 void nrf24l01_SetTxLength ( uint8_t l );
 void nrf24l01_ClearInterrupts ( void );
 void RF_SendByte ( uint8_t data );
 uint8_t RF_ReceiveByte ( void );
 void nrf24l01_FirstStart ( void );
 #endif
- Код: Выделить всё • Развернуть
- /*
 ********************************************************************************
 ** File : nrf24l01+.c
 ** Date : 07.07.2015
 ** Autors :
 ** Notes :
 ********************************************************************************
 */
 #include "nrf24l01+.h"
 /*$PAGE$*/
 /*
 ********************************************************************************
 ** Descriptions :
 ** Parameter :
 ** Return :
 ** Notes :
 ********************************************************************************
 */
 void nrf24l01_SendCommand ( uint8_t cmd )
 {
 PIN_OFF ( CSN_GPIO, CSN_PIN ); // PC.4 = 0
 
 while ( SPI_GetFlagStatus ( SPI_FLAG_TXE ) == RESET );
 SPI_SendData ( cmd );
 while ( SPI_GetFlagStatus ( SPI_FLAG_BSY ) == SET );
 while ( SPI_GetFlagStatus ( SPI_FLAG_RXNE ) == RESET );
 SPI_ReceiveData ();
 
 PIN_ON ( CSN_GPIO, CSN_PIN ); // PC.4 = 1
 }
 /*$PAGE$*/
 /*
 ********************************************************************************
 ** Descriptions :
 ** Parameter :
 ** Return :
 ** Notes :
 ********************************************************************************
 */
 void nrf24l01_WriteRegister ( uint8_t address, uint8_t *value, uint8_t length )
 {
 uint8_t i = 0;
 
 PIN_OFF ( CSN_GPIO, CSN_PIN ); // PC.4 = 0
 
 //spi_SendByte ( address );
 //spi_SendByte ( value );
 
 //Send address and write command
 while ( SPI_GetFlagStatus ( SPI_FLAG_TXE ) == RESET );
 SPI_SendData ( CMD_W_REGISTER | address );
 while ( SPI_GetFlagStatus ( SPI_FLAG_BSY ) == SET );
 while ( SPI_GetFlagStatus ( SPI_FLAG_RXNE ) == RESET );
 SPI_ReceiveData();
 //Send data
 for ( i = 0; i < length; i++ ) {
 while ( SPI_GetFlagStatus ( SPI_FLAG_TXE ) == RESET);
 SPI_SendData ( value[ i ] );
 while ( SPI_GetFlagStatus ( SPI_FLAG_BSY ) == SET );
 while ( SPI_GetFlagStatus ( SPI_FLAG_RXNE ) == RESET );
 SPI_ReceiveData();
 }
 
 PIN_ON ( CSN_GPIO, CSN_PIN ); // PC.4 = 1
 }
 /*$PAGE$*/
 /*
 ********************************************************************************
 ** Descriptions :
 ** Parameter :
 ** Return :
 ** Notes :
 ********************************************************************************
 */
 uint8_t nrf24l01_ReadRegister ( uint8_t address )
 {
 uint8_t retVal = 0;
 
 PIN_OFF ( CSN_GPIO, CSN_PIN ); // PC.4 = 0
 
 //retVal = spi_ReadByte ( address );
 //retVal = spi_ReadByte ( CMD_NOP );
 
 //Send address and read command
 while ( SPI_GetFlagStatus ( SPI_FLAG_TXE ) == RESET );
 SPI_SendData ( CMD_R_REGISTER | address );
 while ( SPI_GetFlagStatus ( SPI_FLAG_BSY ) == SET );
 while ( SPI_GetFlagStatus ( SPI_FLAG_RXNE ) == RESET );
 SPI_ReceiveData ();
 
 //Get data
 while ( SPI_GetFlagStatus( SPI_FLAG_TXE ) == RESET );
 SPI_SendData(0x00);
 while ( SPI_GetFlagStatus( SPI_FLAG_BSY )== SET );
 while ( SPI_GetFlagStatus ( SPI_FLAG_RXNE ) == RESET );
 retVal = SPI_ReceiveData ();
 
 PIN_ON ( CSN_GPIO, CSN_PIN ); // PC.4 = 1
 return retVal;
 }



