/
ntu32_config.c
350 lines (289 loc) · 9.58 KB
/
ntu32_config.c
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
#include "ntu32_config.h"
#include "uz2400/rf.h"
EXTI_InitTypeDef RF_EXTI_InitStructure;
void (*fp)(void);
void (*usart_fp)(int c);
void uart1_set_input(void (*func))
{
usart_fp = func;
// halDigioIntSetEdge(&pinRadio_GPIO0, HAL_DIGIO_INT_RISING_EDGE);
// halDigioIntConnect(&pinRadio_GPIO0, pfISR);
// halDigioIntEnable(&pinRadio_GPIO0);
// And clear the exception
// CLEAR_EXC_RX_FRM_DONE();
}
/***************************************************************************//**
* @brief Setup clocks
******************************************************************************/
void SetupClock()
{
if(!(SYS_RegLockAddr&0x00000001UL)) {
SYS_RegLockAddr = 0x59;
SYS_RegLockAddr = 0x16;
SYS_RegLockAddr = 0x88;
}
/* Set HCLK source form HIRC and divide 1 */
CLK_CLKSEL0 |= 0x00000007UL;
CLK_CLKDIV0 = (CLK_CLKDIV0 & ~0x0000000FUL);
/* Enable HIRC */
CLK_PWRCTL |= 0x00000004UL;
/* Waiting for HIRC clock ready */
while((CLK_CLKSTATUS&0x00000010UL)!=0x00000010UL);
/* Set HCLK frequency 42MHz ,from HIRC, NR=16, NF=56, NO=1, (source = 12M)*/
CLK_PLLCTL = (CLK_PLLCTL&~0x0002133FUL)|0x00020318UL;
CLK_PLLCTL &= ~0x00010000UL;
/* Waiting for PLL ready */
while((CLK_CLKSTATUS&0x00000004UL)!=0x00000004UL);
/* Change HCLK source form PLL and divide 1 */
CLK_CLKSEL0 = (CLK_CLKSEL0 & ~0x00000007UL)|0x00000002UL;
CLK_CLKDIV0 = (CLK_CLKDIV0 & ~0x0000000FUL);
/* Select UART clock source from HIRC, and DIV=1*/
CLK_CLKSEL1 = (CLK_CLKSEL1&~0x00000003UL)|0x00000002UL;
CLK_CLKDIV0 = (CLK_CLKDIV0 & ~0x00000F00UL);
/* Select SPI0 clock source from HCLK, and DIV=1*/
CLK_CLKSEL2 |= 0x00100000UL;
/* Enable UART0, SPI0 IP clock */
CLK_APBCLK |= 0x00011000UL;
/* Enable DMA IP clock */
CLK_AHBCLK |= 0x00000002UL;
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
/* Set PB multi-function pins for UART0 RXD and TXD */
SYS_PB_L_MFP = (SYS_PB_L_MFP&~0x00000077UL)|0x00000011UL;
/* Set multi function pin for SPI0 */
SYS_PC_L_MFP = (SYS_PC_L_MFP&~0x00007777UL)|0x00001111UL;
/* Lock protected registers */
SYS_RegLockAddr = 0;
}
void SetupIO(void){
/* init RF INT pin9, RF Reset pin10, RF wakeup pin11 */
GPIOB_PMD = 0x00500000UL;
//RF WAKEUP to low
GPIOB11 = 0x00000000UL;
//RF RESET to low
GPIOB10 = 0x00000000UL;
}
/***************************************************************************//**
* @brief Init UART
******************************************************************************/
void SetupUART(void)
{
/* Init UART0 to 8n1 FIFO depth = 1 and select UART */
UART0_TLCTL = 0x00000003UL;
UART0_FUN_SEL &= ~0x00000003UL;
/* 115200 = 42M/(364+1) */
UART0_BAUD = 364;
/* RX interrupt enable */
UART0_IER = 0x00000001UL;
}
void SetupI2C(void){
for(;;){
UART0_DATA = 0x31;
while(!(UART0_FSR&0x00000200UL));
}
}
void SetupRfSpi(void){
/* CLK keep low whan idle, TX send data on falling edge, RX get data on
* rising data, data width 8 bit, SPI Master mode
*/
SPI0_CTL = 0x00000044UL;
/* baud rate , because PLL is 42M, so SPI CLK = 42/(9+1) = 4.2M*/
SPI0_CLKDIV = 0x00000009UL;
/* Disable AUTO SS, SPI_SS to high */
SPI0_SSR = 0x00000005UL;
/* enable DMA channel 1& 2 */
DMA_GCRCSR = 0x00000600UL;
/* Set SPI width by byte*/
PDMA_CSR1 = 0x00080000UL;
PDMA_CSR2 = 0x00080000UL;
/* set DAM timeout , disable DMA timer*/
PDMA_TCR2 = 0x5555;
PDMA_CSR2 = (PDMA_CSR2&~0x00001000UL);
/* set DMA mux of SPI0 periphal */
DMA_DSSR0 = (DMA_DSSR0&~0x001F1F00UL)|0x00100000UL;
/* set DMA1 mem to APB */
PDMA_CSR1 = (PDMA_CSR1&~0x0000000CUL)|0x00000008UL|0x00000001UL;
/* set DMA2 APB to mem */
PDMA_CSR2 = (PDMA_CSR2&~0x0000000CUL)|0x00000004UL|0x00000001UL;
/* set source address */
PDMA_DAR1 = SPI0_TX0;
PDMA_CSR1 = (PDMA_CSR1&~0x000000F0UL)|0x00000080UL;
/* set destination address */
PDMA_SAR2 = SPI0_RX0;
PDMA_CSR2 = (PDMA_CSR2&~0x000000F0UL)|0x00000020UL;
}
void SetupIsr(void){
/* NVIC of UART0 enable */
NVIC_ISER |= 1<<12;
// RF INT falling trigger
GPIOB_IMD &= ~0x00000200UL;
GPIOB_IER = (GPIOB_IMD&~0x02000000UL)|0x00000200UL;
/* NVIC of RF int enable */
NVIC_ISER |= 1<<4;
/*---------------------------------------------------------------------------------------------------------*/
/* SysTick initial */
/*---------------------------------------------------------------------------------------------------------*/
/* source is PLL 42Mh, target Tick time is 10ms */
SYST_RVR = 420000;
/* clear current SysTick value */
SYST_CVR = 0;
/* Enable SysTick interrupt, source from internal , start to counting */
SYST_CTL = 0x00000007UL;
}
void EnableRfIsr(void){
GPIOB_IER |= 0x00000200UL;
}
void DisableRfIsr(void){
GPIOB_IER &= ~0x00000200UL;
}
void cc2520_isr_handler() {
if(fp!=0) fp();
}
INTERRUPT_STS IntStatus;
void uz2400_isr_handler(void){
*((uint8_t *)&IntStatus) = spi_sr(ISRSTS);
if(IntStatus.TxN){ //Transmit frame success
}
if(IntStatus.TxG1){
}
if(IntStatus.TxG2){
}
if(IntStatus.Rx){ //Received a frame
if(fp!=0) fp();
}
if(IntStatus.Sec){ //Received a secure frame , need key to decrypt
}
if(IntStatus.Timer){
}
if(IntStatus.Wakeup){
}
if(IntStatus.Sleep){
}
}
void I2C_ByteWrite(I2C_TypeDef* I2Cx, u8 data,u8 devAddr, u8 WriteAddr)
{
#if 0
/* Send STRAT condition */
I2C_GenerateSTART(I2Cx, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2Cx, devAddr, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/* Send the EEPROM's internal address to write to */
I2C_SendData(I2Cx, WriteAddr);
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send the byte to be written */
I2C_SendData(I2Cx, data);
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send STOP condition */
I2C_GenerateSTOP(I2Cx, ENABLE);
#endif
}
void I2C_BufferRead(I2C_TypeDef* I2Cx,u8* pBuffer,u8 devAddr, u8 ReadAddr, u16 NumByteToRead)
{
#if 0
/* Send START condition */
I2C_GenerateSTART(I2Cx, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2Cx, devAddr, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/* Clear EV6 by setting again the PE bit */
I2C_Cmd(I2Cx, ENABLE);
/* Send the EEPROM's internal address to write to */
I2C_SendData(I2Cx, ReadAddr);
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send STRAT condition a second time */
I2C_GenerateSTART(I2Cx, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
/* Send EEPROM address for read */
I2C_Send7bitAddress(I2Cx, devAddr, I2C_Direction_Receiver);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
/* While there is data to be read */
while(NumByteToRead)
{
if(NumByteToRead == 1)
{
/* Disable Acknowledgement */
I2C_AcknowledgeConfig(I2Cx, DISABLE);
/* Send STOP Condition */
I2C_GenerateSTOP(I2Cx, ENABLE);
}
/* Test on EV7 and clear it */
if(I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
/* Read a byte from the EEPROM */
*pBuffer = I2C_ReceiveData(I2Cx);
/* 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(I2Cx, ENABLE);
#endif
}
int pressed = 0;
extern button_pressed();
//USER ISR
void EINT0_IRQHandler(void){
if (EXTI_GetITStatus(EXTI_Line2) != RESET){
if(pressed) pressed = 0;
else pressed = 1;
button_pressed();
EXTI_ClearITPendingBit(EXTI_Line2);
}
}
//ACC ISR
void EINT1_IRQHandler(void){
if (EXTI_GetITStatus(EXTI_Line3) != RESET){
HandleMMA7660Isr();
EXTI_ClearITPendingBit(EXTI_Line3);
}
}
//RF ISR
void GPABC_IRQHandler(void){
if(GPIOB_ISRC&0x00000200UL){
disable_rf_int();
uz2400_isr_handler();
enable_rf_int();
}
GPIOB_ISRC = GPIOB_ISRC;
#if 0
if (EXTI_GetITStatus(EXTI_Line4) != RESET){
#ifdef SELECT_CC2520
halRfDisableRxInterrupt();
cc2520_isr_handler();
halRfEnableRxInterrupt();
#else
//disable_rf_int();
uz2400_isr_handler();
//enable_rf_int();
#endif
EXTI4_ClearBit();
}
#endif
}
//UART0 ISR
void UART0_IRQHandler(void)
{
if(usart_fp!=0) usart_fp(UART0_DATA);
#if 0
if ((USART1->SR & USART_FLAG_RXNE) != (u16)RESET)
{
int c = USART_ReceiveData(USART1);
if(usart_fp!=0) usart_fp(c);
}
#endif
}