SingingCat 0
application
bus.c
1#include "sx1262/sx1262.h"
2#include "sx1262/sx126x.h"
3#include "sx1262/sx126x_hal.h"
4#include "mculibrary.h"
5
6/*
7 * PINS:
8 * SX1262 STM32
9 *
10 * CSN PB12 112 (NSS)
11 * SCLK PB13 113
12 * MOSI PB15 115
13 * MISO PB14 114
14 * DIO1 PA0 000
15 * BUSY PC5 205
16 * nRESET PC13 213
17 *
18 * connected to spi port #2
19 */
20
21// return non-zero if timeout
22int wait_for_chip(struct network_context *nctx) {
23 uint32_t started = 0;
24
25 for (started = 0; started < 0x0000FFFF; started++) {
26 int p = mculib_pin_get(nctx->mculib_handle, SX1262_BUSY);
27 if (p == 0) {
28 return 0;
29 }
30 }
31 printf("[sx1262] timeout\r\n");
32 return 1;
33}
34sx126x_hal_status_t sx126x_hal_read(struct network_context *nctx, const uint8_t *command, const uint16_t command_length,
35 uint8_t *data, const uint16_t data_length) {
36 //printf("read called (%i bytes data buf)\r\n",data_length);
37 //print_buf(command_length,command);
38 int i;
39 int r;
40
41 r = mculib_pin_set(nctx->mculib_handle, SX1262_CSN, 1);
42 MCULIB_ERR_RET("RD_CSN1", r);
43 // Delay(100);
44 wait_for_chip(nctx);
45 r = mculib_pin_set(nctx->mculib_handle, SX1262_CSN, 0);
46 MCULIB_ERR_RET("RD_CSN0", r);
47 uint16_t res;
48
49 for (i = 0; i < command_length; i++) {
50 r = mculib_spi_write_and_read(nctx->mculib_handle, 2, command[i], &res);
51 MCULIB_ERR_RET("RD_READ", r);
52 //printf("16 bit res: %i\r\n",res);
53 }
54 for (i = 0; i < data_length; i++) {
55 r = mculib_spi_write_and_read(nctx->mculib_handle, 2, 0, &res);
56 MCULIB_ERR_RET("RD_STATUS", r);
57 //printf("16 bit extra res: %x\r\n",res);
58 data[i] = res;
59 }
60 r = mculib_pin_set(nctx->mculib_handle, SX1262_CSN, 1);
61 return 0;
62}
63
64sx126x_hal_status_t sx126x_hal_write(struct network_context *nctx, const uint8_t *command, const uint16_t command_length, const uint8_t *data, const uint16_t data_length) {
65 // printf("write %i/%i:", command_length,data_length);
66 //print_buf(command_length, command);
67 int i;
68 int r;
69
70 r = mculib_pin_set(nctx->mculib_handle, SX1262_CSN, 1);
71 MCULIB_ERR_RET("WR_CSN1", r);
72 // Delay(100);
73 wait_for_chip(nctx);
74 r = mculib_pin_set(nctx->mculib_handle, SX1262_CSN, 0);
75 MCULIB_ERR_RET("WR_CSN0", r);
76 uint16_t res;
77
78 for (i = 0; i < command_length; i++) {
79 r = mculib_spi_write_and_read(nctx->mculib_handle, 2, command[i], &res);
80 MCULIB_ERR_RET("WR_WRITE", r)
81 // get status??
82 // printf("Status: %x\r\n",res);
83 }
84 for (i = 0; i < data_length; i++) {
85 r = mculib_spi_write_and_read(nctx->mculib_handle, 2, data[i], &res);
86 MCULIB_ERR_RET("WR_STATUS", r)
87 // get status??
88 // printf("Status: %x\r\n", res);
89 }
90
91 /*
92 * r= mculib_spi_write_and_read(2,0,&res);
93 * printf("Status: %x\r\n",res);
94 * r= mculib_spi_write_and_read(2,0,&res);
95 * printf("Status: %x\r\n",res);
96 */
97 r = mculib_pin_set(nctx->mculib_handle, SX1262_CSN, 1);
98 return 0;
99}
100
101
102sx126x_hal_status_t sx126x_hal_reset(struct network_context *nctx) {
103 printf("[sx1262] Reset called\r\n");
104 int r = mculib_pin_set(nctx->mculib_handle, SX1262_RESET, 0);
105
106 MCULIB_ERR_RET("RESET0", r);
107 Delay(100);
108 r = mculib_pin_set(nctx->mculib_handle, SX1262_RESET, 1);
109 MCULIB_ERR_RET("RESET1", r);
110 return 0;
111}
112sx126x_hal_status_t sx126x_hal_wakeup(struct network_context *nctx) {
113 int r;
114
115 printf("[sx1262] wakeup called\r\n");
116 r = mculib_pin_set(nctx->mculib_handle, SX1262_CSN, 0);
117 MCULIB_ERR_RET("WK_CSNSETPIN", r);
118 r = mculib_spi_write_only(nctx->mculib_handle, 2, 0);
119 MCULIB_ERR_RET("WK_WRITE1", r)
120 r = mculib_spi_write_only(nctx->mculib_handle, 2, 0);
121 MCULIB_ERR_RET("WK_WRITE2", r)
122 r = mculib_pin_set(nctx->mculib_handle, SX1262_CSN, 1);
123 MCULIB_ERR_RET("WK_XSN", r);
124
125 return 0;
126}
SX126x radio driver definition.
Hardware Abstraction Layer for SX126x.
sx126x_hal_status_t sx126x_hal_reset(struct network_context *nctx)
Definition: bus.c:102
sx126x_hal_status_t sx126x_hal_wakeup(struct network_context *nctx)
Definition: bus.c:112
sx126x_hal_status_t sx126x_hal_write(struct network_context *nctx, const uint8_t *command, const uint16_t command_length, const uint8_t *data, const uint16_t data_length)
Definition: bus.c:64
sx126x_hal_status_t sx126x_hal_read(struct network_context *nctx, const uint8_t *command, const uint16_t command_length, uint8_t *data, const uint16_t data_length)
Definition: bus.c:34