Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.20 KB | None | 0 0
  1. #include "stdio_serial.h"
  2.  
  3. #define PORTA_PIO_PDR *(volatile int *) 0x400E0E04
  4. #define PORTA_PIO_ABCDSR1 *(volatile int *) 0x400E0E70
  5. #define PORTA_PIO_ABCDSR2 *(volatile int *) 0x400E0E74
  6.  
  7. #define PORTB_PIO_PER *(volatile int *) 0x400E1000
  8. #define PORTB_PIO_PDR *(volatile int *) 0x400E1004
  9. #define PORTB_PIO_OER *(volatile int *) 0x400E1010
  10. #define PORTB_PIO_ODR *(volatile int *) 0x400E1014
  11. #define PORTB_PIO_SODR *(volatile int *) 0x400E1030
  12. #define PORTB_PIO_CODR *(volatile int *) 0x400E1034
  13. #define PORTB_PIO_ODSR *(volatile int *) 0x400E1038
  14. #define PORTB_PIO_PDSR *(volatile int *) 0x400E103C
  15. #define PORTB_PIO_ABCDSR1 *(volatile int *) 0x400E1070
  16. #define PORTB_PIO_ABCDSR2 *(volatile int *) 0x400E1074
  17.  
  18. #define PORTC_PIO_PER *(volatile int *) 0x400E1200
  19. #define PORTC_PIO_PDR *(volatile int *) 0x400E1204
  20. #define PORTC_PIO_OER *(volatile int *) 0x400E1210
  21. #define PORTC_PIO_ODR *(volatile int *) 0x400E1214
  22. #define PORTC_PIO_SODR *(volatile int *) 0x400E1230
  23. #define PORTC_PIO_CODR *(volatile int *) 0x400E1234
  24. #define PORTC_PIO_ODSR *(volatile int *) 0x400E1238
  25. #define PORTC_PIO_PDSR *(volatile int *) 0x400E123C
  26. #define PORTC_PIO_IER *(volatile int *) 0x400E1240
  27. #define PORTC_PIO_ISR *(volatile int *) 0x400E124C
  28. #define PORTC_PIO_AIMER *(volatile int *) 0x400E12B0
  29. #define PORTC_PIO_REHLSR *(volatile int *) 0x400E12D4
  30.  
  31. #define UART0_CR *(volatile int *) 0x400E0600
  32. #define UART0_MR *(volatile int *) 0x400E0604
  33. #define UART0_IMR *(volatile int *) 0x400E0610
  34. #define UART0_IER *(volatile int *) 0x400E0608
  35. #define UART0_IDR *(volatile int *) 0x400E060C
  36. #define UART0_SR *(volatile int *) 0x400E0614
  37. #define UART0_RHR *(volatile int *) 0x400E0618
  38. #define UART0_THR *(volatile int *) 0x400E061C
  39. #define UART0_BRGR *(volatile int *) 0x400E0620
  40.  
  41. #define UART3_CR *(volatile int *) 0x40048000
  42. #define UART3_MR *(volatile int *) 0x40048004
  43. #define UART3_SR *(volatile int *) 0x40048014
  44. #define UART3_RHR *(volatile int *) 0x40048018
  45. #define UART3_THR *(volatile int *) 0x4004801C
  46. #define UART3_BRGR *(volatile int *) 0x40048020
  47.  
  48. // UART Control Register bits
  49. #define UART_CR_TXDIS 0x80
  50. #define UART_CR_TXEN 0x40
  51. #define UART_CR_RXDIS 0x20
  52. #define UART_CR_RXEN 0x10
  53. #define UART_CR_RSTTX 0x08
  54. #define UART_CR_RSTRX 0x04
  55.  
  56. // UART Mode Register bits
  57. #define UART_MR_NOPARITY 0x800
  58.  
  59. // UART Status Register bits
  60. #define UART_SR_TXRDY 0x02
  61. #define UART_SR_RXRDY 0x01
  62.  
  63. #define SPI_CR *(volatile int *) 0x40008000
  64. #define SPI_MR *(volatile int *) 0x40008004
  65. #define SPI_RDR *(volatile int *) 0x40008008
  66. #define SPI_TDR *(volatile int *) 0x4000800C
  67. #define SPI_SR *(volatile int *) 0x40008010
  68. #define SPI_CSR0 *(volatile int *) 0x40008030
  69.  
  70. // SPI Control Register bits
  71. #define SPI_CR_SPIEN 0x01
  72.  
  73. // SPI Mode Register bits
  74. #define SPI_MR_MSTR 0x01
  75.  
  76. // SPI Status Register bits
  77. #define SPI_SR_TDRE 0x02
  78. #define SPI_SR_RDRF 0x01
  79.  
  80. // ADC Registers
  81. #define ADC_CR *(volatile int *) 0x40038000
  82. #define ADC_MR *(volatile int *) 0x40038004
  83. #define ADC_CHER *(volatile int *) 0x40038010
  84. #define ADC_CHDR *(volatile int *) 0x40038014
  85. #define ADC_CHSR *(volatile int *) 0x40038018
  86. #define ADC_IER *(volatile int *) 0x40038024
  87. #define ADC_ISR *(volatile int *) 0x40038030
  88. #define ADC_CDR0 *(volatile int *) 0x40038050
  89. #define ADC_ACR *(volatile int *) 0x40038094
  90.  
  91. #define ADC_DMA_BASE 0x40038100 // DMA addresses start at PERIPHERAL_BASE_ADDRESS + 0x100
  92. #define ADC_DMA_RPR *(volatile int *) (ADC_DMA_BASE + 0x00)
  93. #define ADC_DMA_RCR *(volatile int *) (ADC_DMA_BASE + 0x04)
  94. #define ADC_DMA_TCR *(volatile int *) (ADC_DMA_BASE + 0x20)
  95. #define ADC_DMA_TSR *(volatile int *) (ADC_DMA_BASE + 0x24)
  96.  
  97. // ADC Control Register Bits
  98. #define ADC_START 0x02
  99. #define ADC_SWRST 0x01
  100.  
  101. // ADC Mode Register Bits
  102. #define ADC_TRANSFER 0x30000000
  103. #define ADC_PRESCAL PRESCAL_VALUE * 0x100
  104. #define PRESCAL_VALUE 0x00 // <---- Change this value to change prescaler
  105. #define ADC_LOWRES 0x10
  106. #define ADC_TRIG_TC0 0x02
  107. #define ADC_TRGEN 0x01
  108.  
  109. // ADC Channel Enable Register Bits
  110. #define ADC_EN_CH0 0x01
  111.  
  112. // ADC Channel Disable Register Bits
  113. #define ADC_DIS_CH0 0x01
  114.  
  115. // ADC Channel Status Register Bits
  116. #define ADC_STAT_CH0 0x01
  117.  
  118. // ADC Analog Control Register Bits
  119. #define ADC_IRCE 0x04
  120. #define ADC_FORCEREF 0x80000
  121.  
  122. // ADC Interrupt Register Bits
  123. #define ADC_RXBUFF_INT 0x10000000
  124.  
  125. // DMA Controller Transfer Control Register Bits
  126. #define DMA_RC_EN (1 << 0)
  127.  
  128. // timer0 channel 0 registers
  129. #define TC_CCR0_CH0 *(volatile int *) 0x40010000
  130. #define TC_CMR0_CH0 *(volatile int *) 0x40010004
  131. #define TC_CVR0_CH0 *(volatile int *) 0x40010010
  132. #define TC_RA0_CH0 *(volatile int *) 0x40010014
  133. #define TC_RB0_CH0 *(volatile int *) 0x40010018
  134. #define TC_RC0_CH0 *(volatile int *) 0x4001001C
  135. #define TC_SR0_CH0 *(volatile int *) 0x40010020
  136. #define TC_IER0_CH0 *(volatile int *) 0x40010024
  137. #define TC_IDR0_CH0 *(volatile int *) 0x40010028
  138. #define TC_IMR0_CH0 *(volatile int *) 0x4001002C
  139.  
  140. // Timer0 Channel 0 Channel Control Register Bits
  141. #define CCR_SWTRG 0x04
  142. #define CCR_CLKDIS 0x02
  143. #define CCR_CLKEN 0x01
  144.  
  145. // Timer0 Channel 0 Channel Mode Register Bits
  146. #define CMR_WAVE_EN 0x8000
  147. #define CMR_WAVE_SEL 0x4000
  148. #define CMR_DIV_128 0x03
  149. #define CMR_DIV_32 0x02
  150. #define CMR_DIV_2 0x00
  151.  
  152. // Timer0 Channel 0 Status Register Bits
  153. #define SR_CPCS 0x10
  154.  
  155. // Timer0 Channel 0 Interrupt Enable/Disable/Mask Register Bits
  156. #define INT_CPCS 0x10
  157. #define INT_CPAS 0x04
  158.  
  159. // PWM Module 0
  160. #define PWM_MR *(volatile int *) 0x40020000
  161. #define PWM_ENA *(volatile int *) 0x40020004
  162. #define PWM_DIS *(volatile int *) 0x40020008
  163. #define PWM_SR *(volatile int *) 0x4002000C
  164. #define PWM_CMR0 *(volatile int *) 0x40020200
  165. #define PWM_CDTY0 *(volatile int *) 0x40020204
  166. #define PWM_CPRD0 *(volatile int *) 0x40020208
  167. #define PWM_CCNT0 *(volatile int *) 0x4002020C
  168. #define PWM_CUPD0 *(volatile int *) 0x40020210
  169.  
  170. #define CHID0 0x01
  171.  
  172.  
  173. // Declare any global variables here
  174. unsigned short x_position, y_position;
  175. unsigned char buttons;
  176.  
  177. /*** ADC DMA Buffer ***/
  178. #define DATA_SIZE 1024
  179. #define DATA_SIZE_SMALL 32
  180. unsigned short adc_array[DATA_SIZE] = {0};
  181. unsigned char xmt_buffer[DATA_SIZE] = {0};
  182. unsigned char rcv_buffer[DATA_SIZE_SMALL] = {0};
  183. unsigned char rcv_buf_cnt = 0;
  184. unsigned short xmt_buf_cnt = 0;
  185. unsigned char * xmt_buf_in = 0;
  186. unsigned char * xmt_buf_out = 0;
  187. unsigned char * rcv_buf_in = 0;
  188. unsigned char * rcv_buf_out = 0;
  189. unsigned char * end_of_xmt_buffer = 0;
  190. unsigned char * end_of_rcv_buffer = 0;
  191. unsigned char * beginning_of_rcv_buffer = 0;
  192.  
  193. // Interrupt Service Routines go here
  194. /* Timer0 ISR */
  195. void TC0_Handler() {
  196. // Declare variables
  197. unsigned short timer0_status;
  198.  
  199. // Read the Status Register (this also clears the interrupt)
  200. timer0_status = TC_SR0_CH0;
  201. }
  202.  
  203. void ADC_Handler() {
  204. // Declare variables
  205. unsigned int adc_status;
  206.  
  207. // Read the Status Register (this also clears the interrupt)
  208. adc_status = ADC_ISR;
  209.  
  210. // Set the Receive Pointer Register in the ADC DMA controller
  211. ADC_DMA_RPR = (int) adc_array; // type cast to suppress warnings
  212. // Set the Receive Counter in the ADC DMA controller
  213. ADC_DMA_RCR = DATA_SIZE;
  214. }
  215.  
  216. void UART0_Handler()
  217. {
  218. unsigned char ch, ch2;
  219. //unsigned int status_reg;
  220. //status_reg = UART0_SR;
  221. if (UART0_SR & UART_SR_RXRDY)
  222. {
  223. // grab character from RHR
  224. ch = UART0_RHR;
  225. // set data in address of rcv_buf_in to ch
  226. *rcv_buf_in = ch;
  227. // increment buf counter
  228. rcv_buf_cnt++;
  229. // increment rcv_buf_in pointer
  230. rcv_buf_in++;
  231. // check if @ end of buffer
  232. if (rcv_buf_in == end_of_rcv_buffer)
  233. {
  234. rcv_buf_in = &rcv_buffer;
  235. }
  236. }
  237. if (UART0_SR & UART_SR_TXRDY)
  238. {
  239. // grab character at xmit_buf_out
  240. ch2 = *xmt_buf_out;
  241. // send the character via THR
  242. UART0_THR = ch2;
  243. // increment pointer
  244. xmt_buf_out++;
  245. // if at end, set back to beginning
  246. if (xmt_buf_out == end_of_xmt_buffer)
  247. {
  248. xmt_buf_out = &xmt_buffer;
  249. }
  250. // decrement buf_cnt
  251. xmt_buf_cnt--;
  252. // if we're out of characters
  253. if(!xmt_buf_cnt)
  254. {
  255. //disable TXRDY interrupt by setting it high on IDR
  256. UART0_IDR = UART_SR_TXRDY;
  257. }
  258. }
  259. }
  260.  
  261. // Function prototypes for user defined subroutines
  262. void our_ioport_init();
  263. void stdio_uart_init();
  264. void uart3_init();
  265. void spi_init();
  266. void adc_init();
  267. void timer_init();
  268. void pwm_init();
  269. void PutCharacter(unsigned char ch);
  270. void PutString(unsigned char string[]);
  271. void delay_ms(unsigned short delay_time);
  272. void delay_us(unsigned short delay_time);
  273. void spi_joystick_exchange(unsigned char leds);
  274. void my_puts(unsigned char *);
  275.  
  276. int main(void)
  277. {
  278. // Declare local variables
  279. unsigned short adc_data=0; // raw A/D value: between 0 and 1023 (0x000 and 0x3FF)
  280. unsigned int dma_ptr = 0;
  281. unsigned int dma_ctr = 0;
  282. unsigned short tmp = 0;
  283. unsigned short tmp2 = 0;
  284. unsigned char ch = 0;
  285.  
  286. // enable interrupts on uart0
  287. NVIC_EnableIRQ(UART0_IRQn);
  288.  
  289. // Initialize the SAM4 board
  290. sysclk_init();
  291. board_init();
  292.  
  293. // Initialize the I/O ports
  294. our_ioport_init();
  295.  
  296. // Initialize UART3 to send data to the Serial LCD
  297. uart3_init();
  298. stdio_uart_init();
  299.  
  300. // Initialize the ADC and start the first conversion
  301. adc_init();
  302.  
  303. // Initialize Timer 0 for interrupts
  304. timer_init();
  305.  
  306. // Clear any previous stuff off of the terminal screen
  307. puts("\x1b[2J\x1b[H");
  308.  
  309. // initialize the xmt and rcv buffer pointers
  310. xmt_buf_in = &xmt_buffer;
  311. xmt_buf_out = &xmt_buffer;
  312. rcv_buf_in = &rcv_buffer;
  313. rcv_buf_out = &rcv_buffer;
  314.  
  315. end_of_xmt_buffer = &xmt_buffer;
  316. end_of_xmt_buffer = end_of_xmt_buffer + 1024;
  317.  
  318. end_of_rcv_buffer = &rcv_buffer;
  319. end_of_rcv_buffer = end_of_rcv_buffer + 32;
  320.  
  321. beginning_of_rcv_buffer = &rcv_buffer;
  322.  
  323. xmt_buf_cnt = 0;
  324. rcv_buf_cnt = 0;
  325.  
  326. // enable RXRDY interrupt via uart IER
  327. UART0_IER = UART_SR_RXRDY;
  328.  
  329. printf("Testing input, first.\r\n");
  330. for (tmp = 0; tmp < 100; tmp++)
  331. {
  332. for (tmp2 = 0; tmp2 < 2; tmp2++)
  333. {
  334. my_puts("\r\nTesting input again.");
  335. }
  336. }
  337.  
  338. printf("Data is : \r\n");
  339. while (rcv_buf_cnt)
  340. {
  341. // get new char from rcv_buf
  342. ch = *rcv_buf_out;
  343. // display it
  344. putchar(ch);
  345. rcv_buf_cnt--;
  346. rcv_buf_out++;
  347. if (rcv_buf_out == end_of_rcv_buffer)
  348. {
  349. rcv_buf_out = &rcv_buffer;
  350. }
  351. }
  352. //printf("Data is : \r\n");
  353. //printf(beginning_of_rcv_buffer);
  354. while (1)
  355. {
  356. /*my_puts("Testing input again.\r\n");
  357.  
  358. if (rcv_buf_cnt)
  359. {
  360. // get new char from rcv_buf
  361. ch = *rcv_buf_out;
  362. // display it
  363. putchar(ch);
  364. rcv_buf_cnt--;
  365. rcv_buf_out++;
  366. if (rcv_buf_out == end_of_rcv_buffer)
  367. {
  368. rcv_buf_out = &rcv_buffer;
  369. }
  370. }
  371. */
  372. }
  373. /* Infinite loop
  374. while (true)
  375. {
  376.  
  377. Make a copy of where the DMA is currently operating
  378. /* previous lab
  379. dma_ptr = ADC_DMA_RPR;
  380. dma_ctr = ADC_DMA_RCR;
  381.  
  382. printf("dma_ptr=0x%x dma_ctr=0x%3x \r", dma_ptr, dma_ctr);
  383.  
  384. } // end of infinite while loop
  385. */
  386. } // end of main
  387.  
  388. // User defined subroutines
  389.  
  390. void our_ioport_init() {
  391.  
  392. // Configure Port A for Peripheral A functionality on bits 0-31
  393. PORTA_PIO_PDR = 0xFFFFFFFF; // Disable all bits
  394. PORTA_PIO_ABCDSR1 = 0x01; // Assign all bits for Peripheral A functions
  395. PORTA_PIO_ABCDSR2 = 0x00;
  396.  
  397. // Configure Port B for General Purpose Input/Output (GPIO) functionality on bits 0-3 and 14,
  398. PORTB_PIO_PER = 0x0000400F; // Enable bits 0-3 and 14
  399. PORTB_PIO_OER = 0x0000400F; // Make bits 0-3 and 14 as outputs
  400.  
  401. // assign bits 10 and 11 for Peripheral B functionality
  402. PORTB_PIO_PDR = 0x00000C00; // Disable bits 10 and 11
  403. PORTB_PIO_ABCDSR1 = 0xC00; // Assign bits 10 and 11 for Peripheral B functions
  404. PORTB_PIO_ABCDSR2 = 0x000; // (RX and TX for UART3)
  405.  
  406. // Configure Port C for General Purpose Input/Output (GPIO) functionality on bits 0-3
  407. PORTC_PIO_PER = 0x0000000F; // Enable bits 0-3
  408. PORTC_PIO_ODR = 0x0000000F; // Make bits 0-3 as inputs
  409.  
  410. // Configure Port C, bit 0 to generate an interrupt on a rising edge
  411. //PORTC_PIO_IER = 0x01; // Enable bit 0 for interrupts
  412. //PORTC_PIO_AIMER = 0x01; // Set bit 0 for additional information
  413. //PORTC_PIO_REHLSR = 0x01; // Set bit 0 for rising edge
  414.  
  415. // Enable Port C interrupts
  416. //NVIC_EnableIRQ(PIOC_IRQn);
  417. }
  418.  
  419. void stdio_uart_init() {
  420.  
  421. // Required data for UART0
  422. const usart_serial_options_t uart_serial_options = {
  423. .baudrate = CONF_UART_BAUDRATE,
  424. .paritytype = CONF_UART_PARITY,
  425. };
  426.  
  427. // Initialize the Standard I/O UART to UART0
  428. pmc_enable_periph_clk(CONSOLE_UART_ID);
  429. stdio_serial_init(CONF_UART, &uart_serial_options);
  430. }
  431.  
  432. void uart3_init() {
  433.  
  434. // Assign a clock signal to this peripheral.
  435. pmc_enable_periph_clk(ID_UART3);
  436.  
  437. // Configure UART3 for 9600 baud and no parity. Data length of 8 bits and 1 stop bit are default values.
  438.  
  439. // Set the baud rate to 9600 as per the formula provided.
  440. UART3_BRGR = ((100000000/9600)/16);
  441. // Set the mode to no parity.
  442. UART3_MR = UART_MR_NOPARITY;
  443. // Lastly enable the UART3 receiver and transmitter.
  444. UART3_CR = UART_CR_RXEN | UART_CR_TXEN;
  445. }
  446.  
  447. void spi_init() {
  448.  
  449. // Assign a clock signal to this peripheral
  450. pmc_enable_periph_clk(ID_SPI);
  451.  
  452. // First disable the SPI module through the Control Register
  453. SPI_CR = SPI_CR_SPIDIS;
  454. // Next reset the SPI module
  455. SPI_CR = SPI_CR_SWRST;
  456. // Next configure for Master mode through the Mode Register
  457. SPI_MR = SPI_MR_MSTR;
  458. // Next configure the SPI serial clock baud rate (SCBR) for 1 MHz (100 MHz / 100)
  459. SPI_CSR0 = SPI_CSR_NCPHA + 0x6400;
  460. // Last enable the SPI module through the Control Register
  461. SPI_CR = SPI_CR_SPIEN;
  462.  
  463. // Set the Slave Select line high on Port B, bit 0
  464. PORTB_PIO_SODR = 0x01;
  465. }
  466.  
  467. void adc_init() {
  468.  
  469. // Assign a clock signal to this peripheral
  470. pmc_enable_periph_clk(ID_ADC);
  471.  
  472. // Set the transfer bits to 2 in the Mode Register <MUST ALWAYS BE DONE>
  473. // and set the A/D to trigger on the TIOA output signal from Timer Counter 0
  474. ADC_MR = ADC_TRANSFER + ADC_PRESCAL + ADC_TRIG_TC0 + ADC_TRGEN;
  475. // Enable channel 0 of the ADC in the Channel Enable Register
  476. ADC_CHER = ADC_EN_CH0;
  477. // Enable internal reference voltage and force internal reference voltage VDDIN
  478. // in the Analog Control Register
  479. ADC_ACR = ADC_IRCE + ADC_FORCEREF;
  480.  
  481.  
  482. // Set the Receive Pointer Register in the ADC DMA controller
  483. ADC_DMA_RPR = (int) adc_array; // type cast to suppress warnings
  484. // Set the Receive Counter in the ADC DMA controller
  485. ADC_DMA_RCR = DATA_SIZE;
  486. // Enable the DMA receiver
  487. ADC_DMA_TCR = DMA_RC_EN;
  488.  
  489. // Configure for ADC RXBUFF interrupt when the DMA counter reaches 0
  490. ADC_IER = ADC_RXBUFF_INT;
  491. // Enable interrupts for the ADC
  492. NVIC_EnableIRQ(ADC_IRQn);
  493.  
  494. // Start the first conversion of the ADC in the Control Register
  495. // ADC_CR = ADC_START;
  496. }
  497.  
  498. void timer_init() {
  499.  
  500. // Assign a clock signal to this peripheral.
  501. pmc_enable_periph_clk(ID_TC0);
  502.  
  503. // Divide the peripheral clock by 128 (100 MHz / 128 = 781.25 kHz)
  504. // Enable waveform mode
  505. TC_CMR0_CH0 = 0xC0000 + CMR_DIV_128 + CMR_WAVE_EN + CMR_WAVE_SEL;
  506. // Load the RC register with a value to interrupt every 20 ms (50 interrupts per second)
  507. TC_RC0_CH0 = 0x4000;
  508.  
  509. // Enable the RA compare interrupt
  510. //TC_IER0_CH0 = INT_CPCS;
  511. // Enable interrupts for timer0
  512. //NVIC_EnableIRQ(TC0_IRQn);
  513.  
  514. // Enable the timer
  515. TC_CCR0_CH0 = CCR_CLKEN + CCR_SWTRG;
  516. }
  517.  
  518. void pwm_init() {
  519.  
  520. // Assign a clock signal to this peripheral.
  521. pmc_enable_periph_clk(ID_PWM);
  522.  
  523. // Set up for CLKA to be MCLK / 32 / 3 = 1.041667 MHz
  524. PWM_MR = 0x05 * 0x100 + 3;
  525. // Set up the PWM to use CLKA and set the polarity to 1
  526. PWM_CMR0 = 0x2 * 0x100 + 0x0B;
  527. // Set up the period to be around 0.5 msec (2 kHz) by setting CPRD = 511
  528. // CLKA / CPRD = 1.041667 MHz / 511 = 2038.48 Hz
  529. PWM_CPRD0 = 511;
  530. // Set up for a 0% duty cycle to start
  531. PWM_CDTY0 = 0;
  532. // Enable the PWM Module
  533. PWM_ENA = CHID0;
  534.  
  535. }
  536.  
  537. void PutCharacter(unsigned char ch) {
  538. // Declare variables
  539. unsigned char status;
  540.  
  541. // Read the UART3 Status register
  542. status = UART3_SR;
  543. // Mask out the Transmit Ready bit
  544. status = status & UART_SR_TXRDY;
  545. // Is the TXRDY bit set? If not, then re-read and re-mask
  546. while (status == 0x00) {
  547. // Re-read the UART3 Status Register
  548. status = UART3_SR;
  549. // Re-mask out the TXRDY bit
  550. status = status & UART_SR_TXRDY;
  551. }
  552. // The TXRDY bit is set, so send the character on the Transmit line
  553. // by writing it into the Transmit Holding Register (THR)
  554. UART3_THR = ch;
  555. }
  556.  
  557. // Added before 10/26 lab
  558. void my_puts(unsigned char * tmp)
  559. {
  560. // get the first character from the string and increment pointer
  561. unsigned char ch;
  562. ch = *tmp;
  563. tmp++;
  564.  
  565. while (ch)
  566. {
  567. // increment xmt_buffer counter
  568. xmt_buf_cnt++;
  569. // store this character in the xmt_buffer using xmt_buf_in pointer
  570. * xmt_buf_in = ch;
  571. // increment ^ pointer
  572. xmt_buf_in++;
  573. // at the end of buffer?
  574. if (xmt_buf_in == end_of_xmt_buffer)
  575. {
  576. // set pointer to the beginning of the buffer
  577. xmt_buf_in = &xmt_buffer;
  578. }
  579. // get next character from string and increment the string pointer
  580. ch = *tmp;
  581. tmp++;
  582. }
  583. // if we get here, ch == 0
  584. // read UART IMT and mask out TXRDY
  585. // enable txrdy interrupt via uart IER if status = 0)
  586. if (!(UART0_IMR & UART_SR_TXRDY))
  587. UART0_IER = UART_SR_TXRDY;
  588. return;
  589. }
  590.  
  591.  
  592.  
  593. void PutString(unsigned char string[]) {
  594. // Declare variables
  595. unsigned char i, ch;
  596.  
  597. // Initialize index counter
  598. i = 0;
  599.  
  600. // Send characters using PutCharacter until the null character is found
  601. while ((ch = string[i]) != 0x00) {
  602. PutCharacter(ch);
  603. // increment the index counter
  604. i++;
  605. }
  606. }
  607.  
  608. void delay_ms(unsigned short delay_time) {
  609. // Declare variables
  610. unsigned short i;
  611.  
  612. // outer loop
  613. while (1) {
  614. // inner for loop
  615. // Wait 1 millisecond
  616. for (i=0; i<7600; i++) {
  617. // do nothing
  618. }
  619. // decrement delay_time
  620. delay_time--;
  621. // see if it is time to leave
  622. if (delay_time == 0) {
  623. // break out of this infinite loop
  624. break;
  625. }
  626. }
  627. }
  628.  
  629. void delay_us(unsigned short delay_time) {
  630. // Declare variables
  631. unsigned char i;
  632.  
  633. // outer loop
  634. while (1) {
  635. // inner for loop
  636. // Wait 1 millisecond
  637. for (i=0; i<8; i++) {
  638. // do nothing
  639. }
  640. // decrement delay_time
  641. delay_time--;
  642. // see if it is time to leave
  643. if (delay_time == 0) {
  644. // break out of this infinite loop
  645. break;
  646. }
  647. }
  648. }
  649.  
  650. void spi_joystick_exchange(unsigned char leds) {
  651.  
  652. // Declare variables
  653. unsigned char x_low, x_high, y_low, y_high;
  654.  
  655. // Set SS low on Port B, bit 3
  656. PORTB_PIO_CODR = 0x08;
  657. delay_us(3);
  658.  
  659. SPI_TDR = 0x80 + leds;
  660. while ((SPI_SR & SPI_SR_RDRF) == 0x00) {
  661. // wait until RDRF is set
  662. }
  663. x_low = SPI_RDR;
  664.  
  665. delay_us(8);
  666.  
  667. SPI_TDR = 0x11;
  668. while ((SPI_SR & SPI_SR_RDRF) == 0x00) {
  669. // wait until RDRF is set
  670. }
  671. x_high = SPI_RDR;
  672.  
  673. delay_us(8);
  674.  
  675. SPI_TDR = 0x22;
  676. while ((SPI_SR & SPI_SR_RDRF) == 0x00) {
  677. // wait until RDRF is set
  678. }
  679. y_low = SPI_RDR;
  680.  
  681. delay_us(8);
  682.  
  683. SPI_TDR = 0x44;
  684. while ((SPI_SR & SPI_SR_RDRF) == 0x00) {
  685. // wait until RDRF is set
  686. }
  687. y_high = SPI_RDR;
  688.  
  689. delay_us(8);
  690.  
  691. SPI_TDR = 0x88;
  692. while ((SPI_SR & SPI_SR_RDRF) == 0x00) {
  693. // wait until RDRF is set
  694. }
  695. buttons = SPI_RDR;
  696.  
  697. PORTB_PIO_SODR = 0x08;
  698.  
  699. x_position = x_high * 0x100 + x_low;
  700. y_position = y_high * 0x100 + y_low;
  701. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement