Advertisement
Guest User

RM46 Code

a guest
Nov 26th, 2022
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.04 KB | None | 0 0
  1. #include "supervisor.h"
  2. #include "obc_sci_io.h"
  3. #include "obc_i2c_io.h"
  4.  
  5. #include <FreeRTOS.h>
  6. #include <os_task.h>
  7.  
  8. #include <sys_common.h>
  9. #include "system.h"
  10. #include <stdio.h>
  11. #include <gio.h>
  12. #include <sci.h>
  13. #include <i2c.h>
  14.  
  15. #include "spi.h"
  16.  
  17. #define SPI_REG spiREG3
  18.  
  19. #define TEST_REG 0x00
  20. #define VERSION_REG 0x40 // RM46: 96, Arduino: 128
  21.  
  22. spiDAT1_t spi_config = {
  23.     .CS_HOLD = FALSE,
  24.     .WDEL = TRUE,
  25.     .DFSEL = SPI_FMT_1,
  26.     .CSNR = SPI_CS_1
  27. };
  28.  
  29. void write_reg(uint16_t addr, uint16_t data) {
  30.     spiPORT3->DCLR = (1 << 1);
  31.     addr = addr | 0x80;
  32.     spiTransmitData(SPI_REG, &spi_config, 1, &addr);
  33.     spiTransmitData(SPI_REG, &spi_config, 1, &data);
  34.     spiPORT3->DSET = (1 << 1);
  35. }
  36.  
  37. void read_reg(uint16_t addr, uint8_t *rx_data) {
  38.     spiPORT3->DCLR = (1 << 1);
  39.     addr = addr & 0x7F;
  40.     spiTransmitData(SPI_REG, &spi_config, 1, &addr);
  41.     spiReceiveData(SPI_REG, &spi_config, 1, (uint16_t *)rx_data);
  42.     spiPORT3->DSET = (1 << 1);
  43. }
  44.  
  45. int main(void) {
  46.  
  47.     // Run hardware initialization code (TODO: refactor all this into one function call)
  48.     gioInit();
  49.     sciInit();
  50.     i2cInit();
  51.  
  52.     // Initialize bus mutexes
  53.     initSciMutex();
  54.     initI2CMutex();
  55.  
  56.     spiInit();
  57.  
  58.     uint16_t tx_data = 0;
  59.     uint8_t rx_data = 7;
  60.  
  61.     //Reset the CPLD
  62.     write_reg(0x07, 0x80);
  63.     // Simple delay.
  64.     for (int i = 0; i < 10000000; i++) {
  65.         // Do nothing.
  66.     }
  67.     write_reg(0x07, 0x00);
  68.     // Simple delay.
  69.     for (int i = 0; i < 10000000; i++) {
  70.         // Do nothing.
  71.     }
  72.  
  73.     while(1) {
  74.  
  75.         // Toggle the LED.
  76.         gioToggleBit(gioPORTB, 1);
  77.  
  78.         write_reg(TEST_REG, tx_data);
  79.         read_reg(0x41, &rx_data);
  80.  
  81.         char buffer[50];
  82.         uint8_t len = sprintf(buffer, "\r\nrx_data: %d", rx_data);
  83.         printTextSci(scilinREG, (unsigned char *)buffer, len);
  84.  
  85.         if(++tx_data >= 6) {
  86.             tx_data = 0;
  87.         }
  88.  
  89.         // Simple delay.
  90.         for (int i = 0; i < 10000000; i++) {
  91.             // Do nothing.
  92.         }
  93.     }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement