HeidiSQL_12.6_32_Portable
아래 코드는 복사 및 삭제입니다(원시 이동 또는 이름 바꾸기 대신). [Code]이 논리를 구현하려면 섹션을 사용하세요 . 지원되는 기능 과 처리할 수 있는 이벤트 에 대한 문서는 꽤 좋습니다. 설치 시작 시 대응 방법:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
procedure CurStepChanged(CurStep: TSetupStep); begin case CurStep of ssInstall: begin { will be executed just before the actual installation starts } end; ssPostInstall: begin { will be executed just after the actual installation finishes } end; end; end; |
파일이 […]
from: https://eroro.tistory.com/591 NTC의 특성 값이나 회로 부분은 변경하여 사용하여야 합니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
#define REF_RESIST 10000 #define VREF 3.3f /******************************************************************************* * Function Name : * Parameters : None * Return : None * Description : 3.3 : 4096 = x : ADC Vout= (x/(x+10000)) * 3.3 *******************************************************************************/ float Calculation_Vout(u16 a_nhADCValue, u8 a_chComp) { float Vout, Resistor; a_nhADCValue += a_chComp; Vout = (a_nhADCValue * VREF); Vout /= 4095; // DEBUGPRINT("ADC Vout %f, ", Vout); Resistor = Vout *(REF_RESIST/(VREF-Vout)); // DEBUGPRINT("Resistor %f\r\n", Resistor); return Resistor; } #define REF_LDTEMP_BETA 3477 #define REF_LDTEMP_BETA_REVERSE 0.000288 //1/REF_LDTEMP_BETA #define REF_LDTEMP_25 298.15f #define REF_LDTEMP_K 273.15f #define REF_LDTEMP_UPPERRESIS 10000 /******************************************************************************* * Function Name : * Description : * Parameters : None * Return : None 0 Degree Resistor = 10000*exp((REF_LDTEMP_BETA*((1/REF_LDTEMP_K)-(1/REF_LDTEMP_25)))); Resistor = 290776.77 Ohm *******************************************************************************/ u16 ADC_LDTemperature_Calc(u16 ADC_Batt, u8 ADC_Compensation) { float Resistor, Celsius; Resistor = Calculation_Vout(ADC_Batt, 0); Celsius = (REF_LDTEMP_BETA_REVERSE*log(Resistor/REF_LDTEMP_UPPERRESIS)); // DEBUGPRINT("Celsius %f\r\n", Celsius); Celsius += (1/REF_LDTEMP_25); // DEBUGPRINT("Celsius %f\r\n", Celsius); Celsius = pow(Celsius, -1); // DEBUGPRINT("Celsius %f\r\n", Celsius); Celsius -= 273.15f; // DEBUGPRINT("Celsius %f\r\n", Celsius); if(Celsius < 0) Celsius = 0; return (u16)Celsius; } |
from: https://eroro.tistory.com/600 PF0, PF1을 사용하기 위해 RCC_DeInit함수 사용
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
/* For PF0, PF1 */ RCC_DeInit(); GPIO_InitTypeDef GPIO_InitStruct; // AFIO_REMAP_ENABLE(AFIO_MAPR_PD01_REMAP); /* Enable GPIOF clock */ RCC_AHBPeriphClockCmd(PERI_BUTTON, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); GPIO_InitStruct.GPIO_Pin = PIN_BUTTON_0 | PIN_BUTTON_1; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;//GPIO_PuPd_UP; //GPIO_Mode_IPU; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init(PORT_BUTTON, &GPIO_InitStruct); while(1){ if (GPIO_ReadInputDataBit(PORT_BUTTON, PIN_BUTTON_0) != SET) {/* Forward */ DEBUGPRINT("BTN0\r\n"); } if (GPIO_ReadInputDataBit(PORT_BUTTON, PIN_BUTTON_1) != SET) {/* Forward */ DEBUGPRINT("BTN1\r\n"); } GPIO_ToggleBit(PORT_LED, PIN_LED_0); GPIO_ToggleBit(PORT_LED, PIN_LED_1); Delay(100); } |
from: https://eroro.tistory.com/601 Remote 16-bit I/O expander for I2C-bus 테스트 프로그램 PIN 7에서 PIN 10번으로 넘어가는 부분을 PIN 8번으로 작성하였습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
/************************** (C) COPYRIGHT 2023 ***************** * File Name : * Author : * Version : Ver 0.1 * Date : 2023/03/15 * Description : * * Revision History * Date Version Name Description 2023/03/15 0.1 Oanny Proto. *******************************************************************************/ #ifndef __USR_I2C_H #define __USR_I2C_H #include "stm32f10x.h" #include "usr_system.h" /* ADDR : 0100 A2 A1 A0 RW */ #define PCF8575_ADDRESS 0x40 /* DATA DIR => 0100 A2 A1 A0 RW (ACK) P07 P06 P05 P04 P03 P02 P01 P00 (ACK) P17 P16 P15 P14 P13 P12 P11 P00 (ACK) */ #define I2C_Speed 100000 #define I2C_1 1 #define I2C_2 2 /* BASE : 0100 A2 A1 A0 RW (ACK) P07 P06 P05 P04 P03 P02 P01 P00 (ACK) P17 P16 P15 P14 P13 P12 P11 P00 (ACK) READ : 0100 A2 A1 A0 0 (ACK) P07 P06 P05 P04 P03 P02 P01 P00 (ACK) P17 P16 P15 P14 P13 P12 P11 P00 (ACK) WRITE : 0100 A2 A1 A0 1 (ACK) P07 P06 P05 P04 P03 P02 P01 P00 (ACK) P17 P16 P15 P14 P13 P12 P11 P00 (ACK) */ #define PCF8575_Pin_0 ((uint8_t)0x0001) /*!< Pin 0 selected */ #define PCF8575_Pin_1 ((uint8_t)0x0002) /*!< Pin 1 selected */ #define PCF8575_Pin_2 ((uint8_t)0x0004) /*!< Pin 2 selected */ #define PCF8575_Pin_3 ((uint8_t)0x0008) /*!< Pin 3 selected */ #define PCF8575_Pin_4 ((uint8_t)0x0010) /*!< Pin 4 selected */ #define PCF8575_Pin_5 ((uint8_t)0x0020) /*!< Pin 5 selected */ #define PCF8575_Pin_6 ((uint8_t)0x0040) /*!< Pin 6 selected */ #define PCF8575_Pin_7 ((uint8_t)0x0080) /*!< Pin 7 selected */ #define PCF8575_Pin_8 ((uint16_t)0x0100) /*!< Pin 8 selected */ #define PCF8575_Pin_9 ((uint16_t)0x0200) /*!< Pin 9 selected */ #define PCF8575_Pin_10 ((uint16_t)0x0400) /*!< Pin 10 selected */ #define PCF8575_Pin_11 ((uint16_t)0x0800) /*!< Pin 11 selected */ #define PCF8575_Pin_12 ((uint16_t)0x1000) /*!< Pin 12 selected */ #define PCF8575_Pin_13 ((uint16_t)0x2000) /*!< Pin 13 selected */ #define PCF8575_Pin_14 ((uint16_t)0x4000) /*!< Pin 14 selected */ #define PCF8575_Pin_15 ((uint16_t)0x8000) /*!< Pin 15 selected */ #define PCF8575_Pin_All ((uint16_t)0xFFFF) /*!< All pins selected */ void I2C_Config(uint8_t i2c, uint32_t speed); void I2C2_ByteWrite(uint16_t a_DeviceAddr, uint8_t* pBuffer); void I2C2_BufferRead(uint16_t a_DeviceAddr, uint8_t* pBuffer, uint16_t NumByteToRead); void PCF8575_Demo(void); #endif |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 |
/************************** (C) COPYRIGHT 2023 ***************** * File Name : * Author : * Version : Ver 0.1 * Date : 2023/03/15 * Description : * * Revision History * Date Version Name Description 2023/03/15 0.1 Oanny Proto. *******************************************************************************/ #include <stdarg.h> #include <stdio.h> #include <string.h> #include "usr_i2c.h" #include "usr_uart.h" /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro ------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ /* Private function prototypes ------------------------------------------------*/ /* Private functions ---------------------------------------------------------*/ #if 0 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus; /******************************************************************************* * Function Name : * Parameters : * Return : none * Description : *******************************************************************************/ TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength) { while(BufferLength--) { if(*pBuffer1 != *pBuffer2) { return FAILED; } pBuffer1++; pBuffer2++; } return PASSED; } #endif /******************************************************************************* * Function Name : * Parameters : * Return : none * Description : - I2C2 - PB10 EN PB11 FREQ I2C_Config(I2C_1, I2C_Speed); *******************************************************************************/ void I2C_Config(uint8_t i2c, uint32_t speed) { GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_InitStructure; if(i2c == I2C_1){ /* I2C Periph clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); /* GPIO Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); /* Configure I2C1 pins: SCL and SDA */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(GPIOB, &GPIO_InitStructure); /* I2C configuration */ I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = PCF8575_ADDRESS; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = speed; /* I2C Peripheral Enable */ I2C_Cmd(I2C1, ENABLE); /* Apply I2C configuration after enabling it */ I2C_Init(I2C1, &I2C_InitStructure); } else if(i2c == I2C_2){ /* I2C Periph clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE); /* GPIO Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); /* Configure I2C2 pins: SCL and SDA */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(GPIOB, &GPIO_InitStructure); /* I2C configuration */ I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = PCF8575_ADDRESS; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = speed; /* I2C Peripheral Enable */ I2C_Cmd(I2C2, ENABLE); /* Apply I2C configuration after enabling it */ I2C_Init(I2C2, &I2C_InitStructure); } } #define I2C_TIMEOUT 0x100000 /******************************************************************************* * Function Name : * Parameters : * Return : none * Description : *******************************************************************************/ void I2C2_ByteWrite(uint16_t a_DeviceAddr, uint8_t* pBuffer) { uint32_t I2C_Timeout = I2C_TIMEOUT; /* Send STRAT condition */ I2C_GenerateSTART(I2C2, ENABLE); /* Test on EV5 and clear it */ while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT)){ I2C_Timeout--; if(I2C_Timeout < 1) break; } /* Send EEPROM address for write */ I2C_Send7bitAddress(I2C2, a_DeviceAddr, I2C_Direction_Transmitter); I2C_Timeout = I2C_TIMEOUT; /* Test on EV6 and clear it */ while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)){ I2C_Timeout--; if(I2C_Timeout < 1) break; } /* Send the byte to be written */ I2C_SendData(I2C2, pBuffer[0]); I2C_Timeout = I2C_TIMEOUT; /* Test on EV8 and clear it */ while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED)){ I2C_Timeout--; if(I2C_Timeout < 1) break; } /* Send the byte to be written */ I2C_SendData(I2C2, pBuffer[1]); I2C_Timeout = I2C_TIMEOUT; /* Test on EV8 and clear it */ while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED)){ I2C_Timeout--; if(I2C_Timeout < 1) break; } /* Send STOP condition */ I2C_GenerateSTOP(I2C2, ENABLE); } /******************************************************************************* * Function Name : * Description : * Parameters : None * Return : None *******************************************************************************/ void I2C2_ByteRead(uint16_t a_DeviceAddr, uint8_t* pBuffer, uint16_t NumByteToRead) { uint32_t I2C_Timeout = I2C_TIMEOUT; /* While the bus is busy */ while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY)){ I2C_Timeout--; if(I2C_Timeout < 1) break; } /* Send STRAT condition a second time */ I2C_GenerateSTART(I2C2, ENABLE); I2C_Timeout = I2C_TIMEOUT; /* Test on EV5 and clear it */ while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT)){ I2C_Timeout--; if(I2C_Timeout < 1) break; } /* Send EEPROM address for read */ I2C_Send7bitAddress(I2C2, a_DeviceAddr, I2C_Direction_Receiver); I2C_Timeout = I2C_TIMEOUT; /* Test on EV6 and clear it */ while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)){ I2C_Timeout--; if(I2C_Timeout < 1) break; } /* While there is data to be read */ while(NumByteToRead) { if(NumByteToRead == 1){ /* Disable Acknowledgement */ I2C_AcknowledgeConfig(I2C2, DISABLE); /* Send STOP Condition */ I2C_GenerateSTOP(I2C2, ENABLE); } /* Test on EV7 and clear it */ if(I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED)){ /* Read a byte from the EEPROM */ *pBuffer = I2C_ReceiveData(I2C2); /* Point to the next location where the byte read will be saved */ pBuffer++; /* Decrement the read bytes counter */ NumByteToRead--; } } /* Enable Acknowledgement to be ready for another reception */ I2C_AcknowledgeConfig(I2C2, ENABLE); } /******************************************************************************* * Function Name : * Parameters : * Return : none * Description : *******************************************************************************/ void I2C2_HalfWordWrite(uint16_t a_DeviceAddr, uint16_t pBuffer) { uint32_t I2C_Timeout = I2C_TIMEOUT; /* Send STRAT condition */ I2C_GenerateSTART(I2C2, ENABLE); /* Test on EV5 and clear it */ while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT)){ I2C_Timeout--; if(I2C_Timeout < 1) break; } /* Send EEPROM address for write */ I2C_Send7bitAddress(I2C2, a_DeviceAddr, I2C_Direction_Transmitter); I2C_Timeout = I2C_TIMEOUT; /* Test on EV6 and clear it */ while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)){ I2C_Timeout--; if(I2C_Timeout < 1) break; } /* Send the byte to be written */ I2C_SendData(I2C2, (uint8_t)(pBuffer & 0x00FF)); // DEBUGPRINT("W1 %X\r\n", (uint8_t)(pBuffer & 0x00FF)); I2C_Timeout = I2C_TIMEOUT; /* Test on EV8 and clear it */ while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED)){ I2C_Timeout--; if(I2C_Timeout < 1) break; } /* Send the byte to be written */ I2C_SendData(I2C2, (uint8_t)((pBuffer & 0xFF00) >> 8)); // DEBUGPRINT("W2 %X\r\n", (uint8_t)((pBuffer & 0xFF00) >> 8)); I2C_Timeout = I2C_TIMEOUT; /* Test on EV8 and clear it */ while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED)){ I2C_Timeout--; if(I2C_Timeout < 1) break; } /* Send STOP condition */ I2C_GenerateSTOP(I2C2, ENABLE); } /******************************************************************************* * Function Name : * Description : * Parameters : None * Return : None *******************************************************************************/ uint16_t I2C2_HalfWordRead(uint16_t a_DeviceAddr) { uint32_t I2C_Timeout = I2C_TIMEOUT; uint8_t I2C_Buffer[2], Buf_Poi = 0, NumByteToRead; /* While the bus is busy */ while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY)){ I2C_Timeout--; if(I2C_Timeout < 1) break; } /* Send STRAT condition a second time */ I2C_GenerateSTART(I2C2, ENABLE); I2C_Timeout = I2C_TIMEOUT; /* Test on EV5 and clear it */ while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT)){ I2C_Timeout--; if(I2C_Timeout < 1) break; } /* Send EEPROM address for read */ I2C_Send7bitAddress(I2C2, a_DeviceAddr, I2C_Direction_Receiver); I2C_Timeout = I2C_TIMEOUT; /* Test on EV6 and clear it */ while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)){ I2C_Timeout--; if(I2C_Timeout < 1) break; } NumByteToRead = 2; /* While there is data to be read */ while(NumByteToRead) { if(NumByteToRead == 1){ /* Disable Acknowledgement */ I2C_AcknowledgeConfig(I2C2, DISABLE); /* Send STOP Condition */ I2C_GenerateSTOP(I2C2, ENABLE); } /* Test on EV7 and clear it */ if(I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED)){ /* Read a byte from the EEPROM */ I2C_Buffer[Buf_Poi] = I2C_ReceiveData(I2C2); /* Point to the next location where the byte read will be saved */ Buf_Poi++; /* Decrement the read bytes counter */ NumByteToRead--; } } /* Enable Acknowledgement to be ready for another reception */ I2C_AcknowledgeConfig(I2C2, ENABLE); return I2C_Buffer[0] | (I2C_Buffer[1] << 8); } /******************************************************************************* * Function Name : * Description : * Parameters : None * Return : None *******************************************************************************/ uint8_t PCF8575_ReadInputDataBit(uint16_t GPIO_Pin) { uint8_t bitstatus = 0x00; if ((I2C2_HalfWordRead(PCF8575_ADDRESS) & GPIO_Pin) != (uint32_t)Bit_RESET){ bitstatus = (uint8_t)Bit_SET; } else{ bitstatus = (uint8_t)Bit_RESET; } return bitstatus; } /******************************************************************************* * Function Name : * Description : * Parameters : None * Return : None *******************************************************************************/ void PCF8575_Demo(void) { I2C_Config(I2C_2, I2C_Speed); while(1){ #if 0 uint8_t i = 0; uint8_t I2C_Buffer[2] = {0, }; if(i == 0){ I2C_Buffer[0] = 0; I2C_Buffer[1] = 0; // DEBUGPRINT("%X, %X\r\n", I2C_Buffer[0], I2C_Buffer[1]); } else if(i == 1){ I2C_Buffer[0] = (uint8_t)GPIO_Pin_0; I2C_Buffer[1] = 0; // DEBUGPRINT("%X, %X\r\n", I2C_Buffer[0], I2C_Buffer[1]); } else if(i == 2){ I2C_Buffer[0] = (uint8_t)(GPIO_Pin_0 | GPIO_Pin_1); I2C_Buffer[1] = 0; // DEBUGPRINT("%X, %X\r\n", I2C_Buffer[0], I2C_Buffer[1]); } else if(i == 3){ I2C_Buffer[0] = (uint8_t)(GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2); I2C_Buffer[1] = 0; // DEBUGPRINT("%X, %X\r\n", I2C_Buffer[0], I2C_Buffer[1]); } else if(i == 4){ I2C_Buffer[0] = (uint8_t)(GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3); I2C_Buffer[1] = 0; // DEBUGPRINT("%X, %X\r\n", I2C_Buffer[0], I2C_Buffer[1]); } else if(i == 5){ I2C_Buffer[0] = (uint8_t)(GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4); I2C_Buffer[1] = 0; // DEBUGPRINT("%X, %X\r\n", I2C_Buffer[0], I2C_Buffer[1]); } else if(i == 6){ I2C_Buffer[0] = (uint8_t)(GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5); I2C_Buffer[1] = 0; // DEBUGPRINT("%X, %X\r\n", I2C_Buffer[0], I2C_Buffer[1]); } else if(i == 7){ I2C_Buffer[0] = (uint8_t)(GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6); I2C_Buffer[1] = 0; // DEBUGPRINT("%X, %X\r\n", I2C_Buffer[0], I2C_Buffer[1]); } else if(i == 8){ I2C_Buffer[0] = (uint8_t)(GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7); I2C_Buffer[1] = 0; // DEBUGPRINT("%X, %X\r\n", I2C_Buffer[0], I2C_Buffer[1]); } else if(i == 9){ I2C_Buffer[1] = (uint8_t)GPIO_Pin_0; I2C_Buffer[0] = 0; // DEBUGPRINT("%X, %X\r\n", I2C_Buffer[0], I2C_Buffer[1]); } else if(i == 10){ I2C_Buffer[1] = (uint8_t)(GPIO_Pin_0 | GPIO_Pin_1); I2C_Buffer[0] = 0; // DEBUGPRINT("%X, %X\r\n", I2C_Buffer[0], I2C_Buffer[1]); } else if(i == 11){ I2C_Buffer[1] = (uint8_t)(GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2); I2C_Buffer[0] = 0; // DEBUGPRINT("%X, %X\r\n", I2C_Buffer[0], I2C_Buffer[1]); } else if(i == 12){ I2C_Buffer[1] = (uint8_t)(GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3); I2C_Buffer[0] = 0; // DEBUGPRINT("%X, %X\r\n", I2C_Buffer[0], I2C_Buffer[1]); } else if(i == 13){ I2C_Buffer[1] = (uint8_t)(GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4); I2C_Buffer[0] = 0; // DEBUGPRINT("%X, %X\r\n", I2C_Buffer[0], I2C_Buffer[1]); } else if(i == 14){ I2C_Buffer[1] = (uint8_t)(GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5); I2C_Buffer[0] = 0; // DEBUGPRINT("%X, %X\r\n", I2C_Buffer[0], I2C_Buffer[1]); } else if(i == 15){ I2C_Buffer[1] = (uint8_t)(GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6); I2C_Buffer[0] = 0; // DEBUGPRINT("%X, %X\r\n", I2C_Buffer[0], I2C_Buffer[1]); } else if(i == 16){ I2C_Buffer[1] = (uint8_t)(GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7); I2C_Buffer[0] = 0; // DEBUGPRINT("%X, %X\r\n", I2C_Buffer[0], I2C_Buffer[1]); } GPIO_ToggleBit(GPIOA, GPIO_Pin_3); I2C2_ByteWrite(PCF8575_ADDRESS, I2C_Buffer); delay_ms(500); I2C_Buffer[0] = 0; I2C_Buffer[1] = 0; I2C2_ByteRead(PCF8575_ADDRESS, I2C_Buffer, 2); DEBUGPRINT("%X, %X\r\n", I2C_Buffer[0], I2C_Buffer[1]); i++; if(i > 16) i=0; #else uint16_t I2C_Buffer; I2C_Buffer = PCF8575_Pin_0 | PCF8575_Pin_8 | PCF8575_Pin_9; GPIO_ToggleBit(GPIOA, GPIO_Pin_3); I2C2_HalfWordWrite(PCF8575_ADDRESS, I2C_Buffer); I2C_Buffer = 0; DEBUGPRINT("R1 %X\r\n", I2C2_HalfWordRead(PCF8575_ADDRESS)); if(PCF8575_ReadInputDataBit(PCF8575_Pin_0) == Bit_SET){ DEBUGPRINT("P0 Set\r\n"); } if(PCF8575_ReadInputDataBit(PCF8575_Pin_1) == Bit_SET){ DEBUGPRINT("P1 Set\r\n"); } if(PCF8575_ReadInputDataBit(PCF8575_Pin_2) == Bit_SET){ DEBUGPRINT("P2 Set\r\n"); } if(PCF8575_ReadInputDataBit(PCF8575_Pin_3) == Bit_SET){ DEBUGPRINT("P3 Set\r\n"); } if(PCF8575_ReadInputDataBit(PCF8575_Pin_4) == Bit_SET){ DEBUGPRINT("P4 Set\r\n"); } if(PCF8575_ReadInputDataBit(PCF8575_Pin_5) == Bit_SET){ DEBUGPRINT("P5 Set\r\n"); } if(PCF8575_ReadInputDataBit(PCF8575_Pin_6) == Bit_SET){ DEBUGPRINT("P6 Set\r\n"); } if(PCF8575_ReadInputDataBit(PCF8575_Pin_7) == Bit_SET){ DEBUGPRINT("P7 Set\r\n"); } if(PCF8575_ReadInputDataBit(PCF8575_Pin_8) == Bit_SET){ DEBUGPRINT("P8 Set\r\n"); } if(PCF8575_ReadInputDataBit(PCF8575_Pin_9) == Bit_SET){ DEBUGPRINT("P9 Set\r\n"); } if(PCF8575_ReadInputDataBit(PCF8575_Pin_10) == Bit_SET){ DEBUGPRINT("P10 Set\r\n"); } if(PCF8575_ReadInputDataBit(PCF8575_Pin_11) == Bit_SET){ DEBUGPRINT("P11 Set\r\n"); } if(PCF8575_ReadInputDataBit(PCF8575_Pin_12) == Bit_SET){ DEBUGPRINT("P12 Set\r\n"); } if(PCF8575_ReadInputDataBit(PCF8575_Pin_13) == Bit_SET){ DEBUGPRINT("P13 Set\r\n"); } if(PCF8575_ReadInputDataBit(PCF8575_Pin_14) == Bit_SET){ DEBUGPRINT("P14 Set\r\n"); } if(PCF8575_ReadInputDataBit(PCF8575_Pin_15) == Bit_SET){ DEBUGPRINT("P15 Set\r\n"); } delay_ms(500); I2C_Buffer = 0; GPIO_ToggleBit(GPIOA, GPIO_Pin_3); I2C2_HalfWordWrite(PCF8575_ADDRESS, I2C_Buffer); I2C_Buffer = 0; // DEBUGPRINT("R2 %X\r\n", I2C2_HalfWordRead(PCF8575_ADDRESS)); delay_ms(500); #endif } } |
from: https://eroro.tistory.com/608 PWM 주파수 설정
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
/* 40kHz */ Set_US_CTRLDuty(40000, 50); /******************************************************************************* * Function Name : * Parameters : None * Return : None * Description : *******************************************************************************/ void TIM3_Init(u8 a_chOption, u32 a_nPrescaler, u32 a_nPeriod, u16 a_nDuty) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; if(a_chOption == 1){ /* TIM Disable */ TIM3->CR1 &= (uint16_t)~TIM_CR1_CEN; /* Init TIM Status */ TIM3->CNT = 0; TIM3->SR = 0; TIM3->CCR1 = 0; /* CH1 */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); if(a_nDuty == 0) GPIO_ResetBits(GPIOA, GPIO_Pin_6); else GPIO_SetBits(GPIOA, GPIO_Pin_6); /* GPIOA Configuration:TIM1 Channel 1 alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); } else{ /* TIM Disable */ TIM3->CR1 &= (uint16_t)~TIM_CR1_CEN; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); /* CH1 */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /* GPIOA Configuration:TIM1 Channel 1 alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Time base configuration */ /* TIM3 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; #if 0 /* 1us */ TIM_TimeBaseStructure.TIM_Prescaler = 72-1;//72Mhz #else TIM_TimeBaseStructure.TIM_Prescaler = a_nPrescaler-1; #endif TIM_TimeBaseStructure.TIM_Period = a_nPeriod - 1; //(72MHz/72)/a_nPeriod = x Hz TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); /* Init TIM Status */ TIM3->CNT = 0; TIM3->SR = 0; /* PWM1 Mode configuration: Channel 1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_Pulse = a_nDuty;//a_nDuty; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; #if 0 TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; #else TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; #endif TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; TIM_OC1Init(TIM3, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); TIM_ARRPreloadConfig(TIM3, ENABLE); /* TIM3 enable counter */ TIM_Cmd(TIM3, ENABLE); TIM_CtrlPWMOutputs(TIM3, ENABLE); } } #define TIMPRESCLER_1 1 #define TIMPRESCLER_2 2 #define TIMPRESCLER_10 10 #define TIMPRESCLER_20 20 #define TIMPRESCLER_100 100 #define TIMPRESCLER_200 200 #define TIMPRESCLER_1000 1000 #define TIMPRESCLER_10000 10000 #define TIMPRESCLER_20000 20000 #define TIM3CLOCK 72000000 #define MAXDUTY 100 #define MAXFREQ 50000 #define MINFREQ 0 /******************************************************************************* * Function Name : * Description : * Parameters : None * Return : None * Description : *******************************************************************************/ void Set_US_CTRLDuty(u16 Frequency, u8 a_chLD_CTRLPWM) { __IO uint16_t Prescler = 0; __IO uint32_t Period = 0; __IO uint32_t Duty = 0; if(Frequency > MAXFREQ) Frequency = MAXFREQ; if(a_chLD_CTRLPWM >= MAXDUTY) a_chLD_CTRLPWM = MAXDUTY; #if defined(PRINTLD) DEBUGPRINT("Frequency %d\r\n", Frequency); DEBUGPRINT("Pulse %d\r\n", a_chLD_CTRLPWM); #endif if(Frequency > MINFREQ){ if(Frequency >= 1400){ Prescler = TIMPRESCLER_1; Period = (TIM3CLOCK/(TIMPRESCLER_1))/Frequency; Duty = (Period * a_chLD_CTRLPWM)/MAXDUTY; } else if(Frequency < 1400 && Frequency >= 140){ Prescler = TIMPRESCLER_10; Period = (TIM3CLOCK/(TIMPRESCLER_10))/Frequency; Duty = (Period * a_chLD_CTRLPWM)/MAXDUTY; } else if(Frequency < 140 && Frequency >= 14){ Prescler = TIMPRESCLER_100; Period = (TIM3CLOCK/(TIMPRESCLER_100))/Frequency; Duty = (Period * a_chLD_CTRLPWM)/MAXDUTY; } else{ Prescler = TIMPRESCLER_10000; Period = (TIM3CLOCK/(TIMPRESCLER_10000))/Frequency; Duty = (Period * a_chLD_CTRLPWM)/MAXDUTY; } /* TIM3 */ TIM3_Init(0, (u16)Prescler, Period, Duty); } else{ TIM3_Init(1, 0, 0, a_chLD_CTRLPWM); } #if 1 DEBUGPRINT("Prescler %d, Period %d, Duty %d\r\n", Prescler, Period, Duty); #endif } |
https://eroro.tistory.com/579 SPI를 GPIO로 처리한 부분을 추가하였습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
/* MIT License Copyright (c) 2016 D. Nesvera Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef NRF24L01_H #define NRF24L01_H #ifdef __cplusplus extern "C" { #endif #include "stm32f1xx_hal.h" #include "stm32f1xx_hal_spi.h" #define MAXPAYLOADSIZE 32 /** * @brief Transmission status enumeration */ typedef enum { NRF24L01_Transmit_Status_Lost = 0x00, /*!< Message is lost, reached maximum number of retransmissions */ NRF24L01_Transmit_Status_Ok = 0x01, /*!< Message sent successfully */ NRF24L01_Transmit_Status_Sending = 0xFF /*!< Message is still sending */ } NRF24L01_Transmit_Status_t; /** * @brief Data rate enumeration */ typedef enum { NRF24L01_DataRate_2M, /*!< Data rate set to 2Mbps */ NRF24L01_DataRate_1M, /*!< Data rate set to 1Mbps */ NRF24L01_DataRate_250k /*!< Data rate set to 250kbps */ } NRF24L01_DataRate_t; /** * @brief Output power enumeration */ typedef enum { NRF24L01_OutputPower_M18dBm, /*!< Output power set to -18dBm */ NRF24L01_OutputPower_M12dBm, /*!< Output power set to -12dBm */ NRF24L01_OutputPower_M6dBm, /*!< Output power set to -6dBm */ NRF24L01_OutputPower_0dBm /*!< Output power set to 0dBm */ } NRF24L01_OutputPower_t; /** * @brief CRC encoding scheme enumeration */ typedef enum{ NRF24L01_CRC_Disable = 0, NRF24L01_CRC_8, NRF24L01_CRC_16 } NRF24L01_CrcLength_t; typedef enum{ NRF24L01_TX_Mode = 0, NRF24L01_RX_Mode } NRF24L01_TRXMode_t; /** * @brief Struct of the NRF24L01 pins */ typedef struct { GPIO_TypeDef* GPIOx; uint16_t GPIO_Pin; } PIN_t; typedef struct { PIN_t CE; PIN_t CSN; PIN_t SCK; PIN_t MOSI; PIN_t MISO; PIN_t IRQ; } NRF24L01_Pins_t; /* Gets interrupt status from device */ #define NRF24L01_GET_INTERRUPTS TM_NRF24L01_GetStatus() /* Interrupt masks */ #define NRF24L01_IRQ_DATA_READY 0x40 /*!< Data ready for receive */ #define NRF24L01_IRQ_TRAN_OK 0x20 /*!< Transmission went OK */ #define NRF24L01_IRQ_MAX_RT 0x10 /*!< Max retransmissions reached, last transmission failed */ /** * @brief Assert the CE pin */ void NRF24L01_CE_LOW(void); /** * @brief Assert the CE pin */ void NRF24L01_CE_HIGH(void); /** * @brief De-assert the CSN pin */ void NRF24L01_CSN_LOW(void); /** * @brief Assert the CSN pin */ void NRF24L01_CSN_HIGH(void); /** * @brief Assert bits [6] [5] [4] to clear this bits of STATUS register */ void NRF24L01_ClearInterrupts(void); /** * @brief Flush TX FIFO, used in TX mode */ void NRF24L01_FlushTx(void); /** * @brief Flush RX FIFO, used in RX mode. * @note Should not be executed during transmission of acknowledge, that is, acknowledge package will not be completed. */ void NRF24L01_FlushRx(void); /** * @brief Initializes NRF24L01 module * @param channel: channel you will use for communication, from 0 to 125 eg. working frequency from 2.4 to 2.525 GHz * @param payload_size: maximum data to be sent in one packet from one NRF to another. * @note Maximal payload size is 32bytes * @param hspi: pointer to a SPI_HandleTypeDef structure that contains * the configuration information for SPI module. * @retval 1 */ uint8_t NRF24L01_Init(uint8_t channel, uint8_t payload_size, SPI_HandleTypeDef *hspi, NRF24L01_Pins_t pins, uint8_t mode ); /** * @brief Sets own address. This is used for settings own id when communication with other modules * @note "Own" address of one device must be the same as "TX" address of other device (and vice versa), * if you want to get successful communication * @param *adr: Pointer to 5-bytes length array with address * @retval None */ void NRF24L01_SetRxAddress(uint8_t* adr); /** * @brief Sets address you will communicate with * @note "Own" address of one device must be the same as "TX" address of other device (and vice versa), * if you want to get successful communication * @param *adr: Pointer to 5-bytes length array with address * @retval None */ void NRF24L01_SetTxAddress(uint8_t* adr); /** * @brief Gets number of retransmissions needed in last transmission * @param None * @retval Number of retransmissions, between 0 and 15. */ uint8_t NRF24L01_GetRetransmissionsCount(void); /** * @brief Sets NRF24L01+ to TX mode * @note In this mode is NRF able to send data to another NRF module * @param None * @retval None */ void NRF24L01_PowerUpTx(void); /** * @brief Sets NRF24L01+ to RX mode * @note In this mode is NRF able to receive data from another NRF module. * This is default mode and should be used all the time, except when sending data * @param None * @retval None */ void NRF24L01_PowerUpRx(void); /** * @brief Sets NRF24L01+ to power down mode * @note In power down mode, you are not able to transmit/receive data. * You can wake up device using @ref TM_NRF24L01_PowerUpTx() or @ref TM_NRF24L01_PowerUpRx() functions * All register values available are maintained and th SPI is kept active. * @param None * @retval None */ void NRF24L01_PowerDown(void); /** * @brief Leave low-power mode * @param None * @retval None */ void NRF24L01_PowerUP(void); /** * @brief Gets transmissions status * @param None * @retval Transmission status. Return is based on @ref TM_NRF24L01_Transmit_Status_t enumeration */ NRF24L01_Transmit_Status_t NRF24L01_GetTransmissionStatus(void); /** * @brief Transmits data with NRF24L01+ to another NRF module * @param *data: Pointer to 8-bit array with data. * Maximum length of array can be the same as "payload_size" parameter on initialization * @retval This parameter can be a value of @ref NRF24L01_Transmit_Status_t enumeration */ NRF24L01_Transmit_Status_t NRF24L01_Transmit(const void *data); /** * @brief Checks if data is ready to be read from NRF24L01+ * @param None * @retval Data ready status: * - 0: No data available for receive in bufferReturns * - > 0: Data is ready to be collected */ uint8_t NRF24L01_DataReady(void); /** * @brief Gets data from NRF24L01+ * @param *data: Pointer to 8-bits array where data from NRF will be saved * @retval None */ void NRF24L01_GetData(uint8_t *data); /** * @brief Sets working channel * @note Channel value is just an offset in units MHz from 2.4GHz * For example, if you select channel 65, then operation frequency will be set to 2.465GHz. * The channel occupies a bandwidth of less than 1MHz at 250kbps and 1Mbps and a bandwidth * of less than 2MHz at 2Mbps. * @param channel: RF channel where device will operate between 0 and 125 * @retval None */ void NRF24L01_SetChannel(uint8_t channel); /** * @brief Sets RF parameters for NRF24L01+ * @param DataRate: Data rate selection for NRF module. This parameter can be a value of @ref TM_NRF24L01_DataRate_t enumeration * @param OutPwr: Output power selection for NRF module. This parameter can be a value of @ref TM_NRF24L01_OutputPower_t enumeration * @retval None */ void NRF24L01_SetRF( NRF24L01_DataRate_t DataRate, NRF24L01_OutputPower_t OutPwr); /** * @brief Sets Power Amplifier(PA) level to one of four levels. * @param OutPwr: Output power selection for NRF module. This parameter can be a value of @ref TM_NRF24L01_OutputPower_t enumeration * @retval None */ void NRF24L01_SetPaLevel( NRF24L01_OutputPower_t OutPwr ); /** * @brief Gets the current Power Amplifier(PA) level. * @retval Returns a value from the NRF24L01_OutputPower_t enum describing the current PA setting. */ NRF24L01_OutputPower_t NRF24L01_GetPaLevel( void ); /** * @brief Set the transmission data rate * @param DataRate: Data rate selection for NRF module. This parameter can be a value of @ref TM_NRF24L01_DataRate_t enumeration */ void NRF24L01_SetDataRate( NRF24L01_DataRate_t DataRate ); /** * @brief Gets the current transmission data rate. * @retval Returns a value from the NRF24L01_DataRate_t enum describing the current data rate. */ NRF24L01_DataRate_t NRF24L01_GetDataRate( void ); /** * @brief Gets NRLF+ status register value * @param None * @retval Status register from NRF */ uint8_t NRF24L01_GetStatus(void); /** * @brief Gets NRLF+ status register value * @param Length: length for 8bits, 16bits or disable CRC * @retval None */ void NRF24L01_SetCrcLength( NRF24L01_CrcLength_t length ); // void NRF24L01_getCrcLength( NRF24L01_CrcLength_t length ); /** * @brief Test whether there are bytes available to be read * @note Use this version to discover on which pipe the message arrived. * @retval true(0x01) if there is a payload available, false(0x00) if there is no payload */ uint8_t NRF24L01_Available( void ); /** * @brief Enable or disable auto-acknowledgment function * @note This is enabled by default, so it's only needed if you want to turn it off for some reason. * @param pipe: which pipeline you want to modify - 0xFF disable all pipes * @param State: 0x00 = disable 0x01 = enable * @retval None */ void NRF24L01_SetAutoAck( uint8_t pipe, uint8_t state ); /** * @brief Test whether there was a carrier on the line for the previous listening period * @note Useful to check for interference on the current channel. * @retval true(0x01) if received power levels above -64dBm, false(0x00) if received power is less than -64dBm. */ uint8_t NRF24L01_TestCarrier(void); /** * @brief Open a pipe for writing - transmission * @note Only one pipe can be open at once, but you can change the pipe you will listen to. Do not call * call this this while actively listening, call stopListening() fist. * @param Address: * @retval None */ void NRF24L01_OpenWritingPipe( uint8_t* address ); /** * @brief Open a pipe for reading - receive * @note Up to 6 pipes can be open for reading at once. Open all the * reading pipes, and then call startListening(). * @param Number: which pipe to open, 0-5 * @param Address: the 40bits address of the pipe to open * @retval None */ void NRF24L01_OpenReadingPipe( uint8_t number, uint8_t* address ); /** * @brief Start listening on the pipes opened for reading. - Go to Rx mode * @note Be sure to call openReadingPipe() first. Do not call write() while * in this mode, without first calling stopListening(). Call * isAvailable() to check for incoming traffic, and read() to get it. * @retval None */ void NRF24L01_StartListening(void); /** * @brief Stop listening for incoming messages - return to standby-I * @note Do this before calling write(). * @retval None */ void NRF24L01_StopListening(void); /** * @brief Read the payload * @note Return the last payload received * @param Data: pointer to a buffer where the data should be written * @retval None */ uint8_t NRF24L01_Read(void* data); /** * @brief Get Static payload size * @retval Number of bytes in the payload */ uint8_t NRF24L01_GetPayloadSize(void); /** * @brief Get Dynamic payload size * @retval Payload size of last received dynamic payload */ uint8_t NRF24L01_GetDynamicPayloadSize(void); /** * @brief Set the number and delay of retries upon failed submit * @param delay How long to wait between each retry, in multiples of 250us, * max is 15. 0 means 250us, 15 means 4000us. * @param count How many retries before giving up, max 15 * @retval NONE */ void NRF24L01_SetRetries(uint8_t delay, uint8_t count); #endif /* __NRF24L01_H */ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 |
/* MIT License Copyright (c) 2016 D. Nesvera Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "main.h" #include "nrf24l01c.h" #include "stm32f1xx_hal.h" // APAGAR BUG REVER #define _SSPI_ 1 uint8_t SPI_SendByte(uint8_t byte); // colocar os copyright // os dataseet do nrf link // os datasheet do stm link // based /* NRF24L01+ registers*/ #define NRF24L01_REG_CONFIG 0x00 //Configuration Register #define NRF24L01_REG_EN_AA 0x01 //Enable ‘Auto Acknowledgment’ Function #define NRF24L01_REG_EN_RXADDR 0x02 //Enabled RX Addresses #define NRF24L01_REG_SETUP_AW 0x03 //Setup of Address Widths (common for all data pipes) #define NRF24L01_REG_SETUP_RETR 0x04 //Setup of Automatic Retransmission #define NRF24L01_REG_RF_CH 0x05 //RF Channel #define NRF24L01_REG_RF_SETUP 0x06 //RF Setup Register #define NRF24L01_REG_STATUS 0x07 //Status Register #define NRF24L01_REG_OBSERVE_TX 0x08 //Transmit observe register #define NRF24L01_REG_RPD 0x09 #define NRF24L01_REG_RX_ADDR_P0 0x0A //Receive address data pipe 0. 5 Bytes maximum length. #define NRF24L01_REG_RX_ADDR_P1 0x0B //Receive address data pipe 1. 5 Bytes maximum length. #define NRF24L01_REG_RX_ADDR_P2 0x0C //Receive address data pipe 2. Only LSB #define NRF24L01_REG_RX_ADDR_P3 0x0D //Receive address data pipe 3. Only LSB #define NRF24L01_REG_RX_ADDR_P4 0x0E //Receive address data pipe 4. Only LSB #define NRF24L01_REG_RX_ADDR_P5 0x0F //Receive address data pipe 5. Only LSB #define NRF24L01_REG_TX_ADDR 0x10 //Transmit address. Used for a PTX device only #define NRF24L01_REG_RX_PW_P0 0x11 #define NRF24L01_REG_RX_PW_P1 0x12 #define NRF24L01_REG_RX_PW_P2 0x13 #define NRF24L01_REG_RX_PW_P3 0x14 #define NRF24L01_REG_RX_PW_P4 0x15 #define NRF24L01_REG_RX_PW_P5 0x16 #define NRF24L01_REG_FIFO_STATUS 0x17 //FIFO Status Register #define NRF24L01_REG_DYNPD 0x1C //Enable dynamic payload length #define NRF24L01_REG_FEATURE 0x1D /* Registers default values */ #define NRF24L01_REG_DEFAULT_VAL_CONFIG 0x08 #define NRF24L01_REG_DEFAULT_VAL_EN_AA 0x3F #define NRF24L01_REG_DEFAULT_VAL_EN_RXADDR 0x03 #define NRF24L01_REG_DEFAULT_VAL_SETUP_AW 0x03 #define NRF24L01_REG_DEFAULT_VAL_SETUP_RETR 0x03 #define NRF24L01_REG_DEFAULT_VAL_RF_CH 0x02 #define NRF24L01_REG_DEFAULT_VAL_RF_SETUP 0x0E #define NRF24L01_REG_DEFAULT_VAL_STATUS 0x0E #define NRF24L01_REG_DEFAULT_VAL_OBSERVE_TX 0x00 #define NRF24L01_REG_DEFAULT_VAL_RPD 0x00 #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P0_0 0xE7 #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P0_1 0xE7 #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P0_2 0xE7 #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P0_3 0xE7 #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P0_4 0xE7 #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P1_0 0xC2 #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P1_1 0xC2 #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P1_2 0xC2 #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P1_3 0xC2 #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P1_4 0xC2 #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P2 0xC3 #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P3 0xC4 #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P4 0xC5 #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P5 0xC6 #define NRF24L01_REG_DEFAULT_VAL_TX_ADDR_0 0xE7 #define NRF24L01_REG_DEFAULT_VAL_TX_ADDR_1 0xE7 #define NRF24L01_REG_DEFAULT_VAL_TX_ADDR_2 0xE7 #define NRF24L01_REG_DEFAULT_VAL_TX_ADDR_3 0xE7 #define NRF24L01_REG_DEFAULT_VAL_TX_ADDR_4 0xE7 #define NRF24L01_REG_DEFAULT_VAL_RX_PW_P0 0x00 #define NRF24L01_REG_DEFAULT_VAL_RX_PW_P1 0x00 #define NRF24L01_REG_DEFAULT_VAL_RX_PW_P2 0x00 #define NRF24L01_REG_DEFAULT_VAL_RX_PW_P3 0x00 #define NRF24L01_REG_DEFAULT_VAL_RX_PW_P4 0x00 #define NRF24L01_REG_DEFAULT_VAL_RX_PW_P5 0x00 #define NRF24L01_REG_DEFAULT_VAL_FIFO_STATUS 0x17 //0x11 do codigo original #define NRF24L01_REG_DEFAULT_VAL_DYNPD 0x00 #define NRF24L01_REG_DEFAULT_VAL_FEATURE 0x00 /* Commands */ #define NRF24L01_CMD_R_REGISTER 0x00 #define NRF24L01_CMD_W_REGISTER 0x20 #define NRF24L01_CMD_R_RX_PAYLOAD 0x61 #define NRF24L01_CMD_W_TX_PAYLOAD 0xA0 #define NRF24L01_CMD_FLUSH_TX 0xE1 #define NRF24L01_CMD_FLUSH_RX 0xE2 #define NRF24L01_CMD_REUSE_TX_PL 0xE3 #define NRF24L01_CMD_ACTIVATE 0x50 #define NRF24L01_CMD_R_RX_PL_WID 0x60 #define NRF24L01_CMD_W_ACK_PAYLOAD 0xA8 #define NRF24L01_CMD_W_TX_PAYLOAD_NOACK 0xB0 #define NRF24L01_CMD_NOP 0xFF #define NRF24L01_SPI_TIMEOUT 2000 #define NRF24L01_TX_TIMEOUT 500 #define NRF24L01_RX_TIMEOUT 500 /* Configuration register*/ #define NRF24L01_MASK_RX_DR 6 #define NRF24L01_MASK_TX_DS 5 #define NRF24L01_MASK_MAX_RT 4 #define NRF24L01_EN_CRC 3 #define NRF24L01_CRCO 2 #define NRF24L01_PWR_UP 1 #define NRF24L01_PRIM_RX 0 /* RX/TX control */ #define PRX 1 // primary receiver mode #define PTX 0 // primary transmitter mode /* Enable auto acknowledgment*/ #define NRF24L01_ENAA_P5 5 #define NRF24L01_ENAA_P4 4 #define NRF24L01_ENAA_P3 3 #define NRF24L01_ENAA_P2 2 #define NRF24L01_ENAA_P1 1 #define NRF24L01_ENAA_P0 0 /* Enable rx addresses */ #define NRF24L01_ERX_P5 5 #define NRF24L01_ERX_P4 4 #define NRF24L01_ERX_P3 3 #define NRF24L01_ERX_P2 2 #define NRF24L01_ERX_P1 1 #define NRF24L01_ERX_P0 0 /* Setup of address width */ #define NRF24L01_AW 0 //2 bits /* Setup of auto re-transmission*/ #define NRF24L01_ARD 4 //4 bits #define NRF24L01_ARC 0 //4 bits /* RF setup register*/ #define NRF24L01_PLL_LOCK 4 #define NRF24L01_RF_DR_LOW 5 #define NRF24L01_RF_DR_HIGH 3 #define NRF24L01_RF_DR 3 #define NRF24L01_RF_PWR 1 //2 bits /* General status register */ #define NRF24L01_RX_DR 6 #define NRF24L01_TX_DS 5 #define NRF24L01_MAX_RT 4 #define NRF24L01_RX_P_NO 1 //3 bits #define NRF24L01_TX_FULL 0 /* Transmit observe register */ #define NRF24L01_PLOS_CNT 4 //4 bits #define NRF24L01_ARC_CNT 0 //4 bits /* FIFO status*/ #define NRF24L01_TX_REUSE 6 #define NRF24L01_FIFO_FULL 5 #define NRF24L01_TX_EMPTY 4 #define NRF24L01_RX_FULL 1 #define NRF24L01_RX_EMPTY 0 //Dynamic length #define NRF24L01_DPL_P0 0 #define NRF24L01_DPL_P1 1 #define NRF24L01_DPL_P2 2 #define NRF24L01_DPL_P3 3 #define NRF24L01_DPL_P4 4 #define NRF24L01_DPL_P5 5 /* Transmitter power*/ #define NRF24L01_M18DBM 0 //-18 dBm #define NRF24L01_M12DBM 1 //-12 dBm #define NRF24L01_M6DBM 2 //-6 dBm #define NRF24L01_0DBM 3 //0 dBm /* Data rates */ #define NRF24L01_2MBPS 0 #define NRF24L01_1MBPS 1 #define NRF24L01_250KBPS 2 /* Configuration */ #define NRF24L01_CONFIG ((1 << NRF24L01_EN_CRC) | (0 << NRF24L01_CRCO)) #define NRF24L01_TRANSMISSON_OK 0 #define NRF24L01_MESSAGE_LOST 1 #define NRF24L01_CHECK_BIT(value, bit) (value & (1 << bit)) typedef struct { uint8_t PayloadSize; //Payload size uint8_t Channel; //Channel selected NRF24L01_OutputPower_t OutPwr; //Output power NRF24L01_DataRate_t DataRate; //Data rate SPI_HandleTypeDef *hspi; //hspi: pointer to a SPI_HandleTypeDef structure that contains the configuration information for SPI module. } NRF24L01_t; /* Private functions */ void NRF24L01_InitPins(void); uint8_t NRF24L01_ReadBit(uint8_t reg, uint8_t bit); uint8_t NRF24L01_ReadRegister(uint8_t reg); void NRF24L01_ReadRegisterMulti(uint8_t reg, uint8_t* data, uint8_t length); uint8_t NRF24L01_RxFifoEmpty(void); void NRF24L01_SoftwareReset(void); void NRF24L01_WriteBit(uint8_t reg, uint8_t bit, uint8_t value); void NRF24L01_WriteRegister( uint8_t reg, uint8_t value ); void NRF24L01_WriteRegisterMulti(uint8_t reg, uint8_t *data, uint8_t length); void NRF24L01_SpiInit( SPI_HandleTypeDef *hspi, NRF24L01_Pins_t pins ); /* NRF structure */ static NRF24L01_t NRF24L01_Struct; /* NRF Pins structure */ static NRF24L01_Pins_t NRF24L01_Pins; uint8_t pipe0_reading_address[5]; // last address set on pipe 0 for reading void NRF24L01_CE_LOW() { HAL_GPIO_WritePin( NRF24L01_Pins.CE.GPIOx, NRF24L01_Pins.CE.GPIO_Pin, GPIO_PIN_RESET ); } void NRF24L01_CE_HIGH(void) { HAL_GPIO_WritePin( NRF24L01_Pins.CE.GPIOx, NRF24L01_Pins.CE.GPIO_Pin, GPIO_PIN_SET ); } void NRF24L01_CSN_LOW(void) { HAL_GPIO_WritePin( NRF24L01_Pins.CSN.GPIOx, NRF24L01_Pins.CSN.GPIO_Pin, GPIO_PIN_RESET ); } void NRF24L01_CSN_HIGH(void) { HAL_GPIO_WritePin( NRF24L01_Pins.CSN.GPIOx, NRF24L01_Pins.CSN.GPIO_Pin, GPIO_PIN_SET ); } /* Clear interrupt flags */ void NRF24L01_ClearInterrupts(void) { NRF24L01_WriteRegister( NRF24L01_REG_STATUS, (1<<NRF24L01_RX_DR)|(1<<NRF24L01_TX_DS)|(1<<NRF24L01_MAX_RT) ); // setting bits 4,5,6 } /* Flush FIFOs */ void NRF24L01_FlushRx(void) { NRF24L01_CSN_LOW(); uint8_t aux = NRF24L01_CMD_FLUSH_RX; #ifdef _SSPI_ SPI_SendByte(aux); #else HAL_SPI_Transmit( NRF24L01_Struct.hspi, &aux, 1, NRF24L01_SPI_TIMEOUT ); #endif NRF24L01_CSN_HIGH(); } void NRF24L01_FlushTx(void) { NRF24L01_CSN_LOW(); uint8_t aux = NRF24L01_CMD_FLUSH_TX; #ifdef _SSPI_ SPI_SendByte(aux); #else HAL_SPI_Transmit( NRF24L01_Struct.hspi, &aux, 1, NRF24L01_SPI_TIMEOUT ); #endif NRF24L01_CSN_HIGH(); } void TM_NRF24L01_Dump_Reg(void) { uint8_t addr[5]; int i; printf("\r\nnRF14L01 Registers Value\r\n"); printf(" CONFIG : %02X\r\n", NRF24L01_ReadRegister(NRF24L01_REG_CONFIG)); printf(" EN_AA : %02X\r\n", NRF24L01_ReadRegister(NRF24L01_REG_EN_AA)); printf(" EN_RXADDR : %02X\r\n", NRF24L01_ReadRegister(NRF24L01_REG_EN_RXADDR)); printf(" SETUP_AW : %02X\r\n", NRF24L01_ReadRegister(NRF24L01_REG_SETUP_AW)); printf("SETUP_RETR : %02X\r\n", NRF24L01_ReadRegister(NRF24L01_REG_SETUP_RETR)); printf(" RF_CH : %02X\r\n", NRF24L01_ReadRegister(NRF24L01_REG_RF_CH)); printf(" RF_SETUP : %02X\r\n", NRF24L01_ReadRegister(NRF24L01_REG_RF_SETUP)); printf(" STATUS : %02X\r\n", NRF24L01_ReadRegister(NRF24L01_REG_STATUS)); printf("OBSERVE_TX : %02X\r\n", NRF24L01_ReadRegister(NRF24L01_REG_OBSERVE_TX)); printf(" RPD : %02X\r\n", NRF24L01_ReadRegister(NRF24L01_REG_RPD)); NRF24L01_ReadRegisterMulti(NRF24L01_REG_RX_ADDR_P0, addr, 5); printf("RX_ADDR_P0 : "); for (i=0; i<5; i++) printf("%02X", addr[i]); printf("\r\n"); NRF24L01_ReadRegisterMulti(NRF24L01_REG_RX_ADDR_P1, addr, 5); printf("RX_ADDR_P1 : "); for (i=0; i<5; i++) printf("%02X", addr[i]); printf("\r\n"); printf("RX_ADDR_P2 : %02X\r\n", NRF24L01_ReadRegister(NRF24L01_REG_RX_ADDR_P2)); printf("RX_ADDR_P3 : %02X\r\n", NRF24L01_ReadRegister(NRF24L01_REG_RX_ADDR_P3)); printf("RX_ADDR_P4 : %02X\r\n", NRF24L01_ReadRegister(NRF24L01_REG_RX_ADDR_P4)); printf("RX_ADDR_P5 : %02X\r\n", NRF24L01_ReadRegister(NRF24L01_REG_RX_ADDR_P5)); NRF24L01_ReadRegisterMulti(NRF24L01_REG_TX_ADDR, addr, 5); printf(" TX_ADDR : "); for (i=0; i<5; i++) printf("%02X", addr[i]); printf("\r\n"); printf(" RX_PWD_P0 : %02X\r\n", NRF24L01_ReadRegister(NRF24L01_REG_RX_PW_P0)); printf(" RX_PWD_P1 : %02X\r\n", NRF24L01_ReadRegister(NRF24L01_REG_RX_PW_P1)); printf(" RX_PWD_P2 : %02X\r\n", NRF24L01_ReadRegister(NRF24L01_REG_RX_PW_P2)); printf(" RX_PWD_P3 : %02X\r\n", NRF24L01_ReadRegister(NRF24L01_REG_RX_PW_P3)); printf(" RX_PWD_P4 : %02X\r\n", NRF24L01_ReadRegister(NRF24L01_REG_RX_PW_P4)); printf(" RX_PWD_P5 : %02X\r\n", NRF24L01_ReadRegister(NRF24L01_REG_RX_PW_P5)); printf("FIFO_STATUS: %02X\r\n", NRF24L01_ReadRegister(NRF24L01_REG_FIFO_STATUS)); printf(" DYNPD : %02X\r\n", NRF24L01_ReadRegister(NRF24L01_REG_DYNPD)); printf(" FEATURE : %02X\r\n", NRF24L01_ReadRegister(NRF24L01_REG_FEATURE)); } void nrf24l01p_prx_mode() { uint8_t new_config = NRF24L01_ReadRegister(NRF24L01_REG_CONFIG); new_config |= 1 << 0; NRF24L01_WriteRegister(NRF24L01_REG_CONFIG, new_config); } void nrf24l01p_ptx_mode() { uint8_t new_config = NRF24L01_ReadRegister(NRF24L01_REG_CONFIG); new_config &= 0xFE; NRF24L01_WriteRegister(NRF24L01_REG_CONFIG, new_config); } uint8_t NRF24L01_Init(uint8_t channel, uint8_t payload_size, SPI_HandleTypeDef *hspi, NRF24L01_Pins_t pins, uint8_t mode ) { NRF24L01_Pins = pins; for( int ind = 0 ; ind < 5 ; ind++ ) pipe0_reading_address[ind] = 0; // Initialize SPI NRF24L01_SpiInit( hspi, NRF24L01_Pins ); // We can consider the worst settle time and use to all HAL_Delay(5); /* Max payload is 32bytes */ if (payload_size > MAXPAYLOADSIZE){ payload_size = MAXPAYLOADSIZE; } /* Fill structure */ NRF24L01_Struct.Channel = !channel; /* Set channel to some different value for TM_NRF24L01_SetChannel() function */ NRF24L01_Struct.PayloadSize = payload_size; NRF24L01_Struct.OutPwr = NRF24L01_OutputPower_0dBm; NRF24L01_Struct.DataRate = NRF24L01_DataRate_250k; NRF24L01_Struct.hspi = hspi; /* Reset nRF24L01+ to power on registers values */ NRF24L01_SoftwareReset(); // if(mode == NRF24L01_TX_Mode) // nrf24l01p_ptx_mode(); // else if(mode == NRF24L01_RX_Mode) // nrf24l01p_prx_mode(); // else // ; TM_NRF24L01_Dump_Reg(); /* Set 1500uS timeouts ( minimum for 32 bytes payload in 250KBPS ) */ NRF24L01_WriteRegister( NRF24L01_REG_SETUP_RETR, ((4 << NRF24L01_ARD)|(15 << NRF24L01_ARC)) ); /* Channel select */ NRF24L01_SetChannel(channel); /* Set pipeline to the payload size */ NRF24L01_WriteRegister(NRF24L01_REG_RX_PW_P0, NRF24L01_Struct.PayloadSize); NRF24L01_WriteRegister(NRF24L01_REG_RX_PW_P1, NRF24L01_Struct.PayloadSize); NRF24L01_WriteRegister(NRF24L01_REG_RX_PW_P2, NRF24L01_Struct.PayloadSize); NRF24L01_WriteRegister(NRF24L01_REG_RX_PW_P3, NRF24L01_Struct.PayloadSize); NRF24L01_WriteRegister(NRF24L01_REG_RX_PW_P4, NRF24L01_Struct.PayloadSize); NRF24L01_WriteRegister(NRF24L01_REG_RX_PW_P5, NRF24L01_Struct.PayloadSize); /* Set RF settings (1mbps, output power) */ NRF24L01_SetRF( NRF24L01_Struct.DataRate, NRF24L01_Struct.OutPwr); /* Config register */ NRF24L01_WriteRegister(NRF24L01_REG_CONFIG, NRF24L01_CONFIG); /* Enable auto-acknowledgment for all pipes */ NRF24L01_WriteRegister(NRF24L01_REG_EN_AA, 0x3F); /* Enable RX addresses */ NRF24L01_WriteRegister(NRF24L01_REG_EN_RXADDR, 0x3F); /* Auto retransmit delay: 1000 (4x250) us and Up to 15 retransmit trials */ NRF24L01_WriteRegister(NRF24L01_REG_SETUP_RETR, 0x4F); /* Dynamic length configurations: No dynamic length */ NRF24L01_WriteRegister(NRF24L01_REG_DYNPD, (0 << NRF24L01_DPL_P0) | (0 << NRF24L01_DPL_P1) | (0 << NRF24L01_DPL_P2) | (0 << NRF24L01_DPL_P3) | (0 << NRF24L01_DPL_P4) | (0 << NRF24L01_DPL_P5)); /* Initialize CRC 2bytes */ NRF24L01_SetCrcLength( NRF24L01_CRC_16 ); /* Clear FIFOs */ NRF24L01_FlushTx(); NRF24L01_FlushRx(); /* Clear interrupts */ NRF24L01_ClearInterrupts(); /* Go to RX mode */ NRF24L01_PowerUpRx(); /* Return OK */ return 1; } /******************************************************************************* * Function Name : void nRF24L01_DELAY(uint32_t nCount) * Description : Microsecond delay * Parameters : Microsecond conunt * Return : None *******************************************************************************/ void nRF24L01_DELAY(uint32_t nCount) { #if 1 nCount *= 1; for(; nCount!=0; nCount--){ __ASM volatile ("nop"); } #else __ASM volatile ("nop"); #endif } /******************************************************************************* * Function Name : * Parameters : * Return : none * Description : CSN(SS) high = disable SPI *******************************************************************************/ void nRF24L01CS_EN(FunctionalState NewState) { if (NewState == DISABLE){ HAL_GPIO_WritePin( SPIx_CSN_GPIO_PORT, SPIx_CSN_PIN, GPIO_PIN_RESET ); } else{ HAL_GPIO_WritePin( SPIx_CSN_GPIO_PORT, SPIx_CSN_PIN, GPIO_PIN_SET ); } } /******************************************************************************* * Function Name : * Parameters : * Return : none * Description : RF EN CE low = disable TX/RX *******************************************************************************/ void nRF24L01CE_EN(FunctionalState NewState) { if (NewState == DISABLE){ HAL_GPIO_WritePin( SPIx_CE_GPIO_PORT, SPIx_CE_PIN, GPIO_PIN_RESET ); } else{ HAL_GPIO_WritePin( SPIx_CE_GPIO_PORT, SPIx_CE_PIN, GPIO_PIN_SET ); } } /******************************************************************************* * Function Name : * Parameters : None * Return : None * Description : *******************************************************************************/ void SPI_CS_HIGH(void) { HAL_GPIO_WritePin( SPIx_CSN_GPIO_PORT, SPIx_CSN_PIN, GPIO_PIN_SET ); } /******************************************************************************* * Function Name : * Parameters : None * Return : None * Description : *******************************************************************************/ void SPI_CS_LOW(void) { HAL_GPIO_WritePin( SPIx_CSN_GPIO_PORT, SPIx_CSN_PIN, GPIO_PIN_RESET ); } /******************************************************************************* * Function Name : * Parameters : None * Return : None * Description : *******************************************************************************/ uint8_t SPI_SendByte(uint8_t byte) { uint8_t RXDat = 0; uint8_t SPI_count; for (SPI_count = 0; SPI_count < 8; SPI_count++){ if((byte & 0x80) == 0x80){ HAL_GPIO_WritePin( SPIx_MOSI_GPIO_PORT, SPIx_MOSI_PIN, GPIO_PIN_SET );//MOSI HIGH } else{ HAL_GPIO_WritePin( SPIx_MOSI_GPIO_PORT, SPIx_MOSI_PIN, GPIO_PIN_RESET );//MOSI LOW } byte = byte << 1; // shift next bit into MSB nRF24L01_DELAY(2); HAL_GPIO_WritePin( SPIx_SCK_GPIO_PORT, SPIx_SCK_PIN, GPIO_PIN_SET );//SCK HIGH nRF24L01_DELAY(3); RXDat = RXDat << 1; if(HAL_GPIO_ReadPin(SPIx_MISO_GPIO_PORT, SPIx_MISO_PIN) == GPIO_PIN_SET){ RXDat |= 0x01; } HAL_GPIO_WritePin( SPIx_SCK_GPIO_PORT, SPIx_SCK_PIN, GPIO_PIN_RESET );//SCK LOW nRF24L01_DELAY(2); } // RXDat = RXDat >> 1; /* Read SPIz received data */ return RXDat; } #define DUMMY_BYTE 0xA5 void SPI_BufferRead(uint8_t* pTXBuffer, uint8_t* pRXBuffer, uint16_t NumByteToRead) { while (NumByteToRead--) /* while there is data to be read */ { /* Read a byte from the FLASH */ *pRXBuffer = SPI_SendByte(*pTXBuffer); /* Point to the next location where the byte read will be saved */ pTXBuffer++; pRXBuffer++; } } void NRF24L01_SpiInit( SPI_HandleTypeDef *hspi, NRF24L01_Pins_t pins ) { #ifdef _SSPI_ GPIO_InitTypeDef GPIO_InitStruct; SPIx_SCK_GPIO_CLK_ENABLE(); SPIx_MISO_GPIO_CLK_ENABLE(); SPIx_MOSI_GPIO_CLK_ENABLE(); SPIx_CSN_GPIO_CLK_ENABLE(); SPIx_CE_GPIO_CLK_ENABLE(); /* LCD CS */ SPI_CS_HIGH(); /* SPI SCK GPIO pin configuration */ GPIO_InitStruct.Pin = SPIx_SCK_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStruct); /* SPI MOSI GPIO pin configuration */ GPIO_InitStruct.Pin = SPIx_MOSI_PIN; HAL_GPIO_Init(SPIx_MOSI_GPIO_PORT, &GPIO_InitStruct); /* SPI MISO GPIO pin configuration */ GPIO_InitStruct.Pin = SPIx_MISO_PIN; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(SPIx_MISO_GPIO_PORT, &GPIO_InitStruct); /* SPI CSN */ GPIO_InitStruct.Pin = SPIx_CSN_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(SPIx_CSN_GPIO_PORT, &GPIO_InitStruct); /* SPI CE */ GPIO_InitStruct.Pin = SPIx_CE_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(SPIx_CE_GPIO_PORT, &GPIO_InitStruct); #else GPIO_InitTypeDef GPIO_InitStruct; SPIx_SCK_GPIO_CLK_ENABLE(); SPIx_MISO_GPIO_CLK_ENABLE(); SPIx_MOSI_GPIO_CLK_ENABLE(); SPIx_CSN_GPIO_CLK_ENABLE(); SPIx_CE_GPIO_CLK_ENABLE(); /*##-1- Configure the SPI peripheral #######################################*/ hspi->Instance = SPIx;//SPI1; hspi->Init.Mode = SPI_MODE_MASTER; hspi->Init.Direction = SPI_DIRECTION_2LINES; hspi->Init.DataSize = SPI_DATASIZE_8BIT; hspi->Init.CLKPolarity = SPI_POLARITY_LOW; hspi->Init.CLKPhase = SPI_PHASE_1EDGE; hspi->Init.NSS = SPI_NSS_SOFT;//SPI_NSS_HARD_OUTPUT; hspi->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256; hspi->Init.FirstBit = SPI_FIRSTBIT_MSB; hspi->Init.TIMode = SPI_TIMODE_DISABLE; hspi->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; hspi->Init.CRCPolynomial = 10; HAL_SPI_Init(hspi); /**SPI1 GPIO Configuration PA4 ------> SPI1_NSS PA5 ------> SPI1_SCK PA6 ------> SPI1_MISO PA7 ------> SPI1_MOSI ` */ /*##-2- Configure peripheral GPIO ##########################################*/ /* SPI SCK GPIO pin configuration */ GPIO_InitStruct.Pin = SPIx_SCK_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL;//GPIO_PULLDOWN; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStruct); /* SPI MISO GPIO pin configuration */ GPIO_InitStruct.Pin = SPIx_MISO_PIN; HAL_GPIO_Init(SPIx_MISO_GPIO_PORT, &GPIO_InitStruct); /* SPI MOSI GPIO pin configuration */ GPIO_InitStruct.Pin = SPIx_MOSI_PIN; HAL_GPIO_Init(SPIx_MOSI_GPIO_PORT, &GPIO_InitStruct); GPIO_InitStruct.Pin = SPIx_CSN_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(SPIx_CSN_GPIO_PORT, &GPIO_InitStruct); GPIO_InitStruct.Pin = SPIx_CE_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(SPIx_CE_GPIO_PORT, &GPIO_InitStruct); #endif /* CSN(SS) high = disable SPI */ HAL_GPIO_WritePin( SPIx_CSN_GPIO_PORT, SPIx_CSN_PIN, GPIO_PIN_SET ); /* CE low = disable TX/RX */ HAL_GPIO_WritePin( SPIx_CE_GPIO_PORT, SPIx_CE_PIN, GPIO_PIN_RESET ); } void NRF24L01_SoftwareReset(void) { uint8_t data[5]; NRF24L01_WriteRegister( NRF24L01_REG_CONFIG, NRF24L01_REG_DEFAULT_VAL_CONFIG); NRF24L01_WriteRegister( NRF24L01_REG_EN_AA, NRF24L01_REG_DEFAULT_VAL_EN_AA); NRF24L01_WriteRegister( NRF24L01_REG_EN_RXADDR, NRF24L01_REG_DEFAULT_VAL_EN_RXADDR); NRF24L01_WriteRegister( NRF24L01_REG_SETUP_AW, NRF24L01_REG_DEFAULT_VAL_SETUP_AW); NRF24L01_WriteRegister( NRF24L01_REG_SETUP_RETR, NRF24L01_REG_DEFAULT_VAL_SETUP_RETR); NRF24L01_WriteRegister( NRF24L01_REG_RF_CH, NRF24L01_REG_DEFAULT_VAL_RF_CH); NRF24L01_WriteRegister( NRF24L01_REG_RF_SETUP, NRF24L01_REG_DEFAULT_VAL_RF_SETUP); NRF24L01_WriteRegister( NRF24L01_REG_STATUS, NRF24L01_REG_DEFAULT_VAL_STATUS); NRF24L01_WriteRegister( NRF24L01_REG_OBSERVE_TX, NRF24L01_REG_DEFAULT_VAL_OBSERVE_TX); NRF24L01_WriteRegister( NRF24L01_REG_RPD, NRF24L01_REG_DEFAULT_VAL_RPD); //P0 data[0] = NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P0_0; data[1] = NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P0_1; data[2] = NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P0_2; data[3] = NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P0_3; data[4] = NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P0_4; NRF24L01_WriteRegisterMulti(NRF24L01_REG_RX_ADDR_P0, data, 5); //P1 data[0] = NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P1_0; data[1] = NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P1_1; data[2] = NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P1_2; data[3] = NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P1_3; data[4] = NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P1_4; NRF24L01_WriteRegisterMulti(NRF24L01_REG_RX_ADDR_P1, data, 5); NRF24L01_WriteRegister(NRF24L01_REG_RX_ADDR_P2, NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P2); NRF24L01_WriteRegister(NRF24L01_REG_RX_ADDR_P3, NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P3); NRF24L01_WriteRegister(NRF24L01_REG_RX_ADDR_P4, NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P4); NRF24L01_WriteRegister(NRF24L01_REG_RX_ADDR_P5, NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P5); //TX data[0] = NRF24L01_REG_DEFAULT_VAL_TX_ADDR_0; data[1] = NRF24L01_REG_DEFAULT_VAL_TX_ADDR_1; data[2] = NRF24L01_REG_DEFAULT_VAL_TX_ADDR_2; data[3] = NRF24L01_REG_DEFAULT_VAL_TX_ADDR_3; data[4] = NRF24L01_REG_DEFAULT_VAL_TX_ADDR_4; NRF24L01_WriteRegisterMulti(NRF24L01_REG_TX_ADDR, data, 5); NRF24L01_WriteRegister(NRF24L01_REG_RX_PW_P0, NRF24L01_REG_DEFAULT_VAL_RX_PW_P0); NRF24L01_WriteRegister(NRF24L01_REG_RX_PW_P1, NRF24L01_REG_DEFAULT_VAL_RX_PW_P1); NRF24L01_WriteRegister(NRF24L01_REG_RX_PW_P2, NRF24L01_REG_DEFAULT_VAL_RX_PW_P2); NRF24L01_WriteRegister(NRF24L01_REG_RX_PW_P3, NRF24L01_REG_DEFAULT_VAL_RX_PW_P3); NRF24L01_WriteRegister(NRF24L01_REG_RX_PW_P4, NRF24L01_REG_DEFAULT_VAL_RX_PW_P4); NRF24L01_WriteRegister(NRF24L01_REG_RX_PW_P5, NRF24L01_REG_DEFAULT_VAL_RX_PW_P5); NRF24L01_WriteRegister(NRF24L01_REG_FIFO_STATUS, NRF24L01_REG_DEFAULT_VAL_FIFO_STATUS); NRF24L01_WriteRegister(NRF24L01_REG_DYNPD, NRF24L01_REG_DEFAULT_VAL_DYNPD); NRF24L01_WriteRegister(NRF24L01_REG_FEATURE, NRF24L01_REG_DEFAULT_VAL_FEATURE); } /** * @brief Read a bit of the current register value * @param reg * @param bit * @param value */ uint8_t NRF24L01_ReadBit(uint8_t reg, uint8_t bit) { uint8_t tmp; tmp = NRF24L01_ReadRegister(reg); if (!NRF24L01_CHECK_BIT(tmp, bit)){ return 0; } return 1; } /** * @brief Change a bit of the current register value * @param reg * @param bit * @param value */ void NRF24L01_WriteBit(uint8_t reg, uint8_t bit, uint8_t value) { uint8_t tmp; /* Read register */ tmp = NRF24L01_ReadRegister(reg); /* Make operation */ if (value){ tmp |= 1 << bit; }else{ tmp &= ~(1 << bit); } /* Write back */ NRF24L01_WriteRegister(reg, tmp); } uint8_t NRF24L01_ReadRegister(uint8_t reg) { /* NRF24L01 shifts the value of status register at the first MOSI byte and at the second byte the value of the interested register */ // Initialize buffers uint8_t txBuffer[2]; txBuffer[0] = NRF24L01_CMD_R_REGISTER | reg; // REGISTER | READ_MASK(000A AAAA) txBuffer[1] = NRF24L01_CMD_NOP; // REVER - problema se não inicializar? uint8_t rxBuffer[2] = { 0x00, 0x00 }; NRF24L01_CSN_LOW(); // Transmit the txBuffer and receive data on rxBuffer at the time #ifdef _SSPI_ SPI_BufferRead(txBuffer, rxBuffer, 2); #else HAL_SPI_TransmitReceive( NRF24L01_Struct.hspi, txBuffer, rxBuffer, 2, NRF24L01_SPI_TIMEOUT ); #endif NRF24L01_CSN_HIGH(); return rxBuffer[1]; } void NRF24L01_WriteRegister( uint8_t reg, uint8_t value ) { uint8_t txBuffer[2], rxBuffer[2]; txBuffer[0] = NRF24L01_CMD_W_REGISTER | reg; // REGISTER | WRITE_MASK(001A AAAA) txBuffer[1] = value; // DATA to write NRF24L01_CSN_LOW(); #ifdef _SSPI_ SPI_BufferRead(txBuffer, rxBuffer, 2); #else HAL_SPI_Transmit( NRF24L01_Struct.hspi, txBuffer, 2, NRF24L01_SPI_TIMEOUT ); #endif NRF24L01_CSN_HIGH(); } // REVER qual byte vai primeiro (LSBYTE ou MSBYTE) void NRF24L01_ReadRegisterMulti(uint8_t reg, uint8_t* data, uint8_t length) { uint8_t txBuffer[ length + 1 ]; uint8_t rxBuffer[ length + 1 ]; // uint8_t *teste; // void *text; txBuffer[0] = NRF24L01_CMD_R_REGISTER | reg; for( uint8_t ind = 1 ; ind <= length ; ind++ ){ // (APAGAR) melhorar otimizacao, talvez de bug por nao inicializar txBuffer[ind] = NRF24L01_CMD_NOP; } NRF24L01_CSN_LOW(); #ifdef _SSPI_ SPI_BufferRead(txBuffer, rxBuffer, (length+1)); #else HAL_SPI_TransmitReceive( NRF24L01_Struct.hspi, txBuffer, rxBuffer, (length+1), NRF24L01_SPI_TIMEOUT ); #endif NRF24L01_CSN_HIGH(); for( uint8_t ind = 0 ; ind < length ; ind++ ){ data[ind] = rxBuffer[ ind + 1 ]; } } // REVER qual byte vai primeiro (LSBYTE ou MSBYTE) void NRF24L01_WriteRegisterMulti(uint8_t reg, uint8_t *data, uint8_t length) // REVER { uint8_t txBuffer[length + 1], rxBuffer[length + 1]; txBuffer[0] = NRF24L01_CMD_W_REGISTER | reg; for( uint8_t ind = 0 ; ind < length ; ind++ ){ txBuffer[ ind + 1 ] = data[ind]; } NRF24L01_CSN_LOW(); #ifdef _SSPI_ SPI_BufferRead(txBuffer, rxBuffer, (length+1)); #else HAL_SPI_Transmit( NRF24L01_Struct.hspi, txBuffer, (length+1), NRF24L01_SPI_TIMEOUT ); #endif NRF24L01_CSN_HIGH(); } void NRF24L01_SetRxAddress(uint8_t *adr) { NRF24L01_CE_LOW(); NRF24L01_WriteRegisterMulti(NRF24L01_REG_RX_ADDR_P1, adr, 5); NRF24L01_CE_HIGH(); } void NRF24L01_SetTxAddress(uint8_t *adr) { NRF24L01_WriteRegisterMulti(NRF24L01_REG_RX_ADDR_P0, adr, 5); NRF24L01_WriteRegisterMulti(NRF24L01_REG_TX_ADDR, adr, 5); } void NRF24L01_PowerDown(void) { NRF24L01_CE_LOW(); NRF24L01_WriteBit(NRF24L01_REG_CONFIG, NRF24L01_PWR_UP, RESET ); } void NRF24L01_PowerUP(void) { NRF24L01_WriteBit(NRF24L01_REG_CONFIG, NRF24L01_PWR_UP, SET ); } void NRF24L01_PowerUpRx(void) { /* Disable RX/TX mode */ NRF24L01_CE_LOW(); /* Clear RX buffer */ NRF24L01_FlushRx(); /* Clear interrupts */ NRF24L01_ClearInterrupts(); /* Setup RX mode */ NRF24L01_WriteRegister(NRF24L01_REG_CONFIG, NRF24L01_CONFIG | 1 << NRF24L01_PWR_UP | 1 << NRF24L01_PRIM_RX ); /* Start listening */ NRF24L01_CE_HIGH(); } void NRF24L01_PowerUpTx(void) { NRF24L01_ClearInterrupts(); NRF24L01_WriteRegister(NRF24L01_REG_CONFIG, (NRF24L01_ReadRegister( NRF24L01_REG_CONFIG ) & ~(1 << NRF24L01_PRIM_RX)) | NRF24L01_CONFIG | (1 << NRF24L01_PWR_UP)); // PWR_UP = 1, PRIM_RX = 0 } NRF24L01_Transmit_Status_t NRF24L01_Transmit(const void *data) { const uint8_t* aux; aux = data; uint8_t length = NRF24L01_Struct.PayloadSize; uint8_t txBuffer[MAXPAYLOADSIZE+1], rxBuffer[MAXPAYLOADSIZE+1];// uint8_t txBuffer[length+1]; /* Chip enable put to low, disable it */ NRF24L01_CE_LOW(); /* Clear TX FIFO from NRF24L01+ */ NRF24L01_FlushTx(); /* Send write payload command */ txBuffer[0] = NRF24L01_CMD_W_TX_PAYLOAD; /* Fill payload with data*/ for( int ind = 0 ; ind < length ; ind++ ){ // REVER se precisa iniciar txBuffer[ind+1] = aux[ind]; } /* Go to power up tx mode */ NRF24L01_PowerUpTx(); /* Settling time = 150us */ HAL_Delay(1); /* Send payload to nRF24L01+ */ NRF24L01_CSN_LOW(); /* Fill payload with data*/ #ifdef _SSPI_ SPI_BufferRead(txBuffer, rxBuffer, (length+1)); #else HAL_SPI_Transmit( NRF24L01_Struct.hspi, txBuffer, (length+1), NRF24L01_SPI_TIMEOUT ); #endif /* Disable SPI */ NRF24L01_CSN_HIGH(); /* Send data! */ NRF24L01_CE_HIGH(); HAL_Delay(1); NRF24L01_CE_LOW(); /* From J. Coliz At this point we could return from a non-blocking write, an then call the rest after an interrupt. Instead, we are going to block here until we get TX_DS(transmission completed and ack'd) or MAX_RT (maximum retries, transmission failed). Also, we'll timeout in case the radio is flaky and we get neither. In the end, the send should be blocking. It comes back in 60ms worst case. */ NRF24L01_Transmit_Status_t status = NRF24L01_Transmit_Status_Lost; uint32_t sent_at = HAL_GetTick(); const uint32_t timeout = NRF24L01_TX_TIMEOUT; do{ status = NRF24L01_GetTransmissionStatus(); }while( (HAL_GetTick() - sent_at < timeout) && (status != NRF24L01_Transmit_Status_Ok) ); NRF24L01_PowerDown(); NRF24L01_FlushTx(); return status; } void NRF24L01_GetData(uint8_t* data) // <<<<<-------------------------------- { /* Pull down chip select */ NRF24L01_CSN_LOW(); /* Send read payload command*/ uint8_t aux = NRF24L01_CMD_R_RX_PAYLOAD; #ifdef _SSPI_ SPI_SendByte(aux); #else HAL_SPI_Transmit( NRF24L01_Struct.hspi, &aux, 1, NRF24L01_SPI_TIMEOUT ); #endif #ifdef _SSPI_ SPI_BufferRead(data, data, NRF24L01_Struct.PayloadSize); #else /* Read payload */ HAL_SPI_TransmitReceive( NRF24L01_Struct.hspi, data, data, NRF24L01_Struct.PayloadSize, NRF24L01_SPI_TIMEOUT ); #endif /* Pull up chip select */ NRF24L01_CSN_HIGH(); /* Reset status register, clear RX_DR interrupt flag */ NRF24L01_WriteRegister(NRF24L01_REG_STATUS, (1 << NRF24L01_RX_DR)); } uint8_t NRF24L01_DataReady(void) { uint8_t status = NRF24L01_GetStatus(); if (NRF24L01_CHECK_BIT(status, NRF24L01_RX_DR)) { return 1; } return !NRF24L01_RxFifoEmpty(); } uint8_t NRF24L01_RxFifoEmpty(void) { uint8_t reg = NRF24L01_ReadRegister(NRF24L01_REG_FIFO_STATUS); return NRF24L01_CHECK_BIT(reg, NRF24L01_RX_EMPTY); } // funcao para tx_full // funcao para tx_empty // funcao para rx_full uint8_t NRF24L01_GetStatus(void) { uint8_t status; NRF24L01_CSN_LOW(); /* First received byte is always status register */ uint8_t aux = NRF24L01_CMD_NOP; #ifdef _SSPI_ // SPI_BufferRead(&aux, &status, 1); status = SPI_SendByte(aux); #else HAL_SPI_TransmitReceive( NRF24L01_Struct.hspi, &aux, &status, 1, NRF24L01_SPI_TIMEOUT ); #endif /* Pull up chip select */ NRF24L01_CSN_HIGH(); return status; } NRF24L01_Transmit_Status_t NRF24L01_GetTransmissionStatus(void) { uint8_t status = NRF24L01_GetStatus(); if (NRF24L01_CHECK_BIT(status, NRF24L01_TX_DS)) { /* Successfully sent */ return NRF24L01_Transmit_Status_Ok; } else if (NRF24L01_CHECK_BIT(status, NRF24L01_MAX_RT)) { /* Message lost */ return NRF24L01_Transmit_Status_Lost; } /* Still sending */ return NRF24L01_Transmit_Status_Sending; } uint8_t NRF24L01_GetRetransmissionsCount(void) { /* Low 4 bits */ return NRF24L01_ReadRegister(NRF24L01_REG_OBSERVE_TX) & 0x0F; } void NRF24L01_SetChannel(uint8_t channel) { if(channel <= 125 && channel != NRF24L01_Struct.Channel){ /* Store new channel setting */ NRF24L01_Struct.Channel = channel; /* Write channel */ NRF24L01_WriteRegister(NRF24L01_REG_RF_CH, channel); } } void NRF24L01_SetCrcLength( NRF24L01_CrcLength_t length ) { uint8_t config = NRF24L01_ReadRegister( NRF24L01_REG_CONFIG ); // "Switch uses RAM (evil!)" - J. Coliz if( length == NRF24L01_CRC_Disable ){ config &= ~( (1<< NRF24L01_CRCO) | (1<<NRF24L01_EN_CRC) ); // crc = 1byte }else if( length == NRF24L01_CRC_8 ){ config |= (1<<NRF24L01_EN_CRC); // crc = 2bytes }else{ config |= (1<<NRF24L01_EN_CRC)|(1<<NRF24L01_CRCO); } NRF24L01_WriteRegister( NRF24L01_REG_CONFIG, config ); } void NRF24L01_SetPaLevel( NRF24L01_OutputPower_t OutPwr ) { // setar power amplifier } NRF24L01_OutputPower_t NRF24L01_GetPaLevel( void ) { // get power amplifier } void setDataRate( NRF24L01_DataRate_t DataRate ) { } NRF24L01_DataRate_t NRF24L01_GetDataRate( void ) { } void NRF24L01_SetRF( NRF24L01_DataRate_t DataRate, NRF24L01_OutputPower_t OutPwr) { uint8_t tmp = 0; NRF24L01_Struct.DataRate = DataRate; NRF24L01_Struct.OutPwr = OutPwr; if(DataRate == NRF24L01_DataRate_2M){ tmp |= 1 << NRF24L01_RF_DR_HIGH; }else if(DataRate == NRF24L01_DataRate_250k){ tmp |= 1 << NRF24L01_RF_DR_LOW; } /* If 1Mbps, all bits set to 0 */ if(OutPwr == NRF24L01_OutputPower_0dBm){ tmp |= 3 << NRF24L01_RF_PWR; }else if(OutPwr == NRF24L01_OutputPower_M6dBm){ tmp |= 2 << NRF24L01_RF_PWR; }else if(OutPwr == NRF24L01_OutputPower_M12dBm){ tmp |= 1 << NRF24L01_RF_PWR; } /* If -18dBm, all bits set to 0 */ NRF24L01_WriteRegister(NRF24L01_REG_RF_SETUP, tmp); } void NRF24L01_SetAutoAck( uint8_t pipe, uint8_t state ) { if( pipe < 6 ){ uint8_t temp = NRF24L01_ReadRegister( NRF24L01_REG_DEFAULT_VAL_EN_AA ); if( state == 0x00 ){ temp &= ~(1 << pipe); }else if( state == 0x01 ){ temp |= (1 << pipe); } NRF24L01_WriteRegister( NRF24L01_REG_EN_AA, temp ); // 0xFF disable/enable all pipes }else if( pipe == 0xFF ){ uint8_t temp = NRF24L01_ReadRegister( NRF24L01_REG_DEFAULT_VAL_EN_AA ); if( state == 0x00 ){ temp &= ~(0x3F); }else if( state == 0x01 ){ temp |= (0x3F); } NRF24L01_WriteRegister( NRF24L01_REG_EN_AA, temp ); } } uint8_t NRF24L01_Available( void ) // REVER { uint8_t temp = NRF24L01_GetStatus(); uint8_t result = ( temp & (1<<NRF24L01_RX_DR) ); // fazer algo aqui para enviar o numero do pipe return result; } //uint8_t NRF24L01_Available( uint8_t* pipe_num ) // REVER //{ // uint8_t temp = NRF24L01_GetStatus(); // // uint8_t result = ( temp & (1<<NRF24L01_RX_DR) ); // // // return what pipe there is data to be read // if( pipe_num ){ // *pipe_num = ( temp & (7<<NRF24L01_RX_P_NO) ); // } // // NRF24L01_WriteRegister( NRF24L01_REG_STATUS, (1<< NRF24L01_RX_DR) ); // // // When there is payload on the acknowledge - Handle ack payload receipt // if ( temp & (1<<NRF24L01_TX_DS) ){ // NRF24L01_WriteRegister( NRF24L01_REG_STATUS, (1<<NRF24L01_TX_DS) ); // } // // return result; //} uint8_t NRF24L01_TestCarrier(void) { return (NRF24L01_ReadRegister(NRF24L01_REG_RPD) & 1); } void NRF24L01_OpenWritingPipe( uint8_t* address ) { NRF24L01_SetTxAddress( address ); NRF24L01_WriteRegister( NRF24L01_REG_RX_PW_P0, NRF24L01_Struct.PayloadSize ); /// APAGAR uint8_t teste[] = {0,0,0,0,0}; NRF24L01_ReadRegisterMulti( NRF24L01_REG_CONFIG, teste, 1 ); //APAGAR } void NRF24L01_StartListening(void) { // Power up and set primary rx NRF24L01_WriteRegister( NRF24L01_REG_CONFIG, (NRF24L01_ReadRegister(NRF24L01_REG_CONFIG) | (1<<NRF24L01_PWR_UP) | (1<<NRF24L01_PRIM_RX)) ); NRF24L01_ClearInterrupts(); // Restore the pipe0 adddress, if exists //if (pipe0_reading_address) //write_register(RX_ADDR_P0, reinterpret_cast<const uint8_t*>(&pipe0_reading_address), 5); NRF24L01_FlushRx(); NRF24L01_FlushTx(); // Go to RX MODE NRF24L01_CE_HIGH(); // only need 130us to settle HAL_Delay(10); } void NRF24L01_StopListening(void) { // Go to stand NRF24L01_CE_LOW(); NRF24L01_FlushRx(); NRF24L01_FlushTx(); } void NRF24L01_OpenReadingPipe( uint8_t number, uint8_t* address ) // REVER //TESTAR { /* "If this is pipe 0, cache the address. This is needed because openWritingPipe() will overwrite the pipe 0 address, so startListening() will have to restore it." - Coliz, J. */ if( number == 0 ){ for( int ind = 0 ; ind < 5 ; ind++ ) pipe0_reading_address[ind] = address[ind]; } if( number < 6 ){ // Pipes 0 and 1 need all 40bits if( number < 2 ){ NRF24L01_WriteRegisterMulti( (NRF24L01_REG_RX_ADDR_P0+number), address, 5 ); // First address + offset of pipes // Pipes 2,3,4,5 need just the LSB byte }else{ NRF24L01_WriteRegister( (NRF24L01_REG_RX_ADDR_P0+number), address[4] ); // The last byte is the LSByte } } } uint8_t NRF24L01_Read(void* data) { uint8_t txBuffer[ NRF24L01_Struct.PayloadSize + 1 ]; /* Set buffer with read command and some data to just shift */ txBuffer[0] = NRF24L01_CMD_R_RX_PAYLOAD; for( uint8_t ind = 0 ; ind < NRF24L01_Struct.PayloadSize ; ind++ ) // APAGAR - ver se array nao iniciado da erro txBuffer[ind+1] = 0x00; uint8_t *rxBuffer = (uint8_t*)data; // BUG troca de tipo de ponteiros /* Pull down chip select */ NRF24L01_CSN_LOW(); /* Read payload */ #ifdef _SSPI_ SPI_BufferRead(txBuffer, rxBuffer, (NRF24L01_Struct.PayloadSize+1)); #else HAL_SPI_TransmitReceive( NRF24L01_Struct.hspi, txBuffer, rxBuffer, (NRF24L01_Struct.PayloadSize+1), NRF24L01_SPI_TIMEOUT ); // (TROCAR) ordem caso de erro #endif // printData("rxBuffer", rxBuffer, (NRF24L01_Struct.PayloadSize+1)); // printChar("rxBuffer", rxBuffer, (NRF24L01_Struct.PayloadSize+1)); /* Pull up chip select */ NRF24L01_CSN_HIGH(); /* Reset status register, clear RX_DR interrupt flag */ NRF24L01_WriteRegister(NRF24L01_REG_STATUS, (1 << NRF24L01_RX_DR)); return (NRF24L01_ReadRegister(NRF24L01_REG_FIFO_STATUS) & (1<<NRF24L01_RX_EMPTY)); } uint8_t NRF24L01_GetPayloadSize(void) { return NRF24L01_Struct.PayloadSize; } uint8_t NRF24L01_GetDynamicPayloadSize(void) { } void NRF24L01_SetRetries(uint8_t delay, uint8_t count) { } |
from: https://eroro.tistory.com/582
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
/* Includes ------------------------------------------------------------------*/ #include "usr_mcp4251.h" /* Private variables ---------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/ /******************************************************************************* * Function Name : * Description : * Parameters : None * Return : None * Description : *******************************************************************************/ void MCP4251_configuration(void) { SPI2_CE0_HIGH(); SPI2_CE1_HIGH(); DelayMs(10); SPIx_Init(SPI_2); } /* 00h - Volatile Wiper 0 Write Data 0000 00xx xxxx xxxx Read Data 0000 11xx xxxx xxxx Increment Wiper 0000 0100 Decrement Wiper 0000 1000 01h - Volatile Wiper 1 Write Data 0001 00xx xxxx xxxx Read Data 0001 11xx xxxx xxxx Increment Wiper 0001 0100 Decrement Wiper 0001 1000 02h - Reserved 03h - Reserved 04h - Volatile TCON Write Data 0100 00xx xxxx xxxx Read Data 0100 11xx xxxx xxxx 05h - Status Register Read Data 0101 11xx xxxx xxxx 06h ~ 0Fh - Reserved #define MCP4131_WRITE (0x00 << 2) #define MCP4131_READ (0x03 << 2) #define MCP4131_WIPER_SHIFT 4 int address = chan->channel << MCP4131_WIPER_SHIFT; --Write-- data->buf[0] = address << MCP4131_WIPER_SHIFT; data->buf[0] |= MCP4131_WRITE | (val >> 8); data->buf[1] = val & 0xFF; --Read-- data->buf[0] = (address << MCP4131_WIPER_SHIFT) | MCP4131_READ; data->buf[1] = 0; */ /******************************************************************************* * Function Name : * Description : * Parameters : None * Return : None * Description : *******************************************************************************/ u8 MCP4251_Read(uint8_t ch, uint8_t Addr, uint8_t reg) { u32 SPITimeout = SPI_FLAG_TIMEOUT; if(ch == 1){ SPI2_CE0_LOW(); SPI2_CE0_HIGH(); } else{ SPI2_CE1_LOW(); SPI2_CE1_HIGH(); } return 0; } /******************************************************************************* * Function Name : * Description : * Parameters : None * Return : None * Description : *******************************************************************************/ u8 SPI_transfer(uint8_t reg) { u32 SPITimeout = SPI_FLAG_TIMEOUT; /* Loop while DR register in not emplty */ while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET){ if((SPITimeout--) == 0) return 1; } /* Send u8 through the SPI1 peripheral */ SPI_I2S_SendData(SPI2, (uint16_t)reg); return 0; } /******************************************************************************* * Function Name : * Description : * Parameters : None * Return : None * Description : *******************************************************************************/ void MCP4251_DigitalPotWiperIncrement(u8 potNum) { u8 cmdu8 = 0x00; SPI2_CE0_LOW(); //CE 0 Low if (potNum){ cmdu8 = ADDRESS_WIPER_1 | COMMAND_INCREMENT; SPI_transfer(cmdu8); } else{ cmdu8 = ADDRESS_WIPER_0 | COMMAND_INCREMENT; SPI_transfer(cmdu8); } SPI2_CE0_HIGH(); //CE 0 High } void MCP4251_DigitalPotWiperDecrement(u8 potNum) { u8 cmdu8 = 0x00; SPI2_CE0_LOW(); //CE 0 Low if (potNum){ cmdu8 = ADDRESS_WIPER_1 | COMMAND_DECREMENT; SPI_transfer(cmdu8); } else{ cmdu8 = ADDRESS_WIPER_0 | COMMAND_DECREMENT; SPI_transfer(cmdu8); } SPI2_CE0_HIGH(); //CE 0 High } void MCP4251_DigitalPotSetWiperPosition(u8 potNum, u32 value) { u8 cmdu8 = 0x00; u8 datau8 = 0x00; if (value > 255) cmdu8 |= 0x01; else datau8 = (u8)(value & 0X00FF); SPI2_CE0_LOW(); //CE 0 Low DelayUs(10); if (potNum) { cmdu8 = cmdu8 | ADDRESS_WIPER_1 | COMMAND_WRITE; SPI_transfer(cmdu8); SPI_transfer(datau8); } else { cmdu8 = cmdu8 | ADDRESS_WIPER_0 | COMMAND_WRITE; SPI_transfer(cmdu8); SPI_transfer(datau8); } DelayUs(10); DEBUGPRINT("cmdu8 %d, datau8 %d\r\n", cmdu8, datau8 ); SPI2_CE0_HIGH(); //CE 0 High } void MCP4251_DigitalPotSetWiperMin(u8 potNum) { if (potNum) MCP4251_DigitalPotSetWiperPosition(1, 0); else MCP4251_DigitalPotSetWiperPosition(0, 0); } void MCP4251_DigitalPotSetWiperMax(u8 potNum) { if (potNum) MCP4251_DigitalPotSetWiperPosition(1, 256); else MCP4251_DigitalPotSetWiperPosition(0, 256); } void MCP4251_DigitalPotSetWiperMid(u8 potNum) { if (potNum) MCP4251_DigitalPotSetWiperPosition(1, 128); else MCP4251_DigitalPotSetWiperPosition(0, 128); } uint16_t MCP4251_DigitalPotReadWiperPosition(u8 potNum) { u8 cmdu8 = 0x00; u8 hu8 = 0x00; u8 lu8 = 0x00; SPI2_CE0_LOW(); //CE 0 Low if (potNum) { cmdu8 = ADDRESS_WIPER_1 | COMMAND_READ; hu8 = SPI_transfer(cmdu8); lu8 = SPI_transfer(DUMMY_DATA); } else { cmdu8 = ADDRESS_WIPER_0 | COMMAND_READ; hu8 = SPI_transfer(cmdu8); lu8 = SPI_transfer(DUMMY_DATA); } SPI2_CE0_HIGH(); //CE 0 High return ((uint16_t)hu8 << 8 | (uint16_t)lu8) & BITMASK_READ_DATA_REGISTER; } uint16_t MCP4251_DigitalPotReadStatusRegister() { u8 cmdu8 = 0x00; u8 hu8 = 0x00; u8 lu8 = 0x00; SPI2_CE0_LOW(); //CE 0 Low cmdu8 = ADDRESS_STATUS | COMMAND_READ; hu8 = SPI_transfer(cmdu8); lu8 = SPI_transfer(DUMMY_DATA); SPI2_CE0_HIGH(); //CE 0 High return ((uint16_t)hu8 << 8 | (uint16_t)lu8) & BITMASK_READ_DATA_REGISTER; } uint16_t MCP4251_DigitalPotReadTconRegister() { u8 cmdu8 = 0x00; u8 hu8 = 0x00; u8 lu8 = 0x00; SPI2_CE0_LOW(); //CE 0 Low cmdu8 = ADDRESS_TCON | COMMAND_READ; hu8 = SPI_transfer(cmdu8); lu8 = SPI_transfer(DUMMY_DATA); SPI2_CE0_HIGH(); //CE 0 High return ((uint16_t)hu8 << 8 | (uint16_t)lu8) & BITMASK_READ_DATA_REGISTER; } void MCP4251_DigitalPotWriteTconRegister(uint16_t value) { u8 cmdu8 = 0x00; u8 datau8 = 0x00; if (value > 255) cmdu8 |= 0x01; else datau8 = (u8)(value & 0X00FF); SPI2_CE0_LOW(); //CE 0 Low cmdu8 = cmdu8 | ADDRESS_TCON | COMMAND_WRITE; SPI_transfer(cmdu8); SPI_transfer(datau8); SPI2_CE0_HIGH(); //CE 0 High } void MCP4251_DigitalPotStartup(u8 potNum) { uint16_t tconData = MCP4251_DigitalPotReadTconRegister(); u8 hu8 = (uint8_t)tconData >> 8; u8 lu8 = (uint8_t)tconData & 0xff; if (potNum) lu8 = lu8 | BITMASK_POT1_STARTUP; else lu8 = lu8 | BITMASK_POT0_STARTUP; tconData = (uint16_t)hu8 << 8 | (uint16_t)lu8; MCP4251_DigitalPotWriteTconRegister(tconData); } void MCP4251_DigitalPotShutdown(u8 potNum) { uint16_t tconData = MCP4251_DigitalPotReadTconRegister(); u8 hu8 = (uint8_t)tconData >> 8; u8 lu8 = (uint8_t)tconData & 0xff; if (potNum) lu8 = lu8 & ~BITMASK_POT1_STARTUP; else lu8 = lu8 & ~BITMASK_POT0_STARTUP; tconData = (uint16_t)hu8 << 8 | (uint16_t)lu8; MCP4251_DigitalPotWriteTconRegister(tconData); } void MCP4251_DigitalPotTerminalBConnect(u8 potNum) { uint16_t tconData = MCP4251_DigitalPotReadTconRegister(); u8 hu8 = (uint8_t)tconD |